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