gemini-poweroff.c 4.3 KB

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