Skip to contentSkip to conversation

iio: light: isl29018: add vcc regulator operation support

PATCH Superseded Anson Huang 2 participants 3 messages Dec 17, 2018 – Dec 23, 2018 IIO

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v7 1/1 iio: light: isl29018: add vcc regulator operation support

The light sensor's power supply could be controllable by regulator on some platforms, such as i.MX6Q-SABRESD board, the light sensor isl29023's power supply is controlled by a GPIO fixed regulator, need to make sure the regulator is enabled before any operation of sensor, this patch adds vcc regulator operation support.

---
ChangeLog since V6
- using devm_regulator_get() instead of devm_regulator_get_optional() since the regulator is there anyway, if dtb does NOT specify one, regulator framework will assign dummy regulator for it;
- Setup devm action for cleaning up regulator resource for error handling.

---
 drivers/iio/light/isl29018.c | 58 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
drivers/iio/light/isl29018.c +58 -0
index b45400f..63f7b9d 100644
@@ -23,6 +23,7 @@
 #include <linux/mutex.h>
 #include <linux/delay.h>
 #include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -95,6 +96,7 @@ struct isl29018_chip {
 	struct isl29018_scale	scale;
 	int			prox_scheme;
 	bool			suspended;
+	struct regulator	*vcc_reg;
 };
 
 static int isl29018_set_integration_time(struct isl29018_chip *chip,
@@ -708,6 +710,17 @@ static const char *isl29018_match_acpi_device(struct device *dev, int *data)
 	return dev_name(dev);
 }
 
