1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#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
35
36
37
38
39
40
41
42
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
53
54
55
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
68
69
70
71
72
73
74
75
76
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
89
90
91
92
93
94
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
106
107
108
109
110
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
126
127
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
138
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
149
150
151
152
153
154
155
156
157
158
159
160static inline void
161ds1685_rtc_begin_data_access(struct ds1685_priv *rtc)
162{
163
164 rtc->write(rtc, RTC_CTRL_B,
165 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
166
167
168 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
169 cpu_relax();
170
171
172 ds1685_rtc_switch_to_bank1(rtc);
173}
174
175
176
177
178
179
180
181
182
183static inline void
184ds1685_rtc_end_data_access(struct ds1685_priv *rtc)
185{
186
187 ds1685_rtc_switch_to_bank1(rtc);
188
189
190 rtc->write(rtc, RTC_CTRL_B,
191 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
192}
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207static inline void
208ds1685_rtc_begin_ctrl_access(struct ds1685_priv *rtc, unsigned long *flags)
209{
210 spin_lock_irqsave(&rtc->lock, *flags);
211 ds1685_rtc_switch_to_bank1(rtc);
212}
213
214
215
216
217
218
219
220
221
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
232
233
234
235
236
237
238
239
240
241
242
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
261
262
263
264
265
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
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
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;
308
309 return rtc_valid_tm(tm);
310}
311
312
313
314
315
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
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
344
345
346
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
360
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
384
385
386
387
388
389
390
391
392
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;
400 int ret;
401
402
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
412
413 ret = ds1685_rtc_check_mday(rtc, mday);
414 if (ret)
415 return ret;
416
417
418
419
420
421
422
423
424 if (likely(seconds < 0xc0))
425 alrm->time.tm_sec = ds1685_rtc_bcd2bin(rtc, seconds,
426 RTC_SECS_BCD_MASK,
427 RTC_SECS_BIN_MASK);
428
429 if (likely(minutes < 0xc0))
430 alrm->time.tm_min = ds1685_rtc_bcd2bin(rtc, minutes,
431 RTC_MINS_BCD_MASK,
432 RTC_MINS_BIN_MASK);
433
434 if (likely(hours < 0xc0))
435 alrm->time.tm_hour = ds1685_rtc_bcd2bin(rtc, hours,
436 RTC_HRS_24_BCD_MASK,
437 RTC_HRS_24_BIN_MASK);
438
439
440 alrm->time.tm_mday = ds1685_rtc_bcd2bin(rtc, mday, RTC_MDAY_BCD_MASK,
441 RTC_MDAY_BIN_MASK);
442 alrm->enabled = !!(ctrlb & RTC_CTRL_B_AIE);
443 alrm->pending = !!(ctrlc & RTC_CTRL_C_AF);
444
445 return 0;
446}
447
448
449
450
451
452
453static int
454ds1685_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
455{
456 struct platform_device *pdev = to_platform_device(dev);
457 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
458 u8 ctrlb, seconds, minutes, hours, mday;
459 int ret;
460
461
462 seconds = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_sec,
463 RTC_SECS_BIN_MASK,
464 RTC_SECS_BCD_MASK);
465 minutes = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_min,
466 RTC_MINS_BIN_MASK,
467 RTC_MINS_BCD_MASK);
468 hours = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_hour,
469 RTC_HRS_24_BIN_MASK,
470 RTC_HRS_24_BCD_MASK);
471 mday = ds1685_rtc_bin2bcd(rtc, alrm->time.tm_mday,
472 RTC_MDAY_BIN_MASK,
473 RTC_MDAY_BCD_MASK);
474
475
476 ret = ds1685_rtc_check_mday(rtc, mday);
477 if (ret)
478 return ret;
479
480
481
482
483
484
485
486
487
488 if (unlikely(seconds >= 0xc0))
489 seconds = 0xff;
490
491 if (unlikely(minutes >= 0xc0))
492 minutes = 0xff;
493
494 if (unlikely(hours >= 0xc0))
495 hours = 0xff;
496
497 alrm->time.tm_mon = -1;
498 alrm->time.tm_year = -1;
499 alrm->time.tm_wday = -1;
500 alrm->time.tm_yday = -1;
501 alrm->time.tm_isdst = -1;
502
503
504 ds1685_rtc_begin_data_access(rtc);
505 ctrlb = rtc->read(rtc, RTC_CTRL_B);
506 rtc->write(rtc, RTC_CTRL_B, (ctrlb & ~(RTC_CTRL_B_AIE)));
507
508
509 rtc->read(rtc, RTC_CTRL_C);
510
511
512
513
514
515 ctrlb = rtc->read(rtc, RTC_CTRL_B);
516 if (rtc->bcd_mode)
517 ctrlb &= ~(RTC_CTRL_B_DM);
518 else
519 ctrlb |= RTC_CTRL_B_DM;
520 rtc->write(rtc, RTC_CTRL_B, ctrlb);
521 rtc->write(rtc, RTC_SECS_ALARM, seconds);
522 rtc->write(rtc, RTC_MINS_ALARM, minutes);
523 rtc->write(rtc, RTC_HRS_ALARM, hours);
524 rtc->write(rtc, RTC_MDAY_ALARM, mday);
525
526
527 if (alrm->enabled) {
528 ctrlb = rtc->read(rtc, RTC_CTRL_B);
529 ctrlb |= RTC_CTRL_B_AIE;
530 rtc->write(rtc, RTC_CTRL_B, ctrlb);
531 }
532
533
534 ds1685_rtc_end_data_access(rtc);
535
536 return 0;
537}
538
539
540
541
542
543
544
545
546
547
548
549static int
550ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
551{
552 struct ds1685_priv *rtc = dev_get_drvdata(dev);
553 unsigned long flags = 0;
554
555
556 spin_lock_irqsave(&rtc->lock, flags);
557
558
559 if (enabled)
560 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) |
561 RTC_CTRL_B_AIE));
562 else
563 rtc->write(rtc, RTC_CTRL_B, (rtc->read(rtc, RTC_CTRL_B) &
564 ~(RTC_CTRL_B_AIE)));
565
566
567 rtc->read(rtc, RTC_CTRL_C);
568 spin_unlock_irqrestore(&rtc->lock, flags);
569
570 return 0;
571}
572
573
574
575
576
577
578
579
580
581
582
583static irqreturn_t
584ds1685_rtc_irq_handler(int irq, void *dev_id)
585{
586 struct platform_device *pdev = dev_id;
587 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
588 u8 ctrlb, ctrlc;
589 unsigned long events = 0;
590 u8 num_irqs = 0;
591
592
593 if (unlikely(!rtc))
594 return IRQ_HANDLED;
595
596
597 spin_lock(&rtc->lock);
598 ctrlb = rtc->read(rtc, RTC_CTRL_B);
599 ctrlc = rtc->read(rtc, RTC_CTRL_C);
600
601
602 if (likely(ctrlc & RTC_CTRL_C_IRQF)) {
603
604
605
606
607
608 if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) {
609 events = RTC_IRQF;
610
611
612 if ((ctrlb & RTC_CTRL_B_PIE) &&
613 (ctrlc & RTC_CTRL_C_PF)) {
614 events |= RTC_PF;
615 num_irqs++;
616 }
617
618
619 if ((ctrlb & RTC_CTRL_B_AIE) &&
620 (ctrlc & RTC_CTRL_C_AF)) {
621 events |= RTC_AF;
622 num_irqs++;
623 }
624
625
626 if ((ctrlb & RTC_CTRL_B_UIE) &&
627 (ctrlc & RTC_CTRL_C_UF)) {
628 events |= RTC_UF;
629 num_irqs++;
630 }
631
632 rtc_update_irq(rtc->dev, num_irqs, events);
633 } else {
634
635
636
637
638
639
640
641
642 spin_unlock(&rtc->lock);
643 schedule_work(&rtc->work);
644 rtc_update_irq(rtc->dev, 0, 0);
645 return IRQ_HANDLED;
646 }
647 }
648 spin_unlock(&rtc->lock);
649
650 return events ? IRQ_HANDLED : IRQ_NONE;
651}
652
653
654
655
656
657static void
658ds1685_rtc_work_queue(struct work_struct *work)
659{
660 struct ds1685_priv *rtc = container_of(work,
661 struct ds1685_priv, work);
662 struct platform_device *pdev = to_platform_device(&rtc->dev->dev);
663 struct mutex *rtc_mutex = &rtc->dev->ops_lock;
664 u8 ctrl4a, ctrl4b;
665
666 mutex_lock(rtc_mutex);
667
668 ds1685_rtc_switch_to_bank1(rtc);
669 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
670 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
671
672
673
674
675
676
677 if ((ctrl4b & RTC_CTRL_4B_KSE) && (ctrl4a & RTC_CTRL_4A_KF)) {
678
679 rtc->write(rtc, RTC_EXT_CTRL_4B,
680 (rtc->read(rtc, RTC_EXT_CTRL_4B) &
681 ~(RTC_CTRL_4B_KSE)));
682
683
684 rtc->write(rtc, RTC_EXT_CTRL_4A,
685 (ctrl4a & ~(RTC_CTRL_4A_KF)));
686
687
688
689
690
691
692
693 msleep(500);
694 rtc->write(rtc, RTC_EXT_CTRL_4B,
695 (rtc->read(rtc, RTC_EXT_CTRL_4B) |
696 RTC_CTRL_4B_KSE));
697
698
699 if (rtc->prepare_poweroff != NULL)
700 rtc->prepare_poweroff();
701 else
702 ds1685_rtc_poweroff(pdev);
703 }
704
705
706
707
708
709
710
711 if ((ctrl4b & RTC_CTRL_4B_WIE) && (ctrl4a & RTC_CTRL_4A_WF)) {
712 rtc->write(rtc, RTC_EXT_CTRL_4A,
713 (ctrl4a & ~(RTC_CTRL_4A_WF)));
714
715
716 if (rtc->wake_alarm != NULL)
717 rtc->wake_alarm();
718 else
719 dev_warn(&pdev->dev,
720 "Wake Alarm IRQ just occurred!\n");
721 }
722
723
724
725
726
727
728
729
730
731
732
733 if ((ctrl4b & RTC_CTRL_4B_RIE) && (ctrl4a & RTC_CTRL_4A_RF)) {
734 rtc->write(rtc, RTC_EXT_CTRL_4A,
735 (ctrl4a & ~(RTC_CTRL_4A_RF)));
736 msleep(150);
737
738
739 if (rtc->post_ram_clear != NULL)
740 rtc->post_ram_clear();
741 else
742 dev_warn(&pdev->dev,
743 "RAM-Clear IRQ just occurred!\n");
744 }
745 ds1685_rtc_switch_to_bank0(rtc);
746
747 mutex_unlock(rtc_mutex);
748}
749
750
751
752
753
754
755#ifdef CONFIG_PROC_FS
756#define NUM_REGS 6
757#define NUM_BITS 8
758#define NUM_SPACES 4
759
760
761
762
763static const char *ds1685_rtc_pirq_rate[16] = {
764 "none", "3.90625ms", "7.8125ms", "0.122070ms", "0.244141ms",
765 "0.488281ms", "0.9765625ms", "1.953125ms", "3.90625ms", "7.8125ms",
766 "15.625ms", "31.25ms", "62.5ms", "125ms", "250ms", "500ms"
767};
768
769
770
771
772static const char *ds1685_rtc_sqw_freq[16] = {
773 "none", "256Hz", "128Hz", "8192Hz", "4096Hz", "2048Hz", "1024Hz",
774 "512Hz", "256Hz", "128Hz", "64Hz", "32Hz", "16Hz", "8Hz", "4Hz", "2Hz"
775};
776
777#ifdef CONFIG_RTC_DS1685_PROC_REGS
778
779
780
781
782
783
784
785
786static char*
787ds1685_rtc_print_regs(u8 hex, char *dest)
788{
789 u32 i, j;
790 char *tmp = dest;
791
792 for (i = 0; i < NUM_BITS; i++) {
793 *tmp++ = ((hex & 0x80) != 0 ? '1' : '0');
794 for (j = 0; j < NUM_SPACES; j++)
795 *tmp++ = ' ';
796 hex <<= 1;
797 }
798 *tmp++ = '\0';
799
800 return dest;
801}
802#endif
803
804
805
806
807
808
809static int
810ds1685_rtc_proc(struct device *dev, struct seq_file *seq)
811{
812 struct platform_device *pdev = to_platform_device(dev);
813 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
814 u8 ctrla, ctrlb, ctrlc, ctrld, ctrl4a, ctrl4b, ssn[8];
815 char *model;
816#ifdef CONFIG_RTC_DS1685_PROC_REGS
817 char bits[NUM_REGS][(NUM_BITS * NUM_SPACES) + NUM_BITS + 1];
818#endif
819
820
821 ds1685_rtc_switch_to_bank1(rtc);
822 ds1685_rtc_get_ssn(rtc, ssn);
823 ctrla = rtc->read(rtc, RTC_CTRL_A);
824 ctrlb = rtc->read(rtc, RTC_CTRL_B);
825 ctrlc = rtc->read(rtc, RTC_CTRL_C);
826 ctrld = rtc->read(rtc, RTC_CTRL_D);
827 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
828 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
829 ds1685_rtc_switch_to_bank0(rtc);
830
831
832 switch (ssn[0]) {
833 case RTC_MODEL_DS1685:
834 model = "DS1685/DS1687\0";
835 break;
836 case RTC_MODEL_DS1689:
837 model = "DS1689/DS1693\0";
838 break;
839 case RTC_MODEL_DS17285:
840 model = "DS17285/DS17287\0";
841 break;
842 case RTC_MODEL_DS17485:
843 model = "DS17485/DS17487\0";
844 break;
845 case RTC_MODEL_DS17885:
846 model = "DS17885/DS17887\0";
847 break;
848 default:
849 model = "Unknown\0";
850 break;
851 }
852
853
854 seq_printf(seq,
855 "Model\t\t: %s\n"
856 "Oscillator\t: %s\n"
857 "12/24hr\t\t: %s\n"
858 "DST\t\t: %s\n"
859 "Data mode\t: %s\n"
860 "Battery\t\t: %s\n"
861 "Aux batt\t: %s\n"
862 "Update IRQ\t: %s\n"
863 "Periodic IRQ\t: %s\n"
864 "Periodic Rate\t: %s\n"
865 "SQW Freq\t: %s\n"
866#ifdef CONFIG_RTC_DS1685_PROC_REGS
867 "Serial #\t: %8phC\n"
868 "Register Status\t:\n"
869 " Ctrl A\t: UIP DV2 DV1 DV0 RS3 RS2 RS1 RS0\n"
870 "\t\t: %s\n"
871 " Ctrl B\t: SET PIE AIE UIE SQWE DM 2412 DSE\n"
872 "\t\t: %s\n"
873 " Ctrl C\t: IRQF PF AF UF --- --- --- ---\n"
874 "\t\t: %s\n"
875 " Ctrl D\t: VRT --- --- --- --- --- --- ---\n"
876 "\t\t: %s\n"
877#if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
878 " Ctrl 4A\t: VRT2 INCR BME --- PAB RF WF KF\n"
879#else
880 " Ctrl 4A\t: VRT2 INCR --- --- PAB RF WF KF\n"
881#endif
882 "\t\t: %s\n"
883 " Ctrl 4B\t: ABE E32k CS RCE PRS RIE WIE KSE\n"
884 "\t\t: %s\n",
885#else
886 "Serial #\t: %8phC\n",
887#endif
888 model,
889 ((ctrla & RTC_CTRL_A_DV1) ? "enabled" : "disabled"),
890 ((ctrlb & RTC_CTRL_B_2412) ? "24-hour" : "12-hour"),
891 ((ctrlb & RTC_CTRL_B_DSE) ? "enabled" : "disabled"),
892 ((ctrlb & RTC_CTRL_B_DM) ? "binary" : "BCD"),
893 ((ctrld & RTC_CTRL_D_VRT) ? "ok" : "exhausted or n/a"),
894 ((ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "exhausted or n/a"),
895 ((ctrlb & RTC_CTRL_B_UIE) ? "yes" : "no"),
896 ((ctrlb & RTC_CTRL_B_PIE) ? "yes" : "no"),
897 (!(ctrl4b & RTC_CTRL_4B_E32K) ?
898 ds1685_rtc_pirq_rate[(ctrla & RTC_CTRL_A_RS_MASK)] : "none"),
899 (!((ctrl4b & RTC_CTRL_4B_E32K)) ?
900 ds1685_rtc_sqw_freq[(ctrla & RTC_CTRL_A_RS_MASK)] : "32768Hz"),
901#ifdef CONFIG_RTC_DS1685_PROC_REGS
902 ssn,
903 ds1685_rtc_print_regs(ctrla, bits[0]),
904 ds1685_rtc_print_regs(ctrlb, bits[1]),
905 ds1685_rtc_print_regs(ctrlc, bits[2]),
906 ds1685_rtc_print_regs(ctrld, bits[3]),
907 ds1685_rtc_print_regs(ctrl4a, bits[4]),
908 ds1685_rtc_print_regs(ctrl4b, bits[5]));
909#else
910 ssn);
911#endif
912 return 0;
913}
914#else
915#define ds1685_rtc_proc NULL
916#endif
917
918
919
920
921
922
923static const struct rtc_class_ops
924ds1685_rtc_ops = {
925 .proc = ds1685_rtc_proc,
926 .read_time = ds1685_rtc_read_time,
927 .set_time = ds1685_rtc_set_time,
928 .read_alarm = ds1685_rtc_read_alarm,
929 .set_alarm = ds1685_rtc_set_alarm,
930 .alarm_irq_enable = ds1685_rtc_alarm_irq_enable,
931};
932
933
934
935
936
937
938#ifdef CONFIG_SYSFS
939
940
941
942
943
944
945
946
947
948static ssize_t
949ds1685_rtc_sysfs_nvram_read(struct file *filp, struct kobject *kobj,
950 struct bin_attribute *bin_attr, char *buf,
951 loff_t pos, size_t size)
952{
953 struct platform_device *pdev =
954 to_platform_device(container_of(kobj, struct device, kobj));
955 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
956 ssize_t count;
957 unsigned long flags = 0;
958
959 spin_lock_irqsave(&rtc->lock, flags);
960 ds1685_rtc_switch_to_bank0(rtc);
961
962
963 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
964 count++, size--) {
965 if (count < NVRAM_SZ_TIME)
966 *buf++ = rtc->read(rtc, (NVRAM_TIME_BASE + pos++));
967 else
968 *buf++ = rtc->read(rtc, (NVRAM_BANK0_BASE + pos++));
969 }
970
971#ifndef CONFIG_RTC_DRV_DS1689
972 if (size > 0) {
973 ds1685_rtc_switch_to_bank1(rtc);
974
975#ifndef CONFIG_RTC_DRV_DS1685
976
977 rtc->write(rtc, RTC_EXT_CTRL_4A,
978 (rtc->read(rtc, RTC_EXT_CTRL_4A) |
979 RTC_CTRL_4A_BME));
980
981
982
983 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
984 (pos - NVRAM_TOTAL_SZ_BANK0));
985#endif
986
987
988 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
989 count++, size--) {
990#ifdef CONFIG_RTC_DRV_DS1685
991
992
993 rtc->write(rtc, RTC_BANK1_RAM_ADDR,
994 (pos - NVRAM_TOTAL_SZ_BANK0));
995#endif
996 *buf++ = rtc->read(rtc, RTC_BANK1_RAM_DATA_PORT);
997 pos++;
998 }
999
1000#ifndef CONFIG_RTC_DRV_DS1685
1001
1002 rtc->write(rtc, RTC_EXT_CTRL_4A,
1003 (rtc->read(rtc, RTC_EXT_CTRL_4A) &
1004 ~(RTC_CTRL_4A_BME)));
1005#endif
1006 ds1685_rtc_switch_to_bank0(rtc);
1007 }
1008#endif
1009 spin_unlock_irqrestore(&rtc->lock, flags);
1010
1011
1012
1013
1014
1015
1016 return count;
1017}
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028static ssize_t
1029ds1685_rtc_sysfs_nvram_write(struct file *filp, struct kobject *kobj,
1030 struct bin_attribute *bin_attr, char *buf,
1031 loff_t pos, size_t size)
1032{
1033 struct platform_device *pdev =
1034 to_platform_device(container_of(kobj, struct device, kobj));
1035 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
1036 ssize_t count;
1037 unsigned long flags = 0;
1038
1039 spin_lock_irqsave(&rtc->lock, flags);
1040 ds1685_rtc_switch_to_bank0(rtc);
1041
1042
1043 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ_BANK0;
1044 count++, size--)
1045 if (count < NVRAM_SZ_TIME)
1046 rtc->write(rtc, (NVRAM_TIME_BASE + pos++),
1047 *buf++);
1048 else
1049 rtc->write(rtc, (NVRAM_BANK0_BASE), *buf++);
1050
1051#ifndef CONFIG_RTC_DRV_DS1689
1052 if (size > 0) {
1053 ds1685_rtc_switch_to_bank1(rtc);
1054
1055#ifndef CONFIG_RTC_DRV_DS1685
1056
1057 rtc->write(rtc, RTC_EXT_CTRL_4A,
1058 (rtc->read(rtc, RTC_EXT_CTRL_4A) |
1059 RTC_CTRL_4A_BME));
1060
1061
1062
1063 rtc->write(rtc, RTC_BANK1_RAM_ADDR_LSB,
1064 (pos - NVRAM_TOTAL_SZ_BANK0));
1065#endif
1066
1067
1068 for (count = 0; size > 0 && pos < NVRAM_TOTAL_SZ;
1069 count++, size--) {
1070#ifdef CONFIG_RTC_DRV_DS1685
1071
1072
1073 rtc->write(rtc, RTC_BANK1_RAM_ADDR,
1074 (pos - NVRAM_TOTAL_SZ_BANK0));
1075#endif
1076 rtc->write(rtc, RTC_BANK1_RAM_DATA_PORT, *buf++);
1077 pos++;
1078 }
1079
1080#ifndef CONFIG_RTC_DRV_DS1685
1081
1082 rtc->write(rtc, RTC_EXT_CTRL_4A,
1083 (rtc->read(rtc, RTC_EXT_CTRL_4A) &
1084 ~(RTC_CTRL_4A_BME)));
1085#endif
1086 ds1685_rtc_switch_to_bank0(rtc);
1087 }
1088#endif
1089 spin_unlock_irqrestore(&rtc->lock, flags);
1090
1091 return count;
1092}
1093
1094
1095
1096
1097
1098
1099
1100
1101static struct bin_attribute
1102ds1685_rtc_sysfs_nvram_attr = {
1103 .attr = {
1104 .name = "nvram",
1105 .mode = S_IRUGO | S_IWUSR,
1106 },
1107 .read = ds1685_rtc_sysfs_nvram_read,
1108 .write = ds1685_rtc_sysfs_nvram_write,
1109 .size = NVRAM_TOTAL_SZ
1110};
1111
1112
1113
1114
1115
1116
1117
1118static ssize_t
1119ds1685_rtc_sysfs_battery_show(struct device *dev,
1120 struct device_attribute *attr, char *buf)
1121{
1122 struct platform_device *pdev = to_platform_device(dev);
1123 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
1124 u8 ctrld;
1125
1126 ctrld = rtc->read(rtc, RTC_CTRL_D);
1127
1128 return sprintf(buf, "%s\n",
1129 (ctrld & RTC_CTRL_D_VRT) ? "ok" : "not ok or N/A");
1130}
1131static DEVICE_ATTR(battery, S_IRUGO, ds1685_rtc_sysfs_battery_show, NULL);
1132
1133
1134
1135
1136
1137
1138
1139static ssize_t
1140ds1685_rtc_sysfs_auxbatt_show(struct device *dev,
1141 struct device_attribute *attr, char *buf)
1142{
1143 struct platform_device *pdev = to_platform_device(dev);
1144 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
1145 u8 ctrl4a;
1146
1147 ds1685_rtc_switch_to_bank1(rtc);
1148 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
1149 ds1685_rtc_switch_to_bank0(rtc);
1150
1151 return sprintf(buf, "%s\n",
1152 (ctrl4a & RTC_CTRL_4A_VRT2) ? "ok" : "not ok or N/A");
1153}
1154static DEVICE_ATTR(auxbatt, S_IRUGO, ds1685_rtc_sysfs_auxbatt_show, NULL);
1155
1156
1157
1158
1159
1160
1161
1162static ssize_t
1163ds1685_rtc_sysfs_serial_show(struct device *dev,
1164 struct device_attribute *attr, char *buf)
1165{
1166 struct platform_device *pdev = to_platform_device(dev);
1167 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
1168 u8 ssn[8];
1169
1170 ds1685_rtc_switch_to_bank1(rtc);
1171 ds1685_rtc_get_ssn(rtc, ssn);
1172 ds1685_rtc_switch_to_bank0(rtc);
1173
1174 return sprintf(buf, "%8phC\n", ssn);
1175}
1176static DEVICE_ATTR(serial, S_IRUGO, ds1685_rtc_sysfs_serial_show, NULL);
1177
1178
1179
1180
1181static struct attribute*
1182ds1685_rtc_sysfs_misc_attrs[] = {
1183 &dev_attr_battery.attr,
1184 &dev_attr_auxbatt.attr,
1185 &dev_attr_serial.attr,
1186 NULL,
1187};
1188
1189
1190
1191
1192static const struct attribute_group
1193ds1685_rtc_sysfs_misc_grp = {
1194 .name = "misc",
1195 .attrs = ds1685_rtc_sysfs_misc_attrs,
1196};
1197
1198#ifdef CONFIG_RTC_DS1685_SYSFS_REGS
1199
1200
1201
1202
1203
1204
1205struct ds1685_rtc_ctrl_regs {
1206 const char *name;
1207 const u8 reg;
1208 const u8 bit;
1209};
1210
1211
1212
1213
1214static const struct ds1685_rtc_ctrl_regs
1215ds1685_ctrl_regs_table[] = {
1216 { "uip", RTC_CTRL_A, RTC_CTRL_A_UIP },
1217 { "dv2", RTC_CTRL_A, RTC_CTRL_A_DV2 },
1218 { "dv1", RTC_CTRL_A, RTC_CTRL_A_DV1 },
1219 { "dv0", RTC_CTRL_A, RTC_CTRL_A_DV0 },
1220 { "rs3", RTC_CTRL_A, RTC_CTRL_A_RS3 },
1221 { "rs2", RTC_CTRL_A, RTC_CTRL_A_RS2 },
1222 { "rs1", RTC_CTRL_A, RTC_CTRL_A_RS1 },
1223 { "rs0", RTC_CTRL_A, RTC_CTRL_A_RS0 },
1224 { "set", RTC_CTRL_B, RTC_CTRL_B_SET },
1225 { "pie", RTC_CTRL_B, RTC_CTRL_B_PIE },
1226 { "aie", RTC_CTRL_B, RTC_CTRL_B_AIE },
1227 { "uie", RTC_CTRL_B, RTC_CTRL_B_UIE },
1228 { "sqwe", RTC_CTRL_B, RTC_CTRL_B_SQWE },
1229 { "dm", RTC_CTRL_B, RTC_CTRL_B_DM },
1230 { "2412", RTC_CTRL_B, RTC_CTRL_B_2412 },
1231 { "dse", RTC_CTRL_B, RTC_CTRL_B_DSE },
1232 { "irqf", RTC_CTRL_C, RTC_CTRL_C_IRQF },
1233 { "pf", RTC_CTRL_C, RTC_CTRL_C_PF },
1234 { "af", RTC_CTRL_C, RTC_CTRL_C_AF },
1235 { "uf", RTC_CTRL_C, RTC_CTRL_C_UF },
1236 { "vrt", RTC_CTRL_D, RTC_CTRL_D_VRT },
1237 { "vrt2", RTC_EXT_CTRL_4A, RTC_CTRL_4A_VRT2 },
1238 { "incr", RTC_EXT_CTRL_4A, RTC_CTRL_4A_INCR },
1239 { "pab", RTC_EXT_CTRL_4A, RTC_CTRL_4A_PAB },
1240 { "rf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_RF },
1241 { "wf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_WF },
1242 { "kf", RTC_EXT_CTRL_4A, RTC_CTRL_4A_KF },
1243#if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
1244 { "bme", RTC_EXT_CTRL_4A, RTC_CTRL_4A_BME },
1245#endif
1246 { "abe", RTC_EXT_CTRL_4B, RTC_CTRL_4B_ABE },
1247 { "e32k", RTC_EXT_CTRL_4B, RTC_CTRL_4B_E32K },
1248 { "cs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_CS },
1249 { "rce", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RCE },
1250 { "prs", RTC_EXT_CTRL_4B, RTC_CTRL_4B_PRS },
1251 { "rie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_RIE },
1252 { "wie", RTC_EXT_CTRL_4B, RTC_CTRL_4B_WIE },
1253 { "kse", RTC_EXT_CTRL_4B, RTC_CTRL_4B_KSE },
1254 { NULL, 0, 0 },
1255};
1256
1257
1258
1259
1260
1261static const struct ds1685_rtc_ctrl_regs*
1262ds1685_rtc_sysfs_ctrl_regs_lookup(const char *name)
1263{
1264 const struct ds1685_rtc_ctrl_regs *p = ds1685_ctrl_regs_table;
1265
1266 for (; p->name != NULL; ++p)
1267 if (strcmp(p->name, name) == 0)
1268 return p;
1269
1270 return NULL;
1271}
1272
1273
1274
1275
1276
1277
1278
1279static ssize_t
1280ds1685_rtc_sysfs_ctrl_regs_show(struct device *dev,
1281 struct device_attribute *attr, char *buf)
1282{
1283 u8 tmp;
1284 struct ds1685_priv *rtc = dev_get_drvdata(dev);
1285 const struct ds1685_rtc_ctrl_regs *reg_info =
1286 ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name);
1287
1288
1289 if (!reg_info)
1290 return -EINVAL;
1291
1292
1293 ds1685_rtc_switch_to_bank1(rtc);
1294 tmp = rtc->read(rtc, reg_info->reg) & reg_info->bit;
1295 ds1685_rtc_switch_to_bank0(rtc);
1296
1297 return sprintf(buf, "%d\n", (tmp ? 1 : 0));
1298}
1299
1300
1301
1302
1303
1304
1305
1306
1307static ssize_t
1308ds1685_rtc_sysfs_ctrl_regs_store(struct device *dev,
1309 struct device_attribute *attr,
1310 const char *buf, size_t count)
1311{
1312 struct ds1685_priv *rtc = dev_get_drvdata(dev);
1313 u8 reg = 0, bit = 0, tmp;
1314 unsigned long flags;
1315 long int val = 0;
1316 const struct ds1685_rtc_ctrl_regs *reg_info =
1317 ds1685_rtc_sysfs_ctrl_regs_lookup(attr->attr.name);
1318
1319
1320 if (kstrtol(buf, 10, &val) < 0)
1321 return -EINVAL;
1322
1323
1324 if ((val != 0) && (val != 1))
1325 return -ERANGE;
1326
1327
1328 if (!reg_info)
1329 return -EINVAL;
1330
1331 reg = reg_info->reg;
1332 bit = reg_info->bit;
1333
1334
1335 ds1685_rtc_begin_ctrl_access(rtc, &flags);
1336 tmp = rtc->read(rtc, reg);
1337 rtc->write(rtc, reg, (val ? (tmp | bit) : (tmp & ~(bit))));
1338 ds1685_rtc_end_ctrl_access(rtc, flags);
1339
1340 return count;
1341}
1342
1343
1344
1345
1346
1347#define DS1685_RTC_SYSFS_CTRL_REG_RO(bit) \
1348 static DEVICE_ATTR(bit, S_IRUGO, \
1349 ds1685_rtc_sysfs_ctrl_regs_show, NULL)
1350
1351
1352
1353
1354
1355#define DS1685_RTC_SYSFS_CTRL_REG_RW(bit) \
1356 static DEVICE_ATTR(bit, S_IRUGO | S_IWUSR, \
1357 ds1685_rtc_sysfs_ctrl_regs_show, \
1358 ds1685_rtc_sysfs_ctrl_regs_store)
1359
1360
1361
1362
1363DS1685_RTC_SYSFS_CTRL_REG_RO(uip);
1364DS1685_RTC_SYSFS_CTRL_REG_RW(dv2);
1365DS1685_RTC_SYSFS_CTRL_REG_RW(dv1);
1366DS1685_RTC_SYSFS_CTRL_REG_RO(dv0);
1367DS1685_RTC_SYSFS_CTRL_REG_RW(rs3);
1368DS1685_RTC_SYSFS_CTRL_REG_RW(rs2);
1369DS1685_RTC_SYSFS_CTRL_REG_RW(rs1);
1370DS1685_RTC_SYSFS_CTRL_REG_RW(rs0);
1371
1372static struct attribute*
1373ds1685_rtc_sysfs_ctrla_attrs[] = {
1374 &dev_attr_uip.attr,
1375 &dev_attr_dv2.attr,
1376 &dev_attr_dv1.attr,
1377 &dev_attr_dv0.attr,
1378 &dev_attr_rs3.attr,
1379 &dev_attr_rs2.attr,
1380 &dev_attr_rs1.attr,
1381 &dev_attr_rs0.attr,
1382 NULL,
1383};
1384
1385static const struct attribute_group
1386ds1685_rtc_sysfs_ctrla_grp = {
1387 .name = "ctrla",
1388 .attrs = ds1685_rtc_sysfs_ctrla_attrs,
1389};
1390
1391
1392
1393
1394
1395DS1685_RTC_SYSFS_CTRL_REG_RO(set);
1396DS1685_RTC_SYSFS_CTRL_REG_RW(pie);
1397DS1685_RTC_SYSFS_CTRL_REG_RW(aie);
1398DS1685_RTC_SYSFS_CTRL_REG_RW(uie);
1399DS1685_RTC_SYSFS_CTRL_REG_RW(sqwe);
1400DS1685_RTC_SYSFS_CTRL_REG_RO(dm);
1401DS1685_RTC_SYSFS_CTRL_REG_RO(2412);
1402DS1685_RTC_SYSFS_CTRL_REG_RO(dse);
1403
1404static struct attribute*
1405ds1685_rtc_sysfs_ctrlb_attrs[] = {
1406 &dev_attr_set.attr,
1407 &dev_attr_pie.attr,
1408 &dev_attr_aie.attr,
1409 &dev_attr_uie.attr,
1410 &dev_attr_sqwe.attr,
1411 &dev_attr_dm.attr,
1412 &dev_attr_2412.attr,
1413 &dev_attr_dse.attr,
1414 NULL,
1415};
1416
1417static const struct attribute_group
1418ds1685_rtc_sysfs_ctrlb_grp = {
1419 .name = "ctrlb",
1420 .attrs = ds1685_rtc_sysfs_ctrlb_attrs,
1421};
1422
1423
1424
1425
1426
1427
1428
1429
1430DS1685_RTC_SYSFS_CTRL_REG_RO(irqf);
1431DS1685_RTC_SYSFS_CTRL_REG_RO(pf);
1432DS1685_RTC_SYSFS_CTRL_REG_RO(af);
1433DS1685_RTC_SYSFS_CTRL_REG_RO(uf);
1434
1435static struct attribute*
1436ds1685_rtc_sysfs_ctrlc_attrs[] = {
1437 &dev_attr_irqf.attr,
1438 &dev_attr_pf.attr,
1439 &dev_attr_af.attr,
1440 &dev_attr_uf.attr,
1441 NULL,
1442};
1443
1444static const struct attribute_group
1445ds1685_rtc_sysfs_ctrlc_grp = {
1446 .name = "ctrlc",
1447 .attrs = ds1685_rtc_sysfs_ctrlc_attrs,
1448};
1449
1450
1451
1452
1453DS1685_RTC_SYSFS_CTRL_REG_RO(vrt);
1454
1455static struct attribute*
1456ds1685_rtc_sysfs_ctrld_attrs[] = {
1457 &dev_attr_vrt.attr,
1458 NULL,
1459};
1460
1461static const struct attribute_group
1462ds1685_rtc_sysfs_ctrld_grp = {
1463 .name = "ctrld",
1464 .attrs = ds1685_rtc_sysfs_ctrld_attrs,
1465};
1466
1467
1468
1469
1470DS1685_RTC_SYSFS_CTRL_REG_RO(vrt2);
1471DS1685_RTC_SYSFS_CTRL_REG_RO(incr);
1472DS1685_RTC_SYSFS_CTRL_REG_RW(pab);
1473DS1685_RTC_SYSFS_CTRL_REG_RW(rf);
1474DS1685_RTC_SYSFS_CTRL_REG_RW(wf);
1475DS1685_RTC_SYSFS_CTRL_REG_RW(kf);
1476#if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
1477DS1685_RTC_SYSFS_CTRL_REG_RO(bme);
1478#endif
1479
1480static struct attribute*
1481ds1685_rtc_sysfs_ctrl4a_attrs[] = {
1482 &dev_attr_vrt2.attr,
1483 &dev_attr_incr.attr,
1484 &dev_attr_pab.attr,
1485 &dev_attr_rf.attr,
1486 &dev_attr_wf.attr,
1487 &dev_attr_kf.attr,
1488#if !defined(CONFIG_RTC_DRV_DS1685) && !defined(CONFIG_RTC_DRV_DS1689)
1489 &dev_attr_bme.attr,
1490#endif
1491 NULL,
1492};
1493
1494static const struct attribute_group
1495ds1685_rtc_sysfs_ctrl4a_grp = {
1496 .name = "ctrl4a",
1497 .attrs = ds1685_rtc_sysfs_ctrl4a_attrs,
1498};
1499
1500
1501
1502
1503DS1685_RTC_SYSFS_CTRL_REG_RW(abe);
1504DS1685_RTC_SYSFS_CTRL_REG_RW(e32k);
1505DS1685_RTC_SYSFS_CTRL_REG_RO(cs);
1506DS1685_RTC_SYSFS_CTRL_REG_RW(rce);
1507DS1685_RTC_SYSFS_CTRL_REG_RW(prs);
1508DS1685_RTC_SYSFS_CTRL_REG_RW(rie);
1509DS1685_RTC_SYSFS_CTRL_REG_RW(wie);
1510DS1685_RTC_SYSFS_CTRL_REG_RW(kse);
1511
1512static struct attribute*
1513ds1685_rtc_sysfs_ctrl4b_attrs[] = {
1514 &dev_attr_abe.attr,
1515 &dev_attr_e32k.attr,
1516 &dev_attr_cs.attr,
1517 &dev_attr_rce.attr,
1518 &dev_attr_prs.attr,
1519 &dev_attr_rie.attr,
1520 &dev_attr_wie.attr,
1521 &dev_attr_kse.attr,
1522 NULL,
1523};
1524
1525static const struct attribute_group
1526ds1685_rtc_sysfs_ctrl4b_grp = {
1527 .name = "ctrl4b",
1528 .attrs = ds1685_rtc_sysfs_ctrl4b_attrs,
1529};
1530
1531
1532
1533
1534
1535
1536
1537
1538struct ds1685_rtc_time_regs {
1539 const char *name;
1540 const u8 reg;
1541 const u8 mask;
1542 const u8 min;
1543 const u8 max;
1544};
1545
1546
1547
1548
1549static const struct ds1685_rtc_time_regs
1550ds1685_time_regs_bcd_table[] = {
1551 { "seconds", RTC_SECS, RTC_SECS_BCD_MASK, 0, 59 },
1552 { "minutes", RTC_MINS, RTC_MINS_BCD_MASK, 0, 59 },
1553 { "hours", RTC_HRS, RTC_HRS_24_BCD_MASK, 0, 23 },
1554 { "wday", RTC_WDAY, RTC_WDAY_MASK, 1, 7 },
1555 { "mday", RTC_MDAY, RTC_MDAY_BCD_MASK, 1, 31 },
1556 { "month", RTC_MONTH, RTC_MONTH_BCD_MASK, 1, 12 },
1557 { "year", RTC_YEAR, RTC_YEAR_BCD_MASK, 0, 99 },
1558 { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0, 99 },
1559 { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BCD_MASK, 0, 59 },
1560 { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BCD_MASK, 0, 59 },
1561 { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BCD_MASK, 0, 23 },
1562 { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 1, 31 },
1563 { NULL, 0, 0, 0, 0 },
1564};
1565
1566static const struct ds1685_rtc_time_regs
1567ds1685_time_regs_bin_table[] = {
1568 { "seconds", RTC_SECS, RTC_SECS_BIN_MASK, 0x00, 0x3b },
1569 { "minutes", RTC_MINS, RTC_MINS_BIN_MASK, 0x00, 0x3b },
1570 { "hours", RTC_HRS, RTC_HRS_24_BIN_MASK, 0x00, 0x17 },
1571 { "wday", RTC_WDAY, RTC_WDAY_MASK, 0x01, 0x07 },
1572 { "mday", RTC_MDAY, RTC_MDAY_BIN_MASK, 0x01, 0x1f },
1573 { "month", RTC_MONTH, RTC_MONTH_BIN_MASK, 0x01, 0x0c },
1574 { "year", RTC_YEAR, RTC_YEAR_BIN_MASK, 0x00, 0x63 },
1575 { "century", RTC_CENTURY, RTC_CENTURY_MASK, 0x00, 0x63 },
1576 { "alarm_seconds", RTC_SECS_ALARM, RTC_SECS_BIN_MASK, 0x00, 0x3b },
1577 { "alarm_minutes", RTC_MINS_ALARM, RTC_MINS_BIN_MASK, 0x00, 0x3b },
1578 { "alarm_hours", RTC_HRS_ALARM, RTC_HRS_24_BIN_MASK, 0x00, 0x17 },
1579 { "alarm_mday", RTC_MDAY_ALARM, RTC_MDAY_ALARM_MASK, 0x01, 0x1f },
1580 { NULL, 0, 0, 0x00, 0x00 },
1581};
1582
1583
1584
1585
1586
1587static const struct ds1685_rtc_time_regs*
1588ds1685_rtc_sysfs_time_regs_lookup(const char *name, bool bcd_mode)
1589{
1590 const struct ds1685_rtc_time_regs *p;
1591
1592 if (bcd_mode)
1593 p = ds1685_time_regs_bcd_table;
1594 else
1595 p = ds1685_time_regs_bin_table;
1596
1597 for (; p->name != NULL; ++p)
1598 if (strcmp(p->name, name) == 0)
1599 return p;
1600
1601 return NULL;
1602}
1603
1604
1605
1606
1607
1608
1609
1610static ssize_t
1611ds1685_rtc_sysfs_time_regs_show(struct device *dev,
1612 struct device_attribute *attr, char *buf)
1613{
1614 u8 tmp;
1615 struct ds1685_priv *rtc = dev_get_drvdata(dev);
1616 const struct ds1685_rtc_time_regs *bcd_reg_info =
1617 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true);
1618 const struct ds1685_rtc_time_regs *bin_reg_info =
1619 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false);
1620
1621
1622 if (!bcd_reg_info || !bin_reg_info)
1623 return -EINVAL;
1624
1625
1626 ds1685_rtc_begin_data_access(rtc);
1627 tmp = rtc->read(rtc, bcd_reg_info->reg);
1628 ds1685_rtc_end_data_access(rtc);
1629
1630 tmp = ds1685_rtc_bcd2bin(rtc, tmp, bcd_reg_info->mask,
1631 bin_reg_info->mask);
1632
1633 return sprintf(buf, "%d\n", tmp);
1634}
1635
1636
1637
1638
1639
1640
1641
1642
1643static ssize_t
1644ds1685_rtc_sysfs_time_regs_store(struct device *dev,
1645 struct device_attribute *attr,
1646 const char *buf, size_t count)
1647{
1648 long int val = 0;
1649 struct ds1685_priv *rtc = dev_get_drvdata(dev);
1650 const struct ds1685_rtc_time_regs *bcd_reg_info =
1651 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, true);
1652 const struct ds1685_rtc_time_regs *bin_reg_info =
1653 ds1685_rtc_sysfs_time_regs_lookup(attr->attr.name, false);
1654
1655
1656 if (kstrtol(buf, 10, &val) < 0)
1657 return -EINVAL;
1658
1659
1660 if (!bcd_reg_info || !bin_reg_info)
1661 return -EINVAL;
1662
1663
1664 if (rtc->bcd_mode) {
1665 if ((val < bcd_reg_info->min) || (val > bcd_reg_info->max))
1666 return -ERANGE;
1667 } else {
1668 if ((val < bin_reg_info->min) || (val > bin_reg_info->max))
1669 return -ERANGE;
1670 }
1671
1672 val = ds1685_rtc_bin2bcd(rtc, val, bin_reg_info->mask,
1673 bcd_reg_info->mask);
1674
1675
1676 ds1685_rtc_begin_data_access(rtc);
1677 rtc->write(rtc, bcd_reg_info->reg, val);
1678 ds1685_rtc_end_data_access(rtc);
1679
1680 return count;
1681}
1682
1683
1684
1685
1686
1687#define DS1685_RTC_SYSFS_TIME_REG_RW(reg) \
1688 static DEVICE_ATTR(reg, S_IRUGO | S_IWUSR, \
1689 ds1685_rtc_sysfs_time_regs_show, \
1690 ds1685_rtc_sysfs_time_regs_store)
1691
1692
1693
1694
1695DS1685_RTC_SYSFS_TIME_REG_RW(seconds);
1696DS1685_RTC_SYSFS_TIME_REG_RW(minutes);
1697DS1685_RTC_SYSFS_TIME_REG_RW(hours);
1698DS1685_RTC_SYSFS_TIME_REG_RW(wday);
1699DS1685_RTC_SYSFS_TIME_REG_RW(mday);
1700DS1685_RTC_SYSFS_TIME_REG_RW(month);
1701DS1685_RTC_SYSFS_TIME_REG_RW(year);
1702DS1685_RTC_SYSFS_TIME_REG_RW(century);
1703DS1685_RTC_SYSFS_TIME_REG_RW(alarm_seconds);
1704DS1685_RTC_SYSFS_TIME_REG_RW(alarm_minutes);
1705DS1685_RTC_SYSFS_TIME_REG_RW(alarm_hours);
1706DS1685_RTC_SYSFS_TIME_REG_RW(alarm_mday);
1707
1708static struct attribute*
1709ds1685_rtc_sysfs_time_attrs[] = {
1710 &dev_attr_seconds.attr,
1711 &dev_attr_minutes.attr,
1712 &dev_attr_hours.attr,
1713 &dev_attr_wday.attr,
1714 &dev_attr_mday.attr,
1715 &dev_attr_month.attr,
1716 &dev_attr_year.attr,
1717 &dev_attr_century.attr,
1718 NULL,
1719};
1720
1721static const struct attribute_group
1722ds1685_rtc_sysfs_time_grp = {
1723 .name = "datetime",
1724 .attrs = ds1685_rtc_sysfs_time_attrs,
1725};
1726
1727static struct attribute*
1728ds1685_rtc_sysfs_alarm_attrs[] = {
1729 &dev_attr_alarm_seconds.attr,
1730 &dev_attr_alarm_minutes.attr,
1731 &dev_attr_alarm_hours.attr,
1732 &dev_attr_alarm_mday.attr,
1733 NULL,
1734};
1735
1736static const struct attribute_group
1737ds1685_rtc_sysfs_alarm_grp = {
1738 .name = "alarm",
1739 .attrs = ds1685_rtc_sysfs_alarm_attrs,
1740};
1741#endif
1742
1743
1744
1745
1746
1747
1748static int
1749ds1685_rtc_sysfs_register(struct device *dev)
1750{
1751 int ret = 0;
1752
1753 sysfs_bin_attr_init(&ds1685_rtc_sysfs_nvram_attr);
1754 ret = sysfs_create_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
1755 if (ret)
1756 return ret;
1757
1758 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
1759 if (ret)
1760 return ret;
1761
1762#ifdef CONFIG_RTC_DS1685_SYSFS_REGS
1763 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp);
1764 if (ret)
1765 return ret;
1766
1767 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp);
1768 if (ret)
1769 return ret;
1770
1771 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp);
1772 if (ret)
1773 return ret;
1774
1775 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp);
1776 if (ret)
1777 return ret;
1778
1779 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp);
1780 if (ret)
1781 return ret;
1782
1783 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp);
1784 if (ret)
1785 return ret;
1786
1787 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp);
1788 if (ret)
1789 return ret;
1790
1791 ret = sysfs_create_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp);
1792 if (ret)
1793 return ret;
1794#endif
1795 return 0;
1796}
1797
1798
1799
1800
1801
1802static int
1803ds1685_rtc_sysfs_unregister(struct device *dev)
1804{
1805 sysfs_remove_bin_file(&dev->kobj, &ds1685_rtc_sysfs_nvram_attr);
1806 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_misc_grp);
1807
1808#ifdef CONFIG_RTC_DS1685_SYSFS_REGS
1809 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrla_grp);
1810 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlb_grp);
1811 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrlc_grp);
1812 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrld_grp);
1813 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4a_grp);
1814 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_ctrl4b_grp);
1815 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_time_grp);
1816 sysfs_remove_group(&dev->kobj, &ds1685_rtc_sysfs_alarm_grp);
1817#endif
1818
1819 return 0;
1820}
1821#endif
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832static int
1833ds1685_rtc_probe(struct platform_device *pdev)
1834{
1835 struct rtc_device *rtc_dev;
1836 struct resource *res;
1837 struct ds1685_priv *rtc;
1838 struct ds1685_rtc_platform_data *pdata;
1839 u8 ctrla, ctrlb, hours;
1840 unsigned char am_pm;
1841 int ret = 0;
1842
1843
1844 pdata = (struct ds1685_rtc_platform_data *) pdev->dev.platform_data;
1845 if (!pdata)
1846 return -ENODEV;
1847
1848
1849 rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
1850 if (!rtc)
1851 return -ENOMEM;
1852
1853
1854
1855
1856
1857
1858
1859 if (pdata->alloc_io_resources) {
1860
1861 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1862 if (!res)
1863 return -ENXIO;
1864 rtc->size = resource_size(res);
1865
1866
1867
1868 if (!devm_request_mem_region(&pdev->dev, res->start, rtc->size,
1869 pdev->name))
1870 return -EBUSY;
1871
1872
1873
1874
1875
1876 rtc->baseaddr = res->start;
1877 rtc->regs = devm_ioremap(&pdev->dev, res->start, rtc->size);
1878 if (!rtc->regs)
1879 return -ENOMEM;
1880 }
1881 rtc->alloc_io_resources = pdata->alloc_io_resources;
1882
1883
1884 if (pdata->regstep > 0)
1885 rtc->regstep = pdata->regstep;
1886 else
1887 rtc->regstep = 1;
1888
1889
1890 if (pdata->plat_read)
1891 rtc->read = pdata->plat_read;
1892 else
1893 if (pdata->alloc_io_resources)
1894 rtc->read = ds1685_read;
1895 else
1896 return -ENXIO;
1897
1898
1899 if (pdata->plat_write)
1900 rtc->write = pdata->plat_write;
1901 else
1902 if (pdata->alloc_io_resources)
1903 rtc->write = ds1685_write;
1904 else
1905 return -ENXIO;
1906
1907
1908 if (pdata->plat_prepare_poweroff)
1909 rtc->prepare_poweroff = pdata->plat_prepare_poweroff;
1910
1911
1912 if (pdata->plat_wake_alarm)
1913 rtc->wake_alarm = pdata->plat_wake_alarm;
1914
1915
1916 if (pdata->plat_post_ram_clear)
1917 rtc->post_ram_clear = pdata->plat_post_ram_clear;
1918
1919
1920 spin_lock_init(&rtc->lock);
1921 INIT_WORK(&rtc->work, ds1685_rtc_work_queue);
1922 platform_set_drvdata(pdev, rtc);
1923
1924
1925 ctrla = rtc->read(rtc, RTC_CTRL_A);
1926 if (!(ctrla & RTC_CTRL_A_DV1))
1927 ctrla |= RTC_CTRL_A_DV1;
1928
1929
1930 ctrla &= ~(RTC_CTRL_A_DV2);
1931
1932
1933 ctrla &= ~(RTC_CTRL_A_RS_MASK);
1934
1935
1936
1937
1938
1939 ctrla |= RTC_CTRL_A_DV0;
1940 rtc->write(rtc, RTC_CTRL_A, ctrla);
1941
1942
1943 rtc->write(rtc, RTC_EXT_CTRL_4B,
1944 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_E32K));
1945
1946
1947 rtc->write(rtc, RTC_CTRL_B,
1948 (rtc->read(rtc, RTC_CTRL_B) | RTC_CTRL_B_SET));
1949
1950
1951 while (rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_INCR)
1952 cpu_relax();
1953
1954
1955
1956
1957
1958 ctrlb = rtc->read(rtc, RTC_CTRL_B);
1959 if (pdata->bcd_mode)
1960 ctrlb &= ~(RTC_CTRL_B_DM);
1961 else
1962 ctrlb |= RTC_CTRL_B_DM;
1963 rtc->bcd_mode = pdata->bcd_mode;
1964
1965
1966
1967
1968
1969
1970 if (ctrlb & RTC_CTRL_B_DSE)
1971 ctrlb &= ~(RTC_CTRL_B_DSE);
1972
1973
1974 if (!(ctrlb & RTC_CTRL_B_2412)) {
1975
1976 hours = rtc->read(rtc, RTC_HRS);
1977 am_pm = hours & RTC_HRS_AMPM_MASK;
1978 hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
1979 RTC_HRS_12_BIN_MASK);
1980 hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
1981
1982
1983 ctrlb |= RTC_CTRL_B_2412;
1984
1985
1986 rtc->write(rtc, RTC_CTRL_B, ctrlb);
1987
1988
1989 rtc->write(rtc, RTC_HRS,
1990 ds1685_rtc_bin2bcd(rtc, hours,
1991 RTC_HRS_24_BIN_MASK,
1992 RTC_HRS_24_BCD_MASK));
1993
1994
1995 hours = rtc->read(rtc, RTC_HRS_ALARM);
1996 am_pm = hours & RTC_HRS_AMPM_MASK;
1997 hours = ds1685_rtc_bcd2bin(rtc, hours, RTC_HRS_12_BCD_MASK,
1998 RTC_HRS_12_BIN_MASK);
1999 hours = ((hours == 12) ? 0 : ((am_pm) ? hours + 12 : hours));
2000
2001
2002 rtc->write(rtc, RTC_HRS_ALARM,
2003 ds1685_rtc_bin2bcd(rtc, hours,
2004 RTC_HRS_24_BIN_MASK,
2005 RTC_HRS_24_BCD_MASK));
2006 } else {
2007
2008 rtc->write(rtc, RTC_CTRL_B, ctrlb);
2009 }
2010
2011
2012 rtc->write(rtc, RTC_CTRL_B,
2013 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_SET)));
2014
2015
2016 if (!(rtc->read(rtc, RTC_CTRL_D) & RTC_CTRL_D_VRT))
2017 dev_warn(&pdev->dev,
2018 "Main battery is exhausted! RTC may be invalid!\n");
2019
2020
2021 if (!(rtc->read(rtc, RTC_EXT_CTRL_4A) & RTC_CTRL_4A_VRT2))
2022 dev_warn(&pdev->dev,
2023 "Aux battery is exhausted or not available.\n");
2024
2025
2026 rtc->write(rtc, RTC_CTRL_B,
2027 (rtc->read(rtc, RTC_CTRL_B) & ~(RTC_CTRL_B_PAU_MASK)));
2028
2029
2030 rtc->read(rtc, RTC_CTRL_C);
2031
2032
2033 rtc->write(rtc, RTC_EXT_CTRL_4B,
2034 (rtc->read(rtc, RTC_EXT_CTRL_4B) & ~(RTC_CTRL_4B_RWK_MASK)));
2035
2036
2037 rtc->write(rtc, RTC_EXT_CTRL_4A,
2038 (rtc->read(rtc, RTC_EXT_CTRL_4A) & ~(RTC_CTRL_4A_RWK_MASK)));
2039
2040
2041
2042
2043
2044 rtc->write(rtc, RTC_EXT_CTRL_4B,
2045 (rtc->read(rtc, RTC_EXT_CTRL_4B) | RTC_CTRL_4B_KSE));
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055 if (!pdata->no_irq) {
2056 ret = platform_get_irq(pdev, 0);
2057 if (ret > 0) {
2058 rtc->irq_num = ret;
2059
2060
2061 ret = devm_request_irq(&pdev->dev, rtc->irq_num,
2062 ds1685_rtc_irq_handler,
2063 IRQF_SHARED, pdev->name, pdev);
2064
2065
2066 if (unlikely(ret)) {
2067 dev_warn(&pdev->dev,
2068 "RTC interrupt not available\n");
2069 rtc->irq_num = 0;
2070 }
2071 } else
2072 return ret;
2073 }
2074 rtc->no_irq = pdata->no_irq;
2075
2076
2077 ds1685_rtc_switch_to_bank0(rtc);
2078
2079
2080 rtc_dev = rtc_device_register(pdev->name, &pdev->dev,
2081 &ds1685_rtc_ops, THIS_MODULE);
2082
2083
2084 if (IS_ERR(rtc_dev))
2085 return PTR_ERR(rtc_dev);
2086
2087
2088 rtc_dev->max_user_freq = RTC_MAX_USER_FREQ;
2089
2090
2091 if (pdata->uie_unsupported)
2092 rtc_dev->uie_unsupported = 1;
2093 rtc->uie_unsupported = pdata->uie_unsupported;
2094
2095 rtc->dev = rtc_dev;
2096
2097#ifdef CONFIG_SYSFS
2098 ret = ds1685_rtc_sysfs_register(&pdev->dev);
2099 if (ret)
2100 rtc_device_unregister(rtc->dev);
2101#endif
2102
2103
2104 return ret;
2105}
2106
2107
2108
2109
2110
2111static int
2112ds1685_rtc_remove(struct platform_device *pdev)
2113{
2114 struct ds1685_priv *rtc = platform_get_drvdata(pdev);
2115
2116#ifdef CONFIG_SYSFS
2117 ds1685_rtc_sysfs_unregister(&pdev->dev);
2118#endif
2119
2120 rtc_device_unregister(rtc->dev);
2121
2122
2123 rtc->write(rtc, RTC_CTRL_B,
2124 (rtc->read(rtc, RTC_CTRL_B) &
2125 ~(RTC_CTRL_B_PAU_MASK)));
2126
2127
2128 rtc->read(rtc, RTC_CTRL_C);
2129
2130
2131 rtc->write(rtc, RTC_EXT_CTRL_4B,
2132 (rtc->read(rtc, RTC_EXT_CTRL_4B) &
2133 ~(RTC_CTRL_4B_RWK_MASK)));
2134
2135
2136 rtc->write(rtc, RTC_EXT_CTRL_4A,
2137 (rtc->read(rtc, RTC_EXT_CTRL_4A) &
2138 ~(RTC_CTRL_4A_RWK_MASK)));
2139
2140 cancel_work_sync(&rtc->work);
2141
2142 return 0;
2143}
2144
2145
2146
2147
2148static struct platform_driver ds1685_rtc_driver = {
2149 .driver = {
2150 .name = "rtc-ds1685",
2151 },
2152 .probe = ds1685_rtc_probe,
2153 .remove = ds1685_rtc_remove,
2154};
2155module_platform_driver(ds1685_rtc_driver);
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166void __noreturn
2167ds1685_rtc_poweroff(struct platform_device *pdev)
2168{
2169 u8 ctrla, ctrl4a, ctrl4b;
2170 struct ds1685_priv *rtc;
2171
2172
2173 if (unlikely(!pdev)) {
2174 pr_emerg("platform device data not available, spinning forever ...\n");
2175 while(1);
2176 unreachable();
2177 } else {
2178
2179 rtc = platform_get_drvdata(pdev);
2180
2181
2182
2183
2184
2185
2186
2187 if (!rtc->no_irq)
2188 disable_irq_nosync(rtc->irq_num);
2189
2190
2191 ctrla = rtc->read(rtc, RTC_CTRL_A);
2192 ctrla |= RTC_CTRL_A_DV1;
2193 ctrla &= ~(RTC_CTRL_A_DV2);
2194 rtc->write(rtc, RTC_CTRL_A, ctrla);
2195
2196
2197
2198
2199
2200
2201 ds1685_rtc_switch_to_bank1(rtc);
2202 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
2203 if (ctrl4a & RTC_CTRL_4A_VRT2) {
2204
2205 ctrl4a &= ~(RTC_CTRL_4A_RWK_MASK);
2206 rtc->write(rtc, RTC_EXT_CTRL_4A, ctrl4a);
2207
2208
2209
2210
2211
2212
2213
2214 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
2215 ctrl4b |= (RTC_CTRL_4B_ABE | RTC_CTRL_4B_WIE |
2216 RTC_CTRL_4B_KSE);
2217 rtc->write(rtc, RTC_EXT_CTRL_4B, ctrl4b);
2218 }
2219
2220
2221 dev_warn(&pdev->dev, "Powerdown.\n");
2222 msleep(20);
2223 rtc->write(rtc, RTC_EXT_CTRL_4A,
2224 (ctrl4a | RTC_CTRL_4A_PAB));
2225
2226
2227 while(1);
2228 unreachable();
2229 }
2230}
2231EXPORT_SYMBOL(ds1685_rtc_poweroff);
2232
2233
2234
2235MODULE_AUTHOR("Joshua Kinard <kumba@gentoo.org>");
2236MODULE_AUTHOR("Matthias Fuchs <matthias.fuchs@esd-electronics.com>");
2237MODULE_DESCRIPTION("Dallas/Maxim DS1685/DS1687-series RTC driver");
2238MODULE_LICENSE("GPL");
2239MODULE_ALIAS("platform:rtc-ds1685");
2240