blob: 8fb43c4438e609ba48ad78f432ce292751f4f525 [file] [log] [blame]
Pawel Moll790440b2012-10-18 17:58:47 +01001/*
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
Pawel Molld08b8032014-04-24 17:19:30 +010014#include <linux/delay.h>
Guenter Roeck46c99ac2014-09-30 10:48:27 -070015#include <linux/notifier.h>
Pawel Moll790440b2012-10-18 17:58:47 +010016#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/platform_device.h>
Guenter Roeck46c99ac2014-09-30 10:48:27 -070019#include <linux/reboot.h>
Pawel Moll790440b2012-10-18 17:58:47 +010020#include <linux/stat.h>
21#include <linux/vexpress.h>
22
23static void vexpress_reset_do(struct device *dev, const char *what)
24{
25 int err = -ENOENT;
Pawel Moll3b9334a2014-04-30 16:46:29 +010026 struct regmap *reg = dev_get_drvdata(dev);
Pawel Moll790440b2012-10-18 17:58:47 +010027
Pawel Moll3b9334a2014-04-30 16:46:29 +010028 if (reg) {
29 err = regmap_write(reg, 0, 0);
Pawel Molld08b8032014-04-24 17:19:30 +010030 if (!err)
31 mdelay(1000);
Pawel Moll790440b2012-10-18 17:58:47 +010032 }
33
34 dev_emerg(dev, "Unable to %s (%d)\n", what, err);
35}
36
37static struct device *vexpress_power_off_device;
Sudeep Holla0830a972018-06-18 16:54:32 +010038static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0);
Pawel Moll790440b2012-10-18 17:58:47 +010039
Catalin Marinas65deb782013-02-28 16:43:19 +000040static void vexpress_power_off(void)
Pawel Moll790440b2012-10-18 17:58:47 +010041{
42 vexpress_reset_do(vexpress_power_off_device, "power off");
43}
44
45static struct device *vexpress_restart_device;
46
Guenter Roeck46c99ac2014-09-30 10:48:27 -070047static int vexpress_restart(struct notifier_block *this, unsigned long mode,
48 void *cmd)
Pawel Moll790440b2012-10-18 17:58:47 +010049{
50 vexpress_reset_do(vexpress_restart_device, "restart");
Guenter Roeck46c99ac2014-09-30 10:48:27 -070051
52 return NOTIFY_DONE;
Pawel Moll790440b2012-10-18 17:58:47 +010053}
54
Guenter Roeck46c99ac2014-09-30 10:48:27 -070055static struct notifier_block vexpress_restart_nb = {
56 .notifier_call = vexpress_restart,
57 .priority = 128,
58};
59
Pawel Moll790440b2012-10-18 17:58:47 +010060static ssize_t vexpress_reset_active_show(struct device *dev,
61 struct device_attribute *attr, char *buf)
62{
63 return sprintf(buf, "%d\n", vexpress_restart_device == dev);
64}
65
66static ssize_t vexpress_reset_active_store(struct device *dev,
67 struct device_attribute *attr, const char *buf, size_t count)
68{
69 long value;
70 int err = kstrtol(buf, 0, &value);
71
72 if (!err && value)
73 vexpress_restart_device = dev;
74
75 return err ? err : count;
76}
77
Ben Dooks0a14d282016-06-08 19:26:31 +010078static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
79 vexpress_reset_active_store);
Pawel Moll790440b2012-10-18 17:58:47 +010080
81
82enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
83
Fabian Frederick8fb08852015-03-16 20:17:12 +010084static const struct of_device_id vexpress_reset_of_match[] = {
Pawel Moll790440b2012-10-18 17:58:47 +010085 {
86 .compatible = "arm,vexpress-reset",
87 .data = (void *)FUNC_RESET,
88 }, {
89 .compatible = "arm,vexpress-shutdown",
90 .data = (void *)FUNC_SHUTDOWN
91 }, {
92 .compatible = "arm,vexpress-reboot",
93 .data = (void *)FUNC_REBOOT
94 },
95 {}
96};
97
Guenter Roeck46c99ac2014-09-30 10:48:27 -070098static int _vexpress_register_restart_handler(struct device *dev)
99{
100 int err;
101
102 vexpress_restart_device = dev;
Sudeep Holla0830a972018-06-18 16:54:32 +0100103 if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) {
104 err = register_restart_handler(&vexpress_restart_nb);
105 if (err) {
106 dev_err(dev, "cannot register restart handler (err=%d)\n", err);
107 atomic_dec(&vexpress_restart_nb_refcnt);
108 return err;
109 }
Guenter Roeck46c99ac2014-09-30 10:48:27 -0700110 }
111 device_create_file(dev, &dev_attr_active);
112
113 return 0;
114}
115
Pawel Moll790440b2012-10-18 17:58:47 +0100116static int vexpress_reset_probe(struct platform_device *pdev)
117{
Pawel Moll790440b2012-10-18 17:58:47 +0100118 const struct of_device_id *match =
119 of_match_device(vexpress_reset_of_match, &pdev->dev);
Pawel Moll3b9334a2014-04-30 16:46:29 +0100120 struct regmap *regmap;
Guenter Roeck46c99ac2014-09-30 10:48:27 -0700121 int ret = 0;
Pawel Moll790440b2012-10-18 17:58:47 +0100122
Pawel Moll8f961c02015-01-20 17:24:02 +0000123 if (!match)
124 return -EINVAL;
Pawel Moll790440b2012-10-18 17:58:47 +0100125
Pawel Moll3b9334a2014-04-30 16:46:29 +0100126 regmap = devm_regmap_init_vexpress_config(&pdev->dev);
127 if (IS_ERR(regmap))
128 return PTR_ERR(regmap);
129 dev_set_drvdata(&pdev->dev, regmap);
Pawel Molld08b8032014-04-24 17:19:30 +0100130
Pawel Moll8f961c02015-01-20 17:24:02 +0000131 switch ((enum vexpress_reset_func)match->data) {
Pawel Moll790440b2012-10-18 17:58:47 +0100132 case FUNC_SHUTDOWN:
133 vexpress_power_off_device = &pdev->dev;
Catalin Marinas65deb782013-02-28 16:43:19 +0000134 pm_power_off = vexpress_power_off;
Pawel Moll790440b2012-10-18 17:58:47 +0100135 break;
136 case FUNC_RESET:
137 if (!vexpress_restart_device)
Guenter Roeck46c99ac2014-09-30 10:48:27 -0700138 ret = _vexpress_register_restart_handler(&pdev->dev);
Pawel Moll790440b2012-10-18 17:58:47 +0100139 break;
140 case FUNC_REBOOT:
Guenter Roeck46c99ac2014-09-30 10:48:27 -0700141 ret = _vexpress_register_restart_handler(&pdev->dev);
Pawel Moll790440b2012-10-18 17:58:47 +0100142 break;
143 };
144
Guenter Roeck46c99ac2014-09-30 10:48:27 -0700145 return ret;
Pawel Moll790440b2012-10-18 17:58:47 +0100146}
147
Pawel Moll790440b2012-10-18 17:58:47 +0100148static struct platform_driver vexpress_reset_driver = {
149 .probe = vexpress_reset_probe,
150 .driver = {
151 .name = "vexpress-reset",
152 .of_match_table = vexpress_reset_of_match,
Anders Roxellefb8d872020-05-27 13:26:04 +0200153 .suppress_bind_attrs = true,
Pawel Moll790440b2012-10-18 17:58:47 +0100154 },
Pawel Moll790440b2012-10-18 17:58:47 +0100155};
156
157static int __init vexpress_reset_init(void)
158{
159 return platform_driver_register(&vexpress_reset_driver);
160}
161device_initcall(vexpress_reset_init);