hwmon: pwm-fan: switch regulator dynamically
Subsystems (3)
v5 cover hwmon: pwm-fan: switch regulator dynamically
Hello everyone,
this is the next version for supporting switching off the pwm-fan regulator. Review points from Guenter are included.
Changes in v5:
* Added Guenter's R-b to Patch 1 & 4
* Removed useless goto (Patch 2)
* Added error checking when switching off PWM (Patch 2)
* Removed useless branches when switching power (Patch 3)
* Removed useless (temporary) variable (Patch 3)
* Updated Patch 5 to changes of previous patches
* Dropped Patch 6 completly for lack of benefit
Changes in v4:
* Reordered commits so current behavior is the default all the time
* Fixed pwm state bug in Patch 1, this affects the patch context in Patch 2
* Refactor even more code in Patch 3 & 4 for smaller further patches
* Squashed the dynamic regulator switch patch and the sysfs attribute patch into one, keeping default behavior.
Overhaul the patch to support different modes altogether
* Fixed bugs in module removal in pwm1_enable=1
* Moved internal PWM state removal to the end to keep the patch smaller
Best regards,
Alexander
Alexander Stein (5):
hwmon: pwm-fan: Refactor fan power on/off
hwmon: pwm-fan: Simplify enable/disable check
hwmon: pwm-fan: Add dedicated power switch function
hwmon: pwm-fan: split __set_pwm into locked/unlocked functions
hwmon: pwm-fan: Switch regulator dynamically
Documentation/hwmon/pwm-fan.rst | 12 ++ drivers/hwmon/pwm-fan.c | 312 +++++++++++++++++++++++--------- 2 files changed, 241 insertions(+), 83 deletions(-)
Signature · 1 line
2.25.1
PATCH v5 1/5 hwmon: pwm-fan: Refactor fan power on/off
In preparation for dynamically switching regulator, split the power on and power off sequence into separate functions.
--- drivers/hwmon/pwm-fan.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-)
drivers/hwmon/pwm-fan.c +30 -6
@@ -82,23 +82,47 @@ static void sample_timer(struct timer_list *t)mod_timer (&ctx->rpm_timer, jiffies +HZ ); } -static int __set_pwm (struct pwm_fan_ctx *ctx,unsigned long pwm) +static int pwm_fan_power_on (struct pwm_fan_ctx *ctx) { +struct pwm_state *state = &ctx->pwm_state;unsigned long period; -int ret =0 ; +int ret; + + period = state->period; + state->duty_cycle =DIV_ROUND_UP (ctx->pwm_value * (period -1 ),MAX_PWM ); + state->enabled = true; + ret =pwm_apply_state (ctx->pwm, state); + +return ret; +} + +static int pwm_fan_power_off (struct pwm_fan_ctx *ctx) +{struct pwm_state *state = &ctx->pwm_state; + state->enabled = false; + state->duty_cycle =0 ; +pwm_apply_state (ctx->pwm, state); + +return 0 ; +} + +static int __set_pwm (struct pwm_fan_ctx *ctx,unsigned long pwm) +{ +int ret =0 ; +mutex_lock (&ctx->lock);if (ctx->pwm_value == pwm) goto exit_set_pwm_err; - period = state->period; - state->duty_cycle =DIV_ROUND_UP (pwm * (period -1 ),MAX_PWM ); - state->enabled = pwm ? true : false; +if (pwm >0 ) + ret =pwm_fan_power_on (ctx); +else + ret =pwm_fan_power_off (ctx); - ret =pwm_apply_state (ctx->pwm, state);if (!ret) ctx->pwm_value = pwm; + exit_set_pwm_err:mutex_unlock (&ctx->lock);return ret;
Signature · 1 line
2.25.1
Re: [PATCH v5 1/5] hwmon: pwm-fan: Refactor fan power on/off
Quoted 5 lines · Alexander Stein
In preparation for dynamically switching regulator, split the power on and power off sequence into separate functions.
Signed-off-by Alexander Stein <[email protected]>Reviewed-by Guenter Roeck <[email protected]>
Applied to hwmon-next.
Thanks,
Guenter
PATCH v5 2/5 hwmon: pwm-fan: Simplify enable/disable check
In reply to Alexander SteinInstead of comparing the current to the new pwm duty to decide whether to enable the PWM, use a dedicated flag. Also apply the new PWM duty in any case. This is a preparation to enable/disable the regulator dynamically.
--- drivers/hwmon/pwm-fan.c | 43 ++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-)
drivers/hwmon/pwm-fan.c +34 -9
@@ -29,10 +29,13 @@ struct pwm_fan_tach { };struct pwm_fan_ctx { +struct device *dev; +struct mutex lock;struct pwm_device *pwm;struct pwm_state pwm_state;struct regulator *reg_en; +bool enabled;int tach_count;struct pwm_fan_tach *tachs;
@@ -85,13 +88,19 @@ static void sample_timer(struct timer_list *t)static int pwm_fan_power_on (struct pwm_fan_ctx *ctx) {struct pwm_state *state = &ctx->pwm_state; -unsigned long period;int ret; - period = state->period; - state->duty_cycle =DIV_ROUND_UP (ctx->pwm_value * (period -1 ),MAX_PWM ); +if (ctx->enabled) +return 0 ; + state->enabled = true; ret =pwm_apply_state (ctx->pwm, state); +if (ret) { +dev_err (ctx->dev,"failed to enable PWM\n" ); +return ret; + } + + ctx->enabled = true;return ret; }
@@ -99,27 +108,42 @@ static int pwm_fan_power_on(struct pwm_fan_ctx *ctx)static int pwm_fan_power_off (struct pwm_fan_ctx *ctx) {struct pwm_state *state = &ctx->pwm_state; +int ret; + +if (!ctx->enabled) +return 0 ; state->enabled = false; state->duty_cycle =0 ; -pwm_apply_state (ctx->pwm, state); + ret =pwm_apply_state (ctx->pwm, state); +if (ret) { +dev_err (ctx->dev,"failed to disable PWM\n" ); +return ret; + } + + ctx->enabled = false;return 0 ; }static int __set_pwm (struct pwm_fan_ctx *ctx,unsigned long pwm) { +struct pwm_state *state = &ctx->pwm_state; +unsigned long period;int ret =0 ;mutex_lock (&ctx->lock); -if (ctx->pwm_value == pwm) - goto exit_set_pwm_err; -if (pwm >0 ) +if (pwm >0 ) { + period = state->period; + state->duty_cycle =DIV_ROUND_UP (pwm * (period -1 ),MAX_PWM ); + ret =pwm_apply_state (ctx->pwm, state); +if (ret) + goto exit_set_pwm_err; ret =pwm_fan_power_on (ctx); -else + }else { ret =pwm_fan_power_off (ctx); - + }if (!ret) ctx->pwm_value = pwm;
@@ -326,6 +350,7 @@ static int pwm_fan_probe(struct platform_device *pdev)mutex_init (&ctx->lock); + ctx->dev = &pdev->dev; ctx->pwm =devm_pwm_get (dev,NULL );if (IS_ERR (ctx->pwm))return dev_err_probe (dev,PTR_ERR (ctx->pwm),"Could not get PWM\n" );
Signature · 1 line
2.25.1
Re: [PATCH v5 2/5] hwmon: pwm-fan: Simplify enable/disable check
Quoted 5 lines · Alexander Stein
Instead of comparing the current to the new pwm duty to decide whether to enable the PWM, use a dedicated flag. Also apply the new PWM duty in any case. This is a preparation to enable/disable the regulator dynamically.
Signed-off-by Alexander Stein <[email protected]>
Applied to hwmon-next
Thanks,
Guenter
PATCH v5 3/5 hwmon: pwm-fan: Add dedicated power switch function
In reply to Alexander SteinThis 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.
--- drivers/hwmon/pwm-fan.c | 46 +++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 15 deletions(-)
drivers/hwmon/pwm-fan.c +31 -15
@@ -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,25 @@ 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 =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; + } +return ret; +} +static int pwm_fan_power_on (struct pwm_fan_ctx *ctx) {struct pwm_state *state = &ctx->pwm_state;
@@ -320,7 +340,7 @@ static int pwm_fan_of_get_cooling_data(struct device *dev,static void pwm_fan_regulator_disable (void *data) { -regulator_disable (data); +pwm_fan_switch_power (data, false); }static void pwm_fan_pwm_disable (void *__ctx)
@@ -364,13 +384,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; }
@@ -516,12 +536,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 ;
@@ -543,12 +561,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 )
Signature · 1 line
2.25.1
Re: [PATCH v5 3/5] hwmon: pwm-fan: Add dedicated power switch function
Quoted 5 lines · Alexander Stein
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 <[email protected]>
Applied to hwmon-next.
Thanks,
Guenter
PATCH v5 4/5 hwmon: pwm-fan: split __set_pwm into locked/unlocked functions
In reply to Alexander SteinRegular calls to set_pwm don't hold the mutex, but the upcoming update_enable support needs to call set_pwm with the mutex being held. So provide the previous behavior in set_pwm (handling the lock), while adding __set_pwm which assumes the lock being held.
--- drivers/hwmon/pwm-fan.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
drivers/hwmon/pwm-fan.c +15 -8
@@ -152,14 +152,12 @@ static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)unsigned long period;int ret =0 ; -mutex_lock (&ctx->lock); -if (pwm >0 ) { period = state->period; state->duty_cycle =DIV_ROUND_UP (pwm * (period -1 ),MAX_PWM ); ret =pwm_apply_state (ctx->pwm, state);if (ret) - goto exit_set_pwm_err; +return ret; ret =pwm_fan_power_on (ctx); }else { ret =pwm_fan_power_off (ctx);
@@ -167,8 +165,17 @@ static int __set_pwm(struct pwm_fan_ctx *ctx, unsigned long pwm)if (!ret) ctx->pwm_value = pwm; -exit_set_pwm_err: +return ret; +} + +static int set_pwm (struct pwm_fan_ctx *ctx,unsigned long pwm) +{ +int ret; + +mutex_lock (&ctx->lock); + ret =__set_pwm (ctx, pwm);mutex_unlock (&ctx->lock); +return ret; }
@@ -192,7 +199,7 @@ static int pwm_fan_write(struct device *dev, enum hwmon_sensor_types type,if (val <0 || val >MAX_PWM )return -EINVAL ; - ret =__set_pwm (ctx, val); + ret =set_pwm (ctx, val);if (ret)return ret;
@@ -280,7 +287,7 @@ pwm_fan_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)if (state == ctx->pwm_fan_state)return 0 ; - ret =__set_pwm (ctx, ctx->pwm_fan_cooling_levels[state]); + ret =set_pwm (ctx, ctx->pwm_fan_cooling_levels[state]);if (ret) {dev_err (&cdev->device,"Cannot set pwm!\n" );return ret;
@@ -398,7 +405,7 @@ static int pwm_fan_probe(struct platform_device *pdev)pwm_init_state (ctx->pwm, &ctx->pwm_state);/* -* __set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned +* set_pwm assumes that MAX_PWM * (period - 1) fits into an unsigned * long. Check this here to prevent the fan running at a too low * frequency. */
@@ -408,7 +415,7 @@ static int pwm_fan_probe(struct platform_device *pdev) }/* Set duty cycle to maximum allowed and enable PWM output */ - ret =__set_pwm (ctx,MAX_PWM ); + ret =set_pwm (ctx,MAX_PWM );if (ret) {dev_err (dev,"Failed to configure PWM: %d\n" , ret);return ret;
Signature · 1 line
2.25.1
Re: [PATCH v5 4/5] hwmon: pwm-fan: split __set_pwm into locked/unlocked functions
Quoted 7 lines · Alexander Stein
Regular calls to
set_pwmdon't hold the mutex, but the upcomingupdate_enablesupport needs to callset_pwmwith the mutex being held. So provide the previous behavior inset_pwm(handling the lock), while adding__set_pwmwhich assumes the lock being held.Signed-off-by Alexander Stein <[email protected]>Reviewed-by Guenter Roeck <[email protected]>
Applied to hwmon-next.
Thanks,
Guenter
PATCH v5 5/5 hwmon: pwm-fan: Switch regulator dynamically
In reply to Alexander SteinThis adds the enable attribute which is used to select if zero PWM duty means to switch off regulator and PWM or to keep them enabled but at inactive PWM output level.
Depending on the select enable mode, turn off the regulator and PWM if the PWM duty is zero, or keep them enabled.
This is especially important for fan using inverted PWM signal polarity. Having regulator supplied and PWM disabled, some PWM controllers provide the active, rather than inactive signal.
With this change the shutdown as well as suspend/resume paths require modifcations as well.
--- Documentation/hwmon/pwm-fan.rst | 12 ++ drivers/hwmon/pwm-fan.c | 210 +++++++++++++++++++++----------- 2 files changed, 154 insertions(+), 68 deletions(-)
Documentation/hwmon/pwm-fan.rst +12 -0
@@ -18,3 +18,15 @@ the hwmon's sysfs interface. The fan rotation speed returned via the optional 'fan1_input' is extrapolated from the sampled interrupts from the tachometer signal within 1 second. + +The driver provides the following sensor accesses in sysfs: + +=============== ======= ======================================================= +fan1_input ro fan tachometer speed in RPM +pwm1_enable rw keep enable mode, defines behaviour when pwm1=0 + 0 -> disable pwm and regulator + 1 -> enable pwm; if pwm==0, disable pwm, keep regulator enabled + 2 -> enable pwm; if pwm==0, keep pwm and regulator enabled + 3 -> enable pwm; if pwm==0, disable pwm and regulator +pwm1 rw relative speed (0-255), 255=max. speed. +=============== ======= =======================================================
drivers/hwmon/pwm-fan.c +142 -68
339 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v5 5/5] hwmon: pwm-fan: Switch regulator dynamically
Quoted 9 lines · Alexander Stein
This adds the enable attribute which is used to select if zero PWM duty means to switch off regulator and PWM or to keep them enabled but at inactive PWM output level.
Depending on the select enable mode, turn off the regulator and PWM if the PWM duty is zero, or keep them enabled.
This is especially important for fan using inverted PWM signal polarity. Having regulator supplied and PWM disabled, some PWM controllers provide the active, rather than inactive signal.
Quoted 4 lines
With this change the shutdown as well as suspend/resume paths require modifcations as well.
Signed-off-by Alexander Stein <[email protected]>
Applied to hwmon-next.
Thanks,
Guenter