vexpress-poweroff.c 3.7 KB

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