gpio-fan.c 15 KB

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