Feng Kan | 09fb07b | 2014-09-30 16:25:03 -0700 | [diff] [blame] | 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/io.h> |
| 18 | #include <linux/of_device.h> |
| 19 | #include <linux/of_address.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/notifier.h> |
| 22 | #include <linux/mfd/syscon.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/reboot.h> |
| 25 | |
| 26 | struct syscon_reboot_context { |
| 27 | struct regmap *map; |
| 28 | u32 offset; |
| 29 | u32 mask; |
| 30 | struct notifier_block restart_handler; |
| 31 | }; |
| 32 | |
| 33 | static struct syscon_reboot_context *syscon_reboot_ctx; |
| 34 | |
| 35 | static int syscon_restart_handle(struct notifier_block *this, |
| 36 | unsigned long mode, void *cmd) |
| 37 | { |
| 38 | struct syscon_reboot_context *ctx = syscon_reboot_ctx; |
| 39 | unsigned long timeout; |
| 40 | |
| 41 | /* Issue the reboot */ |
| 42 | if (ctx->map) |
| 43 | regmap_write(ctx->map, ctx->offset, ctx->mask); |
| 44 | |
| 45 | timeout = jiffies + HZ; |
| 46 | while (time_before(jiffies, timeout)) |
| 47 | cpu_relax(); |
| 48 | |
| 49 | pr_emerg("Unable to restart system\n"); |
| 50 | return NOTIFY_DONE; |
| 51 | } |
| 52 | |
| 53 | static int syscon_reboot_probe(struct platform_device *pdev) |
| 54 | { |
| 55 | struct syscon_reboot_context *ctx; |
| 56 | struct device *dev = &pdev->dev; |
| 57 | int err; |
| 58 | |
| 59 | ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL); |
| 60 | if (!ctx) |
| 61 | return -ENOMEM; |
| 62 | |
| 63 | ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap"); |
| 64 | if (IS_ERR(ctx->map)) |
| 65 | return PTR_ERR(ctx->map); |
| 66 | |
| 67 | if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset)) |
| 68 | return -EINVAL; |
| 69 | |
| 70 | if (of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask)) |
| 71 | return -EINVAL; |
| 72 | |
| 73 | ctx->restart_handler.notifier_call = syscon_restart_handle; |
| 74 | ctx->restart_handler.priority = 128; |
| 75 | err = register_restart_handler(&ctx->restart_handler); |
| 76 | if (err) |
| 77 | dev_err(dev, "can't register restart notifier (err=%d)\n", err); |
| 78 | |
| 79 | syscon_reboot_ctx = ctx; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static struct of_device_id syscon_reboot_of_match[] = { |
| 85 | { .compatible = "syscon-reboot" }, |
| 86 | {} |
| 87 | }; |
| 88 | |
| 89 | static struct platform_driver syscon_reboot_driver = { |
| 90 | .probe = syscon_reboot_probe, |
| 91 | .driver = { |
| 92 | .name = "syscon-reboot", |
| 93 | .of_match_table = syscon_reboot_of_match, |
| 94 | }, |
| 95 | }; |
| 96 | module_platform_driver(syscon_reboot_driver); |