kernel: bump 4.19 to 4.19.115
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-4.19 / 950-0339-rtc-rv3028-add-new-driver.patch
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
5
6 upstream commit e6e7376cfd7b3f9b63de3a22792f64d9bfb2ab53.
7
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.
11
12 The driver handles date/time, alarms, trickle charging, timestamping,
13 frequency offset correction, EEPROM and NVRAM.
14
15 Signed-off-by: Alexandre Belloni <alexandre.belloni@bootlin.com>
16 ---
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
24
25 --- /dev/null
26 +++ b/Documentation/devicetree/bindings/rtc/rtc.txt
27 @@ -0,0 +1,69 @@
28 +Generic device tree bindings for Real Time Clock devices
29 +========================================================
30 +
31 +This document describes generic bindings which can be used to describe Real Time
32 +Clock devices in a device tree.
33 +
34 +Required properties
35 +-------------------
36 +
37 +- compatible : name of RTC device following generic names recommended practice.
38 +
39 +For other required properties e.g. to describe register sets,
40 +clocks, etc. check the binding documentation of the specific driver.
41 +
42 +Optional properties
43 +-------------------
44 +
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.
47 +
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
50 +below.
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
55 + disabled
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.
61 +
62 +Trivial RTCs
63 +------------
64 +
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.
68 +
69 +
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.
102
103 +config RTC_DRV_RV3028
104 + tristate "Micro Crystal RV3028"
105 + help
106 + If you say yes here you get support for the Micro Crystal
107 + RV3028.
108 +
109 + This driver can also be built as a module. If so, the module
110 + will be called rtc-rv3028.
111 +
112 config RTC_DRV_RV8803
113 tristate "Micro Crystal RV8803, Epson RX8900"
114 help
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
125 --- /dev/null
126 +++ b/drivers/rtc/rtc-rv3028.c
127 @@ -0,0 +1,733 @@
128 +// SPDX-License-Identifier: GPL-2.0
129 +/*
130 + * RTC driver for the Micro Crystal RV3028
131 + *
132 + * Copyright (C) 2019 Micro Crystal SA
133 + *
134 + * Alexandre Belloni <alexandre.belloni@bootlin.com>
135 + *
136 + */
137 +
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"
149 +
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
173 +
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)
182 +
183 +#define RV3028_CTRL1_EERD BIT(3)
184 +#define RV3028_CTRL1_WADA BIT(5)
185 +
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)
193 +
194 +#define RV3028_EVT_CTRL_TSR BIT(2)
195 +
196 +#define RV3028_EEPROM_CMD_WRITE 0x21
197 +#define RV3028_EEPROM_CMD_READ 0x22
198 +
199 +#define RV3028_EEBUSY_POLL 10000
200 +#define RV3028_EEBUSY_TIMEOUT 100000
201 +
202 +#define RV3028_BACKUP_TCE BIT(5)
203 +#define RV3028_BACKUP_TCR_MASK GENMASK(1,0)
204 +
205 +#define OFFSET_STEP_PPT 953674
206 +
207 +enum rv3028_type {
208 + rv_3028,
209 +};
210 +
211 +struct rv3028_data {
212 + struct regmap *regmap;
213 + struct rtc_device *rtc;
214 + enum rv3028_type type;
215 +};
216 +
217 +static u16 rv3028_trickle_resistors[] = {1000, 3000, 6000, 11000};
218 +
219 +static ssize_t timestamp0_store(struct device *dev,
220 + struct device_attribute *attr,
221 + const char *buf, size_t count)
222 +{
223 + struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
224 +
225 + regmap_update_bits(rv3028->regmap, RV3028_EVT_CTRL, RV3028_EVT_CTRL_TSR,
226 + RV3028_EVT_CTRL_TSR);
227 +
228 + return count;
229 +};
230 +
231 +static ssize_t timestamp0_show(struct device *dev,
232 + struct device_attribute *attr, char *buf)
233 +{
234 + struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
235 + struct rtc_time tm;
236 + int ret, count;
237 + u8 date[6];
238 +
239 + ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
240 + if (ret)
241 + return ret;
242 +
243 + if (!count)
244 + return 0;
245 +
246 + ret = regmap_bulk_read(rv3028->regmap, RV3028_TS_SEC, date,
247 + sizeof(date));
248 + if (ret)
249 + return ret;
250 +
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;
257 +
258 + ret = rtc_valid_tm(&tm);
259 + if (ret)
260 + return ret;
261 +
262 + return sprintf(buf, "%llu\n",
263 + (unsigned long long)rtc_tm_to_time64(&tm));
264 +};
265 +
266 +static DEVICE_ATTR_RW(timestamp0);
267 +
268 +static ssize_t timestamp0_count_show(struct device *dev,
269 + struct device_attribute *attr, char *buf)
270 +{
271 + struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
272 + int ret, count;
273 +
274 + ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
275 + if (ret)
276 + return ret;
277 +
278 + return sprintf(buf, "%u\n", count);
279 +};
280 +
281 +static DEVICE_ATTR_RO(timestamp0_count);
282 +
283 +static struct attribute *rv3028_attrs[] = {
284 + &dev_attr_timestamp0.attr,
285 + &dev_attr_timestamp0_count.attr,
286 + NULL
287 +};
288 +
289 +static const struct attribute_group rv3028_attr_group = {
290 + .attrs = rv3028_attrs,
291 +};
292 +
293 +static irqreturn_t rv3028_handle_irq(int irq, void *dev_id)
294 +{
295 + struct rv3028_data *rv3028 = dev_id;
296 + unsigned long events = 0;
297 + u32 status = 0, ctrl = 0;
298 +
299 + if (regmap_read(rv3028->regmap, RV3028_STATUS, &status) < 0 ||
300 + status == 0) {
301 + return IRQ_NONE;
302 + }
303 +
304 + if (status & RV3028_STATUS_PORF)
305 + dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n");
306 +
307 + if (status & RV3028_STATUS_TF) {
308 + status |= RV3028_STATUS_TF;
309 + ctrl |= RV3028_CTRL2_TIE;
310 + events |= RTC_PF;
311 + }
312 +
313 + if (status & RV3028_STATUS_AF) {
314 + status |= RV3028_STATUS_AF;
315 + ctrl |= RV3028_CTRL2_AIE;
316 + events |= RTC_AF;
317 + }
318 +
319 + if (status & RV3028_STATUS_UF) {
320 + status |= RV3028_STATUS_UF;
321 + ctrl |= RV3028_CTRL2_UIE;
322 + events |= RTC_UF;
323 + }
324 +
325 + if (events) {
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);
329 + }
330 +
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");
335 + }
336 +
337 + return IRQ_HANDLED;
338 +}
339 +
340 +static int rv3028_get_time(struct device *dev, struct rtc_time *tm)
341 +{
342 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
343 + u8 date[7];
344 + int ret, status;
345 +
346 + ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
347 + if (ret < 0)
348 + return ret;
349 +
350 + if (status & RV3028_STATUS_PORF) {
351 + dev_warn(dev, "Voltage low, data is invalid.\n");
352 + return -EINVAL;
353 + }
354 +
355 + ret = regmap_bulk_read(rv3028->regmap, RV3028_SEC, date, sizeof(date));
356 + if (ret)
357 + return ret;
358 +
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;
366 +
367 + return 0;
368 +}
369 +
370 +static int rv3028_set_time(struct device *dev, struct rtc_time *tm)
371 +{
372 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
373 + u8 date[7];
374 + int ret;
375 +
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);
383 +
384 + /*
385 + * Writing to the Seconds register has the same effect as setting RESET
386 + * bit to 1
387 + */
388 + ret = regmap_bulk_write(rv3028->regmap, RV3028_SEC, date,
389 + sizeof(date));
390 + if (ret)
391 + return ret;
392 +
393 + ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
394 + RV3028_STATUS_PORF, 0);
395 +
396 + return ret;
397 +}
398 +
399 +static int rv3028_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
400 +{
401 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
402 + u8 alarmvals[3];
403 + int status, ctrl, ret;
404 +
405 + ret = regmap_bulk_read(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
406 + sizeof(alarmvals));
407 + if (ret)
408 + return ret;
409 +
410 + ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
411 + if (ret < 0)
412 + return ret;
413 +
414 + ret = regmap_read(rv3028->regmap, RV3028_CTRL2, &ctrl);
415 + if (ret < 0)
416 + return ret;
417 +
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);
422 +
423 + alrm->enabled = !!(ctrl & RV3028_CTRL2_AIE);
424 + alrm->pending = (status & RV3028_STATUS_AF) && alrm->enabled;
425 +
426 + return 0;
427 +}
428 +
429 +static int rv3028_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
430 +{
431 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
432 + u8 alarmvals[3];
433 + u8 ctrl = 0;
434 + int ret;
435 +
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);
439 +
440 + alarm_time += 60 - alrm->time.tm_sec;
441 + rtc_time64_to_tm(alarm_time, &alrm->time);
442 + }
443 +
444 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
445 + RV3028_CTRL2_AIE | RV3028_CTRL2_UIE, 0);
446 + if (ret)
447 + return ret;
448 +
449 + alarmvals[0] = bin2bcd(alrm->time.tm_min);
450 + alarmvals[1] = bin2bcd(alrm->time.tm_hour);
451 + alarmvals[2] = bin2bcd(alrm->time.tm_mday);
452 +
453 + ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
454 + RV3028_STATUS_AF, 0);
455 + if (ret)
456 + return ret;
457 +
458 + ret = regmap_bulk_write(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
459 + sizeof(alarmvals));
460 + if (ret)
461 + return ret;
462 +
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;
468 + }
469 +
470 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
471 + RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
472 +
473 + return ret;
474 +}
475 +
476 +static int rv3028_alarm_irq_enable(struct device *dev, unsigned int enabled)
477 +{
478 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
479 + int ctrl = 0, ret;
480 +
481 + if (enabled) {
482 + if (rv3028->rtc->uie_rtctimer.enabled)
483 + ctrl |= RV3028_CTRL2_UIE;
484 + if (rv3028->rtc->aie_timer.enabled)
485 + ctrl |= RV3028_CTRL2_AIE;
486 + }
487 +
488 + ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
489 + RV3028_STATUS_AF | RV3028_STATUS_UF, 0);
490 + if (ret)
491 + return ret;
492 +
493 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
494 + RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
495 + if (ret)
496 + return ret;
497 +
498 + return 0;
499 +}
500 +
501 +static int rv3028_read_offset(struct device *dev, long *offset)
502 +{
503 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
504 + int ret, value, steps;
505 +
506 + ret = regmap_read(rv3028->regmap, RV3028_OFFSET, &value);
507 + if (ret < 0)
508 + return ret;
509 +
510 + steps = sign_extend32(value << 1, 8);
511 +
512 + ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value);
513 + if (ret < 0)
514 + return ret;
515 +
516 + steps += value >> 7;
517 +
518 + *offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000);
519 +
520 + return 0;
521 +}
522 +
523 +static int rv3028_set_offset(struct device *dev, long offset)
524 +{
525 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
526 + int ret;
527 +
528 + offset = clamp(offset, -244141L, 243187L) * 1000;
529 + offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT);
530 +
531 + ret = regmap_write(rv3028->regmap, RV3028_OFFSET, offset >> 1);
532 + if (ret < 0)
533 + return ret;
534 +
535 + return regmap_update_bits(rv3028->regmap, RV3028_BACKUP, BIT(7),
536 + offset << 7);
537 +}
538 +
539 +static int rv3028_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
540 +{
541 + struct rv3028_data *rv3028 = dev_get_drvdata(dev);
542 + int status, ret = 0;
543 +
544 + switch (cmd) {
545 + case RTC_VL_READ:
546 + ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
547 + if (ret < 0)
548 + return ret;
549 +
550 + if (status & RV3028_STATUS_PORF)
551 + dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n");
552 +
553 + status &= RV3028_STATUS_PORF;
554 +
555 + if (copy_to_user((void __user *)arg, &status, sizeof(int)))
556 + return -EFAULT;
557 +
558 + return 0;
559 +
560 + case RTC_VL_CLR:
561 + ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
562 + RV3028_STATUS_PORF, 0);
563 +
564 + return ret;
565 +
566 + default:
567 + return -ENOIOCTLCMD;
568 + }
569 +}
570 +
571 +static int rv3028_nvram_write(void *priv, unsigned int offset, void *val,
572 + size_t bytes)
573 +{
574 + return regmap_bulk_write(priv, RV3028_RAM1 + offset, val, bytes);
575 +}
576 +
577 +static int rv3028_nvram_read(void *priv, unsigned int offset, void *val,
578 + size_t bytes)
579 +{
580 + return regmap_bulk_read(priv, RV3028_RAM1 + offset, val, bytes);
581 +}
582 +
583 +static int rv3028_eeprom_write(void *priv, unsigned int offset, void *val,
584 + size_t bytes)
585 +{
586 + u32 status, ctrl1;
587 + int i, ret, err;
588 + u8 *buf = val;
589 +
590 + ret = regmap_read(priv, RV3028_CTRL1, &ctrl1);
591 + if (ret)
592 + return ret;
593 +
594 + if (!(ctrl1 & RV3028_CTRL1_EERD)) {
595 + ret = regmap_update_bits(priv, RV3028_CTRL1,
596 + RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
597 + if (ret)
598 + return ret;
599 +
600 + ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
601 + !(status & RV3028_STATUS_EEBUSY),
602 + RV3028_EEBUSY_POLL,
603 + RV3028_EEBUSY_TIMEOUT);
604 + if (ret)
605 + goto restore_eerd;
606 + }
607 +
608 + for (i = 0; i < bytes; i++) {
609 + ret = regmap_write(priv, RV3028_EEPROM_ADDR, offset + i);
610 + if (ret)
611 + goto restore_eerd;
612 +
613 + ret = regmap_write(priv, RV3028_EEPROM_DATA, buf[i]);
614 + if (ret)
615 + goto restore_eerd;
616 +
617 + ret = regmap_write(priv, RV3028_EEPROM_CMD, 0x0);
618 + if (ret)
619 + goto restore_eerd;
620 +
621 + ret = regmap_write(priv, RV3028_EEPROM_CMD,
622 + RV3028_EEPROM_CMD_WRITE);
623 + if (ret)
624 + goto restore_eerd;
625 +
626 + usleep_range(RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
627 +
628 + ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
629 + !(status & RV3028_STATUS_EEBUSY),
630 + RV3028_EEBUSY_POLL,
631 + RV3028_EEBUSY_TIMEOUT);
632 + if (ret)
633 + goto restore_eerd;
634 + }
635 +
636 +restore_eerd:
637 + if (!(ctrl1 & RV3028_CTRL1_EERD))
638 + {
639 + err = regmap_update_bits(priv, RV3028_CTRL1, RV3028_CTRL1_EERD,
640 + 0);
641 + if (err && !ret)
642 + ret = err;
643 + }
644 +
645 + return ret;
646 +}
647 +
648 +static int rv3028_eeprom_read(void *priv, unsigned int offset, void *val,
649 + size_t bytes)
650 +{
651 + u32 status, ctrl1, data;
652 + int i, ret, err;
653 + u8 *buf = val;
654 +
655 + ret = regmap_read(priv, RV3028_CTRL1, &ctrl1);
656 + if (ret)
657 + return ret;
658 +
659 + if (!(ctrl1 & RV3028_CTRL1_EERD)) {
660 + ret = regmap_update_bits(priv, RV3028_CTRL1,
661 + RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
662 + if (ret)
663 + return ret;
664 +
665 + ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
666 + !(status & RV3028_STATUS_EEBUSY),
667 + RV3028_EEBUSY_POLL,
668 + RV3028_EEBUSY_TIMEOUT);
669 + if (ret)
670 + goto restore_eerd;
671 + }
672 +
673 + for (i = 0; i < bytes; i++) {
674 + ret = regmap_write(priv, RV3028_EEPROM_ADDR, offset + i);
675 + if (ret)
676 + goto restore_eerd;
677 +
678 + ret = regmap_write(priv, RV3028_EEPROM_CMD, 0x0);
679 + if (ret)
680 + goto restore_eerd;
681 +
682 + ret = regmap_write(priv, RV3028_EEPROM_CMD,
683 + RV3028_EEPROM_CMD_READ);
684 + if (ret)
685 + goto restore_eerd;
686 +
687 + ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
688 + !(status & RV3028_STATUS_EEBUSY),
689 + RV3028_EEBUSY_POLL,
690 + RV3028_EEBUSY_TIMEOUT);
691 + if (ret)
692 + goto restore_eerd;
693 +
694 + ret = regmap_read(priv, RV3028_EEPROM_DATA, &data);
695 + if (ret)
696 + goto restore_eerd;
697 + buf[i] = data;
698 + }
699 +
700 +restore_eerd:
701 + if (!(ctrl1 & RV3028_CTRL1_EERD))
702 + {
703 + err = regmap_update_bits(priv, RV3028_CTRL1, RV3028_CTRL1_EERD,
704 + 0);
705 + if (err && !ret)
706 + ret = err;
707 + }
708 +
709 + return ret;
710 +}
711 +
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,
718 +};
719 +
720 +static const struct regmap_config regmap_config = {
721 + .reg_bits = 8,
722 + .val_bits = 8,
723 + .max_register = 0x37,
724 +};
725 +
726 +static int rv3028_probe(struct i2c_client *client)
727 +{
728 + struct rv3028_data *rv3028;
729 + int ret, status;
730 + u32 ohms;
731 + struct nvmem_config nvmem_cfg = {
732 + .name = "rv3028_nvram",
733 + .word_size = 1,
734 + .stride = 1,
735 + .size = 2,
736 + .type = NVMEM_TYPE_BATTERY_BACKED,
737 + .reg_read = rv3028_nvram_read,
738 + .reg_write = rv3028_nvram_write,
739 + };
740 + struct nvmem_config eeprom_cfg = {
741 + .name = "rv3028_eeprom",
742 + .word_size = 1,
743 + .stride = 1,
744 + .size = 43,
745 + .type = NVMEM_TYPE_EEPROM,
746 + .reg_read = rv3028_eeprom_read,
747 + .reg_write = rv3028_eeprom_write,
748 + };
749 +
750 + rv3028 = devm_kzalloc(&client->dev, sizeof(struct rv3028_data),
751 + GFP_KERNEL);
752 + if (!rv3028)
753 + return -ENOMEM;
754 +
755 + rv3028->regmap = devm_regmap_init_i2c(client, &regmap_config);
756 +
757 + i2c_set_clientdata(client, rv3028);
758 +
759 + ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
760 + if (ret < 0)
761 + return ret;
762 +
763 + if (status & RV3028_STATUS_PORF)
764 + dev_warn(&client->dev, "Voltage low, data loss detected.\n");
765 +
766 + if (status & RV3028_STATUS_AF)
767 + dev_warn(&client->dev, "An alarm may have been missed.\n");
768 +
769 + rv3028->rtc = devm_rtc_allocate_device(&client->dev);
770 + if (IS_ERR(rv3028->rtc)) {
771 + return PTR_ERR(rv3028->rtc);
772 + }
773 +
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,
778 + "rv3028", rv3028);
779 + if (ret) {
780 + dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
781 + client->irq = 0;
782 + } else {
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;
786 + }
787 + }
788 +
789 + ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
790 + RV3028_CTRL1_WADA, RV3028_CTRL1_WADA);
791 + if (ret)
792 + return ret;
793 +
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);
798 + if (ret)
799 + return ret;
800 +
801 + /* setup trickle charger */
802 + if (!device_property_read_u32(&client->dev, "trickle-resistor-ohms",
803 + &ohms)) {
804 + int i;
805 +
806 + for (i = 0; i < ARRAY_SIZE(rv3028_trickle_resistors); i++)
807 + if (ohms == rv3028_trickle_resistors[i])
808 + break;
809 +
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);
815 + if (ret)
816 + return ret;
817 + } else {
818 + dev_warn(&client->dev, "invalid trickle resistor value\n");
819 + }
820 + }
821 +
822 + ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group);
823 + if (ret)
824 + return ret;
825 +
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);
830 + if (ret)
831 + return ret;
832 +
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);
837 +
838 + rv3028->rtc->max_user_freq = 1;
839 +
840 + return 0;
841 +}
842 +
843 +static const struct of_device_id rv3028_of_match[] = {
844 + { .compatible = "microcrystal,rv3028", },
845 + { }
846 +};
847 +MODULE_DEVICE_TABLE(of, rv3028_of_match);
848 +
849 +static struct i2c_driver rv3028_driver = {
850 + .driver = {
851 + .name = "rtc-rv3028",
852 + .of_match_table = of_match_ptr(rv3028_of_match),
853 + },
854 + .probe_new = rv3028_probe,
855 +};
856 +module_i2c_driver(rv3028_driver);
857 +
858 +MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
859 +MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver");
860 +MODULE_LICENSE("GPL v2");