gpio-fan.c 14 KB

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