+static void isl29018_disable_regulator_action(void *_data)
+{
+	struct isl29018_chip *chip = _data;
+	int err;
+
+	err = regulator_disable(chip->vcc_reg);
+	if (err)
+		dev_err(regmap_get_device(chip->regmap),
+			"failed to disable VCC regulator!\n");
+}
+
 static int isl29018_probe(struct i2c_client *client,
 			  const struct i2c_device_id *id)
 {
@@ -742,6 +755,37 @@ static int isl29018_probe(struct i2c_client *client,
 	chip->scale = isl29018_scales[chip->int_time][0];
 	chip->suspended = false;
 
+	chip->vcc_reg = devm_regulator_get(&client->dev, "vcc");
+	if (IS_ERR(chip->vcc_reg)) {
+		err = PTR_ERR(chip->vcc_reg);
+		if (err != -EPROBE_DEFER)
+			dev_err(&client->dev, "failed to get VCC regulator!\n");
+		return err;
+	}
+
+	err = regulator_enable(chip->vcc_reg);
+	if (err) {
+		dev_err(&client->dev, "failed to enable VCC regulator!\n");
+		return err;
+	}
+
+	chip->regmap = devm_regmap_init_i2c(client,
+				isl29018_chip_info_tbl[dev_id].regmap_cfg);
+	if (IS_ERR(chip->regmap)) {
+		err = PTR_ERR(chip->regmap);
+		dev_err(&client->dev, "regmap initialization fails: %d\n", err);
+		regulator_disable(chip->vcc_reg);
+		return err;
+	}
+
+	err = devm_add_action(&client->dev, isl29018_disable_regulator_action,
+				 chip);
+	if (err) {
+		isl29018_disable_regulator_action(chip);
+		dev_err(&client->dev, "failed to setup regulator cleanup action!\n");
+		return err;
+	}
+
 	chip->regmap = devm_regmap_init_i2c(client,
 				isl29018_chip_info_tbl[dev_id].regmap_cfg);
 	if (IS_ERR(chip->regmap)) {
@@ -768,6 +812,7 @@ static int isl29018_probe(struct i2c_client *client,
 static int isl29018_suspend(struct device *dev)
 {
 	struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
+	int ret;
 
 	mutex_lock(&chip->lock);
 
@@ -777,6 +822,12 @@ static int isl29018_suspend(struct device *dev)
 	 * So we do not have much to do here.
 	 */
 	chip->suspended = true;
+	ret = regulator_disable(chip->vcc_reg);
+	if (ret) {
+		dev_err(dev, "failed to disable VCC regulator\n");
+		mutex_unlock(&chip->lock);
+		return ret;
+	}
 
 	mutex_unlock(&chip->lock);
 
@@ -790,6 +841,13 @@ static int isl29018_resume(struct device *dev)
 
 	mutex_lock(&chip->lock);
 
+	err = regulator_enable(chip->vcc_reg);
+	if (err) {
+		dev_err(dev, "failed to enable VCC regulator\n");
+		mutex_unlock(&chip->lock);
+		return err;
+	}
+
 	err = isl29018_chip_init(chip);
 	if (!err)
 		chip->suspended = false;
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 7 lines · On Mon, 17 Dec 2018 03:25:17 +0000 Anson Huang <[email protected]> wrote:

The light sensor's power supply could be controllable by regulator on some platforms, such as i.MX6Q-SABRESD board, the light sensor isl29023's power supply is controlled by a GPIO fixed regulator, need to make sure the regulator is enabled before any operation of sensor, this patch adds vcc regulator operation support.

Hi Anson

See below.

Quoted 67 lines

---
ChangeLog since V6
- using devm_regulator_get() instead of devm_regulator_get_optional() since the regulator is there anyway, if dtb does NOT specify one, regulator framework will assign dummy regulator for it;
- Setup devm action for cleaning up regulator resource for error handling.

---
 drivers/iio/light/isl29018.c | 58 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
drivers/iio/light/isl29018.c +29 -0
index b45400f..63f7b9d 100644
@@ -23,6 +23,7 @@
 #include <linux/mutex.h>
 #include <linux/delay.h>
 #include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -95,6 +96,7 @@ struct isl29018_chip {
 	struct isl29018_scale	scale;
 	int			prox_scheme;
 	bool			suspended;
+	struct regulator	*vcc_reg;
 };
 
 static int isl29018_set_integration_time(struct isl29018_chip *chip,
@@ -708,6 +710,17 @@ static const char *isl29018_match_acpi_device(struct device *dev, int *data)
 	return dev_name(dev);
 }
 
+static void isl29018_disable_regulator_action(void *_data)
+{
+	struct isl29018_chip *chip = _data;
+	int err;
+
+	err = regulator_disable(chip->vcc_reg);
+	if (err)
+		dev_err(regmap_get_device(chip->regmap),
+			"failed to disable VCC regulator!\n");
+}
+
 static int isl29018_probe(struct i2c_client *client,
 			  const struct i2c_device_id *id)
 {
@@ -742,6 +755,37 @@ static int isl29018_probe(struct i2c_client *client,
 	chip->scale = isl29018_scales[chip->int_time][0];
 	chip->suspended = false;
 
+	chip->vcc_reg = devm_regulator_get(&client->dev, "vcc");
+	if (IS_ERR(chip->vcc_reg)) {
+		err = PTR_ERR(chip->vcc_reg);
+		if (err != -EPROBE_DEFER)
+			dev_err(&client->dev, "failed to get VCC regulator!\n");
+		return err;
+	}
+
+	err = regulator_enable(chip->vcc_reg);
+	if (err) {
+		dev_err(&client->dev, "failed to enable VCC regulator!\n");
+		return err;
+	}
+
+	chip->regmap = devm_regmap_init_i2c(client,
+				isl29018_chip_info_tbl[dev_id].regmap_cfg);

drivers/iio/light/isl29018.c static int isl29018_probe(struct i2c_client *client, · line 783

Quoted 10 lines
drivers/iio/light/isl29018.c +10 -0
+	if (IS_ERR(chip->regmap)) {
+		err = PTR_ERR(chip->regmap);
+		dev_err(&client->dev, "regmap initialization fails: %d\n", err);
+		regulator_disable(chip->vcc_reg);
+		return err;
+	}
+
+	err = devm_add_action(&client->dev, isl29018_disable_regulator_action,
+				 chip);
+	if (err) {

I'm a little confused, why not do this before devm_regmap_init_i2c. That way you won't have to disable the regulator in that one error path. Also, devm_add_action_or_reset will call isl29018_disable_regulator_action for you on error.

Quoted 17 lines
drivers/iio/light/isl29018.c +6 -0
+		isl29018_disable_regulator_action(chip);
+		dev_err(&client->dev, "failed to setup regulator cleanup action!\n");
+		return err;
+	}
+
 	chip->regmap = devm_regmap_init_i2c(client,
 				isl29018_chip_info_tbl[dev_id].regmap_cfg);
 	if (IS_ERR(chip->regmap)) {
@@ -768,6 +812,7 @@ static int isl29018_probe(struct i2c_client *client,
 static int isl29018_suspend(struct device *dev)
 {
 	struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
+	int ret;
 
 	mutex_lock(&chip->lock);
 

drivers/iio/light/isl29018.c static int isl29018_suspend(struct device *dev) · line 829

Quoted 8 lines
drivers/iio/light/isl29018.c +5 -0
@@ -777,6 +822,12 @@ static int isl29018_suspend(struct device *dev)
 	 * So we do not have much to do here.
 	 */
 	chip->suspended = true;
+	ret = regulator_disable(chip->vcc_reg);
+	if (ret) {
+		dev_err(dev, "failed to disable VCC regulator\n");
+		mutex_unlock(&chip->lock);
+		return ret;

Given you are about to unlock anyway a common pattern is to not check ret until after the unlock, thus simplifying the code.

Quoted 18 lines
drivers/iio/light/isl29018.c +8 -0
+	}
 
 	mutex_unlock(&chip->lock);
 
@@ -790,6 +841,13 @@ static int isl29018_resume(struct device *dev)
 
 	mutex_lock(&chip->lock);
 
+	err = regulator_enable(chip->vcc_reg);
+	if (err) {
+		dev_err(dev, "failed to enable VCC regulator\n");
+		mutex_unlock(&chip->lock);
+		return err;
+	}
+
 	err = isl29018_chip_init(chip);
 	if (!err)
 		chip->suspended = false;
#2 lore raw

Hi, Jonathan

Best Regards!
Anson Huang

Quoted 101 lines

-----Original Message-----
From: Jonathan Cameron [mailto:[email protected]]
Sent: 2018年12月23日 1:15
To: Anson Huang <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; dl-linux-imx <[email protected]>
Subject: Re: [PATCH V7] iio: light: isl29018: add vcc regulator operation support

Quoted 7 lines · On Mon, 17 Dec 2018 03:25:17 +0000 Anson Huang <[email protected]> wrote:

The light sensor's power supply could be controllable by regulator on some platforms, such as i.MX6Q-SABRESD board, the light sensor isl29023's power supply is controlled by a GPIO fixed regulator, need to make sure the regulator is enabled before any operation of sensor, this patch adds vcc regulator operation support.

Hi Anson

See below.

Quoted 3 lines

---
ChangeLog since V6
- using devm_regulator_get() instead of devm_regulator_get_optional()

since the regulator is

Quoted 1 line

there anyway, if dtb does NOT specify one, regulator framework will

assign dummy regulator for it;

Quoted 1 line

- Setup devm action for cleaning up regulator resource for error

handling.

drivers/iio/light/isl29018.c static const char

Quoted 26 lines
---
 drivers/iio/light/isl29018.c | 58
++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
(quoted diff) +0 -0

b/drivers/iio/light/isl29018.c index b45400f..63f7b9d 100644

drivers/iio/light/isl29018.c +2 -0
@@ -23,6 +23,7 @@
 #include <linux/mutex.h>
 #include <linux/delay.h>
 #include <linux/regmap.h>
+#include <linux/regulator/consumer.h>
 #include <linux/slab.h>
 #include <linux/iio/iio.h>
 #include <linux/iio/sysfs.h>
@@ -95,6 +96,7 @@ struct isl29018_chip {
 	struct isl29018_scale	scale;
 	int			prox_scheme;
 	bool			suspended;
+	struct regulator	*vcc_reg;
 };
 
 static int isl29018_set_integration_time(struct isl29018_chip *chip,
@@ -708,6 +710,17 @@ static const char

*isl29018_match_acpi_device(struct device *dev, int *data)

Quoted 42 lines
drivers/iio/light/isl29018.c +32 -0
 	return dev_name(dev);
 }
 
+static void isl29018_disable_regulator_action(void *_data) {
+	struct isl29018_chip *chip = _data;
+	int err;
+
+	err = regulator_disable(chip->vcc_reg);
+	if (err)
+		dev_err(regmap_get_device(chip->regmap),
+			"failed to disable VCC regulator!\n"); }
+
 static int isl29018_probe(struct i2c_client *client,
 			  const struct i2c_device_id *id)
 {
@@ -742,6 +755,37 @@ static int isl29018_probe(struct i2c_client *client,
 	chip->scale = isl29018_scales[chip->int_time][0];
 	chip->suspended = false;
 
+	chip->vcc_reg = devm_regulator_get(&client->dev, "vcc");
+	if (IS_ERR(chip->vcc_reg)) {
+		err = PTR_ERR(chip->vcc_reg);
+		if (err != -EPROBE_DEFER)
+			dev_err(&client->dev, "failed to get VCC regulator!\n");
+		return err;
+	}
+
+	err = regulator_enable(chip->vcc_reg);
+	if (err) {
+		dev_err(&client->dev, "failed to enable VCC regulator!\n");
+		return err;
+	}
+
+	chip->regmap = devm_regmap_init_i2c(client,
+				isl29018_chip_info_tbl[dev_id].regmap_cfg);
+	if (IS_ERR(chip->regmap)) {
+		err = PTR_ERR(chip->regmap);
+		dev_err(&client->dev, "regmap initialization fails: %d\n", err);
+		regulator_disable(chip->vcc_reg);
+		return err;
+	}
+
Quoted 8 lines

drivers/iio/light/isl29018.c static int isl29018_probe(struct i2c_client *client, · line 783

Quoted 3 lines
drivers/iio/light/isl29018.c +3 -0
+	err = devm_add_action(&client->dev, isl29018_disable_regulator_action,
+				 chip);
+	if (err) {

I'm a little confused, why not do this before devm_regmap_init_i2c. That way you won't have to disable the regulator in that one error path. Also, devm_add_action_or_reset will call isl29018_disable_regulator_action for you on error.

It is because I used dev_err() in isl29018_disable_regulator_action which need regmap to get "dev" by regmap_get_device(chip->regmap), if it is accepted by just using pr_err() instead of dev_err, then I can do the devm_add_action before devm_regmap_init_i2c.

I think using pr_err should be OK, I will use it in V8 patch.

Quoted 16 lines
Quoted 16 lines
drivers/iio/light/isl29018.c +6 -0
+		isl29018_disable_regulator_action(chip);
+		dev_err(&client->dev, "failed to setup regulator cleanup action!\n");
+		return err;
+	}
+
 	chip->regmap = devm_regmap_init_i2c(client,
 				isl29018_chip_info_tbl[dev_id].regmap_cfg);
 	if (IS_ERR(chip->regmap)) {
@@ -768,6 +812,7 @@ static int isl29018_probe(struct i2c_client
 *client,  static int isl29018_suspend(struct device *dev)  {
 	struct isl29018_chip *chip = iio_priv(dev_get_drvdata(dev));
+	int ret;
 
 	mutex_lock(&chip->lock);
 
Quoted 12 lines

drivers/iio/light/isl29018.c static int isl29018_suspend(struct device *dev) · line 829

Quoted 9 lines
drivers/iio/light/isl29018.c +5 -0
@@ -777,6 +822,12 @@ static int isl29018_suspend(struct device *dev)
 	 * So we do not have much to do here.
 	 */
 	chip->suspended = true;
+	ret = regulator_disable(chip->vcc_reg);
+	if (ret) {
+		dev_err(dev, "failed to disable VCC regulator\n");
+		mutex_unlock(&chip->lock);
+		return ret;

Given you are about to unlock anyway a common pattern is to not check ret until after the unlock, thus simplifying the code.

I will improve this in V8 patch, thanks.

Anson.

Quoted 19 lines
Quoted 19 lines
drivers/iio/light/isl29018.c +8 -0
+	}
 
 	mutex_unlock(&chip->lock);
 
@@ -790,6 +841,13 @@ static int isl29018_resume(struct device *dev)
 
 	mutex_lock(&chip->lock);
 
+	err = regulator_enable(chip->vcc_reg);
+	if (err) {
+		dev_err(dev, "failed to enable VCC regulator\n");
+		mutex_unlock(&chip->lock);
+		return err;
+	}
+
 	err = isl29018_chip_init(chip);
 	if (!err)
 		chip->suspended = false;