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 <kumba@gentoo.org>. 6 * Copyright (C) 2009 Matthias Fuchs <matthias.fuchs@esd-electronics.com>. 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_begin_ctrl_access - prepare the rtc for ctrl access. 196 * @rtc: pointer to the ds1685 rtc structure. 197 * @flags: irq flags variable for spin_lock_irqsave. 198 * 199 * This takes several steps to prepare the rtc for access to read just the 200 * control registers: 201 * - Sets a spinlock on the rtc IRQ. 202 * - Switches the rtc to bank 1. This allows access to the two extended 203 * control registers. 204 * 205 * Only use this where you are certain another lock will not be held. 206 */ 207 static inline void 208 ds1685_rtc_begin_ctrl_access(struct ds1685_priv *rtc, unsigned long *flags) 209 { 210 spin_lock_irqsave(&rtc->lock, *flags); 211 ds1685_rtc_switch_to_bank1(rtc); 212 } 213 214 /** 215 * ds1685_rtc_end_ctrl_access - end ctrl access on the rtc. 216 * @rtc: pointer to the ds1685 rtc structure. 217 * @flags: irq flags variable for spin_unlock_irqrestore. 218 * 219 * This ends what was started by ds1685_rtc_begin_ctrl_access: 220 * - Switches the rtc back to bank 0. 221 * - Unsets the spinlock on the rtc IRQ. 222 */ 223 static inline void 224 ds1685_rtc_end_ctrl_access(struct ds1685_priv *rtc, unsigned long flags) 225 { 226 ds1685_rtc_switch_to_bank0(rtc); 227 spin_unlock_irqrestore(&rtc->lock, flags); 228 } 229 230 /** 231 * ds1685_rtc_get_ssn - retrieve the silicon serial number. 232 * @rtc: pointer to the ds1685 rtc structure. 233 * @ssn: u8 array to hold the bits of the silicon serial number. 234 * 235 * This number starts at 0x40, and is 8-bytes long, ending at 0x47. The 236 * first byte is the model number, the next six bytes are the serial number 237 * digits, and the final byte is a CRC check byte. Together, they form the 238 * silicon serial number. 239 * 240 * These values are stored in bank1, so ds1685_rtc_switch_to_bank1 must be 241 * called first before calling this function, else data will be read out of 242 * the bank0 NVRAM. Be sure to call ds1685_rtc_switch_to_bank0 when done. 243 */ 244 static inline void 245 ds1685_rtc_get_ssn(struct ds1685_priv *rtc, u8 *ssn) 246 { 247 ssn[0] = rtc->read(rtc, RTC_BANK1_SSN_MODEL); 248 ssn[1] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_1); 249 ssn[2] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_2); 250 ssn[3] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_3); 251 ssn[4] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_4); 252 ssn[5] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_5); 253 ssn[6] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_6); 254 ssn[7] = rtc->read(rtc, RTC_BANK1_SSN_CRC); 255 } 256 /* ----------------------------------------------------------------------- */ 257 258 259 /* ----------------------------------------------------------------------- */ 260 /* Read/Set Time & Alarm functions */ 261 262 /** 263 * ds1685_rtc_read_time - reads the time registers. 264 * @dev: pointer to device structure. 265 * @tm: pointer to rtc_time structure. 266 */ 267 static int 268 ds1685_rtc_read_time(struct device *dev, struct rtc_time *tm) 269 { 270 struct ds1685_priv *rtc = dev_get_drvdata(dev); 271 u8 ctrlb, century; 272 u8 seconds, minutes, hours, wday, mday, month, years; 273 274 /* Fetch the time info from the RTC registers. */ 275 ds1685_rtc_begin_data_access(rtc); 276 seconds = rtc->read(rtc, RTC_SECS); 277 minutes = rtc->read(rtc, RTC_MINS); 278 hours = rtc->read(rtc, RTC_HRS); 279 wday = rtc->read(rtc, RTC_WDAY); 280 mday = rtc->read(rtc, RTC_MDAY); 281 month = rtc->read(rtc, RTC_MONTH); 282 years = rtc->read(rtc, RTC_YEAR); 283 century = rtc->read(rtc, RTC_CENTURY); 284 ctrlb = rtc->read(rtc, RTC_CTRL_B); 285 ds1685_rtc_end_data_access(rtc); 286 287 /* bcd2bin if needed, perform fixups, and store to rtc_time. */ 288 years = ds1685_rtc_bcd2bin(rtc, years, RTC_YEAR_BCD_MASK, 289 RTC_YEAR_BIN_MASK); 290 century = ds1685_rtc_bcd2bin(rtc, century, RTC_CENTURY_MASK, 291 RTC_CENTURY_MASK); 292 tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK, 293 RTC_SECS_BIN_MASK); 294 tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK, 295 RTC_MINS_BIN_MASK); 296 tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK, 297 RTC_HRS_24_BIN_MASK); 298 tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK, 299 RTC_WDAY_MASK) - 1); 300 tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK, 301 RTC_MDAY_BIN_MASK); 302 tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK, 303 RTC_MONTH_BIN_MASK) - 1); 304 tm->tm_year = ((years + (century * 100)) - 1900); 305 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year); 306 tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */ 307 308 return 0; 309 } 310 311 /** 312 * ds1685_rtc_set_time - sets the time registers. 313 * @dev: pointer to device structure. 314 * @tm: pointer to rtc_time structure. 315 */ 316 static int 317 ds1685_rtc_set_time(struct device *dev, struct rtc_time *tm) 318 { 319 struct ds1685_priv *rtc = dev_get_drvdata(dev); 320 u8 ctrlb, seconds, minutes, hours, wday, mday, month, years, century; 321 322 /* Fetch the time info from rtc_time. */ 323 seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK, 324 RTC_SECS_BCD_MASK); 325 minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK, 326 RTC_MINS_BCD_MASK); 327 hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK, 328 RTC_HRS_24_BCD_MASK); 329 wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK, 330 RTC_WDAY_MASK); 331 mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK, 332 RTC_MDAY_BCD_MASK); 333 month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK, 334 RTC_MONTH_BCD_MASK); 335 years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100), 336 RTC_YEAR_BIN_MASK, RTC_YEAR_BCD_MASK); 337 century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100), 338 RTC_CENTURY_MASK, RTC_CENTURY_MASK); 339 340 /* 341 * Perform Sanity Checks: 342 * - Months: !> 12, Month Day != 0. 343 * - Month Day !> Max days in current month. 344 * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7. 345 */ 346 if ((tm->tm_mon > 11) || (mday == 0)) 347 return -EDOM; 348 349 if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year)) 350 return -EDOM; 351 352 if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || 353 (tm->tm_sec >= 60) || (wday > 7)) 354 return -EDOM; 355 356 /* 357 * Set the data mode to use and store the time values in the 358 * RTC registers. 359 */ 360 ds1685_rtc_begin_data_access(rtc); 361 ctrlb = rtc->read(rtc, RTC_CTRL_B); 362 if (rtc->bcd_mode) 363 ctrlb &= ~(RTC_CTRL_B_DM); 364 else 365 ctrlb |= RTC_CTRL_B_DM; 366 rtc->write(rtc, RTC_CTRL_B, ctrlb); 367 rtc->write(rtc, RTC_SECS, seconds); 368 rtc->write(rtc, RTC_MINS, minutes); 369 rtc->write(rtc, RTC_HRS, hours); 370 rtc->write(rtc, RTC_WDAY, wday); 371 rtc->write(rtc, RTC_MDAY, mday); 372 rtc->write(rtc, RTC_MONTH, month); 373 rtc->write(rtc, RTC_YEAR, years); 374 rtc->write(rtc, RTC_CENTURY, century); 375 ds1685_rtc_end_data_access(rtc); 376 377 return 0; 378 } 379 380 /** 381 * ds1685_rtc_read_alarm - reads the alarm registers. 382 * @dev: pointer to device structure. 383 * @alrm: pointer to rtc_wkalrm structure. 384 * 385 * There are three primary alarm registers: seconds, minutes, and hours. 386 * A fourth alarm register for the month date is also available in bank1 for 387 * kickstart/wakeup features. The DS1685/DS1687 manual states that a 388 * "don't care" value ranging from 0xc0 to 0xff may be written into one or 389 * more of the three alarm bytes to act as a wildcard value. The fourth 390 * byte doesn't support a "don't care" value. 391 */ 392 static int 393 ds1685_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) 394 { 395 struct ds1685_priv *rtc = dev_get_drvdata(dev); 396 u8 seconds, minutes, hours, mday, ctrlb, ctrlc; 397 int ret; 398 399 /* Fetch the alarm info from the RTC alarm registers. */ 400 ds1685_rtc_begin_data_access(rtc); 401 seconds = rtc->read(rtc, RTC_SECS_ALARM); 402 minutes = rtc->read(rtc, RTC_MINS_ALARM); 403 hours = rtc->read(rtc, RTC_HRS_ALARM); 404 mday = rtc->read(rtc, RTC_MDAY_ALARM); 405 ctrlb = rtc->read(rtc, RTC_CTRL_B); 406 ctrlc = rtc->read(rtc, RTC_CTRL_C); 407 ds1685_rtc_end_data_access(rtc); 408 409 /* Check the month date for validity. */ 410 ret = ds1685_rtc_check_mday(rtc, mday); 411 if (ret) 412 return ret; 413 414 /* 415 * Check the three alarm bytes. 416 * 417 * The Linux RTC system doesn't support the "don't care" capability 418 * of this RTC chip. We check for it anyways in case support is 419 * added in the future and only assign when we care. 420 */ 421 if (likely(seconds < 0xc0)) 422 alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, 423 RTC_SECS_BCD_MASK, 424 RTC_SECS_BIN_MASK); 425 426 if (likely(minutes < 0xc0)) 427 alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes, 428 RTC_MINS_BCD_MASK, 429 RTC_MINS_BIN_MASK); 430 431 if (likely(hours < 0xc0)) 432 alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours, 433 RTC_HRS_24_BCD_MASK, 434 RTC_HRS_24_BIN_MASK); 435 436 /* Write the data to rtc_wkalrm. */ 437 alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK, 438 RTC_MDAY_BIN_MASK); 439 alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE); 440 alrm->pending = !!(ctrlc & RTC_CTRL_C_AF); 441 442 return 0; 443 } 444 445 /** 446 * ds1685_rtc_set_alarm - sets the alarm in registers. 447 * @dev: pointer to device structure. 448 * @alrm: pointer to rtc_wkalrm structure. 449 */ 450 static int 451 ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) 452 { 453 struct ds1685_priv *rtc = dev_get_drvdata(dev); 454 u8 ctrlb, seconds, minutes, hours, mday; 455 int ret; 456 457 /* Fetch the alarm info and convert to BCD. */ 458 seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec, 459 RTC_SECS_BIN_MASK, 460 RTC_SECS_BCD_MASK); 461 minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min, 462 RTC_MINS_BIN_MASK, 463 RTC_MINS_BCD_MASK); 464 hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour, 465 RTC_HRS_24_BIN_MASK, 466 RTC_HRS_24_BCD_MASK); 467 mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday, 468 RTC_MDAY_BIN_MASK, 469 RTC_MDAY_BCD_MASK); 470 471 /* Check the month date for validity. */ 472 ret = ds1685_rtc_check_mday(rtc, mday); 473 if (ret) 474 return ret; 475 476 /* 477 * Check the three alarm bytes. 478 * 479 * The Linux RTC system doesn't support the "don't care" capability 480 * of this RTC chip because rtc_valid_tm tries to validate every 481 * field, and we only support four fields. We put the support 482 * here anyways for the future. 483 */ 484 if (unlikely(seconds >= 0xc0)) 485 seconds = 0xff; 486 487 if (unlikely(minutes >= 0xc0)) 488 minutes = 0xff; 489 490 if (unlikely(hours >= 0xc0)) 491 hours = 0xff; 492 493 alrm->time.tm_mon = -1; 494 alrm->time.tm_year = -1; 495 alrm->time.tm_wday = -1; 496 alrm->time.tm_yday = -1; 497 alrm->time.tm_isdst = -1; 498 499 /* Disable the alarm interrupt first. */ 500 ds1685_rtc_begin_data_access(rtc); 501 ctrlb = rtc->read(rtc, RTC_CTRL_B); 502 rtc->write(rtc, RTC_CTRL_B, (ctrlb & ~(RTC_CTRL_B_AIE))); 503 504 /* Read ctrlc to clear RTC_CTRL_C_AF. */ 505 rtc->read(rtc, RTC_CTRL_C); 506 507 /* 508 * Set the data mode to use and store the time values in the 509 * RTC registers. 510 */ 511 ctrlb = rtc->read(rtc, RTC_CTRL_B); 512 if (rtc->bcd_mode) 513 ctrlb &= ~(RTC_CTRL_B_DM); 514 else 515 ctrlb |= RTC_CTRL_B_DM; 516 rtc->write(rtc, RTC_CTRL_B, ctrlb); 517 rtc->write(rtc, RTC_SECS_ALARM, seconds); 518 rtc->write(rtc, RTC_MINS_ALARM, minutes); 519 rtc->write(rtc, RTC_HRS_ALARM, hours); 520 rtc->write(rtc, RTC_MDAY_ALARM, mday); 521 522 /* Re-enable the alarm if needed. */ 523 if (alrm->enabled) { 524 ctrlb = rtc->read(rtc, RTC_CTRL_B); 525 ctrlb |= RTC_CTRL_B_AIE; 526 rtc->write(rtc, RTC_CTRL_B, ctrlb); 527 } 528 529 /* Done! */ 530 ds1685_rtc_end_data_access(rtc); 531 532 return 0; 533 } 534 /* ----------------------------------------------------------------------- */ 535 536 537 /* ----------------------------------------------------------------------- */ 538 /* /dev/rtcX Interface functions */ 539 540 /** 541 * ds1685_rtc_alarm_irq_enable - replaces ioctl() RTC_AIE on/off. 542 * @dev: pointer to device structure. 543 * @enabled: flag indicating whether to enable or disable. 544 */ 545 static int 546 ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) 547 { 548 struct ds1685_priv *rtc = dev_get_drvdata(dev); 549 unsigned long flags = 0; 550 551 /* Enable/disable the Alarm IRQ-Enable flag. */ 552 spin_lock_irqsave(&rtc->lock, flags); 553 554 /* Flip the requisite interrupt-enable bit. */ 555 if (enabled) 556 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) | 557 RTC_CTRL_B_AIE)); 558 else 559 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) & 560 ~(RTC_CTRL_B_AIE))); 561 562 /* Read Control C to clear all the flag bits. */ 563 rtc->read(rtc, RTC_CTRL_C); 564 spin_unlock_irqrestore(&rtc->lock, flags); 565 566 return 0; 567 } 568 /* ----------------------------------------------------------------------- */ 569 570 571 /* ----------------------------------------------------------------------- */ 572 /* IRQ handler & workqueue. */ 573 574 /** 575 * ds1685_rtc_irq_handler - IRQ handler. 576 * @irq: IRQ number. 577 * @dev_id: platform device pointer. 578 */ 579 static irqreturn_t 580 ds1685_rtc_irq_handler(int irq, void *dev_id) 581 { 582 struct platform_device *pdev = dev_id; 583 struct ds1685_priv *rtc = platform_get_drvdata(pdev); 584 u8 ctrlb, ctrlc; 585 unsigned long events = 0; 586 u8 num_irqs = 0; 587 588 /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */ 589 if (unlikely(!rtc)) 590 return IRQ_HANDLED; 591 592 /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */ 593 spin_lock(&rtc->lock); 594 ctrlb = rtc->read(rtc, RTC_CTRL_B); 595 ctrlc = rtc->read(rtc, RTC_CTRL_C); 596 597 /* Is the IRQF bit set? */ 598 if (likely(ctrlc & RTC_CTRL_C_IRQF)) { 599 /* 600 * We need to determine if it was one of the standard 601 * events: PF, AF, or UF. If so, we handle them and 602 * update the RTC core. 603 */ 604 if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) { 605 events = RTC_IRQF; 606 607 /* Check for a periodic interrupt. */ 608 if ((ctrlb & RTC_CTRL_B_PIE) && 609 (ctrlc & RTC_CTRL_C_PF)) { 610 events |= RTC_PF; 611 num_irqs++; 612 } 613 614 /* Check for an alarm interrupt. */ 615 if ((ctrlb & RTC_CTRL_B_AIE) && 616 (ctrlc & RTC_CTRL_C_AF)) { 617 events |= RTC_AF; 618 num_irqs++; 619 } 620 621 /* Check for an update interrupt. */ 622 if ((ctrlb & RTC_CTRL_B_UIE) && 623 (ctrlc & RTC_CTRL_C_UF)) { 624 events |= RTC_UF; 625 num_irqs++; 626 } 627 628 rtc_update_irq(rtc->dev, num_irqs, events); 629 } else { 630 /* 631 * One of the "extended" interrupts was received that 632 * is not recognized by the RTC core. These need to 633 * be handled in task context as they can call other 634 * functions and the time spent in irq context needs 635 * to be minimized. Schedule them into a workqueue 636 * and inform the RTC core that the IRQs were handled. 637 */ 638 spin_unlock(&rtc->lock); 639 schedule_work(&rtc->work); 640 rtc_update_irq(rtc->dev, 0, 0); 641 return IRQ_HANDLED; 642 } 643 } 644 spin_unlock(&rtc->lock); 645 646 return events ? IRQ_HANDLED : IRQ_NONE; 647 } 648 649 /** 650 * ds1685_rtc_work_queue - work queue handler. 651 * @work: work_struct containing data to work on in task context. 652 */ 653 static void 654 ds1685_rtc_work_queue(struct work_struct *work) 655 { 656 struct ds1685_priv *rtc = container_of(work, 657 struct ds1685_priv, work); 658 struct platform_device *pdev = to_platform_device(&rtc->dev->dev); 659 struct mutex *rtc_mutex = &rtc->dev->ops_lock; 660 u8 ctrl4a, ctrl4b; 661 662 mutex_lock(rtc_mutex); 663 664 ds1685_rtc_switch_to_bank1(rtc); 665 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); 666 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); 667 668 /* 669 * Check for a kickstart interrupt. With Vcc applied, this 670 * typically means that the power button was pressed, so we 671 * begin the shutdown sequence. 672 */ 673 if ((ctrl4b & RTC_CTRL_4B_KSE) && (ctrl4a & RTC_CTRL_4A_KF)) { 674 /* Briefly disable kickstarts to debounce button presses. */ 675 rtc->write(rtc, RTC_EXT_CTRL_4B, 676 (rtc->read(rtc, RTC_EXT_CTRL_4B) & 677 ~(RTC_CTRL_4B_KSE))); 678 679 /* Clear the kickstart flag. */ 680 rtc->write(rtc, RTC_EXT_CTRL_4A, 681 (ctrl4a & ~(RTC_CTRL_4A_KF))); 682 683 684 /* 685 * Sleep 500ms before re-enabling kickstarts. This allows 686 * adequate time to avoid reading signal jitter as additional 687 * button presses. 688 */ 689 msleep(500); 690 rtc->write(rtc, RTC_EXT_CTRL_4B, 691 (rtc->read(rtc, RTC_EXT_CTRL_4B) | 692 RTC_CTRL_4B_KSE)); 693 694 /* Call the platform pre-poweroff function. Else, shutdown. */ 695 if (rtc->prepare_poweroff != NULL) 696 rtc->prepare_poweroff(); 697 else 698 ds1685_rtc_poweroff(pdev); 699 } 700 701 /* 702 * Check for a wake-up interrupt. With Vcc applied, this is 703 * essentially a second alarm interrupt, except it takes into 704 * account the 'date' register in bank1 in addition to the 705 * standard three alarm registers. 706 */ 707 if ((ctrl4b & RTC_CTRL_4B_WIE) && (ctrl4a & RTC_CTRL_4A_WF)) { 708 rtc->write(rtc, RTC_EXT_CTRL_4A, 709 (ctrl4a & ~(RTC_CTRL_4A_WF))); 710 711 /* Call the platform wake_alarm function if defined. */ 712 if (rtc->wake_alarm != NULL) 713 rtc->wake_alarm(); 714 else 715 dev_warn(&pdev->dev, 716 "Wake Alarm IRQ just occurred!\n"); 717 } 718 719 /* 720 * Check for a ram-clear interrupt. This happens if RIE=1 and RF=0 721 * when RCE=1 in 4B. This clears all NVRAM bytes in bank0 by setting 722 * each byte to a logic 1. This has no effect on any extended 723 * NV-SRAM that might be present, nor on the time/calendar/alarm 724 * registers. After a ram-clear is completed, there is a minimum 725 * recovery time of ~150ms in which all reads/writes are locked out. 726 * NOTE: A ram-clear can still occur if RCE=1 and RIE=0. We cannot 727 * catch this scenario. 728 */ 729 if ((ctrl4b & RTC_CTRL_4B_RIE) && (ctrl4a & RTC_CTRL_4A_RF)) { 730 rtc->write(rtc, RTC_EXT_CTRL_4A, 731 (ctrl4a & ~(RTC_CTRL_4A_RF))); 732 msleep(150); 733 734 /* Call the platform post_ram_clear function if defined. */ 735 if (rtc->post_ram_clear != NULL) 736 rtc->post_ram_clear(); 737 else 738 dev_warn(&pdev->dev, 739 "RAM-Clear IRQ just occurred!\n"); 740 } 741 ds1685_rtc_switch_to_bank0(rtc); 742 743 mutex_unlock(rtc_mutex); 744 } 745 /* ----------------------------------------------------------------------- */ 746 747 748 /* ----------------------------------------------------------------------- */ 749 /* ProcFS interface */ 750 751 #ifdef CONFIG_PROC_FS 752 #define NUM_REGS 6 /* Num of control registers. */ 753 #define NUM_BITS 8 /* Num bits per register. */ 754 #define NUM_SPACES 4 /* Num spaces between each bit. */ 755 756 /* 757 * Periodic Interrupt Rates. 758 */ 759 static const char *ds1685_rtc_pirq_rate[16] = { 760 "none", "3.90625ms", "7.8125ms", "0.122070ms", "0.244141ms", 761 "0.488281ms", "0.9765625ms", "1.953125ms", "3.90625ms", "7.8125ms", 762 "15.625ms", "31.25ms", "62.5ms", "125ms", "250ms", "500ms" 763 }; 764 765 /* 766 * Square-Wave Output Frequencies. 767 */ 768 static const char *ds1685_rtc_sqw_freq[16] = { 769 "none", "256Hz", "128Hz", "8192Hz", "4096Hz", "2048Hz", "1024Hz", 770 "512Hz", "256Hz", "128Hz", "64Hz", "32Hz", "16Hz", "8Hz", "4Hz", "2Hz" 771 }; 772 773 #ifdef CONFIG_RTC_DS1685_PROC_REGS 774 /** 775 * ds1685_rtc_print_regs - helper function to print register values. 776 * @hex: hex byte to convert into binary bits. 777 * @dest: destination char array. 778 * 779 * This is basically a hex->binary function, just with extra spacing between 780 * the digits. It only works on 1-byte values (8 bits). 781 */ 782 static char* 783 ds1685_rtc_print_regs(u8 hex, char *dest) 784 { 785 u32 i, j; 786 char *tmp = dest; 787 788 for (i = 0; i < NUM_BITS; i++) { 789 *tmp++ = ((hex & 0x80) != 0 ? '1' : '0'); 790 for (j = 0; j < NUM_SPACES; j++) 791 *tmp++ = ' '; 792 hex <<= 1; 793 } 794 *tmp++ = '\0'; 795 796 return dest; 797 } 798 #endif 799 800 /** 801 * ds1685_rtc_proc - procfs access function. 802 * @dev: pointer to device structure. 803 * @seq: pointer to seq_file structure. 804 */ 805 static int 806 ds1685_rtc_proc(struct device *dev, struct seq_file *seq) 807 { 808 struct platform_device *pdev = to_platform_device(dev); 809 struct ds1685_priv *rtc = platform_get_drvdata(pdev); 810 u8 ctrla, ctrlb, ctrlc, ctrld, ctrl4a, ctrl4b, ssn[8]; 811 char *model; 812 #ifdef CONFIG_RTC_DS1685_PROC_REGS 813 char bits[NUM_REGS][(NUM_BITS * NUM_SPACES) + NUM_BITS + 1]; 814 #endif 815 816 /* Read all the relevant data from the control registers. */ 817 ds1685_rtc_switch_to_bank1(rtc); 818 ds1685_rtc_get_ssn(rtc, ssn); 819 ctrla = rtc->read(rtc, RTC_CTRL_A); 820 ctrlb = rtc->read(rtc, RTC_CTRL_B); 821 ctrlc = rtc->read(rtc, RTC_CTRL_C); 822 ctrld = rtc->read(rtc, RTC_CTRL_D); 823 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); 824 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); 825 ds1685_rtc_switch_to_bank0(rtc); 826 827 /* Determine the RTC model. */ 828 switch (ssn[0]) { 829 case RTC_MODEL_DS1685: 830 model = "DS1685/DS1687\0"; 831 break; 832 case RTC_MODEL_DS1689: 833 model = "DS1689/DS1693\0"; 834 break; 835 case RTC_MODEL_DS17285: 836 model = "DS17285/DS17287\0"; 837 break; 838 case RTC_MODEL_DS17485: 839 model = "DS17485/DS17487\0"; 840 break; 841 case RTC_MODEL_DS17885: 842 model = "DS17885/DS17887\0"; 843 break; 844 default: 845 model = "Unknown\0"; 846 break; 847 } 848 849 /* Print out the information. */ 850 seq_printf(seq, 851 "Model\t\t: %s\n" 852 "Oscillator\t: %s\n" 853 "12/24hr\t\t: %s\n" 854 "DST\t\t: %s\n" 855 "Data mode\t: %s\n" 856 "Battery\t\t: %s\n" 857 "Aux batt\t: %s\n" 858 "Update IRQ\t: %s\n" 859 "Periodic IRQ\t: %s\n" 860 "Periodic Rate\t: %s\n" 861 "SQW Freq\t: %s\n" 862 #ifdef CONFIG_RTC_DS1685_PROC_REGS 863 "Serial #\t: %8phC\n" 864 "Register Status\t:\n" 865 " Ctrl A\t: UIP DV2 DV1 DV0 RS3 RS2 RS1 RS0\n" 866 "\t\t: %s\n" 867 " Ctrl B\t: SET PIE AIE UIE SQWE DM 2412 DSE\n" 868 "\t\t: %s\n" 869 " Ctrl C\t: IRQF PF AF UF --- --- --- ---\n" 870 "\t\t: %s\n" 871 " Ctrl D\t: VRT --- --- --- --- --- --- ---\n" 872 "\t\t: %s\n" 873 #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689) 874 " Ctrl 4A\t: VRT2 INCR BME --- PAB RF WF KF\n" 875 #else 876 " Ctrl 4A\t: VRT2 INCR --- --- PAB RF WF KF\n" 877 #endif 878 "\t\t: %s\n" 879 " Ctrl 4B\t: ABE E32k CS RCE PRS RIE WIE KSE\n" 880 "\t\t: %s\n", 881 #else 882 "Serial #\t: %8phC\n", 883 #endif 884 model, 885 ((ctrla & RTC_CTRL_A_DV1) ? "enabled" : "disabled"), 886 ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"), 887 ((ctrlb & RTC_CTRL_B_DSE) ? "enabled" : "disabled"), 888 ((ctrlb & RTC_CTRL_B_DM) ? "binary" : "BCD"), 889 ((ctrld & RTC_CTRL_D_VRT) ? "ok" : "exhausted or n/a"), 890 ((ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "exhausted or n/a"), 891 ((ctrlb & RTC_CTRL_B_UIE) ? "yes" : "no"), 892 ((ctrlb & RTC_CTRL_B_PIE) ? "yes" : "no"), 893 (!(ctrl4b & RTC_CTRL_4B_E32K) ? 894 ds1685_rtc_pirq_rate[(ctrla & RTC_CTRL_A_RS_MASK)] : "none"), 895 (!((ctrl4b & RTC_CTRL_4B_E32K)) ? 896 ds1685_rtc_sqw_freq[(ctrla & RTC_CTRL_A_RS_MASK)] : "32768Hz"), 897 #ifdef CONFIG_RTC_DS1685_PROC_REGS 898 ssn, 899 ds1685_rtc_print_regs(ctrla, bits[0]), 900 ds1685_rtc_print_regs(ctrlb, bits[1]), 901 ds1685_rtc_print_regs(ctrlc, bits[2]), 902 ds1685_rtc_print_regs(ctrld, bits[3]), 903 ds1685_rtc_print_regs(ctrl4a, bits[4]), 904 ds1685_rtc_print_regs(ctrl4b, bits[5])); 905 #else 906 ssn); 907 #endif 908 return 0; 909 } 910 #else 911 #define ds1685_rtc_proc NULL 912 #endif /* CONFIG_PROC_FS */ 913 /* ----------------------------------------------------------------------- */ 914 915 916 /* ----------------------------------------------------------------------- */ 917 /* RTC Class operations */ 918 919 static const struct rtc_class_ops 920 ds1685_rtc_ops = { 921 .proc = ds1685_rtc_proc, 922 .read_time = ds1685_rtc_read_time, 923 .set_time = ds1685_rtc_set_time, 924 .read_alarm = ds1685_rtc_read_alarm, 925 .set_alarm = ds1685_rtc_set_alarm, 926 .alarm_irq_enable = ds1685_rtc_alarm_irq_enable, 927 }; 928 /* ----------------------------------------------------------------------- */ 929 930 931 /* ----------------------------------------------------------------------- */ 932 /* SysFS interface */ 933 934 #ifdef CONFIG_SYSFS 935 /** 936 * ds1685_rtc_sysfs_nvram_read - reads rtc nvram via sysfs. 937 * @file: pointer to file structure. 938 * @kobj: pointer to kobject structure. 939 * @bin_attr: pointer to bin_attribute structure. 940 * @buf: pointer to char array to hold the output. 941 * @pos: current file position pointer. 942 * @size: size of the data to read. 943 */ 944 static ssize_t 945 ds1685_rtc_sysfs_nvram_read(struct file *filp, struct kobject *kobj, 946 struct bin_attribute *bin_attr, char *buf, 947 loff_t pos, size_t size) 948 { 949 struct platform_device *pdev = 950 to_platform_device(container_of(kobj, struct device, kobj)); 951 struct ds1685_priv *rtc = platform_get_drvdata(pdev); 952 ssize_t count; 953 unsigned long flags = 0; 954 955 spin_lock_irqsave(&rtc->lock, flags); 956 ds1685_rtc_switch_to_bank0(rtc); 957 958 /* Read NVRAM in time and bank0 registers. */ 959 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0; 960 count++, size--) { 961 if (count < NVRAM_SZ_TIME) 962 *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++)); 963 else 964 *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++)); 965 } 966 967 #ifndef CONFIG_RTC_DRV_DS1689 968 if (size > 0) { 969 ds1685_rtc_switch_to_bank1(rtc); 970 971 #ifndef CONFIG_RTC_DRV_DS1685 972 /* Enable burst-mode on DS17x85/DS17x87 */ 973 rtc->write(rtc, RTC_EXT_CTRL_4A, 974 (rtc->read(rtc, RTC_EXT_CTRL_4A) | 975 RTC_CTRL_4A_BME)); 976 977 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start 978 * reading with burst-mode */ 979 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB, 980 (pos - NVRAM_TOTAL_SZ_BANK0)); 981 #endif 982 983 /* Read NVRAM in bank1 registers. */ 984 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ; 985 count++, size--) { 986 #ifdef CONFIG_RTC_DRV_DS1685 987 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR 988 * before each read. */ 989 rtc->write(rtc, RTC_BANK1_RAM_ADDR, 990 (pos - NVRAM_TOTAL_SZ_BANK0)); 991 #endif 992 *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT); 993 pos++; 994 } 995 996 #ifndef CONFIG_RTC_DRV_DS1685 997 /* Disable burst-mode on DS17x85/DS17x87 */ 998 rtc->write(rtc, RTC_EXT_CTRL_4A, 999 (rtc->read(rtc, RTC_EXT_CTRL_4A) & 1000 ~(RTC_CTRL_4A_BME))); 1001 #endif 1002 ds1685_rtc_switch_to_bank0(rtc); 1003 } 1004 #endif /* !CONFIG_RTC_DRV_DS1689 */ 1005 spin_unlock_irqrestore(&rtc->lock, flags); 1006 1007 /* 1008 * XXX: Bug? this appears to cause the function to get executed 1009 * several times in succession. But it's the only way to actually get 1010 * data written out to a file. 1011 */ 1012 return count; 1013 } 1014 1015 /** 1016 * ds1685_rtc_sysfs_nvram_write - writes rtc nvram via sysfs. 1017 * @file: pointer to file structure. 1018 * @kobj: pointer to kobject structure. 1019 * @bin_attr: pointer to bin_attribute structure. 1020 * @buf: pointer to char array to hold the input. 1021 * @pos: current file position pointer. 1022 * @size: size of the data to write. 1023 */ 1024 static ssize_t 1025 ds1685_rtc_sysfs_nvram_write(struct file *filp, struct kobject *kobj, 1026 struct bin_attribute *bin_attr, char *buf, 1027 loff_t pos, size_t size) 1028 { 1029 struct platform_device *pdev = 1030 to_platform_device(container_of(kobj, struct device, kobj)); 1031 struct ds1685_priv *rtc = platform_get_drvdata(pdev); 1032 ssize_t count; 1033 unsigned long flags = 0; 1034 1035 spin_lock_irqsave(&rtc->lock, flags); 1036 ds1685_rtc_switch_to_bank0(rtc); 1037 1038 /* Write NVRAM in time and bank0 registers. */ 1039 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0; 1040 count++, size--) 1041 if (count < NVRAM_SZ_TIME) 1042 rtc->write(rtc, (NVRAM_TIME_BASE + pos++), 1043 *buf++); 1044 else 1045 rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++); 1046 1047 #ifndef CONFIG_RTC_DRV_DS1689 1048 if (size > 0) { 1049 ds1685_rtc_switch_to_bank1(rtc); 1050 1051 #ifndef CONFIG_RTC_DRV_DS1685 1052 /* Enable burst-mode on DS17x85/DS17x87 */ 1053 rtc->write(rtc, RTC_EXT_CTRL_4A, 1054 (rtc->read(rtc, RTC_EXT_CTRL_4A) | 1055 RTC_CTRL_4A_BME)); 1056 1057 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start 1058 * writing with burst-mode */ 1059 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB, 1060 (pos - NVRAM_TOTAL_SZ_BANK0)); 1061 #endif 1062 1063 /* Write NVRAM in bank1 registers. */ 1064 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ; 1065 count++, size--) { 1066 #ifdef CONFIG_RTC_DRV_DS1685 1067 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR 1068 * before each read. */ 1069 rtc->write(rtc, RTC_BANK1_RAM_ADDR, 1070 (pos - NVRAM_TOTAL_SZ_BANK0)); 1071 #endif 1072 rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++); 1073 pos++; 1074 } 1075 1076 #ifndef CONFIG_RTC_DRV_DS1685 1077 /* Disable burst-mode on DS17x85/DS17x87 */ 1078 rtc->write(rtc, RTC_EXT_CTRL_4A, 1079 (rtc->read(rtc, RTC_EXT_CTRL_4A) & 1080 ~(RTC_CTRL_4A_BME))); 1081 #endif 1082 ds1685_rtc_switch_to_bank0(rtc); 1083 } 1084 #endif /* !CONFIG_RTC_DRV_DS1689 */ 1085 spin_unlock_irqrestore(&rtc->lock, flags); 1086 1087 return count; 1088 } 1089 1090 /** 1091 * struct ds1685_rtc_sysfs_nvram_attr - sysfs attributes for rtc nvram. 1092 * @attr: nvram attributes. 1093 * @read: nvram read function. 1094 * @write: nvram write function. 1095 * @size: nvram total size (bank0 + extended). 1096 */ 1097 static struct bin_attribute 1098 ds1685_rtc_sysfs_nvram_attr = { 1099 .attr = { 1100 .name = "nvram", 1101 .mode = S_IRUGO | S_IWUSR, 1102 }, 1103 .read = ds1685_rtc_sysfs_nvram_read, 1104 .write = ds1685_rtc_sysfs_nvram_write, 1105 .size = NVRAM_TOTAL_SZ 1106 }; 1107 1108 /** 1109 * ds1685_rtc_sysfs_battery_show - sysfs file for main battery status. 1110 * @dev: pointer to device structure. 1111 * @attr: pointer to device_attribute structure. 1112 * @buf: pointer to char array to hold the output. 1113 */ 1114 static ssize_t 1115 ds1685_rtc_sysfs_battery_show(struct device *dev, 1116 struct device_attribute *attr, char *buf) 1117 { 1118 struct ds1685_priv *rtc = dev_get_drvdata(dev); 1119 u8 ctrld; 1120 1121 ctrld = rtc->read(rtc, RTC_CTRL_D); 1122 1123 return sprintf(buf, "%s\n", 1124 (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A"); 1125 } 1126 static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL); 1127 1128 /** 1129 * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status. 1130 * @dev: pointer to device structure. 1131 * @attr: pointer to device_attribute structure. 1132 * @buf: pointer to char array to hold the output. 1133 */ 1134 static ssize_t 1135 ds1685_rtc_sysfs_auxbatt_show(struct device *dev, 1136 struct device_attribute *attr, char *buf) 1137 { 1138 struct ds1685_priv *rtc = dev_get_drvdata(dev); 1139 u8 ctrl4a; 1140 1141 ds1685_rtc_switch_to_bank1(rtc); 1142 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); 1143 ds1685_rtc_switch_to_bank0(rtc); 1144 1145 return sprintf(buf, "%s\n", 1146 (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A"); 1147 } 1148 static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL); 1149 1150 /** 1151 * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number. 1152 * @dev: pointer to device structure. 1153 * @attr: pointer to device_attribute structure. 1154 * @buf: pointer to char array to hold the output. 1155 */ 1156 static ssize_t 1157 ds1685_rtc_sysfs_serial_show(struct device *dev, 1158 struct device_attribute *attr, char *buf) 1159 { 1160 struct ds1685_priv *rtc = dev_get_drvdata(dev); 1161 u8 ssn[8]; 1162 1163 ds1685_rtc_switch_to_bank1(rtc); 1164 ds1685_rtc_get_ssn(rtc, ssn); 1165 ds1685_rtc_switch_to_bank0(rtc); 1166 1167 return sprintf(buf, "%8phC\n", ssn); 1168 } 1169 static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL); 1170 1171 /** 1172 * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features. 1173 */ 1174 static struct attribute* 1175 ds1685_rtc_sysfs_misc_attrs[] = { 1176 &dev_attr_battery.attr, 1177 &dev_attr_auxbatt.attr, 1178 &dev_attr_serial.attr, 1179 NULL, 1180 }; 1181 1182 /** 1183 * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features. 1184 */ 1185 static const struct attribute_group 1186 ds1685_rtc_sysfs_misc_grp = { 1187 .name = "misc", 1188 .attrs = ds1685_rtc_sysfs_misc_attrs, 1189 }; 1190 1191 #ifdef CONFIG_RTC_DS1685_SYSFS_REGS 1192 /** 1193 * struct ds1685_rtc_ctrl_regs. 1194 * @name: char pointer for the bit name. 1195 * @reg: control register the bit is in. 1196 * @bit: the bit's offset in the register. 1197 */ 1198 struct ds1685_rtc_ctrl_regs { 1199 const char *name; 1200 const u8 reg; 1201 const u8 bit; 1202 }; 1203 1204 /* 1205 * Ctrl register bit lookup table. 1206 */ 1207 static const struct ds1685_rtc_ctrl_regs 1208 ds1685_ctrl_regs_table[] = { 1209 { "uip", RTC_CTRL_A, RTC_CTRL_A_UIP }, 1210 { "dv2", RTC_CTRL_A, RTC_CTRL_A_DV2 }, 1211 { "dv1", RTC_CTRL_A, RTC_CTRL_A_DV1 }, 1212 { "dv0", RTC_CTRL_A, RTC_CTRL_A_DV0 }, 1213 { "rs3", RTC_CTRL_A, RTC_CTRL_A_RS3 }, 1214 { "rs2", RTC_CTRL_A, RTC_CTRL_A_RS2 }, 1215 { "rs1", RTC_CTRL_A, RTC_CTRL_A_RS1 }, 1216 { "rs0", RTC_CTRL_A, RTC_CTRL_A_RS0 }, 1217 { "set", RTC_CTRL_B, RTC_CTRL_B_SET }, 1218 { "pie", RTC_CTRL_B, RTC_CTRL_B_PIE }, 1219 { "aie", RTC_CTRL_B, RTC_CTRL_B_AIE }, 1220 { "uie", RTC_CTRL_B, RTC_CTRL_B_UIE }, 1221 { "sqwe", RTC_CTRL_B, RTC_CTRL_B_SQWE }, 1222 { "dm", RTC_CTRL_B, RTC_CTRL_B_DM }, 1223 { "2412", RTC_CTRL_B, RTC_CTRL_B_2412 }, 1224 { "dse", RTC_CTRL_B, RTC_CTRL_B_DSE }, 1225 { "irqf", RTC_CTRL_C, RTC_CTRL_C_IRQF }, 1226 { "pf", RTC_CTRL_C, RTC_CTRL_C_PF }, 1227 { "af", RTC_CTRL_C, RTC_CTRL_C_AF }, 1228 { "uf", RTC_CTRL_C, RTC_CTRL_C_UF }, 1229 { "vrt", RTC_CTRL_D, RTC_CTRL_D_VRT }, 1230 { "vrt2", RTC_EXT_CTRL_4A, RTC_CTRL_4A_VRT2 }, 1231 { "incr", RTC_EXT_CTRL_4A, RTC_CTRL_4A_INCR }, 1232 { "pab", RTC_EXT_CTRL_4A, RTC_CTRL_4A_PAB }, 1233 { "rf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_RF }, 1234 { "wf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_WF }, 1235 { "kf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_KF }, 1236 #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689) 1237 { "bme", RTC_EXT_CTRL_4A, RTC_CTRL_4A_BME }, 1238 #endif 1239 { "abe", RTC_EXT_CTRL_4B, RTC_CTRL_4B_ABE }, 1240 { "e32k", RTC_EXT_CTRL_4B, RTC_CTRL_4B_E32K }, 1241 { "cs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_CS }, 1242 { "rce", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RCE }, 1243 { "prs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_PRS }, 1244 { "rie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RIE }, 1245 { "wie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_WIE }, 1246 { "kse", RTC_EXT_CTRL_4B, RTC_CTRL_4B_KSE }, 1247 { NULL, 0, 0 }, 1248 }; 1249 1250 /** 1251 * ds1685_rtc_sysfs_ctrl_regs_lookup - ctrl register bit lookup function. 1252 * @name: ctrl register bit to look up in ds1685_ctrl_regs_table. 1253 */ 1254 static const struct ds1685_rtc_ctrl_regs* 1255 ds1685_rtc_sysfs_ctrl_regs_lookup(const char *name) 1256 { 1257 const struct ds1685_rtc_ctrl_regs *p = ds1685_ctrl_regs_table; 1258 1259 for (; p->name != NULL; ++p) 1260 if (strcmp(p->name, name) == 0) 1261 return p; 1262 1263 return NULL; 1264 } 1265 1266 /** 1267 * ds1685_rtc_sysfs_ctrl_regs_show - reads a ctrl register bit via sysfs. 1268 * @dev: pointer to device structure. 1269 * @attr: pointer to device_attribute structure. 1270 * @buf: pointer to char array to hold the output. 1271 */ 1272 static ssize_t 1273 ds1685_rtc_sysfs_ctrl_regs_show(struct device *dev, 1274 struct device_attribute *attr, char *buf) 1275 { 1276 u8 tmp; 1277 struct ds1685_priv *rtc = dev_get_drvdata(dev); 1278 const struct ds1685_rtc_ctrl_regs *reg_info = 1279 ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name); 1280 1281 /* Make sure we actually matched something. */ 1282 if (!reg_info) 1283 return -EINVAL; 1284 1285 /* No spinlock during a read -- mutex is already held. */ 1286 ds1685_rtc_switch_to_bank1(rtc); 1287 tmp = rtc->read(rtc, reg_info->reg) & reg_info->bit; 1288 ds1685_rtc_switch_to_bank0(rtc); 1289 1290 return sprintf(buf, "%d\n", (tmp ? 1 : 0)); 1291 } 1292 1293 /** 1294 * ds1685_rtc_sysfs_ctrl_regs_store - writes a ctrl register bit via sysfs. 1295 * @dev: pointer to device structure. 1296 * @attr: pointer to device_attribute structure. 1297 * @buf: pointer to char array to hold the output. 1298 * @count: number of bytes written. 1299 */ 1300 static ssize_t 1301 ds1685_rtc_sysfs_ctrl_regs_store(struct device *dev, 1302 struct device_attribute *attr, 1303 const char *buf, size_t count) 1304 { 1305 struct ds1685_priv *rtc = dev_get_drvdata(dev); 1306 u8 reg = 0, bit = 0, tmp; 1307 unsigned long flags; 1308 long int val = 0; 1309 const struct ds1685_rtc_ctrl_regs *reg_info = 1310 ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name); 1311 1312 /* We only accept numbers. */ 1313 if (kstrtol(buf, 10, &val) < 0) 1314 return -EINVAL; 1315 1316 /* bits are binary, 0 or 1 only. */ 1317 if ((val != 0) && (val != 1)) 1318 return -ERANGE; 1319 1320 /* Make sure we actually matched something. */ 1321 if (!reg_info) 1322 return -EINVAL; 1323 1324 reg = reg_info->reg; 1325 bit = reg_info->bit; 1326 1327 /* Safe to spinlock during a write. */ 1328 ds1685_rtc_begin_ctrl_access(rtc, &flags); 1329 tmp = rtc->read(rtc, reg); 1330 rtc->write(rtc, reg, (val ? (tmp | bit) : (tmp & ~(bit)))); 1331 ds1685_rtc_end_ctrl_access(rtc, flags); 1332 1333 return count; 1334 } 1335 1336 /** 1337 * DS1685_RTC_SYSFS_CTRL_REG_RO - device_attribute for read-only register bit. 1338 * @bit: bit to read. 1339 */ 1340 #define DS1685_RTC_SYSFS_CTRL_REG_RO(bit) \ 1341 static DEVICE_ATTR(bit, S_IRUGO, \ 1342 ds1685_rtc_sysfs_ctrl_regs_show, NULL) 1343 1344 /** 1345 * DS1685_RTC_SYSFS_CTRL_REG_RW - device_attribute for read-write register bit. 1346 * @bit: bit to read or write. 1347 */ 1348 #define DS1685_RTC_SYSFS_CTRL_REG_RW(bit) \ 1349 static DEVICE_ATTR(bit, S_IRUGO | S_IWUSR, \ 1350 ds1685_rtc_sysfs_ctrl_regs_show, \ 1351 ds1685_rtc_sysfs_ctrl_regs_store) 1352 1353 /* 1354 * Control Register A bits. 1355 */ 1356 DS1685_RTC_SYSFS_CTRL_REG_RO(uip); 1357 DS1685_RTC_SYSFS_CTRL_REG_RW(dv2); 1358 DS1685_RTC_SYSFS_CTRL_REG_RW(dv1); 1359 DS1685_RTC_SYSFS_CTRL_REG_RO(dv0); 1360 DS1685_RTC_SYSFS_CTRL_REG_RW(rs3); 1361 DS1685_RTC_SYSFS_CTRL_REG_RW(rs2); 1362 DS1685_RTC_SYSFS_CTRL_REG_RW(rs1); 1363 DS1685_RTC_SYSFS_CTRL_REG_RW(rs0); 1364 1365 static struct attribute* 1366 ds1685_rtc_sysfs_ctrla_attrs[] = { 1367 &dev_attr_uip.attr, 1368 &dev_attr_dv2.attr, 1369 &dev_attr_dv1.attr, 1370 &dev_attr_dv0.attr, 1371 &dev_attr_rs3.attr, 1372 &dev_attr_rs2.attr, 1373 &dev_attr_rs1.attr, 1374 &dev_attr_rs0.attr, 1375 NULL, 1376 }; 1377 1378 static const struct attribute_group 1379 ds1685_rtc_sysfs_ctrla_grp = { 1380 .name = "ctrla", 1381 .attrs = ds1685_rtc_sysfs_ctrla_attrs, 1382 }; 1383 1384 1385 /* 1386 * Control Register B bits. 1387 */ 1388 DS1685_RTC_SYSFS_CTRL_REG_RO(set); 1389 DS1685_RTC_SYSFS_CTRL_REG_RW(pie); 1390 DS1685_RTC_SYSFS_CTRL_REG_RW(aie); 1391 DS1685_RTC_SYSFS_CTRL_REG_RW(uie); 1392 DS1685_RTC_SYSFS_CTRL_REG_RW(sqwe); 1393 DS1685_RTC_SYSFS_CTRL_REG_RO(dm); 1394 DS1685_RTC_SYSFS_CTRL_REG_RO(2412); 1395 DS1685_RTC_SYSFS_CTRL_REG_RO(dse); 1396 1397 static struct attribute* 1398 ds1685_rtc_sysfs_ctrlb_attrs[] = { 1399 &dev_attr_set.attr, 1400 &dev_attr_pie.attr, 1401 &dev_attr_aie.attr, 1402 &dev_attr_uie.attr, 1403 &dev_attr_sqwe.attr, 1404 &dev_attr_dm.attr, 1405 &dev_attr_2412.attr, 1406 &dev_attr_dse.attr, 1407 NULL, 1408 }; 1409 1410 static const struct attribute_group 1411 ds1685_rtc_sysfs_ctrlb_grp = { 1412 .name = "ctrlb", 1413 .attrs = ds1685_rtc_sysfs_ctrlb_attrs, 1414 }; 1415 1416 /* 1417 * Control Register C bits. 1418 * 1419 * Reading Control C clears these bits! Reading them individually can 1420 * possibly cause an interrupt to be missed. Use the /proc interface 1421 * to see all the bits in this register simultaneously. 1422 */ 1423 DS1685_RTC_SYSFS_CTRL_REG_RO(irqf); 1424 DS1685_RTC_SYSFS_CTRL_REG_RO(pf); 1425 DS1685_RTC_SYSFS_CTRL_REG_RO(af); 1426 DS1685_RTC_SYSFS_CTRL_REG_RO(uf); 1427 1428 static struct attribute* 1429 ds1685_rtc_sysfs_ctrlc_attrs[] = { 1430 &dev_attr_irqf.attr, 1431 &dev_attr_pf.attr, 1432 &dev_attr_af.attr, 1433 &dev_attr_uf.attr, 1434 NULL, 1435 }; 1436 1437 static const struct attribute_group 1438 ds1685_rtc_sysfs_ctrlc_grp = { 1439 .name = "ctrlc", 1440 .attrs = ds1685_rtc_sysfs_ctrlc_attrs, 1441 }; 1442 1443 /* 1444 * Control Register D bits. 1445 */ 1446 DS1685_RTC_SYSFS_CTRL_REG_RO(vrt); 1447 1448 static struct attribute* 1449 ds1685_rtc_sysfs_ctrld_attrs[] = { 1450 &dev_attr_vrt.attr, 1451 NULL, 1452 }; 1453 1454 static const struct attribute_group 1455 ds1685_rtc_sysfs_ctrld_grp = { 1456 .name = "ctrld", 1457 .attrs = ds1685_rtc_sysfs_ctrld_attrs, 1458 }; 1459 1460 /* 1461 * Control Register 4A bits. 1462 */ 1463 DS1685_RTC_SYSFS_CTRL_REG_RO(vrt2); 1464 DS1685_RTC_SYSFS_CTRL_REG_RO(incr); 1465 DS1685_RTC_SYSFS_CTRL_REG_RW(pab); 1466 DS1685_RTC_SYSFS_CTRL_REG_RW(rf); 1467 DS1685_RTC_SYSFS_CTRL_REG_RW(wf); 1468 DS1685_RTC_SYSFS_CTRL_REG_RW(kf); 1469 #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689) 1470 DS1685_RTC_SYSFS_CTRL_REG_RO(bme); 1471 #endif 1472 1473 static struct attribute* 1474 ds1685_rtc_sysfs_ctrl4a_attrs[] = { 1475 &dev_attr_vrt2.attr, 1476 &dev_attr_incr.attr, 1477 &dev_attr_pab.attr, 1478 &dev_attr_rf.attr, 1479 &dev_attr_wf.attr, 1480 &dev_attr_kf.attr, 1481 #if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689) 1482 &dev_attr_bme.attr, 1483 #endif 1484 NULL, 1485 }; 1486 1487 static const struct attribute_group 1488 ds1685_rtc_sysfs_ctrl4a_grp = { 1489 .name = "ctrl4a", 1490 .attrs = ds1685_rtc_sysfs_ctrl4a_attrs, 1491 }; 1492 1493 /* 1494 * Control Register 4B bits. 1495 */ 1496 DS1685_RTC_SYSFS_CTRL_REG_RW(abe); 1497 DS1685_RTC_SYSFS_CTRL_REG_RW(e32k); 1498 DS1685_RTC_SYSFS_CTRL_REG_RO(cs); 1499 DS1685_RTC_SYSFS_CTRL_REG_RW(rce); 1500 DS1685_RTC_SYSFS_CTRL_REG_RW(prs); 1501 DS1685_RTC_SYSFS_CTRL_REG_RW(rie); 1502 DS1685_RTC_SYSFS_CTRL_REG_RW(wie); 1503 DS1685_RTC_SYSFS_CTRL_REG_RW(kse); 1504 1505 static struct attribute* 1506 ds1685_rtc_sysfs_ctrl4b_attrs[] = { 1507 &dev_attr_abe.attr, 1508 &dev_attr_e32k.attr, 1509 &dev_attr_cs.attr, 1510 &dev_attr_rce.attr, 1511 &dev_attr_prs.attr, 1512 &dev_attr_rie.attr, 1513 &dev_attr_wie.attr, 1514 &dev_attr_kse.attr, 1515 NULL, 1516 }; 1517 1518 static const struct attribute_group 1519 ds1685_rtc_sysfs_ctrl4b_grp = { 1520 .name = "ctrl4b", 1521 .attrs = ds1685_rtc_sysfs_ctrl4b_attrs, 1522 }; 1523 1524 1525 /** 1526 * struct ds1685_rtc_ctrl_regs. 1527 * @name: char pointer for the bit name. 1528 * @reg: control register the bit is in. 1529 * @bit: the bit's offset in the register. 1530 */ 1531 struct ds1685_rtc_time_regs { 1532 const char *name; 1533 const u8 reg; 1534 const u8 mask; 1535 const u8 min; 1536 const u8 max; 1537 }; 1538 1539 /* 1540 * Time/Date register lookup tables. 1541 */ 1542 static const struct ds1685_rtc_time_regs 1543 ds1685_time_regs_bcd_table[] = { 1544 { "seconds", RTC_SECS, RTC_SECS_BCD_MASK, 0, 59 }, 1545 { "minutes", RTC_MINS, RTC_MINS_BCD_MASK, 0, 59 }, 1546 { "hours", RTC_HRS, RTC_HRS_24_BCD_MASK, 0, 23 }, 1547 { "wday", RTC_WDAY, RTC_WDAY_MASK, 1, 7 }, 1548 { "mday", RTC_MDAY, RTC_MDAY_BCD_MASK, 1, 31 }, 1549 { "month", RTC_MONTH, RTC_MONTH_BCD_MASK, 1, 12 }, 1550 { "year", RTC_YEAR, RTC_YEAR_BCD_MASK, 0, 99 }, 1551 { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0, 99 }, 1552 { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BCD_MASK, 0, 59 }, 1553 { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BCD_MASK, 0, 59 }, 1554 { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BCD_MASK, 0, 23 }, 1555 { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 1, 31 }, 1556 { NULL, 0, 0, 0, 0 }, 1557 }; 1558 1559 static const struct ds1685_rtc_time_regs 1560 ds1685_time_regs_bin_table[] = { 1561 { "seconds", RTC_SECS, RTC_SECS_BIN_MASK, 0x00, 0x3b }, 1562 { "minutes", RTC_MINS, RTC_MINS_BIN_MASK, 0x00, 0x3b }, 1563 { "hours", RTC_HRS, RTC_HRS_24_BIN_MASK, 0x00, 0x17 }, 1564 { "wday", RTC_WDAY, RTC_WDAY_MASK, 0x01, 0x07 }, 1565 { "mday", RTC_MDAY, RTC_MDAY_BIN_MASK, 0x01, 0x1f }, 1566 { "month", RTC_MONTH, RTC_MONTH_BIN_MASK, 0x01, 0x0c }, 1567 { "year", RTC_YEAR, RTC_YEAR_BIN_MASK, 0x00, 0x63 }, 1568 { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0x00, 0x63 }, 1569 { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BIN_MASK, 0x00, 0x3b }, 1570 { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BIN_MASK, 0x00, 0x3b }, 1571 { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BIN_MASK, 0x00, 0x17 }, 1572 { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 0x01, 0x1f }, 1573 { NULL, 0, 0, 0x00, 0x00 }, 1574 }; 1575 1576 /** 1577 * ds1685_rtc_sysfs_time_regs_bcd_lookup - time/date reg bit lookup function. 1578 * @name: register bit to look up in ds1685_time_regs_bcd_table. 1579 */ 1580 static const struct ds1685_rtc_time_regs* 1581 ds1685_rtc_sysfs_time_regs_lookup(const char *name, bool bcd_mode) 1582 { 1583 const struct ds1685_rtc_time_regs *p; 1584 1585 if (bcd_mode) 1586 p = ds1685_time_regs_bcd_table; 1587 else 1588 p = ds1685_time_regs_bin_table; 1589 1590 for (; p->name != NULL; ++p) 1591 if (strcmp(p->name, name) == 0) 1592 return p; 1593 1594 return NULL; 1595 } 1596 1597 /** 1598 * ds1685_rtc_sysfs_time_regs_show - reads a time/date register via sysfs. 1599 * @dev: pointer to device structure. 1600 * @attr: pointer to device_attribute structure. 1601 * @buf: pointer to char array to hold the output. 1602 */ 1603 static ssize_t 1604 ds1685_rtc_sysfs_time_regs_show(struct device *dev, 1605 struct device_attribute *attr, char *buf) 1606 { 1607 u8 tmp; 1608 struct ds1685_priv *rtc = dev_get_drvdata(dev); 1609 const struct ds1685_rtc_time_regs *bcd_reg_info = 1610 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true); 1611 const struct ds1685_rtc_time_regs *bin_reg_info = 1612 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false); 1613 1614 /* Make sure we actually matched something. */ 1615 if (!bcd_reg_info || !bin_reg_info) 1616 return -EINVAL; 1617 1618 /* bcd_reg_info->reg == bin_reg_info->reg. */ 1619 ds1685_rtc_begin_data_access(rtc); 1620 tmp = rtc->read(rtc, bcd_reg_info->reg); 1621 ds1685_rtc_end_data_access(rtc); 1622 1623 tmp = ds1685_rtc_bcd2bin(rtc, tmp, bcd_reg_info->mask, 1624 bin_reg_info->mask); 1625 1626 return sprintf(buf, "%d\n", tmp); 1627 } 1628 1629 /** 1630 * ds1685_rtc_sysfs_time_regs_store - writes a time/date register via sysfs. 1631 * @dev: pointer to device structure. 1632 * @attr: pointer to device_attribute structure. 1633 * @buf: pointer to char array to hold the output. 1634 * @count: number of bytes written. 1635 */ 1636 static ssize_t 1637 ds1685_rtc_sysfs_time_regs_store(struct device *dev, 1638 struct device_attribute *attr, 1639 const char *buf, size_t count) 1640 { 1641 long int val = 0; 1642 struct ds1685_priv *rtc = dev_get_drvdata(dev); 1643 const struct ds1685_rtc_time_regs *bcd_reg_info = 1644 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true); 1645 const struct ds1685_rtc_time_regs *bin_reg_info = 1646 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false); 1647 1648 /* We only accept numbers. */ 1649 if (kstrtol(buf, 10, &val) < 0) 1650 return -EINVAL; 1651 1652 /* Make sure we actually matched something. */ 1653 if (!bcd_reg_info || !bin_reg_info) 1654 return -EINVAL; 1655 1656 /* Check for a valid range. */ 1657 if (rtc->bcd_mode) { 1658 if ((val < bcd_reg_info->min) || (val > bcd_reg_info->max)) 1659 return -ERANGE; 1660 } else { 1661 if ((val < bin_reg_info->min) || (val > bin_reg_info->max)) 1662 return -ERANGE; 1663 } 1664 1665 val = ds1685_rtc_bin2bcd(rtc, val, bin_reg_info->mask, 1666 bcd_reg_info->mask); 1667 1668 /* bcd_reg_info->reg == bin_reg_info->reg. */ 1669 ds1685_rtc_begin_data_access(rtc); 1670 rtc->write(rtc, bcd_reg_info->reg, val); 1671 ds1685_rtc_end_data_access(rtc); 1672 1673 return count; 1674 } 1675 1676 /** 1677 * DS1685_RTC_SYSFS_REG_RW - device_attribute for a read-write time register. 1678 * @reg: time/date register to read or write. 1679 */ 1680 #define DS1685_RTC_SYSFS_TIME_REG_RW(reg) \ 1681 static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR, \ 1682 ds1685_rtc_sysfs_time_regs_show, \ 1683 ds1685_rtc_sysfs_time_regs_store) 1684 1685 /* 1686 * Time/Date Register bits. 1687 */ 1688 DS1685_RTC_SYSFS_TIME_REG_RW(seconds); 1689 DS1685_RTC_SYSFS_TIME_REG_RW(minutes); 1690 DS1685_RTC_SYSFS_TIME_REG_RW(hours); 1691 DS1685_RTC_SYSFS_TIME_REG_RW(wday); 1692 DS1685_RTC_SYSFS_TIME_REG_RW(mday); 1693 DS1685_RTC_SYSFS_TIME_REG_RW(month); 1694 DS1685_RTC_SYSFS_TIME_REG_RW(year); 1695 DS1685_RTC_SYSFS_TIME_REG_RW(century); 1696 DS1685_RTC_SYSFS_TIME_REG_RW(alarm_seconds); 1697 DS1685_RTC_SYSFS_TIME_REG_RW(alarm_minutes); 1698 DS1685_RTC_SYSFS_TIME_REG_RW(alarm_hours); 1699 DS1685_RTC_SYSFS_TIME_REG_RW(alarm_mday); 1700 1701 static struct attribute* 1702 ds1685_rtc_sysfs_time_attrs[] = { 1703 &dev_attr_seconds.attr, 1704 &dev_attr_minutes.attr, 1705 &dev_attr_hours.attr, 1706 &dev_attr_wday.attr, 1707 &dev_attr_mday.attr, 1708 &dev_attr_month.attr, 1709 &dev_attr_year.attr, 1710 &dev_attr_century.attr, 1711 NULL, 1712 }; 1713 1714 static const struct attribute_group 1715 ds1685_rtc_sysfs_time_grp = { 1716 .name = "datetime", 1717 .attrs = ds1685_rtc_sysfs_time_attrs, 1718 }; 1719 1720 static struct attribute* 1721 ds1685_rtc_sysfs_alarm_attrs[] = { 1722 &dev_attr_alarm_seconds.attr, 1723 &dev_attr_alarm_minutes.attr, 1724 &dev_attr_alarm_hours.attr, 1725 &dev_attr_alarm_mday.attr, 1726 NULL, 1727 }; 1728 1729 static const struct attribute_group 1730 ds1685_rtc_sysfs_alarm_grp = { 1731 .name = "alarm", 1732 .attrs = ds1685_rtc_sysfs_alarm_attrs, 1733 }; 1734 #endif /* CONFIG_RTC_DS1685_SYSFS_REGS */ 1735 1736 1737 /** 1738 * ds1685_rtc_sysfs_register - register sysfs files. 1739 * @dev: pointer to device structure. 1740 */ 1741 static int 1742 ds1685_rtc_sysfs_register(struct device *dev) 1743 { 1744 int ret = 0; 1745 1746 sysfs_bin_attr_init(&ds1685_rtc_sysfs_nvram_attr); 1747 ret = sysfs_create_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr); 1748 if (ret) 1749 return ret; 1750 1751 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp); 1752 if (ret) 1753 return ret; 1754 1755 #ifdef CONFIG_RTC_DS1685_SYSFS_REGS 1756 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp); 1757 if (ret) 1758 return ret; 1759 1760 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp); 1761 if (ret) 1762 return ret; 1763 1764 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp); 1765 if (ret) 1766 return ret; 1767 1768 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp); 1769 if (ret) 1770 return ret; 1771 1772 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp); 1773 if (ret) 1774 return ret; 1775 1776 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp); 1777 if (ret) 1778 return ret; 1779 1780 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp); 1781 if (ret) 1782 return ret; 1783 1784 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp); 1785 if (ret) 1786 return ret; 1787 #endif 1788 return 0; 1789 } 1790 1791 /** 1792 * ds1685_rtc_sysfs_unregister - unregister sysfs files. 1793 * @dev: pointer to device structure. 1794 */ 1795 static int 1796 ds1685_rtc_sysfs_unregister(struct device *dev) 1797 { 1798 sysfs_remove_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr); 1799 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp); 1800 1801 #ifdef CONFIG_RTC_DS1685_SYSFS_REGS 1802 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp); 1803 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp); 1804 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp); 1805 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp); 1806 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp); 1807 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp); 1808 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp); 1809 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp); 1810 #endif 1811 1812 return 0; 1813 } 1814 #endif /* CONFIG_SYSFS */ 1815 1816 1817 1818 /* ----------------------------------------------------------------------- */ 1819 /* Driver Probe/Removal */ 1820 1821 /** 1822 * ds1685_rtc_probe - initializes rtc driver. 1823 * @pdev: pointer to platform_device structure. 1824 */ 1825 static int 1826 ds1685_rtc_probe(struct platform_device *pdev) 1827 { 1828 struct rtc_device *rtc_dev; 1829 struct resource *res; 1830 struct ds1685_priv *rtc; 1831 struct ds1685_rtc_platform_data *pdata; 1832 u8 ctrla, ctrlb, hours; 1833 unsigned char am_pm; 1834 int ret = 0; 1835 1836 /* Get the platform data. */ 1837 pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data; 1838 if (!pdata) 1839 return -ENODEV; 1840 1841 /* Allocate memory for the rtc device. */ 1842 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); 1843 if (!rtc) 1844 return -ENOMEM; 1845 1846 /* 1847 * Allocate/setup any IORESOURCE_MEM resources, if required. Not all 1848 * platforms put the RTC in an easy-access place. Like the SGI Octane, 1849 * which attaches the RTC to a "ByteBus", hooked to a SuperIO chip 1850 * that sits behind the IOC3 PCI metadevice. 1851 */ 1852 if (pdata->alloc_io_resources) { 1853 /* Get the platform resources. */ 1854 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1855 if (!res) 1856 return -ENXIO; 1857 rtc->size = resource_size(res); 1858 1859 /* Request a memory region. */ 1860 /* XXX: mmio-only for now. */ 1861 if (!devm_request_mem_region(&pdev->dev, res->start, rtc->size, 1862 pdev->name)) 1863 return -EBUSY; 1864 1865 /* 1866 * Set the base address for the rtc, and ioremap its 1867 * registers. 1868 */ 1869 rtc->baseaddr = res->start; 1870 rtc->regs = devm_ioremap(&pdev->dev, res->start, rtc->size); 1871 if (!rtc->regs) 1872 return -ENOMEM; 1873 } 1874 rtc->alloc_io_resources = pdata->alloc_io_resources; 1875 1876 /* Get the register step size. */ 1877 if (pdata->regstep > 0) 1878 rtc->regstep = pdata->regstep; 1879 else 1880 rtc->regstep = 1; 1881 1882 /* Platform read function, else default if mmio setup */ 1883 if (pdata->plat_read) 1884 rtc->read = pdata->plat_read; 1885 else 1886 if (pdata->alloc_io_resources) 1887 rtc->read = ds1685_read; 1888 else 1889 return -ENXIO; 1890 1891 /* Platform write function, else default if mmio setup */ 1892 if (pdata->plat_write) 1893 rtc->write = pdata->plat_write; 1894 else 1895 if (pdata->alloc_io_resources) 1896 rtc->write = ds1685_write; 1897 else 1898 return -ENXIO; 1899 1900 /* Platform pre-shutdown function, if defined. */ 1901 if (pdata->plat_prepare_poweroff) 1902 rtc->prepare_poweroff = pdata->plat_prepare_poweroff; 1903 1904 /* Platform wake_alarm function, if defined. */ 1905 if (pdata->plat_wake_alarm) 1906 rtc->wake_alarm = pdata->plat_wake_alarm; 1907 1908 /* Platform post_ram_clear function, if defined. */ 1909 if (pdata->plat_post_ram_clear) 1910 rtc->post_ram_clear = pdata->plat_post_ram_clear; 1911 1912 /* Init the spinlock, workqueue, & set the driver data. */ 1913 spin_lock_init(&rtc->lock); 1914 INIT_WORK(&rtc->work, ds1685_rtc_work_queue); 1915 platform_set_drvdata(pdev, rtc); 1916 1917 /* Turn the oscillator on if is not already on (DV1 = 1). */ 1918 ctrla = rtc->read(rtc, RTC_CTRL_A); 1919 if (!(ctrla & RTC_CTRL_A_DV1)) 1920 ctrla |= RTC_CTRL_A_DV1; 1921 1922 /* Enable the countdown chain (DV2 = 0) */ 1923 ctrla &= ~(RTC_CTRL_A_DV2); 1924 1925 /* Clear RS3-RS0 in Control A. */ 1926 ctrla &= ~(RTC_CTRL_A_RS_MASK); 1927 1928 /* 1929 * All done with Control A. Switch to Bank 1 for the remainder of 1930 * the RTC setup so we have access to the extended functions. 1931 */ 1932 ctrla |= RTC_CTRL_A_DV0; 1933 rtc->write(rtc, RTC_CTRL_A, ctrla); 1934 1935 /* Default to 32768kHz output. */ 1936 rtc->write(rtc, RTC_EXT_CTRL_4B, 1937 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K)); 1938 1939 /* Set the SET bit in Control B so we can do some housekeeping. */ 1940 rtc->write(rtc, RTC_CTRL_B, 1941 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET)); 1942 1943 /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */ 1944 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR) 1945 cpu_relax(); 1946 1947 /* 1948 * If the platform supports BCD mode, then set DM=0 in Control B. 1949 * Otherwise, set DM=1 for BIN mode. 1950 */ 1951 ctrlb = rtc->read(rtc, RTC_CTRL_B); 1952 if (pdata->bcd_mode) 1953 ctrlb &= ~(RTC_CTRL_B_DM); 1954 else 1955 ctrlb |= RTC_CTRL_B_DM; 1956 rtc->bcd_mode = pdata->bcd_mode; 1957 1958 /* 1959 * Disable Daylight Savings Time (DSE = 0). 1960 * The RTC has hardcoded timezone information that is rendered 1961 * obselete. We'll let the OS deal with DST settings instead. 1962 */ 1963 if (ctrlb & RTC_CTRL_B_DSE) 1964 ctrlb &= ~(RTC_CTRL_B_DSE); 1965 1966 /* Force 24-hour mode (2412 = 1). */ 1967 if (!(ctrlb & RTC_CTRL_B_2412)) { 1968 /* Reinitialize the time hours. */ 1969 hours = rtc->read(rtc, RTC_HRS); 1970 am_pm = hours & RTC_HRS_AMPM_MASK; 1971 hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK, 1972 RTC_HRS_12_BIN_MASK); 1973 hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours)); 1974 1975 /* Enable 24-hour mode. */ 1976 ctrlb |= RTC_CTRL_B_2412; 1977 1978 /* Write back to Control B, including DM & DSE bits. */ 1979 rtc->write(rtc, RTC_CTRL_B, ctrlb); 1980 1981 /* Write the time hours back. */ 1982 rtc->write(rtc, RTC_HRS, 1983 ds1685_rtc_bin2bcd(rtc, hours, 1984 RTC_HRS_24_BIN_MASK, 1985 RTC_HRS_24_BCD_MASK)); 1986 1987 /* Reinitialize the alarm hours. */ 1988 hours = rtc->read(rtc, RTC_HRS_ALARM); 1989 am_pm = hours & RTC_HRS_AMPM_MASK; 1990 hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK, 1991 RTC_HRS_12_BIN_MASK); 1992 hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours)); 1993 1994 /* Write the alarm hours back. */ 1995 rtc->write(rtc, RTC_HRS_ALARM, 1996 ds1685_rtc_bin2bcd(rtc, hours, 1997 RTC_HRS_24_BIN_MASK, 1998 RTC_HRS_24_BCD_MASK)); 1999 } else { 2000 /* 24-hour mode is already set, so write Control B back. */ 2001 rtc->write(rtc, RTC_CTRL_B, ctrlb); 2002 } 2003 2004 /* Unset the SET bit in Control B so the RTC can update. */ 2005 rtc->write(rtc, RTC_CTRL_B, 2006 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET))); 2007 2008 /* Check the main battery. */ 2009 if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT)) 2010 dev_warn(&pdev->dev, 2011 "Main battery is exhausted! RTC may be invalid!\n"); 2012 2013 /* Check the auxillary battery. It is optional. */ 2014 if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2)) 2015 dev_warn(&pdev->dev, 2016 "Aux battery is exhausted or not available.\n"); 2017 2018 /* Read Ctrl B and clear PIE/AIE/UIE. */ 2019 rtc->write(rtc, RTC_CTRL_B, 2020 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK))); 2021 2022 /* Reading Ctrl C auto-clears PF/AF/UF. */ 2023 rtc->read(rtc, RTC_CTRL_C); 2024 2025 /* Read Ctrl 4B and clear RIE/WIE/KSE. */ 2026 rtc->write(rtc, RTC_EXT_CTRL_4B, 2027 (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK))); 2028 2029 /* Clear RF/WF/KF in Ctrl 4A. */ 2030 rtc->write(rtc, RTC_EXT_CTRL_4A, 2031 (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK))); 2032 2033 /* 2034 * Re-enable KSE to handle power button events. We do not enable 2035 * WIE or RIE by default. 2036 */ 2037 rtc->write(rtc, RTC_EXT_CTRL_4B, 2038 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE)); 2039 2040 rtc_dev = devm_rtc_allocate_device(&pdev->dev); 2041 if (IS_ERR(rtc_dev)) 2042 return PTR_ERR(rtc_dev); 2043 2044 rtc_dev->ops = &ds1685_rtc_ops; 2045 2046 /* Century bit is useless because leap year fails in 1900 and 2100 */ 2047 rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000; 2048 rtc_dev->range_max = RTC_TIMESTAMP_END_2099; 2049 2050 /* Maximum periodic rate is 8192Hz (0.122070ms). */ 2051 rtc_dev->max_user_freq = RTC_MAX_USER_FREQ; 2052 2053 /* See if the platform doesn't support UIE. */ 2054 if (pdata->uie_unsupported) 2055 rtc_dev->uie_unsupported = 1; 2056 rtc->uie_unsupported = pdata->uie_unsupported; 2057 2058 rtc->dev = rtc_dev; 2059 2060 /* 2061 * Fetch the IRQ and setup the interrupt handler. 2062 * 2063 * Not all platforms have the IRQF pin tied to something. If not, the 2064 * RTC will still set the *IE / *F flags and raise IRQF in ctrlc, but 2065 * there won't be an automatic way of notifying the kernel about it, 2066 * unless ctrlc is explicitly polled. 2067 */ 2068 if (!pdata->no_irq) { 2069 ret = platform_get_irq(pdev, 0); 2070 if (ret > 0) { 2071 rtc->irq_num = ret; 2072 2073 /* Request an IRQ. */ 2074 ret = devm_request_irq(&pdev->dev, rtc->irq_num, 2075 ds1685_rtc_irq_handler, 2076 IRQF_SHARED, pdev->name, pdev); 2077 2078 /* Check to see if something came back. */ 2079 if (unlikely(ret)) { 2080 dev_warn(&pdev->dev, 2081 "RTC interrupt not available\n"); 2082 rtc->irq_num = 0; 2083 } 2084 } else 2085 return ret; 2086 } 2087 rtc->no_irq = pdata->no_irq; 2088 2089 /* Setup complete. */ 2090 ds1685_rtc_switch_to_bank0(rtc); 2091 2092 #ifdef CONFIG_SYSFS 2093 ret = ds1685_rtc_sysfs_register(&pdev->dev); 2094 if (ret) 2095 return ret; 2096 #endif 2097 2098 return rtc_register_device(rtc_dev); 2099 } 2100 2101 /** 2102 * ds1685_rtc_remove - removes rtc driver. 2103 * @pdev: pointer to platform_device structure. 2104 */ 2105 static int 2106 ds1685_rtc_remove(struct platform_device *pdev) 2107 { 2108 struct ds1685_priv *rtc = platform_get_drvdata(pdev); 2109 2110 #ifdef CONFIG_SYSFS 2111 ds1685_rtc_sysfs_unregister(&pdev->dev); 2112 #endif 2113 2114 /* Read Ctrl B and clear PIE/AIE/UIE. */ 2115 rtc->write(rtc, RTC_CTRL_B, 2116 (rtc->read(rtc, RTC_CTRL_B) & 2117 ~(RTC_CTRL_B_PAU_MASK))); 2118 2119 /* Reading Ctrl C auto-clears PF/AF/UF. */ 2120 rtc->read(rtc, RTC_CTRL_C); 2121 2122 /* Read Ctrl 4B and clear RIE/WIE/KSE. */ 2123 rtc->write(rtc, RTC_EXT_CTRL_4B, 2124 (rtc->read(rtc, RTC_EXT_CTRL_4B) & 2125 ~(RTC_CTRL_4B_RWK_MASK))); 2126 2127 /* Manually clear RF/WF/KF in Ctrl 4A. */ 2128 rtc->write(rtc, RTC_EXT_CTRL_4A, 2129 (rtc->read(rtc, RTC_EXT_CTRL_4A) & 2130 ~(RTC_CTRL_4A_RWK_MASK))); 2131 2132 cancel_work_sync(&rtc->work); 2133 2134 return 0; 2135 } 2136 2137 /** 2138 * ds1685_rtc_driver - rtc driver properties. 2139 */ 2140 static struct platform_driver ds1685_rtc_driver = { 2141 .driver = { 2142 .name = "rtc-ds1685", 2143 }, 2144 .probe = ds1685_rtc_probe, 2145 .remove = ds1685_rtc_remove, 2146 }; 2147 module_platform_driver(ds1685_rtc_driver); 2148 /* ----------------------------------------------------------------------- */ 2149 2150 2151 /* ----------------------------------------------------------------------- */ 2152 /* Poweroff function */ 2153 2154 /** 2155 * ds1685_rtc_poweroff - uses the RTC chip to power the system off. 2156 * @pdev: pointer to platform_device structure. 2157 */ 2158 void __noreturn 2159 ds1685_rtc_poweroff(struct platform_device *pdev) 2160 { 2161 u8 ctrla, ctrl4a, ctrl4b; 2162 struct ds1685_priv *rtc; 2163 2164 /* Check for valid RTC data, else, spin forever. */ 2165 if (unlikely(!pdev)) { 2166 pr_emerg("platform device data not available, spinning forever ...\n"); 2167 while(1); 2168 unreachable(); 2169 } else { 2170 /* Get the rtc data. */ 2171 rtc = platform_get_drvdata(pdev); 2172 2173 /* 2174 * Disable our IRQ. We're powering down, so we're not 2175 * going to worry about cleaning up. Most of that should 2176 * have been taken care of by the shutdown scripts and this 2177 * is the final function call. 2178 */ 2179 if (!rtc->no_irq) 2180 disable_irq_nosync(rtc->irq_num); 2181 2182 /* Oscillator must be on and the countdown chain enabled. */ 2183 ctrla = rtc->read(rtc, RTC_CTRL_A); 2184 ctrla |= RTC_CTRL_A_DV1; 2185 ctrla &= ~(RTC_CTRL_A_DV2); 2186 rtc->write(rtc, RTC_CTRL_A, ctrla); 2187 2188 /* 2189 * Read Control 4A and check the status of the auxillary 2190 * battery. This must be present and working (VRT2 = 1) 2191 * for wakeup and kickstart functionality to be useful. 2192 */ 2193 ds1685_rtc_switch_to_bank1(rtc); 2194 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); 2195 if (ctrl4a & RTC_CTRL_4A_VRT2) { 2196 /* Clear all of the interrupt flags on Control 4A. */ 2197 ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK); 2198 rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a); 2199 2200 /* 2201 * The auxillary battery is present and working. 2202 * Enable extended functions (ABE=1), enable 2203 * wake-up (WIE=1), and enable kickstart (KSE=1) 2204 * in Control 4B. 2205 */ 2206 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); 2207 ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE | 2208 RTC_CTRL_4B_KSE); 2209 rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b); 2210 } 2211 2212 /* Set PAB to 1 in Control 4A to power the system down. */ 2213 dev_warn(&pdev->dev, "Powerdown.\n"); 2214 msleep(20); 2215 rtc->write(rtc, RTC_EXT_CTRL_4A, 2216 (ctrl4a | RTC_CTRL_4A_PAB)); 2217 2218 /* Spin ... we do not switch back to bank0. */ 2219 while(1); 2220 unreachable(); 2221 } 2222 } 2223 EXPORT_SYMBOL(ds1685_rtc_poweroff); 2224 /* ----------------------------------------------------------------------- */ 2225 2226 2227 MODULE_AUTHOR("Joshua Kinard <kumba@gentoo.org>"); 2228 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>"); 2229 MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver"); 2230 MODULE_LICENSE("GPL"); 2231 MODULE_ALIAS("platform:rtc-ds1685"); 2232