vexpress-poweroff.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 void vexpress_power_off(void)
  34. {
  35. vexpress_reset_do(vexpress_power_off_device, "power off");
  36. }
  37. static struct device *vexpress_restart_device;
  38. static int vexpress_restart(struct notifier_block *this, unsigned long mode,
  39. void *cmd)
  40. {
  41. vexpress_reset_do(vexpress_restart_device, "restart");
  42. return NOTIFY_DONE;
  43. }
  44. static struct notifier_block vexpress_restart_nb = {
  45. .notifier_call = vexpress_restart,
  46. .priority = 128,
  47. };
  48. static ssize_t vexpress_reset_active_show(struct device *dev,
  49. struct device_attribute *attr, char *buf)
  50. {
  51. return sprintf(buf, "%d\n", vexpress_restart_device == dev);
  52. }
  53. static ssize_t vexpress_reset_active_store(struct device *dev,
  54. struct device_attribute *attr, const char *buf, size_t count)
  55. {
  56. long value;
  57. int err = kstrtol(buf, 0, &value);
  58. if (!err && value)
  59. vexpress_restart_device = dev;
  60. return err ? err : count;
  61. }
  62. static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
  63. vexpress_reset_active_store);
  64. enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
  65. static const struct of_device_id vexpress_reset_of_match[] = {
  66. {
  67. .compatible = "arm,vexpress-reset",
  68. .data = (void *)FUNC_RESET,
  69. }, {
  70. .compatible = "arm,vexpress-shutdown",
  71. .data = (void *)FUNC_SHUTDOWN
  72. }, {
  73. .compatible = "arm,vexpress-reboot",
  74. .data = (void *)FUNC_REBOOT
  75. },
  76. {}
  77. };
  78. static int _vexpress_register_restart_handler(struct device *dev)
  79. {
  80. int err;
  81. vexpress_restart_device = dev;
  82. err = register_restart_handler(&vexpress_restart_nb);
  83. if (err) {
  84. dev_err(dev, "cannot register restart handler (err=%d)\n", err);
  85. return err;
  86. }
  87. device_create_file(dev, &dev_attr_active);
  88. return 0;
  89. }
  90. static int vexpress_reset_probe(struct platform_device *pdev)
  91. {
  92. const struct of_device_id *match =
  93. of_match_device(vexpress_reset_of_match, &pdev->dev);
  94. struct regmap *regmap;
  95. int ret = 0;
  96. if (!match)
  97. return -EINVAL;
  98. regmap = devm_regmap_init_vexpress_config(&pdev->dev);
  99. if (IS_ERR(regmap))
  100. return PTR_ERR(regmap);
  101. dev_set_drvdata(&pdev->dev, regmap);
  102. switch ((enum vexpress_reset_func)match->data) {
  103. case FUNC_SHUTDOWN:
  104. vexpress_power_off_device = &pdev->dev;
  105. pm_power_off = vexpress_power_off;
  106. break;
  107. case FUNC_RESET:
  108. if (!vexpress_restart_device)
  109. ret = _vexpress_register_restart_handler(&pdev->dev);
  110. break;
  111. case FUNC_REBOOT:
  112. ret = _vexpress_register_restart_handler(&pdev->dev);
  113. break;
  114. };
  115. return ret;
  116. }
  117. static struct platform_driver vexpress_reset_driver = {
  118. .probe = vexpress_reset_probe,
  119. .driver = {
  120. .name = "vexpress-reset",
  121. .of_match_table = vexpress_reset_of_match,
  122. },
  123. };
  124. static int __init vexpress_reset_init(void)
  125. {
  126. return platform_driver_register(&vexpress_reset_driver);
  127. }
  128. device_initcall(vexpress_reset_init);