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