Skip to content

Commit f7a388d

Browse files
Linus Walleijsre
authored andcommitted
power: reset: Add a driver for the Gemini poweroff
The Gemini (SL3516) SoC has a special power controller block that only deal with shutting down the system. If you do not register a driver and activate the block, the power button on the systems utilizing this SoC will do an uncontrolled power cut, which is why it is important to have a special poweroff driver. The most basic functionality is to just shut down the system by writing a special bit in the control register after the system has reached pm_poweroff. It also handles the poweroff from a button or other sources: When the poweroff button is pressed, or a signal is sent to poweroff from an infrared remote control, or when the RTC fires a special alarm (!) the system emits an interrupt. At this point, Linux must acknowledge the interrupt and proceed to do an orderly shutdown of the system. After adding this driver, pressing the poweroff button gives this dmesg: root@gemini:/ root@gemini:/ gemini-poweroff 4b000000.power-controller: poweroff button pressed calling shutdown scripts.. setting /dev/rtc0 from system time unmounting file systems... umount: tmpfs busy - remounted read-only umount: can't unmount /: Invalid argument The system is going down NOW! Sent SIGTERM to all processes Sent SIGKILL to all processes Requesting system poweroff uhci_hcd 0000:00:09.1: HCRESET not completed yet! uhci_hcd 0000:00:09.0: HCRESET not completed yet! reboot: Power down gemini-poweroff 4b000000.power-controller: Gemini power off Cc: Janos Laube <janos.dev@gmail.com> Cc: Paulius Zaleckas <paulius.zaleckas@gmail.com> Cc: Hans Ulli Kroll <ulli.kroll@googlemail.com> Cc: Florian Fainelli <f.fainelli@gmail.com> Cc: linux-pm@vger.kernel.org Cc: Sebastian Reichel <sre@kernel.org> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Sebastian Reichel <sre@kernel.org>
1 parent ba443b5 commit f7a388d

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

drivers/power/reset/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ config POWER_RESET_BRCMSTB
6767
Say Y here if you have a Broadcom STB board and you wish
6868
to have restart support.
6969

70+
config POWER_RESET_GEMINI_POWEROFF
71+
bool "Cortina Gemini power-off driver"
72+
depends on ARCH_GEMINI || COMPILE_TEST
73+
depends on OF && HAS_IOMEM
74+
default ARCH_GEMINI
75+
help
76+
This driver supports turning off the Cortina Gemini SoC.
77+
Select this if you're building a kernel with Gemini SoC support.
78+
7079
config POWER_RESET_GPIO
7180
bool "GPIO power-off driver"
7281
depends on OF_GPIO

