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_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 	struct gpio_desc *gpio_trigger;
76 	struct gpio_desc *gpio_watchdog;
77 	struct gpio_desc *gpio_kill;
78 };
79 
80 static int ltc2952_poweroff_panic;
81 static struct ltc2952_poweroff_data *ltc2952_data;
82 
83 /**
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  */
92 static 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 
101 	state = gpiod_get_value(ltc2952_data->gpio_watchdog);
102 	gpiod_set_value(ltc2952_data->gpio_watchdog, !state);
103 
104 	now = hrtimer_cb_get_time(timer);
105 	overruns = hrtimer_forward(timer, now, ltc2952_data->wde_interval);
106 
107 	return HRTIMER_RESTART;
108 }
109 
110 static enum hrtimer_restart ltc2952_poweroff_timer_trigger(
111 	struct hrtimer *timer)
112 {
113 	int ret;
114 
115 	ret = hrtimer_start(&ltc2952_data->timer_wde,
116 			    ltc2952_data->wde_interval, HRTIMER_MODE_REL);
117 
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  */
146 static 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 
170 irq_ok:
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 int ltc2952_poweroff_suspend(struct platform_device *pdev,
180 	pm_message_t state)
181 {
182 	return -ENOSYS;
183 }
184 
185 static int ltc2952_poweroff_resume(struct platform_device *pdev)
186 {
187 	return -ENOSYS;
188 }
189 
190 static void ltc2952_poweroff_default(struct ltc2952_poweroff_data *data)
191 {
192 	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 
202 static int ltc2952_poweroff_init(struct platform_device *pdev)
203 {
204 	int ret, virq;
205 	struct ltc2952_poweroff_data *data;
206 
207 	data = ltc2952_data;
208 	ltc2952_poweroff_default(ltc2952_data);
209 
210 	ltc2952_data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
211 						     GPIOD_OUT_LOW);
212 	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 	}
217 
218 	ltc2952_data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill",
219 						 GPIOD_OUT_LOW);
220 	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");
223 		return ret;
224 	}
225 
226 	ltc2952_data->gpio_trigger = devm_gpiod_get(&pdev->dev, "trigger",
227 						    GPIOD_IN);
228 	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");
231 		return ret;
232 	}
233 
234 	virq = gpiod_to_irq(ltc2952_data->gpio_trigger);
235 	if (virq < 0) {
236 		dev_err(&pdev->dev, "cannot map GPIO as interrupt");
237 		return ret;
238 	}
239 
240 	ret = devm_request_irq(&pdev->dev, virq,
241 			       ltc2952_poweroff_handler,
242 			       (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
243 			       "ltc2952-poweroff",
244 			       ltc2952_data);
245 
246 	if (ret) {
247 		dev_err(&pdev->dev, "cannot configure an interrupt handler\n");
248 		return ret;
249 	}
250 
251 	return 0;
252 }
253 
254 static 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 
263 	ltc2952_data = devm_kzalloc(&pdev->dev, sizeof(*ltc2952_data),
264 				    GFP_KERNEL);
265 	if (!ltc2952_data)
266 		return -ENOMEM;
267 
268 	ltc2952_data->dev = &pdev->dev;
269 
270 	ret = ltc2952_poweroff_init(pdev);
271 	if (ret)
272 		return ret;
273 
274 	pm_power_off = &ltc2952_poweroff_kill;
275 
276 	dev_info(&pdev->dev, "probe successful\n");
277 
278 	return 0;
279 }
280 
281 static int ltc2952_poweroff_remove(struct platform_device *pdev)
282 {
283 	pm_power_off = NULL;
284 
285 	return 0;
286 }
287 
288 static const struct of_device_id of_ltc2952_poweroff_match[] = {
289 	{ .compatible = "lltc,ltc2952"},
290 	{},
291 };
292 MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
293 
294 static struct platform_driver ltc2952_poweroff_driver = {
295 	.probe = ltc2952_poweroff_probe,
296 	.remove = ltc2952_poweroff_remove,
297 	.driver = {
298 		.name = "ltc2952-poweroff",
299 		.of_match_table = of_ltc2952_poweroff_match,
300 	},
301 	.suspend = ltc2952_poweroff_suspend,
302 	.resume = ltc2952_poweroff_resume,
303 };
304 
305 static 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 
312 static struct notifier_block ltc2952_poweroff_panic_nb = {
313 	.notifier_call = ltc2952_poweroff_notify_panic,
314 };
315 
316 static 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 
326 static 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 
334 module_init(ltc2952_poweroff_platform_init);
335 module_exit(ltc2952_poweroff_platform_exit);
336 
337 MODULE_AUTHOR("Ren� Moll <rene.moll@xsens.com>");
338 MODULE_DESCRIPTION("LTC PowerPath power-off driver");
339 MODULE_LICENSE("GPL v2");
340