Skip to contentSkip to conversation

Provide new API to get the current time resolution

PATCH In review Harald Geyer 4 participants 21 messages Apr 7, 2015 – Jul 19, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover Provide new API to get the current time resolution

Hi,

this patch series introduces a new kernel internal API to get the current time resolution. The first patch adds the implementation and the second patch updates an example driver to show the possible code simplification.

If patches 1 and 2 can't go in via the same tree, I'll resend patch 2 with more cleanup patches to dth11.c later -- the main goal for now is, to get the new code for timekeeping reviewed and merged.

changes since V1:
* dropped patch for adding wrapper code to the iio framework
* improved commit messages

Harald Geyer (2):
timekeeping: Provide new API to get the current time resolution
iio: dht11: Use new function ktime_get_resolution_ns()

 drivers/iio/humidity/dht11.c |   42 ++++++++++++++++++++++--------------------
 include/linux/timekeeping.h  |    1 +
 kernel/time/timekeeping.c    |   17 +++++++++++++++++
 3 files changed, 40 insertions(+), 20 deletions(-)
Signature · 1 line
1.7.2.5
#1 lore raw

PATCH v2 1/2 timekeeping: Provide new API to get the current time resolution

This patch series introduces a new function
u32 ktime_get_resolution_ns(void)
which allows to clean up some driver code.

In particular the IIO subsystem has a function to provide timestamps for events but no means to get their resolution. So currently the dht11 driver tries to guess the resolution in a rather messy and convoluted way. We can do much better with the new code.

This API is not designed to be exposed to user space.

This has been tested on i386, sunxi and mxs.

---
changes since V1:
Improved commit message.

 include/linux/timekeeping.h |    1 +
 kernel/time/timekeeping.c   |   17 +++++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
include/linux/timekeeping.h +1 -0
index 3eaae47..983b61e 100644
@@ -163,6 +163,7 @@ extern ktime_t ktime_get(void);
 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
 extern ktime_t ktime_get_raw(void);
