mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Brian Masney <masneyb@onstation.org>
To: jic23@kernel.org, robh+dt@kernel.org, mark.rutland@arm.com,
	andy.gross@linaro.org, david.brown@linaro.org,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	linux-soc@vger.kernel.org
Cc: jonathan@marek.ca, jmaneyrol@invensense.com, knaack.h@gmx.de,
	lars@metafoo.de, pmeerw@pmeerw.net, mkelly@xevo.com,
	fischerdouglasc@gmail.com, bshah@kde.org, ctatlor97@gmail.com,
	drew.paterson@ams.com
Subject: [PATCH v2 5/7] iio: tsl2772: add support for regulator framework
Date: Tue, 17 Jul 2018 04:41:56 -0400	[thread overview]
Message-ID: <20180717084158.23532-6-masneyb@onstation.org> (raw)
In-Reply-To: <20180717084158.23532-1-masneyb@onstation.org>

This patch adds support for the regulator framework to the tsl2772
driver. Driver was tested using a LG Nexus 5 (hammerhead) phone with
the two regulators and on a Raspberry Pi 2 without any regulators
controlling the power to the sensor.

Signed-off-by: Brian Masney <masneyb@onstation.org>
---
 .../devicetree/bindings/iio/light/tsl2772.txt |  4 +
 drivers/iio/light/tsl2772.c                   | 82 ++++++++++++++++++-
 2 files changed, 85 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/iio/light/tsl2772.txt b/Documentation/devicetree/bindings/iio/light/tsl2772.txt
index ab553d52b9fc..bfde8b2c8790 100644
--- a/Documentation/devicetree/bindings/iio/light/tsl2772.txt
+++ b/Documentation/devicetree/bindings/iio/light/tsl2772.txt
@@ -21,6 +21,8 @@ Optional properties:
                          TSL2772_DIODE_BOTH.
   - amstaos,prox_power - must be TSL2772_100_mA, TSL2772_50_mA, TSL2772_25_mA,
                          or TSL2772_13_mA.
+  - vdd-supply: phandle to the regulator that provides power to the sensor.
+  - vddio-supply: phandle to the regulator that provides power to the bus.
   - interrupt-parent: should be the phandle for the interrupt controller
   - interrupts: the sole interrupt generated by the device
 
@@ -35,5 +37,7 @@ tsl2772@39 {
 	compatible = "amstaos,tsl2772";
 	reg = <0x39>;
 	interrupts-extended = <&msmgpio 61 IRQ_TYPE_EDGE_FALLING>;
+	vdd-supply = <&pm8941_l17>;
+	vddio-supply = <&pm8941_lvs1>;
 	amstaos,prox_diode = <TSL2772_DIODE0>;
 };
diff --git a/drivers/iio/light/tsl2772.c b/drivers/iio/light/tsl2772.c
index 0be57f2ecb02..f6a27f8b3ca7 100644
--- a/drivers/iio/light/tsl2772.c
+++ b/drivers/iio/light/tsl2772.c
@@ -20,6 +20,7 @@
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
 #include <linux/platform_data/tsl2772.h>
+#include <linux/regulator/consumer.h>
 
 /* Cal defs */
 #define PROX_STAT_CAL			0
@@ -146,6 +147,9 @@ struct tsl2772_chip {
 	struct mutex prox_mutex;
 	struct mutex als_mutex;
 	struct i2c_client *client;
+	struct regulator *vdd_supply;
+	struct regulator *vddio_supply;
+	bool regulators_enabled;
 	u16 prox_data;
 	struct tsl2772_als_info als_cur_info;
 	struct tsl2772_settings settings;
@@ -609,6 +613,46 @@ static int tsl2772_als_calibrate(struct iio_dev *indio_dev)
 	return ret;
 }
 
