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