Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Gemini power management controller
0004  * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
0005  *
0006  * Inspired by code from the SL3516 board support by Jason Lee
0007  * Inspired by code from Janos Laube <janos.dev@gmail.com>
0008  */
0009 #include <linux/of.h>
0010 #include <linux/of_platform.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/pm.h>
0013 #include <linux/bitops.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/io.h>
0016 #include <linux/reboot.h>
0017 
0018 #define GEMINI_PWC_ID       0x00010500
0019 #define GEMINI_PWC_IDREG    0x00
0020 #define GEMINI_PWC_CTRLREG  0x04
0021 #define GEMINI_PWC_STATREG  0x08
0022 
0023 #define GEMINI_CTRL_SHUTDOWN    BIT(0)
0024 #define GEMINI_CTRL_ENABLE  BIT(1)
0025 #define GEMINI_CTRL_IRQ_CLR BIT(2)
0026 
0027 #define GEMINI_STAT_CIR     BIT(4)
0028 #define GEMINI_STAT_RTC     BIT(5)
0029 #define GEMINI_STAT_POWERBUTTON BIT(6)
0030 
0031 struct gemini_powercon {
0032         struct device           *dev;
0033         void __iomem            *base;
0034 };
0035 
0036 static irqreturn_t gemini_powerbutton_interrupt(int irq, void *data)
0037 {
0038     struct gemini_powercon *gpw = data;
0039     u32 val;
0040 
0041     /* ACK the IRQ */
0042     val = readl(gpw->base + GEMINI_PWC_CTRLREG);
0043     val |= GEMINI_CTRL_IRQ_CLR;
0044     writel(val, gpw->base + GEMINI_PWC_CTRLREG);
0045 
0046     val = readl(gpw->base + GEMINI_PWC_STATREG);
0047     val &= 0x70U;
0048     switch (val) {
0049     case GEMINI_STAT_CIR:
0050         /*
0051          * We do not yet have a driver for the infrared
0052          * controller so it can cause spurious poweroff
0053          * events. Ignore those for now.
0054          */
0055         dev_info(gpw->dev, "infrared poweroff - ignored\n");
0056         break;
0057     case GEMINI_STAT_RTC:
0058         dev_info(gpw->dev, "RTC poweroff\n");
0059         orderly_poweroff(true);
0060         break;
0061     case GEMINI_STAT_POWERBUTTON:
0062         dev_info(gpw->dev, "poweroff button pressed\n");
0063         orderly_poweroff(true);
0064         break;
0065     default:
0066         dev_info(gpw->dev, "other power management IRQ\n");
0067         break;
0068     }
0069 
0070     return IRQ_HANDLED;
0071 }
0072 
0073 /* This callback needs this static local as it has void as argument */
0074 static struct gemini_powercon *gpw_poweroff;
0075 
0076 static void gemini_poweroff(void)
0077 {
0078     struct gemini_powercon *gpw = gpw_poweroff;
0079     u32 val;
0080 
0081     dev_crit(gpw->dev, "Gemini power off\n");
0082     val = readl(gpw->base + GEMINI_PWC_CTRLREG);
0083     val |= GEMINI_CTRL_ENABLE | GEMINI_CTRL_IRQ_CLR;
0084     writel(val, gpw->base + GEMINI_PWC_CTRLREG);
0085 
0086     val &= ~GEMINI_CTRL_ENABLE;
0087     val |= GEMINI_CTRL_SHUTDOWN;
0088     writel(val, gpw->base + GEMINI_PWC_CTRLREG);
0089 }
0090 
0091 static int gemini_poweroff_probe(struct platform_device *pdev)
0092 {
0093     struct device *dev = &pdev->dev;
0094     struct resource *res;
0095     struct gemini_powercon *gpw;
0096     u32 val;
0097     int irq;
0098     int ret;
0099 
0100     gpw = devm_kzalloc(dev, sizeof(*gpw), GFP_KERNEL);
0101     if (!gpw)
0102         return -ENOMEM;
0103 
0104     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0105     gpw->base = devm_ioremap_resource(dev, res);
0106     if (IS_ERR(gpw->base))
0107         return PTR_ERR(gpw->base);
0108 
0109     irq = platform_get_irq(pdev, 0);
0110     if (irq < 0)
0111         return irq;
0112 
0113     gpw->dev = dev;
0114 
0115     val = readl(gpw->base + GEMINI_PWC_IDREG);
0116     val &= 0xFFFFFF00U;
0117     if (val != GEMINI_PWC_ID) {
0118         dev_err(dev, "wrong power controller ID: %08x\n",
0119             val);
0120         return -ENODEV;
0121     }
0122 
0123     /*
0124      * Enable the power controller. This is crucial on Gemini
0125      * systems: if this is not done, pressing the power button
0126      * will result in unconditional poweroff without any warning.
0127      * This makes the kernel handle the poweroff.
0128      */
0129     val = readl(gpw->base + GEMINI_PWC_CTRLREG);
0130     val |= GEMINI_CTRL_ENABLE;
0131     writel(val, gpw->base + GEMINI_PWC_CTRLREG);
0132 
0133     /* Clear the IRQ */
0134     val = readl(gpw->base + GEMINI_PWC_CTRLREG);
0135     val |= GEMINI_CTRL_IRQ_CLR;
0136     writel(val, gpw->base + GEMINI_PWC_CTRLREG);
0137 
0138     /* Wait for this to clear */
0139     val = readl(gpw->base + GEMINI_PWC_STATREG);
0140     while (val & 0x70U)
0141         val = readl(gpw->base + GEMINI_PWC_STATREG);
0142 
0143     /* Clear the IRQ again */
0144     val = readl(gpw->base + GEMINI_PWC_CTRLREG);
0145     val |= GEMINI_CTRL_IRQ_CLR;
0146     writel(val, gpw->base + GEMINI_PWC_CTRLREG);
0147 
0148     ret = devm_request_irq(dev, irq, gemini_powerbutton_interrupt, 0,
0149                    "poweroff", gpw);
0150     if (ret)
0151         return ret;
0152 
0153     pm_power_off = gemini_poweroff;
0154     gpw_poweroff = gpw;
0155 
0156     dev_info(dev, "Gemini poweroff driver registered\n");
0157 
0158     return 0;
0159 }
0160 
0161 static const struct of_device_id gemini_poweroff_of_match[] = {
0162     {
0163         .compatible = "cortina,gemini-power-controller",
0164     },
0165     {}
0166 };
0167 
0168 static struct platform_driver gemini_poweroff_driver = {
0169     .probe = gemini_poweroff_probe,
0170     .driver = {
0171         .name = "gemini-poweroff",
0172         .of_match_table = gemini_poweroff_of_match,
0173     },
0174 };
0175 builtin_platform_driver(gemini_poweroff_driver);