diff mbox series

[v2,2/4] hwmon: (max6639) Add regulator support

Message ID 4c644f279f6e205d3c9540a4ad35825e4d5f2da1.1642413668.git.sylv@sylv.io (mailing list archive)
State Superseded
Headers show
Series Add max6639 regulator and devicetree support | expand

Commit Message

Marcello Sylvester Bauer Jan. 17, 2022, 10:40 a.m. UTC
Add regulator support for boards where the fan-supply have to be
powered up before it can be used.

Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
Signed-off-by: Marcello Sylvester Bauer <sylv@sylv.io>
---
 drivers/hwmon/max6639.c | 64 +++++++++++++++++++++++++++++++++++------
 1 file changed, 56 insertions(+), 8 deletions(-)

Comments

Guenter Roeck Jan. 18, 2022, 4:11 p.m. UTC | #1
On Mon, Jan 17, 2022 at 11:40:24AM +0100, Marcello Sylvester Bauer wrote:
> Add regulator support for boards where the fan-supply have to be
> powered up before it can be used.
> 
> Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
> Signed-off-by: Marcello Sylvester Bauer <sylv@sylv.io>
> ---
>  drivers/hwmon/max6639.c | 64 +++++++++++++++++++++++++++++++++++------
>  1 file changed, 56 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c
> index ccc0f047bd44..0b241f029229 100644
> --- a/drivers/hwmon/max6639.c
> +++ b/drivers/hwmon/max6639.c
> @@ -87,6 +87,9 @@ struct max6639_data {
>  	/* Register values initialized only once */
>  	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */
>  	u8 rpm_range;		/* Index in above rpm_ranges table */
> +
> +	/* Optional regulator for FAN supply */
> +	struct regulator *reg;
>  };
>  
>  static struct max6639_data *max6639_update_device(struct device *dev)
> @@ -516,6 +519,11 @@ static int max6639_detect(struct i2c_client *client,
>  	return 0;
>  }
>  
> +static void max6639_regulator_disable(void *data)
> +{
> +	regulator_disable(data);
> +}
> +
>  static int max6639_probe(struct i2c_client *client)
>  {
>  	struct device *dev = &client->dev;
> @@ -528,6 +536,30 @@ static int max6639_probe(struct i2c_client *client)
>  		return -ENOMEM;
>  
>  	data->client = client;
> +
> +	data->reg = devm_regulator_get_optional(dev, "fan");
> +	if (IS_ERR(data->reg)) {
> +		if (PTR_ERR(data->reg) != -ENODEV) {
> +			err = (int)PTR_ERR(data->reg);
> +			dev_warn(dev, "Failed looking up fan supply: %d\n", err);

This could be -EPROBE_DEFER. I do not think that warrants
an error message.

> +			return err;
> +		}
> +		data->reg = NULL;
> +	} else {
> +		/* Spin up fans */
> +		err = regulator_enable(data->reg);
> +		if (err) {
> +			dev_err(dev, "Failed to enable fan supply: %d\n", err);
> +			return err;
> +		}
> +		err = devm_add_action_or_reset(dev, max6639_regulator_disable,
> +					       data->reg);
> +		if (err) {
> +			dev_err(dev, "Failed to register action: %d\n", err);
> +			return err;
> +		}
> +	}
> +
>  	mutex_init(&data->update_lock);
>  
>  	/* Initialize the max6639 chip */
> @@ -545,23 +577,39 @@ static int max6639_probe(struct i2c_client *client)
>  static int max6639_suspend(struct device *dev)
>  {
>  	struct i2c_client *client = to_i2c_client(dev);
> -	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
> -	if (data < 0)
> -		return data;
> +	struct max6639_data *data = dev_get_drvdata(dev);
> +	int ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
> +
> +	if (ret < 0)
> +		return ret;
> +
> +	if (data->reg)
> +		regulator_disable(data->reg);
>  
>  	return i2c_smbus_write_byte_data(client,
> -			MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
> +			MAX6639_REG_GCONFIG, ret | MAX6639_GCONFIG_STANDBY);
>  }
>  
>  static int max6639_resume(struct device *dev)
>  {
>  	struct i2c_client *client = to_i2c_client(dev);
> -	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
> -	if (data < 0)
> -		return data;
> +	struct max6639_data *data = dev_get_drvdata(dev);
> +	int ret;
> +
> +	if (data->reg) {
> +		ret = regulator_enable(data->reg);
> +		if (ret) {
> +			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
> +			return ret;
> +		}
> +	}
> +
> +	ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
> +	if (ret < 0)
> +		return ret;
>  
>  	return i2c_smbus_write_byte_data(client,
> -			MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
> +			MAX6639_REG_GCONFIG, ret & ~MAX6639_GCONFIG_STANDBY);
>  }
>  #endif /* CONFIG_PM_SLEEP */
>
Marcello Sylvester Bauer Jan. 19, 2022, 9:42 a.m. UTC | #2
On Tue, 2022-01-18 at 08:11 -0800, Guenter Roeck wrote:
> On Mon, Jan 17, 2022 at 11:40:24AM +0100, Marcello Sylvester Bauer wrote:
> > Add regulator support for boards where the fan-supply have to be
> > powered up before it can be used.
> > 
> > Signed-off-by: Patrick Rudolph <patrick.rudolph@9elements.com>
> > Signed-off-by: Marcello Sylvester Bauer <sylv@sylv.io>
> > ---
> >  drivers/hwmon/max6639.c | 64 +++++++++++++++++++++++++++++++++++------
> >  1 file changed, 56 insertions(+), 8 deletions(-)
> > 
> > diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c
> > index ccc0f047bd44..0b241f029229 100644
> > --- a/drivers/hwmon/max6639.c
> > +++ b/drivers/hwmon/max6639.c
> > @@ -87,6 +87,9 @@ struct max6639_data {
> >  	/* Register values initialized only once */
> >  	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */
> >  	u8 rpm_range;		/* Index in above rpm_ranges table */
> > +
> > +	/* Optional regulator for FAN supply */
> > +	struct regulator *reg;
> >  };
> >  
> >  static struct max6639_data *max6639_update_device(struct device *dev)
> > @@ -516,6 +519,11 @@ static int max6639_detect(struct i2c_client *client,
> >  	return 0;
> >  }
> >  
> > +static void max6639_regulator_disable(void *data)
> > +{
> > +	regulator_disable(data);
> > +}
> > +
> >  static int max6639_probe(struct i2c_client *client)
> >  {
> >  	struct device *dev = &client->dev;
> > @@ -528,6 +536,30 @@ static int max6639_probe(struct i2c_client *client)
> >  		return -ENOMEM;
> >  
> >  	data->client = client;
> > +
> > +	data->reg = devm_regulator_get_optional(dev, "fan");
> > +	if (IS_ERR(data->reg)) {
> > +		if (PTR_ERR(data->reg) != -ENODEV) {
> > +			err = (int)PTR_ERR(data->reg);
> > +			dev_warn(dev, "Failed looking up fan supply: %d\n", err);
> 
> This could be -EPROBE_DEFER. I do not think that warrants
> an error message.

I see. I'll adapt it analog to: drivers/hwmon/pwm-fan.c:323

> 
> > +			return err;
> > +		}
> > +		data->reg = NULL;
> > +	} else {
> > +		/* Spin up fans */
> > +		err = regulator_enable(data->reg);
> > +		if (err) {
> > +			dev_err(dev, "Failed to enable fan supply: %d\n", err);
> > +			return err;
> > +		}
> > +		err = devm_add_action_or_reset(dev, max6639_regulator_disable,
> > +					       data->reg);
> > +		if (err) {
> > +			dev_err(dev, "Failed to register action: %d\n", err);
> > +			return err;
> > +		}
> > +	}
> > +
> >  	mutex_init(&data->update_lock);
> >  
> >  	/* Initialize the max6639 chip */
> > @@ -545,23 +577,39 @@ static int max6639_probe(struct i2c_client *client)
> >  static int max6639_suspend(struct device *dev)
> >  {
> >  	struct i2c_client *client = to_i2c_client(dev);
> > -	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
> > -	if (data < 0)
> > -		return data;
> > +	struct max6639_data *data = dev_get_drvdata(dev);
> > +	int ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
> > +
> > +	if (ret < 0)
> > +		return ret;
> > +
> > +	if (data->reg)
> > +		regulator_disable(data->reg);
> >  
> >  	return i2c_smbus_write_byte_data(client,
> > -			MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
> > +			MAX6639_REG_GCONFIG, ret | MAX6639_GCONFIG_STANDBY);
> >  }
> >  
> >  static int max6639_resume(struct device *dev)
> >  {
> >  	struct i2c_client *client = to_i2c_client(dev);
> > -	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
> > -	if (data < 0)
> > -		return data;
> > +	struct max6639_data *data = dev_get_drvdata(dev);
> > +	int ret;
> > +
> > +	if (data->reg) {
> > +		ret = regulator_enable(data->reg);
> > +		if (ret) {
> > +			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
> > +			return ret;
> > +		}
> > +	}
> > +
> > +	ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
> > +	if (ret < 0)
> > +		return ret;
> >  
> >  	return i2c_smbus_write_byte_data(client,
> > -			MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
> > +			MAX6639_REG_GCONFIG, ret & ~MAX6639_GCONFIG_STANDBY);
> >  }
> >  #endif /* CONFIG_PM_SLEEP */
> >
diff mbox series

Patch

diff --git a/drivers/hwmon/max6639.c b/drivers/hwmon/max6639.c
index ccc0f047bd44..0b241f029229 100644
--- a/drivers/hwmon/max6639.c
+++ b/drivers/hwmon/max6639.c
@@ -87,6 +87,9 @@  struct max6639_data {
 	/* Register values initialized only once */
 	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */
 	u8 rpm_range;		/* Index in above rpm_ranges table */
+
+	/* Optional regulator for FAN supply */
+	struct regulator *reg;
 };
 
 static struct max6639_data *max6639_update_device(struct device *dev)
@@ -516,6 +519,11 @@  static int max6639_detect(struct i2c_client *client,
 	return 0;
 }
 
+static void max6639_regulator_disable(void *data)
+{
+	regulator_disable(data);
+}
+
 static int max6639_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -528,6 +536,30 @@  static int max6639_probe(struct i2c_client *client)
 		return -ENOMEM;
 
 	data->client = client;
+
+	data->reg = devm_regulator_get_optional(dev, "fan");
+	if (IS_ERR(data->reg)) {
+		if (PTR_ERR(data->reg) != -ENODEV) {
+			err = (int)PTR_ERR(data->reg);
+			dev_warn(dev, "Failed looking up fan supply: %d\n", err);
+			return err;
+		}
+		data->reg = NULL;
+	} else {
+		/* Spin up fans */
+		err = regulator_enable(data->reg);
+		if (err) {
+			dev_err(dev, "Failed to enable fan supply: %d\n", err);
+			return err;
+		}
+		err = devm_add_action_or_reset(dev, max6639_regulator_disable,
+					       data->reg);
+		if (err) {
+			dev_err(dev, "Failed to register action: %d\n", err);
+			return err;
+		}
+	}
+
 	mutex_init(&data->update_lock);
 
 	/* Initialize the max6639 chip */
@@ -545,23 +577,39 @@  static int max6639_probe(struct i2c_client *client)
 static int max6639_suspend(struct device *dev)
 {
 	struct i2c_client *client = to_i2c_client(dev);
-	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
-	if (data < 0)
-		return data;
+	struct max6639_data *data = dev_get_drvdata(dev);
+	int ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
+
+	if (ret < 0)
+		return ret;
+
+	if (data->reg)
+		regulator_disable(data->reg);
 
 	return i2c_smbus_write_byte_data(client,
-			MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
+			MAX6639_REG_GCONFIG, ret | MAX6639_GCONFIG_STANDBY);
 }
 
 static int max6639_resume(struct device *dev)
 {
 	struct i2c_client *client = to_i2c_client(dev);
-	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
-	if (data < 0)
-		return data;
+	struct max6639_data *data = dev_get_drvdata(dev);
+	int ret;
+
+	if (data->reg) {
+		ret = regulator_enable(data->reg);
+		if (ret) {
+			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
+			return ret;
+		}
+	}
+
+	ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
+	if (ret < 0)
+		return ret;
 
 	return i2c_smbus_write_byte_data(client,
-			MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
+			MAX6639_REG_GCONFIG, ret & ~MAX6639_GCONFIG_STANDBY);
 }
 #endif /* CONFIG_PM_SLEEP */