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/mod_devicetable.h>
57 #include <linux/gpio/consumer.h>
58 #include <linux/reboot.h>
59 #include <linux/property.h>
60
61 struct ltc2952_poweroff {
62 struct hrtimer timer_trigger;
63 struct hrtimer timer_wde;
64
65 ktime_t trigger_delay;
66 ktime_t wde_interval;
67
68 struct device *dev;
69
70 struct gpio_desc *gpio_trigger;
71 struct gpio_desc *gpio_watchdog;
72 struct gpio_desc *gpio_kill;
73
74 bool kernel_panic;
75 struct notifier_block panic_notifier;
76 };
77
78 #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
79
80 /*
81 * This global variable is only needed for pm_power_off. We should
82 * remove it entirely once we don't need the global state anymore.
83 */
84 static struct ltc2952_poweroff *ltc2952_data;
85
86 /**
87 * ltc2952_poweroff_timer_wde - Timer callback
88 * Toggles the watchdog reset signal each wde_interval
89 *
90 * @timer: corresponding timer
91 *
92 * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
93 * machine actually shuts down
94 */
ltc2952_poweroff_timer_wde(struct hrtimer * timer)95 static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
96 {
97 ktime_t now;
98 int state;
99 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
100
101 if (data->kernel_panic)
102 return HRTIMER_NORESTART;
103
104 state = gpiod_get_value(data->gpio_watchdog);
105 gpiod_set_value(data->gpio_watchdog, !state);
106
107 now = hrtimer_cb_get_time(timer);
108 hrtimer_forward(timer, now, data->wde_interval);
109
110 return HRTIMER_RESTART;
111 }
112
ltc2952_poweroff_start_wde(struct ltc2952_poweroff * data)113 static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff *data)
114 {
115 hrtimer_start(&data->timer_wde, data->wde_interval, HRTIMER_MODE_REL);
116 }
117
118 static enum hrtimer_restart
ltc2952_poweroff_timer_trigger(struct hrtimer * timer)119 ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
120 {
121 struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
122
123 ltc2952_poweroff_start_wde(data);
124 dev_info(data->dev, "executing shutdown\n");
125 orderly_poweroff(true);
126
127 return HRTIMER_NORESTART;
128 }
129
130 /**
131 * ltc2952_poweroff_handler - Interrupt handler
132 * Triggered each time the trigger signal changes state and (de)activates a
133 * time-out (timer_trigger). Once the time-out is actually reached the shut
134 * down is executed.
135 *
136 * @irq: IRQ number
137 * @dev_id: pointer to the main data structure
138 */
ltc2952_poweroff_handler(int irq,void * dev_id)139 static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
140 {
141 struct ltc2952_poweroff *data = dev_id;
142
143 if (data->kernel_panic || hrtimer_active(&data->timer_wde)) {
144 /* shutdown is already triggered, nothing to do any more */
145 return IRQ_HANDLED;
146 }
147
148 if (gpiod_get_value(data->gpio_trigger)) {
149 hrtimer_start(&data->timer_trigger, data->trigger_delay,
150 HRTIMER_MODE_REL);
151 } else {
152 hrtimer_cancel(&data->timer_trigger);
153 }
154 return IRQ_HANDLED;
155 }
156
ltc2952_poweroff_kill(void)157 static void ltc2952_poweroff_kill(void)
158 {
159 gpiod_set_value(ltc2952_data->gpio_kill, 1);
160 }
161
ltc2952_poweroff_default(struct ltc2952_poweroff * data)162 static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
163 {
164 data->wde_interval = 300L * NSEC_PER_MSEC;
165 data->trigger_delay = ktime_set(2, 500L * NSEC_PER_MSEC);
166
167 hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
168 data->timer_trigger.function = ltc2952_poweroff_timer_trigger;
169
170 hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
171 data->timer_wde.function = ltc2952_poweroff_timer_wde;
172 }
173
ltc2952_poweroff_init(struct platform_device * pdev)174 static int ltc2952_poweroff_init(struct platform_device *pdev)
175 {
176 int ret;
177 u32 trigger_delay_ms;
178 struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
179
180 ltc2952_poweroff_default(data);
181
182 if (!device_property_read_u32(&pdev->dev, "trigger-delay-ms",
183 &trigger_delay_ms)) {
184 data->trigger_delay = ktime_set(trigger_delay_ms / MSEC_PER_SEC,
185 (trigger_delay_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
186 }
187
188 data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
189 GPIOD_OUT_LOW);
190 if (IS_ERR(data->gpio_watchdog)) {
191 ret = PTR_ERR(data->gpio_watchdog);
192 dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
193 return ret;
194 }
195
196 data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
197 if (IS_ERR(data->gpio_kill)) {
198 ret = PTR_ERR(data->gpio_kill);
199 dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
200 return ret;
201 }
202
203 data->gpio_trigger = devm_gpiod_get_optional(&pdev->dev, "trigger",
204 GPIOD_IN);
205 if (IS_ERR(data->gpio_trigger)) {
206 /*
207 * It's not a problem if the trigger gpio isn't available, but
208 * it is worth a warning if its use was defined in the device
209 * tree.
210 */
211 dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
212 data->gpio_trigger = NULL;
213 }
214
215 if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger),
216 ltc2952_poweroff_handler,
217 (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
218 "ltc2952-poweroff",
219 data)) {
220 /*
221 * Some things may have happened:
222 * - No trigger input was defined
223 * - Claiming the GPIO failed
224 * - We could not map to an IRQ
225 * - We couldn't register an interrupt handler
226 *
227 * None of these really are problems, but all of them
228 * disqualify the push button from controlling the power.
229 *
230 * It is therefore important to note that if the ltc2952
231 * detects a button press for long enough, it will still start
232 * its own powerdown window and cut the power on us if we don't
233 * start the watchdog trigger.
234 */
235 if (data->gpio_trigger) {
236 dev_warn(&pdev->dev,
237 "unable to configure the trigger interrupt\n");
238 devm_gpiod_put(&pdev->dev, data->gpio_trigger);
239 data->gpio_trigger = NULL;
240 }
241 dev_info(&pdev->dev,
242 "power down trigger input will not be used\n");
243 ltc2952_poweroff_start_wde(data);
244 }
245
246 return 0;
247 }
248
ltc2952_poweroff_notify_panic(struct notifier_block * nb,unsigned long code,void * unused)249 static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
250 unsigned long code, void *unused)
251 {
252 struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier);
253
254 data->kernel_panic = true;
255 return NOTIFY_DONE;
256 }
257
ltc2952_poweroff_probe(struct platform_device * pdev)258 static int ltc2952_poweroff_probe(struct platform_device *pdev)
259 {
260 int ret;
261 struct ltc2952_poweroff *data;
262
263 if (pm_power_off) {
264 dev_err(&pdev->dev, "pm_power_off already registered");
265 return -EBUSY;
266 }
267
268 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
269 if (!data)
270 return -ENOMEM;
271
272 data->dev = &pdev->dev;
273 platform_set_drvdata(pdev, data);
274
275 ret = ltc2952_poweroff_init(pdev);
276 if (ret)
277 return ret;
278
279 /* TODO: remove ltc2952_data */
280 ltc2952_data = data;
281 pm_power_off = ltc2952_poweroff_kill;
282
283 data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
284 atomic_notifier_chain_register(&panic_notifier_list,
285 &data->panic_notifier);
286 dev_info(&pdev->dev, "probe successful\n");
287
288 return 0;
289 }
290
ltc2952_poweroff_remove(struct platform_device * pdev)291 static int ltc2952_poweroff_remove(struct platform_device *pdev)
292 {
293 struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
294
295 pm_power_off = NULL;
296 hrtimer_cancel(&data->timer_trigger);
297 hrtimer_cancel(&data->timer_wde);
298 atomic_notifier_chain_unregister(&panic_notifier_list,
299 &data->panic_notifier);
300 return 0;
301 }
302
303 static const struct of_device_id of_ltc2952_poweroff_match[] = {
304 { .compatible = "lltc,ltc2952"},
305 {},
306 };
307 MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
308
309 static struct platform_driver ltc2952_poweroff_driver = {
310 .probe = ltc2952_poweroff_probe,
311 .remove = ltc2952_poweroff_remove,
312 .driver = {
313 .name = "ltc2952-poweroff",
314 .of_match_table = of_ltc2952_poweroff_match,
315 },
316 };
317
318 module_platform_driver(ltc2952_poweroff_driver);
319
320 MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
321 MODULE_DESCRIPTION("LTC PowerPath power-off driver");
322 MODULE_LICENSE("GPL v2");
323