gpio-fan.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 -ENODEV;
  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 DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm);
  246. static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  247. show_pwm_enable, set_pwm_enable);
  248. static DEVICE_ATTR(pwm1_mode, S_IRUGO, show_pwm_mode, NULL);
  249. static DEVICE_ATTR(fan1_min, S_IRUGO, show_rpm_min, NULL);
  250. static DEVICE_ATTR(fan1_max, S_IRUGO, show_rpm_max, NULL);
  251. static DEVICE_ATTR(fan1_input, S_IRUGO, show_rpm, NULL);
  252. static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_rpm, set_rpm);
  253. static umode_t gpio_fan_is_visible(struct kobject *kobj,
  254. struct attribute *attr, int index)
  255. {
  256. struct device *dev = container_of(kobj, struct device, kobj);
  257. struct gpio_fan_data *data = dev_get_drvdata(dev);
  258. if (index == 0 && !data->alarm)
  259. return 0;
  260. if (index > 0 && !data->ctrl)
  261. return 0;
  262. return attr->mode;
  263. }
  264. static struct attribute *gpio_fan_attributes[] = {
  265. &dev_attr_fan1_alarm.attr, /* 0 */
  266. &dev_attr_pwm1.attr, /* 1 */
  267. &dev_attr_pwm1_enable.attr,
  268. &dev_attr_pwm1_mode.attr,
  269. &dev_attr_fan1_input.attr,
  270. &dev_attr_fan1_target.attr,
  271. &dev_attr_fan1_min.attr,
  272. &dev_attr_fan1_max.attr,
  273. NULL
  274. };
  275. static const struct attribute_group gpio_fan_group = {
  276. .attrs = gpio_fan_attributes,
  277. .is_visible = gpio_fan_is_visible,
  278. };
  279. static const struct attribute_group *gpio_fan_groups[] = {
  280. &gpio_fan_group,
  281. NULL
  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 = devm_gpio_request(&pdev->dev, ctrl[i],
  292. "GPIO fan control");
  293. if (err)
  294. return err;
  295. err = gpio_direction_output(ctrl[i], gpio_get_value(ctrl[i]));
  296. if (err)
  297. return err;
  298. }
  299. fan_data->num_ctrl = num_ctrl;
  300. fan_data->ctrl = ctrl;
  301. fan_data->num_speed = pdata->num_speed;
  302. fan_data->speed = pdata->speed;
  303. fan_data->pwm_enable = true; /* Enable manual fan speed control. */
  304. fan_data->speed_index = get_fan_speed_index(fan_data);
  305. if (fan_data->speed_index < 0)
  306. return fan_data->speed_index;
  307. return 0;
  308. }
  309. #ifdef CONFIG_OF_GPIO
  310. /*
  311. * Translate OpenFirmware node properties into platform_data
  312. */
  313. static int gpio_fan_get_of_pdata(struct device *dev,
  314. struct gpio_fan_platform_data *pdata)
  315. {
  316. struct device_node *node;
  317. struct gpio_fan_speed *speed;
  318. unsigned *ctrl;
  319. unsigned i;
  320. u32 u;
  321. struct property *prop;
  322. const __be32 *p;
  323. node = dev->of_node;
  324. /* Fill GPIO pin array */
  325. pdata->num_ctrl = of_gpio_count(node);
  326. if (pdata->num_ctrl <= 0) {
  327. dev_err(dev, "gpios DT property empty / missing");
  328. return -ENODEV;
  329. }
  330. ctrl = devm_kzalloc(dev, pdata->num_ctrl * sizeof(unsigned),
  331. GFP_KERNEL);
  332. if (!ctrl)
  333. return -ENOMEM;
  334. for (i = 0; i < pdata->num_ctrl; i++) {
  335. int val;
  336. val = of_get_gpio(node, i);
  337. if (val < 0)
  338. return val;
  339. ctrl[i] = val;
  340. }
  341. pdata->ctrl = ctrl;
  342. /* Get number of RPM/ctrl_val pairs in speed map */
  343. prop = of_find_property(node, "gpio-fan,speed-map", &i);
  344. if (!prop) {
  345. dev_err(dev, "gpio-fan,speed-map DT property missing");
  346. return -ENODEV;
  347. }
  348. i = i / sizeof(u32);
  349. if (i == 0 || i & 1) {
  350. dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
  351. return -ENODEV;
  352. }
  353. pdata->num_speed = i / 2;
  354. /*
  355. * Populate speed map
  356. * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
  357. * this needs splitting into pairs to create gpio_fan_speed structs
  358. */
  359. speed = devm_kzalloc(dev,
  360. pdata->num_speed * sizeof(struct gpio_fan_speed),
  361. GFP_KERNEL);
  362. if (!speed)
  363. return -ENOMEM;
  364. p = NULL;
  365. for (i = 0; i < pdata->num_speed; i++) {
  366. p = of_prop_next_u32(prop, p, &u);
  367. if (!p)
  368. return -ENODEV;
  369. speed[i].rpm = u;
  370. p = of_prop_next_u32(prop, p, &u);
  371. if (!p)
  372. return -ENODEV;
  373. speed[i].ctrl_val = u;
  374. }
  375. pdata->speed = speed;
  376. /* Alarm GPIO if one exists */
  377. if (of_gpio_named_count(node, "alarm-gpios") > 0) {
  378. struct gpio_fan_alarm *alarm;
  379. int val;
  380. enum of_gpio_flags flags;
  381. alarm = devm_kzalloc(dev, sizeof(struct gpio_fan_alarm),
  382. GFP_KERNEL);
  383. if (!alarm)
  384. return -ENOMEM;
  385. val = of_get_named_gpio_flags(node, "alarm-gpios", 0, &flags);
  386. if (val < 0)
  387. return val;
  388. alarm->gpio = val;
  389. alarm->active_low = flags & OF_GPIO_ACTIVE_LOW;
  390. pdata->alarm = alarm;
  391. }
  392. return 0;
  393. }
  394. static struct of_device_id of_gpio_fan_match[] = {
  395. { .compatible = "gpio-fan", },
  396. {},
  397. };
  398. #endif /* CONFIG_OF_GPIO */
  399. static int gpio_fan_probe(struct platform_device *pdev)
  400. {
  401. int err;
  402. struct gpio_fan_data *fan_data;
  403. struct gpio_fan_platform_data *pdata = dev_get_platdata(&pdev->dev);
  404. #ifdef CONFIG_OF_GPIO
  405. if (!pdata) {
  406. pdata = devm_kzalloc(&pdev->dev,
  407. sizeof(struct gpio_fan_platform_data),
  408. GFP_KERNEL);
  409. if (!pdata)
  410. return -ENOMEM;
  411. err = gpio_fan_get_of_pdata(&pdev->dev, pdata);
  412. if (err)
  413. return err;
  414. }
  415. #else /* CONFIG_OF_GPIO */
  416. if (!pdata)
  417. return -EINVAL;
  418. #endif /* CONFIG_OF_GPIO */
  419. fan_data = devm_kzalloc(&pdev->dev, sizeof(struct gpio_fan_data),
  420. GFP_KERNEL);
  421. if (!fan_data)
  422. return -ENOMEM;
  423. fan_data->pdev = pdev;
  424. platform_set_drvdata(pdev, fan_data);
  425. mutex_init(&fan_data->lock);
  426. /* Configure alarm GPIO if available. */
  427. if (pdata->alarm) {
  428. err = fan_alarm_init(fan_data, pdata->alarm);
  429. if (err)
  430. return err;
  431. }
  432. /* Configure control GPIOs if available. */
  433. if (pdata->ctrl && pdata->num_ctrl > 0) {
  434. if (!pdata->speed || pdata->num_speed <= 1)
  435. return -EINVAL;
  436. err = fan_ctrl_init(fan_data, pdata);
  437. if (err)
  438. return err;
  439. }
  440. /* Make this driver part of hwmon class. */
  441. fan_data->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
  442. "gpio-fan", fan_data,
  443. gpio_fan_groups);
  444. if (IS_ERR(fan_data->hwmon_dev))
  445. return PTR_ERR(fan_data->hwmon_dev);
  446. dev_info(&pdev->dev, "GPIO fan initialized\n");
  447. return 0;
  448. }
  449. static int gpio_fan_remove(struct platform_device *pdev)
  450. {
  451. struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
  452. hwmon_device_unregister(fan_data->hwmon_dev);
  453. return 0;
  454. }
  455. #ifdef CONFIG_PM_SLEEP
  456. static int gpio_fan_suspend(struct device *dev)
  457. {
  458. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  459. if (fan_data->ctrl) {
  460. fan_data->resume_speed = fan_data->speed_index;
  461. set_fan_speed(fan_data, 0);
  462. }
  463. return 0;
  464. }
  465. static int gpio_fan_resume(struct device *dev)
  466. {
  467. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  468. if (fan_data->ctrl)
  469. set_fan_speed(fan_data, fan_data->resume_speed);
  470. return 0;
  471. }
  472. static SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
  473. #define GPIO_FAN_PM (&gpio_fan_pm)
  474. #else
  475. #define GPIO_FAN_PM NULL
  476. #endif
  477. static struct platform_driver gpio_fan_driver = {
  478. .probe = gpio_fan_probe,
  479. .remove = gpio_fan_remove,
  480. .driver = {
  481. .name = "gpio-fan",
  482. .pm = GPIO_FAN_PM,
  483. #ifdef CONFIG_OF_GPIO
  484. .of_match_table = of_match_ptr(of_gpio_fan_match),
  485. #endif
  486. },
  487. };
  488. module_platform_driver(gpio_fan_driver);
  489. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  490. MODULE_DESCRIPTION("GPIO FAN driver");
  491. MODULE_LICENSE("GPL");
  492. MODULE_ALIAS("platform:gpio-fan");