diff mbox series

[v3,2/2] media: i2c: imx412: Add bulk regulator support

Message ID 20220414124505.1329295-3-bryan.odonoghue@linaro.org (mailing list archive)
State New, archived
Headers show
Series media: i2c: imx412: Add regulator control to imx412 | expand

Commit Message

Bryan O'Donoghue April 14, 2022, 12:45 p.m. UTC
Depending on the platform we may need to enable and disable three separate
regulators for the imx412.

- DOVDD
Digital I/O power

- AVDD
Analog power

- DVDD
Digital core power

The addition of these regulators shouldn't affect existing users using
fixed-on/firmware-controlled regulators.

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
---
 drivers/media/i2c/imx412.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

Comments

Sakari Ailus April 14, 2022, 1 p.m. UTC | #1
Hi Brian,

Thanks for the update. A few minor matters below...

On Thu, Apr 14, 2022 at 01:45:05PM +0100, Bryan O'Donoghue wrote:
> Depending on the platform we may need to enable and disable three separate
> regulators for the imx412.
> 
> - DOVDD
> Digital I/O power
> 
> - AVDD
> Analog power
> 
> - DVDD
> Digital core power
> 
> The addition of these regulators shouldn't affect existing users using
> fixed-on/firmware-controlled regulators.
> 
> Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
> ---
>  drivers/media/i2c/imx412.c | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
> 
> diff --git a/drivers/media/i2c/imx412.c b/drivers/media/i2c/imx412.c
> index be3f6ea55559..27170e641b53 100644
> --- a/drivers/media/i2c/imx412.c
> +++ b/drivers/media/i2c/imx412.c
> @@ -11,6 +11,7 @@
>  #include <linux/i2c.h>
>  #include <linux/module.h>
>  #include <linux/pm_runtime.h>
> +#include <linux/regulator/consumer.h>
>  
>  #include <media/v4l2-ctrls.h>
>  #include <media/v4l2-fwnode.h>
> @@ -101,6 +102,12 @@ struct imx412_mode {
>  	struct imx412_reg_list reg_list;
>  };
>  
> +static const char * const imx412_supply_names[] = {
> +	"dovdd",	/* Digital I/O power */
> +	"avdd",		/* Analog power */
> +	"dvdd",		/* Digital core power */
> +};
> +
>  /**
>   * struct imx412 - imx412 sensor device structure
>   * @dev: Pointer to generic device
> @@ -128,6 +135,8 @@ struct imx412 {
>  	struct media_pad pad;
>  	struct gpio_desc *reset_gpio;
>  	struct clk *inclk;
> +	struct regulator_bulk_data supplies[ARRAY_SIZE(imx412_supply_names)];
> +	unsigned int num_supplies;
>  	struct v4l2_ctrl_handler ctrl_handler;
>  	struct v4l2_ctrl *link_freq_ctrl;
>  	struct v4l2_ctrl *pclk_ctrl;
> @@ -946,6 +955,16 @@ static int imx412_parse_hw_config(struct imx412 *imx412)
>  		return -EINVAL;
>  	}
>  
> +	/* Get optional DT defined regulators */
> +	imx412->num_supplies = ARRAY_SIZE(imx412_supply_names);
> +	for (i = 0; i < imx412->num_supplies; i++)
> +		imx412->supplies[i].supply = imx412_supply_names[i];
> +
> +	ret = devm_regulator_bulk_get(imx412->dev, imx412->num_supplies,
> +				      imx412->supplies);
> +	if (ret)
> +		return ret;
> +
>  	ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
>  	if (!ep)
>  		return -ENXIO;
> @@ -1011,10 +1030,19 @@ static int imx412_power_on(struct device *dev)
>  	struct imx412 *imx412 = to_imx412(sd);
>  	int ret;
>  
> +	ret = regulator_bulk_enable(imx412->num_supplies,
> +				    imx412->supplies);

Fits on the same line.

> +	if (ret < 0) {
> +		dev_err(dev, "failed to enable regulators\n");
> +		return ret;
> +	}
> +
>  	gpiod_set_value_cansleep(imx412->reset_gpio, 1);
>  
>  	ret = clk_prepare_enable(imx412->inclk);
>  	if (ret) {
> +		regulator_bulk_disable(imx412->num_supplies,
> +				       imx412->supplies);

As the function already has an error handling section using labels, this
should go there as well.

>  		dev_err(imx412->dev, "fail to enable inclk");
>  		goto error_reset;
>  	}
> @@ -1044,6 +1072,9 @@ static int imx412_power_off(struct device *dev)
>  
>  	clk_disable_unprepare(imx412->inclk);
>  
> +	regulator_bulk_disable(imx412->num_supplies,
> +			       imx412->supplies);

