2 * Atmel SAMA5D2-Compatible Shutdown Controller (SHDWC) driver.
3 * Found on some SoCs as the sama5d2 (obviously).
5 * Copyright (C) 2015 Atmel Corporation,
6 * Nicolas Ferre <nicolas.ferre@atmel.com>
8 * Evolved from driver at91-poweroff.c.
10 * This file is licensed under the terms of the GNU General Public
11 * License version 2. This program is licensed "as is" without any
12 * warranty of any kind, whether express or implied.
15 * - addition to status of other wake-up inputs [1 - 15]
16 * - Analog Comparator wake-up alarm
17 * - Serial RX wake-up alarm
18 * - low power debouncer
21 #include <linux/clk.h>
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/printk.h>
28 #define SLOW_CLOCK_FREQ 32768
30 #define AT91_SHDW_CR 0x00 /* Shut Down Control Register */
31 #define AT91_SHDW_SHDW BIT(0) /* Shut Down command */
32 #define AT91_SHDW_KEY (0xa5UL << 24) /* KEY Password */
34 #define AT91_SHDW_MR 0x04 /* Shut Down Mode Register */
35 #define AT91_SHDW_WKUPDBC_SHIFT 24
36 #define AT91_SHDW_WKUPDBC_MASK GENMASK(31, 16)
37 #define AT91_SHDW_WKUPDBC(x) (((x) << AT91_SHDW_WKUPDBC_SHIFT) \
38 & AT91_SHDW_WKUPDBC_MASK)
40 #define AT91_SHDW_SR 0x08 /* Shut Down Status Register */
41 #define AT91_SHDW_WKUPIS_SHIFT 16
42 #define AT91_SHDW_WKUPIS_MASK GENMASK(31, 16)
43 #define AT91_SHDW_WKUPIS(x) ((1 << (x)) << AT91_SHDW_WKUPIS_SHIFT \
44 & AT91_SHDW_WKUPIS_MASK)
46 #define AT91_SHDW_WUIR 0x0c /* Shutdown Wake-up Inputs Register */
47 #define AT91_SHDW_WKUPEN_MASK GENMASK(15, 0)
48 #define AT91_SHDW_WKUPEN(x) ((1 << (x)) & AT91_SHDW_WKUPEN_MASK)
49 #define AT91_SHDW_WKUPT_SHIFT 16
50 #define AT91_SHDW_WKUPT_MASK GENMASK(31, 16)
51 #define AT91_SHDW_WKUPT(x) ((1 << (x)) << AT91_SHDW_WKUPT_SHIFT \
52 & AT91_SHDW_WKUPT_MASK)
54 #define SHDW_WK_PIN(reg, cfg) ((reg) & AT91_SHDW_WKUPIS((cfg)->wkup_pin_input))
55 #define SHDW_RTCWK(reg, cfg) (((reg) >> ((cfg)->sr_rtcwk_shift)) & 0x1)
56 #define SHDW_RTCWKEN(cfg) (1 << ((cfg)->mr_rtcwk_shift))
58 #define DBC_PERIOD_US(x) DIV_ROUND_UP_ULL((1000000 * (x)), \
68 struct shdwc_config
*cfg
;
69 void __iomem
*at91_shdwc_base
;
73 * Hold configuration here, cannot be more than one instance of the driver
74 * since pm_power_off itself is global.
76 static struct shdwc
*at91_shdwc
;
77 static struct clk
*sclk
;
79 static const unsigned long long sdwc_dbc_period
[] = {
80 0, 3, 32, 512, 4096, 32768,
83 static void __init
at91_wakeup_status(struct platform_device
*pdev
)
85 struct shdwc
*shdw
= platform_get_drvdata(pdev
);
87 char *reason
= "unknown";
89 reg
= readl(shdw
->at91_shdwc_base
+ AT91_SHDW_SR
);
91 dev_dbg(&pdev
->dev
, "%s: status = %#x\n", __func__
, reg
);
93 /* Simple power-on, just bail out */
97 if (SHDW_WK_PIN(reg
, shdw
->cfg
))
99 else if (SHDW_RTCWK(reg
, shdw
->cfg
))
102 pr_info("AT91: Wake-Up source: %s\n", reason
);
105 static void at91_poweroff(void)
107 writel(AT91_SHDW_KEY
| AT91_SHDW_SHDW
,
108 at91_shdwc
->at91_shdwc_base
+ AT91_SHDW_CR
);
111 static u32
at91_shdwc_debouncer_value(struct platform_device
*pdev
,
115 int max_idx
= ARRAY_SIZE(sdwc_dbc_period
) - 1;
116 unsigned long long period_us
;
117 unsigned long long max_period_us
= DBC_PERIOD_US(sdwc_dbc_period
[max_idx
]);
119 if (in_period_us
> max_period_us
) {
121 "debouncer period %u too big, reduced to %llu us\n",
122 in_period_us
, max_period_us
);
126 for (i
= max_idx
- 1; i
> 0; i
--) {
127 period_us
= DBC_PERIOD_US(sdwc_dbc_period
[i
]);
128 dev_dbg(&pdev
->dev
, "%s: ref[%d] = %llu\n",
129 __func__
, i
, period_us
);
130 if (in_period_us
> period_us
)
137 static u32
at91_shdwc_get_wakeup_input(struct platform_device
*pdev
,
138 struct device_node
*np
)
140 struct device_node
*cnp
;
145 for_each_child_of_node(np
, cnp
) {
146 if (of_property_read_u32(cnp
, "reg", &wk_input
)) {
147 dev_warn(&pdev
->dev
, "reg property is missing for %s\n",
152 wk_input_mask
= 1 << wk_input
;
153 if (!(wk_input_mask
& AT91_SHDW_WKUPEN_MASK
)) {
155 "wake-up input %d out of bounds ignore\n",
159 wuir
|= wk_input_mask
;
161 if (of_property_read_bool(cnp
, "atmel,wakeup-active-high"))
162 wuir
|= AT91_SHDW_WKUPT(wk_input
);
164 dev_dbg(&pdev
->dev
, "%s: (child %d) wuir = %#x\n",
165 __func__
, wk_input
, wuir
);
171 static void at91_shdwc_dt_configure(struct platform_device
*pdev
)
173 struct shdwc
*shdw
= platform_get_drvdata(pdev
);
174 struct device_node
*np
= pdev
->dev
.of_node
;
175 u32 mode
= 0, tmp
, input
;
178 dev_err(&pdev
->dev
, "device node not found\n");
182 if (!of_property_read_u32(np
, "debounce-delay-us", &tmp
))
183 mode
|= AT91_SHDW_WKUPDBC(at91_shdwc_debouncer_value(pdev
, tmp
));
185 if (of_property_read_bool(np
, "atmel,wakeup-rtc-timer"))
186 mode
|= SHDW_RTCWKEN(shdw
->cfg
);
188 dev_dbg(&pdev
->dev
, "%s: mode = %#x\n", __func__
, mode
);
189 writel(mode
, shdw
->at91_shdwc_base
+ AT91_SHDW_MR
);
191 input
= at91_shdwc_get_wakeup_input(pdev
, np
);
192 writel(input
, shdw
->at91_shdwc_base
+ AT91_SHDW_WUIR
);
195 static const struct shdwc_config sama5d2_shdwc_config
= {
197 .mr_rtcwk_shift
= 17,
201 static const struct of_device_id at91_shdwc_of_match
[] = {
203 .compatible
= "atmel,sama5d2-shdwc",
204 .data
= &sama5d2_shdwc_config
,
209 MODULE_DEVICE_TABLE(of
, at91_shdwc_of_match
);
211 static int __init
at91_shdwc_probe(struct platform_device
*pdev
)
213 struct resource
*res
;
214 const struct of_device_id
*match
;
217 if (!pdev
->dev
.of_node
)
220 at91_shdwc
= devm_kzalloc(&pdev
->dev
, sizeof(*at91_shdwc
), GFP_KERNEL
);
224 platform_set_drvdata(pdev
, at91_shdwc
);
226 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
227 at91_shdwc
->at91_shdwc_base
= devm_ioremap_resource(&pdev
->dev
, res
);
228 if (IS_ERR(at91_shdwc
->at91_shdwc_base
)) {
229 dev_err(&pdev
->dev
, "Could not map reset controller address\n");
230 return PTR_ERR(at91_shdwc
->at91_shdwc_base
);
233 match
= of_match_node(at91_shdwc_of_match
, pdev
->dev
.of_node
);
234 at91_shdwc
->cfg
= (struct shdwc_config
*)(match
->data
);
236 sclk
= devm_clk_get(&pdev
->dev
, NULL
);
238 return PTR_ERR(sclk
);
240 ret
= clk_prepare_enable(sclk
);
242 dev_err(&pdev
->dev
, "Could not enable slow clock\n");
246 at91_wakeup_status(pdev
);
248 at91_shdwc_dt_configure(pdev
);
250 pm_power_off
= at91_poweroff
;
255 static int __exit
at91_shdwc_remove(struct platform_device
*pdev
)
257 struct shdwc
*shdw
= platform_get_drvdata(pdev
);
259 if (pm_power_off
== at91_poweroff
)
262 /* Reset values to disable wake-up features */
263 writel(0, shdw
->at91_shdwc_base
+ AT91_SHDW_MR
);
264 writel(0, shdw
->at91_shdwc_base
+ AT91_SHDW_WUIR
);
266 clk_disable_unprepare(sclk
);
271 static struct platform_driver at91_shdwc_driver
= {
272 .remove
= __exit_p(at91_shdwc_remove
),
274 .name
= "at91-shdwc",
275 .of_match_table
= at91_shdwc_of_match
,
278 module_platform_driver_probe(at91_shdwc_driver
, at91_shdwc_probe
);
280 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
281 MODULE_DESCRIPTION("Atmel shutdown controller driver");
282 MODULE_LICENSE("GPL v2");