11 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/rtc.h>
20 #include <linux/module.h>
27 #define DS1305_WRITE 0x80
35 #define DS1305_RTC_LEN 7
37 #define DS1305_SEC 0x00
38 #define DS1305_MIN 0x01
39 #define DS1305_HOUR 0x02
40 # define DS1305_HR_12 0x40
41 # define DS1305_HR_PM 0x20
42 #define DS1305_WDAY 0x03
43 #define DS1305_MDAY 0x04
44 #define DS1305_MON 0x05
45 #define DS1305_YEAR 0x06
60 #define DS1305_ALM_LEN 4
61 #define DS1305_ALM_DISABLE 0x80
63 #define DS1305_ALM0(r) (0x07 + (r))
64 #define DS1305_ALM1(r) (0x0b + (r))
68 #define DS1305_CONTROL_LEN 3
70 #define DS1305_CONTROL 0x0f
71 # define DS1305_nEOSC 0x80
72 # define DS1305_WP 0x40
73 # define DS1305_INTCN 0x04
74 # define DS1306_1HZ 0x04
75 # define DS1305_AEI1 0x02
76 # define DS1305_AEI0 0x01
77 #define DS1305_STATUS 0x10
79 #define DS1305_TRICKLE 0x11
83 #define DS1305_NVRAM_LEN 96
85 #define DS1305_NVRAM 0x20
95 #define FLAG_EXITING 0
109 static unsigned bcd2hour(
u8 bcd)
114 bcd &= ~DS1305_HR_12;
117 bcd &= ~DS1305_HR_PM;
125 static u8 hour2bcd(
bool hr12,
int hour)
143 static int ds1305_alarm_irq_enable(
struct device *
dev,
unsigned int enabled)
150 buf[1] = ds1305->
ctrl[0];
159 buf[1] &= ~DS1305_AEI0;
163 ds1305->
ctrl[0] = buf[1];
189 dev_vdbg(dev,
"%s: %02x %02x %02x, %02x %02x %02x %02x\n",
190 "read", buf[0], buf[1], buf[2], buf[3],
191 buf[4], buf[5], buf[6]);
202 dev_vdbg(dev,
"%s secs=%d, mins=%d, "
203 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
212 static int ds1305_set_time(
struct device *dev,
struct rtc_time *time)
218 dev_vdbg(dev,
"%s secs=%d, mins=%d, "
219 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
235 dev_dbg(dev,
"%s: %02x %02x %02x, %02x %02x %02x %02x\n",
236 "write", buf[1], buf[2], buf[3],
237 buf[4], buf[5], buf[6], buf[7]);
304 dev_vdbg(dev,
"%s: %02x %02x %02x %02x\n",
319 alm->
time.tm_hour = bcd2hour(buf[DS1305_HOUR]);
320 alm->
time.tm_mday = -1;
321 alm->
time.tm_mon = -1;
322 alm->
time.tm_year = -1;
324 alm->
time.tm_wday = -1;
325 alm->
time.tm_mday = -1;
326 alm->
time.tm_isdst = -1;
338 unsigned long now, later;
349 status = ds1305_get_time(dev, &
tm);
359 if ((later - now) > 24 * 60 * 60)
367 buf[1] = ds1305->
ctrl[0];
380 dev_dbg(dev,
"%s: %02x %02x %02x %02x\n",
381 "alm0 write", buf[1 + DS1305_SEC], buf[1 + DS1305_MIN],
393 buf[1] = ds1305->
ctrl[0];
400 #ifdef CONFIG_PROC_FS
406 char *resistors =
"";
410 switch (ds1305->
ctrl[2] & 0x0c) {
412 diodes =
"2 diodes, ";
415 diodes =
"1 diode, ";
420 switch (ds1305->
ctrl[2] & 0x03) {
422 resistors =
"2k Ohm";
425 resistors =
"4k Ohm";
428 resistors =
"8k Ohm";
438 "trickle_charge\t: %s%s\n",
443 #define ds1305_proc NULL
447 .read_time = ds1305_get_time,
448 .set_time = ds1305_set_time,
449 .read_alarm = ds1305_get_alarm,
450 .set_alarm = ds1305_set_alarm,
452 .alarm_irq_enable = ds1305_alarm_irq_enable,
457 struct ds1305 *ds1305 =
container_of(work,
struct ds1305, work);
474 buf[1] = ds1305->
ctrl[0];
480 dev_dbg(&spi->
dev,
"clear irq --> %d\n", status);
497 struct ds1305 *ds1305 =
p;
514 memset(x, 0, 2 *
sizeof(*x));
518 spi_message_add_tail(x, m);
525 spi_message_add_tail(x, m);
529 ds1305_nvram_read(
struct file *filp,
struct kobject *kobj,
531 char *buf, loff_t off,
size_t count)
551 msg_init(&m, x, &addr, count,
NULL, buf);
555 dev_err(&spi->
dev,
"nvram %s error %d\n",
"read", status);
556 return (status < 0) ? status :
count;
560 ds1305_nvram_write(
struct file *filp,
struct kobject *kobj,
562 char *buf, loff_t off,
size_t count)
582 msg_init(&m, x, &addr, count, buf,
NULL);
586 dev_err(&spi->
dev,
"nvram %s error %d\n",
"write", status);
587 return (status < 0) ? status :
count;
591 .attr.name =
"nvram",
593 .read = ds1305_nvram_read,
594 .write = ds1305_nvram_write,
606 struct ds1305 *ds1305;
626 spi_set_drvdata(spi, ds1305);
638 dev_dbg(&spi->
dev,
"ctrl %s: %02x %02x %02x\n",
639 "read", ds1305->
ctrl[0],
647 if ((ds1305->
ctrl[0] & 0x38) != 0 || (ds1305->
ctrl[1] & 0xfc) != 0) {
648 dev_dbg(&spi->
dev,
"RTC chip is not present\n");
652 if (ds1305->
ctrl[2] == 0)
653 dev_dbg(&spi->
dev,
"chip may not be present\n");
664 buf[1] = ds1305->
ctrl[0];
667 dev_dbg(&spi->
dev,
"clear WP --> %d\n", status);
682 if (ds1305->
ctrl[1]) {
716 buf[1] = ds1305->
ctrl[0];
717 buf[2] = ds1305->
ctrl[1];
718 buf[3] = ds1305->
ctrl[2];
726 dev_dbg(&spi->
dev,
"ctrl %s: %02x %02x %02x\n",
727 "write", ds1305->
ctrl[0],
734 &value,
sizeof value);
736 dev_dbg(&spi->
dev,
"read HOUR --> %d\n", status);
747 if (IS_ERR(ds1305->
rtc)) {
748 status = PTR_ERR(ds1305->
rtc);
749 dev_dbg(&spi->
dev,
"register rtc --> %d\n", status);
762 0, dev_name(&ds1305->
rtc->dev), ds1305);
775 dev_dbg(&spi->
dev,
"register nvram --> %d\n", status);
792 struct ds1305 *ds1305 = spi_get_drvdata(spi);
804 spi_set_drvdata(spi,
NULL);
810 .driver.name =
"rtc-ds1305",
812 .probe = ds1305_probe,