gpio-fan.c 14 KB

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