Skip to content

Commit b57e1d4

Browse files
lategoodbyegroeck
authored andcommitted
hwmon: (pwm-fan) Add optional regulator support
This adds optional regulator support to the pwm-fan driver. This is necessary for pwm fans which are powered by a switchable supply. Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
1 parent 29d013a commit b57e1d4

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

drivers/hwmon/pwm-fan.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/of.h>
2424
#include <linux/platform_device.h>
2525
#include <linux/pwm.h>
26+
#include <linux/regulator/consumer.h>
2627
#include <linux/sysfs.h>
2728
#include <linux/thermal.h>
2829

@@ -31,6 +32,7 @@
3132
struct pwm_fan_ctx {
3233
struct mutex lock;
3334
struct pwm_device *pwm;
35+
struct regulator *reg_en;
3436
unsigned int pwm_value;
3537
unsigned int pwm_fan_state;
3638
unsigned int pwm_fan_max_state;
@@ -231,6 +233,21 @@ static int pwm_fan_probe(struct platform_device *pdev)
231233

232234
platform_set_drvdata(pdev, ctx);
233235

236+
ctx->reg_en = devm_regulator_get_optional(&pdev->dev, "fan");
237+
if (IS_ERR(ctx->reg_en)) {
238+
if (PTR_ERR(ctx->reg_en) != -ENODEV)
239+
return PTR_ERR(ctx->reg_en);
240+
241+
ctx->reg_en = NULL;
242+
} else {
243+
ret = regulator_enable(ctx->reg_en);
244+
if (ret) {
245+
dev_err(&pdev->dev,
246+
"Failed to enable fan supply: %d\n", ret);
247+
return ret;
248+
}
249+
}
250+
234251
ctx->pwm_value = MAX_PWM;
235252

236253
/* Set duty cycle to maximum allowed and enable PWM output */
@@ -241,7 +258,7 @@ static int pwm_fan_probe(struct platform_device *pdev)
241258
ret = pwm_apply_state(ctx->pwm, &state);
242259
if (ret) {
243260
dev_err(&pdev->dev, "Failed to configure PWM\n");
244-
return ret;
261+
goto err_reg_disable;
245262
}
246263

247264
hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "pwmfan",
@@ -277,6 +294,10 @@ static int pwm_fan_probe(struct platform_device *pdev)
277294
state.enabled = false;
278295
pwm_apply_state(ctx->pwm, &state);
279296

297+
err_reg_disable:
298+
if (ctx->reg_en)
299+
regulator_disable(ctx->reg_en);
300+
280301
return ret;
281302
}
282303

@@ -287,6 +308,10 @@ static int pwm_fan_remove(struct platform_device *pdev)
287308
thermal_cooling_device_unregister(ctx->cdev);
288309
if (ctx->pwm_value)
289310
pwm_disable(ctx->pwm);
311+
312+
if (ctx->reg_en)
313+
regulator_disable(ctx->reg_en);
314+
290315
return 0;
291316
}
292317

@@ -307,6 +332,14 @@ static int pwm_fan_suspend(struct device *dev)
307332
pwm_disable(ctx->pwm);
308333
}
309334

335+
if (ctx->reg_en) {
336+
ret = regulator_disable(ctx->reg_en);
337+
if (ret) {
338+
dev_err(dev, "Failed to disable fan supply: %d\n", ret);
339+
return ret;
340+
}
341+
}
342+
310343
return 0;
311344
}
312345

@@ -317,6 +350,14 @@ static int pwm_fan_resume(struct device *dev)
317350
unsigned long duty;
318351
int ret;
319352

353+
if (ctx->reg_en) {
354+
ret = regulator_enable(ctx->reg_en);
355+
if (ret) {
356+
dev_err(dev, "Failed to enable fan supply: %d\n", ret);
357+
return ret;
358+
}
359+
}
360+
320361
if (ctx->pwm_value == 0)
321362
return 0;
322363

0 commit comments

Comments
 (0)