1 /* 2 * An rtc driver for the Dallas/Maxim DS1685/DS1687 and related real-time 3 * chips. 4 * 5 * Copyright (C) 2011-2014 Joshua Kinard <[email protected]>. 6 * Copyright (C) 2009 Matthias Fuchs <[email protected]>. 7 * 8 * References: 9 * DS1685/DS1687 3V/5V Real-Time Clocks, 19-5215, Rev 4/10. 10 * DS17x85/DS17x87 3V/5V Real-Time Clocks, 19-5222, Rev 4/10. 11 * DS1689/DS1693 3V/5V Serialized Real-Time Clocks, Rev 112105. 12 * Application Note 90, Using the Multiplex Bus RTC Extended Features. 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License version 2 as 16 * published by the Free Software Foundation. 17 */ 18 19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 20 21 #include <linux/bcd.h> 22 #include <linux/delay.h> 23 #include <linux/io.h> 24 #include <linux/module.h> 25 #include <linux/platform_device.h> 26 #include <linux/rtc.h> 27 #include <linux/workqueue.h> 28 29 #include <linux/rtc/ds1685.h> 30 31 #ifdef CONFIG_PROC_FS 32 #include <linux/proc_fs.h> 33 #endif 34 35 36 /* ----------------------------------------------------------------------- */ 37 /* Standard read/write functions if platform does not provide overrides */ 38 39 /** 40 * ds1685_read - read a value from an rtc register. 41 * @rtc: pointer to the ds1685 rtc structure. 42 * @reg: the register address to read. 43 */ 44 static u8 45 ds1685_read(struct ds1685_priv *rtc, int reg) 46 { 47 return readb((u8 __iomem *)rtc->regs + 48 (reg * rtc->regstep)); 49 } 50 51 /** 52 * ds1685_write - write a value to an rtc register. 53 * @rtc: pointer to the ds1685 rtc structure. 54 * @reg: the register address to write. 55 * @value: value to write to the register. 56 */ 57 static void 58 ds1685_write(struct ds1685_priv *rtc, int reg, u8 value) 59 { 60 writeb(value, ((u8 __iomem *)rtc->regs + 61 (reg * rtc->regstep))); 62 } 63 /* ----------------------------------------------------------------------- */ 64 65 66 /* ----------------------------------------------------------------------- */ 67 /* Inlined functions */ 68 69 /** 70 * ds1685_rtc_bcd2bin - bcd2bin wrapper in case platform doesn't support BCD. 71 * @rtc: pointer to the ds1685 rtc structure. 72 * @val: u8 time value to consider converting. 73 * @bcd_mask: u8 mask value if BCD mode is used. 74 * @bin_mask: u8 mask value if BIN mode is used. 75 * 76 * Returns the value, converted to BIN if originally in BCD and bcd_mode TRUE. 77 */ 78 static inline u8 79 ds1685_rtc_bcd2bin(struct ds1685_priv *rtc, u8 val, u8 bcd_mask, u8 bin_mask) 80 { 81 if (rtc->bcd_mode) 82 return (bcd2bin(val) & bcd_mask); 83 84 return (val & bin_mask); 85 } 86 87 /** 88 * ds1685_rtc_bin2bcd - bin2bcd wrapper in case platform doesn't support BCD. 89 * @rtc: pointer to the ds1685 rtc structure. 90 * @val: u8 time value to consider converting. 91 * @bin_mask: u8 mask value if BIN mode is used. 92 * @bcd_mask: u8 mask value if BCD mode is used. 93 * 94 * Returns the value, converted to BCD if originally in BIN and bcd_mode TRUE. 95 */ 96 static inline u8 97 ds1685_rtc_bin2bcd(struct ds1685_priv *rtc, u8 val, u8 bin_mask, u8 bcd_mask) 98 { 99 if (rtc->bcd_mode) 100 return (bin2bcd(val) & bcd_mask); 101 102 return (val & bin_mask); 103 } 104 105 /** 106 * s1685_rtc_check_mday - check validity of the day of month. 107 * @rtc: pointer to the ds1685 rtc structure. 108 * @mday: day of month. 109 * 110 * Returns -EDOM if the day of month is not within 1..31 range. 111 */ 112 static inline int 113 ds1685_rtc_check_mday(struct ds1685_priv *rtc, u8 mday) 114 { 115 if (rtc->bcd_mode) { 116 if (mday < 0x01 || mday > 0x31 || (mday & 0x0f) > 0x09) 117 return -EDOM; 118 } else { 119 if (mday < 1 || mday > 31) 120 return -EDOM; 121 } 122 return 0; 123 } 124 125 /** 126 * ds1685_rtc_switch_to_bank0 - switch the rtc to bank 0. 127 * @rtc: pointer to the ds1685 rtc structure. 128 */ 129 static inline void 130 ds1685_rtc_switch_to_bank0(struct ds1685_priv *rtc) 131 { 132 rtc->write(rtc, RTC_CTRL_A, 133 (rtc->read(rtc, RTC_CTRL_A) & ~(RTC_CTRL_A_DV0))); 134 } 135 136 /** 137 * ds1685_rtc_switch_to_bank1 - switch the rtc to bank 1. 138 * @rtc: pointer to the ds1685 rtc structure. 139 */ 140 static inline void 141 ds1685_rtc_switch_to_bank1(struct ds1685_priv *rtc) 142 { 143 rtc->write(rtc, RTC_CTRL_A, 144 (rtc->read(rtc, RTC_CTRL_A) | RTC_CTRL_A_DV0)); 145 } 146 147 /** 148 * ds1685_rtc_begin_data_access - prepare the rtc for data access. 149 * @rtc: pointer to the ds1685 rtc structure. 150 * 151 * This takes several steps to prepare the rtc for access to get/set time 152 * and alarm values from the rtc registers: 153 * - Sets the SET bit in Control Register B. 154 * - Reads Ext Control Register 4A and checks the INCR bit. 155 * - If INCR is active, a short delay is added before Ext Control Register 4A 156 * is read again in a loop until INCR is inactive. 157 * - Switches the rtc to bank 1. This allows access to all relevant 158 * data for normal rtc operation, as bank 0 contains only the nvram. 159 */ 160 static inline void 161 ds1685_rtc_begin_data_access(struct ds1685_priv *rtc) 162 { 163 /* Set the SET bit in Ctrl B */ 164 rtc->write(rtc, RTC_CTRL_B, 165 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET)); 166 167 /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */ 168 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR) 169 cpu_relax(); 170 171 /* Switch to Bank 1 */ 172 ds1685_rtc_switch_to_bank1(rtc); 173 } 174 175 /** 176 * ds1685_rtc_end_data_access - end data access on the rtc. 177 * @rtc: pointer to the ds1685 rtc structure. 178 * 179 * This ends what was started by ds1685_rtc_begin_data_access: 180 * - Switches the rtc back to bank 0. 181 * - Clears the SET bit in Control Register B. 182 */ 183 static inline void 184 ds1685_rtc_end_data_access(struct ds1685_priv *rtc) 185 { 186 /* Switch back to Bank 0 */ 187 ds1685_rtc_switch_to_bank1(rtc); 188 189 /* Clear the SET bit in Ctrl B */ 190 rtc->write(rtc, RTC_CTRL_B, 191 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET))); 192 } 193 194 /** 195 * ds1685_rtc_get_ssn - retrieve the silicon serial number. 196 * @rtc: pointer to the ds1685 rtc structure. 197 * @ssn: u8 array to hold the bits of the silicon serial number. 198 * 199 * This number starts at 0x40, and is 8-bytes long, ending at 0x47. The 200 * first byte is the model number, the next six bytes are the serial number 201 * digits, and the final byte is a CRC check byte. Together, they form the 202 * silicon serial number. 203 * 204 * These values are stored in bank1, so ds1685_rtc_switch_to_bank1 must be 205 * called first before calling this function, else data will be read out of 206 * the bank0 NVRAM. Be sure to call ds1685_rtc_switch_to_bank0 when done. 207 */ 208 static inline void 209 ds1685_rtc_get_ssn(struct ds1685_priv *rtc, u8 *ssn) 210 { 211 ssn[0] = rtc->read(rtc, RTC_BANK1_SSN_MODEL); 212 ssn[1] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_1); 213 ssn[2] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_2); 214 ssn[3] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_3); 215 ssn[4] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_4); 216 ssn[5] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_5); 217 ssn[6] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_6); 218 ssn[7] = rtc->read(rtc, RTC_BANK1_SSN_CRC); 219 } 220 /* ----------------------------------------------------------------------- */ 221 222 223 /* ----------------------------------------------------------------------- */ 224 /* Read/Set Time & Alarm functions */ 225 226 /** 227 * ds1685_rtc_read_time - reads the time registers. 228 * @dev: pointer to device structure. 229 * @tm: pointer to rtc_time structure. 230 */ 231 static int 232 ds1685_rtc_read_time(struct device *dev, struct rtc_time *tm) 233 { 234 struct ds1685_priv *rtc = dev_get_drvdata(dev); 235 u8 ctrlb, century; 236 u8 seconds, minutes, hours, wday, mday, month, years; 237 238 /* Fetch the time info from the RTC registers. */ 239 ds1685_rtc_begin_data_access(rtc); 240 seconds = rtc->read(rtc, RTC_SECS); 241 minutes = rtc->read(rtc, RTC_MINS); 242 hours = rtc->read(rtc, RTC_HRS); 243 wday = rtc->read(rtc, RTC_WDAY); 244 mday = rtc->read(rtc, RTC_MDAY); 245 month = rtc->read(rtc, RTC_MONTH); 246 years = rtc->read(rtc, RTC_YEAR); 247 century = rtc->read(rtc, RTC_CENTURY); 248 ctrlb = rtc->read(rtc, RTC_CTRL_B); 249 ds1685_rtc_end_data_access(rtc); 250 251 /* bcd2bin if needed, perform fixups, and store to rtc_time. */ 252 years = ds1685_rtc_bcd2bin(rtc, years, RTC_YEAR_BCD_MASK, 253 RTC_YEAR_BIN_MASK); 254 century = ds1685_rtc_bcd2bin(rtc, century, RTC_CENTURY_MASK, 255 RTC_CENTURY_MASK); 256 tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK, 257 RTC_SECS_BIN_MASK); 258 tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK, 259 RTC_MINS_BIN_MASK); 260 tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK, 261 RTC_HRS_24_BIN_MASK); 262 tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK, 263 RTC_WDAY_MASK) - 1); 264 tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK, 265 RTC_MDAY_BIN_MASK); 266 tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK, 267 RTC_MONTH_BIN_MASK) - 1); 268 tm->tm_year = ((years + (century * 100)) - 1900); 269 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year); 270 tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */ 271 272 return 0; 273 } 274 275 /** 276 * ds1685_rtc_set_time - sets the time registers. 277 * @dev: pointer to device structure. 278 * @tm: pointer to rtc_time structure. 279 */ 280 static int 281 ds1685_rtc_set_time(struct device *dev, struct rtc_time *tm) 282 { 283 struct ds1685_priv *rtc = dev_get_drvdata(dev); 284 u8 ctrlb, seconds, minutes, hours, wday, mday, month, years, century; 285 286 /* Fetch the time info from rtc_time. */ 287 seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK, 288 RTC_SECS_BCD_MASK); 289 minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK, 290 RTC_MINS_BCD_MASK); 291 hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK, 292 RTC_HRS_24_BCD_MASK); 293 wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK, 294 RTC_WDAY_MASK); 295 mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK, 296 RTC_MDAY_BCD_MASK); 297 month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK, 298 RTC_MONTH_BCD_MASK); 299 years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100), 300 RTC_YEAR_BIN_MASK, RTC_YEAR_BCD_MASK); 301 century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100), 302 RTC_CENTURY_MASK, RTC_CENTURY_MASK); 303 304 /* 305 * Perform Sanity Checks: 306 * - Months: !> 12, Month Day != 0. 307 * - Month Day !> Max days in current month. 308 * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7. 309 */ 310 if ((tm->tm_mon > 11) || (mday == 0)) 311 return -EDOM; 312 313 if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year)) 314 return -EDOM; 315 316 if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || 317 (tm->tm_sec >= 60) || (wday > 7)) 318 return -EDOM; 319 320 /* 321 * Set the data mode to use and store the time values in the 322 * RTC registers. 323 */ 324 ds1685_rtc_begin_data_access(rtc); 325 ctrlb = rtc->read(rtc, RTC_CTRL_B); 326 if (rtc->bcd_mode) 327 ctrlb &= ~(RTC_CTRL_B_DM); 328 else 329 ctrlb |= RTC_CTRL_B_DM; 330 rtc->write(rtc, RTC_CTRL_B, ctrlb); 331 rtc->write(rtc, RTC_SECS, seconds); 332 rtc->write(rtc, RTC_MINS, minutes); 333 rtc->write(rtc, RTC_HRS, hours); 334 rtc->write(rtc, RTC_WDAY, wday); 335 rtc->write(rtc, RTC_MDAY, mday); 336 rtc->write(rtc, RTC_MONTH, month); 337 rtc->write(rtc, RTC_YEAR, years); 338 rtc->write(rtc, RTC_CENTURY, century); 339 ds1685_rtc_end_data_access(rtc); 340 341 return 0; 342 } 343 344 /** 345 * ds1685_rtc_read_alarm - reads the alarm registers. 346 * @dev: pointer to device structure. 347 * @alrm: pointer to rtc_wkalrm structure. 348 * 349 * There are three primary alarm registers: seconds, minutes, and hours. 350 * A fourth alarm register for the month date is also available in bank1 for 351 * kickstart/wakeup features. The DS1685/DS1687 manual states that a 352 * "don't care" value ranging from 0xc0 to 0xff may be written into one or 353 * more of the three alarm bytes to act as a wildcard value. The fourth 354 * byte doesn't support a "don't care" value. 355 */ 356 static int 357 ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 358 { 359 struct ds1685_priv *rtc = dev_get_drvdata(dev); 360 u8 seconds, minutes, hours, mday, ctrlb, ctrlc; 361 int ret; 362 363 /* Fetch the alarm info from the RTC alarm registers. */ 364 ds1685_rtc_begin_data_access(rtc); 365 seconds = rtc->read(rtc, RTC_SECS_ALARM); 366 minutes = rtc->read(rtc, RTC_MINS_ALARM); 367 hours = rtc->read(rtc, RTC_HRS_ALARM); 368 mday = rtc->read(rtc, RTC_MDAY_ALARM); 369 ctrlb = rtc->read(rtc, RTC_CTRL_B); 370 ctrlc = rtc->read(rtc, RTC_CTRL_C); 371 ds1685_rtc_end_data_access(rtc); 372 373 /* Check the month date for validity. */ 374 ret = ds1685_rtc_check_mday(rtc, mday); 375 if (ret) 376 return ret; 377 378 /* 379 * Check the three alarm bytes. 380 * 381 * The Linux RTC system doesn't support the "don't care" capability 382 * of this RTC chip. We check for it anyways in case support is 383 * added in the future and only assign when we care. 384 */ 385 if (likely(seconds < 0xc0)) 386 alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, 387 RTC_SECS_BCD_MASK, 388 RTC_SECS_BIN_MASK); 389 390 if (likely(minutes < 0xc0)) 391 alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes, 392 RTC_MINS_BCD_MASK, 393 RTC_MINS_BIN_MASK); 394 395 if (likely(hours < 0xc0)) 396 alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours, 397 RTC_HRS_24_BCD_MASK, 398 RTC_HRS_24_BIN_MASK); 399 400 /* Write the data to rtc_wkalrm. */ 401 alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK, 402 RTC_MDAY_BIN_MASK); 403 alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE); 404 alrm->pending = !!(ctrlc & RTC_CTRL_C_AF); 405 406 return 0; 407 } 408 409 /** 410 * ds1685_rtc_set_alarm - sets the alarm in registers. 411 * @dev: pointer to device structure. 412 * @alrm: pointer to rtc_wkalrm structure. 413 */ 414 static int 415 ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 416 { 417 struct ds1685_priv *rtc = dev_get_drvdata(dev); 418 u8 ctrlb, seconds, minutes, hours, mday; 419 int ret; 420 421 /* Fetch the alarm info and convert to BCD. */ 422 seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec, 423 RTC_SECS_BIN_MASK, 424 RTC_SECS_BCD_MASK); 425 minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min, 426 RTC_MINS_BIN_MASK, 427 RTC_MINS_BCD_MASK); 428 hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour, 429 RTC_HRS_24_BIN_MASK, 430 RTC_HRS_24_BCD_MASK); 431 mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday, 432 RTC_MDAY_BIN_MASK, 433 RTC_MDAY_BCD_MASK); 434 435 /* Check the month date for validity. */ 436 ret = ds1685_rtc_check_mday(rtc, mday); 437 if (ret) 438 return ret; 439 440 /* 441 * Check the three alarm bytes. 442 * 443 * The Linux RTC system doesn't support the "don't care" capability 444 * of this RTC chip because rtc_valid_tm tries to validate every 445 * field, and we only support four fields. We put the support 446 * here anyways for the future. 447 */ 448 if (unlikely(seconds >= 0xc0)) 449 seconds = 0xff; 450 451 if (unlikely(minutes >= 0xc0)) 452 minutes = 0xff; 453 454 if (unlikely(hours >= 0xc0)) 455 hours = 0xff; 456 457 alrm->time.tm_mon = -1; 458 alrm->time.tm_year = -1; 459 alrm->time.tm_wday = -1; 460 alrm->time.tm_yday = -1; 461 alrm->time.tm_isdst = -1; 462 463 /* Disable the alarm interrupt first. */ 464 ds1685_rtc_begin_data_access(rtc); 465 ctrlb = rtc->read(rtc, RTC_CTRL_B); 466 rtc->write(rtc, RTC_CTRL_B, (ctrlb & ~(RTC_CTRL_B_AIE))); 467 468 /* Read ctrlc to clear RTC_CTRL_C_AF. */ 469 rtc->read(rtc, RTC_CTRL_C); 470 471 /* 472 * Set the data mode to use and store the time values in the 473 * RTC registers. 474 */ 475 ctrlb = rtc->read(rtc, RTC_CTRL_B); 476 if (rtc->bcd_mode) 477 ctrlb &= ~(RTC_CTRL_B_DM); 478 else 479 ctrlb |= RTC_CTRL_B_DM; 480 rtc->write(rtc, RTC_CTRL_B, ctrlb); 481 rtc->write(rtc, RTC_SECS_ALARM, seconds); 482 rtc->write(rtc, RTC_MINS_ALARM, minutes); 483 rtc->write(rtc, RTC_HRS_ALARM, hours); 484 rtc->write(rtc, RTC_MDAY_ALARM, mday); 485 486 /* Re-enable the alarm if needed. */ 487 if (alrm->enabled) { 488 ctrlb = rtc->read(rtc, RTC_CTRL_B); 489 ctrlb |= RTC_CTRL_B_AIE; 490 rtc->write(rtc, RTC_CTRL_B, ctrlb); 491 } 492 493 /* Done! */ 494 ds1685_rtc_end_data_access(rtc); 495 496 return 0; 497 } 498 /* ----------------------------------------------------------------------- */ 499 500 501 /* ----------------------------------------------------------------------- */ 502 /* /dev/rtcX Interface functions */ 503 504 /** 505 * ds1685_rtc_alarm_irq_enable - replaces ioctl() RTC_AIE on/off. 506 * @dev: pointer to device structure. 507 * @enabled: flag indicating whether to enable or disable. 508 */ 509 static int 510 ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) 511 { 512 struct ds1685_priv *rtc = dev_get_drvdata(dev); 513 unsigned long flags = 0; 514 515 /* Enable/disable the Alarm IRQ-Enable flag. */ 516 spin_lock_irqsave(&rtc->lock, flags); 517 518 /* Flip the requisite interrupt-enable bit. */ 519 if (enabled) 520 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) | 521 RTC_CTRL_B_AIE)); 522 else 523 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) & 524 ~(RTC_CTRL_B_AIE))); 525 526 /* Read Control C to clear all the flag bits. */ 527 rtc->read(rtc, RTC_CTRL_C); 528 spin_unlock_irqrestore(&rtc->lock, flags); 529 530 return 0; 531 } 532 /* ----------------------------------------------------------------------- */ 533 534 535 /* ----------------------------------------------------------------------- */ 536 /* IRQ handler & workqueue. */ 537 538 /** 539 * ds1685_rtc_irq_handler - IRQ handler. 540 * @irq: IRQ number. 541 * @dev_id: platform device pointer. 542 */ 543 static irqreturn_t 544 ds1685_rtc_irq_handler(int irq, void *dev_id) 545 { 546 struct platform_device *pdev = dev_id; 547 struct ds1685_priv *rtc = platform_get_drvdata(pdev); 548 u8 ctrlb, ctrlc; 549 unsigned long events = 0; 550 u8 num_irqs = 0; 551 552 /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */ 553 if (unlikely(!rtc)) 554 return IRQ_HANDLED; 555 556 /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */ 557 spin_lock(&rtc->lock); 558 ctrlb = rtc->read(rtc, RTC_CTRL_B); 559 ctrlc = rtc->read(rtc, RTC_CTRL_C); 560 561 /* Is the IRQF bit set? */ 562 if (likely(ctrlc & RTC_CTRL_C_IRQF)) { 563 /* 564 * We need to determine if it was one of the standard 565 * events: PF, AF, or UF. If so, we handle them and 566 * update the RTC core. 567 */ 568 if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) { 569 events = RTC_IRQF; 570 571 /* Check for a periodic interrupt. */ 572 if ((ctrlb & RTC_CTRL_B_PIE) && 573 (ctrlc & RTC_CTRL_C_PF)) { 574 events |= RTC_PF; 575 num_irqs++; 576 } 577 578 /* Check for an alarm interrupt. */ 579 if ((ctrlb & RTC_CTRL_B_AIE) && 580 (ctrlc & RTC_CTRL_C_AF)) { 581 events |= RTC_AF; 582 num_irqs++; 583 } 584 585 /* Check for an update interrupt. */ 586 if ((ctrlb & RTC_CTRL_B_UIE) && 587 (ctrlc & RTC_CTRL_C_UF)) { 588 events |= RTC_UF; 589 num_irqs++; 590 } 591 592 rtc_update_irq(rtc->dev, num_irqs, events); 593 } else { 594 /* 595 * One of the "extended" interrupts was received that 596 * is not recognized by the RTC core. These need to 597 * be handled in task context as they can call other 598 * functions and the time spent in irq context needs 599 * to be minimized. Schedule them into a workqueue 600 * and inform the RTC core that the IRQs were handled. 601 */ 602 spin_unlock(&rtc->lock); 603 schedule_work(&rtc->work); 604 rtc_update_irq(rtc->dev, 0, 0); 605 return IRQ_HANDLED; 606 } 607 } 608 spin_unlock(&rtc->lock); 609 610 return events ? IRQ_HANDLED : IRQ_NONE; 611 } 612 613 /** 614 * ds1685_rtc_work_queue - work queue handler. 615 * @work: work_struct containing data to work on in task context. 616 */ 617 static void 618 ds1685_rtc_work_queue(struct work_struct *work) 619 { 620 struct ds1685_priv *rtc = container_of(work, 621 struct ds1685_priv, work); 622 struct platform_device *pdev = to_platform_device(&rtc->dev->dev); 623 struct mutex *rtc_mutex = &rtc->dev->ops_lock; 624 u8 ctrl4a, ctrl4b; 625 626 mutex_lock(rtc_mutex); 627 628 ds1685_rtc_switch_to_bank1(rtc); 629 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); 630 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); 631 632 /* 633 * Check for a kickstart interrupt. With Vcc applied, this 634 * typically means that the power button was pressed, so we 635 * begin the shutdown sequence. 636 */ 637 if ((ctrl4b & RTC_CTRL_4B_KSE) && (ctrl4a & RTC_CTRL_4A_KF)) { 638 /* Briefly disable kickstarts to debounce button presses. */ 639 rtc->write(rtc, RTC_EXT_CTRL_4B, 640 (rtc->read(rtc, RTC_EXT_CTRL_4B) & 641 ~(RTC_CTRL_4B_KSE))); 642 643 /* Clear the kickstart flag. */ 644 rtc->write(rtc, RTC_EXT_CTRL_4A, 645 (ctrl4a & ~(RTC_CTRL_4A_KF))); 646 647 648 /* 649 * Sleep 500ms before re-enabling kickstarts. This allows 650 * adequate time to avoid reading signal jitter as additional 651 * button presses. 652 */ 653 msleep(500); 654 rtc->write(rtc, RTC_EXT_CTRL_4B, 655 (rtc->read(rtc, RTC_EXT_CTRL_4B) | 656 RTC_CTRL_4B_KSE)); 657 658 /* Call the platform pre-poweroff function. Else, shutdown. */ 659 if (rtc->prepare_poweroff != NULL) 660 rtc->prepare_poweroff(); 661 else 662 ds1685_rtc_poweroff(pdev); 663 } 664 665 /* 666 * Check for a wake-up interrupt. With Vcc applied, this is 667 * essentially a second alarm interrupt, except it takes into 668 * account the 'date' register in bank1 in addition to the 669 * standard three alarm registers. 670 */ 671 if ((ctrl4b & RTC_CTRL_4B_WIE) && (ctrl4a & RTC_CTRL_4A_WF)) { 672 rtc->write(rtc, RTC_EXT_CTRL_4A, 673 (ctrl4a & ~(RTC_CTRL_4A_WF))); 674 675 /* Call the platform wake_alarm function if defined. */ 676 if (rtc->wake_alarm != NULL) 677 rtc->wake_alarm(); 678 else 679 dev_warn(&pdev->dev, 680 "Wake Alarm IRQ just occurred!\n"); 681 } 682 683 /* 684 * Check for a ram-clear interrupt. This happens if RIE=1 and RF=0 685 * when RCE=1 in 4B. This clears all NVRAM bytes in bank0 by setting 686 * each byte to a logic 1. This has no effect on any extended 687 * NV-SRAM that might be present, nor on the time/calendar/alarm 688 * registers. After a ram-clear is completed, there is a minimum 689 * recovery time of ~150ms in which all reads/writes are locked out. 690 * NOTE: A ram-clear can still occur if RCE=1 and RIE=0. We cannot 691 * catch this scenario. 692 */ 693 if ((ctrl4b & RTC_CTRL_4B_RIE) && (ctrl4a & RTC_CTRL_4A_RF)) { 694 rtc->write(rtc, RTC_EXT_CTRL_4A, 695 (ctrl4a & ~(RTC_CTRL_4A_RF))); 696 msleep(150); 697 698 /* Call the platform post_ram_clear function if defined. */ 699 if (rtc->post_ram_clear != NULL) 700 rtc->post_ram_clear(); 701 else 702 dev_warn(&pdev->dev, 703 "RAM-Clear IRQ just occurred!\n"); 704 } 705 ds1685_rtc_switch_to_bank0(rtc); 706 707 mutex_unlock(rtc_mutex); 708 } 709 /* ----------------------------------------------------------------------- */ 710 711 712 /* ----------------------------------------------------------------------- */ 713 /* ProcFS interface */ 714 715 #ifdef CONFIG_PROC_FS 716 #define NUM_REGS 6 /* Num of control registers. */ 717 #define NUM_BITS 8 /* Num bits per register. */ 718 #define NUM_SPACES 4 /* Num spaces between each bit. */ 719 720 /* 721 * Periodic Interrupt Rates. 722 */ 723 static const char *ds1685_rtc_pirq_rate[16] = { 724 "none", "3.90625ms", "7.8125ms", "0.122070ms", "0.244141ms", 725 "0.488281ms", "0.9765625ms", "1.953125ms", "3.90625ms", "7.8125ms", 726 "15.625ms", "31.25ms", "62.5ms", "125ms", "250ms", "500ms" 727 }; 728 729 /* 730 * Square-Wave Output Frequencies. 731 */ 732 static const char *ds1685_rtc_sqw_freq[16] = { 733 "none", "256Hz", "128Hz", "8192Hz", "4096Hz", "2048Hz", "1024Hz", 734 "512Hz", "256Hz", "128Hz", "64Hz", "32Hz", "16Hz", "8Hz", "4Hz", "2Hz" 735 }; 736 737 /** 738 * ds1685_rtc_proc - procfs access function. 739 * @dev: pointer to device structure. 740 * @seq: pointer to seq_file structure. 741 */ 742 static int 743 ds1685_rtc_proc(struct device *dev, struct seq_file *seq) 744 { 745 struct ds1685_priv *rtc = dev_get_drvdata(dev); 746 u8 ctrla, ctrlb, ctrlc, ctrld, ctrl4a, ctrl4b, ssn[8]; 747 char *model; 748 749 /* Read all the relevant data from the control registers. */ 750 ds1685_rtc_switch_to_bank1(rtc); 751 ds1685_rtc_get_ssn(rtc, ssn); 752 ctrla = rtc->read(rtc, RTC_CTRL_A); 753 ctrlb = rtc->read(rtc, RTC_CTRL_B); 754 ctrlc = rtc->read(rtc, RTC_CTRL_C); 755 ctrld = rtc->read(rtc, RTC_CTRL_D); 756 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); 757 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); 758 ds1685_rtc_switch_to_bank0(rtc); 759 760 /* Determine the RTC model. */ 761 switch (ssn[0]) { 762 case RTC_MODEL_DS1685: 763 model = "DS1685/DS1687\0"; 764 break; 765 case RTC_MODEL_DS1689: 766 model = "DS1689/DS1693\0"; 767 break; 768 case RTC_MODEL_DS17285: 769 model = "DS17285/DS17287\0"; 770 break; 771 case RTC_MODEL_DS17485: 772 model = "DS17485/DS17487\0"; 773 break; 774 case RTC_MODEL_DS17885: 775 model = "DS17885/DS17887\0"; 776 break; 777 default: 778 model = "Unknown\0"; 779 break; 780 } 781 782 /* Print out the information. */ 783 seq_printf(seq, 784 "Model\t\t: %s\n" 785 "Oscillator\t: %s\n" 786 "12/24hr\t\t: %s\n" 787 "DST\t\t: %s\n" 788 "Data mode\t: %s\n" 789 "Battery\t\t: %s\n" 790 "Aux batt\t: %s\n" 791 "Update IRQ\t: %s\n" 792 "Periodic IRQ\t: %s\n" 793 "Periodic Rate\t: %s\n" 794 "SQW Freq\t: %s\n" 795 "Serial #\t: %8phC\n", 796 model, 797 ((ctrla & RTC_CTRL_A_DV1) ? "enabled" : "disabled"), 798 ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"), 799 ((ctrlb & RTC_CTRL_B_DSE) ? "enabled" : "disabled"), 800 ((ctrlb & RTC_CTRL_B_DM) ? "binary" : "BCD"), 801 ((ctrld & RTC_CTRL_D_VRT) ? "ok" : "exhausted or n/a"), 802 ((ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "exhausted or n/a"), 803 ((ctrlb & RTC_CTRL_B_UIE) ? "yes" : "no"), 804 ((ctrlb & RTC_CTRL_B_PIE) ? "yes" : "no"), 805 (!(ctrl4b & RTC_CTRL_4B_E32K) ? 806 ds1685_rtc_pirq_rate[(ctrla & RTC_CTRL_A_RS_MASK)] : "none"), 807 (!((ctrl4b & RTC_CTRL_4B_E32K)) ? 808 ds1685_rtc_sqw_freq[(ctrla & RTC_CTRL_A_RS_MASK)] : "32768Hz"), 809 ssn); 810 return 0; 811 } 812 #else 813 #define ds1685_rtc_proc NULL 814 #endif /* CONFIG_PROC_FS */ 815 /* ----------------------------------------------------------------------- */ 816 817 818 /* ----------------------------------------------------------------------- */ 819 /* RTC Class operations */ 820 821 static const struct rtc_class_ops 822 ds1685_rtc_ops = { 823 .proc = ds1685_rtc_proc, 824 .read_time = ds1685_rtc_read_time, 825 .set_time = ds1685_rtc_set_time, 826 .read_alarm = ds1685_rtc_read_alarm, 827 .set_alarm = ds1685_rtc_set_alarm, 828 .alarm_irq_enable = ds1685_rtc_alarm_irq_enable, 829 }; 830 /* ----------------------------------------------------------------------- */ 831 832 static int ds1685_nvram_read(void *priv, unsigned int pos, void *val, 833 size_t size) 834 { 835 struct ds1685_priv *rtc = priv; 836 ssize_t count; 837 unsigned long flags = 0; 838 u8 *buf = val; 839 840 spin_lock_irqsave(&rtc->lock, flags); 841 ds1685_rtc_switch_to_bank0(rtc); 842 843 /* Read NVRAM in time and bank0 registers. */ 844 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0; 845 count++, size--) { 846 if (count < NVRAM_SZ_TIME) 847 *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++)); 848 else 849 *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++)); 850 } 851 852 #ifndef CONFIG_RTC_DRV_DS1689 853 if (size > 0) { 854 ds1685_rtc_switch_to_bank1(rtc); 855 856 #ifndef CONFIG_RTC_DRV_DS1685 857 /* Enable burst-mode on DS17x85/DS17x87 */ 858 rtc->write(rtc, RTC_EXT_CTRL_4A, 859 (rtc->read(rtc, RTC_EXT_CTRL_4A) | 860 RTC_CTRL_4A_BME)); 861 862 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start 863 * reading with burst-mode */ 864 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB, 865 (pos - NVRAM_TOTAL_SZ_BANK0)); 866 #endif 867 868 /* Read NVRAM in bank1 registers. */ 869 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ; 870 count++, size--) { 871 #ifdef CONFIG_RTC_DRV_DS1685 872 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR 873 * before each read. */ 874 rtc->write(rtc, RTC_BANK1_RAM_ADDR, 875 (pos - NVRAM_TOTAL_SZ_BANK0)); 876 #endif 877 *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT); 878 pos++; 879 } 880 881 #ifndef CONFIG_RTC_DRV_DS1685 882 /* Disable burst-mode on DS17x85/DS17x87 */ 883 rtc->write(rtc, RTC_EXT_CTRL_4A, 884 (rtc->read(rtc, RTC_EXT_CTRL_4A) & 885 ~(RTC_CTRL_4A_BME))); 886 #endif 887 ds1685_rtc_switch_to_bank0(rtc); 888 } 889 #endif /* !CONFIG_RTC_DRV_DS1689 */ 890 spin_unlock_irqrestore(&rtc->lock, flags); 891 892 return 0; 893 } 894 895 static int ds1685_nvram_write(void *priv, unsigned int pos, void *val, 896 size_t size) 897 { 898 struct ds1685_priv *rtc = priv; 899 ssize_t count; 900 unsigned long flags = 0; 901 u8 *buf = val; 902 903 spin_lock_irqsave(&rtc->lock, flags); 904 ds1685_rtc_switch_to_bank0(rtc); 905 906 /* Write NVRAM in time and bank0 registers. */ 907 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0; 908 count++, size--) 909 if (count < NVRAM_SZ_TIME) 910 rtc->write(rtc, (NVRAM_TIME_BASE + pos++), 911 *buf++); 912 else 913 rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++); 914 915 #ifndef CONFIG_RTC_DRV_DS1689 916 if (size > 0) { 917 ds1685_rtc_switch_to_bank1(rtc); 918 919 #ifndef CONFIG_RTC_DRV_DS1685 920 /* Enable burst-mode on DS17x85/DS17x87 */ 921 rtc->write(rtc, RTC_EXT_CTRL_4A, 922 (rtc->read(rtc, RTC_EXT_CTRL_4A) | 923 RTC_CTRL_4A_BME)); 924 925 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start 926 * writing with burst-mode */ 927 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB, 928 (pos - NVRAM_TOTAL_SZ_BANK0)); 929 #endif 930 931 /* Write NVRAM in bank1 registers. */ 932 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ; 933 count++, size--) { 934 #ifdef CONFIG_RTC_DRV_DS1685 935 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR 936 * before each read. */ 937 rtc->write(rtc, RTC_BANK1_RAM_ADDR, 938 (pos - NVRAM_TOTAL_SZ_BANK0)); 939 #endif 940 rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++); 941 pos++; 942 } 943 944 #ifndef CONFIG_RTC_DRV_DS1685 945 /* Disable burst-mode on DS17x85/DS17x87 */ 946 rtc->write(rtc, RTC_EXT_CTRL_4A, 947 (rtc->read(rtc, RTC_EXT_CTRL_4A) & 948 ~(RTC_CTRL_4A_BME))); 949 #endif 950 ds1685_rtc_switch_to_bank0(rtc); 951 } 952 #endif /* !CONFIG_RTC_DRV_DS1689 */ 953 spin_unlock_irqrestore(&rtc->lock, flags); 954 955 return 0; 956 } 957 958 /* ----------------------------------------------------------------------- */ 959 /* SysFS interface */ 960 961 /** 962 * ds1685_rtc_sysfs_battery_show - sysfs file for main battery status. 963 * @dev: pointer to device structure. 964 * @attr: pointer to device_attribute structure. 965 * @buf: pointer to char array to hold the output. 966 */ 967 static ssize_t 968 ds1685_rtc_sysfs_battery_show(struct device *dev, 969 struct device_attribute *attr, char *buf) 970 { 971 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); 972 u8 ctrld; 973 974 ctrld = rtc->read(rtc, RTC_CTRL_D); 975 976 return sprintf(buf, "%s\n", 977 (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A"); 978 } 979 static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL); 980 981 /** 982 * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status. 983 * @dev: pointer to device structure. 984 * @attr: pointer to device_attribute structure. 985 * @buf: pointer to char array to hold the output. 986 */ 987 static ssize_t 988 ds1685_rtc_sysfs_auxbatt_show(struct device *dev, 989 struct device_attribute *attr, char *buf) 990 { 991 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); 992 u8 ctrl4a; 993 994 ds1685_rtc_switch_to_bank1(rtc); 995 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); 996 ds1685_rtc_switch_to_bank0(rtc); 997 998 return sprintf(buf, "%s\n", 999 (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A"); 1000 } 1001 static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL); 1002 1003 /** 1004 * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number. 1005 * @dev: pointer to device structure. 1006 * @attr: pointer to device_attribute structure. 1007 * @buf: pointer to char array to hold the output. 1008 */ 1009 static ssize_t 1010 ds1685_rtc_sysfs_serial_show(struct device *dev, 1011 struct device_attribute *attr, char *buf) 1012 { 1013 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); 1014 u8 ssn[8]; 1015 1016 ds1685_rtc_switch_to_bank1(rtc); 1017 ds1685_rtc_get_ssn(rtc, ssn); 1018 ds1685_rtc_switch_to_bank0(rtc); 1019 1020 return sprintf(buf, "%8phC\n", ssn); 1021 } 1022 static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL); 1023 1024 /** 1025 * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features. 1026 */ 1027 static struct attribute* 1028 ds1685_rtc_sysfs_misc_attrs[] = { 1029 &dev_attr_battery.attr, 1030 &dev_attr_auxbatt.attr, 1031 &dev_attr_serial.attr, 1032 NULL, 1033 }; 1034 1035 /** 1036 * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features. 1037 */ 1038 static const struct attribute_group 1039 ds1685_rtc_sysfs_misc_grp = { 1040 .name = "misc", 1041 .attrs = ds1685_rtc_sysfs_misc_attrs, 1042 }; 1043 1044 /* ----------------------------------------------------------------------- */ 1045 /* Driver Probe/Removal */ 1046 1047 /** 1048 * ds1685_rtc_probe - initializes rtc driver. 1049 * @pdev: pointer to platform_device structure. 1050 */ 1051 static int 1052 ds1685_rtc_probe(struct platform_device *pdev) 1053 { 1054 struct rtc_device *rtc_dev; 1055 struct resource *res; 1056 struct ds1685_priv *rtc; 1057 struct ds1685_rtc_platform_data *pdata; 1058 u8 ctrla, ctrlb, hours; 1059 unsigned char am_pm; 1060 int ret = 0; 1061 struct nvmem_config nvmem_cfg = { 1062 .name = "ds1685_nvram", 1063 .size = NVRAM_TOTAL_SZ, 1064 .reg_read = ds1685_nvram_read, 1065 .reg_write = ds1685_nvram_write, 1066 }; 1067 1068 /* Get the platform data. */ 1069 pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data; 1070 if (!pdata) 1071 return -ENODEV; 1072 1073 /* Allocate memory for the rtc device. */ 1074 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); 1075 if (!rtc) 1076 return -ENOMEM; 1077 1078 /* 1079 * Allocate/setup any IORESOURCE_MEM resources, if required. Not all 1080 * platforms put the RTC in an easy-access place. Like the SGI Octane, 1081 * which attaches the RTC to a "ByteBus", hooked to a SuperIO chip 1082 * that sits behind the IOC3 PCI metadevice. 1083 */ 1084 if (pdata->alloc_io_resources) { 1085 /* Get the platform resources. */ 1086 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1087 if (!res) 1088 return -ENXIO; 1089 rtc->size = resource_size(res); 1090 1091 /* Request a memory region. */ 1092 /* XXX: mmio-only for now. */ 1093 if (!devm_request_mem_region(&pdev->dev, res->start, rtc->size, 1094 pdev->name)) 1095 return -EBUSY; 1096 1097 /* 1098 * Set the base address for the rtc, and ioremap its 1099 * registers. 1100 */ 1101 rtc->baseaddr = res->start; 1102 rtc->regs = devm_ioremap(&pdev->dev, res->start, rtc->size); 1103 if (!rtc->regs) 1104 return -ENOMEM; 1105 } 1106 rtc->alloc_io_resources = pdata->alloc_io_resources; 1107 1108 /* Get the register step size. */ 1109 if (pdata->regstep > 0) 1110 rtc->regstep = pdata->regstep; 1111 else 1112 rtc->regstep = 1; 1113 1114 /* Platform read function, else default if mmio setup */ 1115 if (pdata->plat_read) 1116 rtc->read = pdata->plat_read; 1117 else 1118 if (pdata->alloc_io_resources) 1119 rtc->read = ds1685_read; 1120 else 1121 return -ENXIO; 1122 1123 /* Platform write function, else default if mmio setup */ 1124 if (pdata->plat_write) 1125 rtc->write = pdata->plat_write; 1126 else 1127 if (pdata->alloc_io_resources) 1128 rtc->write = ds1685_write; 1129 else 1130 return -ENXIO; 1131 1132 /* Platform pre-shutdown function, if defined. */ 1133 if (pdata->plat_prepare_poweroff) 1134 rtc->prepare_poweroff = pdata->plat_prepare_poweroff; 1135 1136 /* Platform wake_alarm function, if defined. */ 1137 if (pdata->plat_wake_alarm) 1138 rtc->wake_alarm = pdata->plat_wake_alarm; 1139 1140 /* Platform post_ram_clear function, if defined. */ 1141 if (pdata->plat_post_ram_clear) 1142 rtc->post_ram_clear = pdata->plat_post_ram_clear; 1143 1144 /* Init the spinlock, workqueue, & set the driver data. */ 1145 spin_lock_init(&rtc->lock); 1146 INIT_WORK(&rtc->work, ds1685_rtc_work_queue); 1147 platform_set_drvdata(pdev, rtc); 1148 1149 /* Turn the oscillator on if is not already on (DV1 = 1). */ 1150 ctrla = rtc->read(rtc, RTC_CTRL_A); 1151 if (!(ctrla & RTC_CTRL_A_DV1)) 1152 ctrla |= RTC_CTRL_A_DV1; 1153 1154 /* Enable the countdown chain (DV2 = 0) */ 1155 ctrla &= ~(RTC_CTRL_A_DV2); 1156 1157 /* Clear RS3-RS0 in Control A. */ 1158 ctrla &= ~(RTC_CTRL_A_RS_MASK); 1159 1160 /* 1161 * All done with Control A. Switch to Bank 1 for the remainder of 1162 * the RTC setup so we have access to the extended functions. 1163 */ 1164 ctrla |= RTC_CTRL_A_DV0; 1165 rtc->write(rtc, RTC_CTRL_A, ctrla); 1166 1167 /* Default to 32768kHz output. */ 1168 rtc->write(rtc, RTC_EXT_CTRL_4B, 1169 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K)); 1170 1171 /* Set the SET bit in Control B so we can do some housekeeping. */ 1172 rtc->write(rtc, RTC_CTRL_B, 1173 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET)); 1174 1175 /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */ 1176 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR) 1177 cpu_relax(); 1178 1179 /* 1180 * If the platform supports BCD mode, then set DM=0 in Control B. 1181 * Otherwise, set DM=1 for BIN mode. 1182 */ 1183 ctrlb = rtc->read(rtc, RTC_CTRL_B); 1184 if (pdata->bcd_mode) 1185 ctrlb &= ~(RTC_CTRL_B_DM); 1186 else 1187 ctrlb |= RTC_CTRL_B_DM; 1188 rtc->bcd_mode = pdata->bcd_mode; 1189 1190 /* 1191 * Disable Daylight Savings Time (DSE = 0). 1192 * The RTC has hardcoded timezone information that is rendered 1193 * obselete. We'll let the OS deal with DST settings instead. 1194 */ 1195 if (ctrlb & RTC_CTRL_B_DSE) 1196 ctrlb &= ~(RTC_CTRL_B_DSE); 1197 1198 /* Force 24-hour mode (2412 = 1). */ 1199 if (!(ctrlb & RTC_CTRL_B_2412)) { 1200 /* Reinitialize the time hours. */ 1201 hours = rtc->read(rtc, RTC_HRS); 1202 am_pm = hours & RTC_HRS_AMPM_MASK; 1203 hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK, 1204 RTC_HRS_12_BIN_MASK); 1205 hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours)); 1206 1207 /* Enable 24-hour mode. */ 1208 ctrlb |= RTC_CTRL_B_2412; 1209 1210 /* Write back to Control B, including DM & DSE bits. */ 1211 rtc->write(rtc, RTC_CTRL_B, ctrlb); 1212 1213 /* Write the time hours back. */ 1214 rtc->write(rtc, RTC_HRS, 1215 ds1685_rtc_bin2bcd(rtc, hours, 1216 RTC_HRS_24_BIN_MASK, 1217 RTC_HRS_24_BCD_MASK)); 1218 1219 /* Reinitialize the alarm hours. */ 1220 hours = rtc->read(rtc, RTC_HRS_ALARM); 1221 am_pm = hours & RTC_HRS_AMPM_MASK; 1222 hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK, 1223 RTC_HRS_12_BIN_MASK); 1224 hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours)); 1225 1226 /* Write the alarm hours back. */ 1227 rtc->write(rtc, RTC_HRS_ALARM, 1228 ds1685_rtc_bin2bcd(rtc, hours, 1229 RTC_HRS_24_BIN_MASK, 1230 RTC_HRS_24_BCD_MASK)); 1231 } else { 1232 /* 24-hour mode is already set, so write Control B back. */ 1233 rtc->write(rtc, RTC_CTRL_B, ctrlb); 1234 } 1235 1236 /* Unset the SET bit in Control B so the RTC can update. */ 1237 rtc->write(rtc, RTC_CTRL_B, 1238 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET))); 1239 1240 /* Check the main battery. */ 1241 if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT)) 1242 dev_warn(&pdev->dev, 1243 "Main battery is exhausted! RTC may be invalid!\n"); 1244 1245 /* Check the auxillary battery. It is optional. */ 1246 if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2)) 1247 dev_warn(&pdev->dev, 1248 "Aux battery is exhausted or not available.\n"); 1249 1250 /* Read Ctrl B and clear PIE/AIE/UIE. */ 1251 rtc->write(rtc, RTC_CTRL_B, 1252 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK))); 1253 1254 /* Reading Ctrl C auto-clears PF/AF/UF. */ 1255 rtc->read(rtc, RTC_CTRL_C); 1256 1257 /* Read Ctrl 4B and clear RIE/WIE/KSE. */ 1258 rtc->write(rtc, RTC_EXT_CTRL_4B, 1259 (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK))); 1260 1261 /* Clear RF/WF/KF in Ctrl 4A. */ 1262 rtc->write(rtc, RTC_EXT_CTRL_4A, 1263 (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK))); 1264 1265 /* 1266 * Re-enable KSE to handle power button events. We do not enable 1267 * WIE or RIE by default. 1268 */ 1269 rtc->write(rtc, RTC_EXT_CTRL_4B, 1270 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE)); 1271 1272 rtc_dev = devm_rtc_allocate_device(&pdev->dev); 1273 if (IS_ERR(rtc_dev)) 1274 return PTR_ERR(rtc_dev); 1275 1276 rtc_dev->ops = &ds1685_rtc_ops; 1277 1278 /* Century bit is useless because leap year fails in 1900 and 2100 */ 1279 rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000; 1280 rtc_dev->range_max = RTC_TIMESTAMP_END_2099; 1281 1282 /* Maximum periodic rate is 8192Hz (0.122070ms). */ 1283 rtc_dev->max_user_freq = RTC_MAX_USER_FREQ; 1284 1285 /* See if the platform doesn't support UIE. */ 1286 if (pdata->uie_unsupported) 1287 rtc_dev->uie_unsupported = 1; 1288 rtc->uie_unsupported = pdata->uie_unsupported; 1289 1290 rtc->dev = rtc_dev; 1291 1292 /* 1293 * Fetch the IRQ and setup the interrupt handler. 1294 * 1295 * Not all platforms have the IRQF pin tied to something. If not, the 1296 * RTC will still set the *IE / *F flags and raise IRQF in ctrlc, but 1297 * there won't be an automatic way of notifying the kernel about it, 1298 * unless ctrlc is explicitly polled. 1299 */ 1300 if (!pdata->no_irq) { 1301 ret = platform_get_irq(pdev, 0); 1302 if (ret > 0) { 1303 rtc->irq_num = ret; 1304 1305 /* Request an IRQ. */ 1306 ret = devm_request_irq(&pdev->dev, rtc->irq_num, 1307 ds1685_rtc_irq_handler, 1308 IRQF_SHARED, pdev->name, pdev); 1309 1310 /* Check to see if something came back. */ 1311 if (unlikely(ret)) { 1312 dev_warn(&pdev->dev, 1313 "RTC interrupt not available\n"); 1314 rtc->irq_num = 0; 1315 } 1316 } else 1317 return ret; 1318 } 1319 rtc->no_irq = pdata->no_irq; 1320 1321 /* Setup complete. */ 1322 ds1685_rtc_switch_to_bank0(rtc); 1323 1324 ret = rtc_add_group(rtc_dev, &ds1685_rtc_sysfs_misc_grp); 1325 if (ret) 1326 return ret; 1327 1328 rtc_dev->nvram_old_abi = true; 1329 nvmem_cfg.priv = rtc; 1330 ret = rtc_nvmem_register(rtc_dev, &nvmem_cfg); 1331 if (ret) 1332 return ret; 1333 1334 return rtc_register_device(rtc_dev); 1335 } 1336 1337 /** 1338 * ds1685_rtc_remove - removes rtc driver. 1339 * @pdev: pointer to platform_device structure. 1340 */ 1341 static int 1342 ds1685_rtc_remove(struct platform_device *pdev) 1343 { 1344 struct ds1685_priv *rtc = platform_get_drvdata(pdev); 1345 1346 /* Read Ctrl B and clear PIE/AIE/UIE. */ 1347 rtc->write(rtc, RTC_CTRL_B, 1348 (rtc->read(rtc, RTC_CTRL_B) & 1349 ~(RTC_CTRL_B_PAU_MASK))); 1350 1351 /* Reading Ctrl C auto-clears PF/AF/UF. */ 1352 rtc->read(rtc, RTC_CTRL_C); 1353 1354 /* Read Ctrl 4B and clear RIE/WIE/KSE. */ 1355 rtc->write(rtc, RTC_EXT_CTRL_4B, 1356 (rtc->read(rtc, RTC_EXT_CTRL_4B) & 1357 ~(RTC_CTRL_4B_RWK_MASK))); 1358 1359 /* Manually clear RF/WF/KF in Ctrl 4A. */ 1360 rtc->write(rtc, RTC_EXT_CTRL_4A, 1361 (rtc->read(rtc, RTC_EXT_CTRL_4A) & 1362 ~(RTC_CTRL_4A_RWK_MASK))); 1363 1364 cancel_work_sync(&rtc->work); 1365 1366 return 0; 1367 } 1368 1369 /** 1370 * ds1685_rtc_driver - rtc driver properties. 1371 */ 1372 static struct platform_driver ds1685_rtc_driver = { 1373 .driver = { 1374 .name = "rtc-ds1685", 1375 }, 1376 .probe = ds1685_rtc_probe, 1377 .remove = ds1685_rtc_remove, 1378 }; 1379 module_platform_driver(ds1685_rtc_driver); 1380 /* ----------------------------------------------------------------------- */ 1381 1382 1383 /* ----------------------------------------------------------------------- */ 1384 /* Poweroff function */ 1385 1386 /** 1387 * ds1685_rtc_poweroff - uses the RTC chip to power the system off. 1388 * @pdev: pointer to platform_device structure. 1389 */ 1390 void __noreturn 1391 ds1685_rtc_poweroff(struct platform_device *pdev) 1392 { 1393 u8 ctrla, ctrl4a, ctrl4b; 1394 struct ds1685_priv *rtc; 1395 1396 /* Check for valid RTC data, else, spin forever. */ 1397 if (unlikely(!pdev)) { 1398 pr_emerg("platform device data not available, spinning forever ...\n"); 1399 while(1); 1400 unreachable(); 1401 } else { 1402 /* Get the rtc data. */ 1403 rtc = platform_get_drvdata(pdev); 1404 1405 /* 1406 * Disable our IRQ. We're powering down, so we're not 1407 * going to worry about cleaning up. Most of that should 1408 * have been taken care of by the shutdown scripts and this 1409 * is the final function call. 1410 */ 1411 if (!rtc->no_irq) 1412 disable_irq_nosync(rtc->irq_num); 1413 1414 /* Oscillator must be on and the countdown chain enabled. */ 1415 ctrla = rtc->read(rtc, RTC_CTRL_A); 1416 ctrla |= RTC_CTRL_A_DV1; 1417 ctrla &= ~(RTC_CTRL_A_DV2); 1418 rtc->write(rtc, RTC_CTRL_A, ctrla); 1419 1420 /* 1421 * Read Control 4A and check the status of the auxillary 1422 * battery. This must be present and working (VRT2 = 1) 1423 * for wakeup and kickstart functionality to be useful. 1424 */ 1425 ds1685_rtc_switch_to_bank1(rtc); 1426 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); 1427 if (ctrl4a & RTC_CTRL_4A_VRT2) { 1428 /* Clear all of the interrupt flags on Control 4A. */ 1429 ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK); 1430 rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a); 1431 1432 /* 1433 * The auxillary battery is present and working. 1434 * Enable extended functions (ABE=1), enable 1435 * wake-up (WIE=1), and enable kickstart (KSE=1) 1436 * in Control 4B. 1437 */ 1438 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); 1439 ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE | 1440 RTC_CTRL_4B_KSE); 1441 rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b); 1442 } 1443 1444 /* Set PAB to 1 in Control 4A to power the system down. */ 1445 dev_warn(&pdev->dev, "Powerdown.\n"); 1446 msleep(20); 1447 rtc->write(rtc, RTC_EXT_CTRL_4A, 1448 (ctrl4a | RTC_CTRL_4A_PAB)); 1449 1450 /* Spin ... we do not switch back to bank0. */ 1451 while(1); 1452 unreachable(); 1453 } 1454 } 1455 EXPORT_SYMBOL(ds1685_rtc_poweroff); 1456 /* ----------------------------------------------------------------------- */ 1457 1458 1459 MODULE_AUTHOR("Joshua Kinard <[email protected]>"); 1460 MODULE_AUTHOR("Matthias Fuchs <[email protected]>"); 1461 MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver"); 1462 MODULE_LICENSE("GPL"); 1463 MODULE_ALIAS("platform:rtc-ds1685"); 1464