23
23
#include <linux/of.h>
24
24
#include <linux/platform_device.h>
25
25
#include <linux/pwm.h>
26
+ #include <linux/regulator/consumer.h>
26
27
#include <linux/sysfs.h>
27
28
#include <linux/thermal.h>
28
29
31
32
struct pwm_fan_ctx {
32
33
struct mutex lock ;
33
34
struct pwm_device * pwm ;
35
+ struct regulator * reg_en ;
34
36
unsigned int pwm_value ;
35
37
unsigned int pwm_fan_state ;
36
38
unsigned int pwm_fan_max_state ;
@@ -231,6 +233,21 @@ static int pwm_fan_probe(struct platform_device *pdev)
231
233
232
234
platform_set_drvdata (pdev , ctx );
233
235
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
+
234
251
ctx -> pwm_value = MAX_PWM ;
235
252
236
253
/* Set duty cycle to maximum allowed and enable PWM output */
@@ -241,7 +258,7 @@ static int pwm_fan_probe(struct platform_device *pdev)
241
258
ret = pwm_apply_state (ctx -> pwm , & state );
242
259
if (ret ) {
243
260
dev_err (& pdev -> dev , "Failed to configure PWM\n" );
244
- return ret ;
261
+ goto err_reg_disable ;
245
262
}
246
263
247
264
hwmon = devm_hwmon_device_register_with_groups (& pdev -> dev , "pwmfan" ,
@@ -277,6 +294,10 @@ static int pwm_fan_probe(struct platform_device *pdev)
277
294
state .enabled = false;
278
295
pwm_apply_state (ctx -> pwm , & state );
279
296
297
+ err_reg_disable :
298
+ if (ctx -> reg_en )
299
+ regulator_disable (ctx -> reg_en );
300
+
280
301
return ret ;
281
302
}
282
303
@@ -287,6 +308,10 @@ static int pwm_fan_remove(struct platform_device *pdev)
287
308
thermal_cooling_device_unregister (ctx -> cdev );
288
309
if (ctx -> pwm_value )
289
310
pwm_disable (ctx -> pwm );
311
+
312
+ if (ctx -> reg_en )
313
+ regulator_disable (ctx -> reg_en );
314
+
290
315
return 0 ;
291
316
}
292
317
@@ -307,6 +332,14 @@ static int pwm_fan_suspend(struct device *dev)
307
332
pwm_disable (ctx -> pwm );
308
333
}
309
334
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
+
310
343
return 0 ;
311
344
}
312
345
@@ -317,6 +350,14 @@ static int pwm_fan_resume(struct device *dev)
317
350
unsigned long duty ;
318
351
int ret ;
319
352
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
+
320
361
if (ctx -> pwm_value == 0 )
321
362
return 0 ;
322
363
0 commit comments