+extern u32 ktime_get_resolution_ns(void);
 
 /**
  * ktime_get_real - get the real (wall-) time in ktime_t format
kernel/time/timekeeping.c +17 -0
index 91db941..8efd964 100644
@@ -586,6 +586,23 @@ ktime_t ktime_get(void)
 }
 EXPORT_SYMBOL_GPL(ktime_get);
 
+u32 ktime_get_resolution_ns(void)
+{
+	struct timekeeper *tk = &tk_core.timekeeper;
+	unsigned int seq;
+	u32 nsecs;
+
+	WARN_ON(timekeeping_suspended);
+
+	do {
+		seq = read_seqcount_begin(&tk_core.seq);
+		nsecs = tk->tkr.mult >> tk->tkr.shift;
+	} while (read_seqcount_retry(&tk_core.seq, seq));
+
+	return nsecs;
+}
+EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);
+
 static ktime_t *offsets[TK_OFFS_MAX] = {
 	[TK_OFFS_REAL]	= &tk_core.timekeeper.offs_real,
 	[TK_OFFS_BOOT]	= &tk_core.timekeeper.offs_boot,
Signature · 1 line
1.7.2.5
#4 lore raw

PATCH v2 2/2 iio: dht11: Use new function ktime_get_resolution_ns()

In reply to Harald Geyer

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.

---
changes since V1:
call ktime_get_xxx() functions directly instead of using the wrappers of the iio subsystem.

 drivers/iio/humidity/dht11.c |   42 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +22 -20
index 7d79a1a..4cb25dc 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
@@ -87,23 +88,11 @@ static unsigned char dht11_decode_byte(int *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -126,7 +115,7 @@ static int dht11_decode(struct dht11 *dht11, int offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 					((temp_int & 0x80) ? -100 : 100);
@@ -154,7 +143,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
 						gpio_get_value(dht11->gpio);
 
@@ -170,10 +159,22 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2*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;
+		}
+
 		reinit_completion(&dht11->completion);
 
 		dht11->num_edges = 0;
@@ -208,7 +209,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 		ret = dht11_decode(dht11,
 				dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -274,7 +276,7 @@ static int dht11_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 
 	platform_set_drvdata(pdev, iio);
Signature · 1 line
1.7.2.5
#2 lore raw

Re: [PATCHv2 1/2] timekeeping: Provide new API to get the current time resolution

In reply to Harald Geyer

Am 07.04.2015 um 13:12 schrieb Harald Geyer:

Quoted 48 lines

This patch series introduces a new function
u32 ktime_get_resolution_ns(void)
which allows to clean up some driver code.

In particular the IIO subsystem has a function to provide timestamps for events but no means to get their resolution. So currently the dht11 driver tries to guess the resolution in a rather messy and convoluted way. We can do much better with the new code.

This API is not designed to be exposed to user space.

This has been tested on i386, sunxi and mxs.

---
changes since V1:
Improved commit message.

 include/linux/timekeeping.h |    1 +
 kernel/time/timekeeping.c   |   17 +++++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
include/linux/timekeeping.h +1 -0
index 3eaae47..983b61e 100644
@@ -163,6 +163,7 @@ extern ktime_t ktime_get(void);
 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
 extern ktime_t ktime_get_raw(void);
+extern u32 ktime_get_resolution_ns(void);
 
 /**
  * ktime_get_real - get the real (wall-) time in ktime_t format
kernel/time/timekeeping.c +6 -0
index 91db941..8efd964 100644
@@ -586,6 +586,23 @@ ktime_t ktime_get(void)
 }
 EXPORT_SYMBOL_GPL(ktime_get);
 
+u32 ktime_get_resolution_ns(void)
+{
+	struct timekeeper *tk = &tk_core.timekeeper;
+	unsigned int seq;
+	u32 nsecs;
+

kernel/time/timekeeping.c ktime_t ktime_get(void) · line 604

Quoted 10 lines
kernel/time/timekeeping.c +10 -0
+	WARN_ON(timekeeping_suspended);
+
+	do {
+		seq = read_seqcount_begin(&tk_core.seq);
+		nsecs = tk->tkr.mult >> tk->tkr.shift;
+	} while (read_seqcount_retry(&tk_core.seq, seq));
+
+	return nsecs;
+}
+EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);

Hmm, isn't this ktime_get_raw_ns()?

Thanks,
//richard

#3 lore raw

Re: [PATCHv2 1/2] timekeeping: Provide new API to get the current time resolution

Am 08.04.2015 um 00:30 schrieb Richard Weinberger:

Quoted 53 lines

Am 07.04.2015 um 13:12 schrieb Harald Geyer:

Quoted 52 lines

This patch series introduces a new function
u32 ktime_get_resolution_ns(void)
which allows to clean up some driver code.

In particular the IIO subsystem has a function to provide timestamps for events but no means to get their resolution. So currently the dht11 driver tries to guess the resolution in a rather messy and convoluted way. We can do much better with the new code.

This API is not designed to be exposed to user space.

This has been tested on i386, sunxi and mxs.

---
changes since V1:
Improved commit message.

 include/linux/timekeeping.h |    1 +
 kernel/time/timekeeping.c   |   17 +++++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
include/linux/timekeeping.h +1 -0
index 3eaae47..983b61e 100644
@@ -163,6 +163,7 @@ extern ktime_t ktime_get(void);
 extern ktime_t ktime_get_with_offset(enum tk_offsets offs);
 extern ktime_t ktime_mono_to_any(ktime_t tmono, enum tk_offsets offs);
 extern ktime_t ktime_get_raw(void);
+extern u32 ktime_get_resolution_ns(void);
 
 /**
  * ktime_get_real - get the real (wall-) time in ktime_t format
kernel/time/timekeeping.c +10 -0
index 91db941..8efd964 100644
@@ -586,6 +586,23 @@ ktime_t ktime_get(void)
 }
 EXPORT_SYMBOL_GPL(ktime_get);
 
+u32 ktime_get_resolution_ns(void)
+{
+	struct timekeeper *tk = &tk_core.timekeeper;
+	unsigned int seq;
+	u32 nsecs;
+
+	WARN_ON(timekeeping_suspended);
+
+	do {
+		seq = read_seqcount_begin(&tk_core.seq);
Quoted 8 lines

kernel/time/timekeeping.c ktime_t ktime_get(void) · line 604

Quoted 6 lines
kernel/time/timekeeping.c +6 -0
+		nsecs = tk->tkr.mult >> tk->tkr.shift;
+	} while (read_seqcount_retry(&tk_core.seq, seq));
+
+	return nsecs;
+}
+EXPORT_SYMBOL_GPL(ktime_get_resolution_ns);

Hmm, isn't this ktime_get_raw_ns()?

Whoops, it is of course not the same.

Thanks,
//richard

maintainer
#5 lore raw

Re: [PATCHv2 2/2] iio: dht11: Use new function ktime_get_resolution_ns()

In reply to Harald Geyer
Quoted 9 lines · Harald Geyer

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.

Quoted 6 lines

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.

Looks good to me. Will wait for comments on the first patch though before taking this... clearly that one will need a few acks if I take it through IIO!

Jonathan

Quoted 109 lines

---
changes since V1:
call ktime_get_xxx() functions directly instead of using the wrappers of the iio subsystem.

 drivers/iio/humidity/dht11.c |   42 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +22 -20
index 7d79a1a..4cb25dc 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
@@ -87,23 +88,11 @@ static unsigned char dht11_decode_byte(int *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -126,7 +115,7 @@ static int dht11_decode(struct dht11 *dht11, int offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 					((temp_int & 0x80) ? -100 : 100);
@@ -154,7 +143,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
 						gpio_get_value(dht11->gpio);
 
@@ -170,10 +159,22 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2*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;
+		}
+
 		reinit_completion(&dht11->completion);
 
 		dht11->num_edges = 0;
@@ -208,7 +209,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 		ret = dht11_decode(dht11,
 				dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -274,7 +276,7 @@ static int dht11_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 
 	platform_set_drvdata(pdev, iio);
#6 lore raw

Re: [PATCHv2 2/2] iio: dht11: Use new function ktime_get_resolution_ns()

Hi Jonathan!

Quoted 15 lines · Jonathan Cameron
Quoted 14 lines · Harald Geyer

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.

Quoted 4 lines
Quoted 1 line

Looks good to me. Will wait for comments on the first patch though before taking this... clearly that one will need a few acks if I take it through IIO!

The first patch has been merged via tip tree for 4.2 (is already available in staging-next tree). Are you going to pick up this patch or do I need to resend?

Thanks,
Harald

Quoted 111 lines

Jonathan

Quoted 109 lines

---
changes since V1:
call ktime_get_xxx() functions directly instead of using the wrappers of the iio subsystem.

 drivers/iio/humidity/dht11.c |   42 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +22 -20
index 7d79a1a..4cb25dc 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
@@ -87,23 +88,11 @@ static unsigned char dht11_decode_byte(int *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -126,7 +115,7 @@ static int dht11_decode(struct dht11 *dht11, int offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 					((temp_int & 0x80) ? -100 : 100);
@@ -154,7 +143,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
 						gpio_get_value(dht11->gpio);
 
@@ -170,10 +159,22 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2*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;
+		}
+
 		reinit_completion(&dht11->completion);
 
 		dht11->num_edges = 0;
@@ -208,7 +209,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 		ret = dht11_decode(dht11,
 				dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -274,7 +276,7 @@ static int dht11_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 
 	platform_set_drvdata(pdev, iio);
#7 lore raw

Re: [PATCHv2 2/2] iio: dht11: Use new function ktime_get_resolution_ns()

Harald Geyer schrieb am 29.06.2015 um 20:59:

Quoted 23 lines

Hi Jonathan!

Quoted 15 lines · Jonathan Cameron
Quoted 14 lines · Harald Geyer

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.

Quoted 4 lines
Quoted 1 line

Looks good to me. Will wait for comments on the first patch though before taking this... clearly that one will need a few acks if I take it through IIO!

Quoted 7 lines

The first patch has been merged via tip tree for 4.2 (is already available in staging-next tree). Are you going to pick up this patch or do I need to resend?

Thanks,
Harald

Hi Harald,
there are a few small style issues, which checkpatch.pl --strict should show you. It would be good to have them eliminated before this patch gets applied.
Thanks,
Hartmut

Quoted 116 lines
Quoted 104 lines

Jonathan

Quoted 103 lines

---
changes since V1:
call ktime_get_xxx() functions directly instead of using the wrappers of the iio subsystem.

 drivers/iio/humidity/dht11.c |   42 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +21 -19
index 7d79a1a..4cb25dc 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
@@ -87,23 +88,11 @@ static unsigned char dht11_decode_byte(int *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -126,7 +115,7 @@ static int dht11_decode(struct dht11 *dht11, int offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 					((temp_int & 0x80) ? -100 : 100);
@@ -154,7 +143,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
 						gpio_get_value(dht11->gpio);
 
@@ -170,10 +159,22 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2*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;
+		}
+
 		reinit_completion(&dht11->completion);
 
 		dht11->num_edges = 0;
@@ -208,7 +209,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 		ret = dht11_decode(dht11,
 				dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -274,7 +276,7 @@ static int dht11_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
Quoted 7 lines

drivers/iio/humidity/dht11.c static int dht11_probe(struct platform_device *pdev) · line 282

Quoted 6 lines
drivers/iio/humidity/dht11.c +1 -1
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 
 	platform_set_drvdata(pdev, iio);

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw

Re: [PATCHv2 2/2] iio: dht11: Use new function ktime_get_resolution_ns()

Quoted 4 lines · Hartmut Knaack

Harald Geyer schrieb am 29.06.2015 um 20:59:

Quoted 3 lines

Hi Jonathan!

Jonathan Cameron writes:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Looks good to me. Will wait for comments on the first patch though before taking this... clearly that one will need a few acks if I take it through IIO!

The first patch has been merged via tip tree for 4.2 (is already available
in staging-next tree). Are you going to pick up this patch or do I need to resend?

Quoted 7 lines
Quoted 3 lines

Thanks,
Harald

Hi Harald,
there are a few small style issues, which checkpatch.pl --strict should show you.

Thanks for pointing out --strict - didn't know about that yet.

I get the following output:

CHECK: spaces preferred around that '*' (ctx:VxV)
#96: FILE: drivers/iio/humidity/dht11.c:167:

+               if (DHT11_DATA_BIT_HIGH < 2*timeres) {
                                           ^

With this one I disagree. Omitting the spaces makes the code easier to read, because it reflects the relative binding power of operators involed. This style (omitting spaces around * if there is a +, <, etc in the same experession) is used throughout the driver. I'd change this only if there is consensus, that this really is the preferred way.

CHECK: Alignment should match open parenthesis
#98: FILE: drivers/iio/humidity/dht11.c:169:
+ dev_err(dht11->dev, "timeresolution %dns too
low\n",
+ timeres);

Ok, I'll post a patch that updates the driver to this style and update the above patch acordingly.

Thanks,
Harald

Quoted 26 lines

Thanks,
Hartmut

Quoted 23 lines
Quoted 23 lines

Jonathan

Quoted 22 lines

---
changes since V1:
call ktime_get_xxx() functions directly instead of using the wrappers of the iio subsystem.

 drivers/iio/humidity/dht11.c |   42
 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +1 -0
index 7d79a1a..4cb25dc 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/humidity/dht11.c static unsigned char dht11_decode_byte(int · line 92

Quoted 7 lines
drivers/iio/humidity/dht11.c +1 -1
@@ -87,23 +88,11 @@ static unsigned char dht11_decode_byte(int
 *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int

timeres)

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
drivers/iio/humidity/dht11.c +1 -13
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
drivers/iio/humidity/dht11.c +1 -1
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -126,7 +115,7 @@ static int dht11_decode(struct dht11 *dht11, int
 offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
drivers/iio/humidity/dht11.c +1 -1
 					((temp_int & 0x80) ? -100 : 100);
@@ -154,7 +143,7 @@ static irqreturn_t dht11_handle_irq(int irq, void
 *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
drivers/iio/humidity/dht11.c +1 -1
 						gpio_get_value(dht11->gpio);
@@ -170,10 +159,22 @@ static int dht11_read_raw(struct iio_dev
 *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/humidity/dht11.c static int dht11_read_raw(struct iio_dev *iio_dev,

Quoted 3 lines
drivers/iio/humidity/dht11.c +1 -1
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns())

{

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/humidity/dht11.c +12 -0
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2*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;
+		}
+
 		reinit_completion(&dht11->completion);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/humidity/dht11.c static int dht11_read_raw(struct iio_dev

Quoted 2 lines
drivers/iio/humidity/dht11.c +0 -0
 		dht11->num_edges = 0;
@@ -208,7 +209,8 @@ static int dht11_read_raw(struct iio_dev

*iio_dev,

Quoted 26 lines
Quoted 26 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/humidity/dht11.c +2 -1
 		ret = dht11_decode(dht11,
 				dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -274,7 +276,7 @@ static int dht11_probe(struct platform_device
 *pdev)
 		return -EINVAL;
 	}
 
Quoted 7 lines
Quoted 6 lines
drivers/iio/humidity/dht11.c +1 -1
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 

platform_set_drvdata(pdev, iio);

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#9 lore raw

Re: [PATCHv2 2/2] iio: dht11: Use new function ktime_get_resolution_ns()

Am 30.06.2015 um 21:37 schrieb [email protected]:

Quoted 20 lines
Quoted 4 lines · Hartmut Knaack

Harald Geyer schrieb am 29.06.2015 um 20:59:

Quoted 3 lines

Hi Jonathan!

Jonathan Cameron writes:

[...]

Quoted 13 lines
Quoted 12 lines
Quoted 3 lines

Looks good to me. Will wait for comments on the first patch though before taking this... clearly that one will need a few acks if I take it through IIO!

The first patch has been merged via tip tree for 4.2 (is already available
in staging-next tree). Are you going to pick up this patch or do I need to resend?

Thanks,
Harald

Quoted 5 lines
Quoted 3 lines

Hi Harald,
there are a few small style issues, which checkpatch.pl --strict should show you.

Thanks for pointing out --strict - didn't know about that yet.

Don't take checkpatch.pl too seriously.

Thanks,
//richard

#10 lore raw

Re: [PATCHv2 2/2] iio: dht11: Use new function ktime_get_resolution_ns()

In reply to [email protected]

[email protected] schrieb am 30.06.2015 um 21:37:

Quoted 30 lines
Quoted 4 lines · Hartmut Knaack

Harald Geyer schrieb am 29.06.2015 um 20:59:

Quoted 3 lines

Hi Jonathan!

Jonathan Cameron writes:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Looks good to me. Will wait for comments on the first patch though before taking this... clearly that one will need a few acks if I take it through IIO!

The first patch has been merged via tip tree for 4.2 (is already available
in staging-next tree). Are you going to pick up this patch or do I need to resend?

Quoted 7 lines
Quoted 3 lines

Thanks,
Harald

Hi Harald,
there are a few small style issues, which checkpatch.pl --strict should show you.

Thanks for pointing out --strict - didn't know about that yet.

I get the following output:

CHECK: spaces preferred around that '*' (ctx:VxV)
#96: FILE: drivers/iio/humidity/dht11.c:167:

Quoted 8 lines
+               if (DHT11_DATA_BIT_HIGH < 2*timeres) {
                                           ^

With this one I disagree. Omitting the spaces makes the code easier to read, because it reflects the relative binding power of operators involed. This style (omitting spaces around * if there is a +, <, etc in the same experession) is used throughout the driver. I'd change this only if there is consensus, that this really is the preferred way.

I would prefer to make use of parenthesis to reflect a precedence instead of violating coding style guidelines.
Honestly, it didn't come to my mind that you left out those spaces on purpose.

Quoted 149 lines

CHECK: Alignment should match open parenthesis
#98: FILE: drivers/iio/humidity/dht11.c:169:
+ dev_err(dht11->dev, "timeresolution %dns too
low\n",
+ timeres);

Ok, I'll post a patch that updates the driver to this style and update the above patch acordingly.

Thanks,
Harald

Quoted 26 lines

Thanks,
Hartmut

Quoted 23 lines
Quoted 23 lines

Jonathan

Quoted 22 lines

---
changes since V1:
call ktime_get_xxx() functions directly instead of using the wrappers of the iio subsystem.

 drivers/iio/humidity/dht11.c |   42
 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +1 -0
index 7d79a1a..4cb25dc 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/humidity/dht11.c static unsigned char dht11_decode_byte(int · line 92

Quoted 7 lines
drivers/iio/humidity/dht11.c +1 -1
@@ -87,23 +88,11 @@ static unsigned char dht11_decode_byte(int
 *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int

timeres)

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
drivers/iio/humidity/dht11.c +1 -13
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
drivers/iio/humidity/dht11.c +1 -1
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -126,7 +115,7 @@ static int dht11_decode(struct dht11 *dht11, int
 offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
drivers/iio/humidity/dht11.c +1 -1
 					((temp_int & 0x80) ? -100 : 100);
@@ -154,7 +143,7 @@ static irqreturn_t dht11_handle_irq(int irq, void
 *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
drivers/iio/humidity/dht11.c +1 -1
 						gpio_get_value(dht11->gpio);
@@ -170,10 +159,22 @@ static int dht11_read_raw(struct iio_dev
 *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/humidity/dht11.c static int dht11_read_raw(struct iio_dev *iio_dev,

Quoted 3 lines
drivers/iio/humidity/dht11.c +1 -1
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns())

{

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/humidity/dht11.c +12 -0
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2*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;
+		}
+
 		reinit_completion(&dht11->completion);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/humidity/dht11.c static int dht11_read_raw(struct iio_dev

Quoted 2 lines
drivers/iio/humidity/dht11.c +0 -0
 		dht11->num_edges = 0;
@@ -208,7 +209,8 @@ static int dht11_read_raw(struct iio_dev

*iio_dev,

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
drivers/iio/humidity/dht11.c +3 -2
 		ret = dht11_decode(dht11,
 				dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -274,7 +276,7 @@ static int dht11_probe(struct platform_device
 *pdev)
 		return -EINVAL;
 	}
 
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

platform_set_drvdata(pdev, iio);

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#11 lore raw

Re: [PATCHv2 2/2] iio: dht11: Use new function ktime_get_resolution_ns()

In reply to [email protected]
Quoted 30 lines · On 30/06/15 20:37, [email protected] wrote:
Quoted 4 lines · Hartmut Knaack

Harald Geyer schrieb am 29.06.2015 um 20:59:

Quoted 3 lines

Hi Jonathan!

Jonathan Cameron writes:

[...]

Quoted 9 lines
Quoted 9 lines
Quoted 3 lines

Looks good to me. Will wait for comments on the first patch though before taking this... clearly that one will need a few acks if I take it through IIO!

The first patch has been merged via tip tree for 4.2 (is already available
in staging-next tree). Are you going to pick up this patch or do I need to resend?

Quoted 7 lines
Quoted 3 lines

Thanks,
Harald

Hi Harald,
there are a few small style issues, which checkpatch.pl --strict should show you.

Thanks for pointing out --strict - didn't know about that yet.

I get the following output:

CHECK: spaces preferred around that '*' (ctx:VxV)
#96: FILE: drivers/iio/humidity/dht11.c:167:

Quoted 8 lines
+               if (DHT11_DATA_BIT_HIGH < 2*timeres) {
                                           ^

With this one I disagree. Omitting the spaces makes the code easier to read, because it reflects the relative binding power of operators involed. This style (omitting spaces around * if there is a +, <, etc in the same experession) is used throughout the driver. I'd change this only if there is consensus, that this really is the preferred way.

Personal taste wise I agree with you, but conventions are conventions and the kernel way is spaces around binary operators to distinguish them from unary operators...

I'll only get a newbie patch 'fixing' this otherwise, so please do change it!

Quoted 144 lines

CHECK: Alignment should match open parenthesis
#98: FILE: drivers/iio/humidity/dht11.c:169:
+ dev_err(dht11->dev, "timeresolution %dns too
low\n",
+ timeres);

Ok, I'll post a patch that updates the driver to this style and update the above patch acordingly.

Thanks,
Harald

Quoted 26 lines

Thanks,
Hartmut

Quoted 23 lines
Quoted 23 lines

Jonathan

Quoted 22 lines

---
changes since V1:
call ktime_get_xxx() functions directly instead of using the wrappers of the iio subsystem.

 drivers/iio/humidity/dht11.c |   42
 ++++++++++++++++++++++--------------------
 1 files changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +1 -0
index 7d79a1a..4cb25dc 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

drivers/iio/humidity/dht11.c static unsigned char dht11_decode_byte(int · line 92

Quoted 7 lines
drivers/iio/humidity/dht11.c +1 -1
@@ -87,23 +88,11 @@ static unsigned char dht11_decode_byte(int
 *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int

timeres)

Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
Quoted 49 lines
drivers/iio/humidity/dht11.c +1 -13
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
drivers/iio/humidity/dht11.c +1 -1
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -126,7 +115,7 @@ static int dht11_decode(struct dht11 *dht11, int
 offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
drivers/iio/humidity/dht11.c +1 -1
 					((temp_int & 0x80) ? -100 : 100);
@@ -154,7 +143,7 @@ static irqreturn_t dht11_handle_irq(int irq, void
 *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
drivers/iio/humidity/dht11.c +1 -1
 						gpio_get_value(dht11->gpio);
@@ -170,10 +159,22 @@ static int dht11_read_raw(struct iio_dev
 *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/humidity/dht11.c static int dht11_read_raw(struct iio_dev *iio_dev,

Quoted 3 lines
drivers/iio/humidity/dht11.c +1 -1
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns())

{

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/humidity/dht11.c +12 -0
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2*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;
+		}
+
 		reinit_completion(&dht11->completion);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

drivers/iio/humidity/dht11.c static int dht11_read_raw(struct iio_dev

Quoted 2 lines
drivers/iio/humidity/dht11.c +0 -0
 		dht11->num_edges = 0;
@@ -208,7 +209,8 @@ static int dht11_read_raw(struct iio_dev

*iio_dev,

Quoted 26 lines
Quoted 26 lines
Quoted 14 lines
Quoted 14 lines
drivers/iio/humidity/dht11.c +2 -1
 		ret = dht11_decode(dht11,
 				dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -274,7 +276,7 @@ static int dht11_probe(struct platform_device
 *pdev)
 		return -EINVAL;
 	}
 
Quoted 7 lines
Quoted 6 lines
drivers/iio/humidity/dht11.c +1 -1
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 

platform_set_drvdata(pdev, iio);

--
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#12 lore raw

Re: [PATCHv2 2/2] iio: dht11: Use new function ktime_get_resolution_ns()

Quoted 10 lines · Jonathan Cameron
Quoted 9 lines · On 30/06/15 20:37, [email protected] wrote:

Thanks for pointing out --strict - didn't know about that yet.

I get the following output:

CHECK: spaces preferred around that '*' (ctx:VxV)
#96: FILE: drivers/iio/humidity/dht11.c:167:

+               if (DHT11_DATA_BIT_HIGH < 2*timeres) {
                                           ^
Quoted 4 lines
Quoted 4 lines

With this one I disagree. Omitting the spaces makes the code easier to read, because it reflects the relative binding power of operators involed.
This style (omitting spaces around * if there is a +, <, etc in the

same

Quoted 1 line
Quoted 1 line

experession) is used throughout the driver. I'd change this only if

there

Quoted 6 lines
Quoted 1 line

is consensus, that this really is the preferred way.

Personal taste wise I agree with you, but conventions are conventions and the kernel way is spaces around binary operators to distinguish them from unary operators...

I'll only get a newbie patch 'fixing' this otherwise, so please do

change

Quoted 1 line

it!

Of course you have the last word on this. As long as the newbies are competent enough to really only do whitespace changes, I'd be tempted to wait for them to do the work. Alas I really want the original patch merged soon so I will do the obfuscation myself.

Thanks,
Harald the newbie :)

#13 lore raw

PATCH v3 1/4 iio: dht11: whitespace changes to make checkpatch.pl --strict happy

In reply to Jonathan Cameron

* add spaces around binary operators in cases where it reduces readability
* align multiline statements around opening parenthesis

	in Message-ID: <[email protected]>
---
 drivers/iio/humidity/dht11.c |   25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
drivers/iio/humidity/dht11.c +13 -12
index 7d79a1a..343d26a 100644
@@ -46,7 +46,8 @@
  * 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)
+#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
+			      DHT11_EDGES_PREAMBLE + 1)
 
 /* Data transmission timing (nano seconds) */
 #define DHT11_START_TRANSMISSION	18  /* ms */
