Lines Matching +full:write +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 * ds1685_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.
669 rtc_lock(rtc->dev); in ds1685_rtc_irq_handler()
671 /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */ in ds1685_rtc_irq_handler()
672 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_irq_handler()
673 ctrlc = rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_irq_handler()
678 * We need to determine if it was one of the standard in ds1685_rtc_irq_handler()
713 rtc_update_irq(rtc->dev, num_irqs, events); in ds1685_rtc_irq_handler()
714 rtc_unlock(rtc->dev); in ds1685_rtc_irq_handler()
718 /* ----------------------------------------------------------------------- */
721 /* ----------------------------------------------------------------------- */
739 * Square-Wave Output Frequencies.
747 * ds1685_rtc_proc - procfs access function.
748 * @dev: pointer to device structure.
749 * @seq: pointer to seq_file structure.
758 /* Read all the relevant data from the control registers. */ in ds1685_rtc_proc()
761 ctrla = rtc->read(rtc, RTC_CTRL_A); in ds1685_rtc_proc()
762 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_proc()
763 ctrld = rtc->read(rtc, RTC_CTRL_D); in ds1685_rtc_proc()
764 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); in ds1685_rtc_proc()
765 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); in ds1685_rtc_proc()
806 ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"), in ds1685_rtc_proc()
823 /* ----------------------------------------------------------------------- */
826 /* ----------------------------------------------------------------------- */
838 /* ----------------------------------------------------------------------- */
844 struct mutex *rtc_mutex = &rtc->dev->ops_lock; in ds1685_nvram_read()
855 /* Read NVRAM in time and bank0 registers. */ in ds1685_nvram_read()
857 count++, size--) { in ds1685_nvram_read()
859 *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++)); in ds1685_nvram_read()
861 *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++)); in ds1685_nvram_read()
869 /* Enable burst-mode on DS17x85/DS17x87 */ in ds1685_nvram_read()
870 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_nvram_read()
871 (rtc->read(rtc, RTC_EXT_CTRL_4A) | in ds1685_nvram_read()
874 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start in ds1685_nvram_read()
875 * reading with burst-mode */ in ds1685_nvram_read()
876 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB, in ds1685_nvram_read()
877 (pos - NVRAM_TOTAL_SZ_BANK0)); in ds1685_nvram_read()
880 /* Read NVRAM in bank1 registers. */ in ds1685_nvram_read()
882 count++, size--) { in ds1685_nvram_read()
884 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR in ds1685_nvram_read()
885 * before each read. */ in ds1685_nvram_read()
886 rtc->write(rtc, RTC_BANK1_RAM_ADDR, in ds1685_nvram_read()
887 (pos - NVRAM_TOTAL_SZ_BANK0)); in ds1685_nvram_read()
889 *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT); in ds1685_nvram_read()
894 /* Disable burst-mode on DS17x85/DS17x87 */ in ds1685_nvram_read()
895 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_nvram_read()
896 (rtc->read(rtc, RTC_EXT_CTRL_4A) & in ds1685_nvram_read()
911 struct mutex *rtc_mutex = &rtc->dev->ops_lock; in ds1685_nvram_write()
922 /* Write NVRAM in time and bank0 registers. */ in ds1685_nvram_write()
924 count++, size--) in ds1685_nvram_write()
926 rtc->write(rtc, (NVRAM_TIME_BASE + pos++), in ds1685_nvram_write()
929 rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++); in ds1685_nvram_write()
936 /* Enable burst-mode on DS17x85/DS17x87 */ in ds1685_nvram_write()
937 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_nvram_write()
938 (rtc->read(rtc, RTC_EXT_CTRL_4A) | in ds1685_nvram_write()
941 /* We need one write to RTC_BANK1_RAM_ADDR_LSB to start in ds1685_nvram_write()
942 * writing with burst-mode */ in ds1685_nvram_write()
943 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB, in ds1685_nvram_write()
944 (pos - NVRAM_TOTAL_SZ_BANK0)); in ds1685_nvram_write()
947 /* Write NVRAM in bank1 registers. */ in ds1685_nvram_write()
949 count++, size--) { in ds1685_nvram_write()
951 /* DS1685/DS1687 has to write to RTC_BANK1_RAM_ADDR in ds1685_nvram_write()
952 * before each read. */ in ds1685_nvram_write()
953 rtc->write(rtc, RTC_BANK1_RAM_ADDR, in ds1685_nvram_write()
954 (pos - NVRAM_TOTAL_SZ_BANK0)); in ds1685_nvram_write()
956 rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++); in ds1685_nvram_write()
961 /* Disable burst-mode on DS17x85/DS17x87 */ in ds1685_nvram_write()
962 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_nvram_write()
963 (rtc->read(rtc, RTC_EXT_CTRL_4A) & in ds1685_nvram_write()
974 /* ----------------------------------------------------------------------- */
978 * ds1685_rtc_sysfs_battery_show - sysfs file for main battery status.
979 * @dev: pointer to device structure.
980 * @attr: pointer to device_attribute structure.
981 * @buf: pointer to char array to hold the output.
987 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); in ds1685_rtc_sysfs_battery_show()
990 ctrld = rtc->read(rtc, RTC_CTRL_D); in ds1685_rtc_sysfs_battery_show()
998 * ds1685_rtc_sysfs_auxbatt_show - sysfs file for aux battery status.
999 * @dev: pointer to device structure.
1000 * @attr: pointer to device_attribute structure.
1001 * @buf: pointer to char array to hold the output.
1007 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); in ds1685_rtc_sysfs_auxbatt_show()
1011 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); in ds1685_rtc_sysfs_auxbatt_show()
1020 * ds1685_rtc_sysfs_serial_show - sysfs file for silicon serial number.
1021 * @dev: pointer to device structure.
1022 * @attr: pointer to device_attribute structure.
1023 * @buf: pointer to char array to hold the output.
1029 struct ds1685_priv *rtc = dev_get_drvdata(dev->parent); in ds1685_rtc_sysfs_serial_show()
1041 * struct ds1685_rtc_sysfs_misc_attrs - list for misc RTC features.
1052 * struct ds1685_rtc_sysfs_misc_grp - attr group for misc RTC features.
1060 /* ----------------------------------------------------------------------- */
1064 * ds1685_rtc_probe - initializes rtc driver.
1065 * @pdev: pointer to platform_device structure.
1084 pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data; in ds1685_rtc_probe()
1086 return -ENODEV; in ds1685_rtc_probe()
1089 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL); in ds1685_rtc_probe()
1091 return -ENOMEM; in ds1685_rtc_probe()
1094 switch (pdata->access_type) { in ds1685_rtc_probe()
1096 rtc->regs = devm_platform_ioremap_resource(pdev, 0); in ds1685_rtc_probe()
1097 if (IS_ERR(rtc->regs)) in ds1685_rtc_probe()
1098 return PTR_ERR(rtc->regs); in ds1685_rtc_probe()
1099 rtc->read = ds1685_read; in ds1685_rtc_probe()
1100 rtc->write = ds1685_write; in ds1685_rtc_probe()
1103 rtc->regs = devm_platform_ioremap_resource(pdev, 0); in ds1685_rtc_probe()
1104 if (IS_ERR(rtc->regs)) in ds1685_rtc_probe()
1105 return PTR_ERR(rtc->regs); in ds1685_rtc_probe()
1106 rtc->data = devm_platform_ioremap_resource(pdev, 1); in ds1685_rtc_probe()
1107 if (IS_ERR(rtc->data)) in ds1685_rtc_probe()
1108 return PTR_ERR(rtc->data); in ds1685_rtc_probe()
1109 rtc->read = ds1685_indirect_read; in ds1685_rtc_probe()
1110 rtc->write = ds1685_indirect_write; in ds1685_rtc_probe()
1114 if (!rtc->read || !rtc->write) in ds1685_rtc_probe()
1115 return -ENXIO; in ds1685_rtc_probe()
1118 if (pdata->regstep > 0) in ds1685_rtc_probe()
1119 rtc->regstep = pdata->regstep; in ds1685_rtc_probe()
1121 rtc->regstep = 1; in ds1685_rtc_probe()
1123 /* Platform pre-shutdown function, if defined. */ in ds1685_rtc_probe()
1124 if (pdata->plat_prepare_poweroff) in ds1685_rtc_probe()
1125 rtc->prepare_poweroff = pdata->plat_prepare_poweroff; in ds1685_rtc_probe()
1128 if (pdata->plat_wake_alarm) in ds1685_rtc_probe()
1129 rtc->wake_alarm = pdata->plat_wake_alarm; in ds1685_rtc_probe()
1132 if (pdata->plat_post_ram_clear) in ds1685_rtc_probe()
1133 rtc->post_ram_clear = pdata->plat_post_ram_clear; in ds1685_rtc_probe()
1139 ctrla = rtc->read(rtc, RTC_CTRL_A); in ds1685_rtc_probe()
1146 /* Clear RS3-RS0 in Control A. */ in ds1685_rtc_probe()
1150 * All done with Control A. Switch to Bank 1 for the remainder of in ds1685_rtc_probe()
1151 * the RTC setup so we have access to the extended functions. in ds1685_rtc_probe()
1154 rtc->write(rtc, RTC_CTRL_A, ctrla); in ds1685_rtc_probe()
1156 /* Default to 32768kHz output. */ in ds1685_rtc_probe()
1157 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_probe()
1158 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K)); in ds1685_rtc_probe()
1161 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_probe()
1162 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET)); in ds1685_rtc_probe()
1164 /* Read Ext Ctrl 4A and check the INCR bit to avoid a lockout. */ in ds1685_rtc_probe()
1165 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR) in ds1685_rtc_probe()
1172 ctrlb = rtc->read(rtc, RTC_CTRL_B); in ds1685_rtc_probe()
1173 if (pdata->bcd_mode) in ds1685_rtc_probe()
1177 rtc->bcd_mode = pdata->bcd_mode; in ds1685_rtc_probe()
1187 /* Force 24-hour mode (2412 = 1). */ in ds1685_rtc_probe()
1190 hours = rtc->read(rtc, RTC_HRS); in ds1685_rtc_probe()
1196 /* Enable 24-hour mode. */ in ds1685_rtc_probe()
1199 /* Write back to Control B, including DM & DSE bits. */ in ds1685_rtc_probe()
1200 rtc->write(rtc, RTC_CTRL_B, ctrlb); in ds1685_rtc_probe()
1202 /* Write the time hours back. */ in ds1685_rtc_probe()
1203 rtc->write(rtc, RTC_HRS, in ds1685_rtc_probe()
1209 hours = rtc->read(rtc, RTC_HRS_ALARM); in ds1685_rtc_probe()
1215 /* Write the alarm hours back. */ in ds1685_rtc_probe()
1216 rtc->write(rtc, RTC_HRS_ALARM, in ds1685_rtc_probe()
1221 /* 24-hour mode is already set, so write Control B back. */ in ds1685_rtc_probe()
1222 rtc->write(rtc, RTC_CTRL_B, ctrlb); in ds1685_rtc_probe()
1226 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_probe()
1227 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET))); in ds1685_rtc_probe()
1230 if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT)) in ds1685_rtc_probe()
1231 dev_warn(&pdev->dev, in ds1685_rtc_probe()
1235 if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2)) in ds1685_rtc_probe()
1236 dev_warn(&pdev->dev, in ds1685_rtc_probe()
1239 /* Read Ctrl B and clear PIE/AIE/UIE. */ in ds1685_rtc_probe()
1240 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_probe()
1241 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK))); in ds1685_rtc_probe()
1243 /* Reading Ctrl C auto-clears PF/AF/UF. */ in ds1685_rtc_probe()
1244 rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_probe()
1246 /* Read Ctrl 4B and clear RIE/WIE/KSE. */ in ds1685_rtc_probe()
1247 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_probe()
1248 (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK))); in ds1685_rtc_probe()
1251 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_probe()
1252 (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK))); in ds1685_rtc_probe()
1255 * Re-enable KSE to handle power button events. We do not enable in ds1685_rtc_probe()
1258 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_probe()
1259 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE)); in ds1685_rtc_probe()
1261 rtc_dev = devm_rtc_allocate_device(&pdev->dev); in ds1685_rtc_probe()
1265 rtc_dev->ops = &ds1685_rtc_ops; in ds1685_rtc_probe()
1268 rtc_dev->range_min = RTC_TIMESTAMP_BEGIN_2000; in ds1685_rtc_probe()
1269 rtc_dev->range_max = RTC_TIMESTAMP_END_2099; in ds1685_rtc_probe()
1272 rtc_dev->max_user_freq = RTC_MAX_USER_FREQ; in ds1685_rtc_probe()
1275 if (pdata->uie_unsupported) in ds1685_rtc_probe()
1276 clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, rtc_dev->features); in ds1685_rtc_probe()
1278 rtc->dev = rtc_dev; in ds1685_rtc_probe()
1283 * Not all platforms have the IRQF pin tied to something. If not, the in ds1685_rtc_probe()
1288 rtc->irq_num = platform_get_irq(pdev, 0); in ds1685_rtc_probe()
1289 if (rtc->irq_num <= 0) { in ds1685_rtc_probe()
1290 clear_bit(RTC_FEATURE_ALARM, rtc_dev->features); in ds1685_rtc_probe()
1293 ret = devm_request_threaded_irq(&pdev->dev, rtc->irq_num, in ds1685_rtc_probe()
1296 pdev->name, pdev); in ds1685_rtc_probe()
1298 /* Check to see if something came back. */ in ds1685_rtc_probe()
1300 dev_warn(&pdev->dev, in ds1685_rtc_probe()
1302 rtc->irq_num = 0; in ds1685_rtc_probe()
1322 * ds1685_rtc_remove - removes rtc driver.
1323 * @pdev: pointer to platform_device structure.
1330 /* Read Ctrl B and clear PIE/AIE/UIE. */ in ds1685_rtc_remove()
1331 rtc->write(rtc, RTC_CTRL_B, in ds1685_rtc_remove()
1332 (rtc->read(rtc, RTC_CTRL_B) & in ds1685_rtc_remove()
1335 /* Reading Ctrl C auto-clears PF/AF/UF. */ in ds1685_rtc_remove()
1336 rtc->read(rtc, RTC_CTRL_C); in ds1685_rtc_remove()
1338 /* Read Ctrl 4B and clear RIE/WIE/KSE. */ in ds1685_rtc_remove()
1339 rtc->write(rtc, RTC_EXT_CTRL_4B, in ds1685_rtc_remove()
1340 (rtc->read(rtc, RTC_EXT_CTRL_4B) & in ds1685_rtc_remove()
1344 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_remove()
1345 (rtc->read(rtc, RTC_EXT_CTRL_4A) & in ds1685_rtc_remove()
1350 * ds1685_rtc_driver - rtc driver properties.
1354 .name = "rtc-ds1685",
1360 /* ----------------------------------------------------------------------- */
1363 /* ----------------------------------------------------------------------- */
1367 * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
1368 * @pdev: pointer to platform_device structure.
1387 * going to worry about cleaning up. Most of that should in ds1685_rtc_poweroff()
1391 if (rtc->irq_num) in ds1685_rtc_poweroff()
1392 disable_irq_nosync(rtc->irq_num); in ds1685_rtc_poweroff()
1395 ctrla = rtc->read(rtc, RTC_CTRL_A); in ds1685_rtc_poweroff()
1398 rtc->write(rtc, RTC_CTRL_A, ctrla); in ds1685_rtc_poweroff()
1401 * Read Control 4A and check the status of the auxillary in ds1685_rtc_poweroff()
1403 * for wakeup and kickstart functionality to be useful. in ds1685_rtc_poweroff()
1406 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); in ds1685_rtc_poweroff()
1410 rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a); in ds1685_rtc_poweroff()
1415 * wake-up (WIE=1), and enable kickstart (KSE=1) in ds1685_rtc_poweroff()
1418 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); in ds1685_rtc_poweroff()
1421 rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b); in ds1685_rtc_poweroff()
1424 /* Set PAB to 1 in Control 4A to power the system down. */ in ds1685_rtc_poweroff()
1425 dev_warn(&pdev->dev, "Powerdown.\n"); in ds1685_rtc_poweroff()
1427 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_poweroff()
1430 /* Spin ... we do not switch back to bank0. */ in ds1685_rtc_poweroff()
1436 /* ----------------------------------------------------------------------- */
1440 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
1441 MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
1443 MODULE_ALIAS("platform:rtc-ds1685");