Lines Matching +full:read +full:- +full:to +full:- +full:read
1 // SPDX-License-Identifier: GPL-2.0-only
3 * An rtc driver for the Dallas/Maxim DS1685/DS1687 and related real-time
6 * Copyright (C) 2011-2014 Joshua Kinard <kumba@gentoo.org>.
7 * Copyright (C) 2009 Matthias Fuchs <matthias.fuchs@esd-electronics.com>.
10 * DS1685/DS1687 3V/5V Real-Time Clocks, 19-5215, Rev 4/10.
11 * DS17x85/DS17x87 3V/5V Real-Time Clocks, 19-5222, Rev 4/10.
12 * DS1689/DS1693 3V/5V Serialized Real-Time Clocks, Rev 112105.
33 /* ----------------------------------------------------------------------- */
35 * Standard read/write
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.
47 return readb((u8 __iomem *)rtc->regs + in ds1685_read()
48 (reg * rtc->regstep)); in ds1685_read()
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.
60 writeb(value, ((u8 __iomem *)rtc->regs + in ds1685_write()
61 (reg * rtc->regstep))); in ds1685_write()
63 /* ----------------------------------------------------------------------- */
66 * Indirect read/write functions
71 * ds1685_indirect_read - read a value from an rtc register.
72 * @rtc: pointer to the ds1685 rtc structure.
73 * @reg: the register address to read.
78 writeb(reg, rtc->regs); in ds1685_indirect_read()
79 return readb(rtc->data); in ds1685_indirect_read()
83 * ds1685_indirect_write - write a value to an rtc register.
84 * @rtc: pointer to the ds1685 rtc structure.
85 * @reg: the register address to write.
86 * @value: value to write to the register.
91 writeb(reg, rtc->regs); in ds1685_indirect_write()
92 writeb(value, rtc->data); in ds1685_indirect_write()
95 /* ----------------------------------------------------------------------- */
99 * ds1685_rtc_bcd2bin - bcd2bin wrapper in case platform doesn't support BCD.
100 * @rtc: pointer to the ds1685 rtc structure.
101 * @val: u8 time value to consider converting.
105 * Returns the value, converted to BIN if originally in BCD and bcd_mode TRUE.
110 if (rtc->bcd_mode) in ds1685_rtc_bcd2bin()
117 * ds1685_rtc_bin2bcd - bin2bcd wrapper in case platform doesn't support BCD.
118 * @rtc: pointer to the ds1685 rtc structure.
119 * @val: u8 time value to consider converting.
123 * Returns the value, converted to BCD if originally in BIN and bcd_mode TRUE.
128 if (rtc->bcd_mode) in ds1685_rtc_bin2bcd()
135 * s1685_rtc_check_mday - check validity of the day of month.
136 * @rtc: pointer to the ds1685 rtc structure.
139 * Returns -EDOM if the day of month is not within 1..31 range.
144 if (rtc->bcd_mode) { in ds1685_rtc_check_mday()
146 return -EDOM; in ds1685_rtc_check_mday()
149 return -EDOM; in ds1685_rtc_check_mday()
155 * ds1685_rtc_switch_to_bank0 - switch the rtc to bank 0.
156 * @rtc: pointer to the ds1685 rtc structure.
161 rtc->write(rtc, RTC_CTRL_A, in ds1685_rtc_switch_to_bank0()
162 (rtc->read(rtc, RTC_CTRL_A) & ~(RTC_CTRL_A_DV0))); in ds1685_rtc_switch_to_bank0()
166 * ds1685_rtc_switch_to_bank1 - switch the rtc to bank 1.
167 * @rtc: pointer to the ds1685 rtc structure.
172 rtc->write(rtc, RTC_CTRL_A, in ds1685_rtc_switch_to_bank1()
173 (rtc->read(rtc, RTC_CTRL_A) | RTC_CTRL_A_DV0)); in ds1685_rtc_switch_to_bank1()
177 * ds1685_rtc_begin_data_access - prepare the rtc for data access.
178 * @rtc: pointer to the ds1685 rtc structure.
180 * This takes several steps to prepare the rtc for access to get/set time
182 * - Sets the SET bit in Control Register B.
183 * - Reads Ext Control Register 4A and checks the INCR bit.
184 * - If INCR is active, a short delay is added before Ext Control Register 4A
185 * is read again in a loop until INCR is inactive.
186 * - Switches the rtc to bank 1. This allows access to all relevant
193 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_begin_data_access()
194 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET)); in ds1685_rtc_begin_data_access()
196 /* Switch to Bank 1 */ in ds1685_rtc_begin_data_access()
199 /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */ in ds1685_rtc_begin_data_access()
200 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR) in ds1685_rtc_begin_data_access()
205 * ds1685_rtc_end_data_access - end data access on the rtc.
206 * @rtc: pointer to the ds1685 rtc structure.
209 * - Switches the rtc back to bank 0.
210 * - Clears the SET bit in Control Register B.
215 /* Switch back to Bank 0 */ in ds1685_rtc_end_data_access()
219 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_end_data_access()
220 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET))); in ds1685_rtc_end_data_access()
224 * ds1685_rtc_get_ssn - retrieve the silicon serial number.
225 * @rtc: pointer to the ds1685 rtc structure.
226 * @ssn: u8 array to hold the bits of the silicon serial number.
228 * This number starts at 0x40, and is 8-bytes long, ending at 0x47. The
234 * called first before calling this function, else data will be read out of
235 * the bank0 NVRAM. Be sure to call ds1685_rtc_switch_to_bank0 when done.
240 ssn[0] = rtc->read(rtc, RTC_BANK1_SSN_MODEL); in ds1685_rtc_get_ssn()
241 ssn[1] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_1); in ds1685_rtc_get_ssn()
242 ssn[2] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_2); in ds1685_rtc_get_ssn()
243 ssn[3] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_3); in ds1685_rtc_get_ssn()
244 ssn[4] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_4); in ds1685_rtc_get_ssn()
245 ssn[5] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_5); in ds1685_rtc_get_ssn()
246 ssn[6] = rtc->read(rtc, RTC_BANK1_SSN_BYTE_6); in ds1685_rtc_get_ssn()
247 ssn[7] = rtc->read(rtc, RTC_BANK1_SSN_CRC); in ds1685_rtc_get_ssn()
249 /* ----------------------------------------------------------------------- */
252 /* ----------------------------------------------------------------------- */
253 /* Read/Set Time & Alarm functions */
256 * ds1685_rtc_read_time - reads the time registers.
257 * @dev: pointer to device structure.
258 * @tm: pointer to rtc_time structure.
269 seconds = rtc->read(rtc, RTC_SECS); in ds1685_rtc_read_time()
270 minutes = rtc->read(rtc, RTC_MINS); in ds1685_rtc_read_time()
271 hours = rtc->read(rtc, RTC_HRS); in ds1685_rtc_read_time()
272 wday = rtc->read(rtc, RTC_WDAY); in ds1685_rtc_read_time()
273 mday = rtc->read(rtc, RTC_MDAY); in ds1685_rtc_read_time()
274 month = rtc->read(rtc, RTC_MONTH); in ds1685_rtc_read_time()
275 years = rtc->read(rtc, RTC_YEAR); in ds1685_rtc_read_time()
276 century = rtc->read(rtc, RTC_CENTURY); in ds1685_rtc_read_time()
279 /* bcd2bin if needed, perform fixups, and store to rtc_time. */ in ds1685_rtc_read_time()
284 tm->tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, RTC_SECS_BCD_MASK, in ds1685_rtc_read_time()
286 tm->tm_min = ds1685_rtc_bcd2bin(rtc, minutes, RTC_MINS_BCD_MASK, in ds1685_rtc_read_time()
288 tm->tm_hour = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_24_BCD_MASK, in ds1685_rtc_read_time()
290 tm->tm_wday = (ds1685_rtc_bcd2bin(rtc, wday, RTC_WDAY_MASK, in ds1685_rtc_read_time()
291 RTC_WDAY_MASK) - 1); in ds1685_rtc_read_time()
292 tm->tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK, in ds1685_rtc_read_time()
294 tm->tm_mon = (ds1685_rtc_bcd2bin(rtc, month, RTC_MONTH_BCD_MASK, in ds1685_rtc_read_time()
295 RTC_MONTH_BIN_MASK) - 1); in ds1685_rtc_read_time()
296 tm->tm_year = ((years + (century * 100)) - 1900); in ds1685_rtc_read_time()
297 tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year); in ds1685_rtc_read_time()
298 tm->tm_isdst = 0; /* RTC has hardcoded timezone, so don't use. */ in ds1685_rtc_read_time()
304 * ds1685_rtc_set_time - sets the time registers.
305 * @dev: pointer to device structure.
306 * @tm: pointer to rtc_time structure.
315 seconds = ds1685_rtc_bin2bcd(rtc, tm->tm_sec, RTC_SECS_BIN_MASK, in ds1685_rtc_set_time()
317 minutes = ds1685_rtc_bin2bcd(rtc, tm->tm_min, RTC_MINS_BIN_MASK, in ds1685_rtc_set_time()
319 hours = ds1685_rtc_bin2bcd(rtc, tm->tm_hour, RTC_HRS_24_BIN_MASK, in ds1685_rtc_set_time()
321 wday = ds1685_rtc_bin2bcd(rtc, (tm->tm_wday + 1), RTC_WDAY_MASK, in ds1685_rtc_set_time()
323 mday = ds1685_rtc_bin2bcd(rtc, tm->tm_mday, RTC_MDAY_BIN_MASK, in ds1685_rtc_set_time()
325 month = ds1685_rtc_bin2bcd(rtc, (tm->tm_mon + 1), RTC_MONTH_BIN_MASK, in ds1685_rtc_set_time()
327 years = ds1685_rtc_bin2bcd(rtc, (tm->tm_year % 100), in ds1685_rtc_set_time()
329 century = ds1685_rtc_bin2bcd(rtc, ((tm->tm_year + 1900) / 100), in ds1685_rtc_set_time()
334 * - Months: !> 12, Month Day != 0. in ds1685_rtc_set_time()
335 * - Month Day !> Max days in current month. in ds1685_rtc_set_time()
336 * - Hours !>= 24, Mins !>= 60, Secs !>= 60, & Weekday !> 7. in ds1685_rtc_set_time()
338 if ((tm->tm_mon > 11) || (mday == 0)) in ds1685_rtc_set_time()
339 return -EDOM; in ds1685_rtc_set_time()
341 if (tm->tm_mday > rtc_month_days(tm->tm_mon, tm->tm_year)) in ds1685_rtc_set_time()
342 return -EDOM; in ds1685_rtc_set_time()
344 if ((tm->tm_hour >= 24) || (tm->tm_min >= 60) || in ds1685_rtc_set_time()
345 (tm->tm_sec >= 60) || (wday > 7)) in ds1685_rtc_set_time()
346 return -EDOM; in ds1685_rtc_set_time()
349 * Set the data mode to use and store the time values in the in ds1685_rtc_set_time()
353 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_set_time()
354 if (rtc->bcd_mode) in ds1685_rtc_set_time()
358 rtc->write(rtc, RTC_CTRL_B, ctrlb); in ds1685_rtc_set_time()
359 rtc->write(rtc, RTC_SECS, seconds); in ds1685_rtc_set_time()
360 rtc->write(rtc, RTC_MINS, minutes); in ds1685_rtc_set_time()
361 rtc->write(rtc, RTC_HRS, hours); in ds1685_rtc_set_time()
362 rtc->write(rtc, RTC_WDAY, wday); in ds1685_rtc_set_time()
363 rtc->write(rtc, RTC_MDAY, mday); in ds1685_rtc_set_time()
364 rtc->write(rtc, RTC_MONTH, month); in ds1685_rtc_set_time()
365 rtc->write(rtc, RTC_YEAR, years); in ds1685_rtc_set_time()
366 rtc->write(rtc, RTC_CENTURY, century); in ds1685_rtc_set_time()
373 * ds1685_rtc_read_alarm - reads the alarm registers.
374 * @dev: pointer to device structure.
375 * @alrm: pointer to rtc_wkalrm structure.
380 * "don't care" value ranging from 0xc0 to 0xff may be written into one or
381 * more of the three alarm bytes to act as a wildcard value. The fourth
393 seconds = rtc->read(rtc, RTC_SECS_ALARM); in ds1685_rtc_read_alarm()
394 minutes = rtc->read(rtc, RTC_MINS_ALARM); in ds1685_rtc_read_alarm()
395 hours = rtc->read(rtc, RTC_HRS_ALARM); in ds1685_rtc_read_alarm()
396 mday = rtc->read(rtc, RTC_MDAY_ALARM); in ds1685_rtc_read_alarm()
397 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_read_alarm()
398 ctrlc = rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_read_alarm()
414 alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds, in ds1685_rtc_read_alarm()
419 alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes, in ds1685_rtc_read_alarm()
424 alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours, in ds1685_rtc_read_alarm()
428 /* Write the data to rtc_wkalrm. */ in ds1685_rtc_read_alarm()
429 alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK, in ds1685_rtc_read_alarm()
431 alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE); in ds1685_rtc_read_alarm()
432 alrm->pending = !!(ctrlc & RTC_CTRL_C_AF); in ds1685_rtc_read_alarm()
438 * ds1685_rtc_set_alarm - sets the alarm in registers.
439 * @dev: pointer to device structure.
440 * @alrm: pointer to rtc_wkalrm structure.
449 /* Fetch the alarm info and convert to BCD. */ in ds1685_rtc_set_alarm()
450 seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec, in ds1685_rtc_set_alarm()
453 minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min, in ds1685_rtc_set_alarm()
456 hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour, in ds1685_rtc_set_alarm()
459 mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday, in ds1685_rtc_set_alarm()
472 * of this RTC chip because rtc_valid_tm tries to validate every in ds1685_rtc_set_alarm()
485 alrm->time.tm_mon = -1; in ds1685_rtc_set_alarm()
486 alrm->time.tm_year = -1; in ds1685_rtc_set_alarm()
487 alrm->time.tm_wday = -1; in ds1685_rtc_set_alarm()
488 alrm->time.tm_yday = -1; in ds1685_rtc_set_alarm()
489 alrm->time.tm_isdst = -1; in ds1685_rtc_set_alarm()
493 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_set_alarm()
494 rtc->write(rtc, RTC_CTRL_B, (ctrlb & ~(RTC_CTRL_B_AIE))); in ds1685_rtc_set_alarm()
496 /* Read ctrlc to clear RTC_CTRL_C_AF. */ in ds1685_rtc_set_alarm()
497 rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_set_alarm()
500 * Set the data mode to use and store the time values in the in ds1685_rtc_set_alarm()
503 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_set_alarm()
504 if (rtc->bcd_mode) in ds1685_rtc_set_alarm()
508 rtc->write(rtc, RTC_CTRL_B, ctrlb); in ds1685_rtc_set_alarm()
509 rtc->write(rtc, RTC_SECS_ALARM, seconds); in ds1685_rtc_set_alarm()
510 rtc->write(rtc, RTC_MINS_ALARM, minutes); in ds1685_rtc_set_alarm()
511 rtc->write(rtc, RTC_HRS_ALARM, hours); in ds1685_rtc_set_alarm()
512 rtc->write(rtc, RTC_MDAY_ALARM, mday); in ds1685_rtc_set_alarm()
514 /* Re-enable the alarm if needed. */ in ds1685_rtc_set_alarm()
515 if (alrm->enabled) { in ds1685_rtc_set_alarm()
516 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_set_alarm()
518 rtc->write(rtc, RTC_CTRL_B, ctrlb); in ds1685_rtc_set_alarm()
526 /* ----------------------------------------------------------------------- */
529 /* ----------------------------------------------------------------------- */
533 * ds1685_rtc_alarm_irq_enable - replaces ioctl() RTC_AIE on/off.
534 * @dev: pointer to device structure.
535 * @enabled: flag indicating whether to enable or disable.
542 /* Flip the requisite interrupt-enable bit. */ in ds1685_rtc_alarm_irq_enable()
544 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) | in ds1685_rtc_alarm_irq_enable()
547 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) & in ds1685_rtc_alarm_irq_enable()
550 /* Read Control C to clear all the flag bits. */ in ds1685_rtc_alarm_irq_enable()
551 rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_alarm_irq_enable()
555 /* ----------------------------------------------------------------------- */
558 /* ----------------------------------------------------------------------- */
562 * ds1685_rtc_extended_irq - take care of extended interrupts
563 * @rtc: pointer to the ds1685 rtc structure.
572 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); in ds1685_rtc_extended_irq()
573 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); in ds1685_rtc_extended_irq()
581 /* Briefly disable kickstarts to debounce button presses. */ in ds1685_rtc_extended_irq()
582 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_extended_irq()
583 (rtc->read(rtc, RTC_EXT_CTRL_4B) & in ds1685_rtc_extended_irq()
587 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_extended_irq()
592 * Sleep 500ms before re-enabling kickstarts. This allows in ds1685_rtc_extended_irq()
593 * adequate time to avoid reading signal jitter as additional in ds1685_rtc_extended_irq()
597 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_extended_irq()
598 (rtc->read(rtc, RTC_EXT_CTRL_4B) | in ds1685_rtc_extended_irq()
601 /* Call the platform pre-poweroff function. Else, shutdown. */ in ds1685_rtc_extended_irq()
602 if (rtc->prepare_poweroff != NULL) in ds1685_rtc_extended_irq()
603 rtc->prepare_poweroff(); in ds1685_rtc_extended_irq()
609 * Check for a wake-up interrupt. With Vcc applied, this is in ds1685_rtc_extended_irq()
611 * account the 'date' register in bank1 in addition to the in ds1685_rtc_extended_irq()
615 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_extended_irq()
619 if (rtc->wake_alarm != NULL) in ds1685_rtc_extended_irq()
620 rtc->wake_alarm(); in ds1685_rtc_extended_irq()
622 dev_warn(&pdev->dev, in ds1685_rtc_extended_irq()
627 * Check for a ram-clear interrupt. This happens if RIE=1 and RF=0 in ds1685_rtc_extended_irq()
629 * each byte to a logic 1. This has no effect on any extended in ds1685_rtc_extended_irq()
630 * NV-SRAM that might be present, nor on the time/calendar/alarm in ds1685_rtc_extended_irq()
631 * registers. After a ram-clear is completed, there is a minimum in ds1685_rtc_extended_irq()
633 * NOTE: A ram-clear can still occur if RCE=1 and RIE=0. We cannot in ds1685_rtc_extended_irq()
637 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_extended_irq()
642 if (rtc->post_ram_clear != NULL) in ds1685_rtc_extended_irq()
643 rtc->post_ram_clear(); in ds1685_rtc_extended_irq()
645 dev_warn(&pdev->dev, in ds1685_rtc_extended_irq()
646 "RAM-Clear IRQ just occurred!\n"); in ds1685_rtc_extended_irq()
652 * ds1685_rtc_irq_handler - IRQ handler.
670 rtc_mutex = &rtc->dev->ops_lock; in ds1685_rtc_irq_handler()
673 /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */ in ds1685_rtc_irq_handler()
674 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_irq_handler()
675 ctrlc = rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_irq_handler()
680 * We need to determine if it was one of the standard in ds1685_rtc_irq_handler()
715 rtc_update_irq(rtc->dev, num_irqs, events); in ds1685_rtc_irq_handler()
720 /* ----------------------------------------------------------------------- */
723 /* ----------------------------------------------------------------------- */
741 * Square-Wave Output Frequencies.
749 * ds1685_rtc_proc - procfs access function.
750 * @dev: pointer to device structure.
751 * @seq: pointer to seq_file structure.
760 /* Read all the relevant data from the control registers. */ in ds1685_rtc_proc()
763 ctrla = rtc->read(rtc, RTC_CTRL_A); in ds1685_rtc_proc()
764 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_proc()
765 ctrld = rtc->read(rtc, RTC_CTRL_D); in ds1685_rtc_proc()
766 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); in ds1685_rtc_proc()
767 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); in ds1685_rtc_proc()
808 ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"), in ds1685_rtc_proc()
825 /* ----------------------------------------------------------------------- */
828 /* ----------------------------------------------------------------------- */
840 /* ----------------------------------------------------------------------- */
846 struct mutex *rtc_mutex = &rtc->dev->ops_lock; in ds1685_nvram_read()
857 /* Read NVRAM in time and bank0 registers. */ in ds1685_nvram_read()
859 count++, size--) { in ds1685_nvram_read()
861 *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++)); in ds1685_nvram_read()
863 *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++)); in ds1685_nvram_read()
871 /* Enable burst-mode on DS17x85/DS17x87 */ in ds1685_nvram_read()
872 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_nvram_read()
873 (rtc->read(rtc, RTC_EXT_CTRL_4A) | in ds1685_nvram_read()
876 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start in ds1685_nvram_read()
877 * reading with burst-mode */ in ds1685_nvram_read()
878 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB, in ds1685_nvram_read()
879 (pos - NVRAM_TOTAL_SZ_BANK0)); in ds1685_nvram_read()
882 /* Read NVRAM in bank1 registers. */ in ds1685_nvram_read()
884 count++, size--) { in ds1685_nvram_read()
886 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR in ds1685_nvram_read()
887 * before each read. */ in ds1685_nvram_read()
888 rtc->write(rtc, RTC_BANK1_RAM_ADDR, in ds1685_nvram_read()
889 (pos - NVRAM_TOTAL_SZ_BANK0)); in ds1685_nvram_read()
891 *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT); in ds1685_nvram_read()
896 /* Disable burst-mode on DS17x85/DS17x87 */ in ds1685_nvram_read()
897 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_nvram_read()
898 (rtc->read(rtc, RTC_EXT_CTRL_4A) & in ds1685_nvram_read()
913 struct mutex *rtc_mutex = &rtc->dev->ops_lock; in ds1685_nvram_write()
926 count++, size--) in ds1685_nvram_write()
928 rtc->write(rtc, (NVRAM_TIME_BASE + pos++), in ds1685_nvram_write()
931 rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++); in ds1685_nvram_write()
938 /* Enable burst-mode on DS17x85/DS17x87 */ in ds1685_nvram_write()
939 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_nvram_write()
940 (rtc->read(rtc, RTC_EXT_CTRL_4A) | in ds1685_nvram_write()
943 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start in ds1685_nvram_write()
944 * writing with burst-mode */ in ds1685_nvram_write()
945 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB, in ds1685_nvram_write()
946 (pos - NVRAM_TOTAL_SZ_BANK0)); in ds1685_nvram_write()
951 count++, size--) { in ds1685_nvram_write()
953 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR in ds1685_nvram_write()
954 * before each read. */ in ds1685_nvram_write()
955 rtc->write(rtc, RTC_BANK1_RAM_ADDR, in ds1685_nvram_write()
956 (pos - NVRAM_TOTAL_SZ_BANK0)); in ds1685_nvram_write()
958 rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++); in ds1685_nvram_write()
963 /* Disable burst-mode on DS17x85/DS17x87 */ in ds1685_nvram_write()
964 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_nvram_write()
965 (rtc->read(rtc, RTC_EXT_CTRL_4A) & in ds1685_nvram_write()
976 /* ----------------------------------------------------------------------- */
980 * ds1685_rtc_sysfs_battery_show - sysfs file for main battery status.
981 * @dev: pointer to device structure.
982 * @attr: pointer to device_attribute structure.
983 * @buf: pointer to char array to hold the output.
989 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); in ds1685_rtc_sysfs_battery_show()
992 ctrld = rtc->read(rtc, RTC_CTRL_D); in ds1685_rtc_sysfs_battery_show()
1000 * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status.
1001 * @dev: pointer to device structure.
1002 * @attr: pointer to device_attribute structure.
1003 * @buf: pointer to char array to hold the output.
1009 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); in ds1685_rtc_sysfs_auxbatt_show()
1013 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); in ds1685_rtc_sysfs_auxbatt_show()
1022 * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number.
1023 * @dev: pointer to device structure.
1024 * @attr: pointer to device_attribute structure.
1025 * @buf: pointer to char array to hold the output.
1031 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); in ds1685_rtc_sysfs_serial_show()
1043 * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features.
1054 * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features.
1062 /* ----------------------------------------------------------------------- */
1066 * ds1685_rtc_probe - initializes rtc driver.
1067 * @pdev: pointer to platform_device structure.
1086 pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data; in ds1685_rtc_probe()
1088 return -ENODEV; in ds1685_rtc_probe()
1091 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); in ds1685_rtc_probe()
1093 return -ENOMEM; in ds1685_rtc_probe()
1096 switch (pdata->access_type) { in ds1685_rtc_probe()
1098 rtc->regs = devm_platform_ioremap_resource(pdev, 0); in ds1685_rtc_probe()
1099 if (IS_ERR(rtc->regs)) in ds1685_rtc_probe()
1100 return PTR_ERR(rtc->regs); in ds1685_rtc_probe()
1101 rtc->read = ds1685_read; in ds1685_rtc_probe()
1102 rtc->write = ds1685_write; in ds1685_rtc_probe()
1105 rtc->regs = devm_platform_ioremap_resource(pdev, 0); in ds1685_rtc_probe()
1106 if (IS_ERR(rtc->regs)) in ds1685_rtc_probe()
1107 return PTR_ERR(rtc->regs); in ds1685_rtc_probe()
1108 rtc->data = devm_platform_ioremap_resource(pdev, 1); in ds1685_rtc_probe()
1109 if (IS_ERR(rtc->data)) in ds1685_rtc_probe()
1110 return PTR_ERR(rtc->data); in ds1685_rtc_probe()
1111 rtc->read = ds1685_indirect_read; in ds1685_rtc_probe()
1112 rtc->write = ds1685_indirect_write; in ds1685_rtc_probe()
1116 if (!rtc->read || !rtc->write) in ds1685_rtc_probe()
1117 return -ENXIO; in ds1685_rtc_probe()
1120 if (pdata->regstep > 0) in ds1685_rtc_probe()
1121 rtc->regstep = pdata->regstep; in ds1685_rtc_probe()
1123 rtc->regstep = 1; in ds1685_rtc_probe()
1125 /* Platform pre-shutdown function, if defined. */ in ds1685_rtc_probe()
1126 if (pdata->plat_prepare_poweroff) in ds1685_rtc_probe()
1127 rtc->prepare_poweroff = pdata->plat_prepare_poweroff; in ds1685_rtc_probe()
1130 if (pdata->plat_wake_alarm) in ds1685_rtc_probe()
1131 rtc->wake_alarm = pdata->plat_wake_alarm; in ds1685_rtc_probe()
1134 if (pdata->plat_post_ram_clear) in ds1685_rtc_probe()
1135 rtc->post_ram_clear = pdata->plat_post_ram_clear; in ds1685_rtc_probe()
1141 ctrla = rtc->read(rtc, RTC_CTRL_A); in ds1685_rtc_probe()
1148 /* Clear RS3-RS0 in Control A. */ in ds1685_rtc_probe()
1152 * All done with Control A. Switch to Bank 1 for the remainder of in ds1685_rtc_probe()
1153 * the RTC setup so we have access to the extended functions. in ds1685_rtc_probe()
1156 rtc->write(rtc, RTC_CTRL_A, ctrla); in ds1685_rtc_probe()
1158 /* Default to 32768kHz output. */ in ds1685_rtc_probe()
1159 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_probe()
1160 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K)); in ds1685_rtc_probe()
1163 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_probe()
1164 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET)); in ds1685_rtc_probe()
1166 /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */ in ds1685_rtc_probe()
1167 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR) in ds1685_rtc_probe()
1174 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_probe()
1175 if (pdata->bcd_mode) in ds1685_rtc_probe()
1179 rtc->bcd_mode = pdata->bcd_mode; in ds1685_rtc_probe()
1189 /* Force 24-hour mode (2412 = 1). */ in ds1685_rtc_probe()
1192 hours = rtc->read(rtc, RTC_HRS); in ds1685_rtc_probe()
1198 /* Enable 24-hour mode. */ in ds1685_rtc_probe()
1201 /* Write back to Control B, including DM & DSE bits. */ in ds1685_rtc_probe()
1202 rtc->write(rtc, RTC_CTRL_B, ctrlb); in ds1685_rtc_probe()
1205 rtc->write(rtc, RTC_HRS, in ds1685_rtc_probe()
1211 hours = rtc->read(rtc, RTC_HRS_ALARM); in ds1685_rtc_probe()
1218 rtc->write(rtc, RTC_HRS_ALARM, in ds1685_rtc_probe()
1223 /* 24-hour mode is already set, so write Control B back. */ in ds1685_rtc_probe()
1224 rtc->write(rtc, RTC_CTRL_B, ctrlb); in ds1685_rtc_probe()
1228 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_probe()
1229 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET))); in ds1685_rtc_probe()
1232 if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT)) in ds1685_rtc_probe()
1233 dev_warn(&pdev->dev, in ds1685_rtc_probe()
1237 if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2)) in ds1685_rtc_probe()
1238 dev_warn(&pdev->dev, in ds1685_rtc_probe()
1241 /* Read Ctrl B and clear PIE/AIE/UIE. */ in ds1685_rtc_probe()
1242 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_probe()
1243 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK))); in ds1685_rtc_probe()
1245 /* Reading Ctrl C auto-clears PF/AF/UF. */ in ds1685_rtc_probe()
1246 rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_probe()
1248 /* Read Ctrl 4B and clear RIE/WIE/KSE. */ in ds1685_rtc_probe()
1249 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_probe()
1250 (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK))); in ds1685_rtc_probe()
1253 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_probe()
1254 (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK))); in ds1685_rtc_probe()
1257 * Re-enable KSE to handle power button events. We do not enable in ds1685_rtc_probe()
1260 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_probe()
1261 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE)); in ds1685_rtc_probe()
1263 rtc_dev = devm_rtc_allocate_device(&pdev->dev); in ds1685_rtc_probe()
1267 rtc_dev->ops = &ds1685_rtc_ops; in ds1685_rtc_probe()
1270 rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000; in ds1685_rtc_probe()
1271 rtc_dev->range_max = RTC_TIMESTAMP_END_2099; in ds1685_rtc_probe()
1274 rtc_dev->max_user_freq = RTC_MAX_USER_FREQ; in ds1685_rtc_probe()
1277 if (pdata->uie_unsupported) in ds1685_rtc_probe()
1278 rtc_dev->uie_unsupported = 1; in ds1685_rtc_probe()
1280 rtc->dev = rtc_dev; in ds1685_rtc_probe()
1285 * Not all platforms have the IRQF pin tied to something. If not, the in ds1685_rtc_probe()
1290 if (!pdata->no_irq) { in ds1685_rtc_probe()
1295 rtc->irq_num = ret; in ds1685_rtc_probe()
1298 ret = devm_request_threaded_irq(&pdev->dev, rtc->irq_num, in ds1685_rtc_probe()
1301 pdev->name, pdev); in ds1685_rtc_probe()
1303 /* Check to see if something came back. */ in ds1685_rtc_probe()
1305 dev_warn(&pdev->dev, in ds1685_rtc_probe()
1307 rtc->irq_num = 0; in ds1685_rtc_probe()
1310 rtc->no_irq = pdata->no_irq; in ds1685_rtc_probe()
1319 rtc_dev->nvram_old_abi = true; in ds1685_rtc_probe()
1329 * ds1685_rtc_remove - removes rtc driver.
1330 * @pdev: pointer to platform_device structure.
1337 /* Read Ctrl B and clear PIE/AIE/UIE. */ in ds1685_rtc_remove()
1338 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_remove()
1339 (rtc->read(rtc, RTC_CTRL_B) & in ds1685_rtc_remove()
1342 /* Reading Ctrl C auto-clears PF/AF/UF. */ in ds1685_rtc_remove()
1343 rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_remove()
1345 /* Read Ctrl 4B and clear RIE/WIE/KSE. */ in ds1685_rtc_remove()
1346 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_remove()
1347 (rtc->read(rtc, RTC_EXT_CTRL_4B) & in ds1685_rtc_remove()
1351 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_remove()
1352 (rtc->read(rtc, RTC_EXT_CTRL_4A) & in ds1685_rtc_remove()
1359 * ds1685_rtc_driver - rtc driver properties.
1363 .name = "rtc-ds1685",
1369 /* ----------------------------------------------------------------------- */
1372 /* ----------------------------------------------------------------------- */
1376 * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
1377 * @pdev: pointer to platform_device structure.
1396 * going to worry about cleaning up. Most of that should in ds1685_rtc_poweroff()
1400 if (!rtc->no_irq) in ds1685_rtc_poweroff()
1401 disable_irq_nosync(rtc->irq_num); in ds1685_rtc_poweroff()
1404 ctrla = rtc->read(rtc, RTC_CTRL_A); in ds1685_rtc_poweroff()
1407 rtc->write(rtc, RTC_CTRL_A, ctrla); in ds1685_rtc_poweroff()
1410 * Read Control 4A and check the status of the auxillary in ds1685_rtc_poweroff()
1412 * for wakeup and kickstart functionality to be useful. in ds1685_rtc_poweroff()
1415 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); in ds1685_rtc_poweroff()
1419 rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a); in ds1685_rtc_poweroff()
1424 * wake-up (WIE=1), and enable kickstart (KSE=1) in ds1685_rtc_poweroff()
1427 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); in ds1685_rtc_poweroff()
1430 rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b); in ds1685_rtc_poweroff()
1433 /* Set PAB to 1 in Control 4A to power the system down. */ in ds1685_rtc_poweroff()
1434 dev_warn(&pdev->dev, "Powerdown.\n"); in ds1685_rtc_poweroff()
1436 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_poweroff()
1439 /* Spin ... we do not switch back to bank0. */ in ds1685_rtc_poweroff()
1445 /* ----------------------------------------------------------------------- */
1449 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
1450 MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
1452 MODULE_ALIAS("platform:rtc-ds1685");