1 From bb0e317bfc453877805a12f975490ad38b6413f1 Mon Sep 17 00:00:00 2001
2 From: Alexandre Belloni <alexandre.belloni@bootlin.com>
3 Date: Wed, 13 Feb 2019 00:21:36 +0100
4 Subject: [PATCH] rtc: rv3028: add new driver
6 upstream commit e6e7376cfd7b3f9b63de3a22792f64d9bfb2ab53.
8 Add a driver for the MicroCrystal RV-3028. It is a SMT Real-Time Clock
9 Module that incorporates an integrated CMOS circuit together with an XTAL.
10 It has an i2c interface.
12 The driver handles date/time, alarms, trickle charging, timestamping,
13 frequency offset correction, EEPROM and NVRAM.
15 Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
17 Documentation/devicetree/bindings/rtc/rtc.txt | 69 ++
18 drivers/rtc/Kconfig | 9 +
19 drivers/rtc/Makefile | 1 +
20 drivers/rtc/rtc-rv3028.c | 733 ++++++++++++++++++
21 4 files changed, 812 insertions(+)
22 create mode 100644 Documentation/devicetree/bindings/rtc/rtc.txt
23 create mode 100644 drivers/rtc/rtc-rv3028.c
26 +++ b/Documentation/devicetree/bindings/rtc/rtc.txt
28 +Generic device tree bindings for Real Time Clock devices
29 +========================================================
31 +This document describes generic bindings which can be used to describe Real Time
32 +Clock devices in a device tree.
37 +- compatible : name of RTC device following generic names recommended practice.
39 +For other required properties e.g. to describe register sets,
40 +clocks, etc. check the binding documentation of the specific driver.
45 +- start-year : if provided, the default hardware range supported by the RTC is
46 + shifted so the first usable year is the specified one.
48 +The following properties may not be supported by all drivers. However, if a
49 +driver wants to support one of the below features, it should adapt the bindings
51 +- trickle-resistor-ohms : Selected resistor for trickle charger. Should be given
52 + if trickle charger should be enabled
53 +- trickle-diode-disable : Do not use internal trickle charger diode Should be
54 + given if internal trickle charger diode should be
56 +- wakeup-source : Enables wake up of host system on alarm
57 +- quartz-load-femtofarads : The capacitive load of the quartz(x-tal),
58 + expressed in femto Farad (fF).
59 + The default value shall be listed (if optional),
60 + and likewise all valid values.
65 +This is a list of trivial RTC devices that have simple device tree
66 +bindings, consisting only of a compatible field, an address and
67 +possibly an interrupt line.
70 +Compatible Vendor / Chip
71 +========== =============
72 +abracon,abb5zes3 AB-RTCMC-32.768kHz-B5ZE-S3: Real Time Clock/Calendar Module with I2C Interface
73 +dallas,ds1374 I2C, 32-Bit Binary Counter Watchdog RTC with Trickle Charger and Reset Input/Output
74 +dallas,ds1672 Dallas DS1672 Real-time Clock
75 +dallas,ds3232 Extremely Accurate I²C RTC with Integrated Crystal and SRAM
76 +epson,rx8010 I2C-BUS INTERFACE REAL TIME CLOCK MODULE
77 +epson,rx8581 I2C-BUS INTERFACE REAL TIME CLOCK MODULE
78 +emmicro,em3027 EM Microelectronic EM3027 Real-time Clock
79 +isil,isl1208 Intersil ISL1208 Low Power RTC with Battery Backed SRAM
80 +isil,isl1218 Intersil ISL1218 Low Power RTC with Battery Backed SRAM
81 +isil,isl12022 Intersil ISL12022 Real-time Clock
82 +microcrystal,rv3028 Real Time Clock Module with I2C-Bus
83 +microcrystal,rv3029 Real Time Clock Module with I2C-Bus
84 +microcrystal,rv8523 Real Time Clock
85 +nxp,pcf2127 Real-time clock
86 +nxp,pcf2129 Real-time clock
87 +nxp,pcf8563 Real-time clock/calendar
88 +pericom,pt7c4338 Real-time Clock Module
89 +ricoh,r2025sd I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
90 +ricoh,r2221tl I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
91 +ricoh,rs5c372a I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
92 +ricoh,rs5c372b I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
93 +ricoh,rv5c386 I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
94 +ricoh,rv5c387a I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
95 +sii,s35390a 2-wire CMOS real-time clock
96 +whwave,sd3078 I2C bus SERIAL INTERFACE REAL-TIME CLOCK IC
97 --- a/drivers/rtc/Kconfig
98 +++ b/drivers/rtc/Kconfig
99 @@ -626,6 +626,15 @@ config RTC_DRV_EM3027
100 This driver can also be built as a module. If so, the module
101 will be called rtc-em3027.
103 +config RTC_DRV_RV3028
104 + tristate "Micro Crystal RV3028"
106 + If you say yes here you get support for the Micro Crystal
109 + This driver can also be built as a module. If so, the module
110 + will be called rtc-rv3028.
112 config RTC_DRV_RV8803
113 tristate "Micro Crystal RV8803, Epson RX8900"
115 --- a/drivers/rtc/Makefile
116 +++ b/drivers/rtc/Makefile
117 @@ -136,6 +136,7 @@ obj-$(CONFIG_RTC_DRV_RS5C313) += rtc-rs5
118 obj-$(CONFIG_RTC_DRV_RS5C348) += rtc-rs5c348.o
119 obj-$(CONFIG_RTC_DRV_RS5C372) += rtc-rs5c372.o
120 obj-$(CONFIG_RTC_DRV_RTD119X) += rtc-rtd119x.o
121 +obj-$(CONFIG_RTC_DRV_RV3028) += rtc-rv3028.o
122 obj-$(CONFIG_RTC_DRV_RV3029C2) += rtc-rv3029c2.o
123 obj-$(CONFIG_RTC_DRV_RV8803) += rtc-rv8803.o
124 obj-$(CONFIG_RTC_DRV_RX4581) += rtc-rx4581.o
126 +++ b/drivers/rtc/rtc-rv3028.c
128 +// SPDX-License-Identifier: GPL-2.0
130 + * RTC driver for the Micro Crystal RV3028
132 + * Copyright (C) 2019 Micro Crystal SA
134 + * Alexandre Belloni <alexandre.belloni@bootlin.com>
138 +#include <linux/bcd.h>
139 +#include <linux/bitops.h>
140 +#include <linux/i2c.h>
141 +#include <linux/interrupt.h>
142 +#include <linux/kernel.h>
143 +#include <linux/log2.h>
144 +#include <linux/module.h>
145 +#include <linux/of_device.h>
146 +#include <linux/regmap.h>
147 +#include <linux/rtc.h>
148 +#include "rtc-core.h"
150 +#define RV3028_SEC 0x00
151 +#define RV3028_MIN 0x01
152 +#define RV3028_HOUR 0x02
153 +#define RV3028_WDAY 0x03
154 +#define RV3028_DAY 0x04
155 +#define RV3028_MONTH 0x05
156 +#define RV3028_YEAR 0x06
157 +#define RV3028_ALARM_MIN 0x07
158 +#define RV3028_ALARM_HOUR 0x08
159 +#define RV3028_ALARM_DAY 0x09
160 +#define RV3028_STATUS 0x0E
161 +#define RV3028_CTRL1 0x0F
162 +#define RV3028_CTRL2 0x10
163 +#define RV3028_EVT_CTRL 0x13
164 +#define RV3028_TS_COUNT 0x14
165 +#define RV3028_TS_SEC 0x15
166 +#define RV3028_RAM1 0x1F
167 +#define RV3028_EEPROM_ADDR 0x25
168 +#define RV3028_EEPROM_DATA 0x26
169 +#define RV3028_EEPROM_CMD 0x27
170 +#define RV3028_CLKOUT 0x35
171 +#define RV3028_OFFSET 0x36
172 +#define RV3028_BACKUP 0x37
174 +#define RV3028_STATUS_PORF BIT(0)
175 +#define RV3028_STATUS_EVF BIT(1)
176 +#define RV3028_STATUS_AF BIT(2)
177 +#define RV3028_STATUS_TF BIT(3)
178 +#define RV3028_STATUS_UF BIT(4)
179 +#define RV3028_STATUS_BSF BIT(5)
180 +#define RV3028_STATUS_CLKF BIT(6)
181 +#define RV3028_STATUS_EEBUSY BIT(7)
183 +#define RV3028_CTRL1_EERD BIT(3)
184 +#define RV3028_CTRL1_WADA BIT(5)
186 +#define RV3028_CTRL2_RESET BIT(0)
187 +#define RV3028_CTRL2_12_24 BIT(1)
188 +#define RV3028_CTRL2_EIE BIT(2)
189 +#define RV3028_CTRL2_AIE BIT(3)
190 +#define RV3028_CTRL2_TIE BIT(4)
191 +#define RV3028_CTRL2_UIE BIT(5)
192 +#define RV3028_CTRL2_TSE BIT(7)
194 +#define RV3028_EVT_CTRL_TSR BIT(2)
196 +#define RV3028_EEPROM_CMD_WRITE 0x21
197 +#define RV3028_EEPROM_CMD_READ 0x22
199 +#define RV3028_EEBUSY_POLL 10000
200 +#define RV3028_EEBUSY_TIMEOUT 100000
202 +#define RV3028_BACKUP_TCE BIT(5)
203 +#define RV3028_BACKUP_TCR_MASK GENMASK(1,0)
205 +#define OFFSET_STEP_PPT 953674
211 +struct rv3028_data {
212 + struct regmap *regmap;
213 + struct rtc_device *rtc;
214 + enum rv3028_type type;
217 +static u16 rv3028_trickle_resistors[] = {1000, 3000, 6000, 11000};
219 +static ssize_t timestamp0_store(struct device *dev,
220 + struct device_attribute *attr,
221 + const char *buf, size_t count)
223 + struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
225 + regmap_update_bits(rv3028->regmap, RV3028_EVT_CTRL, RV3028_EVT_CTRL_TSR,
226 + RV3028_EVT_CTRL_TSR);
231 +static ssize_t timestamp0_show(struct device *dev,
232 + struct device_attribute *attr, char *buf)
234 + struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
235 + struct rtc_time tm;
239 + ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
246 + ret = regmap_bulk_read(rv3028->regmap, RV3028_TS_SEC, date,
251 + tm.tm_sec = bcd2bin(date[0]);
252 + tm.tm_min = bcd2bin(date[1]);
253 + tm.tm_hour = bcd2bin(date[2]);
254 + tm.tm_mday = bcd2bin(date[3]);
255 + tm.tm_mon = bcd2bin(date[4]) - 1;
256 + tm.tm_year = bcd2bin(date[5]) + 100;
258 + ret = rtc_valid_tm(&tm);
262 + return sprintf(buf, "%llu\n",
263 + (unsigned long long)rtc_tm_to_time64(&tm));
266 +static DEVICE_ATTR_RW(timestamp0);
268 +static ssize_t timestamp0_count_show(struct device *dev,
269 + struct device_attribute *attr, char *buf)
271 + struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
274 + ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
278 + return sprintf(buf, "%u\n", count);
281 +static DEVICE_ATTR_RO(timestamp0_count);
283 +static struct attribute *rv3028_attrs[] = {
284 + &dev_attr_timestamp0.attr,
285 + &dev_attr_timestamp0_count.attr,
289 +static const struct attribute_group rv3028_attr_group = {
290 + .attrs = rv3028_attrs,
293 +static irqreturn_t rv3028_handle_irq(int irq, void *dev_id)
295 + struct rv3028_data *rv3028 = dev_id;
296 + unsigned long events = 0;
297 + u32 status = 0, ctrl = 0;
299 + if (regmap_read(rv3028->regmap, RV3028_STATUS, &status) < 0 ||
304 + if (status & RV3028_STATUS_PORF)
305 + dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n");
307 + if (status & RV3028_STATUS_TF) {
308 + status |= RV3028_STATUS_TF;
309 + ctrl |= RV3028_CTRL2_TIE;
313 + if (status & RV3028_STATUS_AF) {
314 + status |= RV3028_STATUS_AF;
315 + ctrl |= RV3028_CTRL2_AIE;
319 + if (status & RV3028_STATUS_UF) {
320 + status |= RV3028_STATUS_UF;
321 + ctrl |= RV3028_CTRL2_UIE;
326 + rtc_update_irq(rv3028->rtc, 1, events);
327 + regmap_update_bits(rv3028->regmap, RV3028_STATUS, status, 0);
328 + regmap_update_bits(rv3028->regmap, RV3028_CTRL2, ctrl, 0);
331 + if (status & RV3028_STATUS_EVF) {
332 + sysfs_notify(&rv3028->rtc->dev.kobj, NULL,
333 + dev_attr_timestamp0.attr.name);
334 + dev_warn(&rv3028->rtc->dev, "event detected");
337 + return IRQ_HANDLED;
340 +static int rv3028_get_time(struct device *dev, struct rtc_time *tm)
342 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
346 + ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
350 + if (status & RV3028_STATUS_PORF) {
351 + dev_warn(dev, "Voltage low, data is invalid.\n");
355 + ret = regmap_bulk_read(rv3028->regmap, RV3028_SEC, date, sizeof(date));
359 + tm->tm_sec = bcd2bin(date[RV3028_SEC] & 0x7f);
360 + tm->tm_min = bcd2bin(date[RV3028_MIN] & 0x7f);
361 + tm->tm_hour = bcd2bin(date[RV3028_HOUR] & 0x3f);
362 + tm->tm_wday = ilog2(date[RV3028_WDAY] & 0x7f);
363 + tm->tm_mday = bcd2bin(date[RV3028_DAY] & 0x3f);
364 + tm->tm_mon = bcd2bin(date[RV3028_MONTH] & 0x1f) - 1;
365 + tm->tm_year = bcd2bin(date[RV3028_YEAR]) + 100;
370 +static int rv3028_set_time(struct device *dev, struct rtc_time *tm)
372 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
376 + date[RV3028_SEC] = bin2bcd(tm->tm_sec);
377 + date[RV3028_MIN] = bin2bcd(tm->tm_min);
378 + date[RV3028_HOUR] = bin2bcd(tm->tm_hour);
379 + date[RV3028_WDAY] = 1 << (tm->tm_wday);
380 + date[RV3028_DAY] = bin2bcd(tm->tm_mday);
381 + date[RV3028_MONTH] = bin2bcd(tm->tm_mon + 1);
382 + date[RV3028_YEAR] = bin2bcd(tm->tm_year - 100);
385 + * Writing to the Seconds register has the same effect as setting RESET
388 + ret = regmap_bulk_write(rv3028->regmap, RV3028_SEC, date,
393 + ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
394 + RV3028_STATUS_PORF, 0);
399 +static int rv3028_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
401 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
403 + int status, ctrl, ret;
405 + ret = regmap_bulk_read(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
406 + sizeof(alarmvals));
410 + ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
414 + ret = regmap_read(rv3028->regmap, RV3028_CTRL2, &ctrl);
418 + alrm->time.tm_sec = 0;
419 + alrm->time.tm_min = bcd2bin(alarmvals[0] & 0x7f);
420 + alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
421 + alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
423 + alrm->enabled = !!(ctrl & RV3028_CTRL2_AIE);
424 + alrm->pending = (status & RV3028_STATUS_AF) && alrm->enabled;
429 +static int rv3028_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
431 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
436 + /* The alarm has no seconds, round up to nearest minute */
437 + if (alrm->time.tm_sec) {
438 + time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
440 + alarm_time += 60 - alrm->time.tm_sec;
441 + rtc_time64_to_tm(alarm_time, &alrm->time);
444 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
445 + RV3028_CTRL2_AIE | RV3028_CTRL2_UIE, 0);
449 + alarmvals[0] = bin2bcd(alrm->time.tm_min);
450 + alarmvals[1] = bin2bcd(alrm->time.tm_hour);
451 + alarmvals[2] = bin2bcd(alrm->time.tm_mday);
453 + ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
454 + RV3028_STATUS_AF, 0);
458 + ret = regmap_bulk_write(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
459 + sizeof(alarmvals));
463 + if (alrm->enabled) {
464 + if (rv3028->rtc->uie_rtctimer.enabled)
465 + ctrl |= RV3028_CTRL2_UIE;
466 + if (rv3028->rtc->aie_timer.enabled)
467 + ctrl |= RV3028_CTRL2_AIE;
470 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
471 + RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
476 +static int rv3028_alarm_irq_enable(struct device *dev, unsigned int enabled)
478 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
482 + if (rv3028->rtc->uie_rtctimer.enabled)
483 + ctrl |= RV3028_CTRL2_UIE;
484 + if (rv3028->rtc->aie_timer.enabled)
485 + ctrl |= RV3028_CTRL2_AIE;
488 + ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
489 + RV3028_STATUS_AF | RV3028_STATUS_UF, 0);
493 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
494 + RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
501 +static int rv3028_read_offset(struct device *dev, long *offset)
503 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
504 + int ret, value, steps;
506 + ret = regmap_read(rv3028->regmap, RV3028_OFFSET, &value);
510 + steps = sign_extend32(value << 1, 8);
512 + ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value);
516 + steps += value >> 7;
518 + *offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000);
523 +static int rv3028_set_offset(struct device *dev, long offset)
525 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
528 + offset = clamp(offset, -244141L, 243187L) * 1000;
529 + offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT);
531 + ret = regmap_write(rv3028->regmap, RV3028_OFFSET, offset >> 1);
535 + return regmap_update_bits(rv3028->regmap, RV3028_BACKUP, BIT(7),
539 +static int rv3028_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
541 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
542 + int status, ret = 0;
546 + ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
550 + if (status & RV3028_STATUS_PORF)
551 + dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n");
553 + status &= RV3028_STATUS_PORF;
555 + if (copy_to_user((void __user *)arg, &status, sizeof(int)))
561 + ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
562 + RV3028_STATUS_PORF, 0);
567 + return -ENOIOCTLCMD;
571 +static int rv3028_nvram_write(void *priv, unsigned int offset, void *val,
574 + return regmap_bulk_write(priv, RV3028_RAM1 + offset, val, bytes);
577 +static int rv3028_nvram_read(void *priv, unsigned int offset, void *val,
580 + return regmap_bulk_read(priv, RV3028_RAM1 + offset, val, bytes);
583 +static int rv3028_eeprom_write(void *priv, unsigned int offset, void *val,
590 + ret = regmap_read(priv, RV3028_CTRL1, &ctrl1);
594 + if (!(ctrl1 & RV3028_CTRL1_EERD)) {
595 + ret = regmap_update_bits(priv, RV3028_CTRL1,
596 + RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
600 + ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
601 + !(status & RV3028_STATUS_EEBUSY),
602 + RV3028_EEBUSY_POLL,
603 + RV3028_EEBUSY_TIMEOUT);
608 + for (i = 0; i < bytes; i++) {
609 + ret = regmap_write(priv, RV3028_EEPROM_ADDR, offset + i);
613 + ret = regmap_write(priv, RV3028_EEPROM_DATA, buf[i]);
617 + ret = regmap_write(priv, RV3028_EEPROM_CMD, 0x0);
621 + ret = regmap_write(priv, RV3028_EEPROM_CMD,
622 + RV3028_EEPROM_CMD_WRITE);
626 + usleep_range(RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
628 + ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
629 + !(status & RV3028_STATUS_EEBUSY),
630 + RV3028_EEBUSY_POLL,
631 + RV3028_EEBUSY_TIMEOUT);
637 + if (!(ctrl1 & RV3028_CTRL1_EERD))
639 + err = regmap_update_bits(priv, RV3028_CTRL1, RV3028_CTRL1_EERD,
648 +static int rv3028_eeprom_read(void *priv, unsigned int offset, void *val,
651 + u32 status, ctrl1, data;
655 + ret = regmap_read(priv, RV3028_CTRL1, &ctrl1);
659 + if (!(ctrl1 & RV3028_CTRL1_EERD)) {
660 + ret = regmap_update_bits(priv, RV3028_CTRL1,
661 + RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
665 + ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
666 + !(status & RV3028_STATUS_EEBUSY),
667 + RV3028_EEBUSY_POLL,
668 + RV3028_EEBUSY_TIMEOUT);
673 + for (i = 0; i < bytes; i++) {
674 + ret = regmap_write(priv, RV3028_EEPROM_ADDR, offset + i);
678 + ret = regmap_write(priv, RV3028_EEPROM_CMD, 0x0);
682 + ret = regmap_write(priv, RV3028_EEPROM_CMD,
683 + RV3028_EEPROM_CMD_READ);
687 + ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
688 + !(status & RV3028_STATUS_EEBUSY),
689 + RV3028_EEBUSY_POLL,
690 + RV3028_EEBUSY_TIMEOUT);
694 + ret = regmap_read(priv, RV3028_EEPROM_DATA, &data);
701 + if (!(ctrl1 & RV3028_CTRL1_EERD))
703 + err = regmap_update_bits(priv, RV3028_CTRL1, RV3028_CTRL1_EERD,
712 +static struct rtc_class_ops rv3028_rtc_ops = {
713 + .read_time = rv3028_get_time,
714 + .set_time = rv3028_set_time,
715 + .read_offset = rv3028_read_offset,
716 + .set_offset = rv3028_set_offset,
717 + .ioctl = rv3028_ioctl,
720 +static const struct regmap_config regmap_config = {
723 + .max_register = 0x37,
726 +static int rv3028_probe(struct i2c_client *client)
728 + struct rv3028_data *rv3028;
731 + struct nvmem_config nvmem_cfg = {
732 + .name = "rv3028_nvram",
736 + .type = NVMEM_TYPE_BATTERY_BACKED,
737 + .reg_read = rv3028_nvram_read,
738 + .reg_write = rv3028_nvram_write,
740 + struct nvmem_config eeprom_cfg = {
741 + .name = "rv3028_eeprom",
745 + .type = NVMEM_TYPE_EEPROM,
746 + .reg_read = rv3028_eeprom_read,
747 + .reg_write = rv3028_eeprom_write,
750 + rv3028 = devm_kzalloc(&client->dev, sizeof(struct rv3028_data),
755 + rv3028->regmap = devm_regmap_init_i2c(client, ®map_config);
757 + i2c_set_clientdata(client, rv3028);
759 + ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
763 + if (status & RV3028_STATUS_PORF)
764 + dev_warn(&client->dev, "Voltage low, data loss detected.\n");
766 + if (status & RV3028_STATUS_AF)
767 + dev_warn(&client->dev, "An alarm may have been missed.\n");
769 + rv3028->rtc = devm_rtc_allocate_device(&client->dev);
770 + if (IS_ERR(rv3028->rtc)) {
771 + return PTR_ERR(rv3028->rtc);
774 + if (client->irq > 0) {
775 + ret = devm_request_threaded_irq(&client->dev, client->irq,
776 + NULL, rv3028_handle_irq,
777 + IRQF_TRIGGER_LOW | IRQF_ONESHOT,
780 + dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
783 + rv3028_rtc_ops.read_alarm = rv3028_get_alarm;
784 + rv3028_rtc_ops.set_alarm = rv3028_set_alarm;
785 + rv3028_rtc_ops.alarm_irq_enable = rv3028_alarm_irq_enable;
789 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
790 + RV3028_CTRL1_WADA, RV3028_CTRL1_WADA);
794 + /* setup timestamping */
795 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
796 + RV3028_CTRL2_EIE | RV3028_CTRL2_TSE,
797 + RV3028_CTRL2_EIE | RV3028_CTRL2_TSE);
801 + /* setup trickle charger */
802 + if (!device_property_read_u32(&client->dev, "trickle-resistor-ohms",
806 + for (i = 0; i < ARRAY_SIZE(rv3028_trickle_resistors); i++)
807 + if (ohms == rv3028_trickle_resistors[i])
810 + if (i < ARRAY_SIZE(rv3028_trickle_resistors)) {
811 + ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP,
812 + RV3028_BACKUP_TCE |
813 + RV3028_BACKUP_TCR_MASK,
814 + RV3028_BACKUP_TCE | i);
818 + dev_warn(&client->dev, "invalid trickle resistor value\n");
822 + ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group);
826 + rv3028->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
827 + rv3028->rtc->range_max = RTC_TIMESTAMP_END_2099;
828 + rv3028->rtc->ops = &rv3028_rtc_ops;
829 + ret = rtc_register_device(rv3028->rtc);
833 + nvmem_cfg.priv = rv3028->regmap;
834 + rtc_nvmem_register(rv3028->rtc, &nvmem_cfg);
835 + eeprom_cfg.priv = rv3028->regmap;
836 + rtc_nvmem_register(rv3028->rtc, &eeprom_cfg);
838 + rv3028->rtc->max_user_freq = 1;
843 +static const struct of_device_id rv3028_of_match[] = {
844 + { .compatible = "microcrystal,rv3028", },
847 +MODULE_DEVICE_TABLE(of, rv3028_of_match);
849 +static struct i2c_driver rv3028_driver = {
851 + .name = "rtc-rv3028",
852 + .of_match_table = of_match_ptr(rv3028_of_match),
854 + .probe_new = rv3028_probe,
856 +module_i2c_driver(rv3028_driver);
858 +MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
859 +MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver");
860 +MODULE_LICENSE("GPL v2");