2 * gpio-fan.c - Hwmon driver for fans connected to GPIO lines.
4 * Copyright (C) 2010 LaCie
6 * Author: Simon Guinot <sguinot@lacie.com>
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.
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.
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
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>
34 #include <linux/of_platform.h>
35 #include <linux/of_gpio.h>
36 #include <linux/thermal.h>
38 struct gpio_fan_alarm {
40 unsigned int active_low;
43 struct gpio_fan_speed {
48 struct gpio_fan_platform_data {
50 unsigned int *ctrl; /* fan control GPIOs. */
51 struct gpio_fan_alarm *alarm; /* fan alarm GPIO. */
53 * Speed conversion array: rpm from/to GPIO bit field.
54 * This array _must_ be sorted in ascending rpm order.
57 struct gpio_fan_speed *speed;
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. */
69 struct gpio_fan_speed *speed;
71 #ifdef CONFIG_PM_SLEEP
75 struct gpio_fan_alarm *alarm;
76 struct work_struct alarm_work;
83 static void fan_alarm_notify(struct work_struct *ws)
85 struct gpio_fan_data *fan_data =
86 container_of(ws, struct gpio_fan_data, alarm_work);
88 sysfs_notify(&fan_data->pdev->dev.kobj, NULL, "fan1_alarm");
89 kobject_uevent(&fan_data->pdev->dev.kobj, KOBJ_CHANGE);
92 static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
94 struct gpio_fan_data *fan_data = dev_id;
96 schedule_work(&fan_data->alarm_work);
101 static ssize_t fan1_alarm_show(struct device *dev,
102 struct device_attribute *attr, char *buf)
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);
108 if (alarm->active_low)
111 return sprintf(buf, "%d\n", value);
114 static DEVICE_ATTR_RO(fan1_alarm);
116 static int fan_alarm_init(struct gpio_fan_data *fan_data,
117 struct gpio_fan_alarm *alarm)
121 struct platform_device *pdev = fan_data->pdev;
123 fan_data->alarm = alarm;
125 err = devm_gpio_request(&pdev->dev, alarm->gpio, "GPIO fan alarm");
129 err = gpio_direction_input(alarm->gpio);
134 * If the alarm GPIO don't support interrupts, just leave
135 * without initializing the fail notification support.
137 alarm_irq = gpio_to_irq(alarm->gpio);
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);
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)
157 for (i = 0; i < fan_data->num_ctrl; i++)
158 gpio_set_value_cansleep(fan_data->ctrl[i], (ctrl_val >> i) & 1);
161 static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
166 for (i = 0; i < fan_data->num_ctrl; i++) {
169 value = gpio_get_value_cansleep(fan_data->ctrl[i]);
170 ctrl_val |= (value << i);
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)
178 if (fan_data->speed_index == speed_index)
181 __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
182 fan_data->speed_index = speed_index;
185 static int get_fan_speed_index(struct gpio_fan_data *fan_data)
187 int ctrl_val = __get_fan_ctrl(fan_data);
190 for (i = 0; i < fan_data->num_speed; i++)
191 if (fan_data->speed[i].ctrl_val == ctrl_val)
194 dev_warn(&fan_data->pdev->dev,
195 "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
200 static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
202 struct gpio_fan_speed *speed = fan_data->speed;
205 for (i = 0; i < fan_data->num_speed; i++)
206 if (speed[i].rpm >= rpm)
209 return fan_data->num_speed - 1;
212 static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
215 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
216 u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
218 return sprintf(buf, "%d\n", pwm);
221 static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
222 const char *buf, size_t count)
224 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
229 if (kstrtoul(buf, 10, &pwm) || pwm > 255)
232 mutex_lock(&fan_data->lock);
234 if (!fan_data->pwm_enable) {
239 speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
240 set_fan_speed(fan_data, speed_index);
243 mutex_unlock(&fan_data->lock);
248 static ssize_t pwm1_enable_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
253 return sprintf(buf, "%d\n", fan_data->pwm_enable);
256 static ssize_t pwm1_enable_store(struct device *dev,
257 struct device_attribute *attr,
258 const char *buf, size_t count)
260 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
263 if (kstrtoul(buf, 10, &val) || val > 1)
266 if (fan_data->pwm_enable == val)
269 mutex_lock(&fan_data->lock);
271 fan_data->pwm_enable = val;
273 /* Disable manual control mode: set fan at full speed. */
275 set_fan_speed(fan_data, fan_data->num_speed - 1);
277 mutex_unlock(&fan_data->lock);
282 static ssize_t pwm1_mode_show(struct device *dev,
283 struct device_attribute *attr, char *buf)
285 return sprintf(buf, "0\n");
288 static ssize_t fan1_min_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
291 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
293 return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
296 static ssize_t fan1_max_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
299 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
301 return sprintf(buf, "%d\n",
302 fan_data->speed[fan_data->num_speed - 1].rpm);
305 static ssize_t fan1_input_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
310 return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
313 static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
314 const char *buf, size_t count)
316 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
320 if (kstrtoul(buf, 10, &rpm))
323 mutex_lock(&fan_data->lock);
325 if (!fan_data->pwm_enable) {
330 set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
333 mutex_unlock(&fan_data->lock);
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);
346 static umode_t gpio_fan_is_visible(struct kobject *kobj,
347 struct attribute *attr, int index)
349 struct device *dev = container_of(kobj, struct device, kobj);
350 struct gpio_fan_data *data = dev_get_drvdata(dev);
352 if (index == 0 && !data->alarm)
354 if (index > 0 && !data->ctrl)
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,
372 static const struct attribute_group gpio_fan_group = {
373 .attrs = gpio_fan_attributes,
374 .is_visible = gpio_fan_is_visible,
377 static const struct attribute_group *gpio_fan_groups[] = {
382 static int fan_ctrl_init(struct gpio_fan_data *fan_data,
383 struct gpio_fan_platform_data *pdata)
385 struct platform_device *pdev = fan_data->pdev;
386 int num_ctrl = pdata->num_ctrl;
387 unsigned *ctrl = pdata->ctrl;
390 for (i = 0; i < num_ctrl; i++) {
391 err = devm_gpio_request(&pdev->dev, ctrl[i],
396 err = gpio_direction_output(ctrl[i],
397 gpio_get_value_cansleep(ctrl[i]));
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;
414 static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
415 unsigned long *state)
417 struct gpio_fan_data *fan_data = cdev->devdata;
422 *state = fan_data->num_speed - 1;
426 static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
427 unsigned long *state)
429 struct gpio_fan_data *fan_data = cdev->devdata;
434 *state = fan_data->speed_index;
438 static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
441 struct gpio_fan_data *fan_data = cdev->devdata;
446 set_fan_speed(fan_data, state);
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,
456 #ifdef CONFIG_OF_GPIO
458 * Translate OpenFirmware node properties into platform_data
460 static int gpio_fan_get_of_pdata(struct device *dev,
461 struct gpio_fan_platform_data *pdata)
463 struct device_node *node;
464 struct gpio_fan_speed *speed;
468 struct property *prop;
473 /* Alarm GPIO if one exists */
474 if (of_gpio_named_count(node, "alarm-gpios") > 0) {
475 struct gpio_fan_alarm *alarm;
477 enum of_gpio_flags flags;
479 alarm = devm_kzalloc(dev, sizeof(struct gpio_fan_alarm),
484 val = of_get_named_gpio_flags(node, "alarm-gpios", 0, &flags);
488 alarm->active_low = flags & OF_GPIO_ACTIVE_LOW;
490 pdata->alarm = alarm;
493 /* Fill GPIO pin array */
494 pdata->num_ctrl = of_gpio_count(node);
495 if (pdata->num_ctrl <= 0) {
498 dev_err(dev, "DT properties empty / missing");
501 ctrl = devm_kzalloc(dev, pdata->num_ctrl * sizeof(unsigned),
505 for (i = 0; i < pdata->num_ctrl; i++) {
508 val = of_get_gpio(node, i);
515 /* Get number of RPM/ctrl_val pairs in speed map */
516 prop = of_find_property(node, "gpio-fan,speed-map", &i);
518 dev_err(dev, "gpio-fan,speed-map DT property missing");
522 if (i == 0 || i & 1) {
523 dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
526 pdata->num_speed = i / 2;
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
533 speed = devm_kzalloc(dev,
534 pdata->num_speed * sizeof(struct gpio_fan_speed),
539 for (i = 0; i < pdata->num_speed; i++) {
540 p = of_prop_next_u32(prop, p, &u);
544 p = of_prop_next_u32(prop, p, &u);
547 speed[i].ctrl_val = u;
549 pdata->speed = speed;
554 static const struct of_device_id of_gpio_fan_match[] = {
555 { .compatible = "gpio-fan", },
558 MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
559 #endif /* CONFIG_OF_GPIO */
561 static int gpio_fan_probe(struct platform_device *pdev)
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);
569 fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
574 #ifdef CONFIG_OF_GPIO
576 pdata = devm_kzalloc(dev,
577 sizeof(struct gpio_fan_platform_data),
582 err = gpio_fan_get_of_pdata(dev, pdata);
586 #else /* CONFIG_OF_GPIO */
589 #endif /* CONFIG_OF_GPIO */
591 fan_data->pdev = pdev;
592 platform_set_drvdata(pdev, fan_data);
593 mutex_init(&fan_data->lock);
595 /* Configure alarm GPIO if available. */
597 err = fan_alarm_init(fan_data, pdata->alarm);
602 /* Configure control GPIOs if available. */
603 if (pdata->ctrl && pdata->num_ctrl > 0) {
604 if (!pdata->speed || pdata->num_speed <= 1)
606 err = fan_ctrl_init(fan_data, pdata);
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,
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,
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,
628 #endif /* CONFIG_OF_GPIO */
630 dev_info(dev, "GPIO fan initialized\n");
635 static int gpio_fan_remove(struct platform_device *pdev)
637 struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
639 if (!IS_ERR(fan_data->cdev))
640 thermal_cooling_device_unregister(fan_data->cdev);
643 set_fan_speed(fan_data, 0);
648 static void gpio_fan_shutdown(struct platform_device *pdev)
650 gpio_fan_remove(pdev);
653 #ifdef CONFIG_PM_SLEEP
654 static int gpio_fan_suspend(struct device *dev)
656 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
658 if (fan_data->ctrl) {
659 fan_data->resume_speed = fan_data->speed_index;
660 set_fan_speed(fan_data, 0);
666 static int gpio_fan_resume(struct device *dev)
668 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
671 set_fan_speed(fan_data, fan_data->resume_speed);
676 static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
677 #define GPIO_FAN_PM (&gpio_fan_pm)
679 #define GPIO_FAN_PM NULL
682 static struct platform_driver gpio_fan_driver = {
683 .probe = gpio_fan_probe,
684 .remove = gpio_fan_remove,
685 .shutdown = gpio_fan_shutdown,
689 #ifdef CONFIG_OF_GPIO
690 .of_match_table = of_match_ptr(of_gpio_fan_match),
695 module_platform_driver(gpio_fan_driver);
697 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
698 MODULE_DESCRIPTION("GPIO FAN driver");
699 MODULE_LICENSE("GPL");
700 MODULE_ALIAS("platform:gpio-fan");