blob: 70f51491809d752c0b1f7dfee9dcba2ee668f005 [file] [log] [blame]
David Brownelldb68b182006-12-06 20:38:36 -08001/*
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>
Afzal Mohammed9e0344d2012-12-17 16:02:15 -080023#include <linux/of.h>
24#include <linux/of_device.h>
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -080025#include <linux/pm_runtime.h>
Sachin Kamat4b30c9f2013-07-03 15:06:00 -070026#include <linux/io.h>
David Brownelldb68b182006-12-06 20:38:36 -080027
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
Sekhar Norifa5b0782010-10-27 15:33:05 -070038 * low power modes) for OMAP1 boards (OMAP-L138 has this built into
39 * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment.
David Brownelldb68b182006-12-06 20:38:36 -080040 */
41
Afzal Mohammedcab14582012-12-17 16:02:11 -080042#define DRIVER_NAME "omap_rtc"
43
David Brownelldb68b182006-12-06 20:38:36 -080044#define OMAP_RTC_BASE 0xfffb4800
45
46/* RTC registers */
47#define OMAP_RTC_SECONDS_REG 0x00
48#define OMAP_RTC_MINUTES_REG 0x04
49#define OMAP_RTC_HOURS_REG 0x08
50#define OMAP_RTC_DAYS_REG 0x0C
51#define OMAP_RTC_MONTHS_REG 0x10
52#define OMAP_RTC_YEARS_REG 0x14
53#define OMAP_RTC_WEEKS_REG 0x18
54
55#define OMAP_RTC_ALARM_SECONDS_REG 0x20
56#define OMAP_RTC_ALARM_MINUTES_REG 0x24
57#define OMAP_RTC_ALARM_HOURS_REG 0x28
58#define OMAP_RTC_ALARM_DAYS_REG 0x2c
59#define OMAP_RTC_ALARM_MONTHS_REG 0x30
60#define OMAP_RTC_ALARM_YEARS_REG 0x34
61
62#define OMAP_RTC_CTRL_REG 0x40
63#define OMAP_RTC_STATUS_REG 0x44
64#define OMAP_RTC_INTERRUPTS_REG 0x48
65
66#define OMAP_RTC_COMP_LSB_REG 0x4c
67#define OMAP_RTC_COMP_MSB_REG 0x50
68#define OMAP_RTC_OSC_REG 0x54
69
Afzal Mohammedcab14582012-12-17 16:02:11 -080070#define OMAP_RTC_KICK0_REG 0x6c
71#define OMAP_RTC_KICK1_REG 0x70
72
Hebbar Gururaja8af750e2013-09-11 14:24:18 -070073#define OMAP_RTC_IRQWAKEEN 0x7c
74
David Brownelldb68b182006-12-06 20:38:36 -080075/* OMAP_RTC_CTRL_REG bit fields: */
76#define OMAP_RTC_CTRL_SPLIT (1<<7)
77#define OMAP_RTC_CTRL_DISABLE (1<<6)
78#define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5)
79#define OMAP_RTC_CTRL_TEST (1<<4)
80#define OMAP_RTC_CTRL_MODE_12_24 (1<<3)
81#define OMAP_RTC_CTRL_AUTO_COMP (1<<2)
82#define OMAP_RTC_CTRL_ROUND_30S (1<<1)
83#define OMAP_RTC_CTRL_STOP (1<<0)
84
85/* OMAP_RTC_STATUS_REG bit fields: */
86#define OMAP_RTC_STATUS_POWER_UP (1<<7)
87#define OMAP_RTC_STATUS_ALARM (1<<6)
88#define OMAP_RTC_STATUS_1D_EVENT (1<<5)
89#define OMAP_RTC_STATUS_1H_EVENT (1<<4)
90#define OMAP_RTC_STATUS_1M_EVENT (1<<3)
91#define OMAP_RTC_STATUS_1S_EVENT (1<<2)
92#define OMAP_RTC_STATUS_RUN (1<<1)
93#define OMAP_RTC_STATUS_BUSY (1<<0)
94
95/* OMAP_RTC_INTERRUPTS_REG bit fields: */
96#define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3)
97#define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2)
98
Hebbar Gururaja8af750e2013-09-11 14:24:18 -070099/* OMAP_RTC_IRQWAKEEN bit fields: */
100#define OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN (1<<1)
101
Afzal Mohammedcab14582012-12-17 16:02:11 -0800102/* OMAP_RTC_KICKER values */
103#define KICK0_VALUE 0x83e70b13
104#define KICK1_VALUE 0x95a4f1e0
105
106#define OMAP_RTC_HAS_KICKER 0x1
107
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700108/*
109 * Few RTC IP revisions has special WAKE-EN Register to enable Wakeup
110 * generation for event Alarm.
111 */
112#define OMAP_RTC_HAS_IRQWAKEEN 0x2
113
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800114static void __iomem *rtc_base;
David Brownelldb68b182006-12-06 20:38:36 -0800115
Afzal Mohammedcab14582012-12-17 16:02:11 -0800116#define rtc_read(addr) readb(rtc_base + (addr))
117#define rtc_write(val, addr) writeb(val, rtc_base + (addr))
118
119#define rtc_writel(val, addr) writel(val, rtc_base + (addr))
David Brownelldb68b182006-12-06 20:38:36 -0800120
121
David Brownelldb68b182006-12-06 20:38:36 -0800122/* we rely on the rtc framework to handle locking (rtc->ops_lock),
123 * so the only other requirement is that register accesses which
124 * require BUSY to be clear are made with IRQs locally disabled
125 */
126static void rtc_wait_not_busy(void)
127{
128 int count = 0;
129 u8 status;
130
131 /* BUSY may stay active for 1/32768 second (~30 usec) */
132 for (count = 0; count < 50; count++) {
133 status = rtc_read(OMAP_RTC_STATUS_REG);
134 if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0)
135 break;
136 udelay(1);
137 }
138 /* now we have ~15 usec to read/write various registers */
139}
140
David Brownellab6a2d72007-05-08 00:33:30 -0700141static irqreturn_t rtc_irq(int irq, void *rtc)
David Brownelldb68b182006-12-06 20:38:36 -0800142{
143 unsigned long events = 0;
144 u8 irq_data;
145
146 irq_data = rtc_read(OMAP_RTC_STATUS_REG);
147
148 /* alarm irq? */
149 if (irq_data & OMAP_RTC_STATUS_ALARM) {
150 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
151 events |= RTC_IRQF | RTC_AF;
152 }
153
154 /* 1/sec periodic/update irq? */
155 if (irq_data & OMAP_RTC_STATUS_1S_EVENT)
156 events |= RTC_IRQF | RTC_UF;
157
David Brownellab6a2d72007-05-08 00:33:30 -0700158 rtc_update_irq(rtc, 1, events);
David Brownelldb68b182006-12-06 20:38:36 -0800159
160 return IRQ_HANDLED;
161}
162
John Stultz16380c12011-02-02 17:02:41 -0800163static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
164{
165 u8 reg;
166
167 local_irq_disable();
168 rtc_wait_not_busy();
169 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
170 if (enabled)
171 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
172 else
173 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
174 rtc_wait_not_busy();
175 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
176 local_irq_enable();
177
178 return 0;
179}
180
David Brownelldb68b182006-12-06 20:38:36 -0800181/* this hardware doesn't support "don't care" alarm fields */
182static int tm2bcd(struct rtc_time *tm)
183{
184 if (rtc_valid_tm(tm) != 0)
185 return -EINVAL;
186
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700187 tm->tm_sec = bin2bcd(tm->tm_sec);
188 tm->tm_min = bin2bcd(tm->tm_min);
189 tm->tm_hour = bin2bcd(tm->tm_hour);
190 tm->tm_mday = bin2bcd(tm->tm_mday);
David Brownelldb68b182006-12-06 20:38:36 -0800191
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700192 tm->tm_mon = bin2bcd(tm->tm_mon + 1);
David Brownelldb68b182006-12-06 20:38:36 -0800193
194 /* epoch == 1900 */
195 if (tm->tm_year < 100 || tm->tm_year > 199)
196 return -EINVAL;
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700197 tm->tm_year = bin2bcd(tm->tm_year - 100);
David Brownelldb68b182006-12-06 20:38:36 -0800198
199 return 0;
200}
201
202static void bcd2tm(struct rtc_time *tm)
203{
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700204 tm->tm_sec = bcd2bin(tm->tm_sec);
205 tm->tm_min = bcd2bin(tm->tm_min);
206 tm->tm_hour = bcd2bin(tm->tm_hour);
207 tm->tm_mday = bcd2bin(tm->tm_mday);
208 tm->tm_mon = bcd2bin(tm->tm_mon) - 1;
David Brownelldb68b182006-12-06 20:38:36 -0800209 /* epoch == 1900 */
Adrian Bunkfe20ba72008-10-18 20:28:41 -0700210 tm->tm_year = bcd2bin(tm->tm_year) + 100;
David Brownelldb68b182006-12-06 20:38:36 -0800211}
212
213
214static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm)
215{
216 /* we don't report wday/yday/isdst ... */
217 local_irq_disable();
218 rtc_wait_not_busy();
219
220 tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG);
221 tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG);
222 tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG);
223 tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG);
224 tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG);
225 tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG);
226
227 local_irq_enable();
228
229 bcd2tm(tm);
230 return 0;
231}
232
233static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm)
234{
235 if (tm2bcd(tm) < 0)
236 return -EINVAL;
237 local_irq_disable();
238 rtc_wait_not_busy();
239
240 rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG);
241 rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG);
242 rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG);
243 rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG);
244 rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG);
245 rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG);
246
247 local_irq_enable();
248
249 return 0;
250}
251
252static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm)
253{
254 local_irq_disable();
255 rtc_wait_not_busy();
256
257 alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG);
258 alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG);
259 alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG);
260 alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG);
261 alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG);
262 alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG);
263
264 local_irq_enable();
265
266 bcd2tm(&alm->time);
David Brownella2db8df2006-12-13 00:35:08 -0800267 alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG)
David Brownelldb68b182006-12-06 20:38:36 -0800268 & OMAP_RTC_INTERRUPTS_IT_ALARM);
David Brownelldb68b182006-12-06 20:38:36 -0800269
270 return 0;
271}
272
273static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm)
274{
275 u8 reg;
276
David Brownelldb68b182006-12-06 20:38:36 -0800277 if (tm2bcd(&alm->time) < 0)
278 return -EINVAL;
279
280 local_irq_disable();
281 rtc_wait_not_busy();
282
283 rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG);
284 rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG);
285 rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG);
286 rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG);
287 rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG);
288 rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG);
289
290 reg = rtc_read(OMAP_RTC_INTERRUPTS_REG);
291 if (alm->enabled)
292 reg |= OMAP_RTC_INTERRUPTS_IT_ALARM;
293 else
294 reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM;
295 rtc_write(reg, OMAP_RTC_INTERRUPTS_REG);
296
297 local_irq_enable();
298
299 return 0;
300}
301
302static struct rtc_class_ops omap_rtc_ops = {
David Brownelldb68b182006-12-06 20:38:36 -0800303 .read_time = omap_rtc_read_time,
304 .set_time = omap_rtc_set_time,
305 .read_alarm = omap_rtc_read_alarm,
306 .set_alarm = omap_rtc_set_alarm,
John Stultz16380c12011-02-02 17:02:41 -0800307 .alarm_irq_enable = omap_rtc_alarm_irq_enable,
David Brownelldb68b182006-12-06 20:38:36 -0800308};
309
310static int omap_rtc_alarm;
311static int omap_rtc_timer;
312
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700313#define OMAP_RTC_DATA_AM3352_IDX 1
314#define OMAP_RTC_DATA_DA830_IDX 2
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800315
Afzal Mohammedcab14582012-12-17 16:02:11 -0800316static struct platform_device_id omap_rtc_devtype[] = {
317 {
318 .name = DRIVER_NAME,
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700319 },
320 [OMAP_RTC_DATA_AM3352_IDX] = {
321 .name = "am3352-rtc",
322 .driver_data = OMAP_RTC_HAS_KICKER | OMAP_RTC_HAS_IRQWAKEEN,
323 },
324 [OMAP_RTC_DATA_DA830_IDX] = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800325 .name = "da830-rtc",
326 .driver_data = OMAP_RTC_HAS_KICKER,
327 },
328 {},
329};
330MODULE_DEVICE_TABLE(platform, omap_rtc_devtype);
331
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800332static const struct of_device_id omap_rtc_of_match[] = {
333 { .compatible = "ti,da830-rtc",
334 .data = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX],
335 },
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700336 { .compatible = "ti,am3352-rtc",
337 .data = &omap_rtc_devtype[OMAP_RTC_DATA_AM3352_IDX],
338 },
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800339 {},
340};
341MODULE_DEVICE_TABLE(of, omap_rtc_of_match);
342
David Brownell71fc8222008-07-23 21:30:38 -0700343static int __init omap_rtc_probe(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800344{
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700345 struct resource *res;
David Brownelldb68b182006-12-06 20:38:36 -0800346 struct rtc_device *rtc;
347 u8 reg, new_ctrl;
Afzal Mohammedcab14582012-12-17 16:02:11 -0800348 const struct platform_device_id *id_entry;
Afzal Mohammed9e0344d2012-12-17 16:02:15 -0800349 const struct of_device_id *of_id;
350
351 of_id = of_match_device(omap_rtc_of_match, &pdev->dev);
352 if (of_id)
353 pdev->id_entry = of_id->data;
David Brownelldb68b182006-12-06 20:38:36 -0800354
Sekhar Nori337b6002014-06-06 14:36:04 -0700355 id_entry = platform_get_device_id(pdev);
356 if (!id_entry) {
357 dev_err(&pdev->dev, "no matching device entry\n");
358 return -ENODEV;
359 }
360
David Brownelldb68b182006-12-06 20:38:36 -0800361 omap_rtc_timer = platform_get_irq(pdev, 0);
362 if (omap_rtc_timer <= 0) {
363 pr_debug("%s: no update irq?\n", pdev->name);
364 return -ENOENT;
365 }
366
367 omap_rtc_alarm = platform_get_irq(pdev, 1);
368 if (omap_rtc_alarm <= 0) {
369 pr_debug("%s: no alarm irq?\n", pdev->name);
370 return -ENOENT;
371 }
372
David Brownelldb68b182006-12-06 20:38:36 -0800373 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700374 rtc_base = devm_ioremap_resource(&pdev->dev, res);
375 if (IS_ERR(rtc_base))
376 return PTR_ERR(rtc_base);
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800377
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800378 /* Enable the clock/module so that we can access the registers */
379 pm_runtime_enable(&pdev->dev);
380 pm_runtime_get_sync(&pdev->dev);
381
Sekhar Nori337b6002014-06-06 14:36:04 -0700382 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER) {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800383 rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG);
384 rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG);
385 }
386
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700387 rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
David Brownelldb68b182006-12-06 20:38:36 -0800388 &omap_rtc_ops, THIS_MODULE);
389 if (IS_ERR(rtc)) {
390 pr_debug("%s: can't register RTC device, err %ld\n",
391 pdev->name, PTR_ERR(rtc));
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800392 goto fail0;
David Brownelldb68b182006-12-06 20:38:36 -0800393 }
394 platform_set_drvdata(pdev, rtc);
David Brownelldb68b182006-12-06 20:38:36 -0800395
396 /* clear pending irqs, and set 1/second periodic,
397 * which we'll use instead of update irqs
398 */
399 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
400
401 /* clear old status */
402 reg = rtc_read(OMAP_RTC_STATUS_REG);
403 if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) {
404 pr_info("%s: RTC power up reset detected\n",
405 pdev->name);
406 rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG);
407 }
408 if (reg & (u8) OMAP_RTC_STATUS_ALARM)
409 rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG);
410
411 /* handle periodic and alarm irqs */
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700412 if (devm_request_irq(&pdev->dev, omap_rtc_timer, rtc_irq, 0,
Kay Sievers744bcb12009-03-24 16:38:22 -0700413 dev_name(&rtc->dev), rtc)) {
David Brownelldb68b182006-12-06 20:38:36 -0800414 pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n",
415 pdev->name, omap_rtc_timer);
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700416 goto fail0;
David Brownelldb68b182006-12-06 20:38:36 -0800417 }
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800418 if ((omap_rtc_timer != omap_rtc_alarm) &&
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700419 (devm_request_irq(&pdev->dev, omap_rtc_alarm, rtc_irq, 0,
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800420 dev_name(&rtc->dev), rtc))) {
David Brownelldb68b182006-12-06 20:38:36 -0800421 pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n",
422 pdev->name, omap_rtc_alarm);
Vishwanathrao Badarkhe, Manish3765e8f2013-04-29 16:20:04 -0700423 goto fail0;
David Brownelldb68b182006-12-06 20:38:36 -0800424 }
425
426 /* On boards with split power, RTC_ON_NOFF won't reset the RTC */
427 reg = rtc_read(OMAP_RTC_CTRL_REG);
428 if (reg & (u8) OMAP_RTC_CTRL_STOP)
429 pr_info("%s: already running\n", pdev->name);
430
431 /* force to 24 hour mode */
Daniel Glöckner12b3e032011-08-03 16:21:02 -0700432 new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP);
David Brownelldb68b182006-12-06 20:38:36 -0800433 new_ctrl |= OMAP_RTC_CTRL_STOP;
434
435 /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE:
436 *
Sekhar Norifa5b0782010-10-27 15:33:05 -0700437 * - Device wake-up capability setting should come through chip
438 * init logic. OMAP1 boards should initialize the "wakeup capable"
439 * flag in the platform device if the board is wired right for
440 * being woken up by RTC alarm. For OMAP-L138, this capability
441 * is built into the SoC by the "Deep Sleep" capability.
David Brownelldb68b182006-12-06 20:38:36 -0800442 *
443 * - Boards wired so RTC_ON_nOFF is used as the reset signal,
444 * rather than nPWRON_RESET, should forcibly enable split
445 * power mode. (Some chip errata report that RTC_CTRL_SPLIT
446 * is write-only, and always reads as zero...)
447 */
David Brownelldb68b182006-12-06 20:38:36 -0800448
Hebbar Gururaja1d2e2b62013-07-03 15:08:02 -0700449 device_init_wakeup(&pdev->dev, true);
450
David Brownelldb68b182006-12-06 20:38:36 -0800451 if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT)
452 pr_info("%s: split power mode\n", pdev->name);
453
454 if (reg != new_ctrl)
455 rtc_write(new_ctrl, OMAP_RTC_CTRL_REG);
456
457 return 0;
458
Mark A. Greer8cfde8c2009-12-15 16:46:11 -0800459fail0:
Sekhar Nori337b6002014-06-06 14:36:04 -0700460 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER)
Afzal Mohammedcab14582012-12-17 16:02:11 -0800461 rtc_writel(0, OMAP_RTC_KICK0_REG);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800462 pm_runtime_put_sync(&pdev->dev);
463 pm_runtime_disable(&pdev->dev);
David Brownelldb68b182006-12-06 20:38:36 -0800464 return -EIO;
465}
466
David Brownell71fc8222008-07-23 21:30:38 -0700467static int __exit omap_rtc_remove(struct platform_device *pdev)
David Brownelldb68b182006-12-06 20:38:36 -0800468{
Afzal Mohammedcab14582012-12-17 16:02:11 -0800469 const struct platform_device_id *id_entry =
470 platform_get_device_id(pdev);
David Brownelldb68b182006-12-06 20:38:36 -0800471
472 device_init_wakeup(&pdev->dev, 0);
473
474 /* leave rtc running, but disable irqs */
475 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
476
Sekhar Nori337b6002014-06-06 14:36:04 -0700477 if (id_entry->driver_data & OMAP_RTC_HAS_KICKER)
Afzal Mohammedcab14582012-12-17 16:02:11 -0800478 rtc_writel(0, OMAP_RTC_KICK0_REG);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800479
480 /* Disable the clock/module */
481 pm_runtime_put_sync(&pdev->dev);
482 pm_runtime_disable(&pdev->dev);
483
David Brownelldb68b182006-12-06 20:38:36 -0800484 return 0;
485}
486
Jingoo Han04ebc352013-04-29 16:21:01 -0700487#ifdef CONFIG_PM_SLEEP
David Brownelldb68b182006-12-06 20:38:36 -0800488static u8 irqstat;
489
Jingoo Han04ebc352013-04-29 16:21:01 -0700490static int omap_rtc_suspend(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800491{
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700492 u8 irqwake_stat;
493 struct platform_device *pdev = to_platform_device(dev);
494 const struct platform_device_id *id_entry =
495 platform_get_device_id(pdev);
496
David Brownelldb68b182006-12-06 20:38:36 -0800497 irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG);
498
499 /* FIXME the RTC alarm is not currently acting as a wakeup event
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700500 * source on some platforms, and in fact this enable() call is just
501 * saving a flag that's never used...
David Brownelldb68b182006-12-06 20:38:36 -0800502 */
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700503 if (device_may_wakeup(dev)) {
David Brownelldb68b182006-12-06 20:38:36 -0800504 enable_irq_wake(omap_rtc_alarm);
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700505
506 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN) {
507 irqwake_stat = rtc_read(OMAP_RTC_IRQWAKEEN);
508 irqwake_stat |= OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
509 rtc_write(irqwake_stat, OMAP_RTC_IRQWAKEEN);
510 }
511 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800512 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700513 }
David Brownelldb68b182006-12-06 20:38:36 -0800514
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800515 /* Disable the clock/module */
Jingoo Han04ebc352013-04-29 16:21:01 -0700516 pm_runtime_put_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800517
David Brownelldb68b182006-12-06 20:38:36 -0800518 return 0;
519}
520
Jingoo Han04ebc352013-04-29 16:21:01 -0700521static int omap_rtc_resume(struct device *dev)
David Brownelldb68b182006-12-06 20:38:36 -0800522{
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700523 u8 irqwake_stat;
524 struct platform_device *pdev = to_platform_device(dev);
525 const struct platform_device_id *id_entry =
526 platform_get_device_id(pdev);
527
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800528 /* Enable the clock/module so that we can access the registers */
Jingoo Han04ebc352013-04-29 16:21:01 -0700529 pm_runtime_get_sync(dev);
Vaibhav Hiremathfc9bd902012-12-17 16:02:18 -0800530
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700531 if (device_may_wakeup(dev)) {
David Brownelldb68b182006-12-06 20:38:36 -0800532 disable_irq_wake(omap_rtc_alarm);
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700533
534 if (id_entry->driver_data & OMAP_RTC_HAS_IRQWAKEEN) {
535 irqwake_stat = rtc_read(OMAP_RTC_IRQWAKEEN);
536 irqwake_stat &= ~OMAP_RTC_IRQWAKEEN_ALARM_WAKEEN;
537 rtc_write(irqwake_stat, OMAP_RTC_IRQWAKEEN);
538 }
539 } else {
David Brownelldb68b182006-12-06 20:38:36 -0800540 rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG);
Hebbar Gururaja8af750e2013-09-11 14:24:18 -0700541 }
David Brownelldb68b182006-12-06 20:38:36 -0800542 return 0;
543}
David Brownelldb68b182006-12-06 20:38:36 -0800544#endif
545
Jingoo Han04ebc352013-04-29 16:21:01 -0700546static SIMPLE_DEV_PM_OPS(omap_rtc_pm_ops, omap_rtc_suspend, omap_rtc_resume);
547
David Brownelldb68b182006-12-06 20:38:36 -0800548static void omap_rtc_shutdown(struct platform_device *pdev)
549{
550 rtc_write(0, OMAP_RTC_INTERRUPTS_REG);
551}
552
Kay Sieversad28a072008-04-10 21:29:25 -0700553MODULE_ALIAS("platform:omap_rtc");
David Brownelldb68b182006-12-06 20:38:36 -0800554static struct platform_driver omap_rtc_driver = {
David Brownell71fc8222008-07-23 21:30:38 -0700555 .remove = __exit_p(omap_rtc_remove),
David Brownelldb68b182006-12-06 20:38:36 -0800556 .shutdown = omap_rtc_shutdown,
557 .driver = {
Afzal Mohammedcab14582012-12-17 16:02:11 -0800558 .name = DRIVER_NAME,
David Brownelldb68b182006-12-06 20:38:36 -0800559 .owner = THIS_MODULE,
Jingoo Han04ebc352013-04-29 16:21:01 -0700560 .pm = &omap_rtc_pm_ops,
Sachin Kamat616b7342013-11-12 15:10:55 -0800561 .of_match_table = omap_rtc_of_match,
David Brownelldb68b182006-12-06 20:38:36 -0800562 },
Afzal Mohammedcab14582012-12-17 16:02:11 -0800563 .id_table = omap_rtc_devtype,
David Brownelldb68b182006-12-06 20:38:36 -0800564};
565
Jingoo Han09c5a362013-04-29 16:18:46 -0700566module_platform_driver_probe(omap_rtc_driver, omap_rtc_probe);
David Brownelldb68b182006-12-06 20:38:36 -0800567
568MODULE_AUTHOR("George G. Davis (and others)");
569MODULE_LICENSE("GPL");