ltc2952-poweroff.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * LTC2952 (PowerPath) driver
  3. *
  4. * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com>
  5. * Maintainer: René Moll <linux@r-moll.nl>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * ----------------------------------------
  18. * - Description
  19. * ----------------------------------------
  20. *
  21. * This driver is to be used with an external PowerPath Controller (LTC2952).
  22. * Its function is to determine when a external shut down is triggered
  23. * and react by properly shutting down the system.
  24. *
  25. * This driver expects a device tree with a ltc2952 entry for pin mapping.
  26. *
  27. * ----------------------------------------
  28. * - GPIO
  29. * ----------------------------------------
  30. *
  31. * The following GPIOs are used:
  32. * - trigger (input)
  33. * A level change indicates the shut-down trigger. If it's state reverts
  34. * within the time-out defined by trigger_delay, the shut down is not
  35. * executed.
  36. *
  37. * - watchdog (output)
  38. * Once a shut down is triggered, the driver will toggle this signal,
  39. * with an internal (wde_interval) to stall the hardware shut down.
  40. *
  41. * - kill (output)
  42. * The last action during shut down is triggering this signalling, such
  43. * that the PowerPath Control will power down the hardware.
  44. *
  45. * ----------------------------------------
  46. * - Interrupts
  47. * ----------------------------------------
  48. *
  49. * The driver requires a non-shared, edge-triggered interrupt on the trigger
  50. * GPIO.
  51. *
  52. */
  53. #include <linux/kernel.h>
  54. #include <linux/init.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/device.h>
  57. #include <linux/platform_device.h>
  58. #include <linux/ktime.h>
  59. #include <linux/slab.h>
  60. #include <linux/kmod.h>
  61. #include <linux/module.h>
  62. #include <linux/gpio/consumer.h>
  63. #include <linux/reboot.h>
  64. struct ltc2952_poweroff_data {
  65. struct hrtimer timer_trigger;
  66. struct hrtimer timer_wde;
  67. ktime_t trigger_delay;
  68. ktime_t wde_interval;
  69. struct device *dev;
  70. unsigned int virq;
  71. /**
  72. * 0: trigger
  73. * 1: watchdog
  74. * 2: kill
  75. */
  76. struct gpio_desc *gpio[3];
  77. };
  78. static int ltc2952_poweroff_panic;
  79. static struct ltc2952_poweroff_data *ltc2952_data;
  80. #define POWERPATH_IO_TRIGGER 0
  81. #define POWERPATH_IO_WATCHDOG 1
  82. #define POWERPATH_IO_KILL 2
  83. /**
  84. * ltc2952_poweroff_timer_wde - Timer callback
  85. * Toggles the watchdog reset signal each wde_interval
  86. *
  87. * @timer: corresponding timer
  88. *
  89. * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
  90. * machine actually shuts down
  91. */
  92. static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
  93. {
  94. ktime_t now;
  95. int state;
  96. unsigned long overruns;
  97. if (ltc2952_poweroff_panic)
  98. return HRTIMER_NORESTART;
  99. state = gpiod_get_value(ltc2952_data->gpio[POWERPATH_IO_WATCHDOG]);
  100. gpiod_set_value(ltc2952_data->gpio[POWERPATH_IO_WATCHDOG], !state);
  101. now = hrtimer_cb_get_time(timer);
  102. overruns = hrtimer_forward(timer, now, ltc2952_data->wde_interval);
  103. return HRTIMER_RESTART;
  104. }
  105. static enum hrtimer_restart ltc2952_poweroff_timer_trigger(
  106. struct hrtimer *timer)
  107. {
  108. int ret;
  109. ret = hrtimer_start(&ltc2952_data->timer_wde,
  110. ltc2952_data->wde_interval, HRTIMER_MODE_REL);
  111. if (ret) {
  112. dev_err(ltc2952_data->dev, "unable to start the timer\n");
  113. /*
  114. * The device will not toggle the watchdog reset,
  115. * thus shut down is only safe if the PowerPath controller
  116. * has a long enough time-off before triggering a hardware
  117. * power-off.
  118. *
  119. * Only sending a warning as the system will power-off anyway
  120. */
  121. }
  122. dev_info(ltc2952_data->dev, "executing shutdown\n");
  123. orderly_poweroff(true);
  124. return HRTIMER_NORESTART;
  125. }
  126. /**
  127. * ltc2952_poweroff_handler - Interrupt handler
  128. * Triggered each time the trigger signal changes state and (de)activates a
  129. * time-out (timer_trigger). Once the time-out is actually reached the shut
  130. * down is executed.
  131. *
  132. * @irq: IRQ number
  133. * @dev_id: pointer to the main data structure
  134. */
  135. static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
  136. {
  137. int ret;
  138. struct ltc2952_poweroff_data *data = dev_id;
  139. if (ltc2952_poweroff_panic)
  140. goto irq_ok;
  141. if (hrtimer_active(&data->timer_wde)) {
  142. /* shutdown is already triggered, nothing to do any more */
  143. goto irq_ok;
  144. }
  145. if (!hrtimer_active(&data->timer_trigger)) {
  146. ret = hrtimer_start(&data->timer_trigger, data->trigger_delay,
  147. HRTIMER_MODE_REL);
  148. if (ret)
  149. dev_err(data->dev, "unable to start the wait timer\n");
  150. } else {
  151. ret = hrtimer_cancel(&data->timer_trigger);
  152. /* omitting return value check, timer should have been valid */
  153. }
  154. irq_ok:
  155. return IRQ_HANDLED;
  156. }
  157. static void ltc2952_poweroff_kill(void)
  158. {
  159. gpiod_set_value(ltc2952_data->gpio[POWERPATH_IO_KILL], 1);
  160. }
  161. static int ltc2952_poweroff_suspend(struct platform_device *pdev,
  162. pm_message_t state)
  163. {
  164. return -ENOSYS;
  165. }
  166. static int ltc2952_poweroff_resume(struct platform_device *pdev)
  167. {
  168. return -ENOSYS;
  169. }
  170. static void ltc2952_poweroff_default(struct ltc2952_poweroff_data *data)
  171. {
  172. unsigned int i;
  173. for (i = 0; i < ARRAY_SIZE(data->gpio); i++)
  174. data->gpio[i] = NULL;
  175. data->wde_interval = ktime_set(0, 300L*1E6L);
  176. data->trigger_delay = ktime_set(2, 500L*1E6L);
  177. hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  178. data->timer_trigger.function = &ltc2952_poweroff_timer_trigger;
  179. hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  180. data->timer_wde.function = &ltc2952_poweroff_timer_wde;
  181. }
  182. static int ltc2952_poweroff_init(struct platform_device *pdev)
  183. {
  184. int ret, virq;
  185. unsigned int i;
  186. struct ltc2952_poweroff_data *data;
  187. static char *name[] = {
  188. "trigger",
  189. "watchdog",
  190. "kill",
  191. NULL
  192. };
  193. data = ltc2952_data;
  194. ltc2952_poweroff_default(ltc2952_data);
  195. for (i = 0; i < ARRAY_SIZE(ltc2952_data->gpio); i++) {
  196. ltc2952_data->gpio[i] = gpiod_get(&pdev->dev, name[i]);
  197. if (IS_ERR(ltc2952_data->gpio[i])) {
  198. ret = PTR_ERR(ltc2952_data->gpio[i]);
  199. dev_err(&pdev->dev,
  200. "unable to claim the following gpio: %s\n",
  201. name[i]);
  202. goto err_io;
  203. }
  204. }
  205. ret = gpiod_direction_output(
  206. ltc2952_data->gpio[POWERPATH_IO_WATCHDOG], 0);
  207. if (ret) {
  208. dev_err(&pdev->dev, "unable to use watchdog-gpio as output\n");
  209. goto err_io;
  210. }
  211. ret = gpiod_direction_output(ltc2952_data->gpio[POWERPATH_IO_KILL], 0);
  212. if (ret) {
  213. dev_err(&pdev->dev, "unable to use kill-gpio as output\n");
  214. goto err_io;
  215. }
  216. virq = gpiod_to_irq(ltc2952_data->gpio[POWERPATH_IO_TRIGGER]);
  217. if (virq < 0) {
  218. dev_err(&pdev->dev, "cannot map GPIO as interrupt");
  219. goto err_io;
  220. }
  221. ltc2952_data->virq = virq;
  222. ret = request_irq(virq,
  223. ltc2952_poweroff_handler,
  224. (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
  225. "ltc2952-poweroff",
  226. ltc2952_data
  227. );
  228. if (ret) {
  229. dev_err(&pdev->dev, "cannot configure an interrupt handler\n");
  230. goto err_io;
  231. }
  232. return 0;
  233. err_io:
  234. for (i = 0; i < ARRAY_SIZE(ltc2952_data->gpio); i++)
  235. if (ltc2952_data->gpio[i])
  236. gpiod_put(ltc2952_data->gpio[i]);
  237. return ret;
  238. }
  239. static int ltc2952_poweroff_probe(struct platform_device *pdev)
  240. {
  241. int ret;
  242. if (pm_power_off) {
  243. dev_err(&pdev->dev, "pm_power_off already registered");
  244. return -EBUSY;
  245. }
  246. ltc2952_data = kzalloc(sizeof(*ltc2952_data), GFP_KERNEL);
  247. if (!ltc2952_data)
  248. return -ENOMEM;
  249. ltc2952_data->dev = &pdev->dev;
  250. ret = ltc2952_poweroff_init(pdev);
  251. if (ret)
  252. goto err;
  253. pm_power_off = &ltc2952_poweroff_kill;
  254. dev_info(&pdev->dev, "probe successful\n");
  255. return 0;
  256. err:
  257. kfree(ltc2952_data);
  258. return ret;
  259. }
  260. static int ltc2952_poweroff_remove(struct platform_device *pdev)
  261. {
  262. unsigned int i;
  263. pm_power_off = NULL;
  264. if (ltc2952_data) {
  265. free_irq(ltc2952_data->virq, ltc2952_data);
  266. for (i = 0; i < ARRAY_SIZE(ltc2952_data->gpio); i++)
  267. gpiod_put(ltc2952_data->gpio[i]);
  268. kfree(ltc2952_data);
  269. }
  270. return 0;
  271. }
  272. static const struct of_device_id of_ltc2952_poweroff_match[] = {
  273. { .compatible = "lltc,ltc2952"},
  274. {},
  275. };
  276. MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
  277. static struct platform_driver ltc2952_poweroff_driver = {
  278. .probe = ltc2952_poweroff_probe,
  279. .remove = ltc2952_poweroff_remove,
  280. .driver = {
  281. .name = "ltc2952-poweroff",
  282. .of_match_table = of_ltc2952_poweroff_match,
  283. },
  284. .suspend = ltc2952_poweroff_suspend,
  285. .resume = ltc2952_poweroff_resume,
  286. };
  287. static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
  288. unsigned long code, void *unused)
  289. {
  290. ltc2952_poweroff_panic = 1;
  291. return NOTIFY_DONE;
  292. }
  293. static struct notifier_block ltc2952_poweroff_panic_nb = {
  294. .notifier_call = ltc2952_poweroff_notify_panic,
  295. };
  296. static int __init ltc2952_poweroff_platform_init(void)
  297. {
  298. ltc2952_poweroff_panic = 0;
  299. atomic_notifier_chain_register(&panic_notifier_list,
  300. &ltc2952_poweroff_panic_nb);
  301. return platform_driver_register(&ltc2952_poweroff_driver);
  302. }
  303. static void __exit ltc2952_poweroff_platform_exit(void)
  304. {
  305. atomic_notifier_chain_unregister(&panic_notifier_list,
  306. &ltc2952_poweroff_panic_nb);
  307. platform_driver_unregister(&ltc2952_poweroff_driver);
  308. }
  309. module_init(ltc2952_poweroff_platform_init);
  310. module_exit(ltc2952_poweroff_platform_exit);
  311. MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
  312. MODULE_DESCRIPTION("LTC PowerPath power-off driver");
  313. MODULE_LICENSE("GPL v2");