rtc: omap: enable wake-up from power off
[deliverable/linux.git] / drivers / rtc / rtc-omap.c
CommitLineData
db68b189
DB
1/*
2 * TI OMAP1 Real Time Clock interface for Linux
3 *
4 * Copyright (C) 2003 MontaVista Software, Inc.
5 * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com>
6 *
7 * Copyright (C) 2006 David Brownell (new RTC framework)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/ioport.h>
19#include <linux/delay.h>
20#include <linux/rtc.h>
21#include <linux/bcd.h>
22#include <linux/platform_device.h>
9e0344dc
AM
23#include <linux/of.h>
24#include <linux/of_device.h>
fc9bd902 25#include <linux/pm_runtime.h>
4b30c9fc 26#include <linux/io.h>
db68b189
DB
27
28/* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock
29 * with century-range alarm matching, driven by the 32kHz clock.
30 *
31 * The main user-visible ways it differs from PC RTCs are by omitting
32 * "don't care" alarm fields and sub-second periodic IRQs, and having
33 * an autoadjust mechanism to calibrate to the true oscillator rate.
34 *
35 * Board-specific wiring options include using split power mode with
36 * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset),
37 * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from
fa5b0782
SN
38 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
39 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
db68b189
DB
40 */
41
db68b189
DB
42/* RTC registers */
43#define OMAP_RTC_SECONDS_REG 0x00
44#define OMAP_RTC_MINUTES_REG 0x04
45#define OMAP_RTC_HOURS_REG 0x08
46#define OMAP_RTC_DAYS_REG 0x0C
47#define OMAP_RTC_MONTHS_REG 0x10
48#define OMAP_RTC_YEARS_REG 0x14
49#define OMAP_RTC_WEEKS_REG 0x18
50
51#define OMAP_RTC_ALARM_SECONDS_REG 0x20
52#define OMAP_RTC_ALARM_MINUTES_REG 0x24
53#define OMAP_RTC_ALARM_HOURS_REG 0x28
54#define OMAP_RTC_ALARM_DAYS_REG 0x2c
55#define OMAP_RTC_ALARM_MONTHS_REG 0x30
56#define OMAP_RTC_ALARM_YEARS_REG 0x34
57
58#define OMAP_RTC_CTRL_REG 0x40
59#define OMAP_RTC_STATUS_REG 0x44
60#define OMAP_RTC_INTERRUPTS_REG 0x48
61
62#define OMAP_RTC_COMP_LSB_REG 0x4c
63#define OMAP_RTC_COMP_MSB_REG 0x50
64#define OMAP_RTC_OSC_REG 0x54
65
cab1458c
AM
66#define OMAP_RTC_KICK0_REG 0x6c
67#define OMAP_RTC_KICK1_REG 0x70
68
8af750e3
HG
69#define OMAP_RTC_IRQWAKEEN 0x7c
70
222a12fc
JH
71#define OMAP_RTC_ALARM2_SECONDS_REG 0x80
72#define OMAP_RTC_ALARM2_MINUTES_REG 0x84
73#define OMAP_RTC_ALARM2_HOURS_REG 0x88
74#define OMAP_RTC_ALARM2_DAYS_REG 0x8c
75#define OMAP_RTC_ALARM2_MONTHS_REG 0x90
76#define OMAP_RTC_ALARM2_YEARS_REG 0x94
77
78#define OMAP_RTC_PMIC_REG 0x98
79
db68b189 80/* OMAP_RTC_CTRL_REG bit fields: */
92adb96a
SN
81#define OMAP_RTC_CTRL_SPLIT BIT(7)
82#define OMAP_RTC_CTRL_DISABLE BIT(6)
83#define OMAP_RTC_CTRL_SET_32_COUNTER BIT(5)
84#define OMAP_RTC_CTRL_TEST BIT(4)
85#define OMAP_RTC_CTRL_MODE_12_24 BIT(3)
86#define OMAP_RTC_CTRL_AUTO_COMP BIT(2)
87#define OMAP_RTC_CTRL_ROUND_30S BIT(1)
88#define OMAP_RTC_CTRL_STOP BIT(0)
db68b189
DB
89
90/* OMAP_RTC_STATUS_REG bit fields: */
92adb96a 91#define OMAP_RTC_STATUS_POWER_UP BIT(7)
222a12fc 92#define OMAP_RTC_STATUS_ALARM2 BIT(7)
92adb96a
SN
93#define OMAP_RTC_STATUS_ALARM BIT(6)
94#define OMAP_RTC_STATUS_1D_EVENT BIT(5)
95#define OMAP_RTC_STATUS_1H_EVENT BIT(4)
96#define OMAP_RTC_STATUS_1M_EVENT BIT(3)
97#define OMAP_RTC_STATUS_1S_EVENT BIT(2)
98#define OMAP_RTC_STATUS_RUN BIT(1)
99#define OMAP_RTC_STATUS_BUSY BIT(0)
db68b189
DB
100
101/* OMAP_RTC_INTERRUPTS_REG bit fields: */
222a12fc 102#define OMAP_RTC_INTERRUPTS_IT_ALARM2 BIT(4)
92adb96a
SN
103#define OMAP_RTC_INTERRUPTS_IT_ALARM BIT(3)
104#define OMAP_RTC_INTERRUPTS_IT_TIMER BIT(2)
db68b189 105
cd914bba
SN
106/* OMAP_RTC_OSC_REG bit fields: */
107#define OMAP_RTC_OSC_32KCLK_EN BIT(6)
108
8af750e3 109/* OMAP_RTC_IRQWAKEEN bit fields: */
92adb96a 110#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN BIT(1)
8af750e3 111
222a12fc
JH
112/* OMAP_RTC_PMIC bit fields: */
113#define OMAP_RTC_PMIC_POWER_EN_EN BIT(16)
114
cab1458c
AM
115/* OMAP_RTC_KICKER values */
116#define KICK0_VALUE 0x83e70b13
117#define KICK1_VALUE 0x95a4f1e0
118
2153f949
JH
119struct omap_rtc_device_type {
120 bool has_32kclk_en;
121 bool has_kicker;
122 bool has_irqwakeen;
222a12fc 123 bool has_pmic_mode;
9291e340 124 bool has_power_up_reset;
2153f949 125};
cd914bba 126
55ba953a
JH
127struct omap_rtc {
128 struct rtc_device *rtc;
129 void __iomem *base;
130 int irq_alarm;
131 int irq_timer;
132 u8 interrupts_reg;
222a12fc 133 bool is_pmic_controller;
2153f949 134 const struct omap_rtc_device_type *type;
55ba953a 135};
db68b189 136
55ba953a
JH
137static inline u8 rtc_read(struct omap_rtc *rtc, unsigned int reg)
138{
139 return readb(rtc->base + reg);
140}
cab1458c 141
c253a896
JH
142static inline u32 rtc_readl(struct omap_rtc *rtc, unsigned int reg)
143{
144 return readl(rtc->base + reg);
145}
146
55ba953a
JH
147static inline void rtc_write(struct omap_rtc *rtc, unsigned int reg, u8 val)
148{
149 writeb(val, rtc->base + reg);
150}
db68b189 151
55ba953a
JH
152static inline void rtc_writel(struct omap_rtc *rtc, unsigned int reg, u32 val)
153{
154 writel(val, rtc->base + reg);
155}
db68b189 156
db68b189
DB
157/* we rely on the rtc framework to handle locking (rtc->ops_lock),
158 * so the only other requirement is that register accesses which
159 * require BUSY to be clear are made with IRQs locally disabled
160 */
55ba953a 161static void rtc_wait_not_busy(struct omap_rtc *rtc)
db68b189
DB
162{
163 int count = 0;
164 u8 status;
165
166 /* BUSY may stay active for 1/32768 second (~30 usec) */
167 for (count = 0; count < 50; count++) {
55ba953a 168 status = rtc_read(rtc, OMAP_RTC_STATUS_REG);
db68b189
DB
169 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
170 break;
171 udelay(1);
172 }
173 /* now we have ~15 usec to read/write various registers */
174}
175
55ba953a 176static irqreturn_t rtc_irq(int irq, void *dev_id)
db68b189 177{
55ba953a 178 struct omap_rtc *rtc = dev_id;
db68b189
DB
179 unsigned long events = 0;
180 u8 irq_data;
181
55ba953a 182 irq_data = rtc_read(rtc, OMAP_RTC_STATUS_REG);
db68b189
DB
183
184 /* alarm irq? */
185 if (irq_data & OMAP_RTC_STATUS_ALARM) {
55ba953a 186 rtc_write(rtc, OMAP_RTC_STATUS_REG, OMAP_RTC_STATUS_ALARM);
db68b189
DB
187 events |= RTC_IRQF | RTC_AF;
188 }
189
190 /* 1/sec periodic/update irq? */
191 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
192 events |= RTC_IRQF | RTC_UF;
193
55ba953a 194 rtc_update_irq(rtc->rtc, 1, events);
db68b189
DB
195
196 return IRQ_HANDLED;
197}
198
16380c15
JS
199static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
200{
55ba953a 201 struct omap_rtc *rtc = dev_get_drvdata(dev);
ab7f580b 202 u8 reg, irqwake_reg = 0;
16380c15
JS
203
204 local_irq_disable();
55ba953a
JH
205 rtc_wait_not_busy(rtc);
206 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
2153f949 207 if (rtc->type->has_irqwakeen)
55ba953a 208 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
ab7f580b
LV
209
210 if (enabled) {
16380c15 211 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
ab7f580b
LV
212 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
213 } else {
16380c15 214 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
ab7f580b
LV
215 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
216 }
55ba953a
JH
217 rtc_wait_not_busy(rtc);
218 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
2153f949 219 if (rtc->type->has_irqwakeen)
55ba953a 220 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
16380c15
JS
221 local_irq_enable();
222
223 return 0;
224}
225
db68b189
DB
226/* this hardware doesn't support "don't care" alarm fields */
227static int tm2bcd(struct rtc_time *tm)
228{
229 if (rtc_valid_tm(tm) != 0)
230 return -EINVAL;
231
fe20ba70
AB
232 tm->tm_sec = bin2bcd(tm->tm_sec);
233 tm->tm_min = bin2bcd(tm->tm_min);
234 tm->tm_hour = bin2bcd(tm->tm_hour);
235 tm->tm_mday = bin2bcd(tm->tm_mday);
db68b189 236
fe20ba70 237 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
db68b189
DB
238
239 /* epoch == 1900 */
240 if (tm->tm_year < 100 || tm->tm_year > 199)
241 return -EINVAL;
fe20ba70 242 tm->tm_year = bin2bcd(tm->tm_year - 100);
db68b189
DB
243
244 return 0;
245}
246
247static void bcd2tm(struct rtc_time *tm)
248{
fe20ba70
AB
249 tm->tm_sec = bcd2bin(tm->tm_sec);
250 tm->tm_min = bcd2bin(tm->tm_min);
251 tm->tm_hour = bcd2bin(tm->tm_hour);
252 tm->tm_mday = bcd2bin(tm->tm_mday);
253 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
db68b189 254 /* epoch == 1900 */
fe20ba70 255 tm->tm_year = bcd2bin(tm->tm_year) + 100;
db68b189
DB
256}
257
cbbe326f 258static void omap_rtc_read_time_raw(struct omap_rtc *rtc, struct rtc_time *tm)
db68b189 259{
55ba953a
JH
260 tm->tm_sec = rtc_read(rtc, OMAP_RTC_SECONDS_REG);
261 tm->tm_min = rtc_read(rtc, OMAP_RTC_MINUTES_REG);
262 tm->tm_hour = rtc_read(rtc, OMAP_RTC_HOURS_REG);
263 tm->tm_mday = rtc_read(rtc, OMAP_RTC_DAYS_REG);
264 tm->tm_mon = rtc_read(rtc, OMAP_RTC_MONTHS_REG);
265 tm->tm_year = rtc_read(rtc, OMAP_RTC_YEARS_REG);
cbbe326f
JH
266}
267
268static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
269{
270 struct omap_rtc *rtc = dev_get_drvdata(dev);
db68b189 271
cbbe326f
JH
272 /* we don't report wday/yday/isdst ... */
273 local_irq_disable();
274 rtc_wait_not_busy(rtc);
275 omap_rtc_read_time_raw(rtc, tm);
db68b189
DB
276 local_irq_enable();
277
278 bcd2tm(tm);
279 return 0;
280}
281
282static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
283{
55ba953a
JH
284 struct omap_rtc *rtc = dev_get_drvdata(dev);
285
db68b189
DB
286 if (tm2bcd(tm) < 0)
287 return -EINVAL;
288 local_irq_disable();
55ba953a 289 rtc_wait_not_busy(rtc);
db68b189 290
55ba953a
JH
291 rtc_write(rtc, OMAP_RTC_YEARS_REG, tm->tm_year);
292 rtc_write(rtc, OMAP_RTC_MONTHS_REG, tm->tm_mon);
293 rtc_write(rtc, OMAP_RTC_DAYS_REG, tm->tm_mday);
294 rtc_write(rtc, OMAP_RTC_HOURS_REG, tm->tm_hour);
295 rtc_write(rtc, OMAP_RTC_MINUTES_REG, tm->tm_min);
296 rtc_write(rtc, OMAP_RTC_SECONDS_REG, tm->tm_sec);
db68b189
DB
297
298 local_irq_enable();
299
300 return 0;
301}
302
303static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
304{
55ba953a
JH
305 struct omap_rtc *rtc = dev_get_drvdata(dev);
306
db68b189 307 local_irq_disable();
55ba953a 308 rtc_wait_not_busy(rtc);
db68b189 309
55ba953a
JH
310 alm->time.tm_sec = rtc_read(rtc, OMAP_RTC_ALARM_SECONDS_REG);
311 alm->time.tm_min = rtc_read(rtc, OMAP_RTC_ALARM_MINUTES_REG);
312 alm->time.tm_hour = rtc_read(rtc, OMAP_RTC_ALARM_HOURS_REG);
313 alm->time.tm_mday = rtc_read(rtc, OMAP_RTC_ALARM_DAYS_REG);
314 alm->time.tm_mon = rtc_read(rtc, OMAP_RTC_ALARM_MONTHS_REG);
315 alm->time.tm_year = rtc_read(rtc, OMAP_RTC_ALARM_YEARS_REG);
db68b189
DB
316
317 local_irq_enable();
318
319 bcd2tm(&alm->time);
55ba953a 320 alm->enabled = !!(rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG)
db68b189 321 & OMAP_RTC_INTERRUPTS_IT_ALARM);
db68b189
DB
322
323 return 0;
324}
325
326static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
327{
55ba953a 328 struct omap_rtc *rtc = dev_get_drvdata(dev);
ab7f580b 329 u8 reg, irqwake_reg = 0;
db68b189 330
db68b189
DB
331 if (tm2bcd(&alm->time) < 0)
332 return -EINVAL;
333
334 local_irq_disable();
55ba953a 335 rtc_wait_not_busy(rtc);
db68b189 336
55ba953a
JH
337 rtc_write(rtc, OMAP_RTC_ALARM_YEARS_REG, alm->time.tm_year);
338 rtc_write(rtc, OMAP_RTC_ALARM_MONTHS_REG, alm->time.tm_mon);
339 rtc_write(rtc, OMAP_RTC_ALARM_DAYS_REG, alm->time.tm_mday);
340 rtc_write(rtc, OMAP_RTC_ALARM_HOURS_REG, alm->time.tm_hour);
341 rtc_write(rtc, OMAP_RTC_ALARM_MINUTES_REG, alm->time.tm_min);
342 rtc_write(rtc, OMAP_RTC_ALARM_SECONDS_REG, alm->time.tm_sec);
db68b189 343
55ba953a 344 reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
2153f949 345 if (rtc->type->has_irqwakeen)
55ba953a 346 irqwake_reg = rtc_read(rtc, OMAP_RTC_IRQWAKEEN);
ab7f580b
LV
347
348 if (alm->enabled) {
db68b189 349 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
ab7f580b
LV
350 irqwake_reg |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
351 } else {
db68b189 352 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
ab7f580b
LV
353 irqwake_reg &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
354 }
55ba953a 355 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, reg);
2153f949 356 if (rtc->type->has_irqwakeen)
55ba953a 357 rtc_write(rtc, OMAP_RTC_IRQWAKEEN, irqwake_reg);
db68b189
DB
358
359 local_irq_enable();
360
361 return 0;
362}
363
222a12fc
JH
364static struct omap_rtc *omap_rtc_power_off_rtc;
365
366/*
367 * omap_rtc_poweroff: RTC-controlled power off
368 *
369 * The RTC can be used to control an external PMIC via the pmic_power_en pin,
370 * which can be configured to transition to OFF on ALARM2 events.
371 *
372 * Notes:
373 * The two-second alarm offset is the shortest offset possible as the alarm
374 * registers must be set before the next timer update and the offset
375 * calculation is too heavy for everything to be done within a single access
376 * period (~15 us).
377 *
378 * Called with local interrupts disabled.
379 */
380static void omap_rtc_power_off(void)
381{
382 struct omap_rtc *rtc = omap_rtc_power_off_rtc;
383 struct rtc_time tm;
384 unsigned long now;
385 u32 val;
386
387 /* enable pmic_power_en control */
388 val = rtc_readl(rtc, OMAP_RTC_PMIC_REG);
389 rtc_writel(rtc, OMAP_RTC_PMIC_REG, val | OMAP_RTC_PMIC_POWER_EN_EN);
390
391 /* set alarm two seconds from now */
392 omap_rtc_read_time_raw(rtc, &tm);
393 bcd2tm(&tm);
394 rtc_tm_to_time(&tm, &now);
395 rtc_time_to_tm(now + 2, &tm);
396
397 if (tm2bcd(&tm) < 0) {
398 dev_err(&rtc->rtc->dev, "power off failed\n");
399 return;
400 }
401
402 rtc_wait_not_busy(rtc);
403
404 rtc_write(rtc, OMAP_RTC_ALARM2_SECONDS_REG, tm.tm_sec);
405 rtc_write(rtc, OMAP_RTC_ALARM2_MINUTES_REG, tm.tm_min);
406 rtc_write(rtc, OMAP_RTC_ALARM2_HOURS_REG, tm.tm_hour);
407 rtc_write(rtc, OMAP_RTC_ALARM2_DAYS_REG, tm.tm_mday);
408 rtc_write(rtc, OMAP_RTC_ALARM2_MONTHS_REG, tm.tm_mon);
409 rtc_write(rtc, OMAP_RTC_ALARM2_YEARS_REG, tm.tm_year);
410
411 /*
412 * enable ALARM2 interrupt
413 *
414 * NOTE: this fails on AM3352 if rtc_write (writeb) is used
415 */
416 val = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
417 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG,
418 val | OMAP_RTC_INTERRUPTS_IT_ALARM2);
419
420 /*
421 * Wait for alarm to trigger (within two seconds) and external PMIC to
422 * power off the system. Add a 500 ms margin for external latencies
423 * (e.g. debounce circuits).
424 */
425 mdelay(2500);
426}
427
db68b189 428static struct rtc_class_ops omap_rtc_ops = {
db68b189
DB
429 .read_time = omap_rtc_read_time,
430 .set_time = omap_rtc_set_time,
431 .read_alarm = omap_rtc_read_alarm,
432 .set_alarm = omap_rtc_set_alarm,
16380c15 433 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
db68b189
DB
434};
435
2153f949 436static const struct omap_rtc_device_type omap_rtc_default_type = {
9291e340 437 .has_power_up_reset = true,
2153f949
JH
438};
439
440static const struct omap_rtc_device_type omap_rtc_am3352_type = {
441 .has_32kclk_en = true,
442 .has_kicker = true,
443 .has_irqwakeen = true,
222a12fc 444 .has_pmic_mode = true,
2153f949
JH
445};
446
447static const struct omap_rtc_device_type omap_rtc_da830_type = {
448 .has_kicker = true,
449};
9e0344dc 450
2153f949 451static const struct platform_device_id omap_rtc_id_table[] = {
cab1458c 452 {
a430ca22 453 .name = "omap_rtc",
2153f949
JH
454 .driver_data = (kernel_ulong_t)&omap_rtc_default_type,
455 }, {
8af750e3 456 .name = "am3352-rtc",
2153f949
JH
457 .driver_data = (kernel_ulong_t)&omap_rtc_am3352_type,
458 }, {
cab1458c 459 .name = "da830-rtc",
2153f949
JH
460 .driver_data = (kernel_ulong_t)&omap_rtc_da830_type,
461 }, {
462 /* sentinel */
463 }
cab1458c 464};
2153f949 465MODULE_DEVICE_TABLE(platform, omap_rtc_id_table);
cab1458c 466
9e0344dc 467static const struct of_device_id omap_rtc_of_match[] = {
2153f949
JH
468 {
469 .compatible = "ti,am3352-rtc",
470 .data = &omap_rtc_am3352_type,
471 }, {
472 .compatible = "ti,da830-rtc",
473 .data = &omap_rtc_da830_type,
474 }, {
475 /* sentinel */
476 }
9e0344dc
AM
477};
478MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
479
71fc8224 480static int __init omap_rtc_probe(struct platform_device *pdev)
db68b189 481{
55ba953a 482 struct omap_rtc *rtc;
3765e8f1 483 struct resource *res;
9291e340 484 u8 reg, mask, new_ctrl;
cab1458c 485 const struct platform_device_id *id_entry;
9e0344dc 486 const struct of_device_id *of_id;
437b37a6 487 int ret;
9e0344dc 488
55ba953a
JH
489 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
490 if (!rtc)
491 return -ENOMEM;
492
9e0344dc 493 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
2153f949
JH
494 if (of_id) {
495 rtc->type = of_id->data;
222a12fc
JH
496 rtc->is_pmic_controller = rtc->type->has_pmic_mode &&
497 of_property_read_bool(pdev->dev.of_node,
498 "ti,system-power-controller");
2153f949
JH
499 } else {
500 id_entry = platform_get_device_id(pdev);
501 rtc->type = (void *)id_entry->driver_data;
337b600f
SN
502 }
503
55ba953a
JH
504 rtc->irq_timer = platform_get_irq(pdev, 0);
505 if (rtc->irq_timer <= 0)
db68b189 506 return -ENOENT;
db68b189 507
55ba953a
JH
508 rtc->irq_alarm = platform_get_irq(pdev, 1);
509 if (rtc->irq_alarm <= 0)
db68b189 510 return -ENOENT;
db68b189 511
db68b189 512 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
55ba953a
JH
513 rtc->base = devm_ioremap_resource(&pdev->dev, res);
514 if (IS_ERR(rtc->base))
515 return PTR_ERR(rtc->base);
516
517 platform_set_drvdata(pdev, rtc);
8cfde8c1 518
fc9bd902
VH
519 /* Enable the clock/module so that we can access the registers */
520 pm_runtime_enable(&pdev->dev);
521 pm_runtime_get_sync(&pdev->dev);
522
2153f949 523 if (rtc->type->has_kicker) {
55ba953a
JH
524 rtc_writel(rtc, OMAP_RTC_KICK0_REG, KICK0_VALUE);
525 rtc_writel(rtc, OMAP_RTC_KICK1_REG, KICK1_VALUE);
cab1458c
AM
526 }
527
1ed8b5d2
JH
528 /*
529 * disable interrupts
530 *
531 * NOTE: ALARM2 is not cleared on AM3352 if rtc_write (writeb) is used
db68b189 532 */
55ba953a 533 rtc_writel(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
db68b189 534
cd914bba 535 /* enable RTC functional clock */
2153f949 536 if (rtc->type->has_32kclk_en) {
55ba953a
JH
537 reg = rtc_read(rtc, OMAP_RTC_OSC_REG);
538 rtc_writel(rtc, OMAP_RTC_OSC_REG,
539 reg | OMAP_RTC_OSC_32KCLK_EN);
44c63a57 540 }
cd914bba 541
db68b189 542 /* clear old status */
55ba953a 543 reg = rtc_read(rtc, OMAP_RTC_STATUS_REG);
9291e340
JH
544
545 mask = OMAP_RTC_STATUS_ALARM;
546
222a12fc
JH
547 if (rtc->type->has_pmic_mode)
548 mask |= OMAP_RTC_STATUS_ALARM2;
549
9291e340
JH
550 if (rtc->type->has_power_up_reset) {
551 mask |= OMAP_RTC_STATUS_POWER_UP;
552 if (reg & OMAP_RTC_STATUS_POWER_UP)
553 dev_info(&pdev->dev, "RTC power up reset detected\n");
db68b189 554 }
9291e340
JH
555
556 if (reg & mask)
557 rtc_write(rtc, OMAP_RTC_STATUS_REG, reg & mask);
db68b189 558
db68b189 559 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
55ba953a 560 reg = rtc_read(rtc, OMAP_RTC_CTRL_REG);
db68b189 561 if (reg & (u8) OMAP_RTC_CTRL_STOP)
397b630a 562 dev_info(&pdev->dev, "already running\n");
db68b189
DB
563
564 /* force to 24 hour mode */
12b3e038 565 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
db68b189
DB
566 new_ctrl |= OMAP_RTC_CTRL_STOP;
567
568 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
569 *
fa5b0782
SN
570 * - Device wake-up capability setting should come through chip
571 * init logic. OMAP1 boards should initialize the "wakeup capable"
572 * flag in the platform device if the board is wired right for
573 * being woken up by RTC alarm. For OMAP-L138, this capability
574 * is built into the SoC by the "Deep Sleep" capability.
db68b189
DB
575 *
576 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
577 * rather than nPWRON_RESET, should forcibly enable split
578 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
579 * is write-only, and always reads as zero...)
580 */
db68b189
DB
581
582 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
397b630a 583 dev_info(&pdev->dev, "split power mode\n");
db68b189
DB
584
585 if (reg != new_ctrl)
55ba953a 586 rtc_write(rtc, OMAP_RTC_CTRL_REG, new_ctrl);
db68b189 587
4390ce00
JH
588 device_init_wakeup(&pdev->dev, true);
589
55ba953a 590 rtc->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
4390ce00 591 &omap_rtc_ops, THIS_MODULE);
55ba953a
JH
592 if (IS_ERR(rtc->rtc)) {
593 ret = PTR_ERR(rtc->rtc);
4390ce00
JH
594 goto err;
595 }
4390ce00
JH
596
597 /* handle periodic and alarm irqs */
55ba953a
JH
598 ret = devm_request_irq(&pdev->dev, rtc->irq_timer, rtc_irq, 0,
599 dev_name(&rtc->rtc->dev), rtc);
4390ce00
JH
600 if (ret)
601 goto err;
602
55ba953a
JH
603 if (rtc->irq_timer != rtc->irq_alarm) {
604 ret = devm_request_irq(&pdev->dev, rtc->irq_alarm, rtc_irq, 0,
605 dev_name(&rtc->rtc->dev), rtc);
4390ce00
JH
606 if (ret)
607 goto err;
608 }
609
222a12fc
JH
610 if (rtc->is_pmic_controller) {
611 if (!pm_power_off) {
612 omap_rtc_power_off_rtc = rtc;
613 pm_power_off = omap_rtc_power_off;
614 }
615 }
616
db68b189
DB
617 return 0;
618
437b37a6 619err:
7ecd9a3f 620 device_init_wakeup(&pdev->dev, false);
2153f949 621 if (rtc->type->has_kicker)
55ba953a 622 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
fc9bd902
VH
623 pm_runtime_put_sync(&pdev->dev);
624 pm_runtime_disable(&pdev->dev);
437b37a6
JH
625
626 return ret;
db68b189
DB
627}
628
71fc8224 629static int __exit omap_rtc_remove(struct platform_device *pdev)
db68b189 630{
55ba953a 631 struct omap_rtc *rtc = platform_get_drvdata(pdev);
db68b189 632
222a12fc
JH
633 if (pm_power_off == omap_rtc_power_off &&
634 omap_rtc_power_off_rtc == rtc) {
635 pm_power_off = NULL;
636 omap_rtc_power_off_rtc = NULL;
637 }
638
db68b189
DB
639 device_init_wakeup(&pdev->dev, 0);
640
641 /* leave rtc running, but disable irqs */
55ba953a 642 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
db68b189 643
2153f949 644 if (rtc->type->has_kicker)
55ba953a 645 rtc_writel(rtc, OMAP_RTC_KICK0_REG, 0);
fc9bd902
VH
646
647 /* Disable the clock/module */
648 pm_runtime_put_sync(&pdev->dev);
649 pm_runtime_disable(&pdev->dev);
650
db68b189
DB
651 return 0;
652}
653
04ebc359 654#ifdef CONFIG_PM_SLEEP
04ebc359 655static int omap_rtc_suspend(struct device *dev)
db68b189 656{
55ba953a
JH
657 struct omap_rtc *rtc = dev_get_drvdata(dev);
658
659 rtc->interrupts_reg = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
db68b189
DB
660
661 /* FIXME the RTC alarm is not currently acting as a wakeup event
8af750e3
HG
662 * source on some platforms, and in fact this enable() call is just
663 * saving a flag that's never used...
db68b189 664 */
ab7f580b 665 if (device_may_wakeup(dev))
55ba953a 666 enable_irq_wake(rtc->irq_alarm);
ab7f580b 667 else
55ba953a 668 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, 0);
db68b189 669
fc9bd902 670 /* Disable the clock/module */
04ebc359 671 pm_runtime_put_sync(dev);
fc9bd902 672
db68b189
DB
673 return 0;
674}
675
04ebc359 676static int omap_rtc_resume(struct device *dev)
db68b189 677{
55ba953a
JH
678 struct omap_rtc *rtc = dev_get_drvdata(dev);
679
fc9bd902 680 /* Enable the clock/module so that we can access the registers */
04ebc359 681 pm_runtime_get_sync(dev);
fc9bd902 682
ab7f580b 683 if (device_may_wakeup(dev))
55ba953a 684 disable_irq_wake(rtc->irq_alarm);
ab7f580b 685 else
55ba953a 686 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, rtc->interrupts_reg);
ab7f580b 687
db68b189
DB
688 return 0;
689}
db68b189
DB
690#endif
691
04ebc359
JH
692static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
693
db68b189
DB
694static void omap_rtc_shutdown(struct platform_device *pdev)
695{
55ba953a 696 struct omap_rtc *rtc = platform_get_drvdata(pdev);
8ad5c722 697 u8 mask;
55ba953a 698
8ad5c722
JH
699 /*
700 * Keep the ALARM interrupt enabled to allow the system to power up on
701 * alarm events.
702 */
703 mask = rtc_read(rtc, OMAP_RTC_INTERRUPTS_REG);
704 mask &= OMAP_RTC_INTERRUPTS_IT_ALARM;
705 rtc_write(rtc, OMAP_RTC_INTERRUPTS_REG, mask);
db68b189
DB
706}
707
db68b189 708static struct platform_driver omap_rtc_driver = {
71fc8224 709 .remove = __exit_p(omap_rtc_remove),
db68b189
DB
710 .shutdown = omap_rtc_shutdown,
711 .driver = {
a430ca22 712 .name = "omap_rtc",
db68b189 713 .owner = THIS_MODULE,
04ebc359 714 .pm = &omap_rtc_pm_ops,
616b7341 715 .of_match_table = omap_rtc_of_match,
db68b189 716 },
2153f949 717 .id_table = omap_rtc_id_table,
db68b189
DB
718};
719
09c5a36b 720module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
db68b189 721
a430ca22 722MODULE_ALIAS("platform:omap_rtc");
db68b189
DB
723MODULE_AUTHOR("George G. Davis (and others)");
724MODULE_LICENSE("GPL");
This page took 0.717376 seconds and 5 git commands to generate.