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.
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.
11 * Copyright (C) 2012 ARM Limited
14 #include <linux/delay.h>
15 #include <linux/notifier.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/reboot.h>
20 #include <linux/stat.h>
21 #include <linux/vexpress.h>
23 static void vexpress_reset_do(struct device *dev, const char *what)
26 struct regmap *reg = dev_get_drvdata(dev);
29 err = regmap_write(reg, 0, 0);
34 dev_emerg(dev, "Unable to %s (%d)\n", what, err);
37 static struct device *vexpress_power_off_device;
38 static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0);
40 static void vexpress_power_off(void)
42 vexpress_reset_do(vexpress_power_off_device, "power off");
45 static struct device *vexpress_restart_device;
47 static int vexpress_restart(struct notifier_block *this, unsigned long mode,
50 vexpress_reset_do(vexpress_restart_device, "restart");
55 static struct notifier_block vexpress_restart_nb = {
56 .notifier_call = vexpress_restart,
60 static ssize_t vexpress_reset_active_show(struct device *dev,
61 struct device_attribute *attr, char *buf)
63 return sprintf(buf, "%d\n", vexpress_restart_device == dev);
66 static ssize_t vexpress_reset_active_store(struct device *dev,
67 struct device_attribute *attr, const char *buf, size_t count)
70 int err = kstrtol(buf, 0, &value);
73 vexpress_restart_device = dev;
75 return err ? err : count;
78 static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
79 vexpress_reset_active_store);
82 enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
84 static const struct of_device_id vexpress_reset_of_match[] = {
86 .compatible = "arm,vexpress-reset",
87 .data = (void *)FUNC_RESET,
89 .compatible = "arm,vexpress-shutdown",
90 .data = (void *)FUNC_SHUTDOWN
92 .compatible = "arm,vexpress-reboot",
93 .data = (void *)FUNC_REBOOT
98 static int _vexpress_register_restart_handler(struct device *dev)
102 vexpress_restart_device = dev;
103 if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) {
104 err = register_restart_handler(&vexpress_restart_nb);
106 dev_err(dev, "cannot register restart handler (err=%d)\n", err);
107 atomic_dec(&vexpress_restart_nb_refcnt);
111 device_create_file(dev, &dev_attr_active);
116 static int vexpress_reset_probe(struct platform_device *pdev)
118 const struct of_device_id *match =
119 of_match_device(vexpress_reset_of_match, &pdev->dev);
120 struct regmap *regmap;
126 regmap = devm_regmap_init_vexpress_config(&pdev->dev);
128 return PTR_ERR(regmap);
129 dev_set_drvdata(&pdev->dev, regmap);
131 switch ((enum vexpress_reset_func)match->data) {
133 vexpress_power_off_device = &pdev->dev;
134 pm_power_off = vexpress_power_off;
137 if (!vexpress_restart_device)
138 ret = _vexpress_register_restart_handler(&pdev->dev);
141 ret = _vexpress_register_restart_handler(&pdev->dev);
148 static struct platform_driver vexpress_reset_driver = {
149 .probe = vexpress_reset_probe,
151 .name = "vexpress-reset",
152 .of_match_table = vexpress_reset_of_match,
153 .suppress_bind_attrs = true,
157 static int __init vexpress_reset_init(void)
159 return platform_driver_register(&vexpress_reset_driver);
161 device_initcall(vexpress_reset_init);