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
38 * Once a shut down is triggered, the driver will toggle this signal,
39 * with an internal (wde_interval) to stall the hardware shut down.
42 * The last action during shut down is triggering this signalling, such
43 * that the PowerPath Control will power down the hardware.
45 * ----------------------------------------
47 * ----------------------------------------
49 * The driver requires a non-shared, edge-triggered interrupt on the trigger
54 #include <linux/kernel.h>
55 #include <linux/init.h>
56 #include <linux/interrupt.h>
57 #include <linux/device.h>
58 #include <linux/platform_device.h>
59 #include <linux/ktime.h>
60 #include <linux/slab.h>
61 #include <linux/kmod.h>
62 #include <linux/module.h>
63 #include <linux/gpio/consumer.h>
64 #include <linux/reboot.h>
66 struct ltc2952_poweroff_data
{
67 struct hrtimer timer_trigger
;
68 struct hrtimer timer_wde
;
70 ktime_t trigger_delay
;
82 struct gpio_desc
*gpio
[3];
85 static int ltc2952_poweroff_panic
;
86 static struct ltc2952_poweroff_data
*ltc2952_data
;
88 #define POWERPATH_IO_TRIGGER 0
89 #define POWERPATH_IO_WATCHDOG 1
90 #define POWERPATH_IO_KILL 2
93 * ltc2952_poweroff_timer_wde - Timer callback
94 * Toggles the watchdog reset signal each wde_interval
96 * @timer: corresponding timer
98 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
99 * machine actually shuts down
101 static enum hrtimer_restart
ltc2952_poweroff_timer_wde(struct hrtimer
*timer
)
105 unsigned long overruns
;
107 if (ltc2952_poweroff_panic
)
108 return HRTIMER_NORESTART
;
110 state
= gpiod_get_value(ltc2952_data
->gpio
[POWERPATH_IO_WATCHDOG
]);
111 gpiod_set_value(ltc2952_data
->gpio
[POWERPATH_IO_WATCHDOG
], !state
);
113 now
= hrtimer_cb_get_time(timer
);
114 overruns
= hrtimer_forward(timer
, now
, ltc2952_data
->wde_interval
);
116 return HRTIMER_RESTART
;
119 static enum hrtimer_restart
ltc2952_poweroff_timer_trigger(
120 struct hrtimer
*timer
)
124 ret
= hrtimer_start(<c2952_data
->timer_wde
,
125 ltc2952_data
->wde_interval
, HRTIMER_MODE_REL
);
128 dev_err(ltc2952_data
->dev
, "unable to start the timer\n");
130 * The device will not toggle the watchdog reset,
131 * thus shut down is only safe if the PowerPath controller
132 * has a long enough time-off before triggering a hardware
135 * Only sending a warning as the system will power-off anyway
139 dev_info(ltc2952_data
->dev
, "executing shutdown\n");
141 orderly_poweroff(true);
143 return HRTIMER_NORESTART
;
147 * ltc2952_poweroff_handler - Interrupt handler
148 * Triggered each time the trigger signal changes state and (de)activates a
149 * time-out (timer_trigger). Once the time-out is actually reached the shut
153 * @dev_id: pointer to the main data structure
155 static irqreturn_t
ltc2952_poweroff_handler(int irq
, void *dev_id
)
158 struct ltc2952_poweroff_data
*data
= dev_id
;
160 if (ltc2952_poweroff_panic
)
163 if (hrtimer_active(&data
->timer_wde
)) {
164 /* shutdown is already triggered, nothing to do any more */
168 if (!hrtimer_active(&data
->timer_trigger
)) {
169 ret
= hrtimer_start(&data
->timer_trigger
, data
->trigger_delay
,
173 dev_err(data
->dev
, "unable to start the wait timer\n");
175 ret
= hrtimer_cancel(&data
->timer_trigger
);
176 /* omitting return value check, timer should have been valid */
183 static void ltc2952_poweroff_kill(void)
185 gpiod_set_value(ltc2952_data
->gpio
[POWERPATH_IO_KILL
], 1);
188 static int ltc2952_poweroff_suspend(struct platform_device
*pdev
,
194 static int ltc2952_poweroff_resume(struct platform_device
*pdev
)
199 static void ltc2952_poweroff_default(struct ltc2952_poweroff_data
*data
)
203 for (i
= 0; i
< ARRAY_SIZE(data
->gpio
); i
++)
204 data
->gpio
[i
] = NULL
;
206 data
->wde_interval
= ktime_set(0, 300L*1E6L
);
207 data
->trigger_delay
= ktime_set(2, 500L*1E6L
);
209 hrtimer_init(&data
->timer_trigger
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
210 data
->timer_trigger
.function
= <c2952_poweroff_timer_trigger
;
212 hrtimer_init(&data
->timer_wde
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
213 data
->timer_wde
.function
= <c2952_poweroff_timer_wde
;
216 static int ltc2952_poweroff_init(struct platform_device
*pdev
)
220 struct ltc2952_poweroff_data
*data
;
222 static char *name
[] = {
230 ltc2952_poweroff_default(ltc2952_data
);
232 for (i
= 0; i
< ARRAY_SIZE(ltc2952_data
->gpio
); i
++) {
233 ltc2952_data
->gpio
[i
] = gpiod_get(&pdev
->dev
, name
[i
]);
235 if (IS_ERR(ltc2952_data
->gpio
[i
])) {
236 ret
= PTR_ERR(ltc2952_data
->gpio
[i
]);
238 "unable to claim the following gpio: %s\n",
244 ret
= gpiod_direction_output(
245 ltc2952_data
->gpio
[POWERPATH_IO_WATCHDOG
], 0);
247 dev_err(&pdev
->dev
, "unable to use watchdog-gpio as output\n");
251 ret
= gpiod_direction_output(ltc2952_data
->gpio
[POWERPATH_IO_KILL
], 0);
253 dev_err(&pdev
->dev
, "unable to use kill-gpio as output\n");
257 virq
= gpiod_to_irq(ltc2952_data
->gpio
[POWERPATH_IO_TRIGGER
]);
259 dev_err(&pdev
->dev
, "cannot map GPIO as interrupt");
263 ltc2952_data
->virq
= virq
;
264 ret
= request_irq(virq
,
265 ltc2952_poweroff_handler
,
266 (IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
),
272 dev_err(&pdev
->dev
, "cannot configure an interrupt handler\n");
279 for (i
= 0; i
< ARRAY_SIZE(ltc2952_data
->gpio
); i
++)
280 if (ltc2952_data
->gpio
[i
])
281 gpiod_put(ltc2952_data
->gpio
[i
]);
286 static int ltc2952_poweroff_probe(struct platform_device
*pdev
)
291 dev_err(&pdev
->dev
, "pm_power_off already registered");
295 ltc2952_data
= kzalloc(sizeof(*ltc2952_data
), GFP_KERNEL
);
299 ltc2952_data
->dev
= &pdev
->dev
;
301 ret
= ltc2952_poweroff_init(pdev
);
305 pm_power_off
= <c2952_poweroff_kill
;
307 dev_info(&pdev
->dev
, "probe successful\n");
316 static int ltc2952_poweroff_remove(struct platform_device
*pdev
)
323 free_irq(ltc2952_data
->virq
, ltc2952_data
);
325 for (i
= 0; i
< ARRAY_SIZE(ltc2952_data
->gpio
); i
++)
326 gpiod_put(ltc2952_data
->gpio
[i
]);
334 static const struct of_device_id of_ltc2952_poweroff_match
[] = {
335 { .compatible
= "lltc,ltc2952"},
338 MODULE_DEVICE_TABLE(of
, of_ltc2952_poweroff_match
);
340 static struct platform_driver ltc2952_poweroff_driver
= {
341 .probe
= ltc2952_poweroff_probe
,
342 .remove
= ltc2952_poweroff_remove
,
344 .name
= "ltc2952-poweroff",
345 .of_match_table
= of_ltc2952_poweroff_match
,
347 .suspend
= ltc2952_poweroff_suspend
,
348 .resume
= ltc2952_poweroff_resume
,
351 static int ltc2952_poweroff_notify_panic(struct notifier_block
*nb
,
352 unsigned long code
, void *unused
)
354 ltc2952_poweroff_panic
= 1;
358 static struct notifier_block ltc2952_poweroff_panic_nb
= {
359 .notifier_call
= ltc2952_poweroff_notify_panic
,
362 static int __init
ltc2952_poweroff_platform_init(void)
364 ltc2952_poweroff_panic
= 0;
366 atomic_notifier_chain_register(&panic_notifier_list
,
367 <c2952_poweroff_panic_nb
);
369 return platform_driver_register(<c2952_poweroff_driver
);
372 static void __exit
ltc2952_poweroff_platform_exit(void)
374 atomic_notifier_chain_unregister(&panic_notifier_list
,
375 <c2952_poweroff_panic_nb
);
377 platform_driver_unregister(<c2952_poweroff_driver
);
380 module_init(ltc2952_poweroff_platform_init
);
381 module_exit(ltc2952_poweroff_platform_exit
);
383 MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
384 MODULE_DESCRIPTION("LTC PowerPath power-off driver");
385 MODULE_LICENSE("GPL v2");