gpio-fan.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * gpio-fan.c - Hwmon driver for fans connected to GPIO lines.
  4. *
  5. * Copyright (C) 2010 LaCie
  6. *
  7. * Author: Simon Guinot <[email protected]>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/err.h>
  16. #include <linux/mutex.h>
  17. #include <linux/hwmon.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/of.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/thermal.h>
  22. struct gpio_fan_speed {
  23. int rpm;
  24. int ctrl_val;
  25. };
  26. struct gpio_fan_data {
  27. struct device *dev;
  28. struct device *hwmon_dev;
  29. /* Cooling device if any */
  30. struct thermal_cooling_device *cdev;
  31. struct mutex lock; /* lock GPIOs operations. */
  32. int num_gpios;
  33. struct gpio_desc **gpios;
  34. int num_speed;
  35. struct gpio_fan_speed *speed;
  36. int speed_index;
  37. int resume_speed;
  38. bool pwm_enable;
  39. struct gpio_desc *alarm_gpio;
  40. struct work_struct alarm_work;
  41. };
  42. /*
  43. * Alarm GPIO.
  44. */
  45. static void fan_alarm_notify(struct work_struct *ws)
  46. {
  47. struct gpio_fan_data *fan_data =
  48. container_of(ws, struct gpio_fan_data, alarm_work);
  49. sysfs_notify(&fan_data->hwmon_dev->kobj, NULL, "fan1_alarm");
  50. kobject_uevent(&fan_data->hwmon_dev->kobj, KOBJ_CHANGE);
  51. }
  52. static irqreturn_t fan_alarm_irq_handler(int irq, void *dev_id)
  53. {
  54. struct gpio_fan_data *fan_data = dev_id;
  55. schedule_work(&fan_data->alarm_work);
  56. return IRQ_NONE;
  57. }
  58. static ssize_t fan1_alarm_show(struct device *dev,
  59. struct device_attribute *attr, char *buf)
  60. {
  61. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  62. return sprintf(buf, "%d\n",
  63. gpiod_get_value_cansleep(fan_data->alarm_gpio));
  64. }
  65. static DEVICE_ATTR_RO(fan1_alarm);
  66. static int fan_alarm_init(struct gpio_fan_data *fan_data)
  67. {
  68. int alarm_irq;
  69. struct device *dev = fan_data->dev;
  70. /*
  71. * If the alarm GPIO don't support interrupts, just leave
  72. * without initializing the fail notification support.
  73. */
  74. alarm_irq = gpiod_to_irq(fan_data->alarm_gpio);
  75. if (alarm_irq <= 0)
  76. return 0;
  77. INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
  78. irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
  79. return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
  80. IRQF_SHARED, "GPIO fan alarm", fan_data);
  81. }
  82. /*
  83. * Control GPIOs.
  84. */
  85. /* Must be called with fan_data->lock held, except during initialization. */
  86. static void __set_fan_ctrl(struct gpio_fan_data *fan_data, int ctrl_val)
  87. {
  88. int i;
  89. for (i = 0; i < fan_data->num_gpios; i++)
  90. gpiod_set_value_cansleep(fan_data->gpios[i],
  91. (ctrl_val >> i) & 1);
  92. }
  93. static int __get_fan_ctrl(struct gpio_fan_data *fan_data)
  94. {
  95. int i;
  96. int ctrl_val = 0;
  97. for (i = 0; i < fan_data->num_gpios; i++) {
  98. int value;
  99. value = gpiod_get_value_cansleep(fan_data->gpios[i]);
  100. ctrl_val |= (value << i);
  101. }
  102. return ctrl_val;
  103. }
  104. /* Must be called with fan_data->lock held, except during initialization. */
  105. static void set_fan_speed(struct gpio_fan_data *fan_data, int speed_index)
  106. {
  107. if (fan_data->speed_index == speed_index)
  108. return;
  109. __set_fan_ctrl(fan_data, fan_data->speed[speed_index].ctrl_val);
  110. fan_data->speed_index = speed_index;
  111. }
  112. static int get_fan_speed_index(struct gpio_fan_data *fan_data)
  113. {
  114. int ctrl_val = __get_fan_ctrl(fan_data);
  115. int i;
  116. for (i = 0; i < fan_data->num_speed; i++)
  117. if (fan_data->speed[i].ctrl_val == ctrl_val)
  118. return i;
  119. dev_warn(fan_data->dev,
  120. "missing speed array entry for GPIO value 0x%x\n", ctrl_val);
  121. return -ENODEV;
  122. }
  123. static int rpm_to_speed_index(struct gpio_fan_data *fan_data, unsigned long rpm)
  124. {
  125. struct gpio_fan_speed *speed = fan_data->speed;
  126. int i;
  127. for (i = 0; i < fan_data->num_speed; i++)
  128. if (speed[i].rpm >= rpm)
  129. return i;
  130. return fan_data->num_speed - 1;
  131. }
  132. static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
  133. char *buf)
  134. {
  135. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  136. u8 pwm = fan_data->speed_index * 255 / (fan_data->num_speed - 1);
  137. return sprintf(buf, "%d\n", pwm);
  138. }
  139. static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
  140. const char *buf, size_t count)
  141. {
  142. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  143. unsigned long pwm;
  144. int speed_index;
  145. int ret = count;
  146. if (kstrtoul(buf, 10, &pwm) || pwm > 255)
  147. return -EINVAL;
  148. mutex_lock(&fan_data->lock);
  149. if (!fan_data->pwm_enable) {
  150. ret = -EPERM;
  151. goto exit_unlock;
  152. }
  153. speed_index = DIV_ROUND_UP(pwm * (fan_data->num_speed - 1), 255);
  154. set_fan_speed(fan_data, speed_index);
  155. exit_unlock:
  156. mutex_unlock(&fan_data->lock);
  157. return ret;
  158. }
  159. static ssize_t pwm1_enable_show(struct device *dev,
  160. struct device_attribute *attr, char *buf)
  161. {
  162. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  163. return sprintf(buf, "%d\n", fan_data->pwm_enable);
  164. }
  165. static ssize_t pwm1_enable_store(struct device *dev,
  166. struct device_attribute *attr,
  167. const char *buf, size_t count)
  168. {
  169. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  170. unsigned long val;
  171. if (kstrtoul(buf, 10, &val) || val > 1)
  172. return -EINVAL;
  173. if (fan_data->pwm_enable == val)
  174. return count;
  175. mutex_lock(&fan_data->lock);
  176. fan_data->pwm_enable = val;
  177. /* Disable manual control mode: set fan at full speed. */
  178. if (val == 0)
  179. set_fan_speed(fan_data, fan_data->num_speed - 1);
  180. mutex_unlock(&fan_data->lock);
  181. return count;
  182. }
  183. static ssize_t pwm1_mode_show(struct device *dev,
  184. struct device_attribute *attr, char *buf)
  185. {
  186. return sprintf(buf, "0\n");
  187. }
  188. static ssize_t fan1_min_show(struct device *dev,
  189. struct device_attribute *attr, char *buf)
  190. {
  191. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  192. return sprintf(buf, "%d\n", fan_data->speed[0].rpm);
  193. }
  194. static ssize_t fan1_max_show(struct device *dev,
  195. struct device_attribute *attr, char *buf)
  196. {
  197. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  198. return sprintf(buf, "%d\n",
  199. fan_data->speed[fan_data->num_speed - 1].rpm);
  200. }
  201. static ssize_t fan1_input_show(struct device *dev,
  202. struct device_attribute *attr, char *buf)
  203. {
  204. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  205. return sprintf(buf, "%d\n", fan_data->speed[fan_data->speed_index].rpm);
  206. }
  207. static ssize_t set_rpm(struct device *dev, struct device_attribute *attr,
  208. const char *buf, size_t count)
  209. {
  210. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  211. unsigned long rpm;
  212. int ret = count;
  213. if (kstrtoul(buf, 10, &rpm))
  214. return -EINVAL;
  215. mutex_lock(&fan_data->lock);
  216. if (!fan_data->pwm_enable) {
  217. ret = -EPERM;
  218. goto exit_unlock;
  219. }
  220. set_fan_speed(fan_data, rpm_to_speed_index(fan_data, rpm));
  221. exit_unlock:
  222. mutex_unlock(&fan_data->lock);
  223. return ret;
  224. }
  225. static DEVICE_ATTR_RW(pwm1);
  226. static DEVICE_ATTR_RW(pwm1_enable);
  227. static DEVICE_ATTR_RO(pwm1_mode);
  228. static DEVICE_ATTR_RO(fan1_min);
  229. static DEVICE_ATTR_RO(fan1_max);
  230. static DEVICE_ATTR_RO(fan1_input);
  231. static DEVICE_ATTR(fan1_target, 0644, fan1_input_show, set_rpm);
  232. static umode_t gpio_fan_is_visible(struct kobject *kobj,
  233. struct attribute *attr, int index)
  234. {
  235. struct device *dev = kobj_to_dev(kobj);
  236. struct gpio_fan_data *data = dev_get_drvdata(dev);
  237. if (index == 0 && !data->alarm_gpio)
  238. return 0;
  239. if (index > 0 && !data->gpios)
  240. return 0;
  241. return attr->mode;
  242. }
  243. static struct attribute *gpio_fan_attributes[] = {
  244. &dev_attr_fan1_alarm.attr, /* 0 */
  245. &dev_attr_pwm1.attr, /* 1 */
  246. &dev_attr_pwm1_enable.attr,
  247. &dev_attr_pwm1_mode.attr,
  248. &dev_attr_fan1_input.attr,
  249. &dev_attr_fan1_target.attr,
  250. &dev_attr_fan1_min.attr,
  251. &dev_attr_fan1_max.attr,
  252. NULL
  253. };
  254. static const struct attribute_group gpio_fan_group = {
  255. .attrs = gpio_fan_attributes,
  256. .is_visible = gpio_fan_is_visible,
  257. };
  258. static const struct attribute_group *gpio_fan_groups[] = {
  259. &gpio_fan_group,
  260. NULL
  261. };
  262. static int fan_ctrl_init(struct gpio_fan_data *fan_data)
  263. {
  264. int num_gpios = fan_data->num_gpios;
  265. struct gpio_desc **gpios = fan_data->gpios;
  266. int i, err;
  267. for (i = 0; i < num_gpios; i++) {
  268. /*
  269. * The GPIO descriptors were retrieved with GPIOD_ASIS so here
  270. * we set the GPIO into output mode, carefully preserving the
  271. * current value by setting it to whatever it is already set
  272. * (no surprise changes in default fan speed).
  273. */
  274. err = gpiod_direction_output(gpios[i],
  275. gpiod_get_value_cansleep(gpios[i]));
  276. if (err)
  277. return err;
  278. }
  279. fan_data->pwm_enable = true; /* Enable manual fan speed control. */
  280. fan_data->speed_index = get_fan_speed_index(fan_data);
  281. if (fan_data->speed_index < 0)
  282. return fan_data->speed_index;
  283. return 0;
  284. }
  285. static int gpio_fan_get_max_state(struct thermal_cooling_device *cdev,
  286. unsigned long *state)
  287. {
  288. struct gpio_fan_data *fan_data = cdev->devdata;
  289. if (!fan_data)
  290. return -EINVAL;
  291. *state = fan_data->num_speed - 1;
  292. return 0;
  293. }
  294. static int gpio_fan_get_cur_state(struct thermal_cooling_device *cdev,
  295. unsigned long *state)
  296. {
  297. struct gpio_fan_data *fan_data = cdev->devdata;
  298. if (!fan_data)
  299. return -EINVAL;
  300. *state = fan_data->speed_index;
  301. return 0;
  302. }
  303. static int gpio_fan_set_cur_state(struct thermal_cooling_device *cdev,
  304. unsigned long state)
  305. {
  306. struct gpio_fan_data *fan_data = cdev->devdata;
  307. if (!fan_data)
  308. return -EINVAL;
  309. if (state >= fan_data->num_speed)
  310. return -EINVAL;
  311. set_fan_speed(fan_data, state);
  312. return 0;
  313. }
  314. static const struct thermal_cooling_device_ops gpio_fan_cool_ops = {
  315. .get_max_state = gpio_fan_get_max_state,
  316. .get_cur_state = gpio_fan_get_cur_state,
  317. .set_cur_state = gpio_fan_set_cur_state,
  318. };
  319. /*
  320. * Translate OpenFirmware node properties into platform_data
  321. */
  322. static int gpio_fan_get_of_data(struct gpio_fan_data *fan_data)
  323. {
  324. struct gpio_fan_speed *speed;
  325. struct device *dev = fan_data->dev;
  326. struct device_node *np = dev->of_node;
  327. struct gpio_desc **gpios;
  328. unsigned i;
  329. u32 u;
  330. struct property *prop;
  331. const __be32 *p;
  332. /* Alarm GPIO if one exists */
  333. fan_data->alarm_gpio = devm_gpiod_get_optional(dev, "alarm", GPIOD_IN);
  334. if (IS_ERR(fan_data->alarm_gpio))
  335. return PTR_ERR(fan_data->alarm_gpio);
  336. /* Fill GPIO pin array */
  337. fan_data->num_gpios = gpiod_count(dev, NULL);
  338. if (fan_data->num_gpios <= 0) {
  339. if (fan_data->alarm_gpio)
  340. return 0;
  341. dev_err(dev, "DT properties empty / missing");
  342. return -ENODEV;
  343. }
  344. gpios = devm_kcalloc(dev,
  345. fan_data->num_gpios, sizeof(struct gpio_desc *),
  346. GFP_KERNEL);
  347. if (!gpios)
  348. return -ENOMEM;
  349. for (i = 0; i < fan_data->num_gpios; i++) {
  350. gpios[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
  351. if (IS_ERR(gpios[i]))
  352. return PTR_ERR(gpios[i]);
  353. }
  354. fan_data->gpios = gpios;
  355. /* Get number of RPM/ctrl_val pairs in speed map */
  356. prop = of_find_property(np, "gpio-fan,speed-map", &i);
  357. if (!prop) {
  358. dev_err(dev, "gpio-fan,speed-map DT property missing");
  359. return -ENODEV;
  360. }
  361. i = i / sizeof(u32);
  362. if (i == 0 || i & 1) {
  363. dev_err(dev, "gpio-fan,speed-map contains zero/odd number of entries");
  364. return -ENODEV;
  365. }
  366. fan_data->num_speed = i / 2;
  367. /*
  368. * Populate speed map
  369. * Speed map is in the form <RPM ctrl_val RPM ctrl_val ...>
  370. * this needs splitting into pairs to create gpio_fan_speed structs
  371. */
  372. speed = devm_kcalloc(dev,
  373. fan_data->num_speed, sizeof(struct gpio_fan_speed),
  374. GFP_KERNEL);
  375. if (!speed)
  376. return -ENOMEM;
  377. p = NULL;
  378. for (i = 0; i < fan_data->num_speed; i++) {
  379. p = of_prop_next_u32(prop, p, &u);
  380. if (!p)
  381. return -ENODEV;
  382. speed[i].rpm = u;
  383. p = of_prop_next_u32(prop, p, &u);
  384. if (!p)
  385. return -ENODEV;
  386. speed[i].ctrl_val = u;
  387. }
  388. fan_data->speed = speed;
  389. return 0;
  390. }
  391. static const struct of_device_id of_gpio_fan_match[] = {
  392. { .compatible = "gpio-fan", },
  393. {},
  394. };
  395. MODULE_DEVICE_TABLE(of, of_gpio_fan_match);
  396. static void gpio_fan_stop(void *data)
  397. {
  398. set_fan_speed(data, 0);
  399. }
  400. static int gpio_fan_probe(struct platform_device *pdev)
  401. {
  402. int err;
  403. struct gpio_fan_data *fan_data;
  404. struct device *dev = &pdev->dev;
  405. struct device_node *np = dev->of_node;
  406. fan_data = devm_kzalloc(dev, sizeof(struct gpio_fan_data),
  407. GFP_KERNEL);
  408. if (!fan_data)
  409. return -ENOMEM;
  410. fan_data->dev = dev;
  411. err = gpio_fan_get_of_data(fan_data);
  412. if (err)
  413. return err;
  414. platform_set_drvdata(pdev, fan_data);
  415. mutex_init(&fan_data->lock);
  416. /* Configure control GPIOs if available. */
  417. if (fan_data->gpios && fan_data->num_gpios > 0) {
  418. if (!fan_data->speed || fan_data->num_speed <= 1)
  419. return -EINVAL;
  420. err = fan_ctrl_init(fan_data);
  421. if (err)
  422. return err;
  423. err = devm_add_action_or_reset(dev, gpio_fan_stop, fan_data);
  424. if (err)
  425. return err;
  426. }
  427. /* Make this driver part of hwmon class. */
  428. fan_data->hwmon_dev =
  429. devm_hwmon_device_register_with_groups(dev,
  430. "gpio_fan", fan_data,
  431. gpio_fan_groups);
  432. if (IS_ERR(fan_data->hwmon_dev))
  433. return PTR_ERR(fan_data->hwmon_dev);
  434. /* Configure alarm GPIO if available. */
  435. if (fan_data->alarm_gpio) {
  436. err = fan_alarm_init(fan_data);
  437. if (err)
  438. return err;
  439. }
  440. /* Optional cooling device register for Device tree platforms */
  441. fan_data->cdev = devm_thermal_of_cooling_device_register(dev, np,
  442. "gpio-fan", fan_data, &gpio_fan_cool_ops);
  443. dev_info(dev, "GPIO fan initialized\n");
  444. return 0;
  445. }
  446. static void gpio_fan_shutdown(struct platform_device *pdev)
  447. {
  448. struct gpio_fan_data *fan_data = platform_get_drvdata(pdev);
  449. if (fan_data->gpios)
  450. set_fan_speed(fan_data, 0);
  451. }
  452. static int gpio_fan_suspend(struct device *dev)
  453. {
  454. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  455. if (fan_data->gpios) {
  456. fan_data->resume_speed = fan_data->speed_index;
  457. set_fan_speed(fan_data, 0);
  458. }
  459. return 0;
  460. }
  461. static int gpio_fan_resume(struct device *dev)
  462. {
  463. struct gpio_fan_data *fan_data = dev_get_drvdata(dev);
  464. if (fan_data->gpios)
  465. set_fan_speed(fan_data, fan_data->resume_speed);
  466. return 0;
  467. }
  468. static DEFINE_SIMPLE_DEV_PM_OPS(gpio_fan_pm, gpio_fan_suspend, gpio_fan_resume);
  469. static struct platform_driver gpio_fan_driver = {
  470. .probe = gpio_fan_probe,
  471. .shutdown = gpio_fan_shutdown,
  472. .driver = {
  473. .name = "gpio-fan",
  474. .pm = pm_sleep_ptr(&gpio_fan_pm),
  475. .of_match_table = of_match_ptr(of_gpio_fan_match),
  476. },
  477. };
  478. module_platform_driver(gpio_fan_driver);
  479. MODULE_AUTHOR("Simon Guinot <[email protected]>");
  480. MODULE_DESCRIPTION("GPIO FAN driver");
  481. MODULE_LICENSE("GPL");
  482. MODULE_ALIAS("platform:gpio-fan");