]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/rtc/rtc-rv3028.c
Merge tag 'for-linus-5.4-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / rtc / rtc-rv3028.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * RTC driver for the Micro Crystal RV3028
4  *
5  * Copyright (C) 2019 Micro Crystal SA
6  *
7  * Alexandre Belloni <alexandre.belloni@bootlin.com>
8  *
9  */
10
11 #include <linux/bcd.h>
12 #include <linux/bitops.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/log2.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/regmap.h>
20 #include <linux/rtc.h>
21
22 #define RV3028_SEC                      0x00
23 #define RV3028_MIN                      0x01
24 #define RV3028_HOUR                     0x02
25 #define RV3028_WDAY                     0x03
26 #define RV3028_DAY                      0x04
27 #define RV3028_MONTH                    0x05
28 #define RV3028_YEAR                     0x06
29 #define RV3028_ALARM_MIN                0x07
30 #define RV3028_ALARM_HOUR               0x08
31 #define RV3028_ALARM_DAY                0x09
32 #define RV3028_STATUS                   0x0E
33 #define RV3028_CTRL1                    0x0F
34 #define RV3028_CTRL2                    0x10
35 #define RV3028_EVT_CTRL                 0x13
36 #define RV3028_TS_COUNT                 0x14
37 #define RV3028_TS_SEC                   0x15
38 #define RV3028_RAM1                     0x1F
39 #define RV3028_EEPROM_ADDR              0x25
40 #define RV3028_EEPROM_DATA              0x26
41 #define RV3028_EEPROM_CMD               0x27
42 #define RV3028_CLKOUT                   0x35
43 #define RV3028_OFFSET                   0x36
44 #define RV3028_BACKUP                   0x37
45
46 #define RV3028_STATUS_PORF              BIT(0)
47 #define RV3028_STATUS_EVF               BIT(1)
48 #define RV3028_STATUS_AF                BIT(2)
49 #define RV3028_STATUS_TF                BIT(3)
50 #define RV3028_STATUS_UF                BIT(4)
51 #define RV3028_STATUS_BSF               BIT(5)
52 #define RV3028_STATUS_CLKF              BIT(6)
53 #define RV3028_STATUS_EEBUSY            BIT(7)
54
55 #define RV3028_CTRL1_EERD               BIT(3)
56 #define RV3028_CTRL1_WADA               BIT(5)
57
58 #define RV3028_CTRL2_RESET              BIT(0)
59 #define RV3028_CTRL2_12_24              BIT(1)
60 #define RV3028_CTRL2_EIE                BIT(2)
61 #define RV3028_CTRL2_AIE                BIT(3)
62 #define RV3028_CTRL2_TIE                BIT(4)
63 #define RV3028_CTRL2_UIE                BIT(5)
64 #define RV3028_CTRL2_TSE                BIT(7)
65
66 #define RV3028_EVT_CTRL_TSR             BIT(2)
67
68 #define RV3028_EEPROM_CMD_WRITE         0x21
69 #define RV3028_EEPROM_CMD_READ          0x22
70
71 #define RV3028_EEBUSY_POLL              10000
72 #define RV3028_EEBUSY_TIMEOUT           100000
73
74 #define RV3028_BACKUP_TCE               BIT(5)
75 #define RV3028_BACKUP_TCR_MASK          GENMASK(1,0)
76
77 #define OFFSET_STEP_PPT                 953674
78
79 enum rv3028_type {
80         rv_3028,
81 };
82
83 struct rv3028_data {
84         struct regmap *regmap;
85         struct rtc_device *rtc;
86         enum rv3028_type type;
87 };
88
89 static u16 rv3028_trickle_resistors[] = {1000, 3000, 6000, 11000};
90
91 static ssize_t timestamp0_store(struct device *dev,
92                                 struct device_attribute *attr,
93                                 const char *buf, size_t count)
94 {
95         struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
96
97         regmap_update_bits(rv3028->regmap, RV3028_EVT_CTRL, RV3028_EVT_CTRL_TSR,
98                            RV3028_EVT_CTRL_TSR);
99
100         return count;
101 };
102
103 static ssize_t timestamp0_show(struct device *dev,
104                                struct device_attribute *attr, char *buf)
105 {
106         struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
107         struct rtc_time tm;
108         int ret, count;
109         u8 date[6];
110
111         ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
112         if (ret)
113                 return ret;
114
115         if (!count)
116                 return 0;
117
118         ret = regmap_bulk_read(rv3028->regmap, RV3028_TS_SEC, date,
119                                sizeof(date));
120         if (ret)
121                 return ret;
122
123         tm.tm_sec = bcd2bin(date[0]);
124         tm.tm_min = bcd2bin(date[1]);
125         tm.tm_hour = bcd2bin(date[2]);
126         tm.tm_mday = bcd2bin(date[3]);
127         tm.tm_mon = bcd2bin(date[4]) - 1;
128         tm.tm_year = bcd2bin(date[5]) + 100;
129
130         ret = rtc_valid_tm(&tm);
131         if (ret)
132                 return ret;
133
134         return sprintf(buf, "%llu\n",
135                        (unsigned long long)rtc_tm_to_time64(&tm));
136 };
137
138 static DEVICE_ATTR_RW(timestamp0);
139
140 static ssize_t timestamp0_count_show(struct device *dev,
141                                      struct device_attribute *attr, char *buf)
142 {
143         struct rv3028_data *rv3028 = dev_get_drvdata(dev->parent);
144         int ret, count;
145
146         ret = regmap_read(rv3028->regmap, RV3028_TS_COUNT, &count);
147         if (ret)
148                 return ret;
149
150         return sprintf(buf, "%u\n", count);
151 };
152
153 static DEVICE_ATTR_RO(timestamp0_count);
154
155 static struct attribute *rv3028_attrs[] = {
156         &dev_attr_timestamp0.attr,
157         &dev_attr_timestamp0_count.attr,
158         NULL
159 };
160
161 static const struct attribute_group rv3028_attr_group = {
162         .attrs  = rv3028_attrs,
163 };
164
165 static irqreturn_t rv3028_handle_irq(int irq, void *dev_id)
166 {
167         struct rv3028_data *rv3028 = dev_id;
168         unsigned long events = 0;
169         u32 status = 0, ctrl = 0;
170
171         if (regmap_read(rv3028->regmap, RV3028_STATUS, &status) < 0 ||
172            status == 0) {
173                 return IRQ_NONE;
174         }
175
176         if (status & RV3028_STATUS_PORF)
177                 dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n");
178
179         if (status & RV3028_STATUS_TF) {
180                 status |= RV3028_STATUS_TF;
181                 ctrl |= RV3028_CTRL2_TIE;
182                 events |= RTC_PF;
183         }
184
185         if (status & RV3028_STATUS_AF) {
186                 status |= RV3028_STATUS_AF;
187                 ctrl |= RV3028_CTRL2_AIE;
188                 events |= RTC_AF;
189         }
190
191         if (status & RV3028_STATUS_UF) {
192                 status |= RV3028_STATUS_UF;
193                 ctrl |= RV3028_CTRL2_UIE;
194                 events |= RTC_UF;
195         }
196
197         if (events) {
198                 rtc_update_irq(rv3028->rtc, 1, events);
199                 regmap_update_bits(rv3028->regmap, RV3028_STATUS, status, 0);
200                 regmap_update_bits(rv3028->regmap, RV3028_CTRL2, ctrl, 0);
201         }
202
203         if (status & RV3028_STATUS_EVF) {
204                 sysfs_notify(&rv3028->rtc->dev.kobj, NULL,
205                              dev_attr_timestamp0.attr.name);
206                 dev_warn(&rv3028->rtc->dev, "event detected");
207         }
208
209         return IRQ_HANDLED;
210 }
211
212 static int rv3028_get_time(struct device *dev, struct rtc_time *tm)
213 {
214         struct rv3028_data *rv3028 = dev_get_drvdata(dev);
215         u8 date[7];
216         int ret, status;
217
218         ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
219         if (ret < 0)
220                 return ret;
221
222         if (status & RV3028_STATUS_PORF) {
223                 dev_warn(dev, "Voltage low, data is invalid.\n");
224                 return -EINVAL;
225         }
226
227         ret = regmap_bulk_read(rv3028->regmap, RV3028_SEC, date, sizeof(date));
228         if (ret)
229                 return ret;
230
231         tm->tm_sec  = bcd2bin(date[RV3028_SEC] & 0x7f);
232         tm->tm_min  = bcd2bin(date[RV3028_MIN] & 0x7f);
233         tm->tm_hour = bcd2bin(date[RV3028_HOUR] & 0x3f);
234         tm->tm_wday = ilog2(date[RV3028_WDAY] & 0x7f);
235         tm->tm_mday = bcd2bin(date[RV3028_DAY] & 0x3f);
236         tm->tm_mon  = bcd2bin(date[RV3028_MONTH] & 0x1f) - 1;
237         tm->tm_year = bcd2bin(date[RV3028_YEAR]) + 100;
238
239         return 0;
240 }
241
242 static int rv3028_set_time(struct device *dev, struct rtc_time *tm)
243 {
244         struct rv3028_data *rv3028 = dev_get_drvdata(dev);
245         u8 date[7];
246         int ret;
247
248         date[RV3028_SEC]   = bin2bcd(tm->tm_sec);
249         date[RV3028_MIN]   = bin2bcd(tm->tm_min);
250         date[RV3028_HOUR]  = bin2bcd(tm->tm_hour);
251         date[RV3028_WDAY]  = 1 << (tm->tm_wday);
252         date[RV3028_DAY]   = bin2bcd(tm->tm_mday);
253         date[RV3028_MONTH] = bin2bcd(tm->tm_mon + 1);
254         date[RV3028_YEAR]  = bin2bcd(tm->tm_year - 100);
255
256         /*
257          * Writing to the Seconds register has the same effect as setting RESET
258          * bit to 1
259          */
260         ret = regmap_bulk_write(rv3028->regmap, RV3028_SEC, date,
261                                 sizeof(date));
262         if (ret)
263                 return ret;
264
265         ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
266                                  RV3028_STATUS_PORF, 0);
267
268         return ret;
269 }
270
271 static int rv3028_get_alarm(struct device *dev, struct rtc_wkalrm *alrm)
272 {
273         struct rv3028_data *rv3028 = dev_get_drvdata(dev);
274         u8 alarmvals[3];
275         int status, ctrl, ret;
276
277         ret = regmap_bulk_read(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
278                                sizeof(alarmvals));
279         if (ret)
280                 return ret;
281
282         ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
283         if (ret < 0)
284                 return ret;
285
286         ret = regmap_read(rv3028->regmap, RV3028_CTRL2, &ctrl);
287         if (ret < 0)
288                 return ret;
289
290         alrm->time.tm_sec  = 0;
291         alrm->time.tm_min  = bcd2bin(alarmvals[0] & 0x7f);
292         alrm->time.tm_hour = bcd2bin(alarmvals[1] & 0x3f);
293         alrm->time.tm_mday = bcd2bin(alarmvals[2] & 0x3f);
294
295         alrm->enabled = !!(ctrl & RV3028_CTRL2_AIE);
296         alrm->pending = (status & RV3028_STATUS_AF) && alrm->enabled;
297
298         return 0;
299 }
300
301 static int rv3028_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
302 {
303         struct rv3028_data *rv3028 = dev_get_drvdata(dev);
304         u8 alarmvals[3];
305         u8 ctrl = 0;
306         int ret;
307
308         /* The alarm has no seconds, round up to nearest minute */
309         if (alrm->time.tm_sec) {
310                 time64_t alarm_time = rtc_tm_to_time64(&alrm->time);
311
312                 alarm_time += 60 - alrm->time.tm_sec;
313                 rtc_time64_to_tm(alarm_time, &alrm->time);
314         }
315
316         ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
317                                  RV3028_CTRL2_AIE | RV3028_CTRL2_UIE, 0);
318         if (ret)
319                 return ret;
320
321         alarmvals[0] = bin2bcd(alrm->time.tm_min);
322         alarmvals[1] = bin2bcd(alrm->time.tm_hour);
323         alarmvals[2] = bin2bcd(alrm->time.tm_mday);
324
325         ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
326                                  RV3028_STATUS_AF, 0);
327         if (ret)
328                 return ret;
329
330         ret = regmap_bulk_write(rv3028->regmap, RV3028_ALARM_MIN, alarmvals,
331                                 sizeof(alarmvals));
332         if (ret)
333                 return ret;
334
335         if (alrm->enabled) {
336                 if (rv3028->rtc->uie_rtctimer.enabled)
337                         ctrl |= RV3028_CTRL2_UIE;
338                 if (rv3028->rtc->aie_timer.enabled)
339                         ctrl |= RV3028_CTRL2_AIE;
340         }
341
342         ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
343                                  RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
344
345         return ret;
346 }
347
348 static int rv3028_alarm_irq_enable(struct device *dev, unsigned int enabled)
349 {
350         struct rv3028_data *rv3028 = dev_get_drvdata(dev);
351         int ctrl = 0, ret;
352
353         if (enabled) {
354                 if (rv3028->rtc->uie_rtctimer.enabled)
355                         ctrl |= RV3028_CTRL2_UIE;
356                 if (rv3028->rtc->aie_timer.enabled)
357                         ctrl |= RV3028_CTRL2_AIE;
358         }
359
360         ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
361                                  RV3028_STATUS_AF | RV3028_STATUS_UF, 0);
362         if (ret)
363                 return ret;
364
365         ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
366                                  RV3028_CTRL2_UIE | RV3028_CTRL2_AIE, ctrl);
367         if (ret)
368                 return ret;
369
370         return 0;
371 }
372
373 static int rv3028_read_offset(struct device *dev, long *offset)
374 {
375         struct rv3028_data *rv3028 = dev_get_drvdata(dev);
376         int ret, value, steps;
377
378         ret = regmap_read(rv3028->regmap, RV3028_OFFSET, &value);
379         if (ret < 0)
380                 return ret;
381
382         steps = sign_extend32(value << 1, 8);
383
384         ret = regmap_read(rv3028->regmap, RV3028_BACKUP, &value);
385         if (ret < 0)
386                 return ret;
387
388         steps += value >> 7;
389
390         *offset = DIV_ROUND_CLOSEST(steps * OFFSET_STEP_PPT, 1000);
391
392         return 0;
393 }
394
395 static int rv3028_set_offset(struct device *dev, long offset)
396 {
397         struct rv3028_data *rv3028 = dev_get_drvdata(dev);
398         int ret;
399
400         offset = clamp(offset, -244141L, 243187L) * 1000;
401         offset = DIV_ROUND_CLOSEST(offset, OFFSET_STEP_PPT);
402
403         ret = regmap_write(rv3028->regmap, RV3028_OFFSET, offset >> 1);
404         if (ret < 0)
405                 return ret;
406
407         return regmap_update_bits(rv3028->regmap, RV3028_BACKUP, BIT(7),
408                                   offset << 7);
409 }
410
411 static int rv3028_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
412 {
413         struct rv3028_data *rv3028 = dev_get_drvdata(dev);
414         int status, ret = 0;
415
416         switch (cmd) {
417         case RTC_VL_READ:
418                 ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
419                 if (ret < 0)
420                         return ret;
421
422                 if (status & RV3028_STATUS_PORF)
423                         dev_warn(&rv3028->rtc->dev, "Voltage low, data loss detected.\n");
424
425                 status &= RV3028_STATUS_PORF;
426
427                 if (copy_to_user((void __user *)arg, &status, sizeof(int)))
428                         return -EFAULT;
429
430                 return 0;
431
432         case RTC_VL_CLR:
433                 ret = regmap_update_bits(rv3028->regmap, RV3028_STATUS,
434                                          RV3028_STATUS_PORF, 0);
435
436                 return ret;
437
438         default:
439                 return -ENOIOCTLCMD;
440         }
441 }
442
443 static int rv3028_nvram_write(void *priv, unsigned int offset, void *val,
444                               size_t bytes)
445 {
446         return regmap_bulk_write(priv, RV3028_RAM1 + offset, val, bytes);
447 }
448
449 static int rv3028_nvram_read(void *priv, unsigned int offset, void *val,
450                              size_t bytes)
451 {
452         return regmap_bulk_read(priv, RV3028_RAM1 + offset, val, bytes);
453 }
454
455 static int rv3028_eeprom_write(void *priv, unsigned int offset, void *val,
456                                size_t bytes)
457 {
458         u32 status, ctrl1;
459         int i, ret, err;
460         u8 *buf = val;
461
462         ret = regmap_read(priv, RV3028_CTRL1, &ctrl1);
463         if (ret)
464                 return ret;
465
466         if (!(ctrl1 & RV3028_CTRL1_EERD)) {
467                 ret = regmap_update_bits(priv, RV3028_CTRL1,
468                                          RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
469                 if (ret)
470                         return ret;
471
472                 ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
473                                                !(status & RV3028_STATUS_EEBUSY),
474                                                RV3028_EEBUSY_POLL,
475                                                RV3028_EEBUSY_TIMEOUT);
476                 if (ret)
477                         goto restore_eerd;
478         }
479
480         for (i = 0; i < bytes; i++) {
481                 ret = regmap_write(priv, RV3028_EEPROM_ADDR, offset + i);
482                 if (ret)
483                         goto restore_eerd;
484
485                 ret = regmap_write(priv, RV3028_EEPROM_DATA, buf[i]);
486                 if (ret)
487                         goto restore_eerd;
488
489                 ret = regmap_write(priv, RV3028_EEPROM_CMD, 0x0);
490                 if (ret)
491                         goto restore_eerd;
492
493                 ret = regmap_write(priv, RV3028_EEPROM_CMD,
494                                    RV3028_EEPROM_CMD_WRITE);
495                 if (ret)
496                         goto restore_eerd;
497
498                 usleep_range(RV3028_EEBUSY_POLL, RV3028_EEBUSY_TIMEOUT);
499
500                 ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
501                                                !(status & RV3028_STATUS_EEBUSY),
502                                                RV3028_EEBUSY_POLL,
503                                                RV3028_EEBUSY_TIMEOUT);
504                 if (ret)
505                         goto restore_eerd;
506         }
507
508 restore_eerd:
509         if (!(ctrl1 & RV3028_CTRL1_EERD))
510         {
511                 err = regmap_update_bits(priv, RV3028_CTRL1, RV3028_CTRL1_EERD,
512                                          0);
513                 if (err && !ret)
514                         ret = err;
515         }
516
517         return ret;
518 }
519
520 static int rv3028_eeprom_read(void *priv, unsigned int offset, void *val,
521                               size_t bytes)
522 {
523         u32 status, ctrl1, data;
524         int i, ret, err;
525         u8 *buf = val;
526
527         ret = regmap_read(priv, RV3028_CTRL1, &ctrl1);
528         if (ret)
529                 return ret;
530
531         if (!(ctrl1 & RV3028_CTRL1_EERD)) {
532                 ret = regmap_update_bits(priv, RV3028_CTRL1,
533                                          RV3028_CTRL1_EERD, RV3028_CTRL1_EERD);
534                 if (ret)
535                         return ret;
536
537                 ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
538                                                !(status & RV3028_STATUS_EEBUSY),
539                                                RV3028_EEBUSY_POLL,
540                                                RV3028_EEBUSY_TIMEOUT);
541                 if (ret)
542                         goto restore_eerd;
543         }
544
545         for (i = 0; i < bytes; i++) {
546                 ret = regmap_write(priv, RV3028_EEPROM_ADDR, offset + i);
547                 if (ret)
548                         goto restore_eerd;
549
550                 ret = regmap_write(priv, RV3028_EEPROM_CMD, 0x0);
551                 if (ret)
552                         goto restore_eerd;
553
554                 ret = regmap_write(priv, RV3028_EEPROM_CMD,
555                                    RV3028_EEPROM_CMD_READ);
556                 if (ret)
557                         goto restore_eerd;
558
559                 ret = regmap_read_poll_timeout(priv, RV3028_STATUS, status,
560                                                !(status & RV3028_STATUS_EEBUSY),
561                                                RV3028_EEBUSY_POLL,
562                                                RV3028_EEBUSY_TIMEOUT);
563                 if (ret)
564                         goto restore_eerd;
565
566                 ret = regmap_read(priv, RV3028_EEPROM_DATA, &data);
567                 if (ret)
568                         goto restore_eerd;
569                 buf[i] = data;
570         }
571
572 restore_eerd:
573         if (!(ctrl1 & RV3028_CTRL1_EERD))
574         {
575                 err = regmap_update_bits(priv, RV3028_CTRL1, RV3028_CTRL1_EERD,
576                                          0);
577                 if (err && !ret)
578                         ret = err;
579         }
580
581         return ret;
582 }
583
584 static struct rtc_class_ops rv3028_rtc_ops = {
585         .read_time = rv3028_get_time,
586         .set_time = rv3028_set_time,
587         .read_offset = rv3028_read_offset,
588         .set_offset = rv3028_set_offset,
589         .ioctl = rv3028_ioctl,
590 };
591
592 static const struct regmap_config regmap_config = {
593         .reg_bits = 8,
594         .val_bits = 8,
595         .max_register = 0x37,
596 };
597
598 static int rv3028_probe(struct i2c_client *client)
599 {
600         struct rv3028_data *rv3028;
601         int ret, status;
602         u32 ohms;
603         struct nvmem_config nvmem_cfg = {
604                 .name = "rv3028_nvram",
605                 .word_size = 1,
606                 .stride = 1,
607                 .size = 2,
608                 .type = NVMEM_TYPE_BATTERY_BACKED,
609                 .reg_read = rv3028_nvram_read,
610                 .reg_write = rv3028_nvram_write,
611         };
612         struct nvmem_config eeprom_cfg = {
613                 .name = "rv3028_eeprom",
614                 .word_size = 1,
615                 .stride = 1,
616                 .size = 43,
617                 .type = NVMEM_TYPE_EEPROM,
618                 .reg_read = rv3028_eeprom_read,
619                 .reg_write = rv3028_eeprom_write,
620         };
621
622         rv3028 = devm_kzalloc(&client->dev, sizeof(struct rv3028_data),
623                               GFP_KERNEL);
624         if (!rv3028)
625                 return -ENOMEM;
626
627         rv3028->regmap = devm_regmap_init_i2c(client, &regmap_config);
628
629         i2c_set_clientdata(client, rv3028);
630
631         ret = regmap_read(rv3028->regmap, RV3028_STATUS, &status);
632         if (ret < 0)
633                 return ret;
634
635         if (status & RV3028_STATUS_PORF)
636                 dev_warn(&client->dev, "Voltage low, data loss detected.\n");
637
638         if (status & RV3028_STATUS_AF)
639                 dev_warn(&client->dev, "An alarm may have been missed.\n");
640
641         rv3028->rtc = devm_rtc_allocate_device(&client->dev);
642         if (IS_ERR(rv3028->rtc))
643                 return PTR_ERR(rv3028->rtc);
644
645         if (client->irq > 0) {
646                 ret = devm_request_threaded_irq(&client->dev, client->irq,
647                                                 NULL, rv3028_handle_irq,
648                                                 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
649                                                 "rv3028", rv3028);
650                 if (ret) {
651                         dev_warn(&client->dev, "unable to request IRQ, alarms disabled\n");
652                         client->irq = 0;
653                 } else {
654                         rv3028_rtc_ops.read_alarm = rv3028_get_alarm;
655                         rv3028_rtc_ops.set_alarm = rv3028_set_alarm;
656                         rv3028_rtc_ops.alarm_irq_enable = rv3028_alarm_irq_enable;
657                 }
658         }
659
660         ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL1,
661                                  RV3028_CTRL1_WADA, RV3028_CTRL1_WADA);
662         if (ret)
663                 return ret;
664
665         /* setup timestamping */
666         ret = regmap_update_bits(rv3028->regmap, RV3028_CTRL2,
667                                  RV3028_CTRL2_EIE | RV3028_CTRL2_TSE,
668                                  RV3028_CTRL2_EIE | RV3028_CTRL2_TSE);
669         if (ret)
670                 return ret;
671
672         /* setup trickle charger */
673         if (!device_property_read_u32(&client->dev, "trickle-resistor-ohms",
674                                       &ohms)) {
675                 int i;
676
677                 for (i = 0; i < ARRAY_SIZE(rv3028_trickle_resistors); i++)
678                         if (ohms == rv3028_trickle_resistors[i])
679                                 break;
680
681                 if (i < ARRAY_SIZE(rv3028_trickle_resistors)) {
682                         ret = regmap_update_bits(rv3028->regmap, RV3028_BACKUP,
683                                                  RV3028_BACKUP_TCE |
684                                                  RV3028_BACKUP_TCR_MASK,
685                                                  RV3028_BACKUP_TCE | i);
686                         if (ret)
687                                 return ret;
688                 } else {
689                         dev_warn(&client->dev, "invalid trickle resistor value\n");
690                 }
691         }
692
693         ret = rtc_add_group(rv3028->rtc, &rv3028_attr_group);
694         if (ret)
695                 return ret;
696
697         rv3028->rtc->range_min = RTC_TIMESTAMP_BEGIN_2000;
698         rv3028->rtc->range_max = RTC_TIMESTAMP_END_2099;
699         rv3028->rtc->ops = &rv3028_rtc_ops;
700         ret = rtc_register_device(rv3028->rtc);
701         if (ret)
702                 return ret;
703
704         nvmem_cfg.priv = rv3028->regmap;
705         rtc_nvmem_register(rv3028->rtc, &nvmem_cfg);
706         eeprom_cfg.priv = rv3028->regmap;
707         rtc_nvmem_register(rv3028->rtc, &eeprom_cfg);
708
709         rv3028->rtc->max_user_freq = 1;
710
711         return 0;
712 }
713
714 static const struct of_device_id rv3028_of_match[] = {
715         { .compatible = "microcrystal,rv3028", },
716         { }
717 };
718 MODULE_DEVICE_TABLE(of, rv3028_of_match);
719
720 static struct i2c_driver rv3028_driver = {
721         .driver = {
722                 .name = "rtc-rv3028",
723                 .of_match_table = of_match_ptr(rv3028_of_match),
724         },
725         .probe_new      = rv3028_probe,
726 };
727 module_i2c_driver(rv3028_driver);
728
729 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
730 MODULE_DESCRIPTION("Micro Crystal RV3028 RTC driver");
731 MODULE_LICENSE("GPL v2");