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/jiffies.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/stat.h>
19 #include <linux/vexpress.h>
21 static void vexpress_reset_do(struct device *dev, const char *what)
24 struct vexpress_config_func *func =
25 vexpress_config_func_get_by_dev(dev);
28 unsigned long timeout;
30 err = vexpress_config_write(func, 0, 0);
32 timeout = jiffies + HZ;
33 while (time_before(jiffies, timeout))
37 dev_emerg(dev, "Unable to %s (%d)\n", what, err);
40 static struct device *vexpress_power_off_device;
42 void vexpress_power_off(void)
44 vexpress_reset_do(vexpress_power_off_device, "power off");
47 static struct device *vexpress_restart_device;
49 void vexpress_restart(char str, const char *cmd)
51 vexpress_reset_do(vexpress_restart_device, "restart");
54 static ssize_t vexpress_reset_active_show(struct device *dev,
55 struct device_attribute *attr, char *buf)
57 return sprintf(buf, "%d\n", vexpress_restart_device == dev);
60 static ssize_t vexpress_reset_active_store(struct device *dev,
61 struct device_attribute *attr, const char *buf, size_t count)
64 int err = kstrtol(buf, 0, &value);
67 vexpress_restart_device = dev;
69 return err ? err : count;
72 DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
73 vexpress_reset_active_store);
76 enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
78 static struct of_device_id vexpress_reset_of_match[] = {
80 .compatible = "arm,vexpress-reset",
81 .data = (void *)FUNC_RESET,
83 .compatible = "arm,vexpress-shutdown",
84 .data = (void *)FUNC_SHUTDOWN
86 .compatible = "arm,vexpress-reboot",
87 .data = (void *)FUNC_REBOOT
92 static int vexpress_reset_probe(struct platform_device *pdev)
94 enum vexpress_reset_func func;
95 const struct of_device_id *match =
96 of_match_device(vexpress_reset_of_match, &pdev->dev);
99 func = (enum vexpress_reset_func)match->data;
101 func = pdev->id_entry->driver_data;
105 vexpress_power_off_device = &pdev->dev;
108 if (!vexpress_restart_device)
109 vexpress_restart_device = &pdev->dev;
110 device_create_file(&pdev->dev, &dev_attr_active);
113 vexpress_restart_device = &pdev->dev;
114 device_create_file(&pdev->dev, &dev_attr_active);
121 static const struct platform_device_id vexpress_reset_id_table[] = {
122 { .name = "vexpress-reset", .driver_data = FUNC_RESET, },
123 { .name = "vexpress-shutdown", .driver_data = FUNC_SHUTDOWN, },
124 { .name = "vexpress-reboot", .driver_data = FUNC_REBOOT, },
128 static struct platform_driver vexpress_reset_driver = {
129 .probe = vexpress_reset_probe,
131 .name = "vexpress-reset",
132 .of_match_table = vexpress_reset_of_match,
134 .id_table = vexpress_reset_id_table,
137 static int __init vexpress_reset_init(void)
139 return platform_driver_register(&vexpress_reset_driver);
141 device_initcall(vexpress_reset_init);