2 * rtc-ds1307.c - RTC driver for some mostly-compatible I2C chips.
4 * Copyright (C) 2005 James Chapman (ds1337 core)
5 * Copyright (C) 2006 David Brownell
6 * Copyright (C) 2009 Matthias Fuchs (rx8025 support)
7 * Copyright (C) 2012 Bertrand Achard (nvram access fixes)
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/bcd.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/rtc/ds1307.h>
19 #include <linux/rtc.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/hwmon.h>
23 #include <linux/hwmon-sysfs.h>
24 #include <linux/clk-provider.h>
27 * We can't determine type by probing, but if we expect pre-Linux code
28 * to have set the chip up as a clock (turning on the oscillator and
29 * setting the date and time), Linux can ignore the non-clock features.
30 * That's a natural job for a factory or repair bench.
43 last_ds_type
/* always last */
44 /* rs5c372 too? different address... */
48 /* RTC registers don't differ much, except for the century flag */
49 #define DS1307_REG_SECS 0x00 /* 00-59 */
50 # define DS1307_BIT_CH 0x80
51 # define DS1340_BIT_nEOSC 0x80
52 # define MCP794XX_BIT_ST 0x80
53 #define DS1307_REG_MIN 0x01 /* 00-59 */
54 #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
55 # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
56 # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
57 # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
58 # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
59 #define DS1307_REG_WDAY 0x03 /* 01-07 */
60 # define MCP794XX_BIT_VBATEN 0x08
61 #define DS1307_REG_MDAY 0x04 /* 01-31 */
62 #define DS1307_REG_MONTH 0x05 /* 01-12 */
63 # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
64 #define DS1307_REG_YEAR 0x06 /* 00-99 */
67 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
68 * start at 7, and they differ a LOT. Only control and status matter for
69 * basic RTC date and time functionality; be careful using them.
71 #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
72 # define DS1307_BIT_OUT 0x80
73 # define DS1338_BIT_OSF 0x20
74 # define DS1307_BIT_SQWE 0x10
75 # define DS1307_BIT_RS1 0x02
76 # define DS1307_BIT_RS0 0x01
77 #define DS1337_REG_CONTROL 0x0e
78 # define DS1337_BIT_nEOSC 0x80
79 # define DS1339_BIT_BBSQI 0x20
80 # define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
81 # define DS1337_BIT_RS2 0x10
82 # define DS1337_BIT_RS1 0x08
83 # define DS1337_BIT_INTCN 0x04
84 # define DS1337_BIT_A2IE 0x02
85 # define DS1337_BIT_A1IE 0x01
86 #define DS1340_REG_CONTROL 0x07
87 # define DS1340_BIT_OUT 0x80
88 # define DS1340_BIT_FT 0x40
89 # define DS1340_BIT_CALIB_SIGN 0x20
90 # define DS1340_M_CALIBRATION 0x1f
91 #define DS1340_REG_FLAG 0x09
92 # define DS1340_BIT_OSF 0x80
93 #define DS1337_REG_STATUS 0x0f
94 # define DS1337_BIT_OSF 0x80
95 # define DS3231_BIT_EN32KHZ 0x08
96 # define DS1337_BIT_A2I 0x02
97 # define DS1337_BIT_A1I 0x01
98 #define DS1339_REG_ALARM1_SECS 0x07
100 #define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
102 #define RX8025_REG_CTRL1 0x0e
103 # define RX8025_BIT_2412 0x20
104 #define RX8025_REG_CTRL2 0x0f
105 # define RX8025_BIT_PON 0x10
106 # define RX8025_BIT_VDET 0x40
107 # define RX8025_BIT_XST 0x20
111 u8 offset
; /* register's offset */
114 struct bin_attribute
*nvram
;
117 #define HAS_NVRAM 0 /* bit 0 == sysfs file active */
118 #define HAS_ALARM 1 /* bit 1 == irq claimed */
119 struct i2c_client
*client
;
120 struct rtc_device
*rtc
;
121 s32 (*read_block_data
)(const struct i2c_client
*client
, u8 command
,
122 u8 length
, u8
*values
);
123 s32 (*write_block_data
)(const struct i2c_client
*client
, u8 command
,
124 u8 length
, const u8
*values
);
125 #ifdef CONFIG_COMMON_CLK
126 struct clk_hw clks
[2];
134 u16 trickle_charger_reg
;
135 u8 trickle_charger_setup
;
136 u8 (*do_trickle_setup
)(struct i2c_client
*, uint32_t, bool);
139 static u8
do_trickle_setup_ds1339(struct i2c_client
*,
140 uint32_t ohms
, bool diode
);
142 static struct chip_desc chips
[last_ds_type
] = {
156 .trickle_charger_reg
= 0x10,
157 .do_trickle_setup
= &do_trickle_setup_ds1339
,
160 .trickle_charger_reg
= 0x08,
163 .trickle_charger_reg
= 0x0a,
170 /* this is battery backed SRAM */
171 .nvram_offset
= 0x20,
176 static const struct i2c_device_id ds1307_id
[] = {
177 { "ds1307", ds_1307
},
178 { "ds1337", ds_1337
},
179 { "ds1338", ds_1338
},
180 { "ds1339", ds_1339
},
181 { "ds1388", ds_1388
},
182 { "ds1340", ds_1340
},
183 { "ds3231", ds_3231
},
184 { "m41t00", m41t00
},
185 { "mcp7940x", mcp794xx
},
186 { "mcp7941x", mcp794xx
},
187 { "pt7c4338", ds_1307
},
188 { "rx8025", rx_8025
},
189 { "isl12057", ds_1337
},
192 MODULE_DEVICE_TABLE(i2c
, ds1307_id
);
194 /*----------------------------------------------------------------------*/
196 #define BLOCK_DATA_MAX_TRIES 10
198 static s32
ds1307_read_block_data_once(const struct i2c_client
*client
,
199 u8 command
, u8 length
, u8
*values
)
203 for (i
= 0; i
< length
; i
++) {
204 data
= i2c_smbus_read_byte_data(client
, command
+ i
);
212 static s32
ds1307_read_block_data(const struct i2c_client
*client
, u8 command
,
213 u8 length
, u8
*values
)
219 dev_dbg(&client
->dev
, "ds1307_read_block_data (length=%d)\n", length
);
220 ret
= ds1307_read_block_data_once(client
, command
, length
, values
);
224 if (++tries
> BLOCK_DATA_MAX_TRIES
) {
225 dev_err(&client
->dev
,
226 "ds1307_read_block_data failed\n");
229 memcpy(oldvalues
, values
, length
);
230 ret
= ds1307_read_block_data_once(client
, command
, length
,
234 } while (memcmp(oldvalues
, values
, length
));
238 static s32
ds1307_write_block_data(const struct i2c_client
*client
, u8 command
,
239 u8 length
, const u8
*values
)
244 dev_dbg(&client
->dev
, "ds1307_write_block_data (length=%d)\n", length
);
248 if (++tries
> BLOCK_DATA_MAX_TRIES
) {
249 dev_err(&client
->dev
,
250 "ds1307_write_block_data failed\n");
253 for (i
= 0; i
< length
; i
++) {
254 ret
= i2c_smbus_write_byte_data(client
, command
+ i
,
259 ret
= ds1307_read_block_data_once(client
, command
, length
,
263 } while (memcmp(currvalues
, values
, length
));
267 /*----------------------------------------------------------------------*/
269 /* These RTC devices are not designed to be connected to a SMbus adapter.
270 SMbus limits block operations length to 32 bytes, whereas it's not
271 limited on I2C buses. As a result, accesses may exceed 32 bytes;
272 in that case, split them into smaller blocks */
274 static s32
ds1307_native_smbus_write_block_data(const struct i2c_client
*client
,
275 u8 command
, u8 length
, const u8
*values
)
279 if (length
<= I2C_SMBUS_BLOCK_MAX
) {
280 s32 retval
= i2c_smbus_write_i2c_block_data(client
,
281 command
, length
, values
);
287 while (suboffset
< length
) {
288 s32 retval
= i2c_smbus_write_i2c_block_data(client
,
290 min(I2C_SMBUS_BLOCK_MAX
, length
- suboffset
),
295 suboffset
+= I2C_SMBUS_BLOCK_MAX
;
300 static s32
ds1307_native_smbus_read_block_data(const struct i2c_client
*client
,
301 u8 command
, u8 length
, u8
*values
)
305 if (length
<= I2C_SMBUS_BLOCK_MAX
)
306 return i2c_smbus_read_i2c_block_data(client
,
307 command
, length
, values
);
309 while (suboffset
< length
) {
310 s32 retval
= i2c_smbus_read_i2c_block_data(client
,
312 min(I2C_SMBUS_BLOCK_MAX
, length
- suboffset
),
317 suboffset
+= I2C_SMBUS_BLOCK_MAX
;
322 /*----------------------------------------------------------------------*/
325 * The ds1337 and ds1339 both have two alarms, but we only use the first
326 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
327 * signal; ds1339 chips have only one alarm signal.
329 static irqreturn_t
ds1307_irq(int irq
, void *dev_id
)
331 struct i2c_client
*client
= dev_id
;
332 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
333 struct mutex
*lock
= &ds1307
->rtc
->ops_lock
;
337 stat
= i2c_smbus_read_byte_data(client
, DS1337_REG_STATUS
);
341 if (stat
& DS1337_BIT_A1I
) {
342 stat
&= ~DS1337_BIT_A1I
;
343 i2c_smbus_write_byte_data(client
, DS1337_REG_STATUS
, stat
);
345 control
= i2c_smbus_read_byte_data(client
, DS1337_REG_CONTROL
);
349 control
&= ~DS1337_BIT_A1IE
;
350 i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
, control
);
352 rtc_update_irq(ds1307
->rtc
, 1, RTC_AF
| RTC_IRQF
);
361 /*----------------------------------------------------------------------*/
363 static int ds1307_get_time(struct device
*dev
, struct rtc_time
*t
)
365 struct ds1307
*ds1307
= dev_get_drvdata(dev
);
368 /* read the RTC date and time registers all at once */
369 tmp
= ds1307
->read_block_data(ds1307
->client
,
370 ds1307
->offset
, 7, ds1307
->regs
);
372 dev_err(dev
, "%s error %d\n", "read", tmp
);
376 dev_dbg(dev
, "%s: %7ph\n", "read", ds1307
->regs
);
378 t
->tm_sec
= bcd2bin(ds1307
->regs
[DS1307_REG_SECS
] & 0x7f);
379 t
->tm_min
= bcd2bin(ds1307
->regs
[DS1307_REG_MIN
] & 0x7f);
380 tmp
= ds1307
->regs
[DS1307_REG_HOUR
] & 0x3f;
381 t
->tm_hour
= bcd2bin(tmp
);
382 t
->tm_wday
= bcd2bin(ds1307
->regs
[DS1307_REG_WDAY
] & 0x07) - 1;
383 t
->tm_mday
= bcd2bin(ds1307
->regs
[DS1307_REG_MDAY
] & 0x3f);
384 tmp
= ds1307
->regs
[DS1307_REG_MONTH
] & 0x1f;
385 t
->tm_mon
= bcd2bin(tmp
) - 1;
386 t
->tm_year
= bcd2bin(ds1307
->regs
[DS1307_REG_YEAR
]) + 100;
388 #ifdef CONFIG_RTC_DRV_DS1307_CENTURY
389 switch (ds1307
->type
) {
393 if (ds1307
->regs
[DS1307_REG_MONTH
] & DS1337_BIT_CENTURY
)
397 if (ds1307
->regs
[DS1307_REG_HOUR
] & DS1340_BIT_CENTURY
)
405 dev_dbg(dev
, "%s secs=%d, mins=%d, "
406 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
407 "read", t
->tm_sec
, t
->tm_min
,
408 t
->tm_hour
, t
->tm_mday
,
409 t
->tm_mon
, t
->tm_year
, t
->tm_wday
);
411 /* initial clock setting can be undefined */
412 return rtc_valid_tm(t
);
415 static int ds1307_set_time(struct device
*dev
, struct rtc_time
*t
)
417 struct ds1307
*ds1307
= dev_get_drvdata(dev
);
420 u8
*buf
= ds1307
->regs
;
422 dev_dbg(dev
, "%s secs=%d, mins=%d, "
423 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
424 "write", t
->tm_sec
, t
->tm_min
,
425 t
->tm_hour
, t
->tm_mday
,
426 t
->tm_mon
, t
->tm_year
, t
->tm_wday
);
428 #ifdef CONFIG_RTC_DRV_DS1307_CENTURY
429 if (t
->tm_year
< 100)
432 switch (ds1307
->type
) {
437 if (t
->tm_year
> 299)
440 if (t
->tm_year
> 199)
445 if (t
->tm_year
< 100 || t
->tm_year
> 199)
449 buf
[DS1307_REG_SECS
] = bin2bcd(t
->tm_sec
);
450 buf
[DS1307_REG_MIN
] = bin2bcd(t
->tm_min
);
451 buf
[DS1307_REG_HOUR
] = bin2bcd(t
->tm_hour
);
452 buf
[DS1307_REG_WDAY
] = bin2bcd(t
->tm_wday
+ 1);
453 buf
[DS1307_REG_MDAY
] = bin2bcd(t
->tm_mday
);
454 buf
[DS1307_REG_MONTH
] = bin2bcd(t
->tm_mon
+ 1);
456 /* assume 20YY not 19YY */
457 tmp
= t
->tm_year
- 100;
458 buf
[DS1307_REG_YEAR
] = bin2bcd(tmp
);
460 switch (ds1307
->type
) {
464 if (t
->tm_year
> 199)
465 buf
[DS1307_REG_MONTH
] |= DS1337_BIT_CENTURY
;
468 buf
[DS1307_REG_HOUR
] |= DS1340_BIT_CENTURY_EN
;
469 if (t
->tm_year
> 199)
470 buf
[DS1307_REG_HOUR
] |= DS1340_BIT_CENTURY
;
474 * these bits were cleared when preparing the date/time
475 * values and need to be set again before writing the
476 * buffer out to the device.
478 buf
[DS1307_REG_SECS
] |= MCP794XX_BIT_ST
;
479 buf
[DS1307_REG_WDAY
] |= MCP794XX_BIT_VBATEN
;
485 dev_dbg(dev
, "%s: %7ph\n", "write", buf
);
487 result
= ds1307
->write_block_data(ds1307
->client
,
488 ds1307
->offset
, 7, buf
);
490 dev_err(dev
, "%s error %d\n", "write", result
);
496 static int ds1337_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
498 struct i2c_client
*client
= to_i2c_client(dev
);
499 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
502 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
505 /* read all ALARM1, ALARM2, and status registers at once */
506 ret
= ds1307
->read_block_data(client
,
507 DS1339_REG_ALARM1_SECS
, 9, ds1307
->regs
);
509 dev_err(dev
, "%s error %d\n", "alarm read", ret
);
513 dev_dbg(dev
, "%s: %4ph, %3ph, %2ph\n", "alarm read",
514 &ds1307
->regs
[0], &ds1307
->regs
[4], &ds1307
->regs
[7]);
517 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
518 * and that all four fields are checked matches
520 t
->time
.tm_sec
= bcd2bin(ds1307
->regs
[0] & 0x7f);
521 t
->time
.tm_min
= bcd2bin(ds1307
->regs
[1] & 0x7f);
522 t
->time
.tm_hour
= bcd2bin(ds1307
->regs
[2] & 0x3f);
523 t
->time
.tm_mday
= bcd2bin(ds1307
->regs
[3] & 0x3f);
526 t
->enabled
= !!(ds1307
->regs
[7] & DS1337_BIT_A1IE
);
527 t
->pending
= !!(ds1307
->regs
[8] & DS1337_BIT_A1I
);
529 dev_dbg(dev
, "%s secs=%d, mins=%d, "
530 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
531 "alarm read", t
->time
.tm_sec
, t
->time
.tm_min
,
532 t
->time
.tm_hour
, t
->time
.tm_mday
,
533 t
->enabled
, t
->pending
);
538 static int ds1337_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
540 struct i2c_client
*client
= to_i2c_client(dev
);
541 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
542 unsigned char *buf
= ds1307
->regs
;
546 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
549 dev_dbg(dev
, "%s secs=%d, mins=%d, "
550 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
551 "alarm set", t
->time
.tm_sec
, t
->time
.tm_min
,
552 t
->time
.tm_hour
, t
->time
.tm_mday
,
553 t
->enabled
, t
->pending
);
555 /* read current status of both alarms and the chip */
556 ret
= ds1307
->read_block_data(client
,
557 DS1339_REG_ALARM1_SECS
, 9, buf
);
559 dev_err(dev
, "%s error %d\n", "alarm write", ret
);
562 control
= ds1307
->regs
[7];
563 status
= ds1307
->regs
[8];
565 dev_dbg(dev
, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
566 &ds1307
->regs
[0], &ds1307
->regs
[4], control
, status
);
568 /* set ALARM1, using 24 hour and day-of-month modes */
569 buf
[0] = bin2bcd(t
->time
.tm_sec
);
570 buf
[1] = bin2bcd(t
->time
.tm_min
);
571 buf
[2] = bin2bcd(t
->time
.tm_hour
);
572 buf
[3] = bin2bcd(t
->time
.tm_mday
);
574 /* set ALARM2 to non-garbage */
580 buf
[7] = control
& ~(DS1337_BIT_A1IE
| DS1337_BIT_A2IE
);
581 buf
[8] = status
& ~(DS1337_BIT_A1I
| DS1337_BIT_A2I
);
583 ret
= ds1307
->write_block_data(client
,
584 DS1339_REG_ALARM1_SECS
, 9, buf
);
586 dev_err(dev
, "can't set alarm time\n");
590 /* optionally enable ALARM1 */
592 dev_dbg(dev
, "alarm IRQ armed\n");
593 buf
[7] |= DS1337_BIT_A1IE
; /* only ALARM1 is used */
594 i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
, buf
[7]);
600 static int ds1307_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
602 struct i2c_client
*client
= to_i2c_client(dev
);
603 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
606 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
609 ret
= i2c_smbus_read_byte_data(client
, DS1337_REG_CONTROL
);
614 ret
|= DS1337_BIT_A1IE
;
616 ret
&= ~DS1337_BIT_A1IE
;
618 ret
= i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
, ret
);
625 static const struct rtc_class_ops ds13xx_rtc_ops
= {
626 .read_time
= ds1307_get_time
,
627 .set_time
= ds1307_set_time
,
628 .read_alarm
= ds1337_read_alarm
,
629 .set_alarm
= ds1337_set_alarm
,
630 .alarm_irq_enable
= ds1307_alarm_irq_enable
,
633 /*----------------------------------------------------------------------*/
636 * Alarm support for mcp794xx devices.
639 #define MCP794XX_REG_WEEKDAY 0x3
640 #define MCP794XX_REG_WEEKDAY_WDAY_MASK 0x7
641 #define MCP794XX_REG_CONTROL 0x07
642 # define MCP794XX_BIT_ALM0_EN 0x10
643 # define MCP794XX_BIT_ALM1_EN 0x20
644 #define MCP794XX_REG_ALARM0_BASE 0x0a
645 #define MCP794XX_REG_ALARM0_CTRL 0x0d
646 #define MCP794XX_REG_ALARM1_BASE 0x11
647 #define MCP794XX_REG_ALARM1_CTRL 0x14
648 # define MCP794XX_BIT_ALMX_IF (1 << 3)
649 # define MCP794XX_BIT_ALMX_C0 (1 << 4)
650 # define MCP794XX_BIT_ALMX_C1 (1 << 5)
651 # define MCP794XX_BIT_ALMX_C2 (1 << 6)
652 # define MCP794XX_BIT_ALMX_POL (1 << 7)
653 # define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
654 MCP794XX_BIT_ALMX_C1 | \
655 MCP794XX_BIT_ALMX_C2)
657 static irqreturn_t
mcp794xx_irq(int irq
, void *dev_id
)
659 struct i2c_client
*client
= dev_id
;
660 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
661 struct mutex
*lock
= &ds1307
->rtc
->ops_lock
;
666 /* Check and clear alarm 0 interrupt flag. */
667 reg
= i2c_smbus_read_byte_data(client
, MCP794XX_REG_ALARM0_CTRL
);
670 if (!(reg
& MCP794XX_BIT_ALMX_IF
))
672 reg
&= ~MCP794XX_BIT_ALMX_IF
;
673 ret
= i2c_smbus_write_byte_data(client
, MCP794XX_REG_ALARM0_CTRL
, reg
);
677 /* Disable alarm 0. */
678 reg
= i2c_smbus_read_byte_data(client
, MCP794XX_REG_CONTROL
);
681 reg
&= ~MCP794XX_BIT_ALM0_EN
;
682 ret
= i2c_smbus_write_byte_data(client
, MCP794XX_REG_CONTROL
, reg
);
686 rtc_update_irq(ds1307
->rtc
, 1, RTC_AF
| RTC_IRQF
);
694 static int mcp794xx_read_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
696 struct i2c_client
*client
= to_i2c_client(dev
);
697 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
698 u8
*regs
= ds1307
->regs
;
701 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
704 /* Read control and alarm 0 registers. */
705 ret
= ds1307
->read_block_data(client
, MCP794XX_REG_CONTROL
, 10, regs
);
709 t
->enabled
= !!(regs
[0] & MCP794XX_BIT_ALM0_EN
);
711 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
712 t
->time
.tm_sec
= bcd2bin(ds1307
->regs
[3] & 0x7f);
713 t
->time
.tm_min
= bcd2bin(ds1307
->regs
[4] & 0x7f);
714 t
->time
.tm_hour
= bcd2bin(ds1307
->regs
[5] & 0x3f);
715 t
->time
.tm_wday
= bcd2bin(ds1307
->regs
[6] & 0x7) - 1;
716 t
->time
.tm_mday
= bcd2bin(ds1307
->regs
[7] & 0x3f);
717 t
->time
.tm_mon
= bcd2bin(ds1307
->regs
[8] & 0x1f) - 1;
718 t
->time
.tm_year
= -1;
719 t
->time
.tm_yday
= -1;
720 t
->time
.tm_isdst
= -1;
722 dev_dbg(dev
, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
723 "enabled=%d polarity=%d irq=%d match=%d\n", __func__
,
724 t
->time
.tm_sec
, t
->time
.tm_min
, t
->time
.tm_hour
,
725 t
->time
.tm_wday
, t
->time
.tm_mday
, t
->time
.tm_mon
, t
->enabled
,
726 !!(ds1307
->regs
[6] & MCP794XX_BIT_ALMX_POL
),
727 !!(ds1307
->regs
[6] & MCP794XX_BIT_ALMX_IF
),
728 (ds1307
->regs
[6] & MCP794XX_MSK_ALMX_MATCH
) >> 4);
733 static int mcp794xx_set_alarm(struct device
*dev
, struct rtc_wkalrm
*t
)
735 struct i2c_client
*client
= to_i2c_client(dev
);
736 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
737 unsigned char *regs
= ds1307
->regs
;
740 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
743 dev_dbg(dev
, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
744 "enabled=%d pending=%d\n", __func__
,
745 t
->time
.tm_sec
, t
->time
.tm_min
, t
->time
.tm_hour
,
746 t
->time
.tm_wday
, t
->time
.tm_mday
, t
->time
.tm_mon
,
747 t
->enabled
, t
->pending
);
749 /* Read control and alarm 0 registers. */
750 ret
= ds1307
->read_block_data(client
, MCP794XX_REG_CONTROL
, 10, regs
);
754 /* Set alarm 0, using 24-hour and day-of-month modes. */
755 regs
[3] = bin2bcd(t
->time
.tm_sec
);
756 regs
[4] = bin2bcd(t
->time
.tm_min
);
757 regs
[5] = bin2bcd(t
->time
.tm_hour
);
758 regs
[6] = bin2bcd(t
->time
.tm_wday
+ 1);
759 regs
[7] = bin2bcd(t
->time
.tm_mday
);
760 regs
[8] = bin2bcd(t
->time
.tm_mon
+ 1);
762 /* Clear the alarm 0 interrupt flag. */
763 regs
[6] &= ~MCP794XX_BIT_ALMX_IF
;
764 /* Set alarm match: second, minute, hour, day, date, month. */
765 regs
[6] |= MCP794XX_MSK_ALMX_MATCH
;
766 /* Disable interrupt. We will not enable until completely programmed */
767 regs
[0] &= ~MCP794XX_BIT_ALM0_EN
;
769 ret
= ds1307
->write_block_data(client
, MCP794XX_REG_CONTROL
, 10, regs
);
775 regs
[0] |= MCP794XX_BIT_ALM0_EN
;
776 return i2c_smbus_write_byte_data(client
, MCP794XX_REG_CONTROL
, regs
[0]);
779 static int mcp794xx_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
781 struct i2c_client
*client
= to_i2c_client(dev
);
782 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
785 if (!test_bit(HAS_ALARM
, &ds1307
->flags
))
788 reg
= i2c_smbus_read_byte_data(client
, MCP794XX_REG_CONTROL
);
793 reg
|= MCP794XX_BIT_ALM0_EN
;
795 reg
&= ~MCP794XX_BIT_ALM0_EN
;
797 return i2c_smbus_write_byte_data(client
, MCP794XX_REG_CONTROL
, reg
);
800 static const struct rtc_class_ops mcp794xx_rtc_ops
= {
801 .read_time
= ds1307_get_time
,
802 .set_time
= ds1307_set_time
,
803 .read_alarm
= mcp794xx_read_alarm
,
804 .set_alarm
= mcp794xx_set_alarm
,
805 .alarm_irq_enable
= mcp794xx_alarm_irq_enable
,
808 /*----------------------------------------------------------------------*/
811 ds1307_nvram_read(struct file
*filp
, struct kobject
*kobj
,
812 struct bin_attribute
*attr
,
813 char *buf
, loff_t off
, size_t count
)
815 struct i2c_client
*client
;
816 struct ds1307
*ds1307
;
819 client
= kobj_to_i2c_client(kobj
);
820 ds1307
= i2c_get_clientdata(client
);
822 result
= ds1307
->read_block_data(client
, ds1307
->nvram_offset
+ off
,
825 dev_err(&client
->dev
, "%s error %d\n", "nvram read", result
);
830 ds1307_nvram_write(struct file
*filp
, struct kobject
*kobj
,
831 struct bin_attribute
*attr
,
832 char *buf
, loff_t off
, size_t count
)
834 struct i2c_client
*client
;
835 struct ds1307
*ds1307
;
838 client
= kobj_to_i2c_client(kobj
);
839 ds1307
= i2c_get_clientdata(client
);
841 result
= ds1307
->write_block_data(client
, ds1307
->nvram_offset
+ off
,
844 dev_err(&client
->dev
, "%s error %d\n", "nvram write", result
);
851 /*----------------------------------------------------------------------*/
853 static u8
do_trickle_setup_ds1339(struct i2c_client
*client
,
854 uint32_t ohms
, bool diode
)
856 u8 setup
= (diode
) ? DS1307_TRICKLE_CHARGER_DIODE
:
857 DS1307_TRICKLE_CHARGER_NO_DIODE
;
861 setup
|= DS1307_TRICKLE_CHARGER_250_OHM
;
864 setup
|= DS1307_TRICKLE_CHARGER_2K_OHM
;
867 setup
|= DS1307_TRICKLE_CHARGER_4K_OHM
;
870 dev_warn(&client
->dev
,
871 "Unsupported ohm value %u in dt\n", ohms
);
877 static void ds1307_trickle_of_init(struct i2c_client
*client
,
878 struct chip_desc
*chip
)
883 if (!chip
->do_trickle_setup
)
885 if (of_property_read_u32(client
->dev
.of_node
, "trickle-resistor-ohms" , &ohms
))
887 if (of_property_read_bool(client
->dev
.of_node
, "trickle-diode-disable"))
889 chip
->trickle_charger_setup
= chip
->do_trickle_setup(client
,
895 /*----------------------------------------------------------------------*/
897 #ifdef CONFIG_RTC_DRV_DS1307_HWMON
900 * Temperature sensor support for ds3231 devices.
903 #define DS3231_REG_TEMPERATURE 0x11
906 * A user-initiated temperature conversion is not started by this function,
907 * so the temperature is updated once every 64 seconds.
909 static int ds3231_hwmon_read_temp(struct device
*dev
, s32
*mC
)
911 struct ds1307
*ds1307
= dev_get_drvdata(dev
);
916 ret
= ds1307
->read_block_data(ds1307
->client
, DS3231_REG_TEMPERATURE
,
917 sizeof(temp_buf
), temp_buf
);
920 if (ret
!= sizeof(temp_buf
))
924 * Temperature is represented as a 10-bit code with a resolution of
925 * 0.25 degree celsius and encoded in two's complement format.
927 temp
= (temp_buf
[0] << 8) | temp_buf
[1];
934 static ssize_t
ds3231_hwmon_show_temp(struct device
*dev
,
935 struct device_attribute
*attr
, char *buf
)
940 ret
= ds3231_hwmon_read_temp(dev
, &temp
);
944 return sprintf(buf
, "%d\n", temp
);
946 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, ds3231_hwmon_show_temp
,
949 static struct attribute
*ds3231_hwmon_attrs
[] = {
950 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
953 ATTRIBUTE_GROUPS(ds3231_hwmon
);
955 static void ds1307_hwmon_register(struct ds1307
*ds1307
)
959 if (ds1307
->type
!= ds_3231
)
962 dev
= devm_hwmon_device_register_with_groups(&ds1307
->client
->dev
,
963 ds1307
->client
->name
,
964 ds1307
, ds3231_hwmon_groups
);
966 dev_warn(&ds1307
->client
->dev
,
967 "unable to register hwmon device %ld\n", PTR_ERR(dev
));
973 static void ds1307_hwmon_register(struct ds1307
*ds1307
)
977 #endif /* CONFIG_RTC_DRV_DS1307_HWMON */
979 /*----------------------------------------------------------------------*/
982 * Square-wave output support for DS3231
983 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
985 #ifdef CONFIG_COMMON_CLK
992 #define clk_sqw_to_ds1307(clk) \
993 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
994 #define clk_32khz_to_ds1307(clk) \
995 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
997 static int ds3231_clk_sqw_rates
[] = {
1004 static int ds1337_write_control(struct ds1307
*ds1307
, u8 mask
, u8 value
)
1006 struct i2c_client
*client
= ds1307
->client
;
1007 struct mutex
*lock
= &ds1307
->rtc
->ops_lock
;
1013 control
= i2c_smbus_read_byte_data(client
, DS1337_REG_CONTROL
);
1022 ret
= i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
, control
);
1029 static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw
*hw
,
1030 unsigned long parent_rate
)
1032 struct ds1307
*ds1307
= clk_sqw_to_ds1307(hw
);
1036 control
= i2c_smbus_read_byte_data(ds1307
->client
, DS1337_REG_CONTROL
);
1039 if (control
& DS1337_BIT_RS1
)
1041 if (control
& DS1337_BIT_RS2
)
1044 return ds3231_clk_sqw_rates
[rate_sel
];
1047 static long ds3231_clk_sqw_round_rate(struct clk_hw
*hw
, unsigned long rate
,
1048 unsigned long *prate
)
1052 for (i
= ARRAY_SIZE(ds3231_clk_sqw_rates
) - 1; i
>= 0; i
--) {
1053 if (ds3231_clk_sqw_rates
[i
] <= rate
)
1054 return ds3231_clk_sqw_rates
[i
];
1060 static int ds3231_clk_sqw_set_rate(struct clk_hw
*hw
, unsigned long rate
,
1061 unsigned long parent_rate
)
1063 struct ds1307
*ds1307
= clk_sqw_to_ds1307(hw
);
1067 for (rate_sel
= 0; rate_sel
< ARRAY_SIZE(ds3231_clk_sqw_rates
);
1069 if (ds3231_clk_sqw_rates
[rate_sel
] == rate
)
1073 if (rate_sel
== ARRAY_SIZE(ds3231_clk_sqw_rates
))
1077 control
|= DS1337_BIT_RS1
;
1079 control
|= DS1337_BIT_RS2
;
1081 return ds1337_write_control(ds1307
, DS1337_BIT_RS1
| DS1337_BIT_RS2
,
1085 static int ds3231_clk_sqw_prepare(struct clk_hw
*hw
)
1087 struct ds1307
*ds1307
= clk_sqw_to_ds1307(hw
);
1089 return ds1337_write_control(ds1307
, DS1337_BIT_INTCN
, 0);
1092 static void ds3231_clk_sqw_unprepare(struct clk_hw
*hw
)
1094 struct ds1307
*ds1307
= clk_sqw_to_ds1307(hw
);
1096 ds1337_write_control(ds1307
, DS1337_BIT_INTCN
, DS1337_BIT_INTCN
);
1099 static int ds3231_clk_sqw_is_prepared(struct clk_hw
*hw
)
1101 struct ds1307
*ds1307
= clk_sqw_to_ds1307(hw
);
1104 control
= i2c_smbus_read_byte_data(ds1307
->client
, DS1337_REG_CONTROL
);
1108 return !(control
& DS1337_BIT_INTCN
);
1111 static const struct clk_ops ds3231_clk_sqw_ops
= {
1112 .prepare
= ds3231_clk_sqw_prepare
,
1113 .unprepare
= ds3231_clk_sqw_unprepare
,
1114 .is_prepared
= ds3231_clk_sqw_is_prepared
,
1115 .recalc_rate
= ds3231_clk_sqw_recalc_rate
,
1116 .round_rate
= ds3231_clk_sqw_round_rate
,
1117 .set_rate
= ds3231_clk_sqw_set_rate
,
1120 static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw
*hw
,
1121 unsigned long parent_rate
)
1126 static int ds3231_clk_32khz_control(struct ds1307
*ds1307
, bool enable
)
1128 struct i2c_client
*client
= ds1307
->client
;
1129 struct mutex
*lock
= &ds1307
->rtc
->ops_lock
;
1135 status
= i2c_smbus_read_byte_data(client
, DS1337_REG_STATUS
);
1142 status
|= DS3231_BIT_EN32KHZ
;
1144 status
&= ~DS3231_BIT_EN32KHZ
;
1146 ret
= i2c_smbus_write_byte_data(client
, DS1337_REG_STATUS
, status
);
1153 static int ds3231_clk_32khz_prepare(struct clk_hw
*hw
)
1155 struct ds1307
*ds1307
= clk_32khz_to_ds1307(hw
);
1157 return ds3231_clk_32khz_control(ds1307
, true);
1160 static void ds3231_clk_32khz_unprepare(struct clk_hw
*hw
)
1162 struct ds1307
*ds1307
= clk_32khz_to_ds1307(hw
);
1164 ds3231_clk_32khz_control(ds1307
, false);
1167 static int ds3231_clk_32khz_is_prepared(struct clk_hw
*hw
)
1169 struct ds1307
*ds1307
= clk_32khz_to_ds1307(hw
);
1172 status
= i2c_smbus_read_byte_data(ds1307
->client
, DS1337_REG_STATUS
);
1176 return !!(status
& DS3231_BIT_EN32KHZ
);
1179 static const struct clk_ops ds3231_clk_32khz_ops
= {
1180 .prepare
= ds3231_clk_32khz_prepare
,
1181 .unprepare
= ds3231_clk_32khz_unprepare
,
1182 .is_prepared
= ds3231_clk_32khz_is_prepared
,
1183 .recalc_rate
= ds3231_clk_32khz_recalc_rate
,
1186 static struct clk_init_data ds3231_clks_init
[] = {
1187 [DS3231_CLK_SQW
] = {
1188 .name
= "ds3231_clk_sqw",
1189 .ops
= &ds3231_clk_sqw_ops
,
1191 [DS3231_CLK_32KHZ
] = {
1192 .name
= "ds3231_clk_32khz",
1193 .ops
= &ds3231_clk_32khz_ops
,
1197 static int ds3231_clks_register(struct ds1307
*ds1307
)
1199 struct i2c_client
*client
= ds1307
->client
;
1200 struct device_node
*node
= client
->dev
.of_node
;
1201 struct clk_onecell_data
*onecell
;
1204 onecell
= devm_kzalloc(&client
->dev
, sizeof(*onecell
), GFP_KERNEL
);
1208 onecell
->clk_num
= ARRAY_SIZE(ds3231_clks_init
);
1209 onecell
->clks
= devm_kcalloc(&client
->dev
, onecell
->clk_num
,
1210 sizeof(onecell
->clks
[0]), GFP_KERNEL
);
1214 for (i
= 0; i
< ARRAY_SIZE(ds3231_clks_init
); i
++) {
1215 struct clk_init_data init
= ds3231_clks_init
[i
];
1218 * Interrupt signal due to alarm conditions and square-wave
1219 * output share same pin, so don't initialize both.
1221 if (i
== DS3231_CLK_SQW
&& test_bit(HAS_ALARM
, &ds1307
->flags
))
1224 /* optional override of the clockname */
1225 of_property_read_string_index(node
, "clock-output-names", i
,
1227 ds1307
->clks
[i
].init
= &init
;
1229 onecell
->clks
[i
] = devm_clk_register(&client
->dev
,
1231 if (IS_ERR(onecell
->clks
[i
]))
1232 return PTR_ERR(onecell
->clks
[i
]);
1238 of_clk_add_provider(node
, of_clk_src_onecell_get
, onecell
);
1243 static void ds1307_clks_register(struct ds1307
*ds1307
)
1247 if (ds1307
->type
!= ds_3231
)
1250 ret
= ds3231_clks_register(ds1307
);
1252 dev_warn(&ds1307
->client
->dev
,
1253 "unable to register clock device %d\n", ret
);
1259 static void ds1307_clks_register(struct ds1307
*ds1307
)
1263 #endif /* CONFIG_COMMON_CLK */
1265 static int ds1307_probe(struct i2c_client
*client
,
1266 const struct i2c_device_id
*id
)
1268 struct ds1307
*ds1307
;
1271 struct chip_desc
*chip
= &chips
[id
->driver_data
];
1272 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
1273 bool want_irq
= false;
1274 bool ds1307_can_wakeup_device
= false;
1276 struct ds1307_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
1278 unsigned long timestamp
;
1280 irq_handler_t irq_handler
= ds1307_irq
;
1282 static const int bbsqi_bitpos
[] = {
1284 [ds_1339
] = DS1339_BIT_BBSQI
,
1285 [ds_3231
] = DS3231_BIT_BBSQW
,
1287 const struct rtc_class_ops
*rtc_ops
= &ds13xx_rtc_ops
;
1289 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)
1290 && !i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
))
1293 ds1307
= devm_kzalloc(&client
->dev
, sizeof(struct ds1307
), GFP_KERNEL
);
1297 i2c_set_clientdata(client
, ds1307
);
1299 ds1307
->client
= client
;
1300 ds1307
->type
= id
->driver_data
;
1302 if (!pdata
&& client
->dev
.of_node
)
1303 ds1307_trickle_of_init(client
, chip
);
1304 else if (pdata
&& pdata
->trickle_charger_setup
)
1305 chip
->trickle_charger_setup
= pdata
->trickle_charger_setup
;
1307 if (chip
->trickle_charger_setup
&& chip
->trickle_charger_reg
) {
1308 dev_dbg(&client
->dev
, "writing trickle charger info 0x%x to 0x%x\n",
1309 DS13XX_TRICKLE_CHARGER_MAGIC
| chip
->trickle_charger_setup
,
1310 chip
->trickle_charger_reg
);
1311 i2c_smbus_write_byte_data(client
, chip
->trickle_charger_reg
,
1312 DS13XX_TRICKLE_CHARGER_MAGIC
|
1313 chip
->trickle_charger_setup
);
1317 if (i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_I2C_BLOCK
)) {
1318 ds1307
->read_block_data
= ds1307_native_smbus_read_block_data
;
1319 ds1307
->write_block_data
= ds1307_native_smbus_write_block_data
;
1321 ds1307
->read_block_data
= ds1307_read_block_data
;
1322 ds1307
->write_block_data
= ds1307_write_block_data
;
1327 * For devices with no IRQ directly connected to the SoC, the RTC chip
1328 * can be forced as a wakeup source by stating that explicitly in
1329 * the device's .dts file using the "wakeup-source" boolean property.
1330 * If the "wakeup-source" property is set, don't request an IRQ.
1331 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1332 * if supported by the RTC.
1334 if (of_property_read_bool(client
->dev
.of_node
, "wakeup-source")) {
1335 ds1307_can_wakeup_device
= true;
1337 /* Intersil ISL12057 DT backward compatibility */
1338 if (of_property_read_bool(client
->dev
.of_node
,
1339 "isil,irq2-can-wakeup-machine")) {
1340 ds1307_can_wakeup_device
= true;
1344 switch (ds1307
->type
) {
1348 /* get registers that the "rtc" read below won't read... */
1349 tmp
= ds1307
->read_block_data(ds1307
->client
,
1350 DS1337_REG_CONTROL
, 2, buf
);
1352 dev_dbg(&client
->dev
, "read error %d\n", tmp
);
1357 /* oscillator off? turn it on, so clock can tick. */
1358 if (ds1307
->regs
[0] & DS1337_BIT_nEOSC
)
1359 ds1307
->regs
[0] &= ~DS1337_BIT_nEOSC
;
1362 * Using IRQ or defined as wakeup-source?
1363 * Disable the square wave and both alarms.
1364 * For some variants, be sure alarms can trigger when we're
1365 * running on Vbackup (BBSQI/BBSQW)
1367 if (chip
->alarm
&& (ds1307
->client
->irq
> 0 ||
1368 ds1307_can_wakeup_device
)) {
1369 ds1307
->regs
[0] |= DS1337_BIT_INTCN
1370 | bbsqi_bitpos
[ds1307
->type
];
1371 ds1307
->regs
[0] &= ~(DS1337_BIT_A2IE
| DS1337_BIT_A1IE
);
1376 i2c_smbus_write_byte_data(client
, DS1337_REG_CONTROL
,
1379 /* oscillator fault? clear flag, and warn */
1380 if (ds1307
->regs
[1] & DS1337_BIT_OSF
) {
1381 i2c_smbus_write_byte_data(client
, DS1337_REG_STATUS
,
1382 ds1307
->regs
[1] & ~DS1337_BIT_OSF
);
1383 dev_warn(&client
->dev
, "SET TIME!\n");
1388 tmp
= i2c_smbus_read_i2c_block_data(ds1307
->client
,
1389 RX8025_REG_CTRL1
<< 4 | 0x08, 2, buf
);
1391 dev_dbg(&client
->dev
, "read error %d\n", tmp
);
1396 /* oscillator off? turn it on, so clock can tick. */
1397 if (!(ds1307
->regs
[1] & RX8025_BIT_XST
)) {
1398 ds1307
->regs
[1] |= RX8025_BIT_XST
;
1399 i2c_smbus_write_byte_data(client
,
1400 RX8025_REG_CTRL2
<< 4 | 0x08,
1402 dev_warn(&client
->dev
,
1403 "oscillator stop detected - SET TIME!\n");
1406 if (ds1307
->regs
[1] & RX8025_BIT_PON
) {
1407 ds1307
->regs
[1] &= ~RX8025_BIT_PON
;
1408 i2c_smbus_write_byte_data(client
,
1409 RX8025_REG_CTRL2
<< 4 | 0x08,
1411 dev_warn(&client
->dev
, "power-on detected\n");
1414 if (ds1307
->regs
[1] & RX8025_BIT_VDET
) {
1415 ds1307
->regs
[1] &= ~RX8025_BIT_VDET
;
1416 i2c_smbus_write_byte_data(client
,
1417 RX8025_REG_CTRL2
<< 4 | 0x08,
1419 dev_warn(&client
->dev
, "voltage drop detected\n");
1422 /* make sure we are running in 24hour mode */
1423 if (!(ds1307
->regs
[0] & RX8025_BIT_2412
)) {
1426 /* switch to 24 hour mode */
1427 i2c_smbus_write_byte_data(client
,
1428 RX8025_REG_CTRL1
<< 4 | 0x08,
1432 tmp
= i2c_smbus_read_i2c_block_data(ds1307
->client
,
1433 RX8025_REG_CTRL1
<< 4 | 0x08, 2, buf
);
1435 dev_dbg(&client
->dev
, "read error %d\n", tmp
);
1441 hour
= bcd2bin(ds1307
->regs
[DS1307_REG_HOUR
]);
1444 if (ds1307
->regs
[DS1307_REG_HOUR
] & DS1307_BIT_PM
)
1447 i2c_smbus_write_byte_data(client
,
1448 DS1307_REG_HOUR
<< 4 | 0x08,
1453 ds1307
->offset
= 1; /* Seconds starts at 1 */
1456 rtc_ops
= &mcp794xx_rtc_ops
;
1457 if (ds1307
->client
->irq
> 0 && chip
->alarm
) {
1458 irq_handler
= mcp794xx_irq
;
1467 /* read RTC registers */
1468 tmp
= ds1307
->read_block_data(ds1307
->client
, ds1307
->offset
, 8, buf
);
1470 dev_dbg(&client
->dev
, "read error %d\n", tmp
);
1476 * minimal sanity checking; some chips (like DS1340) don't
1477 * specify the extra bits as must-be-zero, but there are
1478 * still a few values that are clearly out-of-range.
1480 tmp
= ds1307
->regs
[DS1307_REG_SECS
];
1481 switch (ds1307
->type
) {
1484 /* clock halted? turn it on, so clock can tick. */
1485 if (tmp
& DS1307_BIT_CH
) {
1486 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
1487 dev_warn(&client
->dev
, "SET TIME!\n");
1492 /* clock halted? turn it on, so clock can tick. */
1493 if (tmp
& DS1307_BIT_CH
)
1494 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
1496 /* oscillator fault? clear flag, and warn */
1497 if (ds1307
->regs
[DS1307_REG_CONTROL
] & DS1338_BIT_OSF
) {
1498 i2c_smbus_write_byte_data(client
, DS1307_REG_CONTROL
,
1499 ds1307
->regs
[DS1307_REG_CONTROL
]
1501 dev_warn(&client
->dev
, "SET TIME!\n");
1506 /* clock halted? turn it on, so clock can tick. */
1507 if (tmp
& DS1340_BIT_nEOSC
)
1508 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
, 0);
1510 tmp
= i2c_smbus_read_byte_data(client
, DS1340_REG_FLAG
);
1512 dev_dbg(&client
->dev
, "read error %d\n", tmp
);
1517 /* oscillator fault? clear flag, and warn */
1518 if (tmp
& DS1340_BIT_OSF
) {
1519 i2c_smbus_write_byte_data(client
, DS1340_REG_FLAG
, 0);
1520 dev_warn(&client
->dev
, "SET TIME!\n");
1524 /* make sure that the backup battery is enabled */
1525 if (!(ds1307
->regs
[DS1307_REG_WDAY
] & MCP794XX_BIT_VBATEN
)) {
1526 i2c_smbus_write_byte_data(client
, DS1307_REG_WDAY
,
1527 ds1307
->regs
[DS1307_REG_WDAY
]
1528 | MCP794XX_BIT_VBATEN
);
1531 /* clock halted? turn it on, so clock can tick. */
1532 if (!(tmp
& MCP794XX_BIT_ST
)) {
1533 i2c_smbus_write_byte_data(client
, DS1307_REG_SECS
,
1535 dev_warn(&client
->dev
, "SET TIME!\n");
1544 tmp
= ds1307
->regs
[DS1307_REG_HOUR
];
1545 switch (ds1307
->type
) {
1549 * NOTE: ignores century bits; fix before deploying
1550 * systems that will run through year 2100.
1556 if (!(tmp
& DS1307_BIT_12HR
))
1560 * Be sure we're in 24 hour mode. Multi-master systems
1563 tmp
= bcd2bin(tmp
& 0x1f);
1566 if (ds1307
->regs
[DS1307_REG_HOUR
] & DS1307_BIT_PM
)
1568 i2c_smbus_write_byte_data(client
,
1569 ds1307
->offset
+ DS1307_REG_HOUR
,
1574 * Some IPs have weekday reset value = 0x1 which might not correct
1575 * hence compute the wday using the current date/month/year values
1577 ds1307_get_time(&client
->dev
, &tm
);
1579 timestamp
= rtc_tm_to_time64(&tm
);
1580 rtc_time64_to_tm(timestamp
, &tm
);
1583 * Check if reset wday is different from the computed wday
1584 * If different then set the wday which we computed using
1587 if (wday
!= tm
.tm_wday
) {
1588 wday
= i2c_smbus_read_byte_data(client
, MCP794XX_REG_WEEKDAY
);
1589 wday
= wday
& ~MCP794XX_REG_WEEKDAY_WDAY_MASK
;
1590 wday
= wday
| (tm
.tm_wday
+ 1);
1591 i2c_smbus_write_byte_data(client
, MCP794XX_REG_WEEKDAY
, wday
);
1595 device_set_wakeup_capable(&client
->dev
, true);
1596 set_bit(HAS_ALARM
, &ds1307
->flags
);
1598 ds1307
->rtc
= devm_rtc_device_register(&client
->dev
, client
->name
,
1599 rtc_ops
, THIS_MODULE
);
1600 if (IS_ERR(ds1307
->rtc
)) {
1601 return PTR_ERR(ds1307
->rtc
);
1604 if (ds1307_can_wakeup_device
&& ds1307
->client
->irq
<= 0) {
1605 /* Disable request for an IRQ */
1607 dev_info(&client
->dev
, "'wakeup-source' is set, request for an IRQ is disabled!\n");
1608 /* We cannot support UIE mode if we do not have an IRQ line */
1609 ds1307
->rtc
->uie_unsupported
= 1;
1613 err
= devm_request_threaded_irq(&client
->dev
,
1614 client
->irq
, NULL
, irq_handler
,
1615 IRQF_SHARED
| IRQF_ONESHOT
,
1616 ds1307
->rtc
->name
, client
);
1619 device_set_wakeup_capable(&client
->dev
, false);
1620 clear_bit(HAS_ALARM
, &ds1307
->flags
);
1621 dev_err(&client
->dev
, "unable to request IRQ!\n");
1623 dev_dbg(&client
->dev
, "got IRQ %d\n", client
->irq
);
1626 if (chip
->nvram_size
) {
1628 ds1307
->nvram
= devm_kzalloc(&client
->dev
,
1629 sizeof(struct bin_attribute
),
1631 if (!ds1307
->nvram
) {
1632 dev_err(&client
->dev
, "cannot allocate memory for nvram sysfs\n");
1635 ds1307
->nvram
->attr
.name
= "nvram";
1636 ds1307
->nvram
->attr
.mode
= S_IRUGO
| S_IWUSR
;
1638 sysfs_bin_attr_init(ds1307
->nvram
);
1640 ds1307
->nvram
->read
= ds1307_nvram_read
;
1641 ds1307
->nvram
->write
= ds1307_nvram_write
;
1642 ds1307
->nvram
->size
= chip
->nvram_size
;
1643 ds1307
->nvram_offset
= chip
->nvram_offset
;
1645 err
= sysfs_create_bin_file(&client
->dev
.kobj
,
1648 dev_err(&client
->dev
,
1649 "unable to create sysfs file: %s\n",
1650 ds1307
->nvram
->attr
.name
);
1652 set_bit(HAS_NVRAM
, &ds1307
->flags
);
1653 dev_info(&client
->dev
, "%zu bytes nvram\n",
1654 ds1307
->nvram
->size
);
1659 ds1307_hwmon_register(ds1307
);
1660 ds1307_clks_register(ds1307
);
1668 static int ds1307_remove(struct i2c_client
*client
)
1670 struct ds1307
*ds1307
= i2c_get_clientdata(client
);
1672 if (test_and_clear_bit(HAS_NVRAM
, &ds1307
->flags
))
1673 sysfs_remove_bin_file(&client
->dev
.kobj
, ds1307
->nvram
);
1678 static struct i2c_driver ds1307_driver
= {
1680 .name
= "rtc-ds1307",
1682 .probe
= ds1307_probe
,
1683 .remove
= ds1307_remove
,
1684 .id_table
= ds1307_id
,
1687 module_i2c_driver(ds1307_driver
);
1689 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1690 MODULE_LICENSE("GPL");