gpio-fan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  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. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/err.h>
  29. #include <linux/mutex.h>
  30. #include <linux/hwmon.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/of.h>
  33. #include <linux/of_platform.h>
  34. #include <linux/thermal.h>
  35. struct gpio_fan_speed {
  36. int rpm;
  37. int ctrl_val;
  38. };
  39. struct gpio_fan_data {
  40. struct device *dev;
  41. struct device *hwmon_dev;
  42. /* Cooling device if any */
  43. struct thermal_cooling_device *cdev;
  44. struct mutex lock; /* lock GPIOs operations. */
  45. int num_gpios;
  46. struct gpio_desc **gpios;
  47. int num_speed;
  48. struct gpio_fan_speed *speed;
  49. int speed_index;
  50. #ifdef CONFIG_PM_SLEEP
  51. int resume_speed;
  52. #endif
  53. bool pwm_enable;
  54. struct gpio_desc *alarm_gpio;
  55. struct work_struct alarm_work;
  56. };
  57. /*
  58. * Alarm GPIO.
  59. */
  60. static void fan_alarm_notify(struct work_struct *ws)
  61. {
  62. struct gpio_fan_data *fan_data =
  63. container_of(ws, struct gpio_fan_data, alarm_work);
  64. sysfs_notify(&fan_data->dev->kobj, NULL, "fan1_alarm");
  65. kobject_uevent(&fan_data->dev->kobj, KOBJ_CHANGE);
  66. }
  67. static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
  68. {
  69. struct gpio_fan_data *fan_data = dev_id;
  70. schedule_work(&fan_data->alarm_work);
  71. return IRQ_NONE;
  72. }
  73. static ssize_t fan1_alarm_show(struct device *dev,
  74. struct device_attribute *attr, char *buf)
  75. {
  76. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  77. return sprintf(buf, "%d\n",
  78. gpiod_get_value_cansleep(fan_data->alarm_gpio));
  79. }
  80. static DEVICE_ATTR_RO(fan1_alarm);
  81. static int fan_alarm_init(struct gpio_fan_data *fan_data)
  82. {
  83. int alarm_irq;
  84. struct device *dev = fan_data->dev;
  85. /*
  86. * If the alarm GPIO don't support interrupts, just leave
  87. * without initializing the fail notification support.
  88. */
  89. alarm_irq = gpiod_to_irq(fan_data->alarm_gpio);
  90. if (alarm_irq <= 0)
  91. return 0;
  92. INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
  93. irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
  94. return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
  95. IRQF_SHARED, "GPIO fan alarm", fan_data);
  96. }
  97. /*
  98. * Control GPIOs.
  99. */
  100. /* Must be called with fan_data->lock held, except during initialization. */
  101. static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
  102. {
  103. int i;
  104. for (i = 0; i < fan_data->num_gpios; i++)
  105. gpiod_set_value_cansleep(fan_data->gpios[i],
  106. (ctrl_val >> i) & 1);
  107. }
  108. static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
  109. {
  110. int i;
  111. int ctrl_val = 0;
  112. for (i = 0; i < fan_data->num_gpios; i++) {
  113. int value;
  114. value = gpiod_get_value_cansleep(fan_data->gpios[i]);
  115. ctrl_val |= (value << i);
  116. }
  117. return ctrl_val;
  118. }
  119. /* Must be called with fan_data->lock held, except during initialization. */
  120. static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
  121. {
  122. if (fan_data->speed_index == speed_index)
  123. return;
  124. __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
  125. fan_data->speed_index = speed_index;
  126. }
  127. static int get_fan_speed_index(struct gpio_fan_data *fan_data)
  128. {
  129. int ctrl_val = __get_fan_ctrl(fan_data);
  130. int i;
  131. for (i = 0; i < fan_data->num_speed; i++)
  132. if (fan_data->speed[i].ctrl_val == ctrl_val)
  133. return i;
  134. dev_warn(fan_data->dev,
  135. "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
  136. return -ENODEV;
  137. }
  138. static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
  139. {
  140. struct gpio_fan_speed *speed = fan_data->speed;
  141. int i;
  142. for (i = 0; i < fan_data->num_speed; i++)
  143. if (speed[i].rpm >= rpm)
  144. return i;
  145. return fan_data->num_speed - 1;
  146. }
  147. static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
  148. char *buf)
  149. {
  150. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  151. u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
  152. return sprintf(buf, "%d\n", pwm);
  153. }
  154. static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
  155. const char *buf, size_t count)
  156. {
  157. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  158. unsigned long pwm;
  159. int speed_index;
  160. int ret = count;
  161. if (kstrtoul(buf, 10, &pwm) || pwm > 255)
  162. return -EINVAL;
  163. mutex_lock(&fan_data->lock);
  164. if (!fan_data->pwm_enable) {
  165. ret = -EPERM;
  166. goto exit_unlock;
  167. }
  168. speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
  169. set_fan_speed(fan_data, speed_index);
  170. exit_unlock:
  171. mutex_unlock(&fan_data->lock);
  172. return ret;
  173. }
  174. static ssize_t pwm1_enable_show(struct device *dev,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  178. return sprintf(buf, "%d\n", fan_data->pwm_enable);
  179. }
  180. static ssize_t pwm1_enable_store(struct device *dev,
  181. struct device_attribute *attr,
  182. const char *buf, size_t count)
  183. {
  184. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  185. unsigned long val;
  186. if (kstrtoul(buf, 10, &val) || val > 1)
  187. return -EINVAL;
  188. if (fan_data->pwm_enable == val)
  189. return count;
  190. mutex_lock(&fan_data->lock);
  191. fan_data->pwm_enable = val;
  192. /* Disable manual control mode: set fan at full speed. */
  193. if (val == 0)
  194. set_fan_speed(fan_data, fan_data->num_speed - 1);
  195. mutex_unlock(&fan_data->lock);
  196. return count;
  197. }
  198. static ssize_t pwm1_mode_show(struct device *dev,
  199. struct device_attribute *attr, char *buf)
  200. {
  201. return sprintf(buf, "0\n");
  202. }
  203. static ssize_t fan1_min_show(struct device *dev,
  204. struct device_attribute *attr, char *buf)
  205. {
  206. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  207. return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
  208. }
  209. static ssize_t fan1_max_show(struct device *dev,
  210. struct device_attribute *attr, char *buf)
  211. {
  212. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  213. return sprintf(buf, "%d\n",
  214. fan_data->speed[fan_data->num_speed - 1].rpm);
  215. }
  216. static ssize_t fan1_input_show(struct device *dev,
  217. struct device_attribute *attr, char *buf)
  218. {
  219. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  220. return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
  221. }
  222. static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
  223. const char *buf, size_t count)
  224. {
  225. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  226. unsigned long rpm;
  227. int ret = count;
  228. if (kstrtoul(buf, 10, &rpm))
  229. return -EINVAL;
  230. mutex_lock(&fan_data->lock);
  231. if (!fan_data->pwm_enable) {
  232. ret = -EPERM;
  233. goto exit_unlock;
  234. }
  235. set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
  236. exit_unlock:
  237. mutex_unlock(&fan_data->lock);
  238. return ret;
  239. }
  240. static DEVICE_ATTR_RW(pwm1);
  241. static DEVICE_ATTR_RW(pwm1_enable);
  242. static DEVICE_ATTR_RO(pwm1_mode);
  243. static DEVICE_ATTR_RO(fan1_min);
  244. static DEVICE_ATTR_RO(fan1_max);
  245. static DEVICE_ATTR_RO(fan1_input);
  246. static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, fan1_input_show, set_rpm);
  247. static umode_t gpio_fan_is_visible(struct kobject *kobj,
  248. struct attribute *attr, int index)
  249. {
  250. struct device *dev = container_of(kobj, struct device, kobj);
  251. struct gpio_fan_data *data = dev_get_drvdata(dev);
  252. if (index == 0 && !data->alarm_gpio)
  253. return 0;
  254. if (index > 0 && !data->gpios)
  255. return 0;
  256. return attr->mode;
  257. }
  258. static struct attribute *gpio_fan_attributes[] = {
  259. &dev_attr_fan1_alarm.attr, /* 0 */
  260. &dev_attr_pwm1.attr, /* 1 */
  261. &dev_attr_pwm1_enable.attr,
  262. &dev_attr_pwm1_mode.attr,
  263. &dev_attr_fan1_input.attr,
  264. &dev_attr_fan1_target.attr,
  265. &dev_attr_fan1_min.attr,
  266. &dev_attr_fan1_max.attr,
  267. NULL
  268. };
  269. static const struct attribute_group gpio_fan_group = {
  270. .attrs = gpio_fan_attributes,
  271. .is_visible = gpio_fan_is_visible,
  272. };
  273. static const struct attribute_group *gpio_fan_groups[] = {
  274. &gpio_fan_group,
  275. NULL
  276. };
  277. static int fan_ctrl_init(struct gpio_fan_data *fan_data)
  278. {
  279. int num_gpios = fan_data->num_gpios;
  280. struct gpio_desc **gpios = fan_data->gpios;
  281. int i, err;
  282. for (i = 0; i < num_gpios; i++) {
  283. /*
  284. * The GPIO descriptors were retrieved with GPIOD_ASIS so here
  285. * we set the GPIO into output mode, carefully preserving the
  286. * current value by setting it to whatever it is already set
  287. * (no surprise changes in default fan speed).
  288. */
  289. err = gpiod_direction_output(gpios[i],
  290. gpiod_get_value_cansleep(gpios[i]));
  291. if (err)
  292. return err;
  293. }
  294. fan_data->pwm_enable = true; /* Enable manual fan speed control. */
  295. fan_data->speed_index = get_fan_speed_index(fan_data);
  296. if (fan_data->speed_index < 0)
  297. return fan_data->speed_index;
  298. return 0;
  299. }
  300. static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
  301. unsigned long *state)
  302. {
  303. struct gpio_fan_data *fan_data = cdev->devdata;
  304. if (!fan_data)
  305. return -EINVAL;
  306. *state = fan_data->num_speed - 1;
  307. return 0;
  308. }
  309. static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
  310. unsigned long *state)
  311. {
  312. struct gpio_fan_data *fan_data = cdev->devdata;
  313. if (!fan_data)
  314. return -EINVAL;
  315. *state = fan_data->speed_index;
  316. return 0;
  317. }
  318. static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
  319. unsigned long state)
  320. {
  321. struct gpio_fan_data *fan_data = cdev->devdata;
  322. if (!fan_data)
  323. return -EINVAL;
  324. set_fan_speed(fan_data, state);
  325. return 0;
  326. }
  327. static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
  328. .get_max_state = gpio_fan_get_max_state,
  329. .get_cur_state = gpio_fan_get_cur_state,
  330. .set_cur_state = gpio_fan_set_cur_state,
  331. };
  332. /*
  333. * Translate OpenFirmware node properties into platform_data
  334. */
  335. static int gpio_fan_get_of_data(struct gpio_fan_data *fan_data)
  336. {
  337. struct gpio_fan_speed *speed;
  338. struct device *dev = fan_data->dev;
  339. struct device_node *np = dev->of_node;
  340. struct gpio_desc **gpios;
  341. unsigned i;
  342. u32 u;
  343. struct property *prop;
  344. const __be32 *p;
  345. /* Alarm GPIO if one exists */
  346. fan_data->alarm_gpio = devm_gpiod_get_optional(dev, "alarm", GPIOD_IN);
  347. if (IS_ERR(fan_data->alarm_gpio))
  348. return PTR_ERR(fan_data->alarm_gpio);
  349. /* Fill GPIO pin array */
  350. fan_data->num_gpios = gpiod_count(dev, NULL);
  351. if (fan_data->num_gpios <= 0) {
  352. if (fan_data->alarm_gpio)
  353. return 0;
  354. dev_err(dev, "DT properties empty / missing");
  355. return -ENODEV;
  356. }
  357. gpios = devm_kcalloc(dev,
  358. fan_data->num_gpios, sizeof(struct gpio_desc *),
  359. GFP_KERNEL);
  360. if (!gpios)
  361. return -ENOMEM;
  362. for (i = 0; i < fan_data->num_gpios; i++) {
  363. gpios[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
  364. if (IS_ERR(gpios[i]))
  365. return PTR_ERR(gpios[i]);
  366. }
  367. fan_data->gpios = gpios;
  368. /* Get number of RPM/ctrl_val pairs in speed map */
  369. prop = of_find_property(np, "gpio-fan,speed-map", &i);
  370. if (!prop) {
  371. dev_err(dev, "gpio-fan,speed-map DT property missing");
  372. return -ENODEV;
  373. }
  374. i = i / sizeof(u32);
  375. if (i == 0 || i & 1) {
  376. dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
  377. return -ENODEV;
  378. }
  379. fan_data->num_speed = i / 2;
  380. /*
  381. * Populate speed map
  382. * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
  383. * this needs splitting into pairs to create gpio_fan_speed structs
  384. */
  385. speed = devm_kcalloc(dev,
  386. fan_data->num_speed, sizeof(struct gpio_fan_speed),
  387. GFP_KERNEL);
  388. if (!speed)
  389. return -ENOMEM;
  390. p = NULL;
  391. for (i = 0; i < fan_data->num_speed; i++) {
  392. p = of_prop_next_u32(prop, p, &u);
  393. if (!p)
  394. return -ENODEV;
  395. speed[i].rpm = u;
  396. p = of_prop_next_u32(prop, p, &u);
  397. if (!p)
  398. return -ENODEV;
  399. speed[i].ctrl_val = u;
  400. }
  401. fan_data->speed = speed;
  402. return 0;
  403. }
  404. static const struct of_device_id of_gpio_fan_match[] = {
  405. { .compatible = "gpio-fan", },
  406. {},
  407. };
  408. MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
  409. static int gpio_fan_probe(struct platform_device *pdev)
  410. {
  411. int err;
  412. struct gpio_fan_data *fan_data;
  413. struct device *dev = &pdev->dev;
  414. struct device_node *np = dev->of_node;
  415. fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
  416. GFP_KERNEL);
  417. if (!fan_data)
  418. return -ENOMEM;
  419. fan_data->dev = dev;
  420. err = gpio_fan_get_of_data(fan_data);
  421. if (err)
  422. return err;
  423. platform_set_drvdata(pdev, fan_data);
  424. mutex_init(&fan_data->lock);
  425. /* Configure alarm GPIO if available. */
  426. if (fan_data->alarm_gpio) {
  427. err = fan_alarm_init(fan_data);
  428. if (err)
  429. return err;
  430. }
  431. /* Configure control GPIOs if available. */
  432. if (fan_data->gpios && fan_data->num_gpios > 0) {
  433. if (!fan_data->speed || fan_data->num_speed <= 1)
  434. return -EINVAL;
  435. err = fan_ctrl_init(fan_data);
  436. if (err)
  437. return err;
  438. }
  439. /* Make this driver part of hwmon class. */
  440. fan_data->hwmon_dev =
  441. devm_hwmon_device_register_with_groups(dev,
  442. "gpio_fan", fan_data,
  443. gpio_fan_groups);
  444. if (IS_ERR(fan_data->hwmon_dev))
  445. return PTR_ERR(fan_data->hwmon_dev);
  446. /* Optional cooling device register for Device tree platforms */
  447. fan_data->cdev = thermal_of_cooling_device_register(np,
  448. "gpio-fan",
  449. fan_data,
  450. &gpio_fan_cool_ops);
  451. dev_info(dev, "GPIO fan initialized\n");
  452. return 0;
  453. }
  454. static int gpio_fan_remove(struct platform_device *pdev)
  455. {
  456. struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
  457. if (!IS_ERR(fan_data->cdev))
  458. thermal_cooling_device_unregister(fan_data->cdev);
  459. if (fan_data->gpios)
  460. set_fan_speed(fan_data, 0);
  461. return 0;
  462. }
  463. static void gpio_fan_shutdown(struct platform_device *pdev)
  464. {
  465. gpio_fan_remove(pdev);
  466. }
  467. #ifdef CONFIG_PM_SLEEP
  468. static int gpio_fan_suspend(struct device *dev)
  469. {
  470. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  471. if (fan_data->gpios) {
  472. fan_data->resume_speed = fan_data->speed_index;
  473. set_fan_speed(fan_data, 0);
  474. }
  475. return 0;
  476. }
  477. static int gpio_fan_resume(struct device *dev)
  478. {
  479. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  480. if (fan_data->gpios)
  481. set_fan_speed(fan_data, fan_data->resume_speed);
  482. return 0;
  483. }
  484. static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
  485. #define GPIO_FAN_PM (&gpio_fan_pm)
  486. #else
  487. #define GPIO_FAN_PM NULL
  488. #endif
  489. static struct platform_driver gpio_fan_driver = {
  490. .probe = gpio_fan_probe,
  491. .remove = gpio_fan_remove,
  492. .shutdown = gpio_fan_shutdown,
  493. .driver = {
  494. .name = "gpio-fan",
  495. .pm = GPIO_FAN_PM,
  496. .of_match_table = of_match_ptr(of_gpio_fan_match),
  497. },
  498. };
  499. module_platform_driver(gpio_fan_driver);
  500. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  501. MODULE_DESCRIPTION("GPIO FAN driver");
  502. MODULE_LICENSE("GPL");
  503. MODULE_ALIAS("platform:gpio-fan");