20
20
#include <linux/gpio/consumer.h>
21
21
#include <linux/of.h>
22
22
#include <linux/of_platform.h>
23
+ #include <linux/pm.h>
24
+ #include <linux/pm_runtime.h>
25
+ #include <linux/regulator/consumer.h>
23
26
#include <linux/thermal.h>
24
27
25
28
struct gpio_fan_speed {
@@ -42,6 +45,7 @@ struct gpio_fan_data {
42
45
bool pwm_enable ;
43
46
struct gpio_desc * alarm_gpio ;
44
47
struct work_struct alarm_work ;
48
+ struct regulator * supply ;
45
49
};
46
50
47
51
/*
@@ -125,13 +129,32 @@ static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
125
129
}
126
130
127
131
/* Must be called with fan_data->lock held, except during initialization. */
128
- static void set_fan_speed (struct gpio_fan_data * fan_data , int speed_index )
132
+ static int set_fan_speed (struct gpio_fan_data * fan_data , int speed_index )
129
133
{
130
134
if (fan_data -> speed_index == speed_index )
131
- return ;
135
+ return 0 ;
136
+
137
+ if (fan_data -> speed_index == 0 && speed_index > 0 ) {
138
+ int ret ;
139
+
140
+ ret = pm_runtime_resume_and_get (fan_data -> dev );
141
+ if (ret < 0 )
142
+ return ret ;
143
+ }
132
144
133
145
__set_fan_ctrl (fan_data , fan_data -> speed [speed_index ].ctrl_val );
146
+
147
+ if (fan_data -> speed_index > 0 && speed_index == 0 ) {
148
+ int ret ;
149
+
150
+ ret = pm_runtime_put_sync (fan_data -> dev );
151
+ if (ret < 0 )
152
+ return ret ;
153
+ }
154
+
134
155
fan_data -> speed_index = speed_index ;
156
+
157
+ return 0 ;
135
158
}
136
159
137
160
static int get_fan_speed_index (struct gpio_fan_data * fan_data )
@@ -176,7 +199,7 @@ static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
176
199
struct gpio_fan_data * fan_data = dev_get_drvdata (dev );
177
200
unsigned long pwm ;
178
201
int speed_index ;
179
- int ret = count ;
202
+ int ret ;
180
203
181
204
if (kstrtoul (buf , 10 , & pwm ) || pwm > 255 )
182
205
return - EINVAL ;
@@ -189,12 +212,12 @@ static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
189
212
}
190
213
191
214
speed_index = DIV_ROUND_UP (pwm * (fan_data -> num_speed - 1 ), 255 );
192
- set_fan_speed (fan_data , speed_index );
215
+ ret = set_fan_speed (fan_data , speed_index );
193
216
194
217
exit_unlock :
195
218
mutex_unlock (& fan_data -> lock );
196
219
197
- return ret ;
220
+ return ret ? ret : count ;
198
221
}
199
222
200
223
static ssize_t pwm1_enable_show (struct device * dev ,
@@ -211,6 +234,7 @@ static ssize_t pwm1_enable_store(struct device *dev,
211
234
{
212
235
struct gpio_fan_data * fan_data = dev_get_drvdata (dev );
213
236
unsigned long val ;
237
+ int ret = 0 ;
214
238
215
239
if (kstrtoul (buf , 10 , & val ) || val > 1 )
216
240
return - EINVAL ;
@@ -224,11 +248,11 @@ static ssize_t pwm1_enable_store(struct device *dev,
224
248
225
249
/* Disable manual control mode: set fan at full speed. */
226
250
if (val == 0 )
227
- set_fan_speed (fan_data , fan_data -> num_speed - 1 );
251
+ ret = set_fan_speed (fan_data , fan_data -> num_speed - 1 );
228
252
229
253
mutex_unlock (& fan_data -> lock );
230
254
231
- return count ;
255
+ return ret ? ret : count ;
232
256
}
233
257
234
258
static ssize_t pwm1_mode_show (struct device * dev ,
@@ -279,7 +303,7 @@ static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
279
303
goto exit_unlock ;
280
304
}
281
305
282
- set_fan_speed (fan_data , rpm_to_speed_index (fan_data , rpm ));
306
+ ret = set_fan_speed (fan_data , rpm_to_speed_index (fan_data , rpm ));
283
307
284
308
exit_unlock :
285
309
mutex_unlock (& fan_data -> lock );
@@ -386,6 +410,7 @@ static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
386
410
unsigned long state )
387
411
{
388
412
struct gpio_fan_data * fan_data = cdev -> devdata ;
413
+ int ret ;
389
414
390
415
if (!fan_data )
391
416
return - EINVAL ;
@@ -395,11 +420,11 @@ static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
395
420
396
421
mutex_lock (& fan_data -> lock );
397
422
398
- set_fan_speed (fan_data , state );
423
+ ret = set_fan_speed (fan_data , state );
399
424
400
425
mutex_unlock (& fan_data -> lock );
401
426
402
- return 0 ;
427
+ return ret ;
403
428
}
404
429
405
430
static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
@@ -499,6 +524,8 @@ static void gpio_fan_stop(void *data)
499
524
mutex_lock (& fan_data -> lock );
500
525
set_fan_speed (data , 0 );
501
526
mutex_unlock (& fan_data -> lock );
527
+
528
+ pm_runtime_disable (fan_data -> dev );
502
529
}
503
530
504
531
static int gpio_fan_probe (struct platform_device * pdev )
@@ -521,6 +548,11 @@ static int gpio_fan_probe(struct platform_device *pdev)
521
548
platform_set_drvdata (pdev , fan_data );
522
549
mutex_init (& fan_data -> lock );
523
550
551
+ fan_data -> supply = devm_regulator_get (dev , "fan" );
552
+ if (IS_ERR (fan_data -> supply ))
553
+ return dev_err_probe (dev , PTR_ERR (fan_data -> supply ),
554
+ "Failed to get fan-supply" );
555
+
524
556
/* Configure control GPIOs if available. */
525
557
if (fan_data -> gpios && fan_data -> num_gpios > 0 ) {
526
558
if (!fan_data -> speed || fan_data -> num_speed <= 1 )
@@ -548,6 +580,17 @@ static int gpio_fan_probe(struct platform_device *pdev)
548
580
return err ;
549
581
}
550
582
583
+ pm_runtime_set_suspended (& pdev -> dev );
584
+ pm_runtime_enable (& pdev -> dev );
585
+ /* If current GPIO state is active, mark RPM as active as well */
586
+ if (fan_data -> speed_index > 0 ) {
587
+ int ret ;
588
+
589
+ ret = pm_runtime_resume_and_get (& pdev -> dev );
590
+ if (ret )
591
+ return ret ;
592
+ }
593
+
551
594
/* Optional cooling device register for Device tree platforms */
552
595
fan_data -> cdev = devm_thermal_of_cooling_device_register (dev , np ,
553
596
"gpio-fan" , fan_data , & gpio_fan_cool_ops );
@@ -565,41 +608,69 @@ static void gpio_fan_shutdown(struct platform_device *pdev)
565
608
set_fan_speed (fan_data , 0 );
566
609
}
567
610
611
+ static int gpio_fan_runtime_suspend (struct device * dev )
612
+ {
613
+ struct gpio_fan_data * fan_data = dev_get_drvdata (dev );
614
+ int ret = 0 ;
615
+
616
+ if (fan_data -> supply )
617
+ ret = regulator_disable (fan_data -> supply );
618
+
619
+ return ret ;
620
+ }
621
+
622
+ static int gpio_fan_runtime_resume (struct device * dev )
623
+ {
624
+ struct gpio_fan_data * fan_data = dev_get_drvdata (dev );
625
+ int ret = 0 ;
626
+
627
+ if (fan_data -> supply )
628
+ ret = regulator_enable (fan_data -> supply );
629
+
630
+ return ret ;
631
+ }
632
+
568
633
static int gpio_fan_suspend (struct device * dev )
569
634
{
570
635
struct gpio_fan_data * fan_data = dev_get_drvdata (dev );
636
+ int ret = 0 ;
571
637
572
638
if (fan_data -> gpios ) {
573
639
fan_data -> resume_speed = fan_data -> speed_index ;
574
640
mutex_lock (& fan_data -> lock );
575
- set_fan_speed (fan_data , 0 );
641
+ ret = set_fan_speed (fan_data , 0 );
576
642
mutex_unlock (& fan_data -> lock );
577
643
}
578
644
579
- return 0 ;
645
+ return ret ;
580
646
}
581
647
582
648
static int gpio_fan_resume (struct device * dev )
583
649
{
584
650
struct gpio_fan_data * fan_data = dev_get_drvdata (dev );
651
+ int ret = 0 ;
585
652
586
653
if (fan_data -> gpios ) {
587
654
mutex_lock (& fan_data -> lock );
588
- set_fan_speed (fan_data , fan_data -> resume_speed );
655
+ ret = set_fan_speed (fan_data , fan_data -> resume_speed );
589
656
mutex_unlock (& fan_data -> lock );
590
657
}
591
658
592
- return 0 ;
659
+ return ret ;
593
660
}
594
661
595
- static DEFINE_SIMPLE_DEV_PM_OPS (gpio_fan_pm , gpio_fan_suspend , gpio_fan_resume ) ;
662
+ static const struct dev_pm_ops gpio_fan_pm = {
663
+ RUNTIME_PM_OPS (gpio_fan_runtime_suspend ,
664
+ gpio_fan_runtime_resume , NULL )
665
+ SYSTEM_SLEEP_PM_OPS (gpio_fan_suspend , gpio_fan_resume )
666
+ };
596
667
597
668
static struct platform_driver gpio_fan_driver = {
598
669
.probe = gpio_fan_probe ,
599
670
.shutdown = gpio_fan_shutdown ,
600
671
.driver = {
601
672
.name = "gpio-fan" ,
602
- .pm = pm_sleep_ptr (& gpio_fan_pm ),
673
+ .pm = pm_ptr (& gpio_fan_pm ),
603
674
.of_match_table = of_gpio_fan_match ,
604
675
},
605
676
};
0 commit comments