Line data Source code
1 0 : /*
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 : /**
11 : * @brief PTP Clock Interface
12 : * @defgroup ptp_clock_interface PTP Clock Interface
13 : * @ingroup io_interfaces
14 : * @{
15 : */
16 :
17 : #include <zephyr/kernel.h>
18 : #include <stdint.h>
19 : #include <zephyr/device.h>
20 : #include <zephyr/sys/util.h>
21 : #include <zephyr/net/ptp_time.h>
22 :
23 : #ifdef __cplusplus
24 : extern "C" {
25 : #endif
26 :
27 : /* Name of the PTP clock driver */
28 : #if !defined(PTP_CLOCK_NAME)
29 0 : #define PTP_CLOCK_NAME "PTP_CLOCK"
30 : #endif
31 :
32 0 : __subsystem struct ptp_clock_driver_api {
33 0 : int (*set)(const struct device *dev, struct net_ptp_time *tm);
34 0 : int (*get)(const struct device *dev, struct net_ptp_time *tm);
35 0 : int (*adjust)(const struct device *dev, int increment);
36 0 : int (*rate_adjust)(const struct device *dev, double ratio);
37 : };
38 :
39 : /**
40 : * @brief Set the time of the PTP clock.
41 : *
42 : * @param dev PTP clock device
43 : * @param tm Time to set
44 : *
45 : * @return 0 if ok, <0 if error
46 : */
47 1 : static inline int ptp_clock_set(const struct device *dev,
48 : struct net_ptp_time *tm)
49 : {
50 : const struct ptp_clock_driver_api *api =
51 : (const struct ptp_clock_driver_api *)dev->api;
52 :
53 : return api->set(dev, tm);
54 : }
55 :
56 : /**
57 : * @brief Get the time of the PTP clock.
58 : *
59 : * @param dev PTP clock device
60 : * @param tm Where to store the current time.
61 : *
62 : * @return 0 if ok, <0 if error
63 : */
64 1 : __syscall int ptp_clock_get(const struct device *dev, struct net_ptp_time *tm);
65 :
66 : static inline int z_impl_ptp_clock_get(const struct device *dev,
67 : struct net_ptp_time *tm)
68 : {
69 : const struct ptp_clock_driver_api *api =
70 : (const struct ptp_clock_driver_api *)dev->api;
71 :
72 : return api->get(dev, tm);
73 : }
74 :
75 : /**
76 : * @brief Adjust the PTP clock time.
77 : *
78 : * @param dev PTP clock device
79 : * @param increment Increment of the clock in nanoseconds
80 : *
81 : * @return 0 if ok, <0 if error
82 : */
83 1 : static inline int ptp_clock_adjust(const struct device *dev, int increment)
84 : {
85 : const struct ptp_clock_driver_api *api =
86 : (const struct ptp_clock_driver_api *)dev->api;
87 :
88 : return api->adjust(dev, increment);
89 : }
90 :
91 : /**
92 : * @brief Adjust the PTP clock rate ratio based on its nominal frequency
93 : *
94 : * @param dev PTP clock device
95 : * @param rate Rate ratio based on its nominal frequency
96 : *
97 : * @return 0 if ok, <0 if error
98 : */
99 1 : static inline int ptp_clock_rate_adjust(const struct device *dev, double rate)
100 : {
101 : const struct ptp_clock_driver_api *api =
102 : (const struct ptp_clock_driver_api *)dev->api;
103 :
104 : return api->rate_adjust(dev, rate);
105 : }
106 :
107 : #ifdef __cplusplus
108 : }
109 : #endif
110 :
111 : #include <zephyr/syscalls/ptp_clock.h>
112 :
113 : /**
114 : * @}
115 : */
116 :
117 : #endif /* ZEPHYR_INCLUDE_DRIVERS_PTP_CLOCK_H_ */
|