11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2790440bcSPawel Moll /*
3790440bcSPawel Moll *
4790440bcSPawel Moll * Copyright (C) 2012 ARM Limited
5790440bcSPawel Moll */
6790440bcSPawel Moll
7d08b8037SPawel Moll #include <linux/delay.h>
846c99ac6SGuenter Roeck #include <linux/notifier.h>
9790440bcSPawel Moll #include <linux/of.h>
10790440bcSPawel Moll #include <linux/platform_device.h>
11469d3174SRob Herring #include <linux/property.h>
1246c99ac6SGuenter Roeck #include <linux/reboot.h>
13790440bcSPawel Moll #include <linux/stat.h>
14790440bcSPawel Moll #include <linux/vexpress.h>
15790440bcSPawel Moll
vexpress_reset_do(struct device * dev,const char * what)16790440bcSPawel Moll static void vexpress_reset_do(struct device *dev, const char *what)
17790440bcSPawel Moll {
18790440bcSPawel Moll int err = -ENOENT;
193b9334acSPawel Moll struct regmap *reg = dev_get_drvdata(dev);
20790440bcSPawel Moll
213b9334acSPawel Moll if (reg) {
223b9334acSPawel Moll err = regmap_write(reg, 0, 0);
23d08b8037SPawel Moll if (!err)
24d08b8037SPawel Moll mdelay(1000);
25790440bcSPawel Moll }
26790440bcSPawel Moll
27790440bcSPawel Moll dev_emerg(dev, "Unable to %s (%d)\n", what, err);
28790440bcSPawel Moll }
29790440bcSPawel Moll
30790440bcSPawel Moll static struct device *vexpress_power_off_device;
3109bebb1aSSudeep Holla static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0);
32790440bcSPawel Moll
vexpress_power_off(void)3365deb782SCatalin Marinas static void vexpress_power_off(void)
34790440bcSPawel Moll {
35790440bcSPawel Moll vexpress_reset_do(vexpress_power_off_device, "power off");
36790440bcSPawel Moll }
37790440bcSPawel Moll
38790440bcSPawel Moll static struct device *vexpress_restart_device;
39790440bcSPawel Moll
vexpress_restart(struct notifier_block * this,unsigned long mode,void * cmd)4046c99ac6SGuenter Roeck static int vexpress_restart(struct notifier_block *this, unsigned long mode,
4146c99ac6SGuenter Roeck void *cmd)
42790440bcSPawel Moll {
43790440bcSPawel Moll vexpress_reset_do(vexpress_restart_device, "restart");
4446c99ac6SGuenter Roeck
4546c99ac6SGuenter Roeck return NOTIFY_DONE;
46790440bcSPawel Moll }
47790440bcSPawel Moll
4846c99ac6SGuenter Roeck static struct notifier_block vexpress_restart_nb = {
4946c99ac6SGuenter Roeck .notifier_call = vexpress_restart,
5046c99ac6SGuenter Roeck .priority = 128,
5146c99ac6SGuenter Roeck };
5246c99ac6SGuenter Roeck
vexpress_reset_active_show(struct device * dev,struct device_attribute * attr,char * buf)53790440bcSPawel Moll static ssize_t vexpress_reset_active_show(struct device *dev,
54790440bcSPawel Moll struct device_attribute *attr, char *buf)
55790440bcSPawel Moll {
56790440bcSPawel Moll return sprintf(buf, "%d\n", vexpress_restart_device == dev);
57790440bcSPawel Moll }
58790440bcSPawel Moll
vexpress_reset_active_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)59790440bcSPawel Moll static ssize_t vexpress_reset_active_store(struct device *dev,
60790440bcSPawel Moll struct device_attribute *attr, const char *buf, size_t count)
61790440bcSPawel Moll {
62790440bcSPawel Moll long value;
63790440bcSPawel Moll int err = kstrtol(buf, 0, &value);
64790440bcSPawel Moll
65790440bcSPawel Moll if (!err && value)
66790440bcSPawel Moll vexpress_restart_device = dev;
67790440bcSPawel Moll
68790440bcSPawel Moll return err ? err : count;
69790440bcSPawel Moll }
70790440bcSPawel Moll
710a14d280SBen Dooks static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
72790440bcSPawel Moll vexpress_reset_active_store);
73790440bcSPawel Moll
74790440bcSPawel Moll
75790440bcSPawel Moll enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
76790440bcSPawel Moll
778fb08855SFabian Frederick static const struct of_device_id vexpress_reset_of_match[] = {
78790440bcSPawel Moll {
79790440bcSPawel Moll .compatible = "arm,vexpress-reset",
80790440bcSPawel Moll .data = (void *)FUNC_RESET,
81790440bcSPawel Moll }, {
82790440bcSPawel Moll .compatible = "arm,vexpress-shutdown",
83790440bcSPawel Moll .data = (void *)FUNC_SHUTDOWN
84790440bcSPawel Moll }, {
85790440bcSPawel Moll .compatible = "arm,vexpress-reboot",
86790440bcSPawel Moll .data = (void *)FUNC_REBOOT
87790440bcSPawel Moll },
88790440bcSPawel Moll {}
89790440bcSPawel Moll };
90790440bcSPawel Moll
_vexpress_register_restart_handler(struct device * dev)9146c99ac6SGuenter Roeck static int _vexpress_register_restart_handler(struct device *dev)
9246c99ac6SGuenter Roeck {
9346c99ac6SGuenter Roeck int err;
9446c99ac6SGuenter Roeck
9546c99ac6SGuenter Roeck vexpress_restart_device = dev;
9609bebb1aSSudeep Holla if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) {
9746c99ac6SGuenter Roeck err = register_restart_handler(&vexpress_restart_nb);
9846c99ac6SGuenter Roeck if (err) {
9946c99ac6SGuenter Roeck dev_err(dev, "cannot register restart handler (err=%d)\n", err);
10009bebb1aSSudeep Holla atomic_dec(&vexpress_restart_nb_refcnt);
10146c99ac6SGuenter Roeck return err;
10246c99ac6SGuenter Roeck }
10309bebb1aSSudeep Holla }
10446c99ac6SGuenter Roeck device_create_file(dev, &dev_attr_active);
10546c99ac6SGuenter Roeck
10646c99ac6SGuenter Roeck return 0;
10746c99ac6SGuenter Roeck }
10846c99ac6SGuenter Roeck
vexpress_reset_probe(struct platform_device * pdev)109790440bcSPawel Moll static int vexpress_reset_probe(struct platform_device *pdev)
110790440bcSPawel Moll {
111469d3174SRob Herring enum vexpress_reset_func func;
1123b9334acSPawel Moll struct regmap *regmap;
11346c99ac6SGuenter Roeck int ret = 0;
114790440bcSPawel Moll
1153b9334acSPawel Moll regmap = devm_regmap_init_vexpress_config(&pdev->dev);
1163b9334acSPawel Moll if (IS_ERR(regmap))
1173b9334acSPawel Moll return PTR_ERR(regmap);
1183b9334acSPawel Moll dev_set_drvdata(&pdev->dev, regmap);
119d08b8037SPawel Moll
120469d3174SRob Herring func = (uintptr_t)device_get_match_data(&pdev->dev);
121469d3174SRob Herring switch (func) {
122790440bcSPawel Moll case FUNC_SHUTDOWN:
123790440bcSPawel Moll vexpress_power_off_device = &pdev->dev;
12465deb782SCatalin Marinas pm_power_off = vexpress_power_off;
125790440bcSPawel Moll break;
126790440bcSPawel Moll case FUNC_RESET:
127790440bcSPawel Moll if (!vexpress_restart_device)
12846c99ac6SGuenter Roeck ret = _vexpress_register_restart_handler(&pdev->dev);
129790440bcSPawel Moll break;
130790440bcSPawel Moll case FUNC_REBOOT:
13146c99ac6SGuenter Roeck ret = _vexpress_register_restart_handler(&pdev->dev);
132790440bcSPawel Moll break;
133091d0a3aSdongjian }
134790440bcSPawel Moll
13546c99ac6SGuenter Roeck return ret;
136790440bcSPawel Moll }
137790440bcSPawel Moll
138790440bcSPawel Moll static struct platform_driver vexpress_reset_driver = {
139790440bcSPawel Moll .probe = vexpress_reset_probe,
140790440bcSPawel Moll .driver = {
141790440bcSPawel Moll .name = "vexpress-reset",
142790440bcSPawel Moll .of_match_table = vexpress_reset_of_match,
14373174accSAnders Roxell .suppress_bind_attrs = true,
144790440bcSPawel Moll },
145790440bcSPawel Moll };
146a90d9904SAnders Roxell builtin_platform_driver(vexpress_reset_driver);
147