blob: 7d0d269a0837c0b84e9f72eeabbab354159a3b8e [file] [log] [blame]
Feng Kan09fb07b2014-09-30 16:25:03 -07001/*
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 */
Feng Kanafaebbd2014-10-02 11:24:15 -070017#include <linux/delay.h>
Feng Kan09fb07b2014-09-30 16:25:03 -070018#include <linux/io.h>
Feng Kan09fb07b2014-09-30 16:25:03 -070019#include <linux/notifier.h>
20#include <linux/mfd/syscon.h>
Feng Kanafaebbd2014-10-02 11:24:15 -070021#include <linux/of_address.h>
22#include <linux/of_device.h>
23#include <linux/platform_device.h>
Feng Kan09fb07b2014-09-30 16:25:03 -070024#include <linux/reboot.h>
Feng Kanafaebbd2014-10-02 11:24:15 -070025#include <linux/regmap.h>
Feng Kan09fb07b2014-09-30 16:25:03 -070026
27struct syscon_reboot_context {
28 struct regmap *map;
29 u32 offset;
30 u32 mask;
31 struct notifier_block restart_handler;
32};
33
Feng Kan09fb07b2014-09-30 16:25:03 -070034static int syscon_restart_handle(struct notifier_block *this,
35 unsigned long mode, void *cmd)
36{
Feng Kanafaebbd2014-10-02 11:24:15 -070037 struct syscon_reboot_context *ctx =
38 container_of(this, struct syscon_reboot_context,
39 restart_handler);
Feng Kan09fb07b2014-09-30 16:25:03 -070040
41 /* Issue the reboot */
Feng Kanafaebbd2014-10-02 11:24:15 -070042 regmap_write(ctx->map, ctx->offset, ctx->mask);
Feng Kan09fb07b2014-09-30 16:25:03 -070043
Feng Kanafaebbd2014-10-02 11:24:15 -070044 mdelay(1000);
Feng Kan09fb07b2014-09-30 16:25:03 -070045
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;
Stefan Agnerb81180b2014-12-02 18:11:58 +010071 ctx->restart_handler.priority = 192;
Feng Kan09fb07b2014-09-30 16:25:03 -070072 err = register_restart_handler(&ctx->restart_handler);
73 if (err)
74 dev_err(dev, "can't register restart notifier (err=%d)\n", err);
75
Feng Kanafaebbd2014-10-02 11:24:15 -070076 return err;
Feng Kan09fb07b2014-09-30 16:25:03 -070077}
78
Fabian Frederick8fb08852015-03-16 20:17:12 +010079static const struct of_device_id syscon_reboot_of_match[] = {
Feng Kan09fb07b2014-09-30 16:25:03 -070080 { .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};
Paul Gortmakere35415e2015-05-01 20:10:58 -040091builtin_platform_driver(syscon_reboot_driver);