vexpress-poweroff.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. 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 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. enum vexpress_reset_func func;
  93. const struct of_device_id *match =
  94. of_match_device(vexpress_reset_of_match, &pdev->dev);
  95. struct regmap *regmap;
  96. int ret = 0;
  97. if (match)
  98. func = (enum vexpress_reset_func)match->data;
  99. else
  100. func = pdev->id_entry->driver_data;
  101. regmap = devm_regmap_init_vexpress_config(&pdev->dev);
  102. if (IS_ERR(regmap))
  103. return PTR_ERR(regmap);
  104. dev_set_drvdata(&pdev->dev, regmap);
  105. switch (func) {
  106. case FUNC_SHUTDOWN:
  107. vexpress_power_off_device = &pdev->dev;
  108. pm_power_off = vexpress_power_off;
  109. break;
  110. case FUNC_RESET:
  111. if (!vexpress_restart_device)
  112. ret = _vexpress_register_restart_handler(&pdev->dev);
  113. break;
  114. case FUNC_REBOOT:
  115. ret = _vexpress_register_restart_handler(&pdev->dev);
  116. break;
  117. };
  118. return ret;
  119. }
  120. static const struct platform_device_id vexpress_reset_id_table[] = {
  121. { .name = "vexpress-reset", .driver_data = FUNC_RESET, },
  122. { .name = "vexpress-shutdown", .driver_data = FUNC_SHUTDOWN, },
  123. { .name = "vexpress-reboot", .driver_data = FUNC_REBOOT, },
  124. {}
  125. };
  126. static struct platform_driver vexpress_reset_driver = {
  127. .probe = vexpress_reset_probe,
  128. .driver = {
  129. .name = "vexpress-reset",
  130. .of_match_table = vexpress_reset_of_match,
  131. },
  132. .id_table = vexpress_reset_id_table,
  133. };
  134. static int __init vexpress_reset_init(void)
  135. {
  136. return platform_driver_register(&vexpress_reset_driver);
  137. }
  138. device_initcall(vexpress_reset_init);