regulator-poweroff.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Force-disables a regulator to power down a device
  4. *
  5. * Michael Klein <michael@fossekall.de>
  6. *
  7. * Copyright (C) 2020 Michael Klein
  8. *
  9. * Based on the gpio-poweroff driver.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm.h>
  16. #include <linux/reboot.h>
  17. #include <linux/regulator/consumer.h>
  18. #define TIMEOUT_MS 3000
  19. static int regulator_poweroff_do_poweroff(struct sys_off_data *data)
  20. {
  21. struct regulator *cpu_regulator = data->cb_data;
  22. if (cpu_regulator && regulator_is_enabled(cpu_regulator))
  23. regulator_force_disable(cpu_regulator);
  24. /* give it some time */
  25. mdelay(TIMEOUT_MS);
  26. WARN_ON(1);
  27. return NOTIFY_DONE;
  28. }
  29. static int regulator_poweroff_probe(struct platform_device *pdev)
  30. {
  31. struct regulator *cpu_regulator;
  32. cpu_regulator = devm_regulator_get(&pdev->dev, "cpu");
  33. if (IS_ERR(cpu_regulator))
  34. return PTR_ERR(cpu_regulator);
  35. /* Set this handler to low priority to not override an existing handler */
  36. return devm_register_sys_off_handler(&pdev->dev,
  37. SYS_OFF_MODE_POWER_OFF,
  38. SYS_OFF_PRIO_LOW,
  39. regulator_poweroff_do_poweroff,
  40. cpu_regulator);
  41. }
  42. static const struct of_device_id of_regulator_poweroff_match[] = {
  43. { .compatible = "regulator-poweroff", },
  44. {},
  45. };
  46. MODULE_DEVICE_TABLE(of, of_regulator_poweroff_match);
  47. static struct platform_driver regulator_poweroff_driver = {
  48. .probe = regulator_poweroff_probe,
  49. .driver = {
  50. .name = "poweroff-regulator",
  51. .of_match_table = of_regulator_poweroff_match,
  52. },
  53. };
  54. module_platform_driver(regulator_poweroff_driver);
  55. MODULE_AUTHOR("Michael Klein <michael@fossekall.de>");
  56. MODULE_DESCRIPTION("Regulator poweroff driver");
  57. MODULE_ALIAS("platform:poweroff-regulator");