18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2012 ARM Limited
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/delay.h>
88c2ecf20Sopenharmony_ci#include <linux/notifier.h>
98c2ecf20Sopenharmony_ci#include <linux/of.h>
108c2ecf20Sopenharmony_ci#include <linux/of_device.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/reboot.h>
138c2ecf20Sopenharmony_ci#include <linux/stat.h>
148c2ecf20Sopenharmony_ci#include <linux/vexpress.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_cistatic void vexpress_reset_do(struct device *dev, const char *what)
178c2ecf20Sopenharmony_ci{
188c2ecf20Sopenharmony_ci	int err = -ENOENT;
198c2ecf20Sopenharmony_ci	struct regmap *reg = dev_get_drvdata(dev);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci	if (reg) {
228c2ecf20Sopenharmony_ci		err = regmap_write(reg, 0, 0);
238c2ecf20Sopenharmony_ci		if (!err)
248c2ecf20Sopenharmony_ci			mdelay(1000);
258c2ecf20Sopenharmony_ci	}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	dev_emerg(dev, "Unable to %s (%d)\n", what, err);
288c2ecf20Sopenharmony_ci}
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic struct device *vexpress_power_off_device;
318c2ecf20Sopenharmony_cistatic atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0);
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic void vexpress_power_off(void)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	vexpress_reset_do(vexpress_power_off_device, "power off");
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic struct device *vexpress_restart_device;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int vexpress_restart(struct notifier_block *this, unsigned long mode,
418c2ecf20Sopenharmony_ci			     void *cmd)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	vexpress_reset_do(vexpress_restart_device, "restart");
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic struct notifier_block vexpress_restart_nb = {
498c2ecf20Sopenharmony_ci	.notifier_call = vexpress_restart,
508c2ecf20Sopenharmony_ci	.priority = 128,
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic ssize_t vexpress_reset_active_show(struct device *dev,
548c2ecf20Sopenharmony_ci		struct device_attribute *attr, char *buf)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", vexpress_restart_device == dev);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic ssize_t vexpress_reset_active_store(struct device *dev,
608c2ecf20Sopenharmony_ci		struct device_attribute *attr, const char *buf, size_t count)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	long value;
638c2ecf20Sopenharmony_ci	int err = kstrtol(buf, 0, &value);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (!err && value)
668c2ecf20Sopenharmony_ci		vexpress_restart_device = dev;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	return err ? err : count;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
728c2ecf20Sopenharmony_ci		   vexpress_reset_active_store);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cienum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic const struct of_device_id vexpress_reset_of_match[] = {
788c2ecf20Sopenharmony_ci	{
798c2ecf20Sopenharmony_ci		.compatible = "arm,vexpress-reset",
808c2ecf20Sopenharmony_ci		.data = (void *)FUNC_RESET,
818c2ecf20Sopenharmony_ci	}, {
828c2ecf20Sopenharmony_ci		.compatible = "arm,vexpress-shutdown",
838c2ecf20Sopenharmony_ci		.data = (void *)FUNC_SHUTDOWN
848c2ecf20Sopenharmony_ci	}, {
858c2ecf20Sopenharmony_ci		.compatible = "arm,vexpress-reboot",
868c2ecf20Sopenharmony_ci		.data = (void *)FUNC_REBOOT
878c2ecf20Sopenharmony_ci	},
888c2ecf20Sopenharmony_ci	{}
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic int _vexpress_register_restart_handler(struct device *dev)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	int err;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	vexpress_restart_device = dev;
968c2ecf20Sopenharmony_ci	if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) {
978c2ecf20Sopenharmony_ci		err = register_restart_handler(&vexpress_restart_nb);
988c2ecf20Sopenharmony_ci		if (err) {
998c2ecf20Sopenharmony_ci			dev_err(dev, "cannot register restart handler (err=%d)\n", err);
1008c2ecf20Sopenharmony_ci			atomic_dec(&vexpress_restart_nb_refcnt);
1018c2ecf20Sopenharmony_ci			return err;
1028c2ecf20Sopenharmony_ci		}
1038c2ecf20Sopenharmony_ci	}
1048c2ecf20Sopenharmony_ci	device_create_file(dev, &dev_attr_active);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	return 0;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic int vexpress_reset_probe(struct platform_device *pdev)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	const struct of_device_id *match =
1128c2ecf20Sopenharmony_ci			of_match_device(vexpress_reset_of_match, &pdev->dev);
1138c2ecf20Sopenharmony_ci	struct regmap *regmap;
1148c2ecf20Sopenharmony_ci	int ret = 0;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	if (!match)
1178c2ecf20Sopenharmony_ci		return -EINVAL;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	regmap = devm_regmap_init_vexpress_config(&pdev->dev);
1208c2ecf20Sopenharmony_ci	if (IS_ERR(regmap))
1218c2ecf20Sopenharmony_ci		return PTR_ERR(regmap);
1228c2ecf20Sopenharmony_ci	dev_set_drvdata(&pdev->dev, regmap);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	switch ((enum vexpress_reset_func)match->data) {
1258c2ecf20Sopenharmony_ci	case FUNC_SHUTDOWN:
1268c2ecf20Sopenharmony_ci		vexpress_power_off_device = &pdev->dev;
1278c2ecf20Sopenharmony_ci		pm_power_off = vexpress_power_off;
1288c2ecf20Sopenharmony_ci		break;
1298c2ecf20Sopenharmony_ci	case FUNC_RESET:
1308c2ecf20Sopenharmony_ci		if (!vexpress_restart_device)
1318c2ecf20Sopenharmony_ci			ret = _vexpress_register_restart_handler(&pdev->dev);
1328c2ecf20Sopenharmony_ci		break;
1338c2ecf20Sopenharmony_ci	case FUNC_REBOOT:
1348c2ecf20Sopenharmony_ci		ret = _vexpress_register_restart_handler(&pdev->dev);
1358c2ecf20Sopenharmony_ci		break;
1368c2ecf20Sopenharmony_ci	};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	return ret;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic struct platform_driver vexpress_reset_driver = {
1428c2ecf20Sopenharmony_ci	.probe = vexpress_reset_probe,
1438c2ecf20Sopenharmony_ci	.driver = {
1448c2ecf20Sopenharmony_ci		.name = "vexpress-reset",
1458c2ecf20Sopenharmony_ci		.of_match_table = vexpress_reset_of_match,
1468c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
1478c2ecf20Sopenharmony_ci	},
1488c2ecf20Sopenharmony_ci};
1498c2ecf20Sopenharmony_cibuiltin_platform_driver(vexpress_reset_driver);
150