xref: /OK3568_Linux_fs/kernel/drivers/power/reset/syscon-reboot.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Generic Syscon Reboot Driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2013, Applied Micro Circuits Corporation
6*4882a593Smuzhiyun  * Author: Feng Kan <fkan@apm.com>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun #include <linux/io.h>
10*4882a593Smuzhiyun #include <linux/notifier.h>
11*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
12*4882a593Smuzhiyun #include <linux/of_address.h>
13*4882a593Smuzhiyun #include <linux/of_device.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/reboot.h>
16*4882a593Smuzhiyun #include <linux/regmap.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun struct syscon_reboot_context {
19*4882a593Smuzhiyun 	struct regmap *map;
20*4882a593Smuzhiyun 	u32 offset;
21*4882a593Smuzhiyun 	u32 value;
22*4882a593Smuzhiyun 	u32 mask;
23*4882a593Smuzhiyun 	struct notifier_block restart_handler;
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun 
syscon_restart_handle(struct notifier_block * this,unsigned long mode,void * cmd)26*4882a593Smuzhiyun static int syscon_restart_handle(struct notifier_block *this,
27*4882a593Smuzhiyun 					unsigned long mode, void *cmd)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	struct syscon_reboot_context *ctx =
30*4882a593Smuzhiyun 			container_of(this, struct syscon_reboot_context,
31*4882a593Smuzhiyun 					restart_handler);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	/* Issue the reboot */
34*4882a593Smuzhiyun 	regmap_update_bits(ctx->map, ctx->offset, ctx->mask, ctx->value);
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	mdelay(1000);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	pr_emerg("Unable to restart system\n");
39*4882a593Smuzhiyun 	return NOTIFY_DONE;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
syscon_reboot_probe(struct platform_device * pdev)42*4882a593Smuzhiyun static int syscon_reboot_probe(struct platform_device *pdev)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct syscon_reboot_context *ctx;
45*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
46*4882a593Smuzhiyun 	int mask_err, value_err;
47*4882a593Smuzhiyun 	int err;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
50*4882a593Smuzhiyun 	if (!ctx)
51*4882a593Smuzhiyun 		return -ENOMEM;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
54*4882a593Smuzhiyun 	if (IS_ERR(ctx->map)) {
55*4882a593Smuzhiyun 		ctx->map = syscon_node_to_regmap(dev->parent->of_node);
56*4882a593Smuzhiyun 		if (IS_ERR(ctx->map))
57*4882a593Smuzhiyun 			return PTR_ERR(ctx->map);
58*4882a593Smuzhiyun 	}
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset))
61*4882a593Smuzhiyun 		return -EINVAL;
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	value_err = of_property_read_u32(pdev->dev.of_node, "value", &ctx->value);
64*4882a593Smuzhiyun 	mask_err = of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask);
65*4882a593Smuzhiyun 	if (value_err && mask_err) {
66*4882a593Smuzhiyun 		dev_err(dev, "unable to read 'value' and 'mask'");
67*4882a593Smuzhiyun 		return -EINVAL;
68*4882a593Smuzhiyun 	}
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	if (value_err) {
71*4882a593Smuzhiyun 		/* support old binding */
72*4882a593Smuzhiyun 		ctx->value = ctx->mask;
73*4882a593Smuzhiyun 		ctx->mask = 0xFFFFFFFF;
74*4882a593Smuzhiyun 	} else if (mask_err) {
75*4882a593Smuzhiyun 		/* support value without mask*/
76*4882a593Smuzhiyun 		ctx->mask = 0xFFFFFFFF;
77*4882a593Smuzhiyun 	}
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	ctx->restart_handler.notifier_call = syscon_restart_handle;
80*4882a593Smuzhiyun 	ctx->restart_handler.priority = 192;
81*4882a593Smuzhiyun 	err = register_restart_handler(&ctx->restart_handler);
82*4882a593Smuzhiyun 	if (err)
83*4882a593Smuzhiyun 		dev_err(dev, "can't register restart notifier (err=%d)\n", err);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return err;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun static const struct of_device_id syscon_reboot_of_match[] = {
89*4882a593Smuzhiyun 	{ .compatible = "syscon-reboot" },
90*4882a593Smuzhiyun 	{}
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun static struct platform_driver syscon_reboot_driver = {
94*4882a593Smuzhiyun 	.probe = syscon_reboot_probe,
95*4882a593Smuzhiyun 	.driver = {
96*4882a593Smuzhiyun 		.name = "syscon-reboot",
97*4882a593Smuzhiyun 		.of_match_table = syscon_reboot_of_match,
98*4882a593Smuzhiyun 	},
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun builtin_platform_driver(syscon_reboot_driver);
101