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/acpi.h>
15 #include <linux/bcd.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/rtc/ds1307.h>
21 #include <linux/rtc.h>
22 #include <linux/slab.h>
23 #include <linux/string.h>
24 #include <linux/hwmon.h>
25 #include <linux/hwmon-sysfs.h>
26 #include <linux/clk-provider.h>
27 #include <linux/regmap.h>
30 * We can't determine type by probing, but if we expect pre-Linux code
31 * to have set the chip up as a clock (turning on the oscillator and
32 * setting the date and time), Linux can ignore the non-clock features.
33 * That's a natural job for a factory or repair bench.
51 last_ds_type /* always last */
52 /* rs5c372 too? different address... */
55 /* RTC registers don't differ much, except for the century flag */
56 #define DS1307_REG_SECS 0x00 /* 00-59 */
57 # define DS1307_BIT_CH 0x80
58 # define DS1340_BIT_nEOSC 0x80
59 # define MCP794XX_BIT_ST 0x80
60 #define DS1307_REG_MIN 0x01 /* 00-59 */
61 # define M41T0_BIT_OF 0x80
62 #define DS1307_REG_HOUR 0x02 /* 00-23, or 1-12{am,pm} */
63 # define DS1307_BIT_12HR 0x40 /* in REG_HOUR */
64 # define DS1307_BIT_PM 0x20 /* in REG_HOUR */
65 # define DS1340_BIT_CENTURY_EN 0x80 /* in REG_HOUR */
66 # define DS1340_BIT_CENTURY 0x40 /* in REG_HOUR */
67 #define DS1307_REG_WDAY 0x03 /* 01-07 */
68 # define MCP794XX_BIT_VBATEN 0x08
69 #define DS1307_REG_MDAY 0x04 /* 01-31 */
70 #define DS1307_REG_MONTH 0x05 /* 01-12 */
71 # define DS1337_BIT_CENTURY 0x80 /* in REG_MONTH */
72 #define DS1307_REG_YEAR 0x06 /* 00-99 */
75 * Other registers (control, status, alarms, trickle charge, NVRAM, etc)
76 * start at 7, and they differ a LOT. Only control and status matter for
77 * basic RTC date and time functionality; be careful using them.
79 #define DS1307_REG_CONTROL 0x07 /* or ds1338 */
80 # define DS1307_BIT_OUT 0x80
81 # define DS1338_BIT_OSF 0x20
82 # define DS1307_BIT_SQWE 0x10
83 # define DS1307_BIT_RS1 0x02
84 # define DS1307_BIT_RS0 0x01
85 #define DS1337_REG_CONTROL 0x0e
86 # define DS1337_BIT_nEOSC 0x80
87 # define DS1339_BIT_BBSQI 0x20
88 # define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */
89 # define DS1337_BIT_RS2 0x10
90 # define DS1337_BIT_RS1 0x08
91 # define DS1337_BIT_INTCN 0x04
92 # define DS1337_BIT_A2IE 0x02
93 # define DS1337_BIT_A1IE 0x01
94 #define DS1340_REG_CONTROL 0x07
95 # define DS1340_BIT_OUT 0x80
96 # define DS1340_BIT_FT 0x40
97 # define DS1340_BIT_CALIB_SIGN 0x20
98 # define DS1340_M_CALIBRATION 0x1f
99 #define DS1340_REG_FLAG 0x09
100 # define DS1340_BIT_OSF 0x80
101 #define DS1337_REG_STATUS 0x0f
102 # define DS1337_BIT_OSF 0x80
103 # define DS3231_BIT_EN32KHZ 0x08
104 # define DS1337_BIT_A2I 0x02
105 # define DS1337_BIT_A1I 0x01
106 #define DS1339_REG_ALARM1_SECS 0x07
108 #define DS13XX_TRICKLE_CHARGER_MAGIC 0xa0
110 #define RX8025_REG_CTRL1 0x0e
111 # define RX8025_BIT_2412 0x20
112 #define RX8025_REG_CTRL2 0x0f
113 # define RX8025_BIT_PON 0x10
114 # define RX8025_BIT_VDET 0x40
115 # define RX8025_BIT_XST 0x20
120 #define HAS_NVRAM 0 /* bit 0 == sysfs file active */
121 #define HAS_ALARM 1 /* bit 1 == irq claimed */
123 struct regmap *regmap;
125 struct rtc_device *rtc;
126 #ifdef CONFIG_COMMON_CLK
127 struct clk_hw clks[2];
135 u8 offset; /* register's offset */
137 u8 century_enable_bit;
140 irq_handler_t irq_handler;
141 const struct rtc_class_ops *rtc_ops;
142 u16 trickle_charger_reg;
143 u8 (*do_trickle_setup)(struct ds1307 *, u32,
147 static int ds1307_get_time(struct device *dev, struct rtc_time *t);
148 static int ds1307_set_time(struct device *dev, struct rtc_time *t);
149 static u8 do_trickle_setup_ds1339(struct ds1307 *, u32 ohms, bool diode);
150 static irqreturn_t rx8130_irq(int irq, void *dev_id);
151 static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t);
152 static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t);
153 static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled);
154 static irqreturn_t mcp794xx_irq(int irq, void *dev_id);
155 static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t);
156 static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t);
157 static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled);
159 static const struct rtc_class_ops rx8130_rtc_ops = {
160 .read_time = ds1307_get_time,
161 .set_time = ds1307_set_time,
162 .read_alarm = rx8130_read_alarm,
163 .set_alarm = rx8130_set_alarm,
164 .alarm_irq_enable = rx8130_alarm_irq_enable,
167 static const struct rtc_class_ops mcp794xx_rtc_ops = {
168 .read_time = ds1307_get_time,
169 .set_time = ds1307_set_time,
170 .read_alarm = mcp794xx_read_alarm,
171 .set_alarm = mcp794xx_set_alarm,
172 .alarm_irq_enable = mcp794xx_alarm_irq_enable,
175 static const struct chip_desc chips[last_ds_type] = {
186 .century_reg = DS1307_REG_MONTH,
187 .century_bit = DS1337_BIT_CENTURY,
195 .century_reg = DS1307_REG_MONTH,
196 .century_bit = DS1337_BIT_CENTURY,
197 .bbsqi_bit = DS1339_BIT_BBSQI,
198 .trickle_charger_reg = 0x10,
199 .do_trickle_setup = &do_trickle_setup_ds1339,
202 .century_reg = DS1307_REG_HOUR,
203 .century_enable_bit = DS1340_BIT_CENTURY_EN,
204 .century_bit = DS1340_BIT_CENTURY,
205 .do_trickle_setup = &do_trickle_setup_ds1339,
206 .trickle_charger_reg = 0x08,
209 .century_reg = DS1307_REG_MONTH,
210 .century_bit = DS1337_BIT_CENTURY,
214 .trickle_charger_reg = 0x0a,
218 .century_reg = DS1307_REG_MONTH,
219 .century_bit = DS1337_BIT_CENTURY,
220 .bbsqi_bit = DS3231_BIT_BBSQW,
224 /* this is battery backed SRAM */
225 .nvram_offset = 0x20,
226 .nvram_size = 4, /* 32bit (4 word x 8 bit) */
228 .irq_handler = rx8130_irq,
229 .rtc_ops = &rx8130_rtc_ops,
232 /* this is battery backed SRAM */
238 /* this is battery backed SRAM */
239 .nvram_offset = 0x20,
241 .irq_handler = mcp794xx_irq,
242 .rtc_ops = &mcp794xx_rtc_ops,
246 static const struct i2c_device_id ds1307_id[] = {
247 { "ds1307", ds_1307 },
248 { "ds1308", ds_1308 },
249 { "ds1337", ds_1337 },
250 { "ds1338", ds_1338 },
251 { "ds1339", ds_1339 },
252 { "ds1388", ds_1388 },
253 { "ds1340", ds_1340 },
254 { "ds1341", ds_1341 },
255 { "ds3231", ds_3231 },
257 { "m41t00", m41t00 },
258 { "m41t11", m41t11 },
259 { "mcp7940x", mcp794xx },
260 { "mcp7941x", mcp794xx },
261 { "pt7c4338", ds_1307 },
262 { "rx8025", rx_8025 },
263 { "isl12057", ds_1337 },
264 { "rx8130", rx_8130 },
267 MODULE_DEVICE_TABLE(i2c, ds1307_id);
270 static const struct of_device_id ds1307_of_match[] = {
272 .compatible = "dallas,ds1307",
273 .data = (void *)ds_1307
276 .compatible = "dallas,ds1308",
277 .data = (void *)ds_1308
280 .compatible = "dallas,ds1337",
281 .data = (void *)ds_1337
284 .compatible = "dallas,ds1338",
285 .data = (void *)ds_1338
288 .compatible = "dallas,ds1339",
289 .data = (void *)ds_1339
292 .compatible = "dallas,ds1388",
293 .data = (void *)ds_1388
296 .compatible = "dallas,ds1340",
297 .data = (void *)ds_1340
300 .compatible = "dallas,ds1341",
301 .data = (void *)ds_1341
304 .compatible = "maxim,ds3231",
305 .data = (void *)ds_3231
308 .compatible = "st,m41t0",
309 .data = (void *)m41t0
312 .compatible = "st,m41t00",
313 .data = (void *)m41t00
316 .compatible = "st,m41t11",
317 .data = (void *)m41t11
320 .compatible = "microchip,mcp7940x",
321 .data = (void *)mcp794xx
324 .compatible = "microchip,mcp7941x",
325 .data = (void *)mcp794xx
328 .compatible = "pericom,pt7c4338",
329 .data = (void *)ds_1307
332 .compatible = "epson,rx8025",
333 .data = (void *)rx_8025
336 .compatible = "isil,isl12057",
337 .data = (void *)ds_1337
340 .compatible = "epson,rx8130",
341 .data = (void *)rx_8130
345 MODULE_DEVICE_TABLE(of, ds1307_of_match);
349 static const struct acpi_device_id ds1307_acpi_ids[] = {
350 { .id = "DS1307", .driver_data = ds_1307 },
351 { .id = "DS1308", .driver_data = ds_1308 },
352 { .id = "DS1337", .driver_data = ds_1337 },
353 { .id = "DS1338", .driver_data = ds_1338 },
354 { .id = "DS1339", .driver_data = ds_1339 },
355 { .id = "DS1388", .driver_data = ds_1388 },
356 { .id = "DS1340", .driver_data = ds_1340 },
357 { .id = "DS1341", .driver_data = ds_1341 },
358 { .id = "DS3231", .driver_data = ds_3231 },
359 { .id = "M41T0", .driver_data = m41t0 },
360 { .id = "M41T00", .driver_data = m41t00 },
361 { .id = "M41T11", .driver_data = m41t11 },
362 { .id = "MCP7940X", .driver_data = mcp794xx },
363 { .id = "MCP7941X", .driver_data = mcp794xx },
364 { .id = "PT7C4338", .driver_data = ds_1307 },
365 { .id = "RX8025", .driver_data = rx_8025 },
366 { .id = "ISL12057", .driver_data = ds_1337 },
367 { .id = "RX8130", .driver_data = rx_8130 },
370 MODULE_DEVICE_TABLE(acpi, ds1307_acpi_ids);
374 * The ds1337 and ds1339 both have two alarms, but we only use the first
375 * one (with a "seconds" field). For ds1337 we expect nINTA is our alarm
376 * signal; ds1339 chips have only one alarm signal.
378 static irqreturn_t ds1307_irq(int irq, void *dev_id)
380 struct ds1307 *ds1307 = dev_id;
381 struct mutex *lock = &ds1307->rtc->ops_lock;
385 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &stat);
389 if (stat & DS1337_BIT_A1I) {
390 stat &= ~DS1337_BIT_A1I;
391 regmap_write(ds1307->regmap, DS1337_REG_STATUS, stat);
393 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
398 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
407 /*----------------------------------------------------------------------*/
409 static int ds1307_get_time(struct device *dev, struct rtc_time *t)
411 struct ds1307 *ds1307 = dev_get_drvdata(dev);
413 const struct chip_desc *chip = &chips[ds1307->type];
416 /* read the RTC date and time registers all at once */
417 ret = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
420 dev_err(dev, "%s error %d\n", "read", ret);
424 dev_dbg(dev, "%s: %7ph\n", "read", regs);
426 /* if oscillator fail bit is set, no data can be trusted */
427 if (ds1307->type == m41t0 &&
428 regs[DS1307_REG_MIN] & M41T0_BIT_OF) {
429 dev_warn_once(dev, "oscillator failed, set time!\n");
433 t->tm_sec = bcd2bin(regs[DS1307_REG_SECS] & 0x7f);
434 t->tm_min = bcd2bin(regs[DS1307_REG_MIN] & 0x7f);
435 tmp = regs[DS1307_REG_HOUR] & 0x3f;
436 t->tm_hour = bcd2bin(tmp);
437 /* rx8130 is bit position, not BCD */
438 if (ds1307->type == rx_8130)
439 t->tm_wday = fls(regs[DS1307_REG_WDAY] & 0x7f);
441 t->tm_wday = bcd2bin(regs[DS1307_REG_WDAY] & 0x07) - 1;
442 t->tm_mday = bcd2bin(regs[DS1307_REG_MDAY] & 0x3f);
443 tmp = regs[DS1307_REG_MONTH] & 0x1f;
444 t->tm_mon = bcd2bin(tmp) - 1;
445 t->tm_year = bcd2bin(regs[DS1307_REG_YEAR]) + 100;
447 if (regs[chip->century_reg] & chip->century_bit &&
448 IS_ENABLED(CONFIG_RTC_DRV_DS1307_CENTURY))
451 dev_dbg(dev, "%s secs=%d, mins=%d, "
452 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
453 "read", t->tm_sec, t->tm_min,
454 t->tm_hour, t->tm_mday,
455 t->tm_mon, t->tm_year, t->tm_wday);
460 static int ds1307_set_time(struct device *dev, struct rtc_time *t)
462 struct ds1307 *ds1307 = dev_get_drvdata(dev);
463 const struct chip_desc *chip = &chips[ds1307->type];
468 dev_dbg(dev, "%s secs=%d, mins=%d, "
469 "hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
470 "write", t->tm_sec, t->tm_min,
471 t->tm_hour, t->tm_mday,
472 t->tm_mon, t->tm_year, t->tm_wday);
474 if (t->tm_year < 100)
477 #ifdef CONFIG_RTC_DRV_DS1307_CENTURY
478 if (t->tm_year > (chip->century_bit ? 299 : 199))
481 if (t->tm_year > 199)
485 regs[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
486 regs[DS1307_REG_MIN] = bin2bcd(t->tm_min);
487 regs[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
488 /* rx8130 is bit position, not BCD */
489 if (ds1307->type == rx_8130)
490 regs[DS1307_REG_WDAY] = 1 << t->tm_wday;
492 regs[DS1307_REG_WDAY] = bin2bcd(t->tm_wday + 1);
493 regs[DS1307_REG_MDAY] = bin2bcd(t->tm_mday);
494 regs[DS1307_REG_MONTH] = bin2bcd(t->tm_mon + 1);
496 /* assume 20YY not 19YY */
497 tmp = t->tm_year - 100;
498 regs[DS1307_REG_YEAR] = bin2bcd(tmp);
500 if (chip->century_enable_bit)
501 regs[chip->century_reg] |= chip->century_enable_bit;
502 if (t->tm_year > 199 && chip->century_bit)
503 regs[chip->century_reg] |= chip->century_bit;
505 if (ds1307->type == mcp794xx) {
507 * these bits were cleared when preparing the date/time
508 * values and need to be set again before writing the
509 * regsfer out to the device.
511 regs[DS1307_REG_SECS] |= MCP794XX_BIT_ST;
512 regs[DS1307_REG_WDAY] |= MCP794XX_BIT_VBATEN;
515 dev_dbg(dev, "%s: %7ph\n", "write", regs);
517 result = regmap_bulk_write(ds1307->regmap, chip->offset, regs,
520 dev_err(dev, "%s error %d\n", "write", result);
526 static int ds1337_read_alarm(struct device *dev, struct rtc_wkalrm *t)
528 struct ds1307 *ds1307 = dev_get_drvdata(dev);
532 if (!test_bit(HAS_ALARM, &ds1307->flags))
535 /* read all ALARM1, ALARM2, and status registers at once */
536 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS,
539 dev_err(dev, "%s error %d\n", "alarm read", ret);
543 dev_dbg(dev, "%s: %4ph, %3ph, %2ph\n", "alarm read",
544 ®s[0], ®s[4], ®s[7]);
547 * report alarm time (ALARM1); assume 24 hour and day-of-month modes,
548 * and that all four fields are checked matches
550 t->time.tm_sec = bcd2bin(regs[0] & 0x7f);
551 t->time.tm_min = bcd2bin(regs[1] & 0x7f);
552 t->time.tm_hour = bcd2bin(regs[2] & 0x3f);
553 t->time.tm_mday = bcd2bin(regs[3] & 0x3f);
556 t->enabled = !!(regs[7] & DS1337_BIT_A1IE);
557 t->pending = !!(regs[8] & DS1337_BIT_A1I);
559 dev_dbg(dev, "%s secs=%d, mins=%d, "
560 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
561 "alarm read", t->time.tm_sec, t->time.tm_min,
562 t->time.tm_hour, t->time.tm_mday,
563 t->enabled, t->pending);
568 static int ds1337_set_alarm(struct device *dev, struct rtc_wkalrm *t)
570 struct ds1307 *ds1307 = dev_get_drvdata(dev);
571 unsigned char regs[9];
575 if (!test_bit(HAS_ALARM, &ds1307->flags))
578 dev_dbg(dev, "%s secs=%d, mins=%d, "
579 "hours=%d, mday=%d, enabled=%d, pending=%d\n",
580 "alarm set", t->time.tm_sec, t->time.tm_min,
581 t->time.tm_hour, t->time.tm_mday,
582 t->enabled, t->pending);
584 /* read current status of both alarms and the chip */
585 ret = regmap_bulk_read(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
588 dev_err(dev, "%s error %d\n", "alarm write", ret);
594 dev_dbg(dev, "%s: %4ph, %3ph, %02x %02x\n", "alarm set (old status)",
595 ®s[0], ®s[4], control, status);
597 /* set ALARM1, using 24 hour and day-of-month modes */
598 regs[0] = bin2bcd(t->time.tm_sec);
599 regs[1] = bin2bcd(t->time.tm_min);
600 regs[2] = bin2bcd(t->time.tm_hour);
601 regs[3] = bin2bcd(t->time.tm_mday);
603 /* set ALARM2 to non-garbage */
609 regs[7] = control & ~(DS1337_BIT_A1IE | DS1337_BIT_A2IE);
610 regs[8] = status & ~(DS1337_BIT_A1I | DS1337_BIT_A2I);
612 ret = regmap_bulk_write(ds1307->regmap, DS1339_REG_ALARM1_SECS, regs,
615 dev_err(dev, "can't set alarm time\n");
619 /* optionally enable ALARM1 */
621 dev_dbg(dev, "alarm IRQ armed\n");
622 regs[7] |= DS1337_BIT_A1IE; /* only ALARM1 is used */
623 regmap_write(ds1307->regmap, DS1337_REG_CONTROL, regs[7]);
629 static int ds1307_alarm_irq_enable(struct device *dev, unsigned int enabled)
631 struct ds1307 *ds1307 = dev_get_drvdata(dev);
633 if (!test_bit(HAS_ALARM, &ds1307->flags))
636 return regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
638 enabled ? DS1337_BIT_A1IE : 0);
641 static const struct rtc_class_ops ds13xx_rtc_ops = {
642 .read_time = ds1307_get_time,
643 .set_time = ds1307_set_time,
644 .read_alarm = ds1337_read_alarm,
645 .set_alarm = ds1337_set_alarm,
646 .alarm_irq_enable = ds1307_alarm_irq_enable,
649 /*----------------------------------------------------------------------*/
652 * Alarm support for rx8130 devices.
655 #define RX8130_REG_ALARM_MIN 0x07
656 #define RX8130_REG_ALARM_HOUR 0x08
657 #define RX8130_REG_ALARM_WEEK_OR_DAY 0x09
658 #define RX8130_REG_EXTENSION 0x0c
659 #define RX8130_REG_EXTENSION_WADA BIT(3)
660 #define RX8130_REG_FLAG 0x0d
661 #define RX8130_REG_FLAG_AF BIT(3)
662 #define RX8130_REG_CONTROL0 0x0e
663 #define RX8130_REG_CONTROL0_AIE BIT(3)
665 static irqreturn_t rx8130_irq(int irq, void *dev_id)
667 struct ds1307 *ds1307 = dev_id;
668 struct mutex *lock = &ds1307->rtc->ops_lock;
674 /* Read control registers. */
675 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
679 if (!(ctl[1] & RX8130_REG_FLAG_AF))
681 ctl[1] &= ~RX8130_REG_FLAG_AF;
682 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
684 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
689 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
697 static int rx8130_read_alarm(struct device *dev, struct rtc_wkalrm *t)
699 struct ds1307 *ds1307 = dev_get_drvdata(dev);
703 if (!test_bit(HAS_ALARM, &ds1307->flags))
706 /* Read alarm registers. */
707 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
712 /* Read control registers. */
713 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
718 t->enabled = !!(ctl[2] & RX8130_REG_CONTROL0_AIE);
719 t->pending = !!(ctl[1] & RX8130_REG_FLAG_AF);
721 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
723 t->time.tm_min = bcd2bin(ald[0] & 0x7f);
724 t->time.tm_hour = bcd2bin(ald[1] & 0x7f);
725 t->time.tm_wday = -1;
726 t->time.tm_mday = bcd2bin(ald[2] & 0x7f);
728 t->time.tm_year = -1;
729 t->time.tm_yday = -1;
730 t->time.tm_isdst = -1;
732 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d enabled=%d\n",
733 __func__, t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
734 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled);
739 static int rx8130_set_alarm(struct device *dev, struct rtc_wkalrm *t)
741 struct ds1307 *ds1307 = dev_get_drvdata(dev);
745 if (!test_bit(HAS_ALARM, &ds1307->flags))
748 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
749 "enabled=%d pending=%d\n", __func__,
750 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
751 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
752 t->enabled, t->pending);
754 /* Read control registers. */
755 ret = regmap_bulk_read(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
760 ctl[0] &= RX8130_REG_EXTENSION_WADA;
761 ctl[1] &= ~RX8130_REG_FLAG_AF;
762 ctl[2] &= ~RX8130_REG_CONTROL0_AIE;
764 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_EXTENSION, ctl,
769 /* Hardware alarm precision is 1 minute! */
770 ald[0] = bin2bcd(t->time.tm_min);
771 ald[1] = bin2bcd(t->time.tm_hour);
772 ald[2] = bin2bcd(t->time.tm_mday);
774 ret = regmap_bulk_write(ds1307->regmap, RX8130_REG_ALARM_MIN, ald,
782 ctl[2] |= RX8130_REG_CONTROL0_AIE;
784 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, ctl[2]);
787 static int rx8130_alarm_irq_enable(struct device *dev, unsigned int enabled)
789 struct ds1307 *ds1307 = dev_get_drvdata(dev);
792 if (!test_bit(HAS_ALARM, &ds1307->flags))
795 ret = regmap_read(ds1307->regmap, RX8130_REG_CONTROL0, ®);
800 reg |= RX8130_REG_CONTROL0_AIE;
802 reg &= ~RX8130_REG_CONTROL0_AIE;
804 return regmap_write(ds1307->regmap, RX8130_REG_CONTROL0, reg);
807 /*----------------------------------------------------------------------*/
810 * Alarm support for mcp794xx devices.
813 #define MCP794XX_REG_CONTROL 0x07
814 # define MCP794XX_BIT_ALM0_EN 0x10
815 # define MCP794XX_BIT_ALM1_EN 0x20
816 #define MCP794XX_REG_ALARM0_BASE 0x0a
817 #define MCP794XX_REG_ALARM0_CTRL 0x0d
818 #define MCP794XX_REG_ALARM1_BASE 0x11
819 #define MCP794XX_REG_ALARM1_CTRL 0x14
820 # define MCP794XX_BIT_ALMX_IF BIT(3)
821 # define MCP794XX_BIT_ALMX_C0 BIT(4)
822 # define MCP794XX_BIT_ALMX_C1 BIT(5)
823 # define MCP794XX_BIT_ALMX_C2 BIT(6)
824 # define MCP794XX_BIT_ALMX_POL BIT(7)
825 # define MCP794XX_MSK_ALMX_MATCH (MCP794XX_BIT_ALMX_C0 | \
826 MCP794XX_BIT_ALMX_C1 | \
827 MCP794XX_BIT_ALMX_C2)
829 static irqreturn_t mcp794xx_irq(int irq, void *dev_id)
831 struct ds1307 *ds1307 = dev_id;
832 struct mutex *lock = &ds1307->rtc->ops_lock;
837 /* Check and clear alarm 0 interrupt flag. */
838 ret = regmap_read(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, ®);
841 if (!(reg & MCP794XX_BIT_ALMX_IF))
843 reg &= ~MCP794XX_BIT_ALMX_IF;
844 ret = regmap_write(ds1307->regmap, MCP794XX_REG_ALARM0_CTRL, reg);
848 /* Disable alarm 0. */
849 ret = regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
850 MCP794XX_BIT_ALM0_EN, 0);
854 rtc_update_irq(ds1307->rtc, 1, RTC_AF | RTC_IRQF);
862 static int mcp794xx_read_alarm(struct device *dev, struct rtc_wkalrm *t)
864 struct ds1307 *ds1307 = dev_get_drvdata(dev);
868 if (!test_bit(HAS_ALARM, &ds1307->flags))
871 /* Read control and alarm 0 registers. */
872 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
877 t->enabled = !!(regs[0] & MCP794XX_BIT_ALM0_EN);
879 /* Report alarm 0 time assuming 24-hour and day-of-month modes. */
880 t->time.tm_sec = bcd2bin(regs[3] & 0x7f);
881 t->time.tm_min = bcd2bin(regs[4] & 0x7f);
882 t->time.tm_hour = bcd2bin(regs[5] & 0x3f);
883 t->time.tm_wday = bcd2bin(regs[6] & 0x7) - 1;
884 t->time.tm_mday = bcd2bin(regs[7] & 0x3f);
885 t->time.tm_mon = bcd2bin(regs[8] & 0x1f) - 1;
886 t->time.tm_year = -1;
887 t->time.tm_yday = -1;
888 t->time.tm_isdst = -1;
890 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
891 "enabled=%d polarity=%d irq=%d match=%lu\n", __func__,
892 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
893 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon, t->enabled,
894 !!(regs[6] & MCP794XX_BIT_ALMX_POL),
895 !!(regs[6] & MCP794XX_BIT_ALMX_IF),
896 (regs[6] & MCP794XX_MSK_ALMX_MATCH) >> 4);
902 * We may have a random RTC weekday, therefore calculate alarm weekday based
903 * on current weekday we read from the RTC timekeeping regs
905 static int mcp794xx_alm_weekday(struct device *dev, struct rtc_time *tm_alarm)
907 struct rtc_time tm_now;
908 int days_now, days_alarm, ret;
910 ret = ds1307_get_time(dev, &tm_now);
914 days_now = div_s64(rtc_tm_to_time64(&tm_now), 24 * 60 * 60);
915 days_alarm = div_s64(rtc_tm_to_time64(tm_alarm), 24 * 60 * 60);
917 return (tm_now.tm_wday + days_alarm - days_now) % 7 + 1;
920 static int mcp794xx_set_alarm(struct device *dev, struct rtc_wkalrm *t)
922 struct ds1307 *ds1307 = dev_get_drvdata(dev);
923 unsigned char regs[10];
926 if (!test_bit(HAS_ALARM, &ds1307->flags))
929 wday = mcp794xx_alm_weekday(dev, &t->time);
933 dev_dbg(dev, "%s, sec=%d min=%d hour=%d wday=%d mday=%d mon=%d "
934 "enabled=%d pending=%d\n", __func__,
935 t->time.tm_sec, t->time.tm_min, t->time.tm_hour,
936 t->time.tm_wday, t->time.tm_mday, t->time.tm_mon,
937 t->enabled, t->pending);
939 /* Read control and alarm 0 registers. */
940 ret = regmap_bulk_read(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
945 /* Set alarm 0, using 24-hour and day-of-month modes. */
946 regs[3] = bin2bcd(t->time.tm_sec);
947 regs[4] = bin2bcd(t->time.tm_min);
948 regs[5] = bin2bcd(t->time.tm_hour);
950 regs[7] = bin2bcd(t->time.tm_mday);
951 regs[8] = bin2bcd(t->time.tm_mon + 1);
953 /* Clear the alarm 0 interrupt flag. */
954 regs[6] &= ~MCP794XX_BIT_ALMX_IF;
955 /* Set alarm match: second, minute, hour, day, date, month. */
956 regs[6] |= MCP794XX_MSK_ALMX_MATCH;
957 /* Disable interrupt. We will not enable until completely programmed */
958 regs[0] &= ~MCP794XX_BIT_ALM0_EN;
960 ret = regmap_bulk_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs,
967 regs[0] |= MCP794XX_BIT_ALM0_EN;
968 return regmap_write(ds1307->regmap, MCP794XX_REG_CONTROL, regs[0]);
971 static int mcp794xx_alarm_irq_enable(struct device *dev, unsigned int enabled)
973 struct ds1307 *ds1307 = dev_get_drvdata(dev);
975 if (!test_bit(HAS_ALARM, &ds1307->flags))
978 return regmap_update_bits(ds1307->regmap, MCP794XX_REG_CONTROL,
979 MCP794XX_BIT_ALM0_EN,
980 enabled ? MCP794XX_BIT_ALM0_EN : 0);
983 /*----------------------------------------------------------------------*/
985 static int ds1307_nvram_read(void *priv, unsigned int offset, void *val,
988 struct ds1307 *ds1307 = priv;
989 const struct chip_desc *chip = &chips[ds1307->type];
991 return regmap_bulk_read(ds1307->regmap, chip->nvram_offset + offset,
995 static int ds1307_nvram_write(void *priv, unsigned int offset, void *val,
998 struct ds1307 *ds1307 = priv;
999 const struct chip_desc *chip = &chips[ds1307->type];
1001 return regmap_bulk_write(ds1307->regmap, chip->nvram_offset + offset,
1005 /*----------------------------------------------------------------------*/
1007 static u8 do_trickle_setup_ds1339(struct ds1307 *ds1307,
1008 u32 ohms, bool diode)
1010 u8 setup = (diode) ? DS1307_TRICKLE_CHARGER_DIODE :
1011 DS1307_TRICKLE_CHARGER_NO_DIODE;
1015 setup |= DS1307_TRICKLE_CHARGER_250_OHM;
1018 setup |= DS1307_TRICKLE_CHARGER_2K_OHM;
1021 setup |= DS1307_TRICKLE_CHARGER_4K_OHM;
1024 dev_warn(ds1307->dev,
1025 "Unsupported ohm value %u in dt\n", ohms);
1031 static u8 ds1307_trickle_init(struct ds1307 *ds1307,
1032 const struct chip_desc *chip)
1037 if (!chip->do_trickle_setup)
1040 if (device_property_read_u32(ds1307->dev, "trickle-resistor-ohms",
1044 if (device_property_read_bool(ds1307->dev, "trickle-diode-disable"))
1047 return chip->do_trickle_setup(ds1307, ohms, diode);
1050 /*----------------------------------------------------------------------*/
1052 #if IS_REACHABLE(CONFIG_HWMON)
1055 * Temperature sensor support for ds3231 devices.
1058 #define DS3231_REG_TEMPERATURE 0x11
1061 * A user-initiated temperature conversion is not started by this function,
1062 * so the temperature is updated once every 64 seconds.
1064 static int ds3231_hwmon_read_temp(struct device *dev, s32 *mC)
1066 struct ds1307 *ds1307 = dev_get_drvdata(dev);
1071 ret = regmap_bulk_read(ds1307->regmap, DS3231_REG_TEMPERATURE,
1072 temp_buf, sizeof(temp_buf));
1076 * Temperature is represented as a 10-bit code with a resolution of
1077 * 0.25 degree celsius and encoded in two's complement format.
1079 temp = (temp_buf[0] << 8) | temp_buf[1];
1086 static ssize_t ds3231_hwmon_show_temp(struct device *dev,
1087 struct device_attribute *attr, char *buf)
1092 ret = ds3231_hwmon_read_temp(dev, &temp);
1096 return sprintf(buf, "%d\n", temp);
1098 static SENSOR_DEVICE_ATTR(temp1_input, 0444, ds3231_hwmon_show_temp,
1101 static struct attribute *ds3231_hwmon_attrs[] = {
1102 &sensor_dev_attr_temp1_input.dev_attr.attr,
1105 ATTRIBUTE_GROUPS(ds3231_hwmon);
1107 static void ds1307_hwmon_register(struct ds1307 *ds1307)
1111 if (ds1307->type != ds_3231)
1114 dev = devm_hwmon_device_register_with_groups(ds1307->dev, ds1307->name,
1116 ds3231_hwmon_groups);
1118 dev_warn(ds1307->dev, "unable to register hwmon device %ld\n",
1125 static void ds1307_hwmon_register(struct ds1307 *ds1307)
1129 #endif /* CONFIG_RTC_DRV_DS1307_HWMON */
1131 /*----------------------------------------------------------------------*/
1134 * Square-wave output support for DS3231
1135 * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
1137 #ifdef CONFIG_COMMON_CLK
1144 #define clk_sqw_to_ds1307(clk) \
1145 container_of(clk, struct ds1307, clks[DS3231_CLK_SQW])
1146 #define clk_32khz_to_ds1307(clk) \
1147 container_of(clk, struct ds1307, clks[DS3231_CLK_32KHZ])
1149 static int ds3231_clk_sqw_rates[] = {
1156 static int ds1337_write_control(struct ds1307 *ds1307, u8 mask, u8 value)
1158 struct mutex *lock = &ds1307->rtc->ops_lock;
1162 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_CONTROL,
1169 static unsigned long ds3231_clk_sqw_recalc_rate(struct clk_hw *hw,
1170 unsigned long parent_rate)
1172 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1176 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1179 if (control & DS1337_BIT_RS1)
1181 if (control & DS1337_BIT_RS2)
1184 return ds3231_clk_sqw_rates[rate_sel];
1187 static long ds3231_clk_sqw_round_rate(struct clk_hw *hw, unsigned long rate,
1188 unsigned long *prate)
1192 for (i = ARRAY_SIZE(ds3231_clk_sqw_rates) - 1; i >= 0; i--) {
1193 if (ds3231_clk_sqw_rates[i] <= rate)
1194 return ds3231_clk_sqw_rates[i];
1200 static int ds3231_clk_sqw_set_rate(struct clk_hw *hw, unsigned long rate,
1201 unsigned long parent_rate)
1203 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1207 for (rate_sel = 0; rate_sel < ARRAY_SIZE(ds3231_clk_sqw_rates);
1209 if (ds3231_clk_sqw_rates[rate_sel] == rate)
1213 if (rate_sel == ARRAY_SIZE(ds3231_clk_sqw_rates))
1217 control |= DS1337_BIT_RS1;
1219 control |= DS1337_BIT_RS2;
1221 return ds1337_write_control(ds1307, DS1337_BIT_RS1 | DS1337_BIT_RS2,
1225 static int ds3231_clk_sqw_prepare(struct clk_hw *hw)
1227 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1229 return ds1337_write_control(ds1307, DS1337_BIT_INTCN, 0);
1232 static void ds3231_clk_sqw_unprepare(struct clk_hw *hw)
1234 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1236 ds1337_write_control(ds1307, DS1337_BIT_INTCN, DS1337_BIT_INTCN);
1239 static int ds3231_clk_sqw_is_prepared(struct clk_hw *hw)
1241 struct ds1307 *ds1307 = clk_sqw_to_ds1307(hw);
1244 ret = regmap_read(ds1307->regmap, DS1337_REG_CONTROL, &control);
1248 return !(control & DS1337_BIT_INTCN);
1251 static const struct clk_ops ds3231_clk_sqw_ops = {
1252 .prepare = ds3231_clk_sqw_prepare,
1253 .unprepare = ds3231_clk_sqw_unprepare,
1254 .is_prepared = ds3231_clk_sqw_is_prepared,
1255 .recalc_rate = ds3231_clk_sqw_recalc_rate,
1256 .round_rate = ds3231_clk_sqw_round_rate,
1257 .set_rate = ds3231_clk_sqw_set_rate,
1260 static unsigned long ds3231_clk_32khz_recalc_rate(struct clk_hw *hw,
1261 unsigned long parent_rate)
1266 static int ds3231_clk_32khz_control(struct ds1307 *ds1307, bool enable)
1268 struct mutex *lock = &ds1307->rtc->ops_lock;
1272 ret = regmap_update_bits(ds1307->regmap, DS1337_REG_STATUS,
1274 enable ? DS3231_BIT_EN32KHZ : 0);
1280 static int ds3231_clk_32khz_prepare(struct clk_hw *hw)
1282 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1284 return ds3231_clk_32khz_control(ds1307, true);
1287 static void ds3231_clk_32khz_unprepare(struct clk_hw *hw)
1289 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1291 ds3231_clk_32khz_control(ds1307, false);
1294 static int ds3231_clk_32khz_is_prepared(struct clk_hw *hw)
1296 struct ds1307 *ds1307 = clk_32khz_to_ds1307(hw);
1299 ret = regmap_read(ds1307->regmap, DS1337_REG_STATUS, &status);
1303 return !!(status & DS3231_BIT_EN32KHZ);
1306 static const struct clk_ops ds3231_clk_32khz_ops = {
1307 .prepare = ds3231_clk_32khz_prepare,
1308 .unprepare = ds3231_clk_32khz_unprepare,
1309 .is_prepared = ds3231_clk_32khz_is_prepared,
1310 .recalc_rate = ds3231_clk_32khz_recalc_rate,
1313 static struct clk_init_data ds3231_clks_init[] = {
1314 [DS3231_CLK_SQW] = {
1315 .name = "ds3231_clk_sqw",
1316 .ops = &ds3231_clk_sqw_ops,
1318 [DS3231_CLK_32KHZ] = {
1319 .name = "ds3231_clk_32khz",
1320 .ops = &ds3231_clk_32khz_ops,
1324 static int ds3231_clks_register(struct ds1307 *ds1307)
1326 struct device_node *node = ds1307->dev->of_node;
1327 struct clk_onecell_data *onecell;
1330 onecell = devm_kzalloc(ds1307->dev, sizeof(*onecell), GFP_KERNEL);
1334 onecell->clk_num = ARRAY_SIZE(ds3231_clks_init);
1335 onecell->clks = devm_kcalloc(ds1307->dev, onecell->clk_num,
1336 sizeof(onecell->clks[0]), GFP_KERNEL);
1340 for (i = 0; i < ARRAY_SIZE(ds3231_clks_init); i++) {
1341 struct clk_init_data init = ds3231_clks_init[i];
1344 * Interrupt signal due to alarm conditions and square-wave
1345 * output share same pin, so don't initialize both.
1347 if (i == DS3231_CLK_SQW && test_bit(HAS_ALARM, &ds1307->flags))
1350 /* optional override of the clockname */
1351 of_property_read_string_index(node, "clock-output-names", i,
1353 ds1307->clks[i].init = &init;
1355 onecell->clks[i] = devm_clk_register(ds1307->dev,
1357 if (IS_ERR(onecell->clks[i]))
1358 return PTR_ERR(onecell->clks[i]);
1364 of_clk_add_provider(node, of_clk_src_onecell_get, onecell);
1369 static void ds1307_clks_register(struct ds1307 *ds1307)
1373 if (ds1307->type != ds_3231)
1376 ret = ds3231_clks_register(ds1307);
1378 dev_warn(ds1307->dev, "unable to register clock device %d\n",
1385 static void ds1307_clks_register(struct ds1307 *ds1307)
1389 #endif /* CONFIG_COMMON_CLK */
1391 static const struct regmap_config regmap_config = {
1396 static int ds1307_probe(struct i2c_client *client,
1397 const struct i2c_device_id *id)
1399 struct ds1307 *ds1307;
1402 const struct chip_desc *chip;
1404 bool ds1307_can_wakeup_device = false;
1405 unsigned char regs[8];
1406 struct ds1307_platform_data *pdata = dev_get_platdata(&client->dev);
1407 u8 trickle_charger_setup = 0;
1409 ds1307 = devm_kzalloc(&client->dev, sizeof(struct ds1307), GFP_KERNEL);
1413 dev_set_drvdata(&client->dev, ds1307);
1414 ds1307->dev = &client->dev;
1415 ds1307->name = client->name;
1417 ds1307->regmap = devm_regmap_init_i2c(client, ®map_config);
1418 if (IS_ERR(ds1307->regmap)) {
1419 dev_err(ds1307->dev, "regmap allocation failed\n");
1420 return PTR_ERR(ds1307->regmap);
1423 i2c_set_clientdata(client, ds1307);
1425 if (client->dev.of_node) {
1426 ds1307->type = (enum ds_type)
1427 of_device_get_match_data(&client->dev);
1428 chip = &chips[ds1307->type];
1430 chip = &chips[id->driver_data];
1431 ds1307->type = id->driver_data;
1433 const struct acpi_device_id *acpi_id;
1435 acpi_id = acpi_match_device(ACPI_PTR(ds1307_acpi_ids),
1439 chip = &chips[acpi_id->driver_data];
1440 ds1307->type = acpi_id->driver_data;
1443 want_irq = client->irq > 0 && chip->alarm;
1446 trickle_charger_setup = ds1307_trickle_init(ds1307, chip);
1447 else if (pdata->trickle_charger_setup)
1448 trickle_charger_setup = pdata->trickle_charger_setup;
1450 if (trickle_charger_setup && chip->trickle_charger_reg) {
1451 trickle_charger_setup |= DS13XX_TRICKLE_CHARGER_MAGIC;
1452 dev_dbg(ds1307->dev,
1453 "writing trickle charger info 0x%x to 0x%x\n",
1454 trickle_charger_setup, chip->trickle_charger_reg);
1455 regmap_write(ds1307->regmap, chip->trickle_charger_reg,
1456 trickle_charger_setup);
1461 * For devices with no IRQ directly connected to the SoC, the RTC chip
1462 * can be forced as a wakeup source by stating that explicitly in
1463 * the device's .dts file using the "wakeup-source" boolean property.
1464 * If the "wakeup-source" property is set, don't request an IRQ.
1465 * This will guarantee the 'wakealarm' sysfs entry is available on the device,
1466 * if supported by the RTC.
1468 if (chip->alarm && of_property_read_bool(client->dev.of_node,
1470 ds1307_can_wakeup_device = true;
1473 switch (ds1307->type) {
1478 /* get registers that the "rtc" read below won't read... */
1479 err = regmap_bulk_read(ds1307->regmap, DS1337_REG_CONTROL,
1482 dev_dbg(ds1307->dev, "read error %d\n", err);
1486 /* oscillator off? turn it on, so clock can tick. */
1487 if (regs[0] & DS1337_BIT_nEOSC)
1488 regs[0] &= ~DS1337_BIT_nEOSC;
1491 * Using IRQ or defined as wakeup-source?
1492 * Disable the square wave and both alarms.
1493 * For some variants, be sure alarms can trigger when we're
1494 * running on Vbackup (BBSQI/BBSQW)
1496 if (want_irq || ds1307_can_wakeup_device) {
1497 regs[0] |= DS1337_BIT_INTCN | chip->bbsqi_bit;
1498 regs[0] &= ~(DS1337_BIT_A2IE | DS1337_BIT_A1IE);
1501 regmap_write(ds1307->regmap, DS1337_REG_CONTROL,
1504 /* oscillator fault? clear flag, and warn */
1505 if (regs[1] & DS1337_BIT_OSF) {
1506 regmap_write(ds1307->regmap, DS1337_REG_STATUS,
1507 regs[1] & ~DS1337_BIT_OSF);
1508 dev_warn(ds1307->dev, "SET TIME!\n");
1513 err = regmap_bulk_read(ds1307->regmap,
1514 RX8025_REG_CTRL1 << 4 | 0x08, regs, 2);
1516 dev_dbg(ds1307->dev, "read error %d\n", err);
1520 /* oscillator off? turn it on, so clock can tick. */
1521 if (!(regs[1] & RX8025_BIT_XST)) {
1522 regs[1] |= RX8025_BIT_XST;
1523 regmap_write(ds1307->regmap,
1524 RX8025_REG_CTRL2 << 4 | 0x08,
1526 dev_warn(ds1307->dev,
1527 "oscillator stop detected - SET TIME!\n");
1530 if (regs[1] & RX8025_BIT_PON) {
1531 regs[1] &= ~RX8025_BIT_PON;
1532 regmap_write(ds1307->regmap,
1533 RX8025_REG_CTRL2 << 4 | 0x08,
1535 dev_warn(ds1307->dev, "power-on detected\n");
1538 if (regs[1] & RX8025_BIT_VDET) {
1539 regs[1] &= ~RX8025_BIT_VDET;
1540 regmap_write(ds1307->regmap,
1541 RX8025_REG_CTRL2 << 4 | 0x08,
1543 dev_warn(ds1307->dev, "voltage drop detected\n");
1546 /* make sure we are running in 24hour mode */
1547 if (!(regs[0] & RX8025_BIT_2412)) {
1550 /* switch to 24 hour mode */
1551 regmap_write(ds1307->regmap,
1552 RX8025_REG_CTRL1 << 4 | 0x08,
1553 regs[0] | RX8025_BIT_2412);
1555 err = regmap_bulk_read(ds1307->regmap,
1556 RX8025_REG_CTRL1 << 4 | 0x08,
1559 dev_dbg(ds1307->dev, "read error %d\n", err);
1564 hour = bcd2bin(regs[DS1307_REG_HOUR]);
1567 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1570 regmap_write(ds1307->regmap,
1571 DS1307_REG_HOUR << 4 | 0x08, hour);
1579 /* read RTC registers */
1580 err = regmap_bulk_read(ds1307->regmap, chip->offset, regs,
1583 dev_dbg(ds1307->dev, "read error %d\n", err);
1588 * minimal sanity checking; some chips (like DS1340) don't
1589 * specify the extra bits as must-be-zero, but there are
1590 * still a few values that are clearly out-of-range.
1592 tmp = regs[DS1307_REG_SECS];
1593 switch (ds1307->type) {
1598 /* clock halted? turn it on, so clock can tick. */
1599 if (tmp & DS1307_BIT_CH) {
1600 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1601 dev_warn(ds1307->dev, "SET TIME!\n");
1607 /* clock halted? turn it on, so clock can tick. */
1608 if (tmp & DS1307_BIT_CH)
1609 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1611 /* oscillator fault? clear flag, and warn */
1612 if (regs[DS1307_REG_CONTROL] & DS1338_BIT_OSF) {
1613 regmap_write(ds1307->regmap, DS1307_REG_CONTROL,
1614 regs[DS1307_REG_CONTROL] &
1616 dev_warn(ds1307->dev, "SET TIME!\n");
1621 /* clock halted? turn it on, so clock can tick. */
1622 if (tmp & DS1340_BIT_nEOSC)
1623 regmap_write(ds1307->regmap, DS1307_REG_SECS, 0);
1625 err = regmap_read(ds1307->regmap, DS1340_REG_FLAG, &tmp);
1627 dev_dbg(ds1307->dev, "read error %d\n", err);
1631 /* oscillator fault? clear flag, and warn */
1632 if (tmp & DS1340_BIT_OSF) {
1633 regmap_write(ds1307->regmap, DS1340_REG_FLAG, 0);
1634 dev_warn(ds1307->dev, "SET TIME!\n");
1638 /* make sure that the backup battery is enabled */
1639 if (!(regs[DS1307_REG_WDAY] & MCP794XX_BIT_VBATEN)) {
1640 regmap_write(ds1307->regmap, DS1307_REG_WDAY,
1641 regs[DS1307_REG_WDAY] |
1642 MCP794XX_BIT_VBATEN);
1645 /* clock halted? turn it on, so clock can tick. */
1646 if (!(tmp & MCP794XX_BIT_ST)) {
1647 regmap_write(ds1307->regmap, DS1307_REG_SECS,
1649 dev_warn(ds1307->dev, "SET TIME!\n");
1658 tmp = regs[DS1307_REG_HOUR];
1659 switch (ds1307->type) {
1665 * NOTE: ignores century bits; fix before deploying
1666 * systems that will run through year 2100.
1672 if (!(tmp & DS1307_BIT_12HR))
1676 * Be sure we're in 24 hour mode. Multi-master systems
1679 tmp = bcd2bin(tmp & 0x1f);
1682 if (regs[DS1307_REG_HOUR] & DS1307_BIT_PM)
1684 regmap_write(ds1307->regmap, chip->offset + DS1307_REG_HOUR,
1688 if (want_irq || ds1307_can_wakeup_device) {
1689 device_set_wakeup_capable(ds1307->dev, true);
1690 set_bit(HAS_ALARM, &ds1307->flags);
1693 ds1307->rtc = devm_rtc_allocate_device(ds1307->dev);
1694 if (IS_ERR(ds1307->rtc))
1695 return PTR_ERR(ds1307->rtc);
1697 if (ds1307_can_wakeup_device && !want_irq) {
1698 dev_info(ds1307->dev,
1699 "'wakeup-source' is set, request for an IRQ is disabled!\n");
1700 /* We cannot support UIE mode if we do not have an IRQ line */
1701 ds1307->rtc->uie_unsupported = 1;
1705 err = devm_request_threaded_irq(ds1307->dev, client->irq, NULL,
1706 chip->irq_handler ?: ds1307_irq,
1707 IRQF_SHARED | IRQF_ONESHOT,
1708 ds1307->name, ds1307);
1711 device_set_wakeup_capable(ds1307->dev, false);
1712 clear_bit(HAS_ALARM, &ds1307->flags);
1713 dev_err(ds1307->dev, "unable to request IRQ!\n");
1715 dev_dbg(ds1307->dev, "got IRQ %d\n", client->irq);
1719 ds1307->rtc->ops = chip->rtc_ops ?: &ds13xx_rtc_ops;
1720 err = rtc_register_device(ds1307->rtc);
1724 if (chip->nvram_size) {
1725 struct nvmem_config nvmem_cfg = {
1726 .name = "ds1307_nvram",
1729 .size = chip->nvram_size,
1730 .reg_read = ds1307_nvram_read,
1731 .reg_write = ds1307_nvram_write,
1735 ds1307->rtc->nvram_old_abi = true;
1736 rtc_nvmem_register(ds1307->rtc, &nvmem_cfg);
1739 ds1307_hwmon_register(ds1307);
1740 ds1307_clks_register(ds1307);
1748 static struct i2c_driver ds1307_driver = {
1750 .name = "rtc-ds1307",
1751 .of_match_table = of_match_ptr(ds1307_of_match),
1752 .acpi_match_table = ACPI_PTR(ds1307_acpi_ids),
1754 .probe = ds1307_probe,
1755 .id_table = ds1307_id,
1758 module_i2c_driver(ds1307_driver);
1760 MODULE_DESCRIPTION("RTC driver for DS1307 and similar chips");
1761 MODULE_LICENSE("GPL");