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