1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic Syscon Reboot Driver
5 * Copyright (c) 2013, Applied Micro Circuits Corporation
6 * Author: Feng Kan <fkan@apm.com>
8 #include <linux/delay.h>
10 #include <linux/notifier.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/of_address.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/reboot.h>
16 #include <linux/regmap.h>
18 struct syscon_reboot_context
{
23 struct notifier_block restart_handler
;
26 static int syscon_restart_handle(struct notifier_block
*this,
27 unsigned long mode
, void *cmd
)
29 struct syscon_reboot_context
*ctx
=
30 container_of(this, struct syscon_reboot_context
,
33 /* Issue the reboot */
34 regmap_update_bits(ctx
->map
, ctx
->offset
, ctx
->mask
, ctx
->value
);
38 pr_emerg("Unable to restart system\n");
42 static int syscon_reboot_probe(struct platform_device
*pdev
)
44 struct syscon_reboot_context
*ctx
;
45 struct device
*dev
= &pdev
->dev
;
46 int mask_err
, value_err
;
49 ctx
= devm_kzalloc(&pdev
->dev
, sizeof(*ctx
), GFP_KERNEL
);
53 ctx
->map
= syscon_regmap_lookup_by_phandle(dev
->of_node
, "regmap");
55 return PTR_ERR(ctx
->map
);
57 if (of_property_read_u32(pdev
->dev
.of_node
, "offset", &ctx
->offset
))
60 value_err
= of_property_read_u32(pdev
->dev
.of_node
, "value", &ctx
->value
);
61 mask_err
= of_property_read_u32(pdev
->dev
.of_node
, "mask", &ctx
->mask
);
62 if (value_err
&& mask_err
) {
63 dev_err(dev
, "unable to read 'value' and 'mask'");
68 /* support old binding */
69 ctx
->value
= ctx
->mask
;
70 ctx
->mask
= 0xFFFFFFFF;
71 } else if (mask_err
) {
72 /* support value without mask*/
73 ctx
->mask
= 0xFFFFFFFF;
76 ctx
->restart_handler
.notifier_call
= syscon_restart_handle
;
77 ctx
->restart_handler
.priority
= 192;
78 err
= register_restart_handler(&ctx
->restart_handler
);
80 dev_err(dev
, "can't register restart notifier (err=%d)\n", err
);
85 static const struct of_device_id syscon_reboot_of_match
[] = {
86 { .compatible
= "syscon-reboot" },
90 static struct platform_driver syscon_reboot_driver
= {
91 .probe
= syscon_reboot_probe
,
93 .name
= "syscon-reboot",
94 .of_match_table
= syscon_reboot_of_match
,
97 builtin_platform_driver(syscon_reboot_driver
);