1 /*
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 
66 struct ltc2952_poweroff {
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 	struct gpio_desc *gpio_trigger;
76 	struct gpio_desc *gpio_watchdog;
77 	struct gpio_desc *gpio_kill;
78 
79 	bool kernel_panic;
80 	struct notifier_block panic_notifier;
81 };
82 
83 #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
84 
85 /*
86  * This global variable is only needed for pm_power_off. We should
87  * remove it entirely once we don't need the global state anymore.
88  */
89 static struct ltc2952_poweroff *ltc2952_data;
90 
91 /**
92  * ltc2952_poweroff_timer_wde - Timer callback
93  * Toggles the watchdog reset signal each wde_interval
94  *
95  * @timer: corresponding timer
96  *
97  * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
98  * machine actually shuts down
99  */
100 static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
101 {
102 	ktime_t now;
103 	int state;
104 	unsigned long overruns;
105 	struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
106 
107 	if (data->kernel_panic)
108 		return HRTIMER_NORESTART;
109 
110 	state = gpiod_get_value(data->gpio_watchdog);
111 	gpiod_set_value(data->gpio_watchdog, !state);
112 
113 	now = hrtimer_cb_get_time(timer);
114 	overruns = hrtimer_forward(timer, now, data->wde_interval);
115 
116 	return HRTIMER_RESTART;
117 }
118 
119 static enum hrtimer_restart
120 ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
121 {
122 	struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
123 	int ret = hrtimer_start(&data->timer_wde,
124 				data->wde_interval, HRTIMER_MODE_REL);
125 
126 	if (ret) {
127 		dev_err(data->dev, "unable to start the timer\n");
128 		/*
129 		 * The device will not toggle the watchdog reset,
130 		 * thus shut down is only safe if the PowerPath controller
131 		 * has a long enough time-off before triggering a hardware
132 		 * power-off.
133 		 *
134 		 * Only sending a warning as the system will power-off anyway
135 		 */
136 	}
137 
138 	dev_info(data->dev, "executing shutdown\n");
139 
140 	orderly_poweroff(true);
141 
142 	return HRTIMER_NORESTART;
143 }
144 
145 /**
146  * ltc2952_poweroff_handler - Interrupt handler
147  * Triggered each time the trigger signal changes state and (de)activates a
148  * time-out (timer_trigger). Once the time-out is actually reached the shut
149  * down is executed.
150  *
151  * @irq: IRQ number
152  * @dev_id: pointer to the main data structure
153  */
154 static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
155 {
156 	struct ltc2952_poweroff *data = dev_id;
157 
158 	if (data->kernel_panic || hrtimer_active(&data->timer_wde)) {
159 		/* shutdown is already triggered, nothing to do any more */
160 		return IRQ_HANDLED;
161 	}
162 
163 	if (!hrtimer_active(&data->timer_trigger)) {
164 		if (hrtimer_start(&data->timer_trigger, data->trigger_delay,
165 				  HRTIMER_MODE_REL))
166 			dev_err(data->dev, "unable to start the wait timer\n");
167 	} else {
168 		hrtimer_cancel(&data->timer_trigger);
169 		/* omitting return value check, timer should have been valid */
170 	}
171 	return IRQ_HANDLED;
172 }
173 
174 static void ltc2952_poweroff_kill(void)
175 {
176 	gpiod_set_value(ltc2952_data->gpio_kill, 1);
177 }
178 
179 static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
180 {
181 	data->wde_interval = ktime_set(0, 300L*1E6L);
182 	data->trigger_delay = ktime_set(2, 500L*1E6L);
183 
184 	hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
185 	data->timer_trigger.function = ltc2952_poweroff_timer_trigger;
186 
187 	hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
188 	data->timer_wde.function = ltc2952_poweroff_timer_wde;
189 }
190 
191 static int ltc2952_poweroff_init(struct platform_device *pdev)
192 {
193 	int ret, virq;
194 	struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
195 
196 	ltc2952_poweroff_default(data);
197 
198 	data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
199 					     GPIOD_OUT_LOW);
200 	if (IS_ERR(data->gpio_watchdog)) {
201 		ret = PTR_ERR(data->gpio_watchdog);
202 		dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
203 		return ret;
204 	}
205 
206 	data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
207 	if (IS_ERR(data->gpio_kill)) {
208 		ret = PTR_ERR(data->gpio_kill);
209 		dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
210 		return ret;
211 	}
212 
213 	data->gpio_trigger = devm_gpiod_get(&pdev->dev, "trigger",
214 					    GPIOD_IN);
215 	if (IS_ERR(ltc2952_data->gpio_trigger)) {
216 		ret = PTR_ERR(ltc2952_data->gpio_trigger);
217 		dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
218 		return ret;
219 	}
220 
221 	virq = gpiod_to_irq(data->gpio_trigger);
222 	if (virq < 0) {
223 		dev_err(&pdev->dev, "cannot map GPIO as interrupt");
224 		return ret;
225 	}
226 
227 	ret = devm_request_irq(&pdev->dev, virq,
228 			       ltc2952_poweroff_handler,
229 			       (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
230 			       "ltc2952-poweroff",
231 			       data);
232 
233 	if (ret) {
234 		dev_err(&pdev->dev, "cannot configure an interrupt handler\n");
235 		return ret;
236 	}
237 
238 	return 0;
239 }
240 
241 static 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 
250 static int ltc2952_poweroff_probe(struct platform_device *pdev)
251 {
252 	int ret;
253 	struct ltc2952_poweroff *data;
254 
255 	if (pm_power_off) {
256 		dev_err(&pdev->dev, "pm_power_off already registered");
257 		return -EBUSY;
258 	}
259 
260 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
261 	if (!data)
262 		return -ENOMEM;
263 
264 	data->dev = &pdev->dev;
265 	platform_set_drvdata(pdev, data);
266 
267 	ret = ltc2952_poweroff_init(pdev);
268 	if (ret)
269 		return ret;
270 
271 	/* TODO: remove ltc2952_data */
272 	ltc2952_data = data;
273 	pm_power_off = ltc2952_poweroff_kill;
274 
275 	data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
276 	atomic_notifier_chain_register(&panic_notifier_list,
277 				       &data->panic_notifier);
278 	dev_info(&pdev->dev, "probe successful\n");
279 
280 	return 0;
281 }
282 
283 static int ltc2952_poweroff_remove(struct platform_device *pdev)
284 {
285 	struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
286 
287 	pm_power_off = NULL;
288 	atomic_notifier_chain_unregister(&panic_notifier_list,
289 					 &data->panic_notifier);
290 	return 0;
291 }
292 
293 static const struct of_device_id of_ltc2952_poweroff_match[] = {
294 	{ .compatible = "lltc,ltc2952"},
295 	{},
296 };
297 MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
298 
299 static struct platform_driver ltc2952_poweroff_driver = {
300 	.probe = ltc2952_poweroff_probe,
301 	.remove = ltc2952_poweroff_remove,
302 	.driver = {
303 		.name = "ltc2952-poweroff",
304 		.of_match_table = of_ltc2952_poweroff_match,
305 	},
306 };
307 
308 module_platform_driver(ltc2952_poweroff_driver);
309 
310 MODULE_AUTHOR("Ren� Moll <rene.moll@xsens.com>");
311 MODULE_DESCRIPTION("LTC PowerPath power-off driver");
312 MODULE_LICENSE("GPL v2");
313