summaryrefslogtreecommitdiff
path: root/components/drivers/rtc/rtc-rx8010.c
blob: b5600c95f726fa90f9d18e58c163a7639daf55f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-11-26     GuEe-GUI     first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "rtc.rx8010"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include "rtc_dm.h"

#define RX8010_SEC              0x10
#define RX8010_MIN              0x11
#define RX8010_HOUR             0x12
#define RX8010_WDAY             0x13
#define RX8010_MDAY             0x14
#define RX8010_MONTH            0x15
#define RX8010_YEAR             0x16
#define RX8010_RESV17           0x17
#define RX8010_ALMIN            0x18
#define RX8010_ALHOUR           0x19
#define RX8010_ALWDAY           0x1a
#define RX8010_TCOUNT0          0x1b
#define RX8010_TCOUNT1          0x1c
#define RX8010_EXT              0x1d
#define RX8010_FLAG             0x1e
#define RX8010_CTRL             0x1f
/* 0x20 to 0x2F are user registers */
#define RX8010_RESV30           0x30
#define RX8010_RESV31           0x31
#define RX8010_IRQ              0x32

#define RX8010_EXT_WADA         RT_BIT(3)

#define RX8010_FLAG_VLF         RT_BIT(1)
#define RX8010_FLAG_AF          RT_BIT(3)
#define RX8010_FLAG_TF          RT_BIT(4)
#define RX8010_FLAG_UF          RT_BIT(5)

#define RX8010_CTRL_AIE         RT_BIT(3)
#define RX8010_CTRL_UIE         RT_BIT(5)
#define RX8010_CTRL_STOP        RT_BIT(6)
#define RX8010_CTRL_TEST        RT_BIT(7)

#define RX8010_ALARM_AE         RT_BIT(7)

struct rx8010_rtc
{
    struct rt_device parent;

    int irq;
    rt_uint8_t ctrlreg;

    struct rt_i2c_client *client;
    struct rt_thread *irq_thread;

    struct rt_rtc_wkalarm wkalarm;
};

#define raw_to_rx8010_rtc(raw) rt_container_of(raw, struct rx8010_rtc, parent)

static rt_err_t rx8010_rtc_write(struct rx8010_rtc *rx8010,
        rt_uint8_t reg, rt_uint8_t value)
{
    rt_int32_t res;
    struct rt_i2c_msg msg[1];
    rt_uint8_t data[sizeof(reg) + sizeof(value)] = { reg };
    struct rt_i2c_client *client = rx8010->client;

    rt_memcpy(&data[sizeof(reg)], &value, sizeof(value));

    msg[0].buf = data;
    msg[0].addr = client->client_addr;
    msg[0].len = sizeof(data);
    msg[0].flags = RT_I2C_WR;

    res = rt_i2c_transfer(client->bus, msg, 1);

    return res > 0 ? RT_EOK : res;
}

static rt_err_t rx8010_rtc_read(struct rx8010_rtc *rx8010,
        rt_uint8_t reg, rt_uint8_t *values)
{
    rt_int32_t res;
    struct rt_i2c_msg msg[2];
    struct rt_i2c_client *client = rx8010->client;

    msg[0].buf = &reg;
    msg[0].addr = client->client_addr;
    msg[0].len = sizeof(reg);
    msg[0].flags = RT_I2C_WR;

    msg[1].buf = (rt_uint8_t *)values;
    msg[1].addr = client->client_addr;
    msg[1].len = sizeof(*values);
    msg[1].flags = RT_I2C_RD;

    res = rt_i2c_transfer(client->bus, msg, 2);

    return res > 0 ? RT_EOK : res;
}

static rt_err_t rx8010_rtc_set_bit(struct rx8010_rtc *rx8010,
        rt_uint8_t reg, rt_uint8_t bit)
{
    rt_err_t err;
    rt_uint8_t value;

    if ((err = rx8010_rtc_read(rx8010, reg, &value)))
    {
        return err;
    }

    return rx8010_rtc_write(rx8010, reg, value | bit);
}

