1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License version 2 as
4  * published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * Copyright (C) 2012 ARM Limited
12  */
13 
14 #include <linux/delay.h>
15 #include <linux/of.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/stat.h>
19 #include <linux/vexpress.h>
20 
21 #include <asm/system_misc.h>
22 
23 static void vexpress_reset_do(struct device *dev, const char *what)
24 {
25 	int err = -ENOENT;
26 	struct vexpress_config_func *func = dev_get_drvdata(dev);
27 
28 	if (func) {
29 		err = vexpress_config_write(func, 0, 0);
30 		if (!err)
31 			mdelay(1000);
32 	}
33 
34 	dev_emerg(dev, "Unable to %s (%d)\n", what, err);
35 }
36 
37 static struct device *vexpress_power_off_device;
38 
39 static void vexpress_power_off(void)
40 {
41 	vexpress_reset_do(vexpress_power_off_device, "power off");
42 }
43 
44 static struct device *vexpress_restart_device;
45 
46 static void vexpress_restart(enum reboot_mode reboot_mode, const char *cmd)
47 {
48 	vexpress_reset_do(vexpress_restart_device, "restart");
49 }
50 
51 static ssize_t vexpress_reset_active_show(struct device *dev,
52 		struct device_attribute *attr, char *buf)
53 {
54 	return sprintf(buf, "%d\n", vexpress_restart_device == dev);
55 }
56 
57 static ssize_t vexpress_reset_active_store(struct device *dev,
58 		struct device_attribute *attr, const char *buf, size_t count)
59 {
60 	long value;
61 	int err = kstrtol(buf, 0, &value);
62 
63 	if (!err && value)
64 		vexpress_restart_device = dev;
65 
66 	return err ? err : count;
67 }
68 
69 DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
70 		vexpress_reset_active_store);
71 
72 
73 enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
74 
75 static struct of_device_id vexpress_reset_of_match[] = {
76 	{
77 		.compatible = "arm,vexpress-reset",
78 		.data = (void *)FUNC_RESET,
79 	}, {
80 		.compatible = "arm,vexpress-shutdown",
81 		.data = (void *)FUNC_SHUTDOWN
82 	}, {
83 		.compatible = "arm,vexpress-reboot",
84 		.data = (void *)FUNC_REBOOT
85 	},
86 	{}
87 };
88 
89 static int vexpress_reset_probe(struct platform_device *pdev)
90 {
91 	enum vexpress_reset_func func;
92 	const struct of_device_id *match =
93 			of_match_device(vexpress_reset_of_match, &pdev->dev);
94 	struct vexpress_config_func *config_func;
95 
96 	if (match)
97 		func = (enum vexpress_reset_func)match->data;
98 	else
99 		func = pdev->id_entry->driver_data;
100 
101 	config_func = vexpress_config_func_get_by_dev(&pdev->dev);
102 	if (!config_func)
103 		return -EINVAL;
104 	dev_set_drvdata(&pdev->dev, config_func);
105 
106 	switch (func) {
107 	case FUNC_SHUTDOWN:
108 		vexpress_power_off_device = &pdev->dev;
109 		pm_power_off = vexpress_power_off;
110 		break;
111 	case FUNC_RESET:
112 		if (!vexpress_restart_device)
113 			vexpress_restart_device = &pdev->dev;
114 		arm_pm_restart = vexpress_restart;
115 		device_create_file(&pdev->dev, &dev_attr_active);
116 		break;
117 	case FUNC_REBOOT:
118 		vexpress_restart_device = &pdev->dev;
119 		arm_pm_restart = vexpress_restart;
120 		device_create_file(&pdev->dev, &dev_attr_active);
121 		break;
122 	};
123 
124 	return 0;
125 }
126 
127 static const struct platform_device_id vexpress_reset_id_table[] = {
128 	{ .name = "vexpress-reset", .driver_data = FUNC_RESET, },
129 	{ .name = "vexpress-shutdown", .driver_data = FUNC_SHUTDOWN, },
130 	{ .name = "vexpress-reboot", .driver_data = FUNC_REBOOT, },
131 	{}
132 };
133 
134 static struct platform_driver vexpress_reset_driver = {
135 	.probe = vexpress_reset_probe,
136 	.driver = {
137 		.name = "vexpress-reset",
138 		.of_match_table = vexpress_reset_of_match,
139 	},
140 	.id_table = vexpress_reset_id_table,
141 };
142 
143 static int __init vexpress_reset_init(void)
144 {
145 	return platform_driver_register(&vexpress_reset_driver);
146 }
147 device_initcall(vexpress_reset_init);
148