Skip to content

Commit 8ef1ead

Browse files
committed
drivers: rtc: Add support for Micro Crystal RV-8263-C8
Remove redundant code Signed-off-by: Daniel Kampert <[email protected]>
1 parent b39dbd9 commit 8ef1ead

File tree

2 files changed

+39
-54
lines changed

2 files changed

+39
-54
lines changed

drivers/rtc/rtc_rv8263.c

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ struct rv8263c8_data {
9898

9999
#if (CONFIG_RTC_ALARM || CONFIG_RTC_UPDATE) && DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios)
100100
const struct device *dev;
101-
#endif
102-
103-
#if (CONFIG_RTC_ALARM || CONFIG_RTC_UPDATE) && DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios)
104101
struct gpio_callback gpio_cb;
105102
#endif
106103

@@ -120,19 +117,19 @@ struct rv8263c8_data {
120117
static int rv8263c8_update_disable_timer(const struct device *dev)
121118
{
122119
int err;
123-
uint8_t reg;
120+
uint8_t buf[2];
124121
const struct rv8263c8_config *config = dev->config;
125122

126123
/* Value 0 disables the timer. */
127-
reg = 0;
128-
err = i2c_burst_write_dt(&config->i2c_bus, RV8263C8_REGISTER_TIMER_VALUE, &reg,
129-
sizeof(reg));
124+
buf[0] = RV8263C8_REGISTER_TIMER_VALUE;
125+
buf[1] = 0;
126+
err = i2c_write_dt(&config->i2c_bus, buf, 2);
130127
if (err < 0) {
131128
return err;
132129
}
133130

134-
return i2c_burst_write_dt(&config->i2c_bus, RV8263C8_REGISTER_TIMER_MODE, &reg,
135-
sizeof(reg));
131+
buf[0] = RV8263C8_REGISTER_TIMER_MODE;
132+
return i2c_write_dt(&config->i2c_bus, buf, 2);
136133
}
137134

138135
#if (CONFIG_RTC_ALARM || CONFIG_RTC_UPDATE) && DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios)
@@ -144,11 +141,11 @@ static void rv8263c8_gpio_callback_handler(const struct device *p_port, struct g
144141

145142
struct rv8263c8_data *data = CONTAINER_OF(p_cb, struct rv8263c8_data, gpio_cb);
146143

147-
#if CONFIG_RTC_ALARM && DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios)
144+
#if CONFIG_RTC_ALARM
148145
k_work_submit(&data->alarm_work);
149146
#endif
150147

151-
#if CONFIG_RTC_UPDATE && DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios)
148+
#if CONFIG_RTC_UPDATE
152149
k_work_submit(&data->update_work);
153150
#endif
154151
}
@@ -183,21 +180,21 @@ static void rv8263c8_alarm_worker(struct k_work *p_work)
183180
static int rv8263c8_update_enable_timer(const struct device *dev)
184181
{
185182
int err;
186-
uint8_t reg;
187183
const struct rv8263c8_config *config = dev->config;
184+
uint8_t buf[2];
188185

189186
/* Set the timer preload value for 1 second. */
190-
reg = 1;
191-
err = i2c_burst_write_dt(&config->i2c_bus, RV8263C8_REGISTER_TIMER_VALUE, &reg,
192-
sizeof(reg));
187+
buf[0] = RV8263C8_REGISTER_TIMER_VALUE;
188+
buf[1] = 1;
189+
err = i2c_write_dt(&config->i2c_bus, buf, 2);
193190
if (err < 0) {
194191
return err;
195192
}
196193

197-
reg = RV8263_BM_TD_1HZ | RV8263_BM_TE_ENABLE | RV8263_BM_TIE_ENABLE | RV8263_BM_TI_TP_PULSE;
198-
199-
return i2c_burst_write_dt(&config->i2c_bus, RV8263C8_REGISTER_TIMER_MODE, &reg,
200-
sizeof(reg));
194+
buf[0] = RV8263C8_REGISTER_TIMER_MODE;
195+
buf[1] = RV8263_BM_TD_1HZ | RV8263_BM_TE_ENABLE | RV8263_BM_TIE_ENABLE |
196+
RV8263_BM_TI_TP_PULSE;
197+
return i2c_write_dt(&config->i2c_bus, buf, 2);
201198
}
202199

203200
static void rv8263c8_update_worker(struct k_work *p_work)
@@ -225,7 +222,7 @@ static void rv8263c8_update_worker(struct k_work *p_work)
225222

226223
static int rv8263c8_time_set(const struct device *dev, const struct rtc_time *timeptr)
227224
{
228-
uint8_t regs[7];
225+
uint8_t regs[8];
229226
const struct rv8263c8_config *config = dev->config;
230227

231228
if (timeptr == NULL || (timeptr->tm_year < RV8263_YEAR_OFFSET)) {
@@ -238,15 +235,16 @@ static int rv8263c8_time_set(const struct device *dev, const struct rtc_time *ti
238235
timeptr->tm_year, timeptr->tm_mon, timeptr->tm_mday, timeptr->tm_wday,
239236
timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
240237

241-
regs[0] = bin2bcd(timeptr->tm_sec) & SECONDS_BITS;
242-
regs[1] = bin2bcd(timeptr->tm_min) & MINUTES_BITS;
243-
regs[2] = bin2bcd(timeptr->tm_hour) & HOURS_BITS;
244-
regs[3] = bin2bcd(timeptr->tm_mday) & DATE_BITS;
245-
regs[4] = bin2bcd(timeptr->tm_wday) & WEEKDAY_BITS;
246-
regs[5] = bin2bcd(timeptr->tm_mon) & MONTHS_BITS;
247-
regs[6] = bin2bcd(timeptr->tm_year - RV8263_YEAR_OFFSET) & YEAR_BITS;
238+
regs[0] = RV8263C8_REGISTER_SECONDS;
239+
regs[1] = bin2bcd(timeptr->tm_sec) & SECONDS_BITS;
240+
regs[2] = bin2bcd(timeptr->tm_min) & MINUTES_BITS;
241+
regs[3] = bin2bcd(timeptr->tm_hour) & HOURS_BITS;
242+
regs[4] = bin2bcd(timeptr->tm_mday) & DATE_BITS;
243+
regs[5] = bin2bcd(timeptr->tm_wday) & WEEKDAY_BITS;
244+
regs[6] = bin2bcd(timeptr->tm_mon) & MONTHS_BITS;
245+
regs[7] = bin2bcd(timeptr->tm_year - RV8263_YEAR_OFFSET) & YEAR_BITS;
248246

249-
return i2c_burst_write_dt(&config->i2c_bus, RV8263C8_REGISTER_SECONDS, regs, sizeof(regs));
247+
return i2c_write_dt(&config->i2c_bus, regs, 8);
250248
}
251249

252250
static int rv8263c8_time_get(const struct device *dev, struct rtc_time *timeptr)
@@ -328,38 +326,24 @@ static int rv8263c8_init(const struct device *dev)
328326
return err;
329327
}
330328

331-
temp = 0x00;
332-
if (config->clkout == 0) {
333-
temp = 0x07;
334-
} else if (config->clkout == 1) {
335-
temp = 0x06;
336-
} else if (config->clkout == 1024) {
337-
temp = 0x05;
338-
} else if (config->clkout == 2048) {
339-
temp = 0x04;
340-
} else if (config->clkout == 4096) {
341-
temp = 0x03;
342-
} else if (config->clkout == 8192) {
343-
temp = 0x02;
344-
} else if (config->clkout == 16384) {
345-
temp = 0x01;
346-
}
347-
329+
temp = config->clkout;
348330
LOG_DBG("Configure ClkOut: %u", temp);
349331

350332
err = i2c_reg_write_byte_dt(&config->i2c_bus, RV8263C8_REGISTER_CONTROL_2,
351-
RV8263C8_BM_MINUTE_INT_DISABLE |
352-
RV8263C8_BM_HALF_MINUTE_INT_DISABLE | temp);
333+
RV8263C8_BM_AF | temp);
353334
if (err < 0) {
354335
LOG_ERR("Error while writing CONTROL_2! Error: %i", err);
355336
return err;
356337
}
357338

339+
LOG_DBG("Configure ClkOut: %u", temp);
340+
358341
#if CONFIG_RTC_UPDATE && DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios)
359-
uint8_t regs = 0;
342+
uint8_t buf[2];
360343

361-
err = i2c_burst_write_dt(&config->i2c_bus, RV8263C8_REGISTER_TIMER_MODE, &regs,
362-
sizeof(regs));
344+
buf[0] = RV8263C8_REGISTER_TIMER_MODE;
345+
buf[1] = 0;
346+
err = i2c_write_dt(&config->i2c_bus, buf, 2);
363347
if (err < 0) {
364348
LOG_ERR("Error while writing CONTROL2! Error: %i", err);
365349
return err;
@@ -777,7 +761,7 @@ static const struct rtc_driver_api rv8263c8_driver_api = {
777761
static struct rv8263c8_data rv8263c8_data_##inst; \
778762
static const struct rv8263c8_config rv8263c8_config_##inst = { \
779763
.i2c_bus = I2C_DT_SPEC_INST_GET(inst), \
780-
.clkout = DT_INST_PROP_OR(inst, clkout, 0), \
764+
.clkout = DT_INST_ENUM_IDX(inst, clkout), \
781765
IF_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(int_gpios), \
782766
(.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, {0})))}; \
783767
DEVICE_DT_INST_DEFINE(inst, &rv8263c8_init, NULL, &rv8263c8_data_##inst, \

dts/bindings/rtc/microcrystal,rv-8263-c8.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ properties:
1616

1717
clkout:
1818
type: int
19-
default: 32768
19+
default: 0
2020
enum:
2121
- 32768
2222
- 16384
2323
- 8192
2424
- 4096
25+
- 2048
2526
- 1024
2627
- 1
2728
- 0
2829
description:
29-
Enable CLKOUT at given frequency. When disabled, CLKOUT pin is LOW. Set to 0 to
30-
disable the CLKOUT signal.
30+
Enable CLKOUT at given frequency. When disabled, CLKOUT pin is LOW.
31+
The default is 0 and corresponds to the disable the CLKOUT signal.

0 commit comments

Comments
 (0)