blob: e4a0cc45b3d11a51813012da750b2f07ce79de08 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
René Moll66471562014-08-08 13:12:17 +00002/*
3 * LTC2952 (PowerPath) driver
4 *
5 * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com>
Arnd Bergmann3723c632018-08-23 17:01:26 -07006 * Maintainer: René Moll <linux@r-moll.nl>
René Moll66471562014-08-08 13:12:17 +00007 *
René Moll66471562014-08-08 13:12:17 +00008 * ----------------------------------------
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
Frans Klaveree181852015-01-14 09:15:45 +010026 * 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.
René Moll66471562014-08-08 13:12:17 +000029 *
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.
René Moll66471562014-08-08 13:12:17 +000044 */
45
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/interrupt.h>
49#include <linux/device.h>
50#include <linux/platform_device.h>
51#include <linux/ktime.h>
52#include <linux/slab.h>
53#include <linux/kmod.h>
54#include <linux/module.h>
Randy Dunlapac316722018-06-19 22:47:28 -070055#include <linux/mod_devicetable.h>
René Moll66471562014-08-08 13:12:17 +000056#include <linux/gpio/consumer.h>
57#include <linux/reboot.h>
58
Frans Klavera5f67be2015-01-14 09:15:38 +010059struct ltc2952_poweroff {
René Moll66471562014-08-08 13:12:17 +000060 struct hrtimer timer_trigger;
61 struct hrtimer timer_wde;
62
63 ktime_t trigger_delay;
64 ktime_t wde_interval;
65
66 struct device *dev;
67
Frans Klaverf66472d2015-01-14 09:15:36 +010068 struct gpio_desc *gpio_trigger;
69 struct gpio_desc *gpio_watchdog;
70 struct gpio_desc *gpio_kill;
Frans Klaver818ca4c2015-01-14 09:15:39 +010071
72 bool kernel_panic;
73 struct notifier_block panic_notifier;
René Moll66471562014-08-08 13:12:17 +000074};
75
Frans Klavera5f67be2015-01-14 09:15:38 +010076#define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
77
78/*
79 * This global variable is only needed for pm_power_off. We should
80 * remove it entirely once we don't need the global state anymore.
81 */
82static struct ltc2952_poweroff *ltc2952_data;
René Moll66471562014-08-08 13:12:17 +000083
René Moll66471562014-08-08 13:12:17 +000084/**
85 * ltc2952_poweroff_timer_wde - Timer callback
86 * Toggles the watchdog reset signal each wde_interval
87 *
88 * @timer: corresponding timer
89 *
90 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
91 * machine actually shuts down
92 */
93static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
94{
95 ktime_t now;
96 int state;
97 unsigned long overruns;
Frans Klavera5f67be2015-01-14 09:15:38 +010098 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
René Moll66471562014-08-08 13:12:17 +000099
Frans Klaver818ca4c2015-01-14 09:15:39 +0100100 if (data->kernel_panic)
René Moll66471562014-08-08 13:12:17 +0000101 return HRTIMER_NORESTART;
102
Frans Klavera5f67be2015-01-14 09:15:38 +0100103 state = gpiod_get_value(data->gpio_watchdog);
104 gpiod_set_value(data->gpio_watchdog, !state);
René Moll66471562014-08-08 13:12:17 +0000105
106 now = hrtimer_cb_get_time(timer);
Frans Klavera5f67be2015-01-14 09:15:38 +0100107 overruns = hrtimer_forward(timer, now, data->wde_interval);
René Moll66471562014-08-08 13:12:17 +0000108
109 return HRTIMER_RESTART;
110}
111
Frans Klaveree181852015-01-14 09:15:45 +0100112static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff *data)
René Moll66471562014-08-08 13:12:17 +0000113{
Thomas Gleixner4101ecc2015-04-13 16:43:35 +0200114 hrtimer_start(&data->timer_wde, data->wde_interval, HRTIMER_MODE_REL);
Frans Klaveree181852015-01-14 09:15:45 +0100115}
René Moll66471562014-08-08 13:12:17 +0000116
Frans Klaveree181852015-01-14 09:15:45 +0100117static enum hrtimer_restart
118ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
119{
120 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
121
122 ltc2952_poweroff_start_wde(data);
Frans Klavera5f67be2015-01-14 09:15:38 +0100123 dev_info(data->dev, "executing shutdown\n");
René Moll66471562014-08-08 13:12:17 +0000124 orderly_poweroff(true);
125
126 return HRTIMER_NORESTART;
127}
128
129/**
130 * ltc2952_poweroff_handler - Interrupt handler
131 * Triggered each time the trigger signal changes state and (de)activates a
132 * time-out (timer_trigger). Once the time-out is actually reached the shut
133 * down is executed.
134 *
135 * @irq: IRQ number
136 * @dev_id: pointer to the main data structure
137 */
138static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
139{
Frans Klavera5f67be2015-01-14 09:15:38 +0100140 struct ltc2952_poweroff *data = dev_id;
René Moll66471562014-08-08 13:12:17 +0000141
Frans Klaver5c3faad2015-01-14 09:15:41 +0100142 if (data->kernel_panic || hrtimer_active(&data->timer_wde)) {
René Moll66471562014-08-08 13:12:17 +0000143 /* shutdown is already triggered, nothing to do any more */
Frans Klaver5c3faad2015-01-14 09:15:41 +0100144 return IRQ_HANDLED;
René Moll66471562014-08-08 13:12:17 +0000145 }
146
Frans Klaverc1ada2f2015-01-14 09:15:44 +0100147 if (gpiod_get_value(data->gpio_trigger)) {
Thomas Gleixner4101ecc2015-04-13 16:43:35 +0200148 hrtimer_start(&data->timer_trigger, data->trigger_delay,
149 HRTIMER_MODE_REL);
René Moll66471562014-08-08 13:12:17 +0000150 } else {
Frans Klaver5c3faad2015-01-14 09:15:41 +0100151 hrtimer_cancel(&data->timer_trigger);
René Moll66471562014-08-08 13:12:17 +0000152 }
René Moll66471562014-08-08 13:12:17 +0000153 return IRQ_HANDLED;
154}
155
156static void ltc2952_poweroff_kill(void)
157{
Frans Klaverf66472d2015-01-14 09:15:36 +0100158 gpiod_set_value(ltc2952_data->gpio_kill, 1);
René Moll66471562014-08-08 13:12:17 +0000159}
160
Frans Klavera5f67be2015-01-14 09:15:38 +0100161static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
René Moll66471562014-08-08 13:12:17 +0000162{
Thomas Gleixner8b0e1952016-12-25 12:30:41 +0100163 data->wde_interval = 300L * 1E6L;
René Moll66471562014-08-08 13:12:17 +0000164 data->trigger_delay = ktime_set(2, 500L*1E6L);
165
166 hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Frans Klaver5689b782015-01-14 09:15:42 +0100167 data->timer_trigger.function = ltc2952_poweroff_timer_trigger;
René Moll66471562014-08-08 13:12:17 +0000168
169 hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
Frans Klaver5689b782015-01-14 09:15:42 +0100170 data->timer_wde.function = ltc2952_poweroff_timer_wde;
René Moll66471562014-08-08 13:12:17 +0000171}
172
173static int ltc2952_poweroff_init(struct platform_device *pdev)
174{
Frans Klaveree181852015-01-14 09:15:45 +0100175 int ret;
Frans Klavera5f67be2015-01-14 09:15:38 +0100176 struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
René Moll66471562014-08-08 13:12:17 +0000177
Frans Klavera5f67be2015-01-14 09:15:38 +0100178 ltc2952_poweroff_default(data);
René Moll66471562014-08-08 13:12:17 +0000179
Frans Klavera5f67be2015-01-14 09:15:38 +0100180 data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
181 GPIOD_OUT_LOW);
182 if (IS_ERR(data->gpio_watchdog)) {
183 ret = PTR_ERR(data->gpio_watchdog);
Frans Klaverf66472d2015-01-14 09:15:36 +0100184 dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
185 return ret;
186 }
René Moll66471562014-08-08 13:12:17 +0000187
Frans Klavera5f67be2015-01-14 09:15:38 +0100188 data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
189 if (IS_ERR(data->gpio_kill)) {
190 ret = PTR_ERR(data->gpio_kill);
Frans Klaverf66472d2015-01-14 09:15:36 +0100191 dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
Frans Klaver62113b32015-01-14 09:15:37 +0100192 return ret;
Frans Klaverf66472d2015-01-14 09:15:36 +0100193 }
194
Uwe Kleine-König9f6cd982015-05-18 22:45:07 +0200195 data->gpio_trigger = devm_gpiod_get_optional(&pdev->dev, "trigger",
196 GPIOD_IN);
Frans Klaveree181852015-01-14 09:15:45 +0100197 if (IS_ERR(data->gpio_trigger)) {
198 /*
199 * It's not a problem if the trigger gpio isn't available, but
200 * it is worth a warning if its use was defined in the device
201 * tree.
202 */
Uwe Kleine-König9f6cd982015-05-18 22:45:07 +0200203 dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
Frans Klaveree181852015-01-14 09:15:45 +0100204 data->gpio_trigger = NULL;
René Moll66471562014-08-08 13:12:17 +0000205 }
206
Frans Klaveree181852015-01-14 09:15:45 +0100207 if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger),
208 ltc2952_poweroff_handler,
209 (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
210 "ltc2952-poweroff",
211 data)) {
212 /*
213 * Some things may have happened:
214 * - No trigger input was defined
215 * - Claiming the GPIO failed
216 * - We could not map to an IRQ
217 * - We couldn't register an interrupt handler
218 *
219 * None of these really are problems, but all of them
220 * disqualify the push button from controlling the power.
221 *
222 * It is therefore important to note that if the ltc2952
223 * detects a button press for long enough, it will still start
224 * its own powerdown window and cut the power on us if we don't
225 * start the watchdog trigger.
226 */
227 if (data->gpio_trigger) {
228 dev_warn(&pdev->dev,
229 "unable to configure the trigger interrupt\n");
230 devm_gpiod_put(&pdev->dev, data->gpio_trigger);
231 data->gpio_trigger = NULL;
232 }
233 dev_info(&pdev->dev,
234 "power down trigger input will not be used\n");
235 ltc2952_poweroff_start_wde(data);
René Moll66471562014-08-08 13:12:17 +0000236 }
237
238 return 0;
René Moll66471562014-08-08 13:12:17 +0000239}
240
Frans Klaver818ca4c2015-01-14 09:15:39 +0100241static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
242 unsigned long code, void *unused)
243{
244 struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier);
245
246 data->kernel_panic = true;
247 return NOTIFY_DONE;
248}
249
René Moll66471562014-08-08 13:12:17 +0000250static int ltc2952_poweroff_probe(struct platform_device *pdev)
251{
252 int ret;
Frans Klavera5f67be2015-01-14 09:15:38 +0100253 struct ltc2952_poweroff *data;
René Moll66471562014-08-08 13:12:17 +0000254
255 if (pm_power_off) {
256 dev_err(&pdev->dev, "pm_power_off already registered");
257 return -EBUSY;
258 }
259
Frans Klavera5f67be2015-01-14 09:15:38 +0100260 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
261 if (!data)
René Moll66471562014-08-08 13:12:17 +0000262 return -ENOMEM;
263
Frans Klavera5f67be2015-01-14 09:15:38 +0100264 data->dev = &pdev->dev;
265 platform_set_drvdata(pdev, data);
René Moll66471562014-08-08 13:12:17 +0000266
267 ret = ltc2952_poweroff_init(pdev);
268 if (ret)
Frans Klaver0a5c6a22015-01-14 09:15:34 +0100269 return ret;
René Moll66471562014-08-08 13:12:17 +0000270
Frans Klavera5f67be2015-01-14 09:15:38 +0100271 /* TODO: remove ltc2952_data */
272 ltc2952_data = data;
Frans Klaver5689b782015-01-14 09:15:42 +0100273 pm_power_off = ltc2952_poweroff_kill;
René Moll66471562014-08-08 13:12:17 +0000274
Frans Klaver818ca4c2015-01-14 09:15:39 +0100275 data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
276 atomic_notifier_chain_register(&panic_notifier_list,
277 &data->panic_notifier);
René Moll66471562014-08-08 13:12:17 +0000278 dev_info(&pdev->dev, "probe successful\n");
279
280 return 0;
René Moll66471562014-08-08 13:12:17 +0000281}
282
283static int ltc2952_poweroff_remove(struct platform_device *pdev)
284{
Frans Klaver818ca4c2015-01-14 09:15:39 +0100285 struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
René Moll66471562014-08-08 13:12:17 +0000286
Frans Klaver818ca4c2015-01-14 09:15:39 +0100287 pm_power_off = NULL;
Frans Klaver2f6ea8a2015-01-14 09:15:43 +0100288 hrtimer_cancel(&data->timer_trigger);
289 hrtimer_cancel(&data->timer_wde);
Frans Klaver818ca4c2015-01-14 09:15:39 +0100290 atomic_notifier_chain_unregister(&panic_notifier_list,
291 &data->panic_notifier);
René Moll66471562014-08-08 13:12:17 +0000292 return 0;
293}
294
295static const struct of_device_id of_ltc2952_poweroff_match[] = {
296 { .compatible = "lltc,ltc2952"},
297 {},
298};
299MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
300
301static struct platform_driver ltc2952_poweroff_driver = {
302 .probe = ltc2952_poweroff_probe,
303 .remove = ltc2952_poweroff_remove,
304 .driver = {
305 .name = "ltc2952-poweroff",
René Moll66471562014-08-08 13:12:17 +0000306 .of_match_table = of_ltc2952_poweroff_match,
307 },
René Moll66471562014-08-08 13:12:17 +0000308};
309
Frans Klaver818ca4c2015-01-14 09:15:39 +0100310module_platform_driver(ltc2952_poweroff_driver);
René Moll66471562014-08-08 13:12:17 +0000311
Arnd Bergmann3723c632018-08-23 17:01:26 -0700312MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
René Moll66471562014-08-08 13:12:17 +0000313MODULE_DESCRIPTION("LTC PowerPath power-off driver");
314MODULE_LICENSE("GPL v2");