1 // SPDX-License-Identifier: GPL-2.0
3 * Gemini power management controller
4 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
6 * Inspired by code from the SL3516 board support by Jason Lee
7 * Inspired by code from Janos Laube <janos.dev@gmail.com>
10 #include <linux/of_platform.h>
11 #include <linux/platform_device.h>
13 #include <linux/bitops.h>
14 #include <linux/interrupt.h>
16 #include <linux/reboot.h>
18 #define GEMINI_PWC_ID 0x00010500
19 #define GEMINI_PWC_IDREG 0x00
20 #define GEMINI_PWC_CTRLREG 0x04
21 #define GEMINI_PWC_STATREG 0x08
23 #define GEMINI_CTRL_SHUTDOWN BIT(0)
24 #define GEMINI_CTRL_ENABLE BIT(1)
25 #define GEMINI_CTRL_IRQ_CLR BIT(2)
27 #define GEMINI_STAT_CIR BIT(4)
28 #define GEMINI_STAT_RTC BIT(5)
29 #define GEMINI_STAT_POWERBUTTON BIT(6)
31 struct gemini_powercon
{
36 static irqreturn_t
gemini_powerbutton_interrupt(int irq
, void *data
)
38 struct gemini_powercon
*gpw
= data
;
42 val
= readl(gpw
->base
+ GEMINI_PWC_CTRLREG
);
43 val
|= GEMINI_CTRL_IRQ_CLR
;
44 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
46 val
= readl(gpw
->base
+ GEMINI_PWC_STATREG
);
51 * We do not yet have a driver for the infrared
52 * controller so it can cause spurious poweroff
53 * events. Ignore those for now.
55 dev_info(gpw
->dev
, "infrared poweroff - ignored\n");
58 dev_info(gpw
->dev
, "RTC poweroff\n");
59 orderly_poweroff(true);
61 case GEMINI_STAT_POWERBUTTON
:
62 dev_info(gpw
->dev
, "poweroff button pressed\n");
63 orderly_poweroff(true);
66 dev_info(gpw
->dev
, "other power management IRQ\n");
73 static int gemini_poweroff(struct sys_off_data
*data
)
75 struct gemini_powercon
*gpw
= data
->cb_data
;
78 dev_crit(gpw
->dev
, "Gemini power off\n");
79 val
= readl(gpw
->base
+ GEMINI_PWC_CTRLREG
);
80 val
|= GEMINI_CTRL_ENABLE
| GEMINI_CTRL_IRQ_CLR
;
81 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
83 val
&= ~GEMINI_CTRL_ENABLE
;
84 val
|= GEMINI_CTRL_SHUTDOWN
;
85 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
90 static int gemini_poweroff_probe(struct platform_device
*pdev
)
92 struct device
*dev
= &pdev
->dev
;
93 struct gemini_powercon
*gpw
;
98 gpw
= devm_kzalloc(dev
, sizeof(*gpw
), GFP_KERNEL
);
102 gpw
->base
= devm_platform_ioremap_resource(pdev
, 0);
103 if (IS_ERR(gpw
->base
))
104 return PTR_ERR(gpw
->base
);
106 irq
= platform_get_irq(pdev
, 0);
112 val
= readl(gpw
->base
+ GEMINI_PWC_IDREG
);
114 if (val
!= GEMINI_PWC_ID
) {
115 dev_err(dev
, "wrong power controller ID: %08x\n",
121 * Enable the power controller. This is crucial on Gemini
122 * systems: if this is not done, pressing the power button
123 * will result in unconditional poweroff without any warning.
124 * This makes the kernel handle the poweroff.
126 val
= readl(gpw
->base
+ GEMINI_PWC_CTRLREG
);
127 val
|= GEMINI_CTRL_ENABLE
;
128 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
131 val
= readl(gpw
->base
+ GEMINI_PWC_CTRLREG
);
132 val
|= GEMINI_CTRL_IRQ_CLR
;
133 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
135 /* Wait for this to clear */
136 val
= readl(gpw
->base
+ GEMINI_PWC_STATREG
);
138 val
= readl(gpw
->base
+ GEMINI_PWC_STATREG
);
140 /* Clear the IRQ again */
141 val
= readl(gpw
->base
+ GEMINI_PWC_CTRLREG
);
142 val
|= GEMINI_CTRL_IRQ_CLR
;
143 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
145 ret
= devm_request_irq(dev
, irq
, gemini_powerbutton_interrupt
, 0,
150 ret
= devm_register_sys_off_handler(dev
, SYS_OFF_MODE_POWER_OFF
,
151 SYS_OFF_PRIO_DEFAULT
,
152 gemini_poweroff
, gpw
);
156 dev_info(dev
, "Gemini poweroff driver registered\n");
161 static const struct of_device_id gemini_poweroff_of_match
[] = {
163 .compatible
= "cortina,gemini-power-controller",
168 static struct platform_driver gemini_poweroff_driver
= {
169 .probe
= gemini_poweroff_probe
,
171 .name
= "gemini-poweroff",
172 .of_match_table
= gemini_poweroff_of_match
,
175 builtin_platform_driver(gemini_poweroff_driver
);