vexpress-poweroff.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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/jiffies.h>
  14. #include <linux/of.h>
  15. #include <linux/of_device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/stat.h>
  18. #include <linux/vexpress.h>
  19. #include <asm/system_misc.h>
  20. static void vexpress_reset_do(struct device *dev, const char *what)
  21. {
  22. int err = -ENOENT;
  23. struct vexpress_config_func *func =
  24. vexpress_config_func_get_by_dev(dev);
  25. if (func) {
  26. unsigned long timeout;
  27. err = vexpress_config_write(func, 0, 0);
  28. timeout = jiffies + HZ;
  29. while (time_before(jiffies, timeout))
  30. cpu_relax();
  31. }
  32. dev_emerg(dev, "Unable to %s (%d)\n", what, err);
  33. }
  34. static struct device *vexpress_power_off_device;
  35. static void vexpress_power_off(void)
  36. {
  37. vexpress_reset_do(vexpress_power_off_device, "power off");
  38. }
  39. static struct device *vexpress_restart_device;
  40. static void vexpress_restart(enum reboot_mode reboot_mode, const char *cmd)
  41. {
  42. vexpress_reset_do(vexpress_restart_device, "restart");
  43. }
  44. static ssize_t vexpress_reset_active_show(struct device *dev,
  45. struct device_attribute *attr, char *buf)
  46. {
  47. return sprintf(buf, "%d\n", vexpress_restart_device == dev);
  48. }
  49. static ssize_t vexpress_reset_active_store(struct device *dev,
  50. struct device_attribute *attr, const char *buf, size_t count)
  51. {
  52. long value;
  53. int err = kstrtol(buf, 0, &value);
  54. if (!err && value)
  55. vexpress_restart_device = dev;
  56. return err ? err : count;
  57. }
  58. DEVICE_ATTR(active, S_IRUGO | S_IWUSR, vexpress_reset_active_show,
  59. vexpress_reset_active_store);
  60. enum vexpress_reset_func { FUNC_RESET, FUNC_SHUTDOWN, FUNC_REBOOT };
  61. static struct of_device_id vexpress_reset_of_match[] = {
  62. {
  63. .compatible = "arm,vexpress-reset",
  64. .data = (void *)FUNC_RESET,
  65. }, {
  66. .compatible = "arm,vexpress-shutdown",
  67. .data = (void *)FUNC_SHUTDOWN
  68. }, {
  69. .compatible = "arm,vexpress-reboot",
  70. .data = (void *)FUNC_REBOOT
  71. },
  72. {}
  73. };
  74. static int vexpress_reset_probe(struct platform_device *pdev)
  75. {
  76. enum vexpress_reset_func func;
  77. const struct of_device_id *match =
  78. of_match_device(vexpress_reset_of_match, &pdev->dev);
  79. if (match)
  80. func = (enum vexpress_reset_func)match->data;
  81. else
  82. func = pdev->id_entry->driver_data;
  83. switch (func) {
  84. case FUNC_SHUTDOWN:
  85. vexpress_power_off_device = &pdev->dev;
  86. pm_power_off = vexpress_power_off;
  87. break;
  88. case FUNC_RESET:
  89. if (!vexpress_restart_device)
  90. vexpress_restart_device = &pdev->dev;
  91. arm_pm_restart = vexpress_restart;
  92. device_create_file(&pdev->dev, &dev_attr_active);
  93. break;
  94. case FUNC_REBOOT:
  95. vexpress_restart_device = &pdev->dev;
  96. arm_pm_restart = vexpress_restart;
  97. device_create_file(&pdev->dev, &dev_attr_active);
  98. break;
  99. };
  100. return 0;
  101. }
  102. static const struct platform_device_id vexpress_reset_id_table[] = {
  103. { .name = "vexpress-reset", .driver_data = FUNC_RESET, },
  104. { .name = "vexpress-shutdown", .driver_data = FUNC_SHUTDOWN, },
  105. { .name = "vexpress-reboot", .driver_data = FUNC_REBOOT, },
  106. {}
  107. };
  108. static struct platform_driver vexpress_reset_driver = {
  109. .probe = vexpress_reset_probe,
  110. .driver = {
  111. .name = "vexpress-reset",
  112. .of_match_table = vexpress_reset_of_match,
  113. },
  114. .id_table = vexpress_reset_id_table,
  115. };
  116. static int __init vexpress_reset_init(void)
  117. {
  118. return platform_driver_register(&vexpress_reset_driver);
  119. }
  120. device_initcall(vexpress_reset_init);