1 // SPDX-License-Identifier: GPL-2.0-only
3 * Micro Crystal RV-3029 / RV-3049 rtc class driver
5 * Author: Gregory Hermant <gregory.hermant@calao-systems.com>
6 * Michael Buesch <m@bues.ch>
8 * based on previously existing rtc class drivers
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/spi/spi.h>
14 #include <linux/bcd.h>
15 #include <linux/rtc.h>
16 #include <linux/delay.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/regmap.h>
24 #define RV3029_ONOFF_CTRL 0x00
25 #define RV3029_ONOFF_CTRL_WE BIT(0)
26 #define RV3029_ONOFF_CTRL_TE BIT(1)
27 #define RV3029_ONOFF_CTRL_TAR BIT(2)
28 #define RV3029_ONOFF_CTRL_EERE BIT(3)
29 #define RV3029_ONOFF_CTRL_SRON BIT(4)
30 #define RV3029_ONOFF_CTRL_TD0 BIT(5)
31 #define RV3029_ONOFF_CTRL_TD1 BIT(6)
32 #define RV3029_ONOFF_CTRL_CLKINT BIT(7)
33 #define RV3029_IRQ_CTRL 0x01
34 #define RV3029_IRQ_CTRL_AIE BIT(0)
35 #define RV3029_IRQ_CTRL_TIE BIT(1)
36 #define RV3029_IRQ_CTRL_V1IE BIT(2)
37 #define RV3029_IRQ_CTRL_V2IE BIT(3)
38 #define RV3029_IRQ_CTRL_SRIE BIT(4)
39 #define RV3029_IRQ_FLAGS 0x02
40 #define RV3029_IRQ_FLAGS_AF BIT(0)
41 #define RV3029_IRQ_FLAGS_TF BIT(1)
42 #define RV3029_IRQ_FLAGS_V1IF BIT(2)
43 #define RV3029_IRQ_FLAGS_V2IF BIT(3)
44 #define RV3029_IRQ_FLAGS_SRF BIT(4)
45 #define RV3029_STATUS 0x03
46 #define RV3029_STATUS_VLOW1 BIT(2)
47 #define RV3029_STATUS_VLOW2 BIT(3)
48 #define RV3029_STATUS_SR BIT(4)
49 #define RV3029_STATUS_PON BIT(5)
50 #define RV3029_STATUS_EEBUSY BIT(7)
51 #define RV3029_RST_CTRL 0x04
52 #define RV3029_RST_CTRL_SYSR BIT(4)
53 #define RV3029_CONTROL_SECTION_LEN 0x05
56 #define RV3029_W_SEC 0x08
57 #define RV3029_W_MINUTES 0x09
58 #define RV3029_W_HOURS 0x0A
59 #define RV3029_REG_HR_12_24 BIT(6) /* 24h/12h mode */
60 #define RV3029_REG_HR_PM BIT(5) /* PM/AM bit in 12h mode */
61 #define RV3029_W_DATE 0x0B
62 #define RV3029_W_DAYS 0x0C
63 #define RV3029_W_MONTHS 0x0D
64 #define RV3029_W_YEARS 0x0E
65 #define RV3029_WATCH_SECTION_LEN 0x07
68 #define RV3029_A_SC 0x10
69 #define RV3029_A_MN 0x11
70 #define RV3029_A_HR 0x12
71 #define RV3029_A_DT 0x13
72 #define RV3029_A_DW 0x14
73 #define RV3029_A_MO 0x15
74 #define RV3029_A_YR 0x16
75 #define RV3029_A_AE_X BIT(7)
76 #define RV3029_ALARM_SECTION_LEN 0x07
79 #define RV3029_TIMER_LOW 0x18
80 #define RV3029_TIMER_HIGH 0x19
82 /* temperature section */
83 #define RV3029_TEMP_PAGE 0x20
85 /* eeprom data section */
86 #define RV3029_E2P_EEDATA1 0x28
87 #define RV3029_E2P_EEDATA2 0x29
88 #define RV3029_E2PDATA_SECTION_LEN 0x02
90 /* eeprom control section */
91 #define RV3029_CONTROL_E2P_EECTRL 0x30
92 #define RV3029_EECTRL_THP BIT(0) /* temp scan interval */
93 #define RV3029_EECTRL_THE BIT(1) /* thermometer enable */
94 #define RV3029_EECTRL_FD0 BIT(2) /* CLKOUT */
95 #define RV3029_EECTRL_FD1 BIT(3) /* CLKOUT */
96 #define RV3029_TRICKLE_1K BIT(4) /* 1.5K resistance */
97 #define RV3029_TRICKLE_5K BIT(5) /* 5K resistance */
98 #define RV3029_TRICKLE_20K BIT(6) /* 20K resistance */
99 #define RV3029_TRICKLE_80K BIT(7) /* 80K resistance */
100 #define RV3029_TRICKLE_MASK (RV3029_TRICKLE_1K |\
102 RV3029_TRICKLE_20K |\
104 #define RV3029_TRICKLE_SHIFT 4
105 #define RV3029_CONTROL_E2P_XOFFS 0x31 /* XTAL offset */
106 #define RV3029_CONTROL_E2P_XOFFS_SIGN BIT(7) /* Sign: 1->pos, 0->neg */
107 #define RV3029_CONTROL_E2P_QCOEF 0x32 /* XTAL temp drift coef */
108 #define RV3029_CONTROL_E2P_TURNOVER 0x33 /* XTAL turnover temp (in *C) */
109 #define RV3029_CONTROL_E2P_TOV_MASK 0x3F /* XTAL turnover temp mask */
111 /* user ram section */
112 #define RV3029_RAM_PAGE 0x38
113 #define RV3029_RAM_SECTION_LEN 8
117 struct rtc_device *rtc;
118 struct regmap *regmap;
122 static int rv3029_eeprom_busywait(struct rv3029_data *rv3029)
127 for (i = 100; i > 0; i--) {
128 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
131 if (!(sr & RV3029_STATUS_EEBUSY))
133 usleep_range(1000, 10000);
136 dev_err(rv3029->dev, "EEPROM busy wait timeout.\n");
143 static int rv3029_eeprom_exit(struct rv3029_data *rv3029)
145 /* Re-enable eeprom refresh */
146 return regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
147 RV3029_ONOFF_CTRL_EERE,
148 RV3029_ONOFF_CTRL_EERE);
151 static int rv3029_eeprom_enter(struct rv3029_data *rv3029)
156 /* Check whether we are in the allowed voltage range. */
157 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
160 if (sr & RV3029_STATUS_VLOW2)
162 if (sr & RV3029_STATUS_VLOW1) {
163 /* We clear the bits and retry once just in case
164 * we had a brown out in early startup.
166 ret = regmap_update_bits(rv3029->regmap, RV3029_STATUS,
167 RV3029_STATUS_VLOW1, 0);
170 usleep_range(1000, 10000);
171 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
174 if (sr & RV3029_STATUS_VLOW1) {
176 "Supply voltage is too low to safely access the EEPROM.\n");
181 /* Disable eeprom refresh. */
182 ret = regmap_update_bits(rv3029->regmap, RV3029_ONOFF_CTRL,
183 RV3029_ONOFF_CTRL_EERE, 0);
187 /* Wait for any previous eeprom accesses to finish. */
188 ret = rv3029_eeprom_busywait(rv3029);
190 rv3029_eeprom_exit(rv3029);
195 static int rv3029_eeprom_read(struct rv3029_data *rv3029, u8 reg,
196 u8 buf[], size_t len)
200 err = rv3029_eeprom_enter(rv3029);
204 ret = regmap_bulk_read(rv3029->regmap, reg, buf, len);
206 err = rv3029_eeprom_exit(rv3029);
213 static int rv3029_eeprom_write(struct rv3029_data *rv3029, u8 reg,
214 u8 const buf[], size_t len)
220 err = rv3029_eeprom_enter(rv3029);
224 for (i = 0; i < len; i++, reg++) {
225 ret = regmap_read(rv3029->regmap, reg, &tmp);
230 ret = regmap_write(rv3029->regmap, reg, tmp);
234 ret = rv3029_eeprom_busywait(rv3029);
239 err = rv3029_eeprom_exit(rv3029);
246 static int rv3029_eeprom_update_bits(struct rv3029_data *rv3029,
247 u8 reg, u8 mask, u8 set)
252 ret = rv3029_eeprom_read(rv3029, reg, &buf, 1);
257 ret = rv3029_eeprom_write(rv3029, reg, &buf, 1);
264 static irqreturn_t rv3029_handle_irq(int irq, void *dev_id)
266 struct device *dev = dev_id;
267 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
268 struct mutex *lock = &rv3029->rtc->ops_lock;
269 unsigned int flags, controls;
270 unsigned long events = 0;
275 ret = regmap_read(rv3029->regmap, RV3029_IRQ_CTRL, &controls);
277 dev_warn(dev, "Read IRQ Control Register error %d\n", ret);
282 ret = regmap_read(rv3029->regmap, RV3029_IRQ_FLAGS, &flags);
284 dev_warn(dev, "Read IRQ Flags Register error %d\n", ret);
289 if (flags & RV3029_IRQ_FLAGS_AF) {
290 flags &= ~RV3029_IRQ_FLAGS_AF;
291 controls &= ~RV3029_IRQ_CTRL_AIE;
296 rtc_update_irq(rv3029->rtc, 1, events);
297 regmap_write(rv3029->regmap, RV3029_IRQ_FLAGS, flags);
298 regmap_write(rv3029->regmap, RV3029_IRQ_CTRL, controls);
305 static int rv3029_read_time(struct device *dev, struct rtc_time *tm)
307 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
310 u8 regs[RV3029_WATCH_SECTION_LEN] = { 0, };
312 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
316 if (sr & (RV3029_STATUS_VLOW2 | RV3029_STATUS_PON))
319 ret = regmap_bulk_read(rv3029->regmap, RV3029_W_SEC, regs,
320 RV3029_WATCH_SECTION_LEN);
324 tm->tm_sec = bcd2bin(regs[RV3029_W_SEC - RV3029_W_SEC]);
325 tm->tm_min = bcd2bin(regs[RV3029_W_MINUTES - RV3029_W_SEC]);
327 /* HR field has a more complex interpretation */
329 const u8 _hr = regs[RV3029_W_HOURS - RV3029_W_SEC];
331 if (_hr & RV3029_REG_HR_12_24) {
333 tm->tm_hour = bcd2bin(_hr & 0x1f);
334 if (_hr & RV3029_REG_HR_PM) /* PM flag set */
336 } else /* 24h format */
337 tm->tm_hour = bcd2bin(_hr & 0x3f);
340 tm->tm_mday = bcd2bin(regs[RV3029_W_DATE - RV3029_W_SEC]);
341 tm->tm_mon = bcd2bin(regs[RV3029_W_MONTHS - RV3029_W_SEC]) - 1;
342 tm->tm_year = bcd2bin(regs[RV3029_W_YEARS - RV3029_W_SEC]) + 100;
343 tm->tm_wday = bcd2bin(regs[RV3029_W_DAYS - RV3029_W_SEC]) - 1;
348 static int rv3029_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
350 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
351 struct rtc_time *const tm = &alarm->time;
352 unsigned int controls, flags;
356 ret = regmap_bulk_read(rv3029->regmap, RV3029_A_SC, regs,
357 RV3029_ALARM_SECTION_LEN);
361 ret = regmap_read(rv3029->regmap, RV3029_IRQ_CTRL, &controls);
365 ret = regmap_read(rv3029->regmap, RV3029_IRQ_FLAGS, &flags);
369 tm->tm_sec = bcd2bin(regs[RV3029_A_SC - RV3029_A_SC] & 0x7f);
370 tm->tm_min = bcd2bin(regs[RV3029_A_MN - RV3029_A_SC] & 0x7f);
371 tm->tm_hour = bcd2bin(regs[RV3029_A_HR - RV3029_A_SC] & 0x3f);
372 tm->tm_mday = bcd2bin(regs[RV3029_A_DT - RV3029_A_SC] & 0x3f);
373 tm->tm_mon = bcd2bin(regs[RV3029_A_MO - RV3029_A_SC] & 0x1f) - 1;
374 tm->tm_year = bcd2bin(regs[RV3029_A_YR - RV3029_A_SC] & 0x7f) + 100;
375 tm->tm_wday = bcd2bin(regs[RV3029_A_DW - RV3029_A_SC] & 0x07) - 1;
377 alarm->enabled = !!(controls & RV3029_IRQ_CTRL_AIE);
378 alarm->pending = (flags & RV3029_IRQ_FLAGS_AF) && alarm->enabled;
383 static int rv3029_alarm_irq_enable(struct device *dev, unsigned int enable)
385 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
387 return regmap_update_bits(rv3029->regmap, RV3029_IRQ_CTRL,
389 enable ? RV3029_IRQ_CTRL_AIE : 0);
392 static int rv3029_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
394 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
395 struct rtc_time *const tm = &alarm->time;
399 /* Activate all the alarms with AE_x bit */
400 regs[RV3029_A_SC - RV3029_A_SC] = bin2bcd(tm->tm_sec) | RV3029_A_AE_X;
401 regs[RV3029_A_MN - RV3029_A_SC] = bin2bcd(tm->tm_min) | RV3029_A_AE_X;
402 regs[RV3029_A_HR - RV3029_A_SC] = (bin2bcd(tm->tm_hour) & 0x3f)
404 regs[RV3029_A_DT - RV3029_A_SC] = (bin2bcd(tm->tm_mday) & 0x3f)
406 regs[RV3029_A_MO - RV3029_A_SC] = (bin2bcd(tm->tm_mon + 1) & 0x1f)
408 regs[RV3029_A_DW - RV3029_A_SC] = (bin2bcd(tm->tm_wday + 1) & 0x7)
410 regs[RV3029_A_YR - RV3029_A_SC] = (bin2bcd(tm->tm_year - 100))
413 /* Write the alarm */
414 ret = regmap_bulk_write(rv3029->regmap, RV3029_A_SC, regs,
415 RV3029_ALARM_SECTION_LEN);
419 return rv3029_alarm_irq_enable(dev, alarm->enabled);
422 static int rv3029_set_time(struct device *dev, struct rtc_time *tm)
424 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
428 regs[RV3029_W_SEC - RV3029_W_SEC] = bin2bcd(tm->tm_sec);
429 regs[RV3029_W_MINUTES - RV3029_W_SEC] = bin2bcd(tm->tm_min);
430 regs[RV3029_W_HOURS - RV3029_W_SEC] = bin2bcd(tm->tm_hour);
431 regs[RV3029_W_DATE - RV3029_W_SEC] = bin2bcd(tm->tm_mday);
432 regs[RV3029_W_MONTHS - RV3029_W_SEC] = bin2bcd(tm->tm_mon + 1);
433 regs[RV3029_W_DAYS - RV3029_W_SEC] = bin2bcd(tm->tm_wday + 1) & 0x7;
434 regs[RV3029_W_YEARS - RV3029_W_SEC] = bin2bcd(tm->tm_year - 100);
436 ret = regmap_bulk_write(rv3029->regmap, RV3029_W_SEC, regs,
437 RV3029_WATCH_SECTION_LEN);
441 /* clear PON and VLOW2 bits */
442 return regmap_update_bits(rv3029->regmap, RV3029_STATUS,
443 RV3029_STATUS_PON | RV3029_STATUS_VLOW2, 0);
446 static int rv3029_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
448 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
449 unsigned long vl = 0;
454 ret = regmap_read(rv3029->regmap, RV3029_STATUS, &sr);
458 if (sr & RV3029_STATUS_VLOW1)
459 vl = RTC_VL_ACCURACY_LOW;
461 if (sr & (RV3029_STATUS_VLOW2 | RV3029_STATUS_PON))
462 vl |= RTC_VL_DATA_INVALID;
464 return put_user(vl, (unsigned int __user *)arg);
467 return regmap_update_bits(rv3029->regmap, RV3029_STATUS,
468 RV3029_STATUS_VLOW1, 0);
475 static int rv3029_nvram_write(void *priv, unsigned int offset, void *val,
478 return regmap_bulk_write(priv, RV3029_RAM_PAGE + offset, val, bytes);
481 static int rv3029_nvram_read(void *priv, unsigned int offset, void *val,
484 return regmap_bulk_read(priv, RV3029_RAM_PAGE + offset, val, bytes);
487 static const struct rv3029_trickle_tab_elem {
488 u32 r; /* resistance in ohms */
489 u8 conf; /* trickle config bits */
490 } rv3029_trickle_tab[] = {
493 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
494 RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
497 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
501 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K |
505 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_5K,
508 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K |
512 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_20K,
515 .conf = RV3029_TRICKLE_1K | RV3029_TRICKLE_80K,
518 .conf = RV3029_TRICKLE_1K,
521 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K |
525 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_20K,
528 .conf = RV3029_TRICKLE_5K | RV3029_TRICKLE_80K,
531 .conf = RV3029_TRICKLE_5K,
534 .conf = RV3029_TRICKLE_20K | RV3029_TRICKLE_80K,
537 .conf = RV3029_TRICKLE_20K,
540 .conf = RV3029_TRICKLE_80K,
544 static void rv3029_trickle_config(struct device *dev)
546 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
547 struct device_node *of_node = dev->of_node;
548 const struct rv3029_trickle_tab_elem *elem;
556 /* Configure the trickle charger. */
557 err = of_property_read_u32(of_node, "trickle-resistor-ohms", &ohms);
559 /* Disable trickle charger. */
560 trickle_set_bits = 0;
562 /* Enable trickle charger. */
563 for (i = 0; i < ARRAY_SIZE(rv3029_trickle_tab); i++) {
564 elem = &rv3029_trickle_tab[i];
568 trickle_set_bits = elem->conf;
570 "Trickle charger enabled at %d ohms resistance.\n",
573 err = rv3029_eeprom_update_bits(rv3029, RV3029_CONTROL_E2P_EECTRL,
577 dev_err(dev, "Failed to update trickle charger config\n");
580 #ifdef CONFIG_RTC_DRV_RV3029_HWMON
582 static int rv3029_read_temp(struct rv3029_data *rv3029, int *temp_mC)
587 ret = regmap_read(rv3029->regmap, RV3029_TEMP_PAGE, &temp);
591 *temp_mC = ((int)temp - 60) * 1000;
596 static ssize_t rv3029_hwmon_show_temp(struct device *dev,
597 struct device_attribute *attr,
600 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
603 ret = rv3029_read_temp(rv3029, &temp_mC);
607 return sprintf(buf, "%d\n", temp_mC);
610 static ssize_t rv3029_hwmon_set_update_interval(struct device *dev,
611 struct device_attribute *attr,
615 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
616 unsigned int th_set_bits = 0;
617 unsigned long interval_ms;
620 ret = kstrtoul(buf, 10, &interval_ms);
624 if (interval_ms != 0) {
625 th_set_bits |= RV3029_EECTRL_THE;
626 if (interval_ms >= 16000)
627 th_set_bits |= RV3029_EECTRL_THP;
629 ret = rv3029_eeprom_update_bits(rv3029, RV3029_CONTROL_E2P_EECTRL,
630 RV3029_EECTRL_THE | RV3029_EECTRL_THP,
638 static ssize_t rv3029_hwmon_show_update_interval(struct device *dev,
639 struct device_attribute *attr,
642 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
643 int ret, interval_ms;
646 ret = rv3029_eeprom_read(rv3029, RV3029_CONTROL_E2P_EECTRL,
651 if (eectrl & RV3029_EECTRL_THE) {
652 if (eectrl & RV3029_EECTRL_THP)
660 return sprintf(buf, "%d\n", interval_ms);
663 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, rv3029_hwmon_show_temp,
665 static SENSOR_DEVICE_ATTR(update_interval, S_IWUSR | S_IRUGO,
666 rv3029_hwmon_show_update_interval,
667 rv3029_hwmon_set_update_interval, 0);
669 static struct attribute *rv3029_hwmon_attrs[] = {
670 &sensor_dev_attr_temp1_input.dev_attr.attr,
671 &sensor_dev_attr_update_interval.dev_attr.attr,
674 ATTRIBUTE_GROUPS(rv3029_hwmon);
676 static void rv3029_hwmon_register(struct device *dev, const char *name)
678 struct rv3029_data *rv3029 = dev_get_drvdata(dev);
679 struct device *hwmon_dev;
681 hwmon_dev = devm_hwmon_device_register_with_groups(dev, name, rv3029,
682 rv3029_hwmon_groups);
683 if (IS_ERR(hwmon_dev)) {
684 dev_warn(dev, "unable to register hwmon device %ld\n",
689 #else /* CONFIG_RTC_DRV_RV3029_HWMON */
691 static void rv3029_hwmon_register(struct device *dev, const char *name)
695 #endif /* CONFIG_RTC_DRV_RV3029_HWMON */
697 static struct rtc_class_ops rv3029_rtc_ops = {
698 .read_time = rv3029_read_time,
699 .set_time = rv3029_set_time,
700 .ioctl = rv3029_ioctl,
703 static int rv3029_probe(struct device *dev, struct regmap *regmap, int irq,
706 struct rv3029_data *rv3029;
707 struct nvmem_config nvmem_cfg = {
708 .name = "rv3029_nvram",
711 .size = RV3029_RAM_SECTION_LEN,
712 .type = NVMEM_TYPE_BATTERY_BACKED,
713 .reg_read = rv3029_nvram_read,
714 .reg_write = rv3029_nvram_write,
718 rv3029 = devm_kzalloc(dev, sizeof(*rv3029), GFP_KERNEL);
722 rv3029->regmap = regmap;
725 dev_set_drvdata(dev, rv3029);
727 rv3029_trickle_config(dev);
728 rv3029_hwmon_register(dev, name);
730 rv3029->rtc = devm_rtc_allocate_device(dev);
731 if (IS_ERR(rv3029->rtc))
732 return PTR_ERR(rv3029->rtc);
734 if (rv3029->irq > 0) {
735 rc = devm_request_threaded_irq(dev, rv3029->irq,
736 NULL, rv3029_handle_irq,
737 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
740 dev_warn(dev, "unable to request IRQ, alarms disabled\n");
743 rv3029_rtc_ops.read_alarm = rv3029_read_alarm;
744 rv3029_rtc_ops.set_alarm = rv3029_set_alarm;
745 rv3029_rtc_ops.alarm_irq_enable = rv3029_alarm_irq_enable;
749 rv3029->rtc->ops = &rv3029_rtc_ops;
750 rv3029->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
751 rv3029->rtc->range_max = RTC_TIMESTAMP_END_2079;
753 rc = rtc_register_device(rv3029->rtc);
757 nvmem_cfg.priv = rv3029->regmap;
758 rtc_nvmem_register(rv3029->rtc, &nvmem_cfg);
763 static const struct regmap_range rv3029_holes_range[] = {
764 regmap_reg_range(0x05, 0x07),
765 regmap_reg_range(0x0f, 0x0f),
766 regmap_reg_range(0x17, 0x17),
767 regmap_reg_range(0x1a, 0x1f),
768 regmap_reg_range(0x21, 0x27),
769 regmap_reg_range(0x34, 0x37),
772 static const struct regmap_access_table rv3029_regs = {
773 .no_ranges = rv3029_holes_range,
774 .n_no_ranges = ARRAY_SIZE(rv3029_holes_range),
777 static const struct regmap_config config = {
780 .rd_table = &rv3029_regs,
781 .wr_table = &rv3029_regs,
782 .max_register = 0x3f,
785 #if IS_ENABLED(CONFIG_I2C)
787 static int rv3029_i2c_probe(struct i2c_client *client,
788 const struct i2c_device_id *id)
790 struct regmap *regmap;
791 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_I2C_BLOCK |
792 I2C_FUNC_SMBUS_BYTE)) {
793 dev_err(&client->dev, "Adapter does not support SMBUS_I2C_BLOCK or SMBUS_I2C_BYTE\n");
797 regmap = devm_regmap_init_i2c(client, &config);
799 return PTR_ERR(regmap);
801 return rv3029_probe(&client->dev, regmap, client->irq, client->name);
804 static const struct i2c_device_id rv3029_id[] = {
809 MODULE_DEVICE_TABLE(i2c, rv3029_id);
811 static const struct of_device_id rv3029_of_match[] = {
812 { .compatible = "microcrystal,rv3029" },
815 MODULE_DEVICE_TABLE(of, rv3029_of_match);
817 static struct i2c_driver rv3029_driver = {
820 .of_match_table = of_match_ptr(rv3029_of_match),
822 .probe = rv3029_i2c_probe,
823 .id_table = rv3029_id,
826 static int __init rv3029_register_driver(void)
828 return i2c_add_driver(&rv3029_driver);
831 static void rv3029_unregister_driver(void)
833 i2c_del_driver(&rv3029_driver);
838 static int __init rv3029_register_driver(void)
843 static void rv3029_unregister_driver(void)
849 #if IS_ENABLED(CONFIG_SPI_MASTER)
851 static int rv3049_probe(struct spi_device *spi)
853 struct regmap *regmap;
855 regmap = devm_regmap_init_spi(spi, &config);
857 return PTR_ERR(regmap);
859 return rv3029_probe(&spi->dev, regmap, spi->irq, "rv3049");
862 static struct spi_driver rv3049_driver = {
866 .probe = rv3049_probe,
869 static int __init rv3049_register_driver(void)
871 return spi_register_driver(&rv3049_driver);
874 static void __exit rv3049_unregister_driver(void)
876 spi_unregister_driver(&rv3049_driver);
881 static int __init rv3049_register_driver(void)
886 static void __exit rv3049_unregister_driver(void)
892 static int __init rv30x9_init(void)
896 ret = rv3029_register_driver();
900 ret = rv3049_register_driver();
902 rv3029_unregister_driver();
906 module_init(rv30x9_init)
908 static void __exit rv30x9_exit(void)
910 rv3049_unregister_driver();
911 rv3029_unregister_driver();
913 module_exit(rv30x9_exit)
915 MODULE_AUTHOR("Gregory Hermant <gregory.hermant@calao-systems.com>");
916 MODULE_AUTHOR("Michael Buesch <m@bues.ch>");
917 MODULE_DESCRIPTION("Micro Crystal RV3029/RV3049 RTC driver");
918 MODULE_LICENSE("GPL");
919 MODULE_ALIAS("spi:rv3049");