1/*
2 * Copyright 2017, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(DATA61_BSD)
11 */
12
13#pragma once
14
15#include <stdint.h>
16#include <platsupport/i2c.h>
17
18#define MAX77686RTC_BUSADDR 0xc
19
20struct rtc_time {
21    uint8_t sec;
22    uint8_t min;
23    uint8_t hour;
24/// Represented as a bitfield: (1 << #day)
25    uint8_t weekday;
26    uint8_t month;
27    uint8_t year;
28    uint8_t date;
29};
30
31typedef struct pmic_rtc {
32    i2c_slave_t i2c_slave;
33    i2c_kvslave_t kvslave;
34} pmic_rtc_t;
35
36/**
37 * Initialise the MAX77686 RTC
38 * @parma[in]  i2c      A handle to the I2C bus that the MAX77686 is connected to
39 * @param[out] pmic_rtc A handle to an rtc structure to initialise
40 * @return              0 on success
41 */
42int pmic_rtc_init(i2c_bus_t* i2c, pmic_rtc_t* pmic_rtc);
43
44/**
45 * Read the time from the MAX77686 RTC
46 * @param[in]  pmic_rtc A handle to the pmic_rtc
47 * @param[out] time     A time structure to populate
48 * @return              0 on success
49 */
50int pmic_rtc_get_time(pmic_rtc_t* pmic_rtc, struct rtc_time* time);
51
52/**
53 * Set the time on the MAX77686 RTC
54 * @param[in]  pmic_rtc A handle to the pmic_rtc
55 * @param[in]  time     The time to set
56 * @return              0 on success
57 */
58int pmic_rtc_set_time(pmic_rtc_t* pmic_rtc, const struct rtc_time* time);
59
60/**
61 * Return the number of alarms that this device supports
62 * @param[in]  pmic_rtc A handle to the pmic_rtc
63 * @return     The number of alarms that the device supports, -1 on error
64 */
65int pmic_rtc_nalarms(pmic_rtc_t* pmic_rtc);
66
67/**
68 * Set an alarm on the MAX77686 RTC
69 * @param[in]  pmic_rtc A handle to the pmic_rtc
70 * @param[in]  id       The alarm index to read
71 * @param[out] alarm    A time structure to populate
72 * @return              0 on success
73 */
74int pmic_rtc_get_alarm(pmic_rtc_t* pmic_rtc, int id, struct rtc_time* alarm);
75
76/**
77 * Set an alarm on the MAX77686 RTC
78 * @param[in]  pmic_rtc a handle to the pmic_rtc
79 * @param[in]  id       The alarm index to configure
80 * @param[out] alarm    a time structure to populate
81 * @return              0 on success
82 */
83int pmic_rtc_set_alarm(pmic_rtc_t* pmic_rtc, int id, const struct rtc_time* alarm);
84
85