Linux Audio

Check our new training course

Loading...
v6.13.7
  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		if (of_property_read_u32(pdev->dev.of_node, "reg", &ctx->offset))
 65			return -EINVAL;
 66
 67	value_err = of_property_read_u32(pdev->dev.of_node, "value", &ctx->value);
 68	mask_err = of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask);
 69	if (value_err && mask_err) {
 70		dev_err(dev, "unable to read 'value' and 'mask'");
 71		return -EINVAL;
 72	}
 73
 74	if (value_err) {
 75		/* support old binding */
 76		ctx->value = ctx->mask;
 77		ctx->mask = 0xFFFFFFFF;
 78	} else if (mask_err) {
 79		/* support value without mask*/
 80		ctx->mask = 0xFFFFFFFF;
 81	}
 82
 83	ctx->restart_handler.notifier_call = syscon_restart_handle;
 84	ctx->restart_handler.priority = priority;
 85	err = register_restart_handler(&ctx->restart_handler);
 86	if (err)
 87		dev_err(dev, "can't register restart notifier (err=%d)\n", err);
 88
 89	return err;
 90}
 91
 92static const struct of_device_id syscon_reboot_of_match[] = {
 93	{ .compatible = "syscon-reboot" },
 94	{}
 95};
 96
 97static struct platform_driver syscon_reboot_driver = {
 98	.probe = syscon_reboot_probe,
 99	.driver = {
100		.name = "syscon-reboot",
101		.of_match_table = syscon_reboot_of_match,
102	},
103};
104builtin_platform_driver(syscon_reboot_driver);
v5.4
 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_address.h>
13#include <linux/of_device.h>
14#include <linux/platform_device.h>
15#include <linux/reboot.h>
16#include <linux/regmap.h>
17
18struct syscon_reboot_context {
19	struct regmap *map;
20	u32 offset;
21	u32 value;
22	u32 mask;
23	struct notifier_block restart_handler;
24};
25
26static int syscon_restart_handle(struct notifier_block *this,
27					unsigned long mode, void *cmd)
28{
29	struct syscon_reboot_context *ctx =
30			container_of(this, struct syscon_reboot_context,
31					restart_handler);
32
33	/* Issue the reboot */
34	regmap_update_bits(ctx->map, ctx->offset, ctx->mask, ctx->value);
35
36	mdelay(1000);
37
38	pr_emerg("Unable to restart system\n");
39	return NOTIFY_DONE;
40}
41
42static int syscon_reboot_probe(struct platform_device *pdev)
43{
44	struct syscon_reboot_context *ctx;
45	struct device *dev = &pdev->dev;
46	int mask_err, value_err;
 
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		return PTR_ERR(ctx->map);
 
 
 
 
 
 
56
57	if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset))
58		return -EINVAL;
 
59
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'");
64		return -EINVAL;
65	}
66
67	if (value_err) {
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;
74	}
75
76	ctx->restart_handler.notifier_call = syscon_restart_handle;
77	ctx->restart_handler.priority = 192;
78	err = register_restart_handler(&ctx->restart_handler);
79	if (err)
80		dev_err(dev, "can't register restart notifier (err=%d)\n", err);
81
82	return err;
83}
84
85static const struct of_device_id syscon_reboot_of_match[] = {
86	{ .compatible = "syscon-reboot" },
87	{}
88};
89
90static struct platform_driver syscon_reboot_driver = {
91	.probe = syscon_reboot_probe,
92	.driver = {
93		.name = "syscon-reboot",
94		.of_match_table = syscon_reboot_of_match,
95	},
96};
97builtin_platform_driver(syscon_reboot_driver);