syscon-poweroff.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 mask;
  30. static void syscon_poweroff(void)
  31. {
  32. /* Issue the poweroff */
  33. regmap_write(map, offset, mask);
  34. mdelay(1000);
  35. pr_emerg("Unable to poweroff system\n");
  36. }
  37. static int syscon_poweroff_probe(struct platform_device *pdev)
  38. {
  39. char symname[KSYM_NAME_LEN];
  40. map = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, "regmap");
  41. if (IS_ERR(map)) {
  42. dev_err(&pdev->dev, "unable to get syscon");
  43. return PTR_ERR(map);
  44. }
  45. if (of_property_read_u32(pdev->dev.of_node, "offset", &offset)) {
  46. dev_err(&pdev->dev, "unable to read 'offset'");
  47. return -EINVAL;
  48. }
  49. if (of_property_read_u32(pdev->dev.of_node, "mask", &mask)) {
  50. dev_err(&pdev->dev, "unable to read 'mask'");
  51. return -EINVAL;
  52. }
  53. if (pm_power_off) {
  54. lookup_symbol_name((ulong)pm_power_off, symname);
  55. dev_err(&pdev->dev,
  56. "pm_power_off already claimed %p %s",
  57. pm_power_off, symname);
  58. return -EBUSY;
  59. }
  60. pm_power_off = syscon_poweroff;
  61. return 0;
  62. }
  63. static int syscon_poweroff_remove(struct platform_device *pdev)
  64. {
  65. if (pm_power_off == syscon_poweroff)
  66. pm_power_off = NULL;
  67. return 0;
  68. }
  69. static const struct of_device_id syscon_poweroff_of_match[] = {
  70. { .compatible = "syscon-poweroff" },
  71. {}
  72. };
  73. static struct platform_driver syscon_poweroff_driver = {
  74. .probe = syscon_poweroff_probe,
  75. .remove = syscon_poweroff_remove,
  76. .driver = {
  77. .name = "syscon-poweroff",
  78. .of_match_table = syscon_poweroff_of_match,
  79. },
  80. };
  81. static int __init syscon_poweroff_register(void)
  82. {
  83. return platform_driver_register(&syscon_poweroff_driver);
  84. }
  85. device_initcall(syscon_poweroff_register);