mt6323-poweroff.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Power off through MediaTek PMIC
  4. *
  5. * Copyright (C) 2018 MediaTek Inc.
  6. *
  7. * Author: Sean Wang <[email protected]>
  8. *
  9. */
  10. #include <linux/err.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/mfd/mt6397/core.h>
  15. #include <linux/mfd/mt6397/rtc.h>
  16. struct mt6323_pwrc {
  17. struct device *dev;
  18. struct regmap *regmap;
  19. u32 base;
  20. };
  21. static struct mt6323_pwrc *mt_pwrc;
  22. static void mt6323_do_pwroff(void)
  23. {
  24. struct mt6323_pwrc *pwrc = mt_pwrc;
  25. unsigned int val;
  26. int ret;
  27. regmap_write(pwrc->regmap, pwrc->base + RTC_BBPU, RTC_BBPU_KEY);
  28. regmap_write(pwrc->regmap, pwrc->base + RTC_WRTGR_MT6323, 1);
  29. ret = regmap_read_poll_timeout(pwrc->regmap,
  30. pwrc->base + RTC_BBPU, val,
  31. !(val & RTC_BBPU_CBUSY),
  32. MTK_RTC_POLL_DELAY_US,
  33. MTK_RTC_POLL_TIMEOUT);
  34. if (ret)
  35. dev_err(pwrc->dev, "failed to write BBPU: %d\n", ret);
  36. /* Wait some time until system down, otherwise, notice with a warn */
  37. mdelay(1000);
  38. WARN_ONCE(1, "Unable to power off system\n");
  39. }
  40. static int mt6323_pwrc_probe(struct platform_device *pdev)
  41. {
  42. struct mt6397_chip *mt6397_chip = dev_get_drvdata(pdev->dev.parent);
  43. struct mt6323_pwrc *pwrc;
  44. struct resource *res;
  45. pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
  46. if (!pwrc)
  47. return -ENOMEM;
  48. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  49. if (!res)
  50. return -EINVAL;
  51. pwrc->base = res->start;
  52. pwrc->regmap = mt6397_chip->regmap;
  53. pwrc->dev = &pdev->dev;
  54. mt_pwrc = pwrc;
  55. pm_power_off = &mt6323_do_pwroff;
  56. return 0;
  57. }
  58. static int mt6323_pwrc_remove(struct platform_device *pdev)
  59. {
  60. if (pm_power_off == &mt6323_do_pwroff)
  61. pm_power_off = NULL;
  62. return 0;
  63. }
  64. static const struct of_device_id mt6323_pwrc_dt_match[] = {
  65. { .compatible = "mediatek,mt6323-pwrc" },
  66. {},
  67. };
  68. MODULE_DEVICE_TABLE(of, mt6323_pwrc_dt_match);
  69. static struct platform_driver mt6323_pwrc_driver = {
  70. .probe = mt6323_pwrc_probe,
  71. .remove = mt6323_pwrc_remove,
  72. .driver = {
  73. .name = "mt6323-pwrc",
  74. .of_match_table = mt6323_pwrc_dt_match,
  75. },
  76. };
  77. module_platform_driver(mt6323_pwrc_driver);
  78. MODULE_DESCRIPTION("Poweroff driver for MT6323 PMIC");
  79. MODULE_AUTHOR("Sean Wang <[email protected]>");
  80. MODULE_LICENSE("GPL v2");