1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * LTC2952 (PowerPath) driver
4 *
5 * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com>
6 * Maintainer: René Moll <linux@r-moll.nl>
7 *
8 * ----------------------------------------
9 * - Description
10 * ----------------------------------------
11 *
12 * This driver is to be used with an external PowerPath Controller (LTC2952).
13 * Its function is to determine when a external shut down is triggered
14 * and react by properly shutting down the system.
15 *
16 * This driver expects a device tree with a ltc2952 entry for pin mapping.
17 *
18 * ----------------------------------------
19 * - GPIO
20 * ----------------------------------------
21 *
22 * The following GPIOs are used:
23 * - trigger (input)
24 * A level change indicates the shut-down trigger. If it's state reverts
25 * within the time-out defined by trigger_delay, the shut down is not
26 * executed. If no pin is assigned to this input, the driver will start the
27 * watchdog toggle immediately. The chip will only power off the system if
28 * it is requested to do so through the kill line.
29 *
30 * - watchdog (output)
31 * Once a shut down is triggered, the driver will toggle this signal,
32 * with an internal (wde_interval) to stall the hardware shut down.
33 *
34 * - kill (output)
35 * The last action during shut down is triggering this signalling, such
36 * that the PowerPath Control will power down the hardware.
37 *
38 * ----------------------------------------
39 * - Interrupts
40 * ----------------------------------------
41 *
42 * The driver requires a non-shared, edge-triggered interrupt on the trigger
43 * GPIO.
44 */
45
46 #include <linux/kernel.h>
47 #include <linux/init.h>
48 #include <linux/interrupt.h>
49 #include <linux/device.h>
50 #include <linux/platform_device.h>
51 #include <linux/ktime.h>
52 #include <linux/slab.h>
53 #include <linux/kmod.h>
54 #include <linux/module.h>
55 #include <linux/panic_notifier.h>
56 #include <linux/gpio/consumer.h>
57 #include <linux/reboot.h>
58 #include <linux/property.h>
59
60 struct ltc2952_poweroff {
61 struct hrtimer timer_trigger;
62 struct hrtimer timer_wde;
63
64 ktime_t trigger_delay;
65 ktime_t wde_interval;
66
67 struct device *dev;
68
69 struct gpio_desc *gpio_trigger;
70 struct gpio_desc *gpio_watchdog;
71 struct gpio_desc *gpio_kill;
72
73 bool kernel_panic;
74 struct notifier_block panic_notifier;
75 };
76
77 #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
78
79 /*
80 * This global variable is only needed for pm_power_off. We should
81 * remove it entirely once we don't need the global state anymore.
82 */
83 static struct ltc2952_poweroff *ltc2952_data;
84
85 /**
86 * ltc2952_poweroff_timer_wde - Timer callback
87 * Toggles the watchdog reset signal each wde_interval
88 *
89 * @timer: corresponding timer
90 *
91 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
92 * machine actually shuts down
93 */
ltc2952_poweroff_timer_wde(struct hrtimer * timer)94 static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
95 {
96 int state;
97 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
98
99 if (data->kernel_panic)
100 return HRTIMER_NORESTART;
101
102 state = gpiod_get_value(data->gpio_watchdog);
103 gpiod_set_value(data->gpio_watchdog, !state);
104
105 hrtimer_forward_now(timer, data->wde_interval);
106
107 return HRTIMER_RESTART;
108 }
109
ltc2952_poweroff_start_wde(struct ltc2952_poweroff * data)110 static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff *data)
111 {
112 hrtimer_start(&data->timer_wde, data->wde_interval, HRTIMER_MODE_REL);
113 }
114
115 static enum hrtimer_restart
ltc2952_poweroff_timer_trigger(struct hrtimer * timer)116 ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
117 {
118 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
119
120 ltc2952_poweroff_start_wde(data);
121 dev_info(data->dev, "executing shutdown\n");
122 orderly_poweroff(true);
123
124 return HRTIMER_NORESTART;
125 }
126
127 /**
128 * ltc2952_poweroff_handler - Interrupt handler
129 * Triggered each time the trigger signal changes state and (de)activates a
130 * time-out (timer_trigger). Once the time-out is actually reached the shut
131 * down is executed.
132 *
133 * @irq: IRQ number
134 * @dev_id: pointer to the main data structure
135 */
ltc2952_poweroff_handler(int irq,void * dev_id)136 static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
137 {
138 struct ltc2952_poweroff *data = dev_id;
139
140 if (data->kernel_panic || hrtimer_active(&data->timer_wde)) {
141 /* shutdown is already triggered, nothing to do any more */
142 return IRQ_HANDLED;
143 }
144
145 if (gpiod_get_value(data->gpio_trigger)) {
146 hrtimer_start(&data->timer_trigger, data->trigger_delay,
147 HRTIMER_MODE_REL);
148 } else {
149 hrtimer_cancel(&data->timer_trigger);
150 }
151 return IRQ_HANDLED;
152 }
153
ltc2952_poweroff_kill(void)154 static void ltc2952_poweroff_kill(void)
155 {
156 gpiod_set_value(ltc2952_data->gpio_kill, 1);
157 }
158
ltc2952_poweroff_default(struct ltc2952_poweroff * data)159 static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
160 {
161 data->wde_interval = 300L * NSEC_PER_MSEC;
162 data->trigger_delay = ktime_set(2, 500L * NSEC_PER_MSEC);
163
164 hrtimer_setup(&data->timer_trigger, ltc2952_poweroff_timer_trigger, CLOCK_MONOTONIC,
165 HRTIMER_MODE_REL);
166
167 hrtimer_setup(&data->timer_wde, ltc2952_poweroff_timer_wde, CLOCK_MONOTONIC,
168 HRTIMER_MODE_REL);
169 }
170
ltc2952_poweroff_init(struct platform_device * pdev)171 static int ltc2952_poweroff_init(struct platform_device *pdev)
172 {
173 int ret;
174 u32 trigger_delay_ms;
175 struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
176
177 ltc2952_poweroff_default(data);
178
179 if (!device_property_read_u32(&pdev->dev, "trigger-delay-ms",
180 &trigger_delay_ms)) {
181 data->trigger_delay = ktime_set(trigger_delay_ms / MSEC_PER_SEC,
182 (trigger_delay_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
183 }
184
185 data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
186 GPIOD_OUT_LOW);
187 if (IS_ERR(data->gpio_watchdog)) {
188 ret = PTR_ERR(data->gpio_watchdog);
189 dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
190 return ret;
191 }
192
193 data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
194 if (IS_ERR(data->gpio_kill)) {
195 ret = PTR_ERR(data->gpio_kill);
196 dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
197 return ret;
198 }
199
200 data->gpio_trigger = devm_gpiod_get_optional(&pdev->dev, "trigger",
201 GPIOD_IN);
202 if (IS_ERR(data->gpio_trigger)) {
203 /*
204 * It's not a problem if the trigger gpio isn't available, but
205 * it is worth a warning if its use was defined in the device
206 * tree.
207 */
208 dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
209 data->gpio_trigger = NULL;
210 }
211
212 if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger),
213 ltc2952_poweroff_handler,
214 (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
215 "ltc2952-poweroff",
216 data)) {
217 /*
218 * Some things may have happened:
219 * - No trigger input was defined
220 * - Claiming the GPIO failed
221 * - We could not map to an IRQ
222 * - We couldn't register an interrupt handler
223 *
224 * None of these really are problems, but all of them
225 * disqualify the push button from controlling the power.
226 *
227 * It is therefore important to note that if the ltc2952
228 * detects a button press for long enough, it will still start
229 * its own powerdown window and cut the power on us if we don't
230 * start the watchdog trigger.
231 */
232 if (data->gpio_trigger) {
233 dev_warn(&pdev->dev,
234 "unable to configure the trigger interrupt\n");
235 devm_gpiod_put(&pdev->dev, data->gpio_trigger);
236 data->gpio_trigger = NULL;
237 }
238 dev_info(&pdev->dev,
239 "power down trigger input will not be used\n");
240 ltc2952_poweroff_start_wde(data);
241 }
242
243 return 0;
244 }
245
ltc2952_poweroff_notify_panic(struct notifier_block * nb,unsigned long code,void * unused)246 static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
247 unsigned long code, void *unused)
248 {
249 struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier);
250
251 data->kernel_panic = true;
252 return NOTIFY_DONE;
253 }
254
ltc2952_poweroff_probe(struct platform_device * pdev)255 static int ltc2952_poweroff_probe(struct platform_device *pdev)
256 {
257 int ret;
258 struct ltc2952_poweroff *data;
259
260 if (pm_power_off) {
261 dev_err(&pdev->dev, "pm_power_off already registered");
262 return -EBUSY;
263 }
264
265 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
266 if (!data)
267 return -ENOMEM;
268
269 data->dev = &pdev->dev;
270 platform_set_drvdata(pdev, data);
271
272 ret = ltc2952_poweroff_init(pdev);
273 if (ret)
274 return ret;
275
276 /* TODO: remove ltc2952_data */
277 ltc2952_data = data;
278 pm_power_off = ltc2952_poweroff_kill;
279
280 data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
281 atomic_notifier_chain_register(&panic_notifier_list,
282 &data->panic_notifier);
283 dev_info(&pdev->dev, "probe successful\n");
284
285 return 0;
286 }
287
ltc2952_poweroff_remove(struct platform_device * pdev)288 static void ltc2952_poweroff_remove(struct platform_device *pdev)
289 {
290 struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
291
292 pm_power_off = NULL;
293 hrtimer_cancel(&data->timer_trigger);
294 hrtimer_cancel(&data->timer_wde);
295 atomic_notifier_chain_unregister(&panic_notifier_list,
296 &data->panic_notifier);
297 }
298
299 static const struct of_device_id of_ltc2952_poweroff_match[] = {
300 { .compatible = "lltc,ltc2952"},
301 {},
302 };
303 MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
304
305 static struct platform_driver ltc2952_poweroff_driver = {
306 .probe = ltc2952_poweroff_probe,
307 .remove = ltc2952_poweroff_remove,
308 .driver = {
309 .name = "ltc2952-poweroff",
310 .of_match_table = of_ltc2952_poweroff_match,
311 },
312 };
313
314 module_platform_driver(ltc2952_poweroff_driver);
315
316 MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
317 MODULE_DESCRIPTION("LTC PowerPath power-off driver");
318