Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 12 | #include <linux/of.h> |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/rtc.h> |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 17 | #include <linux/pm.h> |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 18 | #include <linux/regmap.h> |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 22 | /* RTC Register offsets from RTC CTRL REG */ |
| 23 | #define PM8XXX_ALARM_CTRL_OFFSET 0x01 |
| 24 | #define PM8XXX_RTC_WRITE_OFFSET 0x02 |
| 25 | #define PM8XXX_RTC_READ_OFFSET 0x06 |
| 26 | #define PM8XXX_ALARM_RW_OFFSET 0x0A |
| 27 | |
| 28 | /* RTC_CTRL register bit fields */ |
| 29 | #define PM8xxx_RTC_ENABLE BIT(7) |
| 30 | #define PM8xxx_RTC_ALARM_ENABLE BIT(1) |
| 31 | #define PM8xxx_RTC_ALARM_CLEAR BIT(0) |
| 32 | |
| 33 | #define NUM_8_BIT_RTC_REGS 0x4 |
| 34 | |
| 35 | /** |
| 36 | * struct pm8xxx_rtc - rtc driver internal structure |
| 37 | * @rtc: rtc device for this driver. |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 38 | * @regmap: regmap used to access RTC registers |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 39 | * @allow_set_time: indicates whether writing to the RTC is allowed |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 40 | * @rtc_alarm_irq: rtc alarm irq number. |
| 41 | * @rtc_base: address of rtc control register. |
| 42 | * @rtc_read_base: base address of read registers. |
| 43 | * @rtc_write_base: base address of write registers. |
| 44 | * @alarm_rw_base: base address of alarm registers. |
| 45 | * @ctrl_reg: rtc control register. |
| 46 | * @rtc_dev: device structure. |
| 47 | * @ctrl_reg_lock: spinlock protecting access to ctrl_reg. |
| 48 | */ |
| 49 | struct pm8xxx_rtc { |
| 50 | struct rtc_device *rtc; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 51 | struct regmap *regmap; |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 52 | bool allow_set_time; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 53 | int rtc_alarm_irq; |
| 54 | int rtc_base; |
| 55 | int rtc_read_base; |
| 56 | int rtc_write_base; |
| 57 | int alarm_rw_base; |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 58 | u8 ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 59 | struct device *rtc_dev; |
| 60 | spinlock_t ctrl_reg_lock; |
| 61 | }; |
| 62 | |
| 63 | /* |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 64 | * Steps to write the RTC registers. |
| 65 | * 1. Disable alarm if enabled. |
| 66 | * 2. Write 0x00 to LSB. |
| 67 | * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0]. |
| 68 | * 4. Enable alarm if disabled in step 1. |
| 69 | */ |
| 70 | static int pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 71 | { |
| 72 | int rc, i; |
| 73 | unsigned long secs, irq_flags; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 74 | u8 value[NUM_8_BIT_RTC_REGS], alarm_enabled = 0, ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 75 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 76 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 77 | if (!rtc_dd->allow_set_time) |
| 78 | return -EACCES; |
| 79 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 80 | rtc_tm_to_time(tm, &secs); |
| 81 | |
| 82 | for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) { |
| 83 | value[i] = secs & 0xFF; |
| 84 | secs >>= 8; |
| 85 | } |
| 86 | |
| 87 | dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs); |
| 88 | |
| 89 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 90 | ctrl_reg = rtc_dd->ctrl_reg; |
| 91 | |
| 92 | if (ctrl_reg & PM8xxx_RTC_ALARM_ENABLE) { |
| 93 | alarm_enabled = 1; |
| 94 | ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 95 | rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); |
| 96 | if (rc) { |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 97 | dev_err(dev, "Write to RTC control register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 98 | goto rtc_rw_fail; |
| 99 | } |
| 100 | rtc_dd->ctrl_reg = ctrl_reg; |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 101 | } else { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 102 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 103 | } |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 104 | |
| 105 | /* Write 0 to Byte[0] */ |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 106 | rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_write_base, 0); |
| 107 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 108 | dev_err(dev, "Write to RTC write data register failed\n"); |
| 109 | goto rtc_rw_fail; |
| 110 | } |
| 111 | |
| 112 | /* Write Byte[1], Byte[2], Byte[3] */ |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 113 | rc = regmap_bulk_write(rtc_dd->regmap, rtc_dd->rtc_write_base + 1, |
| 114 | &value[1], sizeof(value) - 1); |
| 115 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 116 | dev_err(dev, "Write to RTC write data register failed\n"); |
| 117 | goto rtc_rw_fail; |
| 118 | } |
| 119 | |
| 120 | /* Write Byte[0] */ |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 121 | rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_write_base, value[0]); |
| 122 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 123 | dev_err(dev, "Write to RTC write data register failed\n"); |
| 124 | goto rtc_rw_fail; |
| 125 | } |
| 126 | |
| 127 | if (alarm_enabled) { |
| 128 | ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 129 | rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); |
| 130 | if (rc) { |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 131 | dev_err(dev, "Write to RTC control register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 132 | goto rtc_rw_fail; |
| 133 | } |
| 134 | rtc_dd->ctrl_reg = ctrl_reg; |
| 135 | } |
| 136 | |
| 137 | rtc_rw_fail: |
| 138 | if (alarm_enabled) |
| 139 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 140 | |
| 141 | return rc; |
| 142 | } |
| 143 | |
| 144 | static int pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 145 | { |
| 146 | int rc; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 147 | u8 value[NUM_8_BIT_RTC_REGS]; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 148 | unsigned long secs; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 149 | unsigned int reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 150 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 151 | |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 152 | rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->rtc_read_base, |
| 153 | value, sizeof(value)); |
| 154 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 155 | dev_err(dev, "RTC read data register failed\n"); |
| 156 | return rc; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Read the LSB again and check if there has been a carry over. |
| 161 | * If there is, redo the read operation. |
| 162 | */ |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 163 | rc = regmap_read(rtc_dd->regmap, rtc_dd->rtc_read_base, ®); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 164 | if (rc < 0) { |
| 165 | dev_err(dev, "RTC read data register failed\n"); |
| 166 | return rc; |
| 167 | } |
| 168 | |
| 169 | if (unlikely(reg < value[0])) { |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 170 | rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->rtc_read_base, |
| 171 | value, sizeof(value)); |
| 172 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 173 | dev_err(dev, "RTC read data register failed\n"); |
| 174 | return rc; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24); |
| 179 | |
| 180 | rtc_time_to_tm(secs, tm); |
| 181 | |
| 182 | rc = rtc_valid_tm(tm); |
| 183 | if (rc < 0) { |
| 184 | dev_err(dev, "Invalid time read from RTC\n"); |
| 185 | return rc; |
| 186 | } |
| 187 | |
| 188 | dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n", |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 189 | secs, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 190 | tm->tm_mday, tm->tm_mon, tm->tm_year); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static int pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 196 | { |
| 197 | int rc, i; |
| 198 | u8 value[NUM_8_BIT_RTC_REGS], ctrl_reg; |
| 199 | unsigned long secs, irq_flags; |
| 200 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 201 | |
| 202 | rtc_tm_to_time(&alarm->time, &secs); |
| 203 | |
| 204 | for (i = 0; i < NUM_8_BIT_RTC_REGS; i++) { |
| 205 | value[i] = secs & 0xFF; |
| 206 | secs >>= 8; |
| 207 | } |
| 208 | |
| 209 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 210 | |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 211 | rc = regmap_bulk_write(rtc_dd->regmap, rtc_dd->alarm_rw_base, value, |
| 212 | sizeof(value)); |
| 213 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 214 | dev_err(dev, "Write to RTC ALARM register failed\n"); |
| 215 | goto rtc_rw_fail; |
| 216 | } |
| 217 | |
| 218 | ctrl_reg = rtc_dd->ctrl_reg; |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 219 | |
| 220 | if (alarm->enabled) |
| 221 | ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE; |
| 222 | else |
| 223 | ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 224 | |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 225 | rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); |
| 226 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 227 | dev_err(dev, "Write to RTC control register failed\n"); |
| 228 | goto rtc_rw_fail; |
| 229 | } |
| 230 | |
| 231 | rtc_dd->ctrl_reg = ctrl_reg; |
| 232 | |
| 233 | dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 234 | alarm->time.tm_hour, alarm->time.tm_min, |
| 235 | alarm->time.tm_sec, alarm->time.tm_mday, |
| 236 | alarm->time.tm_mon, alarm->time.tm_year); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 237 | rtc_rw_fail: |
| 238 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 239 | return rc; |
| 240 | } |
| 241 | |
| 242 | static int pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 243 | { |
| 244 | int rc; |
| 245 | u8 value[NUM_8_BIT_RTC_REGS]; |
| 246 | unsigned long secs; |
| 247 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 248 | |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 249 | rc = regmap_bulk_read(rtc_dd->regmap, rtc_dd->alarm_rw_base, value, |
| 250 | sizeof(value)); |
| 251 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 252 | dev_err(dev, "RTC alarm time read failed\n"); |
| 253 | return rc; |
| 254 | } |
| 255 | |
| 256 | secs = value[0] | (value[1] << 8) | (value[2] << 16) | (value[3] << 24); |
| 257 | |
| 258 | rtc_time_to_tm(secs, &alarm->time); |
| 259 | |
| 260 | rc = rtc_valid_tm(&alarm->time); |
| 261 | if (rc < 0) { |
| 262 | dev_err(dev, "Invalid alarm time read from RTC\n"); |
| 263 | return rc; |
| 264 | } |
| 265 | |
| 266 | dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 267 | alarm->time.tm_hour, alarm->time.tm_min, |
| 268 | alarm->time.tm_sec, alarm->time.tm_mday, |
| 269 | alarm->time.tm_mon, alarm->time.tm_year); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static int pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) |
| 275 | { |
| 276 | int rc; |
| 277 | unsigned long irq_flags; |
| 278 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 279 | u8 ctrl_reg; |
| 280 | |
| 281 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 282 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 283 | ctrl_reg = rtc_dd->ctrl_reg; |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 284 | |
| 285 | if (enable) |
| 286 | ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE; |
| 287 | else |
| 288 | ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 289 | |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 290 | rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); |
| 291 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 292 | dev_err(dev, "Write to RTC control register failed\n"); |
| 293 | goto rtc_rw_fail; |
| 294 | } |
| 295 | |
| 296 | rtc_dd->ctrl_reg = ctrl_reg; |
| 297 | |
| 298 | rtc_rw_fail: |
| 299 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 300 | return rc; |
| 301 | } |
| 302 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 303 | static const struct rtc_class_ops pm8xxx_rtc_ops = { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 304 | .read_time = pm8xxx_rtc_read_time, |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 305 | .set_time = pm8xxx_rtc_set_time, |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 306 | .set_alarm = pm8xxx_rtc_set_alarm, |
| 307 | .read_alarm = pm8xxx_rtc_read_alarm, |
| 308 | .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable, |
| 309 | }; |
| 310 | |
| 311 | static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id) |
| 312 | { |
| 313 | struct pm8xxx_rtc *rtc_dd = dev_id; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 314 | unsigned int ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 315 | int rc; |
| 316 | unsigned long irq_flags; |
| 317 | |
| 318 | rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF); |
| 319 | |
| 320 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 321 | |
| 322 | /* Clear the alarm enable bit */ |
| 323 | ctrl_reg = rtc_dd->ctrl_reg; |
| 324 | ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; |
| 325 | |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 326 | rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); |
| 327 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 328 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 329 | dev_err(rtc_dd->rtc_dev, |
| 330 | "Write to RTC control register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 331 | goto rtc_alarm_handled; |
| 332 | } |
| 333 | |
| 334 | rtc_dd->ctrl_reg = ctrl_reg; |
| 335 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 336 | |
| 337 | /* Clear RTC alarm register */ |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 338 | rc = regmap_read(rtc_dd->regmap, |
| 339 | rtc_dd->rtc_base + PM8XXX_ALARM_CTRL_OFFSET, |
| 340 | &ctrl_reg); |
| 341 | if (rc) { |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 342 | dev_err(rtc_dd->rtc_dev, |
| 343 | "RTC Alarm control register read failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 344 | goto rtc_alarm_handled; |
| 345 | } |
| 346 | |
| 347 | ctrl_reg &= ~PM8xxx_RTC_ALARM_CLEAR; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 348 | rc = regmap_write(rtc_dd->regmap, |
| 349 | rtc_dd->rtc_base + PM8XXX_ALARM_CTRL_OFFSET, |
| 350 | ctrl_reg); |
| 351 | if (rc) |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 352 | dev_err(rtc_dd->rtc_dev, |
| 353 | "Write to RTC Alarm control register failed\n"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 354 | |
| 355 | rtc_alarm_handled: |
| 356 | return IRQ_HANDLED; |
| 357 | } |
| 358 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 359 | /* |
| 360 | * Hardcoded RTC bases until IORESOURCE_REG mapping is figured out |
| 361 | */ |
| 362 | static const struct of_device_id pm8xxx_id_table[] = { |
| 363 | { .compatible = "qcom,pm8921-rtc", .data = (void *) 0x11D }, |
| 364 | { .compatible = "qcom,pm8058-rtc", .data = (void *) 0x1E8 }, |
| 365 | { }, |
| 366 | }; |
| 367 | MODULE_DEVICE_TABLE(of, pm8xxx_id_table); |
| 368 | |
Greg Kroah-Hartman | 5a167f4 | 2012-12-21 13:09:38 -0800 | [diff] [blame] | 369 | static int pm8xxx_rtc_probe(struct platform_device *pdev) |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 370 | { |
| 371 | int rc; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 372 | unsigned int ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 373 | struct pm8xxx_rtc *rtc_dd; |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 374 | const struct of_device_id *match; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 375 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 376 | match = of_match_node(pm8xxx_id_table, pdev->dev.of_node); |
| 377 | if (!match) |
| 378 | return -ENXIO; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 379 | |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 380 | rtc_dd = devm_kzalloc(&pdev->dev, sizeof(*rtc_dd), GFP_KERNEL); |
Jingoo Han | 49ae425 | 2014-04-03 14:49:43 -0700 | [diff] [blame] | 381 | if (rtc_dd == NULL) |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 382 | return -ENOMEM; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 383 | |
| 384 | /* Initialise spinlock to protect RTC control register */ |
| 385 | spin_lock_init(&rtc_dd->ctrl_reg_lock); |
| 386 | |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 387 | rtc_dd->regmap = dev_get_regmap(pdev->dev.parent, NULL); |
| 388 | if (!rtc_dd->regmap) { |
| 389 | dev_err(&pdev->dev, "Parent regmap unavailable.\n"); |
| 390 | return -ENXIO; |
| 391 | } |
| 392 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 393 | rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0); |
| 394 | if (rtc_dd->rtc_alarm_irq < 0) { |
| 395 | dev_err(&pdev->dev, "Alarm IRQ resource absent!\n"); |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 396 | return -ENXIO; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 399 | rtc_dd->allow_set_time = of_property_read_bool(pdev->dev.of_node, |
| 400 | "allow-set-time"); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 401 | |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 402 | rtc_dd->rtc_base = (long) match->data; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 403 | |
| 404 | /* Setup RTC register addresses */ |
| 405 | rtc_dd->rtc_write_base = rtc_dd->rtc_base + PM8XXX_RTC_WRITE_OFFSET; |
| 406 | rtc_dd->rtc_read_base = rtc_dd->rtc_base + PM8XXX_RTC_READ_OFFSET; |
| 407 | rtc_dd->alarm_rw_base = rtc_dd->rtc_base + PM8XXX_ALARM_RW_OFFSET; |
| 408 | |
| 409 | rtc_dd->rtc_dev = &pdev->dev; |
| 410 | |
| 411 | /* Check if the RTC is on, else turn it on */ |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 412 | rc = regmap_read(rtc_dd->regmap, rtc_dd->rtc_base, &ctrl_reg); |
| 413 | if (rc) { |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 414 | dev_err(&pdev->dev, "RTC control register read failed!\n"); |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 415 | return rc; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) { |
| 419 | ctrl_reg |= PM8xxx_RTC_ENABLE; |
Josh Cartwright | 5d7dc4c | 2014-04-03 14:50:11 -0700 | [diff] [blame] | 420 | rc = regmap_write(rtc_dd->regmap, rtc_dd->rtc_base, ctrl_reg); |
| 421 | if (rc) { |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 422 | dev_err(&pdev->dev, |
| 423 | "Write to RTC control register failed\n"); |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 424 | return rc; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | rtc_dd->ctrl_reg = ctrl_reg; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 429 | |
| 430 | platform_set_drvdata(pdev, rtc_dd); |
| 431 | |
Josh Cartwright | fda9909 | 2014-04-03 14:50:14 -0700 | [diff] [blame] | 432 | device_init_wakeup(&pdev->dev, 1); |
| 433 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 434 | /* Register the RTC device */ |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 435 | rtc_dd->rtc = devm_rtc_device_register(&pdev->dev, "pm8xxx_rtc", |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 436 | &pm8xxx_rtc_ops, THIS_MODULE); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 437 | if (IS_ERR(rtc_dd->rtc)) { |
| 438 | dev_err(&pdev->dev, "%s: RTC registration failed (%ld)\n", |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 439 | __func__, PTR_ERR(rtc_dd->rtc)); |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 440 | return PTR_ERR(rtc_dd->rtc); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* Request the alarm IRQ */ |
Josh Cartwright | bffcbc0 | 2014-04-03 14:50:12 -0700 | [diff] [blame] | 444 | rc = devm_request_any_context_irq(&pdev->dev, rtc_dd->rtc_alarm_irq, |
| 445 | pm8xxx_alarm_trigger, |
| 446 | IRQF_TRIGGER_RISING, |
| 447 | "pm8xxx_rtc_alarm", rtc_dd); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 448 | if (rc < 0) { |
| 449 | dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc); |
Jingoo Han | c417299 | 2013-07-03 15:07:09 -0700 | [diff] [blame] | 450 | return rc; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 453 | dev_dbg(&pdev->dev, "Probe success !!\n"); |
| 454 | |
| 455 | return 0; |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 458 | #ifdef CONFIG_PM_SLEEP |
| 459 | static int pm8xxx_rtc_resume(struct device *dev) |
| 460 | { |
| 461 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 462 | |
| 463 | if (device_may_wakeup(dev)) |
| 464 | disable_irq_wake(rtc_dd->rtc_alarm_irq); |
| 465 | |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int pm8xxx_rtc_suspend(struct device *dev) |
| 470 | { |
| 471 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 472 | |
| 473 | if (device_may_wakeup(dev)) |
| 474 | enable_irq_wake(rtc_dd->rtc_alarm_irq); |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | #endif |
| 479 | |
Josh Cartwright | 5bed811 | 2014-04-03 14:50:10 -0700 | [diff] [blame] | 480 | static SIMPLE_DEV_PM_OPS(pm8xxx_rtc_pm_ops, |
| 481 | pm8xxx_rtc_suspend, |
| 482 | pm8xxx_rtc_resume); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 483 | |
| 484 | static struct platform_driver pm8xxx_rtc_driver = { |
| 485 | .probe = pm8xxx_rtc_probe, |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 486 | .driver = { |
Josh Cartwright | 5a41855 | 2014-04-03 14:50:13 -0700 | [diff] [blame] | 487 | .name = "rtc-pm8xxx", |
| 488 | .owner = THIS_MODULE, |
| 489 | .pm = &pm8xxx_rtc_pm_ops, |
| 490 | .of_match_table = pm8xxx_id_table, |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 491 | }, |
| 492 | }; |
| 493 | |
Axel Lin | 0c4eae6 | 2012-01-10 15:10:48 -0800 | [diff] [blame] | 494 | module_platform_driver(pm8xxx_rtc_driver); |
Anirudh Ghayal | 9a9a54a | 2011-07-25 17:13:33 -0700 | [diff] [blame] | 495 | |
| 496 | MODULE_ALIAS("platform:rtc-pm8xxx"); |
| 497 | MODULE_DESCRIPTION("PMIC8xxx RTC driver"); |
| 498 | MODULE_LICENSE("GPL v2"); |
| 499 | MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>"); |