Compare commits
	
		
			2 Commits
		
	
	
		
			d2d821c140
			...
			a2276664ea
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					a2276664ea | ||
| 
						 | 
					68cf292c90 | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/03.1_iio_trigger引入与体验.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/03.2_iio_trigger内部机制.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/03.3_iio-trig-hrtimer分析.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/03.4_修改DHT11驱动使用iio_trigger.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										389
									
								
								IMX6ULL/doc_pic/14_IIO/03_iio_trigger的使用.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,389 @@
 | 
			
		||||
# iio_trigger的使用
 | 
			
		||||
 | 
			
		||||
IIO(Industrial I/O)参考资料:
 | 
			
		||||
 | 
			
		||||
* 系列文章:https://blog.csdn.net/lickylin/article/details/108177756
 | 
			
		||||
* https://www.cnblogs.com/yongleili717/p/10758691.html
 | 
			
		||||
* 内核文档:https://www.kernel.org/doc/html/v5.3/driver-api/iio
 | 
			
		||||
* 参考内核源码:`drivers\staging\iio\impedance-analyzer\ad5933.c`
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 1. iio_trigger的引入与体验
 | 
			
		||||
 | 
			
		||||
### 1.1 问题引入
 | 
			
		||||
 | 
			
		||||
在上一个驱动程序里,我们使用工作队列:读DHT11、写Buffer。
 | 
			
		||||
 | 
			
		||||
工作队列的本质是:在一个内核线程(worker线程)里,执行我们提供的work函数。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
我们想使用更多的读数据方式,怎么办?比如:
 | 
			
		||||
 | 
			
		||||
* 类似我们实现的使用内核线程不断读硬件、写buffer
 | 
			
		||||
* 用户手工触发一次读硬件、写buffer
 | 
			
		||||
* 使用其他中断,比如按键,按一下触发一次读硬件、写buffer
 | 
			
		||||
* 定时触发一次读硬件、写buffer
 | 
			
		||||
 | 
			
		||||
内核里已经实现了多种"iio-trigger",比如:
 | 
			
		||||
 | 
			
		||||
* iio-trig-loop:本质就是使用一个内核线程,不断读硬件、写buffer
 | 
			
		||||
* iio-trig-sysfs:用户写一下某个sysfs文件,就读一次硬件、写buffer
 | 
			
		||||
* iio-trig-interrupt:可以使用其他中断来读硬件、写buffer
 | 
			
		||||
* iio-trig-hrtimer:使用定时器,周期性地读硬件、写buffer
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.2 trigger的概念
 | 
			
		||||
 | 
			
		||||
要使用trigger,首先得有驱动程序:配置内核,把如下驱动选上
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
然后创建trigger:比如按照驱动程序iio-trig-loop.ko后,还需要执行如下命令来创建触发器:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/loop/loop0
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
然后,要设置IIO设备使用触发器,比如:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device2/
 | 
			
		||||
echo loop0 > trigger/current_trigger                        # 在设备上使用trigger
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
最后:使能iio device的buffer、读设备
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.3 上机体验
 | 
			
		||||
 | 
			
		||||
IMX6ULL的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
STM32MP157的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
#### 1.3.1 IMX6ULL
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/iio-trig-loop.ko
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/loop/loop0  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device2/
 | 
			
		||||
echo loop0 > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device2
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 1.3.2 STM32MP157
 | 
			
		||||
 | 
			
		||||
```SHELL
 | 
			
		||||
insmod /root/iio-trig-loop.ko
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/loop/loop0  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device1/
 | 
			
		||||
echo loop0 > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device1
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 2. iio_trigger内部机制
 | 
			
		||||
 | 
			
		||||
以`drivers\iio\trigger\iio-trig-loop.c`为例进行分析:
 | 
			
		||||
 | 
			
		||||
### 2.1 核心:虚拟中断控制器
 | 
			
		||||
 | 
			
		||||
iio_trigger的核心是使用"虚拟中断控制器"来实现驱动的分离:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
以iio_trig-loop.c为例:
 | 
			
		||||
 | 
			
		||||
* iio_trig-loop.c实现了一个虚拟中断控制器
 | 
			
		||||
* DHT11提供虚拟的中断处理函数
 | 
			
		||||
* 当使能DHT11的buffer时,向虚拟中断控制器注册中断
 | 
			
		||||
* iio_trig-loop.c的线程调用`iio_trigger_poll_chained`函数直接调用中断处理函数
 | 
			
		||||
* **注意**:没有真正的中断产生
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
### 2.2 注册iio_trigger驱动
 | 
			
		||||
 | 
			
		||||
执行如下命令:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/iio-trig-loop.ko
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
会生成一个目录(以后在此目录下mkdir,就会创建trigger设备,即创建虚拟中断控制器):
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
/sys/kernel/config/iio/triggers/loop
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
### 2.3 创建trigger设备:创建虚拟中断控制器
 | 
			
		||||
 | 
			
		||||
执行如下命令:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/loop/loop0  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
就会创建trigger设备,就是创建虚拟中断控制器:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 2.4 iio_device使用trigger设备
 | 
			
		||||
 | 
			
		||||
iio_device要使用trigger功能,就是去注册一个中断,这个中断是trigger设备提供的虚拟的中断。
 | 
			
		||||
 | 
			
		||||
#### 2.4.1 准备中断函数
 | 
			
		||||
 | 
			
		||||
创建buffer时提供handler、thread_fn:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
#### 2.4.2 注册中断函数
 | 
			
		||||
 | 
			
		||||
使能buffer时注册中断函数,执行如下命令时:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
会有如下调用:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
### 2.5 trigger设备调用中断处理函数
 | 
			
		||||
 | 
			
		||||
使能buffer时,会设置trigger的状态为true:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
对于iio-trig-loop.c:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
iio_trigger_poll_chained会直接调用iio_device提供的中断处理函数:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 3. iio-trig-hrtimer分析
 | 
			
		||||
 | 
			
		||||
### 3.1 注册trigger驱动
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod iio-trig-hrtimer.ko
 | 
			
		||||
ls /sys/kernel/config/iio/triggers/hrtimer/
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 3.2 创建trigger设备
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
cd /sys/kernel/config/iio/triggers/hrtimer/
 | 
			
		||||
mkdir timer_abc
 | 
			
		||||
 | 
			
		||||
# ls /sys/bus/iio/devices/trigger1/
 | 
			
		||||
name                power/              sampling_frequency  subsystem/          uevent
 | 
			
		||||
# cat /sys/bus/iio/devices/trigger1/name
 | 
			
		||||
timer_abc
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 3.3 使用
 | 
			
		||||
 | 
			
		||||
#### 3.3.1 IMX6ULL
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/iio-trig-hrtimer.ko
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/hrtimer/timer_abc  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device2/
 | 
			
		||||
echo timer_abc > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device2
 | 
			
		||||
 | 
			
		||||
# 修改频率
 | 
			
		||||
cd /sys/bus/iio/devices/trigger1
 | 
			
		||||
echo 1000000000 > sampling_frequency  # 单位ns
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 3.3.2 STM32MP157
 | 
			
		||||
 | 
			
		||||
```SHELL
 | 
			
		||||
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/hrtimer/timer_abc  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device1/
 | 
			
		||||
echo timer_abc > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device1
 | 
			
		||||
 | 
			
		||||
# 修改频率
 | 
			
		||||
cd /sys/bus/iio/devices/trigger1
 | 
			
		||||
echo 1000000000 > sampling_frequency  # 单位ns
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 4. 修改DHT11驱动使用iio_trigger
 | 
			
		||||
 | 
			
		||||
IMX6ULL的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
STM32MP157的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
### 4.1 设置triggered_buffer
 | 
			
		||||
 | 
			
		||||
核心有2点:
 | 
			
		||||
 | 
			
		||||
* 设置pollfunc结构体,里面记录有虚拟中断处理函数:handle、thread_fn
 | 
			
		||||
* 提供setup_ops,它里面有postenable函数指针,用来在使能buffer时注册虚拟的中断
 | 
			
		||||
 | 
			
		||||
IIO子系统里提供了现成的函数:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 4.2 实现中断处理函数
 | 
			
		||||
 | 
			
		||||
先实现handle函数:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
再实现thread_fn函数:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 4.3 上机实验
 | 
			
		||||
 | 
			
		||||
#### 4.3.1 IMX6ULL
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/iio-trig-hrtimer.ko
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/hrtimer/timer_abc  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device2/
 | 
			
		||||
echo timer_abc > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device2
 | 
			
		||||
 | 
			
		||||
# 修改频率
 | 
			
		||||
cd /sys/bus/iio/devices/trigger1
 | 
			
		||||
echo 1000000000 > sampling_frequency  # 单位ns
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 4.3.2 STM32MP157
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/hrtimer/timer_abc  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device1/
 | 
			
		||||
echo timer_abc > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device1
 | 
			
		||||
 | 
			
		||||
# 修改频率
 | 
			
		||||
cd /sys/bus/iio/devices/trigger1
 | 
			
		||||
echo 1000000000 > sampling_frequency  # 单位ns
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 4.4 代码调用流程回顾
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241120143037699.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 58 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241120144844917.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.2 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241120144923584.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.6 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241120152308135.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 24 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241120152309404.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 24 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241120162213861.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 17 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122102016616.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 107 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122102724799.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 108 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122102856636.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 149 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122103612419.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 168 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122104728332.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 168 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122105432793.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 38 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122105554105.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 28 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122174847707.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 71 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122175029650.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 228 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241122175306406.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 116 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241125103834678.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 6.7 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241125103916918.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 6.3 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241125105034175.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 98 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241125105104461.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 7.9 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241125105301615.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 72 KiB  | 
							
								
								
									
										639
									
								
								IMX6ULL/source/14_iio/03_dht11_iio_trigger/dht11.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,639 @@
 | 
			
		||||
/*
 | 
			
		||||
 * DHT11/DHT22 bit banging GPIO driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) Harald Geyer <harald@ccbib.org>
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software; you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <linux/err.h>
 | 
			
		||||
#include <linux/interrupt.h>
 | 
			
		||||
#include <linux/device.h>
 | 
			
		||||
#include <linux/kernel.h>
 | 
			
		||||
#include <linux/printk.h>
 | 
			
		||||
#include <linux/slab.h>
 | 
			
		||||
#include <linux/of.h>
 | 
			
		||||
#include <linux/of_device.h>
 | 
			
		||||
#include <linux/sysfs.h>
 | 
			
		||||
#include <linux/io.h>
 | 
			
		||||
#include <linux/module.h>
 | 
			
		||||
#include <linux/platform_device.h>
 | 
			
		||||
#include <linux/wait.h>
 | 
			
		||||
#include <linux/bitops.h>
 | 
			
		||||
#include <linux/completion.h>
 | 
			
		||||
#include <linux/mutex.h>
 | 
			
		||||
#include <linux/delay.h>
 | 
			
		||||
#include <linux/gpio.h>
 | 
			
		||||
#include <linux/of_gpio.h>
 | 
			
		||||
#include <linux/timekeeping.h>
 | 
			
		||||
 | 
			
		||||
#include <linux/iio/iio.h>
 | 
			
		||||
#include <linux/iio/buffer.h>
 | 
			
		||||
#include <linux/iio/kfifo_buf.h>
 | 
			
		||||
#include <linux/iio/trigger_consumer.h>
 | 
			
		||||
#include <linux/iio/triggered_buffer.h>
 | 
			
		||||
 | 
			
		||||
#define DRIVER_NAME	"dht11"
 | 
			
		||||
 | 
			
		||||
#define DHT11_DATA_VALID_TIME	2000000000  /* 2s in ns */
 | 
			
		||||
 | 
			
		||||
#define DHT11_EDGES_PREAMBLE 2
 | 
			
		||||