Fits on the same line.

> +
>  	return 0;
>  }
>
Bryan O'Donoghue April 14, 2022, 1:44 p.m. UTC | #2
On 14/04/2022 14:00, Sakari Ailus wrote:
>>   	ret = clk_prepare_enable(imx412->inclk);
>>   	if (ret) {
>> +		regulator_bulk_disable(imx412->num_supplies,
>> +				       imx412->supplies);
> As the function already has an error handling section using labels, this
> should go there as well.
> 
Are you asking to move regulator_bulk_disable() to error_reset ?
Sakari Ailus April 14, 2022, 1:56 p.m. UTC | #3
On Thu, Apr 14, 2022 at 02:44:00PM +0100, Bryan O'Donoghue wrote:
> On 14/04/2022 14:00, Sakari Ailus wrote:
> > >   	ret = clk_prepare_enable(imx412->inclk);
> > >   	if (ret) {
> > > +		regulator_bulk_disable(imx412->num_supplies,
> > > +				       imx412->supplies);
> > As the function already has an error handling section using labels, this
> > should go there as well.
> > 
> Are you asking to move regulator_bulk_disable() to error_reset ?

No. You'll need another label.
Bryan O'Donoghue April 14, 2022, 2:04 p.m. UTC | #4
On 14/04/2022 14:56, Sakari Ailus wrote:
> On Thu, Apr 14, 2022 at 02:44:00PM +0100, Bryan O'Donoghue wrote:
>> On 14/04/2022 14:00, Sakari Ailus wrote:
>>>>    	ret = clk_prepare_enable(imx412->inclk);
>>>>    	if (ret) {
>>>> +		regulator_bulk_disable(imx412->num_supplies,
>>>> +				       imx412->supplies);
>>> As the function already has an error handling section using labels, this
>>> should go there as well.
>>>
>> Are you asking to move regulator_bulk_disable() to error_reset ?
> 
> No. You'll need another label.
> 

Hmm.

I think another label is not required, have a look at V4.
Sakari Ailus April 14, 2022, 2:16 p.m. UTC | #5
On Thu, Apr 14, 2022 at 03:04:10PM +0100, Bryan O'Donoghue wrote:
> On 14/04/2022 14:56, Sakari Ailus wrote:
> > On Thu, Apr 14, 2022 at 02:44:00PM +0100, Bryan O'Donoghue wrote:
> > > On 14/04/2022 14:00, Sakari Ailus wrote:
> > > > >    	ret = clk_prepare_enable(imx412->inclk);
> > > > >    	if (ret) {
> > > > > +		regulator_bulk_disable(imx412->num_supplies,
> > > > > +				       imx412->supplies);
> > > > As the function already has an error handling section using labels, this
> > > > should go there as well.
> > > > 
> > > Are you asking to move regulator_bulk_disable() to error_reset ?
> > 
> > No. You'll need another label.
> > 
> 
> Hmm.
> 
> I think another label is not required, have a look at V4.

Ah, yes, indeed. There's just a single location where this will be needed.

On another note, gpiod_set_value_cansleep() seems to enable reset in
resume and disable it in suspend. I.e. the polarity is wrong.
Bryan O'Donoghue April 14, 2022, 2:50 p.m. UTC | #6
On 14/04/2022 15:16, Sakari Ailus wrote:
> On Thu, Apr 14, 2022 at 03:04:10PM +0100, Bryan O'Donoghue wrote:
>> On 14/04/2022 14:56, Sakari Ailus wrote:
>>> On Thu, Apr 14, 2022 at 02:44:00PM +0100, Bryan O'Donoghue wrote:
>>>> On 14/04/2022 14:00, Sakari Ailus wrote:
>>>>>>     	ret = clk_prepare_enable(imx412->inclk);
>>>>>>     	if (ret) {
>>>>>> +		regulator_bulk_disable(imx412->num_supplies,
>>>>>> +				       imx412->supplies);
>>>>> As the function already has an error handling section using labels, this
>>>>> should go there as well.
>>>>>
>>>> Are you asking to move regulator_bulk_disable() to error_reset ?
>>>
>>> No. You'll need another label.
>>>
>>
>> Hmm.
>>
>> I think another label is not required, have a look at V4.
> 
> Ah, yes, indeed. There's just a single location where this will be needed.
> 
> On another note, gpiod_set_value_cansleep() seems to enable reset in
> resume and disable it in suspend. I.e. the polarity is wrong.
> 

Agreed, the polarity looks wrong - in my DTS right now I have 
ACTIVE_HIGH for the relevant GPIO.

For example if I do this

@@ -1363,7 +1363,7 @@ camera@1a {
                 compatible = "sony,imx412";
                 reg = <0x1a>;

-               reset-gpios = <&tlmm 78 GPIO_ACTIVE_HIGH>;
+               reset-gpios = <&tlmm 78 GPIO_ACTIVE_LOW>;
                 pinctrl-names = "default", "suspend";
                 pinctrl-0 = <&cam2_default>;
                 pinctrl-1 = <&cam2_suspend>;
diff --git a/drivers/media/i2c/imx412.c b/drivers/media/i2c/imx412.c
index a9cdf4694d58..1442b416f5aa 100644
--- a/drivers/media/i2c/imx412.c
+++ b/drivers/media/i2c/imx412.c
@@ -1036,7 +1036,7 @@ static int imx412_power_on(struct device *dev)
                 return ret;
         }

-       gpiod_set_value_cansleep(imx412->reset_gpio, 1);
+       gpiod_set_value_cansleep(imx412->reset_gpio, 0);

         ret = clk_prepare_enable(imx412->inclk);
         if (ret) {
@@ -1049,7 +1049,7 @@ static int imx412_power_on(struct device *dev)
         return 0;

  error_reset:
-       gpiod_set_value_cansleep(imx412->reset_gpio, 0);
+       gpiod_set_value_cansleep(imx412->reset_gpio, 1);
         regulator_bulk_disable(imx412->num_supplies, imx412->supplies);

         return ret;
@@ -1068,7 +1068,7 @@ static int imx412_power_off(struct device *dev)

         clk_disable_unprepare(imx412->inclk);

-       gpiod_set_value_cansleep(imx412->reset_gpio, 0);
+       gpiod_set_value_cansleep(imx412->reset_gpio, 1);

Seems like changing the logic would negatively affect the Intel people. 
Might have to churn ACPI to change that logic..

Easier probably to leave as is and define as ACTIVE_HIGH in DTS
Sakari Ailus April 14, 2022, 9:58 p.m. UTC | #7
Hi Bryan,

On Thu, Apr 14, 2022 at 03:50:50PM +0100, Bryan O'Donoghue wrote:
> On 14/04/2022 15:16, Sakari Ailus wrote:
> > On Thu, Apr 14, 2022 at 03:04:10PM +0100, Bryan O'Donoghue wrote:
> > > On 14/04/2022 14:56, Sakari Ailus wrote:
> > > > On Thu, Apr 14, 2022 at 02:44:00PM +0100, Bryan O'Donoghue wrote:
> > > > > On 14/04/2022 14:00, Sakari Ailus wrote:
> > > > > > >     	ret = clk_prepare_enable(imx412->inclk);
> > > > > > >     	if (ret) {
> > > > > > > +		regulator_bulk_disable(imx412->num_supplies,
> > > > > > > +				       imx412->supplies);
> > > > > > As the function already has an error handling section using labels, this
> > > > > > should go there as well.
> > > > > > 
> > > > > Are you asking to move regulator_bulk_disable() to error_reset ?
> > > > 
> > > > No. You'll need another label.
> > > > 
> > > 
> > > Hmm.
> > > 
> > > I think another label is not required, have a look at V4.
> > 
> > Ah, yes, indeed. There's just a single location where this will be needed.
> > 
> > On another note, gpiod_set_value_cansleep() seems to enable reset in
> > resume and disable it in suspend. I.e. the polarity is wrong.
> > 
> 
> Agreed, the polarity looks wrong - in my DTS right now I have ACTIVE_HIGH
> for the relevant GPIO.
> 
> For example if I do this
> 
> @@ -1363,7 +1363,7 @@ camera@1a {
>                 compatible = "sony,imx412";
>                 reg = <0x1a>;
> 
> -               reset-gpios = <&tlmm 78 GPIO_ACTIVE_HIGH>;
> +               reset-gpios = <&tlmm 78 GPIO_ACTIVE_LOW>;
>                 pinctrl-names = "default", "suspend";
>                 pinctrl-0 = <&cam2_default>;
>                 pinctrl-1 = <&cam2_suspend>;
> diff --git a/drivers/media/i2c/imx412.c b/drivers/media/i2c/imx412.c
> index a9cdf4694d58..1442b416f5aa 100644
> --- a/drivers/media/i2c/imx412.c
> +++ b/drivers/media/i2c/imx412.c
> @@ -1036,7 +1036,7 @@ static int imx412_power_on(struct device *dev)
>                 return ret;
>         }
> 
> -       gpiod_set_value_cansleep(imx412->reset_gpio, 1);
> +       gpiod_set_value_cansleep(imx412->reset_gpio, 0);
> 
>         ret = clk_prepare_enable(imx412->inclk);
>         if (ret) {
> @@ -1049,7 +1049,7 @@ static int imx412_power_on(struct device *dev)
>         return 0;
> 
>  error_reset:
> -       gpiod_set_value_cansleep(imx412->reset_gpio, 0);
> +       gpiod_set_value_cansleep(imx412->reset_gpio, 1);
>         regulator_bulk_disable(imx412->num_supplies, imx412->supplies);
> 
>         return ret;
> @@ -1068,7 +1068,7 @@ static int imx412_power_off(struct device *dev)
> 
>         clk_disable_unprepare(imx412->inclk);
> 
> -       gpiod_set_value_cansleep(imx412->reset_gpio, 0);
> +       gpiod_set_value_cansleep(imx412->reset_gpio, 1);
> 
> Seems like changing the logic would negatively affect the Intel people.
> Might have to churn ACPI to change that logic..
> 
> Easier probably to leave as is and define as ACTIVE_HIGH in DTS

It's still wrong and should be fixed. It seems there are no boards in the
DT source in the kernel using this sensor. These changes seem fine to me.

I'm not really worried about ACPI: it's unlikely the GPIO is even declared
for the sensor, and instead is controlled in AML. There can of course be
bugs in ACPI tables, too...
Bryan O'Donoghue April 14, 2022, 10:32 p.m. UTC | #8
On 14/04/2022 22:58, Sakari Ailus wrote:
> It's still wrong and should be fixed. It seems there are no boards in the
> DT source in the kernel using this sensor. These changes seem fine to me.
> 
> I'm not really worried about ACPI: it's unlikely the GPIO is even declared
> for the sensor, and instead is controlled in AML. There can of course be
> bugs in ACPI tables, too...

Hmm.

OK well I'll add the GPIO patch in for V5 but with a putative guard in 
for ACPI, inverting the logic.

We can always pull the guard if it turns out not be necessary.

---
bod
diff mbox series

Patch

diff --git a/drivers/media/i2c/imx412.c b/drivers/media/i2c/imx412.c
index be3f6ea55559..27170e641b53 100644
--- a/drivers/media/i2c/imx412.c
+++ b/drivers/media/i2c/imx412.c
@@ -11,6 +11,7 @@ 
 #include <linux/i2c.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
+#include <linux/regulator/consumer.h>
 
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-fwnode.h>
@@ -101,6 +102,12 @@  struct imx412_mode {
 	struct imx412_reg_list reg_list;
 };
 
+static const char * const imx412_supply_names[] = {
+	"dovdd",	/* Digital I/O power */
+	"avdd",		/* Analog power */
+	"dvdd",		/* Digital core power */
+};
+
 /**
  * struct imx412 - imx412 sensor device structure
  * @dev: Pointer to generic device
@@ -128,6 +135,8 @@  struct imx412 {
 	struct media_pad pad;
 	struct gpio_desc *reset_gpio;
 	struct clk *inclk;
+	struct regulator_bulk_data supplies[ARRAY_SIZE(imx412_supply_names)];
+	unsigned int num_supplies;
 	struct v4l2_ctrl_handler ctrl_handler;
 	struct v4l2_ctrl *link_freq_ctrl;
 	struct v4l2_ctrl *pclk_ctrl;
@@ -946,6 +955,16 @@  static int imx412_parse_hw_config(struct imx412 *imx412)
 		return -EINVAL;
 	}
 
+	/* Get optional DT defined regulators */
+	imx412->num_supplies = ARRAY_SIZE(imx412_supply_names);
+	for (i = 0; i < imx412->num_supplies; i++)
+		imx412->supplies[i].supply = imx412_supply_names[i];
+
+	ret = devm_regulator_bulk_get(imx412->dev, imx412->num_supplies,
+				      imx412->supplies);
+	if (ret)
+		return ret;
+
 	ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
 	if (!ep)
 		return -ENXIO;
@@ -1011,10 +1030,19 @@  static int imx412_power_on(struct device *dev)
 	struct imx412 *imx412 = to_imx412(sd);
 	int ret;
 
+	ret = regulator_bulk_enable(imx412->num_supplies,
+				    imx412->supplies);
+	if (ret < 0) {
+		dev_err(dev, "failed to enable regulators\n");
+		return ret;
+	}
+
 	gpiod_set_value_cansleep(imx412->reset_gpio, 1);
 
 	ret = clk_prepare_enable(imx412->inclk);
 	if (ret) {
+		regulator_bulk_disable(imx412->num_supplies,
+				       imx412->supplies);
 		dev_err(imx412->dev, "fail to enable inclk");
 		goto error_reset;
 	}
@@ -1044,6 +1072,9 @@  static int imx412_power_off(struct device *dev)
 
 	clk_disable_unprepare(imx412->inclk);
 
+	regulator_bulk_disable(imx412->num_supplies,
+			       imx412->supplies);
+
 	return 0;
 }