Ashay Jaiswal | 36568b8 | 2013-05-06 16:54:44 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2012-13, The Linux Foundation. All rights reserved. |
Ashay Jaiswal | 4b8f795 | 2012-05-02 14:55:38 +0530 | [diff] [blame] | 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> |
| 16 | #include <linux/pm.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/idr.h> |
| 19 | #include <linux/of_device.h> |
| 20 | #include <linux/spmi.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/spmi.h> |
| 23 | |
| 24 | /* RTC/ALARM Register offsets */ |
| 25 | #define REG_OFFSET_ALARM_RW 0x40 |
| 26 | #define REG_OFFSET_ALARM_CTRL1 0x46 |
| 27 | #define REG_OFFSET_ALARM_CTRL2 0x48 |
| 28 | #define REG_OFFSET_RTC_WRITE 0x40 |
| 29 | #define REG_OFFSET_RTC_CTRL 0x46 |
| 30 | #define REG_OFFSET_RTC_READ 0x48 |
| 31 | #define REG_OFFSET_PERP_SUBTYPE 0x05 |
| 32 | |
| 33 | /* RTC_CTRL register bit fields */ |
| 34 | #define BIT_RTC_ENABLE BIT(7) |
| 35 | #define BIT_RTC_ALARM_ENABLE BIT(7) |
| 36 | #define BIT_RTC_ABORT_ENABLE BIT(0) |
| 37 | #define BIT_RTC_ALARM_CLEAR BIT(0) |
| 38 | |
| 39 | /* RTC/ALARM peripheral subtype values */ |
| 40 | #define RTC_PERPH_SUBTYPE 0x1 |
| 41 | #define ALARM_PERPH_SUBTYPE 0x3 |
| 42 | |
| 43 | #define NUM_8_BIT_RTC_REGS 0x4 |
| 44 | |
| 45 | #define TO_SECS(arr) (arr[0] | (arr[1] << 8) | (arr[2] << 16) | \ |
| 46 | (arr[3] << 24)) |
| 47 | |
| 48 | /* rtc driver internal structure */ |
| 49 | struct qpnp_rtc { |
| 50 | u8 rtc_ctrl_reg; |
| 51 | u8 alarm_ctrl_reg1; |
| 52 | u16 rtc_base; |
| 53 | u16 alarm_base; |
| 54 | u32 rtc_write_enable; |
| 55 | u32 rtc_alarm_powerup; |
| 56 | int rtc_alarm_irq; |
| 57 | struct device *rtc_dev; |
| 58 | struct rtc_device *rtc; |
| 59 | struct spmi_device *spmi; |
| 60 | spinlock_t alarm_ctrl_lock; |
| 61 | }; |
| 62 | |
| 63 | static int qpnp_read_wrapper(struct qpnp_rtc *rtc_dd, u8 *rtc_val, |
| 64 | u16 base, int count) |
| 65 | { |
| 66 | int rc; |
| 67 | struct spmi_device *spmi = rtc_dd->spmi; |
| 68 | |
| 69 | rc = spmi_ext_register_readl(spmi->ctrl, spmi->sid, base, rtc_val, |
| 70 | count); |
| 71 | if (rc) { |
| 72 | dev_err(rtc_dd->rtc_dev, "SPMI read failed\n"); |
| 73 | return rc; |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int qpnp_write_wrapper(struct qpnp_rtc *rtc_dd, u8 *rtc_val, |
| 79 | u16 base, int count) |
| 80 | { |
| 81 | int rc; |
| 82 | struct spmi_device *spmi = rtc_dd->spmi; |
| 83 | |
| 84 | rc = spmi_ext_register_writel(spmi->ctrl, spmi->sid, base, rtc_val, |
| 85 | count); |
| 86 | if (rc) { |
| 87 | dev_err(rtc_dd->rtc_dev, "SPMI write failed\n"); |
| 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int |
| 95 | qpnp_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 96 | { |
| 97 | int rc; |
| 98 | unsigned long secs, irq_flags; |
| 99 | u8 value[4], reg = 0, alarm_enabled = 0, ctrl_reg; |
| 100 | struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev); |
| 101 | |
| 102 | rtc_tm_to_time(tm, &secs); |
| 103 | |
| 104 | value[0] = secs & 0xFF; |
| 105 | value[1] = (secs >> 8) & 0xFF; |
| 106 | value[2] = (secs >> 16) & 0xFF; |
| 107 | value[3] = (secs >> 24) & 0xFF; |
| 108 | |
| 109 | dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs); |
| 110 | |
| 111 | spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 112 | ctrl_reg = rtc_dd->alarm_ctrl_reg1; |
| 113 | |
| 114 | if (ctrl_reg & BIT_RTC_ALARM_ENABLE) { |
| 115 | alarm_enabled = 1; |
| 116 | ctrl_reg &= ~BIT_RTC_ALARM_ENABLE; |
| 117 | rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg, |
| 118 | rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1); |
| 119 | if (rc) { |
| 120 | dev_err(dev, "Write to ALARM ctrl reg failed\n"); |
| 121 | goto rtc_rw_fail; |
| 122 | } |
| 123 | } else |
| 124 | spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 125 | |
| 126 | /* |
| 127 | * 32 bit seconds value is coverted to four 8 bit values |
| 128 | * |<------ 32 bit time value in seconds ------>| |
| 129 | * <- 8 bit ->|<- 8 bit ->|<- 8 bit ->|<- 8 bit ->| |
| 130 | * ---------------------------------------------- |
| 131 | * | BYTE[3] | BYTE[2] | BYTE[1] | BYTE[0] | |
| 132 | * ---------------------------------------------- |
| 133 | * |
| 134 | * RTC has four 8 bit registers for writting time in seconds: |
| 135 | * WDATA[3], WDATA[2], WDATA[1], WDATA[0] |
| 136 | * |
| 137 | * Write to the RTC registers should be done in following order |
| 138 | * Clear WDATA[0] register |
| 139 | * |
| 140 | * Write BYTE[1], BYTE[2] and BYTE[3] of time to |
| 141 | * RTC WDATA[3], WDATA[2], WDATA[1] registers |
| 142 | * |
| 143 | * Write BYTE[0] of time to RTC WDATA[0] register |
| 144 | * |
| 145 | * Clearing BYTE[0] and writting in the end will prevent any |
| 146 | * unintentional overflow from WDATA[0] to higher bytes during the |
| 147 | * write operation |
| 148 | */ |
| 149 | |
| 150 | /* Clear WDATA[0] */ |
| 151 | reg = 0x0; |
| 152 | rc = qpnp_write_wrapper(rtc_dd, ®, |
| 153 | rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE, 1); |
| 154 | if (rc) { |
| 155 | dev_err(dev, "Write to RTC reg failed\n"); |
| 156 | goto rtc_rw_fail; |
| 157 | } |
| 158 | |
| 159 | /* Write to WDATA[3], WDATA[2] and WDATA[1] */ |
| 160 | rc = qpnp_write_wrapper(rtc_dd, &value[1], |
| 161 | rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE + 1, 3); |
| 162 | if (rc) { |
| 163 | dev_err(dev, "Write to RTC reg failed\n"); |
| 164 | goto rtc_rw_fail; |
| 165 | } |
| 166 | |
| 167 | /* Write to WDATA[0] */ |
| 168 | rc = qpnp_write_wrapper(rtc_dd, value, |
| 169 | rtc_dd->rtc_base + REG_OFFSET_RTC_WRITE, 1); |
| 170 | if (rc) { |
| 171 | dev_err(dev, "Write to RTC reg failed\n"); |
| 172 | goto rtc_rw_fail; |
| 173 | } |
| 174 | |
| 175 | if (alarm_enabled) { |
| 176 | ctrl_reg |= BIT_RTC_ALARM_ENABLE; |
| 177 | rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg, |
| 178 | rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1); |
| 179 | if (rc) { |
| 180 | dev_err(dev, "Write to ALARM ctrl reg failed\n"); |
| 181 | goto rtc_rw_fail; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | rtc_dd->alarm_ctrl_reg1 = ctrl_reg; |
| 186 | |
| 187 | rtc_rw_fail: |
| 188 | if (alarm_enabled) |
| 189 | spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 190 | |
| 191 | return rc; |
| 192 | } |
| 193 | |
| 194 | static int |
| 195 | qpnp_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 196 | { |
| 197 | int rc; |
| 198 | u8 value[4], reg; |
| 199 | unsigned long secs; |
| 200 | struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev); |
| 201 | |
| 202 | rc = qpnp_read_wrapper(rtc_dd, value, |
| 203 | rtc_dd->rtc_base + REG_OFFSET_RTC_READ, |
| 204 | NUM_8_BIT_RTC_REGS); |
| 205 | if (rc) { |
| 206 | dev_err(dev, "Read from RTC reg failed\n"); |
| 207 | return rc; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Read the LSB again and check if there has been a carry over |
| 212 | * If there is, redo the read operation |
| 213 | */ |
| 214 | rc = qpnp_read_wrapper(rtc_dd, ®, |
| 215 | rtc_dd->rtc_base + REG_OFFSET_RTC_READ, 1); |
| 216 | if (rc) { |
| 217 | dev_err(dev, "Read from RTC reg failed\n"); |
| 218 | return rc; |
| 219 | } |
| 220 | |
| 221 | if (reg < value[0]) { |
| 222 | rc = qpnp_read_wrapper(rtc_dd, value, |
| 223 | rtc_dd->rtc_base + REG_OFFSET_RTC_READ, |
| 224 | NUM_8_BIT_RTC_REGS); |
| 225 | if (rc) { |
| 226 | dev_err(dev, "Read from RTC reg failed\n"); |
| 227 | return rc; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | secs = TO_SECS(value); |
| 232 | |
| 233 | rtc_time_to_tm(secs, tm); |
| 234 | |
| 235 | rc = rtc_valid_tm(tm); |
| 236 | if (rc) { |
| 237 | dev_err(dev, "Invalid time read from RTC\n"); |
| 238 | return rc; |
| 239 | } |
| 240 | |
| 241 | dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n", |
| 242 | secs, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 243 | tm->tm_mday, tm->tm_mon, tm->tm_year); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int |
| 249 | qpnp_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 250 | { |
| 251 | int rc; |
| 252 | u8 value[4], ctrl_reg; |
| 253 | unsigned long secs, secs_rtc, irq_flags; |
| 254 | struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev); |
| 255 | struct rtc_time rtc_tm; |
| 256 | |
| 257 | rtc_tm_to_time(&alarm->time, &secs); |
| 258 | |
| 259 | /* |
| 260 | * Read the current RTC time and verify if the alarm time is in the |
| 261 | * past. If yes, return invalid |
| 262 | */ |
| 263 | rc = qpnp_rtc_read_time(dev, &rtc_tm); |
| 264 | if (rc) { |
| 265 | dev_err(dev, "Unable to read RTC time\n"); |
| 266 | return -EINVAL; |
| 267 | } |
| 268 | |
| 269 | rtc_tm_to_time(&rtc_tm, &secs_rtc); |
| 270 | if (secs < secs_rtc) { |
| 271 | dev_err(dev, "Trying to set alarm in the past\n"); |
| 272 | return -EINVAL; |
| 273 | } |
| 274 | |
| 275 | value[0] = secs & 0xFF; |
| 276 | value[1] = (secs >> 8) & 0xFF; |
| 277 | value[2] = (secs >> 16) & 0xFF; |
| 278 | value[3] = (secs >> 24) & 0xFF; |
| 279 | |
| 280 | spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 281 | |
| 282 | rc = qpnp_write_wrapper(rtc_dd, value, |
| 283 | rtc_dd->alarm_base + REG_OFFSET_ALARM_RW, |
| 284 | NUM_8_BIT_RTC_REGS); |
| 285 | if (rc) { |
| 286 | dev_err(dev, "Write to ALARM reg failed\n"); |
| 287 | goto rtc_rw_fail; |
| 288 | } |
| 289 | |
| 290 | ctrl_reg = (alarm->enabled) ? |
| 291 | (rtc_dd->alarm_ctrl_reg1 | BIT_RTC_ALARM_ENABLE) : |
| 292 | (rtc_dd->alarm_ctrl_reg1 & ~BIT_RTC_ALARM_ENABLE); |
| 293 | |
| 294 | rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg, |
| 295 | rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1); |
| 296 | if (rc) { |
| 297 | dev_err(dev, "Write to ALARM cntrol reg failed\n"); |
| 298 | goto rtc_rw_fail; |
| 299 | } |
| 300 | |
| 301 | rtc_dd->alarm_ctrl_reg1 = ctrl_reg; |
| 302 | |
| 303 | dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", |
| 304 | alarm->time.tm_hour, alarm->time.tm_min, |
| 305 | alarm->time.tm_sec, alarm->time.tm_mday, |
| 306 | alarm->time.tm_mon, alarm->time.tm_year); |
| 307 | rtc_rw_fail: |
| 308 | spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 309 | return rc; |
| 310 | } |
| 311 | |
| 312 | static int |
| 313 | qpnp_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 314 | { |
| 315 | int rc; |
| 316 | u8 value[4]; |
| 317 | unsigned long secs; |
| 318 | struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev); |
| 319 | |
| 320 | rc = qpnp_read_wrapper(rtc_dd, value, |
| 321 | rtc_dd->alarm_base + REG_OFFSET_ALARM_RW, |
| 322 | NUM_8_BIT_RTC_REGS); |
| 323 | if (rc) { |
| 324 | dev_err(dev, "Read from ALARM reg failed\n"); |
| 325 | return rc; |
| 326 | } |
| 327 | |
| 328 | secs = TO_SECS(value); |
| 329 | rtc_time_to_tm(secs, &alarm->time); |
| 330 | |
| 331 | rc = rtc_valid_tm(&alarm->time); |
| 332 | if (rc) { |
| 333 | dev_err(dev, "Invalid time read from RTC\n"); |
| 334 | return rc; |
| 335 | } |
| 336 | |
| 337 | dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", |
| 338 | alarm->time.tm_hour, alarm->time.tm_min, |
| 339 | alarm->time.tm_sec, alarm->time.tm_mday, |
| 340 | alarm->time.tm_mon, alarm->time.tm_year); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | static int |
| 347 | qpnp_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 348 | { |
| 349 | int rc; |
| 350 | unsigned long irq_flags; |
| 351 | struct qpnp_rtc *rtc_dd = dev_get_drvdata(dev); |
| 352 | u8 ctrl_reg; |
| 353 | |
| 354 | spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 355 | ctrl_reg = rtc_dd->alarm_ctrl_reg1; |
| 356 | ctrl_reg = enabled ? (ctrl_reg | BIT_RTC_ALARM_ENABLE) : |
| 357 | (ctrl_reg & ~BIT_RTC_ALARM_ENABLE); |
| 358 | |
| 359 | rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg, |
| 360 | rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1); |
| 361 | if (rc) { |
| 362 | dev_err(dev, "Write to ALARM control reg failed\n"); |
| 363 | goto rtc_rw_fail; |
| 364 | } |
| 365 | |
| 366 | rtc_dd->alarm_ctrl_reg1 = ctrl_reg; |
| 367 | |
| 368 | rtc_rw_fail: |
| 369 | spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 370 | return rc; |
| 371 | } |
| 372 | |
| 373 | static struct rtc_class_ops qpnp_rtc_ops = { |
| 374 | .read_time = qpnp_rtc_read_time, |
| 375 | .set_alarm = qpnp_rtc_set_alarm, |
| 376 | .read_alarm = qpnp_rtc_read_alarm, |
| 377 | .alarm_irq_enable = qpnp_rtc_alarm_irq_enable, |
| 378 | }; |
| 379 | |
| 380 | static irqreturn_t qpnp_alarm_trigger(int irq, void *dev_id) |
| 381 | { |
| 382 | struct qpnp_rtc *rtc_dd = dev_id; |
| 383 | u8 ctrl_reg; |
| 384 | int rc; |
| 385 | unsigned long irq_flags; |
| 386 | |
| 387 | rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF); |
| 388 | |
| 389 | spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 390 | |
| 391 | /* Clear the alarm enable bit */ |
| 392 | ctrl_reg = rtc_dd->alarm_ctrl_reg1; |
| 393 | ctrl_reg &= ~BIT_RTC_ALARM_ENABLE; |
| 394 | |
| 395 | rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg, |
| 396 | rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1); |
| 397 | if (rc) { |
| 398 | spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 399 | dev_err(rtc_dd->rtc_dev, |
| 400 | "Write to ALARM control reg failed\n"); |
| 401 | goto rtc_alarm_handled; |
| 402 | } |
| 403 | |
| 404 | rtc_dd->alarm_ctrl_reg1 = ctrl_reg; |
| 405 | spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 406 | |
| 407 | /* Set ALARM_CLR bit */ |
| 408 | ctrl_reg = 0x1; |
| 409 | rc = qpnp_write_wrapper(rtc_dd, &ctrl_reg, |
| 410 | rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL2, 1); |
| 411 | if (rc) |
| 412 | dev_err(rtc_dd->rtc_dev, |
| 413 | "Write to ALARM control reg failed\n"); |
| 414 | |
| 415 | rtc_alarm_handled: |
| 416 | return IRQ_HANDLED; |
| 417 | } |
| 418 | |
| 419 | static int __devinit qpnp_rtc_probe(struct spmi_device *spmi) |
| 420 | { |
| 421 | int rc; |
| 422 | u8 subtype; |
| 423 | struct qpnp_rtc *rtc_dd; |
| 424 | struct resource *resource; |
| 425 | struct spmi_resource *spmi_resource; |
| 426 | |
| 427 | rtc_dd = devm_kzalloc(&spmi->dev, sizeof(*rtc_dd), GFP_KERNEL); |
| 428 | if (rtc_dd == NULL) { |
| 429 | dev_err(&spmi->dev, "Unable to allocate memory!\n"); |
| 430 | return -ENOMEM; |
| 431 | } |
| 432 | |
| 433 | /* Get the rtc write property */ |
| 434 | rc = of_property_read_u32(spmi->dev.of_node, "qcom,qpnp-rtc-write", |
| 435 | &rtc_dd->rtc_write_enable); |
| 436 | if (rc && rc != -EINVAL) { |
| 437 | dev_err(&spmi->dev, |
| 438 | "Error reading rtc_write_enable property %d\n", rc); |
| 439 | return rc; |
| 440 | } |
| 441 | |
| 442 | rc = of_property_read_u32(spmi->dev.of_node, |
| 443 | "qcom,qpnp-rtc-alarm-pwrup", |
| 444 | &rtc_dd->rtc_alarm_powerup); |
| 445 | if (rc && rc != -EINVAL) { |
| 446 | dev_err(&spmi->dev, |
| 447 | "Error reading rtc_alarm_powerup property %d\n", rc); |
| 448 | return rc; |
| 449 | } |
| 450 | |
| 451 | /* Initialise spinlock to protect RTC control register */ |
| 452 | spin_lock_init(&rtc_dd->alarm_ctrl_lock); |
| 453 | |
| 454 | rtc_dd->rtc_dev = &(spmi->dev); |
| 455 | rtc_dd->spmi = spmi; |
| 456 | |
| 457 | /* Get RTC/ALARM resources */ |
| 458 | spmi_for_each_container_dev(spmi_resource, spmi) { |
| 459 | if (!spmi_resource) { |
| 460 | dev_err(&spmi->dev, |
| 461 | "%s: rtc_alarm: spmi resource absent!\n", |
| 462 | __func__); |
| 463 | rc = -ENXIO; |
| 464 | goto fail_rtc_enable; |
| 465 | } |
| 466 | |
| 467 | resource = spmi_get_resource(spmi, spmi_resource, |
| 468 | IORESOURCE_MEM, 0); |
| 469 | if (!(resource && resource->start)) { |
| 470 | dev_err(&spmi->dev, |
| 471 | "%s: node %s IO resource absent!\n", |
| 472 | __func__, spmi->dev.of_node->full_name); |
| 473 | rc = -ENXIO; |
| 474 | goto fail_rtc_enable; |
| 475 | } |
| 476 | |
| 477 | rc = qpnp_read_wrapper(rtc_dd, &subtype, |
| 478 | resource->start + REG_OFFSET_PERP_SUBTYPE, 1); |
| 479 | if (rc) { |
| 480 | dev_err(&spmi->dev, |
| 481 | "Peripheral subtype read failed\n"); |
| 482 | goto fail_rtc_enable; |
| 483 | } |
| 484 | |
| 485 | switch (subtype) { |
| 486 | case RTC_PERPH_SUBTYPE: |
| 487 | rtc_dd->rtc_base = resource->start; |
| 488 | break; |
| 489 | case ALARM_PERPH_SUBTYPE: |
| 490 | rtc_dd->alarm_base = resource->start; |
| 491 | rtc_dd->rtc_alarm_irq = |
| 492 | spmi_get_irq(spmi, spmi_resource, 0); |
| 493 | if (rtc_dd->rtc_alarm_irq < 0) { |
| 494 | dev_err(&spmi->dev, "ALARM IRQ absent\n"); |
| 495 | rc = -ENXIO; |
| 496 | goto fail_rtc_enable; |
| 497 | } |
| 498 | break; |
| 499 | default: |
| 500 | dev_err(&spmi->dev, "Invalid peripheral subtype\n"); |
| 501 | rc = -EINVAL; |
| 502 | goto fail_rtc_enable; |
| 503 | } |
| 504 | } |
| 505 | |
Ashay Jaiswal | 36568b8 | 2013-05-06 16:54:44 +0530 | [diff] [blame] | 506 | rc = qpnp_read_wrapper(rtc_dd, &rtc_dd->rtc_ctrl_reg, |
Ashay Jaiswal | 4b8f795 | 2012-05-02 14:55:38 +0530 | [diff] [blame] | 507 | rtc_dd->rtc_base + REG_OFFSET_RTC_CTRL, 1); |
| 508 | if (rc) { |
| 509 | dev_err(&spmi->dev, |
Ashay Jaiswal | 36568b8 | 2013-05-06 16:54:44 +0530 | [diff] [blame] | 510 | "Read from RTC control reg failed\n"); |
| 511 | goto fail_rtc_enable; |
| 512 | } |
| 513 | |
| 514 | if (!(rtc_dd->rtc_ctrl_reg & BIT_RTC_ENABLE)) { |
| 515 | dev_err(&spmi->dev, |
| 516 | "RTC h/w disabled, rtc not registered\n"); |
Ashay Jaiswal | 4b8f795 | 2012-05-02 14:55:38 +0530 | [diff] [blame] | 517 | goto fail_rtc_enable; |
| 518 | } |
| 519 | |
| 520 | /* Enable abort enable feature */ |
| 521 | rtc_dd->alarm_ctrl_reg1 = BIT_RTC_ABORT_ENABLE; |
| 522 | rc = qpnp_write_wrapper(rtc_dd, &rtc_dd->alarm_ctrl_reg1, |
| 523 | rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1); |
| 524 | if (rc) { |
| 525 | dev_err(&spmi->dev, "SPMI write failed!\n"); |
| 526 | goto fail_rtc_enable; |
| 527 | } |
| 528 | |
| 529 | if (rtc_dd->rtc_write_enable == true) |
| 530 | qpnp_rtc_ops.set_time = qpnp_rtc_set_time; |
| 531 | |
| 532 | dev_set_drvdata(&spmi->dev, rtc_dd); |
| 533 | |
| 534 | /* Register the RTC device */ |
| 535 | rtc_dd->rtc = rtc_device_register("qpnp_rtc", &spmi->dev, |
| 536 | &qpnp_rtc_ops, THIS_MODULE); |
| 537 | if (IS_ERR(rtc_dd->rtc)) { |
| 538 | dev_err(&spmi->dev, "%s: RTC registration failed (%ld)\n", |
| 539 | __func__, PTR_ERR(rtc_dd->rtc)); |
| 540 | rc = PTR_ERR(rtc_dd->rtc); |
| 541 | goto fail_rtc_enable; |
| 542 | } |
| 543 | |
| 544 | /* Request the alarm IRQ */ |
| 545 | rc = request_any_context_irq(rtc_dd->rtc_alarm_irq, |
| 546 | qpnp_alarm_trigger, IRQF_TRIGGER_RISING, |
| 547 | "qpnp_rtc_alarm", rtc_dd); |
| 548 | if (rc) { |
| 549 | dev_err(&spmi->dev, "Request IRQ failed (%d)\n", rc); |
| 550 | goto fail_req_irq; |
| 551 | } |
| 552 | |
| 553 | device_init_wakeup(&spmi->dev, 1); |
| 554 | enable_irq_wake(rtc_dd->rtc_alarm_irq); |
| 555 | |
| 556 | dev_dbg(&spmi->dev, "Probe success !!\n"); |
| 557 | |
| 558 | return 0; |
| 559 | |
| 560 | fail_req_irq: |
| 561 | rtc_device_unregister(rtc_dd->rtc); |
| 562 | fail_rtc_enable: |
| 563 | dev_set_drvdata(&spmi->dev, NULL); |
| 564 | |
| 565 | return rc; |
| 566 | } |
| 567 | |
| 568 | static int __devexit qpnp_rtc_remove(struct spmi_device *spmi) |
| 569 | { |
| 570 | struct qpnp_rtc *rtc_dd = dev_get_drvdata(&spmi->dev); |
| 571 | |
| 572 | device_init_wakeup(&spmi->dev, 0); |
| 573 | free_irq(rtc_dd->rtc_alarm_irq, rtc_dd); |
| 574 | rtc_device_unregister(rtc_dd->rtc); |
| 575 | dev_set_drvdata(&spmi->dev, NULL); |
| 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static void qpnp_rtc_shutdown(struct spmi_device *spmi) |
| 581 | { |
| 582 | u8 value[4] = {0}; |
| 583 | u8 reg; |
| 584 | int rc; |
| 585 | unsigned long irq_flags; |
| 586 | struct qpnp_rtc *rtc_dd = dev_get_drvdata(&spmi->dev); |
| 587 | bool rtc_alarm_powerup = rtc_dd->rtc_alarm_powerup; |
| 588 | |
| 589 | if (!rtc_alarm_powerup) { |
| 590 | spin_lock_irqsave(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 591 | dev_dbg(&spmi->dev, "Disabling alarm interrupts\n"); |
| 592 | |
| 593 | /* Disable RTC alarms */ |
| 594 | reg = rtc_dd->alarm_ctrl_reg1; |
| 595 | reg &= ~BIT_RTC_ALARM_ENABLE; |
| 596 | rc = qpnp_write_wrapper(rtc_dd, ®, |
| 597 | rtc_dd->alarm_base + REG_OFFSET_ALARM_CTRL1, 1); |
| 598 | if (rc) { |
| 599 | dev_err(rtc_dd->rtc_dev, "SPMI write failed\n"); |
| 600 | goto fail_alarm_disable; |
| 601 | } |
| 602 | |
| 603 | /* Clear Alarm register */ |
| 604 | rc = qpnp_write_wrapper(rtc_dd, value, |
| 605 | rtc_dd->alarm_base + REG_OFFSET_ALARM_RW, |
| 606 | NUM_8_BIT_RTC_REGS); |
| 607 | if (rc) |
| 608 | dev_err(rtc_dd->rtc_dev, "SPMI write failed\n"); |
| 609 | |
| 610 | fail_alarm_disable: |
| 611 | spin_unlock_irqrestore(&rtc_dd->alarm_ctrl_lock, irq_flags); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | static struct of_device_id spmi_match_table[] = { |
| 616 | { |
| 617 | .compatible = "qcom,qpnp-rtc", |
| 618 | }, |
| 619 | {} |
| 620 | }; |
| 621 | |
| 622 | static struct spmi_driver qpnp_rtc_driver = { |
| 623 | .probe = qpnp_rtc_probe, |
| 624 | .remove = __devexit_p(qpnp_rtc_remove), |
| 625 | .shutdown = qpnp_rtc_shutdown, |
| 626 | .driver = { |
| 627 | .name = "qcom,qpnp-rtc", |
| 628 | .owner = THIS_MODULE, |
| 629 | .of_match_table = spmi_match_table, |
| 630 | }, |
| 631 | }; |
| 632 | |
| 633 | static int __init qpnp_rtc_init(void) |
| 634 | { |
| 635 | return spmi_driver_register(&qpnp_rtc_driver); |
| 636 | } |
| 637 | module_init(qpnp_rtc_init); |
| 638 | |
| 639 | static void __exit qpnp_rtc_exit(void) |
| 640 | { |
| 641 | spmi_driver_unregister(&qpnp_rtc_driver); |
| 642 | } |
| 643 | module_exit(qpnp_rtc_exit); |
| 644 | |
| 645 | MODULE_DESCRIPTION("SMPI PMIC RTC driver"); |
| 646 | MODULE_LICENSE("GPL V2"); |