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