ltc2952-poweroff.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * LTC2952 (PowerPath) driver
  4. *
  5. * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com>
  6. * Maintainer: René Moll <linux@r-moll.nl>
  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/mod_devicetable.h>
  55. #include <linux/gpio/consumer.h>
  56. #include <linux/reboot.h>
  57. struct ltc2952_poweroff {
  58. struct hrtimer timer_trigger;
  59. struct hrtimer timer_wde;
  60. ktime_t trigger_delay;
  61. ktime_t wde_interval;
  62. struct device *dev;
  63. struct gpio_desc *gpio_trigger;
  64. struct gpio_desc *gpio_watchdog;
  65. struct gpio_desc *gpio_kill;
  66. bool kernel_panic;
  67. struct notifier_block panic_notifier;
  68. };
  69. #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
  70. /*
  71. * This global variable is only needed for pm_power_off. We should
  72. * remove it entirely once we don't need the global state anymore.
  73. */
  74. static struct ltc2952_poweroff *ltc2952_data;
  75. /**
  76. * ltc2952_poweroff_timer_wde - Timer callback
  77. * Toggles the watchdog reset signal each wde_interval
  78. *
  79. * @timer: corresponding timer
  80. *
  81. * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
  82. * machine actually shuts down
  83. */
  84. static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
  85. {
  86. ktime_t now;
  87. int state;
  88. struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
  89. if (data->kernel_panic)
  90. return HRTIMER_NORESTART;
  91. state = gpiod_get_value(data->gpio_watchdog);
  92. gpiod_set_value(data->gpio_watchdog, !state);
  93. now = hrtimer_cb_get_time(timer);
  94. hrtimer_forward(timer, now, 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. struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
  151. ltc2952_poweroff_default(data);
  152. data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
  153. GPIOD_OUT_LOW);
  154. if (IS_ERR(data->gpio_watchdog)) {
  155. ret = PTR_ERR(data->gpio_watchdog);
  156. dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
  157. return ret;
  158. }
  159. data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
  160. if (IS_ERR(data->gpio_kill)) {
  161. ret = PTR_ERR(data->gpio_kill);
  162. dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
  163. return ret;
  164. }
  165. data->gpio_trigger = devm_gpiod_get_optional(&pdev->dev, "trigger",
  166. GPIOD_IN);
  167. if (IS_ERR(data->gpio_trigger)) {
  168. /*
  169. * It's not a problem if the trigger gpio isn't available, but
  170. * it is worth a warning if its use was defined in the device
  171. * tree.
  172. */
  173. dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
  174. data->gpio_trigger = NULL;
  175. }
  176. if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger),
  177. ltc2952_poweroff_handler,
  178. (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
  179. "ltc2952-poweroff",
  180. data)) {
  181. /*
  182. * Some things may have happened:
  183. * - No trigger input was defined
  184. * - Claiming the GPIO failed
  185. * - We could not map to an IRQ
  186. * - We couldn't register an interrupt handler
  187. *
  188. * None of these really are problems, but all of them
  189. * disqualify the push button from controlling the power.
  190. *
  191. * It is therefore important to note that if the ltc2952
  192. * detects a button press for long enough, it will still start
  193. * its own powerdown window and cut the power on us if we don't
  194. * start the watchdog trigger.
  195. */
  196. if (data->gpio_trigger) {
  197. dev_warn(&pdev->dev,
  198. "unable to configure the trigger interrupt\n");
  199. devm_gpiod_put(&pdev->dev, data->gpio_trigger);
  200. data->gpio_trigger = NULL;
  201. }
  202. dev_info(&pdev->dev,
  203. "power down trigger input will not be used\n");
  204. ltc2952_poweroff_start_wde(data);
  205. }
  206. return 0;
  207. }
  208. static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
  209. unsigned long code, void *unused)
  210. {
  211. struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier);
  212. data->kernel_panic = true;
  213. return NOTIFY_DONE;
  214. }
  215. static int ltc2952_poweroff_probe(struct platform_device *pdev)
  216. {
  217. int ret;
  218. struct ltc2952_poweroff *data;
  219. if (pm_power_off) {
  220. dev_err(&pdev->dev, "pm_power_off already registered");
  221. return -EBUSY;
  222. }
  223. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  224. if (!data)
  225. return -ENOMEM;
  226. data->dev = &pdev->dev;
  227. platform_set_drvdata(pdev, data);
  228. ret = ltc2952_poweroff_init(pdev);
  229. if (ret)
  230. return ret;
  231. /* TODO: remove ltc2952_data */
  232. ltc2952_data = data;
  233. pm_power_off = ltc2952_poweroff_kill;
  234. data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
  235. atomic_notifier_chain_register(&panic_notifier_list,
  236. &data->panic_notifier);
  237. dev_info(&pdev->dev, "probe successful\n");
  238. return 0;
  239. }
  240. static int ltc2952_poweroff_remove(struct platform_device *pdev)
  241. {
  242. struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
  243. pm_power_off = NULL;
  244. hrtimer_cancel(&data->timer_trigger);
  245. hrtimer_cancel(&data->timer_wde);
  246. atomic_notifier_chain_unregister(&panic_notifier_list,
  247. &data->panic_notifier);
  248. return 0;
  249. }
  250. static const struct of_device_id of_ltc2952_poweroff_match[] = {
  251. { .compatible = "lltc,ltc2952"},
  252. {},
  253. };
  254. MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
  255. static struct platform_driver ltc2952_poweroff_driver = {
  256. .probe = ltc2952_poweroff_probe,
  257. .remove = ltc2952_poweroff_remove,
  258. .driver = {
  259. .name = "ltc2952-poweroff",
  260. .of_match_table = of_ltc2952_poweroff_match,
  261. },
  262. };
  263. module_platform_driver(ltc2952_poweroff_driver);
  264. MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
  265. MODULE_DESCRIPTION("LTC PowerPath power-off driver");
  266. MODULE_LICENSE("GPL v2");