Skip to content

Commit 081d974

Browse files
haraldgjic23
authored andcommitted
iio: dht11: Use new function ktime_get_resolution_ns()
This cleans up the most ugly workaround in this driver. There are no functional changes yet in the decoding algorithm, but we improve the following things: * Get rid of spurious warning messages on systems with fast HRTIMER. * If the clock is not fast enough for decoding to work, we give up immediately. * In that case we return EAGAIN instead of EIO, so it's easier to discriminate causes of failure. Returning EAGAIN is somewhat controversial: It's technically correct as a faster clock might become available. OTOH once all clocks are enabled this is a permanent error. There is no ECLOCKTOOSLOW error code. Signed-off-by: Harald Geyer <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 5fbb0bc commit 081d974

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

drivers/iio/humidity/dht11.c

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/delay.h>
3434
#include <linux/gpio.h>
3535
#include <linux/of_gpio.h>
36+
#include <linux/timekeeping.h>
3637

3738
#include <linux/iio/iio.h>
3839

@@ -89,23 +90,11 @@ static unsigned char dht11_decode_byte(int *timing, int threshold)
8990
return ret;
9091
}
9192

92-
static int dht11_decode(struct dht11 *dht11, int offset)
93+
static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
9394
{
94-
int i, t, timing[DHT11_BITS_PER_READ], threshold,
95-
timeres = DHT11_SENSOR_RESPONSE;
95+
int i, t, timing[DHT11_BITS_PER_READ], threshold;
9696
unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
9797

98-
/* Calculate timestamp resolution */
99-
for (i = 1; i < dht11->num_edges; ++i) {
100-
t = dht11->edges[i].ts - dht11->edges[i - 1].ts;
101-
if (t > 0 && t < timeres)
102-
timeres = t;
103-
}
104-
if (2 * timeres > DHT11_DATA_BIT_HIGH) {
105-
pr_err("dht11: timeresolution %d too bad for decoding\n",
106-
timeres);
107-
return -EIO;
108-
}
10998
threshold = DHT11_DATA_BIT_HIGH / timeres;
11099
if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold)
111100
pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -128,7 +117,7 @@ static int dht11_decode(struct dht11 *dht11, int offset)
128117
if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
129118
return -EIO;
130119

131-
dht11->timestamp = iio_get_time_ns();
120+
dht11->timestamp = ktime_get_real_ns();
132121
if (hum_int < 20) { /* DHT22 */
133122
dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
134123
((temp_int & 0x80) ? -100 : 100);
@@ -156,7 +145,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
156145

157146
/* TODO: Consider making the handler safe for IRQ sharing */
158147
if (dht11->num_edges < DHT11_EDGES_PER_READ && dht11->num_edges >= 0) {
159-
dht11->edges[dht11->num_edges].ts = iio_get_time_ns();
148+
dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
160149
dht11->edges[dht11->num_edges++].value =
161150
gpio_get_value(dht11->gpio);
162151

@@ -172,10 +161,22 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
172161
int *val, int *val2, long m)
173162
{
174163
struct dht11 *dht11 = iio_priv(iio_dev);
175-
int ret;
164+
int ret, timeres;
176165

177166
mutex_lock(&dht11->lock);
178-
if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
167+
if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
168+
timeres = ktime_get_resolution_ns();
169+
if (DHT11_DATA_BIT_HIGH < 2 * timeres) {
170+
dev_err(dht11->dev, "timeresolution %dns too low\n",
171+
timeres);
172+
/* In theory a better clock could become available
173+
* at some point ... and there is no error code
174+
* that really fits better.
175+
*/
176+
ret = -EAGAIN;
177+
goto err;
178+
}
179+
179180
reinit_completion(&dht11->completion);
180181

181182
dht11->num_edges = 0;
@@ -210,7 +211,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
210211
ret = dht11_decode(dht11,
211212
dht11->num_edges == DHT11_EDGES_PER_READ ?
212213
DHT11_EDGES_PREAMBLE :
213-
DHT11_EDGES_PREAMBLE - 2);
214+
DHT11_EDGES_PREAMBLE - 2,
215+
timeres);
214216
if (ret)
215217
goto err;
216218
}
@@ -277,7 +279,7 @@ static int dht11_probe(struct platform_device *pdev)
277279
return -EINVAL;
278280
}
279281

280-
dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
282+
dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
281283
dht11->num_edges = -1;
282284

283285
platform_set_drvdata(pdev, iio);

0 commit comments

Comments
 (0)