#define DHT11_BITS_PER_READ 40
 | 
			
		||||
/*
 | 
			
		||||
 * Note that when reading the sensor actually 84 edges are detected, but
 | 
			
		||||
 * since the last edge is not significant, we only store 83:
 | 
			
		||||
 */
 | 
			
		||||
#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
 | 
			
		||||
			      DHT11_EDGES_PREAMBLE + 1)
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Data transmission timing:
 | 
			
		||||
 * Data bits are encoded as pulse length (high time) on the data line.
 | 
			
		||||
 * 0-bit: 22-30uS -- typically 26uS (AM2302)
 | 
			
		||||
 * 1-bit: 68-75uS -- typically 70uS (AM2302)
 | 
			
		||||
 * The acutal timings also depend on the properties of the cable, with
 | 
			
		||||
 * longer cables typically making pulses shorter.
 | 
			
		||||
 *
 | 
			
		||||
 * Our decoding depends on the time resolution of the system:
 | 
			
		||||
 * timeres > 34uS ... don't know what a 1-tick pulse is
 | 
			
		||||
 * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
 | 
			
		||||
 * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
 | 
			
		||||
 * timeres < 23uS ... no problem
 | 
			
		||||
 *
 | 
			
		||||
 * Luckily clocks in the 33-44kHz range are quite uncommon, so we can
 | 
			
		||||
 * support most systems if the threshold for decoding a pulse as 1-bit
 | 
			
		||||
 * is chosen carefully. If somebody really wants to support clocks around
 | 
			
		||||
 * 40kHz, where this driver is most unreliable, there are two options.
 | 
			
		||||
 * a) select an implementation using busy loop polling on those systems
 | 
			
		||||
 * b) use the checksum to do some probabilistic decoding
 | 
			
		||||
 */
 | 
			
		||||
#define DHT11_START_TRANSMISSION_MIN	18000  /* us */
 | 
			
		||||
#define DHT11_START_TRANSMISSION_MAX	20000  /* us */
 | 
			
		||||
#define DHT11_MIN_TIMERES	34000  /* ns */
 | 
			
		||||
#define DHT11_THRESHOLD		49000  /* ns */
 | 
			
		||||
#define DHT11_AMBIG_LOW		23000  /* ns */
 | 
			
		||||
#define DHT11_AMBIG_HIGH	30000  /* ns */
 | 
			
		||||
 | 
			
		||||
struct dht11 {
 | 
			
		||||
	struct device			*dev;
 | 
			
		||||
 | 
			
		||||
	int				gpio;
 | 
			
		||||
	int				irq;
 | 
			
		||||
 | 
			
		||||
	struct completion		completion;
 | 
			
		||||
	/* The iio sysfs interface doesn't prevent concurrent reads: */
 | 
			
		||||
	struct mutex			lock;
 | 
			
		||||
 | 
			
		||||
	s64				timestamp;
 | 
			
		||||
	int				temperature;
 | 
			
		||||
	int				humidity;
 | 
			
		||||
 | 
			
		||||
	/* num_edges: -1 means "no transmission in progress" */
 | 
			
		||||
	int				num_edges;
 | 
			
		||||
	struct {s64 ts; int value; }	edges[DHT11_EDGES_PER_READ];
 | 
			
		||||
	struct delayed_work		work;
 | 
			
		||||
	struct iio_dev *iio;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
/*
 | 
			
		||||
 * dht11_edges_print: show the data as actually received by the
 | 
			
		||||
 *                    driver.
 | 
			
		||||
 */
 | 
			
		||||
static void dht11_edges_print(struct dht11 *dht11)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
 | 
			
		||||
	for (i = 1; i < dht11->num_edges; ++i) {
 | 
			
		||||
		dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
 | 
			
		||||
			dht11->edges[i].ts - dht11->edges[i - 1].ts,
 | 
			
		||||
			dht11->edges[i - 1].value ? "high" : "low");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
#endif /* CONFIG_DYNAMIC_DEBUG */
 | 
			
		||||
 | 
			
		||||
static unsigned char dht11_decode_byte(char *bits)
 | 
			
