gpio-fan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. struct gpio_fan_data {
  34. struct platform_device *pdev;
  35. struct device *hwmon_dev;
  36. struct mutex lock; /* lock GPIOs operations. */
  37. int num_ctrl;
  38. unsigned *ctrl;
  39. int num_speed;
  40. struct gpio_fan_speed *speed;
  41. int speed_index;
  42. #ifdef CONFIG_PM
  43. int resume_speed;
  44. #endif
  45. bool pwm_enable;
  46. struct gpio_fan_alarm *alarm;
  47. struct work_struct alarm_work;
  48. };
  49. /*
  50. * Alarm GPIO.
  51. */
  52. static void fan_alarm_notify(struct work_struct *ws)
  53. {
  54. struct gpio_fan_data *fan_data =
  55. container_of(ws, struct gpio_fan_data, alarm_work);
  56. sysfs_notify(&fan_data->pdev->dev.kobj, NULL, "fan1_alarm");
  57. kobject_uevent(&fan_data->pdev->dev.kobj, KOBJ_CHANGE);
  58. }
  59. static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
  60. {
  61. struct gpio_fan_data *fan_data = dev_id;
  62. schedule_work(&fan_data->alarm_work);
  63. return IRQ_NONE;
  64. }
  65. static ssize_t show_fan_alarm(struct device *dev,
  66. struct device_attribute *attr, char *buf)
  67. {
  68. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  69. struct gpio_fan_alarm *alarm = fan_data->alarm;
  70. int value = gpio_get_value(alarm->gpio);
  71. if (alarm->active_low)
  72. value = !value;
  73. return sprintf(buf, "%d\n", value);
  74. }
  75. static DEVICE_ATTR(fan1_alarm, S_IRUGO, show_fan_alarm, NULL);
  76. static int fan_alarm_init(struct gpio_fan_data *fan_data,
  77. struct gpio_fan_alarm *alarm)
  78. {
  79. int err;
  80. int alarm_irq;
  81. struct platform_device *pdev = fan_data->pdev;
  82. fan_data->alarm = alarm;
  83. err = gpio_request(alarm->gpio, "GPIO fan alarm");
  84. if (err)
  85. return err;
  86. err = gpio_direction_input(alarm->gpio);
  87. if (err)
  88. goto err_free_gpio;
  89. err = device_create_file(&pdev->dev, &dev_attr_fan1_alarm);
  90. if (err)
  91. goto err_free_gpio;
  92. /*
  93. * If the alarm GPIO don't support interrupts, just leave
  94. * without initializing the fail notification support.
  95. */
  96. alarm_irq = gpio_to_irq(alarm->gpio);
  97. if (alarm_irq < 0)
  98. return 0;
  99. INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
  100. irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
  101. err = request_irq(alarm_irq, fan_alarm_irq_handler, IRQF_SHARED,
  102. "GPIO fan alarm", fan_data);
  103. if (err)
  104. goto err_free_sysfs;
  105. return 0;
  106. err_free_sysfs:
  107. device_remove_file(&pdev->dev, &dev_attr_fan1_alarm);
  108. err_free_gpio:
  109. gpio_free(alarm->gpio);
  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. int alarm_irq = gpio_to_irq(fan_data->alarm->gpio);
  116. if (alarm_irq >= 0)
  117. free_irq(alarm_irq, fan_data);
  118. device_remove_file(&pdev->dev, &dev_attr_fan1_alarm);
  119. gpio_free(fan_data->alarm->gpio);
  120. }
  121. /*
  122. * Control GPIOs.
  123. */
  124. /* Must be called with fan_data->lock held, except during initialization. */
  125. static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
  126. {
  127. int i;
  128. for (i = 0; i < fan_data->num_ctrl; i++)
  129. gpio_set_value(fan_data->ctrl[i], (ctrl_val >> i) & 1);
  130. }
  131. static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
  132. {
  133. int i;
  134. int ctrl_val = 0;
  135. for (i = 0; i < fan_data->num_ctrl; i++) {
  136. int value;
  137. value = gpio_get_value(fan_data->ctrl[i]);
  138. ctrl_val |= (value << i);
  139. }
  140. return ctrl_val;
  141. }
  142. /* Must be called with fan_data->lock held, except during initialization. */
  143. static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
  144. {
  145. if (fan_data->speed_index == speed_index)
  146. return;
  147. __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
  148. fan_data->speed_index = speed_index;
  149. }
  150. static int get_fan_speed_index(struct gpio_fan_data *fan_data)
  151. {
  152. int ctrl_val = __get_fan_ctrl(fan_data);
  153. int i;
  154. for (i = 0; i < fan_data->num_speed; i++)
  155. if (fan_data->speed[i].ctrl_val == ctrl_val)
  156. return i;
  157. dev_warn(&fan_data->pdev->dev,
  158. "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
  159. return -EINVAL;
  160. }
  161. static int rpm_to_speed_index(struct gpio_fan_data *fan_data, int rpm)
  162. {
  163. struct gpio_fan_speed *speed = fan_data->speed;
  164. int i;
  165. for (i = 0; i < fan_data->num_speed; i++)
  166. if (speed[i].rpm >= rpm)
  167. return i;
  168. return fan_data->num_speed - 1;
  169. }
  170. static ssize_t show_pwm(struct device *dev,
  171. struct device_attribute *attr, char *buf)
  172. {
  173. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  174. u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
  175. return sprintf(buf, "%d\n", pwm);
  176. }
  177. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  178. const char *buf, size_t count)
  179. {
  180. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  181. unsigned long pwm;
  182. int speed_index;
  183. int ret = count;
  184. if (kstrtoul(buf, 10, &pwm) || pwm > 255)
  185. return -EINVAL;
  186. mutex_lock(&fan_data->lock);
  187. if (!fan_data->pwm_enable) {
  188. ret = -EPERM;
  189. goto exit_unlock;
  190. }
  191. speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
  192. set_fan_speed(fan_data, speed_index);
  193. exit_unlock:
  194. mutex_unlock(&fan_data->lock);
  195. return ret;
  196. }
  197. static ssize_t show_pwm_enable(struct device *dev,
  198. struct device_attribute *attr, char *buf)
  199. {
  200. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  201. return sprintf(buf, "%d\n", fan_data->pwm_enable);
  202. }
  203. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
  204. const char *buf, size_t count)
  205. {
  206. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  207. unsigned long val;
  208. if (kstrtoul(buf, 10, &val) || val > 1)
  209. return -EINVAL;
  210. if (fan_data->pwm_enable == val)
  211. return count;
  212. mutex_lock(&fan_data->lock);
  213. fan_data->pwm_enable = val;
  214. /* Disable manual control mode: set fan at full speed. */
  215. if (val == 0)
  216. set_fan_speed(fan_data, fan_data->num_speed - 1);
  217. mutex_unlock(&fan_data->lock);
  218. return count;
  219. }
  220. static ssize_t show_pwm_mode(struct device *dev,
  221. struct device_attribute *attr, char *buf)
  222. {
  223. return sprintf(buf, "0\n");
  224. }
  225. static ssize_t show_rpm_min(struct device *dev,
  226. struct device_attribute *attr, char *buf)
  227. {
  228. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  229. return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
  230. }
  231. static ssize_t show_rpm_max(struct device *dev,
  232. struct device_attribute *attr, char *buf)
  233. {
  234. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  235. return sprintf(buf, "%d\n",
  236. fan_data->speed[fan_data->num_speed - 1].rpm);
  237. }
  238. static ssize_t show_rpm(struct device *dev,
  239. struct device_attribute *attr, char *buf)
  240. {
  241. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  242. return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
  243. }
  244. static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
  245. const char *buf, size_t count)
  246. {
  247. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  248. unsigned long rpm;
  249. int ret = count;
  250. if (kstrtoul(buf, 10, &rpm))
  251. return -EINVAL;
  252. mutex_lock(&fan_data->lock);
  253. if (!fan_data->pwm_enable) {
  254. ret = -EPERM;
  255. goto exit_unlock;
  256. }
  257. set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
  258. exit_unlock:
  259. mutex_unlock(&fan_data->lock);
  260. return ret;
  261. }
  262. static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
  263. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  264. show_pwm_enable, set_pwm_enable);
  265. static DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL);
  266. static DEVICE_ATTR(fan1_min, S_IRUGO, show_rpm_min, NULL);
  267. static DEVICE_ATTR(fan1_max, S_IRUGO, show_rpm_max, NULL);
  268. static DEVICE_ATTR(fan1_input, S_IRUGO, show_rpm, NULL);
  269. static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_rpm, set_rpm);
  270. static struct attribute *gpio_fan_ctrl_attributes[] = {
  271. &dev_attr_pwm1.attr,
  272. &dev_attr_pwm1_enable.attr,
  273. &dev_attr_pwm1_mode.attr,
  274. &dev_attr_fan1_input.attr,
  275. &dev_attr_fan1_target.attr,
  276. &dev_attr_fan1_min.attr,
  277. &dev_attr_fan1_max.attr,
  278. NULL
  279. };
  280. static const struct attribute_group gpio_fan_ctrl_group = {
  281. .attrs = gpio_fan_ctrl_attributes,
  282. };
  283. static int fan_ctrl_init(struct gpio_fan_data *fan_data,
  284. struct gpio_fan_platform_data *pdata)
  285. {
  286. struct platform_device *pdev = fan_data->pdev;
  287. int num_ctrl = pdata->num_ctrl;
  288. unsigned *ctrl = pdata->ctrl;
  289. int i, err;
  290. for (i = 0; i < num_ctrl; i++) {
  291. err = gpio_request(ctrl[i], "GPIO fan control");
  292. if (err)
  293. goto err_free_gpio;
  294. err = gpio_direction_output(ctrl[i], gpio_get_value(ctrl[i]));
  295. if (err) {
  296. gpio_free(ctrl[i]);
  297. goto err_free_gpio;
  298. }
  299. }
  300. fan_data->num_ctrl = num_ctrl;
  301. fan_data->ctrl = ctrl;
  302. fan_data->num_speed = pdata->num_speed;
  303. fan_data->speed = pdata->speed;
  304. fan_data->pwm_enable = true; /* Enable manual fan speed control. */
  305. fan_data->speed_index = get_fan_speed_index(fan_data);
  306. if (fan_data->speed_index < 0) {
  307. err = -ENODEV;
  308. goto err_free_gpio;
  309. }
  310. err = sysfs_create_group(&pdev->dev.kobj, &gpio_fan_ctrl_group);
  311. if (err)
  312. goto err_free_gpio;
  313. return 0;
  314. err_free_gpio:
  315. for (i = i - 1; i >= 0; i--)
  316. gpio_free(ctrl[i]);
  317. return err;
  318. }
  319. static void fan_ctrl_free(struct gpio_fan_data *fan_data)
  320. {
  321. struct platform_device *pdev = fan_data->pdev;
  322. int i;
  323. sysfs_remove_group(&pdev->dev.kobj, &gpio_fan_ctrl_group);
  324. for (i = 0; i < fan_data->num_ctrl; i++)
  325. gpio_free(fan_data->ctrl[i]);
  326. }
  327. /*
  328. * Platform driver.
  329. */
  330. static ssize_t show_name(struct device *dev,
  331. struct device_attribute *attr, char *buf)
  332. {
  333. return sprintf(buf, "gpio-fan\n");
  334. }
  335. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  336. static int __devinit gpio_fan_probe(struct platform_device *pdev)
  337. {
  338. int err;
  339. struct gpio_fan_data *fan_data;
  340. struct gpio_fan_platform_data *pdata = pdev->dev.platform_data;
  341. if (!pdata)
  342. return -EINVAL;
  343. fan_data = kzalloc(sizeof(struct gpio_fan_data), GFP_KERNEL);
  344. if (!fan_data)
  345. return -ENOMEM;
  346. fan_data->pdev = pdev;
  347. platform_set_drvdata(pdev, fan_data);
  348. mutex_init(&fan_data->lock);
  349. /* Configure alarm GPIO if available. */
  350. if (pdata->alarm) {
  351. err = fan_alarm_init(fan_data, pdata->alarm);
  352. if (err)
  353. goto err_free_data;
  354. }
  355. /* Configure control GPIOs if available. */
  356. if (pdata->ctrl && pdata->num_ctrl > 0) {
  357. if (!pdata->speed || pdata->num_speed <= 1) {
  358. err = -EINVAL;
  359. goto err_free_alarm;
  360. }
  361. err = fan_ctrl_init(fan_data, pdata);
  362. if (err)
  363. goto err_free_alarm;
  364. }
  365. err = device_create_file(&pdev->dev, &dev_attr_name);
  366. if (err)
  367. goto err_free_ctrl;
  368. /* Make this driver part of hwmon class. */
  369. fan_data->hwmon_dev = hwmon_device_register(&pdev->dev);
  370. if (IS_ERR(fan_data->hwmon_dev)) {
  371. err = PTR_ERR(fan_data->hwmon_dev);
  372. goto err_remove_name;
  373. }
  374. dev_info(&pdev->dev, "GPIO fan initialized\n");
  375. return 0;
  376. err_remove_name:
  377. device_remove_file(&pdev->dev, &dev_attr_name);
  378. err_free_ctrl:
  379. if (fan_data->ctrl)
  380. fan_ctrl_free(fan_data);
  381. err_free_alarm:
  382. if (fan_data->alarm)
  383. fan_alarm_free(fan_data);
  384. err_free_data:
  385. platform_set_drvdata(pdev, NULL);
  386. kfree(fan_data);
  387. return err;
  388. }
  389. static int __devexit gpio_fan_remove(struct platform_device *pdev)
  390. {
  391. struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
  392. hwmon_device_unregister(fan_data->hwmon_dev);
  393. device_remove_file(&pdev->dev, &dev_attr_name);
  394. if (fan_data->alarm)
  395. fan_alarm_free(fan_data);
  396. if (fan_data->ctrl)
  397. fan_ctrl_free(fan_data);
  398. kfree(fan_data);
  399. return 0;
  400. }
  401. #ifdef CONFIG_PM
  402. static int gpio_fan_suspend(struct platform_device *pdev, pm_message_t state)
  403. {
  404. struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
  405. if (fan_data->ctrl) {
  406. fan_data->resume_speed = fan_data->speed_index;
  407. set_fan_speed(fan_data, 0);
  408. }
  409. return 0;
  410. }
  411. static int gpio_fan_resume(struct platform_device *pdev)
  412. {
  413. struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
  414. if (fan_data->ctrl)
  415. set_fan_speed(fan_data, fan_data->resume_speed);
  416. return 0;
  417. }
  418. #else
  419. #define gpio_fan_suspend NULL
  420. #define gpio_fan_resume NULL
  421. #endif
  422. static struct platform_driver gpio_fan_driver = {
  423. .probe = gpio_fan_probe,
  424. .remove = __devexit_p(gpio_fan_remove),
  425. .suspend = gpio_fan_suspend,
  426. .resume = gpio_fan_resume,
  427. .driver = {
  428. .name = "gpio-fan",
  429. },
  430. };
  431. module_platform_driver(gpio_fan_driver);
  432. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  433. MODULE_DESCRIPTION("GPIO FAN driver");
  434. MODULE_LICENSE("GPL");
  435. MODULE_ALIAS("platform:gpio-fan");