ltc2952-poweroff.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. If no pin is assigned to this input, the driver will start the
  36. * watchdog toggle immediately. The chip will only power off the system if
  37. * it is requested to do so through the kill line.
  38. *
  39. * - watchdog (output)
  40. * Once a shut down is triggered, the driver will toggle this signal,
  41. * with an internal (wde_interval) to stall the hardware shut down.
  42. *
  43. * - kill (output)
  44. * The last action during shut down is triggering this signalling, such
  45. * that the PowerPath Control will power down the hardware.
  46. *
  47. * ----------------------------------------
  48. * - Interrupts
  49. * ----------------------------------------
  50. *
  51. * The driver requires a non-shared, edge-triggered interrupt on the trigger
  52. * GPIO.
  53. *
  54. */
  55. #include <linux/kernel.h>
  56. #include <linux/init.h>
  57. #include <linux/interrupt.h>
  58. #include <linux/device.h>
  59. #include <linux/platform_device.h>
  60. #include <linux/ktime.h>
  61. #include <linux/slab.h>
  62. #include <linux/kmod.h>
  63. #include <linux/module.h>
  64. #include <linux/mod_devicetable.h>
  65. #include <linux/gpio/consumer.h>
  66. #include <linux/reboot.h>
  67. struct ltc2952_poweroff {
  68. struct hrtimer timer_trigger;
  69. struct hrtimer timer_wde;
  70. ktime_t trigger_delay;
  71. ktime_t wde_interval;
  72. struct device *dev;
  73. struct gpio_desc *gpio_trigger;
  74. struct gpio_desc *gpio_watchdog;
  75. struct gpio_desc *gpio_kill;
  76. bool kernel_panic;
  77. struct notifier_block panic_notifier;
  78. };
  79. #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
  80. /*
  81. * This global variable is only needed for pm_power_off. We should
  82. * remove it entirely once we don't need the global state anymore.
  83. */
  84. static struct ltc2952_poweroff *ltc2952_data;
  85. /**
  86. * ltc2952_poweroff_timer_wde - Timer callback
  87. * Toggles the watchdog reset signal each wde_interval
  88. *
  89. * @timer: corresponding timer
  90. *
  91. * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
  92. * machine actually shuts down
  93. */
  94. static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
  95. {
  96. ktime_t now;
  97. int state;
  98. unsigned long overruns;
  99. struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
  100. if (data->kernel_panic)
  101. return HRTIMER_NORESTART;
  102. state = gpiod_get_value(data->gpio_watchdog);
  103. gpiod_set_value(data->gpio_watchdog, !state);
  104. now = hrtimer_cb_get_time(timer);
  105. overruns = hrtimer_forward(timer, now, data->wde_interval);
  106. return HRTIMER_RESTART;
  107. }
  108. static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff *data)
  109. {
  110. hrtimer_start(&data->timer_wde, data->wde_interval, HRTIMER_MODE_REL);
  111. }
  112. static enum hrtimer_restart
  113. ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
  114. {
  115. struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
  116. ltc2952_poweroff_start_wde(data);
  117. dev_info(data->dev, "executing shutdown\n");
  118. orderly_poweroff(true);
  119. return HRTIMER_NORESTART;
  120. }
  121. /**
  122. * ltc2952_poweroff_handler - Interrupt handler
  123. * Triggered each time the trigger signal changes state and (de)activates a
  124. * time-out (timer_trigger). Once the time-out is actually reached the shut
  125. * down is executed.
  126. *
  127. * @irq: IRQ number
  128. * @dev_id: pointer to the main data structure
  129. */
  130. static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
  131. {
  132. struct ltc2952_poweroff *data = dev_id;
  133. if (data->kernel_panic || hrtimer_active(&data->timer_wde)) {
  134. /* shutdown is already triggered, nothing to do any more */
  135. return IRQ_HANDLED;
  136. }
  137. if (gpiod_get_value(data->gpio_trigger)) {
  138. hrtimer_start(&data->timer_trigger, data->trigger_delay,
  139. HRTIMER_MODE_REL);
  140. } else {
  141. hrtimer_cancel(&data->timer_trigger);
  142. }
  143. return IRQ_HANDLED;
  144. }
  145. static void ltc2952_poweroff_kill(void)
  146. {
  147. gpiod_set_value(ltc2952_data->gpio_kill, 1);
  148. }
  149. static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
  150. {
  151. data->wde_interval = 300L * 1E6L;
  152. data->trigger_delay = ktime_set(2, 500L*1E6L);
  153. hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  154. data->timer_trigger.function = ltc2952_poweroff_timer_trigger;
  155. hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  156. data->timer_wde.function = ltc2952_poweroff_timer_wde;
  157. }
  158. static int ltc2952_poweroff_init(struct platform_device *pdev)
  159. {
  160. int ret;
  161. struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
  162. ltc2952_poweroff_default(data);
  163. data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
  164. GPIOD_OUT_LOW);
  165. if (IS_ERR(data->gpio_watchdog)) {
  166. ret = PTR_ERR(data->gpio_watchdog);
  167. dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
  168. return ret;
  169. }
  170. data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
  171. if (IS_ERR(data->gpio_kill)) {
  172. ret = PTR_ERR(data->gpio_kill);
  173. dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
  174. return ret;
  175. }
  176. data->gpio_trigger = devm_gpiod_get_optional(&pdev->dev, "trigger",
  177. GPIOD_IN);
  178. if (IS_ERR(data->gpio_trigger)) {
  179. /*
  180. * It's not a problem if the trigger gpio isn't available, but
  181. * it is worth a warning if its use was defined in the device
  182. * tree.
  183. */
  184. dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
  185. data->gpio_trigger = NULL;
  186. }
  187. if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger),
  188. ltc2952_poweroff_handler,
  189. (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
  190. "ltc2952-poweroff",
  191. data)) {
  192. /*
  193. * Some things may have happened:
  194. * - No trigger input was defined
  195. * - Claiming the GPIO failed
  196. * - We could not map to an IRQ
  197. * - We couldn't register an interrupt handler
  198. *
  199. * None of these really are problems, but all of them
  200. * disqualify the push button from controlling the power.
  201. *
  202. * It is therefore important to note that if the ltc2952
  203. * detects a button press for long enough, it will still start
  204. * its own powerdown window and cut the power on us if we don't
  205. * start the watchdog trigger.
  206. */
  207. if (data->gpio_trigger) {
  208. dev_warn(&pdev->dev,
  209. "unable to configure the trigger interrupt\n");
  210. devm_gpiod_put(&pdev->dev, data->gpio_trigger);
  211. data->gpio_trigger = NULL;
  212. }
  213. dev_info(&pdev->dev,
  214. "power down trigger input will not be used\n");
  215. ltc2952_poweroff_start_wde(data);
  216. }
  217. return 0;
  218. }
  219. static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
  220. unsigned long code, void *unused)
  221. {
  222. struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier);
  223. data->kernel_panic = true;
  224. return NOTIFY_DONE;
  225. }
  226. static int ltc2952_poweroff_probe(struct platform_device *pdev)
  227. {
  228. int ret;
  229. struct ltc2952_poweroff *data;
  230. if (pm_power_off) {
  231. dev_err(&pdev->dev, "pm_power_off already registered");
  232. return -EBUSY;
  233. }
  234. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  235. if (!data)
  236. return -ENOMEM;
  237. data->dev = &pdev->dev;
  238. platform_set_drvdata(pdev, data);
  239. ret = ltc2952_poweroff_init(pdev);
  240. if (ret)
  241. return ret;
  242. /* TODO: remove ltc2952_data */
  243. ltc2952_data = data;
  244. pm_power_off = ltc2952_poweroff_kill;
  245. data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
  246. atomic_notifier_chain_register(&panic_notifier_list,
  247. &data->panic_notifier);
  248. dev_info(&pdev->dev, "probe successful\n");
  249. return 0;
  250. }
  251. static int ltc2952_poweroff_remove(struct platform_device *pdev)
  252. {
  253. struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
  254. pm_power_off = NULL;
  255. hrtimer_cancel(&data->timer_trigger);
  256. hrtimer_cancel(&data->timer_wde);
  257. atomic_notifier_chain_unregister(&panic_notifier_list,
  258. &data->panic_notifier);
  259. return 0;
  260. }
  261. static const struct of_device_id of_ltc2952_poweroff_match[] = {
  262. { .compatible = "lltc,ltc2952"},
  263. {},
  264. };
  265. MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
  266. static struct platform_driver ltc2952_poweroff_driver = {
  267. .probe = ltc2952_poweroff_probe,
  268. .remove = ltc2952_poweroff_remove,
  269. .driver = {
  270. .name = "ltc2952-poweroff",
  271. .of_match_table = of_ltc2952_poweroff_match,
  272. },
  273. };
  274. module_platform_driver(ltc2952_poweroff_driver);
  275. MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
  276. MODULE_DESCRIPTION("LTC PowerPath power-off driver");
  277. MODULE_LICENSE("GPL v2");