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