Lines Matching +full:io +full:- +full:multiplex

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.
13 * Application Note 90, Using the Multiplex Bus RTC Extended Features.
20 #include <linux/io.h>
33 /* ----------------------------------------------------------------------- */
40 * ds1685_read - read a value from an rtc register.
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.
60 writeb(value, ((u8 __iomem *)rtc->regs + in ds1685_write()
61 (reg * rtc->regstep))); in ds1685_write()
63 /* ----------------------------------------------------------------------- */
71 * ds1685_indirect_read - read a value from an rtc register.
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.
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.
110 if (rtc->bcd_mode) in ds1685_rtc_bcd2bin()
117 * ds1685_rtc_bin2bcd - bin2bcd wrapper in case platform doesn't support BCD.
128 if (rtc->bcd_mode) in ds1685_rtc_bin2bcd()
135 * ds1685_rtc_check_mday - check validity of the day of month.
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.
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.
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.
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
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()
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.
209 * - Switches the rtc back to bank 0.
210 * - Clears the SET bit in Control Register B.
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.
228 * This number starts at 0x40, and is 8-bytes long, ending at 0x47. The
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 /* ----------------------------------------------------------------------- */
256 * ds1685_rtc_read_time - reads the time registers.
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()
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.
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()
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.
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()
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.
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()
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()
497 rtc->read(rtc, RTC_CTRL_C); 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.
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()
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
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()
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()
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()
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()
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()
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.
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()
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()
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()
882 count++, size--) { 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()
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()
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()
949 count++, size--) { 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.
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.
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.
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.
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()
1154 rtc->write(rtc, RTC_CTRL_A, ctrla); 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()
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()
1200 rtc->write(rtc, RTC_CTRL_B, ctrlb); 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()
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()
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()
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()
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()
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.
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()
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()
1352 * ds1685_rtc_driver - rtc driver properties.
1356 .name = "rtc-ds1685",
1362 /* ----------------------------------------------------------------------- */
1365 /* ----------------------------------------------------------------------- */
1369 * ds1685_rtc_poweroff - uses the RTC chip to power the system off.
1393 if (rtc->irq_num) in ds1685_rtc_poweroff()
1394 disable_irq_nosync(rtc->irq_num); in ds1685_rtc_poweroff()
1397 ctrla = rtc->read(rtc, RTC_CTRL_A); in ds1685_rtc_poweroff()
1400 rtc->write(rtc, RTC_CTRL_A, ctrla); in ds1685_rtc_poweroff()
1408 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A); in ds1685_rtc_poweroff()
1412 rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a); in ds1685_rtc_poweroff()
1417 * wake-up (WIE=1), and enable kickstart (KSE=1) in ds1685_rtc_poweroff()
1420 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B); in ds1685_rtc_poweroff()
1423 rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b); in ds1685_rtc_poweroff()
1427 dev_warn(&pdev->dev, "Powerdown.\n"); in ds1685_rtc_poweroff()
1429 rtc->write(rtc, RTC_EXT_CTRL_4A, in ds1685_rtc_poweroff()
1438 /* ----------------------------------------------------------------------- */
1442 MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
1443 MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
1445 MODULE_ALIAS("platform:rtc-ds1685");