Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 1 | /* |
| 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 Kamat | c50588a | 2013-09-27 16:56:00 +0530 | [diff] [blame] | 33 | #include <linux/of.h> |
Jamie Lentin | 55fb8b06 | 2012-09-14 17:07:06 +0100 | [diff] [blame] | 34 | #include <linux/of_platform.h> |
| 35 | #include <linux/of_gpio.h> |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 36 | #include <linux/thermal.h> |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 37 | |
Linus Walleij | ef7a612 | 2017-09-26 01:09:05 +0200 | [diff] [blame] | 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 | |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 60 | struct gpio_fan_data { |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 61 | struct device *dev; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 62 | struct device *hwmon_dev; |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 63 | /* Cooling device if any */ |
| 64 | struct thermal_cooling_device *cdev; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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; |
Rafael J. Wysocki | 6d20a6c0 | 2012-07-08 00:01:03 +0200 | [diff] [blame] | 71 | #ifdef CONFIG_PM_SLEEP |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 88 | sysfs_notify(&fan_data->dev->kobj, NULL, "fan1_alarm"); |
| 89 | kobject_uevent(&fan_data->dev->kobj, KOBJ_CHANGE); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 101 | static ssize_t fan1_alarm_show(struct device *dev, |
| 102 | struct device_attribute *attr, char *buf) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 103 | { |
| 104 | struct gpio_fan_data *fan_data = dev_get_drvdata(dev); |
| 105 | struct gpio_fan_alarm *alarm = fan_data->alarm; |
Nishanth Menon | 52a95c1 | 2014-12-04 10:58:47 -0600 | [diff] [blame] | 106 | int value = gpio_get_value_cansleep(alarm->gpio); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 107 | |
| 108 | if (alarm->active_low) |
| 109 | value = !value; |
| 110 | |
| 111 | return sprintf(buf, "%d\n", value); |
| 112 | } |
| 113 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 114 | static DEVICE_ATTR_RO(fan1_alarm); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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; |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 121 | struct device *dev = fan_data->dev; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 122 | |
| 123 | fan_data->alarm = alarm; |
| 124 | |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 125 | err = devm_gpio_request(dev, alarm->gpio, "GPIO fan alarm"); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 126 | if (err) |
| 127 | return err; |
| 128 | |
| 129 | err = gpio_direction_input(alarm->gpio); |
| 130 | if (err) |
Guenter Roeck | d00985f | 2012-06-02 09:58:07 -0700 | [diff] [blame] | 131 | return err; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 132 | |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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); |
Thomas Gleixner | dced35a | 2011-03-28 17:49:12 +0200 | [diff] [blame] | 142 | irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH); |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 143 | err = devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler, |
Guenter Roeck | d00985f | 2012-06-02 09:58:07 -0700 | [diff] [blame] | 144 | IRQF_SHARED, "GPIO fan alarm", fan_data); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 145 | return err; |
| 146 | } |
| 147 | |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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++) |
Nishanth Menon | 52a95c1 | 2014-12-04 10:58:47 -0600 | [diff] [blame] | 158 | gpio_set_value_cansleep(fan_data->ctrl[i], (ctrl_val >> i) & 1); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Nishanth Menon | 52a95c1 | 2014-12-04 10:58:47 -0600 | [diff] [blame] | 169 | value = gpio_get_value_cansleep(fan_data->ctrl[i]); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 194 | dev_warn(fan_data->dev, |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 195 | "missing speed array entry for GPIO value 0x%x\n", ctrl_val); |
| 196 | |
Guenter Roeck | c52ae3d | 2013-09-13 10:42:39 -0700 | [diff] [blame] | 197 | return -ENODEV; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Axel Lin | 2565fb0 | 2014-08-02 13:36:38 +0800 | [diff] [blame] | 200 | static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 212 | static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr, |
| 213 | char *buf) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 221 | static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr, |
| 222 | const char *buf, size_t count) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 229 | if (kstrtoul(buf, 10, &pwm) || pwm > 255) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 248 | static ssize_t pwm1_enable_show(struct device *dev, |
| 249 | struct device_attribute *attr, char *buf) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 256 | static ssize_t pwm1_enable_store(struct device *dev, |
| 257 | struct device_attribute *attr, |
| 258 | const char *buf, size_t count) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 259 | { |
| 260 | struct gpio_fan_data *fan_data = dev_get_drvdata(dev); |
| 261 | unsigned long val; |
| 262 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 263 | if (kstrtoul(buf, 10, &val) || val > 1) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 282 | static ssize_t pwm1_mode_show(struct device *dev, |
| 283 | struct device_attribute *attr, char *buf) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 284 | { |
| 285 | return sprintf(buf, "0\n"); |
| 286 | } |
| 287 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 288 | static ssize_t fan1_min_show(struct device *dev, |
| 289 | struct device_attribute *attr, char *buf) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 296 | static ssize_t fan1_max_show(struct device *dev, |
| 297 | struct device_attribute *attr, char *buf) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 305 | static ssize_t fan1_input_show(struct device *dev, |
| 306 | struct device_attribute *attr, char *buf) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Frans Meulenbroeks | 179c4fd | 2012-01-04 20:58:52 +0100 | [diff] [blame] | 320 | if (kstrtoul(buf, 10, &rpm)) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Julia Lawall | c490c63 | 2016-12-22 13:04:44 +0100 | [diff] [blame] | 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); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 345 | |
Guenter Roeck | c81cc5a | 2013-03-30 09:09:39 -0700 | [diff] [blame] | 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 | |
Guenter Roeck | 7258a12 | 2013-07-06 09:46:14 -0700 | [diff] [blame] | 352 | if (index == 0 && !data->alarm) |
Guenter Roeck | c81cc5a | 2013-03-30 09:09:39 -0700 | [diff] [blame] | 353 | return 0; |
Guenter Roeck | 7258a12 | 2013-07-06 09:46:14 -0700 | [diff] [blame] | 354 | if (index > 0 && !data->ctrl) |
Guenter Roeck | c81cc5a | 2013-03-30 09:09:39 -0700 | [diff] [blame] | 355 | return 0; |
| 356 | |
| 357 | return attr->mode; |
| 358 | } |
| 359 | |
| 360 | static struct attribute *gpio_fan_attributes[] = { |
Guenter Roeck | 7258a12 | 2013-07-06 09:46:14 -0700 | [diff] [blame] | 361 | &dev_attr_fan1_alarm.attr, /* 0 */ |
| 362 | &dev_attr_pwm1.attr, /* 1 */ |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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 | |
Guenter Roeck | c81cc5a | 2013-03-30 09:09:39 -0700 | [diff] [blame] | 372 | static const struct attribute_group gpio_fan_group = { |
| 373 | .attrs = gpio_fan_attributes, |
| 374 | .is_visible = gpio_fan_is_visible, |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 375 | }; |
| 376 | |
Guenter Roeck | 7258a12 | 2013-07-06 09:46:14 -0700 | [diff] [blame] | 377 | static const struct attribute_group *gpio_fan_groups[] = { |
| 378 | &gpio_fan_group, |
| 379 | NULL |
| 380 | }; |
| 381 | |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 382 | static int fan_ctrl_init(struct gpio_fan_data *fan_data, |
| 383 | struct gpio_fan_platform_data *pdata) |
| 384 | { |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 385 | struct device *dev = fan_data->dev; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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++) { |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 391 | err = devm_gpio_request(dev, ctrl[i], |
Guenter Roeck | d00985f | 2012-06-02 09:58:07 -0700 | [diff] [blame] | 392 | "GPIO fan control"); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 393 | if (err) |
Guenter Roeck | d00985f | 2012-06-02 09:58:07 -0700 | [diff] [blame] | 394 | return err; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 395 | |
Nishanth Menon | 52a95c1 | 2014-12-04 10:58:47 -0600 | [diff] [blame] | 396 | err = gpio_direction_output(ctrl[i], |
| 397 | gpio_get_value_cansleep(ctrl[i])); |
Guenter Roeck | d00985f | 2012-06-02 09:58:07 -0700 | [diff] [blame] | 398 | if (err) |
| 399 | return err; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 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); |
Guenter Roeck | d00985f | 2012-06-02 09:58:07 -0700 | [diff] [blame] | 408 | if (fan_data->speed_index < 0) |
Guenter Roeck | c52ae3d | 2013-09-13 10:42:39 -0700 | [diff] [blame] | 409 | return fan_data->speed_index; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 410 | |
Guenter Roeck | c81cc5a | 2013-03-30 09:09:39 -0700 | [diff] [blame] | 411 | return 0; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 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; |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 430 | |
| 431 | if (!fan_data) |
| 432 | return -EINVAL; |
| 433 | |
Nishanth Menon | 000e0949 | 2016-02-19 18:09:51 -0600 | [diff] [blame] | 434 | *state = fan_data->speed_index; |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 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 | |
Jamie Lentin | 55fb8b06 | 2012-09-14 17:07:06 +0100 | [diff] [blame] | 456 | /* |
| 457 | * Translate OpenFirmware node properties into platform_data |
| 458 | */ |
| 459 | static int gpio_fan_get_of_pdata(struct device *dev, |
| 460 | struct gpio_fan_platform_data *pdata) |
| 461 | { |
| 462 | struct device_node *node; |
| 463 | struct gpio_fan_speed *speed; |
| 464 | unsigned *ctrl; |
| 465 | unsigned i; |
| 466 | u32 u; |
| 467 | struct property *prop; |
| 468 | const __be32 *p; |
| 469 | |
| 470 | node = dev->of_node; |
| 471 | |
Simon Guinot | 73ef85f | 2015-02-25 18:58:19 +0100 | [diff] [blame] | 472 | /* Alarm GPIO if one exists */ |
| 473 | if (of_gpio_named_count(node, "alarm-gpios") > 0) { |
| 474 | struct gpio_fan_alarm *alarm; |
| 475 | int val; |
| 476 | enum of_gpio_flags flags; |
| 477 | |
| 478 | alarm = devm_kzalloc(dev, sizeof(struct gpio_fan_alarm), |
| 479 | GFP_KERNEL); |
| 480 | if (!alarm) |
| 481 | return -ENOMEM; |
| 482 | |
| 483 | val = of_get_named_gpio_flags(node, "alarm-gpios", 0, &flags); |
| 484 | if (val < 0) |
| 485 | return val; |
| 486 | alarm->gpio = val; |
| 487 | alarm->active_low = flags & OF_GPIO_ACTIVE_LOW; |
| 488 | |
| 489 | pdata->alarm = alarm; |
| 490 | } |
| 491 | |
Jamie Lentin | 55fb8b06 | 2012-09-14 17:07:06 +0100 | [diff] [blame] | 492 | /* Fill GPIO pin array */ |
| 493 | pdata->num_ctrl = of_gpio_count(node); |
Grant Likely | e80beb2 | 2013-02-12 17:48:37 +0000 | [diff] [blame] | 494 | if (pdata->num_ctrl <= 0) { |
Simon Guinot | 73ef85f | 2015-02-25 18:58:19 +0100 | [diff] [blame] | 495 | if (pdata->alarm) |
| 496 | return 0; |
| 497 | dev_err(dev, "DT properties empty / missing"); |
Jamie Lentin | 55fb8b06 | 2012-09-14 17:07:06 +0100 | [diff] [blame] | 498 | return -ENODEV; |
| 499 | } |
| 500 | ctrl = devm_kzalloc(dev, pdata->num_ctrl * sizeof(unsigned), |
| 501 | GFP_KERNEL); |
| 502 | if (!ctrl) |
| 503 | return -ENOMEM; |
| 504 | for (i = 0; i < pdata->num_ctrl; i++) { |
| 505 | int val; |
| 506 | |
| 507 | val = of_get_gpio(node, i); |
| 508 | if (val < 0) |
| 509 | return val; |
| 510 | ctrl[i] = val; |
| 511 | } |
| 512 | pdata->ctrl = ctrl; |
| 513 | |
| 514 | /* Get number of RPM/ctrl_val pairs in speed map */ |
| 515 | prop = of_find_property(node, "gpio-fan,speed-map", &i); |
| 516 | if (!prop) { |
| 517 | dev_err(dev, "gpio-fan,speed-map DT property missing"); |
| 518 | return -ENODEV; |
| 519 | } |
| 520 | i = i / sizeof(u32); |
| 521 | if (i == 0 || i & 1) { |
| 522 | dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries"); |
| 523 | return -ENODEV; |
| 524 | } |
| 525 | pdata->num_speed = i / 2; |
| 526 | |
| 527 | /* |
| 528 | * Populate speed map |
| 529 | * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...> |
| 530 | * this needs splitting into pairs to create gpio_fan_speed structs |
| 531 | */ |
| 532 | speed = devm_kzalloc(dev, |
| 533 | pdata->num_speed * sizeof(struct gpio_fan_speed), |
| 534 | GFP_KERNEL); |
| 535 | if (!speed) |
| 536 | return -ENOMEM; |
| 537 | p = NULL; |
| 538 | for (i = 0; i < pdata->num_speed; i++) { |
| 539 | p = of_prop_next_u32(prop, p, &u); |
| 540 | if (!p) |
| 541 | return -ENODEV; |
| 542 | speed[i].rpm = u; |
| 543 | p = of_prop_next_u32(prop, p, &u); |
| 544 | if (!p) |
| 545 | return -ENODEV; |
| 546 | speed[i].ctrl_val = u; |
| 547 | } |
| 548 | pdata->speed = speed; |
| 549 | |
Jamie Lentin | 55fb8b06 | 2012-09-14 17:07:06 +0100 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
Jingoo Han | 6de709c | 2014-05-07 17:27:54 +0900 | [diff] [blame] | 553 | static const struct of_device_id of_gpio_fan_match[] = { |
Jamie Lentin | 55fb8b06 | 2012-09-14 17:07:06 +0100 | [diff] [blame] | 554 | { .compatible = "gpio-fan", }, |
| 555 | {}, |
| 556 | }; |
Luis de Bethencourt | fe51528 | 2015-09-17 18:09:28 +0200 | [diff] [blame] | 557 | MODULE_DEVICE_TABLE(of, of_gpio_fan_match); |
Jamie Lentin | 55fb8b06 | 2012-09-14 17:07:06 +0100 | [diff] [blame] | 558 | |
Bill Pemberton | 6c931ae | 2012-11-19 13:22:35 -0500 | [diff] [blame] | 559 | static int gpio_fan_probe(struct platform_device *pdev) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 560 | { |
| 561 | int err; |
| 562 | struct gpio_fan_data *fan_data; |
Linus Walleij | f9013c1 | 2017-09-26 01:09:04 +0200 | [diff] [blame] | 563 | struct device *dev = &pdev->dev; |
| 564 | struct device_node *np = dev->of_node; |
Linus Walleij | a9b4c8a | 2017-09-26 01:09:07 +0200 | [diff] [blame^] | 565 | struct gpio_fan_platform_data *pdata; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 566 | |
Linus Walleij | f9013c1 | 2017-09-26 01:09:04 +0200 | [diff] [blame] | 567 | fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data), |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 568 | GFP_KERNEL); |
| 569 | if (!fan_data) |
| 570 | return -ENOMEM; |
| 571 | |
Linus Walleij | a9b4c8a | 2017-09-26 01:09:07 +0200 | [diff] [blame^] | 572 | pdata = devm_kzalloc(dev, |
| 573 | sizeof(struct gpio_fan_platform_data), |
| 574 | GFP_KERNEL); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 575 | if (!pdata) |
Linus Walleij | a9b4c8a | 2017-09-26 01:09:07 +0200 | [diff] [blame^] | 576 | return -ENOMEM; |
| 577 | |
| 578 | err = gpio_fan_get_of_pdata(dev, pdata); |
| 579 | if (err) |
| 580 | return err; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 581 | |
Linus Walleij | 8c0eb9b | 2017-09-26 01:09:06 +0200 | [diff] [blame] | 582 | fan_data->dev = dev; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 583 | platform_set_drvdata(pdev, fan_data); |
| 584 | mutex_init(&fan_data->lock); |
| 585 | |
| 586 | /* Configure alarm GPIO if available. */ |
| 587 | if (pdata->alarm) { |
| 588 | err = fan_alarm_init(fan_data, pdata->alarm); |
| 589 | if (err) |
Guenter Roeck | d00985f | 2012-06-02 09:58:07 -0700 | [diff] [blame] | 590 | return err; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | /* Configure control GPIOs if available. */ |
| 594 | if (pdata->ctrl && pdata->num_ctrl > 0) { |
Guenter Roeck | c81cc5a | 2013-03-30 09:09:39 -0700 | [diff] [blame] | 595 | if (!pdata->speed || pdata->num_speed <= 1) |
| 596 | return -EINVAL; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 597 | err = fan_ctrl_init(fan_data, pdata); |
| 598 | if (err) |
Guenter Roeck | c81cc5a | 2013-03-30 09:09:39 -0700 | [diff] [blame] | 599 | return err; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 602 | /* Make this driver part of hwmon class. */ |
Axel Lin | 49153b0 | 2014-06-14 14:50:50 +0800 | [diff] [blame] | 603 | fan_data->hwmon_dev = |
Linus Walleij | f9013c1 | 2017-09-26 01:09:04 +0200 | [diff] [blame] | 604 | devm_hwmon_device_register_with_groups(dev, |
Axel Lin | 49153b0 | 2014-06-14 14:50:50 +0800 | [diff] [blame] | 605 | "gpio_fan", fan_data, |
| 606 | gpio_fan_groups); |
Guenter Roeck | 7258a12 | 2013-07-06 09:46:14 -0700 | [diff] [blame] | 607 | if (IS_ERR(fan_data->hwmon_dev)) |
| 608 | return PTR_ERR(fan_data->hwmon_dev); |
Linus Walleij | a9b4c8a | 2017-09-26 01:09:07 +0200 | [diff] [blame^] | 609 | |
Nishanth Menon | e76ea26 | 2015-04-08 18:23:52 -0500 | [diff] [blame] | 610 | /* Optional cooling device register for Device tree platforms */ |
Linus Walleij | f9013c1 | 2017-09-26 01:09:04 +0200 | [diff] [blame] | 611 | fan_data->cdev = thermal_of_cooling_device_register(np, |
Nishanth Menon | e76ea26 | 2015-04-08 18:23:52 -0500 | [diff] [blame] | 612 | "gpio-fan", |
| 613 | fan_data, |
| 614 | &gpio_fan_cool_ops); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 615 | |
Linus Walleij | f9013c1 | 2017-09-26 01:09:04 +0200 | [diff] [blame] | 616 | dev_info(dev, "GPIO fan initialized\n"); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 617 | |
| 618 | return 0; |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 621 | static int gpio_fan_remove(struct platform_device *pdev) |
Nishanth Menon | b95579c | 2014-12-04 10:58:56 -0600 | [diff] [blame] | 622 | { |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 623 | struct gpio_fan_data *fan_data = platform_get_drvdata(pdev); |
| 624 | |
| 625 | if (!IS_ERR(fan_data->cdev)) |
| 626 | thermal_cooling_device_unregister(fan_data->cdev); |
Nishanth Menon | b95579c | 2014-12-04 10:58:56 -0600 | [diff] [blame] | 627 | |
| 628 | if (fan_data->ctrl) |
| 629 | set_fan_speed(fan_data, 0); |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static void gpio_fan_shutdown(struct platform_device *pdev) |
| 635 | { |
| 636 | gpio_fan_remove(pdev); |
Nishanth Menon | b95579c | 2014-12-04 10:58:56 -0600 | [diff] [blame] | 637 | } |
| 638 | |
Rafael J. Wysocki | 6d20a6c0 | 2012-07-08 00:01:03 +0200 | [diff] [blame] | 639 | #ifdef CONFIG_PM_SLEEP |
| 640 | static int gpio_fan_suspend(struct device *dev) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 641 | { |
Rafael J. Wysocki | 6d20a6c0 | 2012-07-08 00:01:03 +0200 | [diff] [blame] | 642 | struct gpio_fan_data *fan_data = dev_get_drvdata(dev); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 643 | |
| 644 | if (fan_data->ctrl) { |
| 645 | fan_data->resume_speed = fan_data->speed_index; |
| 646 | set_fan_speed(fan_data, 0); |
| 647 | } |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
Rafael J. Wysocki | 6d20a6c0 | 2012-07-08 00:01:03 +0200 | [diff] [blame] | 652 | static int gpio_fan_resume(struct device *dev) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 653 | { |
Rafael J. Wysocki | 6d20a6c0 | 2012-07-08 00:01:03 +0200 | [diff] [blame] | 654 | struct gpio_fan_data *fan_data = dev_get_drvdata(dev); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 655 | |
| 656 | if (fan_data->ctrl) |
| 657 | set_fan_speed(fan_data, fan_data->resume_speed); |
| 658 | |
| 659 | return 0; |
| 660 | } |
Rafael J. Wysocki | 6d20a6c0 | 2012-07-08 00:01:03 +0200 | [diff] [blame] | 661 | |
| 662 | static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume); |
Guenter Roeck | 24f9c53 | 2013-01-10 05:54:40 -0800 | [diff] [blame] | 663 | #define GPIO_FAN_PM (&gpio_fan_pm) |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 664 | #else |
Rafael J. Wysocki | 6d20a6c0 | 2012-07-08 00:01:03 +0200 | [diff] [blame] | 665 | #define GPIO_FAN_PM NULL |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 666 | #endif |
| 667 | |
| 668 | static struct platform_driver gpio_fan_driver = { |
| 669 | .probe = gpio_fan_probe, |
Nishanth Menon | b5cf88e | 2015-01-08 12:05:03 -0600 | [diff] [blame] | 670 | .remove = gpio_fan_remove, |
Nishanth Menon | b95579c | 2014-12-04 10:58:56 -0600 | [diff] [blame] | 671 | .shutdown = gpio_fan_shutdown, |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 672 | .driver = { |
| 673 | .name = "gpio-fan", |
Rafael J. Wysocki | 6d20a6c0 | 2012-07-08 00:01:03 +0200 | [diff] [blame] | 674 | .pm = GPIO_FAN_PM, |
Jamie Lentin | 55fb8b06 | 2012-09-14 17:07:06 +0100 | [diff] [blame] | 675 | .of_match_table = of_match_ptr(of_gpio_fan_match), |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 676 | }, |
| 677 | }; |
| 678 | |
Axel Lin | 25a236a | 2011-11-25 02:31:00 -0500 | [diff] [blame] | 679 | module_platform_driver(gpio_fan_driver); |
Simon Guinot | d6fe136 | 2010-10-22 00:44:19 +0200 | [diff] [blame] | 680 | |
| 681 | MODULE_AUTHOR("Simon Guinot <[email protected]>"); |
| 682 | MODULE_DESCRIPTION("GPIO FAN driver"); |
| 683 | MODULE_LICENSE("GPL"); |
| 684 | MODULE_ALIAS("platform:gpio-fan"); |