1 /*
2 * Copyright (c) 2024 ANITRA system s.r.o.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT microcrystal_rv3028
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/drivers/i2c.h>
12 #include <zephyr/drivers/rtc.h>
13 #include <zephyr/logging/log.h>
14 #include <zephyr/sys/util.h>
15 #include "rtc_utils.h"
16
17 LOG_MODULE_REGISTER(rv3028, CONFIG_RTC_LOG_LEVEL);
18
19 /* RV3028 RAM register addresses */
20 #define RV3028_REG_SECONDS 0x00
21 #define RV3028_REG_MINUTES 0x01
22 #define RV3028_REG_HOURS 0x02
23 #define RV3028_REG_WEEKDAY 0x03
24 #define RV3028_REG_DATE 0x04
25 #define RV3028_REG_MONTH 0x05
26 #define RV3028_REG_YEAR 0x06
27 #define RV3028_REG_ALARM_MINUTES 0x07
28 #define RV3028_REG_ALARM_HOURS 0x08
29 #define RV3028_REG_ALARM_WEEKDAY 0x09
30 #define RV3028_REG_STATUS 0x0E
31 #define RV3028_REG_CONTROL1 0x0F
32 #define RV3028_REG_CONTROL2 0x10
33 #define RV3028_REG_EVENT_CONTROL 0x13
34 #define RV3028_REG_TS_COUNT 0x14
35 #define RV3028_REG_TS_SECONDS 0x15
36 #define RV3028_REG_TS_MINUTES 0x16
37 #define RV3028_REG_TS_HOURS 0x17
38 #define RV3028_REG_TS_DATE 0x18
39 #define RV3028_REG_TS_MONTH 0x19
40 #define RV3028_REG_TS_YEAR 0x1A
41 #define RV3028_REG_UNIXTIME0 0x1B
42 #define RV3028_REG_UNIXTIME1 0x1C
43 #define RV3028_REG_UNIXTIME2 0x1D
44 #define RV3028_REG_UNIXTIME3 0x1E
45 #define RV3028_REG_USER_RAM1 0x1F
46 #define RV3028_REG_USER_RAM2 0x20
47 #define RV3028_REG_EEPROM_ADDRESS 0x25
48 #define RV3028_REG_EEPROM_DATA 0x26
49 #define RV3028_REG_EEPROM_COMMAND 0x27
50 #define RV3028_REG_ID 0x28
51 #define RV3028_REG_CLKOUT 0x35
52 #define RV3028_REG_OFFSET 0x36
53 #define RV3028_REG_BACKUP 0x37
54
55 #define RV3028_CONTROL1_TD BIT(0)
56 #define RV3028_CONTROL1_TE GENMASK(2, 1)
57 #define RV3028_CONTROL1_EERD BIT(3)
58 #define RV3028_CONTROL1_USEL BIT(4)
59 #define RV3028_CONTROL1_WADA BIT(5)
60 #define RV3028_CONTROL1_TRPT BIT(7)
61
62 #define RV3028_CONTROL2_RESET BIT(0)
63 #define RV3028_CONTROL2_12_24 BIT(1)
64 #define RV3028_CONTROL2_EIE BIT(2)
65 #define RV3028_CONTROL2_AIE BIT(3)
66 #define RV3028_CONTROL2_TIE BIT(4)
67 #define RV3028_CONTROL2_UIE BIT(5)
68 #define RV3028_CONTROL2_TSE BIT(7)
69
70 #define RV3028_STATUS_PORF BIT(0)
71 #define RV3028_STATUS_EVF BIT(1)
72 #define RV3028_STATUS_AF BIT(2)
73 #define RV3028_STATUS_TF BIT(3)
74 #define RV3028_STATUS_UF BIT(4)
75 #define RV3028_STATUS_BSF BIT(5)
76 #define RV3028_STATUS_CLKF BIT(6)
77 #define RV3028_STATUS_EEBUSY BIT(7)
78
79 #define RV3028_CLKOUT_FD GENMASK(2, 0)
80 #define RV3028_CLKOUT_PORIE BIT(3)
81 #define RV3028_CLKOUT_CLKSY BIT(6)
82 #define RV3028_CLKOUT_CLKOE BIT(7)
83
84 #define RV3028_CLKOUT_FD_LOW 0x7
85
86 #define RV3028_BACKUP_TCE BIT(5)
87 #define RV3028_BACKUP_TCR GENMASK(1, 0)
88 #define RV3028_BACKUP_BSM GENMASK(3, 2)
89
90 #define RV3028_BSM_LEVEL 0x3
91 #define RV3028_BSM_DIRECT 0x1
92 #define RV3028_BSM_DISABLED 0x0
93
94 /* RV3028 EE command register values */
95 #define RV3028_EEPROM_CMD_INIT 0x00
96 #define RV3028_EEPROM_CMD_UPDATE 0x11
97 #define RV3028_EEPROM_CMD_REFRESH 0x12
98 #define RV3028_EEPROM_CMD_WRITE 0x21
99 #define RV3028_EEPROM_CMD_READ 0x22
100
101 #define RV3028_SECONDS_MASK GENMASK(6, 0)
102 #define RV3028_MINUTES_MASK GENMASK(6, 0)
103 #define RV3028_HOURS_AMPM BIT(5)
104 #define RV3028_HOURS_12H_MASK GENMASK(4, 0)
105 #define RV3028_HOURS_24H_MASK GENMASK(5, 0)
106 #define RV3028_DATE_MASK GENMASK(5, 0)
107 #define RV3028_WEEKDAY_MASK GENMASK(2, 0)
108 #define RV3028_MONTH_MASK GENMASK(4, 0)
109 #define RV3028_YEAR_MASK GENMASK(7, 0)
110
111 #define RV3028_ALARM_MINUTES_AE_M BIT(7)
112 #define RV3028_ALARM_MINUTES_MASK GENMASK(6, 0)
113 #define RV3028_ALARM_HOURS_AE_H BIT(7)
114 #define RV3028_ALARM_HOURS_AMPM BIT(5)
115 #define RV3028_ALARM_HOURS_12H_MASK GENMASK(4, 0)
116 #define RV3028_ALARM_HOURS_24H_MASK GENMASK(5, 0)
117 #define RV3028_ALARM_DATE_AE_WD BIT(7)
118 #define RV3028_ALARM_DATE_MASK GENMASK(5, 0)
119
120 /* The RV3028 only supports two-digit years. Leap years are correctly handled from 2000 to 2099 */
121 #define RV3028_YEAR_OFFSET (2000 - 1900)
122
123 /* The RV3028 enumerates months 1 to 12 */
124 #define RV3028_MONTH_OFFSET 1
125
126 #define RV3028_EEBUSY_READ_POLL_MS 1
127 #define RV3028_EEBUSY_WRITE_POLL_MS 10
128 #define RV3028_EEBUSY_TIMEOUT_MS 100
129
130 /* RTC alarm time fields supported by the RV3028 */
131 #define RV3028_RTC_ALARM_TIME_MASK \
132 (RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR | RTC_ALARM_TIME_MASK_MONTHDAY)
133
134 /* RTC time fields supported by the RV3028 */
135 #define RV3028_RTC_TIME_MASK \
136 (RTC_ALARM_TIME_MASK_SECOND | RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR | \
137 RTC_ALARM_TIME_MASK_MONTH | RTC_ALARM_TIME_MASK_MONTHDAY | RTC_ALARM_TIME_MASK_YEAR | \
138 RTC_ALARM_TIME_MASK_WEEKDAY)
139
140 /* Helper macro to guard int-gpios related code */
141 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios) && \
142 (defined(CONFIG_RTC_ALARM) || defined(CONFIG_RTC_UPDATE))
143 #define RV3028_INT_GPIOS_IN_USE 1
144 #endif
145
146 struct rv3028_config {
147 const struct i2c_dt_spec i2c;
148 #ifdef RV3028_INT_GPIOS_IN_USE
149 struct gpio_dt_spec gpio_int;
150 #endif /* RV3028_INT_GPIOS_IN_USE */
151 uint8_t cof;
152 uint8_t backup;
153 };
154
155 struct rv3028_data {
156 struct k_sem lock;
157 #if RV3028_INT_GPIOS_IN_USE
158 const struct device *dev;
159 struct gpio_callback int_callback;
160 struct k_work work;
161
162 #ifdef CONFIG_RTC_ALARM
163 rtc_alarm_callback alarm_callback;
164 void *alarm_user_data;
165 #endif /* CONFIG_RTC_ALARM */
166 #ifdef CONFIG_RTC_UPDATE
167 rtc_update_callback update_callback;
168 void *update_user_data;
169 #endif /* CONFIG_RTC_UPDATE */
170 #endif /* RV3028_INT_GPIOS_IN_USE */
171 };
172
rv3028_lock_sem(const struct device * dev)173 static void rv3028_lock_sem(const struct device *dev)
174 {
175 struct rv3028_data *data = dev->data;
176
177 (void)k_sem_take(&data->lock, K_FOREVER);
178 }
179
rv3028_unlock_sem(const struct device * dev)180 static void rv3028_unlock_sem(const struct device *dev)
181 {
182 struct rv3028_data *data = dev->data;
183
184 k_sem_give(&data->lock);
185 }
186
rv3028_read_regs(const struct device * dev,uint8_t addr,void * buf,size_t len)187 static int rv3028_read_regs(const struct device *dev, uint8_t addr, void *buf, size_t len)
188 {
189 const struct rv3028_config *config = dev->config;
190 int err;
191
192 err = i2c_write_read_dt(&config->i2c, &addr, sizeof(addr), buf, len);
193 if (err) {
194 LOG_ERR("failed to read reg addr 0x%02x, len %d (err %d)", addr, len, err);
195 return err;
196 }
197
198 return 0;
199 }
200
rv3028_read_reg8(const struct device * dev,uint8_t addr,uint8_t * val)201 static int rv3028_read_reg8(const struct device *dev, uint8_t addr, uint8_t *val)
202 {
203 return rv3028_read_regs(dev, addr, val, sizeof(*val));
204 }
205
rv3028_write_regs(const struct device * dev,uint8_t addr,void * buf,size_t len)206 static int rv3028_write_regs(const struct device *dev, uint8_t addr, void *buf, size_t len)
207 {
208 const struct rv3028_config *config = dev->config;
209 uint8_t block[sizeof(addr) + len];
210 int err;
211
212 block[0] = addr;
213 memcpy(&block[1], buf, len);
214
215 err = i2c_write_dt(&config->i2c, block, sizeof(block));
216 if (err) {
217 LOG_ERR("failed to write reg addr 0x%02x, len %d (err %d)", addr, len, err);
218 return err;
219 }
220
221 return 0;
222 }
223
rv3028_write_reg8(const struct device * dev,uint8_t addr,uint8_t val)224 static int rv3028_write_reg8(const struct device *dev, uint8_t addr, uint8_t val)
225 {
226 return rv3028_write_regs(dev, addr, &val, sizeof(val));
227 }
228
rv3028_update_reg8(const struct device * dev,uint8_t addr,uint8_t mask,uint8_t val)229 static int rv3028_update_reg8(const struct device *dev, uint8_t addr, uint8_t mask, uint8_t val)
230 {
231 const struct rv3028_config *config = dev->config;
232 int err;
233
234 err = i2c_reg_update_byte_dt(&config->i2c, addr, mask, val);
235 if (err) {
236 LOG_ERR("failed to update reg addr 0x%02x, mask 0x%02x, val 0x%02x (err %d)", addr,
237 mask, val, err);
238 return err;
239 }
240
241 return 0;
242 }
243
rv3028_eeprom_wait_busy(const struct device * dev,int poll_ms)244 static int rv3028_eeprom_wait_busy(const struct device *dev, int poll_ms)
245 {
246 uint8_t status = 0;
247 int err;
248 int64_t timeout_time = k_uptime_get() + RV3028_EEBUSY_TIMEOUT_MS;
249
250 /* Wait while the EEPROM is busy */
251 for (;;) {
252 err = rv3028_read_reg8(dev, RV3028_REG_STATUS, &status);
253 if (err) {
254 return err;
255 }
256
257 if (!(status & RV3028_STATUS_EEBUSY)) {
258 break;
259 }
260
261 if (k_uptime_get() > timeout_time) {
262 return -ETIME;
263 }
264
265 k_msleep(poll_ms);
266 }
267
268 return 0;
269 }
270
rv3028_exit_eerd(const struct device * dev)271 static int rv3028_exit_eerd(const struct device *dev)
272 {
273 return rv3028_update_reg8(dev, RV3028_REG_CONTROL1, RV3028_CONTROL1_EERD, 0);
274 }
275
rv3028_enter_eerd(const struct device * dev)276 static int rv3028_enter_eerd(const struct device *dev)
277 {
278 uint8_t ctrl1;
279 bool eerd;
280 int ret;
281
282 ret = rv3028_read_reg8(dev, RV3028_REG_CONTROL1, &ctrl1);
283 if (ret) {
284 return ret;
285 }
286
287 eerd = ctrl1 & RV3028_CONTROL1_EERD;
288 if (eerd) {
289 return 0;
290 }
291
292 ret = rv3028_update_reg8(dev, RV3028_REG_CONTROL1, RV3028_CONTROL1_EERD,
293 RV3028_CONTROL1_EERD);
294
295 ret = rv3028_eeprom_wait_busy(dev, RV3028_EEBUSY_WRITE_POLL_MS);
296 if (ret) {
297 rv3028_exit_eerd(dev);
298 return ret;
299 }
300
301 return ret;
302 }
303
rv3028_eeprom_command(const struct device * dev,uint8_t command)304 static int rv3028_eeprom_command(const struct device *dev, uint8_t command)
305 {
306 int err;
307
308 err = rv3028_write_reg8(dev, RV3028_REG_EEPROM_COMMAND, RV3028_EEPROM_CMD_INIT);
309 if (err) {
310 return err;
311 }
312
313 return rv3028_write_reg8(dev, RV3028_REG_EEPROM_COMMAND, command);
314 }
315
rv3028_update(const struct device * dev)316 static int rv3028_update(const struct device *dev)
317 {
318 int err;
319
320 err = rv3028_eeprom_command(dev, RV3028_EEPROM_CMD_UPDATE);
321 if (err) {
322 goto exit_eerd;
323 }
324
325 err = rv3028_eeprom_wait_busy(dev, RV3028_EEBUSY_WRITE_POLL_MS);
326
327 exit_eerd:
328 rv3028_exit_eerd(dev);
329
330 return err;
331 }
332
rv3028_refresh(const struct device * dev)333 static int rv3028_refresh(const struct device *dev)
334 {
335 int err;
336
337 err = rv3028_eeprom_command(dev, RV3028_EEPROM_CMD_REFRESH);
338 if (err) {
339 goto exit_eerd;
340 }
341
342 err = rv3028_eeprom_wait_busy(dev, RV3028_EEBUSY_READ_POLL_MS);
343
344 exit_eerd:
345 rv3028_exit_eerd(dev);
346
347 return err;
348 }
349
rv3028_update_cfg(const struct device * dev,uint8_t addr,uint8_t mask,uint8_t val)350 static int rv3028_update_cfg(const struct device *dev, uint8_t addr, uint8_t mask, uint8_t val)
351 {
352 uint8_t val_old, val_new;
353 int err;
354
355 err = rv3028_read_reg8(dev, addr, &val_old);
356 if (err) {
357 return err;
358 }
359
360 val_new = (val_old & ~mask) | (val & mask);
361 if (val_new == val_old) {
362 return 0;
363 }
364
365 err = rv3028_enter_eerd(dev);
366 if (err) {
367 return err;
368 }
369
370 err = rv3028_write_reg8(dev, addr, val_new);
371 if (err) {
372 rv3028_exit_eerd(dev);
373 return err;
374 }
375
376 return rv3028_update(dev);
377 }
378
379 #if RV3028_INT_GPIOS_IN_USE
380
rv3028_work_cb(struct k_work * work)381 static void rv3028_work_cb(struct k_work *work)
382 {
383 struct rv3028_data *data = CONTAINER_OF(work, struct rv3028_data, work);
384 const struct device *dev = data->dev;
385 rtc_alarm_callback alarm_callback = NULL;
386 void *alarm_user_data = NULL;
387 rtc_update_callback update_callback = NULL;
388 void *update_user_data = NULL;
389 uint8_t status;
390 int err;
391
392 rv3028_lock_sem(dev);
393
394 err = rv3028_read_reg8(data->dev, RV3028_REG_STATUS, &status);
395 if (err) {
396 goto unlock;
397 }
398
399 #ifdef CONFIG_RTC_ALARM
400 if ((status & RV3028_STATUS_AF) && data->alarm_callback != NULL) {
401 status &= ~(RV3028_STATUS_AF);
402 alarm_callback = data->alarm_callback;
403 alarm_user_data = data->alarm_user_data;
404 }
405 #endif /* CONFIG_RTC_ALARM */
406
407 #ifdef CONFIG_RTC_UPDATE
408 if ((status & RV3028_STATUS_UF) && data->update_callback != NULL) {
409 status &= ~(RV3028_STATUS_UF);
410 update_callback = data->update_callback;
411 update_user_data = data->update_user_data;
412 }
413 #endif /* CONFIG_RTC_UPDATE */
414
415 err = rv3028_write_reg8(dev, RV3028_REG_STATUS, status);
416 if (err) {
417 goto unlock;
418 }
419
420 /* Check if interrupt occurred between STATUS read/write */
421 err = rv3028_read_reg8(dev, RV3028_REG_STATUS, &status);
422 if (err) {
423 goto unlock;
424 }
425
426 if (((status & RV3028_STATUS_AF) && alarm_callback != NULL) ||
427 ((status & RV3028_STATUS_UF) && update_callback != NULL)) {
428 /* Another interrupt occurred while servicing this one */
429 k_work_submit(&data->work);
430 }
431
432 unlock:
433 rv3028_unlock_sem(dev);
434
435 if (alarm_callback != NULL) {
436 alarm_callback(dev, 0U, alarm_user_data);
437 alarm_callback = NULL;
438 }
439
440 if (update_callback != NULL) {
441 update_callback(dev, update_user_data);
442 update_callback = NULL;
443 }
444 }
445
rv3028_int_handler(const struct device * port,struct gpio_callback * cb,gpio_port_pins_t pins)446 static void rv3028_int_handler(const struct device *port, struct gpio_callback *cb,
447 gpio_port_pins_t pins)
448 {
449 struct rv3028_data *data = CONTAINER_OF(cb, struct rv3028_data, int_callback);
450
451 ARG_UNUSED(port);
452 ARG_UNUSED(pins);
453
454 k_work_submit(&data->work);
455 }
456
457 #endif /* RV3028_INT_GPIOS_IN_USE */
458
rv3028_set_time(const struct device * dev,const struct rtc_time * timeptr)459 static int rv3028_set_time(const struct device *dev, const struct rtc_time *timeptr)
460 {
461 uint8_t date[7];
462 int err;
463
464 if (timeptr == NULL ||
465 !rtc_utils_validate_rtc_time(timeptr, RV3028_RTC_TIME_MASK) ||
466 (timeptr->tm_year < RV3028_YEAR_OFFSET)) {
467 LOG_ERR("invalid time");
468 return -EINVAL;
469 }
470
471 rv3028_lock_sem(dev);
472
473 LOG_DBG("set time: year = %d, mon = %d, mday = %d, wday = %d, hour = %d, "
474 "min = %d, sec = %d",
475 timeptr->tm_year, timeptr->tm_mon, timeptr->tm_mday, timeptr->tm_wday,
476 timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
477
478 date[0] = bin2bcd(timeptr->tm_sec) & RV3028_SECONDS_MASK;
479 date[1] = bin2bcd(timeptr->tm_min) & RV3028_MINUTES_MASK;
480 date[2] = bin2bcd(timeptr->tm_hour) & RV3028_HOURS_24H_MASK;
481 date[3] = timeptr->tm_wday & RV3028_WEEKDAY_MASK;
482 date[4] = bin2bcd(timeptr->tm_mday) & RV3028_DATE_MASK;
483 date[5] = bin2bcd(timeptr->tm_mon + RV3028_MONTH_OFFSET) & RV3028_MONTH_MASK;
484 date[6] = bin2bcd(timeptr->tm_year - RV3028_YEAR_OFFSET) & RV3028_YEAR_MASK;
485
486 err = rv3028_write_regs(dev, RV3028_REG_SECONDS, &date, sizeof(date));
487 if (err) {
488 goto unlock;
489 }
490
491 /* Clear Power On Reset Flag */
492 err = rv3028_update_reg8(dev, RV3028_REG_STATUS, RV3028_STATUS_PORF, 0);
493
494 unlock:
495 rv3028_unlock_sem(dev);
496
497 return err;
498 }
499
rv3028_get_time(const struct device * dev,struct rtc_time * timeptr)500 static int rv3028_get_time(const struct device *dev, struct rtc_time *timeptr)
501 {
502 uint8_t status;
503 uint8_t date[7];
504 int err;
505
506 if (timeptr == NULL) {
507 return -EINVAL;
508 }
509
510 err = rv3028_read_reg8(dev, RV3028_REG_STATUS, &status);
511 if (err) {
512 return err;
513 }
514
515 if (status & RV3028_STATUS_PORF) {
516 /* Power On Reset Flag indicates invalid data */
517 return -ENODATA;
518 }
519
520 err = rv3028_read_regs(dev, RV3028_REG_SECONDS, date, sizeof(date));
521 if (err) {
522 return err;
523 }
524
525 memset(timeptr, 0U, sizeof(*timeptr));
526 timeptr->tm_sec = bcd2bin(date[0] & RV3028_SECONDS_MASK);
527 timeptr->tm_min = bcd2bin(date[1] & RV3028_MINUTES_MASK);
528 timeptr->tm_hour = bcd2bin(date[2] & RV3028_HOURS_24H_MASK);
529 timeptr->tm_wday = date[3] & RV3028_WEEKDAY_MASK;
530 timeptr->tm_mday = bcd2bin(date[4] & RV3028_DATE_MASK);
531 timeptr->tm_mon = bcd2bin(date[5] & RV3028_MONTH_MASK) - RV3028_MONTH_OFFSET;
532 timeptr->tm_year = bcd2bin(date[6] & RV3028_YEAR_MASK) + RV3028_YEAR_OFFSET;
533 timeptr->tm_yday = -1;
534 timeptr->tm_isdst = -1;
535
536 LOG_DBG("get time: year = %d, mon = %d, mday = %d, wday = %d, hour = %d, "
537 "min = %d, sec = %d",
538 timeptr->tm_year, timeptr->tm_mon, timeptr->tm_mday, timeptr->tm_wday,
539 timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
540
541 return 0;
542 }
543
544 #ifdef CONFIG_RTC_ALARM
545
rv3028_alarm_get_supported_fields(const struct device * dev,uint16_t id,uint16_t * mask)546 static int rv3028_alarm_get_supported_fields(const struct device *dev, uint16_t id, uint16_t *mask)
547 {
548 ARG_UNUSED(dev);
549
550 if (id != 0U) {
551 LOG_ERR("invalid alarm ID %d", id);
552 return -EINVAL;
553 }
554
555 *mask = RV3028_RTC_ALARM_TIME_MASK;
556
557 return 0;
558 }
559
rv3028_alarm_set_time(const struct device * dev,uint16_t id,uint16_t mask,const struct rtc_time * timeptr)560 static int rv3028_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
561 const struct rtc_time *timeptr)
562 {
563 uint8_t regs[3];
564
565 if (id != 0U) {
566 LOG_ERR("invalid alarm ID %d", id);
567 return -EINVAL;
568 }
569
570 if (mask & ~(RV3028_RTC_ALARM_TIME_MASK)) {
571 LOG_ERR("unsupported alarm field mask 0x%04x", mask);
572 return -EINVAL;
573 }
574
575 if (!rtc_utils_validate_rtc_time(timeptr, mask)) {
576 LOG_ERR("invalid alarm time");
577 return -EINVAL;
578 }
579
580 if (mask & RTC_ALARM_TIME_MASK_MINUTE) {
581 regs[0] = bin2bcd(timeptr->tm_min) & RV3028_ALARM_MINUTES_MASK;
582 } else {
583 regs[0] = RV3028_ALARM_MINUTES_AE_M;
584 }
585
586 if (mask & RTC_ALARM_TIME_MASK_HOUR) {
587 regs[1] = bin2bcd(timeptr->tm_hour) & RV3028_ALARM_HOURS_24H_MASK;
588 } else {
589 regs[1] = RV3028_ALARM_HOURS_AE_H;
590 }
591
592 if (mask & RTC_ALARM_TIME_MASK_MONTHDAY) {
593 regs[2] = bin2bcd(timeptr->tm_mday) & RV3028_ALARM_DATE_MASK;
594 } else {
595 regs[2] = RV3028_ALARM_DATE_AE_WD;
596 }
597
598 LOG_DBG("set alarm: mday = %d, hour = %d, min = %d, mask = 0x%04x",
599 timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, mask);
600
601 /* Write registers RV3028_REG_ALARM_MINUTES through RV3028_REG_ALARM_WEEKDAY */
602 return rv3028_write_regs(dev, RV3028_REG_ALARM_MINUTES, ®s, sizeof(regs));
603 }
604
rv3028_alarm_get_time(const struct device * dev,uint16_t id,uint16_t * mask,struct rtc_time * timeptr)605 static int rv3028_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
606 struct rtc_time *timeptr)
607 {
608 uint8_t regs[3];
609 int err;
610
611 if (id != 0U) {
612 LOG_ERR("invalid alarm ID %d", id);
613 return -EINVAL;
614 }
615
616 /* Read registers RV3028_REG_ALARM_MINUTES through RV3028_REG_ALARM_WEEKDAY */
617 err = rv3028_read_regs(dev, RV3028_REG_ALARM_MINUTES, ®s, sizeof(regs));
618 if (err) {
619 return err;
620 }
621
622 memset(timeptr, 0U, sizeof(*timeptr));
623 *mask = 0U;
624
625 if ((regs[0] & RV3028_ALARM_MINUTES_AE_M) == 0) {
626 timeptr->tm_min = bcd2bin(regs[0] & RV3028_ALARM_MINUTES_MASK);
627 *mask |= RTC_ALARM_TIME_MASK_MINUTE;
628 }
629
630 if ((regs[1] & RV3028_ALARM_HOURS_AE_H) == 0) {
631 timeptr->tm_hour = bcd2bin(regs[1] & RV3028_ALARM_HOURS_24H_MASK);
632 *mask |= RTC_ALARM_TIME_MASK_HOUR;
633 }
634
635 if ((regs[2] & RV3028_ALARM_DATE_AE_WD) == 0) {
636 timeptr->tm_mday = bcd2bin(regs[2] & RV3028_ALARM_DATE_MASK);
637 *mask |= RTC_ALARM_TIME_MASK_MONTHDAY;
638 }
639
640 LOG_DBG("get alarm: mday = %d, hour = %d, min = %d, mask = 0x%04x",
641 timeptr->tm_mday, timeptr->tm_hour, timeptr->tm_min, *mask);
642
643 return 0;
644 }
645
rv3028_alarm_is_pending(const struct device * dev,uint16_t id)646 static int rv3028_alarm_is_pending(const struct device *dev, uint16_t id)
647 {
648 uint8_t status;
649 int err;
650
651 if (id != 0U) {
652 LOG_ERR("invalid alarm ID %d", id);
653 return -EINVAL;
654 }
655
656 rv3028_lock_sem(dev);
657
658 err = rv3028_read_reg8(dev, RV3028_REG_STATUS, &status);
659 if (err) {
660 goto unlock;
661 }
662
663 if (status & RV3028_STATUS_AF) {
664 /* Clear alarm flag */
665 status &= ~(RV3028_STATUS_AF);
666
667 err = rv3028_write_reg8(dev, RV3028_REG_STATUS, status);
668 if (err) {
669 goto unlock;
670 }
671
672 /* Alarm pending */
673 err = 1;
674 }
675
676 unlock:
677 rv3028_unlock_sem(dev);
678
679 return err;
680 }
681
rv3028_alarm_set_callback(const struct device * dev,uint16_t id,rtc_alarm_callback callback,void * user_data)682 static int rv3028_alarm_set_callback(const struct device *dev, uint16_t id,
683 rtc_alarm_callback callback, void *user_data)
684 {
685 #ifndef RV3028_INT_GPIOS_IN_USE
686 ARG_UNUSED(dev);
687 ARG_UNUSED(id);
688 ARG_UNUSED(callback);
689 ARG_UNUSED(user_data);
690
691 return -ENOTSUP;
692 #else
693 const struct rv3028_config *config = dev->config;
694 struct rv3028_data *data = dev->data;
695 int err;
696
697 if (config->gpio_int.port == NULL) {
698 return -ENOTSUP;
699 }
700
701 if (id != 0U) {
702 LOG_ERR("invalid alarm ID %d", id);
703 return -EINVAL;
704 }
705
706 rv3028_lock_sem(dev);
707
708 data->alarm_callback = callback;
709 data->alarm_user_data = user_data;
710
711 err = rv3028_update_reg8(dev, RV3028_REG_CONTROL2, RV3028_CONTROL2_AIE,
712 callback != NULL ? RV3028_CONTROL2_AIE : 0);
713 if (err) {
714 goto unlock;
715 }
716
717 unlock:
718 rv3028_unlock_sem(dev);
719
720 /* Alarm flag may already be set */
721 k_work_submit(&data->work);
722
723 return err;
724 #endif /* RV3028_INT_GPIOS_IN_USE */
725 }
726
727 #endif /* CONFIG_RTC_ALARM */
728
729 #if RV3028_INT_GPIOS_IN_USE && defined(CONFIG_RTC_UPDATE)
730
rv3028_update_set_callback(const struct device * dev,rtc_update_callback callback,void * user_data)731 static int rv3028_update_set_callback(const struct device *dev, rtc_update_callback callback,
732 void *user_data)
733 {
734 const struct rv3028_config *config = dev->config;
735 struct rv3028_data *data = dev->data;
736 int err;
737
738 if (config->gpio_int.port == NULL) {
739 return -ENOTSUP;
740 }
741
742 rv3028_lock_sem(dev);
743
744 data->update_callback = callback;
745 data->update_user_data = user_data;
746
747 err = rv3028_update_reg8(dev, RV3028_REG_CONTROL2, RV3028_CONTROL2_UIE,
748 callback != NULL ? RV3028_CONTROL2_UIE : 0);
749 if (err) {
750 goto unlock;
751 }
752
753 unlock:
754 rv3028_unlock_sem(dev);
755
756 /* Seconds flag may already be set */
757 k_work_submit(&data->work);
758
759 return err;
760 }
761
762 #endif /* RV3028_INT_GPIOS_IN_USE && defined(CONFIG_RTC_UPDATE) */
763
rv3028_init(const struct device * dev)764 static int rv3028_init(const struct device *dev)
765 {
766 const struct rv3028_config *config = dev->config;
767 struct rv3028_data *data = dev->data;
768 uint8_t regs[3];
769 uint8_t val;
770 int err;
771
772 k_sem_init(&data->lock, 1, 1);
773
774 if (!i2c_is_ready_dt(&config->i2c)) {
775 LOG_ERR("I2C bus not ready");
776 return -ENODEV;
777 }
778
779 err = rv3028_read_reg8(dev, RV3028_REG_ID, &val);
780 if (err) {
781 return -ENODEV;
782 }
783
784 LOG_DBG("HID: 0x%02x, VID: 0x%02x", (val & 0xF0) >> 0x04, val & 0x0F);
785
786 #if RV3028_INT_GPIOS_IN_USE
787 if (config->gpio_int.port != NULL) {
788 if (!gpio_is_ready_dt(&config->gpio_int)) {
789 LOG_ERR("GPIO not ready");
790 return -ENODEV;
791 }
792
793 err = gpio_pin_configure_dt(&config->gpio_int, GPIO_INPUT);
794 if (err) {
795 LOG_ERR("failed to configure GPIO (err %d)", err);
796 return -ENODEV;
797 }
798
799 err = gpio_pin_interrupt_configure_dt(&config->gpio_int, GPIO_INT_EDGE_TO_ACTIVE);
800 if (err) {
801 LOG_ERR("failed to enable GPIO interrupt (err %d)", err);
802 return err;
803 }
804
805 gpio_init_callback(&data->int_callback, rv3028_int_handler,
806 BIT(config->gpio_int.pin));
807
808 err = gpio_add_callback_dt(&config->gpio_int, &data->int_callback);
809 if (err) {
810 LOG_ERR("failed to add GPIO callback (err %d)", err);
811 return -ENODEV;
812 }
813
814 data->dev = dev;
815 data->work.handler = rv3028_work_cb;
816 }
817 #endif /* RV3028_INT_GPIOS_IN_USE */
818
819 err = rv3028_read_reg8(dev, RV3028_REG_STATUS, &val);
820 if (err) {
821 return -ENODEV;
822 }
823
824 if (val & RV3028_STATUS_AF) {
825 LOG_WRN("an alarm may have been missed");
826 }
827
828 /* Refresh the settings in the RAM with the settings from the EEPROM */
829 err = rv3028_enter_eerd(dev);
830 if (err) {
831 return -ENODEV;
832 }
833 err = rv3028_refresh(dev);
834 if (err) {
835 return -ENODEV;
836 }
837
838 /* Configure the CLKOUT register */
839 val = FIELD_PREP(RV3028_CLKOUT_FD, config->cof) |
840 (config->cof != RV3028_CLKOUT_FD_LOW ? RV3028_CLKOUT_CLKOE : 0);
841 err = rv3028_update_cfg(dev,
842 RV3028_REG_CLKOUT,
843 RV3028_CLKOUT_FD | RV3028_CLKOUT_CLKOE,
844 val);
845 if (err) {
846 return -ENODEV;
847 }
848
849 err = rv3028_update_cfg(dev,
850 RV3028_REG_BACKUP,
851 RV3028_BACKUP_TCE | RV3028_BACKUP_TCR | RV3028_BACKUP_BSM,
852 config->backup);
853 if (err) {
854 return -ENODEV;
855 }
856
857 err = rv3028_update_reg8(dev, RV3028_REG_CONTROL1, RV3028_CONTROL1_WADA,
858 RV3028_CONTROL1_WADA);
859 if (err) {
860 return -ENODEV;
861 }
862
863 /* Disable the alarms */
864 err = rv3028_update_reg8(dev,
865 RV3028_REG_CONTROL2,
866 RV3028_CONTROL2_AIE | RV3028_CONTROL2_UIE,
867 0);
868 if (err) {
869 return -ENODEV;
870 }
871
872 err = rv3028_read_regs(dev, RV3028_REG_ALARM_MINUTES, regs, sizeof(regs));
873 if (err) {
874 return -ENODEV;
875 }
876
877 regs[0] |= RV3028_ALARM_MINUTES_AE_M;
878 regs[1] |= RV3028_ALARM_HOURS_AE_H;
879 regs[2] |= RV3028_ALARM_DATE_AE_WD;
880
881 err = rv3028_write_regs(dev, RV3028_REG_ALARM_MINUTES, regs, sizeof(regs));
882 if (err) {
883 return -ENODEV;
884 }
885
886 return 0;
887 }
888
889 static DEVICE_API(rtc, rv3028_driver_api) = {
890 .set_time = rv3028_set_time,
891 .get_time = rv3028_get_time,
892 #ifdef CONFIG_RTC_ALARM
893 .alarm_get_supported_fields = rv3028_alarm_get_supported_fields,
894 .alarm_set_time = rv3028_alarm_set_time,
895 .alarm_get_time = rv3028_alarm_get_time,
896 .alarm_is_pending = rv3028_alarm_is_pending,
897 .alarm_set_callback = rv3028_alarm_set_callback,
898 #endif /* CONFIG_RTC_ALARM */
899 #if RV3028_INT_GPIOS_IN_USE && defined(CONFIG_RTC_UPDATE)
900 .update_set_callback = rv3028_update_set_callback,
901 #endif /* RV3028_INT_GPIOS_IN_USE && defined(CONFIG_RTC_UPDATE) */
902 };
903
904 #define RV3028_BSM_FROM_DT_INST(inst) \
905 UTIL_CAT(RV3028_BSM_, DT_INST_STRING_UPPER_TOKEN(inst, backup_switch_mode))
906
907 #define RV3028_BACKUP_FROM_DT_INST(inst) \
908 ((FIELD_PREP(RV3028_BACKUP_BSM, RV3028_BSM_FROM_DT_INST(inst))) | \
909 (FIELD_PREP(RV3028_BACKUP_TCR, DT_INST_ENUM_IDX_OR(inst, trickle_resistor_ohms, 0))) | \
910 (DT_INST_NODE_HAS_PROP(inst, trickle_resistor_ohms) ? RV3028_BACKUP_TCE : 0))
911
912 #define RV3028_INIT(inst) \
913 static const struct rv3028_config rv3028_config_##inst = { \
914 .i2c = I2C_DT_SPEC_INST_GET(inst), \
915 .cof = DT_INST_ENUM_IDX_OR(inst, clkout_frequency, RV3028_CLKOUT_FD_LOW), \
916 .backup = RV3028_BACKUP_FROM_DT_INST(inst), \
917 IF_ENABLED(RV3028_INT_GPIOS_IN_USE, \
918 (.gpio_int = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, {0})))}; \
919 \
920 static struct rv3028_data rv3028_data_##inst; \
921 \
922 DEVICE_DT_INST_DEFINE(inst, &rv3028_init, NULL, &rv3028_data_##inst, \
923 &rv3028_config_##inst, POST_KERNEL, CONFIG_RTC_INIT_PRIORITY, \
924 &rv3028_driver_api);
925
926 DT_INST_FOREACH_STATUS_OKAY(RV3028_INIT)
927