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