static rt_err_t rx8010_rtc_clear_bit(struct rx8010_rtc *rx8010,
        rt_uint8_t reg, rt_uint8_t bit)
{
    rt_err_t err;
    rt_uint8_t value;

    if ((err = rx8010_rtc_read(rx8010, reg, &value)))
    {
        return err;
    }

    return rx8010_rtc_write(rx8010, reg, value & ~bit);
}

static rt_err_t rx8010_rtc_read_time(struct rx8010_rtc *rx8010, time_t *sec)
{
    struct tm tm;
    rt_err_t err;
    rt_uint8_t flagreg, date[RX8010_YEAR - RX8010_SEC + 1];

    if ((err = rx8010_rtc_read(rx8010, RX8010_FLAG, &flagreg)))
    {
        return err;
    }

    if (flagreg & RX8010_FLAG_VLF)
    {
        LOG_W("Frequency stop detected");

        return -RT_EINVAL;
    }

    for (int i = 0; i < sizeof(date); ++i)
    {
        if ((err = rx8010_rtc_read(rx8010, RX8010_SEC + i, &date[i])))
        {
            return err;
        }
    }

    tm.tm_sec = rt_bcd2bin(date[RX8010_SEC - RX8010_SEC] & 0x7f);
    tm.tm_min = rt_bcd2bin(date[RX8010_MIN - RX8010_SEC] & 0x7f);
    tm.tm_hour = rt_bcd2bin(date[RX8010_HOUR - RX8010_SEC] & 0x3f);
    tm.tm_mday = rt_bcd2bin(date[RX8010_MDAY - RX8010_SEC] & 0x3f);
    tm.tm_mon = rt_bcd2bin(date[RX8010_MONTH - RX8010_SEC] & 0x1f) - 1;
    tm.tm_year = rt_bcd2bin(date[RX8010_YEAR - RX8010_SEC]) + 100;
    tm.tm_wday = __rt_ffs(date[RX8010_WDAY - RX8010_SEC] & 0x7f);

    *sec = timegm(&tm);

    return RT_EOK;
}

static rt_err_t rx8010_rtc_set_time(struct rx8010_rtc *rx8010, time_t *sec)
{
    rt_err_t err;
    struct tm *tm;
    rt_uint8_t date[RX8010_YEAR - RX8010_SEC + 1];

    /* Set STOP bit before changing clock/calendar */
    if ((err = rx8010_rtc_set_bit(rx8010, RX8010_CTRL, RX8010_CTRL_STOP)))
    {
        return err;
    }

    tm = localtime(sec);

    date[RX8010_SEC - RX8010_SEC] = rt_bin2bcd(tm->tm_sec);
    date[RX8010_MIN - RX8010_SEC] = rt_bin2bcd(tm->tm_min);
    date[RX8010_HOUR - RX8010_SEC] = rt_bin2bcd(tm->tm_hour);
    date[RX8010_MDAY - RX8010_SEC] = rt_bin2bcd(tm->tm_mday);
    date[RX8010_MONTH - RX8010_SEC] = rt_bin2bcd(tm->tm_mon + 1);
    date[RX8010_YEAR - RX8010_SEC] = rt_bin2bcd(tm->tm_year - 100);
    date[RX8010_WDAY - RX8010_SEC] = rt_bin2bcd(1 << tm->tm_wday);

    for (int i = 0; i < sizeof(date); ++i)
    {
        if ((err = rx8010_rtc_write(rx8010, RX8010_SEC + i, date[i])))
        {
            return err;
        }
    }

    /* Clear STOP bit after changing clock/calendar */
    if ((err = rx8010_rtc_clear_bit(rx8010, RX8010_CTRL, RX8010_CTRL_STOP)))
    {
        return err;
    }

    if ((err = rx8010_rtc_clear_bit(rx8010, RX8010_FLAG, RX8010_FLAG_VLF)))
    {
        return err;
    }

    return RT_EOK;
}

static rt_err_t rx8010_rtc_read_alarm(struct rx8010_rtc *rx8010,
        struct rt_rtc_wkalarm *alarm)
{
    rt_err_t err;
    rt_uint8_t alarmvals[3], flagreg;

