reset.c 3.5 KB

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