blob: 0d1077a404ba2757ee4c41c409414ef1a5e1fde9 [file] [log] [blame]
Anas Nashif9d6deb42015-11-21 20:58:15 -05001/*
2 * Copyright (c) 2015 Intel Corporation.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Anas Nashif9d6deb42015-11-21 20:58:15 -05005 */
6
7#ifndef _RTC_H_
8#define _RTC_H_
Kumar Gala78908162017-04-19 10:32:08 -05009#include <zephyr/types.h>
Anas Nashif9d6deb42015-11-21 20:58:15 -050010#include <device.h>
11#include <misc/util.h>
12
Peter Mitsisa0e45682016-01-22 12:38:49 -050013#ifdef __cplusplus
14extern "C" {
15#endif
16
Anas Nashif9d6deb42015-11-21 20:58:15 -050017enum clk_rtc_div {
Vinicius Costa Gomes45dcfe12015-12-18 18:47:17 -020018 RTC_CLK_DIV_1,
19 RTC_CLK_DIV_2,
20 RTC_CLK_DIV_4,
21 RTC_CLK_DIV_8,
22 RTC_CLK_DIV_16,
23 RTC_CLK_DIV_32,
24 RTC_CLK_DIV_64,
25 RTC_CLK_DIV_128,
26 RTC_CLK_DIV_256,
27 RTC_CLK_DIV_512,
28 RTC_CLK_DIV_1024,
29 RTC_CLK_DIV_2048,
30 RTC_CLK_DIV_4096,
31 RTC_CLK_DIV_8192,
32 RTC_CLK_DIV_16384,
33 RTC_CLK_DIV_32768
Anas Nashif9d6deb42015-11-21 20:58:15 -050034};
Anas Nashif9d6deb42015-11-21 20:58:15 -050035
Vinicius Costa Gomes45dcfe12015-12-18 18:47:17 -020036#define RTC_DIVIDER RTC_CLK_DIV_1
Anas Nashif9d6deb42015-11-21 20:58:15 -050037
38/** Number of RTC ticks in a second */
Vinicius Costa Gomes45dcfe12015-12-18 18:47:17 -020039#define RTC_ALARM_SECOND (32768 / (1UL << RTC_DIVIDER))
Anas Nashif9d6deb42015-11-21 20:58:15 -050040
41/** Number of RTC ticks in a minute */
42#define RTC_ALARM_MINUTE (RTC_ALARM_SECOND * 60)
43
44/** Number of RTC ticks in an hour */
45#define RTC_ALARM_HOUR (RTC_ALARM_MINUTE * 60)
46
47/** Number of RTC ticks in a day */
48#define RTC_ALARM_DAY (RTC_ALARM_HOUR * 24)
49
50
51
52struct rtc_config {
Kumar Galacc334c72017-04-21 10:55:34 -050053 u32_t init_val;
Anas Nashif9d6deb42015-11-21 20:58:15 -050054 /*!< enable/disable alarm */
Kumar Galacc334c72017-04-21 10:55:34 -050055 u8_t alarm_enable;
Anas Nashif9d6deb42015-11-21 20:58:15 -050056 /*!< initial configuration value for the 32bit RTC alarm value */
Kumar Galacc334c72017-04-21 10:55:34 -050057 u32_t alarm_val;
Tomasz Bursztyka466cb002015-12-02 19:06:24 +010058 /*!< Pointer to function to call when alarm value
59 * matches current RTC value */
60 void (*cb_fn)(struct device *dev);
Anas Nashif9d6deb42015-11-21 20:58:15 -050061};
62
Tomasz Bursztyka466cb002015-12-02 19:06:24 +010063typedef void (*rtc_api_enable)(struct device *dev);
64typedef void (*rtc_api_disable)(struct device *dev);
65typedef int (*rtc_api_set_config)(struct device *dev,
66 struct rtc_config *config);
67typedef int (*rtc_api_set_alarm)(struct device *dev,
Kumar Galacc334c72017-04-21 10:55:34 -050068 const u32_t alarm_val);
69typedef u32_t (*rtc_api_read)(struct device *dev);
70typedef u32_t (*rtc_api_get_pending_int)(struct device *dev);
Anas Nashif9d6deb42015-11-21 20:58:15 -050071
72struct rtc_driver_api {
73 rtc_api_enable enable;
74 rtc_api_disable disable;
75 rtc_api_read read;
76 rtc_api_set_config set_config;
77 rtc_api_set_alarm set_alarm;
Julien Delayenee04a3f2016-11-03 13:45:31 +000078 rtc_api_get_pending_int get_pending_int;
Anas Nashif9d6deb42015-11-21 20:58:15 -050079};
80
Andrew Boie9ca42fb2017-10-25 13:26:14 -070081__syscall u32_t rtc_read(struct device *dev);
82
83static inline u32_t _impl_rtc_read(struct device *dev)
Anas Nashif9d6deb42015-11-21 20:58:15 -050084{
Marcus Shawcroft39d34d72016-10-22 10:01:38 +010085 const struct rtc_driver_api *api = dev->driver_api;
Anas Nashif9d6deb42015-11-21 20:58:15 -050086
Tomasz Bursztyka466cb002015-12-02 19:06:24 +010087 return api->read(dev);
Anas Nashif9d6deb42015-11-21 20:58:15 -050088}
89
Andrew Boie9ca42fb2017-10-25 13:26:14 -070090__syscall void rtc_enable(struct device *dev);
91
92static inline void _impl_rtc_enable(struct device *dev)
Anas Nashif9d6deb42015-11-21 20:58:15 -050093{
Marcus Shawcroft39d34d72016-10-22 10:01:38 +010094 const struct rtc_driver_api *api = dev->driver_api;
Anas Nashif9d6deb42015-11-21 20:58:15 -050095
Tomasz Bursztyka466cb002015-12-02 19:06:24 +010096 api->enable(dev);
Anas Nashif9d6deb42015-11-21 20:58:15 -050097}
98
Andrew Boie9ca42fb2017-10-25 13:26:14 -070099__syscall void rtc_disable(struct device *dev);
Anas Nashif9d6deb42015-11-21 20:58:15 -0500100
Andrew Boie9ca42fb2017-10-25 13:26:14 -0700101static inline void _impl_rtc_disable(struct device *dev)
Anas Nashif9d6deb42015-11-21 20:58:15 -0500102{
Marcus Shawcroft39d34d72016-10-22 10:01:38 +0100103 const struct rtc_driver_api *api = dev->driver_api;
Anas Nashif9d6deb42015-11-21 20:58:15 -0500104
Tomasz Bursztyka466cb002015-12-02 19:06:24 +0100105 api->disable(dev);
Anas Nashif9d6deb42015-11-21 20:58:15 -0500106}
107
Tomasz Bursztyka466cb002015-12-02 19:06:24 +0100108static inline int rtc_set_config(struct device *dev,
109 struct rtc_config *cfg)
Anas Nashif9d6deb42015-11-21 20:58:15 -0500110{
Marcus Shawcroft39d34d72016-10-22 10:01:38 +0100111 const struct rtc_driver_api *api = dev->driver_api;
Anas Nashif9d6deb42015-11-21 20:58:15 -0500112
Tomasz Bursztyka466cb002015-12-02 19:06:24 +0100113 return api->set_config(dev, cfg);
Anas Nashif9d6deb42015-11-21 20:58:15 -0500114}
115
Andrew Boie9ca42fb2017-10-25 13:26:14 -0700116__syscall int rtc_set_alarm(struct device *dev, const u32_t alarm_val);
117
118static inline int _impl_rtc_set_alarm(struct device *dev,
119 const u32_t alarm_val)
Anas Nashif9d6deb42015-11-21 20:58:15 -0500120{
Marcus Shawcroft39d34d72016-10-22 10:01:38 +0100121 const struct rtc_driver_api *api = dev->driver_api;
Anas Nashif9d6deb42015-11-21 20:58:15 -0500122
Tomasz Bursztyka466cb002015-12-02 19:06:24 +0100123 return api->set_alarm(dev, alarm_val);
Anas Nashif9d6deb42015-11-21 20:58:15 -0500124}
125
Julien Delayenee04a3f2016-11-03 13:45:31 +0000126/**
127 * @brief Function to get pending interrupts
128 *
129 * The purpose of this function is to return the interrupt
130 * status register for the device.
131 * This is especially useful when waking up from
132 * low power states to check the wake up source.
133 *
134 * @param dev Pointer to the device structure for the driver instance.
135 *
136 * @retval 1 if the rtc interrupt is pending.
137 * @retval 0 if no rtc interrupt is pending.
138 */
Andrew Boie9ca42fb2017-10-25 13:26:14 -0700139__syscall int rtc_get_pending_int(struct device *dev);
140
141static inline int _impl_rtc_get_pending_int(struct device *dev)
Julien Delayenee04a3f2016-11-03 13:45:31 +0000142{
143 struct rtc_driver_api *api;
144
145 api = (struct rtc_driver_api *)dev->driver_api;
146 return api->get_pending_int(dev);
147}
148
Peter Mitsisa0e45682016-01-22 12:38:49 -0500149#ifdef __cplusplus
150}
151#endif
152
Andrew Boie9ca42fb2017-10-25 13:26:14 -0700153#include <syscalls/rtc.h>
154
Anas Nashif9d6deb42015-11-21 20:58:15 -0500155#endif