02.2_增加iio_buffer并体验sysfs, 02.3_实现iio_buffer的写入
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/02.2_增加iio_buffer并体验sysfs.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/02.3_实现iio_buffer的写入.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -79,14 +79,122 @@ hexdump /dev/iio\:device2
 | 
			
		||||
 | 
			
		||||
### 1.6 上机体验
 | 
			
		||||
 | 
			
		||||
IMX6ULL的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 2. 使用iio_buffer的DHT11驱动
 | 
			
		||||
STM32MP157的源码:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
## 2. 修改DHT11驱动使用iio_buffer
 | 
			
		||||
 | 
			
		||||
IMX6ULL的源码:
 | 
			
		||||
 | 
			
		||||

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

 | 
			
		||||
 | 
			
		||||
### 2.1 增加iio_buffer并体验sysfs
 | 
			
		||||
 | 
			
		||||
#### 2.1.1 驱动核心代码
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 2.1.2 buffer目录下的sysfs文件
 | 
			
		||||
 | 
			
		||||
`/sys/bus/iio/devices/iio:deviceX/buffer`目录下有如下文件:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
这几个文件对应的读写函数如下:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
调用过程:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
dht11_probe
 | 
			
		||||
	devm_iio_device_register
 | 
			
		||||
		iio_device_register
 | 
			
		||||
			ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
 | 
			
		||||
				// 对应/sys/bus/iio/devices/iio:deviceX/buffer
 | 
			
		||||
				memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
 | 
			
		||||
				buffer->buffer_group.name = "buffer";
 | 
			
		||||
				buffer->buffer_group.attrs = attr;
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 2.1.3 scan_elements目录下的sysfs文件
 | 
			
		||||
 | 
			
		||||
`/sys/bus/iio/devices/iio:deviceX/scan_elements`目录下有如下文件:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
这几个文件对应的读写函数如下:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
调用过程:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
dht11_probe
 | 
			
		||||
	devm_iio_device_register
 | 
			
		||||
		iio_device_register
 | 
			
		||||
			ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
 | 
			
		||||
				// 对应/sys/bus/iio/devices/iio:deviceX/buffer
 | 
			
		||||
				memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
 | 
			
		||||
				buffer->buffer_group.name = "buffer";
 | 
			
		||||
				buffer->buffer_group.attrs = attr;
 | 
			
		||||
			
 | 
			
		||||
            // 对于每一个channel
 | 
			
		||||
			for (i = 0; i < indio_dev->num_channels; i++) {
 | 
			
		||||
				ret = iio_buffer_add_channel_sysfs(indio_dev,&channels[i]);
 | 
			
		||||
            }
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 2.1.4 上机体验
 | 
			
		||||
 | 
			
		||||
需要添加如下信息:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 2.2 实现iio_buffer的写入
 | 
			
		||||
 | 
			
		||||
#### 2.2.1 设计思路
 | 
			
		||||
 | 
			
		||||
DHT11没有中断引脚,DHT11的访问比较慢。如何启动数据的采集?最好的办法是使用内核线程。工作队列(work)的上下文就在内核线程里,我们可以使用工作队列来读DHT11。
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 2.2.2 编程要点
 | 
			
		||||
 | 
			
		||||
* 分配、设置工作队列
 | 
			
		||||
  
 | 
			
		||||
* 使能buffer后启动工作队列
 | 
			
		||||
  
 | 
			
		||||
* 工作队列里读取数据、再次启动工作队列
 | 
			
		||||
  
 | 
			
		||||
