From: Jonathan Cameron <jic23-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Harald Geyer <harald-95f8Dae0BrPYtjvyW6yDsg@public.gmane.org>,
Rob Herring <robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>,
Mark Rutland <mark.rutland-5wv7dgnIgG8@public.gmane.org>,
Liam Girdwood <lgirdwood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>,
Mark Brown <broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Cc: Lars-Peter Clausen <lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org>,
linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH 2/2] [RFC] iio: dht11: Add optional support for supply control via regulator framework
Date: Sat, 25 Feb 2017 16:16:30 +0000 [thread overview]
Message-ID: <84dd6f7e-3bd3-edcd-0bda-81a80de35f3c@kernel.org> (raw)
In-Reply-To: <1487869613-11927-2-git-send-email-harald-95f8Dae0BrPYtjvyW6yDsg@public.gmane.org>
On 23/02/17 17:06, Harald Geyer wrote:
> If a supply is provided via DT, release the supply whenever we don't read
> the sensor. Also use notifications to track the actual status of the
> supply to ensure that we meet the timing requirements stated in the
> datasheet.
>
> This change is motivated by the hope that these sensors will work more
> reliably if power-cycled regularly.
>
> Signed-off-by: Harald Geyer <harald-95f8Dae0BrPYtjvyW6yDsg@public.gmane.org>
I'd certainly rather this was driven by the powersaving argument but I guess
it's an added bonus if we happen to improve on the hardware hangs.
Minor comment inline. Looks good to me.
Jonathan
> ---
> This is an RFC this time, because I want to run extensive long term tests
> with DHT11 and DHT22 before officially proposing this for inclusion. I
> have learned my lessons with this quirky bits of HW... :(
>
> However since this will take a lot of time (mostly physical, but also
> man hours), I'd like to get review comments now instead of risking a
> second iteration of long term tests.
>
> .../devicetree/bindings/iio/humidity/dht11.txt | 10 ++++
> drivers/iio/humidity/dht11.c | 57 ++++++++++++++++++++--
> 2 files changed, 64 insertions(+), 3 deletions(-)
>
> diff --git a/Documentation/devicetree/bindings/iio/humidity/dht11.txt b/Documentation/devicetree/bindings/iio/humidity/dht11.txt
> index ecc24c19..ab9ea18 100644
> --- a/Documentation/devicetree/bindings/iio/humidity/dht11.txt
> +++ b/Documentation/devicetree/bindings/iio/humidity/dht11.txt
> @@ -6,9 +6,19 @@ Required properties:
> line, see "gpios property" in
> Documentation/devicetree/bindings/gpio/gpio.txt.
>
> +Optional properties:
> + - vdd-supply: phandle to the regulator node, for details see
> + Documentation/devicetree/bindings/regulator/regulator.txt
> + On Linux the driver disables the regulator after 4 seconds of
> + inactivity, to save power and to ensure that the hardware is
> + reset regularly, because it was found to randomly stop responding
> + otherwise. However for this to work all other consumers of the
> + regulator must cooperate (disable the regulator when possible too).
> +
> Example:
>
> humidity_sensor {
> compatible = "dht11";
> gpios = <&gpio0 6 0>;
> + vdd-supply = <&sensor_supply>;
> }
> diff --git a/drivers/iio/humidity/dht11.c b/drivers/iio/humidity/dht11.c
> index 2a22ad9..5bce712 100644
> --- a/drivers/iio/humidity/dht11.c
> +++ b/drivers/iio/humidity/dht11.c
> @@ -34,12 +34,16 @@
> #include <linux/gpio.h>
> #include <linux/of_gpio.h>
> #include <linux/timekeeping.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/notifier.h>
>
> #include <linux/iio/iio.h>
>
> #define DRIVER_NAME "dht11"
>
> #define DHT11_DATA_VALID_TIME 2000000000 /* 2s in ns */
> +#define DHT11_POWER_ON_TIME 2000000000 /* must be less the MAX_INT */
> +#define DHT11_POWEROFF_DELAY 4000 /* ms */
>
> #define DHT11_EDGES_PREAMBLE 2
> #define DHT11_BITS_PER_READ 40
> @@ -84,6 +88,10 @@ struct dht11 {
> int gpio;
> int irq;
>
> + struct regulator *supply;
> + struct notifier_block nb;
> + s64 timestamp_enabled;
> +
> struct completion completion;
> /* The iio sysfs interface doesn't prevent concurrent reads: */
> struct mutex lock;
> @@ -97,6 +105,21 @@ struct dht11 {
> struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ];
> };
>
> +static int dht11_regulator_update(struct notifier_block *nb,
> + unsigned long event,
> + void *ignored)
> +{
> + struct dht11 *dht11 = container_of(nb, struct dht11, nb);
> +
> + if (event & REGULATOR_EVENT_DISABLE)
> + dht11->timestamp_enabled = -1;
> + else if (event & REGULATOR_EVENT_ENABLE)
> + if (dht11->timestamp_enabled == -1)
This nesting is a bit ugly, why not combine the else if and the if?
> + dht11->timestamp_enabled = ktime_get_boot_ns();
> +
> + return NOTIFY_OK;
> +}
> +
> #ifdef CONFIG_DYNAMIC_DEBUG
> /*
> * dht11_edges_print: show the data as actually received by the
> @@ -203,9 +226,22 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
> {
> struct dht11 *dht11 = iio_priv(iio_dev);
> int ret, timeres, offset;
> + s64 time;
>
> mutex_lock(&dht11->lock);
> - if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) {
> + time = ktime_get_boot_ns();
> + if (dht11->timestamp + DHT11_DATA_VALID_TIME < time) {
> + if (dht11->supply) {
> + ret = regulator_enable(dht11->supply);
> + if (ret) {
> + dev_err(dht11->dev, "Can't enable supply\n");
> + goto err_reg;
> + }
> + time -= dht11->timestamp_enabled + DHT11_POWER_ON_TIME;
> + if (time < 0)
> + msleep(((int)(-time)) / 1000000);
> + }
> +
> timeres = ktime_get_resolution_ns();
> dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres);
> if (timeres > DHT11_MIN_TIMERES) {
> @@ -266,8 +302,13 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
> break;
> }
>
> +err:
> + if (dht11->supply)
> + regulator_disable_deferred(dht11->supply,
> + DHT11_POWEROFF_DELAY);
> +
> if (ret)
> - goto err;
> + goto err_reg;
> }
>
> ret = IIO_VAL_INT;
> @@ -277,7 +318,8 @@ static int dht11_read_raw(struct iio_dev *iio_dev,
> *val = dht11->humidity;
> else
> ret = -EINVAL;
> -err:
> +
> +err_reg:
> dht11->num_edges = -1;
> mutex_unlock(&dht11->lock);
> return ret;
> @@ -332,6 +374,15 @@ static int dht11_probe(struct platform_device *pdev)
> return -EINVAL;
> }
>
> + dht11->timestamp_enabled = -1;
> + dht11->supply = devm_regulator_get(dev, "vdd");
> + if (IS_ERR(dht11->supply)) {
> + dht11->supply = NULL;
> + } else {
> + dht11->nb.notifier_call = &dht11_regulator_update;
> + devm_regulator_register_notifier(dht11->supply, &dht11->nb);
> + }
> +
> dht11->timestamp = ktime_get_boot_ns() - DHT11_DATA_VALID_TIME - 1;
> dht11->num_edges = -1;
>
>
next prev parent reply other threads:[~2017-02-25 16:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-02-23 17:06 [PATCH 1/2] regulator: core: Add new notification for enabling of regulator Harald Geyer
[not found] ` <1487869613-11927-1-git-send-email-harald-95f8Dae0BrPYtjvyW6yDsg@public.gmane.org>
2017-02-23 17:06 ` [PATCH 2/2] [RFC] iio: dht11: Add optional support for supply control via regulator framework Harald Geyer
[not found] ` <1487869613-11927-2-git-send-email-harald-95f8Dae0BrPYtjvyW6yDsg@public.gmane.org>
2017-02-25 16:16 ` Jonathan Cameron [this message]
2017-02-28 0:09 ` Rob Herring
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=84dd6f7e-3bd3-edcd-0bda-81a80de35f3c@kernel.org \
--to=jic23-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
--cc=broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
--cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=harald-95f8Dae0BrPYtjvyW6yDsg@public.gmane.org \
--cc=lars-Qo5EllUWu/uELgA04lAiVw@public.gmane.org \
--cc=lgirdwood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
--cc=linux-iio-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=mark.rutland-5wv7dgnIgG8@public.gmane.org \
--cc=robh+dt-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).