13 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/string.h>
18 #include <linux/rtc.h>
45 #define DS1307_REG_SECS 0x00
46 # define DS1307_BIT_CH 0x80
47 # define DS1340_BIT_nEOSC 0x80
48 # define MCP7941X_BIT_ST 0x80
49 #define DS1307_REG_MIN 0x01
50 #define DS1307_REG_HOUR 0x02
51 # define DS1307_BIT_12HR 0x40
52 # define DS1307_BIT_PM 0x20
53 # define DS1340_BIT_CENTURY_EN 0x80
54 # define DS1340_BIT_CENTURY 0x40
55 #define DS1307_REG_WDAY 0x03
56 # define MCP7941X_BIT_VBATEN 0x08
57 #define DS1307_REG_MDAY 0x04
58 #define DS1307_REG_MONTH 0x05
59 # define DS1337_BIT_CENTURY 0x80
60 #define DS1307_REG_YEAR 0x06
67 #define DS1307_REG_CONTROL 0x07
68 # define DS1307_BIT_OUT 0x80
69 # define DS1338_BIT_OSF 0x20
70 # define DS1307_BIT_SQWE 0x10
71 # define DS1307_BIT_RS1 0x02
72 # define DS1307_BIT_RS0 0x01
73 #define DS1337_REG_CONTROL 0x0e
74 # define DS1337_BIT_nEOSC 0x80
75 # define DS1339_BIT_BBSQI 0x20
76 # define DS3231_BIT_BBSQW 0x40
77 # define DS1337_BIT_RS2 0x10
78 # define DS1337_BIT_RS1 0x08
79 # define DS1337_BIT_INTCN 0x04
80 # define DS1337_BIT_A2IE 0x02
81 # define DS1337_BIT_A1IE 0x01
82 #define DS1340_REG_CONTROL 0x07
83 # define DS1340_BIT_OUT 0x80
84 # define DS1340_BIT_FT 0x40
85 # define DS1340_BIT_CALIB_SIGN 0x20
86 # define DS1340_M_CALIBRATION 0x1f
87 #define DS1340_REG_FLAG 0x09
88 # define DS1340_BIT_OSF 0x80
89 #define DS1337_REG_STATUS 0x0f
90 # define DS1337_BIT_OSF 0x80
91 # define DS1337_BIT_A2I 0x02
92 # define DS1337_BIT_A1I 0x01
93 #define DS1339_REG_ALARM1_SECS 0x07
95 #define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
97 #define RX8025_REG_CTRL1 0x0e
98 # define RX8025_BIT_2412 0x20
99 #define RX8025_REG_CTRL2 0x0f
100 # define RX8025_BIT_PON 0x10
101 # define RX8025_BIT_VDET 0x40
102 # define RX8025_BIT_XST 0x20
144 .trickle_charger_reg = 0x10,
147 .trickle_charger_reg = 0x08,
150 .trickle_charger_reg = 0x0a,
157 .nvram_offset = 0x20,
180 #define BLOCK_DATA_MAX_TRIES 10
187 for (i = 0; i <
length; i++) {
203 dev_dbg(&client->
dev,
"ds1307_read_block_data (length=%d)\n", length);
204 ret = ds1307_read_block_data_once(client, command, length, values);
210 "ds1307_read_block_data failed\n");
213 memcpy(oldvalues, values, length);
214 ret = ds1307_read_block_data_once(client, command, length,
218 }
while (
memcmp(oldvalues, values, length));
222 static s32 ds1307_write_block_data(
const struct i2c_client *client,
u8 command,
223 u8 length,
const u8 *values)
228 dev_dbg(&client->
dev,
"ds1307_write_block_data (length=%d)\n", length);
234 "ds1307_write_block_data failed\n");
237 for (i = 0; i <
length; i++) {
243 ret = ds1307_read_block_data_once(client, command, length,
247 }
while (
memcmp(currvalues, values, length));
273 lock = &ds1307->
rtc->ops_lock;
281 stat &= ~DS1337_BIT_A1I;
303 struct ds1307 *ds1307 = i2c_get_clientdata(client);
321 dev_err(dev,
"%s error %d\n",
"read", tmp);
325 dev_dbg(dev,
"%s: %02x %02x %02x %02x %02x %02x %02x\n",
344 dev_dbg(dev,
"%s secs=%d, mins=%d, "
345 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
354 static int ds1307_set_time(
struct device *dev,
struct rtc_time *t)
361 dev_dbg(dev,
"%s secs=%d, mins=%d, "
362 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
378 switch (ds1307->
type) {
401 dev_dbg(dev,
"%s: %02x %02x %02x %02x %02x %02x %02x\n",
402 "write", buf[0], buf[1], buf[2], buf[3],
403 buf[4], buf[5], buf[6]);
408 dev_err(dev,
"%s error %d\n",
"write", result);
417 struct ds1307 *ds1307 = i2c_get_clientdata(client);
427 dev_err(dev,
"%s error %d\n",
"alarm read", ret);
431 dev_dbg(dev,
"%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
448 t->
time.tm_year = -1;
449 t->
time.tm_wday = -1;
450 t->
time.tm_yday = -1;
451 t->
time.tm_isdst = -1;
457 dev_dbg(dev,
"%s secs=%d, mins=%d, "
458 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
459 "alarm read", t->
time.tm_sec, t->
time.tm_min,
469 struct ds1307 *ds1307 = i2c_get_clientdata(client);
470 unsigned char *buf = ds1307->
regs;
477 dev_dbg(dev,
"%s secs=%d, mins=%d, "
478 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
479 "alarm set", t->
time.tm_sec, t->
time.tm_min,
487 dev_err(dev,
"%s error %d\n",
"alarm write", ret);
490 control = ds1307->
regs[7];
491 status = ds1307->
regs[8];
493 dev_dbg(dev,
"%s: %02x %02x %02x %02x, %02x %02x %02x, %02x %02x\n",
494 "alarm set (old status)",
498 ds1307->
regs[6], control, status);
514 dev_dbg(dev,
"alarm IRQ armed\n");
522 dev_err(dev,
"can't set alarm time\n");
529 static int ds1307_alarm_irq_enable(
struct device *dev,
unsigned int enabled)
532 struct ds1307 *ds1307 = i2c_get_clientdata(client);
555 .read_time = ds1307_get_time,
556 .set_time = ds1307_set_time,
557 .read_alarm = ds1337_read_alarm,
558 .set_alarm = ds1337_set_alarm,
559 .alarm_irq_enable = ds1307_alarm_irq_enable,
565 ds1307_nvram_read(
struct file *filp,
struct kobject *kobj,
567 char *buf, loff_t off,
size_t count)
570 struct ds1307 *ds1307;
573 client = kobj_to_i2c_client(kobj);
574 ds1307 = i2c_get_clientdata(client);
578 if ((off + count) > ds1307->
nvram->size)
579 count = ds1307->
nvram->size - off;
586 dev_err(&client->
dev,
"%s error %d\n",
"nvram read", result);
591 ds1307_nvram_write(
struct file *filp,
struct kobject *kobj,
593 char *buf, loff_t off,
size_t count)
596 struct ds1307 *ds1307;
599 client = kobj_to_i2c_client(kobj);
600 ds1307 = i2c_get_clientdata(client);
604 if ((off + count) > ds1307->
nvram->size)
605 count = ds1307->
nvram->size - off;
612 dev_err(&client->
dev,
"%s error %d\n",
"nvram write", result);
623 struct ds1307 *ds1307;
628 int want_irq =
false;
631 static const int bbsqi_bitpos[] = {
641 ds1307 = kzalloc(
sizeof(
struct ds1307),
GFP_KERNEL);
645 i2c_set_clientdata(client, ds1307);
648 ds1307->
type =
id->driver_data;
663 switch (ds1307->
type) {
689 | bbsqi_bitpos[ds1307->
type];
722 "oscillator stop detected - SET TIME!\n");
793 switch (ds1307->
type) {
805 if (tmp & DS1307_BIT_CH)
840 | MCP7941X_BIT_VBATEN);
857 switch (ds1307->
type) {
887 if (IS_ERR(ds1307->
rtc)) {
888 err = PTR_ERR(ds1307->
rtc);
890 "unable to register the class device\n");
896 ds1307->
rtc->name, client);
899 "unable to request IRQ!\n");
911 if (!ds1307->
nvram) {
915 ds1307->
nvram->attr.name =
"nvram";
918 ds1307->
nvram->read = ds1307_nvram_read,
919 ds1307->
nvram->write = ds1307_nvram_write,
943 struct ds1307 *ds1307 = i2c_get_clientdata(client);
962 .name =
"rtc-ds1307",
965 .probe = ds1307_probe,
967 .id_table = ds1307_id,