    for (int i = 0; i < RT_ARRAY_SIZE(alarmvals); ++i)
    {
        if ((err = rx8010_rtc_read(rx8010, RX8010_ALMIN + i, &alarmvals[i])))
        {
            return err;
        }
    }

    if ((err = rx8010_rtc_read(rx8010, RX8010_FLAG, &flagreg)))
    {
        return err;
    }

    alarm->tm_sec = 0;
    alarm->tm_min = rt_bcd2bin(alarmvals[0] & 0x7f);
    alarm->tm_hour = rt_bcd2bin(alarmvals[1] & 0x3f);

    if (!(alarmvals[2] & RX8010_ALARM_AE))
    {
        alarm->tm_mday = rt_bcd2bin(alarmvals[2] & 0x7f);
    }

    alarm->enable = !!(rx8010->ctrlreg & RX8010_CTRL_AIE);

    return RT_EOK;
}

static rt_err_t rx8010_alarm_irq_enable(struct rx8010_rtc *rx8010, rt_bool_t enabled)
{
    rt_err_t err;
    rt_uint8_t ctrl;

    ctrl = rx8010->ctrlreg;

    if (enabled)
    {
        ctrl |= RX8010_CTRL_AIE | RX8010_CTRL_UIE;
    }
    else
    {
        ctrl &= ~(RX8010_CTRL_UIE | RX8010_CTRL_AIE);
    }

    if ((err = rx8010_rtc_clear_bit(rx8010, RX8010_FLAG, RX8010_FLAG_AF)))
    {
        return err;
    }

    if (ctrl != rx8010->ctrlreg)
    {
        rx8010->ctrlreg = ctrl;

        if ((err = rx8010_rtc_write(rx8010, RX8010_CTRL, rx8010->ctrlreg)))
        {
            return err;
        }
    }

    return RT_EOK;
}

static rt_err_t rx8010_rtc_set_alarm(struct rx8010_rtc *rx8010,
        struct rt_rtc_wkalarm *alarm)
{
    rt_err_t err;
    rt_uint8_t alarmvals[3];
    struct rt_rtc_wkalarm *wkalarm = &rx8010->wkalarm;

    if (rx8010->ctrlreg & (RX8010_CTRL_AIE | RX8010_CTRL_UIE))
    {
        rx8010->ctrlreg &= ~(RX8010_CTRL_AIE | RX8010_CTRL_UIE);

        if ((err = rx8010_rtc_write(rx8010, RX8010_CTRL, rx8010->ctrlreg)))
        {
            return err;
        }
    }

    if ((err = rx8010_rtc_clear_bit(rx8010, RX8010_FLAG, RX8010_FLAG_AF)))
    {
        return err;
    }

    alarmvals[0] = rt_bin2bcd(alarm->tm_min);
    alarmvals[1] = rt_bin2bcd(alarm->tm_hour);
    alarmvals[2] = rt_bin2bcd(alarm->tm_mday);

    for (int i = 0; i < RT_ARRAY_SIZE(alarmvals) - 1; ++i)
    {
        if ((err = rx8010_rtc_write(rx8010, RX8010_ALMIN + i, alarmvals[i])))
        {
            return err;
        }
    }

    if ((err = rx8010_rtc_clear_bit(rx8010, RX8010_EXT, RX8010_EXT_WADA)))
    {
        return err;
    }

    if (alarmvals[2] == 0)
    {
        alarmvals[2] |= RX8010_ALARM_AE;
    }

    if ((err = rx8010_rtc_write(rx8010, RX8010_ALWDAY, alarmvals[2])))
    {
        return err;
    }

    if (alarm->enable)
    {
        rx8010->ctrlreg |= RX8010_CTRL_UIE | RX8010_CTRL_AIE;

        if ((err = rx8010_rtc_write(rx8010, RX8010_CTRL, rx8010->ctrlreg)))
        {
            return err;
        }
    }

    if (!(err = rx8010_alarm_irq_enable(rx8010, alarm->enable)))
    {
        wkalarm->enable = alarm->enable;
        wkalarm->tm_hour = alarm->tm_hour;
        wkalarm->tm_min = alarm->tm_min;
        wkalarm->tm_sec = alarm->tm_sec;
    }

