syscon-reboot.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. struct syscon_reboot_context {
  17. struct regmap *map;
  18. u32 offset;
  19. u32 value;
  20. u32 mask;
  21. struct notifier_block restart_handler;
  22. };
  23. static int syscon_restart_handle(struct notifier_block *this,
  24. unsigned long mode, void *cmd)
  25. {
  26. struct syscon_reboot_context *ctx =
  27. container_of(this, struct syscon_reboot_context,
  28. restart_handler);
  29. /* Issue the reboot */
  30. regmap_update_bits(ctx->map, ctx->offset, ctx->mask, ctx->value);
  31. mdelay(1000);
  32. pr_emerg("Unable to restart system\n");
  33. return NOTIFY_DONE;
  34. }
  35. static int syscon_reboot_probe(struct platform_device *pdev)
  36. {
  37. struct syscon_reboot_context *ctx;
  38. struct device *dev = &pdev->dev;
  39. int mask_err, value_err;
  40. int priority;
  41. int err;
  42. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  43. if (!ctx)
  44. return -ENOMEM;
  45. ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
  46. if (IS_ERR(ctx->map)) {
  47. ctx->map = syscon_node_to_regmap(dev->parent->of_node);
  48. if (IS_ERR(ctx->map))
  49. return PTR_ERR(ctx->map);
  50. }
  51. if (of_property_read_s32(pdev->dev.of_node, "priority", &priority))
  52. priority = 192;
  53. if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset))
  54. return -EINVAL;
  55. value_err = of_property_read_u32(pdev->dev.of_node, "value", &ctx->value);
  56. mask_err = of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask);
  57. if (value_err && mask_err) {
  58. dev_err(dev, "unable to read 'value' and 'mask'");
  59. return -EINVAL;
  60. }
  61. if (value_err) {
  62. /* support old binding */
  63. ctx->value = ctx->mask;
  64. ctx->mask = 0xFFFFFFFF;
  65. } else if (mask_err) {
  66. /* support value without mask*/
  67. ctx->mask = 0xFFFFFFFF;
  68. }
  69. ctx->restart_handler.notifier_call = syscon_restart_handle;
  70. ctx->restart_handler.priority = priority;
  71. err = register_restart_handler(&ctx->restart_handler);
  72. if (err)
  73. dev_err(dev, "can't register restart notifier (err=%d)\n", err);
  74. return err;
  75. }
  76. static const struct of_device_id syscon_reboot_of_match[] = {
  77. { .compatible = "syscon-reboot" },
  78. {}
  79. };
  80. static struct platform_driver syscon_reboot_driver = {
  81. .probe = syscon_reboot_probe,
  82. .driver = {
  83. .name = "syscon-reboot",
  84. .of_match_table = syscon_reboot_of_match,
  85. },
  86. };
  87. builtin_platform_driver(syscon_reboot_driver);