Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -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> |
| 16 | #include <linux/pm.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include<linux/spinlock.h> |
| 19 | |
| 20 | #include <linux/mfd/pm8xxx/core.h> |
| 21 | #include <linux/mfd/pm8xxx/rtc.h> |
| 22 | |
| 23 | |
| 24 | /* RTC Register offsets from RTC CTRL REG */ |
| 25 | #define PM8XXX_ALARM_CTRL_OFFSET 0x01 |
| 26 | #define PM8XXX_RTC_WRITE_OFFSET 0x02 |
| 27 | #define PM8XXX_RTC_READ_OFFSET 0x06 |
| 28 | #define PM8XXX_ALARM_RW_OFFSET 0x0A |
| 29 | |
| 30 | /* RTC_CTRL register bit fields */ |
| 31 | #define PM8xxx_RTC_ENABLE BIT(7) |
| 32 | #define PM8xxx_RTC_ALARM_ENABLE BIT(1) |
Ashay Jaiswal | d66c9d5 | 2011-10-13 17:41:40 +0530 | [diff] [blame] | 33 | #define PM8xxx_RTC_ABORT_ENABLE BIT(0) |
| 34 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | #define PM8xxx_RTC_ALARM_CLEAR BIT(0) |
| 36 | |
| 37 | #define NUM_8_BIT_RTC_REGS 0x4 |
| 38 | |
| 39 | /** |
| 40 | * struct pm8xxx_rtc - rtc driver internal structure |
| 41 | * @rtc: rtc device for this driver |
| 42 | * @rtc_alarm_irq: rtc alarm irq number |
| 43 | */ |
| 44 | struct pm8xxx_rtc { |
| 45 | struct rtc_device *rtc; |
| 46 | int rtc_alarm_irq; |
| 47 | int rtc_base; |
| 48 | int rtc_read_base; |
| 49 | int rtc_write_base; |
| 50 | int alarm_rw_base; |
| 51 | u8 ctrl_reg; |
| 52 | struct device *rtc_dev; |
| 53 | spinlock_t ctrl_reg_lock; |
| 54 | }; |
| 55 | |
| 56 | /* |
| 57 | * The RTC registers need to be read/written one byte at a time. This is a |
| 58 | * hardware limitation. |
| 59 | */ |
| 60 | |
| 61 | static int pm8xxx_read_wrapper(struct pm8xxx_rtc *rtc_dd, u8 *rtc_val, |
| 62 | int base, int count) |
| 63 | { |
| 64 | int i, rc; |
| 65 | struct device *parent = rtc_dd->rtc_dev->parent; |
| 66 | |
| 67 | for (i = 0; i < count; i++) { |
| 68 | rc = pm8xxx_readb(parent, base + i, &rtc_val[i]); |
| 69 | if (rc < 0) { |
| 70 | dev_err(rtc_dd->rtc_dev, "PM8xxx read failed\n"); |
| 71 | return rc; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int pm8xxx_write_wrapper(struct pm8xxx_rtc *rtc_dd, u8 *rtc_val, |
| 79 | int base, int count) |
| 80 | { |
| 81 | int i, rc; |
| 82 | struct device *parent = rtc_dd->rtc_dev->parent; |
| 83 | |
| 84 | for (i = 0; i < count; i++) { |
| 85 | rc = pm8xxx_writeb(parent, base + i, rtc_val[i]); |
| 86 | if (rc < 0) { |
| 87 | dev_err(rtc_dd->rtc_dev, "PM8xxx write failed\n"); |
| 88 | return rc; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* |
| 97 | * Steps to write the RTC registers. |
| 98 | * 1. Disable alarm if enabled. |
| 99 | * 2. Write 0x00 to LSB. |
| 100 | * 3. Write Byte[1], Byte[2], Byte[3] then Byte[0]. |
| 101 | * 4. Enable alarm if disabled in step 1. |
| 102 | */ |
| 103 | static int |
| 104 | pm8xxx_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 105 | { |
| 106 | int rc; |
| 107 | unsigned long secs, irq_flags; |
| 108 | u8 value[4], reg = 0, alarm_enabled = 0, ctrl_reg; |
| 109 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 110 | |
| 111 | rtc_tm_to_time(tm, &secs); |
| 112 | |
| 113 | value[0] = secs & 0xFF; |
| 114 | value[1] = (secs >> 8) & 0xFF; |
| 115 | value[2] = (secs >> 16) & 0xFF; |
| 116 | value[3] = (secs >> 24) & 0xFF; |
| 117 | |
| 118 | dev_dbg(dev, "Seconds value to be written to RTC = %lu\n", secs); |
| 119 | |
| 120 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 121 | ctrl_reg = rtc_dd->ctrl_reg; |
| 122 | |
| 123 | if (ctrl_reg & PM8xxx_RTC_ALARM_ENABLE) { |
| 124 | alarm_enabled = 1; |
| 125 | ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; |
| 126 | rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, |
| 127 | 1); |
| 128 | if (rc < 0) { |
| 129 | dev_err(dev, "PM8xxx write failed\n"); |
| 130 | goto rtc_rw_fail; |
| 131 | } |
| 132 | } else |
| 133 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 134 | |
| 135 | /* Write Byte[1], Byte[2], Byte[3], Byte[0] */ |
| 136 | /* Write 0 to Byte[0] */ |
| 137 | reg = 0; |
| 138 | rc = pm8xxx_write_wrapper(rtc_dd, ®, rtc_dd->rtc_write_base, 1); |
| 139 | if (rc < 0) { |
| 140 | dev_err(dev, "PM8xxx write failed\n"); |
| 141 | goto rtc_rw_fail; |
| 142 | } |
| 143 | |
| 144 | /* Write Byte[1], Byte[2], Byte[3] */ |
| 145 | rc = pm8xxx_write_wrapper(rtc_dd, value + 1, |
| 146 | rtc_dd->rtc_write_base + 1, 3); |
| 147 | if (rc < 0) { |
| 148 | dev_err(dev, "Write to RTC registers failed\n"); |
| 149 | goto rtc_rw_fail; |
| 150 | } |
| 151 | |
| 152 | /* Write Byte[0] */ |
| 153 | rc = pm8xxx_write_wrapper(rtc_dd, value, rtc_dd->rtc_write_base, 1); |
| 154 | if (rc < 0) { |
| 155 | dev_err(dev, "Write to RTC register failed\n"); |
| 156 | goto rtc_rw_fail; |
| 157 | } |
| 158 | |
| 159 | if (alarm_enabled) { |
| 160 | ctrl_reg |= PM8xxx_RTC_ALARM_ENABLE; |
| 161 | rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, |
| 162 | 1); |
| 163 | if (rc < 0) { |
| 164 | dev_err(dev, "PM8xxx write failed\n"); |
| 165 | goto rtc_rw_fail; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | rtc_dd->ctrl_reg = ctrl_reg; |
| 170 | |
| 171 | rtc_rw_fail: |
| 172 | if (alarm_enabled) |
| 173 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 174 | |
| 175 | return rc; |
| 176 | } |
| 177 | |
| 178 | static int |
| 179 | pm8xxx_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 180 | { |
| 181 | int rc; |
| 182 | u8 value[4], reg; |
| 183 | unsigned long secs; |
| 184 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 185 | |
| 186 | rc = pm8xxx_read_wrapper(rtc_dd, value, rtc_dd->rtc_read_base, |
| 187 | NUM_8_BIT_RTC_REGS); |
| 188 | if (rc < 0) { |
| 189 | dev_err(dev, "RTC time read failed\n"); |
| 190 | return rc; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Read the LSB again and check if there has been a carry over. |
| 195 | * If there is, redo the read operation. |
| 196 | */ |
| 197 | rc = pm8xxx_read_wrapper(rtc_dd, ®, rtc_dd->rtc_read_base, 1); |
| 198 | if (rc < 0) { |
| 199 | dev_err(dev, "PM8xxx read failed\n"); |
| 200 | return rc; |
| 201 | } |
| 202 | |
| 203 | if (unlikely(reg < value[0])) { |
| 204 | rc = pm8xxx_read_wrapper(rtc_dd, value, |
| 205 | rtc_dd->rtc_read_base, NUM_8_BIT_RTC_REGS); |
| 206 | if (rc < 0) { |
| 207 | dev_err(dev, "RTC time read failed\n"); |
| 208 | return rc; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | secs = value[0] | (value[1] << 8) | (value[2] << 16) \ |
| 213 | | (value[3] << 24); |
| 214 | |
| 215 | rtc_time_to_tm(secs, tm); |
| 216 | |
| 217 | rc = rtc_valid_tm(tm); |
| 218 | if (rc < 0) { |
| 219 | dev_err(dev, "Invalid time read from PM8xxx\n"); |
| 220 | return rc; |
| 221 | } |
| 222 | |
| 223 | dev_dbg(dev, "secs = %lu, h:m:s == %d:%d:%d, d/m/y = %d/%d/%d\n", |
| 224 | secs, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 225 | tm->tm_mday, tm->tm_mon, tm->tm_year); |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | static int |
| 231 | pm8xxx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 232 | { |
| 233 | int rc; |
| 234 | u8 value[4], ctrl_reg; |
| 235 | unsigned long secs, secs_rtc, irq_flags; |
| 236 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 237 | struct rtc_time rtc_tm; |
| 238 | |
| 239 | rtc_tm_to_time(&alarm->time, &secs); |
| 240 | |
| 241 | /* |
| 242 | * Read the current RTC time and verify if the alarm time is in the |
| 243 | * past. If yes, return invalid. |
| 244 | */ |
| 245 | rc = pm8xxx_rtc_read_time(dev, &rtc_tm); |
| 246 | if (rc < 0) { |
| 247 | dev_err(dev, "Unamble to read RTC time\n"); |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
| 251 | rtc_tm_to_time(&rtc_tm, &secs_rtc); |
| 252 | if (secs < secs_rtc) { |
| 253 | dev_err(dev, "Trying to set alarm in the past\n"); |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | |
| 257 | value[0] = secs & 0xFF; |
| 258 | value[1] = (secs >> 8) & 0xFF; |
| 259 | value[2] = (secs >> 16) & 0xFF; |
| 260 | value[3] = (secs >> 24) & 0xFF; |
| 261 | |
| 262 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 263 | |
| 264 | rc = pm8xxx_write_wrapper(rtc_dd, value, rtc_dd->alarm_rw_base, |
| 265 | NUM_8_BIT_RTC_REGS); |
| 266 | if (rc < 0) { |
| 267 | dev_err(dev, "Write to RTC ALARM registers failed\n"); |
| 268 | goto rtc_rw_fail; |
| 269 | } |
| 270 | |
| 271 | ctrl_reg = rtc_dd->ctrl_reg; |
| 272 | ctrl_reg = (alarm->enabled) ? (ctrl_reg | PM8xxx_RTC_ALARM_ENABLE) : |
| 273 | (ctrl_reg & ~PM8xxx_RTC_ALARM_ENABLE); |
| 274 | |
| 275 | rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1); |
| 276 | if (rc < 0) { |
| 277 | dev_err(dev, "PM8xxx write failed\n"); |
| 278 | goto rtc_rw_fail; |
| 279 | } |
| 280 | |
| 281 | rtc_dd->ctrl_reg = ctrl_reg; |
| 282 | |
| 283 | dev_dbg(dev, "Alarm Set for h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", |
| 284 | alarm->time.tm_hour, alarm->time.tm_min, |
| 285 | alarm->time.tm_sec, alarm->time.tm_mday, |
| 286 | alarm->time.tm_mon, alarm->time.tm_year); |
| 287 | rtc_rw_fail: |
| 288 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 289 | return rc; |
| 290 | } |
| 291 | |
| 292 | static int |
| 293 | pm8xxx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alarm) |
| 294 | { |
| 295 | int rc; |
| 296 | u8 value[4]; |
| 297 | unsigned long secs; |
| 298 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 299 | |
| 300 | rc = pm8xxx_read_wrapper(rtc_dd, value, rtc_dd->alarm_rw_base, |
| 301 | NUM_8_BIT_RTC_REGS); |
| 302 | if (rc < 0) { |
| 303 | dev_err(dev, "RTC alarm time read failed\n"); |
| 304 | return rc; |
| 305 | } |
| 306 | |
| 307 | secs = value[0] | (value[1] << 8) | (value[2] << 16) | \ |
| 308 | (value[3] << 24); |
| 309 | |
| 310 | rtc_time_to_tm(secs, &alarm->time); |
| 311 | |
| 312 | rc = rtc_valid_tm(&alarm->time); |
| 313 | if (rc < 0) { |
| 314 | dev_err(dev, "Invalid time read from PM8xxx\n"); |
| 315 | return rc; |
| 316 | } |
| 317 | |
| 318 | dev_dbg(dev, "Alarm set for - h:r:s=%d:%d:%d, d/m/y=%d/%d/%d\n", |
| 319 | alarm->time.tm_hour, alarm->time.tm_min, |
| 320 | alarm->time.tm_sec, alarm->time.tm_mday, |
| 321 | alarm->time.tm_mon, alarm->time.tm_year); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | static int |
| 328 | pm8xxx_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) |
| 329 | { |
| 330 | int rc; |
| 331 | unsigned long irq_flags; |
| 332 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 333 | u8 ctrl_reg; |
| 334 | |
| 335 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 336 | ctrl_reg = rtc_dd->ctrl_reg; |
| 337 | ctrl_reg = (enabled) ? (ctrl_reg | PM8xxx_RTC_ALARM_ENABLE) : |
| 338 | (ctrl_reg & ~PM8xxx_RTC_ALARM_ENABLE); |
| 339 | |
| 340 | rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1); |
| 341 | if (rc < 0) { |
| 342 | dev_err(dev, "PM8xxx write failed\n"); |
| 343 | goto rtc_rw_fail; |
| 344 | } |
| 345 | |
| 346 | rtc_dd->ctrl_reg = ctrl_reg; |
| 347 | |
| 348 | rtc_rw_fail: |
| 349 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 350 | return rc; |
| 351 | } |
| 352 | |
| 353 | static struct rtc_class_ops pm8xxx_rtc_ops = { |
| 354 | .read_time = pm8xxx_rtc_read_time, |
| 355 | .set_alarm = pm8xxx_rtc_set_alarm, |
| 356 | .read_alarm = pm8xxx_rtc_read_alarm, |
| 357 | .alarm_irq_enable = pm8xxx_rtc_alarm_irq_enable, |
| 358 | }; |
| 359 | |
| 360 | static irqreturn_t pm8xxx_alarm_trigger(int irq, void *dev_id) |
| 361 | { |
| 362 | struct pm8xxx_rtc *rtc_dd = dev_id; |
| 363 | u8 ctrl_reg; |
| 364 | int rc; |
| 365 | unsigned long irq_flags; |
| 366 | |
| 367 | rtc_update_irq(rtc_dd->rtc, 1, RTC_IRQF | RTC_AF); |
| 368 | |
| 369 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 370 | |
| 371 | /* Clear the alarm enable bit */ |
| 372 | ctrl_reg = rtc_dd->ctrl_reg; |
| 373 | ctrl_reg &= ~PM8xxx_RTC_ALARM_ENABLE; |
| 374 | |
| 375 | rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1); |
| 376 | if (rc < 0) { |
| 377 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 378 | dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n"); |
| 379 | goto rtc_alarm_handled; |
| 380 | } |
| 381 | |
| 382 | rtc_dd->ctrl_reg = ctrl_reg; |
| 383 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 384 | |
| 385 | /* Clear RTC alarm register */ |
| 386 | rc = pm8xxx_read_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base + |
| 387 | PM8XXX_ALARM_CTRL_OFFSET, 1); |
| 388 | if (rc < 0) { |
| 389 | dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n"); |
| 390 | goto rtc_alarm_handled; |
| 391 | } |
| 392 | |
| 393 | ctrl_reg &= ~PM8xxx_RTC_ALARM_CLEAR; |
| 394 | rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base + |
| 395 | PM8XXX_ALARM_CTRL_OFFSET, 1); |
| 396 | if (rc < 0) |
| 397 | dev_err(rtc_dd->rtc_dev, "PM8xxx write failed!\n"); |
| 398 | |
| 399 | rtc_alarm_handled: |
| 400 | return IRQ_HANDLED; |
| 401 | } |
| 402 | |
| 403 | static int __devinit pm8xxx_rtc_probe(struct platform_device *pdev) |
| 404 | { |
| 405 | int rc; |
| 406 | u8 ctrl_reg; |
| 407 | bool rtc_write_enable = false; |
| 408 | struct pm8xxx_rtc *rtc_dd; |
| 409 | struct resource *rtc_resource; |
| 410 | const struct pm8xxx_rtc_platform_data *pdata = |
| 411 | pdev->dev.platform_data; |
| 412 | |
| 413 | if (pdata != NULL) |
| 414 | rtc_write_enable = pdata->rtc_write_enable; |
| 415 | |
| 416 | rtc_dd = kzalloc(sizeof(*rtc_dd), GFP_KERNEL); |
| 417 | if (rtc_dd == NULL) { |
| 418 | dev_err(&pdev->dev, "Unable to allocate memory!\n"); |
| 419 | return -ENOMEM; |
| 420 | } |
| 421 | |
| 422 | /* Initialise spinlock to protect RTC cntrol register */ |
| 423 | spin_lock_init(&rtc_dd->ctrl_reg_lock); |
| 424 | |
| 425 | rtc_dd->rtc_alarm_irq = platform_get_irq(pdev, 0); |
| 426 | if (rtc_dd->rtc_alarm_irq < 0) { |
| 427 | dev_err(&pdev->dev, "Alarm IRQ resource absent!\n"); |
| 428 | rc = -ENXIO; |
| 429 | goto fail_rtc_enable; |
| 430 | } |
| 431 | |
| 432 | rtc_resource = platform_get_resource_byname(pdev, IORESOURCE_IO, |
| 433 | "pmic_rtc_base"); |
| 434 | if (!(rtc_resource && rtc_resource->start)) { |
| 435 | dev_err(&pdev->dev, "RTC IO resource absent!\n"); |
| 436 | rc = -ENXIO; |
| 437 | goto fail_rtc_enable; |
| 438 | } |
| 439 | |
| 440 | rtc_dd->rtc_base = rtc_resource->start; |
| 441 | |
| 442 | /* Setup RTC register addresses */ |
| 443 | rtc_dd->rtc_write_base = rtc_dd->rtc_base + PM8XXX_RTC_WRITE_OFFSET; |
| 444 | rtc_dd->rtc_read_base = rtc_dd->rtc_base + PM8XXX_RTC_READ_OFFSET; |
| 445 | rtc_dd->alarm_rw_base = rtc_dd->rtc_base + PM8XXX_ALARM_RW_OFFSET; |
| 446 | |
| 447 | rtc_dd->rtc_dev = &(pdev->dev); |
| 448 | |
| 449 | /* Check if the RTC is on, else turn it on */ |
| 450 | rc = pm8xxx_read_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1); |
| 451 | if (rc < 0) { |
| 452 | dev_err(&pdev->dev, "PM8xxx read failed!\n"); |
| 453 | goto fail_rtc_enable; |
| 454 | } |
| 455 | |
| 456 | if (!(ctrl_reg & PM8xxx_RTC_ENABLE)) { |
| 457 | ctrl_reg |= PM8xxx_RTC_ENABLE; |
| 458 | rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, |
| 459 | 1); |
| 460 | if (rc < 0) { |
| 461 | dev_err(&pdev->dev, "PM8xxx write failed!\n"); |
| 462 | goto fail_rtc_enable; |
| 463 | } |
| 464 | } |
| 465 | |
Ashay Jaiswal | d66c9d5 | 2011-10-13 17:41:40 +0530 | [diff] [blame] | 466 | /* Enable abort enable feature */ |
| 467 | ctrl_reg |= PM8xxx_RTC_ABORT_ENABLE; |
| 468 | rc = pm8xxx_write_wrapper(rtc_dd, &ctrl_reg, rtc_dd->rtc_base, 1); |
| 469 | if (rc < 0) { |
| 470 | dev_err(&pdev->dev, "PM8xxx write failed!\n"); |
| 471 | goto fail_rtc_enable; |
| 472 | } |
| 473 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 474 | rtc_dd->ctrl_reg = ctrl_reg; |
| 475 | if (rtc_write_enable == true) |
| 476 | pm8xxx_rtc_ops.set_time = pm8xxx_rtc_set_time; |
| 477 | |
| 478 | platform_set_drvdata(pdev, rtc_dd); |
| 479 | |
| 480 | /* Register the RTC device */ |
| 481 | rtc_dd->rtc = rtc_device_register("pm8xxx_rtc", &pdev->dev, |
| 482 | &pm8xxx_rtc_ops, THIS_MODULE); |
| 483 | if (IS_ERR(rtc_dd->rtc)) { |
| 484 | dev_err(&pdev->dev, "%s: RTC registration failed (%ld)\n", |
| 485 | __func__, PTR_ERR(rtc_dd->rtc)); |
| 486 | rc = PTR_ERR(rtc_dd->rtc); |
| 487 | goto fail_rtc_enable; |
| 488 | } |
| 489 | |
| 490 | /* Request the alarm IRQ */ |
| 491 | rc = request_any_context_irq(rtc_dd->rtc_alarm_irq, |
| 492 | pm8xxx_alarm_trigger, IRQF_TRIGGER_RISING, |
| 493 | "pm8xxx_rtc_alarm", rtc_dd); |
| 494 | if (rc < 0) { |
| 495 | dev_err(&pdev->dev, "Request IRQ failed (%d)\n", rc); |
| 496 | goto fail_req_irq; |
| 497 | } |
| 498 | |
| 499 | device_init_wakeup(&pdev->dev, 1); |
| 500 | |
| 501 | dev_dbg(&pdev->dev, "Probe success !!\n"); |
| 502 | |
| 503 | return 0; |
| 504 | |
| 505 | fail_req_irq: |
| 506 | rtc_device_unregister(rtc_dd->rtc); |
| 507 | fail_rtc_enable: |
| 508 | platform_set_drvdata(pdev, NULL); |
| 509 | kfree(rtc_dd); |
| 510 | return rc; |
| 511 | } |
| 512 | |
| 513 | #ifdef CONFIG_PM |
| 514 | static int pm8xxx_rtc_resume(struct device *dev) |
| 515 | { |
| 516 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 517 | |
| 518 | if (device_may_wakeup(dev)) |
| 519 | disable_irq_wake(rtc_dd->rtc_alarm_irq); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | static int pm8xxx_rtc_suspend(struct device *dev) |
| 525 | { |
| 526 | struct pm8xxx_rtc *rtc_dd = dev_get_drvdata(dev); |
| 527 | |
| 528 | if (device_may_wakeup(dev)) |
| 529 | enable_irq_wake(rtc_dd->rtc_alarm_irq); |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static const struct dev_pm_ops pm8xxx_rtc_pm_ops = { |
| 535 | .suspend = pm8xxx_rtc_suspend, |
| 536 | .resume = pm8xxx_rtc_resume, |
| 537 | }; |
| 538 | #endif |
| 539 | static int __devexit pm8xxx_rtc_remove(struct platform_device *pdev) |
| 540 | { |
| 541 | struct pm8xxx_rtc *rtc_dd = platform_get_drvdata(pdev); |
| 542 | |
| 543 | device_init_wakeup(&pdev->dev, 0); |
| 544 | free_irq(rtc_dd->rtc_alarm_irq, rtc_dd); |
| 545 | rtc_device_unregister(rtc_dd->rtc); |
| 546 | platform_set_drvdata(pdev, NULL); |
| 547 | kfree(rtc_dd); |
| 548 | |
| 549 | return 0; |
| 550 | } |
| 551 | |
Ashay Jaiswal | d66c9d5 | 2011-10-13 17:41:40 +0530 | [diff] [blame] | 552 | static void pm8xxx_rtc_shutdown(struct platform_device *pdev) |
| 553 | { |
| 554 | u8 value[4] = {0, 0, 0, 0}; |
| 555 | u8 reg; |
| 556 | int rc; |
| 557 | unsigned long irq_flags; |
| 558 | bool rtc_alarm_powerup = false; |
| 559 | struct pm8xxx_rtc *rtc_dd = platform_get_drvdata(pdev); |
| 560 | struct pm8xxx_rtc_platform_data *pdata = pdev->dev.platform_data; |
| 561 | |
| 562 | if (pdata != NULL) |
| 563 | rtc_alarm_powerup = pdata->rtc_alarm_powerup; |
| 564 | |
| 565 | if (!rtc_alarm_powerup) { |
| 566 | |
| 567 | spin_lock_irqsave(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 568 | dev_dbg(&pdev->dev, "Disabling alarm interrupts\n"); |
| 569 | |
| 570 | /* Disable RTC alarms */ |
| 571 | reg = rtc_dd->ctrl_reg; |
| 572 | reg &= ~PM8xxx_RTC_ALARM_ENABLE; |
| 573 | rc = pm8xxx_write_wrapper(rtc_dd, ®, rtc_dd->rtc_base, 1); |
| 574 | if (rc < 0) { |
| 575 | dev_err(rtc_dd->rtc_dev, "PM8xxx write failed\n"); |
| 576 | goto fail_alarm_disable; |
| 577 | } |
| 578 | |
| 579 | /* Clear Alarm register */ |
| 580 | rc = pm8xxx_write_wrapper(rtc_dd, value, |
| 581 | rtc_dd->alarm_rw_base, NUM_8_BIT_RTC_REGS); |
| 582 | if (rc < 0) |
| 583 | dev_err(rtc_dd->rtc_dev, "PM8xxx write failed\n"); |
| 584 | |
| 585 | fail_alarm_disable: |
| 586 | spin_unlock_irqrestore(&rtc_dd->ctrl_reg_lock, irq_flags); |
| 587 | } |
| 588 | } |
| 589 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 590 | static struct platform_driver pm8xxx_rtc_driver = { |
| 591 | .probe = pm8xxx_rtc_probe, |
| 592 | .remove = __devexit_p(pm8xxx_rtc_remove), |
Ashay Jaiswal | d66c9d5 | 2011-10-13 17:41:40 +0530 | [diff] [blame] | 593 | .shutdown = pm8xxx_rtc_shutdown, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 594 | .driver = { |
| 595 | .name = PM8XXX_RTC_DEV_NAME, |
| 596 | .owner = THIS_MODULE, |
| 597 | #ifdef CONFIG_PM |
| 598 | .pm = &pm8xxx_rtc_pm_ops, |
| 599 | #endif |
| 600 | }, |
| 601 | }; |
| 602 | |
| 603 | static int __init pm8xxx_rtc_init(void) |
| 604 | { |
| 605 | return platform_driver_register(&pm8xxx_rtc_driver); |
| 606 | } |
| 607 | module_init(pm8xxx_rtc_init); |
| 608 | |
| 609 | static void __exit pm8xxx_rtc_exit(void) |
| 610 | { |
| 611 | platform_driver_unregister(&pm8xxx_rtc_driver); |
| 612 | } |
| 613 | module_exit(pm8xxx_rtc_exit); |
| 614 | |
| 615 | MODULE_ALIAS("platform:rtc-pm8xxx"); |
| 616 | MODULE_DESCRIPTION("PMIC8xxx RTC driver"); |
| 617 | MODULE_LICENSE("GPL v2"); |
| 618 | MODULE_AUTHOR("Anirudh Ghayal <aghayal@codeaurora.org>"); |