Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Generic Syscon Reboot Driver
4 *
5 * Copyright (c) 2013, Applied Micro Circuits Corporation
6 * Author: Feng Kan <fkan@apm.com>
7 */
8#include <linux/delay.h>
9#include <linux/io.h>
10#include <linux/notifier.h>
11#include <linux/mfd/syscon.h>
12#include <linux/of.h>
13#include <linux/platform_device.h>
14#include <linux/reboot.h>
15#include <linux/regmap.h>
16
17struct syscon_reboot_context {
18 struct regmap *map;
19 u32 offset;
20 u32 value;
21 u32 mask;
22 struct notifier_block restart_handler;
23};
24
25static int syscon_restart_handle(struct notifier_block *this,
26 unsigned long mode, void *cmd)
27{
28 struct syscon_reboot_context *ctx =
29 container_of(this, struct syscon_reboot_context,
30 restart_handler);
31
32 /* Issue the reboot */
33 regmap_update_bits(ctx->map, ctx->offset, ctx->mask, ctx->value);
34
35 mdelay(1000);
36
37 pr_emerg("Unable to restart system\n");
38 return NOTIFY_DONE;
39}
40
41static int syscon_reboot_probe(struct platform_device *pdev)
42{
43 struct syscon_reboot_context *ctx;
44 struct device *dev = &pdev->dev;
45 int mask_err, value_err;
46 int priority;
47 int err;
48
49 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
50 if (!ctx)
51 return -ENOMEM;
52
53 ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
54 if (IS_ERR(ctx->map)) {
55 ctx->map = syscon_node_to_regmap(dev->parent->of_node);
56 if (IS_ERR(ctx->map))
57 return PTR_ERR(ctx->map);
58 }
59
60 if (of_property_read_s32(pdev->dev.of_node, "priority", &priority))
61 priority = 192;
62
63 if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset))
64 return -EINVAL;
65
66 value_err = of_property_read_u32(pdev->dev.of_node, "value", &ctx->value);
67 mask_err = of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask);
68 if (value_err && mask_err) {
69 dev_err(dev, "unable to read 'value' and 'mask'");
70 return -EINVAL;
71 }
72
73 if (value_err) {
74 /* support old binding */
75 ctx->value = ctx->mask;
76 ctx->mask = 0xFFFFFFFF;
77 } else if (mask_err) {
78 /* support value without mask*/
79 ctx->mask = 0xFFFFFFFF;
80 }
81
82 ctx->restart_handler.notifier_call = syscon_restart_handle;
83 ctx->restart_handler.priority = priority;
84 err = register_restart_handler(&ctx->restart_handler);
85 if (err)
86 dev_err(dev, "can't register restart notifier (err=%d)\n", err);
87
88 return err;
89}
90
91static const struct of_device_id syscon_reboot_of_match[] = {
92 { .compatible = "syscon-reboot" },
93 {}
94};
95
96static struct platform_driver syscon_reboot_driver = {
97 .probe = syscon_reboot_probe,
98 .driver = {
99 .name = "syscon-reboot",
100 .of_match_table = syscon_reboot_of_match,
101 },
102};
103builtin_platform_driver(syscon_reboot_driver);
1/*
2 * Generic Syscon Reboot Driver
3 *
4 * Copyright (c) 2013, Applied Micro Circuits Corporation
5 * Author: Feng Kan <fkan@apm.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17#include <linux/delay.h>
18#include <linux/io.h>
19#include <linux/notifier.h>
20#include <linux/mfd/syscon.h>
21#include <linux/of_address.h>
22#include <linux/of_device.h>
23#include <linux/platform_device.h>
24#include <linux/reboot.h>
25#include <linux/regmap.h>
26
27struct syscon_reboot_context {
28 struct regmap *map;
29 u32 offset;
30 u32 mask;
31 struct notifier_block restart_handler;
32};
33
34static int syscon_restart_handle(struct notifier_block *this,
35 unsigned long mode, void *cmd)
36{
37 struct syscon_reboot_context *ctx =
38 container_of(this, struct syscon_reboot_context,
39 restart_handler);
40
41 /* Issue the reboot */
42 regmap_write(ctx->map, ctx->offset, ctx->mask);
43
44 mdelay(1000);
45
46 pr_emerg("Unable to restart system\n");
47 return NOTIFY_DONE;
48}
49
50static int syscon_reboot_probe(struct platform_device *pdev)
51{
52 struct syscon_reboot_context *ctx;
53 struct device *dev = &pdev->dev;
54 int err;
55
56 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
57 if (!ctx)
58 return -ENOMEM;
59
60 ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
61 if (IS_ERR(ctx->map))
62 return PTR_ERR(ctx->map);
63
64 if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset))
65 return -EINVAL;
66
67 if (of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask))
68 return -EINVAL;
69
70 ctx->restart_handler.notifier_call = syscon_restart_handle;
71 ctx->restart_handler.priority = 192;
72 err = register_restart_handler(&ctx->restart_handler);
73 if (err)
74 dev_err(dev, "can't register restart notifier (err=%d)\n", err);
75
76 return err;
77}
78
79static const struct of_device_id syscon_reboot_of_match[] = {
80 { .compatible = "syscon-reboot" },
81 {}
82};
83
84static struct platform_driver syscon_reboot_driver = {
85 .probe = syscon_reboot_probe,
86 .driver = {
87 .name = "syscon-reboot",
88 .of_match_table = syscon_reboot_of_match,
89 },
90};
91builtin_platform_driver(syscon_reboot_driver);