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>
33 #include <linux/gpio-fan.h>
35 struct gpio_fan_data
{
36 struct platform_device
*pdev
;
37 struct device
*hwmon_dev
;
38 struct mutex lock
; /* lock GPIOs operations. */
42 struct gpio_fan_speed
*speed
;
48 struct gpio_fan_alarm
*alarm
;
49 struct work_struct alarm_work
;
56 static void fan_alarm_notify(struct work_struct
*ws
)
58 struct gpio_fan_data
*fan_data
=
59 container_of(ws
, struct gpio_fan_data
, alarm_work
);
61 sysfs_notify(&fan_data
->pdev
->dev
.kobj
, NULL
, "fan1_alarm");
62 kobject_uevent(&fan_data
->pdev
->dev
.kobj
, KOBJ_CHANGE
);
65 static irqreturn_t
fan_alarm_irq_handler(int irq
, void *dev_id
)
67 struct gpio_fan_data
*fan_data
= dev_id
;
69 schedule_work(&fan_data
->alarm_work
);
74 static ssize_t
show_fan_alarm(struct device
*dev
,
75 struct device_attribute
*attr
, char *buf
)
77 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
78 struct gpio_fan_alarm
*alarm
= fan_data
->alarm
;
79 int value
= gpio_get_value(alarm
->gpio
);
81 if (alarm
->active_low
)
84 return sprintf(buf
, "%d\n", value
);
87 static DEVICE_ATTR(fan1_alarm
, S_IRUGO
, show_fan_alarm
, NULL
);
89 static int fan_alarm_init(struct gpio_fan_data
*fan_data
,
90 struct gpio_fan_alarm
*alarm
)
94 struct platform_device
*pdev
= fan_data
->pdev
;
96 fan_data
->alarm
= alarm
;
98 err
= gpio_request(alarm
->gpio
, "GPIO fan alarm");
102 err
= gpio_direction_input(alarm
->gpio
);
106 err
= device_create_file(&pdev
->dev
, &dev_attr_fan1_alarm
);
111 * If the alarm GPIO don't support interrupts, just leave
112 * without initializing the fail notification support.
114 alarm_irq
= gpio_to_irq(alarm
->gpio
);
118 INIT_WORK(&fan_data
->alarm_work
, fan_alarm_notify
);
119 irq_set_irq_type(alarm_irq
, IRQ_TYPE_EDGE_BOTH
);
120 err
= request_irq(alarm_irq
, fan_alarm_irq_handler
, IRQF_SHARED
,
121 "GPIO fan alarm", fan_data
);
128 device_remove_file(&pdev
->dev
, &dev_attr_fan1_alarm
);
130 gpio_free(alarm
->gpio
);
135 static void fan_alarm_free(struct gpio_fan_data
*fan_data
)
137 struct platform_device
*pdev
= fan_data
->pdev
;
138 int alarm_irq
= gpio_to_irq(fan_data
->alarm
->gpio
);
141 free_irq(alarm_irq
, fan_data
);
142 device_remove_file(&pdev
->dev
, &dev_attr_fan1_alarm
);
143 gpio_free(fan_data
->alarm
->gpio
);
150 /* Must be called with fan_data->lock held, except during initialization. */
151 static void __set_fan_ctrl(struct gpio_fan_data
*fan_data
, int ctrl_val
)
155 for (i
= 0; i
< fan_data
->num_ctrl
; i
++)
156 gpio_set_value(fan_data
->ctrl
[i
], (ctrl_val
>> i
) & 1);
159 static int __get_fan_ctrl(struct gpio_fan_data
*fan_data
)
164 for (i
= 0; i
< fan_data
->num_ctrl
; i
++) {
167 value
= gpio_get_value(fan_data
->ctrl
[i
]);
168 ctrl_val
|= (value
<< i
);
173 /* Must be called with fan_data->lock held, except during initialization. */
174 static void set_fan_speed(struct gpio_fan_data
*fan_data
, int speed_index
)
176 if (fan_data
->speed_index
== speed_index
)
179 __set_fan_ctrl(fan_data
, fan_data
->speed
[speed_index
].ctrl_val
);
180 fan_data
->speed_index
= speed_index
;
183 static int get_fan_speed_index(struct gpio_fan_data
*fan_data
)
185 int ctrl_val
= __get_fan_ctrl(fan_data
);
188 for (i
= 0; i
< fan_data
->num_speed
; i
++)
189 if (fan_data
->speed
[i
].ctrl_val
== ctrl_val
)
192 dev_warn(&fan_data
->pdev
->dev
,
193 "missing speed array entry for GPIO value 0x%x\n", ctrl_val
);
198 static int rpm_to_speed_index(struct gpio_fan_data
*fan_data
, int rpm
)
200 struct gpio_fan_speed
*speed
= fan_data
->speed
;
203 for (i
= 0; i
< fan_data
->num_speed
; i
++)
204 if (speed
[i
].rpm
>= rpm
)
207 return fan_data
->num_speed
- 1;
210 static ssize_t
show_pwm(struct device
*dev
,
211 struct device_attribute
*attr
, char *buf
)
213 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
214 u8 pwm
= fan_data
->speed_index
* 255 / (fan_data
->num_speed
- 1);
216 return sprintf(buf
, "%d\n", pwm
);
219 static ssize_t
set_pwm(struct device
*dev
, struct device_attribute
*attr
,
220 const char *buf
, size_t count
)
222 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
227 if (strict_strtoul(buf
, 10, &pwm
) || pwm
> 255)
230 mutex_lock(&fan_data
->lock
);
232 if (!fan_data
->pwm_enable
) {
237 speed_index
= DIV_ROUND_UP(pwm
* (fan_data
->num_speed
- 1), 255);
238 set_fan_speed(fan_data
, speed_index
);
241 mutex_unlock(&fan_data
->lock
);
246 static ssize_t
show_pwm_enable(struct device
*dev
,
247 struct device_attribute
*attr
, char *buf
)
249 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
251 return sprintf(buf
, "%d\n", fan_data
->pwm_enable
);
254 static ssize_t
set_pwm_enable(struct device
*dev
, struct device_attribute
*attr
,
255 const char *buf
, size_t count
)
257 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
260 if (strict_strtoul(buf
, 10, &val
) || val
> 1)
263 if (fan_data
->pwm_enable
== val
)
266 mutex_lock(&fan_data
->lock
);
268 fan_data
->pwm_enable
= val
;
270 /* Disable manual control mode: set fan at full speed. */
272 set_fan_speed(fan_data
, fan_data
->num_speed
- 1);
274 mutex_unlock(&fan_data
->lock
);
279 static ssize_t
show_pwm_mode(struct device
*dev
,
280 struct device_attribute
*attr
, char *buf
)
282 return sprintf(buf
, "0\n");
285 static ssize_t
show_rpm_min(struct device
*dev
,
286 struct device_attribute
*attr
, char *buf
)
288 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
290 return sprintf(buf
, "%d\n", fan_data
->speed
[0].rpm
);
293 static ssize_t
show_rpm_max(struct device
*dev
,
294 struct device_attribute
*attr
, char *buf
)
296 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
298 return sprintf(buf
, "%d\n",
299 fan_data
->speed
[fan_data
->num_speed
- 1].rpm
);
302 static ssize_t
show_rpm(struct device
*dev
,
303 struct device_attribute
*attr
, char *buf
)
305 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
307 return sprintf(buf
, "%d\n", fan_data
->speed
[fan_data
->speed_index
].rpm
);
310 static ssize_t
set_rpm(struct device
*dev
, struct device_attribute
*attr
,
311 const char *buf
, size_t count
)
313 struct gpio_fan_data
*fan_data
= dev_get_drvdata(dev
);
317 if (strict_strtoul(buf
, 10, &rpm
))
320 mutex_lock(&fan_data
->lock
);
322 if (!fan_data
->pwm_enable
) {
327 set_fan_speed(fan_data
, rpm_to_speed_index(fan_data
, rpm
));
330 mutex_unlock(&fan_data
->lock
);
335 static DEVICE_ATTR(pwm1
, S_IRUGO
| S_IWUSR
, show_pwm
, set_pwm
);
336 static DEVICE_ATTR(pwm1_enable
, S_IRUGO
| S_IWUSR
,
337 show_pwm_enable
, set_pwm_enable
);
338 static DEVICE_ATTR(pwm1_mode
, S_IRUGO
, show_pwm_mode
, NULL
);
339 static DEVICE_ATTR(fan1_min
, S_IRUGO
, show_rpm_min
, NULL
);
340 static DEVICE_ATTR(fan1_max
, S_IRUGO
, show_rpm_max
, NULL
);
341 static DEVICE_ATTR(fan1_input
, S_IRUGO
, show_rpm
, NULL
);
342 static DEVICE_ATTR(fan1_target
, S_IRUGO
| S_IWUSR
, show_rpm
, set_rpm
);
344 static struct attribute
*gpio_fan_ctrl_attributes
[] = {
346 &dev_attr_pwm1_enable
.attr
,
347 &dev_attr_pwm1_mode
.attr
,
348 &dev_attr_fan1_input
.attr
,
349 &dev_attr_fan1_target
.attr
,
350 &dev_attr_fan1_min
.attr
,
351 &dev_attr_fan1_max
.attr
,
355 static const struct attribute_group gpio_fan_ctrl_group
= {
356 .attrs
= gpio_fan_ctrl_attributes
,
359 static int fan_ctrl_init(struct gpio_fan_data
*fan_data
,
360 struct gpio_fan_platform_data
*pdata
)
362 struct platform_device
*pdev
= fan_data
->pdev
;
363 int num_ctrl
= pdata
->num_ctrl
;
364 unsigned *ctrl
= pdata
->ctrl
;
367 for (i
= 0; i
< num_ctrl
; i
++) {
368 err
= gpio_request(ctrl
[i
], "GPIO fan control");
372 err
= gpio_direction_output(ctrl
[i
], gpio_get_value(ctrl
[i
]));
379 fan_data
->num_ctrl
= num_ctrl
;
380 fan_data
->ctrl
= ctrl
;
381 fan_data
->num_speed
= pdata
->num_speed
;
382 fan_data
->speed
= pdata
->speed
;
383 fan_data
->pwm_enable
= true; /* Enable manual fan speed control. */
384 fan_data
->speed_index
= get_fan_speed_index(fan_data
);
385 if (fan_data
->speed_index
< 0) {
390 err
= sysfs_create_group(&pdev
->dev
.kobj
, &gpio_fan_ctrl_group
);
397 for (i
= i
- 1; i
>= 0; i
--)
403 static void fan_ctrl_free(struct gpio_fan_data
*fan_data
)
405 struct platform_device
*pdev
= fan_data
->pdev
;
408 sysfs_remove_group(&pdev
->dev
.kobj
, &gpio_fan_ctrl_group
);
409 for (i
= 0; i
< fan_data
->num_ctrl
; i
++)
410 gpio_free(fan_data
->ctrl
[i
]);
417 static ssize_t
show_name(struct device
*dev
,
418 struct device_attribute
*attr
, char *buf
)
420 return sprintf(buf
, "gpio-fan\n");
423 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
425 static int __devinit
gpio_fan_probe(struct platform_device
*pdev
)
428 struct gpio_fan_data
*fan_data
;
429 struct gpio_fan_platform_data
*pdata
= pdev
->dev
.platform_data
;
434 fan_data
= kzalloc(sizeof(struct gpio_fan_data
), GFP_KERNEL
);
438 fan_data
->pdev
= pdev
;
439 platform_set_drvdata(pdev
, fan_data
);
440 mutex_init(&fan_data
->lock
);
442 /* Configure alarm GPIO if available. */
444 err
= fan_alarm_init(fan_data
, pdata
->alarm
);
449 /* Configure control GPIOs if available. */
450 if (pdata
->ctrl
&& pdata
->num_ctrl
> 0) {
451 if (!pdata
->speed
|| pdata
->num_speed
<= 1) {
455 err
= fan_ctrl_init(fan_data
, pdata
);
460 err
= device_create_file(&pdev
->dev
, &dev_attr_name
);
464 /* Make this driver part of hwmon class. */
465 fan_data
->hwmon_dev
= hwmon_device_register(&pdev
->dev
);
466 if (IS_ERR(fan_data
->hwmon_dev
)) {
467 err
= PTR_ERR(fan_data
->hwmon_dev
);
468 goto err_remove_name
;
471 dev_info(&pdev
->dev
, "GPIO fan initialized\n");
476 device_remove_file(&pdev
->dev
, &dev_attr_name
);
479 fan_ctrl_free(fan_data
);
482 fan_alarm_free(fan_data
);
484 platform_set_drvdata(pdev
, NULL
);
490 static int __devexit
gpio_fan_remove(struct platform_device
*pdev
)
492 struct gpio_fan_data
*fan_data
= platform_get_drvdata(pdev
);
494 hwmon_device_unregister(fan_data
->hwmon_dev
);
495 device_remove_file(&pdev
->dev
, &dev_attr_name
);
497 fan_alarm_free(fan_data
);
499 fan_ctrl_free(fan_data
);
506 static int gpio_fan_suspend(struct platform_device
*pdev
, pm_message_t state
)
508 struct gpio_fan_data
*fan_data
= platform_get_drvdata(pdev
);
510 if (fan_data
->ctrl
) {
511 fan_data
->resume_speed
= fan_data
->speed_index
;
512 set_fan_speed(fan_data
, 0);
518 static int gpio_fan_resume(struct platform_device
*pdev
)
520 struct gpio_fan_data
*fan_data
= platform_get_drvdata(pdev
);
523 set_fan_speed(fan_data
, fan_data
->resume_speed
);
528 #define gpio_fan_suspend NULL
529 #define gpio_fan_resume NULL
532 static struct platform_driver gpio_fan_driver
= {
533 .probe
= gpio_fan_probe
,
534 .remove
= __devexit_p(gpio_fan_remove
),
535 .suspend
= gpio_fan_suspend
,
536 .resume
= gpio_fan_resume
,
542 module_platform_driver(gpio_fan_driver
);
544 MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
545 MODULE_DESCRIPTION("GPIO FAN driver");
546 MODULE_LICENSE("GPL");
547 MODULE_ALIAS("platform:gpio-fan");