1 /* Copyright 2019 SiFive, Inc. */
2 /* SPDX-License-Identifier: Apache-2.0 */
11 * @brief API for Real-Time Clocks
17 * @brief List of RTC run behaviors
19 enum metal_rtc_run_option {
24 struct metal_rtc_vtable {
25 uint64_t (*get_rate)(const struct metal_rtc *const rtc);
26 uint64_t (*set_rate)(const struct metal_rtc *const rtc, const uint64_t rate);
27 uint64_t (*get_compare)(const struct metal_rtc *const rtc);
28 uint64_t (*set_compare)(const struct metal_rtc *const rtc, const uint64_t compare);
29 uint64_t (*get_count)(const struct metal_rtc *const rtc);
30 uint64_t (*set_count)(const struct metal_rtc *const rtc, const uint64_t count);
31 int (*run)(const struct metal_rtc *const rtc, const enum metal_rtc_run_option option);
32 struct metal_interrupt *(*get_interrupt)(const struct metal_rtc *const rtc);
33 int (*get_interrupt_id)(const struct metal_rtc *const rtc);
37 * @brief Handle for a Real-Time Clock
40 const struct metal_rtc_vtable *vtable;
44 * @brief Get the rate of the RTC
45 * @return The rate in Hz
47 inline uint64_t metal_rtc_get_rate(const struct metal_rtc *const rtc) {
48 return rtc->vtable->get_rate(rtc);
52 * @brief Set (if possible) the rate of the RTC
53 * @return The new rate of the RTC (not guaranteed to be the same as requested)
55 inline uint64_t metal_rtc_set_rate(const struct metal_rtc *const rtc, const uint64_t rate) {
56 return rtc->vtable->set_rate(rtc, rate);
60 * @brief Get the compare value of the RTC
61 * @return The compare value
63 inline uint64_t metal_rtc_get_compare(const struct metal_rtc *const rtc) {
64 return rtc->vtable->get_compare(rtc);
68 * @brief Set the compare value of the RTC
69 * @return The set compare value (not guaranteed to be exactly the requested value)
71 * The RTC device might impose limits on the maximum compare value or the granularity
72 * of the compare value.
74 inline uint64_t metal_rtc_set_compare(const struct metal_rtc *const rtc, const uint64_t compare) {
75 return rtc->vtable->set_compare(rtc, compare);
79 * @brief Get the current count of the RTC
82 inline uint64_t metal_rtc_get_count(const struct metal_rtc *const rtc) {
83 return rtc->vtable->get_count(rtc);
87 * @brief Set the current count of the RTC
88 * @return The set value of the count (not guaranteed to be exactly the requested value)
90 * The RTC device might impose limits on the maximum value of the count
92 inline uint64_t metal_rtc_set_count(const struct metal_rtc *const rtc, const uint64_t count) {
93 return rtc->vtable->set_count(rtc, count);
97 * @brief Start or stop the RTC
98 * @return 0 if the RTC was successfully started/stopped
100 inline int metal_rtc_run(const struct metal_rtc *const rtc, const enum metal_rtc_run_option option) {
101 return rtc->vtable->run(rtc, option);
105 * @brief Get the interrupt handle for the RTC compare
106 * @return The interrupt handle
108 inline struct metal_interrupt *metal_rtc_get_interrupt(const struct metal_rtc *const rtc) {
109 return rtc->vtable->get_interrupt(rtc);
113 * @brief Get the interrupt ID for the RTC compare
114 * @return The interrupt ID
116 inline int metal_rtc_get_interrupt_id(const struct metal_rtc *const rtc) {
117 return rtc->vtable->get_interrupt_id(rtc);
121 * @brief Get the handle for an RTC by index
122 * @return The RTC handle, or NULL if none is available at that index
124 struct metal_rtc *metal_rtc_get_device(int index);