]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/power/reset/at91-sama5d2_shdwc.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[mirror_ubuntu-zesty-kernel.git] / drivers / power / reset / at91-sama5d2_shdwc.c
1 /*
2 * Atmel SAMA5D2-Compatible Shutdown Controller (SHDWC) driver.
3 * Found on some SoCs as the sama5d2 (obviously).
4 *
5 * Copyright (C) 2015 Atmel Corporation,
6 * Nicolas Ferre <nicolas.ferre@atmel.com>
7 *
8 * Evolved from driver at91-poweroff.c.
9 *
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.
13 *
14 * TODO:
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
19 */
20
21 #include <linux/clk.h>
22 #include <linux/io.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/platform_device.h>
26 #include <linux/printk.h>
27
28 #define SLOW_CLOCK_FREQ 32768
29
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 */
33
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)
39
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)
45
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)
53
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))
57
58 #define DBC_PERIOD_US(x) DIV_ROUND_UP_ULL((1000000 * (x)), \
59 SLOW_CLOCK_FREQ)
60
61 struct shdwc_config {
62 u8 wkup_pin_input;
63 u8 mr_rtcwk_shift;
64 u8 sr_rtcwk_shift;
65 };
66
67 struct shdwc {
68 struct shdwc_config *cfg;
69 void __iomem *at91_shdwc_base;
70 };
71
72 /*
73 * Hold configuration here, cannot be more than one instance of the driver
74 * since pm_power_off itself is global.
75 */
76 static struct shdwc *at91_shdwc;
77 static struct clk *sclk;
78
79 static const unsigned long long sdwc_dbc_period[] = {
80 0, 3, 32, 512, 4096, 32768,
81 };
82
83 static void __init at91_wakeup_status(struct platform_device *pdev)
84 {
85 struct shdwc *shdw = platform_get_drvdata(pdev);
86 u32 reg;
87 char *reason = "unknown";
88
89 reg = readl(shdw->at91_shdwc_base + AT91_SHDW_SR);
90
91 dev_dbg(&pdev->dev, "%s: status = %#x\n", __func__, reg);
92
93 /* Simple power-on, just bail out */
94 if (!reg)
95 return;
96
97 if (SHDW_WK_PIN(reg, shdw->cfg))
98 reason = "WKUP pin";
99 else if (SHDW_RTCWK(reg, shdw->cfg))
100 reason = "RTC";
101
102 pr_info("AT91: Wake-Up source: %s\n", reason);
103 }
104
105 static void at91_poweroff(void)
106 {
107 writel(AT91_SHDW_KEY | AT91_SHDW_SHDW,
108 at91_shdwc->at91_shdwc_base + AT91_SHDW_CR);
109 }
110
111 static u32 at91_shdwc_debouncer_value(struct platform_device *pdev,
112 u32 in_period_us)
113 {
114 int i;
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]);
118
119 if (in_period_us > max_period_us) {
120 dev_warn(&pdev->dev,
121 "debouncer period %u too big, reduced to %llu us\n",
122 in_period_us, max_period_us);
123 return max_idx;
124 }
125
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)
131 break;
132 }
133
134 return i + 1;
135 }
136
137 static u32 at91_shdwc_get_wakeup_input(struct platform_device *pdev,
138 struct device_node *np)
139 {
140 struct device_node *cnp;
141 u32 wk_input_mask;
142 u32 wuir = 0;
143 u32 wk_input;
144
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",
148 cnp->full_name);
149 continue;
150 }
151
152 wk_input_mask = 1 << wk_input;
153 if (!(wk_input_mask & AT91_SHDW_WKUPEN_MASK)) {
154 dev_warn(&pdev->dev,
155 "wake-up input %d out of bounds ignore\n",
156 wk_input);
157 continue;
158 }
159 wuir |= wk_input_mask;
160
161 if (of_property_read_bool(cnp, "atmel,wakeup-active-high"))
162 wuir |= AT91_SHDW_WKUPT(wk_input);
163
164 dev_dbg(&pdev->dev, "%s: (child %d) wuir = %#x\n",
165 __func__, wk_input, wuir);
166 }
167
168 return wuir;
169 }
170
171 static void at91_shdwc_dt_configure(struct platform_device *pdev)
172 {
173 struct shdwc *shdw = platform_get_drvdata(pdev);
174 struct device_node *np = pdev->dev.of_node;
175 u32 mode = 0, tmp, input;
176
177 if (!np) {
178 dev_err(&pdev->dev, "device node not found\n");
179 return;
180 }
181
182 if (!of_property_read_u32(np, "debounce-delay-us", &tmp))
183 mode |= AT91_SHDW_WKUPDBC(at91_shdwc_debouncer_value(pdev, tmp));
184
185 if (of_property_read_bool(np, "atmel,wakeup-rtc-timer"))
186 mode |= SHDW_RTCWKEN(shdw->cfg);
187
188 dev_dbg(&pdev->dev, "%s: mode = %#x\n", __func__, mode);
189 writel(mode, shdw->at91_shdwc_base + AT91_SHDW_MR);
190
191 input = at91_shdwc_get_wakeup_input(pdev, np);
192 writel(input, shdw->at91_shdwc_base + AT91_SHDW_WUIR);
193 }
194
195 static const struct shdwc_config sama5d2_shdwc_config = {
196 .wkup_pin_input = 0,
197 .mr_rtcwk_shift = 17,
198 .sr_rtcwk_shift = 5,
199 };
200
201 static const struct of_device_id at91_shdwc_of_match[] = {
202 {
203 .compatible = "atmel,sama5d2-shdwc",
204 .data = &sama5d2_shdwc_config,
205 }, {
206 /*sentinel*/
207 }
208 };
209 MODULE_DEVICE_TABLE(of, at91_shdwc_of_match);
210
211 static int __init at91_shdwc_probe(struct platform_device *pdev)
212 {
213 struct resource *res;
214 const struct of_device_id *match;
215 int ret;
216
217 if (!pdev->dev.of_node)
218 return -ENODEV;
219
220 at91_shdwc = devm_kzalloc(&pdev->dev, sizeof(*at91_shdwc), GFP_KERNEL);
221 if (!at91_shdwc)
222 return -ENOMEM;
223
224 platform_set_drvdata(pdev, at91_shdwc);
225
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);
231 }
232
233 match = of_match_node(at91_shdwc_of_match, pdev->dev.of_node);
234 at91_shdwc->cfg = (struct shdwc_config *)(match->data);
235
236 sclk = devm_clk_get(&pdev->dev, NULL);
237 if (IS_ERR(sclk))
238 return PTR_ERR(sclk);
239
240 ret = clk_prepare_enable(sclk);
241 if (ret) {
242 dev_err(&pdev->dev, "Could not enable slow clock\n");
243 return ret;
244 }
245
246 at91_wakeup_status(pdev);
247
248 at91_shdwc_dt_configure(pdev);
249
250 pm_power_off = at91_poweroff;
251
252 return 0;
253 }
254
255 static int __exit at91_shdwc_remove(struct platform_device *pdev)
256 {
257 struct shdwc *shdw = platform_get_drvdata(pdev);
258
259 if (pm_power_off == at91_poweroff)
260 pm_power_off = NULL;
261
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);
265
266 clk_disable_unprepare(sclk);
267
268 return 0;
269 }
270
271 static struct platform_driver at91_shdwc_driver = {
272 .remove = __exit_p(at91_shdwc_remove),
273 .driver = {
274 .name = "at91-shdwc",
275 .of_match_table = at91_shdwc_of_match,
276 },
277 };
278 module_platform_driver_probe(at91_shdwc_driver, at91_shdwc_probe);
279
280 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
281 MODULE_DESCRIPTION("Atmel shutdown controller driver");
282 MODULE_LICENSE("GPL v2");