    return err;
}

static rt_err_t rx8010_rtc_control(rt_device_t dev, int cmd, void *args)
{
    rt_err_t err = RT_EOK;
    struct rx8010_rtc *rx8010 = raw_to_rx8010_rtc(dev);

    if (!args)
    {
        return -RT_EINVAL;
    }

    switch (cmd)
    {
    case RT_DEVICE_CTRL_RTC_GET_TIME:
        err = rx8010_rtc_read_time(rx8010, args);
        break;

    case RT_DEVICE_CTRL_RTC_SET_TIME:
        err = rx8010_rtc_set_time(rx8010, args);
        break;

    case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
        err = rx8010_rtc_read_time(rx8010, (time_t *)&((struct timeval *)args)->tv_sec);
        break;

    case RT_DEVICE_CTRL_RTC_SET_TIMEVAL:
        err = rx8010_rtc_set_time(rx8010, (time_t *)&((struct timeval *)args)->tv_sec);
        break;

    case RT_DEVICE_CTRL_RTC_GET_ALARM:
        err = rx8010_rtc_read_alarm(rx8010, args);
        break;

    case RT_DEVICE_CTRL_RTC_SET_ALARM:
        err = rx8010_rtc_set_alarm(rx8010, args);
        break;

    default:
        err = -RT_EINVAL;
        break;
    }

    return err;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops rx8010_rtc_ops =
{
    .control = rx8010_rtc_control,
};
#endif

static void rx8010_rtc_thread_isr(void *param)
{
    rt_err_t err;
    rt_uint8_t flagreg;
    struct rx8010_rtc *rx8010 = param;

    while (RT_TRUE)
    {
        rt_thread_suspend(rx8010->irq_thread);
        rt_schedule();

        if ((err = rx8010_rtc_read(rx8010, RX8010_FLAG, &flagreg)))
        {
            LOG_E("Read flag error = %s", rt_strerror(err));
            continue;
        }

        if (flagreg & RX8010_FLAG_VLF)
        {
            LOG_W("Frequency stop detected");
        }

        if (flagreg & RX8010_FLAG_TF)
        {
            flagreg &= ~RX8010_FLAG_TF;
            rt_alarm_update(&rx8010->parent, 1);
        }

        if (flagreg & RX8010_FLAG_AF)
        {
            flagreg &= ~RX8010_FLAG_AF;
            rt_alarm_update(&rx8010->parent, 1);
        }

        if (flagreg & RX8010_FLAG_UF)
        {
            flagreg &= ~RX8010_FLAG_UF;
            rt_alarm_update(&rx8010->parent, 1);
        }

        if ((err = rx8010_rtc_write(rx8010, RX8010_FLAG, flagreg)))
        {
            LOG_E("Write flag error = %s", rt_strerror(err));
        }
    }
}

static void rx8010_rtc_isr(int irqno, void *param)
{
    struct rx8010_rtc *rx8010 = param;

    rt_thread_resume(rx8010->irq_thread);
}

static rt_err_t rx8010_init(struct rx8010_rtc *rx8010)
{
    rt_err_t err;
    rt_uint8_t ctrl[2];
    int need_clear = 0;

    /* Initialize reserved registers as specified in datasheet */
    if ((err = rx8010_rtc_write(rx8010, RX8010_RESV17, 0xd8)))
    {
        return err;
    }

    if ((err = rx8010_rtc_write(rx8010, RX8010_RESV30, 0x00)))
    {
        return err;
    }

    if ((err = rx8010_rtc_write(rx8010, RX8010_RESV31, 0x08)))
    {
        return err;
    }

    if ((err = rx8010_rtc_write(rx8010, RX8010_IRQ, 0x00)))
    {
        return err;
    }

    err |= rx8010_rtc_read(rx8010, RX8010_FLAG, &ctrl[0]);
    err |= rx8010_rtc_read(rx8010, RX8010_FLAG + 1, &ctrl[1]);

    if (err)
    {
        return err;
    }

    if (ctrl[0] & RX8010_FLAG_VLF)
    {
        LOG_W("Frequency stop was detected");
    }

    if (ctrl[0] & RX8010_FLAG_AF)
    {
        LOG_W("Alarm was detected");
        need_clear = 1;
    }

    if (ctrl[0] & (RX8010_FLAG_TF | RX8010_FLAG_UF))
    {
        need_clear = 1;
    }

    if (need_clear)
    {
        ctrl[0] &= ~(RX8010_FLAG_AF | RX8010_FLAG_TF | RX8010_FLAG_UF);

        if ((err = rx8010_rtc_write(rx8010, RX8010_FLAG, ctrl[0])))
        {
            return err;
        }
    }

    rx8010->ctrlreg = ctrl[1] & ~RX8010_CTRL_TEST;

    return RT_EOK;
}

static rt_err_t rx8010_rtc_probe(struct rt_i2c_client *client)
{
    rt_err_t err;
    const char *dev_name;
    struct rt_device *dev = &client->parent;
    struct rx8010_rtc *rx8010 = rt_calloc(1, sizeof(*rx8010));

    if (!rx8010)
    {
        return -RT_ENOMEM;
    }

    rx8010->client = client;

    if ((err = rx8010_init(rx8010)))
    {
        goto _fail;
    }

    rx8010->irq = rt_dm_dev_get_irq(dev, 0);

    if (rx8010->irq >= 0)
    {
        rx8010->irq_thread = rt_thread_create("rtc-rx8010", &rx8010_rtc_thread_isr,
                rx8010, DM_THREAD_STACK_SIZE, RT_THREAD_PRIORITY_MAX / 2, 10);

        if (!rx8010->irq_thread)
        {
            err = -RT_ERROR;
            LOG_E("Create RTC IRQ thread fail");
            goto _fail;
        }

        rt_thread_startup(rx8010->irq_thread);

        rt_hw_interrupt_install(rx8010->irq, rx8010_rtc_isr, rx8010, "rtc-rx8010");
        rt_hw_interrupt_umask(rx8010->irq);
    }

    dev->user_data = rx8010;

    rx8010->parent.type = RT_Device_Class_RTC;
#ifdef RT_USING_DEVICE_OPS
    rx8010->parent.ops = &rx8010_rtc_ops;
#else
    rx8010->parent.control = rx8010_rtc_control;
#endif

    rtc_dev_set_name(&rx8010->parent);
    dev_name = rt_dm_dev_get_name(&rx8010->parent);
    rt_device_register(&rx8010->parent, dev_name, RT_DEVICE_FLAG_RDWR);

    return RT_EOK;

_fail:
    if (rx8010->irq_thread)
    {
        rt_thread_delete(rx8010->irq_thread);
    }

    rt_free(rx8010);

    return err;
}

static rt_err_t rx8010_rtc_remove(struct rt_i2c_client *client)
{
    struct rx8010_rtc *rx8010 = client->parent.user_data;

    rx8010_rtc_set_bit(rx8010, RX8010_CTRL, RX8010_CTRL_STOP);

    if (rx8010->irq >= 0)
    {
        rt_hw_interrupt_mask(rx8010->irq);
        rt_pic_detach_irq(rx8010->irq, rx8010);

        rt_thread_delete(rx8010->irq_thread);
    }

    rt_device_unregister(&rx8010->parent);

    rt_free(rx8010);

    return RT_EOK;
}

static const struct rt_i2c_device_id rx8010_rtc_ids[] =
{
    { .name = "rx8010" },
    { /* sentinel */ },
};

static const struct rt_ofw_node_id rx8010_rtc_ofw_ids[] =
{
    { .compatible = "epson,rx8010" },
    { /* sentinel */ },
};

static struct rt_i2c_driver rx8010_rtc_driver =
{
    .ids = rx8010_rtc_ids,
    .ofw_ids = rx8010_rtc_ofw_ids,

    .probe = rx8010_rtc_probe,
    .remove = rx8010_rtc_remove,
};
RT_I2C_DRIVER_EXPORT(rx8010_rtc_driver);