syscon-poweroff.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Generic Syscon Poweroff Driver
  3. *
  4. * Copyright (c) 2015, National Instruments Corp.
  5. * Author: Moritz Fischer <moritz.fischer@ettus.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/kallsyms.h>
  18. #include <linux/delay.h>
  19. #include <linux/io.h>
  20. #include <linux/notifier.h>
  21. #include <linux/mfd/syscon.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_device.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm.h>
  26. #include <linux/regmap.h>
  27. static struct regmap *map;
  28. static u32 offset;
  29. static u32 value;
  30. static u32 mask;
  31. static void syscon_poweroff(void)
  32. {
  33. /* Issue the poweroff */
  34. regmap_update_bits(map, offset, mask, value);
  35. mdelay(1000);
  36. pr_emerg("Unable to poweroff system\n");
  37. }
  38. static int syscon_poweroff_probe(struct platform_device *pdev)
  39. {
  40. char symname[KSYM_NAME_LEN];
  41. int mask_err, value_err;
  42. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap");
  43. if (IS_ERR(map)) {
  44. dev_err(&pdev->dev, "unable to get syscon");
  45. return PTR_ERR(map);
  46. }
  47. if (of_property_read_u32(pdev->dev.of_node, "offset", &offset)) {
  48. dev_err(&pdev->dev, "unable to read 'offset'");
  49. return -EINVAL;
  50. }
  51. value_err = of_property_read_u32(pdev->dev.of_node, "value", &value);
  52. mask_err = of_property_read_u32(pdev->dev.of_node, "mask", &mask);
  53. if (value_err && mask_err) {
  54. dev_err(&pdev->dev, "unable to read 'value' and 'mask'");
  55. return -EINVAL;
  56. }
  57. if (value_err) {
  58. /* support old binding */
  59. value = mask;
  60. mask = 0xFFFFFFFF;
  61. } else if (mask_err) {
  62. /* support value without mask*/
  63. mask = 0xFFFFFFFF;
  64. }
  65. if (pm_power_off) {
  66. lookup_symbol_name((ulong)pm_power_off, symname);
  67. dev_err(&pdev->dev,
  68. "pm_power_off already claimed %p %s",
  69. pm_power_off, symname);
  70. return -EBUSY;
  71. }
  72. pm_power_off = syscon_poweroff;
  73. return 0;
  74. }
  75. static int syscon_poweroff_remove(struct platform_device *pdev)
  76. {
  77. if (pm_power_off == syscon_poweroff)
  78. pm_power_off = NULL;
  79. return 0;
  80. }
  81. static const struct of_device_id syscon_poweroff_of_match[] = {
  82. { .compatible = "syscon-poweroff" },
  83. {}
  84. };
  85. static struct platform_driver syscon_poweroff_driver = {
  86. .probe = syscon_poweroff_probe,
  87. .remove = syscon_poweroff_remove,
  88. .driver = {
  89. .name = "syscon-poweroff",
  90. .of_match_table = syscon_poweroff_of_match,
  91. },
  92. };
  93. static int __init syscon_poweroff_register(void)
  94. {
  95. return platform_driver_register(&syscon_poweroff_driver);
  96. }
  97. device_initcall(syscon_poweroff_register);