David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 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> |
Afzal Mohammed | 9e0344d | 2012-12-17 16:02:15 -0800 | [diff] [blame^] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_device.h> |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 25 | |
| 26 | #include <asm/io.h> |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | /* The OMAP1 RTC is a year/month/day/hours/minutes/seconds BCD clock |
| 30 | * with century-range alarm matching, driven by the 32kHz clock. |
| 31 | * |
| 32 | * The main user-visible ways it differs from PC RTCs are by omitting |
| 33 | * "don't care" alarm fields and sub-second periodic IRQs, and having |
| 34 | * an autoadjust mechanism to calibrate to the true oscillator rate. |
| 35 | * |
| 36 | * Board-specific wiring options include using split power mode with |
| 37 | * RTC_OFF_NOFF used as the reset signal (so the RTC won't be reset), |
| 38 | * and wiring RTC_WAKE_INT (so the RTC alarm can wake the system from |
Sekhar Nori | fa5b078 | 2010-10-27 15:33:05 -0700 | [diff] [blame] | 39 | * low power modes) for OMAP1 boards (OMAP-L138 has this built into |
| 40 | * the SoC). See the BOARD-SPECIFIC CUSTOMIZATION comment. |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 41 | */ |
| 42 | |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 43 | #define DRIVER_NAME "omap_rtc" |
| 44 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 45 | #define OMAP_RTC_BASE 0xfffb4800 |
| 46 | |
| 47 | /* RTC registers */ |
| 48 | #define OMAP_RTC_SECONDS_REG 0x00 |
| 49 | #define OMAP_RTC_MINUTES_REG 0x04 |
| 50 | #define OMAP_RTC_HOURS_REG 0x08 |
| 51 | #define OMAP_RTC_DAYS_REG 0x0C |
| 52 | #define OMAP_RTC_MONTHS_REG 0x10 |
| 53 | #define OMAP_RTC_YEARS_REG 0x14 |
| 54 | #define OMAP_RTC_WEEKS_REG 0x18 |
| 55 | |
| 56 | #define OMAP_RTC_ALARM_SECONDS_REG 0x20 |
| 57 | #define OMAP_RTC_ALARM_MINUTES_REG 0x24 |
| 58 | #define OMAP_RTC_ALARM_HOURS_REG 0x28 |
| 59 | #define OMAP_RTC_ALARM_DAYS_REG 0x2c |
| 60 | #define OMAP_RTC_ALARM_MONTHS_REG 0x30 |
| 61 | #define OMAP_RTC_ALARM_YEARS_REG 0x34 |
| 62 | |
| 63 | #define OMAP_RTC_CTRL_REG 0x40 |
| 64 | #define OMAP_RTC_STATUS_REG 0x44 |
| 65 | #define OMAP_RTC_INTERRUPTS_REG 0x48 |
| 66 | |
| 67 | #define OMAP_RTC_COMP_LSB_REG 0x4c |
| 68 | #define OMAP_RTC_COMP_MSB_REG 0x50 |
| 69 | #define OMAP_RTC_OSC_REG 0x54 |
| 70 | |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 71 | #define OMAP_RTC_KICK0_REG 0x6c |
| 72 | #define OMAP_RTC_KICK1_REG 0x70 |
| 73 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 74 | /* OMAP_RTC_CTRL_REG bit fields: */ |
| 75 | #define OMAP_RTC_CTRL_SPLIT (1<<7) |
| 76 | #define OMAP_RTC_CTRL_DISABLE (1<<6) |
| 77 | #define OMAP_RTC_CTRL_SET_32_COUNTER (1<<5) |
| 78 | #define OMAP_RTC_CTRL_TEST (1<<4) |
| 79 | #define OMAP_RTC_CTRL_MODE_12_24 (1<<3) |
| 80 | #define OMAP_RTC_CTRL_AUTO_COMP (1<<2) |
| 81 | #define OMAP_RTC_CTRL_ROUND_30S (1<<1) |
| 82 | #define OMAP_RTC_CTRL_STOP (1<<0) |
| 83 | |
| 84 | /* OMAP_RTC_STATUS_REG bit fields: */ |
| 85 | #define OMAP_RTC_STATUS_POWER_UP (1<<7) |
| 86 | #define OMAP_RTC_STATUS_ALARM (1<<6) |
| 87 | #define OMAP_RTC_STATUS_1D_EVENT (1<<5) |
| 88 | #define OMAP_RTC_STATUS_1H_EVENT (1<<4) |
| 89 | #define OMAP_RTC_STATUS_1M_EVENT (1<<3) |
| 90 | #define OMAP_RTC_STATUS_1S_EVENT (1<<2) |
| 91 | #define OMAP_RTC_STATUS_RUN (1<<1) |
| 92 | #define OMAP_RTC_STATUS_BUSY (1<<0) |
| 93 | |
| 94 | /* OMAP_RTC_INTERRUPTS_REG bit fields: */ |
| 95 | #define OMAP_RTC_INTERRUPTS_IT_ALARM (1<<3) |
| 96 | #define OMAP_RTC_INTERRUPTS_IT_TIMER (1<<2) |
| 97 | |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 98 | /* OMAP_RTC_KICKER values */ |
| 99 | #define KICK0_VALUE 0x83e70b13 |
| 100 | #define KICK1_VALUE 0x95a4f1e0 |
| 101 | |
| 102 | #define OMAP_RTC_HAS_KICKER 0x1 |
| 103 | |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 104 | static void __iomem *rtc_base; |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 105 | |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 106 | #define rtc_read(addr) readb(rtc_base + (addr)) |
| 107 | #define rtc_write(val, addr) writeb(val, rtc_base + (addr)) |
| 108 | |
| 109 | #define rtc_writel(val, addr) writel(val, rtc_base + (addr)) |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 110 | |
| 111 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 112 | /* we rely on the rtc framework to handle locking (rtc->ops_lock), |
| 113 | * so the only other requirement is that register accesses which |
| 114 | * require BUSY to be clear are made with IRQs locally disabled |
| 115 | */ |
| 116 | static void rtc_wait_not_busy(void) |
| 117 | { |
| 118 | int count = 0; |
| 119 | u8 status; |
| 120 | |
| 121 | /* BUSY may stay active for 1/32768 second (~30 usec) */ |
| 122 | for (count = 0; count < 50; count++) { |
| 123 | status = rtc_read(OMAP_RTC_STATUS_REG); |
| 124 | if ((status & (u8)OMAP_RTC_STATUS_BUSY) == 0) |
| 125 | break; |
| 126 | udelay(1); |
| 127 | } |
| 128 | /* now we have ~15 usec to read/write various registers */ |
| 129 | } |
| 130 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 131 | static irqreturn_t rtc_irq(int irq, void *rtc) |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 132 | { |
| 133 | unsigned long events = 0; |
| 134 | u8 irq_data; |
| 135 | |
| 136 | irq_data = rtc_read(OMAP_RTC_STATUS_REG); |
| 137 | |
| 138 | /* alarm irq? */ |
| 139 | if (irq_data & OMAP_RTC_STATUS_ALARM) { |
| 140 | rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); |
| 141 | events |= RTC_IRQF | RTC_AF; |
| 142 | } |
| 143 | |
| 144 | /* 1/sec periodic/update irq? */ |
| 145 | if (irq_data & OMAP_RTC_STATUS_1S_EVENT) |
| 146 | events |= RTC_IRQF | RTC_UF; |
| 147 | |
David Brownell | ab6a2d7 | 2007-05-08 00:33:30 -0700 | [diff] [blame] | 148 | rtc_update_irq(rtc, 1, events); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 149 | |
| 150 | return IRQ_HANDLED; |
| 151 | } |
| 152 | |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 153 | static int omap_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 154 | { |
| 155 | u8 reg; |
| 156 | |
| 157 | local_irq_disable(); |
| 158 | rtc_wait_not_busy(); |
| 159 | reg = rtc_read(OMAP_RTC_INTERRUPTS_REG); |
| 160 | if (enabled) |
| 161 | reg |= OMAP_RTC_INTERRUPTS_IT_ALARM; |
| 162 | else |
| 163 | reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM; |
| 164 | rtc_wait_not_busy(); |
| 165 | rtc_write(reg, OMAP_RTC_INTERRUPTS_REG); |
| 166 | local_irq_enable(); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 171 | /* this hardware doesn't support "don't care" alarm fields */ |
| 172 | static int tm2bcd(struct rtc_time *tm) |
| 173 | { |
| 174 | if (rtc_valid_tm(tm) != 0) |
| 175 | return -EINVAL; |
| 176 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 177 | tm->tm_sec = bin2bcd(tm->tm_sec); |
| 178 | tm->tm_min = bin2bcd(tm->tm_min); |
| 179 | tm->tm_hour = bin2bcd(tm->tm_hour); |
| 180 | tm->tm_mday = bin2bcd(tm->tm_mday); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 181 | |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 182 | tm->tm_mon = bin2bcd(tm->tm_mon + 1); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 183 | |
| 184 | /* epoch == 1900 */ |
| 185 | if (tm->tm_year < 100 || tm->tm_year > 199) |
| 186 | return -EINVAL; |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 187 | tm->tm_year = bin2bcd(tm->tm_year - 100); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void bcd2tm(struct rtc_time *tm) |
| 193 | { |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 194 | tm->tm_sec = bcd2bin(tm->tm_sec); |
| 195 | tm->tm_min = bcd2bin(tm->tm_min); |
| 196 | tm->tm_hour = bcd2bin(tm->tm_hour); |
| 197 | tm->tm_mday = bcd2bin(tm->tm_mday); |
| 198 | tm->tm_mon = bcd2bin(tm->tm_mon) - 1; |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 199 | /* epoch == 1900 */ |
Adrian Bunk | fe20ba7 | 2008-10-18 20:28:41 -0700 | [diff] [blame] | 200 | tm->tm_year = bcd2bin(tm->tm_year) + 100; |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
| 204 | static int omap_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 205 | { |
| 206 | /* we don't report wday/yday/isdst ... */ |
| 207 | local_irq_disable(); |
| 208 | rtc_wait_not_busy(); |
| 209 | |
| 210 | tm->tm_sec = rtc_read(OMAP_RTC_SECONDS_REG); |
| 211 | tm->tm_min = rtc_read(OMAP_RTC_MINUTES_REG); |
| 212 | tm->tm_hour = rtc_read(OMAP_RTC_HOURS_REG); |
| 213 | tm->tm_mday = rtc_read(OMAP_RTC_DAYS_REG); |
| 214 | tm->tm_mon = rtc_read(OMAP_RTC_MONTHS_REG); |
| 215 | tm->tm_year = rtc_read(OMAP_RTC_YEARS_REG); |
| 216 | |
| 217 | local_irq_enable(); |
| 218 | |
| 219 | bcd2tm(tm); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int omap_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 224 | { |
| 225 | if (tm2bcd(tm) < 0) |
| 226 | return -EINVAL; |
| 227 | local_irq_disable(); |
| 228 | rtc_wait_not_busy(); |
| 229 | |
| 230 | rtc_write(tm->tm_year, OMAP_RTC_YEARS_REG); |
| 231 | rtc_write(tm->tm_mon, OMAP_RTC_MONTHS_REG); |
| 232 | rtc_write(tm->tm_mday, OMAP_RTC_DAYS_REG); |
| 233 | rtc_write(tm->tm_hour, OMAP_RTC_HOURS_REG); |
| 234 | rtc_write(tm->tm_min, OMAP_RTC_MINUTES_REG); |
| 235 | rtc_write(tm->tm_sec, OMAP_RTC_SECONDS_REG); |
| 236 | |
| 237 | local_irq_enable(); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | static int omap_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 243 | { |
| 244 | local_irq_disable(); |
| 245 | rtc_wait_not_busy(); |
| 246 | |
| 247 | alm->time.tm_sec = rtc_read(OMAP_RTC_ALARM_SECONDS_REG); |
| 248 | alm->time.tm_min = rtc_read(OMAP_RTC_ALARM_MINUTES_REG); |
| 249 | alm->time.tm_hour = rtc_read(OMAP_RTC_ALARM_HOURS_REG); |
| 250 | alm->time.tm_mday = rtc_read(OMAP_RTC_ALARM_DAYS_REG); |
| 251 | alm->time.tm_mon = rtc_read(OMAP_RTC_ALARM_MONTHS_REG); |
| 252 | alm->time.tm_year = rtc_read(OMAP_RTC_ALARM_YEARS_REG); |
| 253 | |
| 254 | local_irq_enable(); |
| 255 | |
| 256 | bcd2tm(&alm->time); |
David Brownell | a2db8df | 2006-12-13 00:35:08 -0800 | [diff] [blame] | 257 | alm->enabled = !!(rtc_read(OMAP_RTC_INTERRUPTS_REG) |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 258 | & OMAP_RTC_INTERRUPTS_IT_ALARM); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int omap_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 264 | { |
| 265 | u8 reg; |
| 266 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 267 | if (tm2bcd(&alm->time) < 0) |
| 268 | return -EINVAL; |
| 269 | |
| 270 | local_irq_disable(); |
| 271 | rtc_wait_not_busy(); |
| 272 | |
| 273 | rtc_write(alm->time.tm_year, OMAP_RTC_ALARM_YEARS_REG); |
| 274 | rtc_write(alm->time.tm_mon, OMAP_RTC_ALARM_MONTHS_REG); |
| 275 | rtc_write(alm->time.tm_mday, OMAP_RTC_ALARM_DAYS_REG); |
| 276 | rtc_write(alm->time.tm_hour, OMAP_RTC_ALARM_HOURS_REG); |
| 277 | rtc_write(alm->time.tm_min, OMAP_RTC_ALARM_MINUTES_REG); |
| 278 | rtc_write(alm->time.tm_sec, OMAP_RTC_ALARM_SECONDS_REG); |
| 279 | |
| 280 | reg = rtc_read(OMAP_RTC_INTERRUPTS_REG); |
| 281 | if (alm->enabled) |
| 282 | reg |= OMAP_RTC_INTERRUPTS_IT_ALARM; |
| 283 | else |
| 284 | reg &= ~OMAP_RTC_INTERRUPTS_IT_ALARM; |
| 285 | rtc_write(reg, OMAP_RTC_INTERRUPTS_REG); |
| 286 | |
| 287 | local_irq_enable(); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static struct rtc_class_ops omap_rtc_ops = { |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 293 | .read_time = omap_rtc_read_time, |
| 294 | .set_time = omap_rtc_set_time, |
| 295 | .read_alarm = omap_rtc_read_alarm, |
| 296 | .set_alarm = omap_rtc_set_alarm, |
John Stultz | 16380c1 | 2011-02-02 17:02:41 -0800 | [diff] [blame] | 297 | .alarm_irq_enable = omap_rtc_alarm_irq_enable, |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 298 | }; |
| 299 | |
| 300 | static int omap_rtc_alarm; |
| 301 | static int omap_rtc_timer; |
| 302 | |
Afzal Mohammed | 9e0344d | 2012-12-17 16:02:15 -0800 | [diff] [blame^] | 303 | #define OMAP_RTC_DATA_DA830_IDX 1 |
| 304 | |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 305 | static struct platform_device_id omap_rtc_devtype[] = { |
| 306 | { |
| 307 | .name = DRIVER_NAME, |
| 308 | }, { |
| 309 | .name = "da830-rtc", |
| 310 | .driver_data = OMAP_RTC_HAS_KICKER, |
| 311 | }, |
| 312 | {}, |
| 313 | }; |
| 314 | MODULE_DEVICE_TABLE(platform, omap_rtc_devtype); |
| 315 | |
Afzal Mohammed | 9e0344d | 2012-12-17 16:02:15 -0800 | [diff] [blame^] | 316 | static const struct of_device_id omap_rtc_of_match[] = { |
| 317 | { .compatible = "ti,da830-rtc", |
| 318 | .data = &omap_rtc_devtype[OMAP_RTC_DATA_DA830_IDX], |
| 319 | }, |
| 320 | {}, |
| 321 | }; |
| 322 | MODULE_DEVICE_TABLE(of, omap_rtc_of_match); |
| 323 | |
David Brownell | 71fc822 | 2008-07-23 21:30:38 -0700 | [diff] [blame] | 324 | static int __init omap_rtc_probe(struct platform_device *pdev) |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 325 | { |
| 326 | struct resource *res, *mem; |
| 327 | struct rtc_device *rtc; |
| 328 | u8 reg, new_ctrl; |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 329 | const struct platform_device_id *id_entry; |
Afzal Mohammed | 9e0344d | 2012-12-17 16:02:15 -0800 | [diff] [blame^] | 330 | const struct of_device_id *of_id; |
| 331 | |
| 332 | of_id = of_match_device(omap_rtc_of_match, &pdev->dev); |
| 333 | if (of_id) |
| 334 | pdev->id_entry = of_id->data; |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 335 | |
| 336 | omap_rtc_timer = platform_get_irq(pdev, 0); |
| 337 | if (omap_rtc_timer <= 0) { |
| 338 | pr_debug("%s: no update irq?\n", pdev->name); |
| 339 | return -ENOENT; |
| 340 | } |
| 341 | |
| 342 | omap_rtc_alarm = platform_get_irq(pdev, 1); |
| 343 | if (omap_rtc_alarm <= 0) { |
| 344 | pr_debug("%s: no alarm irq?\n", pdev->name); |
| 345 | return -ENOENT; |
| 346 | } |
| 347 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 348 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 349 | if (!res) { |
| 350 | pr_debug("%s: RTC resource data missing\n", pdev->name); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 351 | return -ENOENT; |
| 352 | } |
| 353 | |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 354 | mem = request_mem_region(res->start, resource_size(res), pdev->name); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 355 | if (!mem) { |
| 356 | pr_debug("%s: RTC registers at %08x are not free\n", |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 357 | pdev->name, res->start); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 358 | return -EBUSY; |
| 359 | } |
| 360 | |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 361 | rtc_base = ioremap(res->start, resource_size(res)); |
| 362 | if (!rtc_base) { |
| 363 | pr_debug("%s: RTC registers can't be mapped\n", pdev->name); |
| 364 | goto fail; |
| 365 | } |
| 366 | |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 367 | id_entry = platform_get_device_id(pdev); |
| 368 | if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER)) { |
| 369 | rtc_writel(KICK0_VALUE, OMAP_RTC_KICK0_REG); |
| 370 | rtc_writel(KICK1_VALUE, OMAP_RTC_KICK1_REG); |
| 371 | } |
| 372 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 373 | rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 374 | &omap_rtc_ops, THIS_MODULE); |
| 375 | if (IS_ERR(rtc)) { |
| 376 | pr_debug("%s: can't register RTC device, err %ld\n", |
| 377 | pdev->name, PTR_ERR(rtc)); |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 378 | goto fail0; |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 379 | } |
| 380 | platform_set_drvdata(pdev, rtc); |
David Brownell | 558a40f | 2007-05-16 22:11:14 -0700 | [diff] [blame] | 381 | dev_set_drvdata(&rtc->dev, mem); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 382 | |
| 383 | /* clear pending irqs, and set 1/second periodic, |
| 384 | * which we'll use instead of update irqs |
| 385 | */ |
| 386 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); |
| 387 | |
| 388 | /* clear old status */ |
| 389 | reg = rtc_read(OMAP_RTC_STATUS_REG); |
| 390 | if (reg & (u8) OMAP_RTC_STATUS_POWER_UP) { |
| 391 | pr_info("%s: RTC power up reset detected\n", |
| 392 | pdev->name); |
| 393 | rtc_write(OMAP_RTC_STATUS_POWER_UP, OMAP_RTC_STATUS_REG); |
| 394 | } |
| 395 | if (reg & (u8) OMAP_RTC_STATUS_ALARM) |
| 396 | rtc_write(OMAP_RTC_STATUS_ALARM, OMAP_RTC_STATUS_REG); |
| 397 | |
| 398 | /* handle periodic and alarm irqs */ |
Yong Zhang | 2f6e5f9 | 2012-03-23 15:02:34 -0700 | [diff] [blame] | 399 | if (request_irq(omap_rtc_timer, rtc_irq, 0, |
Kay Sievers | 744bcb1 | 2009-03-24 16:38:22 -0700 | [diff] [blame] | 400 | dev_name(&rtc->dev), rtc)) { |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 401 | pr_debug("%s: RTC timer interrupt IRQ%d already claimed\n", |
| 402 | pdev->name, omap_rtc_timer); |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 403 | goto fail1; |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 404 | } |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 405 | if ((omap_rtc_timer != omap_rtc_alarm) && |
Yong Zhang | 2f6e5f9 | 2012-03-23 15:02:34 -0700 | [diff] [blame] | 406 | (request_irq(omap_rtc_alarm, rtc_irq, 0, |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 407 | dev_name(&rtc->dev), rtc))) { |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 408 | pr_debug("%s: RTC alarm interrupt IRQ%d already claimed\n", |
| 409 | pdev->name, omap_rtc_alarm); |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 410 | goto fail2; |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /* On boards with split power, RTC_ON_NOFF won't reset the RTC */ |
| 414 | reg = rtc_read(OMAP_RTC_CTRL_REG); |
| 415 | if (reg & (u8) OMAP_RTC_CTRL_STOP) |
| 416 | pr_info("%s: already running\n", pdev->name); |
| 417 | |
| 418 | /* force to 24 hour mode */ |
Daniel Glöckner | 12b3e03 | 2011-08-03 16:21:02 -0700 | [diff] [blame] | 419 | new_ctrl = reg & (OMAP_RTC_CTRL_SPLIT|OMAP_RTC_CTRL_AUTO_COMP); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 420 | new_ctrl |= OMAP_RTC_CTRL_STOP; |
| 421 | |
| 422 | /* BOARD-SPECIFIC CUSTOMIZATION CAN GO HERE: |
| 423 | * |
Sekhar Nori | fa5b078 | 2010-10-27 15:33:05 -0700 | [diff] [blame] | 424 | * - Device wake-up capability setting should come through chip |
| 425 | * init logic. OMAP1 boards should initialize the "wakeup capable" |
| 426 | * flag in the platform device if the board is wired right for |
| 427 | * being woken up by RTC alarm. For OMAP-L138, this capability |
| 428 | * is built into the SoC by the "Deep Sleep" capability. |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 429 | * |
| 430 | * - Boards wired so RTC_ON_nOFF is used as the reset signal, |
| 431 | * rather than nPWRON_RESET, should forcibly enable split |
| 432 | * power mode. (Some chip errata report that RTC_CTRL_SPLIT |
| 433 | * is write-only, and always reads as zero...) |
| 434 | */ |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 435 | |
| 436 | if (new_ctrl & (u8) OMAP_RTC_CTRL_SPLIT) |
| 437 | pr_info("%s: split power mode\n", pdev->name); |
| 438 | |
| 439 | if (reg != new_ctrl) |
| 440 | rtc_write(new_ctrl, OMAP_RTC_CTRL_REG); |
| 441 | |
| 442 | return 0; |
| 443 | |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 444 | fail2: |
Axel Lin | 2dd93c4 | 2011-04-17 10:02:58 +0800 | [diff] [blame] | 445 | free_irq(omap_rtc_timer, rtc); |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 446 | fail1: |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 447 | rtc_device_unregister(rtc); |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 448 | fail0: |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 449 | if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER)) |
| 450 | rtc_writel(0, OMAP_RTC_KICK0_REG); |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 451 | iounmap(rtc_base); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 452 | fail: |
Axel Lin | 19412ce | 2011-01-12 17:00:05 -0800 | [diff] [blame] | 453 | release_mem_region(mem->start, resource_size(mem)); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 454 | return -EIO; |
| 455 | } |
| 456 | |
David Brownell | 71fc822 | 2008-07-23 21:30:38 -0700 | [diff] [blame] | 457 | static int __exit omap_rtc_remove(struct platform_device *pdev) |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 458 | { |
Joe Perches | a419aef | 2009-08-18 11:18:35 -0700 | [diff] [blame] | 459 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
Axel Lin | 19412ce | 2011-01-12 17:00:05 -0800 | [diff] [blame] | 460 | struct resource *mem = dev_get_drvdata(&rtc->dev); |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 461 | const struct platform_device_id *id_entry = |
| 462 | platform_get_device_id(pdev); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 463 | |
| 464 | device_init_wakeup(&pdev->dev, 0); |
| 465 | |
| 466 | /* leave rtc running, but disable irqs */ |
| 467 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); |
| 468 | |
| 469 | free_irq(omap_rtc_timer, rtc); |
Mark A. Greer | 8cfde8c | 2009-12-15 16:46:11 -0800 | [diff] [blame] | 470 | |
| 471 | if (omap_rtc_timer != omap_rtc_alarm) |
| 472 | free_irq(omap_rtc_alarm, rtc); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 473 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 474 | rtc_device_unregister(rtc); |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 475 | if (id_entry && (id_entry->driver_data & OMAP_RTC_HAS_KICKER)) |
| 476 | rtc_writel(0, OMAP_RTC_KICK0_REG); |
Axel Lin | 19412ce | 2011-01-12 17:00:05 -0800 | [diff] [blame] | 477 | iounmap(rtc_base); |
| 478 | release_mem_region(mem->start, resource_size(mem)); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | #ifdef CONFIG_PM |
| 483 | |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 484 | static u8 irqstat; |
| 485 | |
| 486 | static int omap_rtc_suspend(struct platform_device *pdev, pm_message_t state) |
| 487 | { |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 488 | irqstat = rtc_read(OMAP_RTC_INTERRUPTS_REG); |
| 489 | |
| 490 | /* FIXME the RTC alarm is not currently acting as a wakeup event |
| 491 | * source, and in fact this enable() call is just saving a flag |
| 492 | * that's never used... |
| 493 | */ |
| 494 | if (device_may_wakeup(&pdev->dev)) |
| 495 | enable_irq_wake(omap_rtc_alarm); |
| 496 | else |
| 497 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int omap_rtc_resume(struct platform_device *pdev) |
| 503 | { |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 504 | if (device_may_wakeup(&pdev->dev)) |
| 505 | disable_irq_wake(omap_rtc_alarm); |
| 506 | else |
| 507 | rtc_write(irqstat, OMAP_RTC_INTERRUPTS_REG); |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | #else |
| 512 | #define omap_rtc_suspend NULL |
| 513 | #define omap_rtc_resume NULL |
| 514 | #endif |
| 515 | |
| 516 | static void omap_rtc_shutdown(struct platform_device *pdev) |
| 517 | { |
| 518 | rtc_write(0, OMAP_RTC_INTERRUPTS_REG); |
| 519 | } |
| 520 | |
Kay Sievers | ad28a07 | 2008-04-10 21:29:25 -0700 | [diff] [blame] | 521 | MODULE_ALIAS("platform:omap_rtc"); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 522 | static struct platform_driver omap_rtc_driver = { |
David Brownell | 71fc822 | 2008-07-23 21:30:38 -0700 | [diff] [blame] | 523 | .remove = __exit_p(omap_rtc_remove), |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 524 | .suspend = omap_rtc_suspend, |
| 525 | .resume = omap_rtc_resume, |
| 526 | .shutdown = omap_rtc_shutdown, |
| 527 | .driver = { |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 528 | .name = DRIVER_NAME, |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 529 | .owner = THIS_MODULE, |
Afzal Mohammed | 9e0344d | 2012-12-17 16:02:15 -0800 | [diff] [blame^] | 530 | .of_match_table = of_match_ptr(omap_rtc_of_match), |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 531 | }, |
Afzal Mohammed | cab1458 | 2012-12-17 16:02:11 -0800 | [diff] [blame] | 532 | .id_table = omap_rtc_devtype, |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | static int __init rtc_init(void) |
| 536 | { |
David Brownell | 71fc822 | 2008-07-23 21:30:38 -0700 | [diff] [blame] | 537 | return platform_driver_probe(&omap_rtc_driver, omap_rtc_probe); |
David Brownell | db68b18 | 2006-12-06 20:38:36 -0800 | [diff] [blame] | 538 | } |
| 539 | module_init(rtc_init); |
| 540 | |
| 541 | static void __exit rtc_exit(void) |
| 542 | { |
| 543 | platform_driver_unregister(&omap_rtc_driver); |
| 544 | } |
| 545 | module_exit(rtc_exit); |
| 546 | |
| 547 | MODULE_AUTHOR("George G. Davis (and others)"); |
| 548 | MODULE_LICENSE("GPL"); |