@@ -95,24 +96,24 @@ static int dht11_decode(struct dht11 *dht11, int offset)
 
 	/* Calculate timestamp resolution */
 	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
+		t = dht11->edges[i].ts - dht11->edges[i - 1].ts;
 		if (t > 0 && t < timeres)
 			timeres = t;
 	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
+	if (2 * timeres > DHT11_DATA_BIT_HIGH) {
 		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
+		       timeres);
 		return -EIO;
 	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
-	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
+	if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold)
 		pr_err("dht11: WARNING: decoding ambiguous\n");
 
 	/* scale down with timeres and check validity */
 	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)
+		t = dht11->edges[offset + 2 * i + 2].ts -
+			dht11->edges[offset + 2 * i + 1].ts;
+		if (!dht11->edges[offset + 2 * i + 1].value)
 			return -EIO;  /* lost synchronisation */
 		timing[i] = t / timeres;
 	}
@@ -166,7 +167,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
 }
 
 static int dht11_read_raw(struct iio_dev *iio_dev,
-			const struct iio_chan_spec *chan,
+			  const struct iio_chan_spec *chan,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
@@ -192,13 +193,13 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			goto err;
 
 		ret = wait_for_completion_killable_timeout(&dht11->completion,
-								 HZ);
+							   HZ);
 
 		free_irq(dht11->irq, iio_dev);
 
 		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 			dev_err(&iio_dev->dev,
-					"Only %d signal edges detected\n",
+				"Only %d signal edges detected\n",
 					dht11->num_edges);
 			ret = -ETIMEDOUT;
 		}
