blob: 18b3c7c27d36637e776e18882b52b3f193091519 [file] [log] [blame]
Simon Guinotd6fe1362010-10-22 00:44:19 +02001/*
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>
Sachin Kamatc50588a2013-09-27 16:56:00 +053033#include <linux/of.h>
Jamie Lentin55fb8b062012-09-14 17:07:06 +010034#include <linux/of_platform.h>
35#include <linux/of_gpio.h>
Nishanth Menonb5cf88e2015-01-08 12:05:03 -060036#include <linux/thermal.h>
Simon Guinotd6fe1362010-10-22 00:44:19 +020037
Linus Walleijef7a6122017-09-26 01:09:05 +020038struct gpio_fan_speed {
39 int rpm;
40 int ctrl_val;
41};
42
Simon Guinotd6fe1362010-10-22 00:44:19 +020043struct gpio_fan_data {
Linus Walleij8c0eb9b2017-09-26 01:09:06 +020044 struct device *dev;
Simon Guinotd6fe1362010-10-22 00:44:19 +020045 struct device *hwmon_dev;
Nishanth Menonb5cf88e2015-01-08 12:05:03 -060046 /* Cooling device if any */
47 struct thermal_cooling_device *cdev;
Simon Guinotd6fe1362010-10-22 00:44:19 +020048 struct mutex lock; /* lock GPIOs operations. */
Linus Walleije99c2e52017-09-26 01:09:10 +020049 int num_gpios;
50 unsigned int *gpios;
Simon Guinotd6fe1362010-10-22 00:44:19 +020051 int num_speed;
52 struct gpio_fan_speed *speed;
53 int speed_index;
Rafael J. Wysocki6d20a6c2012-07-08 00:01:03 +020054#ifdef CONFIG_PM_SLEEP
Simon Guinotd6fe1362010-10-22 00:44:19 +020055 int resume_speed;
56#endif
57 bool pwm_enable;
Linus Walleijc9933cb2017-09-26 01:09:09 +020058 unsigned int alarm_gpio;
59 unsigned int alarm_gpio_active_low;
Simon Guinotd6fe1362010-10-22 00:44:19 +020060 struct work_struct alarm_work;
61};
62
63/*
64 * Alarm GPIO.
65 */
66
67static void fan_alarm_notify(struct work_struct *ws)
68{
69 struct gpio_fan_data *fan_data =
70 container_of(ws, struct gpio_fan_data, alarm_work);
71
Linus Walleij8c0eb9b2017-09-26 01:09:06 +020072 sysfs_notify(&fan_data->dev->kobj, NULL, "fan1_alarm");
73 kobject_uevent(&fan_data->dev->kobj, KOBJ_CHANGE);
Simon Guinotd6fe1362010-10-22 00:44:19 +020074}
75
76static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
77{
78 struct gpio_fan_data *fan_data = dev_id;
79
80 schedule_work(&fan_data->alarm_work);
81
82 return IRQ_NONE;
83}
84
Julia Lawallc490c632016-12-22 13:04:44 +010085static ssize_t fan1_alarm_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +020087{
88 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
Linus Walleijc9933cb2017-09-26 01:09:09 +020089 int value = gpio_get_value_cansleep(fan_data->alarm_gpio);
Simon Guinotd6fe1362010-10-22 00:44:19 +020090
Linus Walleijc9933cb2017-09-26 01:09:09 +020091 if (fan_data->alarm_gpio_active_low)
Simon Guinotd6fe1362010-10-22 00:44:19 +020092 value = !value;
93
94 return sprintf(buf, "%d\n", value);
95}
96
Julia Lawallc490c632016-12-22 13:04:44 +010097static DEVICE_ATTR_RO(fan1_alarm);
Simon Guinotd6fe1362010-10-22 00:44:19 +020098
Linus Walleijb5482f72017-09-26 01:09:08 +020099static int fan_alarm_init(struct gpio_fan_data *fan_data)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200100{
101 int err;
102 int alarm_irq;
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200103 struct device *dev = fan_data->dev;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200104
Linus Walleijc9933cb2017-09-26 01:09:09 +0200105 err = devm_gpio_request(dev, fan_data->alarm_gpio, "GPIO fan alarm");
Simon Guinotd6fe1362010-10-22 00:44:19 +0200106 if (err)
107 return err;
108
Linus Walleijc9933cb2017-09-26 01:09:09 +0200109 err = gpio_direction_input(fan_data->alarm_gpio);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200110 if (err)
Guenter Roeckd00985f2012-06-02 09:58:07 -0700111 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200112
Simon Guinotd6fe1362010-10-22 00:44:19 +0200113 /*
114 * If the alarm GPIO don't support interrupts, just leave
115 * without initializing the fail notification support.
116 */
Linus Walleijc9933cb2017-09-26 01:09:09 +0200117 alarm_irq = gpio_to_irq(fan_data->alarm_gpio);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200118 if (alarm_irq < 0)
119 return 0;
120
121 INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200122 irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200123 err = devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
Guenter Roeckd00985f2012-06-02 09:58:07 -0700124 IRQF_SHARED, "GPIO fan alarm", fan_data);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200125 return err;
126}
127
Simon Guinotd6fe1362010-10-22 00:44:19 +0200128/*
129 * Control GPIOs.
130 */
131
132/* Must be called with fan_data->lock held, except during initialization. */
133static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
134{
135 int i;
136
Linus Walleije99c2e52017-09-26 01:09:10 +0200137 for (i = 0; i < fan_data->num_gpios; i++)
138 gpio_set_value_cansleep(fan_data->gpios[i],
139 (ctrl_val >> i) & 1);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200140}
141
142static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
143{
144 int i;
145 int ctrl_val = 0;
146
Linus Walleije99c2e52017-09-26 01:09:10 +0200147 for (i = 0; i < fan_data->num_gpios; i++) {
Simon Guinotd6fe1362010-10-22 00:44:19 +0200148 int value;
149
Linus Walleije99c2e52017-09-26 01:09:10 +0200150 value = gpio_get_value_cansleep(fan_data->gpios[i]);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200151 ctrl_val |= (value << i);
152 }
153 return ctrl_val;
154}
155
156/* Must be called with fan_data->lock held, except during initialization. */
157static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
158{
159 if (fan_data->speed_index == speed_index)
160 return;
161
162 __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
163 fan_data->speed_index = speed_index;
164}
165
166static int get_fan_speed_index(struct gpio_fan_data *fan_data)
167{
168 int ctrl_val = __get_fan_ctrl(fan_data);
169 int i;
170
171 for (i = 0; i < fan_data->num_speed; i++)
172 if (fan_data->speed[i].ctrl_val == ctrl_val)
173 return i;
174
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200175 dev_warn(fan_data->dev,
Simon Guinotd6fe1362010-10-22 00:44:19 +0200176 "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
177
Guenter Roeckc52ae3d2013-09-13 10:42:39 -0700178 return -ENODEV;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200179}
180
Axel Lin2565fb02014-08-02 13:36:38 +0800181static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200182{
183 struct gpio_fan_speed *speed = fan_data->speed;
184 int i;
185
186 for (i = 0; i < fan_data->num_speed; i++)
187 if (speed[i].rpm >= rpm)
188 return i;
189
190 return fan_data->num_speed - 1;
191}
192
Julia Lawallc490c632016-12-22 13:04:44 +0100193static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
194 char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200195{
196 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
197 u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
198
199 return sprintf(buf, "%d\n", pwm);
200}
201
Julia Lawallc490c632016-12-22 13:04:44 +0100202static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
203 const char *buf, size_t count)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200204{
205 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
206 unsigned long pwm;
207 int speed_index;
208 int ret = count;
209
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100210 if (kstrtoul(buf, 10, &pwm) || pwm > 255)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200211 return -EINVAL;
212
213 mutex_lock(&fan_data->lock);
214
215 if (!fan_data->pwm_enable) {
216 ret = -EPERM;
217 goto exit_unlock;
218 }
219
220 speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
221 set_fan_speed(fan_data, speed_index);
222
223exit_unlock:
224 mutex_unlock(&fan_data->lock);
225
226 return ret;
227}
228
Julia Lawallc490c632016-12-22 13:04:44 +0100229static ssize_t pwm1_enable_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200231{
232 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
233
234 return sprintf(buf, "%d\n", fan_data->pwm_enable);
235}
236
Julia Lawallc490c632016-12-22 13:04:44 +0100237static ssize_t pwm1_enable_store(struct device *dev,
238 struct device_attribute *attr,
239 const char *buf, size_t count)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200240{
241 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
242 unsigned long val;
243
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100244 if (kstrtoul(buf, 10, &val) || val > 1)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200245 return -EINVAL;
246
247 if (fan_data->pwm_enable == val)
248 return count;
249
250 mutex_lock(&fan_data->lock);
251
252 fan_data->pwm_enable = val;
253
254 /* Disable manual control mode: set fan at full speed. */
255 if (val == 0)
256 set_fan_speed(fan_data, fan_data->num_speed - 1);
257
258 mutex_unlock(&fan_data->lock);
259
260 return count;
261}
262
Julia Lawallc490c632016-12-22 13:04:44 +0100263static ssize_t pwm1_mode_show(struct device *dev,
264 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200265{
266 return sprintf(buf, "0\n");
267}
268
Julia Lawallc490c632016-12-22 13:04:44 +0100269static ssize_t fan1_min_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200271{
272 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
273
274 return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
275}
276
Julia Lawallc490c632016-12-22 13:04:44 +0100277static ssize_t fan1_max_show(struct device *dev,
278 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200279{
280 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
281
282 return sprintf(buf, "%d\n",
283 fan_data->speed[fan_data->num_speed - 1].rpm);
284}
285
Julia Lawallc490c632016-12-22 13:04:44 +0100286static ssize_t fan1_input_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200288{
289 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
290
291 return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
292}
293
294static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
295 const char *buf, size_t count)
296{
297 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
298 unsigned long rpm;
299 int ret = count;
300
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100301 if (kstrtoul(buf, 10, &rpm))
Simon Guinotd6fe1362010-10-22 00:44:19 +0200302 return -EINVAL;
303
304 mutex_lock(&fan_data->lock);
305
306 if (!fan_data->pwm_enable) {
307 ret = -EPERM;
308 goto exit_unlock;
309 }
310
311 set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
312
313exit_unlock:
314 mutex_unlock(&fan_data->lock);
315
316 return ret;
317}
318
Julia Lawallc490c632016-12-22 13:04:44 +0100319static DEVICE_ATTR_RW(pwm1);
320static DEVICE_ATTR_RW(pwm1_enable);
321static DEVICE_ATTR_RO(pwm1_mode);
322static DEVICE_ATTR_RO(fan1_min);
323static DEVICE_ATTR_RO(fan1_max);
324static DEVICE_ATTR_RO(fan1_input);
325static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, fan1_input_show, set_rpm);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200326
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700327static umode_t gpio_fan_is_visible(struct kobject *kobj,
328 struct attribute *attr, int index)
329{
330 struct device *dev = container_of(kobj, struct device, kobj);
331 struct gpio_fan_data *data = dev_get_drvdata(dev);
332
Linus Walleijc9933cb2017-09-26 01:09:09 +0200333 if (index == 0 && !data->alarm_gpio)
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700334 return 0;
Linus Walleije99c2e52017-09-26 01:09:10 +0200335 if (index > 0 && !data->gpios)
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700336 return 0;
337
338 return attr->mode;
339}
340
341static struct attribute *gpio_fan_attributes[] = {
Guenter Roeck7258a122013-07-06 09:46:14 -0700342 &dev_attr_fan1_alarm.attr, /* 0 */
343 &dev_attr_pwm1.attr, /* 1 */
Simon Guinotd6fe1362010-10-22 00:44:19 +0200344 &dev_attr_pwm1_enable.attr,
345 &dev_attr_pwm1_mode.attr,
346 &dev_attr_fan1_input.attr,
347 &dev_attr_fan1_target.attr,
348 &dev_attr_fan1_min.attr,
349 &dev_attr_fan1_max.attr,
350 NULL
351};
352
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700353static const struct attribute_group gpio_fan_group = {
354 .attrs = gpio_fan_attributes,
355 .is_visible = gpio_fan_is_visible,
Simon Guinotd6fe1362010-10-22 00:44:19 +0200356};
357
Guenter Roeck7258a122013-07-06 09:46:14 -0700358static const struct attribute_group *gpio_fan_groups[] = {
359 &gpio_fan_group,
360 NULL
361};
362
Linus Walleijb5482f72017-09-26 01:09:08 +0200363static int fan_ctrl_init(struct gpio_fan_data *fan_data)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200364{
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200365 struct device *dev = fan_data->dev;
Linus Walleije99c2e52017-09-26 01:09:10 +0200366 int num_gpios = fan_data->num_gpios;
367 unsigned int *gpios = fan_data->gpios;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200368 int i, err;
369
Linus Walleije99c2e52017-09-26 01:09:10 +0200370 for (i = 0; i < num_gpios; i++) {
371 err = devm_gpio_request(dev, gpios[i],
Guenter Roeckd00985f2012-06-02 09:58:07 -0700372 "GPIO fan control");
Simon Guinotd6fe1362010-10-22 00:44:19 +0200373 if (err)
Guenter Roeckd00985f2012-06-02 09:58:07 -0700374 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200375
Linus Walleije99c2e52017-09-26 01:09:10 +0200376 err = gpio_direction_output(gpios[i],
377 gpio_get_value_cansleep(gpios[i]));
Guenter Roeckd00985f2012-06-02 09:58:07 -0700378 if (err)
379 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200380 }
381
Simon Guinotd6fe1362010-10-22 00:44:19 +0200382 fan_data->pwm_enable = true; /* Enable manual fan speed control. */
383 fan_data->speed_index = get_fan_speed_index(fan_data);
Guenter Roeckd00985f2012-06-02 09:58:07 -0700384 if (fan_data->speed_index < 0)
Guenter Roeckc52ae3d2013-09-13 10:42:39 -0700385 return fan_data->speed_index;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200386
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700387 return 0;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200388}
389
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600390static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
391 unsigned long *state)
392{
393 struct gpio_fan_data *fan_data = cdev->devdata;
394
395 if (!fan_data)
396 return -EINVAL;
397
398 *state = fan_data->num_speed - 1;
399 return 0;
400}
401
402static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
403 unsigned long *state)
404{
405 struct gpio_fan_data *fan_data = cdev->devdata;
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600406
407 if (!fan_data)
408 return -EINVAL;
409
Nishanth Menon000e09492016-02-19 18:09:51 -0600410 *state = fan_data->speed_index;
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600411 return 0;
412}
413
414static int gpio_fan_set_cur_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 set_fan_speed(fan_data, state);
423 return 0;
424}
425
426static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
427 .get_max_state = gpio_fan_get_max_state,
428 .get_cur_state = gpio_fan_get_cur_state,
429 .set_cur_state = gpio_fan_set_cur_state,
430};
431
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100432/*
433 * Translate OpenFirmware node properties into platform_data
434 */
Linus Walleijb5482f72017-09-26 01:09:08 +0200435static int gpio_fan_get_of_data(struct gpio_fan_data *fan_data)
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100436{
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100437 struct gpio_fan_speed *speed;
Linus Walleijb5482f72017-09-26 01:09:08 +0200438 struct device *dev = fan_data->dev;
439 struct device_node *np = dev->of_node;
Linus Walleije99c2e52017-09-26 01:09:10 +0200440 unsigned int *gpios;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100441 unsigned i;
442 u32 u;
443 struct property *prop;
444 const __be32 *p;
445
Simon Guinot73ef85f2015-02-25 18:58:19 +0100446 /* Alarm GPIO if one exists */
Linus Walleijb5482f72017-09-26 01:09:08 +0200447 if (of_gpio_named_count(np, "alarm-gpios") > 0) {
Simon Guinot73ef85f2015-02-25 18:58:19 +0100448 int val;
449 enum of_gpio_flags flags;
450
Linus Walleijb5482f72017-09-26 01:09:08 +0200451 val = of_get_named_gpio_flags(np, "alarm-gpios", 0, &flags);
Simon Guinot73ef85f2015-02-25 18:58:19 +0100452 if (val < 0)
453 return val;
Linus Walleijc9933cb2017-09-26 01:09:09 +0200454 fan_data->alarm_gpio = val;
455 fan_data->alarm_gpio_active_low = flags & OF_GPIO_ACTIVE_LOW;
Simon Guinot73ef85f2015-02-25 18:58:19 +0100456 }
457
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100458 /* Fill GPIO pin array */
Linus Walleije99c2e52017-09-26 01:09:10 +0200459 fan_data->num_gpios = of_gpio_count(np);
460 if (fan_data->num_gpios <= 0) {
Linus Walleijc9933cb2017-09-26 01:09:09 +0200461 if (fan_data->alarm_gpio)
Simon Guinot73ef85f2015-02-25 18:58:19 +0100462 return 0;
463 dev_err(dev, "DT properties empty / missing");
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100464 return -ENODEV;
465 }
Linus Walleije99c2e52017-09-26 01:09:10 +0200466 gpios = devm_kzalloc(dev, fan_data->num_gpios * sizeof(unsigned int),
Linus Walleijb5482f72017-09-26 01:09:08 +0200467 GFP_KERNEL);
Linus Walleije99c2e52017-09-26 01:09:10 +0200468 if (!gpios)
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100469 return -ENOMEM;
Linus Walleije99c2e52017-09-26 01:09:10 +0200470 for (i = 0; i < fan_data->num_gpios; i++) {
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100471 int val;
472
Linus Walleijb5482f72017-09-26 01:09:08 +0200473 val = of_get_gpio(np, i);
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100474 if (val < 0)
475 return val;
Linus Walleije99c2e52017-09-26 01:09:10 +0200476 gpios[i] = val;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100477 }
Linus Walleije99c2e52017-09-26 01:09:10 +0200478 fan_data->gpios = gpios;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100479
480 /* Get number of RPM/ctrl_val pairs in speed map */
Linus Walleijb5482f72017-09-26 01:09:08 +0200481 prop = of_find_property(np, "gpio-fan,speed-map", &i);
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100482 if (!prop) {
483 dev_err(dev, "gpio-fan,speed-map DT property missing");
484 return -ENODEV;
485 }
486 i = i / sizeof(u32);
487 if (i == 0 || i & 1) {
488 dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
489 return -ENODEV;
490 }
Linus Walleijb5482f72017-09-26 01:09:08 +0200491 fan_data->num_speed = i / 2;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100492
493 /*
494 * Populate speed map
495 * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
496 * this needs splitting into pairs to create gpio_fan_speed structs
497 */
498 speed = devm_kzalloc(dev,
Linus Walleijb5482f72017-09-26 01:09:08 +0200499 fan_data->num_speed * sizeof(struct gpio_fan_speed),
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100500 GFP_KERNEL);
501 if (!speed)
502 return -ENOMEM;
503 p = NULL;
Linus Walleijb5482f72017-09-26 01:09:08 +0200504 for (i = 0; i < fan_data->num_speed; i++) {
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100505 p = of_prop_next_u32(prop, p, &u);
506 if (!p)
507 return -ENODEV;
508 speed[i].rpm = u;
509 p = of_prop_next_u32(prop, p, &u);
510 if (!p)
511 return -ENODEV;
512 speed[i].ctrl_val = u;
513 }
Linus Walleijb5482f72017-09-26 01:09:08 +0200514 fan_data->speed = speed;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100515
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100516 return 0;
517}
518
Jingoo Han6de709c2014-05-07 17:27:54 +0900519static const struct of_device_id of_gpio_fan_match[] = {
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100520 { .compatible = "gpio-fan", },
521 {},
522};
Luis de Bethencourtfe515282015-09-17 18:09:28 +0200523MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100524
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500525static int gpio_fan_probe(struct platform_device *pdev)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200526{
527 int err;
528 struct gpio_fan_data *fan_data;
Linus Walleijf9013c12017-09-26 01:09:04 +0200529 struct device *dev = &pdev->dev;
530 struct device_node *np = dev->of_node;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200531
Linus Walleijf9013c12017-09-26 01:09:04 +0200532 fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600533 GFP_KERNEL);
534 if (!fan_data)
535 return -ENOMEM;
536
Linus Walleijb5482f72017-09-26 01:09:08 +0200537 err = gpio_fan_get_of_data(fan_data);
Linus Walleija9b4c8a2017-09-26 01:09:07 +0200538 if (err)
539 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200540
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200541 fan_data->dev = dev;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200542 platform_set_drvdata(pdev, fan_data);
543 mutex_init(&fan_data->lock);
544
545 /* Configure alarm GPIO if available. */
Linus Walleijc9933cb2017-09-26 01:09:09 +0200546 if (fan_data->alarm_gpio) {
Linus Walleijb5482f72017-09-26 01:09:08 +0200547 err = fan_alarm_init(fan_data);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200548 if (err)
Guenter Roeckd00985f2012-06-02 09:58:07 -0700549 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200550 }
551
552 /* Configure control GPIOs if available. */
Linus Walleije99c2e52017-09-26 01:09:10 +0200553 if (fan_data->gpios && fan_data->num_gpios > 0) {
Linus Walleijb5482f72017-09-26 01:09:08 +0200554 if (!fan_data->speed || fan_data->num_speed <= 1)
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700555 return -EINVAL;
Linus Walleijb5482f72017-09-26 01:09:08 +0200556 err = fan_ctrl_init(fan_data);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200557 if (err)
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700558 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200559 }
560
Simon Guinotd6fe1362010-10-22 00:44:19 +0200561 /* Make this driver part of hwmon class. */
Axel Lin49153b02014-06-14 14:50:50 +0800562 fan_data->hwmon_dev =
Linus Walleijf9013c12017-09-26 01:09:04 +0200563 devm_hwmon_device_register_with_groups(dev,
Axel Lin49153b02014-06-14 14:50:50 +0800564 "gpio_fan", fan_data,
565 gpio_fan_groups);
Guenter Roeck7258a122013-07-06 09:46:14 -0700566 if (IS_ERR(fan_data->hwmon_dev))
567 return PTR_ERR(fan_data->hwmon_dev);
Linus Walleija9b4c8a2017-09-26 01:09:07 +0200568
Nishanth Menone76ea262015-04-08 18:23:52 -0500569 /* Optional cooling device register for Device tree platforms */
Linus Walleijf9013c12017-09-26 01:09:04 +0200570 fan_data->cdev = thermal_of_cooling_device_register(np,
Nishanth Menone76ea262015-04-08 18:23:52 -0500571 "gpio-fan",
572 fan_data,
573 &gpio_fan_cool_ops);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200574
Linus Walleijf9013c12017-09-26 01:09:04 +0200575 dev_info(dev, "GPIO fan initialized\n");
Simon Guinotd6fe1362010-10-22 00:44:19 +0200576
577 return 0;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200578}
579
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600580static int gpio_fan_remove(struct platform_device *pdev)
Nishanth Menonb95579c2014-12-04 10:58:56 -0600581{
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600582 struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
583
584 if (!IS_ERR(fan_data->cdev))
585 thermal_cooling_device_unregister(fan_data->cdev);
Nishanth Menonb95579c2014-12-04 10:58:56 -0600586
Linus Walleije99c2e52017-09-26 01:09:10 +0200587 if (fan_data->gpios)
Nishanth Menonb95579c2014-12-04 10:58:56 -0600588 set_fan_speed(fan_data, 0);
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600589
590 return 0;
591}
592
593static void gpio_fan_shutdown(struct platform_device *pdev)
594{
595 gpio_fan_remove(pdev);
Nishanth Menonb95579c2014-12-04 10:58:56 -0600596}
597
Rafael J. Wysocki6d20a6c2012-07-08 00:01:03 +0200598#ifdef CONFIG_PM_SLEEP
599static int gpio_fan_suspend(struct device *dev)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200600{
Rafael J. Wysocki6d20a6c2012-07-08 00:01:03 +0200601 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200602
Linus Walleije99c2e52017-09-26 01:09:10 +0200603 if (fan_data->gpios) {
Simon Guinotd6fe1362010-10-22 00:44:19 +0200604 fan_data->resume_speed = fan_data->speed_index;
605 set_fan_speed(fan_data, 0);
606 }
607
608 return 0;
609}
610
Rafael J. Wysocki6d20a6c2012-07-08 00:01:03 +0200611static int gpio_fan_resume(struct device *dev)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200612{
Rafael J. Wysocki6d20a6c2012-07-08 00:01:03 +0200613 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200614
Linus Walleije99c2e52017-09-26 01:09:10 +0200615 if (fan_data->gpios)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200616 set_fan_speed(fan_data, fan_data->resume_speed);
617
618 return 0;
619}
Rafael J. Wysocki6d20a6c2012-07-08 00:01:03 +0200620
621static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
Guenter Roeck24f9c532013-01-10 05:54:40 -0800622#define GPIO_FAN_PM (&gpio_fan_pm)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200623#else
Rafael J. Wysocki6d20a6c2012-07-08 00:01:03 +0200624#define GPIO_FAN_PM NULL
Simon Guinotd6fe1362010-10-22 00:44:19 +0200625#endif
626
627static struct platform_driver gpio_fan_driver = {
628 .probe = gpio_fan_probe,
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600629 .remove = gpio_fan_remove,
Nishanth Menonb95579c2014-12-04 10:58:56 -0600630 .shutdown = gpio_fan_shutdown,
Simon Guinotd6fe1362010-10-22 00:44:19 +0200631 .driver = {
632 .name = "gpio-fan",
Rafael J. Wysocki6d20a6c2012-07-08 00:01:03 +0200633 .pm = GPIO_FAN_PM,
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100634 .of_match_table = of_match_ptr(of_gpio_fan_match),
Simon Guinotd6fe1362010-10-22 00:44:19 +0200635 },
636};
637
Axel Lin25a236a2011-11-25 02:31:00 -0500638module_platform_driver(gpio_fan_driver);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200639
640MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
641MODULE_DESCRIPTION("GPIO FAN driver");
642MODULE_LICENSE("GPL");
643MODULE_ALIAS("platform:gpio-fan");