1 /*
2  * Copyright (c) 2023 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public APIs for Time-aware GPIO drivers
10  */
11 #ifndef ZEPHYR_DRIVERS_MISC_TIMEAWARE_GPIO_TIMEAWARE_GPIO
12 #define ZEPHYR_DRIVERS_MISC_TIMEAWARE_GPIO_TIMEAWARE_GPIO
13 
14 /**
15  * @brief Time-aware GPIO Interface
16  * @defgroup tgpio_interface Time-aware GPIO Interface
17  * @ingroup io_interfaces
18  * @{
19  */
20 
21 #include <zephyr/sys/__assert.h>
22 #include <zephyr/sys/slist.h>
23 
24 #include <zephyr/types.h>
25 #include <stddef.h>
26 #include <zephyr/device.h>
27 #include <zephyr/internal/syscall_handler.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /**
34  * @brief Event polarity
35  */
36 enum tgpio_pin_polarity {
37 	TGPIO_RISING_EDGE = 0,
38 	TGPIO_FALLING_EDGE,
39 	TGPIO_TOGGLE_EDGE,
40 };
41 
42 /**
43  * @cond INTERNAL_HIDDEN
44  *
45  * TGPIO driver API definition and system call entry points
46  *
47  * (Internal use only.)
48  */
49 
50 __subsystem struct tgpio_driver_api {
51 	int (*pin_disable)(const struct device *dev, uint32_t pin);
52 	int (*get_time)(const struct device *dev, uint64_t *current_time);
53 	int (*cyc_per_sec)(const struct device *dev, uint32_t *cycles);
54 	int (*set_perout)(const struct device *dev, uint32_t pin, uint64_t start_time,
55 			  uint64_t repeat_interval, bool periodic_enable);
56 	int (*config_ext_ts)(const struct device *dev, uint32_t pin, uint32_t event_polarity);
57 	int (*read_ts_ec)(const struct device *dev, uint32_t pin, uint64_t *timestamp,
58 			  uint64_t *event_count);
59 };
60 
61 /**
62  * @endcond
63  */
64 
65 /**
66  * @brief Get time from ART timer
67  *
68  * @param dev TGPIO device
69  * @param current_time Pointer to store timer value in cycles
70  *
71  * @return 0 if successful
72  * @return negative errno code on failure.
73  */
74 __syscall int tgpio_port_get_time(const struct device *dev, uint64_t *current_time);
75 
z_impl_tgpio_port_get_time(const struct device * dev,uint64_t * current_time)76 static inline int z_impl_tgpio_port_get_time(const struct device *dev, uint64_t *current_time)
77 {
78 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
79 
80 	return api->get_time(dev, current_time);
81 }
82 
83 /**
84  * @brief Get current running rate
85  *
86  * @param dev TGPIO device
87  * @param cycles pointer to store current running requency
88  *
89  * @return 0 if successful, negative errno code on failure.
90  */
91 __syscall int tgpio_port_get_cycles_per_second(const struct device *dev, uint32_t *cycles);
92 
z_impl_tgpio_port_get_cycles_per_second(const struct device * dev,uint32_t * cycles)93 static inline int z_impl_tgpio_port_get_cycles_per_second(const struct device *dev,
94 							   uint32_t *cycles)
95 {
96 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
97 
98 	return api->cyc_per_sec(dev, cycles);
99 }
100 
101 /**
102  * @brief Disable operation on pin
103  *
104  * @param dev TGPIO device
105  * @param pin TGPIO pin
106  *
107  * @return 0 if successful, negative errno code on failure.
108  */
109 __syscall int tgpio_pin_disable(const struct device *dev, uint32_t pin);
110 
z_impl_tgpio_pin_disable(const struct device * dev,uint32_t pin)111 static inline int z_impl_tgpio_pin_disable(const struct device *dev, uint32_t pin)
112 {
113 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
114 
115 	return api->pin_disable(dev, pin);
116 }
117 
118 /**
119  * @brief Enable/Continue operation on pin
120  *
121  * @param dev TGPIO device
122  * @param pin TGPIO pin
123  * @param event_polarity TGPIO pin event polarity
124  *
125  * @return 0 if successful, negative errno code on failure.
126  */
127 __syscall int tgpio_pin_config_ext_timestamp(const struct device *dev, uint32_t pin,
128 					      uint32_t event_polarity);
129 
z_impl_tgpio_pin_config_ext_timestamp(const struct device * dev,uint32_t pin,uint32_t event_polarity)130 static inline int z_impl_tgpio_pin_config_ext_timestamp(const struct device *dev, uint32_t pin,
131 							 uint32_t event_polarity)
132 {
133 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
134 
135 	return api->config_ext_ts(dev, pin, event_polarity);
136 }
137 
138 /**
139  * @brief Enable periodic pulse generation on a pin
140  *
141  * @param dev TGPIO device
142  * @param pin TGPIO pin
143  * @param start_time start_time of first pulse in hw cycles
144  * @param repeat_interval repeat interval between two pulses in hw cycles
145  * @param periodic_enable enables periodic mode if 'true' is passed.
146  *
147  * @return 0 if successful, negative errno code on failure.
148  */
149 __syscall int tgpio_pin_periodic_output(const struct device *dev, uint32_t pin,
150 					 uint64_t start_time, uint64_t repeat_interval,
151 					 bool periodic_enable);
152 
z_impl_tgpio_pin_periodic_output(const struct device * dev,uint32_t pin,uint64_t start_time,uint64_t repeat_interval,bool periodic_enable)153 static inline int z_impl_tgpio_pin_periodic_output(const struct device *dev, uint32_t pin,
154 						    uint64_t start_time, uint64_t repeat_interval,
155 						    bool periodic_enable)
156 {
157 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
158 
159 	return api->set_perout(dev, pin, start_time, repeat_interval, periodic_enable);
160 }
161 
162 /**
163  * @brief Read timestamp and event counter from TGPIO
164  *
165  * @param dev TGPIO device
166  * @param pin TGPIO pin
167  * @param timestamp timestamp of the last pulse received
168  * @param event_count number of pulses received since the pin is enabled
169  *
170  * @return 0 if successful, negative errno code on failure.
171  */
172 __syscall int tgpio_pin_read_ts_ec(const struct device *dev, uint32_t pin, uint64_t *timestamp,
173 				    uint64_t *event_count);
174 
z_impl_tgpio_pin_read_ts_ec(const struct device * dev,uint32_t pin,uint64_t * timestamp,uint64_t * event_count)175 static inline int z_impl_tgpio_pin_read_ts_ec(const struct device *dev, uint32_t pin,
176 					       uint64_t *timestamp, uint64_t *event_count)
177 {
178 	const struct tgpio_driver_api *api = (const struct tgpio_driver_api *)dev->api;
179 
180 	return api->read_ts_ec(dev, pin, timestamp, event_count);
181 }
182 
183 /**
184  * @}
185  */
186 
187 #ifdef __cplusplus
188 }
189 #endif
190 
191 #include <syscalls/timeaware_gpio.h>
192 
193 #endif /* ZEPHYR_DRIVERS_MISC_TIMEAWARE_GPIO_TIMEAWARE_GPIO */
194