Chris Zhong | 3ca1e32 | 2014-10-13 15:52:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * RTC driver for Rockchip RK808 |
| 3 | * |
| 4 | * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd |
| 5 | * |
| 6 | * Author: Chris Zhong <zyw@rock-chips.com> |
| 7 | * Author: Zhang Qing <zhangqing@rock-chips.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms and conditions of the GNU General Public License, |
| 11 | * version 2, as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/rtc.h> |
| 22 | #include <linux/bcd.h> |
| 23 | #include <linux/mfd/rk808.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/i2c.h> |
| 26 | |
| 27 | /* RTC_CTRL_REG bitfields */ |
| 28 | #define BIT_RTC_CTRL_REG_STOP_RTC_M BIT(0) |
| 29 | |
| 30 | /* RK808 has a shadowed register for saving a "frozen" RTC time. |
| 31 | * When user setting "GET_TIME" to 1, the time will save in this shadowed |
| 32 | * register. If set "READSEL" to 1, user read rtc time register, actually |
| 33 | * get the time of that moment. If we need the real time, clr this bit. |
| 34 | */ |
| 35 | #define BIT_RTC_CTRL_REG_RTC_GET_TIME BIT(6) |
| 36 | #define BIT_RTC_CTRL_REG_RTC_READSEL_M BIT(7) |
| 37 | #define BIT_RTC_INTERRUPTS_REG_IT_ALARM_M BIT(3) |
| 38 | #define RTC_STATUS_MASK 0xFE |
| 39 | |
| 40 | #define SECONDS_REG_MSK 0x7F |
| 41 | #define MINUTES_REG_MAK 0x7F |
| 42 | #define HOURS_REG_MSK 0x3F |
| 43 | #define DAYS_REG_MSK 0x3F |
| 44 | #define MONTHS_REG_MSK 0x1F |
| 45 | #define YEARS_REG_MSK 0xFF |
| 46 | #define WEEKS_REG_MSK 0x7 |
| 47 | |
| 48 | /* REG_SECONDS_REG through REG_YEARS_REG is how many registers? */ |
| 49 | |
| 50 | #define NUM_TIME_REGS (RK808_WEEKS_REG - RK808_SECONDS_REG + 1) |
| 51 | #define NUM_ALARM_REGS (RK808_ALARM_YEARS_REG - RK808_ALARM_SECONDS_REG + 1) |
| 52 | |
| 53 | struct rk808_rtc { |
| 54 | struct rk808 *rk808; |
| 55 | struct rtc_device *rtc; |
| 56 | int irq; |
| 57 | }; |
| 58 | |
| 59 | /* Read current time and date in RTC */ |
| 60 | static int rk808_rtc_readtime(struct device *dev, struct rtc_time *tm) |
| 61 | { |
| 62 | struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); |
| 63 | struct rk808 *rk808 = rk808_rtc->rk808; |
| 64 | u8 rtc_data[NUM_TIME_REGS]; |
| 65 | int ret; |
| 66 | |
| 67 | /* Force an update of the shadowed registers right now */ |
| 68 | ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, |
| 69 | BIT_RTC_CTRL_REG_RTC_GET_TIME, |
Chris Zhong | c412c60 | 2015-02-13 14:40:54 -0800 | [diff] [blame] | 70 | BIT_RTC_CTRL_REG_RTC_GET_TIME); |
Chris Zhong | 3ca1e32 | 2014-10-13 15:52:42 -0700 | [diff] [blame] | 71 | if (ret) { |
| 72 | dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret); |
| 73 | return ret; |
| 74 | } |
| 75 | |
Chris Zhong | c412c60 | 2015-02-13 14:40:54 -0800 | [diff] [blame] | 76 | /* |
| 77 | * After we set the GET_TIME bit, the rtc time can't be read |
| 78 | * immediately. So we should wait up to 31.25 us, about one cycle of |
| 79 | * 32khz. If we clear the GET_TIME bit here, the time of i2c transfer |
| 80 | * certainly more than 31.25us: 16 * 2.5us at 400kHz bus frequency. |
| 81 | */ |
Chris Zhong | 3ca1e32 | 2014-10-13 15:52:42 -0700 | [diff] [blame] | 82 | ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, |
| 83 | BIT_RTC_CTRL_REG_RTC_GET_TIME, |
Chris Zhong | c412c60 | 2015-02-13 14:40:54 -0800 | [diff] [blame] | 84 | 0); |
Chris Zhong | 3ca1e32 | 2014-10-13 15:52:42 -0700 | [diff] [blame] | 85 | if (ret) { |
| 86 | dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret); |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | ret = regmap_bulk_read(rk808->regmap, RK808_SECONDS_REG, |
| 91 | rtc_data, NUM_TIME_REGS); |
| 92 | if (ret) { |
| 93 | dev_err(dev, "Failed to bulk read rtc_data: %d\n", ret); |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | tm->tm_sec = bcd2bin(rtc_data[0] & SECONDS_REG_MSK); |
| 98 | tm->tm_min = bcd2bin(rtc_data[1] & MINUTES_REG_MAK); |
| 99 | tm->tm_hour = bcd2bin(rtc_data[2] & HOURS_REG_MSK); |
| 100 | tm->tm_mday = bcd2bin(rtc_data[3] & DAYS_REG_MSK); |
| 101 | tm->tm_mon = (bcd2bin(rtc_data[4] & MONTHS_REG_MSK)) - 1; |
| 102 | tm->tm_year = (bcd2bin(rtc_data[5] & YEARS_REG_MSK)) + 100; |
| 103 | tm->tm_wday = bcd2bin(rtc_data[6] & WEEKS_REG_MSK); |
| 104 | dev_dbg(dev, "RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n", |
| 105 | 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, |
| 106 | tm->tm_wday, tm->tm_hour , tm->tm_min, tm->tm_sec); |
| 107 | |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | /* Set current time and date in RTC */ |
| 112 | static int rk808_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 113 | { |
| 114 | struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); |
| 115 | struct rk808 *rk808 = rk808_rtc->rk808; |
| 116 | u8 rtc_data[NUM_TIME_REGS]; |
| 117 | int ret; |
| 118 | |
| 119 | rtc_data[0] = bin2bcd(tm->tm_sec); |
| 120 | rtc_data[1] = bin2bcd(tm->tm_min); |
| 121 | rtc_data[2] = bin2bcd(tm->tm_hour); |
| 122 | rtc_data[3] = bin2bcd(tm->tm_mday); |
| 123 | rtc_data[4] = bin2bcd(tm->tm_mon + 1); |
| 124 | rtc_data[5] = bin2bcd(tm->tm_year - 100); |
| 125 | rtc_data[6] = bin2bcd(tm->tm_wday); |
| 126 | dev_dbg(dev, "set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n", |
| 127 | 1900 + tm->tm_year, tm->tm_mon + 1, tm->tm_mday, |
| 128 | tm->tm_wday, tm->tm_hour , tm->tm_min, tm->tm_sec); |
| 129 | |
| 130 | /* Stop RTC while updating the RTC registers */ |
| 131 | ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, |
| 132 | BIT_RTC_CTRL_REG_STOP_RTC_M, |
| 133 | BIT_RTC_CTRL_REG_STOP_RTC_M); |
| 134 | if (ret) { |
| 135 | dev_err(dev, "Failed to update RTC control: %d\n", ret); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | ret = regmap_bulk_write(rk808->regmap, RK808_SECONDS_REG, |
| 140 | rtc_data, NUM_TIME_REGS); |
| 141 | if (ret) { |
| 142 | dev_err(dev, "Failed to bull write rtc_data: %d\n", ret); |
| 143 | return ret; |
| 144 | } |
| 145 | /* Start RTC again */ |
| 146 | ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, |
| 147 | BIT_RTC_CTRL_REG_STOP_RTC_M, 0); |
| 148 | if (ret) { |
| 149 | dev_err(dev, "Failed to update RTC control: %d\n", ret); |
| 150 | return ret; |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* Read alarm time and date in RTC */ |
| 156 | static int rk808_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 157 | { |
| 158 | struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); |
| 159 | struct rk808 *rk808 = rk808_rtc->rk808; |
| 160 | u8 alrm_data[NUM_ALARM_REGS]; |
| 161 | uint32_t int_reg; |
| 162 | int ret; |
| 163 | |
| 164 | ret = regmap_bulk_read(rk808->regmap, RK808_ALARM_SECONDS_REG, |
| 165 | alrm_data, NUM_ALARM_REGS); |
| 166 | |
| 167 | alrm->time.tm_sec = bcd2bin(alrm_data[0] & SECONDS_REG_MSK); |
| 168 | alrm->time.tm_min = bcd2bin(alrm_data[1] & MINUTES_REG_MAK); |
| 169 | alrm->time.tm_hour = bcd2bin(alrm_data[2] & HOURS_REG_MSK); |
| 170 | alrm->time.tm_mday = bcd2bin(alrm_data[3] & DAYS_REG_MSK); |
| 171 | alrm->time.tm_mon = (bcd2bin(alrm_data[4] & MONTHS_REG_MSK)) - 1; |
| 172 | alrm->time.tm_year = (bcd2bin(alrm_data[5] & YEARS_REG_MSK)) + 100; |
| 173 | |
| 174 | ret = regmap_read(rk808->regmap, RK808_RTC_INT_REG, &int_reg); |
| 175 | if (ret) { |
| 176 | dev_err(dev, "Failed to read RTC INT REG: %d\n", ret); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | dev_dbg(dev, "alrm read RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n", |
| 181 | 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1, |
| 182 | alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour, |
| 183 | alrm->time.tm_min, alrm->time.tm_sec); |
| 184 | |
| 185 | alrm->enabled = (int_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) ? 1 : 0; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int rk808_rtc_stop_alarm(struct rk808_rtc *rk808_rtc) |
| 191 | { |
| 192 | struct rk808 *rk808 = rk808_rtc->rk808; |
| 193 | int ret; |
| 194 | |
| 195 | ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG, |
| 196 | BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 0); |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static int rk808_rtc_start_alarm(struct rk808_rtc *rk808_rtc) |
| 202 | { |
| 203 | struct rk808 *rk808 = rk808_rtc->rk808; |
| 204 | int ret; |
| 205 | |
| 206 | ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG, |
| 207 | BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, |
| 208 | BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static int rk808_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 214 | { |
| 215 | struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); |
| 216 | struct rk808 *rk808 = rk808_rtc->rk808; |
| 217 | u8 alrm_data[NUM_ALARM_REGS]; |
| 218 | int ret; |
| 219 | |
| 220 | ret = rk808_rtc_stop_alarm(rk808_rtc); |
| 221 | if (ret) { |
| 222 | dev_err(dev, "Failed to stop alarm: %d\n", ret); |
| 223 | return ret; |
| 224 | } |
| 225 | dev_dbg(dev, "alrm set RTC date/time %4d-%02d-%02d(%d) %02d:%02d:%02d\n", |
| 226 | 1900 + alrm->time.tm_year, alrm->time.tm_mon + 1, |
| 227 | alrm->time.tm_mday, alrm->time.tm_wday, alrm->time.tm_hour, |
| 228 | alrm->time.tm_min, alrm->time.tm_sec); |
| 229 | |
| 230 | alrm_data[0] = bin2bcd(alrm->time.tm_sec); |
| 231 | alrm_data[1] = bin2bcd(alrm->time.tm_min); |
| 232 | alrm_data[2] = bin2bcd(alrm->time.tm_hour); |
| 233 | alrm_data[3] = bin2bcd(alrm->time.tm_mday); |
| 234 | alrm_data[4] = bin2bcd(alrm->time.tm_mon + 1); |
| 235 | alrm_data[5] = bin2bcd(alrm->time.tm_year - 100); |
| 236 | |
| 237 | ret = regmap_bulk_write(rk808->regmap, RK808_ALARM_SECONDS_REG, |
| 238 | alrm_data, NUM_ALARM_REGS); |
| 239 | if (ret) { |
| 240 | dev_err(dev, "Failed to bulk write: %d\n", ret); |
| 241 | return ret; |
| 242 | } |
| 243 | if (alrm->enabled) { |
| 244 | ret = rk808_rtc_start_alarm(rk808_rtc); |
| 245 | if (ret) { |
| 246 | dev_err(dev, "Failed to start alarm: %d\n", ret); |
| 247 | return ret; |
| 248 | } |
| 249 | } |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int rk808_rtc_alarm_irq_enable(struct device *dev, |
| 254 | unsigned int enabled) |
| 255 | { |
| 256 | struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); |
| 257 | |
| 258 | if (enabled) |
| 259 | return rk808_rtc_start_alarm(rk808_rtc); |
| 260 | |
| 261 | return rk808_rtc_stop_alarm(rk808_rtc); |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * We will just handle setting the frequency and make use the framework for |
| 266 | * reading the periodic interupts. |
| 267 | * |
| 268 | * @freq: Current periodic IRQ freq: |
| 269 | * bit 0: every second |
| 270 | * bit 1: every minute |
| 271 | * bit 2: every hour |
| 272 | * bit 3: every day |
| 273 | */ |
| 274 | static irqreturn_t rk808_alarm_irq(int irq, void *data) |
| 275 | { |
| 276 | struct rk808_rtc *rk808_rtc = data; |
| 277 | struct rk808 *rk808 = rk808_rtc->rk808; |
| 278 | struct i2c_client *client = rk808->i2c; |
| 279 | int ret; |
| 280 | |
| 281 | ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG, |
| 282 | RTC_STATUS_MASK); |
| 283 | if (ret) { |
| 284 | dev_err(&client->dev, |
| 285 | "%s:Failed to update RTC status: %d\n", __func__, ret); |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | rtc_update_irq(rk808_rtc->rtc, 1, RTC_IRQF | RTC_AF); |
| 290 | dev_dbg(&client->dev, |
| 291 | "%s:irq=%d\n", __func__, irq); |
| 292 | return IRQ_HANDLED; |
| 293 | } |
| 294 | |
| 295 | static const struct rtc_class_ops rk808_rtc_ops = { |
| 296 | .read_time = rk808_rtc_readtime, |
| 297 | .set_time = rk808_rtc_set_time, |
| 298 | .read_alarm = rk808_rtc_readalarm, |
| 299 | .set_alarm = rk808_rtc_setalarm, |
| 300 | .alarm_irq_enable = rk808_rtc_alarm_irq_enable, |
| 301 | }; |
| 302 | |
| 303 | #ifdef CONFIG_PM_SLEEP |
| 304 | /* Turn off the alarm if it should not be a wake source. */ |
| 305 | static int rk808_rtc_suspend(struct device *dev) |
| 306 | { |
| 307 | struct platform_device *pdev = to_platform_device(dev); |
| 308 | struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev); |
| 309 | |
| 310 | if (device_may_wakeup(dev)) |
| 311 | enable_irq_wake(rk808_rtc->irq); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* Enable the alarm if it should be enabled (in case it was disabled to |
| 317 | * prevent use as a wake source). |
| 318 | */ |
| 319 | static int rk808_rtc_resume(struct device *dev) |
| 320 | { |
| 321 | struct platform_device *pdev = to_platform_device(dev); |
| 322 | struct rk808_rtc *rk808_rtc = dev_get_drvdata(&pdev->dev); |
| 323 | |
| 324 | if (device_may_wakeup(dev)) |
| 325 | disable_irq_wake(rk808_rtc->irq); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | #endif |
| 330 | |
| 331 | static SIMPLE_DEV_PM_OPS(rk808_rtc_pm_ops, |
| 332 | rk808_rtc_suspend, rk808_rtc_resume); |
| 333 | |
| 334 | static int rk808_rtc_probe(struct platform_device *pdev) |
| 335 | { |
| 336 | struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent); |
| 337 | struct rk808_rtc *rk808_rtc; |
| 338 | struct rtc_time tm; |
| 339 | int ret; |
| 340 | |
| 341 | rk808_rtc = devm_kzalloc(&pdev->dev, sizeof(*rk808_rtc), GFP_KERNEL); |
| 342 | if (rk808_rtc == NULL) |
| 343 | return -ENOMEM; |
| 344 | |
| 345 | platform_set_drvdata(pdev, rk808_rtc); |
| 346 | rk808_rtc->rk808 = rk808; |
| 347 | |
| 348 | /* start rtc running by default, and use shadowed timer. */ |
| 349 | ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, |
| 350 | BIT_RTC_CTRL_REG_STOP_RTC_M | |
| 351 | BIT_RTC_CTRL_REG_RTC_READSEL_M, |
| 352 | BIT_RTC_CTRL_REG_RTC_READSEL_M); |
| 353 | if (ret) { |
| 354 | dev_err(&pdev->dev, |
| 355 | "Failed to update RTC control: %d\n", ret); |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG, |
| 360 | RTC_STATUS_MASK); |
| 361 | if (ret) { |
| 362 | dev_err(&pdev->dev, |
| 363 | "Failed to write RTC status: %d\n", ret); |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | /* set init time */ |
| 368 | ret = rk808_rtc_readtime(&pdev->dev, &tm); |
| 369 | if (ret) { |
| 370 | dev_err(&pdev->dev, "Failed to read RTC time\n"); |
| 371 | return ret; |
| 372 | } |
| 373 | ret = rtc_valid_tm(&tm); |
| 374 | if (ret) |
| 375 | dev_warn(&pdev->dev, "invalid date/time\n"); |
| 376 | |
| 377 | device_init_wakeup(&pdev->dev, 1); |
| 378 | |
| 379 | rk808_rtc->rtc = devm_rtc_device_register(&pdev->dev, "rk808-rtc", |
| 380 | &rk808_rtc_ops, THIS_MODULE); |
| 381 | if (IS_ERR(rk808_rtc->rtc)) { |
| 382 | ret = PTR_ERR(rk808_rtc->rtc); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | rk808_rtc->irq = platform_get_irq(pdev, 0); |
| 387 | if (rk808_rtc->irq < 0) { |
| 388 | if (rk808_rtc->irq != -EPROBE_DEFER) |
| 389 | dev_err(&pdev->dev, "Wake up is not possible as irq = %d\n", |
| 390 | rk808_rtc->irq); |
| 391 | return rk808_rtc->irq; |
| 392 | } |
| 393 | |
| 394 | /* request alarm irq of rk808 */ |
| 395 | ret = devm_request_threaded_irq(&pdev->dev, rk808_rtc->irq, NULL, |
| 396 | rk808_alarm_irq, 0, |
| 397 | "RTC alarm", rk808_rtc); |
| 398 | if (ret) { |
| 399 | dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n", |
| 400 | rk808_rtc->irq, ret); |
| 401 | } |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | static struct platform_driver rk808_rtc_driver = { |
| 407 | .probe = rk808_rtc_probe, |
| 408 | .driver = { |
| 409 | .name = "rk808-rtc", |
| 410 | .pm = &rk808_rtc_pm_ops, |
| 411 | }, |
| 412 | }; |
| 413 | |
| 414 | module_platform_driver(rk808_rtc_driver); |
| 415 | |
| 416 | MODULE_DESCRIPTION("RTC driver for the rk808 series PMICs"); |
| 417 | MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>"); |
| 418 | MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>"); |
| 419 | MODULE_LICENSE("GPL"); |
| 420 | MODULE_ALIAS("platform:rk808-rtc"); |