^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2012 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/vexpress.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static void vexpress_reset_do(struct device *dev, const char *what)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct regmap *reg = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) if (reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) err = regmap_write(reg, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) mdelay(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) dev_emerg(dev, "Unable to %s (%d)\n", what, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct device *vexpress_power_off_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static void vexpress_power_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) vexpress_reset_do(vexpress_power_off_device, "power off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static struct device *vexpress_restart_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int vexpress_restart(struct notifier_block *this, unsigned long mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) void *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) vexpress_reset_do(vexpress_restart_device, "restart");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static struct notifier_block vexpress_restart_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .notifier_call = vexpress_restart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .priority = 128,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static ssize_t vexpress_reset_active_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return sprintf(buf, "%d\n", vexpress_restart_device == dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static ssize_t vexpress_reset_active_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int err = kstrtol(buf, 0, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!err && value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) vexpress_restart_device = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return err ? err : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) vexpress_reset_active_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static const struct of_device_id vexpress_reset_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .compatible = "arm,vexpress-reset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .data = (void *)FUNC_RESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .compatible = "arm,vexpress-shutdown",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .data = (void *)FUNC_SHUTDOWN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .compatible = "arm,vexpress-reboot",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .data = (void *)FUNC_REBOOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static int _vexpress_register_restart_handler(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) vexpress_restart_device = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) err = register_restart_handler(&vexpress_restart_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) dev_err(dev, "cannot register restart handler (err=%d)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) atomic_dec(&vexpress_restart_nb_refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) device_create_file(dev, &dev_attr_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int vexpress_reset_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) const struct of_device_id *match =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) of_match_device(vexpress_reset_of_match, &pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!match)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) regmap = devm_regmap_init_vexpress_config(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (IS_ERR(regmap))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return PTR_ERR(regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) dev_set_drvdata(&pdev->dev, regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) switch ((enum vexpress_reset_func)match->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) case FUNC_SHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) vexpress_power_off_device = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pm_power_off = vexpress_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) case FUNC_RESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!vexpress_restart_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ret = _vexpress_register_restart_handler(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) case FUNC_REBOOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) ret = _vexpress_register_restart_handler(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static struct platform_driver vexpress_reset_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) .probe = vexpress_reset_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .name = "vexpress-reset",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .of_match_table = vexpress_reset_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .suppress_bind_attrs = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) builtin_platform_driver(vexpress_reset_driver);