		||||
{
 | 
			
		||||
	unsigned char ret = 0;
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < 8; ++i) {
 | 
			
		||||
		ret <<= 1;
 | 
			
		||||
		if (bits[i])
 | 
			
		||||
			++ret;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_decode(struct dht11 *dht11, int offset)
 | 
			
		||||
{
 | 
			
		||||
	int i, t;
 | 
			
		||||
	char bits[DHT11_BITS_PER_READ];
 | 
			
		||||
	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
 | 
			
		||||
		t = dht11->edges[offset + 2 * i + 2].ts -
 | 
			
		||||
			dht11->edges[offset + 2 * i + 1].ts;
 | 
			
		||||
		if (!dht11->edges[offset + 2 * i + 1].value) {
 | 
			
		||||
			dev_dbg(dht11->dev,
 | 
			
		||||
				"lost synchronisation at edge %d\n",
 | 
			
		||||
				offset + 2 * i + 1);
 | 
			
		||||
			return -EIO;
 | 
			
		||||
		}
 | 
			
		||||
		bits[i] = t > DHT11_THRESHOLD;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	hum_int = dht11_decode_byte(bits);
 | 
			
		||||
	hum_dec = dht11_decode_byte(&bits[8]);
 | 
			
		||||
	temp_int = dht11_decode_byte(&bits[16]);
 | 
			
		||||
	temp_dec = dht11_decode_byte(&bits[24]);
 | 
			
		||||
	checksum = dht11_decode_byte(&bits[32]);
 | 
			
		||||
 | 
			
		||||
	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
 | 
			
		||||
		dev_dbg(dht11->dev, "invalid checksum\n");
 | 
			
		||||
		return -EIO;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11->timestamp = ktime_get_boot_ns();
 | 
			
		||||
	if (hum_int < 20) {  /* DHT22 */
 | 
			
		||||
		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 | 
			
		||||
					((temp_int & 0x80) ? -100 : 100);
 | 
			
		||||
		dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
 | 
			
		||||
	} else  {  /* DHT11 */
 | 
			
		||||
		dht11->temperature = temp_int;;
 | 
			
		||||
		dht11->humidity = hum_int;;
 | 
			
		||||
	} 
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * IRQ handler called on GPIO edges
 | 
			
		||||
 */
 | 
			
		||||
static irqreturn_t dht11_handle_irq(int irq, void *data)
 | 
			
		||||
{
 | 
			
		||||
	struct iio_dev *iio = data;
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio);
 | 
			
		||||
 | 
			
		||||
	/* TODO: Consider making the handler safe for IRQ sharing */
 | 
			
		||||
	if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
 | 
			
		||||
		dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns();
 | 
			
		||||
		dht11->edges[dht11->num_edges++].value =
 | 
			
		||||
						gpio_get_value(dht11->gpio);
 | 
			
		||||
 | 
			
		||||
		if (dht11->num_edges >= DHT11_EDGES_PER_READ)
 | 
			
		||||
			complete(&dht11->completion);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return IRQ_HANDLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_read_raw(struct iio_dev *iio_dev,
 | 
			
		||||
			  const struct iio_chan_spec *chan,
 | 
			
		||||
			int *val, int *val2, long m)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio_dev);
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpio_direction_output(dht11->gpio, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
			     DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpio_direction_input(dht11->gpio);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ret = IIO_VAL_INT;
 | 
			
		||||
	if (chan->type == IIO_TEMP)
 | 
			
		||||
		*val = dht11->temperature;
 | 
			
		||||
	else if (chan->type == IIO_HUMIDITYRELATIVE)
 | 
			
		||||
		*val = dht11->humidity;
 | 
			
		||||
	else
 | 
			
		||||
		ret = -EINVAL;
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const struct iio_info dht11_iio_info = {
 | 
			
		||||
	.driver_module		= THIS_MODULE,
 | 
			
		||||
	.read_raw		= dht11_read_raw,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct iio_chan_spec dht11_chan_spec[] = {
 | 
			
		||||
	{ .type = IIO_TEMP,
 | 
			
		||||
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 
 | 
			
		||||
		.scan_index = 0,
 | 
			
		||||
		.scan_type = {
 | 
			
		||||
			.sign = 's',
 | 
			
		||||
			.realbits = 8,
 | 
			
		||||
			.storagebits = 8,
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
	{ .type = IIO_HUMIDITYRELATIVE,
 | 
			
		||||
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 
 | 
			
		||||
		.scan_index = 1,
 | 
			
		||||
		.scan_type = {
 | 
			
		||||
			.sign = 's',
 | 
			
		||||
			.realbits = 8,
 | 
			
		||||
			.storagebits = 8,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct of_device_id dht11_dt_ids[] = {
 | 
			
		||||
	{ .compatible = "dht11", },
 | 
			
		||||
	{ }
 | 
			
		||||
};
 | 
			
		||||
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_preenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_postenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
	
 | 
			
		||||
	/* start work */
 | 
			
		||||
	schedule_delayed_work(&dht11->work, 1);
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_postdisable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
 | 
			
		||||
	/* cancel work */
 | 
			
		||||
	cancel_delayed_work_sync(&dht11->work);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static const struct iio_buffer_setup_ops dht11_buf_setup_ops = {
 | 
			
		||||
	.preenable = dht11_buf_preenable,
 | 
			
		||||
	.postenable = dht11_buf_postenable,
 | 
			
		||||
	.postdisable = dht11_buf_postdisable,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
irqreturn_t dht11_irq_handler(int irq, void *p)
 | 
			
		||||
{
 | 
			
		||||
	return IRQ_WAKE_THREAD;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Transfer data from hardware to KFIFO.
 | 
			
		||||
 */
 | 
			
		||||
irqreturn_t dht11_read_datas(int irq, void *p)
 | 
			
		||||
{
 | 
			
		||||
	struct iio_poll_func *pf = p;
 | 
			
		||||
	struct iio_dev *iio_dev = pf->indio_dev;
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio_dev);
 | 
			
		||||
	
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
	
 | 
			
		||||
	/* read dht11 */
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpio_direction_output(dht11->gpio, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
				 DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpio_direction_input(dht11->gpio);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vals[0] = dht11->temperature;
 | 
			
		||||
	vals[1] = dht11->humidity;
 | 
			
		||||
	
 | 
			
		||||
	/* put data into iio_buffer */
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
	
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
 | 
			
		||||
	iio_trigger_notify_done(iio_dev->trig);
 | 
			
		||||
 | 
			
		||||
	return IRQ_HANDLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buffer_init(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
#if 0	
 | 
			
		||||
	struct iio_buffer *buffer;
 | 
			
		||||
 | 
			
		||||
	buffer = iio_kfifo_allocate();
 | 
			
		||||
	if (!buffer)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	iio_device_attach_buffer(indio_dev, buffer);
 | 
			
		||||
 | 
			
		||||
	/* Ring buffer functions - here trigger setup related */
 | 
			
		||||
	indio_dev->setup_ops = &dht11_buf_setup_ops;
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
#else
 | 
			
		||||
	return iio_triggered_buffer_setup(indio_dev,
 | 
			
		||||
						dht11_irq_handler,
 | 
			
		||||
						dht11_read_datas,
 | 
			
		||||
						NULL);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dht11_work(struct work_struct *work)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = container_of(work, struct dht11, work.work);
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	struct iio_dev *iio_dev = dht11->iio;
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
	
 | 
			
		||||
	/* read dht11 */
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpio_direction_output(dht11->gpio, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
				 DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpio_direction_input(dht11->gpio);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vals[0] = dht11->temperature;
 | 
			
		||||
	vals[1] = dht11->humidity;
 | 
			
		||||
	
 | 
			
		||||
	/* put data into iio_buffer */
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
	
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	
 | 
			
		||||
	/* re-start work */
 | 
			
		||||
	if (ret)
 | 
			
		||||
	{
 | 
			
		||||
		/* 有错误 */
 | 
			
		||||
		schedule_delayed_work(&dht11->work, 0);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		schedule_delayed_work(&dht11->work, HZ);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const unsigned long dht11_scan_masks[] = {0x3, 0};
 | 
			
		||||
 | 
			
		||||
static int dht11_probe(struct platform_device *pdev)
 | 
			
		||||
{
 | 
			
		||||
	struct device *dev = &pdev->dev;
 | 
			
		||||
	struct device_node *node = dev->of_node;
 | 
			
		||||
	struct dht11 *dht11;
 | 
			
		||||
	struct iio_dev *iio;
 | 
			
		||||
	int ret;
 | 
			
		||||
 | 
			
		||||
	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
 | 
			
		||||
	if (!iio) {
 | 
			
		||||
		dev_err(dev, "Failed to allocate IIO device\n");
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11 = iio_priv(iio);
 | 
			
		||||
	dht11->dev = dev;
 | 
			
		||||
	dht11->iio = iio;
 | 
			
		||||
 | 
			
		||||
	ret = of_get_gpio(node, 0);
 | 
			
		||||
	if (ret < 0)
 | 
			
		||||
		return ret;
 | 
			
		||||
	dht11->gpio = ret;
 | 
			
		||||
	ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
 | 
			
		||||
	if (ret)
 | 
			
		||||
		return ret;
 | 
			
		||||
 | 
			
		||||
	dht11->irq = gpio_to_irq(dht11->gpio);
 | 
			
		||||
	if (dht11->irq < 0) {
 | 
			
		||||
		dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
 | 
			
		||||
	platform_set_drvdata(pdev, iio);
 | 
			
		||||
 | 
			
		||||
	init_completion(&dht11->completion);
 | 
			
		||||
	mutex_init(&dht11->lock);
 | 
			
		||||
	iio->name = pdev->name;
 | 
			
		||||
	iio->dev.parent = &pdev->dev;
 | 
			
		||||
	iio->info = &dht11_iio_info;
 | 
			
		||||
	iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED;
 | 
			
		||||
	iio->channels = dht11_chan_spec;
 | 
			
		||||
	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
 | 
			
		||||
	iio->available_scan_masks = dht11_scan_masks;
 | 
			
		||||
 | 
			
		||||
	dht11_buffer_init(iio);
 | 
			
		||||
 | 
			
		||||
	/* init work */
 | 
			
		||||
	INIT_DELAYED_WORK(&dht11->work, dht11_work);
 | 
			
		||||
 | 
			
		||||
	return devm_iio_device_register(dev, iio);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct platform_driver dht11_driver = {
 | 
			
		||||
	.driver = {
 | 
			
		||||
		.name	= DRIVER_NAME,
 | 
			
		||||
		.of_match_table = dht11_dt_ids,
 | 
			
		||||
	},
 | 
			
		||||
	.probe  = dht11_probe,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module_platform_driver(dht11_driver);
 | 
			
		||||
 | 
			
		||||
MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
 | 
			
		||||
MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
 | 
			
		||||
MODULE_LICENSE("GPL v2");
 | 
			
		||||
							
								
								
									
										4
									
								
								IMX6ULL/source/14_iio/03_dht11_iio_trigger/dht11.dts
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,4 @@
 | 
			
		||||
humidity_sensor {
 | 
			
		||||
	compatible = "dht11";
 | 
			
		||||
    gpios = <&gpio4 19 GPIO_ACTIVE_HIGH>;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										637
									
								
								IMX6ULL/source/14_iio/04_dht11_iio_trigger_课堂现场编写/dht11.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,637 @@
 | 
			
		||||
/*
 | 
			
		||||
 * DHT11/DHT22 bit banging GPIO driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) Harald Geyer <harald@ccbib.org>
 | 
			
		||||
 *
 | 
			
		||||
 * This program is free software; you can redistribute it and/or modify
 | 
			
		||||
 * it under the terms of the GNU General Public License as published by
 | 
			
		||||
 * the Free Software Foundation; either version 2 of the License, or
 | 
			
		||||
 * (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This program is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 | 
			
		||||
 * GNU General Public License for more details.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <linux/err.h>
 | 
			
		||||
#include <linux/interrupt.h>
 | 
			
		||||
#include <linux/device.h>
 | 
			
		||||
#include <linux/kernel.h>
 | 
			
		||||
#include <linux/printk.h>
 | 
			
		||||
#include <linux/slab.h>
 | 
			
		||||
#include <linux/of.h>
 | 
			
		||||
#include <linux/of_device.h>
 | 
			
		||||
#include <linux/sysfs.h>
 | 
			
		||||
#include <linux/io.h>
 | 
			
		||||
#include <linux/module.h>
 | 
			
		||||
#include <linux/platform_device.h>
 | 
			
		||||
#include <linux/wait.h>
 | 
			
		||||
#include <linux/bitops.h>
 | 
			
		||||
#include <linux/completion.h>
 | 
			
		||||
#include <linux/mutex.h>
 | 
			
		||||
#include <linux/delay.h>
 | 
			
		||||
#include <linux/gpio.h>
 | 
			
		||||
#include <linux/of_gpio.h>
 | 
			
		||||
#include <linux/timekeeping.h>
 | 
			
		||||
 | 
			
		||||
#include <linux/iio/iio.h>
 | 
			
		||||
#include <linux/iio/buffer.h>
 | 
			
		||||
#include <linux/iio/kfifo_buf.h>
 | 
			
		||||
#include <linux/iio/trigger_consumer.h>
 | 
			
		||||
#include <linux/iio/triggered_buffer.h>
 | 
			
		||||
 | 
			
		||||
#define DRIVER_NAME	"dht11"
 | 
			
		||||
 | 
			
		||||
#define DHT11_DATA_VALID_TIME	2000000000  /* 2s in ns */
 | 
			
		||||
 | 
			
		||||
#define DHT11_EDGES_PREAMBLE 2
 | 
			
		||||
#define DHT11_BITS_PER_READ 40
 | 
			
		||||
/*
 | 
			
		||||
 * Note that when reading the sensor actually 84 edges are detected, but
 | 
			
		||||
 * since the last edge is not significant, we only store 83:
 | 
			
		||||
 */
 | 
			
		||||
#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
 | 
			
		||||
			      DHT11_EDGES_PREAMBLE + 1)
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Data transmission timing:
 | 
			
		||||
 * Data bits are encoded as pulse length (high time) on the data line.
 | 
			
		||||
 * 0-bit: 22-30uS -- typically 26uS (AM2302)
 | 
			
		||||
 * 1-bit: 68-75uS -- typically 70uS (AM2302)
 | 
			
		||||
 * The acutal timings also depend on the properties of the cable, with
 | 
			
		||||
 * longer cables typically making pulses shorter.
 | 
			
		||||
 *
 | 
			
		||||
 * Our decoding depends on the time resolution of the system:
 | 
			
		||||
 * timeres > 34uS ... don't know what a 1-tick pulse is
 | 
			
		||||
 * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
 | 
			
		||||
 * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
 | 
			
		||||
 * timeres < 23uS ... no problem
 | 
			
		||||
 *
 | 
			
		||||
 * Luckily clocks in the 33-44kHz range are quite uncommon, so we can
 | 
			
		||||
 * support most systems if the threshold for decoding a pulse as 1-bit
 | 
			
		||||
 * is chosen carefully. If somebody really wants to support clocks around
 | 
			
		||||
 * 40kHz, where this driver is most unreliable, there are two options.
 | 
			
		||||
 * a) select an implementation using busy loop polling on those systems
 | 
			
		||||
 * b) use the checksum to do some probabilistic decoding
 | 
			
		||||
 */
 | 
			
		||||
#define DHT11_START_TRANSMISSION_MIN	18000  /* us */
 | 
			
		||||
#define DHT11_START_TRANSMISSION_MAX	20000  /* us */
 | 
			
		||||
#define DHT11_MIN_TIMERES	34000  /* ns */
 | 
			
		||||
#define DHT11_THRESHOLD		49000  /* ns */
 | 
			
		||||
#define DHT11_AMBIG_LOW		23000  /* ns */
 | 
			
		||||
#define DHT11_AMBIG_HIGH	30000  /* ns */
 | 
			
		||||
 | 
			
		||||
struct dht11 {
 | 
			
		||||
	struct device			*dev;
 | 
			
		||||
 | 
			
		||||
	int				gpio;
 | 
			
		||||
	int				irq;
 | 
			
		||||
 | 
			
		||||
	struct completion		completion;
 | 
			
		||||
	/* The iio sysfs interface doesn't prevent concurrent reads: */
 | 
			
		||||
	struct mutex			lock;
 | 
			
		||||
 | 
			
		||||
	s64				timestamp;
 | 
			
		||||
	int				temperature;
 | 
			
		||||
	int				humidity;
 | 
			
		||||
 | 
			
		||||
	/* num_edges: -1 means "no transmission in progress" */
 | 
			
		||||
	int				num_edges;
 | 
			
		||||
	struct {s64 ts; int value; }	edges[DHT11_EDGES_PER_READ];
 | 
			
		||||
	struct delayed_work		work;
 | 
			
		||||
	struct iio_dev *iio;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
/*
 | 
			
		||||
 * dht11_edges_print: show the data as actually received by the
 | 
			
		||||
 *                    driver.
 | 
			
		||||
 */
 | 
			
		||||
static void dht11_edges_print(struct dht11 *dht11)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
 | 
			
		||||
	for (i = 1; i < dht11->num_edges; ++i) {
 | 
			
		||||
		dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
 | 
			
		||||
			dht11->edges[i].ts - dht11->edges[i - 1].ts,
 | 
			
		||||
			dht11->edges[i - 1].value ? "high" : "low");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
#endif /* CONFIG_DYNAMIC_DEBUG */
 | 
			
		||||
 | 
			
		||||
static unsigned char dht11_decode_byte(char *bits)
 | 
			
		||||
{
 | 
			
		||||
	unsigned char ret = 0;
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < 8; ++i) {
 | 
			
		||||
		ret <<= 1;
 | 
			
		||||
		if (bits[i])
 | 
			
		||||
			++ret;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_decode(struct dht11 *dht11, int offset)
 | 
			
		||||
{
 | 
			
		||||
	int i, t;
 | 
			
		||||
	char bits[DHT11_BITS_PER_READ];
 | 
			
		||||
	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
 | 
			
		||||
		t = dht11->edges[offset + 2 * i + 2].ts -
 | 
			
		||||
			dht11->edges[offset + 2 * i + 1].ts;
 | 
			
		||||
		if (!dht11->edges[offset + 2 * i + 1].value) {
 | 
			
		||||
			dev_dbg(dht11->dev,
 | 
			
		||||
				"lost synchronisation at edge %d\n",
 | 
			
		||||
				offset + 2 * i + 1);
 | 
			
		||||
			return -EIO;
 | 
			
		||||
		}
 | 
			
		||||
		bits[i] = t > DHT11_THRESHOLD;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	hum_int = dht11_decode_byte(bits);
 | 
			
		||||
	hum_dec = dht11_decode_byte(&bits[8]);
 | 
			
		||||
	temp_int = dht11_decode_byte(&bits[16]);
 | 
			
		||||
	temp_dec = dht11_decode_byte(&bits[24]);
 | 
			
		||||
	checksum = dht11_decode_byte(&bits[32]);
 | 
			
		||||
 | 
			
		||||
	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
 | 
			
		||||
		dev_dbg(dht11->dev, "invalid checksum\n");
 | 
			
		||||
		return -EIO;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11->timestamp = ktime_get_boot_ns();
 | 
			
		||||
	if (hum_int < 20) {  /* DHT22 */
 | 
			
		||||
		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 | 
			
		||||
					((temp_int & 0x80) ? -100 : 100);
 | 
			
		||||
		dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
 | 
			
		||||
	} else  {  /* DHT11 */
 | 
			
		||||
		dht11->temperature = temp_int;;
 | 
			
		||||
		dht11->humidity = hum_int;;
 | 
			
		||||
	} 
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * IRQ handler called on GPIO edges
 | 
			
		||||
 */
 | 
			
		||||
static irqreturn_t dht11_handle_irq(int irq, void *data)
 | 
			
		||||
{
 | 
			
		||||
	struct iio_dev *iio = data;
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio);
 | 
			
		||||
 | 
			
		||||
	/* TODO: Consider making the handler safe for IRQ sharing */
 | 
			
		||||
	if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
 | 
			
		||||
		dht11->edges[dht11->num_edges].ts = ktime_get_boot_ns();
 | 
			
		||||
		dht11->edges[dht11->num_edges++].value =
 | 
			
		||||
						gpio_get_value(dht11->gpio);
 | 
			
		||||
 | 
			
		||||
		if (dht11->num_edges >= DHT11_EDGES_PER_READ)
 | 
			
		||||
			complete(&dht11->completion);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return IRQ_HANDLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_read_raw(struct iio_dev *iio_dev,
 | 
			
		||||
			  const struct iio_chan_spec *chan,
 | 
			
		||||
			int *val, int *val2, long m)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio_dev);
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpio_direction_output(dht11->gpio, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
			     DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpio_direction_input(dht11->gpio);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ret = IIO_VAL_INT;
 | 
			
		||||
	if (chan->type == IIO_TEMP)
 | 
			
		||||
		*val = dht11->temperature;
 | 
			
		||||
	else if (chan->type == IIO_HUMIDITYRELATIVE)
 | 
			
		||||
		*val = dht11->humidity;
 | 
			
		||||
	else
 | 
			
		||||
		ret = -EINVAL;
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const struct iio_info dht11_iio_info = {
 | 
			
		||||
	.driver_module		= THIS_MODULE,
 | 
			
		||||
	.read_raw		= dht11_read_raw,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct iio_chan_spec dht11_chan_spec[] = {
 | 
			
		||||
	{ .type = IIO_TEMP,
 | 
			
		||||
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 
 | 
			
		||||
		.scan_index = 0,
 | 
			
		||||
		.scan_type = {
 | 
			
		||||
			.sign = 's',
 | 
			
		||||
			.realbits = 8,
 | 
			
		||||
			.storagebits = 8,
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
	{ .type = IIO_HUMIDITYRELATIVE,
 | 
			
		||||
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 
 | 
			
		||||
		.scan_index = 1,
 | 
			
		||||
		.scan_type = {
 | 
			
		||||
			.sign = 's',
 | 
			
		||||
			.realbits = 8,
 | 
			
		||||
			.storagebits = 8,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct of_device_id dht11_dt_ids[] = {
 | 
			
		||||
	{ .compatible = "dht11", },
 | 
			
		||||
	{ }
 | 
			
		||||
};
 | 
			
		||||
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_preenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_postenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
	
 | 
			
		||||
	/* start work */
 | 
			
		||||
	schedule_delayed_work(&dht11->work, 1);
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_postdisable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
 | 
			
		||||
	/* cancel work */
 | 
			
		||||
	cancel_delayed_work_sync(&dht11->work);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static const struct iio_buffer_setup_ops dht11_buf_setup_ops = {
 | 
			
		||||
	.preenable = dht11_buf_preenable,
 | 
			
		||||
	.postenable = dht11_buf_postenable,
 | 
			
		||||
	.postdisable = dht11_buf_postdisable,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static irqreturn_t dht11_triggered_buf_handle(int irq, void * p)
 | 
			
		||||
{
 | 
			
		||||
	struct iio_poll_func *pf = p;
 | 
			
		||||
	pf->timestamp = iio_get_time_ns(pf->indio_dev);
 | 
			
		||||
	return IRQ_WAKE_THREAD;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static irqreturn_t dht11_triggered_buf_thread(int irq, void * p)
 | 
			
		||||
{
 | 
			
		||||
	/* read data */
 | 
			
		||||
	struct iio_poll_func *pf = p;
 | 
			
		||||
	struct iio_dev *iio_dev = pf->indio_dev;	
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio_dev);
 | 
			
		||||
	
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
	
 | 
			
		||||
	/* read dht11 */
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpio_direction_output(dht11->gpio, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
				 DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpio_direction_input(dht11->gpio);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vals[0] = dht11->temperature;
 | 
			
		||||
	vals[1] = dht11->humidity;
 | 
			
		||||
	
 | 
			
		||||
	/* put data into iio_buffer */
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
	
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
 | 
			
		||||
	iio_trigger_notify_done(iio_dev->trig);
 | 
			
		||||
 | 
			
		||||
	return IRQ_HANDLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static int dht11_buffer_init(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
#if 0	
 | 
			
		||||
	struct iio_buffer *buffer;
 | 
			
		||||
 | 
			
		||||
	buffer = iio_kfifo_allocate();
 | 
			
		||||
	if (!buffer)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	iio_device_attach_buffer(indio_dev, buffer);
 | 
			
		||||
 | 
			
		||||
	/* Ring buffer functions - here trigger setup related */
 | 
			
		||||
	indio_dev->setup_ops = &dht11_buf_setup_ops;
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
#else
 | 
			
		||||
	return iio_triggered_buffer_setup(indio_dev, dht11_triggered_buf_handle, dht11_triggered_buf_thread, NULL);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dht11_work(struct work_struct *work)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = container_of(work, struct dht11, work.work);
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	struct iio_dev *iio_dev = dht11->iio;
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
	
 | 
			
		||||
	/* read dht11 */
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpio_direction_output(dht11->gpio, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
				 DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpio_direction_input(dht11->gpio);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vals[0] = dht11->temperature;
 | 
			
		||||
	vals[1] = dht11->humidity;
 | 
			
		||||
	
 | 
			
		||||
	/* put data into iio_buffer */
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
	
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	
 | 
			
		||||
	/* re-start work */
 | 
			
		||||
	if (ret)
 | 
			
		||||
	{
 | 
			
		||||
		/* 有错误 */
 | 
			
		||||
		schedule_delayed_work(&dht11->work, 0);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		schedule_delayed_work(&dht11->work, HZ);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const unsigned long dht11_scan_masks[] = {0x3, 0};
 | 
			
		||||
 | 
			
		||||
static int dht11_probe(struct platform_device *pdev)
 | 
			
		||||
{
 | 
			
		||||
	struct device *dev = &pdev->dev;
 | 
			
		||||
	struct device_node *node = dev->of_node;
 | 
			
		||||
	struct dht11 *dht11;
 | 
			
		||||
	struct iio_dev *iio;
 | 
			
		||||
	int ret;
 | 
			
		||||
 | 
			
		||||
	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
 | 
			
		||||
	if (!iio) {
 | 
			
		||||
		dev_err(dev, "Failed to allocate IIO device\n");
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11 = iio_priv(iio);
 | 
			
		||||
	dht11->dev = dev;
 | 
			
		||||
	dht11->iio = iio;
 | 
			
		||||
 | 
			
		||||
	ret = of_get_gpio(node, 0);
 | 
			
		||||
	if (ret < 0)
 | 
			
		||||
		return ret;
 | 
			
		||||
	dht11->gpio = ret;
 | 
			
		||||
	ret = devm_gpio_request_one(dev, dht11->gpio, GPIOF_IN, pdev->name);
 | 
			
		||||
	if (ret)
 | 
			
		||||
		return ret;
 | 
			
		||||
 | 
			
		||||
	dht11->irq = gpio_to_irq(dht11->gpio);
 | 
			
		||||
	if (dht11->irq < 0) {
 | 
			
		||||
		dev_err(dev, "GPIO %d has no interrupt\n", dht11->gpio);
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
 | 
			
		||||
	platform_set_drvdata(pdev, iio);
 | 
			
		||||
 | 
			
		||||
	init_completion(&dht11->completion);
 | 
			
		||||
	mutex_init(&dht11->lock);
 | 
			
		||||
	iio->name = pdev->name;
 | 
			
		||||
	iio->dev.parent = &pdev->dev;
 | 
			
		||||
	iio->info = &dht11_iio_info;
 | 
			
		||||
	iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
 | 
			
		||||
	iio->channels = dht11_chan_spec;
 | 
			
		||||
	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
 | 
			
		||||
	iio->available_scan_masks = dht11_scan_masks;
 | 
			
		||||
 | 
			
		||||
	dht11_buffer_init(iio);
 | 
			
		||||
 | 
			
		||||
	/* init work */
 | 
			
		||||
	INIT_DELAYED_WORK(&dht11->work, dht11_work);
 | 
			
		||||
 | 
			
		||||
	return devm_iio_device_register(dev, iio);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct platform_driver dht11_driver = {
 | 
			
		||||
	.driver = {
 | 
			
		||||
		.name	= DRIVER_NAME,
 | 
			
		||||
		.of_match_table = dht11_dt_ids,
 | 
			
		||||
	},
 | 
			
		||||
	.probe  = dht11_probe,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module_platform_driver(dht11_driver);
 | 
			
		||||
 | 
			
		||||
MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
 | 
			
		||||
MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
 | 
			
		||||
MODULE_LICENSE("GPL v2");
 | 
			
		||||
@@ -0,0 +1,4 @@
 | 
			
		||||
humidity_sensor {
 | 
			
		||||
	compatible = "dht11";
 | 
			
		||||
    gpios = <&gpio4 19 GPIO_ACTIVE_HIGH>;
 | 
			
		||||
};
 | 
			
		||||
@@ -914,6 +914,15 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
 | 
			
		||||
  02.3_实现iio_buffer的写入
 | 
			
		||||
  ```
 | 
			
		||||
 | 
			
		||||
* 2024.11.25  发布IIO"子系统"
 | 
			
		||||
 | 
			
		||||
  ```shell
 | 
			
		||||
  03.1_iio_trigger的引入与体验
 | 
			
		||||
  03.2_iio_trigger内部机制
 | 
			
		||||
  03.3_iio-trig-hrtimer分析
 | 
			
		||||
  03.4_修改DHT11驱动使用iio_trigger
 | 
			
		||||
  ```
 | 
			
		||||
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
## 6. 联系方式
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/03.1_iio_trigger引入与体验.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/03.2_iio_trigger内部机制.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/03.3_iio-trig-hrtimer分析.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/03.4_修改DHT11驱动使用iio_trigger.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										389
									
								
								STM32MP157/doc_pic/14_IIO/03_iio_trigger的使用.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,389 @@
 | 
			
		||||
# iio_trigger的使用
 | 
			
		||||
 | 
			
		||||
IIO(Industrial I/O)参考资料:
 | 
			
		||||
 | 
			
		||||
* 系列文章:https://blog.csdn.net/lickylin/article/details/108177756
 | 
			
		||||
* https://www.cnblogs.com/yongleili717/p/10758691.html
 | 
			
		||||
* 内核文档:https://www.kernel.org/doc/html/v5.3/driver-api/iio
 | 
			
		||||
* 参考内核源码:`drivers\staging\iio\impedance-analyzer\ad5933.c`
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 1. iio_trigger的引入与体验
 | 
			
		||||
 | 
			
		||||
### 1.1 问题引入
 | 
			
		||||
 | 
			
		||||
在上一个驱动程序里,我们使用工作队列:读DHT11、写Buffer。
 | 
			
		||||
 | 
			
		||||
工作队列的本质是:在一个内核线程(worker线程)里,执行我们提供的work函数。
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
我们想使用更多的读数据方式,怎么办?比如:
 | 
			
		||||
 | 
			
		||||
* 类似我们实现的使用内核线程不断读硬件、写buffer
 | 
			
		||||
* 用户手工触发一次读硬件、写buffer
 | 
			
		||||
* 使用其他中断,比如按键,按一下触发一次读硬件、写buffer
 | 
			
		||||
* 定时触发一次读硬件、写buffer
 | 
			
		||||
 | 
			
		||||
内核里已经实现了多种"iio-trigger",比如:
 | 
			
		||||
 | 
			
		||||
* iio-trig-loop:本质就是使用一个内核线程,不断读硬件、写buffer
 | 
			
		||||
* iio-trig-sysfs:用户写一下某个sysfs文件,就读一次硬件、写buffer
 | 
			
		||||
* iio-trig-interrupt:可以使用其他中断来读硬件、写buffer
 | 
			
		||||
* iio-trig-hrtimer:使用定时器,周期性地读硬件、写buffer
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.2 trigger的概念
 | 
			
		||||
 | 
			
		||||
要使用trigger,首先得有驱动程序:配置内核,把如下驱动选上
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
然后创建trigger:比如按照驱动程序iio-trig-loop.ko后,还需要执行如下命令来创建触发器:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/loop/loop0
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
然后,要设置IIO设备使用触发器,比如:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device2/
 | 
			
		||||
echo loop0 > trigger/current_trigger                        # 在设备上使用trigger
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
最后:使能iio device的buffer、读设备
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.3 上机体验
 | 
			
		||||
 | 
			
		||||
IMX6ULL的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
STM32MP157的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
#### 1.3.1 IMX6ULL
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/iio-trig-loop.ko
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/loop/loop0  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device2/
 | 
			
		||||
echo loop0 > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device2
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 1.3.2 STM32MP157
 | 
			
		||||
 | 
			
		||||
```SHELL
 | 
			
		||||
insmod /root/iio-trig-loop.ko
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/loop/loop0  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device1/
 | 
			
		||||
echo loop0 > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device1
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 2. iio_trigger内部机制
 | 
			
		||||
 | 
			
		||||
以`drivers\iio\trigger\iio-trig-loop.c`为例进行分析:
 | 
			
		||||
 | 
			
		||||
### 2.1 核心:虚拟中断控制器
 | 
			
		||||
 | 
			
		||||
iio_trigger的核心是使用"虚拟中断控制器"来实现驱动的分离:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
以iio_trig-loop.c为例:
 | 
			
		||||
 | 
			
		||||
* iio_trig-loop.c实现了一个虚拟中断控制器
 | 
			
		||||
* DHT11提供虚拟的中断处理函数
 | 
			
		||||
* 当使能DHT11的buffer时,向虚拟中断控制器注册中断
 | 
			
		||||
* iio_trig-loop.c的线程调用`iio_trigger_poll_chained`函数直接调用中断处理函数
 | 
			
		||||
* **注意**:没有真正的中断产生
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
### 2.2 注册iio_trigger驱动
 | 
			
		||||
 | 
			
		||||
执行如下命令:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/iio-trig-loop.ko
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
会生成一个目录(以后在此目录下mkdir,就会创建trigger设备,即创建虚拟中断控制器):
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
/sys/kernel/config/iio/triggers/loop
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
### 2.3 创建trigger设备:创建虚拟中断控制器
 | 
			
		||||
 | 
			
		||||
执行如下命令:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/loop/loop0  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
就会创建trigger设备,就是创建虚拟中断控制器:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 2.4 iio_device使用trigger设备
 | 
			
		||||
 | 
			
		||||
iio_device要使用trigger功能,就是去注册一个中断,这个中断是trigger设备提供的虚拟的中断。
 | 
			
		||||
 | 
			
		||||
#### 2.4.1 准备中断函数
 | 
			
		||||
 | 
			
		||||
创建buffer时提供handler、thread_fn:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
#### 2.4.2 注册中断函数
 | 
			
		||||
 | 
			
		||||
使能buffer时注册中断函数,执行如下命令时:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
会有如下调用:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
### 2.5 trigger设备调用中断处理函数
 | 
			
		||||
 | 
			
		||||
使能buffer时,会设置trigger的状态为true:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
对于iio-trig-loop.c:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
iio_trigger_poll_chained会直接调用iio_device提供的中断处理函数:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 3. iio-trig-hrtimer分析
 | 
			
		||||
 | 
			
		||||
### 3.1 注册trigger驱动
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod iio-trig-hrtimer.ko
 | 
			
		||||
ls /sys/kernel/config/iio/triggers/hrtimer/
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 3.2 创建trigger设备
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
cd /sys/kernel/config/iio/triggers/hrtimer/
 | 
			
		||||
mkdir timer_abc
 | 
			
		||||
 | 
			
		||||
# ls /sys/bus/iio/devices/trigger1/
 | 
			
		||||
name                power/              sampling_frequency  subsystem/          uevent
 | 
			
		||||
# cat /sys/bus/iio/devices/trigger1/name
 | 
			
		||||
timer_abc
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 3.3 使用
 | 
			
		||||
 | 
			
		||||
#### 3.3.1 IMX6ULL
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/iio-trig-hrtimer.ko
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/hrtimer/timer_abc  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device2/
 | 
			
		||||
echo timer_abc > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device2
 | 
			
		||||
 | 
			
		||||
# 修改频率
 | 
			
		||||
cd /sys/bus/iio/devices/trigger1
 | 
			
		||||
echo 1000000000 > sampling_frequency  # 单位ns
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 3.3.2 STM32MP157
 | 
			
		||||
 | 
			
		||||
```SHELL
 | 
			
		||||
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/hrtimer/timer_abc  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device1/
 | 
			
		||||
echo timer_abc > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device1
 | 
			
		||||
 | 
			
		||||
# 修改频率
 | 
			
		||||
cd /sys/bus/iio/devices/trigger1
 | 
			
		||||
echo 1000000000 > sampling_frequency  # 单位ns
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 4. 修改DHT11驱动使用iio_trigger
 | 
			
		||||
 | 
			
		||||
IMX6ULL的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
STM32MP157的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
### 4.1 设置triggered_buffer
 | 
			
		||||
 | 
			
		||||
核心有2点:
 | 
			
		||||
 | 
			
		||||
* 设置pollfunc结构体,里面记录有虚拟中断处理函数:handle、thread_fn
 | 
			
		||||
* 提供setup_ops,它里面有postenable函数指针,用来在使能buffer时注册虚拟的中断
 | 
			
		||||
 | 
			
		||||
IIO子系统里提供了现成的函数:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 4.2 实现中断处理函数
 | 
			
		||||
 | 
			
		||||
先实现handle函数:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
再实现thread_fn函数:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 4.3 上机实验
 | 
			
		||||
 | 
			
		||||
#### 4.3.1 IMX6ULL
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
insmod /root/iio-trig-hrtimer.ko
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/hrtimer/timer_abc  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device2/
 | 
			
		||||
echo timer_abc > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device2
 | 
			
		||||
 | 
			
		||||
# 修改频率
 | 
			
		||||
cd /sys/bus/iio/devices/trigger1
 | 
			
		||||
echo 1000000000 > sampling_frequency  # 单位ns
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 4.3.2 STM32MP157
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
# insmod /root/iio-trig-hrtimer.ko # 157上已经有了这个驱动
 | 
			
		||||
mkdir  /sys/kernel/config/iio/triggers/hrtimer/timer_abc  # 创建trigger
 | 
			
		||||
cat /sys/bus/iio/devices/trigger1/name             # 可以看到这个trigger
 | 
			
		||||
 | 
			
		||||
insmod /root/dht11.ko
 | 
			
		||||
cd /sys/bus/iio/devices/iio\:device1/
 | 
			
		||||
echo timer_abc > trigger/current_trigger               # 在设备上使用trigger
 | 
			
		||||
 | 
			
		||||
echo 1 > scan_elements/in_humidityrelative_en
 | 
			
		||||
echo 1 > scan_elements/in_temp_en
 | 
			
		||||
echo 1 > buffer/enable
 | 
			
		||||
 | 
			
		||||
hexdump /dev/iio\:device1
 | 
			
		||||
 | 
			
		||||
# 修改频率
 | 
			
		||||
cd /sys/bus/iio/devices/trigger1
 | 
			
		||||
echo 1000000000 > sampling_frequency  # 单位ns
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 4.4 代码调用流程回顾
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241120143037699.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 58 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241120144844917.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.2 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241120144923584.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.6 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241120152308135.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 24 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241120152309404.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 24 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241120162213861.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 17 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122102016616.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 107 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122102724799.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 108 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122102856636.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 149 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122103612419.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 168 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122104728332.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 168 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122105432793.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 38 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122105554105.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 28 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122174847707.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 71 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122175029650.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 228 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241122175306406.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 116 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241125103834678.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 6.7 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241125103916918.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 6.3 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241125105034175.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 98 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241125105104461.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 7.9 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241125105301615.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 72 KiB  | 
							
								
								
									
										617
									
								
								STM32MP157/source/A7/14_iio/03_dht11_iio_trigger/dht11.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,617 @@
 | 
			
		||||
/*
 | 
			
		||||
 * DHT11/DHT22 bit banging GPIO driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) Harald Geyer <harald@ccbib.org>
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <linux/err.h>
 | 
			
		||||
#include <linux/interrupt.h>
 | 
			
		||||
#include <linux/device.h>
 | 
			
		||||
#include <linux/kernel.h>
 | 
			
		||||
#include <linux/printk.h>
 | 
			
		||||
#include <linux/slab.h>
 | 
			
		||||
#include <linux/of.h>
 | 
			
		||||
#include <linux/of_device.h>
 | 
			
		||||
#include <linux/sysfs.h>
 | 
			
		||||
#include <linux/io.h>
 | 
			
		||||
#include <linux/module.h>
 | 
			
		||||
#include <linux/platform_device.h>
 | 
			
		||||
#include <linux/wait.h>
 | 
			
		||||
#include <linux/bitops.h>
 | 
			
		||||
#include <linux/completion.h>
 | 
			
		||||
#include <linux/mutex.h>
 | 
			
		||||
#include <linux/delay.h>
 | 
			
		||||
#include <linux/gpio/consumer.h>
 | 
			
		||||
#include <linux/timekeeping.h>
 | 
			
		||||
 | 
			
		||||
#include <linux/iio/iio.h>
 | 
			
		||||
#include <linux/iio/buffer.h>
 | 
			
		||||
#include <linux/iio/kfifo_buf.h>
 | 
			
		||||
#include <linux/iio/trigger_consumer.h>
 | 
			
		||||
#include <linux/iio/triggered_buffer.h>
 | 
			
		||||
 | 
			
		||||
#define DRIVER_NAME	"dht11"
 | 
			
		||||
 | 
			
		||||
#define DHT11_DATA_VALID_TIME	2000000000  /* 2s in ns */
 | 
			
		||||
 | 
			
		||||
#define DHT11_EDGES_PREAMBLE 2
 | 
			
		||||
#define DHT11_BITS_PER_READ 40
 | 
			
		||||
/*
 | 
			
		||||
 * Note that when reading the sensor actually 84 edges are detected, but
 | 
			
		||||
 * since the last edge is not significant, we only store 83:
 | 
			
		||||
 */
 | 
			
		||||
#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
 | 
			
		||||
			      DHT11_EDGES_PREAMBLE + 1)
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Data transmission timing:
 | 
			
		||||
 * Data bits are encoded as pulse length (high time) on the data line.
 | 
			
		||||
 * 0-bit: 22-30uS -- typically 26uS (AM2302)
 | 
			
		||||
 * 1-bit: 68-75uS -- typically 70uS (AM2302)
 | 
			
		||||
 * The acutal timings also depend on the properties of the cable, with
 | 
			
		||||
 * longer cables typically making pulses shorter.
 | 
			
		||||
 *
 | 
			
		||||
 * Our decoding depends on the time resolution of the system:
 | 
			
		||||
 * timeres > 34uS ... don't know what a 1-tick pulse is
 | 
			
		||||
 * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
 | 
			
		||||
 * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
 | 
			
		||||
 * timeres < 23uS ... no problem
 | 
			
		||||
 *
 | 
			
		||||
 * Luckily clocks in the 33-44kHz range are quite uncommon, so we can
 | 
			
		||||
 * support most systems if the threshold for decoding a pulse as 1-bit
 | 
			
		||||
 * is chosen carefully. If somebody really wants to support clocks around
 | 
			
		||||
 * 40kHz, where this driver is most unreliable, there are two options.
 | 
			
		||||
 * a) select an implementation using busy loop polling on those systems
 | 
			
		||||
 * b) use the checksum to do some probabilistic decoding
 | 
			
		||||
 */
 | 
			
		||||
#define DHT11_START_TRANSMISSION_MIN	18000  /* us */
 | 
			
		||||
#define DHT11_START_TRANSMISSION_MAX	20000  /* us */
 | 
			
		||||
#define DHT11_MIN_TIMERES	34000  /* ns */
 | 
			
		||||
#define DHT11_THRESHOLD		49000  /* ns */
 | 
			
		||||
#define DHT11_AMBIG_LOW		23000  /* ns */
 | 
			
		||||
#define DHT11_AMBIG_HIGH	30000  /* ns */
 | 
			
		||||
 | 
			
		||||
struct dht11 {
 | 
			
		||||
	struct device			*dev;
 | 
			
		||||
 | 
			
		||||
	struct gpio_desc		*gpiod;
 | 
			
		||||
	int				irq;
 | 
			
		||||
 | 
			
		||||
	struct completion		completion;
 | 
			
		||||
	/* The iio sysfs interface doesn't prevent concurrent reads: */
 | 
			
		||||
	struct mutex			lock;
 | 
			
		||||
 | 
			
		||||
	s64				timestamp;
 | 
			
		||||
	int				temperature;
 | 
			
		||||
	int				humidity;
 | 
			
		||||
 | 
			
		||||
	/* num_edges: -1 means "no transmission in progress" */
 | 
			
		||||
	int				num_edges;
 | 
			
		||||
	struct {s64 ts; int value; }	edges[DHT11_EDGES_PER_READ];
 | 
			
		||||
	struct delayed_work		work;
 | 
			
		||||
	struct iio_dev *iio;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
/*
 | 
			
		||||
 * dht11_edges_print: show the data as actually received by the
 | 
			
		||||
 *                    driver.
 | 
			
		||||
 */
 | 
			
		||||
static void dht11_edges_print(struct dht11 *dht11)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
 | 
			
		||||
	for (i = 1; i < dht11->num_edges; ++i) {
 | 
			
		||||
		dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
 | 
			
		||||
			dht11->edges[i].ts - dht11->edges[i - 1].ts,
 | 
			
		||||
			dht11->edges[i - 1].value ? "high" : "low");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
#endif /* CONFIG_DYNAMIC_DEBUG */
 | 
			
		||||
 | 
			
		||||
static unsigned char dht11_decode_byte(char *bits)
 | 
			
		||||
{
 | 
			
		||||
	unsigned char ret = 0;
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < 8; ++i) {
 | 
			
		||||
		ret <<= 1;
 | 
			
		||||
		if (bits[i])
 | 
			
		||||
			++ret;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_decode(struct dht11 *dht11, int offset)
 | 
			
		||||
{
 | 
			
		||||
	int i, t;
 | 
			
		||||
	char bits[DHT11_BITS_PER_READ];
 | 
			
		||||
	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
 | 
			
		||||
		t = dht11->edges[offset + 2 * i + 2].ts -
 | 
			
		||||
			dht11->edges[offset + 2 * i + 1].ts;
 | 
			
		||||
		if (!dht11->edges[offset + 2 * i + 1].value) {
 | 
			
		||||
			dev_dbg(dht11->dev,
 | 
			
		||||
				"lost synchronisation at edge %d\n",
 | 
			
		||||
				offset + 2 * i + 1);
 | 
			
		||||
			return -EIO;
 | 
			
		||||
		}
 | 
			
		||||
		bits[i] = t > DHT11_THRESHOLD;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	hum_int = dht11_decode_byte(bits);
 | 
			
		||||
	hum_dec = dht11_decode_byte(&bits[8]);
 | 
			
		||||
	temp_int = dht11_decode_byte(&bits[16]);
 | 
			
		||||
	temp_dec = dht11_decode_byte(&bits[24]);
 | 
			
		||||
	checksum = dht11_decode_byte(&bits[32]);
 | 
			
		||||
 | 
			
		||||
	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
 | 
			
		||||
		dev_dbg(dht11->dev, "invalid checksum\n");
 | 
			
		||||
		return -EIO;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11->timestamp = ktime_get_boottime_ns();
 | 
			
		||||
	if (hum_int < 4) {  /* DHT22: 100000 = (3*256+232)*100 */
 | 
			
		||||
		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 | 
			
		||||
					((temp_int & 0x80) ? -100 : 100);
 | 
			
		||||
		dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
 | 
			
		||||
	} else  {
 | 
			
		||||
		dht11->temperature = temp_int;
 | 
			
		||||
		dht11->humidity = hum_int;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * IRQ handler called on GPIO edges
 | 
			
		||||
 */
 | 
			
		||||
static irqreturn_t dht11_handle_irq(int irq, void *data)
 | 
			
		||||
{
 | 
			
		||||
	struct iio_dev *iio = data;
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio);
 | 
			
		||||
 | 
			
		||||
	/* TODO: Consider making the handler safe for IRQ sharing */
 | 
			
		||||
	if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
 | 
			
		||||
		dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
 | 
			
		||||
		dht11->edges[dht11->num_edges++].value =
 | 
			
		||||
						gpiod_get_value(dht11->gpiod);
 | 
			
		||||
 | 
			
		||||
		if (dht11->num_edges >= DHT11_EDGES_PER_READ)
 | 
			
		||||
			complete(&dht11->completion);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return IRQ_HANDLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_read_raw(struct iio_dev *iio_dev,
 | 
			
		||||
			  const struct iio_chan_spec *chan,
 | 
			
		||||
			int *val, int *val2, long m)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio_dev);
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpiod_direction_output(dht11->gpiod, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
			     DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpiod_direction_input(dht11->gpiod);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ret = IIO_VAL_INT;
 | 
			
		||||
	if (chan->type == IIO_TEMP)
 | 
			
		||||
		*val = dht11->temperature;
 | 
			
		||||
	else if (chan->type == IIO_HUMIDITYRELATIVE)
 | 
			
		||||
		*val = dht11->humidity;
 | 
			
		||||
	else
 | 
			
		||||
		ret = -EINVAL;
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const struct iio_info dht11_iio_info = {
 | 
			
		||||
	.read_raw		= dht11_read_raw,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct iio_chan_spec dht11_chan_spec[] = {
 | 
			
		||||
	{ .type = IIO_TEMP,
 | 
			
		||||
		.scan_index = 0,
 | 
			
		||||
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 
 | 
			
		||||
		.scan_type = {
 | 
			
		||||
			.sign = 's',
 | 
			
		||||
			.realbits = 8,
 | 
			
		||||
			.storagebits = 8,
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
	{ .type = IIO_HUMIDITYRELATIVE,
 | 
			
		||||
		.scan_index = 1,
 | 
			
		||||
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 
 | 
			
		||||
		.scan_type = {
 | 
			
		||||
			.sign = 's',
 | 
			
		||||
			.realbits = 8,
 | 
			
		||||
			.storagebits = 8,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct of_device_id dht11_dt_ids[] = {
 | 
			
		||||
	{ .compatible = "dht11", },
 | 
			
		||||
	{ }
 | 
			
		||||
};
 | 
			
		||||
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_preenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_postenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
	
 | 
			
		||||
	/* start work */
 | 
			
		||||
	schedule_delayed_work(&dht11->work, 1);
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_postdisable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
 | 
			
		||||
	/* cancel work */
 | 
			
		||||
	cancel_delayed_work_sync(&dht11->work);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static const struct iio_buffer_setup_ops dht11_buf_setup_ops = {
 | 
			
		||||
	.preenable = dht11_buf_preenable,
 | 
			
		||||
	.postenable = dht11_buf_postenable,
 | 
			
		||||
	.postdisable = dht11_buf_postdisable,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static irqreturn_t dht11_triggered_buf_handle(int irq, void * p)
 | 
			
		||||
{
 | 
			
		||||
	struct iio_poll_func *pf = p;
 | 
			
		||||
	pf->timestamp = iio_get_time_ns(pf->indio_dev);
 | 
			
		||||
	return IRQ_WAKE_THREAD;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static irqreturn_t dht11_triggered_buf_thread(int irq, void * p)
 | 
			
		||||
{
 | 
			
		||||
	/* read data */
 | 
			
		||||
	struct iio_poll_func *pf = p;
 | 
			
		||||
	struct iio_dev *iio_dev = pf->indio_dev;	
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio_dev);
 | 
			
		||||
	
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
	
 | 
			
		||||
	/* read dht11 */
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpiod_direction_output(dht11->gpiod, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
				 DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpiod_direction_input(dht11->gpiod);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vals[0] = dht11->temperature;
 | 
			
		||||
	vals[1] = dht11->humidity;
 | 
			
		||||
	
 | 
			
		||||
	/* put data into iio_buffer */
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
	
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
 | 
			
		||||
	iio_trigger_notify_done(iio_dev->trig);
 | 
			
		||||
 | 
			
		||||
	return IRQ_HANDLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buffer_init(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
#if 0
 | 
			
		||||
	struct iio_buffer *buffer;
 | 
			
		||||
 | 
			
		||||
	buffer = iio_kfifo_allocate();
 | 
			
		||||
	if (!buffer)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	iio_device_attach_buffer(indio_dev, buffer);
 | 
			
		||||
 | 
			
		||||
	/* Ring buffer functions - here trigger setup related */
 | 
			
		||||
	indio_dev->setup_ops = &dht11_buf_setup_ops;
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
#else
 | 
			
		||||
	return iio_triggered_buffer_setup(indio_dev, dht11_triggered_buf_handle, dht11_triggered_buf_thread, NULL);
 | 
			
		||||
#endif	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dht11_work(struct work_struct *work)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = container_of(work, struct dht11, work.work);
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	struct iio_dev *iio_dev = dht11->iio;
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
	
 | 
			
		||||
	/* read dht11 */
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpiod_direction_output(dht11->gpiod, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
			     DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpiod_direction_input(dht11->gpiod);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vals[0] = dht11->temperature;
 | 
			
		||||
	vals[1] = dht11->humidity;
 | 
			
		||||
	
 | 
			
		||||
	/* put data into iio_buffer */
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
	
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	
 | 
			
		||||
	/* re-start work */
 | 
			
		||||
	if (ret)
 | 
			
		||||
	{
 | 
			
		||||
		/* 有错误 */
 | 
			
		||||
		schedule_delayed_work(&dht11->work, 0);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		schedule_delayed_work(&dht11->work, HZ);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const unsigned long dht11_scan_masks[] = {0x3, 0};
 | 
			
		||||
 | 
			
		||||
static int dht11_probe(struct platform_device *pdev)
 | 
			
		||||
{
 | 
			
		||||
	struct device *dev = &pdev->dev;
 | 
			
		||||
	struct dht11 *dht11;
 | 
			
		||||
	struct iio_dev *iio;
 | 
			
		||||
 | 
			
		||||
	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
 | 
			
		||||
	if (!iio) {
 | 
			
		||||
		dev_err(dev, "Failed to allocate IIO device\n");
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11 = iio_priv(iio);
 | 
			
		||||
	dht11->dev = dev;
 | 
			
		||||
	dht11->iio = iio;
 | 
			
		||||
 | 
			
		||||
	dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
 | 
			
		||||
	if (IS_ERR(dht11->gpiod))
 | 
			
		||||
		return PTR_ERR(dht11->gpiod);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	dht11->irq = gpiod_to_irq(dht11->gpiod);
 | 
			
		||||
	if (dht11->irq < 0) {
 | 
			
		||||
		dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
 | 
			
		||||
	platform_set_drvdata(pdev, iio);
 | 
			
		||||
 | 
			
		||||
	init_completion(&dht11->completion);
 | 
			
		||||
	mutex_init(&dht11->lock);
 | 
			
		||||
	iio->name = pdev->name;
 | 
			
		||||
	iio->dev.parent = &pdev->dev;
 | 
			
		||||
	iio->info = &dht11_iio_info;
 | 
			
		||||
	iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
 | 
			
		||||
	iio->channels = dht11_chan_spec;
 | 
			
		||||
	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
 | 
			
		||||
	iio->available_scan_masks = dht11_scan_masks;
 | 
			
		||||
 | 
			
		||||
	dht11_buffer_init(iio);
 | 
			
		||||
 | 
			
		||||
	/* init work */
 | 
			
		||||
	INIT_DELAYED_WORK(&dht11->work, dht11_work);
 | 
			
		||||
 | 
			
		||||
	return devm_iio_device_register(dev, iio);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct platform_driver dht11_driver = {
 | 
			
		||||
	.driver = {
 | 
			
		||||
		.name	= DRIVER_NAME,
 | 
			
		||||
		.of_match_table = dht11_dt_ids,
 | 
			
		||||
	},
 | 
			
		||||
	.probe  = dht11_probe,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module_platform_driver(dht11_driver);
 | 
			
		||||
 | 
			
		||||
MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
 | 
			
		||||
MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
 | 
			
		||||
MODULE_LICENSE("GPL v2");
 | 
			
		||||
@@ -0,0 +1,4 @@
 | 
			
		||||
humidity_sensor {
 | 
			
		||||
	compatible = "dht11";
 | 
			
		||||
    gpios = <&gpioa 5 GPIO_ACTIVE_HIGH>;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										617
									
								
								STM32MP157/source/A7/14_iio/04_dht11_iio_trigger_课堂现场编写/dht11.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,617 @@
 | 
			
		||||
/*
 | 
			
		||||
 * DHT11/DHT22 bit banging GPIO driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) Harald Geyer <harald@ccbib.org>
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include <linux/err.h>
 | 
			
		||||
#include <linux/interrupt.h>
 | 
			
		||||
#include <linux/device.h>
 | 
			
		||||
#include <linux/kernel.h>
 | 
			
		||||
#include <linux/printk.h>
 | 
			
		||||
#include <linux/slab.h>
 | 
			
		||||
#include <linux/of.h>
 | 
			
		||||
#include <linux/of_device.h>
 | 
			
		||||
#include <linux/sysfs.h>
 | 
			
		||||
#include <linux/io.h>
 | 
			
		||||
#include <linux/module.h>
 | 
			
		||||
#include <linux/platform_device.h>
 | 
			
		||||
#include <linux/wait.h>
 | 
			
		||||
#include <linux/bitops.h>
 | 
			
		||||
#include <linux/completion.h>
 | 
			
		||||
#include <linux/mutex.h>
 | 
			
		||||
#include <linux/delay.h>
 | 
			
		||||
#include <linux/gpio/consumer.h>
 | 
			
		||||
#include <linux/timekeeping.h>
 | 
			
		||||
 | 
			
		||||
#include <linux/iio/iio.h>
 | 
			
		||||
#include <linux/iio/buffer.h>
 | 
			
		||||
#include <linux/iio/kfifo_buf.h>
 | 
			
		||||
#include <linux/iio/trigger_consumer.h>
 | 
			
		||||
#include <linux/iio/triggered_buffer.h>
 | 
			
		||||
 | 
			
		||||
#define DRIVER_NAME	"dht11"
 | 
			
		||||
 | 
			
		||||
#define DHT11_DATA_VALID_TIME	2000000000  /* 2s in ns */
 | 
			
		||||
 | 
			
		||||
#define DHT11_EDGES_PREAMBLE 2
 | 
			
		||||
#define DHT11_BITS_PER_READ 40
 | 
			
		||||
/*
 | 
			
		||||
 * Note that when reading the sensor actually 84 edges are detected, but
 | 
			
		||||
 * since the last edge is not significant, we only store 83:
 | 
			
		||||
 */
 | 
			
		||||
#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
 | 
			
		||||
			      DHT11_EDGES_PREAMBLE + 1)
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Data transmission timing:
 | 
			
		||||
 * Data bits are encoded as pulse length (high time) on the data line.
 | 
			
		||||
 * 0-bit: 22-30uS -- typically 26uS (AM2302)
 | 
			
		||||
 * 1-bit: 68-75uS -- typically 70uS (AM2302)
 | 
			
		||||
 * The acutal timings also depend on the properties of the cable, with
 | 
			
		||||
 * longer cables typically making pulses shorter.
 | 
			
		||||
 *
 | 
			
		||||
 * Our decoding depends on the time resolution of the system:
 | 
			
		||||
 * timeres > 34uS ... don't know what a 1-tick pulse is
 | 
			
		||||
 * 34uS > timeres > 30uS ... no problem (30kHz and 32kHz clocks)
 | 
			
		||||
 * 30uS > timeres > 23uS ... don't know what a 2-tick pulse is
 | 
			
		||||
 * timeres < 23uS ... no problem
 | 
			
		||||
 *
 | 
			
		||||
 * Luckily clocks in the 33-44kHz range are quite uncommon, so we can
 | 
			
		||||
 * support most systems if the threshold for decoding a pulse as 1-bit
 | 
			
		||||
 * is chosen carefully. If somebody really wants to support clocks around
 | 
			
		||||
 * 40kHz, where this driver is most unreliable, there are two options.
 | 
			
		||||
 * a) select an implementation using busy loop polling on those systems
 | 
			
		||||
 * b) use the checksum to do some probabilistic decoding
 | 
			
		||||
 */
 | 
			
		||||
#define DHT11_START_TRANSMISSION_MIN	18000  /* us */
 | 
			
		||||
#define DHT11_START_TRANSMISSION_MAX	20000  /* us */
 | 
			
		||||
#define DHT11_MIN_TIMERES	34000  /* ns */
 | 
			
		||||
#define DHT11_THRESHOLD		49000  /* ns */
 | 
			
		||||
#define DHT11_AMBIG_LOW		23000  /* ns */
 | 
			
		||||
#define DHT11_AMBIG_HIGH	30000  /* ns */
 | 
			
		||||
 | 
			
		||||
struct dht11 {
 | 
			
		||||
	struct device			*dev;
 | 
			
		||||
 | 
			
		||||
	struct gpio_desc		*gpiod;
 | 
			
		||||
	int				irq;
 | 
			
		||||
 | 
			
		||||
	struct completion		completion;
 | 
			
		||||
	/* The iio sysfs interface doesn't prevent concurrent reads: */
 | 
			
		||||
	struct mutex			lock;
 | 
			
		||||
 | 
			
		||||
	s64				timestamp;
 | 
			
		||||
	int				temperature;
 | 
			
		||||
	int				humidity;
 | 
			
		||||
 | 
			
		||||
	/* num_edges: -1 means "no transmission in progress" */
 | 
			
		||||
	int				num_edges;
 | 
			
		||||
	struct {s64 ts; int value; }	edges[DHT11_EDGES_PER_READ];
 | 
			
		||||
	struct delayed_work		work;
 | 
			
		||||
	struct iio_dev *iio;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
/*
 | 
			
		||||
 * dht11_edges_print: show the data as actually received by the
 | 
			
		||||
 *                    driver.
 | 
			
		||||
 */
 | 
			
		||||
static void dht11_edges_print(struct dht11 *dht11)
 | 
			
		||||
{
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges);
 | 
			
		||||
	for (i = 1; i < dht11->num_edges; ++i) {
 | 
			
		||||
		dev_dbg(dht11->dev, "%d: %lld ns %s\n", i,
 | 
			
		||||
			dht11->edges[i].ts - dht11->edges[i - 1].ts,
 | 
			
		||||
			dht11->edges[i - 1].value ? "high" : "low");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
#endif /* CONFIG_DYNAMIC_DEBUG */
 | 
			
		||||
 | 
			
		||||
static unsigned char dht11_decode_byte(char *bits)
 | 
			
		||||
{
 | 
			
		||||
	unsigned char ret = 0;
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < 8; ++i) {
 | 
			
		||||
		ret <<= 1;
 | 
			
		||||
		if (bits[i])
 | 
			
		||||
			++ret;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_decode(struct dht11 *dht11, int offset)
 | 
			
		||||
{
 | 
			
		||||
	int i, t;
 | 
			
		||||
	char bits[DHT11_BITS_PER_READ];
 | 
			
		||||
	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 | 
			
		||||
 | 
			
		||||
	for (i = 0; i < DHT11_BITS_PER_READ; ++i) {
 | 
			
		||||
		t = dht11->edges[offset + 2 * i + 2].ts -
 | 
			
		||||
			dht11->edges[offset + 2 * i + 1].ts;
 | 
			
		||||
		if (!dht11->edges[offset + 2 * i + 1].value) {
 | 
			
		||||
			dev_dbg(dht11->dev,
 | 
			
		||||
				"lost synchronisation at edge %d\n",
 | 
			
		||||
				offset + 2 * i + 1);
 | 
			
		||||
			return -EIO;
 | 
			
		||||
		}
 | 
			
		||||
		bits[i] = t > DHT11_THRESHOLD;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	hum_int = dht11_decode_byte(bits);
 | 
			
		||||
	hum_dec = dht11_decode_byte(&bits[8]);
 | 
			
		||||
	temp_int = dht11_decode_byte(&bits[16]);
 | 
			
		||||
	temp_dec = dht11_decode_byte(&bits[24]);
 | 
			
		||||
	checksum = dht11_decode_byte(&bits[32]);
 | 
			
		||||
 | 
			
		||||
	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) {
 | 
			
		||||
		dev_dbg(dht11->dev, "invalid checksum\n");
 | 
			
		||||
		return -EIO;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11->timestamp = ktime_get_boottime_ns();
 | 
			
		||||
	if (hum_int < 4) {  /* DHT22: 100000 = (3*256+232)*100 */
 | 
			
		||||
		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 | 
			
		||||
					((temp_int & 0x80) ? -100 : 100);
 | 
			
		||||
		dht11->humidity = ((hum_int << 8) + hum_dec) * 100;
 | 
			
		||||
	} else  {
 | 
			
		||||
		dht11->temperature = temp_int;
 | 
			
		||||
		dht11->humidity = hum_int;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * IRQ handler called on GPIO edges
 | 
			
		||||
 */
 | 
			
		||||
static irqreturn_t dht11_handle_irq(int irq, void *data)
 | 
			
		||||
{
 | 
			
		||||
	struct iio_dev *iio = data;
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio);
 | 
			
		||||
 | 
			
		||||
	/* TODO: Consider making the handler safe for IRQ sharing */
 | 
			
		||||
	if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
 | 
			
		||||
		dht11->edges[dht11->num_edges].ts = ktime_get_boottime_ns();
 | 
			
		||||
		dht11->edges[dht11->num_edges++].value =
 | 
			
		||||
						gpiod_get_value(dht11->gpiod);
 | 
			
		||||
 | 
			
		||||
		if (dht11->num_edges >= DHT11_EDGES_PER_READ)
 | 
			
		||||
			complete(&dht11->completion);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return IRQ_HANDLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_read_raw(struct iio_dev *iio_dev,
 | 
			
		||||
			  const struct iio_chan_spec *chan,
 | 
			
		||||
			int *val, int *val2, long m)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio_dev);
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpiod_direction_output(dht11->gpiod, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
			     DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpiod_direction_input(dht11->gpiod);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ret = IIO_VAL_INT;
 | 
			
		||||
	if (chan->type == IIO_TEMP)
 | 
			
		||||
		*val = dht11->temperature;
 | 
			
		||||
	else if (chan->type == IIO_HUMIDITYRELATIVE)
 | 
			
		||||
		*val = dht11->humidity;
 | 
			
		||||
	else
 | 
			
		||||
		ret = -EINVAL;
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const struct iio_info dht11_iio_info = {
 | 
			
		||||
	.read_raw		= dht11_read_raw,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct iio_chan_spec dht11_chan_spec[] = {
 | 
			
		||||
	{ .type = IIO_TEMP,
 | 
			
		||||
		.scan_index = 0,
 | 
			
		||||
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 
 | 
			
		||||
		.scan_type = {
 | 
			
		||||
			.sign = 's',
 | 
			
		||||
			.realbits = 8,
 | 
			
		||||
			.storagebits = 8,
 | 
			
		||||
		},
 | 
			
		||||
	},
 | 
			
		||||
	{ .type = IIO_HUMIDITYRELATIVE,
 | 
			
		||||
		.scan_index = 1,
 | 
			
		||||
		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 
 | 
			
		||||
		.scan_type = {
 | 
			
		||||
			.sign = 's',
 | 
			
		||||
			.realbits = 8,
 | 
			
		||||
			.storagebits = 8,
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct of_device_id dht11_dt_ids[] = {
 | 
			
		||||
	{ .compatible = "dht11", },
 | 
			
		||||
	{ }
 | 
			
		||||
};
 | 
			
		||||
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_preenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_postenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
	
 | 
			
		||||
	/* start work */
 | 
			
		||||
	schedule_delayed_work(&dht11->work, 1);
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buf_postdisable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
 | 
			
		||||
	/* cancel work */
 | 
			
		||||
	cancel_delayed_work_sync(&dht11->work);
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static const struct iio_buffer_setup_ops dht11_buf_setup_ops = {
 | 
			
		||||
	.preenable = dht11_buf_preenable,
 | 
			
		||||
	.postenable = dht11_buf_postenable,
 | 
			
		||||
	.postdisable = dht11_buf_postdisable,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static irqreturn_t dht11_triggered_buf_handle(int irq, void * p)
 | 
			
		||||
{
 | 
			
		||||
	struct iio_poll_func *pf = p;
 | 
			
		||||
	pf->timestamp = iio_get_time_ns(pf->indio_dev);
 | 
			
		||||
	return IRQ_WAKE_THREAD;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static irqreturn_t dht11_triggered_buf_thread(int irq, void * p)
 | 
			
		||||
{
 | 
			
		||||
	/* read data */
 | 
			
		||||
	struct iio_poll_func *pf = p;
 | 
			
		||||
	struct iio_dev *iio_dev = pf->indio_dev;	
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(iio_dev);
 | 
			
		||||
	
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
	
 | 
			
		||||
	/* read dht11 */
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpiod_direction_output(dht11->gpiod, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
				 DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpiod_direction_input(dht11->gpiod);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vals[0] = dht11->temperature;
 | 
			
		||||
	vals[1] = dht11->humidity;
 | 
			
		||||
	
 | 
			
		||||
	/* put data into iio_buffer */
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
	
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
 | 
			
		||||
	iio_trigger_notify_done(iio_dev->trig);
 | 
			
		||||
 | 
			
		||||
	return IRQ_HANDLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_buffer_init(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
#if 0
 | 
			
		||||
	struct iio_buffer *buffer;
 | 
			
		||||
 | 
			
		||||
	buffer = iio_kfifo_allocate();
 | 
			
		||||
	if (!buffer)
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
 | 
			
		||||
	iio_device_attach_buffer(indio_dev, buffer);
 | 
			
		||||
 | 
			
		||||
	/* Ring buffer functions - here trigger setup related */
 | 
			
		||||
	indio_dev->setup_ops = &dht11_buf_setup_ops;
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
#else
 | 
			
		||||
	return iio_triggered_buffer_setup(indio_dev, dht11_triggered_buf_handle, dht11_triggered_buf_thread, NULL);
 | 
			
		||||
#endif	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dht11_work(struct work_struct *work)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = container_of(work, struct dht11, work.work);
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	struct iio_dev *iio_dev = dht11->iio;
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
	
 | 
			
		||||
	/* read dht11 */
 | 
			
		||||
	mutex_lock(&dht11->lock);
 | 
			
		||||
	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boottime_ns()) {
 | 
			
		||||
		timeres = ktime_get_resolution_ns();
 | 
			
		||||
		dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
 | 
			
		||||
		if (timeres > DHT11_MIN_TIMERES) {
 | 
			
		||||
			dev_err(dht11->dev, "timeresolution %dns too low\n",
 | 
			
		||||
				timeres);
 | 
			
		||||
			/* In theory a better clock could become available
 | 
			
		||||
			 * at some point ... and there is no error code
 | 
			
		||||
			 * that really fits better.
 | 
			
		||||
			 */
 | 
			
		||||
			ret = -EAGAIN;
 | 
			
		||||
			goto err;
 | 
			
		||||
		}
 | 
			
		||||
		if (timeres > DHT11_AMBIG_LOW && timeres < DHT11_AMBIG_HIGH)
 | 
			
		||||
			dev_warn(dht11->dev,
 | 
			
		||||
				 "timeresolution: %dns - decoding ambiguous\n",
 | 
			
		||||
				 timeres);
 | 
			
		||||
 | 
			
		||||
		reinit_completion(&dht11->completion);
 | 
			
		||||
 | 
			
		||||
		dht11->num_edges = 0;
 | 
			
		||||
		ret = gpiod_direction_output(dht11->gpiod, 0);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
		usleep_range(DHT11_START_TRANSMISSION_MIN,
 | 
			
		||||
			     DHT11_START_TRANSMISSION_MAX);
 | 
			
		||||
		ret = gpiod_direction_input(dht11->gpiod);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = request_irq(dht11->irq, dht11_handle_irq,
 | 
			
		||||
				  IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
 | 
			
		||||
				  iio_dev->name, iio_dev);
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		ret = wait_for_completion_killable_timeout(&dht11->completion,
 | 
			
		||||
							   HZ);
 | 
			
		||||
 | 
			
		||||
		free_irq(dht11->irq, iio_dev);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_DYNAMIC_DEBUG
 | 
			
		||||
		dht11_edges_print(dht11);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 | 
			
		||||
			dev_err(dht11->dev, "Only %d signal edges detected\n",
 | 
			
		||||
				dht11->num_edges);
 | 
			
		||||
			ret = -ETIMEDOUT;
 | 
			
		||||
		}
 | 
			
		||||
		if (ret < 0)
 | 
			
		||||
			goto err;
 | 
			
		||||
 | 
			
		||||
		offset = DHT11_EDGES_PREAMBLE +
 | 
			
		||||
				dht11->num_edges - DHT11_EDGES_PER_READ;
 | 
			
		||||
		for (; offset >= 0; --offset) {
 | 
			
		||||
			ret = dht11_decode(dht11, offset);
 | 
			
		||||
			if (!ret)
 | 
			
		||||
				break;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if (ret)
 | 
			
		||||
			goto err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	vals[0] = dht11->temperature;
 | 
			
		||||
	vals[1] = dht11->humidity;
 | 
			
		||||
	
 | 
			
		||||
	/* put data into iio_buffer */
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
	
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	
 | 
			
		||||
	/* re-start work */
 | 
			
		||||
	if (ret)
 | 
			
		||||
	{
 | 
			
		||||
		/* 有错误 */
 | 
			
		||||
		schedule_delayed_work(&dht11->work, 0);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		schedule_delayed_work(&dht11->work, HZ);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const unsigned long dht11_scan_masks[] = {0x3, 0};
 | 
			
		||||
 | 
			
		||||
static int dht11_probe(struct platform_device *pdev)
 | 
			
		||||
{
 | 
			
		||||
	struct device *dev = &pdev->dev;
 | 
			
		||||
	struct dht11 *dht11;
 | 
			
		||||
	struct iio_dev *iio;
 | 
			
		||||
 | 
			
		||||
	iio = devm_iio_device_alloc(dev, sizeof(*dht11));
 | 
			
		||||
	if (!iio) {
 | 
			
		||||
		dev_err(dev, "Failed to allocate IIO device\n");
 | 
			
		||||
		return -ENOMEM;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11 = iio_priv(iio);
 | 
			
		||||
	dht11->dev = dev;
 | 
			
		||||
	dht11->iio = iio;
 | 
			
		||||
 | 
			
		||||
	dht11->gpiod = devm_gpiod_get(dev, NULL, GPIOD_IN);
 | 
			
		||||
	if (IS_ERR(dht11->gpiod))
 | 
			
		||||
		return PTR_ERR(dht11->gpiod);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	dht11->irq = gpiod_to_irq(dht11->gpiod);
 | 
			
		||||
	if (dht11->irq < 0) {
 | 
			
		||||
		dev_err(dev, "GPIO %d has no interrupt\n", desc_to_gpio(dht11->gpiod));
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dht11->timestamp = ktime_get_boottime_ns() - DHT11_DATA_VALID_TIME - 1;
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
 | 
			
		||||
	platform_set_drvdata(pdev, iio);
 | 
			
		||||
 | 
			
		||||
	init_completion(&dht11->completion);
 | 
			
		||||
	mutex_init(&dht11->lock);
 | 
			
		||||
	iio->name = pdev->name;
 | 
			
		||||
	iio->dev.parent = &pdev->dev;
 | 
			
		||||
	iio->info = &dht11_iio_info;
 | 
			
		||||
	iio->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
 | 
			
		||||
	iio->channels = dht11_chan_spec;
 | 
			
		||||
	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
 | 
			
		||||
	iio->available_scan_masks = dht11_scan_masks;
 | 
			
		||||
 | 
			
		||||
	dht11_buffer_init(iio);
 | 
			
		||||
 | 
			
		||||
	/* init work */
 | 
			
		||||
	INIT_DELAYED_WORK(&dht11->work, dht11_work);
 | 
			
		||||
 | 
			
		||||
	return devm_iio_device_register(dev, iio);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct platform_driver dht11_driver = {
 | 
			
		||||
	.driver = {
 | 
			
		||||
		.name	= DRIVER_NAME,
 | 
			
		||||
		.of_match_table = dht11_dt_ids,
 | 
			
		||||
	},
 | 
			
		||||
	.probe  = dht11_probe,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
module_platform_driver(dht11_driver);
 | 
			
		||||
 | 
			
		||||
MODULE_AUTHOR("Harald Geyer <harald@ccbib.org>");
 | 
			
		||||
MODULE_DESCRIPTION("DHT11 humidity/temperature sensor driver");
 | 
			
		||||
MODULE_LICENSE("GPL v2");
 | 
			
		||||
@@ -0,0 +1,4 @@
 | 
			
		||||
humidity_sensor {
 | 
			
		||||
	compatible = "dht11";
 | 
			
		||||
    gpios = <&gpioa 5 GPIO_ACTIVE_HIGH>;
 | 
			
		||||
};
 | 
			
		||||