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 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 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 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 
202 
203 #ifdef CONFIG_RTC_ALARM
204 
pcf8563_alarm_get_supported_fields(const struct device * dev,uint16_t id,uint16_t * mask)205 static int pcf8563_alarm_get_supported_fields(const struct device *dev, uint16_t id,
206 					      uint16_t *mask)
207 {
208 	ARG_UNUSED(dev);
209 
210 	/* This device only has one channel*/
211 	if (id != 0) {
212 		LOG_ERR("invalid ID %d", id);
213 		return -EINVAL;
214 	}
215 
216 	*mask = PCF8563_RTC_ALARM_TIME_MASK;
217 
218 	return 0;
219 }
220 
pcf8563_alarm_set_time(const struct device * dev,uint16_t id,uint16_t mask,const struct rtc_time * timeptr)221 static int pcf8563_alarm_set_time(const struct device *dev, uint16_t id, uint16_t mask,
222 				  const struct rtc_time *timeptr)
223 {
224 	const struct pcf8563_config *config = dev->config;
225 	uint8_t regs[4];
226 	int ret;
227 
228 	if (id != 0) {
229 		LOG_ERR("invalid ID %d", id);
230 		return -EINVAL;
231 	}
232 
233 	if ((mask & ~(PCF8563_RTC_ALARM_TIME_MASK)) != 0) {
234 		LOG_ERR("invalid alarm field mask 0x%04x", mask);
235 		return -EINVAL;
236 	}
237 
238 	if (!rtc_utils_validate_rtc_time(timeptr, mask)) {
239 		LOG_ERR("invalid alarm time");
240 		return -EINVAL;
241 	}
242 
243 	/*
244 	 * The first bit is used as enabled/disabled flag.
245 	 * The mask will clean it and also the unused bits
246 	 */
247 	if ((mask & RTC_ALARM_TIME_MASK_MINUTE) != 0) {
248 		regs[0] = bin2bcd(timeptr->tm_min) & PCF8563_MINUTES_MASK;
249 	} else {
250 		/* First bit to 1 is alarm disabled */
251 		regs[0] = BIT(7);
252 	}
253 
254 	if ((mask & RTC_ALARM_TIME_MASK_HOUR) != 0) {
255 		regs[1] = bin2bcd(timeptr->tm_hour) & PCF8563_HOURS_MASK;
256 	} else {
257 		regs[1] = BIT(7);
258 	}
259 
260 	if ((mask & RTC_ALARM_TIME_MASK_MONTHDAY) != 0) {
261 		regs[2] = bin2bcd(timeptr->tm_mday) & PCF8563_DAYS_MASK;
262 	} else {
263 		regs[2] = BIT(7);
264 	}
265 
266 	if ((mask & RTC_ALARM_TIME_MASK_WEEKDAY) != 0) {
267 		regs[3] = bin2bcd(timeptr->tm_wday) & PCF8563_WEEKDAYS_MASK;
268 	} else {
269 		regs[3] = BIT(7);
270 	}
271 
272 	ret = i2c_burst_write_dt(&config->i2c, PCF8563_ALARM_REGISTER, regs, sizeof(regs));
273 	if (ret) {
274 		LOG_ERR("Error when setting alarm: %i", ret);
275 		return ret;
276 	}
277 
278 	/* Dont forget to enable interrupts */
279 	i2c_reg_write_byte_dt(
280 		&config->i2c,
281 		PCF8563_CONTROL2_REGISTER,
282 		PCF8563_CONTROL2_REGISTER_TIE_EN | PCF8563_CONTROL2_REGISTER_AIE_EN
283 	);
284 
285 	return 0;
286 }
287 
pcf8563_alarm_get_time(const struct device * dev,uint16_t id,uint16_t * mask,struct rtc_time * timeptr)288 static int pcf8563_alarm_get_time(const struct device *dev, uint16_t id, uint16_t *mask,
289 				  struct rtc_time *timeptr)
290 {
291 	const struct pcf8563_config *config = dev->config;
292 	uint8_t regs[4];
293 	int err;
294 
295 	if (id != 0) {
296 		LOG_ERR("invalid ID %d", id);
297 		return -EINVAL;
298 	}
299 
300 	err = i2c_burst_read_dt(&config->i2c, PCF8563_ALARM_REGISTER, regs, sizeof(regs));
301 	if (err) {
302 		LOG_ERR("Error when getting alarm time: %i", err);
303 		return err;
304 	}
305 
306 	/* Initialize data structure and mask */
307 	memset(timeptr, 0U, sizeof(*timeptr));
308 	*mask = 0U;
309 
310 	/* The first bit is the enabled flag */
311 	if (regs[0] & BIT(7)) {
312 		timeptr->tm_min = bcd2bin(regs[0] & GENMASK(6, 0));
313 		*mask |= RTC_ALARM_TIME_MASK_MINUTE;
314 	}
315 
316 	if (regs[1] & BIT(7)) {
317 		timeptr->tm_hour = bcd2bin(regs[1] & GENMASK(5, 0));
318 		*mask |= RTC_ALARM_TIME_MASK_HOUR;
319 	}
320 
321 	if (regs[2] & BIT(7)) {
322 		timeptr->tm_mday = bcd2bin(regs[2] & GENMASK(5, 0));
323 		*mask |= RTC_ALARM_TIME_MASK_MONTHDAY;
324 	}
325 
326 	if (regs[3] & BIT(7)) {
327 		timeptr->tm_wday = bcd2bin(regs[3] & GENMASK(2, 0));
328 		*mask |= RTC_ALARM_TIME_MASK_WEEKDAY;
329 	}
330 
331 	return 0;
332 }
333 
pcf8563_alarm_is_pending(const struct device * dev,uint16_t id)334 static int pcf8563_alarm_is_pending(const struct device *dev, uint16_t id)
335 {
336 	/* The description of this register is at page 7, section 8.3.2 Register Control_status_2
337 	 * There are several kinds of alarms, but here we only need to know that anything but 0
338 	 * means that there was some kind of alarm active
339 	 */
340 	const struct pcf8563_config *config = dev->config;
341 	uint8_t reg;
342 	int err;
343 
344 	if (id != 0) {
345 		LOG_ERR("invalid ID %d", id);
346 		return -EINVAL;
347 	}
348 
349 	err = i2c_reg_read_byte_dt(&config->i2c, PCF8563_CONTROL2_REGISTER, &reg);
350 	if (err) {
351 		LOG_ERR("Error when getting the control register 2: %i", err);
352 		return err;
353 	}
354 
355 	/* Only the last bits use useful here */
356 	if (reg & GENMASK(3, 2)) {
357 		/* Clean the alarm */
358 		err = i2c_reg_write_byte_dt(&config->i2c, PCF8563_CONTROL2_REGISTER, GENMASK(1, 0));
359 		if (err) {
360 			LOG_ERR("Error when clearing alarms: %d", err);
361 			return err;
362 		}
363 		/* There was an alarm */
364 		return 1;
365 	}
366 	/* No alarms */
367 	return 0;
368 }
369 #endif
370 
371 #ifdef PCF8563_INT1_GPIOS_IN_USE
372 /* The logic related to the pin interrupt logic */
373 
callback_work_handler(struct k_work * work)374 void callback_work_handler(struct k_work *work)
375 {
376 	/* This function is run as a work so the user can spend here all the necessary time */
377 	struct pcf8563_data *data = CONTAINER_OF(work, struct pcf8563_data, callback_work);
378 
379 	if (data->alarm_callback == NULL) {
380 		LOG_WRN("No PCF8563 alarm callback function provided");
381 	} else {
382 		data->alarm_callback(data->dev, 0, data->alarm_user_data);
383 	}
384 }
385 
386 
387 /* The function called when the clock alarm activates the interrupt*/
gpio_callback_function(const struct device * dev,struct gpio_callback * cb,uint32_t pins)388 void gpio_callback_function(const struct device *dev, struct gpio_callback *cb,
389 		    uint32_t pins)
390 {
391 	struct pcf8563_data *data = CONTAINER_OF(cb, struct pcf8563_data, int1_callback);
392 
393 	LOG_DBG("PCF8563 interrupt detected");
394 	/* By using a work we are able to run "heavier" code */
395 	k_work_submit(&(data->callback_work));
396 
397 }
398 
399 #endif
400 
pcf8563_alarm_set_callback(const struct device * dev,uint16_t id,rtc_alarm_callback callback,void * user_data)401 static int pcf8563_alarm_set_callback(const struct device *dev, uint16_t id,
402 				      rtc_alarm_callback callback, void *user_data)
403 {
404 #ifndef PCF8563_INT1_GPIOS_IN_USE
405 	ARG_UNUSED(dev);
406 	ARG_UNUSED(id);
407 	ARG_UNUSED(callback);
408 	ARG_UNUSED(user_data);
409 
410 	return -ENOTSUP;
411 #else
412 	const struct pcf8563_config *config = dev->config;
413 	struct pcf8563_data *data = dev->data;
414 	int ret;
415 
416 	if (config->int1.port == NULL) {
417 		return -ENOTSUP;
418 	}
419 
420 	if (id != 0) {
421 		LOG_ERR("invalid ID %d", id);
422 		return -EINVAL;
423 	}
424 
425 	data->alarm_callback = callback;
426 	data->alarm_user_data = user_data;
427 	data->dev = dev;
428 
429 	/* The PCF8563 int pin requires a pull up to work */
430 	ret = gpio_pin_configure_dt(&config->int1, GPIO_INPUT | GPIO_PULL_UP);
431 	if (ret < 0) {
432 		LOG_ERR("Error %d: failed to configure %s pin %d",
433 		       ret, config->int1.port->name, config->int1.pin);
434 		return ret;
435 	}
436 
437 	ret = gpio_pin_interrupt_configure_dt(&config->int1, GPIO_INT_EDGE_FALLING);
438 	if (ret < 0) {
439 		LOG_ERR("Error %d: failed to configure interrupt on %s pin %d",
440 			ret, config->int1.port->name, config->int1.pin);
441 		return ret;
442 	}
443 
444 
445 	gpio_init_callback(&data->int1_callback, gpio_callback_function, BIT(config->int1.pin));
446 	gpio_add_callback(config->int1.port, &data->int1_callback);
447 	LOG_DBG("Alarm set");
448 	return 0;
449 #endif
450 }
451 
452 static DEVICE_API(rtc, pcf8563_driver_api) = {
453 	.set_time = pcf8563_set_time,
454 	.get_time = pcf8563_get_time,
455 #ifdef CONFIG_RTC_ALARM
456 	.alarm_get_supported_fields = pcf8563_alarm_get_supported_fields,
457 	.alarm_set_time = pcf8563_alarm_set_time,
458 	.alarm_get_time = pcf8563_alarm_get_time,
459 	.alarm_is_pending = pcf8563_alarm_is_pending,
460 	.alarm_set_callback = pcf8563_alarm_set_callback,
461 #endif
462 };
463 
464 
pcf8563_init(const struct device * dev)465 int pcf8563_init(const struct device *dev)
466 {
467 	const struct pcf8563_config *config = dev->config;
468 	int ret;
469 	uint8_t reg;
470 	#ifdef PCF8563_INT1_GPIOS_IN_USE
471 	struct pcf8563_data *data = dev->data;
472 
473 	data->callback_work = callback_work;
474 	#endif
475 
476 	if (!device_is_ready(config->i2c.bus)) {
477 		LOG_ERR("Failed to get pointer to %s device!", config->i2c.bus->name);
478 		return -ENODEV;
479 	}
480 
481 	/* Check if it's alive. */
482 	ret = i2c_reg_read_byte_dt(&config->i2c, PCF8563_CONTROL1_REGISTER, &reg);
483 	if (ret) {
484 		LOG_ERR("Failed to read from PCF85063! (err %i)", ret);
485 		return -ENODEV;
486 	}
487 
488 	LOG_INF("%s is initialized!", dev->name);
489 
490 	return 0;
491 }
492 
493 #define PCF8563_INIT(inst)                                                                         \
494 	static const struct pcf8563_config pcf8563_config_##inst = {                               \
495 		.i2c = I2C_DT_SPEC_INST_GET(inst),                                                 \
496 		IF_ENABLED(PCF8563_INT1_GPIOS_IN_USE,                                              \
497 		(.int1 = GPIO_DT_SPEC_INST_GET_OR(inst, int1_gpios, {0})))                         \
498 		};                                                                                 \
499                                                                                                    \
500 	static struct pcf8563_data pcf8563_data_##inst;                                            \
501                                                                                                    \
502 	DEVICE_DT_INST_DEFINE(inst, &pcf8563_init, NULL,                                           \
503 			      &pcf8563_data_##inst, &pcf8563_config_##inst, POST_KERNEL,           \
504 			      CONFIG_RTC_INIT_PRIORITY, &pcf8563_driver_api);
505 
506 DT_INST_FOREACH_STATUS_OKAY(PCF8563_INIT)
507