1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  *
4  * Copyright (c) 2023 Linumiz
5  * Author: Sri Surya  <srisurya@linumiz.com>
6  */
7 
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/drivers/rtc.h>
11 #include <zephyr/logging/log.h>
12 #include <zephyr/pm/device.h>
13 #include <zephyr/sys/util.h>
14 
15 #define DT_DRV_COMPAT ambiq_am1805
16 
17 LOG_MODULE_REGISTER(am1805, CONFIG_RTC_LOG_LEVEL);
18 
19 #define AM1805_IDENTITY_CODE	0x69
20 
21 /* AM1805 register address */
22 #define REG_HUNDREDS_ADDR	0x00
23 #define REG_SECONDS_ADDR	0x01
24 #define REG_MINUTES_ADDR	0x02
25 #define REG_HOURS_ADDR		0x03
26 #define REG_MDAY_ADDR		0x04
27 #define REG_MONTH_ADDR		0x05
28 #define REG_YEAR_ADDR		0x06
29 #define REG_WDAY_ADDR		0x07
30 
31 #define REG_ALM_HUNDREDS_ADDR	0x08
32 #define REG_ALM_SECONDS_ADDR	0x09
33 #define REG_ALM_MINUTES_ADDR	0x0A
34 #define REG_ALM_HOURS_ADDR	0x0B
35 #define REG_ALM_MDAY_ADDR	0x0C
36 #define REG_ALM_MONTH_ADDR	0x0D
37 #define REG_ALM_WDAY_ADDR	0x0E
38 #define REG_STATUS_ADDR		0x0F
39 #define REG_CONTROL1_ADDR	0x10
40 #define REG_CONTROL2_ADDR	0x11
41 #define REG_XT_CALIB_ADDR	0x14
42 #define REG_TIMER_CTRL_ADDR	0x18
43 #define REG_IRQ_MASK_ADDR       0x12
44 #define REG_WATCHDOG_ADDR	0x1B
45 #define REG_OSC_STATUS_ADDR	0x1D
46 
47 /* AM1805 control bits */
48 #define SECONDS_BITS		GENMASK(6, 0)
49 #define MINUTES_BITS		GENMASK(6, 0)
50 #define HOURS_BITS		GENMASK(5, 0)
51 #define DATE_BITS		GENMASK(5, 0)
52 #define MONTHS_BITS		GENMASK(4, 0)
53 #define WEEKDAY_BITS		GENMASK(2, 0)
54 #define YEAR_BITS		GENMASK(7, 0)
55 #define REG_CONTROL2_OUT2S_BITS GENMASK(4, 2)
56 #define REG_TIMER_CTRL_RPT_BITS GENMASK(4, 2)
57 #define REG_XT_CALIB_OFF_MASK	GENMASK(6, 0)
58 
59 #define REG_STATUS_ALM		BIT(2)
60 #define REG_CONTROL1_STOP	BIT(7)
61 #define REG_IRQ_MASK_AIE        BIT(2)
62 #define REG_XT_CALIB_CMDX       BIT(7)
63 
64 #define TIMER_CTRL_ALM_OFF	0x00
65 #define TIMER_CTRL_ALM_DAY	BIT(4)
66 #define TIMER_CTRL_ALM_YEAR	BIT(2)
67 #define TIMER_CTRL_ALM_HR	(BIT(2) | BIT(4))
68 #define TIMER_CTRL_ALM_SEC	GENMASK(4, 2)
69 #define TIMER_CTRL_ALM_MIN	GENMASK(4, 3)
70 #define TIMER_CTRL_ALM_WEEK	GENMASK(3, 2)
71 
72 #define REG_WATCHDOG_WDS	BIT(7)
73 #define WRB_1_SECOND		BIT(1)
74 #define WRB_4_SECONDS		GENMASK(1, 0)
75 
76 #define REG_OSC_STATUS_ACAL_0	0x00
77 #define REG_OSC_STATUS_ACAL_1	BIT(6)
78 #define REG_OSC_STATUS_ACAL_2	BIT(7)
79 #define REG_OSC_STATUS_ACAL_3	GENMASK(7, 6)
80 #define REG_OSC_STATUS_MASK	BIT(1)
81 #define REG_STATUS_DEFAULT	0x00
82 
83 #define AM1805_RTC_ALARM_TIME_MASK                                                                 \
84 	(RTC_ALARM_TIME_MASK_SECOND | RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR |      \
85 	 RTC_ALARM_TIME_MASK_MONTHDAY | RTC_ALARM_TIME_MASK_MONTH | RTC_ALARM_TIME_MASK_WEEKDAY)
86 
87 #ifdef CONFIG_RTC_ALARM
88 /* am1805-gpios property must be in the devicetree inorder to use the RTC_ALARM */
89 #if !DT_ANY_INST_HAS_PROP_STATUS_OKAY(am1805_gpios)
90 #error "am1805-gpios" - property not available in devicetree.
91 #endif
92 #endif
93 
94 struct am1805_config {
95 	const struct i2c_dt_spec int_i2c;
96 #ifdef CONFIG_RTC_ALARM
97 	struct gpio_dt_spec int_gpio;
98 #endif
99 };
100 
101 struct am1805_data {
102 	struct k_mutex lock;
103 #ifdef CONFIG_RTC_ALARM
104 	rtc_alarm_callback alarm_user_callback;
105 	void *alarm_user_data;
106 	/* For gpio-interrupt */
107 	struct gpio_callback am1805_callback;
108 	struct k_thread am1805_thread;
109 	struct k_sem int_sem;
110 
111 	K_KERNEL_STACK_MEMBER(am1805_stack, CONFIG_RTC_AM1805_THREAD_STACK_SIZE);
112 #endif
113 };
114 
115 /* To set the timer registers */
am1805_set_time(const struct device * dev,const struct rtc_time * tm)116 static int am1805_set_time(const struct device *dev, const struct rtc_time *tm)
117 {
118 	int err;
119 	uint8_t regs[7];
120 
121 	struct am1805_data *data = dev->data;
122 	const struct am1805_config *config = dev->config;
123 
124 	k_mutex_lock(&data->lock, K_FOREVER);
125 
126 	/* To unlock Stop-bit */
127 	err = i2c_reg_update_byte_dt(&config->int_i2c, REG_CONTROL1_ADDR,
128 					REG_CONTROL1_STOP, REG_CONTROL1_STOP);
129 	if (err != 0) {
130 		goto unlock;
131 	}
132 
133 	LOG_DBG("set time: year = %d, mon = %d, mday = %d, wday = %d, hour = %d, "
134 			"min = %d, sec = %d",
135 			tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday, tm->tm_hour, tm->tm_min,
136 			tm->tm_sec);
137 
138 	regs[0] = bin2bcd(tm->tm_sec) & SECONDS_BITS;
139 	regs[1] = bin2bcd(tm->tm_min) & MINUTES_BITS;
140 	regs[2] = bin2bcd(tm->tm_hour) & HOURS_BITS;
141 	regs[3] = bin2bcd(tm->tm_mday) & DATE_BITS;
142 	regs[4] = bin2bcd(tm->tm_mon) & MONTHS_BITS;
143 	regs[5] = bin2bcd(tm->tm_year) & YEAR_BITS;
144 	regs[6] = bin2bcd(tm->tm_wday) & WEEKDAY_BITS;
145 
146 	err = i2c_burst_write_dt(&config->int_i2c, REG_SECONDS_ADDR, regs, sizeof(regs));
147 	if (err != 0) {
148 		goto unlock;
149 	}
150 
151 	/* To lock Stop-bit */
152 	err = i2c_reg_update_byte_dt(&config->int_i2c, REG_CONTROL1_ADDR, REG_CONTROL1_STOP, 0);
153 
154 unlock:
155 	k_mutex_unlock(&data->lock);
156 
157 	return err;
158 }
159 
160 /* To get from the timer registers */
am1805_get_time(const struct device * dev,struct rtc_time * timeptr)161 static int am1805_get_time(const struct device *dev, struct rtc_time *timeptr)
162 {
163 	int err;
164 	uint8_t ctl_reg;
165 	uint8_t regs[7];
166 
167 	struct am1805_data *data = dev->data;
168 	const struct am1805_config *config = dev->config;
169 
170 	k_mutex_lock(&data->lock, K_FOREVER);
171 
172 	err = i2c_reg_read_byte_dt(&config->int_i2c, REG_CONTROL1_ADDR, &ctl_reg);
173 	if (err != 0) {
174 		goto unlock;
175 	}
176 
177 	err = ctl_reg & REG_CONTROL1_STOP;
178 	if (err != 0) {
179 		LOG_WRN("No control to get time now!!");
180 		goto unlock;
181 	}
182 
183 	err = i2c_burst_read_dt(&config->int_i2c, REG_SECONDS_ADDR, regs, sizeof(regs));
184 	if (err != 0) {
185 		goto unlock;
186 	}
187 
188 	timeptr->tm_sec = bcd2bin(regs[0] & SECONDS_BITS);
189 	timeptr->tm_min = bcd2bin(regs[1] & MINUTES_BITS);
190 	timeptr->tm_hour = bcd2bin(regs[2] & HOURS_BITS);
191 	timeptr->tm_mday = bcd2bin(regs[3] & DATE_BITS);
192 	timeptr->tm_mon = bcd2bin(regs[4] & MONTHS_BITS);
193 	timeptr->tm_year = bcd2bin(regs[5] & YEAR_BITS);
194 	timeptr->tm_wday = bcd2bin(regs[6] & WEEKDAY_BITS);
195 
196 	LOG_DBG("get time: year = %d, mon = %d, mday = %d, wday = %d, hour = %d, "
197 			"min = %d, sec = %d",
198 			timeptr->tm_year, timeptr->tm_mon, timeptr->tm_mday, timeptr->tm_wday,
199 			timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
200 
201 unlock:
202 	k_mutex_unlock(&data->lock);
203 
204 	return err;
205 }
206 
207 #ifdef CONFIG_RTC_CALIBRATION
208 /* To Calibrate the XT oscillator */
am1805_set_calibration(const struct device * dev,int32_t xt_clock_adj)209 static int am1805_set_calibration(const struct device *dev, int32_t xt_clock_adj)
210 {
211 	int err;
212 	uint8_t xt_calib_value;
213 
214 	struct am1805_data *data = dev->data;
215 	const struct am1805_config *config = dev->config;
216 	uint8_t reg = REG_OSC_STATUS_MASK;
217 
218 	if (xt_clock_adj < -320 || xt_clock_adj > 127) {
219 		LOG_DBG("Cannot be calibrated adj = %d\n", xt_clock_adj);
220 		return -EINVAL;
221 	} else if (xt_clock_adj < -256) {
222 		/* XTCAL=3 CMDX=1 OFFSETX=(adj+192)/2 */
223 		reg |= REG_OSC_STATUS_ACAL_3;
224 		xt_calib_value = ((uint8_t)(xt_clock_adj + 192) >> 1);
225 		xt_calib_value &= REG_XT_CALIB_OFF_MASK;
226 		xt_calib_value |= REG_XT_CALIB_CMDX;
227 	} else if (xt_clock_adj < -192) {
228 		/* XTCAL=3 CMDX=0 OFFSETX=(adj+192) */
229 		reg |= REG_OSC_STATUS_ACAL_3;
230 		xt_calib_value = (uint8_t)(xt_clock_adj + 192);
231 		xt_calib_value &= REG_XT_CALIB_OFF_MASK;
232 	} else if (xt_clock_adj < -128) {
233 		/* XTCAL=2 CMDX=0 OFFSETX=(adj+128) */
234 		reg |= REG_OSC_STATUS_ACAL_2;
235 		xt_calib_value = (uint8_t)(xt_clock_adj + 128);
236 		xt_calib_value &= REG_XT_CALIB_OFF_MASK;
237 	} else if (xt_clock_adj < -64) {
238 		/* XTCAL=1 CMDX=0 OFFSETX=(adj+64) */
239 		reg |= REG_OSC_STATUS_ACAL_1;
240 		xt_calib_value = (uint8_t)(xt_clock_adj + 64);
241 		xt_calib_value &= REG_XT_CALIB_OFF_MASK;
242 	} else if (xt_clock_adj < 64) {
243 		/* XTCAL=0 CMDX=0 OFFSETX=(adj) */
244 		reg |= REG_OSC_STATUS_ACAL_0;
245 		xt_calib_value = (uint8_t)(xt_clock_adj);
246 		xt_calib_value &= REG_XT_CALIB_OFF_MASK;
247 	} else if (xt_clock_adj < 128) {
248 		/* XTCAL=0 CMDX=1 OFFSETX=(adj)/2 */
249 		reg |= REG_OSC_STATUS_ACAL_0;
250 		xt_calib_value = ((uint8_t)(xt_clock_adj >> 1));
251 		xt_calib_value &= REG_XT_CALIB_OFF_MASK;
252 		xt_calib_value |= REG_XT_CALIB_CMDX;
253 	}
254 
255 	k_mutex_lock(&data->lock, K_FOREVER);
256 
257 	err = i2c_reg_write_byte_dt(&config->int_i2c, REG_OSC_STATUS_ADDR, reg);
258 	if (err != 0) {
259 		LOG_DBG("fail to set oscillator status register\n");
260 		goto unlock;
261 	}
262 
263 	/* Calibration XT Register */
264 	reg = xt_calib_value;
265 	err = i2c_reg_write_byte_dt(&config->int_i2c, REG_XT_CALIB_ADDR, reg);
266 	if (err != 0) {
267 		LOG_DBG("fail to set XT calibration register\n");
268 	}
269 
270 unlock:
271 	k_mutex_unlock(&data->lock);
272 
273 	return err;
274 }
275 
am1805_get_calibration(const struct device * dev,int32_t * calib)276 static int am1805_get_calibration(const struct device *dev, int32_t *calib)
277 {
278 	int err;
279 	bool cmdx;
280 	uint8_t reg;
281 	uint8_t xtcal;
282 
283 	struct am1805_data *data = dev->data;
284 	const struct am1805_config *config = dev->config;
285 
286 	k_mutex_lock(&data->lock, K_FOREVER);
287 
288 	err = i2c_reg_read_byte_dt(&config->int_i2c, REG_OSC_STATUS_ADDR, &reg);
289 	if (err != 0) {
290 		goto unlock;
291 	}
292 
293 	/* First 2-bits (from MSB) */
294 	xtcal = reg >> 6;
295 
296 	err = i2c_reg_read_byte_dt(&config->int_i2c, REG_XT_CALIB_ADDR, &reg);
297 	if (err != 0) {
298 		goto unlock;
299 	}
300 
301 	*calib = reg;
302 	/* First bit (from MSB) */
303 	cmdx = reg & BIT(7);
304 	/* Set or Clear the bit-7 based on bit-6, to achieve the given range (in datasheet) */
305 	WRITE_BIT(reg, 7, (reg & BIT(6)));
306 	WRITE_BIT(reg, 6, 0);
307 
308 	LOG_DBG("XTCAL = %d, CMDX = %d, OFFSETX = %d\n", xtcal, cmdx, (int8_t) reg);
309 
310 unlock:
311 	k_mutex_unlock(&data->lock);
312 
313 	return err;
314 }
315 #endif
316 
317 #ifdef CONFIG_RTC_ALARM
318 /* To get from the alarm registers */
am1805_alarm_get_time(const struct device * dev,uint16_t id,uint16_t * mask,struct rtc_time * timeptr)319 static int am1805_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
320 		struct rtc_time *timeptr)
321 {
322 
323 	int err;
324 	uint8_t regs[6];
325 
326 	struct am1805_data *data = dev->data;
327 	const struct am1805_config *config = dev->config;
328 
329 	if (id != 0U) {
330 		LOG_ERR("invalid ID %d", id);
331 		return -EINVAL;
332 	}
333 
334 	k_mutex_lock(&data->lock, K_FOREVER);
335 
336 	err = i2c_burst_read_dt(&config->int_i2c, REG_ALM_SECONDS_ADDR, regs, sizeof(regs));
337 	if (err != 0) {
338 		goto unlock;
339 	}
340 
341 	timeptr->tm_sec = bcd2bin(regs[0] & SECONDS_BITS);
342 	timeptr->tm_min = bcd2bin(regs[1] & MINUTES_BITS);
343 	timeptr->tm_hour = bcd2bin(regs[2] & HOURS_BITS);
344 	timeptr->tm_mday = bcd2bin(regs[3] & DATE_BITS);
345 	timeptr->tm_mon = bcd2bin(regs[4] & MONTHS_BITS);
346 	timeptr->tm_wday = bcd2bin(regs[5] & WEEKDAY_BITS);
347 
348 	*mask = (AM1805_RTC_ALARM_TIME_MASK);
349 
350 	LOG_DBG("get alarm: wday = %d, mon = %d, mday = %d, hour = %d, min = %d, sec = %d, "
351 		"mask = 0x%04x", timeptr->tm_wday, timeptr->tm_mon, timeptr->tm_mday,
352 		timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec, *mask);
353 
354 unlock:
355 	k_mutex_unlock(&data->lock);
356 
357 	return err;
358 }
359 
am1805_alarm_set_time(const struct device * dev,uint16_t id,uint16_t mask,const struct rtc_time * timeptr)360 static int am1805_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
361 		const struct rtc_time *timeptr)
362 {
363 	int err;
364 	uint8_t regs[6];
365 
366 	struct am1805_data *data = dev->data;
367 	const struct am1805_config *config = dev->config;
368 
369 	if (id != 0U) {
370 		LOG_ERR("invalid ID %d", id);
371 		return -EINVAL;
372 	}
373 
374 	if ((mask & ~(AM1805_RTC_ALARM_TIME_MASK)) != 0U) {
375 		LOG_ERR("unsupported alarm field mask 0x%04x", mask);
376 		return -EINVAL;
377 	}
378 
379 	k_mutex_lock(&data->lock, K_FOREVER);
380 
381 	/* Disable timer control registers before the initialization */
382 	err = i2c_reg_update_byte_dt(&config->int_i2c, REG_TIMER_CTRL_ADDR,
383 					REG_TIMER_CTRL_RPT_BITS, 0);
384 	if (err != 0) {
385 		goto unlock;
386 	}
387 
388 	/* Clear the interrupt mask for alarm */
389 	err = i2c_reg_update_byte_dt(&config->int_i2c, REG_IRQ_MASK_ADDR,
390 					REG_IRQ_MASK_AIE, 0);
391 	if (err != 0) {
392 		goto unlock;
393 	}
394 
395 	/* Clear the status bit */
396 	err = i2c_reg_update_byte_dt(&config->int_i2c, REG_STATUS_ADDR,
397 					REG_STATUS_ALM, 0);
398 	if (err != 0) {
399 		goto unlock;
400 	}
401 
402 	/* When mask is 0 */
403 	if (mask == 0) {
404 		LOG_DBG("The alarm is disabled");
405 		goto unlock;
406 	}
407 
408 	regs[0] = bin2bcd(timeptr->tm_sec) & SECONDS_BITS;
409 	regs[1] = bin2bcd(timeptr->tm_min) & MINUTES_BITS;
410 	regs[2] = bin2bcd(timeptr->tm_hour) & HOURS_BITS;
411 	regs[3] = bin2bcd(timeptr->tm_mday) & DATE_BITS;
412 	regs[4] = bin2bcd(timeptr->tm_mon) & MONTHS_BITS;
413 	regs[5] = bin2bcd(timeptr->tm_wday) & WEEKDAY_BITS;
414 
415 	LOG_DBG("set alarm: second = %d, min = %d, hour = %d, mday = %d, month = %d,"
416 			"wday = %d,  mask = 0x%04x",
417 			timeptr->tm_sec, timeptr->tm_min, timeptr->tm_hour, timeptr->tm_mday,
418 			timeptr->tm_mon, timeptr->tm_wday, mask);
419 
420 	err = i2c_burst_write_dt(&config->int_i2c, REG_ALM_SECONDS_ADDR, regs, sizeof(regs));
421 	if (err != 0) {
422 		goto unlock;
423 	}
424 
425 	/* Enable irq timer after the initialization */
426 	err = i2c_reg_update_byte_dt(&config->int_i2c, REG_IRQ_MASK_ADDR,
427 					REG_IRQ_MASK_AIE, REG_IRQ_MASK_AIE);
428 	if (err != 0) {
429 		goto unlock;
430 	}
431 
432 	/* Enable timer after the initialization for the config of repetation */
433 	err = i2c_reg_update_byte_dt(&config->int_i2c, REG_TIMER_CTRL_ADDR,
434 					TIMER_CTRL_ALM_SEC, TIMER_CTRL_ALM_SEC);
435 
436 unlock:
437 	k_mutex_unlock(&data->lock);
438 
439 	return err;
440 }
441 
am1805_alarm_get_supported_fields(const struct device * dev,uint16_t id,uint16_t * mask)442 static int am1805_alarm_get_supported_fields(const struct device *dev, uint16_t id, uint16_t *mask)
443 {
444 	ARG_UNUSED(dev);
445 
446 	if (id != 0U) {
447 		LOG_ERR("invalid ID %d", id);
448 		return -EINVAL;
449 	}
450 
451 	*mask = AM1805_RTC_ALARM_TIME_MASK;
452 
453 	return 0;
454 }
455 
am1805_alarm_set_callback(const struct device * dev,uint16_t id,rtc_alarm_callback callback,void * user_data)456 static int am1805_alarm_set_callback(const struct device *dev, uint16_t id,
457 		rtc_alarm_callback callback, void *user_data)
458 {
459 
460 	struct am1805_data *data = dev->data;
461 	const struct am1805_config *config = dev->config;
462 
463 	if (config->int_gpio.port == NULL) {
464 		return -ENOTSUP;
465 	}
466 
467 	if (id != 0U) {
468 		LOG_ERR("invalid ID %d", id);
469 		return -EINVAL;
470 	}
471 
472 	k_mutex_lock(&data->lock, K_FOREVER);
473 
474 	/* Passing the callback function and userdata filled by the user */
475 	data->alarm_user_callback = callback;
476 	data->alarm_user_data = user_data;
477 
478 	k_mutex_unlock(&data->lock);
479 
480 	return 0;
481 }
482 
am1805_interrupt_thread(const struct device * dev)483 static void am1805_interrupt_thread(const struct device *dev)
484 {
485 	struct am1805_data *data = dev->data;
486 
487 	while (1) {
488 		k_sem_take(&data->int_sem, K_FOREVER);
489 
490 		if (data->alarm_user_callback == NULL) {
491 			LOG_DBG("Interrupt received, But No Alarm-Callback Initilized!!\n");
492 			continue;
493 		}
494 		data->alarm_user_callback(dev, 0, data->alarm_user_data);
495 	}
496 }
497 
am1805_gpio_callback_handler(const struct device * port,struct gpio_callback * cb,gpio_port_pins_t pins)498 static void am1805_gpio_callback_handler(const struct device *port, struct gpio_callback *cb,
499 		gpio_port_pins_t pins)
500 {
501 	struct am1805_data *data = CONTAINER_OF(cb, struct am1805_data, am1805_callback);
502 
503 	ARG_UNUSED(port);
504 	ARG_UNUSED(pins);
505 
506 	k_sem_give(&data->int_sem);
507 }
508 #endif
509 
am1805_init(const struct device * dev)510 static int am1805_init(const struct device *dev)
511 {
512 	int err;
513 	uint8_t reg;
514 
515 	const struct am1805_config *config = dev->config;
516 	struct am1805_data *data = dev->data;
517 
518 	k_mutex_init(&data->lock);
519 
520 	if (!i2c_is_ready_dt(&config->int_i2c)) {
521 		LOG_ERR("I2C bus not ready");
522 		return -ENODEV;
523 	}
524 
525 	err = i2c_reg_read_byte_dt(&config->int_i2c, REG_STATUS_ADDR, &reg);
526 	if (err != 0) {
527 		LOG_ERR("failed to read the status register");
528 		return -ENODEV;
529 	}
530 
531 #ifdef CONFIG_RTC_ALARM
532 	k_tid_t tid;
533 
534 	k_sem_init(&data->int_sem, 0, INT_MAX);
535 
536 	if (!gpio_is_ready_dt(&config->int_gpio)) {
537 		LOG_ERR("GPIO not ready");
538 		return -ENODEV;
539 	}
540 
541 	err = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
542 	if (err != 0) {
543 		LOG_ERR("failed to configure GPIO (err %d)", err);
544 		return -ENODEV;
545 	}
546 
547 	err = gpio_pin_interrupt_configure_dt(&config->int_gpio,
548 			GPIO_INT_EDGE_TO_INACTIVE);
549 	if (err != 0) {
550 		LOG_ERR("failed to configure interrupt (err %d)", err);
551 		return -ENODEV;
552 	}
553 
554 	gpio_init_callback(&data->am1805_callback, am1805_gpio_callback_handler,
555 			BIT(config->int_gpio.pin));
556 
557 	err = gpio_add_callback_dt(&config->int_gpio, &data->am1805_callback);
558 	if (err != 0) {
559 		LOG_ERR("failed to add GPIO callback (err %d)", err);
560 		return -ENODEV;
561 	}
562 
563 	tid = k_thread_create(&data->am1805_thread, data->am1805_stack,
564 			K_THREAD_STACK_SIZEOF(data->am1805_stack),
565 			(k_thread_entry_t)am1805_interrupt_thread, (void *)dev, NULL,
566 			NULL, CONFIG_RTC_AM1805_THREAD_PRIO, 0, K_NO_WAIT);
567 	k_thread_name_set(tid, dev->name);
568 #endif
569 	return 0;
570 }
571 
572 static const struct rtc_driver_api am1805_driver_api = {
573 	.set_time = am1805_set_time,
574 	.get_time = am1805_get_time,
575 #ifdef CONFIG_RTC_ALARM
576 	.alarm_get_supported_fields = am1805_alarm_get_supported_fields,
577 	.alarm_set_time = am1805_alarm_set_time,
578 	.alarm_get_time = am1805_alarm_get_time,
579 	.alarm_set_callback = am1805_alarm_set_callback,
580 #endif
581 #ifdef CONFIG_RTC_CALIBRATION
582 	.set_calibration = am1805_set_calibration,
583 	.get_calibration = am1805_get_calibration,
584 #endif
585 };
586 
587 #define AM1805_INIT(inst)									\
588 static const struct am1805_config am1805_config_##inst = {					\
589 	.int_i2c = I2C_DT_SPEC_INST_GET(inst),							\
590 	IF_ENABLED(CONFIG_RTC_ALARM,								\
591 		(.int_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, am1805_gpios, {0})))};		\
592 												\
593 static struct am1805_data am1805_data_##inst;							\
594 												\
595 DEVICE_DT_INST_DEFINE(inst, &am1805_init, NULL, &am1805_data_##inst,				\
596 		      &am1805_config_##inst, POST_KERNEL, CONFIG_RTC_INIT_PRIORITY,		\
597 		      &am1805_driver_api);
598 
599 DT_INST_FOREACH_STATUS_OKAY(AM1805_INIT)
600