* 停止buffer后取消工作队列
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
#### 2.2.3 细节
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108102322454.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 62 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108102839764.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 72 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108102940208.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.9 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108103346047.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 14 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108103459523.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 81 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108103946049.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 93 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108104137696.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 51 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108112942730.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 4.0 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108113032977.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 21 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108113327227.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 90 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108113349427.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 20 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108113544300.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 51 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108131823283.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.8 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108131903721.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 6.9 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								IMX6ULL/doc_pic/14_IIO/pic/image-20241108131925907.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 6.5 KiB  | 
@@ -325,7 +325,7 @@ static int dht11_iio_buffer_postenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
 | 
			
		||||
 	schedule_delayed_work(&dht11->work, HZ*5);
 | 
			
		||||
 	schedule_delayed_work(&dht11->work, HZ);
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,530 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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>
 | 
			
		||||
 | 
			
		||||
#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 int dht11_buffer_init(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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>;
 | 
			
		||||
};
 | 
			
		||||
@@ -907,6 +907,13 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
 | 
			
		||||
  02.1_iio_buffer的核心要素与体验
 | 
			
		||||
  ```
 | 
			
		||||
 | 
			
		||||
* 2024.11.08 发布IIO"子系统"
 | 
			
		||||
 | 
			
		||||
  ```shell
 | 
			
		||||
  02.2_增加iio_buffer并体验sysfs
 | 
			
		||||
  02.3_实现iio_buffer的写入
 | 
			
		||||
  ```
 | 
			
		||||
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
## 6. 联系方式
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/02.1_iio_buffer的核心要素与体验.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/02.2_增加iio_buffer并体验sysfs.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/02.3_实现iio_buffer的写入.tif
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -9,9 +9,191 @@ IIO(Industrial I/O)参考资料:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 1. 驱动框架
 | 
			
		||||
## 1. iio_buffer的核心要素与体验
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
### 1.1 问题引入
 | 
			
		||||
 | 
			
		||||
以DHT11为例,读取`/sys/bus/iio/devices/iio:device2/in_temp_input`时,读一次得到一个数据。如果想得到连续的数据,就要使用iio_buffer:
 | 
			
		||||
 | 
			
		||||
* 驱动程序内部读取多次数据,把数据存在iio_buffer里
 | 
			
		||||
* APP读取/dev/iio:deviceX节点,可以一次读到多个数据
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
以DHT11为例,它有2个通道:温度、湿度,那么读取到的数据格式是怎样的?有如下4种组合:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
使用iio_buffer读数据时,APP要解决这些问题:
 | 
			
		||||
 | 
			
		||||
* 只想读温度或湿度,或者想同时读取温度和湿度,怎么设置?
 | 
			
		||||
* 读到的数据里,怎么分辨哪些是温度、哪些是湿度?
 | 
			
		||||
 | 
			
		||||
驱动程序把数据存入iio_buffer时,驱动要解决这些问题:
 | 
			
		||||
 | 
			
		||||
* 怎么向APP表示数据的格式:比如温度占据多少bit数据、湿度占据多少bit数据
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.2 数据如何排列
 | 
			
		||||
 | 
			
		||||
在驱动程序里,对于每个通道,数据的格式如下描述:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
对于APP,可以如下操作看到这些:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.3 如何使能/禁止某个通道
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.4 如何使能/禁止iio_buffer
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.5 如何读取buffer
 | 
			
		||||
 | 
			
		||||
读取设备节点即可,比如:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
hexdump /dev/iio\:device2
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
注意:hexdump读到16个字节的数据才会打印出来,如果看到打印缓慢,这是正常的。
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 1.6 上机体验
 | 
			
		||||
 | 
			
		||||
IMX6ULL的源码:
 | 
			
		||||
 | 
			
		||||

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

 | 
			
		||||
 | 
			
		||||
## 2. 修改DHT11驱动使用iio_buffer
 | 
			
		||||
 | 
			
		||||
IMX6ULL的源码:
 | 
			
		||||
 | 
			
		||||

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

 | 
			
		||||
 | 
			
		||||
### 2.1 增加iio_buffer并体验sysfs
 | 
			
		||||
 | 
			
		||||
#### 2.1.1 驱动核心代码
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 2.1.2 buffer目录下的sysfs文件
 | 
			
		||||
 | 
			
		||||
`/sys/bus/iio/devices/iio:deviceX/buffer`目录下有如下文件:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
这几个文件对应的读写函数如下:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
调用过程:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
dht11_probe
 | 
			
		||||
	devm_iio_device_register
 | 
			
		||||
		iio_device_register
 | 
			
		||||
			ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
 | 
			
		||||
				// 对应/sys/bus/iio/devices/iio:deviceX/buffer
 | 
			
		||||
				memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
 | 
			
		||||
				buffer->buffer_group.name = "buffer";
 | 
			
		||||
				buffer->buffer_group.attrs = attr;
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 2.1.3 scan_elements目录下的sysfs文件
 | 
			
		||||
 | 
			
		||||
`/sys/bus/iio/devices/iio:deviceX/scan_elements`目录下有如下文件:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
这几个文件对应的读写函数如下:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
调用过程:
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
dht11_probe
 | 
			
		||||
	devm_iio_device_register
 | 
			
		||||
		iio_device_register
 | 
			
		||||
			ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
 | 
			
		||||
				// 对应/sys/bus/iio/devices/iio:deviceX/buffer
 | 
			
		||||
				memcpy(attr, iio_buffer_attrs, sizeof(iio_buffer_attrs));
 | 
			
		||||
				buffer->buffer_group.name = "buffer";
 | 
			
		||||
				buffer->buffer_group.attrs = attr;
 | 
			
		||||
			
 | 
			
		||||
            // 对于每一个channel
 | 
			
		||||
			for (i = 0; i < indio_dev->num_channels; i++) {
 | 
			
		||||
				ret = iio_buffer_add_channel_sysfs(indio_dev,&channels[i]);
 | 
			
		||||
            }
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 2.1.4 上机体验
 | 
			
		||||
 | 
			
		||||
需要添加如下信息:
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 2.2 实现iio_buffer的写入
 | 
			
		||||
 | 
			
		||||
#### 2.2.1 设计思路
 | 
			
		||||
 | 
			
		||||
DHT11没有中断引脚,DHT11的访问比较慢。如何启动数据的采集?最好的办法是使用内核线程。工作队列(work)的上下文就在内核线程里,我们可以使用工作队列来读DHT11。
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 2.2.2 编程要点
 | 
			
		||||
 | 
			
		||||
* 分配、设置工作队列
 | 
			
		||||
  
 | 
			
		||||
* 使能buffer后启动工作队列
 | 
			
		||||
  
 | 
			
		||||
* 工作队列里读取数据、再次启动工作队列
 | 
			
		||||
  
 | 
			
		||||
* 停止buffer后取消工作队列
 | 
			
		||||
  
 | 
			
		||||
 | 
			
		||||
#### 2.2.3 细节
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -50,6 +232,24 @@ iio_channel_start_all_cb > iio_update_buffers > __iio_update_buffers  > iio_enab
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#### 1.1 iio_buffer细节
 | 
			
		||||
 | 
			
		||||
```shell
 | 
			
		||||