@@ -206,7 +207,7 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			goto err;
 
 		ret = dht11_decode(dht11,
-				dht11->num_edges == DHT11_EDGES_PER_READ ?
+				   dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
 					DHT11_EDGES_PREAMBLE - 2);
 		if (ret)
Signature · 1 line
1.7.10.4
#14 lore raw

PATCH v3 2/4 iio: dht11: add comment to make checkpatch.pl --strict happy

Explain why the driver needs a mutex.

---
 drivers/iio/humidity/dht11.c |    1 +
 1 file changed, 1 insertion(+)
drivers/iio/humidity/dht11.c +1 -0
index 343d26a..bb5ad57 100644
@@ -63,6 +63,7 @@ struct dht11 {
 	int				irq;
 
 	struct completion		completion;
+	/* The iio sysfs interface doesn't prevent concurrent reads: */
 	struct mutex			lock;
 
 	s64				timestamp;
Signature · 1 line
1.7.10.4
#16 lore raw

PATCH v3 3/4 iio: dht11: avoid multiple assignments to make checkpatch.pl --strict happy

In reply to Harald Geyer

We just do the assignments in two steps.

---
 drivers/iio/humidity/dht11.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/humidity/dht11.c +2 -1
index bb5ad57..91976e0 100644
@@ -263,9 +263,10 @@ static int dht11_probe(struct platform_device *pdev)
 	dht11 = iio_priv(iio);
 	dht11->dev = dev;
 
-	dht11->gpio = ret = of_get_gpio(node, 0);
+	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;
Signature · 1 line
1.7.10.4
#18 lore raw

PATCH v3 4/4 iio: dht11: Use new function ktime_get_resolution_ns()

In reply to Harald Geyer

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.

---
 drivers/iio/humidity/dht11.c |   42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +22 -20
index 91976e0..1165b1c 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
@@ -89,23 +90,11 @@ static unsigned char dht11_decode_byte(int *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i - 1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2 * timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-		       timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold)
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -128,7 +117,7 @@ static int dht11_decode(struct dht11 *dht11, int offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 					((temp_int & 0x80) ? -100 : 100);
@@ -156,7 +145,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
 						gpio_get_value(dht11->gpio);
 
@@ -172,10 +161,22 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2 * 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;
+		}
+
 		reinit_completion(&dht11->completion);
 
 		dht11->num_edges = 0;
@@ -210,7 +211,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 		ret = dht11_decode(dht11,
 				   dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -277,7 +279,7 @@ static int dht11_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 
 	platform_set_drvdata(pdev, iio);
Signature · 1 line
1.7.10.4
maintainer Applied
#20 lore raw

Re: [PATCHv3 1/4] iio: dht11: whitespace changes to make checkpatch.pl --strict happy

In reply to Harald Geyer
Quoted 7 lines · Harald Geyer

* add spaces around binary operators in cases where it reduces readability
* align multiline statements around opening parenthesis

	in Message-ID: <[email protected]>

Applied to the togreg branch of iio.git

Thanks,

Jonathan

Quoted 85 lines
---
 drivers/iio/humidity/dht11.c |   25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
drivers/iio/humidity/dht11.c +13 -12
index 7d79a1a..343d26a 100644
@@ -46,7 +46,8 @@
  * 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)
+#define DHT11_EDGES_PER_READ (2 * DHT11_BITS_PER_READ + \
+			      DHT11_EDGES_PREAMBLE + 1)
 
 /* Data transmission timing (nano seconds) */
 #define DHT11_START_TRANSMISSION	18  /* ms */
