blob: 4caa9d285026a8ba7cf8e9ae7f235448110a0594 [file] [log] [blame]
René Moll66471562014-08-08 13:12:17 +00001/*
2 * LTC2952 (PowerPath) driver
3 *
4 * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com>
5 * Maintainer: René Moll <linux@r-moll.nl>
6 *
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.
11 *
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.
16 *
17 * ----------------------------------------
18 * - Description
19 * ----------------------------------------
20 *
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.
24 *
25 * This driver expects a device tree with a ltc2952 entry for pin mapping.
26 *
27 * ----------------------------------------
28 * - GPIO
29 * ----------------------------------------
30 *
31 * The following GPIOs are used:
32 * - trigger (input)
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.
36 *
37 * - watchdog (output)
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.
40 *
41 * - kill (output)
42 * The last action during shut down is triggering this signalling, such
43 * that the PowerPath Control will power down the hardware.
44 *
45 * ----------------------------------------
46 * - Interrupts
47 * ----------------------------------------
48 *
49 * The driver requires a non-shared, edge-triggered interrupt on the trigger
50 * GPIO.
51 *
52 */
53
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>
65
66struct ltc2952_poweroff_data {
67 struct hrtimer timer_trigger;
68 struct hrtimer timer_wde;
69
70 ktime_t trigger_delay;
71 ktime_t wde_interval;
72
73 struct device *dev;
74
Frans Klaverf66472d2015-01-14 09:15:36 +010075 struct gpio_desc *gpio_trigger;
76 struct gpio_desc *gpio_watchdog;
77 struct gpio_desc *gpio_kill;
René Moll66471562014-08-08 13:12:17 +000078};
79
80static int ltc2952_poweroff_panic;
81static struct ltc2952_poweroff_data *ltc2952_data;
82
René Moll66471562014-08-08 13:12:17 +000083/**
84 * ltc2952_poweroff_timer_wde - Timer callback
85 * Toggles the watchdog reset signal each wde_interval
86 *
87 * @timer: corresponding timer
88 *
89 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
90 * machine actually shuts down
91 */
92static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
93{
94 ktime_t now;
95 int state;
96 unsigned long overruns;
97
98 if (ltc2952_poweroff_panic)
99 return HRTIMER_NORESTART;
100
Frans Klaverf66472d2015-01-14 09:15:36 +0100101 state = gpiod_get_value(ltc2952_data->gpio_watchdog);
102 gpiod_set_value(ltc2952_data->gpio_watchdog, !state);
René Moll66471562014-08-08 13:12:17 +0000103
104 now = hrtimer_cb_get_time(timer);
105 overruns = hrtimer_forward(timer, now, ltc2952_data->wde_interval);
106
107 return HRTIMER_RESTART;
108}
109
110static enum hrtimer_restart ltc2952_poweroff_timer_trigger(
111 struct hrtimer *timer)
112{
113 int ret;
114
115 ret = hrtimer_start(&ltc2952_data->timer_wde,
Frans Klaverf66472d2015-01-14 09:15:36 +0100116 ltc2952_data->wde_interval, HRTIMER_MODE_REL);
René Moll66471562014-08-08 13:12:17 +0000117
118 if (ret) {
119 dev_err(ltc2952_data->dev, "unable to start the timer\n");
120 /*
121 * The device will not toggle the watchdog reset,
122 * thus shut down is only safe if the PowerPath controller
123 * has a long enough time-off before triggering a hardware
124 * power-off.
125 *
126 * Only sending a warning as the system will power-off anyway
127 */
128 }
129
130 dev_info(ltc2952_data->dev, "executing shutdown\n");
131
132 orderly_poweroff(true);
133
134 return HRTIMER_NORESTART;
135}
136
137/**
138 * ltc2952_poweroff_handler - Interrupt handler
139 * Triggered each time the trigger signal changes state and (de)activates a
140 * time-out (timer_trigger). Once the time-out is actually reached the shut
141 * down is executed.
142 *
143 * @irq: IRQ number
144 * @dev_id: pointer to the main data structure
145 */
146static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
147{
148 int ret;
149 struct ltc2952_poweroff_data *data = dev_id;
150
151 if (ltc2952_poweroff_panic)
152 goto irq_ok;
153
154 if (hrtimer_active(&data->timer_wde)) {
155 /* shutdown is already triggered, nothing to do any more */
156 goto irq_ok;
157 }
158
159 if (!hrtimer_active(&data->timer_trigger)) {
160 ret = hrtimer_start(&data->timer_trigger, data->trigger_delay,
161 HRTIMER_MODE_REL);
162
163 if (ret)
164 dev_err(data->dev, "unable to start the wait timer\n");
165 } else {
166 ret = hrtimer_cancel(&data->timer_trigger);
167 /* omitting return value check, timer should have been valid */
168 }
169
170irq_ok:
171 return IRQ_HANDLED;
172}
173
174static void ltc2952_poweroff_kill(void)
175{
Frans Klaverf66472d2015-01-14 09:15:36 +0100176 gpiod_set_value(ltc2952_data->gpio_kill, 1);
René Moll66471562014-08-08 13:12:17 +0000177}
178
179static int ltc2952_poweroff_suspend(struct platform_device *pdev,
180 pm_message_t state)
181{
182 return -ENOSYS;
183}
184
185static int ltc2952_poweroff_resume(struct platform_device *pdev)
186{
187 return -ENOSYS;
188}
189
190static void ltc2952_poweroff_default(struct ltc2952_poweroff_data *data)
191{
René Moll66471562014-08-08 13:12:17 +0000192 data->wde_interval = ktime_set(0, 300L*1E6L);
193 data->trigger_delay = ktime_set(2, 500L*1E6L);
194
195 hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
196 data->timer_trigger.function = &ltc2952_poweroff_timer_trigger;
197
198 hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
199 data->timer_wde.function = &ltc2952_poweroff_timer_wde;
200}
201
202static int ltc2952_poweroff_init(struct platform_device *pdev)
203{
204 int ret, virq;
René Moll66471562014-08-08 13:12:17 +0000205 struct ltc2952_poweroff_data *data;
206
René Moll66471562014-08-08 13:12:17 +0000207 data = ltc2952_data;
208 ltc2952_poweroff_default(ltc2952_data);
209
Frans Klaver62113b32015-01-14 09:15:37 +0100210 ltc2952_data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
211 GPIOD_OUT_LOW);
Frans Klaverf66472d2015-01-14 09:15:36 +0100212 if (IS_ERR(ltc2952_data->gpio_watchdog)) {
213 ret = PTR_ERR(ltc2952_data->gpio_watchdog);
214 dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
215 return ret;
216 }
René Moll66471562014-08-08 13:12:17 +0000217
Frans Klaver62113b32015-01-14 09:15:37 +0100218 ltc2952_data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill",
219 GPIOD_OUT_LOW);
Frans Klaverf66472d2015-01-14 09:15:36 +0100220 if (IS_ERR(ltc2952_data->gpio_kill)) {
221 ret = PTR_ERR(ltc2952_data->gpio_kill);
222 dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
Frans Klaver62113b32015-01-14 09:15:37 +0100223 return ret;
Frans Klaverf66472d2015-01-14 09:15:36 +0100224 }
225
Frans Klaver62113b32015-01-14 09:15:37 +0100226 ltc2952_data->gpio_trigger = devm_gpiod_get(&pdev->dev, "trigger",
227 GPIOD_IN);
Frans Klaverf66472d2015-01-14 09:15:36 +0100228 if (IS_ERR(ltc2952_data->gpio_trigger)) {
229 ret = PTR_ERR(ltc2952_data->gpio_trigger);
230 dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
Frans Klaver62113b32015-01-14 09:15:37 +0100231 return ret;
René Moll66471562014-08-08 13:12:17 +0000232 }
233
Frans Klaverf66472d2015-01-14 09:15:36 +0100234 virq = gpiod_to_irq(ltc2952_data->gpio_trigger);
René Moll66471562014-08-08 13:12:17 +0000235 if (virq < 0) {
236 dev_err(&pdev->dev, "cannot map GPIO as interrupt");
Frans Klaver62113b32015-01-14 09:15:37 +0100237 return ret;
René Moll66471562014-08-08 13:12:17 +0000238 }
239
Frans Klaver07d08232015-01-14 09:15:35 +0100240 ret = devm_request_irq(&pdev->dev, virq,
241 ltc2952_poweroff_handler,
242 (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
243 "ltc2952-poweroff",
244 ltc2952_data);
René Moll66471562014-08-08 13:12:17 +0000245
246 if (ret) {
247 dev_err(&pdev->dev, "cannot configure an interrupt handler\n");
Frans Klaver62113b32015-01-14 09:15:37 +0100248 return ret;
René Moll66471562014-08-08 13:12:17 +0000249 }
250
251 return 0;
René Moll66471562014-08-08 13:12:17 +0000252}
253
254static int ltc2952_poweroff_probe(struct platform_device *pdev)
255{
256 int ret;
257
258 if (pm_power_off) {
259 dev_err(&pdev->dev, "pm_power_off already registered");
260 return -EBUSY;
261 }
262
Frans Klaver0a5c6a22015-01-14 09:15:34 +0100263 ltc2952_data = devm_kzalloc(&pdev->dev, sizeof(*ltc2952_data),
264 GFP_KERNEL);
René Moll66471562014-08-08 13:12:17 +0000265 if (!ltc2952_data)
266 return -ENOMEM;
267
268 ltc2952_data->dev = &pdev->dev;
269
270 ret = ltc2952_poweroff_init(pdev);
271 if (ret)
Frans Klaver0a5c6a22015-01-14 09:15:34 +0100272 return ret;
René Moll66471562014-08-08 13:12:17 +0000273
274 pm_power_off = &ltc2952_poweroff_kill;
275
276 dev_info(&pdev->dev, "probe successful\n");
277
278 return 0;
René Moll66471562014-08-08 13:12:17 +0000279}
280
281static int ltc2952_poweroff_remove(struct platform_device *pdev)
282{
René Moll66471562014-08-08 13:12:17 +0000283 pm_power_off = NULL;
284
René Moll66471562014-08-08 13:12:17 +0000285 return 0;
286}
287
288static const struct of_device_id of_ltc2952_poweroff_match[] = {
289 { .compatible = "lltc,ltc2952"},
290 {},
291};
292MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
293
294static struct platform_driver ltc2952_poweroff_driver = {
295 .probe = ltc2952_poweroff_probe,
296 .remove = ltc2952_poweroff_remove,
297 .driver = {
298 .name = "ltc2952-poweroff",
René Moll66471562014-08-08 13:12:17 +0000299 .of_match_table = of_ltc2952_poweroff_match,
300 },
301 .suspend = ltc2952_poweroff_suspend,
302 .resume = ltc2952_poweroff_resume,
303};
304
305static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
306 unsigned long code, void *unused)
307{
308 ltc2952_poweroff_panic = 1;
309 return NOTIFY_DONE;
310}
311
312static struct notifier_block ltc2952_poweroff_panic_nb = {
313 .notifier_call = ltc2952_poweroff_notify_panic,
314};
315
316static int __init ltc2952_poweroff_platform_init(void)
317{
318 ltc2952_poweroff_panic = 0;
319
320 atomic_notifier_chain_register(&panic_notifier_list,
321 &ltc2952_poweroff_panic_nb);
322
323 return platform_driver_register(&ltc2952_poweroff_driver);
324}
325
326static void __exit ltc2952_poweroff_platform_exit(void)
327{
328 atomic_notifier_chain_unregister(&panic_notifier_list,
329 &ltc2952_poweroff_panic_nb);
330
331 platform_driver_unregister(&ltc2952_poweroff_driver);
332}
333
334module_init(ltc2952_poweroff_platform_init);
335module_exit(ltc2952_poweroff_platform_exit);
336
337MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
338MODULE_DESCRIPTION("LTC PowerPath power-off driver");
339MODULE_LICENSE("GPL v2");