echo 1 > /sys/bus/iio/devices/iio:device2/buffer/enable
 | 
			
		||||
iio_buffer_store_enable
 | 
			
		||||
    __iio_update_buffers
 | 
			
		||||
        iio_buffer_request_update
 | 
			
		||||
            iio_buffer_update_bytes_per_datum
 | 
			
		||||
                buffer->access->set_bytes_per_datum(buffer, bytes);
 | 
			
		||||
                    iio_set_bytes_per_datum_kfifo
 | 
			
		||||
                        r->bytes_per_datum = bpd;
 | 
			
		||||
             ret = buffer->access->request_update(buffer);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
### 2. 从硬件中断到APP
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241106185139079.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 53 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241106185251544.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 26 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241106185855617.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 94 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241106192827693.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 28 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241106193719202.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 38 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241106194714300.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 21 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241107103530226.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 52 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241107110327591.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.4 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241107110425775.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 21 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241107115503773.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 38 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108102322454.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 62 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108102839764.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 72 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108102940208.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.9 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108103346047.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 14 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108103459523.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 81 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108103946049.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 93 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108104137696.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 51 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108112942730.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 4.0 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108113032977.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 21 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108113327227.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 90 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108113349427.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 20 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108113544300.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 51 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108131823283.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 5.8 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108131903721.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 6.9 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								STM32MP157/doc_pic/14_IIO/pic/image-20241108131925907.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						| 
		 After Width: | Height: | Size: 6.5 KiB  | 
							
								
								
									
										500
									
								
								STM32MP157/source/A7/14_iio/01_dht11_iio_buffer_software/dht11.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -0,0 +1,500 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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>
 | 
			
		||||
 | 
			
		||||
