2 * LTC2952 (PowerPath) driver
4 * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com>
5 * Maintainer: René Moll <linux@r-moll.nl>
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.
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.
17 * ----------------------------------------
19 * ----------------------------------------
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.
25 * This driver expects a device tree with a ltc2952 entry for pin mapping.
27 * ----------------------------------------
29 * ----------------------------------------
31 * The following GPIOs are used:
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.
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.
44 * The last action during shut down is triggering this signalling, such
45 * that the PowerPath Control will power down the hardware.
47 * ----------------------------------------
49 * ----------------------------------------
51 * The driver requires a non-shared, edge-triggered interrupt on the trigger
56 #include <linux/kernel.h>
57 #include <linux/init.h>
58 #include <linux/interrupt.h>
59 #include <linux/device.h>
60 #include <linux/platform_device.h>
61 #include <linux/ktime.h>
62 #include <linux/slab.h>
63 #include <linux/kmod.h>
64 #include <linux/module.h>
65 #include <linux/mod_devicetable.h>
66 #include <linux/gpio/consumer.h>
67 #include <linux/reboot.h>
69 struct ltc2952_poweroff
{
70 struct hrtimer timer_trigger
;
71 struct hrtimer timer_wde
;
73 ktime_t trigger_delay
;
78 struct gpio_desc
*gpio_trigger
;
79 struct gpio_desc
*gpio_watchdog
;
80 struct gpio_desc
*gpio_kill
;
83 struct notifier_block panic_notifier
;
86 #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
89 * This global variable is only needed for pm_power_off. We should
90 * remove it entirely once we don't need the global state anymore.
92 static struct ltc2952_poweroff
*ltc2952_data
;
95 * ltc2952_poweroff_timer_wde - Timer callback
96 * Toggles the watchdog reset signal each wde_interval
98 * @timer: corresponding timer
100 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
101 * machine actually shuts down
103 static enum hrtimer_restart
ltc2952_poweroff_timer_wde(struct hrtimer
*timer
)
107 unsigned long overruns
;
108 struct ltc2952_poweroff
*data
= to_ltc2952(timer
, timer_wde
);
110 if (data
->kernel_panic
)
111 return HRTIMER_NORESTART
;
113 state
= gpiod_get_value(data
->gpio_watchdog
);
114 gpiod_set_value(data
->gpio_watchdog
, !state
);
116 now
= hrtimer_cb_get_time(timer
);
117 overruns
= hrtimer_forward(timer
, now
, data
->wde_interval
);
119 return HRTIMER_RESTART
;
122 static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff
*data
)
124 hrtimer_start(&data
->timer_wde
, data
->wde_interval
, HRTIMER_MODE_REL
);
127 static enum hrtimer_restart
128 ltc2952_poweroff_timer_trigger(struct hrtimer
*timer
)
130 struct ltc2952_poweroff
*data
= to_ltc2952(timer
, timer_trigger
);
132 ltc2952_poweroff_start_wde(data
);
133 dev_info(data
->dev
, "executing shutdown\n");
134 orderly_poweroff(true);
136 return HRTIMER_NORESTART
;
140 * ltc2952_poweroff_handler - Interrupt handler
141 * Triggered each time the trigger signal changes state and (de)activates a
142 * time-out (timer_trigger). Once the time-out is actually reached the shut
146 * @dev_id: pointer to the main data structure
148 static irqreturn_t
ltc2952_poweroff_handler(int irq
, void *dev_id
)
150 struct ltc2952_poweroff
*data
= dev_id
;
152 if (data
->kernel_panic
|| hrtimer_active(&data
->timer_wde
)) {
153 /* shutdown is already triggered, nothing to do any more */
157 if (gpiod_get_value(data
->gpio_trigger
)) {
158 hrtimer_start(&data
->timer_trigger
, data
->trigger_delay
,
161 hrtimer_cancel(&data
->timer_trigger
);
166 static void ltc2952_poweroff_kill(void)
168 gpiod_set_value(ltc2952_data
->gpio_kill
, 1);
171 static void ltc2952_poweroff_default(struct ltc2952_poweroff
*data
)
173 data
->wde_interval
= 300L * 1E6L
;
174 data
->trigger_delay
= ktime_set(2, 500L*1E6L
);
176 hrtimer_init(&data
->timer_trigger
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
177 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
;
183 static int ltc2952_poweroff_init(struct platform_device
*pdev
)
186 struct ltc2952_poweroff
*data
= platform_get_drvdata(pdev
);
188 ltc2952_poweroff_default(data
);
190 data
->gpio_watchdog
= devm_gpiod_get(&pdev
->dev
, "watchdog",
192 if (IS_ERR(data
->gpio_watchdog
)) {
193 ret
= PTR_ERR(data
->gpio_watchdog
);
194 dev_err(&pdev
->dev
, "unable to claim gpio \"watchdog\"\n");
198 data
->gpio_kill
= devm_gpiod_get(&pdev
->dev
, "kill", GPIOD_OUT_LOW
);
199 if (IS_ERR(data
->gpio_kill
)) {
200 ret
= PTR_ERR(data
->gpio_kill
);
201 dev_err(&pdev
->dev
, "unable to claim gpio \"kill\"\n");
205 data
->gpio_trigger
= devm_gpiod_get_optional(&pdev
->dev
, "trigger",
207 if (IS_ERR(data
->gpio_trigger
)) {
209 * It's not a problem if the trigger gpio isn't available, but
210 * it is worth a warning if its use was defined in the device
213 dev_err(&pdev
->dev
, "unable to claim gpio \"trigger\"\n");
214 data
->gpio_trigger
= NULL
;
217 if (devm_request_irq(&pdev
->dev
, gpiod_to_irq(data
->gpio_trigger
),
218 ltc2952_poweroff_handler
,
219 (IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
),
223 * Some things may have happened:
224 * - No trigger input was defined
225 * - Claiming the GPIO failed
226 * - We could not map to an IRQ
227 * - We couldn't register an interrupt handler
229 * None of these really are problems, but all of them
230 * disqualify the push button from controlling the power.
232 * It is therefore important to note that if the ltc2952
233 * detects a button press for long enough, it will still start
234 * its own powerdown window and cut the power on us if we don't
235 * start the watchdog trigger.
237 if (data
->gpio_trigger
) {
239 "unable to configure the trigger interrupt\n");
240 devm_gpiod_put(&pdev
->dev
, data
->gpio_trigger
);
241 data
->gpio_trigger
= NULL
;
244 "power down trigger input will not be used\n");
245 ltc2952_poweroff_start_wde(data
);
251 static int ltc2952_poweroff_notify_panic(struct notifier_block
*nb
,
252 unsigned long code
, void *unused
)
254 struct ltc2952_poweroff
*data
= to_ltc2952(nb
, panic_notifier
);
256 data
->kernel_panic
= true;
260 static int ltc2952_poweroff_probe(struct platform_device
*pdev
)
263 struct ltc2952_poweroff
*data
;
266 dev_err(&pdev
->dev
, "pm_power_off already registered");
270 data
= devm_kzalloc(&pdev
->dev
, sizeof(*data
), GFP_KERNEL
);
274 data
->dev
= &pdev
->dev
;
275 platform_set_drvdata(pdev
, data
);
277 ret
= ltc2952_poweroff_init(pdev
);
281 /* TODO: remove ltc2952_data */
283 pm_power_off
= ltc2952_poweroff_kill
;
285 data
->panic_notifier
.notifier_call
= ltc2952_poweroff_notify_panic
;
286 atomic_notifier_chain_register(&panic_notifier_list
,
287 &data
->panic_notifier
);
288 dev_info(&pdev
->dev
, "probe successful\n");
293 static int ltc2952_poweroff_remove(struct platform_device
*pdev
)
295 struct ltc2952_poweroff
*data
= platform_get_drvdata(pdev
);
298 hrtimer_cancel(&data
->timer_trigger
);
299 hrtimer_cancel(&data
->timer_wde
);
300 atomic_notifier_chain_unregister(&panic_notifier_list
,
301 &data
->panic_notifier
);
305 static const struct of_device_id of_ltc2952_poweroff_match
[] = {
306 { .compatible
= "lltc,ltc2952"},
309 MODULE_DEVICE_TABLE(of
, of_ltc2952_poweroff_match
);
311 static struct platform_driver ltc2952_poweroff_driver
= {
312 .probe
= ltc2952_poweroff_probe
,
313 .remove
= ltc2952_poweroff_remove
,
315 .name
= "ltc2952-poweroff",
316 .of_match_table
= of_ltc2952_poweroff_match
,
320 module_platform_driver(ltc2952_poweroff_driver
);
322 MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
323 MODULE_DESCRIPTION("LTC PowerPath power-off driver");
324 MODULE_LICENSE("GPL v2");