gpio-fan.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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_cansleep(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_cansleep(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_cansleep(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, unsigned long 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],
  297. gpio_get_value_cansleep(ctrl[i]));
  298. if (err)
  299. return err;
  300. }
  301. fan_data->num_ctrl = num_ctrl;
  302. fan_data->ctrl = ctrl;
  303. fan_data->num_speed = pdata->num_speed;
  304. fan_data->speed = pdata->speed;
  305. fan_data->pwm_enable = true; /* Enable manual fan speed control. */
  306. fan_data->speed_index = get_fan_speed_index(fan_data);
  307. if (fan_data->speed_index < 0)
  308. return fan_data->speed_index;
  309. return 0;
  310. }
  311. #ifdef CONFIG_OF_GPIO
  312. /*
  313. * Translate OpenFirmware node properties into platform_data
  314. */
  315. static int gpio_fan_get_of_pdata(struct device *dev,
  316. struct gpio_fan_platform_data *pdata)
  317. {
  318. struct device_node *node;
  319. struct gpio_fan_speed *speed;
  320. unsigned *ctrl;
  321. unsigned i;
  322. u32 u;
  323. struct property *prop;
  324. const __be32 *p;
  325. node = dev->of_node;
  326. /* Fill GPIO pin array */
  327. pdata->num_ctrl = of_gpio_count(node);
  328. if (pdata->num_ctrl <= 0) {
  329. dev_err(dev, "gpios DT property empty / missing");
  330. return -ENODEV;
  331. }
  332. ctrl = devm_kzalloc(dev, pdata->num_ctrl * sizeof(unsigned),
  333. GFP_KERNEL);
  334. if (!ctrl)
  335. return -ENOMEM;
  336. for (i = 0; i < pdata->num_ctrl; i++) {
  337. int val;
  338. val = of_get_gpio(node, i);
  339. if (val < 0)
  340. return val;
  341. ctrl[i] = val;
  342. }
  343. pdata->ctrl = ctrl;
  344. /* Get number of RPM/ctrl_val pairs in speed map */
  345. prop = of_find_property(node, "gpio-fan,speed-map", &i);
  346. if (!prop) {
  347. dev_err(dev, "gpio-fan,speed-map DT property missing");
  348. return -ENODEV;
  349. }
  350. i = i / sizeof(u32);
  351. if (i == 0 || i & 1) {
  352. dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
  353. return -ENODEV;
  354. }
  355. pdata->num_speed = i / 2;
  356. /*
  357. * Populate speed map
  358. * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
  359. * this needs splitting into pairs to create gpio_fan_speed structs
  360. */
  361. speed = devm_kzalloc(dev,
  362. pdata->num_speed * sizeof(struct gpio_fan_speed),
  363. GFP_KERNEL);
  364. if (!speed)
  365. return -ENOMEM;
  366. p = NULL;
  367. for (i = 0; i < pdata->num_speed; i++) {
  368. p = of_prop_next_u32(prop, p, &u);
  369. if (!p)
  370. return -ENODEV;
  371. speed[i].rpm = u;
  372. p = of_prop_next_u32(prop, p, &u);
  373. if (!p)
  374. return -ENODEV;
  375. speed[i].ctrl_val = u;
  376. }
  377. pdata->speed = speed;
  378. /* Alarm GPIO if one exists */
  379. if (of_gpio_named_count(node, "alarm-gpios") > 0) {
  380. struct gpio_fan_alarm *alarm;
  381. int val;
  382. enum of_gpio_flags flags;
  383. alarm = devm_kzalloc(dev, sizeof(struct gpio_fan_alarm),
  384. GFP_KERNEL);
  385. if (!alarm)
  386. return -ENOMEM;
  387. val = of_get_named_gpio_flags(node, "alarm-gpios", 0, &flags);
  388. if (val < 0)
  389. return val;
  390. alarm->gpio = val;
  391. alarm->active_low = flags & OF_GPIO_ACTIVE_LOW;
  392. pdata->alarm = alarm;
  393. }
  394. return 0;
  395. }
  396. static const struct of_device_id of_gpio_fan_match[] = {
  397. { .compatible = "gpio-fan", },
  398. {},
  399. };
  400. #endif /* CONFIG_OF_GPIO */
  401. static int gpio_fan_probe(struct platform_device *pdev)
  402. {
  403. int err;
  404. struct gpio_fan_data *fan_data;
  405. struct gpio_fan_platform_data *pdata = dev_get_platdata(&pdev->dev);
  406. #ifdef CONFIG_OF_GPIO
  407. if (!pdata) {
  408. pdata = devm_kzalloc(&pdev->dev,
  409. sizeof(struct gpio_fan_platform_data),
  410. GFP_KERNEL);
  411. if (!pdata)
  412. return -ENOMEM;
  413. err = gpio_fan_get_of_pdata(&pdev->dev, pdata);
  414. if (err)
  415. return err;
  416. }
  417. #else /* CONFIG_OF_GPIO */
  418. if (!pdata)
  419. return -EINVAL;
  420. #endif /* CONFIG_OF_GPIO */
  421. fan_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_fan_data),
  422. GFP_KERNEL);
  423. if (!fan_data)
  424. return -ENOMEM;
  425. fan_data->pdev = pdev;
  426. platform_set_drvdata(pdev, fan_data);
  427. mutex_init(&fan_data->lock);
  428. /* Configure alarm GPIO if available. */
  429. if (pdata->alarm) {
  430. err = fan_alarm_init(fan_data, pdata->alarm);
  431. if (err)
  432. return err;
  433. }
  434. /* Configure control GPIOs if available. */
  435. if (pdata->ctrl && pdata->num_ctrl > 0) {
  436. if (!pdata->speed || pdata->num_speed <= 1)
  437. return -EINVAL;
  438. err = fan_ctrl_init(fan_data, pdata);
  439. if (err)
  440. return err;
  441. }
  442. /* Make this driver part of hwmon class. */
  443. fan_data->hwmon_dev =
  444. devm_hwmon_device_register_with_groups(&pdev->dev,
  445. "gpio_fan", fan_data,
  446. gpio_fan_groups);
  447. if (IS_ERR(fan_data->hwmon_dev))
  448. return PTR_ERR(fan_data->hwmon_dev);
  449. dev_info(&pdev->dev, "GPIO fan initialized\n");
  450. return 0;
  451. }
  452. static void gpio_fan_shutdown(struct platform_device *pdev)
  453. {
  454. struct gpio_fan_data *fan_data = dev_get_drvdata(&pdev->dev);
  455. if (fan_data->ctrl)
  456. set_fan_speed(fan_data, 0);
  457. }
  458. #ifdef CONFIG_PM_SLEEP
  459. static int gpio_fan_suspend(struct device *dev)
  460. {
  461. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  462. if (fan_data->ctrl) {
  463. fan_data->resume_speed = fan_data->speed_index;
  464. set_fan_speed(fan_data, 0);
  465. }
  466. return 0;
  467. }
  468. static int gpio_fan_resume(struct device *dev)
  469. {
  470. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  471. if (fan_data->ctrl)
  472. set_fan_speed(fan_data, fan_data->resume_speed);
  473. return 0;
  474. }
  475. static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
  476. #define GPIO_FAN_PM (&gpio_fan_pm)
  477. #else
  478. #define GPIO_FAN_PM NULL
  479. #endif
  480. static struct platform_driver gpio_fan_driver = {
  481. .probe = gpio_fan_probe,
  482. .shutdown = gpio_fan_shutdown,
  483. .driver = {
  484. .name = "gpio-fan",
  485. .pm = GPIO_FAN_PM,
  486. #ifdef CONFIG_OF_GPIO
  487. .of_match_table = of_match_ptr(of_gpio_fan_match),
  488. #endif
  489. },
  490. };
  491. module_platform_driver(gpio_fan_driver);
  492. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  493. MODULE_DESCRIPTION("GPIO FAN driver");
  494. MODULE_LICENSE("GPL");
  495. MODULE_ALIAS("platform:gpio-fan");