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/bitfield.h>
14 #include <linux/bitops.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/log2.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/regmap.h>
22 #include <linux/rtc.h>
24 #define RV3028_SEC 0x00
25 #define RV3028_MIN 0x01
26 #define RV3028_HOUR 0x02
27 #define RV3028_WDAY 0x03
28 #define RV3028_DAY 0x04
29 #define RV3028_MONTH 0x05
30 #define RV3028_YEAR 0x06
31 #define RV3028_ALARM_MIN 0x07
32 #define RV3028_ALARM_HOUR 0x08
33 #define RV3028_ALARM_DAY 0x09
34 #define RV3028_STATUS 0x0E
35 #define RV3028_CTRL1 0x0F
36 #define RV3028_CTRL2 0x10
37 #define RV3028_EVT_CTRL 0x13
38 #define RV3028_TS_COUNT 0x14
39 #define RV3028_TS_SEC 0x15
40 #define RV3028_RAM1 0x1F
41 #define RV3028_EEPROM_ADDR 0x25
42 #define RV3028_EEPROM_DATA 0x26
43 #define RV3028_EEPROM_CMD 0x27
44 #define RV3028_CLKOUT 0x35
45 #define RV3028_OFFSET 0x36
46 #define RV3028_BACKUP 0x37
48 #define RV3028_STATUS_PORF BIT(0)
49 #define RV3028_STATUS_EVF BIT(1)
50 #define RV3028_STATUS_AF BIT(2)
51 #define RV3028_STATUS_TF BIT(3)
52 #define RV3028_STATUS_UF BIT(4)
53 #define RV3028_STATUS_BSF BIT(5)
54 #define RV3028_STATUS_CLKF BIT(6)
55 #define RV3028_STATUS_EEBUSY BIT(7)
57 #define RV3028_CLKOUT_FD_MASK GENMASK(2, 0)
58 #define RV3028_CLKOUT_PORIE BIT(3)
59 #define RV3028_CLKOUT_CLKSY BIT(6)
60 #define RV3028_CLKOUT_CLKOE BIT(7)
62 #define RV3028_CTRL1_EERD BIT(3)
63 #define RV3028_CTRL1_WADA BIT(5)
65 #define RV3028_CTRL2_RESET BIT(0)
66 #define RV3028_CTRL2_12_24 BIT(1)
67 #define RV3028_CTRL2_EIE BIT(2)
68 #define RV3028_CTRL2_AIE BIT(3)
69 #define RV3028_CTRL2_TIE BIT(4)
70 #define RV3028_CTRL2_UIE BIT(5)
71 #define RV3028_CTRL2_TSE BIT(7)
73 #define RV3028_EVT_CTRL_TSR BIT(2)
75 #define RV3028_EEPROM_CMD_UPDATE 0x11
76 #define RV3028_EEPROM_CMD_WRITE 0x21
77 #define RV3028_EEPROM_CMD_READ 0x22
79 #define RV3028_EEBUSY_POLL 10000
80 #define RV3028_EEBUSY_TIMEOUT 100000
82 #define RV3028_BACKUP_TCE BIT(5)
83 #define RV3028_BACKUP_TCR_MASK GENMASK(1,0)
84 #define RV3028_BACKUP_BSM GENMASK(3,2)
86 #define RV3028_BACKUP_BSM_DSM 0x1
87 #define RV3028_BACKUP_BSM_LSM 0x3
89 #define OFFSET_STEP_PPT 953674
96 struct regmap *regmap;
97 struct rtc_device *rtc;
98 enum rv3028_type type;
99 #ifdef CONFIG_COMMON_CLK
100 struct clk_hw clkout_hw;
104 static u16 rv3028_trickle_resistors[] = {3000, 5000, 9000, 15000};
106 static ssize_t timestamp0_store(struct device *dev,
107 struct device_attribute *attr,
108 const char *buf, size_t count)
110 struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
112 regmap_update_bits(rv3028->regmap, RV3028_EVT_CTRL, RV3028_EVT_CTRL_TSR,
113 RV3028_EVT_CTRL_TSR);
118 static ssize_t timestamp0_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
121 struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
126 ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
133 ret = regmap_bulk_read(rv3028->regmap, RV3028_TS_SEC, date,
138 tm.tm_sec = bcd2bin(date[0]);
139 tm.tm_min = bcd2bin(date[1]);
140 tm.tm_hour = bcd2bin(date[2]);
141 tm.tm_mday = bcd2bin(date[3]);
142 tm.tm_mon = bcd2bin(date[4]) - 1;
143 tm.tm_year = bcd2bin(date[5]) + 100;
145 ret = rtc_valid_tm(&tm);
149 return sprintf(buf, "%llu\n",
150 (unsigned long long)rtc_tm_to_time64(&tm));
153 static DEVICE_ATTR_RW(timestamp0);
155 static ssize_t timestamp0_count_show(struct device *dev,
156 struct device_attribute *attr, char *buf)
158 struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
161 ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
165 return sprintf(buf, "%u\n", count);
168 static DEVICE_ATTR_RO(timestamp0_count);
170 static struct attribute *rv3028_attrs[] = {
171 &dev_attr_timestamp0.attr,
172 &dev_attr_timestamp0_count.attr,
176 static const struct attribute_group rv3028_attr_group = {
177 .attrs = rv3028_attrs,
180 static int rv3028_exit_eerd(struct rv3028_data *rv3028, u32 eerd)
185 return regmap_update_bits(rv3028->regmap, RV3028_CTRL1, RV3028_CTRL1_EERD, 0);
188 static int rv3028_enter_eerd(struct rv3028_data *rv3028, u32 *eerd)
193 ret = regmap_read(rv3028->regmap, RV3028_CTRL1, &ctrl1);
197 *eerd = ctrl1 & RV3028_CTRL1_EERD;
201 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
202 RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
206 ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
207 !(status & RV3028_STATUS_EEBUSY),
208 RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
210 rv3028_exit_eerd(rv3028, *eerd);
218 static int rv3028_update_eeprom(struct rv3028_data *rv3028, u32 eerd)
223 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0);
227 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, RV3028_EEPROM_CMD_UPDATE);
231 usleep_range(63000, RV3028_EEBUSY_TIMEOUT);
233 ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
234 !(status & RV3028_STATUS_EEBUSY),
235 RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
238 rv3028_exit_eerd(rv3028, eerd);
243 static int rv3028_update_cfg(struct rv3028_data *rv3028, unsigned int reg,
244 unsigned int mask, unsigned int val)
249 ret = rv3028_enter_eerd(rv3028, &eerd);
253 ret = regmap_update_bits(rv3028->regmap, reg, mask, val);
255 rv3028_exit_eerd(rv3028, eerd);
259 return rv3028_update_eeprom(rv3028, eerd);
262 static irqreturn_t rv3028_handle_irq(int irq, void *dev_id)
264 struct rv3028_data *rv3028 = dev_id;
265 unsigned long events = 0;
266 u32 status = 0, ctrl = 0;
268 if (regmap_read(rv3028->regmap, RV3028_STATUS, &status) < 0 ||
273 status &= ~RV3028_STATUS_PORF;
275 if (status & RV3028_STATUS_TF) {
276 status |= RV3028_STATUS_TF;
277 ctrl |= RV3028_CTRL2_TIE;
281 if (status & RV3028_STATUS_AF) {
282 status |= RV3028_STATUS_AF;
283 ctrl |= RV3028_CTRL2_AIE;
287 if (status & RV3028_STATUS_UF) {
288 status |= RV3028_STATUS_UF;
289 ctrl |= RV3028_CTRL2_UIE;
294 rtc_update_irq(rv3028->rtc, 1, events);
295 regmap_update_bits(rv3028->regmap, RV3028_STATUS, status, 0);
296 regmap_update_bits(rv3028->regmap, RV3028_CTRL2, ctrl, 0);
299 if (status & RV3028_STATUS_EVF) {
300 sysfs_notify(&rv3028->rtc->dev.kobj, NULL,
301 dev_attr_timestamp0.attr.name);
302 dev_warn(&rv3028->rtc->dev, "event detected");
308 static int rv3028_get_time(struct device *dev, struct rtc_time *tm)
310 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
314 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
318 if (status & RV3028_STATUS_PORF)
321 ret = regmap_bulk_read(rv3028->regmap, RV3028_SEC, date, sizeof(date));
325 tm->tm_sec = bcd2bin(date[RV3028_SEC] & 0x7f);
326 tm->tm_min = bcd2bin(date[RV3028_MIN] & 0x7f);
327 tm->tm_hour = bcd2bin(date[RV3028_HOUR] & 0x3f);
328 tm->tm_wday = date[RV3028_WDAY] & 0x7f;
329 tm->tm_mday = bcd2bin(date[RV3028_DAY] & 0x3f);
330 tm->tm_mon = bcd2bin(date[RV3028_MONTH] & 0x1f) - 1;
331 tm->tm_year = bcd2bin(date[RV3028_YEAR]) + 100;
336 static int rv3028_set_time(struct device *dev, struct rtc_time *tm)
338 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
342 date[RV3028_SEC] = bin2bcd(tm->tm_sec);
343 date[RV3028_MIN] = bin2bcd(tm->tm_min);
344 date[RV3028_HOUR] = bin2bcd(tm->tm_hour);
345 date[RV3028_WDAY] = tm->tm_wday;
346 date[RV3028_DAY] = bin2bcd(tm->tm_mday);
347 date[RV3028_MONTH] = bin2bcd(tm->tm_mon + 1);
348 date[RV3028_YEAR] = bin2bcd(tm->tm_year - 100);
351 * Writing to the Seconds register has the same effect as setting RESET
354 ret = regmap_bulk_write(rv3028->regmap, RV3028_SEC, date,
359 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
360 RV3028_STATUS_PORF, 0);
365 static int rv3028_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
367 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
369 int status, ctrl, ret;
371 ret = regmap_bulk_read(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
376 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
380 ret = regmap_read(rv3028->regmap, RV3028_CTRL2, &ctrl);
384 alrm->time.tm_sec = 0;
385 alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
386 alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
387 alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
389 alrm->enabled = !!(ctrl & RV3028_CTRL2_AIE);
390 alrm->pending = (status & RV3028_STATUS_AF) && alrm->enabled;
395 static int rv3028_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
397 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
402 /* The alarm has no seconds, round up to nearest minute */
403 if (alrm->time.tm_sec) {
404 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
406 alarm_time += 60 - alrm->time.tm_sec;
407 rtc_time64_to_tm(alarm_time, &alrm->time);
410 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
411 RV3028_CTRL2_AIE | RV3028_CTRL2_UIE, 0);
415 alarmvals[0] = bin2bcd(alrm->time.tm_min);
416 alarmvals[1] = bin2bcd(alrm->time.tm_hour);
417 alarmvals[2] = bin2bcd(alrm->time.tm_mday);
419 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
420 RV3028_STATUS_AF, 0);
424 ret = regmap_bulk_write(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
430 if (rv3028->rtc->uie_rtctimer.enabled)
431 ctrl |= RV3028_CTRL2_UIE;
432 if (rv3028->rtc->aie_timer.enabled)
433 ctrl |= RV3028_CTRL2_AIE;
436 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
437 RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
442 static int rv3028_alarm_irq_enable(struct device *dev, unsigned int enabled)
444 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
448 if (rv3028->rtc->uie_rtctimer.enabled)
449 ctrl |= RV3028_CTRL2_UIE;
450 if (rv3028->rtc->aie_timer.enabled)
451 ctrl |= RV3028_CTRL2_AIE;
454 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
455 RV3028_STATUS_AF | RV3028_STATUS_UF, 0);
459 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
460 RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
467 static int rv3028_read_offset(struct device *dev, long *offset)
469 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
470 int ret, value, steps;
472 ret = regmap_read(rv3028->regmap, RV3028_OFFSET, &value);
476 steps = sign_extend32(value << 1, 8);
478 ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value);
484 *offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000);
489 static int rv3028_set_offset(struct device *dev, long offset)
491 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
495 offset = clamp(offset, -244141L, 243187L) * 1000;
496 offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT);
498 ret = rv3028_enter_eerd(rv3028, &eerd);
502 ret = regmap_write(rv3028->regmap, RV3028_OFFSET, offset >> 1);
506 ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP, BIT(7),
511 return rv3028_update_eeprom(rv3028, eerd);
514 rv3028_exit_eerd(rv3028, eerd);
520 static int rv3028_param_get(struct device *dev, struct rtc_param *param)
522 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
525 switch(param->param) {
528 case RTC_PARAM_BACKUP_SWITCH_MODE:
529 ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value);
533 value = FIELD_GET(RV3028_BACKUP_BSM, value);
536 case RV3028_BACKUP_BSM_DSM:
537 param->uvalue = RTC_BSM_DIRECT;
539 case RV3028_BACKUP_BSM_LSM:
540 param->uvalue = RTC_BSM_LEVEL;
543 param->uvalue = RTC_BSM_DISABLED;
554 static int rv3028_param_set(struct device *dev, struct rtc_param *param)
556 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
558 switch(param->param) {
560 case RTC_PARAM_BACKUP_SWITCH_MODE:
561 switch (param->uvalue) {
562 case RTC_BSM_DISABLED:
566 mode = RV3028_BACKUP_BSM_DSM;
569 mode = RV3028_BACKUP_BSM_LSM;
575 return rv3028_update_cfg(rv3028, RV3028_BACKUP, RV3028_BACKUP_BSM,
576 FIELD_PREP(RV3028_BACKUP_BSM, mode));
585 static int rv3028_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
587 struct rv3028_data *rv3028 = dev_get_drvdata(dev);
592 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
596 status = status & RV3028_STATUS_PORF ? RTC_VL_DATA_INVALID : 0;
597 return put_user(status, (unsigned int __user *)arg);
604 static int rv3028_nvram_write(void *priv, unsigned int offset, void *val,
607 return regmap_bulk_write(priv, RV3028_RAM1 + offset, val, bytes);
610 static int rv3028_nvram_read(void *priv, unsigned int offset, void *val,
613 return regmap_bulk_read(priv, RV3028_RAM1 + offset, val, bytes);
616 static int rv3028_eeprom_write(void *priv, unsigned int offset, void *val,
619 struct rv3028_data *rv3028 = priv;
624 ret = rv3028_enter_eerd(rv3028, &eerd);
628 for (i = 0; i < bytes; i++) {
629 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_ADDR, offset + i);
633 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_DATA, buf[i]);
637 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0);
641 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD,
642 RV3028_EEPROM_CMD_WRITE);
646 usleep_range(RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
648 ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
649 !(status & RV3028_STATUS_EEBUSY),
651 RV3028_EEBUSY_TIMEOUT);
657 rv3028_exit_eerd(rv3028, eerd);
662 static int rv3028_eeprom_read(void *priv, unsigned int offset, void *val,
665 struct rv3028_data *rv3028 = priv;
666 u32 status, eerd, data;
670 ret = rv3028_enter_eerd(rv3028, &eerd);
674 for (i = 0; i < bytes; i++) {
675 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_ADDR, offset + i);
679 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD, 0x0);
683 ret = regmap_write(rv3028->regmap, RV3028_EEPROM_CMD,
684 RV3028_EEPROM_CMD_READ);
688 ret = regmap_read_poll_timeout(rv3028->regmap, RV3028_STATUS, status,
689 !(status & RV3028_STATUS_EEBUSY),
691 RV3028_EEBUSY_TIMEOUT);
695 ret = regmap_read(rv3028->regmap, RV3028_EEPROM_DATA, &data);
702 rv3028_exit_eerd(rv3028, eerd);
707 #ifdef CONFIG_COMMON_CLK
708 #define clkout_hw_to_rv3028(hw) container_of(hw, struct rv3028_data, clkout_hw)
710 static int clkout_rates[] = {
719 static unsigned long rv3028_clkout_recalc_rate(struct clk_hw *hw,
720 unsigned long parent_rate)
723 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
725 ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout);
729 clkout &= RV3028_CLKOUT_FD_MASK;
730 return clkout_rates[clkout];
733 static long rv3028_clkout_round_rate(struct clk_hw *hw, unsigned long rate,
734 unsigned long *prate)
738 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
739 if (clkout_rates[i] <= rate)
740 return clkout_rates[i];
745 static int rv3028_clkout_set_rate(struct clk_hw *hw, unsigned long rate,
746 unsigned long parent_rate)
750 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
752 ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &enabled);
756 ret = regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0);
760 enabled &= RV3028_CLKOUT_CLKOE;
762 for (i = 0; i < ARRAY_SIZE(clkout_rates); i++)
763 if (clkout_rates[i] == rate)
764 return rv3028_update_cfg(rv3028, RV3028_CLKOUT, 0xff,
765 RV3028_CLKOUT_CLKSY | enabled | i);
770 static int rv3028_clkout_prepare(struct clk_hw *hw)
772 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
774 return regmap_write(rv3028->regmap, RV3028_CLKOUT,
775 RV3028_CLKOUT_CLKSY | RV3028_CLKOUT_CLKOE);
778 static void rv3028_clkout_unprepare(struct clk_hw *hw)
780 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
782 regmap_write(rv3028->regmap, RV3028_CLKOUT, 0x0);
783 regmap_update_bits(rv3028->regmap, RV3028_STATUS,
784 RV3028_STATUS_CLKF, 0);
787 static int rv3028_clkout_is_prepared(struct clk_hw *hw)
790 struct rv3028_data *rv3028 = clkout_hw_to_rv3028(hw);
792 ret = regmap_read(rv3028->regmap, RV3028_CLKOUT, &clkout);
796 return !!(clkout & RV3028_CLKOUT_CLKOE);
799 static const struct clk_ops rv3028_clkout_ops = {
800 .prepare = rv3028_clkout_prepare,
801 .unprepare = rv3028_clkout_unprepare,
802 .is_prepared = rv3028_clkout_is_prepared,
803 .recalc_rate = rv3028_clkout_recalc_rate,
804 .round_rate = rv3028_clkout_round_rate,
805 .set_rate = rv3028_clkout_set_rate,
808 static int rv3028_clkout_register_clk(struct rv3028_data *rv3028,
809 struct i2c_client *client)
813 struct clk_init_data init;
814 struct device_node *node = client->dev.of_node;
816 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
817 RV3028_STATUS_CLKF, 0);
821 init.name = "rv3028-clkout";
822 init.ops = &rv3028_clkout_ops;
824 init.parent_names = NULL;
825 init.num_parents = 0;
826 rv3028->clkout_hw.init = &init;
828 /* optional override of the clockname */
829 of_property_read_string(node, "clock-output-names", &init.name);
831 /* register the clock */
832 clk = devm_clk_register(&client->dev, &rv3028->clkout_hw);
834 of_clk_add_provider(node, of_clk_src_simple_get, clk);
840 static const struct rtc_class_ops rv3028_rtc_ops = {
841 .read_time = rv3028_get_time,
842 .set_time = rv3028_set_time,
843 .read_alarm = rv3028_get_alarm,
844 .set_alarm = rv3028_set_alarm,
845 .alarm_irq_enable = rv3028_alarm_irq_enable,
846 .read_offset = rv3028_read_offset,
847 .set_offset = rv3028_set_offset,
848 .ioctl = rv3028_ioctl,
849 .param_get = rv3028_param_get,
850 .param_set = rv3028_param_set,
853 static const struct regmap_config regmap_config = {
856 .max_register = 0x37,
859 static int rv3028_probe(struct i2c_client *client)
861 struct rv3028_data *rv3028;
864 struct nvmem_config nvmem_cfg = {
865 .name = "rv3028_nvram",
869 .type = NVMEM_TYPE_BATTERY_BACKED,
870 .reg_read = rv3028_nvram_read,
871 .reg_write = rv3028_nvram_write,
873 struct nvmem_config eeprom_cfg = {
874 .name = "rv3028_eeprom",
878 .type = NVMEM_TYPE_EEPROM,
879 .reg_read = rv3028_eeprom_read,
880 .reg_write = rv3028_eeprom_write,
883 rv3028 = devm_kzalloc(&client->dev, sizeof(struct rv3028_data),
888 rv3028->regmap = devm_regmap_init_i2c(client, ®map_config);
889 if (IS_ERR(rv3028->regmap))
890 return PTR_ERR(rv3028->regmap);
892 i2c_set_clientdata(client, rv3028);
894 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
898 if (status & RV3028_STATUS_AF)
899 dev_warn(&client->dev, "An alarm may have been missed.\n");
901 rv3028->rtc = devm_rtc_allocate_device(&client->dev);
902 if (IS_ERR(rv3028->rtc))
903 return PTR_ERR(rv3028->rtc);
905 if (client->irq > 0) {
906 ret = devm_request_threaded_irq(&client->dev, client->irq,
907 NULL, rv3028_handle_irq,
908 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
911 dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
916 clear_bit(RTC_FEATURE_ALARM, rv3028->rtc->features);
918 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
919 RV3028_CTRL1_WADA, RV3028_CTRL1_WADA);
923 /* setup timestamping */
924 ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
925 RV3028_CTRL2_EIE | RV3028_CTRL2_TSE,
926 RV3028_CTRL2_EIE | RV3028_CTRL2_TSE);
930 /* setup trickle charger */
931 if (!device_property_read_u32(&client->dev, "trickle-resistor-ohms",
935 for (i = 0; i < ARRAY_SIZE(rv3028_trickle_resistors); i++)
936 if (ohms == rv3028_trickle_resistors[i])
939 if (i < ARRAY_SIZE(rv3028_trickle_resistors)) {
940 ret = rv3028_update_cfg(rv3028, RV3028_BACKUP, RV3028_BACKUP_TCE |
941 RV3028_BACKUP_TCR_MASK, RV3028_BACKUP_TCE | i);
945 dev_warn(&client->dev, "invalid trickle resistor value\n");
949 ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group);
953 set_bit(RTC_FEATURE_BACKUP_SWITCH_MODE, rv3028->rtc->features);
955 rv3028->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
956 rv3028->rtc->range_max = RTC_TIMESTAMP_END_2099;
957 rv3028->rtc->ops = &rv3028_rtc_ops;
958 ret = devm_rtc_register_device(rv3028->rtc);
962 nvmem_cfg.priv = rv3028->regmap;
963 devm_rtc_nvmem_register(rv3028->rtc, &nvmem_cfg);
964 eeprom_cfg.priv = rv3028;
965 devm_rtc_nvmem_register(rv3028->rtc, &eeprom_cfg);
967 rv3028->rtc->max_user_freq = 1;
969 #ifdef CONFIG_COMMON_CLK
970 rv3028_clkout_register_clk(rv3028, client);
975 static const __maybe_unused struct of_device_id rv3028_of_match[] = {
976 { .compatible = "microcrystal,rv3028", },
979 MODULE_DEVICE_TABLE(of, rv3028_of_match);
981 static struct i2c_driver rv3028_driver = {
983 .name = "rtc-rv3028",
984 .of_match_table = of_match_ptr(rv3028_of_match),
986 .probe_new = rv3028_probe,
988 module_i2c_driver(rv3028_driver);
990 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
991 MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver");
992 MODULE_LICENSE("GPL v2");