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 /* 60 * The Rockchip calendar used by the RK808 counts November with 31 days. We use 61 * these translation functions to convert its dates to/from the Gregorian 62 * calendar used by the rest of the world. We arbitrarily define Jan 1st, 2016 63 * as the day when both calendars were in sync, and treat all other dates 64 * relative to that. 65 * NOTE: Other system software (e.g. firmware) that reads the same hardware must 66 * implement this exact same conversion algorithm, with the same anchor date. 67 */ 68 static time64_t nov2dec_transitions(struct rtc_time *tm) 69 { 70 return (tm->tm_year + 1900) - 2016 + (tm->tm_mon + 1 > 11 ? 1 : 0); 71 } 72 73 static void rockchip_to_gregorian(struct rtc_time *tm) 74 { 75 /* If it's Nov 31st, rtc_tm_to_time64() will count that like Dec 1st */ 76 time64_t time = rtc_tm_to_time64(tm); 77 rtc_time64_to_tm(time + nov2dec_transitions(tm) * 86400, tm); 78 } 79 80 static void gregorian_to_rockchip(struct rtc_time *tm) 81 { 82 time64_t extra_days = nov2dec_transitions(tm); 83 time64_t time = rtc_tm_to_time64(tm); 84 rtc_time64_to_tm(time - extra_days * 86400, tm); 85 86 /* Compensate if we went back over Nov 31st (will work up to 2381) */ 87 if (nov2dec_transitions(tm) < extra_days) { 88 if (tm->tm_mon + 1 == 11) 89 tm->tm_mday++; /* This may result in 31! */ 90 else 91 rtc_time64_to_tm(time - (extra_days - 1) * 86400, tm); 92 } 93 } 94 95 /* Read current time and date in RTC */ 96 static int rk808_rtc_readtime(struct device *dev, struct rtc_time *tm) 97 { 98 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 99 struct rk808 *rk808 = rk808_rtc->rk808; 100 u8 rtc_data[NUM_TIME_REGS]; 101 int ret; 102 103 /* Force an update of the shadowed registers right now */ 104 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, 105 BIT_RTC_CTRL_REG_RTC_GET_TIME, 106 BIT_RTC_CTRL_REG_RTC_GET_TIME); 107 if (ret) { 108 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret); 109 return ret; 110 } 111 112 /* 113 * After we set the GET_TIME bit, the rtc time can't be read 114 * immediately. So we should wait up to 31.25 us, about one cycle of 115 * 32khz. If we clear the GET_TIME bit here, the time of i2c transfer 116 * certainly more than 31.25us: 16 * 2.5us at 400kHz bus frequency. 117 */ 118 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, 119 BIT_RTC_CTRL_REG_RTC_GET_TIME, 120 0); 121 if (ret) { 122 dev_err(dev, "Failed to update bits rtc_ctrl: %d\n", ret); 123 return ret; 124 } 125 126 ret = regmap_bulk_read(rk808->regmap, RK808_SECONDS_REG, 127 rtc_data, NUM_TIME_REGS); 128 if (ret) { 129 dev_err(dev, "Failed to bulk read rtc_data: %d\n", ret); 130 return ret; 131 } 132 133 tm->tm_sec = bcd2bin(rtc_data[0] & SECONDS_REG_MSK); 134 tm->tm_min = bcd2bin(rtc_data[1] & MINUTES_REG_MAK); 135 tm->tm_hour = bcd2bin(rtc_data[2] & HOURS_REG_MSK); 136 tm->tm_mday = bcd2bin(rtc_data[3] & DAYS_REG_MSK); 137 tm->tm_mon = (bcd2bin(rtc_data[4] & MONTHS_REG_MSK)) - 1; 138 tm->tm_year = (bcd2bin(rtc_data[5] & YEARS_REG_MSK)) + 100; 139 tm->tm_wday = bcd2bin(rtc_data[6] & WEEKS_REG_MSK); 140 rockchip_to_gregorian(tm); 141 dev_dbg(dev, "RTC date/time %ptRd(%d) %ptRt\n", tm, tm->tm_wday, tm); 142 143 return ret; 144 } 145 146 /* Set current time and date in RTC */ 147 static int rk808_rtc_set_time(struct device *dev, struct rtc_time *tm) 148 { 149 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 150 struct rk808 *rk808 = rk808_rtc->rk808; 151 u8 rtc_data[NUM_TIME_REGS]; 152 int ret; 153 154 dev_dbg(dev, "set RTC date/time %ptRd(%d) %ptRt\n", tm, tm->tm_wday, tm); 155 gregorian_to_rockchip(tm); 156 rtc_data[0] = bin2bcd(tm->tm_sec); 157 rtc_data[1] = bin2bcd(tm->tm_min); 158 rtc_data[2] = bin2bcd(tm->tm_hour); 159 rtc_data[3] = bin2bcd(tm->tm_mday); 160 rtc_data[4] = bin2bcd(tm->tm_mon + 1); 161 rtc_data[5] = bin2bcd(tm->tm_year - 100); 162 rtc_data[6] = bin2bcd(tm->tm_wday); 163 164 /* Stop RTC while updating the RTC registers */ 165 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, 166 BIT_RTC_CTRL_REG_STOP_RTC_M, 167 BIT_RTC_CTRL_REG_STOP_RTC_M); 168 if (ret) { 169 dev_err(dev, "Failed to update RTC control: %d\n", ret); 170 return ret; 171 } 172 173 ret = regmap_bulk_write(rk808->regmap, RK808_SECONDS_REG, 174 rtc_data, NUM_TIME_REGS); 175 if (ret) { 176 dev_err(dev, "Failed to bull write rtc_data: %d\n", ret); 177 return ret; 178 } 179 /* Start RTC again */ 180 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, 181 BIT_RTC_CTRL_REG_STOP_RTC_M, 0); 182 if (ret) { 183 dev_err(dev, "Failed to update RTC control: %d\n", ret); 184 return ret; 185 } 186 return 0; 187 } 188 189 /* Read alarm time and date in RTC */ 190 static int rk808_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm) 191 { 192 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 193 struct rk808 *rk808 = rk808_rtc->rk808; 194 u8 alrm_data[NUM_ALARM_REGS]; 195 uint32_t int_reg; 196 int ret; 197 198 ret = regmap_bulk_read(rk808->regmap, RK808_ALARM_SECONDS_REG, 199 alrm_data, NUM_ALARM_REGS); 200 201 alrm->time.tm_sec = bcd2bin(alrm_data[0] & SECONDS_REG_MSK); 202 alrm->time.tm_min = bcd2bin(alrm_data[1] & MINUTES_REG_MAK); 203 alrm->time.tm_hour = bcd2bin(alrm_data[2] & HOURS_REG_MSK); 204 alrm->time.tm_mday = bcd2bin(alrm_data[3] & DAYS_REG_MSK); 205 alrm->time.tm_mon = (bcd2bin(alrm_data[4] & MONTHS_REG_MSK)) - 1; 206 alrm->time.tm_year = (bcd2bin(alrm_data[5] & YEARS_REG_MSK)) + 100; 207 rockchip_to_gregorian(&alrm->time); 208 209 ret = regmap_read(rk808->regmap, RK808_RTC_INT_REG, &int_reg); 210 if (ret) { 211 dev_err(dev, "Failed to read RTC INT REG: %d\n", ret); 212 return ret; 213 } 214 215 dev_dbg(dev, "alrm read RTC date/time %ptRd(%d) %ptRt\n", 216 &alrm->time, alrm->time.tm_wday, &alrm->time); 217 218 alrm->enabled = (int_reg & BIT_RTC_INTERRUPTS_REG_IT_ALARM_M) ? 1 : 0; 219 220 return 0; 221 } 222 223 static int rk808_rtc_stop_alarm(struct rk808_rtc *rk808_rtc) 224 { 225 struct rk808 *rk808 = rk808_rtc->rk808; 226 int ret; 227 228 ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG, 229 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 0); 230 231 return ret; 232 } 233 234 static int rk808_rtc_start_alarm(struct rk808_rtc *rk808_rtc) 235 { 236 struct rk808 *rk808 = rk808_rtc->rk808; 237 int ret; 238 239 ret = regmap_update_bits(rk808->regmap, RK808_RTC_INT_REG, 240 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M, 241 BIT_RTC_INTERRUPTS_REG_IT_ALARM_M); 242 243 return ret; 244 } 245 246 static int rk808_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm) 247 { 248 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 249 struct rk808 *rk808 = rk808_rtc->rk808; 250 u8 alrm_data[NUM_ALARM_REGS]; 251 int ret; 252 253 ret = rk808_rtc_stop_alarm(rk808_rtc); 254 if (ret) { 255 dev_err(dev, "Failed to stop alarm: %d\n", ret); 256 return ret; 257 } 258 dev_dbg(dev, "alrm set RTC date/time %ptRd(%d) %ptRt\n", 259 &alrm->time, alrm->time.tm_wday, &alrm->time); 260 261 gregorian_to_rockchip(&alrm->time); 262 alrm_data[0] = bin2bcd(alrm->time.tm_sec); 263 alrm_data[1] = bin2bcd(alrm->time.tm_min); 264 alrm_data[2] = bin2bcd(alrm->time.tm_hour); 265 alrm_data[3] = bin2bcd(alrm->time.tm_mday); 266 alrm_data[4] = bin2bcd(alrm->time.tm_mon + 1); 267 alrm_data[5] = bin2bcd(alrm->time.tm_year - 100); 268 269 ret = regmap_bulk_write(rk808->regmap, RK808_ALARM_SECONDS_REG, 270 alrm_data, NUM_ALARM_REGS); 271 if (ret) { 272 dev_err(dev, "Failed to bulk write: %d\n", ret); 273 return ret; 274 } 275 if (alrm->enabled) { 276 ret = rk808_rtc_start_alarm(rk808_rtc); 277 if (ret) { 278 dev_err(dev, "Failed to start alarm: %d\n", ret); 279 return ret; 280 } 281 } 282 return 0; 283 } 284 285 static int rk808_rtc_alarm_irq_enable(struct device *dev, 286 unsigned int enabled) 287 { 288 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 289 290 if (enabled) 291 return rk808_rtc_start_alarm(rk808_rtc); 292 293 return rk808_rtc_stop_alarm(rk808_rtc); 294 } 295 296 /* 297 * We will just handle setting the frequency and make use the framework for 298 * reading the periodic interupts. 299 * 300 * @freq: Current periodic IRQ freq: 301 * bit 0: every second 302 * bit 1: every minute 303 * bit 2: every hour 304 * bit 3: every day 305 */ 306 static irqreturn_t rk808_alarm_irq(int irq, void *data) 307 { 308 struct rk808_rtc *rk808_rtc = data; 309 struct rk808 *rk808 = rk808_rtc->rk808; 310 struct i2c_client *client = rk808->i2c; 311 int ret; 312 313 ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG, 314 RTC_STATUS_MASK); 315 if (ret) { 316 dev_err(&client->dev, 317 "%s:Failed to update RTC status: %d\n", __func__, ret); 318 return ret; 319 } 320 321 rtc_update_irq(rk808_rtc->rtc, 1, RTC_IRQF | RTC_AF); 322 dev_dbg(&client->dev, 323 "%s:irq=%d\n", __func__, irq); 324 return IRQ_HANDLED; 325 } 326 327 static const struct rtc_class_ops rk808_rtc_ops = { 328 .read_time = rk808_rtc_readtime, 329 .set_time = rk808_rtc_set_time, 330 .read_alarm = rk808_rtc_readalarm, 331 .set_alarm = rk808_rtc_setalarm, 332 .alarm_irq_enable = rk808_rtc_alarm_irq_enable, 333 }; 334 335 #ifdef CONFIG_PM_SLEEP 336 /* Turn off the alarm if it should not be a wake source. */ 337 static int rk808_rtc_suspend(struct device *dev) 338 { 339 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 340 341 if (device_may_wakeup(dev)) 342 enable_irq_wake(rk808_rtc->irq); 343 344 return 0; 345 } 346 347 /* Enable the alarm if it should be enabled (in case it was disabled to 348 * prevent use as a wake source). 349 */ 350 static int rk808_rtc_resume(struct device *dev) 351 { 352 struct rk808_rtc *rk808_rtc = dev_get_drvdata(dev); 353 354 if (device_may_wakeup(dev)) 355 disable_irq_wake(rk808_rtc->irq); 356 357 return 0; 358 } 359 #endif 360 361 static SIMPLE_DEV_PM_OPS(rk808_rtc_pm_ops, 362 rk808_rtc_suspend, rk808_rtc_resume); 363 364 static int rk808_rtc_probe(struct platform_device *pdev) 365 { 366 struct rk808 *rk808 = dev_get_drvdata(pdev->dev.parent); 367 struct rk808_rtc *rk808_rtc; 368 int ret; 369 370 rk808_rtc = devm_kzalloc(&pdev->dev, sizeof(*rk808_rtc), GFP_KERNEL); 371 if (rk808_rtc == NULL) 372 return -ENOMEM; 373 374 platform_set_drvdata(pdev, rk808_rtc); 375 rk808_rtc->rk808 = rk808; 376 377 /* start rtc running by default, and use shadowed timer. */ 378 ret = regmap_update_bits(rk808->regmap, RK808_RTC_CTRL_REG, 379 BIT_RTC_CTRL_REG_STOP_RTC_M | 380 BIT_RTC_CTRL_REG_RTC_READSEL_M, 381 BIT_RTC_CTRL_REG_RTC_READSEL_M); 382 if (ret) { 383 dev_err(&pdev->dev, 384 "Failed to update RTC control: %d\n", ret); 385 return ret; 386 } 387 388 ret = regmap_write(rk808->regmap, RK808_RTC_STATUS_REG, 389 RTC_STATUS_MASK); 390 if (ret) { 391 dev_err(&pdev->dev, 392 "Failed to write RTC status: %d\n", ret); 393 return ret; 394 } 395 396 device_init_wakeup(&pdev->dev, 1); 397 398 rk808_rtc->rtc = devm_rtc_allocate_device(&pdev->dev); 399 if (IS_ERR(rk808_rtc->rtc)) 400 return PTR_ERR(rk808_rtc->rtc); 401 402 rk808_rtc->rtc->ops = &rk808_rtc_ops; 403 404 rk808_rtc->irq = platform_get_irq(pdev, 0); 405 if (rk808_rtc->irq < 0) { 406 if (rk808_rtc->irq != -EPROBE_DEFER) 407 dev_err(&pdev->dev, "Wake up is not possible as irq = %d\n", 408 rk808_rtc->irq); 409 return rk808_rtc->irq; 410 } 411 412 /* request alarm irq of rk808 */ 413 ret = devm_request_threaded_irq(&pdev->dev, rk808_rtc->irq, NULL, 414 rk808_alarm_irq, 0, 415 "RTC alarm", rk808_rtc); 416 if (ret) { 417 dev_err(&pdev->dev, "Failed to request alarm IRQ %d: %d\n", 418 rk808_rtc->irq, ret); 419 return ret; 420 } 421 422 return rtc_register_device(rk808_rtc->rtc); 423 } 424 425 static struct platform_driver rk808_rtc_driver = { 426 .probe = rk808_rtc_probe, 427 .driver = { 428 .name = "rk808-rtc", 429 .pm = &rk808_rtc_pm_ops, 430 }, 431 }; 432 433 module_platform_driver(rk808_rtc_driver); 434 435 MODULE_DESCRIPTION("RTC driver for the rk808 series PMICs"); 436 MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>"); 437 MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>"); 438 MODULE_LICENSE("GPL"); 439 MODULE_ALIAS("platform:rk808-rtc"); 440