drivers/power/reset/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ obj-$(CONFIG_POWER_RESET_AT91_SAMA5D2_SHDWC) += at91-sama5d2_shdwc.o
55
obj-$(CONFIG_POWER_RESET_AXXIA) += axxia-reset.o
66
obj-$(CONFIG_POWER_RESET_BRCMKONA) += brcm-kona-reset.o
77
obj-$(CONFIG_POWER_RESET_BRCMSTB) += brcmstb-reboot.o
8+
obj-$(CONFIG_POWER_RESET_GEMINI_POWEROFF) += gemini-poweroff.o
89
obj-$(CONFIG_POWER_RESET_GPIO) += gpio-poweroff.o
910
obj-$(CONFIG_POWER_RESET_GPIO_RESTART) += gpio-restart.o
1011
obj-$(CONFIG_POWER_RESET_HISI) += hisi-reboot.o
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
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+
22+
#define GEMINI_CTRL_SHUTDOWN BIT(0)
23+
#define GEMINI_CTRL_ENABLE BIT(1)
24+
#define GEMINI_CTRL_IRQ_CLR BIT(2)
25+
26+
#define GEMINI_STAT_CIR BIT(4)
27+
#define GEMINI_STAT_RTC BIT(5)
28+
#define GEMINI_STAT_POWERBUTTON BIT(6)
29+
30+
struct gemini_powercon {
31+
struct device *dev;
32+
void __iomem *base;
33+
};
34+
35+
static irqreturn_t gemini_powerbutton_interrupt(int irq, void *data)
36+
{
37+
struct gemini_powercon *gpw = data;
38+
u32 val;
39+
40+
/* ACK the IRQ */
41+
val = readl(gpw->base + GEMINI_PWC_CTRLREG);
42+
val |= GEMINI_CTRL_IRQ_CLR;
43+
writel(val, gpw->base + GEMINI_PWC_CTRLREG);
44+
45+
val = readl(gpw->base + GEMINI_PWC_STATREG);
46+
val &= 0x70U;
47+
switch (val) {
48+
case GEMINI_STAT_CIR:
49+
dev_info(gpw->dev, "infrared poweroff\n");
50+
orderly_poweroff(true);
51+
break;
52+
case GEMINI_STAT_RTC:
53+
dev_info(gpw->dev, "RTC poweroff\n");
54+
orderly_poweroff(true);
55+
break;
56+
case GEMINI_STAT_POWERBUTTON:
57+
dev_info(gpw->dev, "poweroff button pressed\n");
58+
orderly_poweroff(true);
59+
break;
60+
default:
61+
dev_info(gpw->dev, "other power management IRQ\n");
62+
break;
63+
}
64+
65+
return IRQ_HANDLED;
66+
}
67+
68+
/* This callback needs this static local as it has void as argument */
69+
static struct gemini_powercon *gpw_poweroff;
70+
71+
static void gemini_poweroff(void)
72+
{
73+
struct gemini_powercon *gpw = gpw_poweroff;
74+
u32 val;
75+
76+
dev_crit(gpw->dev, "Gemini power off\n");
77+
val = readl(gpw->base + GEMINI_PWC_CTRLREG);
78+
val |= GEMINI_CTRL_ENABLE | GEMINI_CTRL_IRQ_CLR;
79+
writel(val, gpw->base + GEMINI_PWC_CTRLREG);
80+
81+
val &= ~GEMINI_CTRL_ENABLE;
82+
val |= GEMINI_CTRL_SHUTDOWN;
83+
writel(val, gpw->base + GEMINI_PWC_CTRLREG);
84+
}
85+
86+
static int gemini_poweroff_probe(struct platform_device *pdev)
87+
{
88+
struct device *dev = &pdev->dev;
89+
struct resource *res;
90+
struct gemini_powercon *gpw;
91+
u32 val;
92+
int irq;
93+
int ret;
94+
95+
gpw = devm_kzalloc(dev, sizeof(*gpw), GFP_KERNEL);
96+
if (!gpw)
97+
return -ENOMEM;
98+
99+
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
100+
gpw->base = devm_ioremap_resource(dev, res);
101+
if (IS_ERR(gpw->base))
102+
return PTR_ERR(gpw->base);
103+
104+
irq = platform_get_irq(pdev, 0);
105+
if (!irq)
106+
return -EINVAL;
107+
108+
gpw->dev = dev;
109+
110+
val = readl(gpw->base + GEMINI_PWC_IDREG);
111+
val &= 0xFFFFFF00U;
112+
if (val != GEMINI_PWC_ID) {
113+
dev_err(dev, "wrong power controller ID: %08x\n",
114+
val);
115+
return -ENODEV;
116+
}
117+
118+
/* Clear the power management IRQ */
119+
val = readl(gpw->base + GEMINI_PWC_CTRLREG);
120+
val |= GEMINI_CTRL_IRQ_CLR;
121+
writel(val, gpw->base + GEMINI_PWC_CTRLREG);
122+
123+
ret = devm_request_irq(dev, irq, gemini_powerbutton_interrupt, 0,
124+
"poweroff", gpw);
125+
if (ret)
126+
return ret;
127+
128+
pm_power_off = gemini_poweroff;
129+
gpw_poweroff = gpw;
130+
131+
/*
132+
* Enable the power controller. This is crucial on Gemini
133+
* systems: if this is not done, pressing the power button
134+
* will result in unconditional poweroff without any warning.
135+
* This makes the kernel handle the poweroff.
136+
*/
137+
val = readl(gpw->base + GEMINI_PWC_CTRLREG);
138+
val |= GEMINI_CTRL_ENABLE;
139+
writel(val, gpw->base + GEMINI_PWC_CTRLREG);
140+
141+
dev_info(dev, "Gemini poweroff driver registered\n");
142+
143+
return 0;
144+
}
145+
146+
static const struct of_device_id gemini_poweroff_of_match[] = {
147+
{
148+
.compatible = "cortina,gemini-power-controller",
149+
},
150+
{}
151+
};
152+
153+
static struct platform_driver gemini_poweroff_driver = {
154+
.probe = gemini_poweroff_probe,
155+
.driver = {
156+
.name = "gemini-poweroff",
157+
.of_match_table = gemini_poweroff_of_match,
158+
},
159+
};
160+
builtin_platform_driver(gemini_poweroff_driver);

0 commit comments

Comments
 (0)