blob: 4dc102e2b230e9476119d4b3e1d439b17151e084 [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>
Pawel Moll790440b2012-10-18 17:58:47 +010015#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
Catalin Marinas65deb782013-02-28 16:43:19 +000021#include <asm/system_misc.h>
22
Pawel Moll790440b2012-10-18 17:58:47 +010023static 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;
38
Catalin Marinas65deb782013-02-28 16:43:19 +000039static void vexpress_power_off(void)
Pawel Moll790440b2012-10-18 17:58:47 +010040{
41 vexpress_reset_do(vexpress_power_off_device, "power off");
42}
43
44static struct device *vexpress_restart_device;
45
Robin Holt7b6d8642013-07-08 16:01:40 -070046static void vexpress_restart(enum reboot_mode reboot_mode, const char *cmd)
Pawel Moll790440b2012-10-18 17:58:47 +010047{
48 vexpress_reset_do(vexpress_restart_device, "restart");
49}
50
51static 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
57static 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
69DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
70 vexpress_reset_active_store);
71
72
73enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
74
75static 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
89static 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);
Pawel Moll3b9334a2014-04-30 16:46:29 +010094 struct regmap *regmap;
Pawel Moll790440b2012-10-18 17:58:47 +010095
96 if (match)
97 func = (enum vexpress_reset_func)match->data;
98 else
99 func = pdev->id_entry->driver_data;
100
Pawel Moll3b9334a2014-04-30 16:46:29 +0100101 regmap = devm_regmap_init_vexpress_config(&pdev->dev);
102 if (IS_ERR(regmap))
103 return PTR_ERR(regmap);
104 dev_set_drvdata(&pdev->dev, regmap);
Pawel Molld08b8032014-04-24 17:19:30 +0100105
Pawel Moll790440b2012-10-18 17:58:47 +0100106 switch (func) {
107 case FUNC_SHUTDOWN:
108 vexpress_power_off_device = &pdev->dev;
Catalin Marinas65deb782013-02-28 16:43:19 +0000109 pm_power_off = vexpress_power_off;
Pawel Moll790440b2012-10-18 17:58:47 +0100110 break;
111 case FUNC_RESET:
112 if (!vexpress_restart_device)
113 vexpress_restart_device = &pdev->dev;
Catalin Marinas65deb782013-02-28 16:43:19 +0000114 arm_pm_restart = vexpress_restart;
Pawel Moll790440b2012-10-18 17:58:47 +0100115 device_create_file(&pdev->dev, &dev_attr_active);
116 break;
117 case FUNC_REBOOT:
118 vexpress_restart_device = &pdev->dev;
Catalin Marinas65deb782013-02-28 16:43:19 +0000119 arm_pm_restart = vexpress_restart;
Pawel Moll790440b2012-10-18 17:58:47 +0100120 device_create_file(&pdev->dev, &dev_attr_active);
121 break;
122 };
123
124 return 0;
125}
126
127static 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
134static 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
143static int __init vexpress_reset_init(void)
144{
145 return platform_driver_register(&vexpress_reset_driver);
146}
147device_initcall(vexpress_reset_init);