xref: /linux/drivers/power/reset/regulator-poweroff.c (revision 4db102dcb0396a4ccf89b1eac0f4eb3fd167a080)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Force-disables a regulator to power down a device
4  *
5  * Michael Klein <michael@fossekall.de>
6  *
7  * Copyright (C) 2020 Michael Klein
8  *
9  * Based on the gpio-poweroff driver.
10  */
11 #include <linux/delay.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm.h>
16 #include <linux/regulator/consumer.h>
17 
18 #define TIMEOUT_MS 3000
19 
20 /*
21  * Hold configuration here, cannot be more than one instance of the driver
22  * since pm_power_off itself is global.
23  */
24 static struct regulator *cpu_regulator;
25 
26 static void regulator_poweroff_do_poweroff(void)
27 {
28 	if (cpu_regulator && regulator_is_enabled(cpu_regulator))
29 		regulator_force_disable(cpu_regulator);
30 
31 	/* give it some time */
32 	mdelay(TIMEOUT_MS);
33 
34 	WARN_ON(1);
35 }
36 
37 static int regulator_poweroff_probe(struct platform_device *pdev)
38 {
39 	/* If a pm_power_off function has already been added, leave it alone */
40 	if (pm_power_off != NULL) {
41 		dev_err(&pdev->dev,
42 			"%s: pm_power_off function already registered\n",
43 			__func__);
44 		return -EBUSY;
45 	}
46 
47 	cpu_regulator = devm_regulator_get(&pdev->dev, "cpu");
48 	if (IS_ERR(cpu_regulator))
49 		return PTR_ERR(cpu_regulator);
50 
51 	pm_power_off = &regulator_poweroff_do_poweroff;
52 	return 0;
53 }
54 
55 static void regulator_poweroff_remove(struct platform_device *pdev)
56 {
57 	if (pm_power_off == &regulator_poweroff_do_poweroff)
58 		pm_power_off = NULL;
59 }
60 
61 static const struct of_device_id of_regulator_poweroff_match[] = {
62 	{ .compatible = "regulator-poweroff", },
63 	{},
64 };
65 MODULE_DEVICE_TABLE(of, of_regulator_poweroff_match);
66 
67 static struct platform_driver regulator_poweroff_driver = {
68 	.probe = regulator_poweroff_probe,
69 	.remove_new = regulator_poweroff_remove,
70 	.driver = {
71 		.name = "poweroff-regulator",
72 		.of_match_table = of_regulator_poweroff_match,
73 	},
74 };
75 
76 module_platform_driver(regulator_poweroff_driver);
77 
78 MODULE_AUTHOR("Michael Klein <michael@fossekall.de>");
79 MODULE_DESCRIPTION("Regulator poweroff driver");
80 MODULE_ALIAS("platform:poweroff-regulator");
81