+static int tsl2772_enable_regulators(struct tsl2772_chip *chip)
+{
+	int ret;
+
+	ret = regulator_enable(chip->vddio_supply);
+	if (ret < 0) {
+		dev_err(&chip->client->dev, "Failed to enable regulator: %d\n",
+			ret);
+		return ret;
+	}
+
+	ret = regulator_enable(chip->vdd_supply);
+	if (ret < 0) {
+		regulator_disable(chip->vddio_supply);
+		dev_err(&chip->client->dev, "Failed to enable regulator: %d\n",
+			ret);
+		return ret;
+	}
+
+	chip->regulators_enabled = true;
+
+	return 0;
+}
+
+static void tsl2772_disable_regulators(struct tsl2772_chip *chip)
+{
+	if (!chip->regulators_enabled)
+		return;
+
+	regulator_disable(chip->vdd_supply);
+	regulator_disable(chip->vddio_supply);
+
+	chip->regulators_enabled = false;
+}
+
+static void tsl2772_disable_regulators_action(void *_data)
+{
+	tsl2772_disable_regulators(_data);
+}
+
 static int tsl2772_chip_on(struct iio_dev *indio_dev)
 {
 	struct tsl2772_chip *chip = iio_priv(indio_dev);
@@ -666,6 +710,10 @@ static int tsl2772_chip_on(struct iio_dev *indio_dev)
 	chip->als_gain_time_scale = als_time_us *
 		tsl2772_als_gain[chip->settings.als_gain];
 
+	ret = tsl2772_enable_regulators(chip);
+	if (ret < 0)
+		return ret;
+
 	/*
 	 * TSL2772 Specific power-on / adc enable sequence
 	 * Power on the device 1st.
@@ -724,10 +772,13 @@ static int tsl2772_chip_on(struct iio_dev *indio_dev)
 static int tsl2772_chip_off(struct iio_dev *indio_dev)
 {
 	struct tsl2772_chip *chip = iio_priv(indio_dev);
+	int ret;
 
 	/* turn device off */
 	chip->tsl2772_chip_status = TSL2772_CHIP_SUSPENDED;
-	return tsl2772_write_control_reg(chip, 0x00);
+	ret = tsl2772_write_control_reg(chip, 0x00);
+	tsl2772_disable_regulators(chip);
+	return ret;
 }
 
 /**
@@ -1666,6 +1717,35 @@ static int tsl2772_probe(struct i2c_client *clientp,
 	chip->client = clientp;
 	i2c_set_clientdata(clientp, indio_dev);
 
+	chip->vddio_supply = devm_regulator_get(&clientp->dev, "vddio");
+	if (IS_ERR(chip->vddio_supply)) {
+		if (PTR_ERR(chip->vddio_supply) != -EPROBE_DEFER)
+			dev_err(&clientp->dev,
+				"Failed to get vddio regulator %d\n",
+				(int)PTR_ERR(chip->vddio_supply));
+
+		return PTR_ERR(chip->vddio_supply);
+	}
+
+	chip->vdd_supply = devm_regulator_get(&clientp->dev, "vdd");
+	if (IS_ERR(chip->vdd_supply)) {
+		if (PTR_ERR(chip->vdd_supply) != -EPROBE_DEFER)
+			dev_err(&clientp->dev,
+				"Failed to get vdd regulator %d\n",
+				(int)PTR_ERR(chip->vdd_supply));
+
+		return PTR_ERR(chip->vdd_supply);
+	}
+
+	ret = devm_add_action(&clientp->dev, tsl2772_disable_regulators_action,
+			      chip);
+	if (ret < 0) {
+		tsl2772_disable_regulators_action(chip);
+		dev_err(&clientp->dev, "Failed to setup regulator cleanup action %d\n",
+			ret);
+		return ret;
+	}
+
 	ret = i2c_smbus_read_byte_data(chip->client,
 				       TSL2772_CMD_REG | TSL2772_CHIPID);
 	if (ret < 0)
-- 
2.17.1


  parent reply	other threads:[~2018-07-17  8:42 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-17  8:41 [PATCH v2 0/7] ARM: dts: qcom: msm8974-hammerhead: add more sensors Brian Masney
2018-07-17  8:41 ` [PATCH v2 1/7] iio: imu: mpu6050: add support for regulator framework Brian Masney
2018-07-20 17:02   ` Rob Herring
2018-07-21 17:31   ` Jonathan Cameron
2018-07-22 12:31     ` Brian Masney
2018-07-22 17:20       ` Jonathan Cameron
2018-07-17  8:41 ` [PATCH v2 2/7] ARM: dts: qcom: msm8974-hammerhead: add device tree bindings for mpu6515 Brian Masney
2018-07-17  8:41 ` [PATCH v2 3/7] iio: tsl2772: add support for reading power settings from device tree Brian Masney
2018-07-20 17:36   ` Rob Herring
2018-07-21 17:37     ` Jonathan Cameron
2018-07-22 12:37       ` Brian Masney
2018-07-22 17:17         ` Jonathan Cameron
2018-07-23 13:28           ` Rob Herring
2018-07-21 17:35   ` Jonathan Cameron
2018-07-17  8:41 ` [PATCH v2 4/7] dt-bindings: trivial: remove tsl2772 Brian Masney
2018-07-25 16:01   ` Rob Herring
2018-07-17  8:41 ` Brian Masney [this message]
2018-07-20 17:38   ` [PATCH v2 5/7] iio: tsl2772: add support for regulator framework Rob Herring
2018-07-21 17:45   ` Jonathan Cameron
2018-07-17  8:41 ` [PATCH v2 6/7] iio: tsl2772: add device tree binding for avago,apds9930 Brian Masney
2018-07-17  8:41 ` [PATCH v2 7/7] ARM: dts: qcom: msm8974-hammerhead: add device tree bindings for ALS / proximity Brian Masney

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=20180717084158.23532-6-masneyb@onstation.org \
    --to=masneyb@onstation.org \
    --cc=andy.gross@linaro.org \
    --cc=bshah@kde.org \
    --cc=ctatlor97@gmail.com \
    --cc=david.brown@linaro.org \
    --cc=devicetree@vger.kernel.org \
    --cc=drew.paterson@ams.com \
    --cc=fischerdouglasc@gmail.com \
    --cc=jic23@kernel.org \
    --cc=jmaneyrol@invensense.com \
    --cc=jonathan@marek.ca \
    --cc=knaack.h@gmx.de \
    --cc=lars@metafoo.de \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-soc@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mkelly@xevo.com \
    --cc=pmeerw@pmeerw.net \
    --cc=robh+dt@kernel.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

all inboxes | Powered by JetHome®