@@ -95,24 +96,24 @@ static int dht11_decode(struct dht11 *dht11, int offset)
 
 	/* Calculate timestamp resolution */
 	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i-1].ts;
+		t = dht11->edges[i].ts - dht11->edges[i - 1].ts;
 		if (t > 0 && t < timeres)
 			timeres = t;
 	}
-	if (2*timeres > DHT11_DATA_BIT_HIGH) {
+	if (2 * timeres > DHT11_DATA_BIT_HIGH) {
 		pr_err("dht11: timeresolution %d too bad for decoding\n",
-			timeres);
+		       timeres);
 		return -EIO;
 	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
-	if (DHT11_DATA_BIT_LOW/timeres + 1 >= threshold)
+	if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold)
 		pr_err("dht11: WARNING: decoding ambiguous\n");
 
 	/* scale down with timeres and check validity */
 	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)
+		t = dht11->edges[offset + 2 * i + 2].ts -
+			dht11->edges[offset + 2 * i + 1].ts;
+		if (!dht11->edges[offset + 2 * i + 1].value)
 			return -EIO;  /* lost synchronisation */
 		timing[i] = t / timeres;
 	}
@@ -166,7 +167,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
 }
 
 static int dht11_read_raw(struct iio_dev *iio_dev,
-			const struct iio_chan_spec *chan,
+			  const struct iio_chan_spec *chan,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
@@ -192,13 +193,13 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			goto err;
 
 		ret = wait_for_completion_killable_timeout(&dht11->completion,
-								 HZ);
+							   HZ);
 
 		free_irq(dht11->irq, iio_dev);
 
 		if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) {
 			dev_err(&iio_dev->dev,
-					"Only %d signal edges detected\n",
+				"Only %d signal edges detected\n",
 					dht11->num_edges);
 			ret = -ETIMEDOUT;
 		}
