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