Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 1 | /* |
| 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 Moll | d08b803 | 2014-04-24 17:19:30 +0100 | [diff] [blame] | 14 | #include <linux/delay.h> |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 15 | #include <linux/notifier.h> |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
| 18 | #include <linux/platform_device.h> |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 19 | #include <linux/reboot.h> |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 20 | #include <linux/stat.h> |
| 21 | #include <linux/vexpress.h> |
| 22 | |
| 23 | static void vexpress_reset_do(struct device *dev, const char *what) |
| 24 | { |
| 25 | int err = -ENOENT; |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 26 | struct regmap *reg = dev_get_drvdata(dev); |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 27 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 28 | if (reg) { |
| 29 | err = regmap_write(reg, 0, 0); |
Pawel Moll | d08b803 | 2014-04-24 17:19:30 +0100 | [diff] [blame] | 30 | if (!err) |
| 31 | mdelay(1000); |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | dev_emerg(dev, "Unable to %s (%d)\n", what, err); |
| 35 | } |
| 36 | |
| 37 | static struct device *vexpress_power_off_device; |
| 38 | |
Catalin Marinas | 65deb78 | 2013-02-28 16:43:19 +0000 | [diff] [blame] | 39 | static void vexpress_power_off(void) |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 40 | { |
| 41 | vexpress_reset_do(vexpress_power_off_device, "power off"); |
| 42 | } |
| 43 | |
| 44 | static struct device *vexpress_restart_device; |
| 45 | |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 46 | static int vexpress_restart(struct notifier_block *this, unsigned long mode, |
| 47 | void *cmd) |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 48 | { |
| 49 | vexpress_reset_do(vexpress_restart_device, "restart"); |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 50 | |
| 51 | return NOTIFY_DONE; |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 52 | } |
| 53 | |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 54 | static struct notifier_block vexpress_restart_nb = { |
| 55 | .notifier_call = vexpress_restart, |
| 56 | .priority = 128, |
| 57 | }; |
| 58 | |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 59 | static ssize_t vexpress_reset_active_show(struct device *dev, |
| 60 | struct device_attribute *attr, char *buf) |
| 61 | { |
| 62 | return sprintf(buf, "%d\n", vexpress_restart_device == dev); |
| 63 | } |
| 64 | |
| 65 | static ssize_t vexpress_reset_active_store(struct device *dev, |
| 66 | struct device_attribute *attr, const char *buf, size_t count) |
| 67 | { |
| 68 | long value; |
| 69 | int err = kstrtol(buf, 0, &value); |
| 70 | |
| 71 | if (!err && value) |
| 72 | vexpress_restart_device = dev; |
| 73 | |
| 74 | return err ? err : count; |
| 75 | } |
| 76 | |
Ben Dooks | 0a14d28 | 2016-06-08 19:26:31 +0100 | [diff] [blame] | 77 | static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show, |
| 78 | vexpress_reset_active_store); |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 79 | |
| 80 | |
| 81 | enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT }; |
| 82 | |
Fabian Frederick | 8fb0885 | 2015-03-16 20:17:12 +0100 | [diff] [blame] | 83 | static const struct of_device_id vexpress_reset_of_match[] = { |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 84 | { |
| 85 | .compatible = "arm,vexpress-reset", |
| 86 | .data = (void *)FUNC_RESET, |
| 87 | }, { |
| 88 | .compatible = "arm,vexpress-shutdown", |
| 89 | .data = (void *)FUNC_SHUTDOWN |
| 90 | }, { |
| 91 | .compatible = "arm,vexpress-reboot", |
| 92 | .data = (void *)FUNC_REBOOT |
| 93 | }, |
| 94 | {} |
| 95 | }; |
| 96 | |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 97 | static int _vexpress_register_restart_handler(struct device *dev) |
| 98 | { |
| 99 | int err; |
| 100 | |
| 101 | vexpress_restart_device = dev; |
| 102 | err = register_restart_handler(&vexpress_restart_nb); |
| 103 | if (err) { |
| 104 | dev_err(dev, "cannot register restart handler (err=%d)\n", err); |
| 105 | return err; |
| 106 | } |
| 107 | device_create_file(dev, &dev_attr_active); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 112 | static int vexpress_reset_probe(struct platform_device *pdev) |
| 113 | { |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 114 | const struct of_device_id *match = |
| 115 | of_match_device(vexpress_reset_of_match, &pdev->dev); |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 116 | struct regmap *regmap; |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 117 | int ret = 0; |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 118 | |
Pawel Moll | 8f961c0 | 2015-01-20 17:24:02 +0000 | [diff] [blame] | 119 | if (!match) |
| 120 | return -EINVAL; |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 121 | |
Pawel Moll | 3b9334a | 2014-04-30 16:46:29 +0100 | [diff] [blame] | 122 | regmap = devm_regmap_init_vexpress_config(&pdev->dev); |
| 123 | if (IS_ERR(regmap)) |
| 124 | return PTR_ERR(regmap); |
| 125 | dev_set_drvdata(&pdev->dev, regmap); |
Pawel Moll | d08b803 | 2014-04-24 17:19:30 +0100 | [diff] [blame] | 126 | |
Pawel Moll | 8f961c0 | 2015-01-20 17:24:02 +0000 | [diff] [blame] | 127 | switch ((enum vexpress_reset_func)match->data) { |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 128 | case FUNC_SHUTDOWN: |
| 129 | vexpress_power_off_device = &pdev->dev; |
Catalin Marinas | 65deb78 | 2013-02-28 16:43:19 +0000 | [diff] [blame] | 130 | pm_power_off = vexpress_power_off; |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 131 | break; |
| 132 | case FUNC_RESET: |
| 133 | if (!vexpress_restart_device) |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 134 | ret = _vexpress_register_restart_handler(&pdev->dev); |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 135 | break; |
| 136 | case FUNC_REBOOT: |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 137 | ret = _vexpress_register_restart_handler(&pdev->dev); |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 138 | break; |
| 139 | }; |
| 140 | |
Guenter Roeck | 46c99ac | 2014-09-30 10:48:27 -0700 | [diff] [blame] | 141 | return ret; |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 144 | static struct platform_driver vexpress_reset_driver = { |
| 145 | .probe = vexpress_reset_probe, |
| 146 | .driver = { |
| 147 | .name = "vexpress-reset", |
| 148 | .of_match_table = vexpress_reset_of_match, |
| 149 | }, |
Pawel Moll | 790440b | 2012-10-18 17:58:47 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | static int __init vexpress_reset_init(void) |
| 153 | { |
| 154 | return platform_driver_register(&vexpress_reset_driver); |
| 155 | } |
| 156 | device_initcall(vexpress_reset_init); |