@@ -206,7 +207,7 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			goto err;
 
 		ret = dht11_decode(dht11,
-				dht11->num_edges == DHT11_EDGES_PER_READ ?
+				   dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
 					DHT11_EDGES_PREAMBLE - 2);
 		if (ret)
maintainer Applied
#15 lore raw

Re: [PATCHv3 2/4] iio: dht11: add comment to make checkpatch.pl --strict happy

In reply to Harald Geyer
Quoted 3 lines · Harald Geyer

Explain why the driver needs a mutex.

Applied to the togreg branch of iio.git.

Thanks,

Jonathan

Quoted 17 lines
---
 drivers/iio/humidity/dht11.c |    1 +
 1 file changed, 1 insertion(+)
drivers/iio/humidity/dht11.c +1 -0
index 343d26a..bb5ad57 100644
@@ -63,6 +63,7 @@ struct dht11 {
 	int				irq;
 
 	struct completion		completion;
+	/* The iio sysfs interface doesn't prevent concurrent reads: */
 	struct mutex			lock;
 
 	s64				timestamp;
maintainer Applied
#17 lore raw

Re: [PATCHv3 3/4] iio: dht11: avoid multiple assignments to make checkpatch.pl --strict happy

In reply to Harald Geyer
Quoted 3 lines · Harald Geyer

We just do the assignments in two steps.

Applied to the togreg branch of iio.git.

Thanks,

Jonathan

Quoted 21 lines
---
 drivers/iio/humidity/dht11.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/iio/humidity/dht11.c +2 -1
index bb5ad57..91976e0 100644
@@ -263,9 +263,10 @@ static int dht11_probe(struct platform_device *pdev)
 	dht11 = iio_priv(iio);
 	dht11->dev = dev;
 
-	dht11->gpio = ret = of_get_gpio(node, 0);
+	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;
maintainer Applied
#19 lore raw

Re: [PATCHv3 4/4] iio: dht11: Use new function ktime_get_resolution_ns()

In reply to Harald Geyer
Quoted 9 lines · Harald Geyer

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.

Quoted 6 lines

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.

Applied to the togreg branch of iio.git. Thanks,

Jonathan

Quoted 105 lines
---
 drivers/iio/humidity/dht11.c |   42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)
