|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Silicon Laboratories Inc. |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/devicetree.h> |
| 8 | +#include <zephyr/drivers/clock_control.h> |
| 9 | +#include <zephyr/drivers/rtc.h> |
| 10 | +#include <zephyr/logging/log.h> |
| 11 | +#include <zephyr/pm/device.h> |
| 12 | +#include <zephyr/sys/util.h> |
| 13 | +#include "rtc_utils.h" |
| 14 | +#include "sl_si91x_calendar.h" |
| 15 | + |
| 16 | +#define DT_DRV_COMPAT silabs_siwx91x_rtc |
| 17 | + |
| 18 | +LOG_MODULE_REGISTER(siwx91x_rtc, CONFIG_RTC_LOG_LEVEL); |
| 19 | + |
| 20 | +#define TM_YEAR_REF 1900 |
| 21 | +#define SIWX91X_RTC_YEAR_MAX 2399 |
| 22 | +#define SIWX91X_RTC_YEAR_MIN 2000 |
| 23 | + |
| 24 | +struct siwx91x_rtc_config { |
| 25 | + const struct device *clock_dev; |
| 26 | + clock_control_subsys_t clock_subsys; |
| 27 | +}; |
| 28 | +struct siwx91x_rtc_data { |
| 29 | + struct k_spinlock lock; |
| 30 | +}; |
| 31 | + |
| 32 | +static void rtc_time_to_siwx91x_time_set(const struct rtc_time *tm, |
| 33 | + sl_calendar_datetime_config_t *cldr) |
| 34 | +{ |
| 35 | + int full_year = tm->tm_year + TM_YEAR_REF; |
| 36 | + |
| 37 | + cldr->Year = (full_year % 100); |
| 38 | + cldr->Century = (full_year >= 2000) ? (full_year - 2000) / 100 : 0; |
| 39 | + cldr->Month = tm->tm_mon + 1; |
| 40 | + cldr->Day = tm->tm_mday; |
| 41 | + cldr->DayOfWeek = (RTC_DAY_OF_WEEK_T)tm->tm_wday; |
| 42 | + cldr->Hour = tm->tm_hour; |
| 43 | + cldr->Minute = tm->tm_min; |
| 44 | + cldr->Second = tm->tm_sec; |
| 45 | + cldr->MilliSeconds = tm->tm_nsec / NSEC_PER_MSEC; |
| 46 | +} |
| 47 | + |
| 48 | +static void siwx91x_time_to_rtc_time_set(const sl_calendar_datetime_config_t *cldr, |
| 49 | + struct rtc_time *tm) |
| 50 | +{ |
| 51 | + int full_year = 2000 + (cldr->Century * 100) + cldr->Year; |
| 52 | + |
| 53 | + tm->tm_year = full_year - TM_YEAR_REF; |
| 54 | + tm->tm_mon = cldr->Month - 1; |
| 55 | + tm->tm_mday = cldr->Day; |
| 56 | + tm->tm_wday = (int)cldr->DayOfWeek; |
| 57 | + tm->tm_hour = cldr->Hour; |
| 58 | + tm->tm_min = cldr->Minute; |
| 59 | + tm->tm_sec = cldr->Second; |
| 60 | + tm->tm_nsec = cldr->MilliSeconds * NSEC_PER_MSEC; |
| 61 | +} |
| 62 | + |
| 63 | +static int siwx91x_rtc_set_time(const struct device *dev, const struct rtc_time *timeptr) |
| 64 | +{ |
| 65 | + sl_calendar_datetime_config_t siwx91x_time = {}; |
| 66 | + struct siwx91x_rtc_data *data = dev->data; |
| 67 | + int year; |
| 68 | + int ret; |
| 69 | + |
| 70 | + year = timeptr->tm_year + TM_YEAR_REF; |
| 71 | + if (year < SIWX91X_RTC_YEAR_MIN || year > SIWX91X_RTC_YEAR_MAX) { |
| 72 | + return -EINVAL; |
| 73 | + } |
| 74 | + |
| 75 | + k_spinlock_key_t key = k_spin_lock(&data->lock); |
| 76 | + |
| 77 | + LOG_DBG("Set RTC time: year = %d, mon = %d, mday = %d, wday = %d, hour = %d, " |
| 78 | + "min = %d, sec = %d", |
| 79 | + timeptr->tm_year, timeptr->tm_mon, timeptr->tm_mday, timeptr->tm_wday, |
| 80 | + timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec); |
| 81 | + |
| 82 | + rtc_time_to_siwx91x_time_set(timeptr, &siwx91x_time); |
| 83 | + |
| 84 | + ret = sl_si91x_calendar_set_date_time(&siwx91x_time); |
| 85 | + if (ret) { |
| 86 | + LOG_WRN("Set Timer returned an error - %d!", ret); |
| 87 | + } |
| 88 | + |
| 89 | + k_spin_unlock(&data->lock, key); |
| 90 | + |
| 91 | + return ret; |
| 92 | +} |
| 93 | + |
| 94 | +static int siwx91x_rtc_get_time(const struct device *dev, struct rtc_time *timeptr) |
| 95 | +{ |
| 96 | + sl_calendar_datetime_config_t siwx91x_time = {}; |
| 97 | + struct siwx91x_rtc_data *data = dev->data; |
| 98 | + int ret; |
| 99 | + |
| 100 | + k_spinlock_key_t key = k_spin_lock(&data->lock); |
| 101 | + |
| 102 | + ret = sl_si91x_calendar_get_date_time(&siwx91x_time); |
| 103 | + if (ret != 0) { |
| 104 | + LOG_WRN("Get Timer returned an error - %d!", ret); |
| 105 | + goto unlock; |
| 106 | + } |
| 107 | + |
| 108 | + siwx91x_time_to_rtc_time_set(&siwx91x_time, timeptr); |
| 109 | + |
| 110 | + LOG_DBG("get time: year = %d, mon = %d, mday = %d, wday = %d, hour = %d, " |
| 111 | + "min = %d, sec = %d", |
| 112 | + timeptr->tm_year, timeptr->tm_mon, timeptr->tm_mday, timeptr->tm_wday, |
| 113 | + timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec); |
| 114 | + |
| 115 | +unlock: |
| 116 | + k_spin_unlock(&data->lock, key); |
| 117 | + |
| 118 | + return ret; |
| 119 | +} |
| 120 | + |
| 121 | +static int siwx91x_rtc_init(const struct device *dev) |
| 122 | +{ |
| 123 | + const struct siwx91x_rtc_config *config = dev->config; |
| 124 | + int ret; |
| 125 | + |
| 126 | + ret = clock_control_on(config->clock_dev, config->clock_subsys); |
| 127 | + if (ret) { |
| 128 | + return ret; |
| 129 | + } |
| 130 | + |
| 131 | + sl_si91x_calendar_init(); |
| 132 | + |
| 133 | + return 0; |
| 134 | +} |
| 135 | + |
| 136 | +static DEVICE_API(rtc, siwx91x_rtc_driver_api) = { |
| 137 | + .set_time = siwx91x_rtc_set_time, |
| 138 | + .get_time = siwx91x_rtc_get_time, |
| 139 | +}; |
| 140 | + |
| 141 | +#define SIWX91X_RTC_INIT(inst) \ |
| 142 | + static const struct siwx91x_rtc_config siwx91x_rtc_config_##inst = { \ |
| 143 | + .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(inst)), \ |
| 144 | + .clock_subsys = (clock_control_subsys_t)DT_INST_PHA(inst, clocks, clkid), \ |
| 145 | + }; \ |
| 146 | + \ |
| 147 | + static struct siwx91x_rtc_data siwx91x_rtc_data##inst; \ |
| 148 | + \ |
| 149 | + DEVICE_DT_INST_DEFINE(inst, &siwx91x_rtc_init, NULL, &siwx91x_rtc_data##inst, \ |
| 150 | + &siwx91x_rtc_config_##inst, POST_KERNEL, CONFIG_RTC_INIT_PRIORITY, \ |
| 151 | + &siwx91x_rtc_driver_api); |
| 152 | + |
| 153 | +DT_INST_FOREACH_STATUS_OKAY(SIWX91X_RTC_INIT) |
0 commit comments