xref: /linux/drivers/dpll/zl3073x/ref.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef _ZL3073X_REF_H
4 #define _ZL3073X_REF_H
5 
6 #include <linux/bitfield.h>
7 #include <linux/math64.h>
8 #include <linux/stddef.h>
9 #include <linux/types.h>
10 
11 #include "regs.h"
12 
13 struct zl3073x_dev;
14 
15 /**
16  * struct zl3073x_ref - input reference state
17  * @phase_comp: phase compensation
18  * @esync_n_div: divisor for embedded sync or n-divided signal formats
19  * @freq_base: frequency base
20  * @freq_mult: frequnecy multiplier
21  * @freq_ratio_m: FEC mode multiplier
22  * @freq_ratio_n: FEC mode divisor
23  * @sync_ctrl: reference sync control
24  * @config: reference config
25  * @meas_freq: measured input frequency in Hz
26  * @mon_status: reference monitor status
27  */
28 struct zl3073x_ref {
29 	struct_group(cfg, /* Configuration */
30 		u64	phase_comp;
31 		u32	esync_n_div;
32 		u16	freq_base;
33 		u16	freq_mult;
34 		u16	freq_ratio_m;
35 		u16	freq_ratio_n;
36 		u8	sync_ctrl;
37 	);
38 	struct_group(inv, /* Invariants */
39 		u8	config;
40 	);
41 	struct_group(stat, /* Status */
42 		u32	meas_freq;
43 		u8	mon_status;
44 	);
45 };
46 
47 int zl3073x_ref_state_fetch(struct zl3073x_dev *zldev, u8 index);
48 
49 const struct zl3073x_ref *zl3073x_ref_state_get(struct zl3073x_dev *zldev,
50 						u8 index);
51 
52 int zl3073x_ref_state_set(struct zl3073x_dev *zldev, u8 index,
53 			  const struct zl3073x_ref *ref);
54 
55 int zl3073x_ref_state_update(struct zl3073x_dev *zldev, u8 index);
56 
57 int zl3073x_ref_freq_factorize(u32 freq, u16 *base, u16 *mult);
58 
59 /**
60  * zl3073x_ref_meas_freq_get - get measured input frequency
61  * @ref: pointer to ref state
62  *
63  * Return: measured input frequency in Hz
64  */
65 static inline u32
66 zl3073x_ref_meas_freq_get(const struct zl3073x_ref *ref)
67 {
68 	return ref->meas_freq;
69 }
70 
71 /**
72  * zl3073x_ref_freq_get - get given input reference frequency
73  * @ref: pointer to ref state
74  *
75  * Return: frequency of the given input reference
76  */
77 static inline u32
78 zl3073x_ref_freq_get(const struct zl3073x_ref *ref)
79 {
80 	return mul_u64_u32_div(ref->freq_base * ref->freq_mult,
81 			       ref->freq_ratio_m, ref->freq_ratio_n);
82 }
83 
84 /**
85  * zl3073x_ref_freq_set - set given input reference frequency
86  * @ref: pointer to ref state
87  * @freq: frequency to be set
88  *
89  * Return: 0 on success, <0 when frequency cannot be factorized
90  */
91 static inline int
92 zl3073x_ref_freq_set(struct zl3073x_ref *ref, u32 freq)
93 {
94 	u16 base, mult;
95 	int rc;
96 
97 	rc = zl3073x_ref_freq_factorize(freq, &base, &mult);
98 	if (rc)
99 		return rc;
100 
101 	ref->freq_base = base;
102 	ref->freq_mult = mult;
103 	ref->freq_ratio_m = 1;
104 	ref->freq_ratio_n = 1;
105 
106 	return 0;
107 }
108 
109 /**
110  * zl3073x_ref_sync_mode_get - get sync control mode
111  * @ref: pointer to ref state
112  *
113  * Return: sync control mode (ZL_REF_SYNC_CTRL_MODE_*)
114  */
115 static inline u8
116 zl3073x_ref_sync_mode_get(const struct zl3073x_ref *ref)
117 {
118 	return FIELD_GET(ZL_REF_SYNC_CTRL_MODE, ref->sync_ctrl);
119 }
120 
121 /**
122  * zl3073x_ref_sync_mode_set - set sync control mode
123  * @ref: pointer to ref state
124  * @mode: sync control mode (ZL_REF_SYNC_CTRL_MODE_*)
125  */
126 static inline void
127 zl3073x_ref_sync_mode_set(struct zl3073x_ref *ref, u8 mode)
128 {
129 	FIELD_MODIFY(ZL_REF_SYNC_CTRL_MODE, &ref->sync_ctrl, mode);
130 }
131 
132 /**
133  * zl3073x_ref_sync_pair_get - get sync pair reference index
134  * @ref: pointer to ref state
135  *
136  * Return: paired reference index
137  */
138 static inline u8
139 zl3073x_ref_sync_pair_get(const struct zl3073x_ref *ref)
140 {
141 	return FIELD_GET(ZL_REF_SYNC_CTRL_PAIR, ref->sync_ctrl);
142 }
143 
144 /**
145  * zl3073x_ref_sync_pair_set - set sync pair reference index
146  * @ref: pointer to ref state
147  * @pair: paired reference index
148  */
149 static inline void
150 zl3073x_ref_sync_pair_set(struct zl3073x_ref *ref, u8 pair)
151 {
152 	FIELD_MODIFY(ZL_REF_SYNC_CTRL_PAIR, &ref->sync_ctrl, pair);
153 }
154 
155 /**
156  * zl3073x_ref_is_diff - check if the given input reference is differential
157  * @ref: pointer to ref state
158  *
159  * Return: true if reference is differential, false if reference is single-ended
160  */
161 static inline bool
162 zl3073x_ref_is_diff(const struct zl3073x_ref *ref)
163 {
164 	return !!FIELD_GET(ZL_REF_CONFIG_DIFF_EN, ref->config);
165 }
166 
167 /**
168  * zl3073x_ref_is_enabled - check if the given input reference is enabled
169  * @ref: pointer to ref state
170  *
171  * Return: true if input refernce is enabled, false otherwise
172  */
173 static inline bool
174 zl3073x_ref_is_enabled(const struct zl3073x_ref *ref)
175 {
176 	return !!FIELD_GET(ZL_REF_CONFIG_ENABLE, ref->config);
177 }
178 
179 /**
180  * zl3073x_ref_is_status_ok - check the given input reference status
181  * @ref: pointer to ref state
182  *
183  * Return: true if the status is ok, false otherwise
184  */
185 static inline bool
186 zl3073x_ref_is_status_ok(const struct zl3073x_ref *ref)
187 {
188 	return ref->mon_status == ZL_REF_MON_STATUS_OK;
189 }
190 
191 #endif /* _ZL3073X_REF_H */
192