gpio-fan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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_SLEEP
  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 = devm_gpio_request(&pdev->dev, alarm->gpio, "GPIO fan alarm");
  84. if (err)
  85. return err;
  86. err = gpio_direction_input(alarm->gpio);
  87. if (err)
  88. return err;
  89. err = device_create_file(&pdev->dev, &dev_attr_fan1_alarm);
  90. if (err)
  91. return err;
  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 = devm_request_irq(&pdev->dev, alarm_irq, fan_alarm_irq_handler,
  102. IRQF_SHARED, "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. return err;
  109. }
  110. static void fan_alarm_free(struct gpio_fan_data *fan_data)
  111. {
  112. struct platform_device *pdev = fan_data->pdev;
  113. device_remove_file(&pdev->dev, &dev_attr_fan1_alarm);
  114. }
  115. /*
  116. * Control GPIOs.
  117. */
  118. /* Must be called with fan_data->lock held, except during initialization. */
  119. static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
  120. {
  121. int i;
  122. for (i = 0; i < fan_data->num_ctrl; i++)
  123. gpio_set_value(fan_data->ctrl[i], (ctrl_val >> i) & 1);
  124. }
  125. static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
  126. {
  127. int i;
  128. int ctrl_val = 0;
  129. for (i = 0; i < fan_data->num_ctrl; i++) {
  130. int value;
  131. value = gpio_get_value(fan_data->ctrl[i]);
  132. ctrl_val |= (value << i);
  133. }
  134. return ctrl_val;
  135. }
  136. /* Must be called with fan_data->lock held, except during initialization. */
  137. static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
  138. {
  139. if (fan_data->speed_index == speed_index)
  140. return;
  141. __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
  142. fan_data->speed_index = speed_index;
  143. }
  144. static int get_fan_speed_index(struct gpio_fan_data *fan_data)
  145. {
  146. int ctrl_val = __get_fan_ctrl(fan_data);
  147. int i;
  148. for (i = 0; i < fan_data->num_speed; i++)
  149. if (fan_data->speed[i].ctrl_val == ctrl_val)
  150. return i;
  151. dev_warn(&fan_data->pdev->dev,
  152. "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
  153. return -EINVAL;
  154. }
  155. static int rpm_to_speed_index(struct gpio_fan_data *fan_data, int rpm)
  156. {
  157. struct gpio_fan_speed *speed = fan_data->speed;
  158. int i;
  159. for (i = 0; i < fan_data->num_speed; i++)
  160. if (speed[i].rpm >= rpm)
  161. return i;
  162. return fan_data->num_speed - 1;
  163. }
  164. static ssize_t show_pwm(struct device *dev,
  165. struct device_attribute *attr, char *buf)
  166. {
  167. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  168. u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
  169. return sprintf(buf, "%d\n", pwm);
  170. }
  171. static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
  172. const char *buf, size_t count)
  173. {
  174. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  175. unsigned long pwm;
  176. int speed_index;
  177. int ret = count;
  178. if (kstrtoul(buf, 10, &pwm) || pwm > 255)
  179. return -EINVAL;
  180. mutex_lock(&fan_data->lock);
  181. if (!fan_data->pwm_enable) {
  182. ret = -EPERM;
  183. goto exit_unlock;
  184. }
  185. speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
  186. set_fan_speed(fan_data, speed_index);
  187. exit_unlock:
  188. mutex_unlock(&fan_data->lock);
  189. return ret;
  190. }
  191. static ssize_t show_pwm_enable(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  195. return sprintf(buf, "%d\n", fan_data->pwm_enable);
  196. }
  197. static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
  198. const char *buf, size_t count)
  199. {
  200. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  201. unsigned long val;
  202. if (kstrtoul(buf, 10, &val) || val > 1)
  203. return -EINVAL;
  204. if (fan_data->pwm_enable == val)
  205. return count;
  206. mutex_lock(&fan_data->lock);
  207. fan_data->pwm_enable = val;
  208. /* Disable manual control mode: set fan at full speed. */
  209. if (val == 0)
  210. set_fan_speed(fan_data, fan_data->num_speed - 1);
  211. mutex_unlock(&fan_data->lock);
  212. return count;
  213. }
  214. static ssize_t show_pwm_mode(struct device *dev,
  215. struct device_attribute *attr, char *buf)
  216. {
  217. return sprintf(buf, "0\n");
  218. }
  219. static ssize_t show_rpm_min(struct device *dev,
  220. struct device_attribute *attr, char *buf)
  221. {
  222. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  223. return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
  224. }
  225. static ssize_t show_rpm_max(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",
  230. fan_data->speed[fan_data->num_speed - 1].rpm);
  231. }
  232. static ssize_t show_rpm(struct device *dev,
  233. struct device_attribute *attr, char *buf)
  234. {
  235. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  236. return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
  237. }
  238. static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
  239. const char *buf, size_t count)
  240. {
  241. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  242. unsigned long rpm;
  243. int ret = count;
  244. if (kstrtoul(buf, 10, &rpm))
  245. return -EINVAL;
  246. mutex_lock(&fan_data->lock);
  247. if (!fan_data->pwm_enable) {
  248. ret = -EPERM;
  249. goto exit_unlock;
  250. }
  251. set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
  252. exit_unlock:
  253. mutex_unlock(&fan_data->lock);
  254. return ret;
  255. }
  256. static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
  257. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  258. show_pwm_enable, set_pwm_enable);
  259. static DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL);
  260. static DEVICE_ATTR(fan1_min, S_IRUGO, show_rpm_min, NULL);
  261. static DEVICE_ATTR(fan1_max, S_IRUGO, show_rpm_max, NULL);
  262. static DEVICE_ATTR(fan1_input, S_IRUGO, show_rpm, NULL);
  263. static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_rpm, set_rpm);
  264. static struct attribute *gpio_fan_ctrl_attributes[] = {
  265. &dev_attr_pwm1.attr,
  266. &dev_attr_pwm1_enable.attr,
  267. &dev_attr_pwm1_mode.attr,
  268. &dev_attr_fan1_input.attr,
  269. &dev_attr_fan1_target.attr,
  270. &dev_attr_fan1_min.attr,
  271. &dev_attr_fan1_max.attr,
  272. NULL
  273. };
  274. static const struct attribute_group gpio_fan_ctrl_group = {
  275. .attrs = gpio_fan_ctrl_attributes,
  276. };
  277. static int fan_ctrl_init(struct gpio_fan_data *fan_data,
  278. struct gpio_fan_platform_data *pdata)
  279. {
  280. struct platform_device *pdev = fan_data->pdev;
  281. int num_ctrl = pdata->num_ctrl;
  282. unsigned *ctrl = pdata->ctrl;
  283. int i, err;
  284. for (i = 0; i < num_ctrl; i++) {
  285. err = devm_gpio_request(&pdev->dev, ctrl[i],
  286. "GPIO fan control");
  287. if (err)
  288. return err;
  289. err = gpio_direction_output(ctrl[i], gpio_get_value(ctrl[i]));
  290. if (err)
  291. return err;
  292. }
  293. fan_data->num_ctrl = num_ctrl;
  294. fan_data->ctrl = ctrl;
  295. fan_data->num_speed = pdata->num_speed;
  296. fan_data->speed = pdata->speed;
  297. fan_data->pwm_enable = true; /* Enable manual fan speed control. */
  298. fan_data->speed_index = get_fan_speed_index(fan_data);
  299. if (fan_data->speed_index < 0)
  300. return -ENODEV;
  301. err = sysfs_create_group(&pdev->dev.kobj, &gpio_fan_ctrl_group);
  302. return err;
  303. }
  304. static void fan_ctrl_free(struct gpio_fan_data *fan_data)
  305. {
  306. struct platform_device *pdev = fan_data->pdev;
  307. sysfs_remove_group(&pdev->dev.kobj, &gpio_fan_ctrl_group);
  308. }
  309. /*
  310. * Platform driver.
  311. */
  312. static ssize_t show_name(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. return sprintf(buf, "gpio-fan\n");
  316. }
  317. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  318. static int __devinit gpio_fan_probe(struct platform_device *pdev)
  319. {
  320. int err;
  321. struct gpio_fan_data *fan_data;
  322. struct gpio_fan_platform_data *pdata = pdev->dev.platform_data;
  323. if (!pdata)
  324. return -EINVAL;
  325. fan_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_fan_data),
  326. GFP_KERNEL);
  327. if (!fan_data)
  328. return -ENOMEM;
  329. fan_data->pdev = pdev;
  330. platform_set_drvdata(pdev, fan_data);
  331. mutex_init(&fan_data->lock);
  332. /* Configure alarm GPIO if available. */
  333. if (pdata->alarm) {
  334. err = fan_alarm_init(fan_data, pdata->alarm);
  335. if (err)
  336. return err;
  337. }
  338. /* Configure control GPIOs if available. */
  339. if (pdata->ctrl && pdata->num_ctrl > 0) {
  340. if (!pdata->speed || pdata->num_speed <= 1) {
  341. err = -EINVAL;
  342. goto err_free_alarm;
  343. }
  344. err = fan_ctrl_init(fan_data, pdata);
  345. if (err)
  346. goto err_free_alarm;
  347. }
  348. err = device_create_file(&pdev->dev, &dev_attr_name);
  349. if (err)
  350. goto err_free_ctrl;
  351. /* Make this driver part of hwmon class. */
  352. fan_data->hwmon_dev = hwmon_device_register(&pdev->dev);
  353. if (IS_ERR(fan_data->hwmon_dev)) {
  354. err = PTR_ERR(fan_data->hwmon_dev);
  355. goto err_remove_name;
  356. }
  357. dev_info(&pdev->dev, "GPIO fan initialized\n");
  358. return 0;
  359. err_remove_name:
  360. device_remove_file(&pdev->dev, &dev_attr_name);
  361. err_free_ctrl:
  362. if (fan_data->ctrl)
  363. fan_ctrl_free(fan_data);
  364. err_free_alarm:
  365. if (fan_data->alarm)
  366. fan_alarm_free(fan_data);
  367. return err;
  368. }
  369. static int __devexit gpio_fan_remove(struct platform_device *pdev)
  370. {
  371. struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
  372. hwmon_device_unregister(fan_data->hwmon_dev);
  373. device_remove_file(&pdev->dev, &dev_attr_name);
  374. if (fan_data->alarm)
  375. fan_alarm_free(fan_data);
  376. if (fan_data->ctrl)
  377. fan_ctrl_free(fan_data);
  378. return 0;
  379. }
  380. #ifdef CONFIG_PM_SLEEP
  381. static int gpio_fan_suspend(struct device *dev)
  382. {
  383. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  384. if (fan_data->ctrl) {
  385. fan_data->resume_speed = fan_data->speed_index;
  386. set_fan_speed(fan_data, 0);
  387. }
  388. return 0;
  389. }
  390. static int gpio_fan_resume(struct device *dev)
  391. {
  392. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  393. if (fan_data->ctrl)
  394. set_fan_speed(fan_data, fan_data->resume_speed);
  395. return 0;
  396. }
  397. static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
  398. #define GPIO_FAN_PM &gpio_fan_pm
  399. #else
  400. #define GPIO_FAN_PM NULL
  401. #endif
  402. static struct platform_driver gpio_fan_driver = {
  403. .probe = gpio_fan_probe,
  404. .remove = __devexit_p(gpio_fan_remove),
  405. .driver = {
  406. .name = "gpio-fan",
  407. .pm = GPIO_FAN_PM,
  408. },
  409. };
  410. module_platform_driver(gpio_fan_driver);
  411. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  412. MODULE_DESCRIPTION("GPIO FAN driver");
  413. MODULE_LICENSE("GPL");
  414. MODULE_ALIAS("platform:gpio-fan");