xref: /openbmc/linux/drivers/power/reset/ltc2952-poweroff.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26647156cSRené Moll /*
36647156cSRené Moll  * LTC2952 (PowerPath) driver
46647156cSRené Moll  *
56647156cSRené Moll  * Copyright (C) 2014, Xsens Technologies BV <info@xsens.com>
63723c632SArnd Bergmann  * Maintainer: René Moll <linux@r-moll.nl>
76647156cSRené Moll  *
86647156cSRené Moll  * ----------------------------------------
96647156cSRené Moll  * - Description
106647156cSRené Moll  * ----------------------------------------
116647156cSRené Moll  *
126647156cSRené Moll  * This driver is to be used with an external PowerPath Controller (LTC2952).
136647156cSRené Moll  * Its function is to determine when a external shut down is triggered
146647156cSRené Moll  * and react by properly shutting down the system.
156647156cSRené Moll  *
166647156cSRené Moll  * This driver expects a device tree with a ltc2952 entry for pin mapping.
176647156cSRené Moll  *
186647156cSRené Moll  * ----------------------------------------
196647156cSRené Moll  * - GPIO
206647156cSRené Moll  * ----------------------------------------
216647156cSRené Moll  *
226647156cSRené Moll  * The following GPIOs are used:
236647156cSRené Moll  * - trigger (input)
246647156cSRené Moll  *     A level change indicates the shut-down trigger. If it's state reverts
256647156cSRené Moll  *     within the time-out defined by trigger_delay, the shut down is not
26ee181853SFrans Klaver  *     executed. If no pin is assigned to this input, the driver will start the
27ee181853SFrans Klaver  *     watchdog toggle immediately. The chip will only power off the system if
28ee181853SFrans Klaver  *     it is requested to do so through the kill line.
296647156cSRené Moll  *
306647156cSRené Moll  * - watchdog (output)
316647156cSRené Moll  *     Once a shut down is triggered, the driver will toggle this signal,
326647156cSRené Moll  *     with an internal (wde_interval) to stall the hardware shut down.
336647156cSRené Moll  *
346647156cSRené Moll  * - kill (output)
356647156cSRené Moll  *     The last action during shut down is triggering this signalling, such
366647156cSRené Moll  *     that the PowerPath Control will power down the hardware.
376647156cSRené Moll  *
386647156cSRené Moll  * ----------------------------------------
396647156cSRené Moll  * - Interrupts
406647156cSRené Moll  * ----------------------------------------
416647156cSRené Moll  *
426647156cSRené Moll  * The driver requires a non-shared, edge-triggered interrupt on the trigger
436647156cSRené Moll  * GPIO.
446647156cSRené Moll  */
456647156cSRené Moll 
466647156cSRené Moll #include <linux/kernel.h>
476647156cSRené Moll #include <linux/init.h>
486647156cSRené Moll #include <linux/interrupt.h>
496647156cSRené Moll #include <linux/device.h>
506647156cSRené Moll #include <linux/platform_device.h>
516647156cSRené Moll #include <linux/ktime.h>
526647156cSRené Moll #include <linux/slab.h>
536647156cSRené Moll #include <linux/kmod.h>
546647156cSRené Moll #include <linux/module.h>
55f39650deSAndy Shevchenko #include <linux/panic_notifier.h>
56ac316725SRandy Dunlap #include <linux/mod_devicetable.h>
576647156cSRené Moll #include <linux/gpio/consumer.h>
586647156cSRené Moll #include <linux/reboot.h>
5952473b07SMarek Czerski #include <linux/property.h>
606647156cSRené Moll 
61a5f67be5SFrans Klaver struct ltc2952_poweroff {
626647156cSRené Moll 	struct hrtimer timer_trigger;
636647156cSRené Moll 	struct hrtimer timer_wde;
646647156cSRené Moll 
656647156cSRené Moll 	ktime_t trigger_delay;
666647156cSRené Moll 	ktime_t wde_interval;
676647156cSRené Moll 
686647156cSRené Moll 	struct device *dev;
696647156cSRené Moll 
70f66472dfSFrans Klaver 	struct gpio_desc *gpio_trigger;
71f66472dfSFrans Klaver 	struct gpio_desc *gpio_watchdog;
72f66472dfSFrans Klaver 	struct gpio_desc *gpio_kill;
73818ca4c8SFrans Klaver 
74818ca4c8SFrans Klaver 	bool kernel_panic;
75818ca4c8SFrans Klaver 	struct notifier_block panic_notifier;
766647156cSRené Moll };
776647156cSRené Moll 
78a5f67be5SFrans Klaver #define to_ltc2952(p, m) container_of(p, struct ltc2952_poweroff, m)
79a5f67be5SFrans Klaver 
80a5f67be5SFrans Klaver /*
81a5f67be5SFrans Klaver  * This global variable is only needed for pm_power_off. We should
82a5f67be5SFrans Klaver  * remove it entirely once we don't need the global state anymore.
83a5f67be5SFrans Klaver  */
84a5f67be5SFrans Klaver static struct ltc2952_poweroff *ltc2952_data;
856647156cSRené Moll 
866647156cSRené Moll /**
876647156cSRené Moll  * ltc2952_poweroff_timer_wde - Timer callback
886647156cSRené Moll  * Toggles the watchdog reset signal each wde_interval
896647156cSRené Moll  *
906647156cSRené Moll  * @timer: corresponding timer
916647156cSRené Moll  *
926647156cSRené Moll  * Returns HRTIMER_RESTART for an infinite loop which will only stop when the
936647156cSRené Moll  * machine actually shuts down
946647156cSRené Moll  */
ltc2952_poweroff_timer_wde(struct hrtimer * timer)956647156cSRené Moll static enum hrtimer_restart ltc2952_poweroff_timer_wde(struct hrtimer *timer)
966647156cSRené Moll {
976647156cSRené Moll 	int state;
98a5f67be5SFrans Klaver 	struct ltc2952_poweroff *data = to_ltc2952(timer, timer_wde);
996647156cSRené Moll 
100818ca4c8SFrans Klaver 	if (data->kernel_panic)
1016647156cSRené Moll 		return HRTIMER_NORESTART;
1026647156cSRené Moll 
103a5f67be5SFrans Klaver 	state = gpiod_get_value(data->gpio_watchdog);
104a5f67be5SFrans Klaver 	gpiod_set_value(data->gpio_watchdog, !state);
1056647156cSRené Moll 
10606793072SThomas Gleixner 	hrtimer_forward_now(timer, data->wde_interval);
1076647156cSRené Moll 
1086647156cSRené Moll 	return HRTIMER_RESTART;
1096647156cSRené Moll }
1106647156cSRené Moll 
ltc2952_poweroff_start_wde(struct ltc2952_poweroff * data)111ee181853SFrans Klaver static void ltc2952_poweroff_start_wde(struct ltc2952_poweroff *data)
1126647156cSRené Moll {
1134101ecc2SThomas Gleixner 	hrtimer_start(&data->timer_wde, data->wde_interval, HRTIMER_MODE_REL);
1146647156cSRené Moll }
1156647156cSRené Moll 
116ee181853SFrans Klaver static enum hrtimer_restart
ltc2952_poweroff_timer_trigger(struct hrtimer * timer)117ee181853SFrans Klaver ltc2952_poweroff_timer_trigger(struct hrtimer *timer)
118ee181853SFrans Klaver {
119ee181853SFrans Klaver 	struct ltc2952_poweroff *data = to_ltc2952(timer, timer_trigger);
1206647156cSRené Moll 
121ee181853SFrans Klaver 	ltc2952_poweroff_start_wde(data);
122ee181853SFrans Klaver 	dev_info(data->dev, "executing shutdown\n");
1236647156cSRené Moll 	orderly_poweroff(true);
1246647156cSRené Moll 
1256647156cSRené Moll 	return HRTIMER_NORESTART;
1266647156cSRené Moll }
1276647156cSRené Moll 
1286647156cSRené Moll /**
1296647156cSRené Moll  * ltc2952_poweroff_handler - Interrupt handler
1306647156cSRené Moll  * Triggered each time the trigger signal changes state and (de)activates a
1316647156cSRené Moll  * time-out (timer_trigger). Once the time-out is actually reached the shut
1326647156cSRené Moll  * down is executed.
1336647156cSRené Moll  *
1346647156cSRené Moll  * @irq: IRQ number
1356647156cSRené Moll  * @dev_id: pointer to the main data structure
1366647156cSRené Moll  */
ltc2952_poweroff_handler(int irq,void * dev_id)1376647156cSRené Moll static irqreturn_t ltc2952_poweroff_handler(int irq, void *dev_id)
1386647156cSRené Moll {
139a5f67be5SFrans Klaver 	struct ltc2952_poweroff *data = dev_id;
1406647156cSRené Moll 
1415c3faad2SFrans Klaver 	if (data->kernel_panic || hrtimer_active(&data->timer_wde)) {
1426647156cSRené Moll 		/* shutdown is already triggered, nothing to do any more */
1435c3faad2SFrans Klaver 		return IRQ_HANDLED;
1446647156cSRené Moll 	}
1456647156cSRené Moll 
146c1ada2ffSFrans Klaver 	if (gpiod_get_value(data->gpio_trigger)) {
1474101ecc2SThomas Gleixner 		hrtimer_start(&data->timer_trigger, data->trigger_delay,
1484101ecc2SThomas Gleixner 			      HRTIMER_MODE_REL);
1496647156cSRené Moll 	} else {
1505c3faad2SFrans Klaver 		hrtimer_cancel(&data->timer_trigger);
1516647156cSRené Moll 	}
1526647156cSRené Moll 	return IRQ_HANDLED;
1536647156cSRené Moll }
1546647156cSRené Moll 
ltc2952_poweroff_kill(void)1556647156cSRené Moll static void ltc2952_poweroff_kill(void)
1566647156cSRené Moll {
157f66472dfSFrans Klaver 	gpiod_set_value(ltc2952_data->gpio_kill, 1);
1586647156cSRené Moll }
1596647156cSRené Moll 
ltc2952_poweroff_default(struct ltc2952_poweroff * data)160a5f67be5SFrans Klaver static void ltc2952_poweroff_default(struct ltc2952_poweroff *data)
1616647156cSRené Moll {
162*644106cdSNathan Chancellor 	data->wde_interval = 300L * NSEC_PER_MSEC;
163*644106cdSNathan Chancellor 	data->trigger_delay = ktime_set(2, 500L * NSEC_PER_MSEC);
1646647156cSRené Moll 
1656647156cSRené Moll 	hrtimer_init(&data->timer_trigger, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1665689b786SFrans Klaver 	data->timer_trigger.function = ltc2952_poweroff_timer_trigger;
1676647156cSRené Moll 
1686647156cSRené Moll 	hrtimer_init(&data->timer_wde, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1695689b786SFrans Klaver 	data->timer_wde.function = ltc2952_poweroff_timer_wde;
1706647156cSRené Moll }
1716647156cSRené Moll 
ltc2952_poweroff_init(struct platform_device * pdev)1726647156cSRené Moll static int ltc2952_poweroff_init(struct platform_device *pdev)
1736647156cSRené Moll {
174ee181853SFrans Klaver 	int ret;
17552473b07SMarek Czerski 	u32 trigger_delay_ms;
176a5f67be5SFrans Klaver 	struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
1776647156cSRené Moll 
178a5f67be5SFrans Klaver 	ltc2952_poweroff_default(data);
1796647156cSRené Moll 
18052473b07SMarek Czerski 	if (!device_property_read_u32(&pdev->dev, "trigger-delay-ms",
18152473b07SMarek Czerski 				      &trigger_delay_ms)) {
18252473b07SMarek Czerski 		data->trigger_delay = ktime_set(trigger_delay_ms / MSEC_PER_SEC,
18352473b07SMarek Czerski 			(trigger_delay_ms % MSEC_PER_SEC) * NSEC_PER_MSEC);
18452473b07SMarek Czerski 	}
18552473b07SMarek Czerski 
186a5f67be5SFrans Klaver 	data->gpio_watchdog = devm_gpiod_get(&pdev->dev, "watchdog",
18762113b31SFrans Klaver 					     GPIOD_OUT_LOW);
188a5f67be5SFrans Klaver 	if (IS_ERR(data->gpio_watchdog)) {
189a5f67be5SFrans Klaver 		ret = PTR_ERR(data->gpio_watchdog);
190f66472dfSFrans Klaver 		dev_err(&pdev->dev, "unable to claim gpio \"watchdog\"\n");
191f66472dfSFrans Klaver 		return ret;
1926647156cSRené Moll 	}
193f66472dfSFrans Klaver 
194a5f67be5SFrans Klaver 	data->gpio_kill = devm_gpiod_get(&pdev->dev, "kill", GPIOD_OUT_LOW);
195a5f67be5SFrans Klaver 	if (IS_ERR(data->gpio_kill)) {
196a5f67be5SFrans Klaver 		ret = PTR_ERR(data->gpio_kill);
197f66472dfSFrans Klaver 		dev_err(&pdev->dev, "unable to claim gpio \"kill\"\n");
19862113b31SFrans Klaver 		return ret;
199f66472dfSFrans Klaver 	}
200f66472dfSFrans Klaver 
2019f6cd98fSUwe Kleine-König 	data->gpio_trigger = devm_gpiod_get_optional(&pdev->dev, "trigger",
2029f6cd98fSUwe Kleine-König 						     GPIOD_IN);
203ee181853SFrans Klaver 	if (IS_ERR(data->gpio_trigger)) {
204ee181853SFrans Klaver 		/*
205ee181853SFrans Klaver 		 * It's not a problem if the trigger gpio isn't available, but
206ee181853SFrans Klaver 		 * it is worth a warning if its use was defined in the device
207ee181853SFrans Klaver 		 * tree.
208ee181853SFrans Klaver 		 */
2099f6cd98fSUwe Kleine-König 		dev_err(&pdev->dev, "unable to claim gpio \"trigger\"\n");
210ee181853SFrans Klaver 		data->gpio_trigger = NULL;
2116647156cSRené Moll 	}
2126647156cSRené Moll 
213ee181853SFrans Klaver 	if (devm_request_irq(&pdev->dev, gpiod_to_irq(data->gpio_trigger),
2146647156cSRené Moll 			     ltc2952_poweroff_handler,
2156647156cSRené Moll 			     (IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING),
2166647156cSRené Moll 			     "ltc2952-poweroff",
217ee181853SFrans Klaver 			     data)) {
218ee181853SFrans Klaver 		/*
219ee181853SFrans Klaver 		 * Some things may have happened:
220ee181853SFrans Klaver 		 * - No trigger input was defined
221ee181853SFrans Klaver 		 * - Claiming the GPIO failed
222ee181853SFrans Klaver 		 * - We could not map to an IRQ
223ee181853SFrans Klaver 		 * - We couldn't register an interrupt handler
224ee181853SFrans Klaver 		 *
225ee181853SFrans Klaver 		 * None of these really are problems, but all of them
226ee181853SFrans Klaver 		 * disqualify the push button from controlling the power.
227ee181853SFrans Klaver 		 *
228ee181853SFrans Klaver 		 * It is therefore important to note that if the ltc2952
229ee181853SFrans Klaver 		 * detects a button press for long enough, it will still start
230ee181853SFrans Klaver 		 * its own powerdown window and cut the power on us if we don't
231ee181853SFrans Klaver 		 * start the watchdog trigger.
232ee181853SFrans Klaver 		 */
233ee181853SFrans Klaver 		if (data->gpio_trigger) {
234ee181853SFrans Klaver 			dev_warn(&pdev->dev,
235ee181853SFrans Klaver 				 "unable to configure the trigger interrupt\n");
236ee181853SFrans Klaver 			devm_gpiod_put(&pdev->dev, data->gpio_trigger);
237ee181853SFrans Klaver 			data->gpio_trigger = NULL;
238ee181853SFrans Klaver 		}
239ee181853SFrans Klaver 		dev_info(&pdev->dev,
240ee181853SFrans Klaver 			 "power down trigger input will not be used\n");
241ee181853SFrans Klaver 		ltc2952_poweroff_start_wde(data);
2426647156cSRené Moll 	}
2436647156cSRené Moll 
2446647156cSRené Moll 	return 0;
2456647156cSRené Moll }
2466647156cSRené Moll 
ltc2952_poweroff_notify_panic(struct notifier_block * nb,unsigned long code,void * unused)247818ca4c8SFrans Klaver static int ltc2952_poweroff_notify_panic(struct notifier_block *nb,
248818ca4c8SFrans Klaver 					 unsigned long code, void *unused)
249818ca4c8SFrans Klaver {
250818ca4c8SFrans Klaver 	struct ltc2952_poweroff *data = to_ltc2952(nb, panic_notifier);
251818ca4c8SFrans Klaver 
252818ca4c8SFrans Klaver 	data->kernel_panic = true;
253818ca4c8SFrans Klaver 	return NOTIFY_DONE;
254818ca4c8SFrans Klaver }
255818ca4c8SFrans Klaver 
ltc2952_poweroff_probe(struct platform_device * pdev)2566647156cSRené Moll static int ltc2952_poweroff_probe(struct platform_device *pdev)
2576647156cSRené Moll {
2586647156cSRené Moll 	int ret;
259a5f67be5SFrans Klaver 	struct ltc2952_poweroff *data;
2606647156cSRené Moll 
2616647156cSRené Moll 	if (pm_power_off) {
2626647156cSRené Moll 		dev_err(&pdev->dev, "pm_power_off already registered");
2636647156cSRené Moll 		return -EBUSY;
2646647156cSRené Moll 	}
2656647156cSRené Moll 
266a5f67be5SFrans Klaver 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
267a5f67be5SFrans Klaver 	if (!data)
2686647156cSRené Moll 		return -ENOMEM;
2696647156cSRené Moll 
270a5f67be5SFrans Klaver 	data->dev = &pdev->dev;
271a5f67be5SFrans Klaver 	platform_set_drvdata(pdev, data);
2726647156cSRené Moll 
2736647156cSRené Moll 	ret = ltc2952_poweroff_init(pdev);
2746647156cSRené Moll 	if (ret)
2750a5c6a22SFrans Klaver 		return ret;
2766647156cSRené Moll 
277a5f67be5SFrans Klaver 	/* TODO: remove ltc2952_data */
278a5f67be5SFrans Klaver 	ltc2952_data = data;
2795689b786SFrans Klaver 	pm_power_off = ltc2952_poweroff_kill;
2806647156cSRené Moll 
281818ca4c8SFrans Klaver 	data->panic_notifier.notifier_call = ltc2952_poweroff_notify_panic;
282818ca4c8SFrans Klaver 	atomic_notifier_chain_register(&panic_notifier_list,
283818ca4c8SFrans Klaver 				       &data->panic_notifier);
2846647156cSRené Moll 	dev_info(&pdev->dev, "probe successful\n");
2856647156cSRené Moll 
2866647156cSRené Moll 	return 0;
2876647156cSRené Moll }
2886647156cSRené Moll 
ltc2952_poweroff_remove(struct platform_device * pdev)2896647156cSRené Moll static int ltc2952_poweroff_remove(struct platform_device *pdev)
2906647156cSRené Moll {
291818ca4c8SFrans Klaver 	struct ltc2952_poweroff *data = platform_get_drvdata(pdev);
2926647156cSRené Moll 
293818ca4c8SFrans Klaver 	pm_power_off = NULL;
2942f6ea8adSFrans Klaver 	hrtimer_cancel(&data->timer_trigger);
2952f6ea8adSFrans Klaver 	hrtimer_cancel(&data->timer_wde);
296818ca4c8SFrans Klaver 	atomic_notifier_chain_unregister(&panic_notifier_list,
297818ca4c8SFrans Klaver 					 &data->panic_notifier);
2986647156cSRené Moll 	return 0;
2996647156cSRené Moll }
3006647156cSRené Moll 
3016647156cSRené Moll static const struct of_device_id of_ltc2952_poweroff_match[] = {
3026647156cSRené Moll 	{ .compatible = "lltc,ltc2952"},
3036647156cSRené Moll 	{},
3046647156cSRené Moll };
3056647156cSRené Moll MODULE_DEVICE_TABLE(of, of_ltc2952_poweroff_match);
3066647156cSRené Moll 
3076647156cSRené Moll static struct platform_driver ltc2952_poweroff_driver = {
3086647156cSRené Moll 	.probe = ltc2952_poweroff_probe,
3096647156cSRené Moll 	.remove = ltc2952_poweroff_remove,
3106647156cSRené Moll 	.driver = {
3116647156cSRené Moll 		.name = "ltc2952-poweroff",
3126647156cSRené Moll 		.of_match_table = of_ltc2952_poweroff_match,
3136647156cSRené Moll 	},
3146647156cSRené Moll };
3156647156cSRené Moll 
316818ca4c8SFrans Klaver module_platform_driver(ltc2952_poweroff_driver);
3176647156cSRené Moll 
3183723c632SArnd Bergmann MODULE_AUTHOR("René Moll <rene.moll@xsens.com>");
3196647156cSRené Moll MODULE_DESCRIPTION("LTC PowerPath power-off driver");
320