1 // SPDX-License-Identifier: GPL-2.0
3 * RTC driver for the Micro Crystal RV3028
5 * Copyright (C) 2019 Micro Crystal SA
7 * Alexandre Belloni <alexandre.belloni@bootlin.com>
11 #include <linux/clk-provider.h>
12 #include <linux/bcd.h>
13 #include <linux/bitops.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/log2.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/regmap.h>
21 #include <linux/rtc.h>
23 #define RV3028_SEC 0x00
24 #define RV3028_MIN 0x01
25 #define RV3028_HOUR 0x02
26 #define RV3028_WDAY 0x03
27 #define RV3028_DAY 0x04
28 #define RV3028_MONTH 0x05
29 #define RV3028_YEAR 0x06
30 #define RV3028_ALARM_MIN 0x07
31 #define RV3028_ALARM_HOUR 0x08
32 #define RV3028_ALARM_DAY 0x09
33 #define RV3028_STATUS 0x0E
34 #define RV3028_CTRL1 0x0F
35 #define RV3028_CTRL2 0x10
36 #define RV3028_EVT_CTRL 0x13
37 #define RV3028_TS_COUNT 0x14
38 #define RV3028_TS_SEC 0x15
39 #define RV3028_RAM1 0x1F
40 #define RV3028_EEPROM_ADDR 0x25
41 #define RV3028_EEPROM_DATA 0x26
42 #define RV3028_EEPROM_CMD 0x27
43 #define RV3028_CLKOUT 0x35
44 #define RV3028_OFFSET 0x36
45 #define RV3028_BACKUP 0x37
47 #define RV3028_STATUS_PORF BIT(0)
48 #define RV3028_STATUS_EVF BIT(1)
49 #define RV3028_STATUS_AF BIT(2)
50 #define RV3028_STATUS_TF BIT(3)
51 #define RV3028_STATUS_UF BIT(4)
52 #define RV3028_STATUS_BSF BIT(5)
53 #define RV3028_STATUS_CLKF BIT(6)
54 #define RV3028_STATUS_EEBUSY BIT(7)
56 #define RV3028_CLKOUT_FD_MASK GENMASK(2, 0)
57 #define RV3028_CLKOUT_PORIE BIT(3)
58 #define RV3028_CLKOUT_CLKSY BIT(6)
59 #define RV3028_CLKOUT_CLKOE BIT(7)
61 #define RV3028_CTRL1_EERD BIT(3)
62 #define RV3028_CTRL1_WADA BIT(5)
64 #define RV3028_CTRL2_RESET BIT(0)
65 #define RV3028_CTRL2_12_24 BIT(1)
66 #define RV3028_CTRL2_EIE BIT(2)
67 #define RV3028_CTRL2_AIE BIT(3)
68 #define RV3028_CTRL2_TIE BIT(4)
69 #define RV3028_CTRL2_UIE BIT(5)
70 #define RV3028_CTRL2_TSE BIT(7)
72 #define RV3028_EVT_CTRL_TSR BIT(2)
74 #define RV3028_EEPROM_CMD_WRITE 0x21
75 #define RV3028_EEPROM_CMD_READ 0x22
77 #define RV3028_EEBUSY_POLL 10000
78 #define RV3028_EEBUSY_TIMEOUT 100000
80 #define RV3028_BACKUP_TCE BIT(5)
81 #define RV3028_BACKUP_TCR_MASK GENMASK(1,0)
83 #define OFFSET_STEP_PPT 953674
90 struct regmap
*regmap
;
91 struct rtc_device
*rtc
;
92 enum rv3028_type type
;
93 #ifdef CONFIG_COMMON_CLK
94 struct clk_hw clkout_hw
;
98 static u16 rv3028_trickle_resistors
[] = {1000, 3000, 6000, 11000};
100 static ssize_t
timestamp0_store(struct device
*dev
,
101 struct device_attribute
*attr
,
102 const char *buf
, size_t count
)
104 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
->parent
);
106 regmap_update_bits(rv3028
->regmap
, RV3028_EVT_CTRL
, RV3028_EVT_CTRL_TSR
,
107 RV3028_EVT_CTRL_TSR
);
112 static ssize_t
timestamp0_show(struct device
*dev
,
113 struct device_attribute
*attr
, char *buf
)
115 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
->parent
);
120 ret
= regmap_read(rv3028
->regmap
, RV3028_TS_COUNT
, &count
);
127 ret
= regmap_bulk_read(rv3028
->regmap
, RV3028_TS_SEC
, date
,
132 tm
.tm_sec
= bcd2bin(date
[0]);
133 tm
.tm_min
= bcd2bin(date
[1]);
134 tm
.tm_hour
= bcd2bin(date
[2]);
135 tm
.tm_mday
= bcd2bin(date
[3]);
136 tm
.tm_mon
= bcd2bin(date
[4]) - 1;
137 tm
.tm_year
= bcd2bin(date
[5]) + 100;
139 ret
= rtc_valid_tm(&tm
);
143 return sprintf(buf
, "%llu\n",
144 (unsigned long long)rtc_tm_to_time64(&tm
));
147 static DEVICE_ATTR_RW(timestamp0
);
149 static ssize_t
timestamp0_count_show(struct device
*dev
,
150 struct device_attribute
*attr
, char *buf
)
152 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
->parent
);
155 ret
= regmap_read(rv3028
->regmap
, RV3028_TS_COUNT
, &count
);
159 return sprintf(buf
, "%u\n", count
);
162 static DEVICE_ATTR_RO(timestamp0_count
);
164 static struct attribute
*rv3028_attrs
[] = {
165 &dev_attr_timestamp0
.attr
,
166 &dev_attr_timestamp0_count
.attr
,
170 static const struct attribute_group rv3028_attr_group
= {
171 .attrs
= rv3028_attrs
,
174 static irqreturn_t
rv3028_handle_irq(int irq
, void *dev_id
)
176 struct rv3028_data
*rv3028
= dev_id
;
177 unsigned long events
= 0;
178 u32 status
= 0, ctrl
= 0;
180 if (regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
) < 0 ||
185 if (status
& RV3028_STATUS_PORF
)
186 dev_warn(&rv3028
->rtc
->dev
, "Voltage low, data loss detected.\n");
188 if (status
& RV3028_STATUS_TF
) {
189 status
|= RV3028_STATUS_TF
;
190 ctrl
|= RV3028_CTRL2_TIE
;
194 if (status
& RV3028_STATUS_AF
) {
195 status
|= RV3028_STATUS_AF
;
196 ctrl
|= RV3028_CTRL2_AIE
;
200 if (status
& RV3028_STATUS_UF
) {
201 status
|= RV3028_STATUS_UF
;
202 ctrl
|= RV3028_CTRL2_UIE
;
207 rtc_update_irq(rv3028
->rtc
, 1, events
);
208 regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
, status
, 0);
209 regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
, ctrl
, 0);
212 if (status
& RV3028_STATUS_EVF
) {
213 sysfs_notify(&rv3028
->rtc
->dev
.kobj
, NULL
,
214 dev_attr_timestamp0
.attr
.name
);
215 dev_warn(&rv3028
->rtc
->dev
, "event detected");
221 static int rv3028_get_time(struct device
*dev
, struct rtc_time
*tm
)
223 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
227 ret
= regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
);
231 if (status
& RV3028_STATUS_PORF
) {
232 dev_warn(dev
, "Voltage low, data is invalid.\n");
236 ret
= regmap_bulk_read(rv3028
->regmap
, RV3028_SEC
, date
, sizeof(date
));
240 tm
->tm_sec
= bcd2bin(date
[RV3028_SEC
] & 0x7f);
241 tm
->tm_min
= bcd2bin(date
[RV3028_MIN
] & 0x7f);
242 tm
->tm_hour
= bcd2bin(date
[RV3028_HOUR
] & 0x3f);
243 tm
->tm_wday
= ilog2(date
[RV3028_WDAY
] & 0x7f);
244 tm
->tm_mday
= bcd2bin(date
[RV3028_DAY
] & 0x3f);
245 tm
->tm_mon
= bcd2bin(date
[RV3028_MONTH
] & 0x1f) - 1;
246 tm
->tm_year
= bcd2bin(date
[RV3028_YEAR
]) + 100;
251 static int rv3028_set_time(struct device
*dev
, struct rtc_time
*tm
)
253 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
257 date
[RV3028_SEC
] = bin2bcd(tm
->tm_sec
);
258 date
[RV3028_MIN
] = bin2bcd(tm
->tm_min
);
259 date
[RV3028_HOUR
] = bin2bcd(tm
->tm_hour
);
260 date
[RV3028_WDAY
] = 1 << (tm
->tm_wday
);
261 date
[RV3028_DAY
] = bin2bcd(tm
->tm_mday
);
262 date
[RV3028_MONTH
] = bin2bcd(tm
->tm_mon
+ 1);
263 date
[RV3028_YEAR
] = bin2bcd(tm
->tm_year
- 100);
266 * Writing to the Seconds register has the same effect as setting RESET
269 ret
= regmap_bulk_write(rv3028
->regmap
, RV3028_SEC
, date
,
274 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
275 RV3028_STATUS_PORF
, 0);
280 static int rv3028_get_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
282 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
284 int status
, ctrl
, ret
;
286 ret
= regmap_bulk_read(rv3028
->regmap
, RV3028_ALARM_MIN
, alarmvals
,
291 ret
= regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
);
295 ret
= regmap_read(rv3028
->regmap
, RV3028_CTRL2
, &ctrl
);
299 alrm
->time
.tm_sec
= 0;
300 alrm
->time
.tm_min
= bcd2bin(alarmvals
[0] & 0x7f);
301 alrm
->time
.tm_hour
= bcd2bin(alarmvals
[1] & 0x3f);
302 alrm
->time
.tm_mday
= bcd2bin(alarmvals
[2] & 0x3f);
304 alrm
->enabled
= !!(ctrl
& RV3028_CTRL2_AIE
);
305 alrm
->pending
= (status
& RV3028_STATUS_AF
) && alrm
->enabled
;
310 static int rv3028_set_alarm(struct device
*dev
, struct rtc_wkalrm
*alrm
)
312 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
317 /* The alarm has no seconds, round up to nearest minute */
318 if (alrm
->time
.tm_sec
) {
319 time64_t alarm_time
= rtc_tm_to_time64(&alrm
->time
);
321 alarm_time
+= 60 - alrm
->time
.tm_sec
;
322 rtc_time64_to_tm(alarm_time
, &alrm
->time
);
325 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
,
326 RV3028_CTRL2_AIE
| RV3028_CTRL2_UIE
, 0);
330 alarmvals
[0] = bin2bcd(alrm
->time
.tm_min
);
331 alarmvals
[1] = bin2bcd(alrm
->time
.tm_hour
);
332 alarmvals
[2] = bin2bcd(alrm
->time
.tm_mday
);
334 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
335 RV3028_STATUS_AF
, 0);
339 ret
= regmap_bulk_write(rv3028
->regmap
, RV3028_ALARM_MIN
, alarmvals
,
345 if (rv3028
->rtc
->uie_rtctimer
.enabled
)
346 ctrl
|= RV3028_CTRL2_UIE
;
347 if (rv3028
->rtc
->aie_timer
.enabled
)
348 ctrl
|= RV3028_CTRL2_AIE
;
351 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
,
352 RV3028_CTRL2_UIE
| RV3028_CTRL2_AIE
, ctrl
);
357 static int rv3028_alarm_irq_enable(struct device
*dev
, unsigned int enabled
)
359 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
363 if (rv3028
->rtc
->uie_rtctimer
.enabled
)
364 ctrl
|= RV3028_CTRL2_UIE
;
365 if (rv3028
->rtc
->aie_timer
.enabled
)
366 ctrl
|= RV3028_CTRL2_AIE
;
369 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
370 RV3028_STATUS_AF
| RV3028_STATUS_UF
, 0);
374 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
,
375 RV3028_CTRL2_UIE
| RV3028_CTRL2_AIE
, ctrl
);
382 static int rv3028_read_offset(struct device
*dev
, long *offset
)
384 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
385 int ret
, value
, steps
;
387 ret
= regmap_read(rv3028
->regmap
, RV3028_OFFSET
, &value
);
391 steps
= sign_extend32(value
<< 1, 8);
393 ret
= regmap_read(rv3028
->regmap
, RV3028_BACKUP
, &value
);
399 *offset
= DIV_ROUND_CLOSEST(steps
* OFFSET_STEP_PPT
, 1000);
404 static int rv3028_set_offset(struct device
*dev
, long offset
)
406 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
409 offset
= clamp(offset
, -244141L, 243187L) * 1000;
410 offset
= DIV_ROUND_CLOSEST(offset
, OFFSET_STEP_PPT
);
412 ret
= regmap_write(rv3028
->regmap
, RV3028_OFFSET
, offset
>> 1);
416 return regmap_update_bits(rv3028
->regmap
, RV3028_BACKUP
, BIT(7),
420 static int rv3028_ioctl(struct device
*dev
, unsigned int cmd
, unsigned long arg
)
422 struct rv3028_data
*rv3028
= dev_get_drvdata(dev
);
427 ret
= regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
);
431 if (status
& RV3028_STATUS_PORF
)
432 dev_warn(&rv3028
->rtc
->dev
, "Voltage low, data loss detected.\n");
434 status
&= RV3028_STATUS_PORF
;
436 if (copy_to_user((void __user
*)arg
, &status
, sizeof(int)))
442 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
443 RV3028_STATUS_PORF
, 0);
452 static int rv3028_nvram_write(void *priv
, unsigned int offset
, void *val
,
455 return regmap_bulk_write(priv
, RV3028_RAM1
+ offset
, val
, bytes
);
458 static int rv3028_nvram_read(void *priv
, unsigned int offset
, void *val
,
461 return regmap_bulk_read(priv
, RV3028_RAM1
+ offset
, val
, bytes
);
464 static int rv3028_eeprom_write(void *priv
, unsigned int offset
, void *val
,
471 ret
= regmap_read(priv
, RV3028_CTRL1
, &ctrl1
);
475 if (!(ctrl1
& RV3028_CTRL1_EERD
)) {
476 ret
= regmap_update_bits(priv
, RV3028_CTRL1
,
477 RV3028_CTRL1_EERD
, RV3028_CTRL1_EERD
);
481 ret
= regmap_read_poll_timeout(priv
, RV3028_STATUS
, status
,
482 !(status
& RV3028_STATUS_EEBUSY
),
484 RV3028_EEBUSY_TIMEOUT
);
489 for (i
= 0; i
< bytes
; i
++) {
490 ret
= regmap_write(priv
, RV3028_EEPROM_ADDR
, offset
+ i
);
494 ret
= regmap_write(priv
, RV3028_EEPROM_DATA
, buf
[i
]);
498 ret
= regmap_write(priv
, RV3028_EEPROM_CMD
, 0x0);
502 ret
= regmap_write(priv
, RV3028_EEPROM_CMD
,
503 RV3028_EEPROM_CMD_WRITE
);
507 usleep_range(RV3028_EEBUSY_POLL
, RV3028_EEBUSY_TIMEOUT
);
509 ret
= regmap_read_poll_timeout(priv
, RV3028_STATUS
, status
,
510 !(status
& RV3028_STATUS_EEBUSY
),
512 RV3028_EEBUSY_TIMEOUT
);
518 if (!(ctrl1
& RV3028_CTRL1_EERD
))
520 err
= regmap_update_bits(priv
, RV3028_CTRL1
, RV3028_CTRL1_EERD
,
529 static int rv3028_eeprom_read(void *priv
, unsigned int offset
, void *val
,
532 u32 status
, ctrl1
, data
;
536 ret
= regmap_read(priv
, RV3028_CTRL1
, &ctrl1
);
540 if (!(ctrl1
& RV3028_CTRL1_EERD
)) {
541 ret
= regmap_update_bits(priv
, RV3028_CTRL1
,
542 RV3028_CTRL1_EERD
, RV3028_CTRL1_EERD
);
546 ret
= regmap_read_poll_timeout(priv
, RV3028_STATUS
, status
,
547 !(status
& RV3028_STATUS_EEBUSY
),
549 RV3028_EEBUSY_TIMEOUT
);
554 for (i
= 0; i
< bytes
; i
++) {
555 ret
= regmap_write(priv
, RV3028_EEPROM_ADDR
, offset
+ i
);
559 ret
= regmap_write(priv
, RV3028_EEPROM_CMD
, 0x0);
563 ret
= regmap_write(priv
, RV3028_EEPROM_CMD
,
564 RV3028_EEPROM_CMD_READ
);
568 ret
= regmap_read_poll_timeout(priv
, RV3028_STATUS
, status
,
569 !(status
& RV3028_STATUS_EEBUSY
),
571 RV3028_EEBUSY_TIMEOUT
);
575 ret
= regmap_read(priv
, RV3028_EEPROM_DATA
, &data
);
582 if (!(ctrl1
& RV3028_CTRL1_EERD
))
584 err
= regmap_update_bits(priv
, RV3028_CTRL1
, RV3028_CTRL1_EERD
,
593 #ifdef CONFIG_COMMON_CLK
594 #define clkout_hw_to_rv3028(hw) container_of(hw, struct rv3028_data, clkout_hw)
596 static int clkout_rates
[] = {
605 static unsigned long rv3028_clkout_recalc_rate(struct clk_hw
*hw
,
606 unsigned long parent_rate
)
609 struct rv3028_data
*rv3028
= clkout_hw_to_rv3028(hw
);
611 ret
= regmap_read(rv3028
->regmap
, RV3028_CLKOUT
, &clkout
);
615 clkout
&= RV3028_CLKOUT_FD_MASK
;
616 return clkout_rates
[clkout
];
619 static long rv3028_clkout_round_rate(struct clk_hw
*hw
, unsigned long rate
,
620 unsigned long *prate
)
624 for (i
= 0; i
< ARRAY_SIZE(clkout_rates
); i
++)
625 if (clkout_rates
[i
] <= rate
)
626 return clkout_rates
[i
];
631 static int rv3028_clkout_set_rate(struct clk_hw
*hw
, unsigned long rate
,
632 unsigned long parent_rate
)
635 struct rv3028_data
*rv3028
= clkout_hw_to_rv3028(hw
);
637 ret
= regmap_write(rv3028
->regmap
, RV3028_CLKOUT
, 0x0);
641 for (i
= 0; i
< ARRAY_SIZE(clkout_rates
); i
++) {
642 if (clkout_rates
[i
] == rate
) {
643 ret
= regmap_update_bits(rv3028
->regmap
,
645 RV3028_CLKOUT_FD_MASK
, i
);
649 return regmap_write(rv3028
->regmap
, RV3028_CLKOUT
,
650 RV3028_CLKOUT_CLKSY
| RV3028_CLKOUT_CLKOE
);
657 static int rv3028_clkout_prepare(struct clk_hw
*hw
)
659 struct rv3028_data
*rv3028
= clkout_hw_to_rv3028(hw
);
661 return regmap_write(rv3028
->regmap
, RV3028_CLKOUT
,
662 RV3028_CLKOUT_CLKSY
| RV3028_CLKOUT_CLKOE
);
665 static void rv3028_clkout_unprepare(struct clk_hw
*hw
)
667 struct rv3028_data
*rv3028
= clkout_hw_to_rv3028(hw
);
669 regmap_write(rv3028
->regmap
, RV3028_CLKOUT
, 0x0);
670 regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
671 RV3028_STATUS_CLKF
, 0);
674 static int rv3028_clkout_is_prepared(struct clk_hw
*hw
)
677 struct rv3028_data
*rv3028
= clkout_hw_to_rv3028(hw
);
679 ret
= regmap_read(rv3028
->regmap
, RV3028_CLKOUT
, &clkout
);
683 return !!(clkout
& RV3028_CLKOUT_CLKOE
);
686 static const struct clk_ops rv3028_clkout_ops
= {
687 .prepare
= rv3028_clkout_prepare
,
688 .unprepare
= rv3028_clkout_unprepare
,
689 .is_prepared
= rv3028_clkout_is_prepared
,
690 .recalc_rate
= rv3028_clkout_recalc_rate
,
691 .round_rate
= rv3028_clkout_round_rate
,
692 .set_rate
= rv3028_clkout_set_rate
,
695 static int rv3028_clkout_register_clk(struct rv3028_data
*rv3028
,
696 struct i2c_client
*client
)
700 struct clk_init_data init
;
701 struct device_node
*node
= client
->dev
.of_node
;
703 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_STATUS
,
704 RV3028_STATUS_CLKF
, 0);
708 init
.name
= "rv3028-clkout";
709 init
.ops
= &rv3028_clkout_ops
;
711 init
.parent_names
= NULL
;
712 init
.num_parents
= 0;
713 rv3028
->clkout_hw
.init
= &init
;
715 /* optional override of the clockname */
716 of_property_read_string(node
, "clock-output-names", &init
.name
);
718 /* register the clock */
719 clk
= devm_clk_register(&client
->dev
, &rv3028
->clkout_hw
);
721 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
727 static struct rtc_class_ops rv3028_rtc_ops
= {
728 .read_time
= rv3028_get_time
,
729 .set_time
= rv3028_set_time
,
730 .read_offset
= rv3028_read_offset
,
731 .set_offset
= rv3028_set_offset
,
732 .ioctl
= rv3028_ioctl
,
735 static const struct regmap_config regmap_config
= {
738 .max_register
= 0x37,
741 static int rv3028_probe(struct i2c_client
*client
)
743 struct rv3028_data
*rv3028
;
746 struct nvmem_config nvmem_cfg
= {
747 .name
= "rv3028_nvram",
751 .type
= NVMEM_TYPE_BATTERY_BACKED
,
752 .reg_read
= rv3028_nvram_read
,
753 .reg_write
= rv3028_nvram_write
,
755 struct nvmem_config eeprom_cfg
= {
756 .name
= "rv3028_eeprom",
760 .type
= NVMEM_TYPE_EEPROM
,
761 .reg_read
= rv3028_eeprom_read
,
762 .reg_write
= rv3028_eeprom_write
,
765 rv3028
= devm_kzalloc(&client
->dev
, sizeof(struct rv3028_data
),
770 rv3028
->regmap
= devm_regmap_init_i2c(client
, ®map_config
);
772 i2c_set_clientdata(client
, rv3028
);
774 ret
= regmap_read(rv3028
->regmap
, RV3028_STATUS
, &status
);
778 if (status
& RV3028_STATUS_PORF
)
779 dev_warn(&client
->dev
, "Voltage low, data loss detected.\n");
781 if (status
& RV3028_STATUS_AF
)
782 dev_warn(&client
->dev
, "An alarm may have been missed.\n");
784 rv3028
->rtc
= devm_rtc_allocate_device(&client
->dev
);
785 if (IS_ERR(rv3028
->rtc
))
786 return PTR_ERR(rv3028
->rtc
);
788 if (client
->irq
> 0) {
789 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
790 NULL
, rv3028_handle_irq
,
791 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
794 dev_warn(&client
->dev
, "unable to request IRQ, alarms disabled\n");
797 rv3028_rtc_ops
.read_alarm
= rv3028_get_alarm
;
798 rv3028_rtc_ops
.set_alarm
= rv3028_set_alarm
;
799 rv3028_rtc_ops
.alarm_irq_enable
= rv3028_alarm_irq_enable
;
803 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL1
,
804 RV3028_CTRL1_WADA
, RV3028_CTRL1_WADA
);
808 /* setup timestamping */
809 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_CTRL2
,
810 RV3028_CTRL2_EIE
| RV3028_CTRL2_TSE
,
811 RV3028_CTRL2_EIE
| RV3028_CTRL2_TSE
);
815 /* setup trickle charger */
816 if (!device_property_read_u32(&client
->dev
, "trickle-resistor-ohms",
820 for (i
= 0; i
< ARRAY_SIZE(rv3028_trickle_resistors
); i
++)
821 if (ohms
== rv3028_trickle_resistors
[i
])
824 if (i
< ARRAY_SIZE(rv3028_trickle_resistors
)) {
825 ret
= regmap_update_bits(rv3028
->regmap
, RV3028_BACKUP
,
827 RV3028_BACKUP_TCR_MASK
,
828 RV3028_BACKUP_TCE
| i
);
832 dev_warn(&client
->dev
, "invalid trickle resistor value\n");
836 ret
= rtc_add_group(rv3028
->rtc
, &rv3028_attr_group
);
840 rv3028
->rtc
->range_min
= RTC_TIMESTAMP_BEGIN_2000
;
841 rv3028
->rtc
->range_max
= RTC_TIMESTAMP_END_2099
;
842 rv3028
->rtc
->ops
= &rv3028_rtc_ops
;
843 ret
= rtc_register_device(rv3028
->rtc
);
847 nvmem_cfg
.priv
= rv3028
->regmap
;
848 rtc_nvmem_register(rv3028
->rtc
, &nvmem_cfg
);
849 eeprom_cfg
.priv
= rv3028
->regmap
;
850 rtc_nvmem_register(rv3028
->rtc
, &eeprom_cfg
);
852 rv3028
->rtc
->max_user_freq
= 1;
854 #ifdef CONFIG_COMMON_CLK
855 rv3028_clkout_register_clk(rv3028
, client
);
860 static const struct of_device_id rv3028_of_match
[] = {
861 { .compatible
= "microcrystal,rv3028", },
864 MODULE_DEVICE_TABLE(of
, rv3028_of_match
);
866 static struct i2c_driver rv3028_driver
= {
868 .name
= "rtc-rv3028",
869 .of_match_table
= of_match_ptr(rv3028_of_match
),
871 .probe_new
= rv3028_probe
,
873 module_i2c_driver(rv3028_driver
);
875 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
876 MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver");
877 MODULE_LICENSE("GPL v2");