1 /*
2  * Copyright (c) 2018 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_DRIVERS_PTP_CLOCK_H_
8 #define ZEPHYR_INCLUDE_DRIVERS_PTP_CLOCK_H_
9 
10 #include <zephyr/kernel.h>
11 #include <stdint.h>
12 #include <zephyr/device.h>
13 #include <zephyr/sys/util.h>
14 #include <zephyr/net/ptp_time.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /* Name of the PTP clock driver */
21 #if !defined(PTP_CLOCK_NAME)
22 #define PTP_CLOCK_NAME "PTP_CLOCK"
23 #endif
24 
25 __subsystem struct ptp_clock_driver_api {
26 	int (*set)(const struct device *dev, struct net_ptp_time *tm);
27 	int (*get)(const struct device *dev, struct net_ptp_time *tm);
28 	int (*adjust)(const struct device *dev, int increment);
29 	int (*rate_adjust)(const struct device *dev, double ratio);
30 };
31 
32 /**
33  * @brief Set the time of the PTP clock.
34  *
35  * @param dev PTP clock device
36  * @param tm Time to set
37  *
38  * @return 0 if ok, <0 if error
39  */
ptp_clock_set(const struct device * dev,struct net_ptp_time * tm)40 static inline int ptp_clock_set(const struct device *dev,
41 				struct net_ptp_time *tm)
42 {
43 	const struct ptp_clock_driver_api *api =
44 		(const struct ptp_clock_driver_api *)dev->api;
45 
46 	return api->set(dev, tm);
47 }
48 
49 /**
50  * @brief Get the time of the PTP clock.
51  *
52  * @param dev PTP clock device
53  * @param tm Where to store the current time.
54  *
55  * @return 0 if ok, <0 if error
56  */
57 __syscall int ptp_clock_get(const struct device *dev, struct net_ptp_time *tm);
58 
z_impl_ptp_clock_get(const struct device * dev,struct net_ptp_time * tm)59 static inline int z_impl_ptp_clock_get(const struct device *dev,
60 				       struct net_ptp_time *tm)
61 {
62 	const struct ptp_clock_driver_api *api =
63 		(const struct ptp_clock_driver_api *)dev->api;
64 
65 	return api->get(dev, tm);
66 }
67 
68 /**
69  * @brief Adjust the PTP clock time.
70  *
71  * @param dev PTP clock device
72  * @param increment Increment of the clock in nanoseconds
73  *
74  * @return 0 if ok, <0 if error
75  */
ptp_clock_adjust(const struct device * dev,int increment)76 static inline int ptp_clock_adjust(const struct device *dev, int increment)
77 {
78 	const struct ptp_clock_driver_api *api =
79 		(const struct ptp_clock_driver_api *)dev->api;
80 
81 	return api->adjust(dev, increment);
82 }
83 
84 /**
85  * @brief Adjust the PTP clock time change rate when compared to its neighbor.
86  *
87  * @param dev PTP clock device
88  * @param rate Rate of the clock time change
89  *
90  * @return 0 if ok, <0 if error
91  */
ptp_clock_rate_adjust(const struct device * dev,double rate)92 static inline int ptp_clock_rate_adjust(const struct device *dev, double rate)
93 {
94 	const struct ptp_clock_driver_api *api =
95 		(const struct ptp_clock_driver_api *)dev->api;
96 
97 	return api->rate_adjust(dev, rate);
98 }
99 
100 #ifdef __cplusplus
101 }
102 #endif
103 
104 #include <syscalls/ptp_clock.h>
105 
106 #endif /* ZEPHYR_INCLUDE_DRIVERS_PTP_CLOCK_H_ */
107