syscon-poweroff.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic Syscon Poweroff Driver
  4. *
  5. * Copyright (c) 2015, National Instruments Corp.
  6. * Author: Moritz Fischer <moritz.fischer@ettus.com>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/io.h>
  10. #include <linux/notifier.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm.h>
  15. #include <linux/reboot.h>
  16. #include <linux/regmap.h>
  17. struct syscon_poweroff_data {
  18. struct regmap *map;
  19. u32 offset;
  20. u32 value;
  21. u32 mask;
  22. };
  23. static int syscon_poweroff(struct sys_off_data *off_data)
  24. {
  25. struct syscon_poweroff_data *data = off_data->cb_data;
  26. /* Issue the poweroff */
  27. regmap_update_bits(data->map, data->offset, data->mask, data->value);
  28. mdelay(1000);
  29. pr_emerg("Unable to poweroff system\n");
  30. return NOTIFY_DONE;
  31. }
  32. static int syscon_poweroff_probe(struct platform_device *pdev)
  33. {
  34. struct device *dev = &pdev->dev;
  35. struct syscon_poweroff_data *data;
  36. int mask_err, value_err;
  37. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  38. if (!data)
  39. return -ENOMEM;
  40. data->map = syscon_regmap_lookup_by_phandle(dev->of_node, "regmap");
  41. if (IS_ERR(data->map)) {
  42. data->map = syscon_node_to_regmap(dev->parent->of_node);
  43. if (IS_ERR(data->map)) {
  44. dev_err(dev, "unable to get syscon");
  45. return PTR_ERR(data->map);
  46. }
  47. }
  48. if (of_property_read_u32(dev->of_node, "offset", &data->offset)) {
  49. dev_err(dev, "unable to read 'offset'");
  50. return -EINVAL;
  51. }
  52. value_err = of_property_read_u32(dev->of_node, "value", &data->value);
  53. mask_err = of_property_read_u32(dev->of_node, "mask", &data->mask);
  54. if (value_err && mask_err) {
  55. dev_err(dev, "unable to read 'value' and 'mask'");
  56. return -EINVAL;
  57. }
  58. if (value_err) {
  59. /* support old binding */
  60. data->value = data->mask;
  61. data->mask = 0xFFFFFFFF;
  62. } else if (mask_err) {
  63. /* support value without mask*/
  64. data->mask = 0xFFFFFFFF;
  65. }
  66. return devm_register_sys_off_handler(&pdev->dev,
  67. SYS_OFF_MODE_POWER_OFF,
  68. SYS_OFF_PRIO_DEFAULT,
  69. syscon_poweroff, data);
  70. }
  71. static const struct of_device_id syscon_poweroff_of_match[] = {
  72. { .compatible = "syscon-poweroff" },
  73. {}
  74. };
  75. static struct platform_driver syscon_poweroff_driver = {
  76. .probe = syscon_poweroff_probe,
  77. .driver = {
  78. .name = "syscon-poweroff",
  79. .of_match_table = syscon_poweroff_of_match,
  80. },
  81. };
  82. builtin_platform_driver(syscon_poweroff_driver);