gemini-poweroff.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Gemini power management controller
  3. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  4. *
  5. * Inspired by code from the SL3516 board support by Jason Lee
  6. * Inspired by code from Janos Laube <janos.dev@gmail.com>
  7. */
  8. #include <linux/of.h>
  9. #include <linux/of_platform.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pm.h>
  12. #include <linux/bitops.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/reboot.h>
  16. #define GEMINI_PWC_ID 0x00010500
  17. #define GEMINI_PWC_IDREG 0x00
  18. #define GEMINI_PWC_CTRLREG 0x04
  19. #define GEMINI_PWC_STATREG 0x08
  20. #define GEMINI_CTRL_SHUTDOWN BIT(0)
  21. #define GEMINI_CTRL_ENABLE BIT(1)
  22. #define GEMINI_CTRL_IRQ_CLR BIT(2)
  23. #define GEMINI_STAT_CIR BIT(4)
  24. #define GEMINI_STAT_RTC BIT(5)
  25. #define GEMINI_STAT_POWERBUTTON BIT(6)
  26. struct gemini_powercon {
  27. struct device *dev;
  28. void __iomem *base;
  29. };
  30. static irqreturn_t gemini_powerbutton_interrupt(int irq, void *data)
  31. {
  32. struct gemini_powercon *gpw = data;
  33. u32 val;
  34. /* ACK the IRQ */
  35. val = readl(gpw->base + GEMINI_PWC_CTRLREG);
  36. val |= GEMINI_CTRL_IRQ_CLR;
  37. writel(val, gpw->base + GEMINI_PWC_CTRLREG);
  38. val = readl(gpw->base + GEMINI_PWC_STATREG);
  39. val &= 0x70U;
  40. switch (val) {
  41. case GEMINI_STAT_CIR:
  42. dev_info(gpw->dev, "infrared poweroff\n");
  43. orderly_poweroff(true);
  44. break;
  45. case GEMINI_STAT_RTC:
  46. dev_info(gpw->dev, "RTC poweroff\n");
  47. orderly_poweroff(true);
  48. break;
  49. case GEMINI_STAT_POWERBUTTON:
  50. dev_info(gpw->dev, "poweroff button pressed\n");
  51. orderly_poweroff(true);
  52. break;
  53. default:
  54. dev_info(gpw->dev, "other power management IRQ\n");
  55. break;
  56. }
  57. return IRQ_HANDLED;
  58. }
  59. /* This callback needs this static local as it has void as argument */
  60. static struct gemini_powercon *gpw_poweroff;
  61. static void gemini_poweroff(void)
  62. {
  63. struct gemini_powercon *gpw = gpw_poweroff;
  64. u32 val;
  65. dev_crit(gpw->dev, "Gemini power off\n");
  66. val = readl(gpw->base + GEMINI_PWC_CTRLREG);
  67. val |= GEMINI_CTRL_ENABLE | GEMINI_CTRL_IRQ_CLR;
  68. writel(val, gpw->base + GEMINI_PWC_CTRLREG);
  69. val &= ~GEMINI_CTRL_ENABLE;
  70. val |= GEMINI_CTRL_SHUTDOWN;
  71. writel(val, gpw->base + GEMINI_PWC_CTRLREG);
  72. }
  73. static int gemini_poweroff_probe(struct platform_device *pdev)
  74. {
  75. struct device *dev = &pdev->dev;
  76. struct resource *res;
  77. struct gemini_powercon *gpw;
  78. u32 val;
  79. int irq;
  80. int ret;
  81. gpw = devm_kzalloc(dev, sizeof(*gpw), GFP_KERNEL);
  82. if (!gpw)
  83. return -ENOMEM;
  84. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  85. gpw->base = devm_ioremap_resource(dev, res);
  86. if (IS_ERR(gpw->base))
  87. return PTR_ERR(gpw->base);
  88. irq = platform_get_irq(pdev, 0);
  89. if (!irq)
  90. return -EINVAL;
  91. gpw->dev = dev;
  92. val = readl(gpw->base + GEMINI_PWC_IDREG);
  93. val &= 0xFFFFFF00U;
  94. if (val != GEMINI_PWC_ID) {
  95. dev_err(dev, "wrong power controller ID: %08x\n",
  96. val);
  97. return -ENODEV;
  98. }
  99. /* Clear the power management IRQ */
  100. val = readl(gpw->base + GEMINI_PWC_CTRLREG);
  101. val |= GEMINI_CTRL_IRQ_CLR;
  102. writel(val, gpw->base + GEMINI_PWC_CTRLREG);
  103. ret = devm_request_irq(dev, irq, gemini_powerbutton_interrupt, 0,
  104. "poweroff", gpw);
  105. if (ret)
  106. return ret;
  107. pm_power_off = gemini_poweroff;
  108. gpw_poweroff = gpw;
  109. /*
  110. * Enable the power controller. This is crucial on Gemini
  111. * systems: if this is not done, pressing the power button
  112. * will result in unconditional poweroff without any warning.
  113. * This makes the kernel handle the poweroff.
  114. */
  115. val = readl(gpw->base + GEMINI_PWC_CTRLREG);
  116. val |= GEMINI_CTRL_ENABLE;
  117. writel(val, gpw->base + GEMINI_PWC_CTRLREG);
  118. dev_info(dev, "Gemini poweroff driver registered\n");
  119. return 0;
  120. }
  121. static const struct of_device_id gemini_poweroff_of_match[] = {
  122. {
  123. .compatible = "cortina,gemini-power-controller",
  124. },
  125. {}
  126. };
  127. static struct platform_driver gemini_poweroff_driver = {
  128. .probe = gemini_poweroff_probe,
  129. .driver = {
  130. .name = "gemini-poweroff",
  131. .of_match_table = gemini_poweroff_of_match,
  132. },
  133. };
  134. builtin_platform_driver(gemini_poweroff_driver);