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