syscon-reboot.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. struct syscon_reboot_context {
  27. struct regmap *map;
  28. u32 offset;
  29. u32 mask;
  30. struct notifier_block restart_handler;
  31. };
  32. static int syscon_restart_handle(struct notifier_block *this,
  33. unsigned long mode, void *cmd)
  34. {
  35. struct syscon_reboot_context *ctx =
  36. container_of(this, struct syscon_reboot_context,
  37. restart_handler);
  38. /* Issue the reboot */
  39. regmap_write(ctx->map, ctx->offset, ctx->mask);
  40. mdelay(1000);
  41. pr_emerg("Unable to restart system\n");
  42. return NOTIFY_DONE;
  43. }
  44. static int syscon_reboot_probe(struct platform_device *pdev)
  45. {
  46. struct syscon_reboot_context *ctx;
  47. struct device *dev = &pdev->dev;
  48. int err;
  49. ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
  50. if (!ctx)
  51. return -ENOMEM;
  52. ctx->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
  53. if (IS_ERR(ctx->map))
  54. return PTR_ERR(ctx->map);
  55. if (of_property_read_u32(pdev->dev.of_node, "offset", &ctx->offset))
  56. return -EINVAL;
  57. if (of_property_read_u32(pdev->dev.of_node, "mask", &ctx->mask))
  58. return -EINVAL;
  59. ctx->restart_handler.notifier_call = syscon_restart_handle;
  60. ctx->restart_handler.priority = 192;
  61. err = register_restart_handler(&ctx->restart_handler);
  62. if (err)
  63. dev_err(dev, "can't register restart notifier (err=%d)\n", err);
  64. return err;
  65. }
  66. static const struct of_device_id syscon_reboot_of_match[] = {
  67. { .compatible = "syscon-reboot" },
  68. {}
  69. };
  70. static struct platform_driver syscon_reboot_driver = {
  71. .probe = syscon_reboot_probe,
  72. .driver = {
  73. .name = "syscon-reboot",
  74. .of_match_table = syscon_reboot_of_match,
  75. },
  76. };
  77. builtin_platform_driver(syscon_reboot_driver);