Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Copyright (C) 2012 ARM Limited
0005  */
0006 
0007 #include <linux/delay.h>
0008 #include <linux/notifier.h>
0009 #include <linux/of.h>
0010 #include <linux/of_device.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/reboot.h>
0013 #include <linux/stat.h>
0014 #include <linux/vexpress.h>
0015 
0016 static void vexpress_reset_do(struct device *dev, const char *what)
0017 {
0018     int err = -ENOENT;
0019     struct regmap *reg = dev_get_drvdata(dev);
0020 
0021     if (reg) {
0022         err = regmap_write(reg, 0, 0);
0023         if (!err)
0024             mdelay(1000);
0025     }
0026 
0027     dev_emerg(dev, "Unable to %s (%d)\n", what, err);
0028 }
0029 
0030 static struct device *vexpress_power_off_device;
0031 static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0);
0032 
0033 static void vexpress_power_off(void)
0034 {
0035     vexpress_reset_do(vexpress_power_off_device, "power off");
0036 }
0037 
0038 static struct device *vexpress_restart_device;
0039 
0040 static int vexpress_restart(struct notifier_block *this, unsigned long mode,
0041                  void *cmd)
0042 {
0043     vexpress_reset_do(vexpress_restart_device, "restart");
0044 
0045     return NOTIFY_DONE;
0046 }
0047 
0048 static struct notifier_block vexpress_restart_nb = {
0049     .notifier_call = vexpress_restart,
0050     .priority = 128,
0051 };
0052 
0053 static ssize_t vexpress_reset_active_show(struct device *dev,
0054         struct device_attribute *attr, char *buf)
0055 {
0056     return sprintf(buf, "%d\n", vexpress_restart_device == dev);
0057 }
0058 
0059 static ssize_t vexpress_reset_active_store(struct device *dev,
0060         struct device_attribute *attr, const char *buf, size_t count)
0061 {
0062     long value;
0063     int err = kstrtol(buf, 0, &value);
0064 
0065     if (!err && value)
0066         vexpress_restart_device = dev;
0067 
0068     return err ? err : count;
0069 }
0070 
0071 static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
0072            vexpress_reset_active_store);
0073 
0074 
0075 enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
0076 
0077 static const struct of_device_id vexpress_reset_of_match[] = {
0078     {
0079         .compatible = "arm,vexpress-reset",
0080         .data = (void *)FUNC_RESET,
0081     }, {
0082         .compatible = "arm,vexpress-shutdown",
0083         .data = (void *)FUNC_SHUTDOWN
0084     }, {
0085         .compatible = "arm,vexpress-reboot",
0086         .data = (void *)FUNC_REBOOT
0087     },
0088     {}
0089 };
0090 
0091 static int _vexpress_register_restart_handler(struct device *dev)
0092 {
0093     int err;
0094 
0095     vexpress_restart_device = dev;
0096     if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) {
0097         err = register_restart_handler(&vexpress_restart_nb);
0098         if (err) {
0099             dev_err(dev, "cannot register restart handler (err=%d)\n", err);
0100             atomic_dec(&vexpress_restart_nb_refcnt);
0101             return err;
0102         }
0103     }
0104     device_create_file(dev, &dev_attr_active);
0105 
0106     return 0;
0107 }
0108 
0109 static int vexpress_reset_probe(struct platform_device *pdev)
0110 {
0111     const struct of_device_id *match =
0112             of_match_device(vexpress_reset_of_match, &pdev->dev);
0113     struct regmap *regmap;
0114     int ret = 0;
0115 
0116     if (!match)
0117         return -EINVAL;
0118 
0119     regmap = devm_regmap_init_vexpress_config(&pdev->dev);
0120     if (IS_ERR(regmap))
0121         return PTR_ERR(regmap);
0122     dev_set_drvdata(&pdev->dev, regmap);
0123 
0124     switch ((enum vexpress_reset_func)match->data) {
0125     case FUNC_SHUTDOWN:
0126         vexpress_power_off_device = &pdev->dev;
0127         pm_power_off = vexpress_power_off;
0128         break;
0129     case FUNC_RESET:
0130         if (!vexpress_restart_device)
0131             ret = _vexpress_register_restart_handler(&pdev->dev);
0132         break;
0133     case FUNC_REBOOT:
0134         ret = _vexpress_register_restart_handler(&pdev->dev);
0135         break;
0136     }
0137 
0138     return ret;
0139 }
0140 
0141 static struct platform_driver vexpress_reset_driver = {
0142     .probe = vexpress_reset_probe,
0143     .driver = {
0144         .name = "vexpress-reset",
0145         .of_match_table = vexpress_reset_of_match,
0146         .suppress_bind_attrs = true,
0147     },
0148 };
0149 builtin_platform_driver(vexpress_reset_driver);