sc27xx-poweroff.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Spreadtrum Communications Inc.
  4. * Copyright (C) 2018 Linaro Ltd.
  5. */
  6. #include <linux/cpu.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm.h>
  11. #include <linux/regmap.h>
  12. #include <linux/syscore_ops.h>
  13. #define SC27XX_PWR_PD_HW 0xc2c
  14. #define SC27XX_PWR_OFF_EN BIT(0)
  15. #define SC27XX_SLP_CTRL 0xdf0
  16. #define SC27XX_LDO_XTL_EN BIT(3)
  17. static struct regmap *regmap;
  18. /*
  19. * On Spreadtrum platform, we need power off system through external SC27xx
  20. * series PMICs, and it is one similar SPI bus mapped by regmap to access PMIC,
  21. * which is not fast io access.
  22. *
  23. * So before stopping other cores, we need release other cores' resource by
  24. * taking cpus down to avoid racing regmap or spi mutex lock when poweroff
  25. * system through PMIC.
  26. */
  27. static void sc27xx_poweroff_shutdown(void)
  28. {
  29. #ifdef CONFIG_HOTPLUG_CPU
  30. int cpu;
  31. for_each_online_cpu(cpu) {
  32. if (cpu != smp_processor_id())
  33. remove_cpu(cpu);
  34. }
  35. #endif
  36. }
  37. static struct syscore_ops poweroff_syscore_ops = {
  38. .shutdown = sc27xx_poweroff_shutdown,
  39. };
  40. static void sc27xx_poweroff_do_poweroff(void)
  41. {
  42. /* Disable the external subsys connection's power firstly */
  43. regmap_write(regmap, SC27XX_SLP_CTRL, SC27XX_LDO_XTL_EN);
  44. regmap_write(regmap, SC27XX_PWR_PD_HW, SC27XX_PWR_OFF_EN);
  45. }
  46. static int sc27xx_poweroff_probe(struct platform_device *pdev)
  47. {
  48. if (regmap)
  49. return -EINVAL;
  50. regmap = dev_get_regmap(pdev->dev.parent, NULL);
  51. if (!regmap)
  52. return -ENODEV;
  53. pm_power_off = sc27xx_poweroff_do_poweroff;
  54. register_syscore_ops(&poweroff_syscore_ops);
  55. return 0;
  56. }
  57. static struct platform_driver sc27xx_poweroff_driver = {
  58. .probe = sc27xx_poweroff_probe,
  59. .driver = {
  60. .name = "sc27xx-poweroff",
  61. },
  62. };
  63. module_platform_driver(sc27xx_poweroff_driver);
  64. MODULE_DESCRIPTION("Power off driver for SC27XX PMIC Device");
  65. MODULE_AUTHOR("Baolin Wang <baolin.wang@unisoc.com>");
  66. MODULE_LICENSE("GPL v2");