drivers/iio/humidity/dht11.c +22 -20
index 91976e0..1165b1c 100644
@@ -33,6 +33,7 @@
 #include <linux/delay.h>
 #include <linux/gpio.h>
 #include <linux/of_gpio.h>
+#include <linux/timekeeping.h>
 
 #include <linux/iio/iio.h>
 
@@ -89,23 +90,11 @@ static unsigned char dht11_decode_byte(int *timing, int threshold)
 	return ret;
 }
 
-static int dht11_decode(struct dht11 *dht11, int offset)
+static int dht11_decode(struct dht11 *dht11, int offset, int timeres)
 {
-	int i, t, timing[DHT11_BITS_PER_READ], threshold,
-		timeres = DHT11_SENSOR_RESPONSE;
+	int i, t, timing[DHT11_BITS_PER_READ], threshold;
 	unsigned char temp_int, temp_dec, hum_int, hum_dec, checksum;
 
-	/* Calculate timestamp resolution */
-	for (i = 1; i < dht11->num_edges; ++i) {
-		t = dht11->edges[i].ts - dht11->edges[i - 1].ts;
-		if (t > 0 && t < timeres)
-			timeres = t;
-	}
-	if (2 * timeres > DHT11_DATA_BIT_HIGH) {
-		pr_err("dht11: timeresolution %d too bad for decoding\n",
-		       timeres);
-		return -EIO;
-	}
 	threshold = DHT11_DATA_BIT_HIGH / timeres;
 	if (DHT11_DATA_BIT_LOW / timeres + 1 >= threshold)
 		pr_err("dht11: WARNING: decoding ambiguous\n");
@@ -128,7 +117,7 @@ static int dht11_decode(struct dht11 *dht11, int offset)
 	if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum)
 		return -EIO;
 
