xref: /OK3568_Linux_fs/kernel/drivers/power/reset/vexpress-poweroff.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2012 ARM Limited
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/delay.h>
8*4882a593Smuzhiyun #include <linux/notifier.h>
9*4882a593Smuzhiyun #include <linux/of.h>
10*4882a593Smuzhiyun #include <linux/of_device.h>
11*4882a593Smuzhiyun #include <linux/platform_device.h>
12*4882a593Smuzhiyun #include <linux/reboot.h>
13*4882a593Smuzhiyun #include <linux/stat.h>
14*4882a593Smuzhiyun #include <linux/vexpress.h>
15*4882a593Smuzhiyun 
vexpress_reset_do(struct device * dev,const char * what)16*4882a593Smuzhiyun static void vexpress_reset_do(struct device *dev, const char *what)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	int err = -ENOENT;
19*4882a593Smuzhiyun 	struct regmap *reg = dev_get_drvdata(dev);
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun 	if (reg) {
22*4882a593Smuzhiyun 		err = regmap_write(reg, 0, 0);
23*4882a593Smuzhiyun 		if (!err)
24*4882a593Smuzhiyun 			mdelay(1000);
25*4882a593Smuzhiyun 	}
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	dev_emerg(dev, "Unable to %s (%d)\n", what, err);
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun static struct device *vexpress_power_off_device;
31*4882a593Smuzhiyun static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0);
32*4882a593Smuzhiyun 
vexpress_power_off(void)33*4882a593Smuzhiyun static void vexpress_power_off(void)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	vexpress_reset_do(vexpress_power_off_device, "power off");
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun static struct device *vexpress_restart_device;
39*4882a593Smuzhiyun 
vexpress_restart(struct notifier_block * this,unsigned long mode,void * cmd)40*4882a593Smuzhiyun static int vexpress_restart(struct notifier_block *this, unsigned long mode,
41*4882a593Smuzhiyun 			     void *cmd)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	vexpress_reset_do(vexpress_restart_device, "restart");
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	return NOTIFY_DONE;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun static struct notifier_block vexpress_restart_nb = {
49*4882a593Smuzhiyun 	.notifier_call = vexpress_restart,
50*4882a593Smuzhiyun 	.priority = 128,
51*4882a593Smuzhiyun };
52*4882a593Smuzhiyun 
vexpress_reset_active_show(struct device * dev,struct device_attribute * attr,char * buf)53*4882a593Smuzhiyun static ssize_t vexpress_reset_active_show(struct device *dev,
54*4882a593Smuzhiyun 		struct device_attribute *attr, char *buf)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", vexpress_restart_device == dev);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun 
vexpress_reset_active_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)59*4882a593Smuzhiyun static ssize_t vexpress_reset_active_store(struct device *dev,
60*4882a593Smuzhiyun 		struct device_attribute *attr, const char *buf, size_t count)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	long value;
63*4882a593Smuzhiyun 	int err = kstrtol(buf, 0, &value);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	if (!err && value)
66*4882a593Smuzhiyun 		vexpress_restart_device = dev;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	return err ? err : count;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
72*4882a593Smuzhiyun 		   vexpress_reset_active_store);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun static const struct of_device_id vexpress_reset_of_match[] = {
78*4882a593Smuzhiyun 	{
79*4882a593Smuzhiyun 		.compatible = "arm,vexpress-reset",
80*4882a593Smuzhiyun 		.data = (void *)FUNC_RESET,
81*4882a593Smuzhiyun 	}, {
82*4882a593Smuzhiyun 		.compatible = "arm,vexpress-shutdown",
83*4882a593Smuzhiyun 		.data = (void *)FUNC_SHUTDOWN
84*4882a593Smuzhiyun 	}, {
85*4882a593Smuzhiyun 		.compatible = "arm,vexpress-reboot",
86*4882a593Smuzhiyun 		.data = (void *)FUNC_REBOOT
87*4882a593Smuzhiyun 	},
88*4882a593Smuzhiyun 	{}
89*4882a593Smuzhiyun };
90*4882a593Smuzhiyun 
_vexpress_register_restart_handler(struct device * dev)91*4882a593Smuzhiyun static int _vexpress_register_restart_handler(struct device *dev)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	int err;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	vexpress_restart_device = dev;
96*4882a593Smuzhiyun 	if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) {
97*4882a593Smuzhiyun 		err = register_restart_handler(&vexpress_restart_nb);
98*4882a593Smuzhiyun 		if (err) {
99*4882a593Smuzhiyun 			dev_err(dev, "cannot register restart handler (err=%d)\n", err);
100*4882a593Smuzhiyun 			atomic_dec(&vexpress_restart_nb_refcnt);
101*4882a593Smuzhiyun 			return err;
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 	device_create_file(dev, &dev_attr_active);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	return 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
vexpress_reset_probe(struct platform_device * pdev)109*4882a593Smuzhiyun static int vexpress_reset_probe(struct platform_device *pdev)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	const struct of_device_id *match =
112*4882a593Smuzhiyun 			of_match_device(vexpress_reset_of_match, &pdev->dev);
113*4882a593Smuzhiyun 	struct regmap *regmap;
114*4882a593Smuzhiyun 	int ret = 0;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	if (!match)
117*4882a593Smuzhiyun 		return -EINVAL;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	regmap = devm_regmap_init_vexpress_config(&pdev->dev);
120*4882a593Smuzhiyun 	if (IS_ERR(regmap))
121*4882a593Smuzhiyun 		return PTR_ERR(regmap);
122*4882a593Smuzhiyun 	dev_set_drvdata(&pdev->dev, regmap);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	switch ((enum vexpress_reset_func)match->data) {
125*4882a593Smuzhiyun 	case FUNC_SHUTDOWN:
126*4882a593Smuzhiyun 		vexpress_power_off_device = &pdev->dev;
127*4882a593Smuzhiyun 		pm_power_off = vexpress_power_off;
128*4882a593Smuzhiyun 		break;
129*4882a593Smuzhiyun 	case FUNC_RESET:
130*4882a593Smuzhiyun 		if (!vexpress_restart_device)
131*4882a593Smuzhiyun 			ret = _vexpress_register_restart_handler(&pdev->dev);
132*4882a593Smuzhiyun 		break;
133*4882a593Smuzhiyun 	case FUNC_REBOOT:
134*4882a593Smuzhiyun 		ret = _vexpress_register_restart_handler(&pdev->dev);
135*4882a593Smuzhiyun 		break;
136*4882a593Smuzhiyun 	};
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	return ret;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun static struct platform_driver vexpress_reset_driver = {
142*4882a593Smuzhiyun 	.probe = vexpress_reset_probe,
143*4882a593Smuzhiyun 	.driver = {
144*4882a593Smuzhiyun 		.name = "vexpress-reset",
145*4882a593Smuzhiyun 		.of_match_table = vexpress_reset_of_match,
146*4882a593Smuzhiyun 		.suppress_bind_attrs = true,
147*4882a593Smuzhiyun 	},
148*4882a593Smuzhiyun };
149*4882a593Smuzhiyun builtin_platform_driver(vexpress_reset_driver);
150