2 * Gemini power management controller
3 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
5 * Inspired by code from the SL3516 board support by Jason Lee
6 * Inspired by code from Janos Laube <janos.dev@gmail.com>
9 #include <linux/of_platform.h>
10 #include <linux/platform_device.h>
12 #include <linux/bitops.h>
13 #include <linux/interrupt.h>
15 #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
22 #define GEMINI_CTRL_SHUTDOWN BIT(0)
23 #define GEMINI_CTRL_ENABLE BIT(1)
24 #define GEMINI_CTRL_IRQ_CLR BIT(2)
26 #define GEMINI_STAT_CIR BIT(4)
27 #define GEMINI_STAT_RTC BIT(5)
28 #define GEMINI_STAT_POWERBUTTON BIT(6)
30 struct gemini_powercon
{
35 static irqreturn_t
gemini_powerbutton_interrupt(int irq
, void *data
)
37 struct gemini_powercon
*gpw
= data
;
41 val
= readl(gpw
->base
+ GEMINI_PWC_CTRLREG
);
42 val
|= GEMINI_CTRL_IRQ_CLR
;
43 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
45 val
= readl(gpw
->base
+ GEMINI_PWC_STATREG
);
49 dev_info(gpw
->dev
, "infrared poweroff\n");
50 orderly_poweroff(true);
53 dev_info(gpw
->dev
, "RTC poweroff\n");
54 orderly_poweroff(true);
56 case GEMINI_STAT_POWERBUTTON
:
57 dev_info(gpw
->dev
, "poweroff button pressed\n");
58 orderly_poweroff(true);
61 dev_info(gpw
->dev
, "other power management IRQ\n");
68 /* This callback needs this static local as it has void as argument */
69 static struct gemini_powercon
*gpw_poweroff
;
71 static void gemini_poweroff(void)
73 struct gemini_powercon
*gpw
= gpw_poweroff
;
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
);
81 val
&= ~GEMINI_CTRL_ENABLE
;
82 val
|= GEMINI_CTRL_SHUTDOWN
;
83 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
86 static int gemini_poweroff_probe(struct platform_device
*pdev
)
88 struct device
*dev
= &pdev
->dev
;
90 struct gemini_powercon
*gpw
;
95 gpw
= devm_kzalloc(dev
, sizeof(*gpw
), GFP_KERNEL
);
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
);
104 irq
= platform_get_irq(pdev
, 0);
110 val
= readl(gpw
->base
+ GEMINI_PWC_IDREG
);
112 if (val
!= GEMINI_PWC_ID
) {
113 dev_err(dev
, "wrong power controller ID: %08x\n",
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
);
123 ret
= devm_request_irq(dev
, irq
, gemini_powerbutton_interrupt
, 0,
128 pm_power_off
= gemini_poweroff
;
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.
137 val
= readl(gpw
->base
+ GEMINI_PWC_CTRLREG
);
138 val
|= GEMINI_CTRL_ENABLE
;
139 writel(val
, gpw
->base
+ GEMINI_PWC_CTRLREG
);
141 dev_info(dev
, "Gemini poweroff driver registered\n");
146 static const struct of_device_id gemini_poweroff_of_match
[] = {
148 .compatible
= "cortina,gemini-power-controller",
153 static struct platform_driver gemini_poweroff_driver
= {
154 .probe
= gemini_poweroff_probe
,
156 .name
= "gemini-poweroff",
157 .of_match_table
= gemini_poweroff_of_match
,
160 builtin_platform_driver(gemini_poweroff_driver
);