vexpress-poweroff.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright (C) 2012 ARM Limited
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/notifier.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reboot.h>
  19. #include <linux/stat.h>
  20. #include <linux/vexpress.h>
  21. static void vexpress_reset_do(struct device *dev, const char *what)
  22. {
  23. int err = -ENOENT;
  24. struct regmap *reg = dev_get_drvdata(dev);
  25. if (reg) {
  26. err = regmap_write(reg, 0, 0);
  27. if (!err)
  28. mdelay(1000);
  29. }
  30. dev_emerg(dev, "Unable to %s (%d)\n", what, err);
  31. }
  32. static struct device *vexpress_power_off_device;
  33. static atomic_t vexpress_restart_nb_refcnt = ATOMIC_INIT(0);
  34. static void vexpress_power_off(void)
  35. {
  36. vexpress_reset_do(vexpress_power_off_device, "power off");
  37. }
  38. static struct device *vexpress_restart_device;
  39. static int vexpress_restart(struct notifier_block *this, unsigned long mode,
  40. void *cmd)
  41. {
  42. vexpress_reset_do(vexpress_restart_device, "restart");
  43. return NOTIFY_DONE;
  44. }
  45. static struct notifier_block vexpress_restart_nb = {
  46. .notifier_call = vexpress_restart,
  47. .priority = 128,
  48. };
  49. static ssize_t vexpress_reset_active_show(struct device *dev,
  50. struct device_attribute *attr, char *buf)
  51. {
  52. return sprintf(buf, "%d\n", vexpress_restart_device == dev);
  53. }
  54. static ssize_t vexpress_reset_active_store(struct device *dev,
  55. struct device_attribute *attr, const char *buf, size_t count)
  56. {
  57. long value;
  58. int err = kstrtol(buf, 0, &value);
  59. if (!err && value)
  60. vexpress_restart_device = dev;
  61. return err ? err : count;
  62. }
  63. DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
  64. vexpress_reset_active_store);
  65. enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
  66. static const struct of_device_id vexpress_reset_of_match[] = {
  67. {
  68. .compatible = "arm,vexpress-reset",
  69. .data = (void *)FUNC_RESET,
  70. }, {
  71. .compatible = "arm,vexpress-shutdown",
  72. .data = (void *)FUNC_SHUTDOWN
  73. }, {
  74. .compatible = "arm,vexpress-reboot",
  75. .data = (void *)FUNC_REBOOT
  76. },
  77. {}
  78. };
  79. static int _vexpress_register_restart_handler(struct device *dev)
  80. {
  81. int err;
  82. vexpress_restart_device = dev;
  83. if (atomic_inc_return(&vexpress_restart_nb_refcnt) == 1) {
  84. err = register_restart_handler(&vexpress_restart_nb);
  85. if (err) {
  86. dev_err(dev, "cannot register restart handler (err=%d)\n", err);
  87. atomic_dec(&vexpress_restart_nb_refcnt);
  88. return err;
  89. }
  90. }
  91. device_create_file(dev, &dev_attr_active);
  92. return 0;
  93. }
  94. static int vexpress_reset_probe(struct platform_device *pdev)
  95. {
  96. const struct of_device_id *match =
  97. of_match_device(vexpress_reset_of_match, &pdev->dev);
  98. struct regmap *regmap;
  99. int ret = 0;
  100. if (!match)
  101. return -EINVAL;
  102. regmap = devm_regmap_init_vexpress_config(&pdev->dev);
  103. if (IS_ERR(regmap))
  104. return PTR_ERR(regmap);
  105. dev_set_drvdata(&pdev->dev, regmap);
  106. switch ((enum vexpress_reset_func)match->data) {
  107. case FUNC_SHUTDOWN:
  108. vexpress_power_off_device = &pdev->dev;
  109. pm_power_off = vexpress_power_off;
  110. break;
  111. case FUNC_RESET:
  112. if (!vexpress_restart_device)
  113. ret = _vexpress_register_restart_handler(&pdev->dev);
  114. break;
  115. case FUNC_REBOOT:
  116. ret = _vexpress_register_restart_handler(&pdev->dev);
  117. break;
  118. };
  119. return ret;
  120. }
  121. static struct platform_driver vexpress_reset_driver = {
  122. .probe = vexpress_reset_probe,
  123. .driver = {
  124. .name = "vexpress-reset",
  125. .of_match_table = vexpress_reset_of_match,
  126. },
  127. };
  128. static int __init vexpress_reset_init(void)
  129. {
  130. return platform_driver_register(&vexpress_reset_driver);
  131. }
  132. device_initcall(vexpress_reset_init);