blob: 6487b991ed458a702efa27c648f0369441e13e2e [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
75 unsigned int virq;
76
77 /**
78 * 0: trigger
79 * 1: watchdog
80 * 2: kill
81 */
82 struct gpio_desc *gpio[3];
83};
84
85static int ltc2952_poweroff_panic;
86static struct ltc2952_poweroff_data *ltc2952_data;
87
88#define POWERPATH_IO_TRIGGER 0
89#define POWERPATH_IO_WATCHDOG 1
90#define POWERPATH_IO_KILL 2
91
92/**
93 * ltc2952_poweroff_timer_wde - Timer callback
94 * Toggles the watchdog reset signal each wde_interval
95 *
96 * @timer: corresponding timer
97 *
98 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
99 * machine actually shuts down
100 */
101static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
102{
103 ktime_t now;
104 int state;
105 unsigned long overruns;
106
107 if (ltc2952_poweroff_panic)
108 return HRTIMER_NORESTART;
109
110 state = gpiod_get_value(ltc2952_data->gpio[POWERPATH_IO_WATCHDOG]);
111 gpiod_set_value(ltc2952_data->gpio[POWERPATH_IO_WATCHDOG], !state);
112
113 now = hrtimer_cb_get_time(timer);
114 overruns = hrtimer_forward(timer, now, ltc2952_data->wde_interval);
115
116 return HRTIMER_RESTART;
117}
118
119static enum hrtimer_restart ltc2952_poweroff_timer_trigger(
120 struct hrtimer *timer)
121{
122 int ret;
123
124 ret = hrtimer_start(&ltc2952_data->timer_wde,
125 ltc2952_data->wde_interval, HRTIMER_MODE_REL);
126
127 if (ret) {
128 dev_err(ltc2952_data->dev, "unable to start the timer\n");
129 /*
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
133 * power-off.
134 *
135 * Only sending a warning as the system will power-off anyway
136 */
137 }
138
139 dev_info(ltc2952_data->dev, "executing shutdown\n");
140
141 orderly_poweroff(true);
142
143 return HRTIMER_NORESTART;
144}
145
146/**
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
150 * down is executed.
151 *
152 * @irq: IRQ number
153 * @dev_id: pointer to the main data structure
154 */
155static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
156{
157 int ret;
158 struct ltc2952_poweroff_data *data = dev_id;
159
160 if (ltc2952_poweroff_panic)
161 goto irq_ok;
162
163 if (hrtimer_active(&data->timer_wde)) {
164 /* shutdown is already triggered, nothing to do any more */
165 goto irq_ok;
166 }
167
168 if (!hrtimer_active(&data->timer_trigger)) {
169 ret = hrtimer_start(&data->timer_trigger, data->trigger_delay,
170 HRTIMER_MODE_REL);
171
172 if (ret)
173 dev_err(data->dev, "unable to start the wait timer\n");
174 } else {
175 ret = hrtimer_cancel(&data->timer_trigger);
176 /* omitting return value check, timer should have been valid */
177 }
178
179irq_ok:
180 return IRQ_HANDLED;
181}
182
183static void ltc2952_poweroff_kill(void)
184{
185 gpiod_set_value(ltc2952_data->gpio[POWERPATH_IO_KILL], 1);
186}
187
188static int ltc2952_poweroff_suspend(struct platform_device *pdev,
189 pm_message_t state)
190{
191 return -ENOSYS;
192}
193
194static int ltc2952_poweroff_resume(struct platform_device *pdev)
195{
196 return -ENOSYS;
197}
198
199static void ltc2952_poweroff_default(struct ltc2952_poweroff_data *data)
200{
201 unsigned int i;
202
203 for (i = 0; i < ARRAY_SIZE(data->gpio); i++)
204 data->gpio[i] = NULL;
205
206 data->wde_interval = ktime_set(0, 300L*1E6L);
207 data->trigger_delay = ktime_set(2, 500L*1E6L);
208
209 hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
210 data->timer_trigger.function = &ltc2952_poweroff_timer_trigger;
211
212 hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
213 data->timer_wde.function = &ltc2952_poweroff_timer_wde;
214}
215
216static int ltc2952_poweroff_init(struct platform_device *pdev)
217{
218 int ret, virq;
219 unsigned int i;
220 struct ltc2952_poweroff_data *data;
221
222 static char *name[] = {
223 "trigger",
224 "watchdog",
225 "kill",
226 NULL
227 };
228
229 data = ltc2952_data;
230 ltc2952_poweroff_default(ltc2952_data);
231
232 for (i = 0; i < ARRAY_SIZE(ltc2952_data->gpio); i++) {
233 ltc2952_data->gpio[i] = gpiod_get(&pdev->dev, name[i]);
234
235 if (IS_ERR(ltc2952_data->gpio[i])) {
236 ret = PTR_ERR(ltc2952_data->gpio[i]);
237 dev_err(&pdev->dev,
238 "unable to claim the following gpio: %s\n",
239 name[i]);
240 goto err_io;
241 }
242 }
243
244 ret = gpiod_direction_output(
245 ltc2952_data->gpio[POWERPATH_IO_WATCHDOG], 0);
246 if (ret) {
247 dev_err(&pdev->dev, "unable to use watchdog-gpio as output\n");
248 goto err_io;
249 }
250
251 ret = gpiod_direction_output(ltc2952_data->gpio[POWERPATH_IO_KILL], 0);
252 if (ret) {
253 dev_err(&pdev->dev, "unable to use kill-gpio as output\n");
254 goto err_io;
255 }
256
257 virq = gpiod_to_irq(ltc2952_data->gpio[POWERPATH_IO_TRIGGER]);
258 if (virq < 0) {
259 dev_err(&pdev->dev, "cannot map GPIO as interrupt");
260 goto err_io;
261 }
262
263 ltc2952_data->virq = virq;
264 ret = request_irq(virq,
265 ltc2952_poweroff_handler,
266 (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
267 "ltc2952-poweroff",
268 ltc2952_data
269 );
270
271 if (ret) {
272 dev_err(&pdev->dev, "cannot configure an interrupt handler\n");
273 goto err_io;
274 }
275
276 return 0;
277
278err_io:
279 for (i = 0; i < ARRAY_SIZE(ltc2952_data->gpio); i++)
280 if (ltc2952_data->gpio[i])
281 gpiod_put(ltc2952_data->gpio[i]);
282
283 return ret;
284}
285
286static int ltc2952_poweroff_probe(struct platform_device *pdev)
287{
288 int ret;
289
290 if (pm_power_off) {
291 dev_err(&pdev->dev, "pm_power_off already registered");
292 return -EBUSY;
293 }
294
Frans Klaver0a5c6a22015-01-14 09:15:34 +0100295 ltc2952_data = devm_kzalloc(&pdev->dev, sizeof(*ltc2952_data),
296 GFP_KERNEL);
René Moll66471562014-08-08 13:12:17 +0000297 if (!ltc2952_data)
298 return -ENOMEM;
299
300 ltc2952_data->dev = &pdev->dev;
301
302 ret = ltc2952_poweroff_init(pdev);
303 if (ret)
Frans Klaver0a5c6a22015-01-14 09:15:34 +0100304 return ret;
René Moll66471562014-08-08 13:12:17 +0000305
306 pm_power_off = &ltc2952_poweroff_kill;
307
308 dev_info(&pdev->dev, "probe successful\n");
309
310 return 0;
René Moll66471562014-08-08 13:12:17 +0000311}
312
313static int ltc2952_poweroff_remove(struct platform_device *pdev)
314{
315 unsigned int i;
316
317 pm_power_off = NULL;
318
319 if (ltc2952_data) {
320 free_irq(ltc2952_data->virq, ltc2952_data);
321
322 for (i = 0; i < ARRAY_SIZE(ltc2952_data->gpio); i++)
323 gpiod_put(ltc2952_data->gpio[i]);
René Moll66471562014-08-08 13:12:17 +0000324 }
325
326 return 0;
327}
328
329static const struct of_device_id of_ltc2952_poweroff_match[] = {
330 { .compatible = "lltc,ltc2952"},
331 {},
332};
333MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
334
335static struct platform_driver ltc2952_poweroff_driver = {
336 .probe = ltc2952_poweroff_probe,
337 .remove = ltc2952_poweroff_remove,
338 .driver = {
339 .name = "ltc2952-poweroff",
René Moll66471562014-08-08 13:12:17 +0000340 .of_match_table = of_ltc2952_poweroff_match,
341 },
342 .suspend = ltc2952_poweroff_suspend,
343 .resume = ltc2952_poweroff_resume,
344};
345
346static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
347 unsigned long code, void *unused)
348{
349 ltc2952_poweroff_panic = 1;
350 return NOTIFY_DONE;
351}
352
353static struct notifier_block ltc2952_poweroff_panic_nb = {
354 .notifier_call = ltc2952_poweroff_notify_panic,
355};
356
357static int __init ltc2952_poweroff_platform_init(void)
358{
359 ltc2952_poweroff_panic = 0;
360
361 atomic_notifier_chain_register(&panic_notifier_list,
362 &ltc2952_poweroff_panic_nb);
363
364 return platform_driver_register(&ltc2952_poweroff_driver);
365}
366
367static void __exit ltc2952_poweroff_platform_exit(void)
368{
369 atomic_notifier_chain_unregister(&panic_notifier_list,
370 &ltc2952_poweroff_panic_nb);
371
372 platform_driver_unregister(&ltc2952_poweroff_driver);
373}
374
375module_init(ltc2952_poweroff_platform_init);
376module_exit(ltc2952_poweroff_platform_exit);
377
378MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
379MODULE_DESCRIPTION("LTC PowerPath power-off driver");
380MODULE_LICENSE("GPL v2");