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