-	dht11->timestamp = iio_get_time_ns();
+	dht11->timestamp = ktime_get_real_ns();
 	if (hum_int < 20) {  /* DHT22 */
 		dht11->temperature = (((temp_int & 0x7f) << 8) + temp_dec) *
 					((temp_int & 0x80) ? -100 : 100);
@@ -156,7 +145,7 @@ static irqreturn_t dht11_handle_irq(int irq, void *data)
 
 	/* 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 = iio_get_time_ns();
+		dht11->edges[dht11->num_edges].ts = ktime_get_real_ns();
 		dht11->edges[dht11->num_edges++].value =
 						gpio_get_value(dht11->gpio);
 
@@ -172,10 +161,22 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 			int *val, int *val2, long m)
 {
 	struct dht11 *dht11 = iio_priv(iio_dev);
-	int ret;
+	int ret, timeres;
 
 	mutex_lock(&dht11->lock);
-	if (dht11->timestamp + DHT11_DATA_VALID_TIME < iio_get_time_ns()) {
+	if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_real_ns()) {
+		timeres = ktime_get_resolution_ns();
+		if (DHT11_DATA_BIT_HIGH < 2 * 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;
+		}
+
 		reinit_completion(&dht11->completion);
 
 		dht11->num_edges = 0;
@@ -210,7 +211,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
 		ret = dht11_decode(dht11,
 				   dht11->num_edges == DHT11_EDGES_PER_READ ?
 					DHT11_EDGES_PREAMBLE :
-					DHT11_EDGES_PREAMBLE - 2);
+					DHT11_EDGES_PREAMBLE - 2,
+				timeres);
 		if (ret)
 			goto err;
 	}
@@ -277,7 +279,7 @@ static int dht11_probe(struct platform_device *pdev)
 		return -EINVAL;
 	}
 
-	dht11->timestamp = iio_get_time_ns() - DHT11_DATA_VALID_TIME - 1;
+	dht11->timestamp = ktime_get_real_ns() - DHT11_DATA_VALID_TIME - 1;
 	dht11->num_edges = -1;
 
 	platform_set_drvdata(pdev, iio);