blob: 568ce4b25a9e380d9eefddcbc0d332333d9b0295 [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 <[email protected]>
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. */
49 int num_ctrl;
50 unsigned *ctrl;
51 int num_speed;
52 struct gpio_fan_speed *speed;
53 int speed_index;
Rafael J. Wysocki6d20a6c02012-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
137 for (i = 0; i < fan_data->num_ctrl; i++)
Nishanth Menon52a95c12014-12-04 10:58:47 -0600138 gpio_set_value_cansleep(fan_data->ctrl[i], (ctrl_val >> i) & 1);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200139}
140
141static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
142{
143 int i;
144 int ctrl_val = 0;
145
146 for (i = 0; i < fan_data->num_ctrl; i++) {
147 int value;
148
Nishanth Menon52a95c12014-12-04 10:58:47 -0600149 value = gpio_get_value_cansleep(fan_data->ctrl[i]);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200150 ctrl_val |= (value << i);
151 }
152 return ctrl_val;
153}
154
155/* Must be called with fan_data->lock held, except during initialization. */
156static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
157{
158 if (fan_data->speed_index == speed_index)
159 return;
160
161 __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
162 fan_data->speed_index = speed_index;
163}
164
165static int get_fan_speed_index(struct gpio_fan_data *fan_data)
166{
167 int ctrl_val = __get_fan_ctrl(fan_data);
168 int i;
169
170 for (i = 0; i < fan_data->num_speed; i++)
171 if (fan_data->speed[i].ctrl_val == ctrl_val)
172 return i;
173
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200174 dev_warn(fan_data->dev,
Simon Guinotd6fe1362010-10-22 00:44:19 +0200175 "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
176
Guenter Roeckc52ae3d2013-09-13 10:42:39 -0700177 return -ENODEV;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200178}
179
Axel Lin2565fb02014-08-02 13:36:38 +0800180static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200181{
182 struct gpio_fan_speed *speed = fan_data->speed;
183 int i;
184
185 for (i = 0; i < fan_data->num_speed; i++)
186 if (speed[i].rpm >= rpm)
187 return i;
188
189 return fan_data->num_speed - 1;
190}
191
Julia Lawallc490c632016-12-22 13:04:44 +0100192static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
193 char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200194{
195 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
196 u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
197
198 return sprintf(buf, "%d\n", pwm);
199}
200
Julia Lawallc490c632016-12-22 13:04:44 +0100201static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
202 const char *buf, size_t count)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200203{
204 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
205 unsigned long pwm;
206 int speed_index;
207 int ret = count;
208
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100209 if (kstrtoul(buf, 10, &pwm) || pwm > 255)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200210 return -EINVAL;
211
212 mutex_lock(&fan_data->lock);
213
214 if (!fan_data->pwm_enable) {
215 ret = -EPERM;
216 goto exit_unlock;
217 }
218
219 speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
220 set_fan_speed(fan_data, speed_index);
221
222exit_unlock:
223 mutex_unlock(&fan_data->lock);
224
225 return ret;
226}
227
Julia Lawallc490c632016-12-22 13:04:44 +0100228static ssize_t pwm1_enable_show(struct device *dev,
229 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200230{
231 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
232
233 return sprintf(buf, "%d\n", fan_data->pwm_enable);
234}
235
Julia Lawallc490c632016-12-22 13:04:44 +0100236static ssize_t pwm1_enable_store(struct device *dev,
237 struct device_attribute *attr,
238 const char *buf, size_t count)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200239{
240 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
241 unsigned long val;
242
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100243 if (kstrtoul(buf, 10, &val) || val > 1)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200244 return -EINVAL;
245
246 if (fan_data->pwm_enable == val)
247 return count;
248
249 mutex_lock(&fan_data->lock);
250
251 fan_data->pwm_enable = val;
252
253 /* Disable manual control mode: set fan at full speed. */
254 if (val == 0)
255 set_fan_speed(fan_data, fan_data->num_speed - 1);
256
257 mutex_unlock(&fan_data->lock);
258
259 return count;
260}
261
Julia Lawallc490c632016-12-22 13:04:44 +0100262static ssize_t pwm1_mode_show(struct device *dev,
263 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200264{
265 return sprintf(buf, "0\n");
266}
267
Julia Lawallc490c632016-12-22 13:04:44 +0100268static ssize_t fan1_min_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200270{
271 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
272
273 return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
274}
275
Julia Lawallc490c632016-12-22 13:04:44 +0100276static ssize_t fan1_max_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200278{
279 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
280
281 return sprintf(buf, "%d\n",
282 fan_data->speed[fan_data->num_speed - 1].rpm);
283}
284
Julia Lawallc490c632016-12-22 13:04:44 +0100285static ssize_t fan1_input_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200287{
288 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
289
290 return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
291}
292
293static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
294 const char *buf, size_t count)
295{
296 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
297 unsigned long rpm;
298 int ret = count;
299
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100300 if (kstrtoul(buf, 10, &rpm))
Simon Guinotd6fe1362010-10-22 00:44:19 +0200301 return -EINVAL;
302
303 mutex_lock(&fan_data->lock);
304
305 if (!fan_data->pwm_enable) {
306 ret = -EPERM;
307 goto exit_unlock;
308 }
309
310 set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
311
312exit_unlock:
313 mutex_unlock(&fan_data->lock);
314
315 return ret;
316}
317
Julia Lawallc490c632016-12-22 13:04:44 +0100318static DEVICE_ATTR_RW(pwm1);
319static DEVICE_ATTR_RW(pwm1_enable);
320static DEVICE_ATTR_RO(pwm1_mode);
321static DEVICE_ATTR_RO(fan1_min);
322static DEVICE_ATTR_RO(fan1_max);
323static DEVICE_ATTR_RO(fan1_input);
324static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, fan1_input_show, set_rpm);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200325
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700326static umode_t gpio_fan_is_visible(struct kobject *kobj,
327 struct attribute *attr, int index)
328{
329 struct device *dev = container_of(kobj, struct device, kobj);
330 struct gpio_fan_data *data = dev_get_drvdata(dev);
331
Linus Walleijc9933cb2017-09-26 01:09:09 +0200332 if (index == 0 && !data->alarm_gpio)
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700333 return 0;
Guenter Roeck7258a122013-07-06 09:46:14 -0700334 if (index > 0 && !data->ctrl)
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700335 return 0;
336
337 return attr->mode;
338}
339
340static struct attribute *gpio_fan_attributes[] = {
Guenter Roeck7258a122013-07-06 09:46:14 -0700341 &dev_attr_fan1_alarm.attr, /* 0 */
342 &dev_attr_pwm1.attr, /* 1 */
Simon Guinotd6fe1362010-10-22 00:44:19 +0200343 &dev_attr_pwm1_enable.attr,
344 &dev_attr_pwm1_mode.attr,
345 &dev_attr_fan1_input.attr,
346 &dev_attr_fan1_target.attr,
347 &dev_attr_fan1_min.attr,
348 &dev_attr_fan1_max.attr,
349 NULL
350};
351
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700352static const struct attribute_group gpio_fan_group = {
353 .attrs = gpio_fan_attributes,
354 .is_visible = gpio_fan_is_visible,
Simon Guinotd6fe1362010-10-22 00:44:19 +0200355};
356
Guenter Roeck7258a122013-07-06 09:46:14 -0700357static const struct attribute_group *gpio_fan_groups[] = {
358 &gpio_fan_group,
359 NULL
360};
361
Linus Walleijb5482f72017-09-26 01:09:08 +0200362static int fan_ctrl_init(struct gpio_fan_data *fan_data)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200363{
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200364 struct device *dev = fan_data->dev;
Linus Walleijb5482f72017-09-26 01:09:08 +0200365 int num_ctrl = fan_data->num_ctrl;
366 unsigned int *ctrl = fan_data->ctrl;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200367 int i, err;
368
369 for (i = 0; i < num_ctrl; i++) {
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200370 err = devm_gpio_request(dev, ctrl[i],
Guenter Roeckd00985f2012-06-02 09:58:07 -0700371 "GPIO fan control");
Simon Guinotd6fe1362010-10-22 00:44:19 +0200372 if (err)
Guenter Roeckd00985f2012-06-02 09:58:07 -0700373 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200374
Nishanth Menon52a95c12014-12-04 10:58:47 -0600375 err = gpio_direction_output(ctrl[i],
376 gpio_get_value_cansleep(ctrl[i]));
Guenter Roeckd00985f2012-06-02 09:58:07 -0700377 if (err)
378 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200379 }
380
Simon Guinotd6fe1362010-10-22 00:44:19 +0200381 fan_data->pwm_enable = true; /* Enable manual fan speed control. */
382 fan_data->speed_index = get_fan_speed_index(fan_data);
Guenter Roeckd00985f2012-06-02 09:58:07 -0700383 if (fan_data->speed_index < 0)
Guenter Roeckc52ae3d2013-09-13 10:42:39 -0700384 return fan_data->speed_index;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200385
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700386 return 0;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200387}
388
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600389static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
390 unsigned long *state)
391{
392 struct gpio_fan_data *fan_data = cdev->devdata;
393
394 if (!fan_data)
395 return -EINVAL;
396
397 *state = fan_data->num_speed - 1;
398 return 0;
399}
400
401static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
402 unsigned long *state)
403{
404 struct gpio_fan_data *fan_data = cdev->devdata;
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600405
406 if (!fan_data)
407 return -EINVAL;
408
Nishanth Menon000e09492016-02-19 18:09:51 -0600409 *state = fan_data->speed_index;
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600410 return 0;
411}
412
413static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
414 unsigned long state)
415{
416 struct gpio_fan_data *fan_data = cdev->devdata;
417
418 if (!fan_data)
419 return -EINVAL;
420
421 set_fan_speed(fan_data, state);
422 return 0;
423}
424
425static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
426 .get_max_state = gpio_fan_get_max_state,
427 .get_cur_state = gpio_fan_get_cur_state,
428 .set_cur_state = gpio_fan_set_cur_state,
429};
430
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100431/*
432 * Translate OpenFirmware node properties into platform_data
433 */
Linus Walleijb5482f72017-09-26 01:09:08 +0200434static int gpio_fan_get_of_data(struct gpio_fan_data *fan_data)
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100435{
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100436 struct gpio_fan_speed *speed;
Linus Walleijb5482f72017-09-26 01:09:08 +0200437 struct device *dev = fan_data->dev;
438 struct device_node *np = dev->of_node;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100439 unsigned *ctrl;
440 unsigned i;
441 u32 u;
442 struct property *prop;
443 const __be32 *p;
444
Simon Guinot73ef85f2015-02-25 18:58:19 +0100445 /* Alarm GPIO if one exists */
Linus Walleijb5482f72017-09-26 01:09:08 +0200446 if (of_gpio_named_count(np, "alarm-gpios") > 0) {
Simon Guinot73ef85f2015-02-25 18:58:19 +0100447 int val;
448 enum of_gpio_flags flags;
449
Linus Walleijb5482f72017-09-26 01:09:08 +0200450 val = of_get_named_gpio_flags(np, "alarm-gpios", 0, &flags);
Simon Guinot73ef85f2015-02-25 18:58:19 +0100451 if (val < 0)
452 return val;
Linus Walleijc9933cb2017-09-26 01:09:09 +0200453 fan_data->alarm_gpio = val;
454 fan_data->alarm_gpio_active_low = flags & OF_GPIO_ACTIVE_LOW;
Simon Guinot73ef85f2015-02-25 18:58:19 +0100455 }
456
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100457 /* Fill GPIO pin array */
Linus Walleijb5482f72017-09-26 01:09:08 +0200458 fan_data->num_ctrl = of_gpio_count(np);
459 if (fan_data->num_ctrl <= 0) {
Linus Walleijc9933cb2017-09-26 01:09:09 +0200460 if (fan_data->alarm_gpio)
Simon Guinot73ef85f2015-02-25 18:58:19 +0100461 return 0;
462 dev_err(dev, "DT properties empty / missing");
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100463 return -ENODEV;
464 }
Linus Walleijb5482f72017-09-26 01:09:08 +0200465 ctrl = devm_kzalloc(dev, fan_data->num_ctrl * sizeof(unsigned int),
466 GFP_KERNEL);
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100467 if (!ctrl)
468 return -ENOMEM;
Linus Walleijb5482f72017-09-26 01:09:08 +0200469 for (i = 0; i < fan_data->num_ctrl; i++) {
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100470 int val;
471
Linus Walleijb5482f72017-09-26 01:09:08 +0200472 val = of_get_gpio(np, i);
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100473 if (val < 0)
474 return val;
475 ctrl[i] = val;
476 }
Linus Walleijb5482f72017-09-26 01:09:08 +0200477 fan_data->ctrl = ctrl;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100478
479 /* Get number of RPM/ctrl_val pairs in speed map */
Linus Walleijb5482f72017-09-26 01:09:08 +0200480 prop = of_find_property(np, "gpio-fan,speed-map", &i);
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100481 if (!prop) {
482 dev_err(dev, "gpio-fan,speed-map DT property missing");
483 return -ENODEV;
484 }
485 i = i / sizeof(u32);
486 if (i == 0 || i & 1) {
487 dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
488 return -ENODEV;
489 }
Linus Walleijb5482f72017-09-26 01:09:08 +0200490 fan_data->num_speed = i / 2;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100491
492 /*
493 * Populate speed map
494 * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
495 * this needs splitting into pairs to create gpio_fan_speed structs
496 */
497 speed = devm_kzalloc(dev,
Linus Walleijb5482f72017-09-26 01:09:08 +0200498 fan_data->num_speed * sizeof(struct gpio_fan_speed),
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100499 GFP_KERNEL);
500 if (!speed)
501 return -ENOMEM;
502 p = NULL;
Linus Walleijb5482f72017-09-26 01:09:08 +0200503 for (i = 0; i < fan_data->num_speed; i++) {
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100504 p = of_prop_next_u32(prop, p, &u);
505 if (!p)
506 return -ENODEV;
507 speed[i].rpm = u;
508 p = of_prop_next_u32(prop, p, &u);
509 if (!p)
510 return -ENODEV;
511 speed[i].ctrl_val = u;
512 }
Linus Walleijb5482f72017-09-26 01:09:08 +0200513 fan_data->speed = speed;
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100514
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100515 return 0;
516}
517
Jingoo Han6de709c2014-05-07 17:27:54 +0900518static const struct of_device_id of_gpio_fan_match[] = {
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100519 { .compatible = "gpio-fan", },
520 {},
521};
Luis de Bethencourtfe515282015-09-17 18:09:28 +0200522MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100523
Bill Pemberton6c931ae2012-11-19 13:22:35 -0500524static int gpio_fan_probe(struct platform_device *pdev)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200525{
526 int err;
527 struct gpio_fan_data *fan_data;
Linus Walleijf9013c12017-09-26 01:09:04 +0200528 struct device *dev = &pdev->dev;
529 struct device_node *np = dev->of_node;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200530
Linus Walleijf9013c12017-09-26 01:09:04 +0200531 fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600532 GFP_KERNEL);
533 if (!fan_data)
534 return -ENOMEM;
535
Linus Walleijb5482f72017-09-26 01:09:08 +0200536 err = gpio_fan_get_of_data(fan_data);
Linus Walleija9b4c8a2017-09-26 01:09:07 +0200537 if (err)
538 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200539
Linus Walleij8c0eb9b2017-09-26 01:09:06 +0200540 fan_data->dev = dev;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200541 platform_set_drvdata(pdev, fan_data);
542 mutex_init(&fan_data->lock);
543
544 /* Configure alarm GPIO if available. */
Linus Walleijc9933cb2017-09-26 01:09:09 +0200545 if (fan_data->alarm_gpio) {
Linus Walleijb5482f72017-09-26 01:09:08 +0200546 err = fan_alarm_init(fan_data);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200547 if (err)
Guenter Roeckd00985f2012-06-02 09:58:07 -0700548 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200549 }
550
551 /* Configure control GPIOs if available. */
Linus Walleijb5482f72017-09-26 01:09:08 +0200552 if (fan_data->ctrl && fan_data->num_ctrl > 0) {
553 if (!fan_data->speed || fan_data->num_speed <= 1)
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700554 return -EINVAL;
Linus Walleijb5482f72017-09-26 01:09:08 +0200555 err = fan_ctrl_init(fan_data);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200556 if (err)
Guenter Roeckc81cc5a2013-03-30 09:09:39 -0700557 return err;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200558 }
559
Simon Guinotd6fe1362010-10-22 00:44:19 +0200560 /* Make this driver part of hwmon class. */
Axel Lin49153b02014-06-14 14:50:50 +0800561 fan_data->hwmon_dev =
Linus Walleijf9013c12017-09-26 01:09:04 +0200562 devm_hwmon_device_register_with_groups(dev,
Axel Lin49153b02014-06-14 14:50:50 +0800563 "gpio_fan", fan_data,
564 gpio_fan_groups);
Guenter Roeck7258a122013-07-06 09:46:14 -0700565 if (IS_ERR(fan_data->hwmon_dev))
566 return PTR_ERR(fan_data->hwmon_dev);
Linus Walleija9b4c8a2017-09-26 01:09:07 +0200567
Nishanth Menone76ea262015-04-08 18:23:52 -0500568 /* Optional cooling device register for Device tree platforms */
Linus Walleijf9013c12017-09-26 01:09:04 +0200569 fan_data->cdev = thermal_of_cooling_device_register(np,
Nishanth Menone76ea262015-04-08 18:23:52 -0500570 "gpio-fan",
571 fan_data,
572 &gpio_fan_cool_ops);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200573
Linus Walleijf9013c12017-09-26 01:09:04 +0200574 dev_info(dev, "GPIO fan initialized\n");
Simon Guinotd6fe1362010-10-22 00:44:19 +0200575
576 return 0;
Simon Guinotd6fe1362010-10-22 00:44:19 +0200577}
578
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600579static int gpio_fan_remove(struct platform_device *pdev)
Nishanth Menonb95579c2014-12-04 10:58:56 -0600580{
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600581 struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
582
583 if (!IS_ERR(fan_data->cdev))
584 thermal_cooling_device_unregister(fan_data->cdev);
Nishanth Menonb95579c2014-12-04 10:58:56 -0600585
586 if (fan_data->ctrl)
587 set_fan_speed(fan_data, 0);
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600588
589 return 0;
590}
591
592static void gpio_fan_shutdown(struct platform_device *pdev)
593{
594 gpio_fan_remove(pdev);
Nishanth Menonb95579c2014-12-04 10:58:56 -0600595}
596
Rafael J. Wysocki6d20a6c02012-07-08 00:01:03 +0200597#ifdef CONFIG_PM_SLEEP
598static int gpio_fan_suspend(struct device *dev)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200599{
Rafael J. Wysocki6d20a6c02012-07-08 00:01:03 +0200600 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200601
602 if (fan_data->ctrl) {
603 fan_data->resume_speed = fan_data->speed_index;
604 set_fan_speed(fan_data, 0);
605 }
606
607 return 0;
608}
609
Rafael J. Wysocki6d20a6c02012-07-08 00:01:03 +0200610static int gpio_fan_resume(struct device *dev)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200611{
Rafael J. Wysocki6d20a6c02012-07-08 00:01:03 +0200612 struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200613
614 if (fan_data->ctrl)
615 set_fan_speed(fan_data, fan_data->resume_speed);
616
617 return 0;
618}
Rafael J. Wysocki6d20a6c02012-07-08 00:01:03 +0200619
620static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
Guenter Roeck24f9c532013-01-10 05:54:40 -0800621#define GPIO_FAN_PM (&gpio_fan_pm)
Simon Guinotd6fe1362010-10-22 00:44:19 +0200622#else
Rafael J. Wysocki6d20a6c02012-07-08 00:01:03 +0200623#define GPIO_FAN_PM NULL
Simon Guinotd6fe1362010-10-22 00:44:19 +0200624#endif
625
626static struct platform_driver gpio_fan_driver = {
627 .probe = gpio_fan_probe,
Nishanth Menonb5cf88e2015-01-08 12:05:03 -0600628 .remove = gpio_fan_remove,
Nishanth Menonb95579c2014-12-04 10:58:56 -0600629 .shutdown = gpio_fan_shutdown,
Simon Guinotd6fe1362010-10-22 00:44:19 +0200630 .driver = {
631 .name = "gpio-fan",
Rafael J. Wysocki6d20a6c02012-07-08 00:01:03 +0200632 .pm = GPIO_FAN_PM,
Jamie Lentin55fb8b062012-09-14 17:07:06 +0100633 .of_match_table = of_match_ptr(of_gpio_fan_match),
Simon Guinotd6fe1362010-10-22 00:44:19 +0200634 },
635};
636
Axel Lin25a236a2011-11-25 02:31:00 -0500637module_platform_driver(gpio_fan_driver);
Simon Guinotd6fe1362010-10-22 00:44:19 +0200638
639MODULE_AUTHOR("Simon Guinot <[email protected]>");
640MODULE_DESCRIPTION("GPIO FAN driver");
641MODULE_LICENSE("GPL");
642MODULE_ALIAS("platform:gpio-fan");