Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad9361/sw/ad9361.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad9361.c
3 * @brief Implementation of AD9361 Driver.
4 ********************************************************************************
5 * Copyright 2014-2015(c) Analog Devices, Inc.
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * - Neither the name of Analog Devices, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 * - The use of this software may or may not infringe the patent rights
21 * of one or more patent holders. This license does not release you
22 * from the requirement that you obtain separate licenses from these
23 * patent holders to use this software.
24 * - Use of the software either in source or binary form, must be run
25 * on or directly connected to an Analog Devices Inc. component.
26 *
27 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *******************************************************************************/
38
39 /******************************************************************************/
40 /***************************** Include Files **********************************/
41 /******************************************************************************/
42 #include <malloc.h>
43 #include <limits.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <inttypes.h>
49 #include "ad9361.h"
50 #include "platform.h"
51 #include "util.h"
52 #include "config.h"
53
54 /* Used for static code size optimization: please see config.h */
55 const bool has_split_gt = HAVE_SPLIT_GAIN_TABLE;
56 const bool have_tdd_tables = HAVE_TDD_SYNTH_TABLE;
57
58 #define SYNTH_LUT_SIZE 53
59
60 static const struct SynthLUT SynthLUT_FDD[LUT_FTDD_ENT][SYNTH_LUT_SIZE] = {
61 {
62 {12605, 13, 1, 4, 2, 15, 12, 7, 14, 6, 14, 5, 15}, /* 40 MHz */
63 {12245, 13, 1, 4, 2, 15, 12, 7, 14, 6, 14, 5, 15},
64 {11906, 13, 1, 4, 2, 15, 12, 7, 15, 6, 14, 5, 15},
65 {11588, 13, 1, 4, 2, 15, 12, 8, 15, 6, 14, 5, 15},
66 {11288, 13, 1, 4, 2, 15, 12, 8, 15, 6, 14, 5, 15},
67 {11007, 13, 1, 4, 2, 15, 12, 9, 15, 6, 14, 5, 15},
68 {10742, 13, 1, 4, 2, 15, 12, 9, 15, 6, 14, 5, 15},
69 {10492, 13, 1, 6, 2, 15, 12, 10, 15, 6, 14, 5, 15},
70 {10258, 13, 1, 6, 2, 15, 12, 10, 15, 6, 14, 5, 15},
71 {10036, 13, 1, 6, 2, 15, 12, 11, 15, 6, 14, 5, 15},
72 {9827, 13, 1, 6, 2, 14, 12, 11, 15, 6, 14, 5, 15},
73 {9631, 13, 1, 6, 2, 13, 12, 12, 15, 6, 14, 5, 15},
74 {9445, 13, 1, 6, 2, 12, 12, 12, 15, 6, 14, 5, 15},
75 {9269, 13, 1, 6, 2, 12, 12, 13, 15, 6, 14, 5, 15},
76 {9103, 13, 1, 6, 2, 12, 12, 13, 15, 6, 14, 5, 15},
77 {8946, 13, 1, 6, 2, 12, 12, 14, 15, 6, 14, 5, 15},
78 {8797, 12, 1, 7, 2, 12, 12, 13, 15, 6, 14, 5, 15},
79 {8655, 12, 1, 7, 2, 12, 12, 14, 15, 6, 14, 5, 15},
80 {8520, 12, 1, 7, 2, 12, 12, 14, 15, 6, 14, 5, 15},
81 {8392, 12, 1, 7, 2, 12, 12, 15, 15, 6, 14, 5, 15},
82 {8269, 12, 1, 7, 2, 12, 12, 15, 15, 6, 14, 5, 15},
83 {8153, 12, 1, 7, 2, 12, 12, 16, 15, 6, 14, 5, 15},
84 {8041, 12, 1, 7, 2, 13, 12, 16, 15, 6, 14, 5, 15},
85 {7934, 11, 1, 7, 2, 12, 12, 16, 15, 6, 14, 5, 15},
86 {7831, 11, 1, 7, 2, 12, 12, 16, 15, 6, 14, 5, 15},
87 {7733, 10, 1, 7, 3, 13, 12, 16, 15, 6, 14, 5, 15},
88 {7638, 10, 1, 7, 2, 12, 12, 16, 15, 6, 14, 5, 15},
89 {7547, 10, 1, 7, 2, 12, 12, 17, 15, 6, 14, 5, 15},
90 {7459, 10, 1, 7, 2, 12, 12, 17, 15, 6, 14, 5, 15},
91 {7374, 10, 2, 7, 3, 14, 13, 14, 15, 6, 14, 5, 15},
92 {7291, 10, 2, 7, 3, 14, 13, 14, 15, 6, 14, 5, 15},
93 {7212, 10, 2, 7, 3, 14, 13, 14, 15, 6, 14, 5, 15},
94 {7135, 10, 2, 7, 3, 14, 13, 15, 15, 7, 14, 5, 15},
95 {7061, 10, 2, 7, 3, 14, 13, 15, 15, 6, 14, 5, 15},
96 {6988, 10, 1, 7, 3, 12, 14, 20, 15, 6, 14, 5, 15},
97 {6918, 9, 2, 7, 3, 14, 13, 15, 15, 6, 14, 5, 15},
98 {6850, 9, 2, 7, 3, 14, 13, 15, 15, 6, 14, 5, 15},
99 {6784, 9, 2, 7, 2, 13, 13, 15, 15, 6, 14, 5, 15},
100 {6720, 9, 2, 7, 2, 13, 13, 16, 15, 6, 14, 5, 15},
101 {6658, 8, 2, 7, 3, 14, 13, 15, 15, 6, 14, 5, 15},
102 {6597, 8, 2, 7, 2, 13, 13, 15, 15, 6, 14, 5, 15},
103 {6539, 8, 2, 7, 2, 13, 13, 15, 15, 6, 14, 5, 15},
104 {6482, 8, 2, 7, 2, 13, 13, 16, 15, 6, 14, 5, 15},
105 {6427, 7, 2, 7, 3, 14, 13, 15, 15, 6, 14, 5, 15},
106 {6373, 7, 2, 7, 3, 15, 13, 15, 15, 6, 14, 5, 15},
107 {6321, 7, 2, 7, 3, 15, 13, 15, 15, 6, 14, 5, 15},
108 {6270, 7, 2, 7, 3, 15, 13, 16, 15, 6, 14, 5, 15},
109 {6222, 7, 2, 7, 3, 15, 13, 16, 15, 6, 14, 5, 15},
110 {6174, 6, 2, 7, 3, 15, 13, 15, 15, 6, 14, 5, 15},
111 {6128, 6, 2, 7, 3, 15, 13, 15, 15, 6, 14, 5, 15},
112 {6083, 6, 2, 7, 3, 15, 13, 16, 15, 6, 14, 5, 15},
113 {6040, 6, 2, 7, 3, 15, 13, 16, 15, 6, 14, 5, 15},
114 {5997, 6, 2, 7, 3, 15, 13, 16, 15, 6, 14, 5, 15},
115 }, {
116 {12605, 13, 1, 4, 2, 15, 12, 13, 15, 12, 12, 5, 14}, /* 60 MHz */
117 {12245, 13, 1, 4, 2, 15, 12, 13, 15, 12, 12, 5, 14},
118 {11906, 13, 1, 4, 2, 15, 12, 13, 15, 13, 12, 5, 13},
119 {11588, 13, 1, 4, 2, 15, 12, 14, 15, 13, 12, 5, 13},
120 {11288, 13, 1, 5, 2, 15, 12, 15, 15, 13, 12, 5, 13},
121 {11007, 13, 1, 5, 2, 15, 12, 16, 15, 13, 12, 5, 13},
122 {10742, 13, 1, 5, 2, 15, 12, 16, 15, 12, 12, 5, 14},
123 {10492, 13, 1, 6, 2, 15, 12, 17, 15, 12, 12, 5, 14},
124 {10258, 13, 1, 6, 2, 15, 12, 18, 15, 13, 12, 5, 13},
125 {10036, 13, 1, 6, 2, 15, 12, 19, 15, 13, 12, 5, 13},
126 {9827, 13, 1, 6, 2, 14, 12, 20, 15, 13, 12, 5, 13},
127 {9631, 13, 1, 6, 2, 13, 12, 21, 15, 13, 12, 5, 13},
128 {9445, 13, 1, 6, 2, 12, 12, 22, 15, 13, 12, 5, 13},
129 {9269, 13, 1, 6, 2, 12, 12, 22, 15, 12, 12, 5, 14},
130 {9103, 13, 1, 6, 2, 12, 12, 23, 15, 13, 12, 5, 13},
131 {8946, 13, 1, 6, 2, 12, 12, 24, 15, 13, 12, 5, 13},
132 {8797, 12, 1, 7, 2, 12, 12, 24, 15, 13, 12, 5, 13},
133 {8655, 12, 1, 7, 2, 12, 12, 25, 15, 13, 12, 5, 13},
134 {8520, 12, 1, 7, 2, 12, 12, 25, 15, 13, 12, 5, 13},
135 {8392, 12, 1, 7, 2, 12, 12, 26, 15, 13, 12, 5, 13},
136 {8269, 12, 1, 7, 2, 12, 12, 27, 15, 13, 12, 5, 13},
137 {8153, 12, 1, 7, 2, 12, 12, 28, 15, 13, 12, 5, 13},
138 {8041, 12, 1, 7, 2, 13, 12, 29, 15, 13, 12, 5, 13},
139 {7934, 11, 1, 7, 2, 12, 12, 28, 15, 13, 12, 5, 13},
140 {7831, 11, 1, 7, 2, 12, 12, 29, 15, 13, 12, 5, 13},
141 {7733, 10, 1, 7, 3, 13, 12, 28, 15, 13, 12, 5, 13},
142 {7638, 10, 1, 7, 2, 12, 12, 29, 15, 13, 12, 5, 13},
143 {7547, 10, 1, 7, 2, 12, 12, 29, 15, 13, 12, 5, 13},
144 {7459, 10, 1, 7, 2, 12, 12, 30, 15, 13, 12, 5, 13},
145 {7374, 10, 2, 7, 3, 14, 13, 24, 15, 13, 12, 5, 13},
146 {7291, 10, 2, 7, 3, 14, 13, 25, 15, 13, 12, 5, 13},
147 {7212, 10, 2, 7, 3, 14, 13, 25, 15, 13, 12, 5, 13},
148 {7135, 10, 2, 7, 3, 14, 13, 26, 15, 13, 12, 5, 13},
149 {7061, 10, 2, 7, 3, 14, 13, 26, 15, 13, 12, 5, 13},
150 {6988, 10, 1, 7, 3, 12, 14, 35, 15, 13, 12, 5, 13},
151 {6918, 9, 1, 7, 3, 12, 14, 33, 15, 13, 12, 5, 13},
152 {6850, 9, 1, 7, 3, 12, 14, 34, 15, 13, 12, 5, 13},
153 {6784, 9, 1, 7, 2, 11, 14, 35, 15, 13, 12, 5, 13},
154 {6720, 9, 1, 7, 2, 11, 14, 35, 15, 13, 12, 5, 13},
155 {6658, 8, 2, 7, 3, 15, 13, 26, 15, 13, 12, 5, 13},
156 {6597, 8, 2, 7, 2, 15, 13, 27, 15, 13, 12, 5, 13},
157 {6539, 8, 2, 7, 2, 15, 13, 27, 15, 13, 12, 5, 13},
158 {6482, 8, 2, 7, 2, 15, 13, 28, 15, 13, 12, 5, 13},
159 {6427, 7, 2, 7, 3, 14, 13, 27, 15, 13, 12, 5, 13},
160 {6373, 7, 2, 7, 3, 15, 13, 27, 15, 13, 12, 5, 13},
161 {6321, 7, 2, 7, 3, 15, 13, 27, 15, 13, 12, 5, 13},
162 {6270, 7, 2, 7, 3, 15, 13, 28, 15, 13, 12, 5, 13},
163 {6222, 7, 2, 7, 3, 15, 13, 28, 15, 13, 12, 5, 13},
164 {6174, 6, 2, 7, 3, 15, 13, 27, 15, 13, 12, 5, 13},
165 {6128, 6, 2, 7, 3, 15, 13, 27, 15, 13, 12, 5, 13},
166 {6083, 6, 2, 7, 3, 15, 13, 28, 15, 13, 12, 5, 13},
167 {6040, 6, 2, 7, 3, 15, 13, 28, 15, 13, 12, 5, 13},
168 {5997, 6, 2, 7, 3, 15, 13, 29, 15, 13, 12, 5, 13},
169 }, {
170 {12605, 13, 1, 4, 2, 15, 12, 7, 15, 6, 13, 5, 14}, /* 80 MHz */
171 {12245, 13, 1, 4, 2, 15, 12, 7, 15, 6, 13, 5, 14},
172 {11906, 13, 1, 4, 2, 15, 12, 7, 15, 6, 13, 5, 14},
173 {11588, 13, 1, 4, 2, 15, 12, 7, 14, 6, 14, 4, 14},
174 {11288, 13, 1, 4, 2, 15, 12, 8, 15, 6, 13, 5, 14},
175 {11007, 13, 1, 4, 2, 15, 12, 8, 14, 6, 13, 5, 14},
176 {10742, 13, 1, 4, 2, 15, 12, 9, 15, 6, 13, 5, 14},
177 {10492, 13, 1, 6, 2, 15, 12, 9, 14, 6, 13, 5, 14},
178 {10258, 13, 1, 6, 2, 15, 12, 10, 15, 6, 13, 5, 14},
179 {10036, 13, 1, 6, 2, 15, 12, 10, 15, 6, 13, 5, 14},
180 {9827, 13, 1, 6, 2, 14, 12, 11, 15, 6, 13, 5, 14},
181 {9631, 13, 1, 6, 2, 13, 12, 11, 15, 6, 13, 5, 14},
182 {9445, 13, 1, 6, 2, 12, 12, 12, 15, 6, 13, 5, 14},
183 {9269, 13, 1, 6, 2, 12, 12, 12, 15, 6, 13, 5, 14},
184 {9103, 13, 1, 6, 2, 12, 12, 13, 15, 6, 13, 5, 14},
185 {8946, 13, 1, 6, 2, 12, 12, 13, 15, 6, 13, 5, 14},
186 {8797, 12, 1, 7, 2, 12, 12, 13, 15, 6, 13, 5, 14},
187 {8655, 12, 1, 7, 2, 12, 12, 14, 15, 6, 13, 5, 14},
188 {8520, 12, 1, 7, 2, 12, 12, 14, 15, 6, 13, 5, 14},
189 {8392, 12, 1, 7, 2, 12, 12, 15, 15, 7, 13, 5, 14},
190 {8269, 12, 1, 7, 2, 12, 12, 15, 15, 6, 13, 5, 14},
191 {8153, 12, 1, 7, 2, 12, 12, 15, 15, 6, 13, 5, 14},
192 {8041, 12, 1, 7, 2, 13, 12, 16, 15, 6, 13, 5, 14},
193 {7934, 11, 1, 7, 2, 12, 12, 15, 15, 6, 13, 5, 14},
194 {7831, 11, 1, 7, 2, 12, 12, 16, 15, 6, 13, 5, 14},
195 {7733, 10, 1, 7, 3, 13, 12, 15, 15, 6, 13, 5, 14},
196 {7638, 10, 1, 7, 2, 12, 12, 16, 15, 6, 13, 5, 14},
197 {7547, 10, 1, 7, 2, 12, 12, 16, 15, 6, 13, 5, 14},
198 {7459, 10, 1, 7, 2, 12, 12, 17, 15, 6, 13, 5, 14},
199 {7374, 10, 2, 7, 3, 14, 13, 13, 15, 6, 13, 5, 14},
200 {7291, 10, 2, 7, 3, 14, 13, 14, 15, 6, 13, 5, 14},
201 {7212, 10, 2, 7, 3, 14, 13, 14, 15, 6, 13, 5, 14},
202 {7135, 10, 2, 7, 3, 14, 13, 14, 15, 6, 13, 5, 14},
203 {7061, 10, 2, 7, 3, 14, 13, 15, 15, 6, 13, 5, 14},
204 {6988, 10, 1, 7, 3, 12, 14, 19, 15, 6, 13, 5, 14},
205 {6918, 9, 2, 7, 3, 14, 13, 14, 15, 6, 13, 5, 14},
206 {6850, 9, 2, 7, 3, 14, 13, 15, 15, 6, 13, 5, 14},
207 {6784, 9, 2, 7, 2, 13, 13, 15, 15, 6, 13, 5, 14},
208 {6720, 9, 2, 7, 2, 13, 13, 15, 15, 6, 13, 5, 14},
209 {6658, 8, 2, 7, 3, 14, 13, 15, 15, 6, 13, 5, 14},
210 {6597, 8, 2, 7, 2, 13, 13, 15, 15, 6, 13, 5, 14},
211 {6539, 8, 2, 7, 2, 13, 13, 15, 15, 6, 13, 5, 14},
212 {6482, 8, 2, 7, 2, 13, 13, 15, 15, 6, 13, 5, 14},
213 {6427, 7, 2, 7, 3, 14, 13, 15, 15, 6, 13, 5, 14},
214 {6373, 7, 2, 7, 3, 15, 13, 15, 15, 6, 13, 5, 14},
215 {6321, 7, 2, 7, 3, 15, 13, 15, 15, 6, 13, 5, 14},
216 {6270, 7, 2, 7, 3, 15, 13, 15, 15, 6, 13, 5, 14},
217 {6222, 7, 2, 7, 3, 15, 13, 16, 15, 6, 13, 5, 14},
218 {6174, 6, 2, 7, 3, 15, 13, 15, 15, 6, 13, 5, 14},
219 {6128, 6, 2, 7, 3, 15, 13, 15, 15, 6, 13, 5, 14},
220 {6083, 6, 2, 7, 3, 15, 13, 15, 15, 6, 13, 5, 14},
221 {6040, 6, 2, 7, 3, 15, 13, 16, 15, 6, 13, 5, 14},
222 {5997, 6, 2, 7, 3, 15, 13, 16, 15, 6, 13, 5, 14},
223 } };
224
225 static const struct SynthLUT SynthLUT_TDD[LUT_FTDD_ENT][SYNTH_LUT_SIZE] = {
226 {
227 {12605, 13, 1, 4, 2, 15, 12, 27, 12, 15, 12, 4, 13}, /* 40 MHz */
228 {12245, 13, 1, 4, 2, 15, 12, 27, 12, 15, 12, 4, 13},
229 {11906, 13, 1, 4, 2, 15, 12, 26, 11, 15, 12, 4, 13},
230 {11588, 13, 1, 4, 2, 15, 12, 28, 12, 15, 12, 4, 13},
231 {11288, 13, 1, 4, 2, 15, 12, 30, 12, 15, 12, 4, 13},
232 {11007, 13, 1, 4, 2, 15, 12, 32, 12, 15, 12, 4, 13},
233 {10742, 13, 1, 4, 2, 15, 12, 33, 12, 15, 12, 4, 13},
234 {10492, 13, 1, 6, 2, 15, 12, 35, 12, 15, 12, 4, 13},
235 {10258, 13, 1, 6, 2, 15, 12, 37, 12, 15, 12, 4, 13},
236 {10036, 13, 1, 6, 2, 15, 12, 38, 12, 15, 12, 4, 13},
237 {9827, 13, 1, 6, 2, 14, 12, 40, 12, 15, 12, 4, 13},
238 {9631, 13, 1, 6, 2, 13, 12, 42, 12, 15, 12, 4, 13},
239 {9445, 13, 1, 6, 2, 12, 12, 44, 12, 15, 12, 4, 13},
240 {9269, 13, 1, 6, 2, 12, 12, 45, 12, 15, 12, 4, 13},
241 {9103, 13, 1, 6, 2, 12, 12, 47, 12, 15, 12, 4, 13},
242 {8946, 13, 1, 6, 2, 12, 12, 49, 12, 15, 12, 4, 13},
243 {8797, 12, 1, 7, 2, 12, 12, 48, 12, 15, 12, 4, 13},
244 {8655, 12, 1, 7, 2, 12, 12, 50, 12, 15, 12, 4, 13},
245 {8520, 12, 1, 7, 2, 12, 12, 51, 12, 15, 12, 4, 13},
246 {8392, 12, 1, 7, 2, 12, 12, 53, 12, 15, 12, 4, 13},
247 {8269, 12, 1, 7, 2, 12, 12, 55, 12, 15, 12, 4, 13},
248 {8153, 12, 1, 7, 2, 12, 12, 56, 12, 15, 12, 4, 13},
249 {8041, 12, 1, 7, 2, 13, 12, 58, 12, 15, 12, 4, 13},
250 {7934, 11, 1, 7, 2, 12, 12, 57, 12, 15, 12, 4, 13},
251 {7831, 11, 1, 7, 2, 12, 12, 58, 12, 15, 12, 4, 13},
252 {7733, 10, 1, 7, 3, 13, 12, 56, 12, 15, 12, 4, 13},
253 {7638, 10, 1, 7, 2, 12, 12, 58, 12, 15, 12, 4, 13},
254 {7547, 10, 1, 7, 2, 12, 12, 59, 12, 15, 12, 4, 13},
255 {7459, 10, 1, 7, 2, 12, 12, 61, 12, 15, 12, 4, 13},
256 {7374, 10, 2, 7, 3, 14, 13, 49, 12, 15, 12, 4, 13},
257 {7291, 10, 2, 7, 3, 14, 13, 50, 12, 15, 12, 4, 13},
258 {7212, 10, 2, 7, 3, 14, 13, 51, 12, 15, 12, 4, 13},
259 {7135, 10, 2, 7, 3, 14, 13, 52, 12, 15, 12, 4, 13},
260 {7061, 10, 2, 7, 3, 14, 13, 53, 12, 15, 12, 4, 13},
261 {6988, 10, 1, 7, 3, 12, 14, 63, 11, 14, 12, 3, 13},
262 {6918, 9, 2, 7, 3, 14, 13, 52, 12, 15, 12, 4, 13},
263 {6850, 9, 2, 7, 3, 14, 13, 53, 12, 15, 12, 4, 13},
264 {6784, 9, 2, 7, 2, 13, 13, 54, 12, 15, 12, 4, 13},
265 {6720, 9, 2, 7, 2, 13, 13, 56, 12, 15, 12, 4, 13},
266 {6658, 8, 2, 7, 3, 14, 13, 53, 12, 15, 12, 4, 13},
267 {6597, 8, 2, 7, 2, 13, 13, 54, 12, 15, 12, 4, 13},
268 {6539, 8, 2, 7, 2, 13, 13, 55, 12, 15, 12, 4, 13},
269 {6482, 8, 2, 7, 2, 13, 13, 56, 12, 15, 12, 4, 13},
270 {6427, 7, 2, 7, 3, 14, 13, 54, 12, 15, 12, 4, 13},
271 {6373, 7, 2, 7, 3, 15, 13, 54, 12, 15, 12, 4, 13},
272 {6321, 7, 2, 7, 3, 15, 13, 55, 12, 15, 12, 4, 13},
273 {6270, 7, 2, 7, 3, 15, 13, 56, 12, 15, 12, 4, 13},
274 {6222, 7, 2, 7, 3, 15, 13, 57, 12, 15, 12, 4, 13},
275 {6174, 6, 2, 7, 3, 15, 13, 54, 12, 15, 12, 4, 13},
276 {6128, 6, 2, 7, 3, 15, 13, 55, 12, 15, 12, 4, 13},
277 {6083, 6, 2, 7, 3, 15, 13, 56, 12, 15, 12, 4, 13},
278 {6040, 6, 2, 7, 3, 15, 13, 57, 12, 15, 12, 4, 13},
279 {5997, 6, 2, 7, 3, 15, 13, 58, 12, 15, 12, 4, 13},
280 }, {
281 {12605, 13, 1, 4, 2, 15, 12, 26, 11, 15, 11, 4, 13}, /* 60 MHz */
282 {12245, 13, 1, 4, 2, 15, 12, 26, 11, 15, 11, 4, 13},
283 {11906, 13, 1, 4, 2, 15, 12, 26, 12, 15, 11, 4, 12},
284 {11588, 13, 1, 4, 2, 15, 12, 30, 12, 15, 11, 4, 12},
285 {11288, 13, 1, 4, 2, 15, 12, 32, 12, 15, 10, 4, 12},
286 {11007, 13, 1, 4, 2, 15, 12, 31, 12, 15, 11, 4, 12},
287 {10742, 13, 1, 4, 2, 15, 12, 33, 12, 15, 10, 4, 12},
288 {10492, 13, 1, 6, 2, 15, 12, 37, 12, 15, 10, 4, 12},
289 {10258, 13, 1, 6, 2, 15, 12, 38, 12, 15, 11, 4, 13},
290 {10036, 13, 1, 6, 2, 15, 12, 38, 12, 15, 10, 4, 12},
291 {9827, 13, 1, 6, 2, 14, 12, 42, 12, 15, 11, 4, 12},
292 {9631, 13, 1, 6, 2, 13, 12, 41, 12, 15, 11, 4, 12},
293 {9445, 13, 1, 6, 2, 12, 12, 45, 12, 15, 11, 4, 12},
294 {9269, 13, 1, 6, 2, 12, 12, 47, 12, 15, 11, 4, 12},
295 {9103, 13, 1, 6, 2, 12, 12, 46, 12, 15, 11, 4, 12},
296 {8946, 13, 1, 6, 2, 12, 12, 48, 12, 15, 10, 4, 12},
297 {8797, 12, 1, 7, 2, 12, 12, 49, 12, 15, 11, 4, 13},
298 {8655, 12, 1, 7, 2, 12, 12, 51, 12, 15, 11, 4, 12},
299 {8520, 12, 1, 7, 2, 12, 12, 50, 12, 15, 11, 4, 12},
300 {8392, 12, 1, 7, 2, 12, 12, 52, 12, 15, 10, 4, 12},
301 {8269, 12, 1, 7, 2, 12, 12, 56, 12, 15, 10, 4, 12},
302 {8153, 12, 1, 7, 2, 12, 12, 55, 12, 15, 11, 4, 12},
303 {8041, 12, 1, 7, 2, 13, 12, 57, 12, 15, 10, 4, 12},
304 {7934, 11, 1, 7, 2, 12, 12, 55, 12, 15, 11, 4, 12},
305 {7831, 11, 1, 7, 2, 12, 12, 57, 12, 15, 10, 4, 12},
306 {7733, 10, 1, 7, 3, 13, 12, 55, 12, 15, 11, 4, 12},
307 {7638, 10, 1, 7, 2, 12, 12, 59, 12, 15, 10, 4, 12},
308 {7547, 10, 1, 7, 2, 12, 12, 60, 12, 15, 11, 4, 12},
309 {7459, 10, 1, 7, 2, 12, 12, 48, 12, 15, 11, 4, 12},
310 {7374, 10, 2, 7, 3, 14, 13, 47, 12, 15, 11, 4, 13},
311 {7291, 10, 2, 7, 3, 14, 13, 49, 12, 15, 10, 4, 12},
312 {7212, 10, 2, 7, 3, 14, 13, 50, 12, 15, 10, 4, 12},
313 {7135, 10, 2, 7, 3, 14, 13, 52, 12, 15, 11, 4, 13},
314 {7061, 10, 2, 7, 3, 14, 13, 52, 12, 15, 11, 4, 12},
315 {6988, 10, 1, 7, 3, 12, 14, 63, 11, 15, 11, 4, 13},
316 {6918, 9, 1, 7, 3, 12, 14, 63, 11, 15, 11, 4, 13},
317 {6850, 9, 1, 7, 3, 12, 14, 63, 11, 15, 11, 4, 13},
318 {6784, 9, 1, 7, 2, 11, 14, 63, 11, 15, 11, 4, 13},
319 {6720, 9, 1, 7, 2, 11, 14, 63, 11, 14, 11, 3, 13},
320 {6658, 8, 1, 7, 3, 12, 14, 63, 11, 15, 11, 4, 13},
321 {6597, 8, 1, 7, 2, 11, 14, 63, 11, 14, 11, 3, 13},
322 {6539, 8, 1, 7, 2, 11, 14, 63, 10, 14, 11, 3, 13},
323 {6482, 8, 1, 7, 2, 11, 14, 63, 10, 14, 11, 3, 13},
324 {6427, 7, 2, 7, 3, 14, 13, 54, 12, 15, 10, 4, 12},
325 {6373, 7, 2, 7, 3, 15, 13, 53, 12, 15, 11, 4, 12},
326 {6321, 7, 2, 7, 3, 15, 13, 54, 12, 15, 11, 4, 12},
327 {6270, 7, 2, 7, 3, 15, 13, 55, 12, 15, 11, 4, 12},
328 {6222, 7, 2, 7, 3, 15, 13, 56, 12, 15, 11, 4, 12},
329 {6174, 6, 2, 7, 3, 15, 13, 53, 12, 15, 11, 4, 12},
330 {6128, 6, 2, 7, 3, 15, 13, 55, 12, 15, 11, 4, 12},
331 {6083, 6, 2, 7, 3, 15, 13, 55, 12, 15, 10, 4, 12},
332 {6040, 6, 2, 7, 3, 15, 13, 56, 12, 15, 10, 4, 12},
333 {5997, 6, 2, 7, 3, 15, 13, 57, 12, 15, 10, 4, 12},
334 }, {
335 {12605, 13, 1, 4, 2, 15, 12, 21, 12, 15, 11, 4, 13}, /* 80 MHz */
336 {12245, 13, 1, 4, 2, 15, 12, 21, 12, 15, 11, 4, 13},
337 {11906, 13, 1, 4, 2, 15, 12, 20, 11, 15, 11, 4, 13},
338 {11588, 13, 1, 4, 2, 15, 12, 22, 12, 15, 11, 4, 12},
339 {11288, 13, 1, 5, 2, 15, 12, 23, 12, 15, 11, 4, 13},
340 {11007, 13, 1, 5, 2, 15, 12, 25, 12, 15, 10, 4, 12},
341 {10742, 13, 1, 5, 2, 15, 12, 26, 12, 15, 11, 4, 13},
342 {10492, 13, 1, 6, 2, 15, 12, 27, 11, 15, 11, 4, 13},
343 {10258, 13, 1, 6, 2, 15, 12, 29, 12, 15, 10, 4, 12},
344 {10036, 13, 1, 6, 2, 15, 12, 30, 12, 15, 11, 4, 12},
345 {9827, 13, 1, 6, 2, 14, 12, 31, 12, 15, 11, 4, 13},
346 {9631, 13, 1, 6, 2, 13, 12, 33, 12, 15, 10, 4, 12},
347 {9445, 13, 1, 6, 2, 12, 12, 34, 12, 15, 11, 4, 12},
348 {9269, 13, 1, 6, 2, 12, 12, 35, 12, 15, 11, 4, 13},
349 {9103, 13, 1, 6, 2, 12, 12, 37, 12, 15, 10, 4, 12},
350 {8946, 13, 1, 6, 2, 12, 12, 38, 12, 15, 11, 4, 12},
351 {8797, 12, 1, 7, 2, 12, 12, 37, 12, 15, 11, 4, 13},
352 {8655, 12, 1, 7, 2, 12, 12, 39, 12, 15, 11, 4, 12},
353 {8520, 12, 1, 7, 2, 12, 12, 40, 12, 15, 11, 4, 12},
354 {8392, 12, 1, 7, 2, 12, 12, 41, 12, 15, 11, 4, 13},
355 {8269, 12, 1, 7, 2, 12, 12, 43, 12, 15, 10, 4, 12},
356 {8153, 12, 1, 7, 2, 12, 12, 44, 12, 15, 11, 4, 12},
357 {8041, 12, 1, 7, 2, 13, 12, 45, 12, 15, 11, 4, 12},
358 {7934, 11, 1, 7, 2, 12, 12, 44, 12, 15, 11, 4, 12},
359 {7831, 11, 1, 7, 2, 12, 12, 45, 12, 15, 11, 4, 13},
360 {7733, 10, 1, 7, 3, 13, 12, 44, 12, 15, 11, 4, 12},
361 {7638, 10, 1, 7, 2, 12, 12, 45, 12, 15, 11, 4, 12},
362 {7547, 10, 1, 7, 2, 12, 12, 46, 12, 15, 11, 4, 12},
363 {7459, 10, 1, 7, 2, 12, 12, 47, 12, 15, 11, 4, 13},
364 {7374, 10, 2, 7, 3, 14, 13, 38, 12, 15, 11, 4, 12},
365 {7291, 10, 2, 7, 3, 14, 13, 39, 12, 15, 10, 4, 12},
366 {7212, 10, 2, 7, 3, 14, 13, 40, 12, 15, 10, 4, 12},
367 {7135, 10, 2, 7, 3, 14, 13, 41, 12, 15, 10, 4, 12},
368 {7061, 10, 2, 7, 3, 14, 13, 41, 12, 15, 11, 4, 13},
369 {6988, 10, 1, 7, 3, 12, 14, 54, 12, 15, 11, 4, 12},
370 {6918, 9, 2, 7, 3, 14, 13, 41, 12, 15, 10, 4, 12},
371 {6850, 9, 2, 7, 3, 14, 13, 42, 12, 15, 10, 4, 12},
372 {6784, 9, 2, 7, 2, 13, 13, 42, 12, 15, 11, 4, 13},
373 {6720, 9, 2, 7, 2, 13, 13, 43, 12, 15, 11, 4, 13},
374 {6658, 8, 2, 7, 3, 14, 13, 41, 12, 15, 11, 4, 13},
375 {6597, 8, 2, 7, 2, 13, 13, 42, 12, 15, 11, 4, 12},
376 {6539, 8, 2, 7, 2, 13, 13, 43, 12, 15, 11, 4, 12},
377 {6482, 8, 2, 7, 2, 13, 13, 44, 12, 15, 11, 4, 12},
378 {6427, 7, 2, 7, 3, 14, 13, 42, 12, 15, 10, 4, 12},
379 {6373, 7, 2, 7, 3, 15, 13, 42, 12, 15, 11, 4, 13},
380 {6321, 7, 2, 7, 3, 15, 13, 43, 12, 15, 11, 4, 12},
381 {6270, 7, 2, 7, 3, 15, 13, 44, 12, 15, 11, 4, 12},
382 {6222, 7, 2, 7, 3, 15, 13, 45, 12, 15, 10, 4, 12},
383 {6174, 6, 2, 7, 3, 15, 13, 42, 12, 15, 11, 4, 13},
384 {6128, 6, 2, 7, 3, 15, 13, 43, 12, 15, 11, 4, 12},
385 {6083, 6, 2, 7, 3, 15, 13, 44, 12, 15, 10, 4, 12},
386 {6040, 6, 2, 7, 3, 15, 13, 44, 12, 15, 11, 4, 13},
387 {5997, 6, 2, 7, 3, 15, 13, 45, 12, 15, 11, 4, 12},
388 } };
389
390 /* Rx Gain Tables */
391
392 #define SIZE_FULL_TABLE 77
393
394 static const uint8_t full_gain_table[RXGAIN_TBLS_END][SIZE_FULL_TABLE][3] =
395 { { /* 800 MHz */
396 { 0x00, 0x00, 0x20 }, { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00 },
397 { 0x00, 0x01, 0x00 }, { 0x00, 0x02, 0x00 }, { 0x00, 0x03, 0x00 },
398 { 0x00, 0x04, 0x00 }, { 0x00, 0x05, 0x00 }, { 0x01, 0x03, 0x20 },
399 { 0x01, 0x04, 0x00 }, { 0x01, 0x05, 0x00 }, { 0x01, 0x06, 0x00 },
400 { 0x01, 0x07, 0x00 }, { 0x01, 0x08, 0x00 }, { 0x01, 0x09, 0x00 },
401 { 0x01, 0x0A, 0x00 }, { 0x01, 0x0B, 0x00 }, { 0x01, 0x0C, 0x00 },
402 { 0x01, 0x0D, 0x00 }, { 0x01, 0x0E, 0x00 }, { 0x02, 0x09, 0x20 },
403 { 0x02, 0x0A, 0x00 }, { 0x02, 0x0B, 0x00 }, { 0x02, 0x0C, 0x00 },
404 { 0x02, 0x0D, 0x00 }, { 0x02, 0x0E, 0x00 }, { 0x02, 0x0F, 0x00 },
405 { 0x02, 0x10, 0x00 }, { 0x02, 0x2B, 0x20 }, { 0x02, 0x2C, 0x00 },
406 { 0x04, 0x28, 0x20 }, { 0x04, 0x29, 0x00 }, { 0x04, 0x2A, 0x00 },
407 { 0x04, 0x2B, 0x00 }, { 0x24, 0x20, 0x20 }, { 0x24, 0x21, 0x00 },
408 { 0x44, 0x20, 0x20 }, { 0x44, 0x21, 0x00 }, { 0x44, 0x22, 0x00 },
409 { 0x44, 0x23, 0x00 }, { 0x44, 0x24, 0x00 }, { 0x44, 0x25, 0x00 },
410 { 0x44, 0x26, 0x00 }, { 0x44, 0x27, 0x00 }, { 0x44, 0x28, 0x00 },
411 { 0x44, 0x29, 0x00 }, { 0x44, 0x2A, 0x00 }, { 0x44, 0x2B, 0x00 },
412 { 0x44, 0x2C, 0x00 }, { 0x44, 0x2D, 0x00 }, { 0x44, 0x2E, 0x00 },
413 { 0x44, 0x2F, 0x00 }, { 0x44, 0x30, 0x00 }, { 0x44, 0x31, 0x00 },
414 { 0x44, 0x32, 0x00 }, { 0x64, 0x2E, 0x20 }, { 0x64, 0x2F, 0x00 },
415 { 0x64, 0x30, 0x00 }, { 0x64, 0x31, 0x00 }, { 0x64, 0x32, 0x00 },
416 { 0x64, 0x33, 0x00 }, { 0x64, 0x34, 0x00 }, { 0x64, 0x35, 0x00 },
417 { 0x64, 0x36, 0x00 }, { 0x64, 0x37, 0x00 }, { 0x64, 0x38, 0x00 },
418 { 0x65, 0x38, 0x20 }, { 0x66, 0x38, 0x20 }, { 0x67, 0x38, 0x20 },
419 { 0x68, 0x38, 0x20 }, { 0x69, 0x38, 0x20 }, { 0x6A, 0x38, 0x20 },
420 { 0x6B, 0x38, 0x20 }, { 0x6C, 0x38, 0x20 }, { 0x6D, 0x38, 0x20 },
421 { 0x6E, 0x38, 0x20 }, { 0x6F, 0x38, 0x20 }
422 }, { /* 2300 MHz */
423 { 0x00, 0x00, 0x20 }, { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00 },
424 { 0x00, 0x01, 0x00 }, { 0x00, 0x02, 0x00 }, { 0x00, 0x03, 0x00 },
425 { 0x00, 0x04, 0x00 }, { 0x00, 0x05, 0x00 }, { 0x01, 0x03, 0x20 },
426 { 0x01, 0x04, 0x00 }, { 0x01, 0x05, 0x00 }, { 0x01, 0x06, 0x00 },
427 { 0x01, 0x07, 0x00 }, { 0x01, 0x08, 0x00 }, { 0x01, 0x09, 0x00 },
428 { 0x01, 0x0A, 0x00 }, { 0x01, 0x0B, 0x00 }, { 0x01, 0x0C, 0x00 },
429 { 0x01, 0x0D, 0x00 }, { 0x01, 0x0E, 0x00 }, { 0x02, 0x09, 0x20 },
430 { 0x02, 0x0A, 0x00 }, { 0x02, 0x0B, 0x00 }, { 0x02, 0x0C, 0x00 },
431 { 0x02, 0x0D, 0x00 }, { 0x02, 0x0E, 0x00 }, { 0x02, 0x0F, 0x00 },
432 { 0x02, 0x10, 0x00 }, { 0x02, 0x2B, 0x20 }, { 0x02, 0x2C, 0x00 },
433 { 0x04, 0x27, 0x20 }, { 0x04, 0x28, 0x00 }, { 0x04, 0x29, 0x00 },
434 { 0x04, 0x2A, 0x00 }, { 0x04, 0x2B, 0x00 }, { 0x24, 0x21, 0x20 },
435 { 0x24, 0x22, 0x00 }, { 0x44, 0x20, 0x20 }, { 0x44, 0x21, 0x00 },
436 { 0x44, 0x22, 0x00 }, { 0x44, 0x23, 0x00 }, { 0x44, 0x24, 0x00 },
437 { 0x44, 0x25, 0x00 }, { 0x44, 0x26, 0x00 }, { 0x44, 0x27, 0x00 },
438 { 0x44, 0x28, 0x00 }, { 0x44, 0x29, 0x00 }, { 0x44, 0x2A, 0x00 },
439 { 0x44, 0x2B, 0x00 }, { 0x44, 0x2C, 0x00 }, { 0x44, 0x2D, 0x00 },
440 { 0x44, 0x2E, 0x00 }, { 0x44, 0x2F, 0x00 }, { 0x44, 0x30, 0x00 },
441 { 0x44, 0x31, 0x00 }, { 0x64, 0x2E, 0x20 }, { 0x64, 0x2F, 0x00 },
442 { 0x64, 0x30, 0x00 }, { 0x64, 0x31, 0x00 }, { 0x64, 0x32, 0x00 },
443 { 0x64, 0x33, 0x00 }, { 0x64, 0x34, 0x00 }, { 0x64, 0x35, 0x00 },
444 { 0x64, 0x36, 0x00 }, { 0x64, 0x37, 0x00 }, { 0x64, 0x38, 0x00 },
445 { 0x65, 0x38, 0x20 }, { 0x66, 0x38, 0x20 }, { 0x67, 0x38, 0x20 },
446 { 0x68, 0x38, 0x20 }, { 0x69, 0x38, 0x20 }, { 0x6A, 0x38, 0x20 },
447 { 0x6B, 0x38, 0x20 }, { 0x6C, 0x38, 0x20 }, { 0x6D, 0x38, 0x20 },
448 { 0x6E, 0x38, 0x20 }, { 0x6F, 0x38, 0x20 },
449 }, { /* 5500 MHz */
450 { 0x00, 0x00, 0x20 }, { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00 },
451 { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00 }, { 0x00, 0x01, 0x00 },
452 { 0x00, 0x02, 0x00 }, { 0x00, 0x03, 0x00 }, { 0x01, 0x01, 0x20 },
453 { 0x01, 0x02, 0x00 }, { 0x01, 0x03, 0x00 }, { 0x01, 0x04, 0x20 },
454 { 0x01, 0x05, 0x00 }, { 0x01, 0x06, 0x00 }, { 0x01, 0x07, 0x00 },
455 { 0x01, 0x08, 0x00 }, { 0x01, 0x09, 0x00 }, { 0x01, 0x0A, 0x00 },
456 { 0x01, 0x0B, 0x00 }, { 0x01, 0x0C, 0x00 }, { 0x02, 0x08, 0x20 },
457 { 0x02, 0x09, 0x00 }, { 0x02, 0x0A, 0x00 }, { 0x02, 0x0B, 0x20 },
458 { 0x02, 0x0C, 0x00 }, { 0x02, 0x0D, 0x00 }, { 0x02, 0x0E, 0x00 },
459 { 0x02, 0x0F, 0x00 }, { 0x02, 0x2A, 0x20 }, { 0x02, 0x2B, 0x00 },
460 { 0x04, 0x27, 0x20 }, { 0x04, 0x28, 0x00 }, { 0x04, 0x29, 0x00 },
461 { 0x04, 0x2A, 0x00 }, { 0x04, 0x2B, 0x00 }, { 0x04, 0x2C, 0x00 },
462 { 0x04, 0x2D, 0x00 }, { 0x24, 0x20, 0x20 }, { 0x24, 0x21, 0x00 },
463 { 0x24, 0x22, 0x00 }, { 0x44, 0x20, 0x20 }, { 0x44, 0x21, 0x00 },
464 { 0x44, 0x22, 0x00 }, { 0x44, 0x23, 0x00 }, { 0x44, 0x24, 0x00 },
465 { 0x44, 0x25, 0x00 }, { 0x44, 0x26, 0x00 }, { 0x44, 0x27, 0x00 },
466 { 0x44, 0x28, 0x00 }, { 0x44, 0x29, 0x00 }, { 0x44, 0x2A, 0x00 },
467 { 0x44, 0x2B, 0x00 }, { 0x44, 0x2C, 0x00 }, { 0x44, 0x2D, 0x00 },
468 { 0x44, 0x2E, 0x00 }, { 0x64, 0x2E, 0x20 }, { 0x64, 0x2F, 0x00 },
469 { 0x64, 0x30, 0x00 }, { 0x64, 0x31, 0x00 }, { 0x64, 0x32, 0x00 },
470 { 0x64, 0x33, 0x00 }, { 0x64, 0x34, 0x00 }, { 0x64, 0x35, 0x00 },
471 { 0x64, 0x36, 0x00 }, { 0x64, 0x37, 0x00 }, { 0x64, 0x38, 0x00 },
472 { 0x65, 0x38, 0x20 }, { 0x66, 0x38, 0x20 }, { 0x67, 0x38, 0x20 },
473 { 0x68, 0x38, 0x20 }, { 0x69, 0x38, 0x20 }, { 0x6A, 0x38, 0x20 },
474 { 0x6B, 0x38, 0x20 }, { 0x6C, 0x38, 0x20 }, { 0x6D, 0x38, 0x20 },
475 { 0x6E, 0x38, 0x20 }, { 0x6F, 0x38, 0x20 }
476 } };
477
478 #define SIZE_SPLIT_TABLE 41
479
480 static const uint8_t split_gain_table[RXGAIN_TBLS_END][SIZE_SPLIT_TABLE][3] =
481 { { /* 800 MHz */
482 { 0x00, 0x18, 0x20 }, { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 },
483 { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 },
484 { 0x00, 0x18, 0x20 }, { 0x01, 0x18, 0x20 }, { 0x02, 0x18, 0x20 },
485 { 0x04, 0x18, 0x20 }, { 0x04, 0x38, 0x20 }, { 0x05, 0x38, 0x20 },
486 { 0x06, 0x38, 0x20 }, { 0x07, 0x38, 0x20 }, { 0x08, 0x38, 0x20 },
487 { 0x09, 0x38, 0x20 }, { 0x0A, 0x38, 0x20 }, { 0x0B, 0x38, 0x20 },
488 { 0x0C, 0x38, 0x20 }, { 0x0D, 0x38, 0x20 }, { 0x0E, 0x38, 0x20 },
489 { 0x0F, 0x38, 0x20 }, { 0x24, 0x38, 0x20 }, { 0x25, 0x38, 0x20 },
490 { 0x44, 0x38, 0x20 }, { 0x45, 0x38, 0x20 }, { 0x46, 0x38, 0x20 },
491 { 0x47, 0x38, 0x20 }, { 0x48, 0x38, 0x20 }, { 0x64, 0x38, 0x20 },
492 { 0x65, 0x38, 0x20 }, { 0x66, 0x38, 0x20 }, { 0x67, 0x38, 0x20 },
493 { 0x68, 0x38, 0x20 }, { 0x69, 0x38, 0x20 }, { 0x6A, 0x38, 0x20 },
494 { 0x6B, 0x38, 0x20 }, { 0x6C, 0x38, 0x20 }, { 0x6D, 0x38, 0x20 },
495 { 0x6E, 0x38, 0x20 }, { 0x6F, 0x38, 0x20 },
496 }, { /* 2300 MHz */
497 { 0x00, 0x18, 0x20 }, { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 },
498 { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 },
499 { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x20 }, { 0x01, 0x18, 0x20 },
500 { 0x02, 0x18, 0x20 }, { 0x04, 0x18, 0x20 }, { 0x04, 0x38, 0x20 },
501 { 0x05, 0x38, 0x20 }, { 0x06, 0x38, 0x20 }, { 0x07, 0x38, 0x20 },
502 { 0x08, 0x38, 0x20 }, { 0x09, 0x38, 0x20 }, { 0x0A, 0x38, 0x20 },
503 { 0x0B, 0x38, 0x20 }, { 0x0C, 0x38, 0x20 }, { 0x0D, 0x38, 0x20 },
504 { 0x0E, 0x38, 0x20 }, { 0x0F, 0x38, 0x20 }, { 0x25, 0x38, 0x20 },
505 { 0x26, 0x38, 0x20 }, { 0x44, 0x38, 0x20 }, { 0x45, 0x38, 0x20 },
506 { 0x46, 0x38, 0x20 }, { 0x47, 0x38, 0x20 }, { 0x64, 0x38, 0x20 },
507 { 0x65, 0x38, 0x20 }, { 0x66, 0x38, 0x20 }, { 0x67, 0x38, 0x20 },
508 { 0x68, 0x38, 0x20 }, { 0x69, 0x38, 0x20 }, { 0x6A, 0x38, 0x20 },
509 { 0x6B, 0x38, 0x20 }, { 0x6C, 0x38, 0x20 }, { 0x6D, 0x38, 0x20 },
510 { 0x6E, 0x38, 0x20 }, { 0x6F, 0x38, 0x20 },
511 }, { /* 5500 MHz */
512 { 0x00, 0x18, 0x20 }, { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 },
513 { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 },
514 { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 }, { 0x00, 0x18, 0x00 },
515 { 0x00, 0x18, 0x00 }, { 0x01, 0x18, 0x20 }, { 0x02, 0x18, 0x20 },
516 { 0x04, 0x18, 0x20 }, { 0x04, 0x38, 0x20 }, { 0x05, 0x38, 0x20 },
517 { 0x06, 0x38, 0x20 }, { 0x07, 0x38, 0x20 }, { 0x08, 0x38, 0x20 },
518 { 0x09, 0x38, 0x20 }, { 0x0A, 0x38, 0x20 }, { 0x0B, 0x38, 0x20 },
519 { 0x0C, 0x38, 0x20 }, { 0x0D, 0x38, 0x20 }, { 0x0E, 0x38, 0x20 },
520 { 0x0F, 0x38, 0x20 }, { 0x62, 0x38, 0x20 }, { 0x25, 0x38, 0x20 },
521 { 0x26, 0x38, 0x20 }, { 0x44, 0x38, 0x20 }, { 0x64, 0x38, 0x20 },
522 { 0x65, 0x38, 0x20 }, { 0x66, 0x38, 0x20 }, { 0x67, 0x38, 0x20 },
523 { 0x68, 0x38, 0x20 }, { 0x69, 0x38, 0x20 }, { 0x6A, 0x38, 0x20 },
524 { 0x6B, 0x38, 0x20 }, { 0x6C, 0x38, 0x20 }, { 0x6D, 0x38, 0x20 },
525 { 0x6E, 0x38, 0x20 }, { 0x6F, 0x38, 0x20 },
526 } };
527
528 /* Mixer GM Sub-table */
529
530 static const uint8_t gm_st_gain[16] = { 0x78, 0x74, 0x70, 0x6C, 0x68, 0x64, 0x60,
531 0x5C, 0x58, 0x54, 0x50, 0x4C, 0x48, 0x30, 0x18, 0x0 };
532 static const uint8_t gm_st_ctrl[16] = { 0x0, 0xD, 0x15, 0x1B, 0x21, 0x25, 0x29,
533 0x2C, 0x2F, 0x31, 0x33, 0x34, 0x35, 0x3A, 0x3D, 0x3E };
534
535 static const int8_t lna_table[RXGAIN_TBLS_END][4] = {
536 {5, 17, 19, 24}, {3, 14, 17, 21}, {-4, 10, 13, 14}};
537 static const int8_t tia_table[] = { -6, 0 };
538 static const int8_t mixer_table[RXGAIN_TBLS_END][16] = {
539 {0, 3, 9, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25},
540 {0, 3, 9, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26},
541 {0, 3, 8, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}};
542
543 static const uint32_t gain_step_calib_reg_val[4][5] = {
544 {0xC0, 0x2E, 0x10, 0x06, 0x00}, // LO Frequency Range: 600 to 1300 MHz
545 {0xC0, 0x2C, 0x10, 0x06, 0x00}, // LO Frequency Range: 1300 to 3300 MHz
546 {0xB8, 0x2C, 0x10, 0x06, 0x00}, // LO Frequency Range: 2700 to 4100 MHz
547 {0xA0, 0x24, 0x10, 0x06, 0x00}, // LO Frequency Range: 4000 to 6000 MHz
548 };
549
550 /******************************************************************************/
551 /********************** Macros and Constants Definitions **********************/
552 /******************************************************************************/
553 const char *ad9361_ensm_states[] = {
554 "sleep", "", "", "", "", "alert", "tx", "tx flush",
555 "rx", "rx_flush", "fdd", "fdd_flush"
556 };
557
558 /**
559 * SPI multiple bytes register read.
560 * @param spi
561 * @param reg The register address.
562 * @param rbuf The data buffer.
563 * @param num The number of bytes to read.
564 * @return 0 in case of success, negative error code otherwise.
565 */
566 int32_t ad9361_spi_readm(struct spi_device *spi, uint32_t reg,
567 uint8_t *rbuf, uint32_t num)
568 {
569 uint8_t buf[2];
570 int32_t ret;
571 uint16_t cmd;
572
573 if (num > MAX_MBYTE_SPI)
574 return -EINVAL;
575
576 cmd = AD_READ | AD_CNT(num) | AD_ADDR(reg);
577 buf[0] = cmd >> 8;
578 buf[1] = cmd & 0xFF;
579
580 ret = spi_write_then_read(spi, &buf[0], 2, rbuf, num);
581 if (ret < 0) {
582 dev_err(&spi->dev, "Read Error %"PRId32, ret);
583 return ret;
584 }
585 #ifdef _DEBUG
586 {
587 int32_t i;
588 for (i = 0; i < num; i++)
589 dev_dbg(&spi->dev, "%s: reg 0x%"PRIX32" val 0x%X",
590 __func__, reg--, rbuf[i]);
591 }
592 #endif
593
594 return 0;
595 }
596
597 /**
598 * SPI register read.
599 * @param spi
600 * @param reg The register address.
601 * @return The register value or negative error code in case of failure.
602 */
603 int32_t ad9361_spi_read(struct spi_device *spi, uint32_t reg)
604 {
605 uint8_t buf;
606 int32_t ret;
607
608 ret = ad9361_spi_readm(spi, reg, &buf, 1);
609 if (ret < 0)
610 return ret;
611
612 return buf;
613 }
614
615 /**
616 * SPI register bits read.
617 * @param spi
618 * @param reg The register address.
619 * @param mask The bits mask.
620 * @param offset The mask offset.
621 * @return The bits value or negative error code in case of failure.
622 */
623 static int32_t __ad9361_spi_readf(struct spi_device *spi, uint32_t reg,
624 uint32_t mask, uint32_t offset)
625 {
626 uint8_t buf;
627 int32_t ret;
628
629 if (!mask)
630 return -EINVAL;
631
632 ret = ad9361_spi_readm(spi, reg, &buf, 1);
633 if (ret < 0)
634 return ret;
635
636 buf &= mask;
637 buf >>= offset;
638
639 return buf;
640 }
641
642 /**
643 * SPI register bits read.
644 * @param spi
645 * @param reg The register address.
646 * @param mask The bits mask.
647 * @return The bits value or negative error code in case of failure.
648 */
649 #define ad9361_spi_readf(spi, reg, mask) \
650 __ad9361_spi_readf(spi, reg, mask, find_first_bit(mask))
651
652 /**
653 * SPI register write.
654 * @param spi
655 * @param reg The register address.
656 * @param val The value of the register.
657 * @return 0 in case of success, negative error code otherwise.
658 */
659 int32_t ad9361_spi_write(struct spi_device *spi,
660 uint32_t reg, uint32_t val)
661 {
662 uint8_t buf[3];
663 int32_t ret;
664 uint16_t cmd;
665
666 cmd = AD_WRITE | AD_CNT(1) | AD_ADDR(reg);
667 buf[0] = cmd >> 8;
668 buf[1] = cmd & 0xFF;
669 buf[2] = val;
670
671 ret = spi_write_then_read(spi, buf, 3, NULL, 0);
672 if (ret < 0) {
673 dev_err(&spi->dev, "Write Error %"PRId32, ret);
674 return ret;
675 }
676
677 #ifdef _DEBUG
678 dev_dbg(&spi->dev, "%s: reg 0x%"PRIX32" val 0x%X", __func__, reg, buf[2]);
679 #endif
680
681 return 0;
682 }
683
684 /**
685 * SPI register bits write.
686 * @param spi
687 * @param reg The register address.
688 * @param mask The bits mask.
689 * @param offset The mask offset.
690 * @param val The bits value.
691 * @return 0 in case of success, negative error code otherwise.
692 */
693 static int32_t __ad9361_spi_writef(struct spi_device *spi, uint32_t reg,
694 uint32_t mask, uint32_t offset, uint32_t val)
695 {
696 uint8_t buf;
697 int32_t ret;
698
699 if (!mask)
700 return -EINVAL;
701
702 ret = ad9361_spi_readm(spi, reg, &buf, 1);
703 if (ret < 0)
704 return ret;
705
706 buf &= ~mask;
707 buf |= ((val << offset) & mask);
708
709 return ad9361_spi_write(spi, reg, buf);
710 }
711
712 /**
713 * SPI register bits write.
714 * @param spi
715 * @param reg The register address.
716 * @param mask The bits mask.
717 * @param val The bits value.
718 * @return 0 in case of success, negative error code otherwise.
719 */
720 #define ad9361_spi_writef(spi, reg, mask, val) \
721 __ad9361_spi_writef(spi, reg, mask, find_first_bit(mask), val)
722
723 /**
724 * SPI multiple bytes register write.
725 * @param spi
726 * @param reg The register address.
727 * @param tbuf The data buffer.
728 * @param num The number of bytes to read.
729 * @return 0 in case of success, negative error code otherwise.
730 */
731 static int32_t ad9361_spi_writem(struct spi_device *spi,
732 uint32_t reg, uint8_t *tbuf, uint32_t num)
733 {
734 uint8_t buf[10];
735 int32_t ret;
736 uint16_t cmd;
737
738 if (num > MAX_MBYTE_SPI)
739 return -EINVAL;
740
741 cmd = AD_WRITE | AD_CNT(num) | AD_ADDR(reg);
742 buf[0] = cmd >> 8;
743 buf[1] = cmd & 0xFF;
744
745 #ifndef ALTERA_PLATFORM
746 memcpy(&buf[2], tbuf, num);
747 #else
748 int32_t i;
749 for (i = 0; i < num; i++)
750 buf[2 + i] = tbuf[i];
751 #endif
752 ret = spi_write_then_read(spi, buf, num + 2, NULL, 0);
753 if (ret < 0) {
754 dev_err(&spi->dev, "Write Error %"PRId32, ret);
755 return ret;
756 }
757
758 #ifdef _DEBUG
759 {
760 int32_t i;
761 for (i = 0; i < num; i++)
762 dev_dbg(&spi->dev, "Reg 0x%"PRIX32" val 0x%X", reg--, tbuf[i]);
763 }
764 #endif
765
766 return 0;
767 }
768
769 /**
770 * Validate RF BW frequency.
771 * @param phy The AD9361 state structure.
772 * @param bw The RF BW frequency.
773 * @return The validated RF BW frequency.
774 */
775 uint32_t ad9361_validate_rf_bw(struct ad9361_rf_phy *phy, uint32_t bw)
776 {
777 switch (phy->dev_sel) {
778 case ID_AD9363A:
779 return clamp_t(uint32_t, bw, 0, 20000000UL);
780 default:
781 return clamp_t(uint32_t, bw, 0, 56000000UL);
782 }
783 }
784
785 /**
786 * Validate RF PLL frequency.
787 * @param phy The AD9361 state structure.
788 * @param freq The RF PLL frequency.
789 * @return 0 in case of success, negative error code otherwise.
790 */
791 int32_t ad9361_validate_rfpll(struct ad9361_rf_phy *phy, uint64_t freq)
792 {
793 switch (phy->dev_sel) {
794 case ID_AD9363A:
795 if (freq > AD9363A_MAX_CARRIER_FREQ_HZ ||
796 freq < AD9363A_MIN_CARRIER_FREQ_HZ)
797 return -EINVAL;
798 break;
799 default:
800 if (freq > MAX_CARRIER_FREQ_HZ || freq < MIN_CARRIER_FREQ_HZ)
801 return -EINVAL;
802 }
803
804 return 0;
805 }
806
807 /**
808 * Find optimal value.
809 * @param field
810 * @param ret_start
811 * @return The optimal delay in case of success, negative error code otherwise.
812 */
813 int32_t ad9361_find_opt(uint8_t *field, uint32_t size, uint32_t *ret_start)
814 {
815 int32_t i, cnt = 0, max_cnt = 0, start, max_start = 0;
816
817 for(i = 0, start = -1; i < (int64_t)size; i++) {
818 if (field[i] == 0) {
819 if (start == -1)
820 start = i;
821 cnt++;
822 } else {
823 if (cnt > max_cnt) {
824 max_cnt = cnt;
825 max_start = start;
826 }
827 start = -1;
828 cnt = 0;
829 }
830 }
831
832 if (cnt > max_cnt) {
833 max_cnt = cnt;
834 max_start = start;
835 }
836
837 *ret_start = max_start;
838
839 return max_cnt;
840 }
841
842 /**
843 * Select the channel mapping in 1rx1tx mode.
844 * @param phy The AD9361 state structure.
845 * @param map Map
846 * @param channel Channel
847 * @return The channel number.
848 */
849 int32_t ad9361_1rx1tx_channel_map(struct ad9361_rf_phy *phy, bool tx, int32_t channel)
850 {
851 uint32_t map;
852
853 if (phy->pdata->rx2tx2)
854 return channel;
855
856 if (tx)
857 map = phy->pdata->rx1tx1_mode_use_tx_num;
858 else
859 map = phy->pdata->rx1tx1_mode_use_rx_num;
860
861 if (map == 2)
862 return channel + 1;
863
864 return channel;
865 }
866
867 /**
868 * AD9361 Device Reset
869 * @param phy The AD9361 state structure.
870 * @return 0 in case of success, negative error code otherwise.
871 */
872 int32_t ad9361_reset(struct ad9361_rf_phy *phy)
873 {
874 if (gpio_is_valid(phy->pdata->gpio_resetb)) {
875 gpio_set_value(phy->pdata->gpio_resetb, 0);
876 mdelay(1);
877 gpio_set_value(phy->pdata->gpio_resetb, 1);
878 mdelay(1);
879 dev_dbg(&phy->spi->dev, "%s: by GPIO", __func__);
880 return 0;
881 }
882
883 /* SPI Soft Reset was removed from the register map, since it doesn't
884 * work reliably. Without a prober HW reset randomness may happen.
885 * Please specify a RESET GPIO.
886 */
887
888 ad9361_spi_write(phy->spi, REG_SPI_CONF, SOFT_RESET | _SOFT_RESET);
889 ad9361_spi_write(phy->spi, REG_SPI_CONF, 0x0);
890 dev_err(&phy->spi->dev,
891 "%s: by SPI, this may cause unpredicted behavior!", __func__);
892
893 return -ENODEV;
894 }
895
896 /**
897 * Enable/disable the desired TX channel.
898 * @param phy The AD9361 state structure.
899 * @param tx_if The desired channel number [1, 2].
900 * @param enable Enable/disable option.
901 * @return 0 in case of success, negative error code otherwise.
902 */
903 int32_t ad9361_en_dis_tx(struct ad9361_rf_phy *phy, uint32_t tx_if, uint32_t enable)
904 {
905 if ((tx_if & enable) > 1 && AD9364_DEVICE && enable)
906 return -EINVAL;
907
908 return ad9361_spi_writef(phy->spi, REG_TX_ENABLE_FILTER_CTRL,
909 TX_CHANNEL_ENABLE(tx_if), enable);
910 }
911
912 /**
913 * Enable/disable the desired RX channel.
914 * @param phy The AD9361 state structure.
915 * @param tx_if The desired channel number [1, 2].
916 * @param enable Enable/disable option.
917 * @return 0 in case of success, negative error code otherwise.
918 */
919 int32_t ad9361_en_dis_rx(struct ad9361_rf_phy *phy, uint32_t rx_if, uint32_t enable)
920 {
921 if ((rx_if & enable) > 1 && AD9364_DEVICE && enable)
922 return -EINVAL;
923
924 return ad9361_spi_writef(phy->spi, REG_RX_ENABLE_FILTER_CTRL,
925 RX_CHANNEL_ENABLE(rx_if), enable);
926 }
927
928 /**
929 * Loopback works only TX1->RX1 or RX2->RX2.
930 * @param phy The AD9361 state structure.
931 * @param enable Enable.
932 * @return 0 in case of success, negative error code otherwise.
933 */
934 static int32_t ad9361_int_loopback_fix_ch_cross(struct ad9361_rf_phy *phy, bool enable)
935 {
936 /* Loopback works only TX1->RX1 or RX2->RX2 */
937 if (!phy->pdata->rx2tx2 && phy->pdata->rx1tx1_mode_use_rx_num !=
938 phy->pdata->rx1tx1_mode_use_tx_num)
939 return ad9361_en_dis_tx(phy, TX_1 | TX_2,
940 enable ? phy->pdata->rx1tx1_mode_use_rx_num :
941 phy->pdata->rx1tx1_mode_use_tx_num);
942
943 return 0;
944 }
945
946 /**
947 * BIST loopback mode.
948 * @param phy The AD9361 state structure.
949 * @param mode BIST loopback mode.
950 * @return 0 in case of success, negative error code otherwise.
951 */
952 int32_t ad9361_bist_loopback(struct ad9361_rf_phy *phy, int32_t mode)
953 {
954 uint32_t sp_hd, reg;
955
956 dev_dbg(&phy->spi->dev, "%s: mode %"PRId32, __func__, mode);
957
958 reg = ad9361_spi_read(phy->spi, REG_OBSERVE_CONFIG);
959
960 phy->bist_loopback_mode = mode;
961
962 switch (mode) {
963 case 0:
964 ad9361_hdl_loopback(phy, false);
965 ad9361_int_loopback_fix_ch_cross(phy, false);
966 reg &= ~(DATA_PORT_SP_HD_LOOP_TEST_OE |
967 DATA_PORT_LOOP_TEST_ENABLE);
968 return ad9361_spi_write(phy->spi, REG_OBSERVE_CONFIG, reg);
969 case 1:
970 /* loopback (AD9361 internal) TX->RX */
971 ad9361_hdl_loopback(phy, false);
972 ad9361_int_loopback_fix_ch_cross(phy, true);
973 sp_hd = ad9361_spi_read(phy->spi, REG_PARALLEL_PORT_CONF_3);
974 if ((sp_hd & SINGLE_PORT_MODE) && (sp_hd & HALF_DUPLEX_MODE))
975 reg |= DATA_PORT_SP_HD_LOOP_TEST_OE;
976 else
977 reg &= ~DATA_PORT_SP_HD_LOOP_TEST_OE;
978
979 reg |= DATA_PORT_LOOP_TEST_ENABLE;
980
981 return ad9361_spi_write(phy->spi, REG_OBSERVE_CONFIG, reg);
982 case 2:
983 /* loopback (FPGA internal) RX->TX */
984 ad9361_hdl_loopback(phy, true);
985 ad9361_int_loopback_fix_ch_cross(phy, false);
986 reg &= ~(DATA_PORT_SP_HD_LOOP_TEST_OE |
987 DATA_PORT_LOOP_TEST_ENABLE);
988 return ad9361_spi_write(phy->spi, REG_OBSERVE_CONFIG, reg);
989 default:
990 return -EINVAL;
991 }
992 }
993
994 /**
995 * Get BIST loopback mode.
996 * @param phy The AD9361 state structure.
997 * @param mode BIST loopback mode.
998 * @return 0 in case of success, negative error code otherwise.
999 */
1000 void ad9361_get_bist_loopback(struct ad9361_rf_phy *phy, int32_t *mode)
1001 {
1002 *mode = phy->bist_loopback_mode;
1003 }
1004
1005 /**
1006 * BIST mode.
1007 * @param phy The AD9361 state structure.
1008 * @param mode Bist mode.
1009 * @return 0 in case of success, negative error code otherwise.
1010 */
1011 int32_t ad9361_bist_prbs(struct ad9361_rf_phy *phy, enum ad9361_bist_mode mode)
1012 {
1013 uint32_t reg = 0;
1014
1015 dev_dbg(&phy->spi->dev, "%s: mode %d", __func__, mode);
1016
1017 phy->bist_prbs_mode = mode;
1018
1019 switch (mode) {
1020 case BIST_DISABLE:
1021 reg = 0;
1022 break;
1023 case BIST_INJ_TX:
1024 reg = BIST_CTRL_POINT(0) | BIST_ENABLE;
1025 break;
1026 case BIST_INJ_RX:
1027 reg = BIST_CTRL_POINT(2) | BIST_ENABLE;
1028 break;
1029 };
1030
1031 return ad9361_spi_write(phy->spi, REG_BIST_CONFIG, reg);
1032 }
1033
1034 /**
1035 * Get BIST mode settings.
1036 * @param phy The AD9361 state structure.
1037 * @param mode Bist mode.
1038 * @return 0 in case of success, negative error code otherwise.
1039 */
1040 void ad9361_get_bist_prbs(struct ad9361_rf_phy *phy, enum ad9361_bist_mode *mode)
1041 {
1042 *mode = phy->bist_prbs_mode;
1043 }
1044
1045 /**
1046 * BIST tone.
1047 * @param phy The AD9361 state structure.
1048 * @param mode Bist tone mode.
1049 * @param freq_Hz Bist tone frequency.
1050 * @param level_dB Bist tone level.
1051 * @param mask Bist reg mask.
1052 * @return 0 in case of success, negative error code otherwise.
1053 */
1054 int32_t ad9361_bist_tone(struct ad9361_rf_phy *phy,
1055 enum ad9361_bist_mode mode, uint32_t freq_Hz,
1056 uint32_t level_dB, uint32_t mask)
1057 {
1058 uint32_t clk = 0;
1059 uint32_t reg = 0, reg1, reg_mask;
1060
1061 dev_dbg(&phy->spi->dev, "%s: mode %d", __func__, mode);
1062
1063 phy->bist_tone_mode = mode;
1064 phy->bist_tone_freq_Hz = freq_Hz;
1065 phy->bist_tone_level_dB = level_dB;
1066 phy->bist_tone_mask = mask;
1067
1068 switch (mode) {
1069 case BIST_DISABLE:
1070 reg = 0;
1071 break;
1072 case BIST_INJ_TX:
1073 clk = clk_get_rate(phy, phy->ref_clk_scale[TX_SAMPL_CLK]);
1074 reg = BIST_CTRL_POINT(0) | BIST_ENABLE;
1075 break;
1076 case BIST_INJ_RX:
1077 clk = clk_get_rate(phy, phy->ref_clk_scale[RX_SAMPL_CLK]);
1078 reg = BIST_CTRL_POINT(2) | BIST_ENABLE;
1079 break;
1080 };
1081
1082 reg |= TONE_PRBS;
1083 reg |= TONE_LEVEL(level_dB / 6);
1084
1085 if (freq_Hz < 4) {
1086 reg |= TONE_FREQ(freq_Hz);
1087 }
1088 else {
1089 if (clk)
1090 reg |= TONE_FREQ(DIV_ROUND_CLOSEST(freq_Hz * 32, clk) - 1);
1091 }
1092
1093 reg_mask = BIST_MASK_CHANNEL_1_I_DATA | BIST_MASK_CHANNEL_1_Q_DATA |
1094 BIST_MASK_CHANNEL_2_I_DATA | BIST_MASK_CHANNEL_2_Q_DATA;
1095
1096 reg1 = ((mask << 2) & reg_mask);
1097 ad9361_spi_write(phy->spi, REG_BIST_AND_DATA_PORT_TEST_CONFIG, reg1);
1098
1099 return ad9361_spi_write(phy->spi, REG_BIST_CONFIG, reg);
1100 }
1101
1102 /**
1103 * Get BIST tone settings.
1104 * @param phy The AD9361 state structure.
1105 * @param mode Bist tone mode.
1106 * @param freq_Hz Bist tone frequency.
1107 * @param level_dB Bist tone level.
1108 * @param mask Bist reg mask.
1109 * @return 0 in case of success, negative error code otherwise.
1110 */
1111 void ad9361_get_bist_tone(struct ad9361_rf_phy *phy,
1112 enum ad9361_bist_mode *mode, uint32_t *freq_Hz,
1113 uint32_t *level_dB, uint32_t *mask)
1114 {
1115 *mode = phy->bist_tone_mode;
1116 *freq_Hz = phy->bist_tone_freq_Hz;
1117 *level_dB = phy->bist_tone_level_dB;
1118 *mask = phy->bist_tone_mask;
1119 }
1120
1121 /**
1122 * Check the calibration done bit.
1123 * @param phy The AD9361 state structure.
1124 * @param reg The register address.
1125 * @param mask The bit mask.
1126 * @param done_state The done state [0,1].
1127 * @return 0 in case of success, negative error code otherwise.
1128 */
1129 static int32_t ad9361_check_cal_done(struct ad9361_rf_phy *phy, uint32_t reg,
1130 uint32_t mask, uint32_t done_state)
1131 {
1132 uint32_t timeout = 5000; /* RFDC_CAL can take long */
1133 uint32_t state;
1134
1135 do {
1136 state = ad9361_spi_readf(phy->spi, reg, mask);
1137 if (state == done_state)
1138 return 0;
1139
1140 if (reg == REG_CALIBRATION_CTRL)
1141 udelay(1200);
1142 else
1143 udelay(120);
1144 } while (timeout--);
1145
1146 dev_err(&phy->spi->dev, "Calibration TIMEOUT (0x%"PRIX32", 0x%"PRIX32")", reg, mask);
1147
1148 return -ETIMEDOUT;
1149 }
1150
1151 /**
1152 * Run an AD9361 calibration and check the calibration done bit.
1153 * @param phy The AD9361 state structure.
1154 * @param mask The calibration bit mask[RX_BB_TUNE_CAL, TX_BB_TUNE_CAL,
1155 * RX_QUAD_CAL, TX_QUAD_CAL, RX_GAIN_STEP_CAL, TXMON_CAL,
1156 * RFDC_CAL, BBDC_CAL].
1157 * @return 0 in case of success, negative error code otherwise.
1158 */
1159 static int32_t ad9361_run_calibration(struct ad9361_rf_phy *phy, uint32_t mask)
1160 {
1161 int32_t ret = ad9361_spi_write(phy->spi, REG_CALIBRATION_CTRL, mask);
1162 if (ret < 0)
1163 return ret;
1164
1165 dev_dbg(&phy->spi->dev, "%s: CAL Mask 0x%"PRIx32, __func__, mask);
1166
1167 return ad9361_check_cal_done(phy, REG_CALIBRATION_CTRL, mask, 0);
1168 }
1169
1170 /**
1171 * Choose the right RX gain table index for the selected frequency.
1172 * @param freq The frequency value [Hz].
1173 * @return The index to the RX gain table.
1174 */
1175 static enum rx_gain_table_name ad9361_gt_tableindex(uint64_t freq)
1176 {
1177 if (freq <= 1300000000ULL)
1178 return TBL_200_1300_MHZ;
1179
1180 if (freq <= 4000000000ULL)
1181 return TBL_1300_4000_MHZ;
1182
1183 return TBL_4000_6000_MHZ;
1184 }
1185
1186 /**
1187 * Shift the real frequency value, so it fits type unsigned long
1188 * Note: PLL operates between 47 .. 6000 MHz which is > 2^32.
1189 * @param freq The frequency value [Hz].
1190 * @return The shifted frequency value.
1191 */
1192 uint32_t ad9361_to_clk(uint64_t freq)
1193 {
1194 return (uint32_t)(freq >> 1);
1195 }
1196
1197 /**
1198 * Shift back the frequency value, so it reflects the real value.
1199 * Note: PLL operates between 47 .. 6000 MHz which is > 2^32.
1200 * @param freq The frequency value [Hz].
1201 * @return The shifted frequency value.
1202 */
1203 uint64_t ad9361_from_clk(uint32_t freq)
1204 {
1205 return ((uint64_t)freq << 1);
1206 }
1207
1208 /**
1209 * Load the gain table for the selected frequency range and receiver.
1210 * @param phy The AD9361 state structure.
1211 * @param freq The frequency value [Hz].
1212 * @param dest The destination [GT_RX1, GT_RX2].
1213 * @return 0 in case of success, negative error code otherwise.
1214 */
1215 static int32_t ad9361_load_gt(struct ad9361_rf_phy *phy, uint64_t freq, uint32_t dest)
1216 {
1217 struct spi_device *spi = phy->spi;
1218 const uint8_t(*tab)[3];
1219 enum rx_gain_table_name band;
1220 uint32_t index_max, i, lna;
1221
1222 dev_dbg(&phy->spi->dev, "%s: frequency %"PRIu64, __func__, freq);
1223
1224 band = ad9361_gt_tableindex(freq);
1225
1226 dev_dbg(&phy->spi->dev, "%s: frequency %"PRIu64" (band %d)",
1227 __func__, freq, band);
1228
1229 /* check if table is present */
1230 if (phy->current_table == band)
1231 return 0;
1232
1233 ad9361_spi_writef(spi, REG_AGC_CONFIG_2,
1234 AGC_USE_FULL_GAIN_TABLE, !phy->pdata->split_gt);
1235
1236 if (has_split_gt && phy->pdata->split_gt) {
1237 tab = &split_gain_table[band][0];
1238 index_max = SIZE_SPLIT_TABLE;
1239 }
1240 else {
1241 tab = &full_gain_table[band][0];
1242 index_max = SIZE_FULL_TABLE;
1243 }
1244
1245 lna = phy->pdata->elna_ctrl.elna_in_gaintable_all_index_en ?
1246 EXT_LNA_CTRL : 0;
1247
1248 ad9361_spi_write(spi, REG_GAIN_TABLE_CONFIG, START_GAIN_TABLE_CLOCK |
1249 RECEIVER_SELECT(dest)); /* Start Gain Table Clock */
1250
1251 for (i = 0; i < index_max; i++) {
1252 ad9361_spi_write(spi, REG_GAIN_TABLE_ADDRESS, i); /* Gain Table Index */
1253 ad9361_spi_write(spi, REG_GAIN_TABLE_WRITE_DATA1, tab[i][0] | lna); /* Ext LNA, Int LNA, & Mixer Gain Word */
1254 ad9361_spi_write(spi, REG_GAIN_TABLE_WRITE_DATA2, tab[i][1]); /* TIA & LPF Word */
1255 ad9361_spi_write(spi, REG_GAIN_TABLE_WRITE_DATA3, tab[i][2]); /* DC Cal bit & Dig Gain Word */
1256 ad9361_spi_write(spi, REG_GAIN_TABLE_CONFIG,
1257 START_GAIN_TABLE_CLOCK |
1258 WRITE_GAIN_TABLE |
1259 RECEIVER_SELECT(dest)); /* Gain Table Index */
1260 ad9361_spi_write(spi, REG_GAIN_TABLE_READ_DATA1, 0); /* Dummy Write to delay 3 ADCCLK/16 cycles */
1261 ad9361_spi_write(spi, REG_GAIN_TABLE_READ_DATA1, 0); /* Dummy Write to delay ~1u */
1262 }
1263
1264 ad9361_spi_write(spi, REG_GAIN_TABLE_CONFIG, START_GAIN_TABLE_CLOCK |
1265 RECEIVER_SELECT(dest)); /* Clear Write Bit */
1266 ad9361_spi_write(spi, REG_GAIN_TABLE_READ_DATA1, 0); /* Dummy Write to delay ~1u */
1267 ad9361_spi_write(spi, REG_GAIN_TABLE_READ_DATA1, 0); /* Dummy Write to delay ~1u */
1268 ad9361_spi_write(spi, REG_GAIN_TABLE_CONFIG, 0); /* Stop Gain Table Clock */
1269
1270 phy->current_table = band;
1271
1272 return 0;
1273 }
1274
1275 /**
1276 * Setup the external low-noise amplifier (LNA).
1277 * @param phy The AD9361 state structure.
1278 * @param ctrl Pointer to eLNA control structure.
1279 * @return 0 in case of success, negative error code otherwise.
1280 */
1281 static int32_t ad9361_setup_ext_lna(struct ad9361_rf_phy *phy,
1282 struct elna_control *ctrl)
1283 {
1284 ad9361_spi_writef(phy->spi, REG_EXTERNAL_LNA_CTRL, EXTERNAL_LNA1_CTRL,
1285 ctrl->elna_1_control_en);
1286
1287 ad9361_spi_writef(phy->spi, REG_EXTERNAL_LNA_CTRL, EXTERNAL_LNA2_CTRL,
1288 ctrl->elna_2_control_en);
1289
1290 ad9361_spi_write(phy->spi, REG_EXT_LNA_HIGH_GAIN,
1291 EXT_LNA_HIGH_GAIN(ctrl->gain_mdB / 500));
1292
1293 return ad9361_spi_write(phy->spi, REG_EXT_LNA_LOW_GAIN,
1294 EXT_LNA_LOW_GAIN(ctrl->bypass_loss_mdB / 500));
1295 }
1296
1297 /**
1298 * Set the clock output mode.
1299 * @param phy The AD9361 state structure.
1300 * @param mode The clock output mode [].
1301 * @return 0 in case of success, negative error code otherwise.
1302 */
1303 static int32_t ad9361_clkout_control(struct ad9361_rf_phy *phy,
1304 enum ad9361_clkout mode)
1305 {
1306 if (mode == CLKOUT_DISABLE)
1307 return ad9361_spi_writef(phy->spi, REG_BBPLL, CLKOUT_ENABLE, 0);
1308
1309 return ad9361_spi_writef(phy->spi, REG_BBPLL,
1310 CLKOUT_ENABLE | CLKOUT_SELECT(~0),
1311 ((mode - 1) << 1) | 0x1);
1312 }
1313
1314 /**
1315 * Load the Gm Sub Table.
1316 * @param phy The AD9361 state structure.
1317 * @return 0 in case of success, negative error code otherwise.
1318 */
1319 static int32_t ad9361_load_mixer_gm_subtable(struct ad9361_rf_phy *phy)
1320 {
1321 int32_t i, addr;
1322 dev_dbg(&phy->spi->dev, "%s", __func__);
1323
1324 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_CONFIG,
1325 START_GM_SUB_TABLE_CLOCK); /* Start Clock */
1326
1327 for (i = 0, addr = ARRAY_SIZE(gm_st_ctrl); i < (int64_t)ARRAY_SIZE(gm_st_ctrl); i++) {
1328 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_ADDRESS, --addr); /* Gain Table Index */
1329 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_BIAS_WRITE, 0); /* Bias */
1330 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_GAIN_WRITE, gm_st_gain[i]); /* Gain */
1331 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_CTRL_WRITE, gm_st_ctrl[i]); /* Control */
1332 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_CONFIG,
1333 WRITE_GM_SUB_TABLE | START_GM_SUB_TABLE_CLOCK); /* Write Words */
1334 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_GAIN_READ, 0); /* Dummy Delay */
1335 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_GAIN_READ, 0); /* Dummy Delay */
1336 }
1337
1338 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_CONFIG, START_GM_SUB_TABLE_CLOCK); /* Clear Write */
1339 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_GAIN_READ, 0); /* Dummy Delay */
1340 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_GAIN_READ, 0); /* Dummy Delay */
1341 ad9361_spi_write(phy->spi, REG_GM_SUB_TABLE_CONFIG, 0); /* Stop Clock */
1342
1343 return 0;
1344 }
1345
1346 /**
1347 * Set the attenuation for the selected TX channels.
1348 * @param phy The AD9361 state structure.
1349 * @param atten_mdb Attenuation value [mdB].
1350 * @param tx1 Set true, the attenuation of the TX1 will be affected.
1351 * @param tx2 Set true, the attenuation of the TX2 will be affected.
1352 * @param immed Set true, an immediate update will take place.
1353 * @return 0 in case of success, negative error code otherwise.
1354 */
1355 int32_t ad9361_set_tx_atten(struct ad9361_rf_phy *phy, uint32_t atten_mdb,
1356 bool tx1, bool tx2, bool immed)
1357 {
1358 uint8_t buf[2];
1359 int32_t ret = 0;
1360
1361 dev_dbg(&phy->spi->dev, "%s : attenuation %"PRIu32" mdB tx1=%d tx2=%d",
1362 __func__, atten_mdb, tx1, tx2);
1363
1364 if (atten_mdb > 89750) /* 89.75 dB */
1365 return -EINVAL;
1366
1367 atten_mdb /= 250; /* Scale to 0.25dB / LSB */
1368
1369 buf[0] = atten_mdb >> 8;
1370 buf[1] = atten_mdb & 0xFF;
1371
1372 ad9361_spi_writef(phy->spi, REG_TX2_DIG_ATTEN,
1373 IMMEDIATELY_UPDATE_TPC_ATTEN, 0);
1374
1375 if (tx1)
1376 ret = ad9361_spi_writem(phy->spi, REG_TX1_ATTEN_1, buf, 2);
1377
1378 if (tx2)
1379 ret = ad9361_spi_writem(phy->spi, REG_TX2_ATTEN_1, buf, 2);
1380
1381 if (immed)
1382 ad9361_spi_writef(phy->spi, REG_TX2_DIG_ATTEN,
1383 IMMEDIATELY_UPDATE_TPC_ATTEN, 1);
1384
1385 return ret;
1386 }
1387
1388 /**
1389 * Get the attenuation for the selected TX channel.
1390 * @param phy The AD9361 state structure.
1391 * @param tx_num The selected channel [1, 2].
1392 * @return The attenuation value [mdB] or negative error code in case of failure.
1393 */
1394 int32_t ad9361_get_tx_atten(struct ad9361_rf_phy *phy, uint32_t tx_num)
1395 {
1396 uint8_t buf[2];
1397 int32_t ret = 0;
1398 uint32_t code;
1399
1400 ret = ad9361_spi_readm(phy->spi, (tx_num == 1) ?
1401 REG_TX1_ATTEN_1 : REG_TX2_ATTEN_1, buf, 2);
1402
1403 if (ret < 0)
1404 return ret;
1405
1406 code = (buf[0] << 8) | buf[1];
1407
1408 code *= 250;
1409
1410 return code;
1411 }
1412
1413 /**
1414 * Mute TX.
1415 * @param phy The AD9361 state structure.
1416 * @param state The state - 0 mute; 1 - unmute.
1417 * @return 0 in case of success
1418 */
1419 int32_t ad9361_tx_mute(struct ad9361_rf_phy *phy, uint32_t state)
1420 {
1421 int32_t ret;
1422
1423 if (state) {
1424 phy->tx1_atten_cached = ad9361_get_tx_atten(phy, 1);
1425 phy->tx2_atten_cached = ad9361_get_tx_atten(phy, 2);
1426
1427 return ad9361_set_tx_atten(phy, 89750, true, true, true);
1428 } else {
1429 if (phy->tx1_atten_cached == phy->tx2_atten_cached)
1430 return ad9361_set_tx_atten(phy, phy->tx1_atten_cached,
1431 true, true, true);
1432
1433 ret = ad9361_set_tx_atten(phy, phy->tx1_atten_cached,
1434 true, false, true);
1435 ret |= ad9361_set_tx_atten(phy, phy->tx2_atten_cached,
1436 false, true, true);
1437
1438 return ret;
1439 }
1440 }
1441
1442 /**
1443 * Choose the right RF VCO table index for the selected frequency.
1444 * @param freq The frequency value [Hz].
1445 * @return The index from the RF VCO table.
1446 */
1447 static uint32_t ad9361_rfvco_tableindex(uint32_t freq)
1448 {
1449 if (freq < 50000000UL)
1450 return LUT_FTDD_40;
1451
1452 if (freq <= 70000000UL)
1453 return LUT_FTDD_60;
1454
1455 return LUT_FTDD_80;
1456 }
1457
1458 /**
1459 * Initialize the RFPLL VCO.
1460 * @param phy The AD9361 state structure.
1461 * @param tx Set true for TX_RFPLL.
1462 * @param vco_freq The VCO frequency [Hz].
1463 * @param ref_clk The reference clock frequency [Hz].
1464 * @return 0 in case of success
1465 */
1466 static int32_t ad9361_rfpll_vco_init(struct ad9361_rf_phy *phy,
1467 bool tx, uint64_t vco_freq,
1468 uint32_t ref_clk)
1469 {
1470 struct spi_device *spi = phy->spi;
1471 const struct SynthLUT(*tab);
1472 int32_t i = 0;
1473 uint32_t range, offs = 0;
1474
1475 range = ad9361_rfvco_tableindex(ref_clk);
1476
1477 dev_dbg(&phy->spi->dev, "%s : vco_freq %"PRIu64" : ref_clk %"PRIu32" : range %"PRIu32,
1478 __func__, vco_freq, ref_clk, range);
1479
1480 do_div(&vco_freq, 1000000UL); /* vco_freq in MHz */
1481
1482 if ((phy->pdata->fdd && !phy->pdata->fdd_independent_mode)
1483 && (phy->current_tx_lo_freq != phy->current_rx_lo_freq)) {
1484 tab = &SynthLUT_FDD[range][0];
1485 if (tx)
1486 phy->current_tx_use_tdd_table = false;
1487 else
1488 phy->current_rx_use_tdd_table = false;
1489 } else {
1490 tab = &SynthLUT_TDD[range][0];
1491 if (tx)
1492 phy->current_tx_use_tdd_table = true;
1493 else
1494 phy->current_rx_use_tdd_table = true;
1495 }
1496
1497 if (tx)
1498 offs = REG_TX_VCO_OUTPUT - REG_RX_VCO_OUTPUT;
1499
1500 while (i < SYNTH_LUT_SIZE && tab[i].VCO_MHz > vco_freq)
1501 i++;
1502
1503 dev_dbg(&phy->spi->dev, "%s : freq %d MHz : index %"PRId32,
1504 __func__, tab[i].VCO_MHz, i);
1505
1506 ad9361_spi_write(spi, REG_RX_VCO_OUTPUT + offs,
1507 VCO_OUTPUT_LEVEL(tab[i].VCO_Output_Level) |
1508 PORB_VCO_LOGIC);
1509 ad9361_spi_writef(spi, REG_RX_ALC_VARACTOR + offs,
1510 VCO_VARACTOR(~0), tab[i].VCO_Varactor);
1511 ad9361_spi_write(spi, REG_RX_VCO_BIAS_1 + offs,
1512 VCO_BIAS_REF(tab[i].VCO_Bias_Ref) |
1513 VCO_BIAS_TCF(tab[i].VCO_Bias_Tcf));
1514
1515 ad9361_spi_write(spi, REG_RX_FORCE_VCO_TUNE_1 + offs,
1516 VCO_CAL_OFFSET(tab[i].VCO_Cal_Offset));
1517 ad9361_spi_write(spi, REG_RX_VCO_VARACTOR_CTRL_1 + offs,
1518 VCO_VARACTOR_REFERENCE(
1519 tab[i].VCO_Varactor_Reference));
1520
1521 ad9361_spi_write(spi, REG_RX_VCO_CAL_REF + offs, VCO_CAL_REF_TCF(0));
1522
1523 ad9361_spi_write(spi, REG_RX_VCO_VARACTOR_CTRL_0 + offs,
1524 VCO_VARACTOR_OFFSET(0) |
1525 VCO_VARACTOR_REFERENCE_TCF(7));
1526
1527 ad9361_spi_writef(spi, REG_RX_CP_CURRENT + offs, CHARGE_PUMP_CURRENT(~0),
1528 tab[i].Charge_Pump_Current);
1529 ad9361_spi_write(spi, REG_RX_LOOP_FILTER_1 + offs,
1530 LOOP_FILTER_C2(tab[i].LF_C2) |
1531 LOOP_FILTER_C1(tab[i].LF_C1));
1532 ad9361_spi_write(spi, REG_RX_LOOP_FILTER_2 + offs,
1533 LOOP_FILTER_R1(tab[i].LF_R1) |
1534 LOOP_FILTER_C3(tab[i].LF_C3));
1535 ad9361_spi_write(spi, REG_RX_LOOP_FILTER_3 + offs,
1536 LOOP_FILTER_R3(tab[i].LF_R3));
1537
1538 return 0;
1539 }
1540
1541 /**
1542 * Get the current gain in Split Gain Table Mode
1543 * @param phy The AD9361 state structure.
1544 * @param idx_reg Register base address for the selected receiver
1545 * @param rx_gain A rf_rx_gain struct to store the RF gain.
1546 * @return 0 in case of success,
1547 */
1548 static int32_t ad9361_get_split_table_gain(struct ad9361_rf_phy *phy, uint32_t idx_reg,
1549 struct rf_rx_gain *rx_gain)
1550 {
1551 struct spi_device *spi = phy->spi;
1552 uint32_t val, tbl_addr;
1553 int32_t rc = 0;
1554
1555 rx_gain->fgt_lmt_index = ad9361_spi_readf(spi, idx_reg,
1556 FULL_TABLE_GAIN_INDEX(~0));
1557 tbl_addr = ad9361_spi_read(spi, REG_GAIN_TABLE_ADDRESS);
1558
1559 ad9361_spi_write(spi, REG_GAIN_TABLE_ADDRESS, rx_gain->fgt_lmt_index);
1560
1561 val = ad9361_spi_read(spi, REG_GAIN_TABLE_READ_DATA1);
1562 rx_gain->lna_index = TO_LNA_GAIN(val);
1563 rx_gain->mixer_index = TO_MIXER_GM_GAIN(val);
1564
1565 rx_gain->tia_index = ad9361_spi_readf(spi, REG_GAIN_TABLE_READ_DATA2, TIA_GAIN);
1566
1567 rx_gain->lmt_gain = lna_table[phy->current_table][rx_gain->lna_index] +
1568 mixer_table[phy->current_table][rx_gain->mixer_index] +
1569 tia_table[rx_gain->tia_index];
1570
1571 ad9361_spi_write(spi, REG_GAIN_TABLE_ADDRESS, tbl_addr);
1572
1573 /* Read LPF Index */
1574 rx_gain->lpf_gain = ad9361_spi_readf(spi, idx_reg + 1, LPF_GAIN_RX(~0));
1575
1576 /* Read Digital Gain */
1577 rx_gain->digital_gain = ad9361_spi_readf(spi, idx_reg + 2,
1578 DIGITAL_GAIN_RX(~0));
1579
1580 rx_gain->gain_db = rx_gain->lmt_gain + rx_gain->lpf_gain +
1581 rx_gain->digital_gain;
1582 return rc;
1583 }
1584
1585 /**
1586 * Get the current gain in Full Gain Table Mode
1587 * @param phy The AD9361 state structure.
1588 * @param idx_reg Register base address for the selected receiver
1589 * @param rx_gain A rf_rx_gain struct to store the RF gain.
1590 * @return 0 in case of success
1591 */
1592 static int32_t ad9361_get_full_table_gain(struct ad9361_rf_phy *phy, uint32_t idx_reg,
1593 struct rf_rx_gain *rx_gain)
1594 {
1595 struct spi_device *spi = phy->spi;
1596 int32_t val;
1597 enum rx_gain_table_name tbl;
1598 struct rx_gain_info *gain_info;
1599 int32_t rc = 0, rx_gain_db;
1600
1601 tbl = ad9361_gt_tableindex(
1602 ad9361_from_clk(clk_get_rate(phy, phy->ref_clk_scale[RX_RFPLL])));
1603
1604 rx_gain->fgt_lmt_index = val = ad9361_spi_readf(spi, idx_reg,
1605 FULL_TABLE_GAIN_INDEX(~0));
1606 gain_info = &phy->rx_gain[tbl];
1607 if (val > gain_info->idx_step_offset) {
1608 val = val - gain_info->idx_step_offset;
1609 rx_gain_db = gain_info->starting_gain_db +
1610 ((val)* gain_info->gain_step_db);
1611 }
1612 else {
1613 rx_gain_db = gain_info->starting_gain_db;
1614 }
1615
1616 /* Read Digital Gain */
1617 rx_gain->digital_gain = ad9361_spi_readf(spi, idx_reg + 2,
1618 DIGITAL_GAIN_RX(~0));
1619
1620 rx_gain->gain_db = rx_gain_db;
1621
1622 return rc;
1623 }
1624
1625 /**
1626 * Get current RX gain for the selected channel.
1627 * @param phy The AD9361 state structure.
1628 * @param rx_id The desired channel number (0, 1).
1629 * @param rx_gain A rf_rx_gain struct to store the RF gain.
1630 * @return 0 in case of success, negative error code otherwise.
1631 */
1632 int32_t ad9361_get_rx_gain(struct ad9361_rf_phy *phy,
1633 uint32_t rx_id, struct rf_rx_gain *rx_gain)
1634 {
1635 struct spi_device *spi = phy->spi;
1636 uint32_t val, idx_reg;
1637 uint8_t gain_ctl_shift, rx_enable_mask;
1638 uint8_t fast_atk_shift;
1639 int32_t rc = 0;
1640
1641 if (rx_id == 1) {
1642 gain_ctl_shift = RX1_GAIN_CTRL_SHIFT;
1643 idx_reg = REG_GAIN_RX1;
1644 rx_enable_mask = RX_CHANNEL_ENABLE(RX_1);
1645 fast_atk_shift = RX1_FAST_ATK_SHIFT;
1646
1647 }
1648 else if (rx_id == 2) {
1649 gain_ctl_shift = RX2_GAIN_CTRL_SHIFT;
1650 idx_reg = REG_GAIN_RX2;
1651 rx_enable_mask = RX_CHANNEL_ENABLE(RX_2);
1652 fast_atk_shift = RX2_FAST_ATK_SHIFT;
1653 }
1654 else {
1655 dev_err(dev, "Unknown Rx path %"PRIu32, rx_id);
1656 rc = -EINVAL;
1657 goto out;
1658 }
1659
1660 val = ad9361_spi_readf(spi, REG_RX_ENABLE_FILTER_CTRL, rx_enable_mask);
1661
1662 if (!val) {
1663 dev_dbg(dev, "Rx%"PRIu32" is not enabled", rx_gain->ant);
1664 rc = -EAGAIN;
1665 goto out;
1666 }
1667
1668 val = ad9361_spi_read(spi, REG_AGC_CONFIG_1);
1669
1670 val = (val >> gain_ctl_shift) & RX_GAIN_CTL_MASK;
1671
1672 if (val == RX_GAIN_CTL_AGC_FAST_ATK) {
1673 /* In fast attack mode check whether Fast attack state machine
1674 * has locked gain, if not then we can not read gain.
1675 */
1676 val = ad9361_spi_read(spi, REG_FAST_ATTACK_STATE);
1677 val = (val >> fast_atk_shift) & FAST_ATK_MASK;
1678 if (val != FAST_ATK_GAIN_LOCKED) {
1679 dev_warn(dev, "Failed to read gain, state m/c at %"PRIx32,
1680 val);
1681 rc = -EAGAIN;
1682 goto out;
1683 }
1684 }
1685
1686 if (has_split_gt && phy->pdata->split_gt)
1687 rc = ad9361_get_split_table_gain(phy, idx_reg, rx_gain);
1688 else
1689 rc = ad9361_get_full_table_gain(phy, idx_reg, rx_gain);
1690
1691 out:
1692 return rc;
1693 }
1694
1695 /**
1696 * Force Enable State Machine (ENSM) to the desired state (internally used only).
1697 * @param phy The AD9361 state structure.
1698 * @param ensm_state The ENSM state [ENSM_STATE_SLEEP_WAIT, ENSM_STATE_ALERT,
1699 * ENSM_STATE_TX, ENSM_STATE_TX_FLUSH, ENSM_STATE_RX,
1700 * ENSM_STATE_RX_FLUSH, ENSM_STATE_FDD, ENSM_STATE_FDD_FLUSH].
1701 * @return None.
1702 */
1703 void ad9361_ensm_force_state(struct ad9361_rf_phy *phy, uint8_t ensm_state)
1704 {
1705 struct spi_device *spi = phy->spi;
1706 uint8_t dev_ensm_state;
1707 int32_t rc;
1708 uint32_t val;
1709
1710 dev_ensm_state = ad9361_spi_readf(spi, REG_STATE, ENSM_STATE(~0));
1711
1712 phy->prev_ensm_state = dev_ensm_state;
1713
1714 if (dev_ensm_state == ensm_state) {
1715 dev_dbg(dev, "Nothing to do, device is already in %d state",
1716 ensm_state);
1717 goto out;
1718 }
1719
1720 dev_dbg(dev, "Device is in %x state, forcing to %x", dev_ensm_state,
1721 ensm_state);
1722
1723 val = ad9361_spi_read(spi, REG_ENSM_CONFIG_1);
1724
1725 /* Enable control through SPI writes, and take out from
1726 * Alert
1727 */
1728 if (val & ENABLE_ENSM_PIN_CTRL) {
1729 val &= ~ENABLE_ENSM_PIN_CTRL;
1730 phy->ensm_pin_ctl_en = true;
1731 }
1732 else {
1733 phy->ensm_pin_ctl_en = false;
1734 }
1735
1736 if (dev_ensm_state)
1737 val &= ~(TO_ALERT);
1738
1739 switch (ensm_state) {
1740
1741 case ENSM_STATE_TX:
1742 case ENSM_STATE_FDD:
1743 val |= FORCE_TX_ON;
1744 break;
1745 case ENSM_STATE_RX:
1746 val |= FORCE_RX_ON;
1747 break;
1748 case ENSM_STATE_ALERT:
1749 val &= ~(FORCE_TX_ON | FORCE_RX_ON);
1750 val |= TO_ALERT | FORCE_ALERT_STATE;
1751 break;
1752 default:
1753 dev_err(dev, "No handling for forcing %d ensm state",
1754 ensm_state);
1755 goto out;
1756 }
1757
1758 ad9361_spi_write(spi, REG_ENSM_CONFIG_1, TO_ALERT | FORCE_ALERT_STATE);
1759
1760 rc = ad9361_spi_write(spi, REG_ENSM_CONFIG_1, val);
1761 if (rc)
1762 dev_err(dev, "Failed to restore state");
1763
1764 out:
1765 return;
1766
1767 }
1768
1769 /**
1770 * Restore the previous Enable State Machine (ENSM) state.
1771 * @param phy The AD9361 state structure.
1772 * @return None.
1773 */
1774 void ad9361_ensm_restore_prev_state(struct ad9361_rf_phy *phy)
1775 {
1776 struct spi_device *spi = phy->spi;
1777 int32_t rc;
1778 uint32_t val;
1779
1780 val = ad9361_spi_read(spi, REG_ENSM_CONFIG_1);
1781
1782 /* We are restoring state only, so clear State bits first
1783 * which might have set while forcing a particular state
1784 */
1785 val &= ~(FORCE_TX_ON | FORCE_RX_ON |
1786 TO_ALERT | FORCE_ALERT_STATE);
1787
1788 switch (phy->prev_ensm_state) {
1789
1790 case ENSM_STATE_TX:
1791 case ENSM_STATE_FDD:
1792 val |= FORCE_TX_ON;
1793 break;
1794 case ENSM_STATE_RX:
1795 val |= FORCE_RX_ON;
1796 break;
1797 case ENSM_STATE_ALERT:
1798 val |= TO_ALERT;
1799 break;
1800 case ENSM_STATE_INVALID:
1801 dev_dbg(dev, "No need to restore, ENSM state wasn't saved");
1802 goto out;
1803 default:
1804 dev_dbg(dev, "Could not restore to %d ENSM state",
1805 phy->prev_ensm_state);
1806 goto out;
1807 }
1808
1809 ad9361_spi_write(spi, REG_ENSM_CONFIG_1, TO_ALERT | FORCE_ALERT_STATE);
1810
1811 rc = ad9361_spi_write(spi, REG_ENSM_CONFIG_1, val);
1812 if (rc) {
1813 dev_err(dev, "Failed to write ENSM_CONFIG_1");
1814 goto out;
1815 }
1816
1817 if (phy->ensm_pin_ctl_en) {
1818 val |= ENABLE_ENSM_PIN_CTRL;
1819 rc = ad9361_spi_write(spi, REG_ENSM_CONFIG_1, val);
1820 if (rc)
1821 dev_err(dev, "Failed to write ENSM_CONFIG_1");
1822 }
1823
1824 out:
1825 return;
1826 }
1827
1828 /**
1829 * Set gain in Split Gain Table Mode (used only in Manual Gain Control Mode).
1830 * @param phy The AD9361 state structure.
1831 * @param idx_reg Register base address for the selected receiver
1832 * @param rx_gain The rf_rx_gain struct containing the RF gain.
1833 * @return 0 in case of success, negative error code otherwise.
1834 */
1835 static int32_t set_split_table_gain(struct ad9361_rf_phy *phy, uint32_t idx_reg,
1836 struct rf_rx_gain *rx_gain)
1837 {
1838 struct spi_device *spi = phy->spi;
1839 int32_t rc = 0;
1840
1841 if ((rx_gain->fgt_lmt_index > MAX_LMT_INDEX) ||
1842 (rx_gain->lpf_gain > MAX_LPF_GAIN) ||
1843 (rx_gain->digital_gain > MAX_DIG_GAIN)) {
1844 dev_err(dev, "LMT_INDEX missing or greater than max value %d",
1845 MAX_LMT_INDEX);
1846 dev_err(dev, "LPF_GAIN missing or greater than max value %d",
1847 MAX_LPF_GAIN);
1848 dev_err(dev, "DIGITAL_GAIN cannot be more than %d",
1849 MAX_DIG_GAIN);
1850 rc = -EINVAL;
1851 goto out;
1852 }
1853 if (rx_gain->gain_db > 0)
1854 dev_dbg(dev, "Ignoring rx_gain value in split table mode.");
1855 if (rx_gain->fgt_lmt_index == 0 && rx_gain->lpf_gain == 0 &&
1856 rx_gain->digital_gain == 0) {
1857 dev_err(dev,
1858 "In split table mode, All LMT/LPF/digital gains cannot be 0");
1859 rc = -EINVAL;
1860 goto out;
1861 }
1862
1863 ad9361_spi_writef(spi, idx_reg, RX_FULL_TBL_IDX_MASK, rx_gain->fgt_lmt_index);
1864 ad9361_spi_writef(spi, idx_reg + 1, RX_LPF_IDX_MASK, rx_gain->lpf_gain);
1865
1866 if (phy->pdata->gain_ctrl.dig_gain_en) {
1867 ad9361_spi_writef(spi, idx_reg + 2, RX_DIGITAL_IDX_MASK, rx_gain->digital_gain);
1868
1869 }
1870 else if (rx_gain->digital_gain > 0) {
1871 dev_err(dev, "Digital gain is disabled and cannot be set");
1872 }
1873 out:
1874 return rc;
1875 }
1876
1877 /**
1878 * Set gain in Full Gain Table Mode (used only in Manual Gain Control Mode).
1879 * @param phy The AD9361 state structure.
1880 * @param idx_reg Register base address for the selected receiver
1881 * @param rx_gain The rf_rx_gain struct containing the RF gain.
1882 * @return 0 in case of success, negative error code otherwise.
1883 */
1884 static int32_t set_full_table_gain(struct ad9361_rf_phy *phy, uint32_t idx_reg,
1885 struct rf_rx_gain *rx_gain)
1886 {
1887 struct spi_device *spi = phy->spi;
1888 enum rx_gain_table_name tbl;
1889 struct rx_gain_info *gain_info;
1890 uint32_t val;
1891 int32_t rc = 0;
1892
1893 if (rx_gain->fgt_lmt_index != (uint32_t)~0 || (int64_t)rx_gain->lpf_gain != (uint32_t)~0 ||
1894 rx_gain->digital_gain > 0)
1895 dev_dbg(dev,
1896 "Ignoring lmt/lpf/digital gains in Single Table mode");
1897
1898 tbl = ad9361_gt_tableindex(
1899 ad9361_from_clk(clk_get_rate(phy, phy->ref_clk_scale[RX_RFPLL])));
1900
1901 gain_info = &phy->rx_gain[tbl];
1902 if ((rx_gain->gain_db < gain_info->starting_gain_db) ||
1903 (rx_gain->gain_db > gain_info->max_gain_db)) {
1904
1905 dev_err(dev, "Invalid gain %"PRId32", supported range [%"PRId32" - %"PRId32"]",
1906 rx_gain->gain_db, gain_info->starting_gain_db,
1907 gain_info->max_gain_db);
1908 rc = -EINVAL;
1909 goto out;
1910
1911 }
1912
1913 val = ((rx_gain->gain_db - gain_info->starting_gain_db) /
1914 gain_info->gain_step_db) + gain_info->idx_step_offset;
1915 ad9361_spi_writef(spi, idx_reg, RX_FULL_TBL_IDX_MASK, val);
1916
1917 out:
1918 return rc;
1919 }
1920
1921 /**
1922 * Set the RX gain for the selected channel.
1923 * @param phy The AD9361 state structure.
1924 * @param rx_id The desired channel number (0, 1).
1925 * @param rx_gain The rf_rx_gain struct containing the RF gain.
1926 * @return 0 in case of success, negative error code otherwise.
1927 */
1928 int32_t ad9361_set_rx_gain(struct ad9361_rf_phy *phy,
1929 uint32_t rx_id, struct rf_rx_gain *rx_gain)
1930 {
1931 struct spi_device *spi = phy->spi;
1932 uint32_t val, idx_reg;
1933 uint8_t gain_ctl_shift;
1934 int32_t rc = 0;
1935
1936 if (rx_id == 1) {
1937 gain_ctl_shift = RX1_GAIN_CTRL_SHIFT;
1938 idx_reg = REG_RX1_MANUAL_LMT_FULL_GAIN;
1939
1940 }
1941 else if (rx_id == 2) {
1942 gain_ctl_shift = RX2_GAIN_CTRL_SHIFT;
1943 idx_reg = REG_RX2_MANUAL_LMT_FULL_GAIN;
1944 }
1945 else {
1946 dev_err(dev, "Unknown Rx path %"PRIu32, rx_id);
1947 rc = -EINVAL;
1948 goto out;
1949
1950 }
1951
1952 val = ad9361_spi_read(spi, REG_AGC_CONFIG_1);
1953 val = (val >> gain_ctl_shift) & RX_GAIN_CTL_MASK;
1954
1955 if (val != RX_GAIN_CTL_MGC) {
1956 dev_dbg(dev, "Rx gain can be set in MGC mode only");
1957 goto out;
1958 }
1959
1960 if (has_split_gt && phy->pdata->split_gt)
1961 rc = set_split_table_gain(phy, idx_reg, rx_gain);
1962 else
1963 rc = set_full_table_gain(phy, idx_reg, rx_gain);
1964
1965 if (rc) {
1966 dev_err(dev, "Unable to write gain tbl idx reg: %"PRIu32, idx_reg);
1967 goto out;
1968 }
1969
1970 out:
1971 return rc;
1972
1973 }
1974
1975 /**
1976 * Initialize the rx_gain_info structure.
1977 * @param rx_gain The rx_gain_info structure pointer.
1978 * @param type Either Full or Split Table
1979 * @param starting_gain The starting gain value.
1980 * @param max_gain The maximum gain value.
1981 * @param gain_step The gain step.
1982 * @param max_idx The max table size.
1983 * @param idx_offset Offset in the table where linear progression starts
1984 * @return None
1985 */
1986 static void ad9361_init_gain_info(struct rx_gain_info *rx_gain,
1987 enum rx_gain_table_type type, int32_t starting_gain,
1988 int32_t max_gain, int32_t gain_step, int32_t max_idx, int32_t idx_offset)
1989 {
1990 rx_gain->tbl_type = type;
1991 rx_gain->starting_gain_db = starting_gain;
1992 rx_gain->max_gain_db = max_gain;
1993 rx_gain->gain_step_db = gain_step;
1994 rx_gain->max_idx = max_idx;
1995 rx_gain->idx_step_offset = idx_offset;
1996 }
1997
1998 /**
1999 * Initialize the gain table information.
2000 * @param phy The AD9361 state structure.
2001 * @return 0 in case of success, negative error code otherwise.
2002 */
2003 int32_t ad9361_init_gain_tables(struct ad9361_rf_phy *phy)
2004 {
2005 struct rx_gain_info *rx_gain;
2006
2007 /* Intialize Meta data according to default gain tables
2008 * of AD9631. Changing/Writing of gain tables is not
2009 * supported yet.
2010 */
2011 rx_gain = &phy->rx_gain[TBL_200_1300_MHZ];
2012 ad9361_init_gain_info(rx_gain, RXGAIN_FULL_TBL, 1, 77, 1,
2013 SIZE_FULL_TABLE, 0);
2014
2015 rx_gain = &phy->rx_gain[TBL_1300_4000_MHZ];
2016 ad9361_init_gain_info(rx_gain, RXGAIN_FULL_TBL, -4, 71, 1,
2017 SIZE_FULL_TABLE, 1);
2018
2019 rx_gain = &phy->rx_gain[TBL_4000_6000_MHZ];
2020 ad9361_init_gain_info(rx_gain, RXGAIN_FULL_TBL, -10, 62, 1,
2021 SIZE_FULL_TABLE, 4);
2022
2023 return 0;
2024 }
2025
2026 /**
2027 * Update the Gain Control.
2028 * @param phy The AD9361 state structure.
2029 * @return 0 in case of success, negative error code otherwise.
2030 */
2031 static int32_t ad9361_gc_update(struct ad9361_rf_phy *phy)
2032 {
2033 struct spi_device *spi = phy->spi;
2034 uint32_t clkrf;
2035 uint32_t reg, delay_lna, settling_delay, dec_pow_meas_dur;
2036 int32_t ret;
2037 uint32_t fir_div;
2038
2039 clkrf = clk_get_rate(phy, phy->ref_clk_scale[CLKRF_CLK]);
2040 delay_lna = phy->pdata->elna_ctrl.settling_delay_ns;
2041
2042 /*
2043 * AGC Attack Delay (us)=ceiling((((0.2+Delay_LNA)*ClkRF+14))/(2*ClkRF))+1
2044 * ClkRF in MHz, delay in us
2045 */
2046
2047 reg = (200 * delay_lna) / 2 + (14000000UL / (clkrf / 500U));
2048 reg = DIV_ROUND_UP(reg, 1000UL) +
2049 phy->pdata->gain_ctrl.agc_attack_delay_extra_margin_us;
2050 reg = clamp_t(uint8_t, reg, 0U, 31U);
2051 ret = ad9361_spi_writef(spi, REG_AGC_ATTACK_DELAY,
2052 AGC_ATTACK_DELAY(~0), reg);
2053
2054 /*
2055 * Peak Overload Wait Time (ClkRF cycles)=ceiling((0.1+Delay_LNA) *clkRF+1)
2056 */
2057
2058 reg = (delay_lna + 100UL) * (clkrf / 1000UL);
2059 reg = DIV_ROUND_UP(reg, 1000000UL) + 1;
2060 reg = clamp_t(uint8_t, reg, 0U, 31U);
2061 ret |= ad9361_spi_writef(spi, REG_PEAK_WAIT_TIME,
2062 PEAK_OVERLOAD_WAIT_TIME(~0), reg);
2063
2064 /*
2065 * Settling Delay in 0x111. Applies to all gain control modes:
2066 * 0x111[D4:D0]= ceiling(((0.2+Delay_LNA)*clkRF
2067 dodebug = false;+14)/2)
2068 */
2069
2070 reg = (delay_lna + 200UL) * (clkrf / 2000UL);
2071 reg = DIV_ROUND_UP(reg, 1000000UL) + 7;
2072 reg = settling_delay = clamp_t(uint8_t, reg, 0U, 31U);
2073 ret |= ad9361_spi_writef(spi, REG_FAST_CONFIG_2_SETTLING_DELAY,
2074 SETTLING_DELAY(~0), reg);
2075
2076 /*
2077 * Gain Update Counter [15:0]= round((((time*ClkRF-0x111[D4:D0]*2)-2))/2)
2078 */
2079 reg = phy->pdata->gain_ctrl.gain_update_interval_us * (clkrf / 1000UL) -
2080 settling_delay * 2000UL - 2000UL;
2081
2082 reg = DIV_ROUND_CLOSEST(reg, 2000UL);
2083 reg = clamp_t(uint32_t, reg, 0U, 131071UL);
2084
2085 if (phy->agc_mode[0] == RF_GAIN_FASTATTACK_AGC ||
2086 phy->agc_mode[1] == RF_GAIN_FASTATTACK_AGC) {
2087 dec_pow_meas_dur =
2088 phy->pdata->gain_ctrl.f_agc_dec_pow_measuremnt_duration;
2089 } else {
2090 fir_div = DIV_ROUND_CLOSEST(clkrf,
2091 clk_get_rate(phy, phy->ref_clk_scale[RX_SAMPL_CLK]));
2092 dec_pow_meas_dur = phy->pdata->gain_ctrl.dec_pow_measuremnt_duration;
2093
2094 if (((reg * 2 / fir_div) / dec_pow_meas_dur) < 2) {
2095 dec_pow_meas_dur = reg / fir_div;
2096 }
2097 }
2098
2099 /* Power Measurement Duration */
2100 ad9361_spi_writef(spi, REG_DEC_POWER_MEASURE_DURATION_0,
2101 DEC_POWER_MEASUREMENT_DURATION(~0),
2102 ilog2(dec_pow_meas_dur / 16));
2103
2104
2105 ret |= ad9361_spi_writef(spi, REG_DIGITAL_SAT_COUNTER,
2106 DOUBLE_GAIN_COUNTER, reg > 65535);
2107
2108 if (reg > 65535)
2109 reg /= 2;
2110
2111 ret |= ad9361_spi_write(spi, REG_GAIN_UPDATE_COUNTER1, reg & 0xFF);
2112 ret |= ad9361_spi_write(spi, REG_GAIN_UPDATE_COUNTER2, reg >> 8);
2113
2114 /*
2115 * Fast AGC State Wait Time - Energy Detect Count
2116 */
2117
2118 reg = DIV_ROUND_CLOSEST(phy->pdata->gain_ctrl.f_agc_state_wait_time_ns *
2119 (clkrf / 1000UL), 1000000UL);
2120 reg = clamp_t(uint32_t, reg, 0U, 31U);
2121 ret |= ad9361_spi_writef(spi, REG_FAST_ENERGY_DETECT_COUNT,
2122 ENERGY_DETECT_COUNT(~0), reg);
2123
2124 return ret;
2125 }
2126
2127 /**
2128 * Set the gain control mode.
2129 * @param phy The AD9361 state structure.
2130 * @param rf_gain_ctrl A rf_gain_ctrl struct that contains the the desired
2131 * channel information and the gain control mode.
2132 * @return 0 in case of success, negative error code otherwise.
2133 */
2134 int32_t ad9361_set_gain_ctrl_mode(struct ad9361_rf_phy *phy,
2135 struct rf_gain_ctrl *gain_ctrl)
2136 {
2137 struct spi_device *spi = phy->spi;
2138 int32_t rc = 0;
2139 uint32_t gain_ctl_shift, mode;
2140 uint8_t val;
2141
2142 rc = ad9361_spi_readm(spi, REG_AGC_CONFIG_1, &val, 1);
2143 if (rc) {
2144 dev_err(dev, "Unable to read AGC config1 register: %x",
2145 REG_AGC_CONFIG_1);
2146 goto out;
2147 }
2148
2149 switch (gain_ctrl->mode) {
2150 case RF_GAIN_MGC:
2151 mode = RX_GAIN_CTL_MGC;
2152 break;
2153 case RF_GAIN_FASTATTACK_AGC:
2154 mode = RX_GAIN_CTL_AGC_FAST_ATK;
2155 break;
2156 case RF_GAIN_SLOWATTACK_AGC:
2157 mode = RX_GAIN_CTL_AGC_SLOW_ATK;
2158 break;
2159 case RF_GAIN_HYBRID_AGC:
2160 mode = RX_GAIN_CTL_AGC_SLOW_ATK_HYBD;
2161 break;
2162 default:
2163 rc = -EINVAL;
2164 goto out;
2165 }
2166
2167 if (gain_ctrl->ant == 1) {
2168 gain_ctl_shift = RX1_GAIN_CTRL_SHIFT;
2169 }
2170 else if (gain_ctrl->ant == 2) {
2171 gain_ctl_shift = RX2_GAIN_CTRL_SHIFT;
2172 }
2173 else {
2174 dev_err(dev, "Unknown Rx path %"PRIu32, gain_ctrl->ant);
2175 rc = -EINVAL;
2176 goto out;
2177 }
2178
2179 rc = ad9361_en_dis_rx(phy, gain_ctrl->ant, RX_DISABLE);
2180 if (rc) {
2181 dev_err(dev, "Unable to disable rx%"PRIu32, gain_ctrl->ant);
2182 goto out;
2183 }
2184
2185 val &= ~(RX_GAIN_CTL_MASK << gain_ctl_shift);
2186 val |= mode << gain_ctl_shift;
2187 if (mode == RX_GAIN_CTL_AGC_SLOW_ATK_HYBD)
2188 val |= SLOW_ATTACK_HYBRID_MODE;
2189 else
2190 val &= ~SLOW_ATTACK_HYBRID_MODE;
2191
2192 rc = ad9361_spi_write(spi, REG_AGC_CONFIG_1, val);
2193 if (rc) {
2194 dev_err(dev, "Unable to write AGC config1 register: %x",
2195 REG_AGC_CONFIG_1);
2196 goto out;
2197 }
2198
2199 ad9361_en_dis_rx(phy, gain_ctrl->ant, RX_ENABLE);
2200 rc = ad9361_gc_update(phy);
2201 out:
2202 return rc;
2203 }
2204
2205 /**
2206 * Get the RSSI.
2207 * @param phy The AD9361 state structure.
2208 * @param rssi A rf_rssi struct to store the RSSI.
2209 * @return 0 in case of success, negative error code otherwise.
2210 */
2211 int32_t ad9361_read_rssi(struct ad9361_rf_phy *phy, struct rf_rssi *rssi)
2212 {
2213 struct spi_device *spi = phy->spi;
2214 uint8_t reg_val_buf[6];
2215 int32_t rc;
2216
2217 rc = ad9361_spi_readm(spi, REG_PREAMBLE_LSB,
2218 reg_val_buf, ARRAY_SIZE(reg_val_buf));
2219 if (rssi->ant == 1) {
2220 rssi->symbol = RSSI_RESOLUTION *
2221 ((reg_val_buf[5] << RSSI_LSB_SHIFT) +
2222 (reg_val_buf[1] & RSSI_LSB_MASK1));
2223 rssi->preamble = RSSI_RESOLUTION *
2224 ((reg_val_buf[4] << RSSI_LSB_SHIFT) +
2225 (reg_val_buf[0] & RSSI_LSB_MASK1));
2226 }
2227 else if (rssi->ant == 2) {
2228 rssi->symbol = RSSI_RESOLUTION *
2229 ((reg_val_buf[3] << RSSI_LSB_SHIFT) +
2230 ((reg_val_buf[1] & RSSI_LSB_MASK2) >> 1));
2231 rssi->preamble = RSSI_RESOLUTION *
2232 ((reg_val_buf[2] << RSSI_LSB_SHIFT) +
2233 ((reg_val_buf[0] & RSSI_LSB_MASK2) >> 1));
2234 }
2235 else
2236 rc = -EFAULT;
2237
2238 rssi->multiplier = RSSI_MULTIPLIER;
2239
2240 return rc;
2241 }
2242
2243 /**
2244 * Setup the RX ADC.
2245 * @param phy The AD9361 state structure.
2246 * @param bbpll_freq The BBPLL frequency [Hz].
2247 * @param adc_sampl_freq_Hz The ADC sampling frequency [Hz].
2248 * @return 0 in case of success, negative error code otherwise.
2249 */
2250 static int32_t ad9361_rx_adc_setup(struct ad9361_rf_phy *phy, uint32_t bbpll_freq,
2251 uint32_t adc_sampl_freq_Hz)
2252 {
2253
2254 uint32_t scale_snr_1e3, maxsnr, sqrt_inv_rc_tconst_1e3, tmp_1e3,
2255 scaled_adc_clk_1e6, inv_scaled_adc_clk_1e3, sqrt_term_1e3,
2256 min_sqrt_term_1e3, bb_bw_Hz;
2257 uint64_t tmp, invrc_tconst_1e6;
2258 uint8_t data[40];
2259 uint32_t i;
2260 int32_t ret;
2261
2262 uint8_t c3_msb = ad9361_spi_read(phy->spi, REG_RX_BBF_C3_MSB);
2263 uint8_t c3_lsb = ad9361_spi_read(phy->spi, REG_RX_BBF_C3_LSB);
2264 uint8_t r2346 = ad9361_spi_read(phy->spi, REG_RX_BBF_R2346);
2265
2266 /*
2267 * BBBW = (BBPLL / RxTuneDiv) * ln(2) / (1.4 * 2PI )
2268 * We assume ad9361_rx_bb_analog_filter_calib() is always run prior
2269 */
2270
2271 tmp = bbpll_freq * 10000ULL;
2272 do_div(&tmp, 126906UL * phy->rxbbf_div);
2273 bb_bw_Hz = tmp;
2274
2275 dev_dbg(&phy->spi->dev, "%s : BBBW %"PRIu32" : ADCfreq %"PRIu32,
2276 __func__, bb_bw_Hz, adc_sampl_freq_Hz);
2277
2278 dev_dbg(&phy->spi->dev, "c3_msb 0x%X : c3_lsb 0x%X : r2346 0x%X : ",
2279 c3_msb, c3_lsb, r2346);
2280
2281 bb_bw_Hz = clamp(bb_bw_Hz, 200000UL, 28000000UL);
2282
2283 if (adc_sampl_freq_Hz < 80000000)
2284 scale_snr_1e3 = 1000;
2285 else
2286 scale_snr_1e3 = 1585; /* pow(10, scale_snr_dB/10); */
2287
2288 if (bb_bw_Hz >= 18000000) {
2289 invrc_tconst_1e6 = (160975ULL * r2346 *
2290 (160 * c3_msb + 10 * c3_lsb + 140) *
2291 (bb_bw_Hz)* (1000 + (10 * (bb_bw_Hz - 18000000) / 1000000)));
2292
2293 do_div(&invrc_tconst_1e6, 1000UL);
2294
2295 }
2296 else {
2297 invrc_tconst_1e6 = (160975ULL * r2346 *
2298 (160 * c3_msb + 10 * c3_lsb + 140) *
2299 (bb_bw_Hz));
2300 }
2301
2302 do_div(&invrc_tconst_1e6, 1000000000UL);
2303
2304 if (invrc_tconst_1e6 > ULONG_MAX)
2305 dev_err(&phy->spi->dev, "invrc_tconst_1e6 > ULONG_MAX");
2306
2307 sqrt_inv_rc_tconst_1e3 = int_sqrt((uint32_t)invrc_tconst_1e6);
2308 maxsnr = 640 / 160;
2309 scaled_adc_clk_1e6 = DIV_ROUND_CLOSEST(adc_sampl_freq_Hz, 640);
2310 inv_scaled_adc_clk_1e3 = DIV_ROUND_CLOSEST(640000000,
2311 DIV_ROUND_CLOSEST(adc_sampl_freq_Hz, 1000));
2312 tmp_1e3 = DIV_ROUND_CLOSEST(980000 + 20 * max_t(uint32_t, 1000U,
2313 DIV_ROUND_CLOSEST(inv_scaled_adc_clk_1e3, maxsnr)), 1000);
2314 sqrt_term_1e3 = int_sqrt(scaled_adc_clk_1e6);
2315 min_sqrt_term_1e3 = min_t(uint32_t, 1000U,
2316 int_sqrt(maxsnr * scaled_adc_clk_1e6));
2317
2318 dev_dbg(&phy->spi->dev, "invrc_tconst_1e6 %"PRIu64", sqrt_inv_rc_tconst_1e3 %"PRIu32,
2319 invrc_tconst_1e6, sqrt_inv_rc_tconst_1e3);
2320 dev_dbg(&phy->spi->dev, "scaled_adc_clk_1e6 %"PRIu32", inv_scaled_adc_clk_1e3 %"PRIu32,
2321 scaled_adc_clk_1e6, inv_scaled_adc_clk_1e3);
2322 dev_dbg(&phy->spi->dev, "tmp_1e3 %"PRIu32", sqrt_term_1e3 %"PRIu32", min_sqrt_term_1e3 %"PRIu32,
2323 tmp_1e3, sqrt_term_1e3, min_sqrt_term_1e3);
2324
2325 data[0] = 0;
2326 data[1] = 0;
2327 data[2] = 0;
2328 data[3] = 0x24;
2329 data[4] = 0x24;
2330 data[5] = 0;
2331 data[6] = 0;
2332
2333 tmp = -50000000 + 8ULL * scale_snr_1e3 * sqrt_inv_rc_tconst_1e3 *
2334 min_sqrt_term_1e3;
2335 do_div(&tmp, 100000000UL);
2336 data[7] = min_t(uint64_t, 124U, tmp);
2337
2338 tmp = (invrc_tconst_1e6 >> 1) + 20 * inv_scaled_adc_clk_1e3 *
2339 data[7] / 80 * 1000ULL;
2340 do_div(&tmp, invrc_tconst_1e6);
2341 data[8] = min_t(uint64_t, 255U, tmp);
2342
2343 tmp = (-500000 + 77ULL * sqrt_inv_rc_tconst_1e3 * min_sqrt_term_1e3);
2344 do_div(&tmp, 1000000UL);
2345 data[10] = min_t(uint64_t, 127U, tmp);
2346
2347 data[9] = min_t(uint32_t, 127U, ((800 * data[10]) / 1000));
2348 tmp = ((invrc_tconst_1e6 >> 1) + (20 * inv_scaled_adc_clk_1e3 *
2349 data[10] * 1000ULL));
2350 do_div(&tmp, invrc_tconst_1e6 * 77);
2351 data[11] = min_t(uint64_t, 255U, tmp);
2352 data[12] = min_t(uint32_t, 127U, (-500000 + 80 * sqrt_inv_rc_tconst_1e3 *
2353 min_sqrt_term_1e3) / 1000000UL);
2354
2355 tmp = -3 * (long)(invrc_tconst_1e6 >> 1) + inv_scaled_adc_clk_1e3 *
2356 data[12] * (1000ULL * 20 / 80);
2357 do_div(&tmp, invrc_tconst_1e6);
2358 data[13] = min_t(uint64_t, 255, tmp);
2359
2360 data[14] = 21 * (inv_scaled_adc_clk_1e3 / 10000);
2361 data[15] = min_t(uint32_t, 127U, (500 + 1025 * data[7]) / 1000);
2362 data[16] = min_t(uint32_t, 127U, (data[15] * tmp_1e3) / 1000);
2363 data[17] = data[15];
2364 data[18] = min_t(uint32_t, 127U, (500 + 975 * data[10]) / 1000);
2365 data[19] = min_t(uint32_t, 127U, (data[18] * tmp_1e3) / 1000);
2366 data[20] = data[18];
2367 data[21] = min_t(uint32_t, 127U, (500 + 975 * data[12]) / 1000);
2368 data[22] = min_t(uint32_t, 127, (data[21] * tmp_1e3) / 1000);
2369 data[23] = data[21];
2370 data[24] = 0x2E;
2371 data[25] = (128 + min_t(uint32_t, 63000U, DIV_ROUND_CLOSEST(63 *
2372 scaled_adc_clk_1e6, 1000)) / 1000);
2373 data[26] = min_t(uint32_t, 63U, 63 * scaled_adc_clk_1e6 / 1000000 *
2374 (920 + 80 * inv_scaled_adc_clk_1e3 / 1000) / 1000);
2375 data[27] = min_t(uint32_t, 63, (32 * sqrt_term_1e3) / 1000);
2376 data[28] = data[25];
2377 data[29] = data[26];
2378 data[30] = data[27];
2379 data[31] = data[25];
2380 data[32] = data[26];
2381 data[33] = min_t(uint32_t, 63U, 63 * sqrt_term_1e3 / 1000);
2382 data[34] = min_t(uint32_t, 127U, 64 * sqrt_term_1e3 / 1000);
2383 data[35] = 0x40;
2384 data[36] = 0x40;
2385 data[37] = 0x2C;
2386 data[38] = 0x00;
2387 data[39] = 0x00;
2388
2389 for (i = 0; i < 40; i++) {
2390 ret = ad9361_spi_write(phy->spi, 0x200 + i, data[i]);
2391 if (ret < 0)
2392 return ret;
2393 }
2394
2395 return 0;
2396 }
2397
2398 /**
2399 * Perform a RX TIA calibration.
2400 * @param phy The AD9361 state structure.
2401 * @param bb_bw_Hz The baseband bandwidth [Hz].
2402 * @return 0 in case of success, negative error code otherwise.
2403 */
2404 static int32_t ad9361_rx_tia_calib(struct ad9361_rf_phy *phy, uint32_t bb_bw_Hz)
2405 {
2406 uint32_t Cbbf, R2346;
2407 uint64_t CTIA_fF;
2408
2409 uint8_t reg1EB = ad9361_spi_read(phy->spi, REG_RX_BBF_C3_MSB);
2410 uint8_t reg1EC = ad9361_spi_read(phy->spi, REG_RX_BBF_C3_LSB);
2411 uint8_t reg1E6 = ad9361_spi_read(phy->spi, REG_RX_BBF_R2346);
2412 uint8_t reg1DB, reg1DF, reg1DD, reg1DC, reg1DE, temp;
2413
2414 dev_dbg(&phy->spi->dev, "%s : bb_bw_Hz %"PRIu32,
2415 __func__, bb_bw_Hz);
2416
2417 bb_bw_Hz = clamp(bb_bw_Hz, 200000UL, 20000000UL);
2418
2419 Cbbf = (reg1EB * 160) + (reg1EC * 10) + 140; /* fF */
2420 R2346 = 18300 * RX_BBF_R2346(reg1E6);
2421
2422 CTIA_fF = Cbbf * R2346 * 560ULL;
2423 do_div(&CTIA_fF, 3500000UL);
2424
2425 if (bb_bw_Hz <= 3000000UL)
2426 reg1DB = 0xE0;
2427 else if (bb_bw_Hz <= 10000000UL)
2428 reg1DB = 0x60;
2429 else
2430 reg1DB = 0x20;
2431
2432 if (CTIA_fF > 2920ULL) {
2433 reg1DC = 0x40;
2434 reg1DE = 0x40;
2435 temp = min(127U, DIV_ROUND_CLOSEST((uint32_t)CTIA_fF - 400, 320U));
2436 reg1DD = temp;
2437 reg1DF = temp;
2438 }
2439 else {
2440 temp = DIV_ROUND_CLOSEST((uint32_t)CTIA_fF - 400, 40U) + 0x40;
2441 reg1DC = temp;
2442 reg1DE = temp;
2443 reg1DD = 0;
2444 reg1DF = 0;
2445 }
2446
2447 ad9361_spi_write(phy->spi, REG_RX_TIA_CONFIG, reg1DB);
2448 ad9361_spi_write(phy->spi, REG_TIA1_C_LSB, reg1DC);
2449 ad9361_spi_write(phy->spi, REG_TIA1_C_MSB, reg1DD);
2450 ad9361_spi_write(phy->spi, REG_TIA2_C_LSB, reg1DE);
2451 ad9361_spi_write(phy->spi, REG_TIA2_C_MSB, reg1DF);
2452
2453 return 0;
2454 }
2455
2456 /**
2457 * Perform a baseband RX analog filter calibration.
2458 * @param phy The AD9361 state structure.
2459 * @param rx_bb_bw The baseband bandwidth [Hz].
2460 * @param bbpll_freq The BBPLL frequency [Hz].
2461 * @return 0 in case of success, negative error code otherwise.
2462 */
2463 static int32_t ad9361_rx_bb_analog_filter_calib(struct ad9361_rf_phy *phy,
2464 uint32_t rx_bb_bw,
2465 uint32_t bbpll_freq)
2466 {
2467 uint32_t target;
2468 uint8_t tmp;
2469 int32_t ret;
2470
2471 dev_dbg(&phy->spi->dev, "%s : rx_bb_bw %"PRIu32" bbpll_freq %"PRIu32,
2472 __func__, rx_bb_bw, bbpll_freq);
2473
2474 rx_bb_bw = clamp(rx_bb_bw, 200000UL, 28000000UL);
2475
2476 /* 1.4 * BBBW * 2PI / ln(2) */
2477 target = 126906UL * (rx_bb_bw / 10000UL);
2478 phy->rxbbf_div = min_t(uint32_t, 511UL, DIV_ROUND_UP(bbpll_freq, target));
2479
2480 /* Set RX baseband filter divide value */
2481 ad9361_spi_write(phy->spi, REG_RX_BBF_TUNE_DIVIDE, phy->rxbbf_div);
2482 ad9361_spi_writef(phy->spi, REG_RX_BBF_TUNE_CONFIG, BIT(0), phy->rxbbf_div >> 8);
2483
2484 /* Write the BBBW into registers 0x1FB and 0x1FC */
2485 ad9361_spi_write(phy->spi, REG_RX_BBBW_MHZ, rx_bb_bw / 1000000UL);
2486
2487 tmp = DIV_ROUND_CLOSEST((rx_bb_bw % 1000000UL) * 128, 1000000UL);
2488 ad9361_spi_write(phy->spi, REG_RX_BBBW_KHZ, min_t(uint8_t, 127, tmp));
2489
2490 ad9361_spi_write(phy->spi, REG_RX_MIX_LO_CM, RX_MIX_LO_CM(0x3F)); /* Set Rx Mix LO CM */
2491 ad9361_spi_write(phy->spi, REG_RX_MIX_GM_CONFIG, RX_MIX_GM_PLOAD(3)); /* Set GM common mode */
2492
2493 /* Enable the RX BBF tune circuit by writing 0x1E2=0x02 and 0x1E3=0x02 */
2494 ad9361_spi_write(phy->spi, REG_RX1_TUNE_CTRL, RX1_TUNE_RESAMPLE);
2495 ad9361_spi_write(phy->spi, REG_RX2_TUNE_CTRL, RX2_TUNE_RESAMPLE);
2496
2497 /* Start the RX Baseband Filter calibration in register 0x016[7] */
2498 /* Calibration is complete when register 0x016[7] self clears */
2499 ret = ad9361_run_calibration(phy, RX_BB_TUNE_CAL);
2500
2501 /* Disable the RX baseband filter tune circuit, write 0x1E2=3, 0x1E3=3 */
2502 ad9361_spi_write(phy->spi, REG_RX1_TUNE_CTRL,
2503 RX1_TUNE_RESAMPLE | RX1_PD_TUNE);
2504 ad9361_spi_write(phy->spi, REG_RX2_TUNE_CTRL,
2505 RX2_TUNE_RESAMPLE | RX2_PD_TUNE);
2506
2507 return ret;
2508 }
2509
2510 /**
2511 * Perform a baseband TX analog filter calibration.
2512 * @param phy The AD9361 state structure.
2513 * @param tx_bb_bw The baseband bandwidth [Hz].
2514 * @param bbpll_freq The BBPLL frequency [Hz].
2515 * @return 0 in case of success, negative error code otherwise.
2516 */
2517 static int32_t ad9361_tx_bb_analog_filter_calib(struct ad9361_rf_phy *phy,
2518 uint32_t tx_bb_bw,
2519 uint32_t bbpll_freq)
2520 {
2521 uint32_t target, txbbf_div;
2522 int32_t ret;
2523
2524 dev_dbg(&phy->spi->dev, "%s : tx_bb_bw %"PRIu32" bbpll_freq %"PRIu32,
2525 __func__, tx_bb_bw, bbpll_freq);
2526
2527 tx_bb_bw = clamp(tx_bb_bw, 625000UL, 20000000UL);
2528
2529 /* 1.6 * BBBW * 2PI / ln(2) */
2530 target = 145036 * (tx_bb_bw / 10000UL);
2531 txbbf_div = min_t(uint32_t, 511UL, DIV_ROUND_UP(bbpll_freq, target));
2532
2533 /* Set TX baseband filter divide value */
2534 ad9361_spi_write(phy->spi, REG_TX_BBF_TUNE_DIVIDER, txbbf_div);
2535 ad9361_spi_writef(phy->spi, REG_TX_BBF_TUNE_MODE,
2536 TX_BBF_TUNE_DIVIDER, txbbf_div >> 8);
2537
2538 /* Enable the TX baseband filter tune circuit by setting 0x0CA=0x22. */
2539 ad9361_spi_write(phy->spi, REG_TX_TUNE_CTRL, TUNER_RESAMPLE | TUNE_CTRL(1));
2540
2541 /* Start the TX Baseband Filter calibration in register 0x016[6] */
2542 /* Calibration is complete when register 0x016[] self clears */
2543 ret = ad9361_run_calibration(phy, TX_BB_TUNE_CAL);
2544
2545 /* Disable the TX baseband filter tune circuit by writing 0x0CA=0x26. */
2546 ad9361_spi_write(phy->spi, REG_TX_TUNE_CTRL,
2547 TUNER_RESAMPLE | TUNE_CTRL(1) | PD_TUNE);
2548
2549 return ret;
2550 }
2551
2552 /**
2553 * Perform a baseband TX secondary filter calibration.
2554 * @param phy The AD9361 state structure.
2555 * @param tx_rf_bw The RF bandwidth [Hz].
2556 * @return 0 in case of success, negative error code otherwise.
2557 */
2558 static int32_t ad9361_tx_bb_second_filter_calib(struct ad9361_rf_phy *phy,
2559 uint32_t tx_bb_bw)
2560 {
2561 uint64_t cap;
2562 uint32_t corner, res, div;
2563 uint32_t reg_conf, reg_res;
2564 int32_t ret, i;
2565
2566 dev_dbg(&phy->spi->dev, "%s : tx_bb_bw %"PRIu32,
2567 __func__, tx_bb_bw);
2568
2569 tx_bb_bw = clamp(tx_bb_bw, 530000UL, 20000000UL);
2570
2571 /* BBBW * 5PI */
2572 corner = 15708 * (tx_bb_bw / 10000UL);
2573
2574 for (i = 0, res = 1; i < 4; i++) {
2575 div = corner * res;
2576 cap = (500000000ULL) + (div >> 1);
2577 do_div(&cap, div);
2578 cap -= 12ULL;
2579 if (cap < 64ULL)
2580 break;
2581
2582 res <<= 1;
2583 }
2584
2585 if (cap > 63ULL)
2586 cap = 63ULL;
2587
2588 if (tx_bb_bw <= 4500000UL)
2589 reg_conf = 0x59;
2590 else if (tx_bb_bw <= 12000000UL)
2591 reg_conf = 0x56;
2592 else
2593 reg_conf = 0x57;
2594
2595 switch (res) {
2596 case 1:
2597 reg_res = 0x0C;
2598 break;
2599 case 2:
2600 reg_res = 0x04;
2601 break;
2602 case 4:
2603 reg_res = 0x03;
2604 break;
2605 case 8:
2606 reg_res = 0x01;
2607 break;
2608 default:
2609 reg_res = 0x01;
2610 }
2611
2612 ret = ad9361_spi_write(phy->spi, REG_CONFIG0, reg_conf);
2613 ret |= ad9361_spi_write(phy->spi, REG_RESISTOR, reg_res);
2614 ret |= ad9361_spi_write(phy->spi, REG_CAPACITOR, (uint8_t)cap);
2615
2616 return ret;
2617 }
2618
2619 /**
2620 * Perform a RF synthesizer charge pump calibration.
2621 * @param phy The AD9361 state structure.
2622 * @param ref_clk_hz The reference clock rate [Hz].
2623 * @param tx The Synthesizer TX = 1, RX = 0.
2624 * @return 0 in case of success, negative error code otherwise.
2625 */
2626 static int32_t ad9361_txrx_synth_cp_calib(struct ad9361_rf_phy *phy,
2627 uint32_t ref_clk_hz, bool tx)
2628 {
2629 uint32_t offs = tx ? 0x40 : 0;
2630 uint32_t vco_cal_cnt;
2631 dev_dbg(&phy->spi->dev, "%s : ref_clk_hz %"PRIu32" : is_tx %d",
2632 __func__, ref_clk_hz, tx);
2633
2634 /* REVIST: */
2635 ad9361_spi_write(phy->spi, REG_RX_CP_LEVEL_DETECT + offs, 0x17);
2636
2637 ad9361_spi_write(phy->spi, REG_RX_DSM_SETUP_1 + offs, 0x0);
2638
2639 ad9361_spi_write(phy->spi, REG_RX_LO_GEN_POWER_MODE + offs, 0x00);
2640 ad9361_spi_write(phy->spi, REG_RX_VCO_LDO + offs, 0x0B);
2641 ad9361_spi_write(phy->spi, REG_RX_VCO_PD_OVERRIDES + offs, 0x02);
2642 ad9361_spi_write(phy->spi, REG_RX_CP_CURRENT + offs, 0x80);
2643 ad9361_spi_write(phy->spi, REG_RX_CP_CONFIG + offs, 0x00);
2644
2645 /* see Table 70 Example Calibration Times for RF VCO Cal */
2646 if (phy->pdata->fdd) {
2647 vco_cal_cnt = VCO_CAL_EN | VCO_CAL_COUNT(3) | FB_CLOCK_ADV(2);
2648 }
2649 else {
2650 if (ref_clk_hz > 40000000UL)
2651 vco_cal_cnt = VCO_CAL_EN | VCO_CAL_COUNT(1) |
2652 FB_CLOCK_ADV(2);
2653 else
2654 vco_cal_cnt = VCO_CAL_EN | VCO_CAL_COUNT(0) |
2655 FB_CLOCK_ADV(2);
2656 }
2657
2658 ad9361_spi_write(phy->spi, REG_RX_VCO_CAL + offs, vco_cal_cnt);
2659
2660 /* Enable FDD mode during calibrations */
2661
2662 if (!phy->pdata->fdd) {
2663 ad9361_spi_writef(phy->spi, REG_PARALLEL_PORT_CONF_3,
2664 HALF_DUPLEX_MODE, 0);
2665 }
2666
2667 ad9361_spi_write(phy->spi, REG_ENSM_CONFIG_2, DUAL_SYNTH_MODE);
2668 ad9361_spi_write(phy->spi, REG_ENSM_CONFIG_1,
2669 FORCE_ALERT_STATE |
2670 TO_ALERT);
2671 ad9361_spi_write(phy->spi, REG_ENSM_MODE, FDD_MODE);
2672
2673 ad9361_spi_write(phy->spi, REG_RX_CP_CONFIG + offs, CP_CAL_ENABLE);
2674
2675 return ad9361_check_cal_done(phy, REG_RX_CAL_STATUS + offs,
2676 CP_CAL_VALID, 1);
2677 }
2678
2679 /**
2680 * Perform a baseband DC offset calibration.
2681 * @param phy The AD9361 state structure.
2682 * @return 0 in case of success, negative error code otherwise.
2683 */
2684 static int32_t ad9361_bb_dc_offset_calib(struct ad9361_rf_phy *phy)
2685 {
2686 dev_dbg(&phy->spi->dev, "%s", __func__);
2687
2688 ad9361_spi_write(phy->spi, REG_BB_DC_OFFSET_COUNT, 0x3F);
2689 ad9361_spi_write(phy->spi, REG_BB_DC_OFFSET_SHIFT, BB_DC_M_SHIFT(0xF));
2690 ad9361_spi_write(phy->spi, REG_BB_DC_OFFSET_ATTEN, BB_DC_OFFSET_ATTEN(1));
2691
2692 return ad9361_run_calibration(phy, BBDC_CAL);
2693 }
2694
2695 /**
2696 * Perform a RF DC offset calibration.
2697 * @param phy The AD9361 state structure.
2698 * @param ref_clk_hz The RX LO frequency [Hz].
2699 * @return 0 in case of success, negative error code otherwise.
2700 */
2701 static int32_t ad9361_rf_dc_offset_calib(struct ad9361_rf_phy *phy,
2702 uint64_t rx_freq)
2703 {
2704 struct spi_device *spi = phy->spi;
2705
2706 dev_dbg(&phy->spi->dev, "%s : rx_freq %"PRIu64,
2707 __func__, rx_freq);
2708
2709 ad9361_spi_write(spi, REG_WAIT_COUNT, 0x20);
2710
2711 if (rx_freq <= 4000000000ULL) {
2712 ad9361_spi_write(spi, REG_RF_DC_OFFSET_COUNT,
2713 phy->pdata->rf_dc_offset_count_low);
2714 ad9361_spi_write(spi, REG_RF_DC_OFFSET_CONFIG_1,
2715 RF_DC_CALIBRATION_COUNT(4) | DAC_FS(2));
2716 ad9361_spi_write(spi, REG_RF_DC_OFFSET_ATTEN,
2717 RF_DC_OFFSET_ATTEN(
2718 phy->pdata->dc_offset_attenuation_low));
2719 }
2720 else {
2721 ad9361_spi_write(spi, REG_RF_DC_OFFSET_COUNT,
2722 phy->pdata->rf_dc_offset_count_high);
2723 ad9361_spi_write(spi, REG_RF_DC_OFFSET_CONFIG_1,
2724 RF_DC_CALIBRATION_COUNT(4) | DAC_FS(3));
2725 ad9361_spi_write(spi, REG_RF_DC_OFFSET_ATTEN,
2726 RF_DC_OFFSET_ATTEN(
2727 phy->pdata->dc_offset_attenuation_high));
2728 }
2729
2730 ad9361_spi_write(spi, REG_DC_OFFSET_CONFIG2,
2731 USE_WAIT_COUNTER_FOR_RF_DC_INIT_CAL |
2732 DC_OFFSET_UPDATE(3));
2733
2734 if (phy->pdata->rx1rx2_phase_inversion_en ||
2735 (phy->pdata->port_ctrl.pp_conf[1] & INVERT_RX2)) {
2736 ad9361_spi_write(spi, REG_INVERT_BITS,
2737 INVERT_RX1_RF_DC_CGOUT_WORD);
2738 } else {
2739 ad9361_spi_write(spi, REG_INVERT_BITS,
2740 INVERT_RX1_RF_DC_CGOUT_WORD |
2741 INVERT_RX2_RF_DC_CGOUT_WORD);
2742 }
2743
2744 return ad9361_run_calibration(phy, RFDC_CAL);
2745 }
2746
2747 /**
2748 * Update RF bandwidth.
2749 * @param phy The AD9361 state structure.
2750 * @param rf_rx_bw RF RX bandwidth [Hz].
2751 * @param rf_tx_bw RF TX bandwidth [Hz].
2752 * @return 0 in case of success, negative error code otherwise.
2753 */
2754 static int32_t __ad9361_update_rf_bandwidth(struct ad9361_rf_phy *phy,
2755 uint32_t rf_rx_bw, uint32_t rf_tx_bw)
2756 {
2757 uint32_t real_rx_bandwidth = rf_rx_bw / 2;
2758 uint32_t real_tx_bandwidth = rf_tx_bw / 2;
2759 uint32_t bbpll_freq;
2760 int32_t ret;
2761
2762 dev_dbg(&phy->spi->dev, "%s: %"PRIu32" %"PRIu32,
2763 __func__, rf_rx_bw, rf_tx_bw);
2764
2765 bbpll_freq = clk_get_rate(phy, phy->ref_clk_scale[BBPLL_CLK]);
2766
2767 ret = ad9361_rx_bb_analog_filter_calib(phy,
2768 real_rx_bandwidth,
2769 bbpll_freq);
2770 if (ret < 0)
2771 return ret;
2772
2773 ret = ad9361_tx_bb_analog_filter_calib(phy,
2774 real_tx_bandwidth,
2775 bbpll_freq);
2776 if (ret < 0)
2777 return ret;
2778
2779 ret = ad9361_rx_tia_calib(phy, real_rx_bandwidth);
2780 if (ret < 0)
2781 return ret;
2782
2783 ret = ad9361_tx_bb_second_filter_calib(phy, real_tx_bandwidth);
2784 if (ret < 0)
2785 return ret;
2786
2787 ret = ad9361_rx_adc_setup(phy,
2788 bbpll_freq,
2789 clk_get_rate(phy, phy->ref_clk_scale[ADC_CLK]));
2790 if (ret < 0)
2791 return ret;
2792
2793 return 0;
2794 }
2795
2796 /**
2797 * TX Quad Calib.
2798 * @param phy The AD9361 state structure.
2799 * @param phase phase
2800 * @param rxnco_word Rx NCO word.
2801 * @param decim decim
2802 * @param res res
2803 * @return 0 in case of success, negative error code otherwise.
2804 */
2805 static int32_t __ad9361_tx_quad_calib(struct ad9361_rf_phy *phy, uint32_t phase,
2806 uint32_t rxnco_word, uint32_t decim, uint8_t *res)
2807 {
2808 int32_t ret;
2809
2810 ad9361_spi_write(phy->spi, REG_QUAD_CAL_NCO_FREQ_PHASE_OFFSET,
2811 RX_NCO_FREQ(rxnco_word) | RX_NCO_PHASE_OFFSET(phase));
2812 ad9361_spi_write(phy->spi, REG_QUAD_CAL_CTRL,
2813 SETTLE_MAIN_ENABLE | DC_OFFSET_ENABLE | QUAD_CAL_SOFT_RESET |
2814 GAIN_ENABLE | PHASE_ENABLE | M_DECIM(decim));
2815 ad9361_spi_write(phy->spi, REG_QUAD_CAL_CTRL,
2816 SETTLE_MAIN_ENABLE | DC_OFFSET_ENABLE |
2817 GAIN_ENABLE | PHASE_ENABLE | M_DECIM(decim));
2818
2819 ret = ad9361_run_calibration(phy, TX_QUAD_CAL);
2820 if (ret < 0)
2821 return ret;
2822
2823 if (res)
2824 *res = ad9361_spi_read(phy->spi,
2825 (phy->pdata->rx1tx1_mode_use_tx_num == 2) ?
2826 REG_QUAD_CAL_STATUS_TX2 : REG_QUAD_CAL_STATUS_TX1) &
2827 (TX1_LO_CONV | TX1_SSB_CONV);
2828
2829 return 0;
2830 }
2831
2832 /**
2833 * Loop through all possible phase offsets in case the QUAD CAL doesn't converge.
2834 * @param phy The AD9361 state structure.
2835 * @param rxnco_word Rx NCO word.
2836 * @return 0 in case of success, negative error code otherwise.
2837 */
2838 static int32_t ad9361_tx_quad_phase_search(struct ad9361_rf_phy *phy, uint32_t rxnco_word, uint8_t decim)
2839 {
2840 int32_t i, ret;
2841 uint8_t field[64], val;
2842 uint32_t start;
2843
2844 dev_dbg(&phy->spi->dev, "%s", __func__);
2845
2846 for (i = 0; i < (int64_t)(ARRAY_SIZE(field) / 2); i++) {
2847 ret = __ad9361_tx_quad_calib(phy, i, rxnco_word, decim, &val);
2848 if (ret < 0)
2849 return ret;
2850
2851 /* Handle 360/0 wrap around */
2852 field[i] = field[i + 32] = !((val & TX1_LO_CONV) && (val & TX1_SSB_CONV));
2853 }
2854
2855 ret = ad9361_find_opt(field, ARRAY_SIZE(field), &start);
2856
2857 phy->last_tx_quad_cal_phase = (start + ret / 2) & 0x1F;
2858
2859 #ifdef _DEBUG
2860 for (i = 0; i < 64; i++) {
2861 printk("%c", (field[i] ? '#' : 'o'));
2862 }
2863 #ifdef WIN32
2864 printk(" RX_NCO_PHASE_OFFSET(%d, 0x%X) \n", phy->last_tx_quad_cal_phase,
2865 phy->last_tx_quad_cal_phase);
2866 #else
2867 printk(" RX_NCO_PHASE_OFFSET(%"PRIu32", 0x%"PRIX32") \n", phy->last_tx_quad_cal_phase,
2868 phy->last_tx_quad_cal_phase);
2869 #endif
2870 #endif
2871
2872 ret = __ad9361_tx_quad_calib(phy, phy->last_tx_quad_cal_phase, rxnco_word, decim, NULL);
2873 if (ret < 0)
2874 return ret;
2875
2876 return 0;
2877 }
2878
2879 /**
2880 * Perform a TX quadrature calibration.
2881 * @param phy The AD9361 state structure.
2882 * @param bw The bandwidth [Hz].
2883 * @param rx_phase The optional RX phase value overwrite (set to zero).
2884 * @return 0 in case of success, negative error code otherwise.
2885 */
2886 static int32_t ad9361_tx_quad_calib(struct ad9361_rf_phy *phy,
2887 uint32_t bw_rx, uint32_t bw_tx,
2888 int32_t rx_phase)
2889 {
2890 struct spi_device *spi = phy->spi;
2891 uint32_t clktf, clkrf;
2892 int32_t txnco_word, rxnco_word, txnco_freq, ret;
2893 uint8_t __rx_phase = 0, reg_inv_bits = 0, val, decim;
2894 const uint8_t(*tab)[3];
2895 uint32_t index_max, i, lpf_tia_mask;
2896
2897 /*
2898 * Find NCO frequency that matches this equation:
2899 * BW / 4 = Rx NCO freq = Tx NCO freq:
2900 * Rx NCO = ClkRF * (rxNCO <1:0> + 1) / 32
2901 * Tx NCO = ClkTF * (txNCO <1:0> + 1) / 32
2902 */
2903
2904 clkrf = clk_get_rate(phy, phy->ref_clk_scale[CLKRF_CLK]);
2905 clktf = clk_get_rate(phy, phy->ref_clk_scale[CLKTF_CLK]);
2906
2907 dev_dbg(&phy->spi->dev, "%s : bw_tx %"PRIu32" clkrf %"PRIu32" clktf %"PRIu32,
2908 __func__, bw_tx, clkrf, clktf);
2909
2910 txnco_word = DIV_ROUND_CLOSEST(bw_tx * 8, clktf) - 1;
2911 txnco_word = clamp_t(int, txnco_word, 0, 3);
2912 rxnco_word = txnco_word;
2913
2914 dev_dbg(dev, "Tx NCO frequency: %"PRIu32" (BW/4: %"PRIu32") txnco_word %"PRId32,
2915 clktf * (txnco_word + 1) / 32, bw_tx / 4, txnco_word);
2916
2917 if (clktf <= 4000000UL)
2918 decim = 2;
2919 else
2920 decim = 3;
2921
2922 if (clkrf == (2 * clktf)) {
2923 __rx_phase = 0x0E;
2924 switch (txnco_word) {
2925 case 0:
2926 txnco_word++;
2927 break;
2928 case 1:
2929 rxnco_word--;
2930 break;
2931 case 2:
2932 rxnco_word -= 2;
2933 txnco_word--;
2934 break;
2935 case 3:
2936 rxnco_word -= 2; /* REVISIT */
2937 __rx_phase = 0x08;
2938 break;
2939 }
2940 }
2941 else if (clkrf == clktf) {
2942 switch (txnco_word) {
2943 case 0:
2944 case 3:
2945 __rx_phase = 0x15;
2946 break;
2947 case 2:
2948 __rx_phase = 0x1F;
2949 break;
2950 case 1:
2951 if (ad9361_spi_readf(spi,
2952 REG_TX_ENABLE_FILTER_CTRL, 0x3F) == 0x22)
2953 __rx_phase = 0x15; /* REVISIT */
2954 else
2955 __rx_phase = 0x1A;
2956 break;
2957 }
2958 }
2959 else
2960 dev_err(dev, "Unhandled case in %s line %d clkrf %"PRIu32" clktf %"PRIu32,
2961 __func__, __LINE__, clkrf, clktf);
2962
2963
2964 if (rx_phase >= 0)
2965 __rx_phase = rx_phase;
2966
2967 txnco_freq = clktf * (txnco_word + 1) / 32;
2968
2969 if (txnco_freq > (int64_t)(bw_rx / 4) || txnco_freq > (int64_t)(bw_tx / 4)) {
2970 /* Make sure the BW during calibration is wide enough */
2971 ret = __ad9361_update_rf_bandwidth(phy, txnco_freq * 8, txnco_freq * 8);
2972 if (ret < 0)
2973 return ret;
2974 }
2975
2976 if (phy->pdata->rx1rx2_phase_inversion_en ||
2977 (phy->pdata->port_ctrl.pp_conf[1] & INVERT_RX2)) {
2978
2979 ad9361_spi_writef(spi, REG_PARALLEL_PORT_CONF_2, INVERT_RX2, 0);
2980
2981 reg_inv_bits = ad9361_spi_read(spi, REG_INVERT_BITS);
2982
2983 ad9361_spi_write(spi, REG_INVERT_BITS,
2984 INVERT_RX1_RF_DC_CGOUT_WORD |
2985 INVERT_RX2_RF_DC_CGOUT_WORD);
2986 }
2987
2988 ad9361_spi_writef(spi, REG_KEXP_2, TX_NCO_FREQ(~0), txnco_word);
2989 ad9361_spi_write(spi, REG_QUAD_CAL_COUNT, 0xFF);
2990 ad9361_spi_write(spi, REG_KEXP_1, KEXP_TX(1) | KEXP_TX_COMP(3) |
2991 KEXP_DC_I(3) | KEXP_DC_Q(3));
2992 ad9361_spi_write(spi, REG_MAG_FTEST_THRESH, 0x01);
2993 ad9361_spi_write(spi, REG_MAG_FTEST_THRESH_2, 0x01);
2994
2995 if (has_split_gt && phy->pdata->split_gt) {
2996 tab = &split_gain_table[phy->current_table][0];
2997 index_max = SIZE_SPLIT_TABLE;
2998 lpf_tia_mask = 0x20;
2999 }
3000 else {
3001 tab = &full_gain_table[phy->current_table][0];
3002 index_max = SIZE_FULL_TABLE;
3003 lpf_tia_mask = 0x3F;
3004 }
3005
3006 for (i = 0; i < index_max; i++)
3007 if ((tab[i][1] & lpf_tia_mask) == 0x20) {
3008 ad9361_spi_write(spi, REG_TX_QUAD_FULL_LMT_GAIN, i);
3009 break;
3010 }
3011
3012 if (i >= index_max)
3013 dev_err(dev, "failed to find suitable LPF TIA value in gain table");
3014
3015 ad9361_spi_write(spi, REG_QUAD_SETTLE_COUNT, 0xF0);
3016 ad9361_spi_write(spi, REG_TX_QUAD_LPF_GAIN, 0x00);
3017
3018 ret = __ad9361_tx_quad_calib(phy, __rx_phase, rxnco_word, decim, &val);
3019
3020 dev_dbg(dev, "LO leakage: %d Quadrature Calibration: %d : rx_phase %d",
3021 !!(val & TX1_LO_CONV), !!(val & TX1_SSB_CONV), __rx_phase);
3022
3023 /* Calibration failed -> try last phase offset */
3024 if (val != (TX1_LO_CONV | TX1_SSB_CONV)) {
3025 if (phy->last_tx_quad_cal_phase < 31)
3026 ret = __ad9361_tx_quad_calib(phy, phy->last_tx_quad_cal_phase,
3027 rxnco_word, decim, &val);
3028 } else {
3029 phy->last_tx_quad_cal_phase = __rx_phase;
3030 }
3031
3032 /* Calibration failed -> loop through all 32 phase offsets */
3033 if (val != (TX1_LO_CONV | TX1_SSB_CONV))
3034 ret = ad9361_tx_quad_phase_search(phy, rxnco_word, decim);
3035
3036 if (phy->pdata->rx1rx2_phase_inversion_en ||
3037 (phy->pdata->port_ctrl.pp_conf[1] & INVERT_RX2)) {
3038 ad9361_spi_writef(spi, REG_PARALLEL_PORT_CONF_2, INVERT_RX2, 1);
3039 ad9361_spi_write(spi, REG_INVERT_BITS, reg_inv_bits);
3040 }
3041
3042 if (phy->pdata->rx1rx2_phase_inversion_en ||
3043 (phy->pdata->port_ctrl.pp_conf[1] & INVERT_RX2)) {
3044 ad9361_spi_writef(spi, REG_PARALLEL_PORT_CONF_2, INVERT_RX2, 1);
3045 ad9361_spi_write(spi, REG_INVERT_BITS, reg_inv_bits);
3046 }
3047
3048 if (txnco_freq > (int64_t)(bw_rx / 4) || txnco_freq > (int64_t)(bw_tx / 4)) {
3049 __ad9361_update_rf_bandwidth(phy,
3050 phy->current_rx_bw_Hz,
3051 phy->current_tx_bw_Hz);
3052 }
3053
3054 return ret;
3055 }
3056
3057 /**
3058 * Setup RX tracking calibrations.
3059 * @param phy The AD9361 state structure.
3060 * @param bbdc_track Set true, will enable the BBDC tracking.
3061 * @param rfdc_track Set true, will enable the RFDC tracking.
3062 * @param rxquad_track Set true, will enable the RXQUAD tracking.
3063 * @return 0 in case of success, negative error code otherwise.
3064 */
3065 int32_t ad9361_tracking_control(struct ad9361_rf_phy *phy, bool bbdc_track,
3066 bool rfdc_track, bool rxquad_track)
3067 {
3068 struct spi_device *spi = phy->spi;
3069 uint32_t qtrack = 0;
3070
3071 dev_dbg(&spi->dev, "%s : bbdc_track=%d, rfdc_track=%d, rxquad_track=%d",
3072 __func__, bbdc_track, rfdc_track, rxquad_track);
3073
3074 ad9361_spi_write(spi, REG_CALIBRATION_CONFIG_2,
3075 CALIBRATION_CONFIG2_DFLT | K_EXP_PHASE(0x15));
3076 ad9361_spi_write(spi, REG_CALIBRATION_CONFIG_3,
3077 PREVENT_POS_LOOP_GAIN | K_EXP_AMPLITUDE(0x15));
3078
3079 ad9361_spi_write(spi, REG_DC_OFFSET_CONFIG2,
3080 USE_WAIT_COUNTER_FOR_RF_DC_INIT_CAL |
3081 DC_OFFSET_UPDATE(phy->pdata->dc_offset_update_events) |
3082 (bbdc_track ? ENABLE_BB_DC_OFFSET_TRACKING : 0) |
3083 (rfdc_track ? ENABLE_RF_OFFSET_TRACKING : 0));
3084
3085 ad9361_spi_writef(spi, REG_RX_QUAD_GAIN2,
3086 CORRECTION_WORD_DECIMATION_M(~0),
3087 phy->pdata->qec_tracking_slow_mode_en ? 4 : 0);
3088
3089 if (rxquad_track) {
3090 if (phy->pdata->rx2tx2)
3091 qtrack = ENABLE_TRACKING_MODE_CH1 | ENABLE_TRACKING_MODE_CH2;
3092 else
3093 qtrack = (phy->pdata->rx1tx1_mode_use_rx_num == 1) ?
3094 ENABLE_TRACKING_MODE_CH1 : ENABLE_TRACKING_MODE_CH2;
3095 }
3096
3097 ad9361_spi_write(spi, REG_CALIBRATION_CONFIG_1,
3098 ENABLE_PHASE_CORR | ENABLE_GAIN_CORR |
3099 FREE_RUN_MODE | ENABLE_CORR_WORD_DECIMATION |
3100 qtrack);
3101
3102 return 0;
3103 }
3104
3105 /**
3106 * Enable/disable the VCO cal.
3107 * @param phy The AD9361 state structure.
3108 * @param rx Set true for rx.
3109 * @param enable Set true to enable.
3110 * @return 0 in case of success, negative error code otherwise.
3111 */
3112 static int32_t ad9361_trx_vco_cal_control(struct ad9361_rf_phy *phy,
3113 bool tx, bool enable)
3114 {
3115 dev_dbg(&phy->spi->dev, "%s : state %d",
3116 __func__, enable);
3117
3118 return ad9361_spi_writef(phy->spi,
3119 tx ? REG_TX_PFD_CONFIG : REG_RX_PFD_CONFIG,
3120 BYPASS_LD_SYNTH, !enable);
3121 }
3122
3123 /**
3124 * Enable/disable the ext. LO.
3125 * @param phy The AD9361 state structure.
3126 * @param rx Set true for rx.
3127 * @param enable Set true to enable.
3128 * @return 0 in case of success, negative error code otherwise.
3129 */
3130 static int32_t ad9361_trx_ext_lo_control(struct ad9361_rf_phy *phy,
3131 bool tx, bool enable)
3132 {
3133 int32_t val = enable ? ~0 : 0;
3134 int32_t ret;
3135
3136 /* REVIST:
3137 * POWER_DOWN_TRX_SYNTH and MCS_RF_ENABLE somehow conflict
3138 */
3139
3140 bool mcs_rf_enable = ad9361_spi_readf(phy->spi,
3141 REG_MULTICHIP_SYNC_AND_TX_MON_CTRL, MCS_RF_ENABLE);
3142
3143 dev_dbg(&phy->spi->dev, "%s : %s state %d",
3144 __func__, tx ? "TX" : "RX", enable);
3145
3146 if (tx) {
3147 ret = ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_2,
3148 POWER_DOWN_TX_SYNTH, mcs_rf_enable ? 0 : enable);
3149
3150 ret |= ad9361_spi_writef(phy->spi, REG_RFPLL_DIVIDERS,
3151 TX_VCO_DIVIDER(~0), enable ? 7 :
3152 phy->cached_tx_rfpll_div);
3153
3154 ret |= ad9361_spi_write(phy->spi, REG_TX_SYNTH_POWER_DOWN_OVERRIDE,
3155 enable ? TX_SYNTH_VCO_ALC_POWER_DOWN |
3156 TX_SYNTH_PTAT_POWER_DOWN |
3157 TX_SYNTH_VCO_POWER_DOWN : 0);
3158
3159 ret |= ad9361_spi_writef(phy->spi, REG_ANALOG_POWER_DOWN_OVERRIDE,
3160 TX_EXT_VCO_BUFFER_POWER_DOWN, !enable);
3161
3162 ret |= ad9361_spi_write(phy->spi, REG_TX_LO_GEN_POWER_MODE,
3163 TX_LO_GEN_POWER_MODE(val));
3164 }
3165 else {
3166 ret = ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_2,
3167 POWER_DOWN_RX_SYNTH, mcs_rf_enable ? 0 : enable);
3168
3169 ret |= ad9361_spi_writef(phy->spi, REG_RFPLL_DIVIDERS,
3170 RX_VCO_DIVIDER(~0), enable ? 7 :
3171 phy->cached_rx_rfpll_div);
3172
3173 ret |= ad9361_spi_write(phy->spi, REG_RX_SYNTH_POWER_DOWN_OVERRIDE,
3174 enable ? RX_SYNTH_VCO_ALC_POWER_DOWN |
3175 RX_SYNTH_PTAT_POWER_DOWN |
3176 RX_SYNTH_VCO_POWER_DOWN : 0);
3177
3178 ret |= ad9361_spi_writef(phy->spi, REG_ANALOG_POWER_DOWN_OVERRIDE,
3179 RX_EXT_VCO_BUFFER_POWER_DOWN, !enable);
3180
3181 ret |= ad9361_spi_write(phy->spi, REG_RX_LO_GEN_POWER_MODE,
3182 RX_LO_GEN_POWER_MODE(val));
3183 }
3184
3185 return ret;
3186 }
3187
3188 /**
3189 * Setup the reference clock delay unit counter register.
3190 * @param phy The AD9361 state structure.
3191 * @param ref_clk_hz The reference clock frequency [Hz].
3192 * @return 0 in case of success, negative error code otherwise.
3193 */
3194 static int32_t ad9361_set_ref_clk_cycles(struct ad9361_rf_phy *phy,
3195 uint32_t ref_clk_hz)
3196 {
3197 dev_dbg(&phy->spi->dev, "%s : ref_clk_hz %"PRIu32,
3198 __func__, ref_clk_hz);
3199
3200 return ad9361_spi_write(phy->spi, REG_REFERENCE_CLOCK_CYCLES,
3201 REFERENCE_CLOCK_CYCLES_PER_US((ref_clk_hz / 1000000UL) - 1));
3202 }
3203
3204 /**
3205 * Setup the DCXO tune.
3206 * @param phy The AD9361 state structure.
3207 * @param coarse The DCXO tune coarse.
3208 * @param fine The DCXO tune fine.
3209 * @return 0 in case of success, negative error code otherwise.
3210 */
3211 int32_t ad9361_set_dcxo_tune(struct ad9361_rf_phy *phy,
3212 uint32_t coarse, uint32_t fine)
3213 {
3214 dev_dbg(&phy->spi->dev, "%s : coarse %"PRIu32" fine %"PRIu32,
3215 __func__, coarse, fine);
3216
3217 ad9361_spi_write(phy->spi, REG_DCXO_COARSE_TUNE,
3218 DCXO_TUNE_COARSE(coarse));
3219 ad9361_spi_write(phy->spi, REG_DCXO_FINE_TUNE_LOW,
3220 DCXO_TUNE_FINE_LOW(fine));
3221 return ad9361_spi_write(phy->spi, REG_DCXO_FINE_TUNE_HIGH,
3222 DCXO_TUNE_FINE_HIGH(fine));
3223 }
3224
3225 /**
3226 * Setup TXMON.
3227 * @param phy The AD9361 state structure.
3228 * @param ctrl The TXMON settings.
3229 * @return 0 in case of success, negative error code otherwise.
3230 */
3231 static int32_t ad9361_txmon_setup(struct ad9361_rf_phy *phy,
3232 struct tx_monitor_control *ctrl)
3233 {
3234 struct spi_device *spi = phy->spi;
3235
3236 dev_dbg(&phy->spi->dev, "%s", __func__);
3237
3238 ad9361_spi_write(spi, REG_TPM_MODE_ENABLE,
3239 (ctrl->one_shot_mode_en ? ONE_SHOT_MODE : 0) |
3240 TX_MON_DURATION(ilog2(ctrl->tx_mon_duration / 16)));
3241
3242 ad9361_spi_write(spi, REG_TX_MON_DELAY, ctrl->tx_mon_delay);
3243
3244 ad9361_spi_write(spi, REG_TX_MON_1_CONFIG,
3245 TX_MON_1_LO_CM(ctrl->tx1_mon_lo_cm) |
3246 TX_MON_1_GAIN(ctrl->tx1_mon_front_end_gain));
3247 ad9361_spi_write(spi, REG_TX_MON_2_CONFIG,
3248 TX_MON_2_LO_CM(ctrl->tx2_mon_lo_cm) |
3249 TX_MON_2_GAIN(ctrl->tx2_mon_front_end_gain));
3250
3251 ad9361_spi_write(spi, REG_TX_ATTEN_THRESH,
3252 ctrl->low_high_gain_threshold_mdB / 250);
3253
3254 ad9361_spi_write(spi, REG_TX_MON_HIGH_GAIN,
3255 TX_MON_HIGH_GAIN(ctrl->high_gain_dB));
3256
3257 ad9361_spi_write(spi, REG_TX_MON_LOW_GAIN,
3258 (ctrl->tx_mon_track_en ? TX_MON_TRACK : 0) |
3259 TX_MON_LOW_GAIN(ctrl->low_gain_dB));
3260
3261 return 0;
3262 }
3263
3264 /**
3265 * Enable TXMON.
3266 * @param phy The AD9361 state structure.
3267 * @param en_mask The enable mask.
3268 * @return 0 in case of success, negative error code otherwise.
3269 */
3270 static int32_t ad9361_txmon_control(struct ad9361_rf_phy *phy,
3271 int32_t en_mask)
3272 {
3273 dev_dbg(&phy->spi->dev, "%s: mask 0x%"PRIx32, __func__, en_mask);
3274
3275 #if 0
3276 if (!phy->pdata->fdd && en_mask) {
3277 ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_1,
3278 ENABLE_RX_DATA_PORT_FOR_CAL, 1);
3279 phy->txmon_tdd_en = true;
3280 } else {
3281 ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_1,
3282 ENABLE_RX_DATA_PORT_FOR_CAL, 0);
3283 phy->txmon_tdd_en = false;
3284 }
3285 #endif
3286
3287 ad9361_spi_writef(phy->spi, REG_ANALOG_POWER_DOWN_OVERRIDE,
3288 TX_MONITOR_POWER_DOWN(~0), ~en_mask);
3289
3290 ad9361_spi_writef(phy->spi, REG_TPM_MODE_ENABLE,
3291 TX1_MON_ENABLE, !!(en_mask & TX_1));
3292
3293 return ad9361_spi_writef(phy->spi, REG_TPM_MODE_ENABLE,
3294 TX2_MON_ENABLE, !!(en_mask & TX_2));
3295 }
3296
3297 /**
3298 * Setup the RF port.
3299 * Note:
3300 * val
3301 * 0 (RX1A_N & RX1A_P) and (RX2A_N & RX2A_P) enabled; balanced
3302 * 1 (RX1B_N & RX1B_P) and (RX2B_N & RX2B_P) enabled; balanced
3303 * 2 (RX1C_N & RX1C_P) and (RX2C_N & RX2C_P) enabled; balanced
3304 *
3305 * 3 RX1A_N and RX2A_N enabled; unbalanced
3306 * 4 RX1A_P and RX2A_P enabled; unbalanced
3307 * 5 RX1B_N and RX2B_N enabled; unbalanced
3308 * 6 RX1B_P and RX2B_P enabled; unbalanced
3309 * 7 RX1C_N and RX2C_N enabled; unbalanced
3310 * 8 RX1C_P and RX2C_P enabled; unbalanced
3311 * 9 TX_MON1
3312 * 10 TX_MON2
3313 * 11 TX_MON1 & TX_MON2
3314 * @param phy The AD9361 state structure.
3315 * @param rx_inputs RX input option identifier
3316 * @param txb TX output option identifier
3317 * @return 0 in case of success, negative error code otherwise.
3318 */
3319 int32_t ad9361_rf_port_setup(struct ad9361_rf_phy *phy, bool is_out,
3320 uint32_t rx_inputs, uint32_t txb)
3321 {
3322 uint32_t val;
3323
3324 if (rx_inputs > 11)
3325 return -EINVAL;
3326
3327 if (!is_out) {
3328 if (rx_inputs > 8)
3329 return ad9361_txmon_control(phy, rx_inputs & (TX_1 | TX_2));
3330 else
3331 ad9361_txmon_control(phy, 0);
3332 }
3333
3334 if (rx_inputs < 3)
3335 val = 3 << (rx_inputs * 2);
3336 else
3337 val = 1 << (rx_inputs - 3);
3338
3339 if (txb)
3340 val |= TX_OUTPUT; /* Select TX1B, TX2B */
3341
3342 dev_dbg(&phy->spi->dev, "%s : INPUT_SELECT 0x%"PRIx32,
3343 __func__, val);
3344
3345 return ad9361_spi_write(phy->spi, REG_INPUT_SELECT, val);
3346 }
3347
3348 /**
3349 * Setup the Parallel Port (Digital Data Interface).
3350 * @param phy The AD9361 state structure.
3351 * @param restore_c3 Set true, will restore the Parallel Port Configuration 3
3352 * register.
3353 * @return 0 in case of success, negative error code otherwise.
3354 */
3355 static int32_t ad9361_pp_port_setup(struct ad9361_rf_phy *phy, bool restore_c3)
3356 {
3357 struct spi_device *spi = phy->spi;
3358 struct ad9361_phy_platform_data *pd = phy->pdata;
3359
3360 dev_dbg(&phy->spi->dev, "%s", __func__);
3361
3362 if (restore_c3) {
3363 return ad9361_spi_write(spi, REG_PARALLEL_PORT_CONF_3,
3364 pd->port_ctrl.pp_conf[2]);
3365 }
3366
3367 /* Sanity check */
3368 if (pd->port_ctrl.pp_conf[2] & LVDS_MODE)
3369 pd->port_ctrl.pp_conf[2] &=
3370 ~(HALF_DUPLEX_MODE | SINGLE_DATA_RATE | SINGLE_PORT_MODE);
3371
3372 if (pd->port_ctrl.pp_conf[2] & FULL_PORT)
3373 pd->port_ctrl.pp_conf[2] &= ~(HALF_DUPLEX_MODE | SINGLE_PORT_MODE);
3374
3375 ad9361_spi_write(spi, REG_PARALLEL_PORT_CONF_1, pd->port_ctrl.pp_conf[0]);
3376 ad9361_spi_write(spi, REG_PARALLEL_PORT_CONF_2, pd->port_ctrl.pp_conf[1]);
3377 ad9361_spi_write(spi, REG_PARALLEL_PORT_CONF_3, pd->port_ctrl.pp_conf[2]);
3378 ad9361_spi_write(spi, REG_RX_CLOCK_DATA_DELAY, pd->port_ctrl.rx_clk_data_delay);
3379 ad9361_spi_write(spi, REG_TX_CLOCK_DATA_DELAY, pd->port_ctrl.tx_clk_data_delay);
3380
3381 ad9361_spi_write(spi, REG_LVDS_BIAS_CTRL, pd->port_ctrl.lvds_bias_ctrl);
3382 // ad9361_spi_write(spi, REG_DIGITAL_IO_CTRL, pd->port_ctrl.digital_io_ctrl);
3383 ad9361_spi_write(spi, REG_LVDS_INVERT_CTRL1, pd->port_ctrl.lvds_invert[0]);
3384 ad9361_spi_write(spi, REG_LVDS_INVERT_CTRL2, pd->port_ctrl.lvds_invert[1]);
3385
3386 if (pd->rx1rx2_phase_inversion_en ||
3387 (pd->port_ctrl.pp_conf[1] & INVERT_RX2)) {
3388
3389 ad9361_spi_writef(spi, REG_PARALLEL_PORT_CONF_2, INVERT_RX2, 1);
3390 ad9361_spi_writef(spi, REG_INVERT_BITS,
3391 INVERT_RX2_RF_DC_CGOUT_WORD, 0);
3392 }
3393
3394 return 0;
3395 }
3396
3397 /**
3398 * Setup the Gain Control Blocks (common function for MGC, AGC modes)
3399 * @param phy The AD9361 state structure.
3400 * @param ctrl The gain control settings.
3401 * @return 0 in case of success, negative error code otherwise.
3402 */
3403 static int32_t ad9361_gc_setup(struct ad9361_rf_phy *phy, struct gain_control *ctrl)
3404 {
3405 struct spi_device *spi = phy->spi;
3406 uint32_t reg, tmp1, tmp2;
3407
3408 dev_dbg(&phy->spi->dev, "%s", __func__);
3409
3410 reg = DEC_PWR_FOR_GAIN_LOCK_EXIT | DEC_PWR_FOR_LOCK_LEVEL |
3411 DEC_PWR_FOR_LOW_PWR;
3412
3413 if (ctrl->rx1_mode == RF_GAIN_HYBRID_AGC ||
3414 ctrl->rx2_mode == RF_GAIN_HYBRID_AGC)
3415 reg |= SLOW_ATTACK_HYBRID_MODE;
3416
3417 reg |= RX1_GAIN_CTRL_SETUP(ctrl->rx1_mode) |
3418 RX2_GAIN_CTRL_SETUP(ctrl->rx2_mode);
3419
3420 phy->agc_mode[0] = ctrl->rx1_mode;
3421 phy->agc_mode[1] = ctrl->rx2_mode;
3422
3423 ad9361_spi_write(spi, REG_AGC_CONFIG_1, reg); // Gain Control Mode Select
3424
3425 /* AGC_USE_FULL_GAIN_TABLE handled in ad9361_load_gt() */
3426 ad9361_spi_writef(spi, REG_AGC_CONFIG_2, MAN_GAIN_CTRL_RX1,
3427 ctrl->mgc_rx1_ctrl_inp_en);
3428 ad9361_spi_writef(spi, REG_AGC_CONFIG_2, MAN_GAIN_CTRL_RX2,
3429 ctrl->mgc_rx2_ctrl_inp_en);
3430 ad9361_spi_writef(spi, REG_AGC_CONFIG_2, DIG_GAIN_EN,
3431 ctrl->dig_gain_en);
3432
3433 ctrl->adc_ovr_sample_size = clamp_t(uint8_t, ctrl->adc_ovr_sample_size, 1U, 8U);
3434 reg = ADC_OVERRANGE_SAMPLE_SIZE(ctrl->adc_ovr_sample_size - 1);
3435
3436 if (has_split_gt && phy->pdata->split_gt &&
3437 (ctrl->mgc_rx1_ctrl_inp_en || ctrl->mgc_rx2_ctrl_inp_en)) {
3438 switch (ctrl->mgc_split_table_ctrl_inp_gain_mode) {
3439 case 1:
3440 reg &= ~INCDEC_LMT_GAIN;
3441 break;
3442 case 2:
3443 reg |= INCDEC_LMT_GAIN;
3444 break;
3445 default:
3446 case 0:
3447 reg |= USE_AGC_FOR_LMTLPF_GAIN;
3448 break;
3449 }
3450 }
3451
3452 ctrl->mgc_inc_gain_step = clamp_t(uint8_t, ctrl->mgc_inc_gain_step, 1U, 8U);
3453 reg |= MANUAL_INCR_STEP_SIZE(ctrl->mgc_inc_gain_step - 1);
3454 ad9361_spi_write(spi, REG_AGC_CONFIG_3, reg); // Incr Step Size, ADC Overrange Size
3455
3456 if (has_split_gt && phy->pdata->split_gt) {
3457 reg = SIZE_SPLIT_TABLE - 1;
3458 }
3459 else {
3460 reg = SIZE_FULL_TABLE - 1;
3461 }
3462 ad9361_spi_write(spi, REG_MAX_LMT_FULL_GAIN, reg); // Max Full/LMT Gain Table Index
3463 ad9361_spi_write(spi, REG_RX1_MANUAL_LMT_FULL_GAIN, reg); // Rx1 Full/LMT Gain Index
3464 ad9361_spi_write(spi, REG_RX2_MANUAL_LMT_FULL_GAIN, reg); // Rx2 Full/LMT Gain Index
3465
3466 ctrl->mgc_dec_gain_step = clamp_t(uint8_t, ctrl->mgc_dec_gain_step, 1U, 8U);
3467 reg = MANUAL_CTRL_IN_DECR_GAIN_STP_SIZE(ctrl->mgc_dec_gain_step);
3468 ad9361_spi_write(spi, REG_PEAK_WAIT_TIME, reg); // Decr Step Size, Peak Overload Time
3469
3470 if (ctrl->dig_gain_en)
3471 ad9361_spi_write(spi, REG_DIGITAL_GAIN,
3472 MAXIMUM_DIGITAL_GAIN(ctrl->max_dig_gain) |
3473 DIG_GAIN_STP_SIZE(ctrl->dig_gain_step_size));
3474
3475 if (ctrl->adc_large_overload_thresh >= ctrl->adc_small_overload_thresh) {
3476 ad9361_spi_write(spi, REG_ADC_SMALL_OVERLOAD_THRESH,
3477 ctrl->adc_small_overload_thresh); // ADC Small Overload Threshold
3478 ad9361_spi_write(spi, REG_ADC_LARGE_OVERLOAD_THRESH,
3479 ctrl->adc_large_overload_thresh); // ADC Large Overload Threshold
3480 }
3481 else {
3482 ad9361_spi_write(spi, REG_ADC_SMALL_OVERLOAD_THRESH,
3483 ctrl->adc_large_overload_thresh); // ADC Small Overload Threshold
3484 ad9361_spi_write(spi, REG_ADC_LARGE_OVERLOAD_THRESH,
3485 ctrl->adc_small_overload_thresh); // ADC Large Overload Threshold
3486 }
3487
3488 reg = (ctrl->lmt_overload_high_thresh / 16) - 1;
3489 reg = clamp(reg, 0U, 63U);
3490 ad9361_spi_write(spi, REG_LARGE_LMT_OVERLOAD_THRESH, reg);
3491 reg = (ctrl->lmt_overload_low_thresh / 16) - 1;
3492 reg = clamp(reg, 0U, 63U);
3493 ad9361_spi_writef(spi, REG_SMALL_LMT_OVERLOAD_THRESH,
3494 SMALL_LMT_OVERLOAD_THRESH(~0), reg);
3495
3496 if (has_split_gt && phy->pdata->split_gt) {
3497 /* REVIST */
3498 ad9361_spi_write(spi, REG_RX1_MANUAL_LPF_GAIN, 0x58); // Rx1 LPF Gain Index
3499 ad9361_spi_write(spi, REG_RX2_MANUAL_LPF_GAIN, 0x18); // Rx2 LPF Gain Index
3500 ad9361_spi_write(spi, REG_FAST_INITIAL_LMT_GAIN_LIMIT, 0x27); // Initial LMT Gain Limit
3501 }
3502
3503 ad9361_spi_write(spi, REG_RX1_MANUAL_DIGITALFORCED_GAIN, 0x00); // Rx1 Digital Gain Index
3504 ad9361_spi_write(spi, REG_RX2_MANUAL_DIGITALFORCED_GAIN, 0x00); // Rx2 Digital Gain Index
3505
3506 reg = clamp_t(uint8_t, ctrl->low_power_thresh, 0U, 64U) * 2;
3507 ad9361_spi_write(spi, REG_FAST_LOW_POWER_THRESH, reg); // Low Power Threshold
3508 ad9361_spi_write(spi, REG_TX_SYMBOL_ATTEN_CONFIG, 0x00); // Tx Symbol Gain Control
3509
3510 ad9361_spi_writef(spi, REG_DEC_POWER_MEASURE_DURATION_0,
3511 USE_HB1_OUT_FOR_DEC_PWR_MEAS, 1); // Power Measurement Duration
3512
3513 ad9361_spi_writef(spi, REG_DEC_POWER_MEASURE_DURATION_0,
3514 ENABLE_DEC_PWR_MEAS, 1); // Power Measurement Duration
3515
3516 if (ctrl->rx1_mode == RF_GAIN_FASTATTACK_AGC ||
3517 ctrl->rx2_mode == RF_GAIN_FASTATTACK_AGC)
3518 reg = ilog2(ctrl->f_agc_dec_pow_measuremnt_duration / 16);
3519 else
3520 reg = ilog2(ctrl->dec_pow_measuremnt_duration / 16);
3521
3522 ad9361_spi_writef(spi, REG_DEC_POWER_MEASURE_DURATION_0,
3523 DEC_POWER_MEASUREMENT_DURATION(~0), reg); // Power Measurement Duration
3524
3525 /* AGC */
3526
3527 tmp1 = reg = clamp_t(uint8_t, ctrl->agc_inner_thresh_high, 0U, 127U);
3528 ad9361_spi_writef(spi, REG_AGC_LOCK_LEVEL,
3529 AGC_LOCK_LEVEL_FAST_AGC_INNER_HIGH_THRESH_SLOW(~0),
3530 reg);
3531
3532 tmp2 = reg = clamp_t(uint8_t, ctrl->agc_inner_thresh_low, 0U, 127U);
3533 reg |= (ctrl->adc_lmt_small_overload_prevent_gain_inc ?
3534 PREVENT_GAIN_INC : 0);
3535 ad9361_spi_write(spi, REG_AGC_INNER_LOW_THRESH, reg);
3536
3537 reg = AGC_OUTER_HIGH_THRESH(tmp1 - ctrl->agc_outer_thresh_high) |
3538 AGC_OUTER_LOW_THRESH(ctrl->agc_outer_thresh_low - tmp2);
3539 ad9361_spi_write(spi, REG_OUTER_POWER_THRESHS, reg);
3540
3541 reg = AGC_OUTER_HIGH_THRESH_EXED_STP_SIZE(ctrl->agc_outer_thresh_high_dec_steps) |
3542 AGC_OUTER_LOW_THRESH_EXED_STP_SIZE(ctrl->agc_outer_thresh_low_inc_steps);
3543 ad9361_spi_write(spi, REG_GAIN_STP_2, reg);
3544
3545 reg = ((ctrl->immed_gain_change_if_large_adc_overload) ?
3546 IMMED_GAIN_CHANGE_IF_LG_ADC_OVERLOAD : 0) |
3547 ((ctrl->immed_gain_change_if_large_lmt_overload) ?
3548 IMMED_GAIN_CHANGE_IF_LG_LMT_OVERLOAD : 0) |
3549 AGC_INNER_HIGH_THRESH_EXED_STP_SIZE(ctrl->agc_inner_thresh_high_dec_steps) |
3550 AGC_INNER_LOW_THRESH_EXED_STP_SIZE(ctrl->agc_inner_thresh_low_inc_steps);
3551 ad9361_spi_write(spi, REG_GAIN_STP1, reg);
3552
3553 reg = LARGE_ADC_OVERLOAD_EXED_COUNTER(ctrl->adc_large_overload_exceed_counter) |
3554 SMALL_ADC_OVERLOAD_EXED_COUNTER(ctrl->adc_small_overload_exceed_counter);
3555 ad9361_spi_write(spi, REG_ADC_OVERLOAD_COUNTERS, reg);
3556
3557 ad9361_spi_writef(spi, REG_GAIN_STP_CONFIG_2, LARGE_LPF_GAIN_STEP(~0),
3558 LARGE_LPF_GAIN_STEP(ctrl->adc_large_overload_inc_steps));
3559
3560 reg = LARGE_LMT_OVERLOAD_EXED_COUNTER(ctrl->lmt_overload_large_exceed_counter) |
3561 SMALL_LMT_OVERLOAD_EXED_COUNTER(ctrl->lmt_overload_small_exceed_counter);
3562 ad9361_spi_write(spi, REG_LMT_OVERLOAD_COUNTERS, reg);
3563
3564 ad9361_spi_writef(spi, REG_GAIN_STP_CONFIG1,
3565 DEC_STP_SIZE_FOR_LARGE_LMT_OVERLOAD(~0),
3566 ctrl->lmt_overload_large_inc_steps);
3567
3568 reg = DIG_SATURATION_EXED_COUNTER(ctrl->dig_saturation_exceed_counter) |
3569 (ctrl->sync_for_gain_counter_en ?
3570 ENABLE_SYNC_FOR_GAIN_COUNTER : 0);
3571 ad9361_spi_write(spi, REG_DIGITAL_SAT_COUNTER, reg);
3572
3573 /*
3574 * Fast AGC
3575 */
3576
3577 /* Fast AGC - Low Power */
3578 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3579 ENABLE_INCR_GAIN,
3580 ctrl->f_agc_allow_agc_gain_increase);
3581
3582 ad9361_spi_write(spi, REG_FAST_INCREMENT_TIME,
3583 ctrl->f_agc_lp_thresh_increment_time);
3584
3585 reg = ctrl->f_agc_lp_thresh_increment_steps - 1;
3586 reg = clamp_t(uint32_t, reg, 0U, 7U);
3587 ad9361_spi_writef(spi, REG_FAST_ENERGY_DETECT_COUNT,
3588 INCREMENT_GAIN_STP_LPFLMT(~0), reg);
3589
3590 /* Fast AGC - Lock Level */
3591 /* Dual use see also agc_inner_thresh_high */
3592 ad9361_spi_writef(spi, REG_FAST_CONFIG_2_SETTLING_DELAY,
3593 ENABLE_LMT_GAIN_INC_FOR_LOCK_LEVEL,
3594 ctrl->f_agc_lock_level_lmt_gain_increase_en);
3595
3596 reg = ctrl->f_agc_lock_level_gain_increase_upper_limit;
3597 reg = clamp_t(uint32_t, reg, 0U, 63U);
3598 ad9361_spi_writef(spi, REG_FAST_AGCLL_UPPER_LIMIT,
3599 AGCLL_MAX_INCREASE(~0), reg);
3600
3601 /* Fast AGC - Peak Detectors and Final Settling */
3602 reg = ctrl->f_agc_lpf_final_settling_steps;
3603 reg = clamp_t(uint32_t, reg, 0U, 3U);
3604 ad9361_spi_writef(spi, REG_FAST_ENERGY_LOST_THRESH,
3605 POST_LOCK_LEVEL_STP_SIZE_FOR_LPF_TABLE_FULL_TABLE(~0),
3606 reg);
3607
3608 reg = ctrl->f_agc_lmt_final_settling_steps;
3609 reg = clamp_t(uint32_t, reg, 0U, 3U);
3610 ad9361_spi_writef(spi, REG_FAST_STRONGER_SIGNAL_THRESH,
3611 POST_LOCK_LEVEL_STP_FOR_LMT_TABLE(~0), reg);
3612
3613 reg = ctrl->f_agc_final_overrange_count;
3614 reg = clamp_t(uint32_t, reg, 0U, 7U);
3615 ad9361_spi_writef(spi, REG_FAST_FINAL_OVER_RANGE_AND_OPT_GAIN,
3616 FINAL_OVER_RANGE_COUNT(~0), reg);
3617
3618 /* Fast AGC - Final Power Test */
3619 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3620 ENABLE_GAIN_INC_AFTER_GAIN_LOCK,
3621 ctrl->f_agc_gain_increase_after_gain_lock_en);
3622
3623 /* Fast AGC - Unlocking the Gain */
3624 /* 0 = MAX Gain, 1 = Optimized Gain, 2 = Set Gain */
3625
3626 reg = ctrl->f_agc_gain_index_type_after_exit_rx_mode;
3627 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3628 GOTO_SET_GAIN_IF_EXIT_RX_STATE, reg == SET_GAIN);
3629 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3630 GOTO_OPTIMIZED_GAIN_IF_EXIT_RX_STATE,
3631 reg == OPTIMIZED_GAIN);
3632
3633 ad9361_spi_writef(spi, REG_FAST_CONFIG_2_SETTLING_DELAY,
3634 USE_LAST_LOCK_LEVEL_FOR_SET_GAIN,
3635 ctrl->f_agc_use_last_lock_level_for_set_gain_en);
3636
3637 reg = ctrl->f_agc_optimized_gain_offset;
3638 reg = clamp_t(uint32_t, reg, 0U, 15U);
3639 ad9361_spi_writef(spi, REG_FAST_FINAL_OVER_RANGE_AND_OPT_GAIN,
3640 OPTIMIZE_GAIN_OFFSET(~0), reg);
3641
3642 tmp1 = !ctrl->f_agc_rst_gla_stronger_sig_thresh_exceeded_en ||
3643 !ctrl->f_agc_rst_gla_engergy_lost_sig_thresh_exceeded_en ||
3644 !ctrl->f_agc_rst_gla_large_adc_overload_en ||
3645 !ctrl->f_agc_rst_gla_large_lmt_overload_en ||
3646 ctrl->f_agc_rst_gla_en_agc_pulled_high_en;
3647
3648 ad9361_spi_writef(spi, REG_AGC_CONFIG_2,
3649 AGC_GAIN_UNLOCK_CTRL, tmp1);
3650
3651 reg = !ctrl->f_agc_rst_gla_stronger_sig_thresh_exceeded_en;
3652 ad9361_spi_writef(spi, REG_FAST_STRONG_SIGNAL_FREEZE,
3653 DONT_UNLOCK_GAIN_IF_STRONGER_SIGNAL, reg);
3654
3655 reg = ctrl->f_agc_rst_gla_stronger_sig_thresh_above_ll;
3656 reg = clamp_t(uint32_t, reg, 0U, 63U);
3657 ad9361_spi_writef(spi, REG_FAST_STRONGER_SIGNAL_THRESH,
3658 STRONGER_SIGNAL_THRESH(~0), reg);
3659
3660 reg = ctrl->f_agc_rst_gla_engergy_lost_sig_thresh_below_ll;
3661 reg = clamp_t(uint32_t, reg, 0U, 63U);
3662 ad9361_spi_writef(spi, REG_FAST_ENERGY_LOST_THRESH,
3663 ENERGY_LOST_THRESH(~0), reg);
3664
3665 reg = ctrl->f_agc_rst_gla_engergy_lost_goto_optim_gain_en;
3666 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3667 GOTO_OPT_GAIN_IF_ENERGY_LOST_OR_EN_AGC_HIGH, reg);
3668
3669 reg = !ctrl->f_agc_rst_gla_engergy_lost_sig_thresh_exceeded_en;
3670 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3671 DONT_UNLOCK_GAIN_IF_ENERGY_LOST, reg);
3672
3673 reg = ctrl->f_agc_energy_lost_stronger_sig_gain_lock_exit_cnt;
3674 reg = clamp_t(uint32_t, reg, 0U, 63U);
3675 ad9361_spi_writef(spi, REG_FAST_GAIN_LOCK_EXIT_COUNT,
3676 GAIN_LOCK_EXIT_COUNT(~0), reg);
3677
3678 reg = !ctrl->f_agc_rst_gla_large_adc_overload_en ||
3679 !ctrl->f_agc_rst_gla_large_lmt_overload_en;
3680 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3681 DONT_UNLOCK_GAIN_IF_LG_ADC_OR_LMT_OVRG, reg);
3682
3683 reg = !ctrl->f_agc_rst_gla_large_adc_overload_en;
3684 ad9361_spi_writef(spi, REG_FAST_LOW_POWER_THRESH,
3685 DONT_UNLOCK_GAIN_IF_ADC_OVRG, reg);
3686
3687 /* 0 = Max Gain, 1 = Set Gain, 2 = Optimized Gain, 3 = No Gain Change */
3688
3689 if (ctrl->f_agc_rst_gla_en_agc_pulled_high_en) {
3690 switch (ctrl->f_agc_rst_gla_if_en_agc_pulled_high_mode) {
3691 case MAX_GAIN:
3692 ad9361_spi_writef(spi, REG_FAST_CONFIG_2_SETTLING_DELAY,
3693 GOTO_MAX_GAIN_OR_OPT_GAIN_IF_EN_AGC_HIGH, 1);
3694
3695 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3696 GOTO_SET_GAIN_IF_EN_AGC_HIGH, 0);
3697
3698 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3699 GOTO_OPT_GAIN_IF_ENERGY_LOST_OR_EN_AGC_HIGH, 0);
3700 break;
3701 case SET_GAIN:
3702 ad9361_spi_writef(spi, REG_FAST_CONFIG_2_SETTLING_DELAY,
3703 GOTO_MAX_GAIN_OR_OPT_GAIN_IF_EN_AGC_HIGH, 0);
3704
3705 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3706 GOTO_SET_GAIN_IF_EN_AGC_HIGH, 1);
3707 break;
3708 case OPTIMIZED_GAIN:
3709 ad9361_spi_writef(spi, REG_FAST_CONFIG_2_SETTLING_DELAY,
3710 GOTO_MAX_GAIN_OR_OPT_GAIN_IF_EN_AGC_HIGH, 1);
3711
3712 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3713 GOTO_SET_GAIN_IF_EN_AGC_HIGH, 0);
3714
3715 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3716 GOTO_OPT_GAIN_IF_ENERGY_LOST_OR_EN_AGC_HIGH, 1);
3717 break;
3718 case NO_GAIN_CHANGE:
3719 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3720 GOTO_SET_GAIN_IF_EN_AGC_HIGH, 0);
3721 ad9361_spi_writef(spi, REG_FAST_CONFIG_2_SETTLING_DELAY,
3722 GOTO_MAX_GAIN_OR_OPT_GAIN_IF_EN_AGC_HIGH, 0);
3723 break;
3724 }
3725 }
3726 else {
3727 ad9361_spi_writef(spi, REG_FAST_CONFIG_1,
3728 GOTO_SET_GAIN_IF_EN_AGC_HIGH, 0);
3729 ad9361_spi_writef(spi, REG_FAST_CONFIG_2_SETTLING_DELAY,
3730 GOTO_MAX_GAIN_OR_OPT_GAIN_IF_EN_AGC_HIGH, 0);
3731 }
3732
3733 reg = ilog2(ctrl->f_agc_power_measurement_duration_in_state5 / 16);
3734 reg = clamp_t(uint32_t, reg, 0U, 15U);
3735 ad9361_spi_writef(spi, REG_RX1_MANUAL_LPF_GAIN,
3736 POWER_MEAS_IN_STATE_5(~0), reg);
3737 ad9361_spi_writef(spi, REG_RX1_MANUAL_LMT_FULL_GAIN,
3738 POWER_MEAS_IN_STATE_5_MSB, reg >> 3);
3739
3740 return ad9361_gc_update(phy);
3741 }
3742
3743 /**
3744 * Set the Aux DAC.
3745 * @param phy The AD9361 state structure.
3746 * @param dac The DAC.
3747 * @param val_mV The value.
3748 * @return 0 in case of success, negative error code otherwise.
3749 */
3750 static int32_t ad9361_auxdac_set(struct ad9361_rf_phy *phy, int32_t dac,
3751 int32_t val_mV)
3752 {
3753 struct spi_device *spi = phy->spi;
3754 uint32_t val, tmp;
3755
3756 dev_dbg(&phy->spi->dev, "%s DAC%"PRId32" = %"PRId32" mV", __func__, dac, val_mV);
3757
3758 /* Disable DAC if val == 0, Ignored in ENSM Auto Mode */
3759 ad9361_spi_writef(spi, REG_AUXDAC_ENABLE_CTRL,
3760 AUXDAC_MANUAL_BAR(dac), val_mV ? 0 : 1);
3761
3762 if (val_mV < 306)
3763 val_mV = 306;
3764
3765 if (val_mV < 1888) {
3766 val = ((val_mV - 306) * 1000) / 1404; /* Vref = 1V, Step = 2 */
3767 tmp = AUXDAC_1_VREF(0);
3768 }
3769 else {
3770 val = ((val_mV - 1761) * 1000) / 1836; /* Vref = 2.5V, Step = 2 */
3771 tmp = AUXDAC_1_VREF(3);
3772 }
3773
3774 val = clamp_t(uint32_t, val, 0, 1023);
3775
3776 switch (dac) {
3777 case 1:
3778 ad9361_spi_write(spi, REG_AUXDAC_1_WORD, val >> 2);
3779 ad9361_spi_write(spi, REG_AUXDAC_1_CONFIG, AUXDAC_1_WORD_LSB(val) | tmp);
3780 phy->auxdac1_value = val_mV;
3781 break;
3782 case 2:
3783 ad9361_spi_write(spi, REG_AUXDAC_2_WORD, val >> 2);
3784 ad9361_spi_write(spi, REG_AUXDAC_2_CONFIG, AUXDAC_2_WORD_LSB(val) | tmp);
3785 phy->auxdac2_value = val_mV;
3786 break;
3787 default:
3788 return -EINVAL;
3789 }
3790
3791 return 0;
3792 }
3793
3794 /**
3795 * Get the Aux DAC value.
3796 * @param phy The AD9361 state structure.
3797 * @param dac The DAC.
3798 * @return The value in case of success, negative error code otherwise.
3799 */
3800 int32_t ad9361_auxdac_get(struct ad9361_rf_phy *phy, int32_t dac)
3801 {
3802
3803 switch (dac) {
3804 case 1:
3805 return phy->auxdac1_value;
3806 case 2:
3807 return phy->auxdac2_value;
3808 default:
3809 return -EINVAL;
3810 }
3811
3812 return 0;
3813 }
3814
3815 /**
3816 * Setup the AuxDAC.
3817 * @param phy The AD9361 state structure.
3818 * @param ctrl Pointer to auxdac_control structure.
3819 * @return 0 in case of success, negative error code otherwise.
3820 */
3821 static int32_t ad9361_auxdac_setup(struct ad9361_rf_phy *phy,
3822 struct auxdac_control *ctrl)
3823 {
3824 struct spi_device *spi = phy->spi;
3825 uint8_t tmp;
3826
3827 dev_dbg(&phy->spi->dev, "%s", __func__);
3828
3829 ad9361_auxdac_set(phy, 1, ctrl->dac1_default_value);
3830 ad9361_auxdac_set(phy, 2, ctrl->dac2_default_value);
3831
3832 tmp = ~(AUXDAC_AUTO_TX_BAR(ctrl->dac2_in_tx_en << 1 | ctrl->dac1_in_tx_en) |
3833 AUXDAC_AUTO_RX_BAR(ctrl->dac2_in_rx_en << 1 | ctrl->dac1_in_rx_en) |
3834 AUXDAC_INIT_BAR(ctrl->dac2_in_alert_en << 1 | ctrl->dac1_in_alert_en));
3835
3836 ad9361_spi_writef(spi, REG_AUXDAC_ENABLE_CTRL,
3837 AUXDAC_AUTO_TX_BAR(~0) |
3838 AUXDAC_AUTO_RX_BAR(~0) |
3839 AUXDAC_INIT_BAR(~0),
3840 tmp); /* Auto Control */
3841
3842 ad9361_spi_writef(spi, REG_EXTERNAL_LNA_CTRL,
3843 AUXDAC_MANUAL_SELECT, ctrl->auxdac_manual_mode_en);
3844 ad9361_spi_write(spi, REG_AUXDAC1_RX_DELAY, ctrl->dac1_rx_delay_us);
3845 ad9361_spi_write(spi, REG_AUXDAC1_TX_DELAY, ctrl->dac1_tx_delay_us);
3846 ad9361_spi_write(spi, REG_AUXDAC2_RX_DELAY, ctrl->dac2_rx_delay_us);
3847 ad9361_spi_write(spi, REG_AUXDAC2_TX_DELAY, ctrl->dac2_tx_delay_us);
3848
3849 return 0;
3850 }
3851
3852 /**
3853 * Setup the AuxADC.
3854 * @param phy The AD9361 state structure.
3855 * @param ctrl The AuxADC settings.
3856 * @param bbpll_freq The BBPLL frequency [Hz].
3857 * @return 0 in case of success, negative error code otherwise.
3858 */
3859 static int32_t ad9361_auxadc_setup(struct ad9361_rf_phy *phy,
3860 struct auxadc_control *ctrl,
3861 uint32_t bbpll_freq)
3862 {
3863 struct spi_device *spi = phy->spi;
3864 uint32_t val;
3865
3866 dev_dbg(&phy->spi->dev, "%s", __func__);
3867
3868 val = DIV_ROUND_CLOSEST(ctrl->temp_time_inteval_ms *
3869 (bbpll_freq / 1000UL), (1 << 29));
3870
3871 ad9361_spi_write(spi, REG_TEMP_OFFSET, ctrl->offset);
3872 ad9361_spi_write(spi, REG_START_TEMP_READING, 0x00);
3873 ad9361_spi_write(spi, REG_TEMP_SENSE2,
3874 MEASUREMENT_TIME_INTERVAL(val) |
3875 (ctrl->periodic_temp_measuremnt ?
3876 TEMP_SENSE_PERIODIC_ENABLE : 0));
3877 ad9361_spi_write(spi, REG_TEMP_SENSOR_CONFIG,
3878 TEMP_SENSOR_DECIMATION(
3879 ilog2(ctrl->temp_sensor_decimation) - 8));
3880 ad9361_spi_write(spi, REG_AUXADC_CLOCK_DIVIDER,
3881 bbpll_freq / ctrl->auxadc_clock_rate);
3882 ad9361_spi_write(spi, REG_AUXADC_CONFIG,
3883 AUX_ADC_DECIMATION(
3884 ilog2(ctrl->auxadc_decimation) - 8));
3885
3886 return 0;
3887 }
3888
3889 /**
3890 * Get the measured temperature of the device.
3891 * @param phy The AD9361 state structure.
3892 * @return The measured temperature of the device.
3893 */
3894 int32_t ad9361_get_temp(struct ad9361_rf_phy *phy)
3895 {
3896 uint32_t val;
3897
3898 ad9361_spi_writef(phy->spi, REG_AUXADC_CONFIG, AUXADC_POWER_DOWN, 1);
3899 val = ad9361_spi_read(phy->spi, REG_TEMPERATURE);
3900 ad9361_spi_writef(phy->spi, REG_AUXADC_CONFIG, AUXADC_POWER_DOWN, 0);
3901
3902 return DIV_ROUND_CLOSEST(val * 1000000, 1140);
3903 }
3904
3905 /**
3906 * Get the Aux ADC value.
3907 * @param phy The AD9361 state structure.
3908 * @return The value in case of success, negative error code otherwise.
3909 */
3910 int32_t ad9361_get_auxadc(struct ad9361_rf_phy *phy)
3911 {
3912 uint8_t buf[2];
3913
3914 ad9361_spi_writef(phy->spi, REG_AUXADC_CONFIG, AUXADC_POWER_DOWN, 1);
3915 ad9361_spi_readm(phy->spi, REG_AUXADC_LSB, buf, 2);
3916 ad9361_spi_writef(phy->spi, REG_AUXADC_CONFIG, AUXADC_POWER_DOWN, 0);
3917
3918 return (buf[1] << 4) | AUXADC_WORD_LSB(buf[0]);
3919 }
3920
3921 /**
3922 * Setup the Control Output pins.
3923 * @param phy The AD9361 state structure.
3924 * @param ctrl The Control Output pins settings.
3925 * @return 0 in case of success, negative error code otherwise.
3926 */
3927 static int32_t ad9361_ctrl_outs_setup(struct ad9361_rf_phy *phy,
3928 struct ctrl_outs_control *ctrl)
3929 {
3930 struct spi_device *spi = phy->spi;
3931
3932 dev_dbg(&phy->spi->dev, "%s", __func__);
3933
3934 ad9361_spi_write(spi, REG_CTRL_OUTPUT_POINTER, ctrl->index); // Ctrl Out index
3935 return ad9361_spi_write(spi, REG_CTRL_OUTPUT_ENABLE, ctrl->en_mask); // Ctrl Out [7:0] output enable
3936 }
3937
3938 /**
3939 * Setup the GPO pins.
3940 * @param phy The AD9361 state structure.
3941 * @return 0 in case of success, negative error code otherwise.
3942 */
3943 static int32_t ad9361_gpo_setup(struct ad9361_rf_phy *phy, struct gpo_control *ctrl)
3944 {
3945 struct spi_device *spi = phy->spi;
3946
3947 dev_dbg(&phy->spi->dev, "%s", __func__);
3948
3949 ad9361_spi_write(spi, REG_AUTO_GPO,
3950 GPO_ENABLE_AUTO_RX(ctrl->gpo0_slave_rx_en |
3951 (ctrl->gpo1_slave_rx_en << 1) |
3952 (ctrl->gpo2_slave_rx_en << 2) |
3953 (ctrl->gpo3_slave_rx_en << 3)) |
3954 GPO_ENABLE_AUTO_TX(ctrl->gpo0_slave_tx_en |
3955 (ctrl->gpo1_slave_tx_en << 1) |
3956 (ctrl->gpo2_slave_tx_en << 2) |
3957 (ctrl->gpo3_slave_tx_en << 3)));
3958
3959 ad9361_spi_write(spi, REG_GPO_FORCE_AND_INIT,
3960 GPO_INIT_STATE(ctrl->gpo0_inactive_state_high_en |
3961 (ctrl->gpo1_inactive_state_high_en << 1) |
3962 (ctrl->gpo2_inactive_state_high_en << 2) |
3963 (ctrl->gpo3_inactive_state_high_en << 3)));
3964
3965 ad9361_spi_write(spi, REG_GPO0_RX_DELAY, ctrl->gpo0_rx_delay_us);
3966 ad9361_spi_write(spi, REG_GPO0_TX_DELAY, ctrl->gpo0_tx_delay_us);
3967 ad9361_spi_write(spi, REG_GPO1_RX_DELAY, ctrl->gpo1_rx_delay_us);
3968 ad9361_spi_write(spi, REG_GPO1_TX_DELAY, ctrl->gpo1_tx_delay_us);
3969 ad9361_spi_write(spi, REG_GPO2_RX_DELAY, ctrl->gpo2_rx_delay_us);
3970 ad9361_spi_write(spi, REG_GPO2_TX_DELAY, ctrl->gpo2_tx_delay_us);
3971 ad9361_spi_write(spi, REG_GPO3_RX_DELAY, ctrl->gpo3_rx_delay_us);
3972 ad9361_spi_write(spi, REG_GPO3_TX_DELAY, ctrl->gpo3_tx_delay_us);
3973
3974 return 0;
3975 }
3976
3977 /**
3978 * Setup the RSSI.
3979 * @param phy The AD9361 state structure.
3980 * @param ctrl The RSSI settings.
3981 * @param is_update True if update
3982 * @return 0 in case of success, negative error code otherwise.
3983 */
3984 static int32_t ad9361_rssi_setup(struct ad9361_rf_phy *phy,
3985 struct rssi_control *ctrl,
3986 bool is_update)
3987 {
3988 struct spi_device *spi = phy->spi;
3989 uint32_t total_weight, weight[4], total_dur = 0, temp;
3990 uint8_t dur_buf[4] = { 0 };
3991 int32_t val, ret, i, j = 0;
3992 uint32_t rssi_delay;
3993 uint32_t rssi_wait;
3994 int32_t rssi_duration;
3995 uint32_t rate;
3996
3997 dev_dbg(&phy->spi->dev, "%s", __func__);
3998
3999 if (ctrl->rssi_unit_is_rx_samples) {
4000 if (is_update)
4001 return 0; /* no update required */
4002
4003 rssi_delay = ctrl->rssi_delay;
4004 rssi_wait = ctrl->rssi_wait;
4005 rssi_duration = ctrl->rssi_duration;
4006 }
4007 else {
4008 /* update sample based on RX rate */
4009 rate = DIV_ROUND_CLOSEST(
4010 clk_get_rate(phy, phy->ref_clk_scale[RX_SAMPL_CLK]), 1000);
4011 /* units are in us */
4012 rssi_delay = DIV_ROUND_CLOSEST(ctrl->rssi_delay * rate, 1000);
4013 rssi_wait = DIV_ROUND_CLOSEST(ctrl->rssi_wait * rate, 1000);
4014 rssi_duration = DIV_ROUND_CLOSEST(
4015 ctrl->rssi_duration * rate, 1000);
4016 }
4017
4018 if (ctrl->restart_mode == EN_AGC_PIN_IS_PULLED_HIGH)
4019 rssi_delay = 0;
4020
4021 rssi_delay = clamp(rssi_delay / 8, 0U, 255U);
4022 rssi_wait = clamp(rssi_wait / 4, 0U, 255U);
4023
4024 do {
4025 for (i = 14; rssi_duration > 0 && i >= 0; i--) {
4026 val = 1 << i;
4027 if ((int64_t)rssi_duration >= val) {
4028 dur_buf[j++] = i;
4029 total_dur += val;
4030 rssi_duration -= val;
4031 break;
4032 }
4033 }
4034
4035 } while (j < 4 && rssi_duration > 0);
4036
4037 for (i = 0, total_weight = 0; i < 4; i++) {
4038 if (i < j)
4039 total_weight += weight[i] =
4040 DIV_ROUND_CLOSEST(RSSI_MAX_WEIGHT *
4041 (1 << dur_buf[i]), total_dur);
4042 else
4043 total_weight += weight[i] = 0;
4044 }
4045
4046 /* total of all weights must be 0xFF */
4047 val = total_weight - 0xFF;
4048 weight[j - 1] -= val;
4049
4050 ad9361_spi_write(spi, REG_MEASURE_DURATION_01,
4051 (dur_buf[1] << 4) | dur_buf[0]); // RSSI Measurement Duration 0, 1
4052 ad9361_spi_write(spi, REG_MEASURE_DURATION_23,
4053 (dur_buf[3] << 4) | dur_buf[2]); // RSSI Measurement Duration 2, 3
4054 ad9361_spi_write(spi, REG_RSSI_WEIGHT_0, weight[0]); // RSSI Weighted Multiplier 0
4055 ad9361_spi_write(spi, REG_RSSI_WEIGHT_1, weight[1]); // RSSI Weighted Multiplier 1
4056 ad9361_spi_write(spi, REG_RSSI_WEIGHT_2, weight[2]); // RSSI Weighted Multiplier 2
4057 ad9361_spi_write(spi, REG_RSSI_WEIGHT_3, weight[3]); // RSSI Weighted Multiplier 3
4058 ad9361_spi_write(spi, REG_RSSI_DELAY, rssi_delay); // RSSI Delay
4059 ad9361_spi_write(spi, REG_RSSI_WAIT_TIME, rssi_wait); // RSSI Wait
4060
4061 temp = RSSI_MODE_SELECT(ctrl->restart_mode);
4062 if (ctrl->restart_mode == SPI_WRITE_TO_REGISTER)
4063 temp |= START_RSSI_MEAS;
4064
4065 if (rssi_duration == 0 && j == 1) /* Power of two */
4066 temp |= DEFAULT_RSSI_MEAS_MODE;
4067
4068 ret = ad9361_spi_write(spi, REG_RSSI_CONFIG, temp); // RSSI Mode Select
4069
4070 if (ret < 0)
4071 dev_err(&phy->spi->dev, "Unable to write rssi config");
4072
4073 return 0;
4074 }
4075
4076 /**
4077 * This function needs to be called whenever BBPLL changes.
4078 * @param phy The AD9361 state structure.
4079 * @return 0 in case of success, negative error code otherwise.
4080 */
4081 static int32_t ad9361_bb_clk_change_handler(struct ad9361_rf_phy *phy)
4082 {
4083 int32_t ret;
4084
4085 ret = ad9361_gc_update(phy);
4086 ret |= ad9361_rssi_setup(phy, &phy->pdata->rssi_ctrl, true);
4087 ret |= ad9361_auxadc_setup(phy, &phy->pdata->auxadc_ctrl,
4088 clk_get_rate(phy, phy->ref_clk_scale[BBPLL_CLK]));
4089
4090 return ret;
4091 }
4092
4093 /**
4094 * Set the desired Enable State Machine (ENSM) state.
4095 * @param phy The AD9361 state structure.
4096 * @param ensm_state The ENSM state [ENSM_STATE_SLEEP_WAIT, ENSM_STATE_ALERT,
4097 * ENSM_STATE_TX, ENSM_STATE_TX_FLUSH, ENSM_STATE_RX,
4098 * ENSM_STATE_RX_FLUSH, ENSM_STATE_FDD, ENSM_STATE_FDD_FLUSH].
4099 * @param pinctrl Set true, will enable the ENSM pin control.
4100 * @return 0 in case of success, negative error code otherwise.
4101 */
4102 int32_t ad9361_ensm_set_state(struct ad9361_rf_phy *phy, uint8_t ensm_state,
4103 bool pinctrl)
4104 {
4105 struct spi_device *spi = phy->spi;
4106 int32_t rc = 0;
4107 uint32_t val;
4108 uint32_t tmp;
4109
4110 dev_dbg(dev, "Device is in %x state, moving to %x", phy->curr_ensm_state,
4111 ensm_state);
4112
4113
4114 if (phy->curr_ensm_state == ENSM_STATE_SLEEP) {
4115 ad9361_spi_write(spi, REG_CLOCK_ENABLE,
4116 DIGITAL_POWER_UP | CLOCK_ENABLE_DFLT | BBPLL_ENABLE |
4117 (phy->pdata->use_extclk ? XO_BYPASS : 0)); /* Enable Clocks */
4118 udelay(20);
4119 ad9361_spi_write(spi, REG_ENSM_CONFIG_1, TO_ALERT | FORCE_ALERT_STATE);
4120 ad9361_trx_vco_cal_control(phy, false, true); /* Enable VCO Cal */
4121 ad9361_trx_vco_cal_control(phy, true, true);
4122 }
4123
4124 val = (phy->pdata->ensm_pin_pulse_mode ? 0 : LEVEL_MODE) |
4125 (pinctrl ? ENABLE_ENSM_PIN_CTRL : 0) |
4126 (phy->txmon_tdd_en ? ENABLE_RX_DATA_PORT_FOR_CAL : 0) |
4127 TO_ALERT;
4128
4129 switch (ensm_state) {
4130 case ENSM_STATE_TX:
4131 val |= FORCE_TX_ON;
4132 if (phy->pdata->fdd)
4133 rc = -EINVAL;
4134 else if (phy->curr_ensm_state != ENSM_STATE_ALERT)
4135 rc = -EINVAL;
4136 break;
4137 case ENSM_STATE_RX:
4138 val |= FORCE_RX_ON;
4139 if (phy->pdata->fdd)
4140 rc = -EINVAL;
4141 else if (phy->curr_ensm_state != ENSM_STATE_ALERT)
4142 rc = -EINVAL;
4143 break;
4144 case ENSM_STATE_FDD:
4145 val |= FORCE_TX_ON;
4146 if (!phy->pdata->fdd)
4147 rc = -EINVAL;
4148 break;
4149 case ENSM_STATE_ALERT:
4150 val &= ~(FORCE_TX_ON | FORCE_RX_ON);
4151 val |= TO_ALERT | FORCE_ALERT_STATE;
4152 break;
4153 case ENSM_STATE_SLEEP_WAIT:
4154 break;
4155 case ENSM_STATE_SLEEP:
4156 ad9361_trx_vco_cal_control(phy, false, false); /* Disable VCO Cal */
4157 ad9361_trx_vco_cal_control(phy, true, false);
4158 ad9361_spi_write(spi, REG_ENSM_CONFIG_1, 0); /* Clear To Alert */
4159 ad9361_spi_write(spi, REG_ENSM_CONFIG_1,
4160 phy->pdata->fdd ? FORCE_TX_ON : FORCE_RX_ON);
4161 /* Delay Flush Time 384 ADC clock cycles */
4162 udelay(384000000UL / clk_get_rate(phy, phy->ref_clk_scale[ADC_CLK]));
4163 ad9361_spi_write(spi, REG_ENSM_CONFIG_1, 0); /* Move to Wait*/
4164 udelay(1); /* Wait for ENSM settle */
4165 ad9361_spi_write(spi, REG_CLOCK_ENABLE,
4166 (phy->pdata->use_extclk ? XO_BYPASS : 0)); /* Turn off all clocks */
4167 phy->curr_ensm_state = ensm_state;
4168 return 0;
4169
4170 default:
4171 dev_err(dev, "No handling for forcing %d ensm state",
4172 ensm_state);
4173 goto out;
4174 }
4175
4176 if (rc) {
4177 if ((phy->curr_ensm_state != ENSM_STATE_ALERT) && (val & (FORCE_RX_ON | FORCE_TX_ON))) {
4178 uint32_t val2 = val;
4179
4180 val2 &= ~(FORCE_TX_ON | FORCE_RX_ON);
4181 val2 |= TO_ALERT | FORCE_ALERT_STATE;
4182 ad9361_spi_write(spi, REG_ENSM_CONFIG_1, val2);
4183
4184 ad9361_check_cal_done(phy, REG_STATE, ENSM_STATE(~0), ENSM_STATE_ALERT);
4185 } else {
4186 dev_err(dev, "Invalid ENSM state transition in %s mode",
4187 phy->pdata->fdd ? "FDD" : "TDD");
4188 goto out;
4189 }
4190 }
4191
4192 if (!phy->pdata->fdd && !pinctrl && !phy->pdata->tdd_use_dual_synth &&
4193 (ensm_state == ENSM_STATE_TX || ensm_state == ENSM_STATE_RX)) {
4194 ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_2,
4195 TXNRX_SPI_CTRL, ensm_state == ENSM_STATE_TX);
4196
4197 ad9361_check_cal_done(phy, (ensm_state == ENSM_STATE_TX) ?
4198 REG_TX_CP_OVERRANGE_VCO_LOCK :
4199 REG_RX_CP_OVERRANGE_VCO_LOCK,
4200 VCO_LOCK, 1);
4201 }
4202
4203 rc = ad9361_spi_write(spi, REG_ENSM_CONFIG_1, val);
4204 if (rc)
4205 dev_err(dev, "Failed to restore state");
4206
4207 if ((val & FORCE_RX_ON) &&
4208 (phy->agc_mode[0] == RF_GAIN_MGC ||
4209 phy->agc_mode[1] == RF_GAIN_MGC)) {
4210 tmp = ad9361_spi_read(spi, REG_SMALL_LMT_OVERLOAD_THRESH);
4211 ad9361_spi_write(spi, REG_SMALL_LMT_OVERLOAD_THRESH,
4212 (tmp & SMALL_LMT_OVERLOAD_THRESH(~0)) |
4213 (phy->agc_mode[0] == RF_GAIN_MGC ? FORCE_PD_RESET_RX1 : 0) |
4214 (phy->agc_mode[1] == RF_GAIN_MGC ? FORCE_PD_RESET_RX2 : 0));
4215 ad9361_spi_write(spi, REG_SMALL_LMT_OVERLOAD_THRESH,
4216 tmp & SMALL_LMT_OVERLOAD_THRESH(~0));
4217 }
4218
4219 phy->curr_ensm_state = ensm_state;
4220
4221 out:
4222 return rc;
4223
4224 }
4225
4226 /**
4227 * Check if at least one of the clock rates is equal to the DATA_CLK (lvds) rate.
4228 * @param phy The AD9361 state structure.
4229 * @param rx_path_clks RX path rates buffer.
4230 * @return 0 in case of success, negative error code otherwise.
4231 */
4232 static int32_t ad9361_validate_trx_clock_chain(struct ad9361_rf_phy *phy,
4233 uint32_t *rx_path_clks)
4234 {
4235 int32_t i;
4236 uint32_t data_clk;
4237
4238 data_clk = (phy->pdata->rx2tx2 ? 4 : 2) /
4239 ((phy->pdata->port_ctrl.pp_conf[2] & LVDS_MODE) ? 1 : 2) *
4240 rx_path_clks[RX_SAMPL_FREQ];
4241
4242 /* CMOS Mode */
4243 if (!(phy->pdata->port_ctrl.pp_conf[2] & LVDS_MODE) &&
4244 (data_clk > 61440000UL)) {
4245 dev_err(&phy->spi->dev,
4246 "%s: Failed CMOS MODE DATA_CLK > 61.44MSPS", __func__);
4247 return -EINVAL;
4248 }
4249
4250 for (i = 1; i <= 3; i++) {
4251 if (abs(rx_path_clks[ADC_FREQ] / i - data_clk) < 4)
4252 return 0;
4253 }
4254
4255 for (i = 1; i <= 4; i++) {
4256 if (abs((rx_path_clks[R2_FREQ] >> i) - data_clk) < 4)
4257 return 0;
4258 }
4259
4260 dev_err(&phy->spi->dev, "%s: Failed - at least one of the clock rates"
4261 " must be equal to the DATA_CLK (lvds) rate", __func__);
4262
4263 return -EINVAL;
4264 }
4265
4266 /**
4267 * Set the RX and TX path rates.
4268 * @param phy The AD9361 state structure.
4269 * @param rx_path_clks RX path rates buffer.
4270 * @param tx_path_clks TX path rates buffer.
4271 * @return 0 in case of success, negative error code otherwise.
4272 */
4273 int32_t ad9361_set_trx_clock_chain(struct ad9361_rf_phy *phy,
4274 uint32_t *rx_path_clks,
4275 uint32_t *tx_path_clks)
4276 {
4277 int32_t ret, i, j, n;
4278
4279 dev_dbg(&phy->spi->dev, "%s", __func__);
4280
4281 if (!rx_path_clks || !tx_path_clks)
4282 return -EINVAL;
4283
4284 dev_dbg(&phy->spi->dev, "%s: %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32,
4285 __func__, rx_path_clks[BBPLL_FREQ], rx_path_clks[ADC_FREQ],
4286 rx_path_clks[R2_FREQ], rx_path_clks[R1_FREQ],
4287 rx_path_clks[CLKRF_FREQ], rx_path_clks[RX_SAMPL_FREQ]);
4288
4289 dev_dbg(&phy->spi->dev, "%s: %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32,
4290 __func__, tx_path_clks[BBPLL_FREQ], tx_path_clks[ADC_FREQ],
4291 tx_path_clks[R2_FREQ], tx_path_clks[R1_FREQ],
4292 tx_path_clks[CLKRF_FREQ], tx_path_clks[RX_SAMPL_FREQ]);
4293
4294 ret = ad9361_validate_trx_clock_chain(phy, rx_path_clks);
4295 if (ret < 0)
4296 return ret;
4297
4298 ret = clk_set_rate(phy, phy->ref_clk_scale[BBPLL_CLK], rx_path_clks[BBPLL_FREQ]);
4299 if (ret < 0)
4300 return ret;
4301
4302 for (i = ADC_CLK, j = DAC_CLK, n = ADC_FREQ;
4303 i <= RX_SAMPL_CLK; i++, j++, n++) {
4304 ret = clk_set_rate(phy, phy->ref_clk_scale[i], rx_path_clks[n]);
4305 if (ret < 0) {
4306 dev_err(dev, "Failed to set BB ref clock rate (%"PRId32")",
4307 ret);
4308 return ret;
4309 }
4310 ret = clk_set_rate(phy, phy->ref_clk_scale[j], tx_path_clks[n]);
4311 if (ret < 0) {
4312 dev_err(dev, "Failed to set BB ref clock rate (%"PRId32")",
4313 ret);
4314 return ret;
4315 }
4316 }
4317
4318 /*
4319 * Workaround for clock framework since clocks don't change we
4320 * manually need to enable the filter
4321 */
4322
4323 if (phy->rx_fir_dec == 1 || phy->bypass_rx_fir) {
4324 ad9361_spi_writef(phy->spi, REG_RX_ENABLE_FILTER_CTRL,
4325 RX_FIR_ENABLE_DECIMATION(~0), !phy->bypass_rx_fir);
4326 }
4327
4328 if (phy->tx_fir_int == 1 || phy->bypass_tx_fir) {
4329 ad9361_spi_writef(phy->spi, REG_TX_ENABLE_FILTER_CTRL,
4330 TX_FIR_ENABLE_INTERPOLATION(~0), !phy->bypass_tx_fir);
4331 }
4332
4333 /* The FIR filter once enabled causes the interface timing to change.
4334 * It's typically not a problem if the timing margin is big enough.
4335 * However at 61.44 MSPS it causes problems on some systems.
4336 * So we always run the digital tune in case the filter is enabled.
4337 * If it is disabled we restore the values from the initial calibration.
4338 */
4339
4340 if (!phy->pdata->dig_interface_tune_fir_disable &&
4341 !(phy->bypass_tx_fir && phy->bypass_rx_fir))
4342 ret = ad9361_dig_tune(phy, 0, SKIP_STORE_RESULT);
4343
4344 return ad9361_bb_clk_change_handler(phy);
4345 }
4346
4347 /**
4348 * Get the RX and TX path rates.
4349 * @param phy The AD9361 state structure.
4350 * @param rx_path_clks RX path rates buffer.
4351 * @param tx_path_clks TX path rates buffer.
4352 * @return 0 in case of success, negative error code otherwise.
4353 */
4354 int32_t ad9361_get_trx_clock_chain(struct ad9361_rf_phy *phy, uint32_t *rx_path_clks,
4355 uint32_t *tx_path_clks)
4356 {
4357 int32_t i, j, n;
4358 uint32_t bbpll_freq;
4359
4360 if (!rx_path_clks && !tx_path_clks)
4361 return -EINVAL;
4362
4363 bbpll_freq = clk_get_rate(phy, phy->ref_clk_scale[BBPLL_CLK]);
4364
4365 if (rx_path_clks)
4366 rx_path_clks[BBPLL_FREQ] = bbpll_freq;
4367
4368 if (tx_path_clks)
4369 tx_path_clks[BBPLL_FREQ] = bbpll_freq;
4370
4371 for (i = ADC_CLK, j = DAC_CLK, n = ADC_FREQ;
4372 i <= RX_SAMPL_CLK; i++, j++, n++) {
4373 if (rx_path_clks)
4374 rx_path_clks[n] = clk_get_rate(phy, phy->ref_clk_scale[i]);
4375 if (tx_path_clks)
4376 tx_path_clks[n] = clk_get_rate(phy, phy->ref_clk_scale[j]);
4377 }
4378
4379 return 0;
4380 }
4381
4382 /**
4383 * Calculate the RX and TX path rates to obtain the desired sample rate.
4384 * @param phy The AD9361 state structure.
4385 * @param tx_sample_rate The desired sample rate.
4386 * @param rate_gov The rate governor option.
4387 * @param rx_path_clks RX path rates buffer.
4388 * @param tx_path_clks TX path rates buffer.
4389 * @return 0 in case of success, negative error code otherwise.
4390 */
4391 int32_t ad9361_calculate_rf_clock_chain(struct ad9361_rf_phy *phy,
4392 uint32_t tx_sample_rate,
4393 uint32_t rate_gov,
4394 uint32_t *rx_path_clks,
4395 uint32_t *tx_path_clks)
4396 {
4397 uint32_t clktf, clkrf, adc_rate = 0, dac_rate = 0;
4398 uint64_t bbpll_rate;
4399 int32_t i, index_rx = -1, index_tx = -1, tmp;
4400 uint32_t div, tx_intdec, rx_intdec, recursion = 1;
4401 const int8_t clk_dividers[][4] = {
4402 { 12, 3, 2, 2 },
4403 { 8, 2, 2, 2 },
4404 { 6, 3, 1, 2 },
4405 { 4, 2, 2, 1 },
4406 { 3, 3, 1, 1 },
4407 { 2, 2, 1, 1 },
4408 { 1, 1, 1, 1 },
4409 };
4410
4411 if (phy->bypass_rx_fir)
4412 rx_intdec = 1;
4413 else
4414 rx_intdec = phy->rx_fir_dec;
4415
4416 if (phy->bypass_tx_fir)
4417 tx_intdec = 1;
4418 else
4419 tx_intdec = phy->tx_fir_int;
4420
4421 if ((rate_gov == 1) && ((rx_intdec * tx_sample_rate * 8) < MIN_ADC_CLK)) {
4422 recursion = 0;
4423 rate_gov = 0;
4424 }
4425
4426 dev_dbg(&phy->spi->dev, "%s: requested rate %"PRIu32" TXFIR int %"PRIu32" RXFIR dec %"PRIu32" mode %s",
4427 __func__, tx_sample_rate, tx_intdec, rx_intdec,
4428 rate_gov ? "Nominal" : "Highest OSR");
4429
4430 if (tx_sample_rate > 61440000UL)
4431 return -EINVAL;
4432
4433 clktf = tx_sample_rate * tx_intdec;
4434 clkrf = tx_sample_rate * rx_intdec * (phy->rx_eq_2tx ? 2 : 1);
4435
4436 for (i = rate_gov; i < 7; i++) {
4437 adc_rate = clkrf * clk_dividers[i][0];
4438 dac_rate = clktf * clk_dividers[i][0];
4439
4440 if ((adc_rate <= MAX_ADC_CLK) && (adc_rate >= MIN_ADC_CLK)) {
4441
4442
4443 if (dac_rate > adc_rate)
4444 tmp = (dac_rate / adc_rate) * -1;
4445 else
4446 tmp = adc_rate / dac_rate;
4447
4448 if (adc_rate <= MAX_DAC_CLK) {
4449 index_rx = i;
4450 index_tx = i - ((tmp == 1) ? 0 : tmp);
4451 dac_rate = adc_rate; /* ADC_CLK */
4452 break;
4453 }
4454 else {
4455 dac_rate = adc_rate / 2; /* ADC_CLK/2 */
4456 index_rx = i;
4457
4458 if (i == 4 && tmp >= 0)
4459 index_tx = 7; /* STOP: 3/2 != 1 */
4460 else
4461 index_tx = i + ((i == 5 && tmp >= 0) ? 1 : 2) -
4462 ((tmp == 1) ? 0 : tmp);
4463
4464 break;
4465 }
4466 }
4467 }
4468
4469 if ((index_tx < 0 || index_tx > 6 || index_rx < 0 || index_rx > 6) && rate_gov < 7 && recursion) {
4470 return ad9361_calculate_rf_clock_chain(phy, tx_sample_rate,
4471 ++rate_gov, rx_path_clks, tx_path_clks);
4472 }
4473 else if ((index_tx < 0 || index_tx > 6 || index_rx < 0 || index_rx > 6)) {
4474 dev_err(&phy->spi->dev, "%s: Failed to find suitable dividers: %s",
4475 __func__, (adc_rate < MIN_ADC_CLK) ? "ADC clock below limit" : "BBPLL rate above limit");
4476
4477 return -EINVAL;
4478 }
4479
4480 /* Calculate target BBPLL rate */
4481 div = MAX_BBPLL_DIV;
4482
4483 do {
4484 bbpll_rate = (uint64_t)adc_rate * div;
4485 div >>= 1;
4486
4487 } while ((bbpll_rate > MAX_BBPLL_FREQ) && (div >= MIN_BBPLL_DIV));
4488
4489 rx_path_clks[BBPLL_FREQ] = bbpll_rate;
4490 rx_path_clks[ADC_FREQ] = adc_rate;
4491 rx_path_clks[R2_FREQ] = rx_path_clks[ADC_FREQ] / clk_dividers[index_rx][1];
4492 rx_path_clks[R1_FREQ] = rx_path_clks[R2_FREQ] / clk_dividers[index_rx][2];
4493 rx_path_clks[CLKRF_FREQ] = rx_path_clks[R1_FREQ] / clk_dividers[index_rx][3];
4494 rx_path_clks[RX_SAMPL_FREQ] = rx_path_clks[CLKRF_FREQ] / rx_intdec;
4495
4496 tx_path_clks[BBPLL_FREQ] = bbpll_rate;
4497 tx_path_clks[DAC_FREQ] = dac_rate;
4498 tx_path_clks[T2_FREQ] = tx_path_clks[DAC_FREQ] / clk_dividers[index_tx][1];
4499 tx_path_clks[T1_FREQ] = tx_path_clks[T2_FREQ] / clk_dividers[index_tx][2];
4500 tx_path_clks[CLKTF_FREQ] = tx_path_clks[T1_FREQ] / clk_dividers[index_tx][3];
4501 tx_path_clks[TX_SAMPL_FREQ] = tx_path_clks[CLKTF_FREQ] / tx_intdec;
4502
4503 return 0;
4504 }
4505
4506 /**
4507 * Set the desired sample rate.
4508 * @param phy The AD9361 state structure.
4509 * @param freq The desired sample rate.
4510 * @return 0 in case of success, negative error code otherwise.
4511 */
4512 int32_t ad9361_set_trx_clock_chain_freq(struct ad9361_rf_phy *phy,
4513 uint32_t freq)
4514 {
4515 uint32_t rx[6], tx[6];
4516 int32_t ret;
4517
4518 ret = ad9361_calculate_rf_clock_chain(phy, freq,
4519 phy->rate_governor, rx, tx);
4520 if (ret < 0)
4521 return ret;
4522 return ad9361_set_trx_clock_chain(phy, rx, tx);
4523 }
4524
4525 /**
4526 * Internal ENSM mode options helper function.
4527 * @param phy The AD9361 state structure.
4528 * @param fdd
4529 * @param pinctrl
4530 * @return 0 in case of success, negative error code otherwise.
4531 */
4532 int32_t ad9361_set_ensm_mode(struct ad9361_rf_phy *phy, bool fdd, bool pinctrl)
4533 {
4534 struct ad9361_phy_platform_data *pd = phy->pdata;
4535 int32_t ret;
4536 uint32_t val = 0;
4537
4538 ad9361_spi_write(phy->spi, REG_ENSM_MODE, fdd ? FDD_MODE : 0);
4539
4540 val = ad9361_spi_read(phy->spi, REG_ENSM_CONFIG_2);
4541 val &= POWER_DOWN_RX_SYNTH | POWER_DOWN_TX_SYNTH;
4542
4543 if (fdd)
4544 ret = ad9361_spi_write(phy->spi, REG_ENSM_CONFIG_2,
4545 val | DUAL_SYNTH_MODE |
4546 (pd->fdd_independent_mode ? FDD_EXTERNAL_CTRL_ENABLE : 0));
4547 else
4548 ret = ad9361_spi_write(phy->spi, REG_ENSM_CONFIG_2, val |
4549 (pd->tdd_use_dual_synth ? DUAL_SYNTH_MODE : 0) |
4550 (pd->tdd_use_dual_synth ? 0 :
4551 (pinctrl ? SYNTH_ENABLE_PIN_CTRL_MODE : 0)));
4552
4553 return ret;
4554 }
4555
4556 /**
4557 * Fastlock read value.
4558 * @param spi
4559 * @param tx
4560 * @param profile
4561 * @param word
4562 * @return 0 in case of success, negative error code otherwise.
4563 */
4564 static int32_t ad9361_fastlock_readval(struct spi_device *spi, bool tx,
4565 uint32_t profile, uint32_t word)
4566 {
4567 uint32_t offs = 0;
4568
4569 if (tx)
4570 offs = REG_TX_FAST_LOCK_SETUP - REG_RX_FAST_LOCK_SETUP;
4571
4572 ad9361_spi_write(spi, REG_RX_FAST_LOCK_PROGRAM_ADDR + offs,
4573 RX_FAST_LOCK_PROFILE_ADDR(profile) |
4574 RX_FAST_LOCK_PROFILE_WORD(word));
4575
4576 return ad9361_spi_read(spi, REG_RX_FAST_LOCK_PROGRAM_READ + offs);
4577 }
4578
4579 /**
4580 * Fastlock write value.
4581 * @param spi
4582 * @param tx
4583 * @param profile
4584 * @param word
4585 * @param val
4586 * @param last
4587 * @return 0 in case of success, negative error code otherwise.
4588 */
4589 static int32_t ad9361_fastlock_writeval(struct spi_device *spi, bool tx,
4590 uint32_t profile, uint32_t word, uint8_t val, bool last)
4591 {
4592 uint32_t offs = 0;
4593 int32_t ret;
4594
4595 if (tx)
4596 offs = REG_TX_FAST_LOCK_SETUP - REG_RX_FAST_LOCK_SETUP;
4597
4598 ret = ad9361_spi_write(spi, REG_RX_FAST_LOCK_PROGRAM_ADDR + offs,
4599 RX_FAST_LOCK_PROFILE_ADDR(profile) |
4600 RX_FAST_LOCK_PROFILE_WORD(word));
4601 ret |= ad9361_spi_write(spi, REG_RX_FAST_LOCK_PROGRAM_DATA + offs, val);
4602 ret |= ad9361_spi_write(spi, REG_RX_FAST_LOCK_PROGRAM_CTRL + offs,
4603 RX_FAST_LOCK_PROGRAM_WRITE |
4604 RX_FAST_LOCK_PROGRAM_CLOCK_ENABLE);
4605
4606 if (last) /* Stop Clocks */
4607 ret |= ad9361_spi_write(spi,
4608 REG_RX_FAST_LOCK_PROGRAM_CTRL + offs, 0);
4609
4610 return ret;
4611 }
4612
4613 /**
4614 * Fastlock load values.
4615 * @param phy The AD9361 state structure.
4616 * @param tx
4617 * @param profile
4618 * @param values
4619 * @return 0 in case of success, negative error code otherwise.
4620 */
4621 int32_t ad9361_fastlock_load(struct ad9361_rf_phy *phy, bool tx,
4622 uint32_t profile, uint8_t *values)
4623 {
4624 uint32_t offs = 0;
4625 int32_t i, ret = 0;
4626 uint8_t buf[4];
4627
4628 dev_dbg(&phy->spi->dev, "%s: %s Profile %"PRIu32":",
4629 __func__, tx ? "TX" : "RX", profile);
4630
4631 if (tx)
4632 offs = REG_TX_FAST_LOCK_SETUP - REG_RX_FAST_LOCK_SETUP;
4633
4634 buf[0] = values[0];
4635 buf[1] = RX_FAST_LOCK_PROFILE_ADDR(profile) | RX_FAST_LOCK_PROFILE_WORD(0);
4636 ad9361_spi_writem(phy->spi, REG_RX_FAST_LOCK_PROGRAM_DATA + offs, buf, 2);
4637
4638 for (i = 1; i < RX_FAST_LOCK_CONFIG_WORD_NUM; i++) {
4639 buf[0] = RX_FAST_LOCK_PROGRAM_WRITE | RX_FAST_LOCK_PROGRAM_CLOCK_ENABLE;
4640 buf[1] = 0;
4641 buf[2] = values[i];
4642 buf[3] = RX_FAST_LOCK_PROFILE_ADDR(profile) | RX_FAST_LOCK_PROFILE_WORD(i);
4643 ad9361_spi_writem(phy->spi, REG_RX_FAST_LOCK_PROGRAM_CTRL + offs, buf, 4);
4644 }
4645
4646 ad9361_spi_write(phy->spi, REG_RX_FAST_LOCK_PROGRAM_CTRL + offs,
4647 RX_FAST_LOCK_PROGRAM_WRITE | RX_FAST_LOCK_PROGRAM_CLOCK_ENABLE);
4648 ad9361_spi_write(phy->spi, REG_RX_FAST_LOCK_PROGRAM_CTRL + offs, 0);
4649
4650 phy->fastlock.entry[tx][profile].flags = FASTLOOK_INIT;
4651 phy->fastlock.entry[tx][profile].alc_orig = values[15];
4652 phy->fastlock.entry[tx][profile].alc_written = values[15];
4653
4654 return ret;
4655 }
4656
4657 /**
4658 * Fastlock store.
4659 * @param phy The AD9361 state structure.
4660 * @param tx
4661 * @param profile
4662 * @return 0 in case of success, negative error code otherwise.
4663 */
4664 int32_t ad9361_fastlock_store(struct ad9361_rf_phy *phy, bool tx, uint32_t profile)
4665 {
4666 struct spi_device *spi = phy->spi;
4667 uint8_t val[16];
4668 uint32_t offs = 0, x, y;
4669
4670 dev_dbg(&phy->spi->dev, "%s: %s Profile %"PRIu32":",
4671 __func__, tx ? "TX" : "RX", profile);
4672
4673 if (tx)
4674 offs = REG_TX_FAST_LOCK_SETUP - REG_RX_FAST_LOCK_SETUP;
4675
4676 val[0] = ad9361_spi_read(spi, REG_RX_INTEGER_BYTE_0 + offs);
4677 val[1] = ad9361_spi_read(spi, REG_RX_INTEGER_BYTE_1 + offs);
4678 val[2] = ad9361_spi_read(spi, REG_RX_FRACT_BYTE_0 + offs);
4679 val[3] = ad9361_spi_read(spi, REG_RX_FRACT_BYTE_1 + offs);
4680 val[4] = ad9361_spi_read(spi, REG_RX_FRACT_BYTE_2 + offs);
4681
4682 x = ad9361_spi_readf(spi, REG_RX_VCO_BIAS_1 + offs, VCO_BIAS_REF(~0));
4683 y = ad9361_spi_readf(spi, REG_RX_ALC_VARACTOR + offs, VCO_VARACTOR(~0));
4684 val[5] = (x << 4) | y;
4685
4686 x = ad9361_spi_readf(spi, REG_RX_VCO_BIAS_1 + offs, VCO_BIAS_TCF(~0));
4687 y = ad9361_spi_readf(spi, REG_RX_CP_CURRENT + offs, CHARGE_PUMP_CURRENT(~0));
4688 /* Wide BW option: N = 1
4689 * Set init and steady state values to the same - let user space handle it
4690 */
4691 val[6] = (x << 3) | y;
4692 val[7] = y;
4693
4694 x = ad9361_spi_readf(spi, REG_RX_LOOP_FILTER_3 + offs, LOOP_FILTER_R3(~0));
4695 val[8] = (x << 4) | x;
4696
4697 x = ad9361_spi_readf(spi, REG_RX_LOOP_FILTER_2 + offs, LOOP_FILTER_C3(~0));
4698 val[9] = (x << 4) | x;
4699
4700 x = ad9361_spi_readf(spi, REG_RX_LOOP_FILTER_1 + offs, LOOP_FILTER_C1(~0));
4701 y = ad9361_spi_readf(spi, REG_RX_LOOP_FILTER_1 + offs, LOOP_FILTER_C2(~0));
4702 val[10] = (x << 4) | y;
4703
4704 x = ad9361_spi_readf(spi, REG_RX_LOOP_FILTER_2 + offs, LOOP_FILTER_R1(~0));
4705 val[11] = (x << 4) | x;
4706
4707 x = ad9361_spi_readf(spi, REG_RX_VCO_VARACTOR_CTRL_0 + offs,
4708 VCO_VARACTOR_REFERENCE_TCF(~0));
4709 y = ad9361_spi_readf(spi, REG_RFPLL_DIVIDERS,
4710 tx ? TX_VCO_DIVIDER(~0) : RX_VCO_DIVIDER(~0));
4711 val[12] = (x << 4) | y;
4712
4713 x = ad9361_spi_readf(spi, REG_RX_FORCE_VCO_TUNE_1 + offs, VCO_CAL_OFFSET(~0));
4714 y = ad9361_spi_readf(spi, REG_RX_VCO_VARACTOR_CTRL_1 + offs, VCO_VARACTOR_REFERENCE(~0));
4715 val[13] = (x << 4) | y;
4716
4717 val[14] = ad9361_spi_read(spi, REG_RX_FORCE_VCO_TUNE_0 + offs);
4718
4719 x = ad9361_spi_readf(spi, REG_RX_FORCE_ALC + offs, FORCE_ALC_WORD(~0));
4720 y = ad9361_spi_readf(spi, REG_RX_FORCE_VCO_TUNE_1 + offs, FORCE_VCO_TUNE);
4721 val[15] = (x << 1) | y;
4722
4723 return ad9361_fastlock_load(phy, tx, profile, val);
4724 }
4725
4726 /**
4727 * Fastlock prepare.
4728 * @param phy The AD9361 state structure.
4729 * @param tx
4730 * @param profile
4731 * @param prepare
4732 * @return 0 in case of success, negative error code otherwise.
4733 */
4734 static int32_t ad9361_fastlock_prepare(struct ad9361_rf_phy *phy, bool tx,
4735 uint32_t profile, bool prepare)
4736 {
4737 uint32_t offs, ready_mask;
4738 bool is_prepared;
4739
4740 dev_dbg(&phy->spi->dev, "%s: %s Profile %"PRIu32": %s",
4741 __func__, tx ? "TX" : "RX", profile,
4742 prepare ? "Prepare" : "Un-Prepare");
4743
4744 if (tx) {
4745 offs = REG_TX_FAST_LOCK_SETUP - REG_RX_FAST_LOCK_SETUP;
4746 ready_mask = TX_SYNTH_READY_MASK;
4747 }
4748 else {
4749 offs = 0;
4750 ready_mask = RX_SYNTH_READY_MASK;
4751 }
4752
4753 is_prepared = !!phy->fastlock.current_profile[tx];
4754
4755 if (prepare && !is_prepared) {
4756 ad9361_spi_write(phy->spi,
4757 REG_RX_FAST_LOCK_SETUP_INIT_DELAY + offs,
4758 (tx ? phy->pdata->tx_fastlock_delay_ns :
4759 phy->pdata->rx_fastlock_delay_ns) / 250);
4760 ad9361_spi_write(phy->spi, REG_RX_FAST_LOCK_SETUP + offs,
4761 RX_FAST_LOCK_PROFILE(profile) |
4762 RX_FAST_LOCK_MODE_ENABLE);
4763 ad9361_spi_write(phy->spi, REG_RX_FAST_LOCK_PROGRAM_CTRL + offs,
4764 0);
4765
4766 ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_2, ready_mask, 1);
4767 ad9361_trx_vco_cal_control(phy, tx, false);
4768 }
4769 else if (!prepare && is_prepared) {
4770 ad9361_spi_write(phy->spi, REG_RX_FAST_LOCK_SETUP + offs, 0);
4771
4772 /* Workaround: Exiting Fastlock Mode */
4773 ad9361_spi_writef(phy->spi, REG_RX_FORCE_ALC + offs, FORCE_ALC_ENABLE, 1);
4774 ad9361_spi_writef(phy->spi, REG_RX_FORCE_VCO_TUNE_1 + offs, FORCE_VCO_TUNE, 1);
4775 ad9361_spi_writef(phy->spi, REG_RX_FORCE_ALC + offs, FORCE_ALC_ENABLE, 0);
4776 ad9361_spi_writef(phy->spi, REG_RX_FORCE_VCO_TUNE_1 + offs, FORCE_VCO_TUNE, 0);
4777
4778 ad9361_trx_vco_cal_control(phy, tx, true);
4779 ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_2, ready_mask, 0);
4780
4781 phy->fastlock.current_profile[tx] = 0;
4782 }
4783
4784 return 0;
4785 }
4786
4787 /**
4788 * Fastlock recall.
4789 * @param phy The AD9361 state structure.
4790 * @param tx
4791 * @param profile
4792 * @return 0 in case of success, negative error code otherwise.
4793 */
4794 int32_t ad9361_fastlock_recall(struct ad9361_rf_phy *phy, bool tx, uint32_t profile)
4795 {
4796 uint32_t offs = 0;
4797 uint8_t curr, _new, orig, current_profile;
4798
4799 dev_dbg(&phy->spi->dev, "%s: %s Profile %"PRIu32":",
4800 __func__, tx ? "TX" : "RX", profile);
4801
4802 if (tx)
4803 offs = REG_TX_FAST_LOCK_SETUP - REG_RX_FAST_LOCK_SETUP;
4804
4805 if (phy->fastlock.entry[tx][profile].flags != FASTLOOK_INIT)
4806 return -EINVAL;
4807
4808 /* Workaround: Lock problem with same ALC word */
4809
4810 current_profile = phy->fastlock.current_profile[tx];
4811 _new = phy->fastlock.entry[tx][profile].alc_written;
4812
4813 if (current_profile == 0)
4814 curr = ad9361_spi_readf(phy->spi, REG_RX_FORCE_ALC + offs,
4815 FORCE_ALC_WORD(~0)) << 1;
4816 else
4817 curr = phy->fastlock.entry[tx][current_profile - 1].alc_written;
4818
4819 if ((curr >> 1) == (_new >> 1)) {
4820 orig = phy->fastlock.entry[tx][profile].alc_orig;
4821
4822 if ((orig >> 1) == (_new >> 1))
4823 phy->fastlock.entry[tx][profile].alc_written += 2;
4824 else
4825 phy->fastlock.entry[tx][profile].alc_written = orig;
4826
4827 ad9361_fastlock_writeval(phy->spi, tx, profile, 0xF,
4828 phy->fastlock.entry[tx][profile].alc_written, true);
4829 }
4830
4831 ad9361_fastlock_prepare(phy, tx, profile, true);
4832 phy->fastlock.current_profile[tx] = profile + 1;
4833
4834 return ad9361_spi_write(phy->spi, REG_RX_FAST_LOCK_SETUP + offs,
4835 RX_FAST_LOCK_PROFILE(profile) |
4836 (phy->pdata->trx_fastlock_pinctrl_en[tx] ?
4837 RX_FAST_LOCK_PROFILE_PIN_SELECT : 0) |
4838 RX_FAST_LOCK_MODE_ENABLE);
4839 }
4840
4841 /**
4842 * Fastlock save.
4843 * @param phy The AD9361 state structure.
4844 * @param tx
4845 * @param profile
4846 * @param values
4847 * @return 0 in case of success, negative error code otherwise.
4848 */
4849 int32_t ad9361_fastlock_save(struct ad9361_rf_phy *phy, bool tx,
4850 uint32_t profile, uint8_t *values)
4851 {
4852 int32_t i;
4853
4854 dev_dbg(&phy->spi->dev, "%s: %s Profile %"PRIu32":",
4855 __func__, tx ? "TX" : "RX", profile);
4856
4857 for (i = 0; i < RX_FAST_LOCK_CONFIG_WORD_NUM; i++)
4858 values[i] = ad9361_fastlock_readval(phy->spi, tx, profile, i);
4859
4860 return 0;
4861 }
4862
4863 /**
4864 * Multi Chip Sync (MCS) config.
4865 * @param phy The AD9361 state structure.
4866 * @param step MCS step.
4867 * @return 0 in case of success, negative error code otherwise.
4868 */
4869 int32_t ad9361_mcs(struct ad9361_rf_phy *phy, int32_t step)
4870 {
4871 int32_t mcs_mask = MCS_RF_ENABLE | MCS_BBPLL_ENABLE |
4872 MCS_DIGITAL_CLK_ENABLE | MCS_BB_ENABLE;
4873
4874 dev_dbg(&phy->spi->dev, "%s: MCS step %"PRId32, __func__, step);
4875
4876 switch (step) {
4877 case 0:
4878 /* REVIST:
4879 * POWER_DOWN_TRX_SYNTH and MCS_RF_ENABLE somehow conflict
4880 */
4881 ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_2,
4882 POWER_DOWN_TX_SYNTH | POWER_DOWN_RX_SYNTH, 0);
4883 case 1:
4884 /* REVIST:
4885 * POWER_DOWN_TRX_SYNTH and MCS_RF_ENABLE somehow conflict
4886 */
4887 ad9361_spi_writef(phy->spi, REG_ENSM_CONFIG_2,
4888 POWER_DOWN_TX_SYNTH | POWER_DOWN_RX_SYNTH, 0);
4889
4890 ad9361_spi_writef(phy->spi, REG_MULTICHIP_SYNC_AND_TX_MON_CTRL,
4891 mcs_mask, MCS_BB_ENABLE | MCS_BBPLL_ENABLE | MCS_RF_ENABLE);
4892 ad9361_spi_writef(phy->spi, REG_CP_BLEED_CURRENT,
4893 MCS_REFCLK_SCALE_EN, 1);
4894 break;
4895 case 2:
4896 if(!gpio_is_valid(phy->pdata->gpio_sync))
4897 break;
4898 /*
4899 * NOTE: This is not a regular GPIO -
4900 * HDL ensures Multi-chip Synchronization SYNC_IN Pulse Timing
4901 * relative to rising and falling edge of REF_CLK
4902 */
4903 gpio_set_value(phy->pdata->gpio_sync, 1);
4904 gpio_set_value(phy->pdata->gpio_sync, 0);
4905 break;
4906 case 3:
4907 ad9361_spi_writef(phy->spi, REG_MULTICHIP_SYNC_AND_TX_MON_CTRL,
4908 mcs_mask, MCS_BB_ENABLE | MCS_DIGITAL_CLK_ENABLE | MCS_RF_ENABLE);
4909 break;
4910 case 4:
4911 if(!gpio_is_valid(phy->pdata->gpio_sync))
4912 break;
4913 gpio_set_value(phy->pdata->gpio_sync, 1);
4914 gpio_set_value(phy->pdata->gpio_sync, 0);
4915 break;
4916 case 5:
4917 ad9361_spi_writef(phy->spi, REG_MULTICHIP_SYNC_AND_TX_MON_CTRL,
4918 mcs_mask, MCS_RF_ENABLE);
4919 break;
4920 }
4921
4922 return 0;
4923 }
4924
4925 /**
4926 * Clear state.
4927 * @param phy The AD9361 state structure.
4928 * @return None.
4929 */
4930 void ad9361_clear_state(struct ad9361_rf_phy *phy)
4931 {
4932 phy->current_table = RXGAIN_TBLS_END;
4933 phy->bypass_tx_fir = true;
4934 phy->bypass_rx_fir = true;
4935 phy->rate_governor = 1;
4936 phy->rfdc_track_en = true;
4937 phy->bbdc_track_en = true;
4938 phy->quad_track_en = true;
4939 phy->prev_ensm_state = 0;
4940 phy->curr_ensm_state = 0;
4941 phy->auto_cal_en = false;
4942 phy->last_tx_quad_cal_freq = 0;
4943 phy->flags = 0;
4944 phy->current_rx_bw_Hz = 0;
4945 phy->current_tx_bw_Hz = 0;
4946 phy->rxbbf_div = 0;
4947 phy->tx_fir_int = 0;
4948 phy->tx_fir_ntaps = 0;
4949 phy->rx_fir_dec = 0;
4950 phy->rx_fir_ntaps = 0;
4951 phy->ensm_pin_ctl_en = false;
4952 phy->txmon_tdd_en = 0;
4953 phy->current_tx_lo_freq = 0;
4954 phy->current_rx_lo_freq = 0;
4955 phy->current_tx_use_tdd_table = false;
4956 phy->current_rx_use_tdd_table = false;
4957
4958 memset(&phy->fastlock, 0, sizeof(phy->fastlock));
4959 }
4960
4961 /**
4962 * Determine the reference frequency value.
4963 * @param refin_Hz Maximum allowed frequency.
4964 * @param max Reference in frequency value.
4965 * @return Reference frequency value.
4966 */
4967 static uint32_t ad9361_ref_div_sel(uint32_t refin_Hz, uint32_t max)
4968 {
4969 if (refin_Hz <= (max / 2))
4970 return 2 * refin_Hz;
4971 else if (refin_Hz <= max)
4972 return refin_Hz;
4973 else if (refin_Hz <= (max * 2))
4974 return refin_Hz / 2;
4975 else if (refin_Hz <= (max * 4))
4976 return refin_Hz / 4;
4977 else
4978 return 0;
4979 }
4980
4981 /**
4982 * Setup the AD9361 device.
4983 * @param phy The AD9361 state structure.
4984 * @return 0 in case of success, negative error code otherwise.
4985 */
4986 int32_t ad9361_setup(struct ad9361_rf_phy *phy)
4987 {
4988 uint32_t refin_Hz, ref_freq, bbpll_freq;
4989 struct spi_device *spi = phy->spi;
4990 struct ad9361_phy_platform_data *pd = phy->pdata;
4991 int32_t ret;
4992 uint32_t real_rx_bandwidth, real_tx_bandwidth;
4993 bool tmp_use_ext_rx_lo = pd->use_ext_rx_lo;
4994 bool tmp_use_ext_tx_lo = pd->use_ext_tx_lo;
4995
4996 dev_dbg(dev, "%s", __func__);
4997
4998 pd->rf_rx_bandwidth_Hz = ad9361_validate_rf_bw(phy, pd->rf_rx_bandwidth_Hz);
4999 pd->rf_tx_bandwidth_Hz = ad9361_validate_rf_bw(phy, pd->rf_tx_bandwidth_Hz);
5000
5001 real_rx_bandwidth = pd->rf_rx_bandwidth_Hz / 2;
5002 real_tx_bandwidth = pd->rf_tx_bandwidth_Hz / 2;
5003
5004 if (pd->fdd) {
5005 pd->tdd_skip_vco_cal = false;
5006 if (pd->ensm_pin_ctrl && pd->fdd_independent_mode) {
5007 dev_warn(dev,
5008 "%s: Either set ENSM PINCTRL or FDD Independent Mode",
5009 __func__);
5010 pd->ensm_pin_ctrl = false;
5011 }
5012 }
5013
5014 if (pd->port_ctrl.pp_conf[2] & FDD_RX_RATE_2TX_RATE)
5015 phy->rx_eq_2tx = true;
5016
5017 ad9361_spi_write(spi, REG_CTRL, CTRL_ENABLE);
5018 ad9361_spi_write(spi, REG_BANDGAP_CONFIG0, MASTER_BIAS_TRIM(0x0E)); /* Enable Master Bias */
5019 ad9361_spi_write(spi, REG_BANDGAP_CONFIG1, BANDGAP_TEMP_TRIM(0x0E)); /* Set Bandgap Trim */
5020
5021 ad9361_set_dcxo_tune(phy, pd->dcxo_coarse, pd->dcxo_fine);
5022
5023 refin_Hz = phy->clk_refin->rate;
5024
5025 ref_freq = ad9361_ref_div_sel(refin_Hz, MAX_BBPLL_FREF);
5026 if (!ref_freq)
5027 return -EINVAL;
5028
5029 ad9361_spi_writef(spi, REG_REF_DIVIDE_CONFIG_1, RX_REF_RESET_BAR, 1);
5030 ad9361_spi_writef(spi, REG_REF_DIVIDE_CONFIG_2, TX_REF_RESET_BAR, 1);
5031 ad9361_spi_writef(spi, REG_REF_DIVIDE_CONFIG_2,
5032 TX_REF_DOUBLER_FB_DELAY(~0), 3); /* FB DELAY */
5033 ad9361_spi_writef(spi, REG_REF_DIVIDE_CONFIG_2,
5034 RX_REF_DOUBLER_FB_DELAY(~0), 3); /* FB DELAY */
5035
5036 ad9361_spi_write(spi, REG_CLOCK_ENABLE,
5037 DIGITAL_POWER_UP | CLOCK_ENABLE_DFLT | BBPLL_ENABLE |
5038 (pd->use_extclk ? XO_BYPASS : 0)); /* Enable Clocks */
5039
5040 ret = clk_set_rate(phy, phy->ref_clk_scale[BB_REFCLK], ref_freq);
5041 if (ret < 0) {
5042 dev_err(dev, "Failed to set BB ref clock rate (%"PRId32")",
5043 ret);
5044 return ret;
5045 }
5046
5047 ret = ad9361_set_trx_clock_chain(phy, pd->rx_path_clks,
5048 pd->tx_path_clks);
5049 if (ret < 0)
5050 return ret;
5051
5052 ret = clk_prepare_enable(phy->clks[BB_REFCLK]);
5053 if (ret < 0) {
5054 dev_err(dev, "Failed to enable BB ref clock rate (%"PRId32")",
5055 ret);
5056 return ret;
5057 }
5058
5059 if (!pd->rx2tx2) {
5060 pd->rx1tx1_mode_use_tx_num =
5061 clamp_t(uint32_t, pd->rx1tx1_mode_use_tx_num, TX_1, TX_2);
5062 pd->rx1tx1_mode_use_rx_num =
5063 clamp_t(uint32_t, pd->rx1tx1_mode_use_rx_num, RX_1, RX_2);
5064
5065 ad9361_en_dis_tx(phy, TX_1 | TX_2, pd->rx1tx1_mode_use_tx_num);
5066 ad9361_en_dis_rx(phy, TX_1 | TX_2, pd->rx1tx1_mode_use_rx_num);
5067 } else {
5068 ad9361_en_dis_tx(phy, TX_1 | TX_2, TX_1 | TX_2);
5069 ad9361_en_dis_rx(phy, RX_1 | RX_2, RX_1 | RX_2);
5070 }
5071
5072 ret = ad9361_rf_port_setup(phy, true, pd->rf_rx_input_sel,
5073 pd->rf_tx_output_sel);
5074 if (ret < 0)
5075 return ret;
5076
5077 ret = ad9361_pp_port_setup(phy, false);
5078 if (ret < 0)
5079 return ret;
5080
5081 ret = ad9361_auxdac_setup(phy, &pd->auxdac_ctrl);
5082 if (ret < 0)
5083 return ret;
5084
5085 bbpll_freq = clk_get_rate(phy, phy->ref_clk_scale[BBPLL_CLK]);
5086 ret = ad9361_auxadc_setup(phy, &pd->auxadc_ctrl, bbpll_freq);
5087 if (ret < 0)
5088 return ret;
5089
5090 ret = ad9361_ctrl_outs_setup(phy, &pd->ctrl_outs_ctrl);
5091 if (ret < 0)
5092 return ret;
5093
5094 ret = ad9361_gpo_setup(phy, &pd->gpo_ctrl);
5095 if (ret < 0)
5096 return ret;
5097
5098 ret = ad9361_set_ref_clk_cycles(phy, refin_Hz);
5099 if (ret < 0)
5100 return ret;
5101
5102 ret = ad9361_setup_ext_lna(phy, &pd->elna_ctrl);
5103 if (ret < 0)
5104 return ret;
5105
5106 /*
5107 * This allows forcing a lower F_REF window
5108 * (worse phase noise, better fractional spurs)
5109 */
5110 pd->trx_synth_max_fref = clamp_t(uint32_t, pd->trx_synth_max_fref,
5111 MIN_SYNTH_FREF, MAX_SYNTH_FREF);
5112
5113 ref_freq = ad9361_ref_div_sel(refin_Hz, pd->trx_synth_max_fref);
5114 if (!ref_freq)
5115 return -EINVAL;
5116
5117 ret = clk_set_rate(phy, phy->ref_clk_scale[RX_REFCLK], ref_freq);
5118 if (ret < 0) {
5119 dev_err(dev, "Failed to set RX Synth ref clock rate (%"PRId32")", ret);
5120 return ret;
5121 }
5122
5123 ret = clk_set_rate(phy, phy->ref_clk_scale[TX_REFCLK], ref_freq);
5124 if (ret < 0) {
5125 dev_err(dev, "Failed to set TX Synth ref clock rate (%"PRId32")", ret);
5126 return ret;
5127 }
5128
5129 ret = ad9361_txrx_synth_cp_calib(phy, ref_freq, false); /* RXCP */
5130 if (ret < 0)
5131 return ret;
5132
5133 ret = ad9361_txrx_synth_cp_calib(phy, ref_freq, true); /* TXCP */
5134 if (ret < 0)
5135 return ret;
5136
5137 phy->pdata->use_ext_rx_lo = 0;
5138 phy->pdata->use_ext_tx_lo = 0;
5139
5140 ret = clk_set_rate(phy, phy->ref_clk_scale[RX_RFPLL], ad9361_to_clk(pd->rx_synth_freq));
5141 if (ret < 0) {
5142 dev_err(dev, "Failed to set RX Synth rate (%"PRId32")",
5143 ret);
5144 return ret;
5145 }
5146
5147 ret = clk_prepare_enable(phy->clks[RX_REFCLK]);
5148 if (ret < 0) {
5149 dev_err(dev, "Failed to enable RX Synth ref clock (%"PRId32")", ret);
5150 return ret;
5151 }
5152
5153 ret = clk_prepare_enable(phy->clks[RX_RFPLL]);
5154 if (ret < 0)
5155 return ret;
5156
5157 /* Skip quad cal here we do it later again */
5158 phy->last_tx_quad_cal_freq = pd->tx_synth_freq;
5159 ret = clk_set_rate(phy, phy->ref_clk_scale[TX_RFPLL], ad9361_to_clk(pd->tx_synth_freq));
5160 if (ret < 0) {
5161 dev_err(dev, "Failed to set TX Synth rate (%"PRId32")",
5162 ret);
5163 return ret;
5164 }
5165
5166 ret = clk_prepare_enable(phy->clks[TX_REFCLK]);
5167 if (ret < 0) {
5168 dev_err(dev, "Failed to enable TX Synth ref clock (%"PRId32")", ret);
5169 return ret;
5170 }
5171
5172 ret = clk_prepare_enable(phy->clks[TX_RFPLL]);
5173 if (ret < 0)
5174 return ret;
5175
5176 phy->pdata->use_ext_rx_lo = tmp_use_ext_rx_lo;
5177 phy->pdata->use_ext_tx_lo = tmp_use_ext_tx_lo;
5178
5179 ad9361_clk_mux_set_parent(phy->ref_clk_scale[RX_RFPLL],
5180 pd->use_ext_rx_lo);
5181
5182 ad9361_clk_mux_set_parent(phy->ref_clk_scale[TX_RFPLL],
5183 pd->use_ext_tx_lo);
5184
5185 ret = ad9361_load_mixer_gm_subtable(phy);
5186 if (ret < 0)
5187 return ret;
5188
5189 ret = ad9361_gc_setup(phy, &pd->gain_ctrl);
5190 if (ret < 0)
5191 return ret;
5192
5193 ret = ad9361_rx_bb_analog_filter_calib(phy,
5194 real_rx_bandwidth,
5195 bbpll_freq);
5196 if (ret < 0)
5197 return ret;
5198
5199 ret = ad9361_tx_bb_analog_filter_calib(phy,
5200 real_tx_bandwidth,
5201 bbpll_freq);
5202 if (ret < 0)
5203 return ret;
5204
5205 ret = ad9361_rx_tia_calib(phy, real_rx_bandwidth);
5206 if (ret < 0)
5207 return ret;
5208
5209 ret = ad9361_tx_bb_second_filter_calib(phy, real_tx_bandwidth);
5210 if (ret < 0)
5211 return ret;
5212
5213 ret = ad9361_rx_adc_setup(phy,
5214 bbpll_freq,
5215 clk_get_rate(phy, phy->ref_clk_scale[ADC_CLK]));
5216 if (ret < 0)
5217 return ret;
5218
5219 ret = ad9361_bb_dc_offset_calib(phy);
5220 if (ret < 0)
5221 return ret;
5222
5223 ret = ad9361_rf_dc_offset_calib(phy,
5224 ad9361_from_clk(clk_get_rate(phy, phy->ref_clk_scale[RX_RFPLL])));
5225 if (ret < 0)
5226 return ret;
5227
5228 phy->current_rx_bw_Hz = pd->rf_rx_bandwidth_Hz;
5229 phy->current_tx_bw_Hz = pd->rf_tx_bandwidth_Hz;
5230 phy->last_tx_quad_cal_phase = ~0;
5231 ret = ad9361_tx_quad_calib(phy, real_rx_bandwidth, real_tx_bandwidth, -1);
5232 if (ret < 0)
5233 return ret;
5234
5235 ret = ad9361_tracking_control(phy, phy->bbdc_track_en,
5236 phy->rfdc_track_en, phy->quad_track_en);
5237 if (ret < 0)
5238 return ret;
5239
5240 if (!pd->fdd)
5241 ad9361_run_calibration(phy, TXMON_CAL);
5242
5243 ad9361_pp_port_setup(phy, true);
5244
5245 ret = ad9361_set_ensm_mode(phy, pd->fdd, pd->ensm_pin_ctrl);
5246 if (ret < 0)
5247 return ret;
5248
5249 ad9361_spi_writef(phy->spi, REG_TX_ATTEN_OFFSET,
5250 MASK_CLR_ATTEN_UPDATE, 0);
5251
5252 ret = ad9361_set_tx_atten(phy, pd->tx_atten,
5253 pd->rx2tx2 ? true : pd->rx1tx1_mode_use_tx_num == 1,
5254 pd->rx2tx2 ? true : pd->rx1tx1_mode_use_tx_num == 2, true);
5255 if (ret < 0)
5256 return ret;
5257
5258 if (!pd->rx2tx2) {
5259 ret = ad9361_set_tx_atten(phy, 89750,
5260 pd->rx1tx1_mode_use_tx_num == 2,
5261 pd->rx1tx1_mode_use_tx_num == 1, true);
5262 if (ret < 0)
5263 return ret;
5264 }
5265
5266 ret = ad9361_rssi_setup(phy, &pd->rssi_ctrl, false);
5267 if (ret < 0)
5268 return ret;
5269
5270 ret = ad9361_clkout_control(phy, pd->ad9361_clkout_mode);
5271 if (ret < 0)
5272 return ret;
5273
5274
5275 ret = ad9361_txmon_setup(phy, &pd->txmon_ctrl);
5276 if (ret < 0)
5277 return ret;
5278
5279 phy->curr_ensm_state = ad9361_spi_readf(spi, REG_STATE, ENSM_STATE(~0));
5280 ad9361_ensm_set_state(phy, pd->fdd ? ENSM_STATE_FDD : ENSM_STATE_RX,
5281 pd->ensm_pin_ctrl);
5282
5283 phy->auto_cal_en = true;
5284 phy->cal_threshold_freq = 100000000ULL; /* 100 MHz */
5285
5286 return 0;
5287
5288 }
5289
5290 /**
5291 * Perform the selected calibration
5292 * @param phy The AD9361 state structure.
5293 * @param cal The selected calibration.
5294 * @param arg The argument of the calibration.
5295 * @return 0 in case of success, negative error code otherwise.
5296 */
5297 int32_t ad9361_do_calib_run(struct ad9361_rf_phy *phy, uint32_t cal, int32_t arg)
5298 {
5299 int32_t ret;
5300
5301 dev_dbg(&phy->spi->dev, "%s: CAL %"PRIu32" ARG %"PRId32, __func__, cal, arg);
5302
5303 ret = ad9361_tracking_control(phy, false, false, false);
5304 if (ret < 0)
5305 return ret;
5306
5307 ad9361_ensm_force_state(phy, ENSM_STATE_ALERT);
5308
5309 switch (cal) {
5310 case TX_QUAD_CAL:
5311 ret = ad9361_tx_quad_calib(phy, phy->current_rx_bw_Hz / 2,
5312 phy->current_tx_bw_Hz / 2, arg);
5313 break;
5314 case RFDC_CAL:
5315 ret = ad9361_rf_dc_offset_calib(phy,
5316 ad9361_from_clk(clk_get_rate(phy, phy->ref_clk_scale[RX_RFPLL])));
5317 break;
5318 default:
5319 ret = -EINVAL;
5320 break;
5321 }
5322
5323 ret = ad9361_tracking_control(phy, phy->bbdc_track_en,
5324 phy->rfdc_track_en, phy->quad_track_en);
5325 ad9361_ensm_restore_prev_state(phy);
5326
5327 return ret;
5328 }
5329
5330 /**
5331 * Set the RF bandwidth.
5332 * @param phy The AD9361 state structure.
5333 * @param rf_rx_bw The desired RX bandwidth [Hz].
5334 * @param rf_tx_bw The desired TX bandwidth [Hz].
5335 * @return 0 in case of success, negative error code otherwise.
5336 */
5337 int32_t ad9361_update_rf_bandwidth(struct ad9361_rf_phy *phy,
5338 uint32_t rf_rx_bw, uint32_t rf_tx_bw)
5339 {
5340 int32_t ret;
5341
5342 ret = ad9361_tracking_control(phy, false, false, false);
5343 if (ret < 0)
5344 return ret;
5345
5346 ad9361_ensm_force_state(phy, ENSM_STATE_ALERT);
5347
5348 ret = __ad9361_update_rf_bandwidth(phy, rf_rx_bw, rf_tx_bw);
5349 if (ret < 0)
5350 return ret;
5351
5352 phy->current_rx_bw_Hz = rf_rx_bw;
5353 phy->current_tx_bw_Hz = rf_tx_bw;
5354
5355 ret = ad9361_tx_quad_calib(phy, rf_rx_bw / 2, rf_tx_bw / 2, -1);
5356 if (ret < 0)
5357 return ret;
5358
5359 ret = ad9361_tracking_control(phy, phy->bbdc_track_en,
5360 phy->rfdc_track_en, phy->quad_track_en);
5361 if (ret < 0)
5362 return ret;
5363
5364 ad9361_ensm_restore_prev_state(phy);
5365
5366 return 0;
5367 }
5368
5369 /**
5370 * Verify the FIR filter coefficients.
5371 * @param phy The AD9361 state structure.
5372 * @param dest Destination identifier (RX1,2 / TX1,2).
5373 * @param ntaps Number of filter Taps.
5374 * @param coef Pointer to filter coefficients.
5375 * @return 0 in case of success, negative error code otherwise.
5376 */
5377 static int32_t ad9361_verify_fir_filter_coef(struct ad9361_rf_phy *phy,
5378 enum fir_dest dest,
5379 uint32_t ntaps, short *coef)
5380 {
5381 struct spi_device *spi = phy->spi;
5382 uint32_t val, offs = 0, gain = 0, conf, sel, cnt;
5383 int32_t ret = 0;
5384
5385 #ifndef DEBUG
5386 return 0;
5387 #endif
5388 dev_dbg(&phy->spi->dev, "%s: TAPS %"PRIu32", dest %d",
5389 __func__, ntaps, dest);
5390
5391 if (dest & FIR_IS_RX) {
5392 gain = ad9361_spi_read(spi, REG_RX_FILTER_GAIN);
5393 offs = REG_RX_FILTER_COEF_ADDR - REG_TX_FILTER_COEF_ADDR;
5394 ad9361_spi_write(spi, REG_RX_FILTER_GAIN, 0);
5395 }
5396
5397 conf = ad9361_spi_read(spi, REG_TX_FILTER_CONF + offs);
5398
5399 if ((dest & 3) == 3) {
5400 sel = 1;
5401 cnt = 2;
5402 } else {
5403 sel = (dest & 3);
5404 cnt = 1;
5405 }
5406
5407 for (; cnt > 0; cnt--, sel++) {
5408
5409 ad9361_spi_write(spi, REG_TX_FILTER_CONF + offs,
5410 FIR_NUM_TAPS(ntaps / 16 - 1) |
5411 FIR_SELECT(sel) | FIR_START_CLK);
5412 for (val = 0; val < ntaps; val++) {
5413 short tmp;
5414 ad9361_spi_write(spi, REG_TX_FILTER_COEF_ADDR + offs, val);
5415
5416 tmp = (ad9361_spi_read(spi, REG_TX_FILTER_COEF_READ_DATA_1 + offs) & 0xFF) |
5417 (ad9361_spi_read(spi, REG_TX_FILTER_COEF_READ_DATA_2 + offs) << 8);
5418
5419 if (tmp != coef[val]) {
5420 dev_err(&phy->spi->dev,"%s%"PRIu32" read verify failed TAP%"PRIu32" %d =! %d",
5421 (dest & FIR_IS_RX) ? "RX" : "TX", sel,
5422 val, tmp, coef[val]);
5423 ret = -EIO;
5424 }
5425 }
5426 }
5427
5428 if (dest & FIR_IS_RX) {
5429 ad9361_spi_write(spi, REG_RX_FILTER_GAIN, gain);
5430 }
5431
5432 ad9361_spi_write(spi, REG_TX_FILTER_CONF + offs, conf);
5433
5434 return ret;
5435 }
5436
5437 /**
5438 * Load the FIR filter coefficients.
5439 * @param phy The AD9361 state structure.
5440 * @param dest Destination identifier (RX1,2 / TX1,2).
5441 * @param gain_dB Gain option.
5442 * @param ntaps Number of filter Taps.
5443 * @param coef Pointer to filter coefficients.
5444 * @return 0 in case of success, negative error code otherwise.
5445 */
5446 int32_t ad9361_load_fir_filter_coef(struct ad9361_rf_phy *phy,
5447 enum fir_dest dest, int32_t gain_dB,
5448 uint32_t ntaps, int16_t *coef)
5449 {
5450 struct spi_device *spi = phy->spi;
5451 uint32_t val, offs = 0, fir_conf = 0, fir_enable = 0;
5452
5453 dev_dbg(&phy->spi->dev, "%s: TAPS %"PRIu32", gain %"PRId32", dest %d",
5454 __func__, ntaps, gain_dB, dest);
5455
5456 if (coef == NULL || !ntaps || ntaps > 128 || ntaps % 16) {
5457 dev_err(&phy->spi->dev,
5458 "%s: Invalid parameters: TAPS %"PRIu32", gain %"PRId32", dest 0x%X",
5459 __func__, ntaps, gain_dB, dest);
5460
5461 return -EINVAL;
5462 }
5463
5464 ad9361_ensm_force_state(phy, ENSM_STATE_ALERT);
5465
5466 if (dest & FIR_IS_RX) {
5467 val = 3 - (gain_dB + 12) / 6;
5468 ad9361_spi_write(spi, REG_RX_FILTER_GAIN, val & 0x3);
5469 offs = REG_RX_FILTER_COEF_ADDR - REG_TX_FILTER_COEF_ADDR;
5470 phy->rx_fir_ntaps = ntaps;
5471 fir_enable = ad9361_spi_readf(phy->spi,
5472 REG_RX_ENABLE_FILTER_CTRL, RX_FIR_ENABLE_DECIMATION(~0));
5473 ad9361_spi_writef(phy->spi, REG_RX_ENABLE_FILTER_CTRL,
5474 RX_FIR_ENABLE_DECIMATION(~0),
5475 (phy->rx_fir_dec == 4) ? 3 : phy->rx_fir_dec);
5476 }
5477 else {
5478 if (gain_dB == -6)
5479 fir_conf = TX_FIR_GAIN_6DB;
5480 phy->tx_fir_ntaps = ntaps;
5481 fir_enable = ad9361_spi_readf(phy->spi,
5482 REG_TX_ENABLE_FILTER_CTRL, TX_FIR_ENABLE_INTERPOLATION(~0));
5483 ad9361_spi_writef(phy->spi, REG_TX_ENABLE_FILTER_CTRL,
5484 TX_FIR_ENABLE_INTERPOLATION(~0),
5485 (phy->tx_fir_int == 4) ? 3 : phy->tx_fir_int);
5486 }
5487
5488 val = ntaps / 16 - 1;
5489
5490 fir_conf |= FIR_NUM_TAPS(val) | FIR_SELECT(dest) | FIR_START_CLK;
5491
5492 ad9361_spi_write(spi, REG_TX_FILTER_CONF + offs, fir_conf);
5493
5494 for (val = 0; val < ntaps; val++) {
5495 ad9361_spi_write(spi, REG_TX_FILTER_COEF_ADDR + offs, val);
5496 ad9361_spi_write(spi, REG_TX_FILTER_COEF_WRITE_DATA_1 + offs,
5497 coef[val] & 0xFF);
5498 ad9361_spi_write(spi, REG_TX_FILTER_COEF_WRITE_DATA_2 + offs,
5499 coef[val] >> 8);
5500 ad9361_spi_write(spi, REG_TX_FILTER_CONF + offs,
5501 fir_conf | FIR_WRITE);
5502 ad9361_spi_write(spi, REG_TX_FILTER_COEF_READ_DATA_2 + offs, 0);
5503 ad9361_spi_write(spi, REG_TX_FILTER_COEF_READ_DATA_2 + offs, 0);
5504 }
5505
5506 ad9361_spi_write(spi, REG_TX_FILTER_CONF + offs, fir_conf);
5507 fir_conf &= ~FIR_START_CLK;
5508 ad9361_spi_write(spi, REG_TX_FILTER_CONF + offs, fir_conf);
5509
5510 if (dest & FIR_IS_RX)
5511 ad9361_spi_writef(phy->spi, REG_RX_ENABLE_FILTER_CTRL,
5512 RX_FIR_ENABLE_DECIMATION(~0), fir_enable);
5513 else
5514 ad9361_spi_writef(phy->spi, REG_TX_ENABLE_FILTER_CTRL,
5515 TX_FIR_ENABLE_INTERPOLATION(~0), fir_enable);
5516
5517 ad9361_ensm_restore_prev_state(phy);
5518
5519 return ad9361_verify_fir_filter_coef(phy, dest, ntaps, coef);
5520 }
5521
5522 /**
5523 * Parse the FIR filter file/buffer.
5524 * @param phy The AD9361 state structure.
5525 * @param data Pointer to buffer.
5526 * @param size Buffer size.
5527 * @return 0 in case of success, negative error code otherwise.
5528 */
5529 int32_t ad9361_parse_fir(struct ad9361_rf_phy *phy,
5530 char *data, uint32_t size)
5531 {
5532 char *line;
5533 int32_t i = 0, ret, txc, rxc;
5534 int32_t tx = -1, tx_gain, tx_int;
5535 int32_t rx = -1, rx_gain, rx_dec;
5536 int32_t rtx = -1, rrx = -1;
5537 int16_t coef_tx[128];
5538 int16_t coef_rx[128];
5539 char *ptr = data;
5540
5541 phy->filt_rx_bw_Hz = 0;
5542 phy->filt_tx_bw_Hz = 0;
5543 phy->filt_valid = false;
5544
5545 while ((line = strsep(&ptr, "\n"))) {
5546 if (line >= data + size) {
5547 break;
5548 }
5549
5550 if (line[0] == '#')
5551 continue;
5552
5553 if (tx < 0) {
5554 #ifdef WIN32
5555 ret = sscanf_s(line, "TX %d GAIN %d INT %d",
5556 &tx, &tx_gain, &tx_int);
5557 #else
5558 ret = sscanf(line, "TX %"PRId32" GAIN %"PRId32" INT %"PRId32,
5559 &tx, &tx_gain, &tx_int);
5560 #endif
5561 if (ret == 3)
5562 continue;
5563 else
5564 tx = -1;
5565 }
5566 if (rx < 0) {
5567 #ifdef WIN32
5568 ret = sscanf_s(line, "RX %d GAIN %d DEC %d",
5569 &rx, &rx_gain, &rx_dec);
5570 #else
5571 ret = sscanf(line, "RX %"PRId32" GAIN %"PRId32" DEC %"PRId32,
5572 &rx, &rx_gain, &rx_dec);
5573 #endif
5574 if (ret == 3)
5575 continue;
5576 else
5577 tx = -1;
5578 }
5579
5580 if (rtx < 0) {
5581 #ifdef WIN32
5582 ret = sscanf(line, "RTX %lu %lu %lu %lu %lu %lu",
5583 #else
5584 ret = sscanf(line, "RTX %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32,
5585 #endif
5586 &phy->filt_tx_path_clks[0],
5587 &phy->filt_tx_path_clks[1],
5588 &phy->filt_tx_path_clks[2],
5589 &phy->filt_tx_path_clks[3],
5590 &phy->filt_tx_path_clks[4],
5591 &phy->filt_tx_path_clks[5]);
5592 if (ret == 6) {
5593 rtx = 0;
5594 continue;
5595 } else {
5596 rtx = -1;
5597 }
5598 }
5599
5600 if (rrx < 0) {
5601 #ifdef WIN32
5602 ret = sscanf(line, "RRX %lu %lu %lu %lu %lu %lu",
5603 #else
5604 ret = sscanf(line, "RRX %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32,
5605 #endif
5606 &phy->filt_rx_path_clks[0],
5607 &phy->filt_rx_path_clks[1],
5608 &phy->filt_rx_path_clks[2],
5609 &phy->filt_rx_path_clks[3],
5610 &phy->filt_rx_path_clks[4],
5611 &phy->filt_rx_path_clks[5]);
5612 if (ret == 6) {
5613 rrx = 0;
5614 continue;
5615 } else {
5616 rrx = -1;
5617 }
5618 }
5619
5620 if (!phy->filt_rx_bw_Hz) {
5621 #ifdef WIN32
5622 ret = sscanf(line, "BWRX %d", &phy->filt_rx_bw_Hz);
5623 #else
5624 ret = sscanf(line, "BWRX %"PRId32, &phy->filt_rx_bw_Hz);
5625 #endif
5626 if (ret == 1)
5627 continue;
5628 else
5629 phy->filt_rx_bw_Hz = 0;
5630 }
5631
5632 if (!phy->filt_tx_bw_Hz) {
5633 #ifdef WIN32
5634 ret = sscanf(line, "BWTX %d", &phy->filt_tx_bw_Hz);
5635 #else
5636 ret = sscanf(line, "BWTX %"PRId32, &phy->filt_tx_bw_Hz);
5637 #endif
5638 if (ret == 1)
5639 continue;
5640 else
5641 phy->filt_tx_bw_Hz = 0;
5642 }
5643
5644 #ifdef WIN32
5645 ret = sscanf_s(line, "%d,%d", &txc, &rxc);
5646 #else
5647 ret = sscanf(line, "%"PRId32",%"PRId32, &txc, &rxc);
5648 #endif
5649 if (ret == 1) {
5650 coef_tx[i] = coef_rx[i] = (int16_t)txc;
5651 i++;
5652 continue;
5653 }
5654 else if (ret == 2) {
5655 coef_tx[i] = (int16_t)txc;
5656 coef_rx[i] = (int16_t)rxc;
5657 i++;
5658 continue;
5659 }
5660 }
5661
5662 switch (tx) {
5663 case FIR_TX1:
5664 case FIR_TX2:
5665 case FIR_TX1_TX2:
5666 phy->tx_fir_int = tx_int;
5667 ret = ad9361_load_fir_filter_coef(phy, (enum fir_dest)tx, tx_gain, i, coef_tx);
5668 break;
5669 default:
5670 ret = -EINVAL;
5671 }
5672
5673 switch (rx | FIR_IS_RX) {
5674 case FIR_RX1:
5675 case FIR_RX2:
5676 case FIR_RX1_RX2:
5677 phy->rx_fir_dec = rx_dec;
5678 ret = ad9361_load_fir_filter_coef(phy, (enum fir_dest)(rx | FIR_IS_RX),
5679 rx_gain, i, coef_rx);
5680 break;
5681 default:
5682 ret = -EINVAL;
5683 }
5684
5685 if (ret < 0)
5686 return ret;
5687
5688 if (!(rrx | rtx))
5689 phy->filt_valid = true;
5690
5691 return size;
5692 }
5693
5694 /**
5695 * Validate FIR filter configuration - on pass enable.
5696 * @param phy The AD9361 state structure.
5697 * @return 0 in case of success, negative error code otherwise.
5698 */
5699 int32_t ad9361_validate_enable_fir(struct ad9361_rf_phy *phy)
5700 {
5701 int32_t ret;
5702 uint32_t rx[6], tx[6];
5703 uint32_t max, min, valid;
5704
5705 dev_dbg(dev, "%s: TX FIR EN=%d/TAPS%d/INT%d, RX FIR EN=%d/TAPS%d/DEC%d",
5706 __func__, !phy->bypass_tx_fir, phy->tx_fir_ntaps, phy->tx_fir_int,
5707 !phy->bypass_rx_fir, phy->rx_fir_ntaps, phy->rx_fir_dec);
5708
5709 if (!phy->bypass_tx_fir) {
5710 if (!(phy->tx_fir_int == 1 || phy->tx_fir_int == 2 ||
5711 phy->tx_fir_int == 4)) {
5712 dev_err(dev,
5713 "%s: Invalid: Interpolation %d in filter config",
5714 __func__, phy->tx_fir_int);
5715 return -EINVAL;
5716 }
5717
5718
5719 if (phy->tx_fir_int == 1 && phy->tx_fir_ntaps > 64) {
5720 dev_err(dev,
5721 "%s: Invalid: TAPS > 64 and Interpolation = 1",
5722 __func__);
5723 return -EINVAL;
5724 }
5725 }
5726
5727 if (!phy->bypass_rx_fir) {
5728 if (!(phy->rx_fir_dec == 1 || phy->rx_fir_dec == 2 ||
5729 phy->rx_fir_dec == 4)) {
5730 dev_err(dev,
5731 "%s: Invalid: Decimation %d in filter config",
5732 __func__, phy->rx_fir_dec);
5733
5734 return -EINVAL;
5735 }
5736 }
5737
5738 if (!phy->filt_valid || phy->bypass_rx_fir || phy->bypass_tx_fir) {
5739 ret = ad9361_calculate_rf_clock_chain(phy,
5740 clk_get_rate(phy, phy->ref_clk_scale[TX_SAMPL_CLK]),
5741 phy->rate_governor, rx, tx);
5742 if (ret < 0) {
5743 min = phy->rate_governor ? 1500000U : 1000000U;
5744 dev_err(dev,
5745 "%s: Calculating filter rates failed %"PRId32
5746 " using min frequency",__func__, ret);
5747 ret = ad9361_calculate_rf_clock_chain(phy, min,
5748 phy->rate_governor, rx, tx);
5749 if (ret < 0) {
5750 return ret;
5751 }
5752 }
5753 valid = false;
5754 } else {
5755 #ifndef ALTERA_PLATFORM
5756 memcpy(rx, phy->filt_rx_path_clks, sizeof(rx));
5757 memcpy(tx, phy->filt_tx_path_clks, sizeof(tx));
5758 #else
5759 int32_t i;
5760 uint32_t num;
5761 num = sizeof(rx);
5762 for (i = 0; i < num; i++)
5763 rx[i] = phy->filt_rx_path_clks[i];
5764 num = sizeof(tx);
5765 for (i = 0; i < num; i++)
5766 tx[i] = phy->filt_tx_path_clks[i];
5767 #endif
5768 valid = true;
5769
5770 }
5771
5772 #ifdef _DEBUG
5773 dev_dbg(&phy->spi->dev, "%s:RX %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32,
5774 __func__, rx[BBPLL_FREQ], rx[ADC_FREQ],
5775 rx[R2_FREQ], rx[R1_FREQ],
5776 rx[CLKRF_FREQ], rx[RX_SAMPL_FREQ]);
5777
5778 dev_dbg(&phy->spi->dev, "%s:TX %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32" %"PRIu32,
5779 __func__, tx[BBPLL_FREQ], tx[ADC_FREQ],
5780 tx[R2_FREQ], tx[R1_FREQ],
5781 tx[CLKRF_FREQ], tx[RX_SAMPL_FREQ]);
5782 #endif
5783
5784 if (!phy->bypass_tx_fir) {
5785 max = (tx[DAC_FREQ] / tx[TX_SAMPL_FREQ]) * 16;
5786 if (phy->tx_fir_ntaps > max) {
5787 dev_err(dev,
5788 "%s: Invalid: ratio ADC/2 / TX_SAMPL * 16 > TAPS"
5789 "(max %"PRIu32", adc %"PRIu32", tx %"PRIu32")",
5790 __func__, max, rx[ADC_FREQ], tx[TX_SAMPL_FREQ]);
5791 return -EINVAL;
5792 }
5793 }
5794
5795 if (!phy->bypass_rx_fir) {
5796 max = ((rx[ADC_FREQ] / ((rx[ADC_FREQ] == rx[R2_FREQ]) ? 1 : 2)) /
5797 rx[RX_SAMPL_FREQ]) * 16;
5798 if (phy->rx_fir_ntaps > max) {
5799 dev_err(dev,
5800 "%s: Invalid: ratio ADC/2 / RX_SAMPL * 16 > TAPS (max %"PRIu32")",
5801 __func__, max);
5802 return -EINVAL;
5803 }
5804 }
5805
5806 ret = ad9361_set_trx_clock_chain(phy, rx, tx);
5807 if (ret < 0)
5808 return ret;
5809
5810 /* See also: ad9361_set_trx_clock_chain() */
5811 if (!phy->pdata->dig_interface_tune_fir_disable &&
5812 phy->bypass_tx_fir && phy->bypass_rx_fir)
5813 ad9361_dig_tune(phy, 0, RESTORE_DEFAULT);
5814
5815 return ad9361_update_rf_bandwidth(phy,
5816 valid ? phy->filt_rx_bw_Hz : phy->current_rx_bw_Hz,
5817 valid ? phy->filt_tx_bw_Hz : phy->current_tx_bw_Hz);
5818 }
5819
5820 /*
5821 * AD9361 Clocks
5822 */
5823
5824 /**
5825 * Set the multiplier and the divider for the selected refclk_scale structure.
5826 * @param priv The selected refclk_scale structure.
5827 * @param mul The multiplier value.
5828 * @param div The divider value.
5829 * @return 0 in case of success, negative error code otherwise.
5830 */
5831 static inline int32_t ad9361_set_muldiv(struct refclk_scale *priv, uint32_t mul, uint32_t div)
5832 {
5833 priv->mult = mul;
5834 priv->div = div;
5835 return 0;
5836 }
5837
5838 /**
5839 * Get the clk scaler for the selected refclk_scale structure.
5840 * @param priv The selected refclk_scale structure.
5841 * @return 0 in case of success, negative error code otherwise.
5842 */
5843 static int32_t ad9361_get_clk_scaler(struct refclk_scale *clk_priv)
5844 {
5845 struct spi_device *spi = clk_priv->spi;
5846 uint32_t tmp, tmp1;
5847
5848 switch (clk_priv->source) {
5849 case BB_REFCLK:
5850 tmp = ad9361_spi_read(spi, REG_CLOCK_CTRL);
5851 tmp &= 0x3;
5852 break;
5853 case RX_REFCLK:
5854 tmp = ad9361_spi_readf(spi, REG_REF_DIVIDE_CONFIG_1,
5855 RX_REF_DIVIDER_MSB);
5856 tmp1 = ad9361_spi_readf(spi, REG_REF_DIVIDE_CONFIG_2,
5857 RX_REF_DIVIDER_LSB);
5858 tmp = (tmp << 1) | tmp1;
5859 break;
5860 case TX_REFCLK:
5861 tmp = ad9361_spi_readf(spi, REG_REF_DIVIDE_CONFIG_2,
5862 TX_REF_DIVIDER(~0));
5863 break;
5864 case ADC_CLK:
5865 tmp = ad9361_spi_read(spi, REG_BBPLL);
5866 return ad9361_set_muldiv(clk_priv, 1, 1 << (tmp & 0x7));
5867 case R2_CLK:
5868 tmp = ad9361_spi_readf(spi, REG_RX_ENABLE_FILTER_CTRL,
5869 DEC3_ENABLE_DECIMATION(~0));
5870 return ad9361_set_muldiv(clk_priv, 1, tmp + 1);
5871 case R1_CLK:
5872 tmp = ad9361_spi_readf(spi, REG_RX_ENABLE_FILTER_CTRL, RHB2_EN);
5873 return ad9361_set_muldiv(clk_priv, 1, tmp + 1);
5874 case CLKRF_CLK:
5875 tmp = ad9361_spi_readf(spi, REG_RX_ENABLE_FILTER_CTRL, RHB1_EN);
5876 return ad9361_set_muldiv(clk_priv, 1, tmp + 1);
5877 case RX_SAMPL_CLK:
5878 tmp = ad9361_spi_readf(spi, REG_RX_ENABLE_FILTER_CTRL,
5879 RX_FIR_ENABLE_DECIMATION(~0));
5880
5881 if (!tmp)
5882 tmp = 1; /* bypass filter */
5883 else
5884 tmp = (1 << (tmp - 1));
5885
5886 return ad9361_set_muldiv(clk_priv, 1, tmp);
5887 case DAC_CLK:
5888 tmp = ad9361_spi_readf(spi, REG_BBPLL, BIT(3));
5889 return ad9361_set_muldiv(clk_priv, 1, tmp + 1);
5890 case T2_CLK:
5891 tmp = ad9361_spi_readf(spi, REG_TX_ENABLE_FILTER_CTRL,
5892 THB3_ENABLE_INTERP(~0));
5893 return ad9361_set_muldiv(clk_priv, 1, tmp + 1);
5894 case T1_CLK:
5895 tmp = ad9361_spi_readf(spi, REG_TX_ENABLE_FILTER_CTRL, THB2_EN);
5896 return ad9361_set_muldiv(clk_priv, 1, tmp + 1);
5897 case CLKTF_CLK:
5898 tmp = ad9361_spi_readf(spi, REG_TX_ENABLE_FILTER_CTRL, THB1_EN);
5899 return ad9361_set_muldiv(clk_priv, 1, tmp + 1);
5900 case TX_SAMPL_CLK:
5901 tmp = ad9361_spi_readf(spi, REG_TX_ENABLE_FILTER_CTRL,
5902 TX_FIR_ENABLE_INTERPOLATION(~0));
5903
5904 if (!tmp)
5905 tmp = 1; /* bypass filter */
5906 else
5907 tmp = (1 << (tmp - 1));
5908
5909 return ad9361_set_muldiv(clk_priv, 1, tmp);
5910 default:
5911 return -EINVAL;
5912 }
5913
5914 /* REFCLK Scaler */
5915 switch (tmp) {
5916 case 0:
5917 ad9361_set_muldiv(clk_priv, 1, 1);
5918 break;
5919 case 1:
5920 ad9361_set_muldiv(clk_priv, 1, 2);
5921 break;
5922 case 2:
5923 ad9361_set_muldiv(clk_priv, 1, 4);
5924 break;
5925 case 3:
5926 ad9361_set_muldiv(clk_priv, 2, 1);
5927 break;
5928 default:
5929 return -EINVAL;
5930
5931 }
5932
5933 return 0;
5934 }
5935
5936 /**
5937 * Calculate the REFCLK Scaler for the selected refclk_scale structure.
5938 * Note: REFCLK Scaler values - 00: x1; 01: x½; 10: x¼; 11: x2.
5939 * @param clk_priv The selected refclk_scale structure.
5940 * @return 0 in case of success, negative error code otherwise.
5941 */
5942 static int32_t ad9361_to_refclk_scaler(struct refclk_scale *clk_priv)
5943 {
5944 /* REFCLK Scaler */
5945 switch (((clk_priv->mult & 0xF) << 4) | (clk_priv->div & 0xF)) {
5946 case 0x11:
5947 return 0;
5948 case 0x12:
5949 return 1;
5950 case 0x14:
5951 return 2;
5952 case 0x21:
5953 return 3;
5954 default:
5955 return -EINVAL;
5956 }
5957 };
5958
5959 /**
5960 * Set clk scaler for the selected refclk_scale structure.
5961 * @param clk_priv The selected refclk_scale structure.
5962 * @param set Set true, the reference clock frequency will be scaled before
5963 * it enters the BBPLL.
5964 * @return 0 in case of success, negative error code otherwise.
5965 */
5966 static int32_t ad9361_set_clk_scaler(struct refclk_scale *clk_priv, bool set)
5967 {
5968 struct spi_device *spi = clk_priv->spi;
5969 uint32_t tmp;
5970 int32_t ret;
5971
5972 switch (clk_priv->source) {
5973 case BB_REFCLK:
5974 ret = ad9361_to_refclk_scaler(clk_priv);
5975 if (ret < 0)
5976 return ret;
5977 if (set)
5978 return ad9361_spi_writef(spi, REG_CLOCK_CTRL,
5979 REF_FREQ_SCALER(~0), ret);
5980 break;
5981
5982 case RX_REFCLK:
5983 ret = ad9361_to_refclk_scaler(clk_priv);
5984 if (ret < 0)
5985 return ret;
5986 if (set) {
5987 tmp = ret;
5988 ret = ad9361_spi_writef(spi, REG_REF_DIVIDE_CONFIG_1,
5989 RX_REF_DIVIDER_MSB, tmp >> 1);
5990 ret |= ad9361_spi_writef(spi, REG_REF_DIVIDE_CONFIG_2,
5991 RX_REF_DIVIDER_LSB, tmp & 1);
5992 return ret;
5993 }
5994 break;
5995 case TX_REFCLK:
5996 ret = ad9361_to_refclk_scaler(clk_priv);
5997 if (ret < 0)
5998 return ret;
5999 if (set)
6000 return ad9361_spi_writef(spi, REG_REF_DIVIDE_CONFIG_2,
6001 TX_REF_DIVIDER(~0), ret);
6002 break;
6003 case ADC_CLK:
6004 tmp = ilog2((uint8_t)clk_priv->div);
6005 if (clk_priv->mult != 1 || tmp > 6 || tmp < 1)
6006 return -EINVAL;
6007
6008 if (set)
6009 return ad9361_spi_writef(spi, REG_BBPLL, 0x7, tmp);
6010 break;
6011 case R2_CLK:
6012 if (clk_priv->mult != 1 || clk_priv->div > 3 || clk_priv->div < 1)
6013 return -EINVAL;
6014 if (set)
6015 return ad9361_spi_writef(spi, REG_RX_ENABLE_FILTER_CTRL,
6016 DEC3_ENABLE_DECIMATION(~0),
6017 clk_priv->div - 1);
6018 break;
6019 case R1_CLK:
6020 if (clk_priv->mult != 1 || clk_priv->div > 2 || clk_priv->div < 1)
6021 return -EINVAL;
6022 if (set)
6023 return ad9361_spi_writef(spi, REG_RX_ENABLE_FILTER_CTRL,
6024 RHB2_EN, clk_priv->div - 1);
6025 break;
6026 case CLKRF_CLK:
6027 if (clk_priv->mult != 1 || clk_priv->div > 2 || clk_priv->div < 1)
6028 return -EINVAL;
6029 if (set)
6030 return ad9361_spi_writef(spi, REG_RX_ENABLE_FILTER_CTRL,
6031 RHB1_EN, clk_priv->div - 1);
6032 break;
6033 case RX_SAMPL_CLK:
6034 if (clk_priv->mult != 1 || clk_priv->div > 4 ||
6035 clk_priv->div < 1 || clk_priv->div == 3)
6036 return -EINVAL;
6037
6038 if (clk_priv->phy->bypass_rx_fir)
6039 tmp = 0;
6040 else
6041 tmp = ilog2(clk_priv->div) + 1;
6042
6043 if (set)
6044 return ad9361_spi_writef(spi, REG_RX_ENABLE_FILTER_CTRL,
6045 RX_FIR_ENABLE_DECIMATION(~0), tmp);
6046 break;
6047 case DAC_CLK:
6048 if (clk_priv->mult != 1 || clk_priv->div > 2 || clk_priv->div < 1)
6049 return -EINVAL;
6050 if (set)
6051 return ad9361_spi_writef(spi, REG_BBPLL,
6052 BIT(3), clk_priv->div - 1);
6053 break;
6054 case T2_CLK:
6055 if (clk_priv->mult != 1 || clk_priv->div > 3 || clk_priv->div < 1)
6056 return -EINVAL;
6057 if (set)
6058 return ad9361_spi_writef(spi, REG_TX_ENABLE_FILTER_CTRL,
6059 THB3_ENABLE_INTERP(~0),
6060 clk_priv->div - 1);
6061 break;
6062 case T1_CLK:
6063 if (clk_priv->mult != 1 || clk_priv->div > 2 || clk_priv->div < 1)
6064 return -EINVAL;
6065 if (set)
6066 return ad9361_spi_writef(spi, REG_TX_ENABLE_FILTER_CTRL,
6067 THB2_EN, clk_priv->div - 1);
6068 break;
6069 case CLKTF_CLK:
6070 if (clk_priv->mult != 1 || clk_priv->div > 2 || clk_priv->div < 1)
6071 return -EINVAL;
6072 if (set)
6073 return ad9361_spi_writef(spi, REG_TX_ENABLE_FILTER_CTRL,
6074 THB1_EN, clk_priv->div - 1);
6075 break;
6076 case TX_SAMPL_CLK:
6077 if (clk_priv->mult != 1 || clk_priv->div > 4 ||
6078 clk_priv->div < 1 || clk_priv->div == 3)
6079 return -EINVAL;
6080
6081 if (clk_priv->phy->bypass_tx_fir)
6082 tmp = 0;
6083 else
6084 tmp = ilog2(clk_priv->div) + 1;
6085
6086 if (set)
6087 return ad9361_spi_writef(spi, REG_TX_ENABLE_FILTER_CTRL,
6088 TX_FIR_ENABLE_INTERPOLATION(~0), tmp);
6089 break;
6090 default:
6091 return -EINVAL;
6092 }
6093
6094 return 0;
6095 }
6096
6097 /**
6098 * Recalculate the clock rate.
6099 * @param refclk_scale The refclk_scale structure.
6100 * @param parent_rate The parent clock rate.
6101 * @return The clock rate.
6102 */
6103 uint32_t ad9361_clk_factor_recalc_rate(struct refclk_scale *clk_priv,
6104 uint32_t parent_rate)
6105 {
6106 uint64_t rate;
6107
6108 ad9361_get_clk_scaler(clk_priv);
6109 rate = (parent_rate * clk_priv->mult) / clk_priv->div;
6110
6111 return (uint32_t)rate;
6112 }
6113
6114 /**
6115 * Calculate the closest possible clock rate that can be set.
6116 * @param refclk_scale The refclk_scale structure.
6117 * @param rate The clock rate.
6118 * @param parent_rate The parent clock rate.
6119 * @return The closest possible clock rate that can be set.
6120 */
6121 int32_t ad9361_clk_factor_round_rate(struct refclk_scale *clk_priv, uint32_t rate,
6122 uint32_t *prate)
6123 {
6124 int32_t ret;
6125
6126 if (rate >= *prate) {
6127 clk_priv->mult = DIV_ROUND_CLOSEST(rate, *prate);
6128 clk_priv->div = 1;
6129
6130 }
6131 else {
6132 clk_priv->div = DIV_ROUND_CLOSEST(*prate, rate);
6133 clk_priv->mult = 1;
6134 if (!clk_priv->div) {
6135 dev_err(&clk_priv->spi->dev, "%s: divide by zero",
6136 __func__);
6137 clk_priv->div = 1;
6138 }
6139 }
6140
6141 ret = ad9361_set_clk_scaler(clk_priv, false);
6142 if (ret < 0)
6143 return ret;
6144
6145 return (*prate / clk_priv->div) * clk_priv->mult;
6146 }
6147
6148 /**
6149 * Set the clock rate.
6150 * @param refclk_scale The refclk_scale structure.
6151 * @param rate The clock rate.
6152 * @param parent_rate The parent clock rate.
6153 * @return 0 in case of success, negative error code otherwise.
6154 */
6155 int32_t ad9361_clk_factor_set_rate(struct refclk_scale *clk_priv, uint32_t rate,
6156 uint32_t parent_rate)
6157 {
6158 dev_dbg(&clk_priv->spi->dev, "%s: Rate %"PRIu32" Hz Parent Rate %"PRIu32" Hz",
6159 __func__, rate, parent_rate);
6160
6161 if (rate >= parent_rate) {
6162 clk_priv->mult = DIV_ROUND_CLOSEST(rate, parent_rate);
6163 clk_priv->div = 1;
6164 }
6165 else {
6166 clk_priv->div = DIV_ROUND_CLOSEST(parent_rate, rate);
6167 clk_priv->mult = 1;
6168 if (!clk_priv->div) {
6169 dev_err(&clk_priv->spi->dev, "%s: divide by zero",
6170 __func__);
6171 clk_priv->div = 1;
6172 }
6173 }
6174
6175 return ad9361_set_clk_scaler(clk_priv, true);
6176 }
6177
6178 /*
6179 * BBPLL
6180 */
6181 /**
6182 * Recalculate the clock rate.
6183 * @param refclk_scale The refclk_scale structure.
6184 * @param parent_rate The parent clock rate.
6185 * @return The clock rate.
6186 */
6187 uint32_t ad9361_bbpll_recalc_rate(struct refclk_scale *clk_priv,
6188 uint32_t parent_rate)
6189 {
6190 uint64_t rate;
6191 uint32_t fract, integer;
6192 uint8_t buf[4];
6193
6194 ad9361_spi_readm(clk_priv->spi, REG_INTEGER_BB_FREQ_WORD, &buf[0],
6195 REG_INTEGER_BB_FREQ_WORD - REG_FRACT_BB_FREQ_WORD_1 + 1);
6196
6197 fract = (buf[3] << 16) | (buf[2] << 8) | buf[1];
6198 integer = buf[0];
6199
6200 rate = ((uint64_t)parent_rate * fract);
6201 do_div(&rate, BBPLL_MODULUS);
6202 rate += (uint64_t)parent_rate * integer;
6203
6204 return (uint32_t)rate;
6205 }
6206
6207 /**
6208 * Calculate the closest possible clock rate that can be set.
6209 * @param refclk_scale The refclk_scale structure.
6210 * @param rate The clock rate.
6211 * @param parent_rate The parent clock rate.
6212 * @return The closest possible clock rate that can be set.
6213 */
6214 int32_t ad9361_bbpll_round_rate(struct refclk_scale *clk_priv, uint32_t rate,
6215 uint32_t *prate)
6216 {
6217 uint64_t tmp;
6218 uint32_t fract, integer;
6219 uint64_t temp;
6220
6221 if (clk_priv) {
6222 // Unused variable - fix compiler warning
6223 }
6224
6225 if (rate > MAX_BBPLL_FREQ)
6226 return MAX_BBPLL_FREQ;
6227
6228 if (rate < MIN_BBPLL_FREQ)
6229 return MIN_BBPLL_FREQ;
6230
6231 temp = rate;
6232 tmp = do_div(&temp, *prate);
6233 rate = temp;
6234 tmp = tmp * BBPLL_MODULUS + (*prate >> 1);
6235 do_div(&tmp, *prate);
6236
6237 integer = rate;
6238 fract = tmp;
6239
6240 tmp = *prate * (uint64_t)fract;
6241 do_div(&tmp, BBPLL_MODULUS);
6242 tmp += *prate * integer;
6243
6244 return tmp;
6245 }
6246
6247 /**
6248 * Set the clock rate.
6249 * @param refclk_scale The refclk_scale structure.
6250 * @param rate The clock rate.
6251 * @param parent_rate The parent clock rate.
6252 * @return 0 in case of success, negative error code otherwise.
6253 */
6254 int32_t ad9361_bbpll_set_rate(struct refclk_scale *clk_priv, uint32_t rate,
6255 uint32_t parent_rate)
6256 {
6257 struct spi_device *spi = clk_priv->spi;
6258 uint64_t tmp;
6259 uint32_t fract, integer;
6260 int32_t icp_val;
6261 uint8_t lf_defaults[3] = { 0x35, 0x5B, 0xE8 };
6262 uint64_t temp;
6263
6264 dev_dbg(&spi->dev, "%s: Rate %"PRIu32" Hz Parent Rate %"PRIu32" Hz",
6265 __func__, rate, parent_rate);
6266
6267 /*
6268 * Setup Loop Filter and CP Current
6269 * Scale is 150uA @ (1280MHz BBPLL, 40MHz REFCLK)
6270 */
6271 tmp = (rate >> 7) * 150ULL;
6272 do_div(&tmp, (parent_rate >> 7) * 32UL);
6273
6274 /* 25uA/LSB, Offset 25uA */
6275 icp_val = DIV_ROUND_CLOSEST((uint32_t)tmp, 25U) - 1;
6276
6277 icp_val = clamp(icp_val, 1, 64);
6278
6279 ad9361_spi_write(spi, REG_CP_CURRENT, icp_val);
6280 ad9361_spi_writem(spi, REG_LOOP_FILTER_3, lf_defaults,
6281 ARRAY_SIZE(lf_defaults));
6282
6283 /* Allow calibration to occur and set cal count to 1024 for max accuracy */
6284 ad9361_spi_write(spi, REG_VCO_CTRL,
6285 FREQ_CAL_ENABLE | FREQ_CAL_COUNT_LENGTH(3));
6286 /* Set calibration clock to REFCLK/4 for more accuracy */
6287 ad9361_spi_write(spi, REG_SDM_CTRL, 0x10);
6288
6289 /* Calculate and set BBPLL frequency word */
6290 temp = rate;
6291 tmp = do_div(&temp, parent_rate);
6292 rate = temp;
6293 tmp = tmp *(uint64_t)BBPLL_MODULUS + (parent_rate >> 1);
6294 do_div(&tmp, parent_rate);
6295
6296 integer = rate;
6297 fract = tmp;
6298
6299 ad9361_spi_write(spi, REG_INTEGER_BB_FREQ_WORD, integer);
6300 ad9361_spi_write(spi, REG_FRACT_BB_FREQ_WORD_3, fract);
6301 ad9361_spi_write(spi, REG_FRACT_BB_FREQ_WORD_2, fract >> 8);
6302 ad9361_spi_write(spi, REG_FRACT_BB_FREQ_WORD_1, fract >> 16);
6303
6304 ad9361_spi_write(spi, REG_SDM_CTRL_1, INIT_BB_FO_CAL | BBPLL_RESET_BAR); /* Start BBPLL Calibration */
6305 ad9361_spi_write(spi, REG_SDM_CTRL_1, BBPLL_RESET_BAR); /* Clear BBPLL start calibration bit */
6306
6307 ad9361_spi_write(spi, REG_VCO_PROGRAM_1, 0x86); /* Increase BBPLL KV and phase margin */
6308 ad9361_spi_write(spi, REG_VCO_PROGRAM_2, 0x01); /* Increase BBPLL KV and phase margin */
6309 ad9361_spi_write(spi, REG_VCO_PROGRAM_2, 0x05); /* Increase BBPLL KV and phase margin */
6310
6311 return ad9361_check_cal_done(clk_priv->phy, REG_CH_1_OVERFLOW,
6312 BBPLL_LOCK, 1);
6313 }
6314
6315 /*
6316 * RFPLL
6317 */
6318
6319 /**
6320 * Calculate the RFPLL frequency.
6321 * @param parent_rate The parent clock rate.
6322 * @param integer The integer value.
6323 * @param fract The fractional value.
6324 * @param vco_div The VCO divider.
6325 * @return The RFPLL frequency.
6326 */
6327 static uint64_t ad9361_calc_rfpll_int_freq(uint64_t parent_rate,
6328 uint64_t integer,
6329 uint64_t fract, uint32_t vco_div)
6330 {
6331 uint64_t rate;
6332
6333 rate = parent_rate * fract;
6334 do_div(&rate, RFPLL_MODULUS);
6335 rate += parent_rate * integer;
6336
6337 return rate >> (vco_div + 1);
6338 }
6339
6340 /**
6341 * Calculate the RFPLL dividers.
6342 * @param freq The RFPLL frequency.
6343 * @param parent_rate The parent clock rate.
6344 * @param integer The integer value.
6345 * @param fract The fractional value.
6346 * @param vco_div The VCO divider.
6347 * @param vco_freq The VCO frequency.
6348 * @return The RFPLL frequency.
6349 */
6350 static int32_t ad9361_calc_rfpll_int_divder(struct ad9361_rf_phy *phy,
6351 uint64_t freq, uint64_t parent_rate, uint32_t *integer,
6352 uint32_t *fract, int32_t *vco_div, uint64_t *vco_freq)
6353 {
6354 uint64_t tmp;
6355 int32_t div, ret;
6356
6357 ret = ad9361_validate_rfpll(phy, freq);
6358 if (ret)
6359 return ret;
6360
6361 div = -1;
6362
6363 while (freq <= MIN_VCO_FREQ_HZ) {
6364 freq <<= 1;
6365 div++;
6366 }
6367
6368 *vco_div = div;
6369 *vco_freq = freq;
6370 tmp = do_div(&freq, parent_rate);
6371 tmp = tmp * RFPLL_MODULUS + (parent_rate >> 1);
6372 do_div(&tmp, parent_rate);
6373 *integer = freq;
6374 *fract = tmp;
6375
6376 return 0;
6377 }
6378
6379 /**
6380 * Recalculate the clock rate.
6381 * @param refclk_scale The refclk_scale structure.
6382 * @param parent_rate The parent clock rate.
6383 * @return The clock rate.
6384 */
6385 uint32_t ad9361_rfpll_int_recalc_rate(struct refclk_scale *clk_priv,
6386 uint32_t parent_rate)
6387 {
6388 struct ad9361_rf_phy *phy = clk_priv->phy;
6389 uint32_t fract, integer;
6390 uint8_t buf[5];
6391 uint32_t reg, div_mask, vco_div, profile;
6392
6393 dev_dbg(&clk_priv->spi->dev, "%s: Parent Rate %"PRIu32" Hz",
6394 __func__, parent_rate);
6395
6396 switch (clk_priv->source) {
6397 case RX_RFPLL_INT:
6398 reg = REG_RX_FRACT_BYTE_2;
6399 div_mask = RX_VCO_DIVIDER(~0);
6400 profile = phy->fastlock.current_profile[0];
6401 break;
6402 case TX_RFPLL_INT:
6403 reg = REG_TX_FRACT_BYTE_2;
6404 div_mask = TX_VCO_DIVIDER(~0);
6405 profile = phy->fastlock.current_profile[1];
6406 break;
6407 default:
6408 return -EINVAL;
6409 }
6410
6411 if (profile) {
6412 bool tx = clk_priv->source == TX_RFPLL_INT;
6413 profile = profile - 1;
6414
6415 buf[0] = ad9361_fastlock_readval(phy->spi, tx, profile, 4);
6416 buf[1] = ad9361_fastlock_readval(phy->spi, tx, profile, 3);
6417 buf[2] = ad9361_fastlock_readval(phy->spi, tx, profile, 2);
6418 buf[3] = ad9361_fastlock_readval(phy->spi, tx, profile, 1);
6419 buf[4] = ad9361_fastlock_readval(phy->spi, tx, profile, 0);
6420 vco_div = ad9361_fastlock_readval(phy->spi, tx, profile, 12) & 0xF;
6421
6422 }
6423 else {
6424 ad9361_spi_readm(clk_priv->spi, reg, &buf[0], ARRAY_SIZE(buf));
6425 vco_div = ad9361_spi_readf(clk_priv->spi, REG_RFPLL_DIVIDERS, div_mask);
6426 }
6427
6428 fract = (SYNTH_FRACT_WORD(buf[0]) << 16) | (buf[1] << 8) | buf[2];
6429 integer = (SYNTH_INTEGER_WORD(buf[3]) << 8) | buf[4];
6430
6431 return ad9361_to_clk(ad9361_calc_rfpll_int_freq(parent_rate, integer,
6432 fract, vco_div));
6433 }
6434
6435 /**
6436 * Calculate the closest possible clock rate that can be set.
6437 * @param refclk_scale The refclk_scale structure.
6438 * @param rate The clock rate.
6439 * @param parent_rate The parent clock rate.
6440 * @return The closest possible clock rate that can be set.
6441 */
6442 int32_t ad9361_rfpll_int_round_rate(struct refclk_scale *clk_priv, uint32_t rate,
6443 uint32_t *prate)
6444 {
6445 dev_dbg(&clk_priv->spi->dev, "%s: Rate %"PRIu32" Hz", __func__, rate);
6446
6447 if (prate) {
6448 // Unused variable - fix compiler warning
6449 }
6450
6451 if (clk_priv) {
6452 // Unused variable - fix compiler warning
6453 }
6454
6455 if (ad9361_from_clk(rate) > MAX_CARRIER_FREQ_HZ ||
6456 ad9361_from_clk(rate) < MIN_CARRIER_FREQ_HZ)
6457 return -EINVAL;
6458
6459 return rate;
6460 }
6461
6462 /**
6463 * Set the clock rate.
6464 * @param refclk_scale The refclk_scale structure.
6465 * @param rate The clock rate.
6466 * @param parent_rate The parent clock rate.
6467 * @return 0 in case of success, negative error code otherwise.
6468 */
6469 int32_t ad9361_rfpll_int_set_rate(struct refclk_scale *clk_priv, uint32_t rate,
6470 uint32_t parent_rate)
6471 {
6472 struct ad9361_rf_phy *phy = clk_priv->phy;
6473 uint64_t vco = 0;
6474 uint8_t buf[5];
6475 uint32_t reg, div_mask, lock_reg, fract = 0, integer = 0;
6476 int32_t vco_div, ret, fixup_other;
6477
6478 dev_dbg(&clk_priv->spi->dev, "%s: %s Rate %"PRIu32" Hz Parent Rate %"PRIu32" Hz",
6479 __func__, clk_priv->source == TX_RFPLL_INT ? "TX" : "RX",
6480 rate, parent_rate);
6481
6482 ad9361_fastlock_prepare(phy, clk_priv->source == TX_RFPLL_INT, 0, false);
6483
6484 ret = ad9361_calc_rfpll_int_divder(phy, ad9361_from_clk(rate), parent_rate,
6485 &integer, &fract, &vco_div, &vco);
6486 if (ret < 0)
6487 return ret;
6488
6489 switch (clk_priv->source) {
6490 case RX_RFPLL_INT:
6491 reg = REG_RX_FRACT_BYTE_2;
6492 lock_reg = REG_RX_CP_OVERRANGE_VCO_LOCK;
6493 div_mask = RX_VCO_DIVIDER(~0);
6494 phy->cached_rx_rfpll_div = vco_div;
6495 phy->current_rx_lo_freq = rate;
6496 break;
6497 case TX_RFPLL_INT:
6498 reg = REG_TX_FRACT_BYTE_2;
6499 lock_reg = REG_TX_CP_OVERRANGE_VCO_LOCK;
6500 div_mask = TX_VCO_DIVIDER(~0);
6501 phy->cached_tx_rfpll_div = vco_div;
6502 phy->current_tx_lo_freq = rate;
6503 break;
6504 default:
6505 return -EINVAL;
6506
6507 }
6508
6509 /* Option to skip VCO cal in TDD mode when moving from TX/RX to Alert */
6510 if (phy->pdata->tdd_skip_vco_cal)
6511 ad9361_trx_vco_cal_control(phy, clk_priv->source == TX_RFPLL_INT,
6512 true);
6513
6514 do {
6515 fixup_other = 0;
6516 ad9361_rfpll_vco_init(phy, div_mask == TX_VCO_DIVIDER(~0),
6517 vco, parent_rate);
6518
6519 buf[0] = SYNTH_FRACT_WORD(fract >> 16);
6520 buf[1] = fract >> 8;
6521 buf[2] = fract & 0xFF;
6522 buf[3] = SYNTH_INTEGER_WORD(integer >> 8) |
6523 (~SYNTH_INTEGER_WORD(~0) &
6524 ad9361_spi_read(clk_priv->spi, reg - 3));
6525 buf[4] = integer & 0xFF;
6526
6527 ad9361_spi_writem(clk_priv->spi, reg, buf, 5);
6528 ad9361_spi_writef(clk_priv->spi, REG_RFPLL_DIVIDERS, div_mask, vco_div);
6529
6530 ret = ad9361_check_cal_done(phy, lock_reg, VCO_LOCK, 1);
6531
6532 /* In FDD mode with RX LO == TX LO frequency we use TDD tables to
6533 * reduce VCO pulling
6534 */
6535
6536 if (((phy->pdata->fdd && !phy->pdata->fdd_independent_mode) &&
6537 (phy->current_tx_lo_freq == phy->current_rx_lo_freq) &&
6538 (phy->current_tx_use_tdd_table != phy->current_rx_use_tdd_table)) ||
6539 ((phy->pdata->fdd && !phy->pdata->fdd_independent_mode) &&
6540 (phy->current_tx_lo_freq != phy->current_rx_lo_freq) &&
6541 (phy->current_tx_use_tdd_table || phy->current_rx_use_tdd_table))) {
6542 unsigned long _rate;
6543
6544 switch (clk_priv->source) {
6545 case RX_RFPLL_INT:
6546 reg = REG_TX_FRACT_BYTE_2;
6547 lock_reg = REG_TX_CP_OVERRANGE_VCO_LOCK;
6548 div_mask = TX_VCO_DIVIDER(~0);
6549 _rate = phy->current_tx_lo_freq;
6550 break;
6551 case TX_RFPLL_INT:
6552 reg = REG_RX_FRACT_BYTE_2;
6553 lock_reg = REG_RX_CP_OVERRANGE_VCO_LOCK;
6554 div_mask = RX_VCO_DIVIDER(~0);
6555 _rate = phy->current_rx_lo_freq;
6556 break;
6557 default:
6558 return -EINVAL;
6559
6560 }
6561
6562 if (phy->current_tx_lo_freq != phy->current_rx_lo_freq) {
6563 ad9361_calc_rfpll_int_divder(phy, ad9361_from_clk(_rate),
6564 parent_rate, &integer, &fract, &vco_div, &vco);
6565
6566 ad9361_fastlock_prepare(phy, clk_priv->source == RX_RFPLL_INT, 0, false);
6567 }
6568
6569 fixup_other = 1;
6570 }
6571
6572 } while (fixup_other);
6573
6574 if (phy->pdata->tdd_skip_vco_cal)
6575 ad9361_trx_vco_cal_control(phy, clk_priv->source == TX_RFPLL_INT,
6576 false);
6577
6578 return ret;
6579 }
6580
6581 /**
6582 * Recalculate the clock rate.
6583 * @param refclk_scale The refclk_scale structure.
6584 * @param parent_rate The parent clock rate.
6585 * @return The clock rate.
6586 */
6587 uint32_t ad9361_rfpll_dummy_recalc_rate(struct refclk_scale *clk_priv)
6588 {
6589 struct ad9361_rf_phy *phy = clk_priv->phy;
6590
6591 return phy->clks[clk_priv->source]->rate;
6592 }
6593
6594 /**
6595 * Set the clock rate.
6596 * @param refclk_scale The refclk_scale structure.
6597 * @param rate The clock rate.
6598 * @param parent_rate The parent clock rate.
6599 * @return 0 in case of success, negative error code otherwise.
6600 */
6601 int32_t ad9361_rfpll_dummy_set_rate(struct refclk_scale *clk_priv, uint32_t rate)
6602 {
6603 struct ad9361_rf_phy *phy = clk_priv->phy;
6604
6605 phy->clks[clk_priv->source]->rate = rate;
6606
6607 return 0;
6608 }
6609
6610 /**
6611 * Recalculate the clock rate.
6612 * @param refclk_scale The refclk_scale structure.
6613 * @param parent_rate The parent clock rate.
6614 * @return The clock rate.
6615 */
6616 uint32_t ad9361_rfpll_recalc_rate(struct refclk_scale *clk_priv)
6617 {
6618 struct ad9361_rf_phy *phy = clk_priv->phy;
6619 uint32_t rate;
6620
6621 switch (clk_priv->source) {
6622 case RX_RFPLL:
6623 if (phy->pdata->use_ext_rx_lo) {
6624 if (phy->ad9361_rfpll_ext_recalc_rate)
6625 rate = phy->ad9361_rfpll_ext_recalc_rate(clk_priv);
6626 else
6627 rate = ad9361_rfpll_dummy_recalc_rate(phy->ref_clk_scale[RX_RFPLL_DUMMY]);
6628 } else {
6629 rate = ad9361_rfpll_int_recalc_rate(phy->ref_clk_scale[RX_RFPLL_INT],
6630 phy->clks[RX_REFCLK]->rate);
6631 }
6632 break;
6633 case TX_RFPLL:
6634 if (phy->pdata->use_ext_tx_lo) {
6635 if (phy->ad9361_rfpll_ext_recalc_rate)
6636 rate = phy->ad9361_rfpll_ext_recalc_rate(clk_priv);
6637 else
6638 rate = ad9361_rfpll_dummy_recalc_rate(phy->ref_clk_scale[TX_RFPLL_DUMMY]);
6639 } else {
6640 rate = ad9361_rfpll_int_recalc_rate(phy->ref_clk_scale[TX_RFPLL_INT],
6641 phy->clks[TX_REFCLK]->rate);
6642 }
6643 break;
6644 default:
6645 rate = 0;
6646 break;
6647 }
6648
6649 return rate;
6650 }
6651
6652 /**
6653 * Calculate the closest possible clock rate that can be set.
6654 * @param refclk_scale The refclk_scale structure.
6655 * @param rate The clock rate.
6656 * @param parent_rate The parent clock rate.
6657 * @return The closest possible clock rate that can be set.
6658 */
6659 int32_t ad9361_rfpll_round_rate(struct refclk_scale *clk_priv, uint32_t rate)
6660 {
6661 struct ad9361_rf_phy *phy = clk_priv->phy;
6662 int32_t round_rate;
6663
6664 switch (clk_priv->source) {
6665 case RX_RFPLL:
6666 if (phy->pdata->use_ext_rx_lo) {
6667 if (phy->ad9361_rfpll_ext_round_rate)
6668 round_rate = phy->ad9361_rfpll_ext_round_rate(clk_priv, rate);
6669 else
6670 round_rate = rate;
6671 } else {
6672 round_rate = ad9361_rfpll_int_round_rate(phy->ref_clk_scale[RX_RFPLL_INT], rate,
6673 &phy->clks[phy->ref_clk_scale[RX_RFPLL_INT]->parent_source]->rate);
6674 }
6675 case TX_RFPLL:
6676 if (phy->pdata->use_ext_tx_lo) {
6677 if (phy->ad9361_rfpll_ext_round_rate)
6678 round_rate = phy->ad9361_rfpll_ext_round_rate(clk_priv, rate);
6679 else
6680 round_rate = rate;
6681 } else {
6682 round_rate = ad9361_rfpll_int_round_rate(phy->ref_clk_scale[TX_RFPLL_INT], rate,
6683 &phy->clks[phy->ref_clk_scale[TX_RFPLL_INT]->parent_source]->rate);
6684 }
6685 break;
6686 default:
6687 round_rate = 0;
6688 break;
6689 }
6690
6691 return round_rate;
6692 }
6693
6694 /**
6695 * Set the clock rate.
6696 * @param refclk_scale The refclk_scale structure.
6697 * @param rate The clock rate.
6698 * @param parent_rate The parent clock rate.
6699 * @return 0 in case of success, negative error code otherwise.
6700 */
6701 int32_t ad9361_rfpll_set_rate(struct refclk_scale *clk_priv, uint32_t rate)
6702 {
6703 struct ad9361_rf_phy *phy = clk_priv->phy;
6704 int32_t ret;
6705
6706 switch (clk_priv->source) {
6707 case RX_RFPLL:
6708 if (phy->pdata->use_ext_rx_lo) {
6709 if (phy->ad9361_rfpll_ext_set_rate)
6710 phy->ad9361_rfpll_ext_set_rate(clk_priv, rate);
6711 else
6712 ad9361_rfpll_dummy_set_rate(phy->ref_clk_scale[RX_RFPLL_DUMMY], rate);
6713 } else {
6714 ad9361_rfpll_int_set_rate(phy->ref_clk_scale[RX_RFPLL_INT], rate,
6715 phy->clks[phy->ref_clk_scale[RX_RFPLL_INT]->parent_source]->rate);
6716 }
6717 /* Load Gain Table */
6718 ret = ad9361_load_gt(phy, ad9361_from_clk(rate), GT_RX1 + GT_RX2);
6719 if (ret < 0)
6720 return ret;
6721 break;
6722 case TX_RFPLL:
6723 if (phy->pdata->use_ext_tx_lo) {
6724 if (phy->ad9361_rfpll_ext_set_rate)
6725 phy->ad9361_rfpll_ext_set_rate(clk_priv, rate);
6726 else
6727 ad9361_rfpll_dummy_set_rate(phy->ref_clk_scale[TX_RFPLL_DUMMY], rate);
6728 } else {
6729 ad9361_rfpll_int_set_rate(phy->ref_clk_scale[TX_RFPLL_INT], rate,
6730 phy->clks[phy->ref_clk_scale[TX_RFPLL_INT]->parent_source]->rate);
6731 }
6732 /* For RX LO we typically have the tracking option enabled
6733 * so for now do nothing here.
6734 */
6735 if (phy->auto_cal_en && (clk_priv->source == TX_RFPLL_INT))
6736 if (abs((int64_t)(phy->last_tx_quad_cal_freq - ad9361_from_clk(rate))) >
6737 (int64_t)phy->cal_threshold_freq) {
6738 ret = ad9361_do_calib_run(phy, TX_QUAD_CAL, -1);
6739 if (ret < 0)
6740 dev_err(&phy->spi->dev,
6741 "%s: TX QUAD cal failed", __func__);
6742 phy->last_tx_quad_cal_freq = ad9361_from_clk(rate);
6743 }
6744 break;
6745 default:
6746 break;
6747 }
6748
6749 return 0;
6750 }
6751
6752 /**
6753 * Set clock mux parent.
6754 * @param refclk_scale The refclk_scale structure.
6755 * @param index Index - Enable (1), disable (0) ext lo.
6756 * @return 0 in case of success, negative error code otherwise.
6757 */
6758 int32_t ad9361_clk_mux_set_parent(struct refclk_scale *clk_priv, uint8_t index)
6759 {
6760 struct ad9361_rf_phy *phy = clk_priv->phy;
6761 int32_t ret;
6762
6763 dev_dbg(&clk_priv->spi->dev, "%s: index %d", __func__, index);
6764
6765 ad9361_ensm_force_state(phy, ENSM_STATE_ALERT);
6766
6767 ret = ad9361_trx_ext_lo_control(phy, clk_priv->source == TX_RFPLL, index == 1);
6768 if (ret >= 0)
6769 clk_priv->mult = index;
6770
6771 ad9361_ensm_restore_prev_state(phy);
6772
6773 return ret;
6774 }
6775
6776 /**
6777 * Register and initialize a new clock.
6778 * @param phy The AD9361 state structure.
6779 * @param name The name of the new clock.
6780 * @param parent_name The name of the parent clock.
6781 * @param flags The flags.
6782 * @param source The source of the new clock.
6783 * @param parent_source The source of the parent clock.
6784 * @return A struct clk for the new clock or a negative error code.
6785 */
6786 static struct clk *ad9361_clk_register(struct ad9361_rf_phy *phy, const char *name,
6787 const char *parent_name, uint32_t flags,
6788 uint32_t source, uint32_t parent_source)
6789 {
6790 struct refclk_scale *clk_priv;
6791 struct clk *clk;
6792
6793 if (name) {
6794 // Unused variable - fix compiler warning
6795 }
6796 if (parent_name) {
6797 // Unused variable - fix compiler warning
6798 }
6799 if (flags) {
6800 // Unused variable - fix compiler warning
6801 }
6802
6803 clk_priv = (struct refclk_scale *)malloc(sizeof(*clk_priv));
6804 if (!clk_priv) {
6805 dev_err(&phy->spi->dev, "ad9361_clk_register: could not allocate fixed factor clk");
6806 return (struct clk *)ERR_PTR(-ENOMEM);
6807 }
6808
6809 /* struct refclk_scale assignments */
6810 clk_priv->source = (enum ad9361_clocks)source;
6811 clk_priv->parent_source = (enum ad9361_clocks)parent_source;
6812 clk_priv->spi = phy->spi;
6813 clk_priv->phy = phy;
6814
6815 phy->ref_clk_scale[source] = clk_priv;
6816
6817 clk = (struct clk *)malloc(sizeof(*clk));
6818 if (!clk) {
6819 free(clk_priv);
6820 return (struct clk *)ERR_PTR(-ENOMEM);
6821 }
6822
6823 switch (source) {
6824 case TX_REFCLK:
6825 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clk_refin->rate);
6826 break;
6827 case RX_REFCLK:
6828 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clk_refin->rate);
6829 break;
6830 case BB_REFCLK:
6831 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clk_refin->rate);
6832 break;
6833 case BBPLL_CLK:
6834 clk->rate = ad9361_bbpll_recalc_rate(clk_priv, phy->clks[BB_REFCLK]->rate);
6835 break;
6836 case ADC_CLK:
6837 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[BBPLL_CLK]->rate);
6838 break;
6839 case R2_CLK:
6840 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[ADC_CLK]->rate);
6841 break;
6842 case R1_CLK:
6843 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[R2_CLK]->rate);
6844 break;
6845 case CLKRF_CLK:
6846 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[R1_CLK]->rate);
6847 break;
6848 case RX_SAMPL_CLK:
6849 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[CLKRF_CLK]->rate);
6850 break;
6851 case DAC_CLK:
6852 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[ADC_CLK]->rate);
6853 break;
6854 case T2_CLK:
6855 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[DAC_CLK]->rate);
6856 break;
6857 case T1_CLK:
6858 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[T2_CLK]->rate);
6859 break;
6860 case CLKTF_CLK:
6861 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[T1_CLK]->rate);
6862 break;
6863 case TX_SAMPL_CLK:
6864 clk->rate = ad9361_clk_factor_recalc_rate(clk_priv, phy->clks[CLKTF_CLK]->rate);
6865 break;
6866 case RX_RFPLL_INT:
6867 clk->rate = ad9361_rfpll_int_recalc_rate(clk_priv, phy->clks[RX_REFCLK]->rate);
6868 break;
6869 case TX_RFPLL_INT:
6870 clk->rate = ad9361_rfpll_int_recalc_rate(clk_priv, phy->clks[TX_REFCLK]->rate);
6871 break;
6872 case RX_RFPLL_DUMMY:
6873 clk->rate = phy->pdata->rx_synth_freq;
6874 break;
6875 case TX_RFPLL_DUMMY:
6876 clk->rate = phy->pdata->tx_synth_freq;
6877 break;
6878 case RX_RFPLL:
6879 clk->rate = ad9361_rfpll_recalc_rate(clk_priv);
6880 break;
6881 case TX_RFPLL:
6882 clk->rate = ad9361_rfpll_recalc_rate(clk_priv);
6883 }
6884
6885 return clk;
6886 }
6887
6888 /**
6889 * Register and initialize all the system clocks.
6890 * @param phy The AD9361 state structure.
6891 * @return 0 in case of success, negative error code otherwise.
6892 */
6893 int32_t register_clocks(struct ad9361_rf_phy *phy)
6894 {
6895 uint32_t flags = CLK_GET_RATE_NOCACHE;
6896
6897 phy->clk_data.clks = (struct clk **)malloc(sizeof(*phy->clk_data.clks) *
6898 NUM_AD9361_CLKS);
6899 if (!phy->clk_data.clks) {
6900 dev_err(&phy->spi->dev, "could not allocate memory");
6901 return -ENOMEM;
6902 }
6903
6904 phy->clk_data.clk_num = NUM_AD9361_CLKS;
6905
6906 /* Scaled Reference Clocks */
6907 phy->clks[TX_REFCLK] = ad9361_clk_register(phy,
6908 "tx_refclk", "ad9361_ext_refclk",
6909 flags | CLK_IGNORE_UNUSED,
6910 TX_REFCLK, EXT_REF_CLK);
6911
6912 phy->clks[RX_REFCLK] = ad9361_clk_register(phy,
6913 "rx_refclk", "ad9361_ext_refclk",
6914 flags | CLK_IGNORE_UNUSED,
6915 RX_REFCLK, EXT_REF_CLK);
6916
6917 phy->clks[BB_REFCLK] = ad9361_clk_register(phy,
6918 "bb_refclk", "ad9361_ext_refclk",
6919 flags | CLK_IGNORE_UNUSED,
6920 BB_REFCLK, EXT_REF_CLK);
6921
6922 /* Base Band PLL Clock */
6923 phy->clks[BBPLL_CLK] = ad9361_clk_register(phy,
6924 "bbpll_clk", "bb_refclk",
6925 flags | CLK_IGNORE_UNUSED,
6926 BBPLL_CLK, BB_REFCLK);
6927
6928 phy->clks[ADC_CLK] = ad9361_clk_register(phy,
6929 "adc_clk", "bbpll_clk",
6930 flags | CLK_IGNORE_UNUSED,
6931 ADC_CLK, BBPLL_CLK);
6932
6933 phy->clks[R2_CLK] = ad9361_clk_register(phy,
6934 "r2_clk", "adc_clk",
6935 flags | CLK_IGNORE_UNUSED,
6936 R2_CLK, ADC_CLK);
6937
6938 phy->clks[R1_CLK] = ad9361_clk_register(phy,
6939 "r1_clk", "r2_clk",
6940 flags | CLK_IGNORE_UNUSED,
6941 R1_CLK, R2_CLK);
6942
6943 phy->clks[CLKRF_CLK] = ad9361_clk_register(phy,
6944 "clkrf_clk", "r1_clk",
6945 flags | CLK_IGNORE_UNUSED,
6946 CLKRF_CLK, R1_CLK);
6947
6948 phy->clks[RX_SAMPL_CLK] = ad9361_clk_register(phy,
6949 "rx_sampl_clk", "clkrf_clk",
6950 flags | CLK_IGNORE_UNUSED,
6951 RX_SAMPL_CLK, CLKRF_CLK);
6952
6953
6954 phy->clks[DAC_CLK] = ad9361_clk_register(phy,
6955 "dac_clk", "adc_clk",
6956 flags | CLK_IGNORE_UNUSED,
6957 DAC_CLK, ADC_CLK);
6958
6959 phy->clks[T2_CLK] = ad9361_clk_register(phy,
6960 "t2_clk", "dac_clk",
6961 flags | CLK_IGNORE_UNUSED,
6962 T2_CLK, DAC_CLK);
6963
6964 phy->clks[T1_CLK] = ad9361_clk_register(phy,
6965 "t1_clk", "t2_clk",
6966 flags | CLK_IGNORE_UNUSED,
6967 T1_CLK, T2_CLK);
6968
6969 phy->clks[CLKTF_CLK] = ad9361_clk_register(phy,
6970 "clktf_clk", "t1_clk",
6971 flags | CLK_IGNORE_UNUSED,
6972 CLKTF_CLK, T1_CLK);
6973
6974 phy->clks[TX_SAMPL_CLK] = ad9361_clk_register(phy,
6975 "tx_sampl_clk", "clktf_clk",
6976 flags | CLK_IGNORE_UNUSED,
6977 TX_SAMPL_CLK, CLKTF_CLK);
6978
6979 phy->clks[RX_RFPLL_INT] = ad9361_clk_register(phy,
6980 "rx_rfpll", "rx_refclk",
6981 flags | CLK_IGNORE_UNUSED,
6982 RX_RFPLL_INT, RX_REFCLK);
6983
6984 phy->clks[TX_RFPLL_INT] = ad9361_clk_register(phy,
6985 "tx_rfpll", "tx_refclk",
6986 flags | CLK_IGNORE_UNUSED,
6987 TX_RFPLL_INT, TX_REFCLK);
6988
6989 phy->clks[RX_RFPLL_DUMMY] = ad9361_clk_register(phy,
6990 "rx_rfpll_dummy", NULL,
6991 flags | CLK_IGNORE_UNUSED,
6992 RX_RFPLL_DUMMY, 0);
6993
6994 phy->clks[TX_RFPLL_DUMMY] = ad9361_clk_register(phy,
6995 "tx_rfpll_dummy", NULL,
6996 flags | CLK_IGNORE_UNUSED,
6997 TX_RFPLL_DUMMY, 0);
6998
6999 phy->clks[RX_RFPLL] = ad9361_clk_register(phy,
7000 "rx_rfpll", NULL,
7001 flags | CLK_IGNORE_UNUSED,
7002 RX_RFPLL, 0);
7003
7004 phy->clks[TX_RFPLL] = ad9361_clk_register(phy,
7005 "tx_rfpll", NULL,
7006 flags | CLK_IGNORE_UNUSED,
7007 TX_RFPLL, 0);
7008
7009 return 0;
7010 }
7011
7012 /**
7013 * Perform an RSSI gain step calibration.
7014 * Note: Before running the function, provide a single tone within the channel
7015 * bandwidth and monitor the received data. Adjust the tone amplitude until
7016 * the received data is within a few dB of full scale but not overloading.
7017 * @param phy The AD9361 state structure.
7018 * @return 0 in case of success, negative error code otherwise.
7019 */
7020 int32_t ad9361_rssi_gain_step_calib(struct ad9361_rf_phy *phy)
7021 {
7022 uint32_t lna_error[4];
7023 uint32_t mixer_error[15];
7024 uint64_t lo_freq_hz;
7025 uint8_t lo_index;
7026 uint8_t i;
7027
7028 lo_freq_hz = ad9361_from_clk(clk_get_rate(phy,
7029 phy->ref_clk_scale[RX_RFPLL]));
7030 if (lo_freq_hz < 1300000000ULL)
7031 lo_index = 0;
7032 else
7033 if (lo_freq_hz < 3300000000ULL)
7034 lo_index = 1;
7035 else
7036 if (lo_freq_hz < 4100000000ULL)
7037 lo_index = 2;
7038 else
7039 lo_index = 3;
7040
7041 /* Put the AD9361 into the Alert state. */
7042 ad9361_ensm_force_state(phy, ENSM_STATE_ALERT);
7043
7044 /* Program the directly-addressable register values. */
7045 ad9361_spi_write(phy->spi, REG_MAX_MIXER_CALIBRATION_GAIN_INDEX,
7046 MAX_MIXER_CALIBRATION_GAIN_INDEX(0x0F));
7047 ad9361_spi_write(phy->spi, REG_MEASURE_DURATION,
7048 GAIN_CAL_MEAS_DURATION(0x0E));
7049 ad9361_spi_write(phy->spi, REG_SETTLE_TIME,
7050 SETTLE_TIME(0x3F));
7051 ad9361_spi_write(phy->spi, REG_RSSI_CONFIG,
7052 RSSI_MODE_SELECT(0x3) | DEFAULT_RSSI_MEAS_MODE);
7053 ad9361_spi_write(phy->spi, REG_MEASURE_DURATION_01,
7054 MEASUREMENT_DURATION_0(0x0E));
7055 ad9361_spi_write(phy->spi, REG_LNA_GAIN,
7056 gain_step_calib_reg_val[lo_index][0]);
7057
7058 /* Program the LNA gain step words into the internal table. */
7059 ad9361_spi_write(phy->spi, REG_CONFIG,
7060 CALIB_TABLE_SELECT(0x3) | START_CALIB_TABLE_CLOCK);
7061 for(i = 0; i < 4; i++) {
7062 ad9361_spi_write(phy->spi, REG_WORD_ADDRESS, i);
7063 ad9361_spi_write(phy->spi, REG_GAIN_DIFF_WORDERROR_WRITE,
7064 gain_step_calib_reg_val[lo_index][i+1]);
7065 ad9361_spi_write(phy->spi, REG_CONFIG,
7066 CALIB_TABLE_SELECT(0x3) | WRITE_LNA_GAIN_DIFF | START_CALIB_TABLE_CLOCK);
7067 udelay(3); //Wait for data to fully write to internal table
7068 }
7069
7070 ad9361_spi_write(phy->spi, REG_CONFIG, START_CALIB_TABLE_CLOCK);
7071 ad9361_spi_write(phy->spi, REG_CONFIG, 0x00);
7072
7073 /* Run and wait until the calibration completes. */
7074 ad9361_run_calibration(phy, RX_GAIN_STEP_CAL);
7075
7076 /* Read the LNA and Mixer error terms into nonvolatile memory. */
7077 ad9361_spi_write(phy->spi, REG_CONFIG, CALIB_TABLE_SELECT(0x1) | READ_SELECT);
7078 for(i = 0; i < 4; i++) {
7079 ad9361_spi_write(phy->spi, REG_WORD_ADDRESS, i);
7080 lna_error[i] = ad9361_spi_read(phy->spi, REG_GAIN_ERROR_READ);
7081 }
7082 ad9361_spi_write(phy->spi, REG_CONFIG, CALIB_TABLE_SELECT(0x1));
7083 for(i = 0; i < 15; i++) {
7084 ad9361_spi_write(phy->spi, REG_WORD_ADDRESS, i);
7085 mixer_error[i] = ad9361_spi_read(phy->spi, REG_GAIN_ERROR_READ);
7086 }
7087 ad9361_spi_write(phy->spi, REG_CONFIG, 0x00);
7088
7089 /* Programming gain step errors into the AD9361 in the field */
7090 ad9361_spi_write(phy->spi,
7091 REG_CONFIG, CALIB_TABLE_SELECT(0x3) | START_CALIB_TABLE_CLOCK);
7092 for(i = 0; i < 4; i++) {
7093 ad9361_spi_write(phy->spi, REG_WORD_ADDRESS, i);
7094 ad9361_spi_write(phy->spi, REG_GAIN_DIFF_WORDERROR_WRITE, lna_error[i]);
7095 ad9361_spi_write(phy->spi, REG_CONFIG,
7096 CALIB_TABLE_SELECT(0x3) | WRITE_LNA_ERROR_TABLE | START_CALIB_TABLE_CLOCK);
7097 }
7098 ad9361_spi_write(phy->spi, REG_CONFIG,
7099 CALIB_TABLE_SELECT(0x3) | START_CALIB_TABLE_CLOCK);
7100 for(i = 0; i < 15; i++) {
7101 ad9361_spi_write(phy->spi, REG_WORD_ADDRESS, i);
7102 ad9361_spi_write(phy->spi, REG_GAIN_DIFF_WORDERROR_WRITE, mixer_error[i]);
7103 ad9361_spi_write(phy->spi, REG_CONFIG,
7104 CALIB_TABLE_SELECT(0x3) | WRITE_MIXER_ERROR_TABLE | START_CALIB_TABLE_CLOCK);
7105 }
7106 ad9361_spi_write(phy->spi, REG_CONFIG, 0x00);
7107
7108 ad9361_ensm_restore_prev_state(phy);
7109
7110 return 0;
7111 }