hwmon: (gpio-fan) Localize platform data
[platform/kernel/linux-starfive.git] / drivers / hwmon / gpio-fan.c
1 /*
2  * gpio-fan.c - Hwmon driver for fans connected to GPIO lines.
3  *
4  * Copyright (C) 2010 LaCie
5  *
6  * Author: Simon Guinot <sguinot@lacie.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/platform_device.h>
29 #include <linux/err.h>
30 #include <linux/mutex.h>
31 #include <linux/hwmon.h>
32 #include <linux/gpio.h>
33 #include <linux/of.h>
34 #include <linux/of_platform.h>
35 #include <linux/of_gpio.h>
36 #include <linux/thermal.h>
37
38 struct gpio_fan_alarm {
39         unsigned int    gpio;
40         unsigned int    active_low;
41 };
42
43 struct gpio_fan_speed {
44         int rpm;
45         int ctrl_val;
46 };
47
48 struct gpio_fan_platform_data {
49         int                     num_ctrl;
50         unsigned int            *ctrl;  /* fan control GPIOs. */
51         struct gpio_fan_alarm   *alarm; /* fan alarm GPIO. */
52         /*
53          * Speed conversion array: rpm from/to GPIO bit field.
54          * This array _must_ be sorted in ascending rpm order.
55          */
56         int                     num_speed;
57         struct gpio_fan_speed   *speed;
58 };
59
60 struct gpio_fan_data {
61         struct platform_device  *pdev;
62         struct device           *hwmon_dev;
63         /* Cooling device if any */
64         struct thermal_cooling_device *cdev;
65         struct mutex            lock; /* lock GPIOs operations. */
66         int                     num_ctrl;
67         unsigned                *ctrl;
68         int                     num_speed;
69         struct gpio_fan_speed   *speed;
70         int                     speed_index;
71 #ifdef CONFIG_PM_SLEEP
72         int                     resume_speed;
73 #endif
74         bool                    pwm_enable;
75         struct gpio_fan_alarm   *alarm;
76         struct work_struct      alarm_work;
77 };
78
79 /*
80  * Alarm GPIO.
81  */
82
83 static void fan_alarm_notify(struct work_struct *ws)
84 {
85         struct gpio_fan_data *fan_data =
86                 container_of(ws, struct gpio_fan_data, alarm_work);
87
88         sysfs_notify(&fan_data->pdev->dev.kobj, NULL, "fan1_alarm");
89         kobject_uevent(&fan_data->pdev->dev.kobj, KOBJ_CHANGE);
90 }
91
92 static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
93 {
94         struct gpio_fan_data *fan_data = dev_id;
95
96         schedule_work(&fan_data->alarm_work);
97
98         return IRQ_NONE;
99 }
100
101 static ssize_t fan1_alarm_show(struct device *dev,
102                                struct device_attribute *attr, char *buf)
103 {
104         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
105         struct gpio_fan_alarm *alarm = fan_data->alarm;
106         int value = gpio_get_value_cansleep(alarm->gpio);
107
108         if (alarm->active_low)
109                 value = !value;
110
111         return sprintf(buf, "%d\n", value);
112 }
113
114 static DEVICE_ATTR_RO(fan1_alarm);
115
116 static int fan_alarm_init(struct gpio_fan_data *fan_data,
117                           struct gpio_fan_alarm *alarm)
118 {
119         int err;
120         int alarm_irq;
121         struct platform_device *pdev = fan_data->pdev;
122
123         fan_data->alarm = alarm;
124
125         err = devm_gpio_request(&pdev->dev, alarm->gpio, "GPIO fan alarm");
126         if (err)
127                 return err;
128
129         err = gpio_direction_input(alarm->gpio);
130         if (err)
131                 return err;
132
133         /*
134          * If the alarm GPIO don't support interrupts, just leave
135          * without initializing the fail notification support.
136          */
137         alarm_irq = gpio_to_irq(alarm->gpio);
138         if (alarm_irq < 0)
139                 return 0;
140
141         INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
142         irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
143         err = devm_request_irq(&pdev->dev, alarm_irq, fan_alarm_irq_handler,
144                                IRQF_SHARED, "GPIO fan alarm", fan_data);
145         return err;
146 }
147
148 /*
149  * Control GPIOs.
150  */
151
152 /* Must be called with fan_data->lock held, except during initialization. */
153 static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
154 {
155         int i;
156
157         for (i = 0; i < fan_data->num_ctrl; i++)
158                 gpio_set_value_cansleep(fan_data->ctrl[i], (ctrl_val >> i) & 1);
159 }
160
161 static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
162 {
163         int i;
164         int ctrl_val = 0;
165
166         for (i = 0; i < fan_data->num_ctrl; i++) {
167                 int value;
168
169                 value = gpio_get_value_cansleep(fan_data->ctrl[i]);
170                 ctrl_val |= (value << i);
171         }
172         return ctrl_val;
173 }
174
175 /* Must be called with fan_data->lock held, except during initialization. */
176 static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
177 {
178         if (fan_data->speed_index == speed_index)
179                 return;
180
181         __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
182         fan_data->speed_index = speed_index;
183 }
184
185 static int get_fan_speed_index(struct gpio_fan_data *fan_data)
186 {
187         int ctrl_val = __get_fan_ctrl(fan_data);
188         int i;
189
190         for (i = 0; i < fan_data->num_speed; i++)
191                 if (fan_data->speed[i].ctrl_val == ctrl_val)
192                         return i;
193
194         dev_warn(&fan_data->pdev->dev,
195                  "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
196
197         return -ENODEV;
198 }
199
200 static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
201 {
202         struct gpio_fan_speed *speed = fan_data->speed;
203         int i;
204
205         for (i = 0; i < fan_data->num_speed; i++)
206                 if (speed[i].rpm >= rpm)
207                         return i;
208
209         return fan_data->num_speed - 1;
210 }
211
212 static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
213                          char *buf)
214 {
215         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
216         u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
217
218         return sprintf(buf, "%d\n", pwm);
219 }
220
221 static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
222                           const char *buf, size_t count)
223 {
224         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
225         unsigned long pwm;
226         int speed_index;
227         int ret = count;
228
229         if (kstrtoul(buf, 10, &pwm) || pwm > 255)
230                 return -EINVAL;
231
232         mutex_lock(&fan_data->lock);
233
234         if (!fan_data->pwm_enable) {
235                 ret = -EPERM;
236                 goto exit_unlock;
237         }
238
239         speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
240         set_fan_speed(fan_data, speed_index);
241
242 exit_unlock:
243         mutex_unlock(&fan_data->lock);
244
245         return ret;
246 }
247
248 static ssize_t pwm1_enable_show(struct device *dev,
249                                 struct device_attribute *attr, char *buf)
250 {
251         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
252
253         return sprintf(buf, "%d\n", fan_data->pwm_enable);
254 }
255
256 static ssize_t pwm1_enable_store(struct device *dev,
257                                  struct device_attribute *attr,
258                                  const char *buf, size_t count)
259 {
260         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
261         unsigned long val;
262
263         if (kstrtoul(buf, 10, &val) || val > 1)
264                 return -EINVAL;
265
266         if (fan_data->pwm_enable == val)
267                 return count;
268
269         mutex_lock(&fan_data->lock);
270
271         fan_data->pwm_enable = val;
272
273         /* Disable manual control mode: set fan at full speed. */
274         if (val == 0)
275                 set_fan_speed(fan_data, fan_data->num_speed - 1);
276
277         mutex_unlock(&fan_data->lock);
278
279         return count;
280 }
281
282 static ssize_t pwm1_mode_show(struct device *dev,
283                               struct device_attribute *attr, char *buf)
284 {
285         return sprintf(buf, "0\n");
286 }
287
288 static ssize_t fan1_min_show(struct device *dev,
289                              struct device_attribute *attr, char *buf)
290 {
291         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
292
293         return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
294 }
295
296 static ssize_t fan1_max_show(struct device *dev,
297                              struct device_attribute *attr, char *buf)
298 {
299         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
300
301         return sprintf(buf, "%d\n",
302                        fan_data->speed[fan_data->num_speed - 1].rpm);
303 }
304
305 static ssize_t fan1_input_show(struct device *dev,
306                                struct device_attribute *attr, char *buf)
307 {
308         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
309
310         return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
311 }
312
313 static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
314                        const char *buf, size_t count)
315 {
316         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
317         unsigned long rpm;
318         int ret = count;
319
320         if (kstrtoul(buf, 10, &rpm))
321                 return -EINVAL;
322
323         mutex_lock(&fan_data->lock);
324
325         if (!fan_data->pwm_enable) {
326                 ret = -EPERM;
327                 goto exit_unlock;
328         }
329
330         set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
331
332 exit_unlock:
333         mutex_unlock(&fan_data->lock);
334
335         return ret;
336 }
337
338 static DEVICE_ATTR_RW(pwm1);
339 static DEVICE_ATTR_RW(pwm1_enable);
340 static DEVICE_ATTR_RO(pwm1_mode);
341 static DEVICE_ATTR_RO(fan1_min);
342 static DEVICE_ATTR_RO(fan1_max);
343 static DEVICE_ATTR_RO(fan1_input);
344 static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, fan1_input_show, set_rpm);
345
346 static umode_t gpio_fan_is_visible(struct kobject *kobj,
347                                    struct attribute *attr, int index)
348 {
349         struct device *dev = container_of(kobj, struct device, kobj);
350         struct gpio_fan_data *data = dev_get_drvdata(dev);
351
352         if (index == 0 && !data->alarm)
353                 return 0;
354         if (index > 0 && !data->ctrl)
355                 return 0;
356
357         return attr->mode;
358 }
359
360 static struct attribute *gpio_fan_attributes[] = {
361         &dev_attr_fan1_alarm.attr,              /* 0 */
362         &dev_attr_pwm1.attr,                    /* 1 */
363         &dev_attr_pwm1_enable.attr,
364         &dev_attr_pwm1_mode.attr,
365         &dev_attr_fan1_input.attr,
366         &dev_attr_fan1_target.attr,
367         &dev_attr_fan1_min.attr,
368         &dev_attr_fan1_max.attr,
369         NULL
370 };
371
372 static const struct attribute_group gpio_fan_group = {
373         .attrs = gpio_fan_attributes,
374         .is_visible = gpio_fan_is_visible,
375 };
376
377 static const struct attribute_group *gpio_fan_groups[] = {
378         &gpio_fan_group,
379         NULL
380 };
381
382 static int fan_ctrl_init(struct gpio_fan_data *fan_data,
383                          struct gpio_fan_platform_data *pdata)
384 {
385         struct platform_device *pdev = fan_data->pdev;
386         int num_ctrl = pdata->num_ctrl;
387         unsigned *ctrl = pdata->ctrl;
388         int i, err;
389
390         for (i = 0; i < num_ctrl; i++) {
391                 err = devm_gpio_request(&pdev->dev, ctrl[i],
392                                         "GPIO fan control");
393                 if (err)
394                         return err;
395
396                 err = gpio_direction_output(ctrl[i],
397                                             gpio_get_value_cansleep(ctrl[i]));
398                 if (err)
399                         return err;
400         }
401
402         fan_data->num_ctrl = num_ctrl;
403         fan_data->ctrl = ctrl;
404         fan_data->num_speed = pdata->num_speed;
405         fan_data->speed = pdata->speed;
406         fan_data->pwm_enable = true; /* Enable manual fan speed control. */
407         fan_data->speed_index = get_fan_speed_index(fan_data);
408         if (fan_data->speed_index < 0)
409                 return fan_data->speed_index;
410
411         return 0;
412 }
413
414 static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
415                                   unsigned long *state)
416 {
417         struct gpio_fan_data *fan_data = cdev->devdata;
418
419         if (!fan_data)
420                 return -EINVAL;
421
422         *state = fan_data->num_speed - 1;
423         return 0;
424 }
425
426 static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
427                                   unsigned long *state)
428 {
429         struct gpio_fan_data *fan_data = cdev->devdata;
430
431         if (!fan_data)
432                 return -EINVAL;
433
434         *state = fan_data->speed_index;
435         return 0;
436 }
437
438 static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
439                                   unsigned long state)
440 {
441         struct gpio_fan_data *fan_data = cdev->devdata;
442
443         if (!fan_data)
444                 return -EINVAL;
445
446         set_fan_speed(fan_data, state);
447         return 0;
448 }
449
450 static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
451         .get_max_state = gpio_fan_get_max_state,
452         .get_cur_state = gpio_fan_get_cur_state,
453         .set_cur_state = gpio_fan_set_cur_state,
454 };
455
456 #ifdef CONFIG_OF_GPIO
457 /*
458  * Translate OpenFirmware node properties into platform_data
459  */
460 static int gpio_fan_get_of_pdata(struct device *dev,
461                             struct gpio_fan_platform_data *pdata)
462 {
463         struct device_node *node;
464         struct gpio_fan_speed *speed;
465         unsigned *ctrl;
466         unsigned i;
467         u32 u;
468         struct property *prop;
469         const __be32 *p;
470
471         node = dev->of_node;
472
473         /* Alarm GPIO if one exists */
474         if (of_gpio_named_count(node, "alarm-gpios") > 0) {
475                 struct gpio_fan_alarm *alarm;
476                 int val;
477                 enum of_gpio_flags flags;
478
479                 alarm = devm_kzalloc(dev, sizeof(struct gpio_fan_alarm),
480                                         GFP_KERNEL);
481                 if (!alarm)
482                         return -ENOMEM;
483
484                 val = of_get_named_gpio_flags(node, "alarm-gpios", 0, &flags);
485                 if (val < 0)
486                         return val;
487                 alarm->gpio = val;
488                 alarm->active_low = flags & OF_GPIO_ACTIVE_LOW;
489
490                 pdata->alarm = alarm;
491         }
492
493         /* Fill GPIO pin array */
494         pdata->num_ctrl = of_gpio_count(node);
495         if (pdata->num_ctrl <= 0) {
496                 if (pdata->alarm)
497                         return 0;
498                 dev_err(dev, "DT properties empty / missing");
499                 return -ENODEV;
500         }
501         ctrl = devm_kzalloc(dev, pdata->num_ctrl * sizeof(unsigned),
502                                 GFP_KERNEL);
503         if (!ctrl)
504                 return -ENOMEM;
505         for (i = 0; i < pdata->num_ctrl; i++) {
506                 int val;
507
508                 val = of_get_gpio(node, i);
509                 if (val < 0)
510                         return val;
511                 ctrl[i] = val;
512         }
513         pdata->ctrl = ctrl;
514
515         /* Get number of RPM/ctrl_val pairs in speed map */
516         prop = of_find_property(node, "gpio-fan,speed-map", &i);
517         if (!prop) {
518                 dev_err(dev, "gpio-fan,speed-map DT property missing");
519                 return -ENODEV;
520         }
521         i = i / sizeof(u32);
522         if (i == 0 || i & 1) {
523                 dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
524                 return -ENODEV;
525         }
526         pdata->num_speed = i / 2;
527
528         /*
529          * Populate speed map
530          * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
531          * this needs splitting into pairs to create gpio_fan_speed structs
532          */
533         speed = devm_kzalloc(dev,
534                         pdata->num_speed * sizeof(struct gpio_fan_speed),
535                         GFP_KERNEL);
536         if (!speed)
537                 return -ENOMEM;
538         p = NULL;
539         for (i = 0; i < pdata->num_speed; i++) {
540                 p = of_prop_next_u32(prop, p, &u);
541                 if (!p)
542                         return -ENODEV;
543                 speed[i].rpm = u;
544                 p = of_prop_next_u32(prop, p, &u);
545                 if (!p)
546                         return -ENODEV;
547                 speed[i].ctrl_val = u;
548         }
549         pdata->speed = speed;
550
551         return 0;
552 }
553
554 static const struct of_device_id of_gpio_fan_match[] = {
555         { .compatible = "gpio-fan", },
556         {},
557 };
558 MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
559 #endif /* CONFIG_OF_GPIO */
560
561 static int gpio_fan_probe(struct platform_device *pdev)
562 {
563         int err;
564         struct gpio_fan_data *fan_data;
565         struct device *dev = &pdev->dev;
566         struct device_node *np = dev->of_node;
567         struct gpio_fan_platform_data *pdata = dev_get_platdata(dev);
568
569         fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
570                                 GFP_KERNEL);
571         if (!fan_data)
572                 return -ENOMEM;
573
574 #ifdef CONFIG_OF_GPIO
575         if (!pdata) {
576                 pdata = devm_kzalloc(dev,
577                                         sizeof(struct gpio_fan_platform_data),
578                                         GFP_KERNEL);
579                 if (!pdata)
580                         return -ENOMEM;
581
582                 err = gpio_fan_get_of_pdata(dev, pdata);
583                 if (err)
584                         return err;
585         }
586 #else /* CONFIG_OF_GPIO */
587         if (!pdata)
588                 return -EINVAL;
589 #endif /* CONFIG_OF_GPIO */
590
591         fan_data->pdev = pdev;
592         platform_set_drvdata(pdev, fan_data);
593         mutex_init(&fan_data->lock);
594
595         /* Configure alarm GPIO if available. */
596         if (pdata->alarm) {
597                 err = fan_alarm_init(fan_data, pdata->alarm);
598                 if (err)
599                         return err;
600         }
601
602         /* Configure control GPIOs if available. */
603         if (pdata->ctrl && pdata->num_ctrl > 0) {
604                 if (!pdata->speed || pdata->num_speed <= 1)
605                         return -EINVAL;
606                 err = fan_ctrl_init(fan_data, pdata);
607                 if (err)
608                         return err;
609         }
610
611         /* Make this driver part of hwmon class. */
612         fan_data->hwmon_dev =
613                 devm_hwmon_device_register_with_groups(dev,
614                                                        "gpio_fan", fan_data,
615                                                        gpio_fan_groups);
616         if (IS_ERR(fan_data->hwmon_dev))
617                 return PTR_ERR(fan_data->hwmon_dev);
618 #ifdef CONFIG_OF_GPIO
619         /* Optional cooling device register for Device tree platforms */
620         fan_data->cdev = thermal_of_cooling_device_register(np,
621                                                             "gpio-fan",
622                                                             fan_data,
623                                                             &gpio_fan_cool_ops);
624 #else /* CONFIG_OF_GPIO */
625         /* Optional cooling device register for non Device tree platforms */
626         fan_data->cdev = thermal_cooling_device_register("gpio-fan", fan_data,
627                                                          &gpio_fan_cool_ops);
628 #endif /* CONFIG_OF_GPIO */
629
630         dev_info(dev, "GPIO fan initialized\n");
631
632         return 0;
633 }
634
635 static int gpio_fan_remove(struct platform_device *pdev)
636 {
637         struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
638
639         if (!IS_ERR(fan_data->cdev))
640                 thermal_cooling_device_unregister(fan_data->cdev);
641
642         if (fan_data->ctrl)
643                 set_fan_speed(fan_data, 0);
644
645         return 0;
646 }
647
648 static void gpio_fan_shutdown(struct platform_device *pdev)
649 {
650         gpio_fan_remove(pdev);
651 }
652
653 #ifdef CONFIG_PM_SLEEP
654 static int gpio_fan_suspend(struct device *dev)
655 {
656         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
657
658         if (fan_data->ctrl) {
659                 fan_data->resume_speed = fan_data->speed_index;
660                 set_fan_speed(fan_data, 0);
661         }
662
663         return 0;
664 }
665
666 static int gpio_fan_resume(struct device *dev)
667 {
668         struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
669
670         if (fan_data->ctrl)
671                 set_fan_speed(fan_data, fan_data->resume_speed);
672
673         return 0;
674 }
675
676 static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
677 #define GPIO_FAN_PM     (&gpio_fan_pm)
678 #else
679 #define GPIO_FAN_PM     NULL
680 #endif
681
682 static struct platform_driver gpio_fan_driver = {
683         .probe          = gpio_fan_probe,
684         .remove         = gpio_fan_remove,
685         .shutdown       = gpio_fan_shutdown,
686         .driver = {
687                 .name   = "gpio-fan",
688                 .pm     = GPIO_FAN_PM,
689 #ifdef CONFIG_OF_GPIO
690                 .of_match_table = of_match_ptr(of_gpio_fan_match),
691 #endif
692         },
693 };
694
695 module_platform_driver(gpio_fan_driver);
696
697 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
698 MODULE_DESCRIPTION("GPIO FAN driver");
699 MODULE_LICENSE("GPL");
700 MODULE_ALIAS("platform:gpio-fan");