#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 unsigned long dht11_scan_masks[] = {0x3, 0};
 | 
			
		||||
 | 
			
		||||
static const struct of_device_id dht11_dt_ids[] = {
 | 
			
		||||
	{ .compatible = "dht11", },
 | 
			
		||||
	{ }
 | 
			
		||||
};
 | 
			
		||||
MODULE_DEVICE_TABLE(of, dht11_dt_ids);
 | 
			
		||||
 | 
			
		||||
static int dht11_iio_buffer_preenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_iio_buffer_postenable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
 | 
			
		||||
 	schedule_delayed_work(&dht11->work, HZ);
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dht11_iio_buffer_postdisable(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = iio_priv(indio_dev);
 | 
			
		||||
 | 
			
		||||
	cancel_delayed_work_sync(&dht11->work);
 | 
			
		||||
	
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const struct iio_buffer_setup_ops dht11_iio_buffer_setup_ops = {
 | 
			
		||||
	.preenable = dht11_iio_buffer_preenable,
 | 
			
		||||
	.postenable = dht11_iio_buffer_postenable,
 | 
			
		||||
	.postdisable = dht11_iio_buffer_postdisable,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int dht11_iio_buffer_init(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	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_iio_buffer_setup_ops;
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dht11_work(struct work_struct *work)
 | 
			
		||||
{
 | 
			
		||||
	struct dht11 *dht11 = container_of(work, struct dht11, work.work);
 | 
			
		||||
	struct iio_dev *iio_dev = dht11->iio;
 | 
			
		||||
 | 
			
		||||
	int ret, timeres, offset;
 | 
			
		||||
	uint8_t vals[2];
 | 
			
		||||
 | 
			
		||||
	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;
 | 
			
		||||
	iio_push_to_buffers(iio_dev, vals);
 | 
			
		||||
err:
 | 
			
		||||
	dht11->num_edges = -1;
 | 
			
		||||
	mutex_unlock(&dht11->lock);
 | 
			
		||||
	
 | 
			
		||||
	if (!ret)
 | 
			
		||||
		schedule_delayed_work(&dht11->work, HZ);
 | 
			
		||||
	else
 | 
			
		||||
		schedule_work(&dht11->work.work);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
	INIT_DELAYED_WORK(&dht11->work, dht11_work);
 | 
			
		||||
 | 
			
		||||
	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_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
 | 
			
		||||
	iio->channels = dht11_chan_spec;
 | 
			
		||||
	iio->num_channels = ARRAY_SIZE(dht11_chan_spec);
 | 
			
		||||
	iio->available_scan_masks = dht11_scan_masks;
 | 
			
		||||
 | 
			
		||||
	dht11_iio_buffer_init(iio);
 | 
			
		||||
 | 
			
		||||
	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>;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,513 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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>
 | 
			
		||||
 | 
			
		||||
#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 int dht11_buffer_init(struct iio_dev *indio_dev)
 | 
			
		||||
{
 | 
			
		||||
	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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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>;
 | 
			
		||||
};
 | 
			
		||||