ltc2952-poweroff.c 8.9 KB

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