1 /*
2  * Copyright (c) 2023 Alvaro Garcia Gomez <maxpowel@gmail.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/init.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/sys/util.h>
11 
12 #include "rtc_utils.h"
13 
14 #include <stdint.h>
15 #include <string.h>
16 #include <zephyr/device.h>
17 #include <zephyr/drivers/gpio.h>
18 #include <zephyr/drivers/i2c.h>
19 #include <zephyr/drivers/rtc.h>
20 #include <zephyr/sys/timeutil.h>
21 #include <zephyr/logging/log.h>
22 LOG_MODULE_REGISTER(pcf8563);
23 
24 #define DT_DRV_COMPAT nxp_pcf8563
25 
26 #if DT_ANY_INST_HAS_PROP_STATUS_OKAY(int1_gpios) &&                                                \
27 	(defined(CONFIG_RTC_ALARM) || defined(CONFIG_RTC_UPDATE))
28 /* The user may need only alarms but not interrupts so we will only
29  * include all the interrupt code if the user configured it in the dts
30  */
31 #define PCF8563_INT1_GPIOS_IN_USE 1
32 #endif
33 
34 /* The device registers */
35 #define PCF8563_TIME_DATE_REGISTER	0x02
36 #define PCF8563_ALARM_REGISTER		0x09
37 #define PCF8563_CONTROL1_REGISTER	0x00
38 #define PCF8563_CONTROL2_REGISTER	0x01
39 #define PCF8563_CONTROL2_REGISTER_TIE_EN (1 << 0)
40 #define PCF8563_CONTROL2_REGISTER_AIE_EN (1 << 1)
41 
42 /* These masks were retrieved from the datasheet
43  * https://www.nxp.com/docs/en/data-sheet/PCF8563.pdf
44  * page 6, section 8.2 Register organization.
45  * Basically, I clean the unused bits and the bits used
46  * for other stuff
47  */
48 #define PCF8563_SECONDS_MASK  GENMASK(6, 0)
49 #define PCF8563_MINUTES_MASK  GENMASK(6, 0)
50 #define PCF8563_HOURS_MASK    GENMASK(5, 0)
51 #define PCF8563_DAYS_MASK     GENMASK(5, 0)
52 #define PCF8563_WEEKDAYS_MASK GENMASK(2, 0)
53 #define PCF8563_MONTHS_MASK   GENMASK(4, 0)
54 
55 /* RTC alarm time fields supported by the PCF8563, page 7 of the datasheet */
56 #define PCF8563_RTC_ALARM_TIME_MASK                                                                \
57 	(RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR | RTC_ALARM_TIME_MASK_MONTHDAY |    \
58 	 RTC_ALARM_TIME_MASK_WEEKDAY)
59 
60 #define PCF8563_RTC_TIME_MASK                                                                      \
61 	(RTC_ALARM_TIME_MASK_SECOND | RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR |      \
62 	 RTC_ALARM_TIME_MASK_MONTH | RTC_ALARM_TIME_MASK_MONTHDAY | RTC_ALARM_TIME_MASK_YEAR |     \
63 	 RTC_ALARM_TIME_MASK_WEEKDAY)
64 
65 struct pcf8563_config {
66 	const struct i2c_dt_spec i2c;
67 	#ifdef PCF8563_INT1_GPIOS_IN_USE
68 	const struct gpio_dt_spec int1;
69 	#endif
70 };
71 
72 
73 #ifdef PCF8563_INT1_GPIOS_IN_USE
74 /* This work will run the user callback function */
75 static void callback_work_handler(struct k_work *work);
76 K_WORK_DEFINE(callback_work, callback_work_handler);
77 #endif
78 
79 struct pcf8563_data {
80 #ifdef PCF8563_INT1_GPIOS_IN_USE
81 	rtc_alarm_callback alarm_callback;
82 	void *alarm_user_data;
83 	const struct device *dev;
84 	struct gpio_callback int1_callback;
85 	struct k_work callback_work;
86 #endif
87 };
88 
89 /**
90  * The format described below is described in the datasheet
91  * https://www.nxp.com/docs/en/data-sheet/PCF8563.pdf page 10 starting
92  * with 8.4.2 Register Minutes.
93  *
94  * For seconds, first bit is ignored (it is used to check the clock integrity).
95  * The upper digit takes the next 3 bits for the tens place and then the rest
96  * bits for the unit
97  * So for example, value 43 is 40 * 10 + 3, so the tens digit is 4 and unit digit is 3.
98  * Then we put the number 3 in the last 4 bits and the number 4 in next 3 bits
99  * It uses BCD notation so the number 3 is 0011 and the number for is 100 so the final
100  * byte is 0 (ignored bit) 100 (the 4) 0011 (the 3) -> 0100001
101  * Luckily, zephyr provides a couple of functions to do exactlly this: bin2bcd and bcd2bin,
102  * but we will take care about the bits marked as non used in
103  * the datasheet because they may contain unexpected values. Applying a mask will help us
104  * to sanitize the read values
105  */
pcf8563_set_time(const struct device * dev,const struct rtc_time * timeptr)106 static int pcf8563_set_time(const struct device *dev, const struct rtc_time *timeptr)
107 {
108 	const struct pcf8563_config *config = dev->config;
109 	int ret;
110 	uint8_t raw_time[7];
111 
112 	if (!rtc_utils_validate_rtc_time(timeptr, PCF8563_RTC_TIME_MASK)) {
113 		LOG_ERR("invalid time");
114 		return -EINVAL;
115 	}
116 
117 	/* Set seconds */
118 	raw_time[0] = bin2bcd(timeptr->tm_sec);
119 
120 	/* Set minutes */
121 	raw_time[1] = bin2bcd(timeptr->tm_min);
122 
123 	/* Set hours */
124 	raw_time[2] = bin2bcd(timeptr->tm_hour);
125 
126 	/* Set days */
127 	raw_time[3] = bin2bcd(timeptr->tm_mday);
128 
129 	/* Set weekdays */
130 	raw_time[4] = timeptr->tm_wday;
131 
132 	/*Set month */
133 	raw_time[5] = bin2bcd(timeptr->tm_mon);
134 
135 	/* Set year */
136 	raw_time[6] = bin2bcd(timeptr->tm_year);
137 
138 	/* Write to device */
139 	ret = i2c_burst_write_dt(&config->i2c, PCF8563_TIME_DATE_REGISTER,
140 				 raw_time, sizeof(raw_time));
141 	if (ret) {
142 		LOG_ERR("Error when setting time: %i", ret);
143 		return ret;
144 	}
145 
146 	return 0;
147 }
148 
pcf8563_get_time(const struct device * dev,struct rtc_time * timeptr)149 static int pcf8563_get_time(const struct device *dev, struct rtc_time *timeptr)
150 {
151 	const struct pcf8563_config *config = dev->config;
152 	int ret;
153 	uint8_t raw_time[7];
154 
155 	ret = i2c_burst_read_dt(&config->i2c, PCF8563_TIME_DATE_REGISTER,
156 				raw_time, sizeof(raw_time));
157 	if (ret) {
158 		LOG_ERR("Unable to get time. Err: %i", ret);
159 		return ret;
160 	}
161 
162 	/* Check integrity, if the first bit is 1 it is ok */
163 	if (raw_time[0] & BIT(7)) {
164 		LOG_WRN("Clock integrity failed");
165 		return -ENODATA;
166 	}
167 
168 	/* Nanoseconds */
169 	timeptr->tm_nsec = 0;
170 
171 	/* Get seconds */
172 	timeptr->tm_sec = bcd2bin(raw_time[0] & PCF8563_SECONDS_MASK);
173 
174 	/* Get minutes */
175 	timeptr->tm_min = bcd2bin(raw_time[1] & PCF8563_MINUTES_MASK);
176 
177 	/* Get hours */
178 	timeptr->tm_hour = bcd2bin(raw_time[2] & PCF8563_HOURS_MASK);
179 
180 	/* Get days */
181 	timeptr->tm_mday = bcd2bin(raw_time[3] & PCF8563_DAYS_MASK);
182 
183 	/* Get weekdays */
184 	timeptr->tm_wday = raw_time[4] & PCF8563_WEEKDAYS_MASK;
185 
186 	/* Get month */
187 	timeptr->tm_mon = bcd2bin(raw_time[5] & PCF8563_MONTHS_MASK);
188 
189 	/* Get year */
190 	timeptr->tm_year = bcd2bin(raw_time[6]);
191 
192 	/* Day number not used */
193 	timeptr->tm_yday = -1;
194 
195 	/* DST not used  */
196 	timeptr->tm_isdst = -1;
197 
198 	return 0;
199 }
200 
201 #ifdef PCF8563_INT1_GPIOS_IN_USE
202 
203 /* The logic related to the pin interrupt logic */
callback_work_handler(struct k_work * work)204 static void callback_work_handler(struct k_work *work)
205 {
206 	/* This function is run as a work so the user can spend here all the necessary time */
207 	struct pcf8563_data *data = CONTAINER_OF(work, struct pcf8563_data, callback_work);
208 
209 	if (data->alarm_callback == NULL) {
210 		LOG_WRN("No PCF8563 alarm callback function provided");
211 	} else {
212 		data->alarm_callback(data->dev, 0, data->alarm_user_data);
213 	}
214 }
215 
216 /* The function called when the clock alarm activates the interrupt*/
gpio_callback_function(const struct device * dev,struct gpio_callback * cb,uint32_t pins)217 static void gpio_callback_function(const struct device *dev, struct gpio_callback *cb,
218 		    uint32_t pins)
219 {
220 	struct pcf8563_data *data = CONTAINER_OF(cb, struct pcf8563_data, int1_callback);
221 
222 	LOG_DBG("PCF8563 interrupt detected");
223 	/* By using a work we are able to run "heavier" code */
224 	k_work_submit(&(data->callback_work));
225 
226 }
227 
228 #endif /* PCF8563_INT1_GPIOS_IN_USE */
229 
230 #ifdef CONFIG_RTC_ALARM
231 
pcf8563_alarm_get_supported_fields(const struct device * dev,uint16_t id,uint16_t * mask)232 static int pcf8563_alarm_get_supported_fields(const struct device *dev, uint16_t id,
233 					      uint16_t *mask)
234 {
235 	ARG_UNUSED(dev);
236 
237 	/* This device only has one channel*/
238 	if (id != 0) {
239 		LOG_ERR("invalid ID %d", id);
240 		return -EINVAL;
241 	}
242 
243 	*mask = PCF8563_RTC_ALARM_TIME_MASK;
244 
245 	return 0;
246 }
247 
pcf8563_alarm_set_time(const struct device * dev,uint16_t id,uint16_t mask,const struct rtc_time * timeptr)248 static int pcf8563_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
249 				  const struct rtc_time *timeptr)
250 {
251 	const struct pcf8563_config *config = dev->config;
252 	uint8_t regs[4];
253 	int ret;
254 
255 	if (id != 0) {
256 		LOG_ERR("invalid ID %d", id);
257 		return -EINVAL;
258 	}
259 
260 	if ((mask & ~(PCF8563_RTC_ALARM_TIME_MASK)) != 0) {
261 		LOG_ERR("invalid alarm field mask 0x%04x", mask);
262 		return -EINVAL;
263 	}
264 
265 	if (!rtc_utils_validate_rtc_time(timeptr, mask)) {
266 		LOG_ERR("invalid alarm time");
267 		return -EINVAL;
268 	}
269 
270 	/*
271 	 * The first bit is used as enabled/disabled flag.
272 	 * The mask will clean it and also the unused bits
273 	 */
274 	if ((mask & RTC_ALARM_TIME_MASK_MINUTE) != 0) {
275 		regs[0] = bin2bcd(timeptr->tm_min) & PCF8563_MINUTES_MASK;
276 	} else {
277 		/* First bit to 1 is alarm disabled */
278 		regs[0] = BIT(7);
279 	}
280 
281 	if ((mask & RTC_ALARM_TIME_MASK_HOUR) != 0) {
282 		regs[1] = bin2bcd(timeptr->tm_hour) & PCF8563_HOURS_MASK;
283 	} else {
284 		regs[1] = BIT(7);
285 	}
286 
287 	if ((mask & RTC_ALARM_TIME_MASK_MONTHDAY) != 0) {
288 		regs[2] = bin2bcd(timeptr->tm_mday) & PCF8563_DAYS_MASK;
289 	} else {
290 		regs[2] = BIT(7);
291 	}
292 
293 	if ((mask & RTC_ALARM_TIME_MASK_WEEKDAY) != 0) {
294 		regs[3] = bin2bcd(timeptr->tm_wday) & PCF8563_WEEKDAYS_MASK;
295 	} else {
296 		regs[3] = BIT(7);
297 	}
298 
299 	ret = i2c_burst_write_dt(&config->i2c, PCF8563_ALARM_REGISTER, regs, sizeof(regs));
300 	if (ret) {
301 		LOG_ERR("Error when setting alarm: %i", ret);
302 		return ret;
303 	}
304 
305 	/* Dont forget to enable interrupts */
306 	i2c_reg_write_byte_dt(
307 		&config->i2c,
308 		PCF8563_CONTROL2_REGISTER,
309 		PCF8563_CONTROL2_REGISTER_TIE_EN | PCF8563_CONTROL2_REGISTER_AIE_EN
310 	);
311 
312 	return 0;
313 }
314 
pcf8563_alarm_get_time(const struct device * dev,uint16_t id,uint16_t * mask,struct rtc_time * timeptr)315 static int pcf8563_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
316 				  struct rtc_time *timeptr)
317 {
318 	const struct pcf8563_config *config = dev->config;
319 	uint8_t regs[4];
320 	int err;
321 
322 	if (id != 0) {
323 		LOG_ERR("invalid ID %d", id);
324 		return -EINVAL;
325 	}
326 
327 	err = i2c_burst_read_dt(&config->i2c, PCF8563_ALARM_REGISTER, regs, sizeof(regs));
328 	if (err) {
329 		LOG_ERR("Error when getting alarm time: %i", err);
330 		return err;
331 	}
332 
333 	/* Initialize data structure and mask */
334 	memset(timeptr, 0U, sizeof(*timeptr));
335 	*mask = 0U;
336 
337 	/* The first bit is the enabled flag */
338 	if (!(regs[0] & BIT(7))) {
339 		timeptr->tm_min = bcd2bin(regs[0] & GENMASK(6, 0));
340 		*mask |= RTC_ALARM_TIME_MASK_MINUTE;
341 	}
342 
343 	if (!(regs[1] & BIT(7))) {
344 		timeptr->tm_hour = bcd2bin(regs[1] & GENMASK(5, 0));
345 		*mask |= RTC_ALARM_TIME_MASK_HOUR;
346 	}
347 
348 	if (!(regs[2] & BIT(7))) {
349 		timeptr->tm_mday = bcd2bin(regs[2] & GENMASK(5, 0));
350 		*mask |= RTC_ALARM_TIME_MASK_MONTHDAY;
351 	}
352 
353 	if (!(regs[3] & BIT(7))) {
354 		timeptr->tm_wday = bcd2bin(regs[3] & GENMASK(2, 0));
355 		*mask |= RTC_ALARM_TIME_MASK_WEEKDAY;
356 	}
357 
358 	return 0;
359 }
360 
pcf8563_alarm_is_pending(const struct device * dev,uint16_t id)361 static int pcf8563_alarm_is_pending(const struct device *dev, uint16_t id)
362 {
363 	/* The description of this register is at page 7, section 8.3.2 Register Control_status_2
364 	 * There are several kinds of alarms, but here we only need to know that anything but 0
365 	 * means that there was some kind of alarm active
366 	 */
367 	const struct pcf8563_config *config = dev->config;
368 	uint8_t reg;
369 	int err;
370 
371 	if (id != 0) {
372 		LOG_ERR("invalid ID %d", id);
373 		return -EINVAL;
374 	}
375 
376 	err = i2c_reg_read_byte_dt(&config->i2c, PCF8563_CONTROL2_REGISTER, &reg);
377 	if (err) {
378 		LOG_ERR("Error when getting the control register 2: %i", err);
379 		return err;
380 	}
381 
382 	/* Only the last bits use useful here */
383 	if (reg & GENMASK(3, 2)) {
384 		/* Clean the alarm */
385 		err = i2c_reg_write_byte_dt(&config->i2c, PCF8563_CONTROL2_REGISTER, GENMASK(1, 0));
386 		if (err) {
387 			LOG_ERR("Error when clearing alarms: %d", err);
388 			return err;
389 		}
390 		/* There was an alarm */
391 		return 1;
392 	}
393 	/* No alarms */
394 	return 0;
395 }
396 
pcf8563_alarm_set_callback(const struct device * dev,uint16_t id,rtc_alarm_callback callback,void * user_data)397 static int pcf8563_alarm_set_callback(const struct device *dev, uint16_t id,
398 				      rtc_alarm_callback callback, void *user_data)
399 {
400 #ifndef PCF8563_INT1_GPIOS_IN_USE
401 	ARG_UNUSED(dev);
402 	ARG_UNUSED(id);
403 	ARG_UNUSED(callback);
404 	ARG_UNUSED(user_data);
405 
406 	return -ENOTSUP;
407 #else /* PCF8563_INT1_GPIOS_IN_USE */
408 	const struct pcf8563_config *config = dev->config;
409 	struct pcf8563_data *data = dev->data;
410 	int ret;
411 
412 	if (config->int1.port == NULL) {
413 		return -ENOTSUP;
414 	}
415 
416 	if (id != 0) {
417 		LOG_ERR("invalid ID %d", id);
418 		return -EINVAL;
419 	}
420 
421 	data->alarm_callback = callback;
422 	data->alarm_user_data = user_data;
423 	data->dev = dev;
424 
425 	/* The PCF8563 int pin requires a pull up to work */
426 	ret = gpio_pin_configure_dt(&config->int1, GPIO_INPUT);
427 	if (ret < 0) {
428 		LOG_ERR("Error %d: failed to configure %s pin %d",
429 		       ret, config->int1.port->name, config->int1.pin);
430 		return ret;
431 	}
432 
433 	ret = gpio_pin_interrupt_configure_dt(&config->int1, GPIO_INT_EDGE_FALLING);
434 	if (ret < 0) {
435 		LOG_ERR("Error %d: failed to configure interrupt on %s pin %d",
436 			ret, config->int1.port->name, config->int1.pin);
437 		return ret;
438 	}
439 
440 
441 	gpio_init_callback(&data->int1_callback, gpio_callback_function, BIT(config->int1.pin));
442 	gpio_add_callback(config->int1.port, &data->int1_callback);
443 	LOG_DBG("Alarm set");
444 	return 0;
445 #endif /* PCF8563_INT1_GPIOS_IN_USE */
446 }
447 
448 #endif /* CONFIG_RTC_ALARM */
449 
450 static DEVICE_API(rtc, pcf8563_driver_api) = {
451 	.set_time = pcf8563_set_time,
452 	.get_time = pcf8563_get_time,
453 #ifdef CONFIG_RTC_ALARM
454 	.alarm_get_supported_fields = pcf8563_alarm_get_supported_fields,
455 	.alarm_set_time = pcf8563_alarm_set_time,
456 	.alarm_get_time = pcf8563_alarm_get_time,
457 	.alarm_is_pending = pcf8563_alarm_is_pending,
458 	.alarm_set_callback = pcf8563_alarm_set_callback,
459 #endif
460 };
461 
462 
pcf8563_init(const struct device * dev)463 static int pcf8563_init(const struct device *dev)
464 {
465 	const struct pcf8563_config *config = dev->config;
466 	int ret;
467 	uint8_t reg;
468 	#ifdef PCF8563_INT1_GPIOS_IN_USE
469 	struct pcf8563_data *data = dev->data;
470 
471 	data->callback_work = callback_work;
472 	#endif
473 
474 	if (!device_is_ready(config->i2c.bus)) {
475 		LOG_ERR("Failed to get pointer to %s device!", config->i2c.bus->name);
476 		return -ENODEV;
477 	}
478 
479 	/* Check if it's alive. */
480 	ret = i2c_reg_read_byte_dt(&config->i2c, PCF8563_CONTROL1_REGISTER, &reg);
481 	if (ret) {
482 		LOG_ERR("Failed to read from PCF85063! (err %i)", ret);
483 		return -ENODEV;
484 	}
485 
486 	LOG_INF("%s is initialized!", dev->name);
487 
488 	return 0;
489 }
490 
491 #define PCF8563_INIT(inst)                                                                         \
492 	static const struct pcf8563_config pcf8563_config_##inst = {                               \
493 		.i2c = I2C_DT_SPEC_INST_GET(inst),                                                 \
494 		IF_ENABLED(PCF8563_INT1_GPIOS_IN_USE,                                              \
495 		(.int1 = GPIO_DT_SPEC_INST_GET_OR(inst, int1_gpios, {0})))                         \
496 		};                                                                                 \
497                                                                                                    \
498 	static struct pcf8563_data pcf8563_data_##inst;                                            \
499                                                                                                    \
500 	DEVICE_DT_INST_DEFINE(inst, &pcf8563_init, NULL,                                           \
501 			      &pcf8563_data_##inst, &pcf8563_config_##inst, POST_KERNEL,           \
502 			      CONFIG_RTC_INIT_PRIORITY, &pcf8563_driver_api);
503 
504 DT_INST_FOREACH_STATUS_OKAY(PCF8563_INIT)
505