Linux Hardware Monitor development
 help / color / mirror / Atom feed
From: Guenter Roeck <linux@roeck-us.net>
To: Alexander Stein <alexander.stein@ew.tq-group.com>
Cc: "Jean Delvare" <jdelvare@suse.com>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Thierry Reding" <thierry.reding@gmail.com>,
	"Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>,
	"Lee Jones" <lee.jones@linaro.org>,
	linux-hwmon@vger.kernel.org, linux-pwm@vger.kernel.org,
	"Markus Niebel" <Markus.Niebel@ew.tq-group.com>
Subject: Re: [PATCH v4 3/6] hwmon: pwm-fan: Add dedicated power switch function
Date: Tue, 30 Aug 2022 06:50:13 -0700	[thread overview]
Message-ID: <20220830135013.GA229925@roeck-us.net> (raw)
In-Reply-To: <20220523110513.407516-4-alexander.stein@ew.tq-group.com>

On Mon, May 23, 2022 at 01:05:10PM +0200, Alexander Stein wrote:
> This handles enabling/disabling the regulator in a single function, while
> keeping the enables/disabled balanced. This is a preparation when
> regulator is switched from different code paths.
> 
> Signed-off-by: Alexander Stein <alexander.stein@ew.tq-group.com>
> ---
>  drivers/hwmon/pwm-fan.c | 52 +++++++++++++++++++++++++++++------------
>  1 file changed, 37 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/hwmon/pwm-fan.c b/drivers/hwmon/pwm-fan.c
> index 96b10d422828..04af24268963 100644
> --- a/drivers/hwmon/pwm-fan.c
> +++ b/drivers/hwmon/pwm-fan.c
> @@ -35,6 +35,7 @@ struct pwm_fan_ctx {
>  	struct pwm_device *pwm;
>  	struct pwm_state pwm_state;
>  	struct regulator *reg_en;
> +	bool regulator_enabled;
>  	bool enabled;
>  
>  	int tach_count;
> @@ -85,6 +86,29 @@ static void sample_timer(struct timer_list *t)
>  	mod_timer(&ctx->rpm_timer, jiffies + HZ);
>  }
>  
> +static int pwm_fan_switch_power(struct pwm_fan_ctx *ctx, bool on)
> +{
> +	int ret = 0;
> +
> +	if (!ctx->reg_en)
> +		return ret;
> +
> +	if (ctx->regulator_enabled && on) {
> +		ret = 0;

ret is already 0 here.

> +	} else if (!ctx->regulator_enabled && on) {
> +		ret = regulator_enable(ctx->reg_en);
> +		if (ret == 0)
> +			ctx->regulator_enabled = true;
> +	} else if (ctx->regulator_enabled && !on) {
> +		ret = regulator_disable(ctx->reg_en);
> +		if (ret == 0)
> +			ctx->regulator_enabled = false;
> +	} else if (!ctx->regulator_enabled && !on) {
> +		ret = 0;

ret is already 0 here.

> +	}
> +	return ret;
> +}
> +
>  static int pwm_fan_power_on(struct pwm_fan_ctx *ctx)
>  {
>  	struct pwm_state *state = &ctx->pwm_state;
> @@ -316,7 +340,9 @@ static int pwm_fan_of_get_cooling_data(struct device *dev,
>  
>  static void pwm_fan_regulator_disable(void *data)
>  {
> -	regulator_disable(data);
> +	struct pwm_fan_ctx *ctx = data;
> +
> +	pwm_fan_switch_power(ctx, false);

You can directly pass 'data' as argument here; there is no need
for the extra variable.

>  }
>  
>  static void pwm_fan_pwm_disable(void *__ctx)
> @@ -360,13 +386,13 @@ static int pwm_fan_probe(struct platform_device *pdev)
>  
>  		ctx->reg_en = NULL;
>  	} else {
> -		ret = regulator_enable(ctx->reg_en);
> +		ret = pwm_fan_switch_power(ctx, true);
>  		if (ret) {
>  			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
>  			return ret;
>  		}
>  		ret = devm_add_action_or_reset(dev, pwm_fan_regulator_disable,
> -					       ctx->reg_en);
> +					       ctx);
>  		if (ret)
>  			return ret;
>  	}
> @@ -512,12 +538,10 @@ static int pwm_fan_disable(struct device *dev)
>  			return ret;
>  	}
>  
> -	if (ctx->reg_en) {
> -		ret = regulator_disable(ctx->reg_en);
> -		if (ret) {
> -			dev_err(dev, "Failed to disable fan supply: %d\n", ret);
> -			return ret;
> -		}
> +	ret = pwm_fan_switch_power(ctx, false);
> +	if (ret) {
> +		dev_err(dev, "Failed to disable fan supply: %d\n", ret);
> +		return ret;
>  	}
>  
>  	return 0;
> @@ -539,12 +563,10 @@ static int pwm_fan_resume(struct device *dev)
>  	struct pwm_fan_ctx *ctx = dev_get_drvdata(dev);
>  	int ret;
>  
> -	if (ctx->reg_en) {
> -		ret = regulator_enable(ctx->reg_en);
> -		if (ret) {
> -			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
> -			return ret;
> -		}
> +	ret = pwm_fan_switch_power(ctx, true);
> +	if (ret) {
> +		dev_err(dev, "Failed to enable fan supply: %d\n", ret);
> +		return ret;
>  	}
>  
>  	if (ctx->pwm_value == 0)

  reply	other threads:[~2022-08-30 13:52 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-23 11:05 [PATCH v4 0/6] hwmon: pwm-fan: switch regulator dynamically Alexander Stein
2022-05-23 11:05 ` [PATCH v4 1/6] hwmon: pwm-fan: Refactor fan power on/off Alexander Stein
2022-08-30 13:45   ` Guenter Roeck
2022-05-23 11:05 ` [PATCH v4 2/6] hwmon: pwm-fan: Simplify enable/disable check Alexander Stein
2022-08-30 13:43   ` Guenter Roeck
2022-09-14 15:06     ` Alexander Stein
2022-05-23 11:05 ` [PATCH v4 3/6] hwmon: pwm-fan: Add dedicated power switch function Alexander Stein
2022-08-30 13:50   ` Guenter Roeck [this message]
2022-09-14 15:10     ` Alexander Stein
2022-05-23 11:05 ` [PATCH v4 4/6] hwmon: pwm-fan: split __set_pwm into locked/unlocked functions Alexander Stein
2022-08-30 13:52   ` Guenter Roeck
2022-05-23 11:05 ` [PATCH v4 5/6] hwmon: pwm-fan: Switch regulator dynamically Alexander Stein
2022-08-30 14:01   ` Guenter Roeck
2022-05-23 11:05 ` [PATCH v4 6/6] hwmon: pwm-fan: Remove internal duplicated pwm_state Alexander Stein
2022-05-23 12:46   ` Uwe Kleine-König
2022-05-23 13:55     ` Alexander Stein
2022-05-23 14:18       ` Guenter Roeck
2022-06-21  6:41         ` Alexander Stein
2022-06-21  7:22           ` Uwe Kleine-König

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=20220830135013.GA229925@roeck-us.net \
    --to=linux@roeck-us.net \
    --cc=Markus.Niebel@ew.tq-group.com \
    --cc=alexander.stein@ew.tq-group.com \
    --cc=corbet@lwn.net \
    --cc=jdelvare@suse.com \
    --cc=lee.jones@linaro.org \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-pwm@vger.kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=u.kleine-koenig@pengutronix.de \
    /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