Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad9361_api.c
3 * @brief Implementation of AD9361 API Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 /******************************************************************************/
41 /***************************** Include Files **********************************/
42 /******************************************************************************/
43 #include "ad9361.h"
44 #include "ad9361_api.h"
45 #include "platform.h"
46 #include "util.h"
47 #include "config.h"
48 #include <string.h>
49
50 #ifndef AXI_ADC_NOT_PRESENT
51 /******************************************************************************/
52 /************************ Constants Definitions *******************************/
53 /******************************************************************************/
54 static struct axiadc_chip_info axiadc_chip_info_tbl[] =
55 {
56 {
57 "4_CH_DEV",
58 4
59 },
60 {
61 "2_CH_DEV",
62 2
63 },
64 };
65 #endif
66
67 /**
68 * Initialize the AD9361 part.
69 * @param init_param The structure that contains the AD9361 initial parameters.
70 * @return A structure that contains the AD9361 current state in case of
71 * success, negative error code otherwise.
72 *
73 * Note: This function will/may affect the data path.
74 */
75 int32_t ad9361_init (struct ad9361_rf_phy **ad9361_phy, AD9361_InitParam *init_param)
76 {
77 struct ad9361_rf_phy *phy;
78 int32_t ret = 0;
79 int32_t rev = 0;
80 int32_t i = 0;
81
82 phy = (struct ad9361_rf_phy *)zmalloc(sizeof(*phy));
83 if (!phy) {
84 return -ENOMEM;
85 }
86
87 phy->spi = (struct spi_device *)zmalloc(sizeof(*phy->spi));
88 if (!phy->spi) {
89 return -ENOMEM;
90 }
91
92 phy->clk_refin = (struct clk *)zmalloc(sizeof(*phy->clk_refin));
93 if (!phy->clk_refin) {
94 return -ENOMEM;
95 }
96
97 phy->pdata = (struct ad9361_phy_platform_data *)zmalloc(sizeof(*phy->pdata));
98 if (!phy->pdata) {
99 return -ENOMEM;
100 }
101 #ifndef AXI_ADC_NOT_PRESENT
102 phy->adc_conv = (struct axiadc_converter *)zmalloc(sizeof(*phy->adc_conv));
103 if (!phy->adc_conv) {
104 return -ENOMEM;
105 }
106
107 phy->adc_state = (struct axiadc_state *)zmalloc(sizeof(*phy->adc_state));
108 if (!phy->adc_state) {
109 return -ENOMEM;
110 }
111 phy->adc_state->phy = phy;
112 #endif
113
114 /* Device selection */
115 phy->dev_sel = init_param->dev_sel;
116
117 /* Identification number */
118 phy->spi->id_no = init_param->id_no;
119 phy->id_no = init_param->id_no;
120
121 /* Reference Clock */
122 phy->clk_refin->rate = init_param->reference_clk_rate;
123
124 /* Base Configuration */
125 phy->pdata->fdd = init_param->frequency_division_duplex_mode_enable;
126 phy->pdata->fdd_independent_mode = init_param->frequency_division_duplex_independent_mode_enable;
127 phy->pdata->rx2tx2 = init_param->two_rx_two_tx_mode_enable;
128 phy->pdata->rx1tx1_mode_use_rx_num = init_param->one_rx_one_tx_mode_use_rx_num;
129 phy->pdata->rx1tx1_mode_use_tx_num = init_param->one_rx_one_tx_mode_use_tx_num;
130 phy->pdata->tdd_use_dual_synth = init_param->tdd_use_dual_synth_mode_enable;
131 phy->pdata->tdd_skip_vco_cal = init_param->tdd_skip_vco_cal_enable;
132 phy->pdata->rx_fastlock_delay_ns = init_param->rx_fastlock_delay_ns;
133 phy->pdata->tx_fastlock_delay_ns = init_param->tx_fastlock_delay_ns;
134 phy->pdata->trx_fastlock_pinctrl_en[0] = init_param->rx_fastlock_pincontrol_enable;
135 phy->pdata->trx_fastlock_pinctrl_en[1] = init_param->tx_fastlock_pincontrol_enable;
136 if (phy->dev_sel == ID_AD9363A) {
137 phy->pdata->use_ext_rx_lo = false;
138 phy->pdata->use_ext_tx_lo = false;
139 } else {
140 phy->pdata->use_ext_rx_lo = init_param->external_rx_lo_enable;
141 phy->pdata->use_ext_tx_lo = init_param->external_tx_lo_enable;
142 }
143 phy->pdata->dc_offset_update_events = init_param->dc_offset_tracking_update_event_mask;
144 phy->pdata->dc_offset_attenuation_high = init_param->dc_offset_attenuation_high_range;
145 phy->pdata->dc_offset_attenuation_low = init_param->dc_offset_attenuation_low_range;
146 phy->pdata->rf_dc_offset_count_high = init_param->dc_offset_count_high_range;
147 phy->pdata->rf_dc_offset_count_low = init_param->dc_offset_count_low_range;
148 phy->pdata->split_gt = init_param->split_gain_table_mode_enable;
149 phy->pdata->trx_synth_max_fref = init_param->trx_synthesizer_target_fref_overwrite_hz;
150 phy->pdata->qec_tracking_slow_mode_en = init_param->qec_tracking_slow_mode_enable;
151
152 /* ENSM Control */
153 phy->pdata->ensm_pin_pulse_mode = init_param->ensm_enable_pin_pulse_mode_enable;
154 phy->pdata->ensm_pin_ctrl = init_param->ensm_enable_txnrx_control_enable;
155
156 /* LO Control */
157 phy->pdata->rx_synth_freq = init_param->rx_synthesizer_frequency_hz;
158 phy->pdata->tx_synth_freq = init_param->tx_synthesizer_frequency_hz;
159
160 /* Rate & BW Control */
161 for(i = 0; i < 6; i++) {
162 phy->pdata->rx_path_clks[i] = init_param->rx_path_clock_frequencies[i];
163 }
164 for(i = 0; i < 6; i++) {
165 phy->pdata->tx_path_clks[i] = init_param->tx_path_clock_frequencies[i];
166 }
167 phy->pdata->rf_rx_bandwidth_Hz = init_param->rf_rx_bandwidth_hz;
168 phy->pdata->rf_tx_bandwidth_Hz = init_param->rf_tx_bandwidth_hz;
169
170 /* RF Port Control */
171 phy->pdata->rf_rx_input_sel = init_param->rx_rf_port_input_select;
172 phy->pdata->rf_tx_output_sel = init_param->tx_rf_port_input_select;
173
174 /* TX Attenuation Control */
175 phy->pdata->tx_atten = init_param->tx_attenuation_mdB;
176 phy->pdata->update_tx_gain_via_alert = init_param->update_tx_gain_in_alert_enable;
177
178 /* Reference Clock Control */
179 switch (phy->dev_sel) {
180 case ID_AD9363A:
181 phy->pdata->use_extclk = true;
182 break;
183 default:
184 phy->pdata->use_extclk = init_param->xo_disable_use_ext_refclk_enable;
185 }
186 phy->pdata->dcxo_coarse = init_param->dcxo_coarse_and_fine_tune[0];
187 phy->pdata->dcxo_fine = init_param->dcxo_coarse_and_fine_tune[1];
188 phy->pdata->ad9361_clkout_mode = (enum ad9361_clkout)init_param->clk_output_mode_select;
189
190 /* Gain Control */
191 phy->pdata->gain_ctrl.rx1_mode = (enum rf_gain_ctrl_mode)init_param->gc_rx1_mode;
192 phy->pdata->gain_ctrl.rx2_mode = (enum rf_gain_ctrl_mode)init_param->gc_rx2_mode;
193 phy->pdata->gain_ctrl.adc_large_overload_thresh = init_param->gc_adc_large_overload_thresh;
194 phy->pdata->gain_ctrl.adc_ovr_sample_size = init_param->gc_adc_ovr_sample_size;
195 phy->pdata->gain_ctrl.adc_small_overload_thresh = init_param->gc_adc_small_overload_thresh;
196 phy->pdata->gain_ctrl.dec_pow_measuremnt_duration = init_param->gc_dec_pow_measurement_duration;
197 phy->pdata->gain_ctrl.dig_gain_en = init_param->gc_dig_gain_enable;
198 phy->pdata->gain_ctrl.lmt_overload_high_thresh = init_param->gc_lmt_overload_high_thresh;
199 phy->pdata->gain_ctrl.lmt_overload_low_thresh = init_param->gc_lmt_overload_low_thresh;
200 phy->pdata->gain_ctrl.low_power_thresh = init_param->gc_low_power_thresh;
201 phy->pdata->gain_ctrl.max_dig_gain = init_param->gc_max_dig_gain;
202
203 /* Gain MGC Control */
204 phy->pdata->gain_ctrl.mgc_dec_gain_step = init_param->mgc_dec_gain_step;
205 phy->pdata->gain_ctrl.mgc_inc_gain_step = init_param->mgc_inc_gain_step;
206 phy->pdata->gain_ctrl.mgc_rx1_ctrl_inp_en = init_param->mgc_rx1_ctrl_inp_enable;
207 phy->pdata->gain_ctrl.mgc_rx2_ctrl_inp_en = init_param->mgc_rx2_ctrl_inp_enable;
208 phy->pdata->gain_ctrl.mgc_split_table_ctrl_inp_gain_mode = init_param->mgc_split_table_ctrl_inp_gain_mode;
209
210 /* Gain AGC Control */
211 phy->pdata->gain_ctrl.adc_large_overload_exceed_counter = init_param->agc_adc_large_overload_exceed_counter;
212 phy->pdata->gain_ctrl.adc_large_overload_inc_steps = init_param->agc_adc_large_overload_inc_steps;
213 phy->pdata->gain_ctrl.adc_lmt_small_overload_prevent_gain_inc = init_param->agc_adc_lmt_small_overload_prevent_gain_inc_enable;
214 phy->pdata->gain_ctrl.adc_small_overload_exceed_counter = init_param->agc_adc_small_overload_exceed_counter;
215 phy->pdata->gain_ctrl.dig_gain_step_size = init_param->agc_dig_gain_step_size;
216 phy->pdata->gain_ctrl.dig_saturation_exceed_counter = init_param->agc_dig_saturation_exceed_counter;
217 phy->pdata->gain_ctrl.gain_update_interval_us = init_param->agc_gain_update_interval_us;
218 phy->pdata->gain_ctrl.immed_gain_change_if_large_adc_overload = init_param->agc_immed_gain_change_if_large_adc_overload_enable;
219 phy->pdata->gain_ctrl.immed_gain_change_if_large_lmt_overload = init_param->agc_immed_gain_change_if_large_lmt_overload_enable;
220 phy->pdata->gain_ctrl.agc_inner_thresh_high = init_param->agc_inner_thresh_high;
221 phy->pdata->gain_ctrl.agc_inner_thresh_high_dec_steps = init_param->agc_inner_thresh_high_dec_steps;
222 phy->pdata->gain_ctrl.agc_inner_thresh_low = init_param->agc_inner_thresh_low;
223 phy->pdata->gain_ctrl.agc_inner_thresh_low_inc_steps = init_param->agc_inner_thresh_low_inc_steps;
224 phy->pdata->gain_ctrl.lmt_overload_large_exceed_counter = init_param->agc_lmt_overload_large_exceed_counter;
225 phy->pdata->gain_ctrl.lmt_overload_large_inc_steps = init_param->agc_lmt_overload_large_inc_steps;
226 phy->pdata->gain_ctrl.lmt_overload_small_exceed_counter = init_param->agc_lmt_overload_small_exceed_counter;
227 phy->pdata->gain_ctrl.agc_outer_thresh_high = init_param->agc_outer_thresh_high;
228 phy->pdata->gain_ctrl.agc_outer_thresh_high_dec_steps = init_param->agc_outer_thresh_high_dec_steps;
229 phy->pdata->gain_ctrl.agc_outer_thresh_low = init_param->agc_outer_thresh_low;
230 phy->pdata->gain_ctrl.agc_outer_thresh_low_inc_steps = init_param->agc_outer_thresh_low_inc_steps;
231 phy->pdata->gain_ctrl.agc_attack_delay_extra_margin_us = init_param->agc_attack_delay_extra_margin_us;
232 phy->pdata->gain_ctrl.sync_for_gain_counter_en = init_param->agc_sync_for_gain_counter_enable;
233
234 /* Fast AGC */
235 phy->pdata->gain_ctrl.f_agc_dec_pow_measuremnt_duration = init_param->fagc_dec_pow_measuremnt_duration;
236 phy->pdata->gain_ctrl.f_agc_state_wait_time_ns = init_param->fagc_state_wait_time_ns;
237 /* Fast AGC - Low Power */
238 phy->pdata->gain_ctrl.f_agc_allow_agc_gain_increase = init_param->fagc_allow_agc_gain_increase;
239 phy->pdata->gain_ctrl.f_agc_lp_thresh_increment_time = init_param->fagc_lp_thresh_increment_time;
240 phy->pdata->gain_ctrl.f_agc_lp_thresh_increment_steps = init_param->fagc_lp_thresh_increment_steps;
241 /* Fast AGC - Lock Level */
242 phy->pdata->gain_ctrl.f_agc_lock_level = init_param->fagc_lock_level;
243 phy->pdata->gain_ctrl.f_agc_lock_level_lmt_gain_increase_en = init_param->fagc_lock_level_lmt_gain_increase_en;
244 phy->pdata->gain_ctrl.f_agc_lock_level_gain_increase_upper_limit = init_param->fagc_lock_level_gain_increase_upper_limit;
245 /* Fast AGC - Peak Detectors and Final Settling */
246 phy->pdata->gain_ctrl.f_agc_lpf_final_settling_steps = init_param->fagc_lpf_final_settling_steps;
247 phy->pdata->gain_ctrl.f_agc_lmt_final_settling_steps = init_param->fagc_lmt_final_settling_steps;
248 phy->pdata->gain_ctrl.f_agc_final_overrange_count = init_param->fagc_final_overrange_count;
249 /* Fast AGC - Final Power Test */
250 phy->pdata->gain_ctrl.f_agc_gain_increase_after_gain_lock_en = init_param->fagc_gain_increase_after_gain_lock_en;
251 /* Fast AGC - Unlocking the Gain */
252 phy->pdata->gain_ctrl.f_agc_gain_index_type_after_exit_rx_mode = (enum f_agc_target_gain_index_type)init_param->fagc_gain_index_type_after_exit_rx_mode;
253 phy->pdata->gain_ctrl.f_agc_use_last_lock_level_for_set_gain_en = init_param->fagc_use_last_lock_level_for_set_gain_en;
254 phy->pdata->gain_ctrl.f_agc_rst_gla_stronger_sig_thresh_exceeded_en = init_param->fagc_rst_gla_stronger_sig_thresh_exceeded_en;
255 phy->pdata->gain_ctrl.f_agc_optimized_gain_offset = init_param->fagc_optimized_gain_offset;
256 phy->pdata->gain_ctrl.f_agc_rst_gla_stronger_sig_thresh_above_ll = init_param->fagc_rst_gla_stronger_sig_thresh_above_ll;
257 phy->pdata->gain_ctrl.f_agc_rst_gla_engergy_lost_sig_thresh_exceeded_en = init_param->fagc_rst_gla_engergy_lost_sig_thresh_exceeded_en;
258 phy->pdata->gain_ctrl.f_agc_rst_gla_engergy_lost_goto_optim_gain_en = init_param->fagc_rst_gla_engergy_lost_goto_optim_gain_en;
259 phy->pdata->gain_ctrl.f_agc_rst_gla_engergy_lost_sig_thresh_below_ll = init_param->fagc_rst_gla_engergy_lost_sig_thresh_below_ll;
260 phy->pdata->gain_ctrl.f_agc_energy_lost_stronger_sig_gain_lock_exit_cnt = init_param->fagc_energy_lost_stronger_sig_gain_lock_exit_cnt;
261 phy->pdata->gain_ctrl.f_agc_rst_gla_large_adc_overload_en = init_param->fagc_rst_gla_large_adc_overload_en;
262 phy->pdata->gain_ctrl.f_agc_rst_gla_large_lmt_overload_en = init_param->fagc_rst_gla_large_lmt_overload_en;
263 phy->pdata->gain_ctrl.f_agc_rst_gla_en_agc_pulled_high_en = init_param->fagc_rst_gla_en_agc_pulled_high_en;
264 phy->pdata->gain_ctrl.f_agc_rst_gla_if_en_agc_pulled_high_mode = (enum f_agc_target_gain_index_type)init_param->fagc_rst_gla_if_en_agc_pulled_high_mode;
265 phy->pdata->gain_ctrl.f_agc_power_measurement_duration_in_state5 = init_param->fagc_power_measurement_duration_in_state5;
266
267 /* RSSI Control */
268 phy->pdata->rssi_ctrl.rssi_delay = init_param->rssi_delay;
269 phy->pdata->rssi_ctrl.rssi_duration = init_param->rssi_duration;
270 phy->pdata->rssi_ctrl.restart_mode = (enum rssi_restart_mode)init_param->rssi_restart_mode;
271 phy->pdata->rssi_ctrl.rssi_unit_is_rx_samples = init_param->rssi_unit_is_rx_samples_enable;
272 phy->pdata->rssi_ctrl.rssi_wait = init_param->rssi_wait;
273
274 /* Aux ADC Control */
275 phy->pdata->auxadc_ctrl.auxadc_decimation = init_param->aux_adc_decimation;
276 phy->pdata->auxadc_ctrl.auxadc_clock_rate = init_param->aux_adc_rate;
277
278 /* AuxDAC Control */
279 phy->pdata->auxdac_ctrl.auxdac_manual_mode_en = init_param->aux_dac_manual_mode_enable;
280 phy->pdata->auxdac_ctrl.dac1_default_value = init_param->aux_dac1_default_value_mV;
281 phy->pdata->auxdac_ctrl.dac1_in_rx_en = init_param->aux_dac1_active_in_rx_enable;
282 phy->pdata->auxdac_ctrl.dac1_in_tx_en = init_param->aux_dac1_active_in_tx_enable;
283 phy->pdata->auxdac_ctrl.dac1_in_alert_en = init_param->aux_dac1_active_in_alert_enable;
284 phy->pdata->auxdac_ctrl.dac1_rx_delay_us = init_param->aux_dac1_rx_delay_us;
285 phy->pdata->auxdac_ctrl.dac1_tx_delay_us = init_param->aux_dac1_tx_delay_us;
286 phy->pdata->auxdac_ctrl.dac2_default_value = init_param->aux_dac2_default_value_mV;
287 phy->pdata->auxdac_ctrl.dac2_in_rx_en = init_param->aux_dac2_active_in_rx_enable;
288 phy->pdata->auxdac_ctrl.dac2_in_tx_en = init_param->aux_dac2_active_in_tx_enable;
289 phy->pdata->auxdac_ctrl.dac2_in_alert_en = init_param->aux_dac2_active_in_alert_enable;
290 phy->pdata->auxdac_ctrl.dac2_rx_delay_us = init_param->aux_dac2_rx_delay_us;
291 phy->pdata->auxdac_ctrl.dac2_tx_delay_us = init_param->aux_dac2_tx_delay_us;
292
293 /* Temperature Sensor Control */
294 phy->pdata->auxadc_ctrl.temp_sensor_decimation = init_param->temp_sense_decimation;
295 phy->pdata->auxadc_ctrl.temp_time_inteval_ms = init_param->temp_sense_measurement_interval_ms;
296 phy->pdata->auxadc_ctrl.offset = init_param->temp_sense_offset_signed;
297 phy->pdata->auxadc_ctrl.periodic_temp_measuremnt = init_param->temp_sense_periodic_measurement_enable;
298
299 /* Control Out Setup */
300 phy->pdata->ctrl_outs_ctrl.en_mask = init_param->ctrl_outs_enable_mask;
301 phy->pdata->ctrl_outs_ctrl.index = init_param->ctrl_outs_index;
302
303 /* External LNA Control */
304 phy->pdata->elna_ctrl.settling_delay_ns = init_param->elna_settling_delay_ns;
305 phy->pdata->elna_ctrl.gain_mdB = init_param->elna_gain_mdB;
306 phy->pdata->elna_ctrl.bypass_loss_mdB = init_param->elna_bypass_loss_mdB;
307 phy->pdata->elna_ctrl.elna_1_control_en = init_param->elna_rx1_gpo0_control_enable;
308 phy->pdata->elna_ctrl.elna_2_control_en = init_param->elna_rx2_gpo1_control_enable;
309 phy->pdata->elna_ctrl.elna_in_gaintable_all_index_en = init_param->elna_gaintable_all_index_enable;
310
311 /* Digital Interface Control */
312 phy->pdata->dig_interface_tune_skipmode = (init_param->digital_interface_tune_skip_mode);
313 phy->pdata->dig_interface_tune_fir_disable = (init_param->digital_interface_tune_fir_disable);
314 phy->pdata->port_ctrl.pp_conf[0] = (init_param->pp_tx_swap_enable << 7);
315 phy->pdata->port_ctrl.pp_conf[0] |= (init_param->pp_rx_swap_enable << 6);
316 phy->pdata->port_ctrl.pp_conf[0] |= (init_param->tx_channel_swap_enable << 5);
317 phy->pdata->port_ctrl.pp_conf[0] |= (init_param->rx_channel_swap_enable << 4);
318 phy->pdata->port_ctrl.pp_conf[0] |= (init_param->rx_frame_pulse_mode_enable << 3);
319 phy->pdata->port_ctrl.pp_conf[0] |= (init_param->two_t_two_r_timing_enable << 2);
320 phy->pdata->port_ctrl.pp_conf[0] |= (init_param->invert_data_bus_enable << 1);
321 phy->pdata->port_ctrl.pp_conf[0] |= (init_param->invert_data_clk_enable << 0);
322 phy->pdata->port_ctrl.pp_conf[1] = (init_param->fdd_alt_word_order_enable << 7);
323 phy->pdata->port_ctrl.pp_conf[1] |= (init_param->invert_rx_frame_enable << 2);
324 phy->pdata->port_ctrl.pp_conf[2] = (init_param->fdd_rx_rate_2tx_enable << 7);
325 phy->pdata->port_ctrl.pp_conf[2] |= (init_param->swap_ports_enable << 6);
326 phy->pdata->port_ctrl.pp_conf[2] |= (init_param->single_data_rate_enable << 5);
327 phy->pdata->port_ctrl.pp_conf[2] |= (init_param->lvds_mode_enable << 4);
328 phy->pdata->port_ctrl.pp_conf[2] |= (init_param->half_duplex_mode_enable << 3);
329 phy->pdata->port_ctrl.pp_conf[2] |= (init_param->single_port_mode_enable << 2);
330 phy->pdata->port_ctrl.pp_conf[2] |= (init_param->full_port_enable << 1);
331 phy->pdata->port_ctrl.pp_conf[2] |= (init_param->full_duplex_swap_bits_enable << 0);
332 phy->pdata->port_ctrl.pp_conf[1] |= (init_param->delay_rx_data & 0x3);
333 phy->pdata->port_ctrl.rx_clk_data_delay = DATA_CLK_DELAY(init_param->rx_data_clock_delay);
334 phy->pdata->port_ctrl.rx_clk_data_delay |= RX_DATA_DELAY(init_param->rx_data_delay);
335 phy->pdata->port_ctrl.tx_clk_data_delay = FB_CLK_DELAY(init_param->tx_fb_clock_delay);
336 phy->pdata->port_ctrl.tx_clk_data_delay |= TX_DATA_DELAY(init_param->tx_data_delay);
337 phy->pdata->port_ctrl.lvds_bias_ctrl = ((init_param->lvds_bias_mV - 75) / 75) & 0x7;
338 phy->pdata->port_ctrl.lvds_bias_ctrl |= (init_param->lvds_rx_onchip_termination_enable << 5);
339 phy->pdata->rx1rx2_phase_inversion_en = init_param->rx1rx2_phase_inversion_en;
340
341 /* GPO Control */
342 phy->pdata->gpo_ctrl.gpo0_inactive_state_high_en = init_param->gpo0_inactive_state_high_enable;
343 phy->pdata->gpo_ctrl.gpo1_inactive_state_high_en = init_param->gpo1_inactive_state_high_enable;
344 phy->pdata->gpo_ctrl.gpo2_inactive_state_high_en = init_param->gpo2_inactive_state_high_enable;
345 phy->pdata->gpo_ctrl.gpo3_inactive_state_high_en = init_param->gpo3_inactive_state_high_enable;
346
347 phy->pdata->gpo_ctrl.gpo0_slave_rx_en = init_param->gpo0_slave_rx_enable;
348 phy->pdata->gpo_ctrl.gpo0_slave_tx_en = init_param->gpo0_slave_tx_enable;
349 phy->pdata->gpo_ctrl.gpo1_slave_rx_en = init_param->gpo1_slave_rx_enable;
350 phy->pdata->gpo_ctrl.gpo1_slave_tx_en = init_param->gpo1_slave_tx_enable;
351 phy->pdata->gpo_ctrl.gpo2_slave_rx_en = init_param->gpo2_slave_rx_enable;
352 phy->pdata->gpo_ctrl.gpo2_slave_tx_en = init_param->gpo2_slave_tx_enable;
353 phy->pdata->gpo_ctrl.gpo3_slave_rx_en = init_param->gpo3_slave_rx_enable;
354 phy->pdata->gpo_ctrl.gpo3_slave_tx_en = init_param->gpo3_slave_tx_enable;
355
356 phy->pdata->gpo_ctrl.gpo0_rx_delay_us = init_param->gpo0_rx_delay_us;
357 phy->pdata->gpo_ctrl.gpo0_tx_delay_us = init_param->gpo0_tx_delay_us;
358 phy->pdata->gpo_ctrl.gpo1_rx_delay_us = init_param->gpo1_rx_delay_us;
359 phy->pdata->gpo_ctrl.gpo1_tx_delay_us = init_param->gpo1_tx_delay_us;
360 phy->pdata->gpo_ctrl.gpo2_rx_delay_us = init_param->gpo2_rx_delay_us;
361 phy->pdata->gpo_ctrl.gpo2_tx_delay_us = init_param->gpo2_tx_delay_us;
362 phy->pdata->gpo_ctrl.gpo3_rx_delay_us = init_param->gpo3_rx_delay_us;
363 phy->pdata->gpo_ctrl.gpo3_tx_delay_us = init_param->gpo3_tx_delay_us;
364
365 /* Tx Monitor Control */
366 phy->pdata->txmon_ctrl.low_high_gain_threshold_mdB = init_param->low_high_gain_threshold_mdB;
367 phy->pdata->txmon_ctrl.low_gain_dB = init_param->low_gain_dB;
368 phy->pdata->txmon_ctrl.high_gain_dB = init_param->high_gain_dB;
369 phy->pdata->txmon_ctrl.tx_mon_track_en = init_param->tx_mon_track_en;
370 phy->pdata->txmon_ctrl.one_shot_mode_en = init_param->one_shot_mode_en;
371 phy->pdata->txmon_ctrl.tx_mon_delay = init_param->tx_mon_delay;
372 phy->pdata->txmon_ctrl.tx_mon_duration = init_param->tx_mon_duration;
373 phy->pdata->txmon_ctrl.tx1_mon_front_end_gain = init_param->tx1_mon_front_end_gain;
374 phy->pdata->txmon_ctrl.tx2_mon_front_end_gain = init_param->tx2_mon_front_end_gain;
375 phy->pdata->txmon_ctrl.tx1_mon_lo_cm = init_param->tx1_mon_lo_cm;
376 phy->pdata->txmon_ctrl.tx2_mon_lo_cm = init_param->tx2_mon_lo_cm;
377
378 phy->pdata->debug_mode = true;
379 phy->pdata->gpio_resetb = init_param->gpio_resetb;
380 /* Optional: next three GPIOs are used for MCS synchronization */
381 phy->pdata->gpio_sync = init_param->gpio_sync;
382 phy->pdata->gpio_cal_sw1 = init_param->gpio_cal_sw1;
383 phy->pdata->gpio_cal_sw2 = init_param->gpio_cal_sw2;
384
385 phy->pdata->port_ctrl.digital_io_ctrl = 0;
386 phy->pdata->port_ctrl.lvds_invert[0] = init_param->lvds_invert1_control;
387 phy->pdata->port_ctrl.lvds_invert[1] = init_param->lvds_invert2_control;
388
389 #ifndef AXI_ADC_NOT_PRESENT
390 phy->adc_conv->chip_info = &axiadc_chip_info_tbl[phy->pdata->rx2tx2 ? ID_AD9361 : ID_AD9364];
391 #endif
392
393 phy->rx_eq_2tx = false;
394
395 phy->current_table = RXGAIN_TBLS_END;
396 phy->bypass_tx_fir = true;
397 phy->bypass_rx_fir = true;
398 phy->rate_governor = 1;
399 phy->rfdc_track_en = true;
400 phy->bbdc_track_en = true;
401 phy->quad_track_en = true;
402
403 phy->bist_loopback_mode = 0;
404 phy->bist_prbs_mode = BIST_DISABLE;
405 phy->bist_tone_mode = BIST_DISABLE;
406 phy->bist_tone_freq_Hz = 0;
407 phy->bist_tone_level_dB = 0;
408 phy->bist_tone_mask = 0;
409
410 ad9361_reset(phy);
411
412 ret = ad9361_spi_read(phy->spi, REG_PRODUCT_ID);
413 if ((ret & PRODUCT_ID_MASK) != PRODUCT_ID_9361) {
414 printf("%s : Unsupported PRODUCT_ID 0x%X", __func__, (unsigned int)ret);
415 ret = -ENODEV;
416 goto out;
417 }
418 rev = ret & REV_MASK;
419
420 if (AD9364_DEVICE) {
421 phy->pdata->rx2tx2 = false;
422 phy->pdata->rx1tx1_mode_use_rx_num = 1;
423 phy->pdata->rx1tx1_mode_use_tx_num = 1;
424 }
425
426 phy->ad9361_rfpll_ext_recalc_rate = init_param->ad9361_rfpll_ext_recalc_rate;
427 phy->ad9361_rfpll_ext_round_rate = init_param->ad9361_rfpll_ext_round_rate;
428 phy->ad9361_rfpll_ext_set_rate = init_param->ad9361_rfpll_ext_set_rate;
429
430 ret = register_clocks(phy);
431 if (ret < 0)
432 goto out;
433
434 #ifndef AXI_ADC_NOT_PRESENT
435 axiadc_init(phy);
436 phy->adc_state->pcore_version = axiadc_read(phy->adc_state, ADI_REG_VERSION);
437 #endif
438
439 ad9361_init_gain_tables(phy);
440
441 ret = ad9361_setup(phy);
442 if (ret < 0)
443 goto out;
444
445 #ifndef AXI_ADC_NOT_PRESENT
446 /* platform specific wrapper to call ad9361_post_setup() */
447 ret = axiadc_post_setup(phy);
448 if (ret < 0)
449 goto out;
450 #endif
451
452 printf("%s : AD936x Rev %d successfully initialized\n", __func__, (int)rev);
453
454 *ad9361_phy = phy;
455
456 return 0;
457
458 out:
459 free(phy->spi);
460 #ifndef AXI_ADC_NOT_PRESENT
461 free(phy->adc_conv);
462 free(phy->adc_state);
463 #endif
464 free(phy->clk_refin);
465 free(phy->pdata);
466 free(phy);
467 printf("%s : AD936x initialization error\n", __func__);
468
469 return -ENODEV;
470 }
471
472 /**
473 * Set the Enable State Machine (ENSM) mode.
474 * @param phy The AD9361 current state structure.
475 * @param mode The ENSM mode.
476 * Accepted values:
477 * ENSM_MODE_TX
478 * ENSM_MODE_RX
479 * ENSM_MODE_ALERT
480 * ENSM_MODE_FDD
481 * ENSM_MODE_WAIT
482 * ENSM_MODE_SLEEP
483 * ENSM_MODE_PINCTRL
484 * ENSM_MODE_PINCTRL_FDD_INDEP
485 * @return 0 in case of success, negative error code otherwise.
486 *
487 * Note: This function will/may affect the data path.
488 */
489 int32_t ad9361_set_en_state_machine_mode (struct ad9361_rf_phy *phy,
490 uint32_t mode)
491 {
492 int32_t ret;
493 uint8_t ensm_state;
494 bool pinctrl = false;
495
496 phy->pdata->fdd_independent_mode = false;
497
498 switch (mode) {
499 case ENSM_MODE_TX:
500 ensm_state = ENSM_STATE_TX;
501 break;
502 case ENSM_MODE_RX:
503 ensm_state = ENSM_STATE_RX;
504 break;
505 case ENSM_MODE_ALERT:
506 ensm_state = ENSM_STATE_ALERT;
507 break;
508 case ENSM_MODE_FDD:
509 ensm_state = ENSM_STATE_FDD;
510 break;
511 case ENSM_MODE_WAIT:
512 ensm_state = ENSM_STATE_SLEEP_WAIT;
513 break;
514 case ENSM_MODE_SLEEP:
515 ensm_state = ENSM_STATE_SLEEP;
516 break;
517 case ENSM_MODE_PINCTRL:
518 ensm_state = ENSM_STATE_SLEEP_WAIT;
519 pinctrl = true;
520 break;
521 case ENSM_MODE_PINCTRL_FDD_INDEP:
522 ensm_state = ENSM_STATE_FDD;
523 phy->pdata->fdd_independent_mode = true;
524 break;
525 default:
526 return -EINVAL;
527 }
528
529 ad9361_set_ensm_mode(phy, phy->pdata->fdd, pinctrl);
530 ret = ad9361_ensm_set_state(phy, ensm_state, pinctrl);
531
532 return ret;
533 }
534
535 /**
536 * Get the Enable State Machine (ENSM) mode.
537 * @param phy The AD9361 current state structure.
538 * @param mode A variable to store the selected ENSM mode.
539 * @return 0 in case of success, negative error code otherwise.
540 */
541 int32_t ad9361_get_en_state_machine_mode (struct ad9361_rf_phy *phy,
542 uint32_t *mode)
543 {
544 uint8_t ensm_state;
545 bool pinctrl = false;
546 int32_t ret;
547
548 ensm_state = ad9361_spi_read(phy->spi, REG_STATE);
549 ensm_state &= ENSM_STATE(~0);
550 ret = ad9361_spi_read(phy->spi, REG_ENSM_CONFIG_1);
551 if ((ret & ENABLE_ENSM_PIN_CTRL) == ENABLE_ENSM_PIN_CTRL)
552 pinctrl = true;
553
554 switch (ensm_state) {
555 case ENSM_STATE_TX:
556 *mode = ENSM_MODE_TX;
557 break;
558 case ENSM_STATE_RX:
559 *mode = ENSM_MODE_RX;
560 break;
561 case ENSM_STATE_ALERT:
562 *mode = ENSM_MODE_ALERT;
563 break;
564 case ENSM_STATE_FDD:
565 if (phy->pdata->fdd_independent_mode)
566 *mode = ENSM_MODE_PINCTRL_FDD_INDEP;
567 else
568 *mode = ENSM_MODE_FDD;
569 break;
570 case ENSM_STATE_SLEEP_WAIT:
571 if (pinctrl)
572 *mode = ENSM_MODE_PINCTRL;
573 else
574 *mode = ENSM_MODE_WAIT;
575 break;
576 case ENSM_STATE_SLEEP:
577 *mode = ENSM_MODE_SLEEP;
578 break;
579 default:
580 return -EINVAL;
581 }
582
583 return 0;
584 }
585
586 /**
587 * Set the receive RF gain for the selected channel.
588 * @param phy The AD9361 current state structure.
589 * @param ch The desired channel number (RX1, RX2).
590 * Accepted values in 2x2 mode:
591 * RX1 (0)
592 * RX2 (1)
593 * Accepted values in 1x1 mode:
594 * RX1 (0)
595 * @param gain_db The RF gain (dB).
596 * Example:
597 * 10 (10 dB)
598 * @return 0 in case of success, negative error code otherwise.
599 */
600 int32_t ad9361_set_rx_rf_gain (struct ad9361_rf_phy *phy,
601 uint8_t ch, int32_t gain_db)
602 {
603 struct rf_rx_gain rx_gain = {0};
604 int32_t ret = 0;
605
606 if ((phy->pdata->rx2tx2 == 0) && (ch == RX2)) {
607 printf("%s : RX2 is an invalid option in 1x1 mode!\n", __func__);
608 return -1;
609 }
610
611 rx_gain.gain_db = gain_db;
612 ret = ad9361_set_rx_gain(phy,
613 ad9361_1rx1tx_channel_map(phy, false,
614 ch + 1), &rx_gain);
615
616 return ret;
617 }
618
619 /**
620 * Get current receive RF gain for the selected channel.
621 * @param phy The AD9361 current state structure.
622 * @param ch The desired channel number (RX1, RX2).
623 * Accepted values in 2x2 mode:
624 * RX1 (0)
625 * RX2 (1)
626 * Accepted values in 1x1 mode:
627 * RX1 (0)
628 * @param gain_db A variable to store the RF gain (dB).
629 * @return 0 in case of success, negative error code otherwise.
630 */
631 int32_t ad9361_get_rx_rf_gain (struct ad9361_rf_phy *phy,
632 uint8_t ch, int32_t *gain_db)
633 {
634 struct rf_rx_gain rx_gain = {0};
635 int32_t ret = 0;
636
637 if ((phy->pdata->rx2tx2 == 0) && (ch == RX2)) {
638 printf("%s : RX2 is an invalid option in 1x1 mode!\n", __func__);
639 return -1;
640 }
641
642 ret = ad9361_get_rx_gain(phy, ad9361_1rx1tx_channel_map(phy,
643 false, ch + 1), &rx_gain);
644
645 *gain_db = rx_gain.gain_db;
646
647 return ret;
648 }
649
650 /**
651 * Set the RX RF bandwidth.
652 * @param phy The AD9361 current state structure.
653 * @param bandwidth_hz The desired bandwidth (Hz).
654 * Example:
655 * 18000000 (18 MHz)
656 * @return 0 in case of success, negative error code otherwise.
657 *
658 * Note: This function will/may affect the data path.
659 */
660 int32_t ad9361_set_rx_rf_bandwidth (struct ad9361_rf_phy *phy,
661 uint32_t bandwidth_hz)
662 {
663 int32_t ret = 0;
664
665 bandwidth_hz = ad9361_validate_rf_bw(phy, bandwidth_hz);
666
667 if (phy->current_rx_bw_Hz != bandwidth_hz)
668 ret = ad9361_update_rf_bandwidth(phy, bandwidth_hz,
669 phy->current_tx_bw_Hz);
670 else
671 ret = 0;
672
673 return ret;
674 }
675
676 /**
677 * Get the RX RF bandwidth.
678 * @param phy The AD9361 current state structure.
679 * @param bandwidth_hz A variable to store the bandwidth value (Hz).
680 * @return 0 in case of success, negative error code otherwise.
681 */
682 int32_t ad9361_get_rx_rf_bandwidth (struct ad9361_rf_phy *phy,
683 uint32_t *bandwidth_hz)
684 {
685 *bandwidth_hz = phy->current_rx_bw_Hz;
686
687 return 0;
688 }
689
690 /**
691 * Set the RX sampling frequency.
692 * @param phy The AD9361 current state structure.
693 * @param sampling_freq_hz The desired frequency (Hz).
694 * Example:
695 * 30720000 (30.72 MHz)
696 * @return 0 in case of success, negative error code otherwise.
697 *
698 * Note: This function will/may affect the data path.
699 */
700 int32_t ad9361_set_rx_sampling_freq (struct ad9361_rf_phy *phy,
701 uint32_t sampling_freq_hz)
702 {
703 int32_t ret;
704 uint32_t rx[6], tx[6];
705
706 ret = ad9361_calculate_rf_clock_chain(phy, sampling_freq_hz,
707 phy->rate_governor, rx, tx);
708 if (ret < 0)
709 return ret;
710
711 ad9361_set_trx_clock_chain(phy, rx, tx);
712
713 ret = ad9361_update_rf_bandwidth(phy, phy->current_rx_bw_Hz,
714 phy->current_tx_bw_Hz);
715
716 return ret;
717 }
718
719 /**
720 * Get current RX sampling frequency.
721 * @param phy The AD9361 current state structure.
722 * @param sampling_freq_hz A variable to store the frequency value (Hz).
723 * @return 0 in case of success, negative error code otherwise.
724 */
725 int32_t ad9361_get_rx_sampling_freq (struct ad9361_rf_phy *phy,
726 uint32_t *sampling_freq_hz)
727 {
728 *sampling_freq_hz = (uint32_t)clk_get_rate(phy,
729 phy->ref_clk_scale[RX_SAMPL_CLK]);
730
731 return 0;
732 }
733
734 /**
735 * Set the RX LO frequency.
736 * @param phy The AD9361 current state structure.
737 * @param lo_freq_hz The desired frequency (Hz).
738 * Example:
739 * 2400000000 (2.4 GHz)
740 * @return 0 in case of success, negative error code otherwise.
741 *
742 * Note: This function will/may affect the data path.
743 */
744 int32_t ad9361_set_rx_lo_freq (struct ad9361_rf_phy *phy,
745 uint64_t lo_freq_hz)
746 {
747 int32_t ret;
748
749 ret = clk_set_rate(phy, phy->ref_clk_scale[RX_RFPLL],
750 ad9361_to_clk(lo_freq_hz));
751
752 return ret;
753 }
754
755 /**
756 * Get current RX LO frequency.
757 * @param phy The AD9361 current state structure.
758 * @param lo_freq_hz A variable to store the frequency value (Hz).
759 * @return 0 in case of success, negative error code otherwise.
760 */
761 int32_t ad9361_get_rx_lo_freq (struct ad9361_rf_phy *phy,
762 uint64_t *lo_freq_hz)
763 {
764 *lo_freq_hz = ad9361_from_clk(clk_get_rate(phy,
765 phy->ref_clk_scale[RX_RFPLL]));
766
767 return 0;
768 }
769
770 /**
771 * Switch between internal and external LO.
772 * @param phy The AD9361 state structure.
773 * @param int_ext The selected lo (INT_LO, EXT_LO).
774 * Accepted values:
775 * INT_LO
776 * EXT_LO
777 * @return 0 in case of success, negative error code otherwise.
778 */
779 int32_t ad9361_set_rx_lo_int_ext(struct ad9361_rf_phy *phy, uint8_t int_ext)
780 {
781 if ((phy->dev_sel == ID_AD9363A) && (int_ext = EXT_LO)) {
782 printf("%s : EXT_LO is not supported by AD9363!\n", __func__);
783 return -1;
784 }
785
786 phy->pdata->use_ext_rx_lo = int_ext;
787
788 return ad9361_clk_mux_set_parent(phy->ref_clk_scale[RX_RFPLL], int_ext);
789 }
790
791 /**
792 * Get the RSSI for the selected channel.
793 * @param phy The AD9361 current state structure.
794 * @param ch The desired channel (RX1, RX2).
795 * Accepted values in 2x2 mode:
796 * RX1 (0)
797 * RX2 (1)
798 * Accepted values in 1x1 mode:
799 * RX1 (0)
800 * @param rssi A variable to store the RSSI.
801 * @return 0 in case of success, negative error code otherwise.
802 */
803 int32_t ad9361_get_rx_rssi (struct ad9361_rf_phy *phy,
804 uint8_t ch, struct rf_rssi *rssi)
805 {
806 int32_t ret;
807
808 if ((phy->pdata->rx2tx2 == 0) && (ch == RX2)) {
809 printf("%s : RX2 is an invalid option in 1x1 mode!\n", __func__);
810 return -1;
811 }
812
813 rssi->ant = ad9361_1rx1tx_channel_map(phy, false, ch + 1);
814 rssi->duration = 1;
815 ret = ad9361_read_rssi(phy, rssi);
816
817 return ret;
818 }
819
820 /**
821 * Set the gain control mode for the selected channel.
822 * @param phy The AD9361 current state structure.
823 * @param ch The desired channel (RX1, RX2).
824 * Accepted values in 2x2 mode:
825 * RX1 (0)
826 * RX2 (1)
827 * Accepted values in 1x1 mode:
828 * RX1 (0)
829 * @param gc_mode The gain control mode (manual, fast_attack, slow_attack,
830 * hybrid).
831 * Accepted values:
832 * RF_GAIN_MGC (manual)
833 * RF_GAIN_FASTATTACK_AGC (fast_attack)
834 * RF_GAIN_SLOWATTACK_AGC (slow_attack)
835 * RF_GAIN_HYBRID_AGC (hybrid)
836 * @return 0 in case of success, negative error code otherwise.
837 */
838 int32_t ad9361_set_rx_gain_control_mode (struct ad9361_rf_phy *phy,
839 uint8_t ch, uint8_t gc_mode)
840 {
841 struct rf_gain_ctrl gc = {0};
842
843 if ((phy->pdata->rx2tx2 == 0) && (ch == RX2)) {
844 printf("%s : RX2 is an invalid option in 1x1 mode!\n", __func__);
845 return -1;
846 }
847
848 gc.ant = ad9361_1rx1tx_channel_map(phy, false, ch + 1);
849 gc.mode = phy->agc_mode[ch] = gc_mode;
850
851 ad9361_set_gain_ctrl_mode(phy, &gc);
852
853 return 0;
854 }
855
856 /**
857 * Get the gain control mode for the selected channel.
858 * @param phy The AD9361 current state structure.
859 * @param ch The desired channel (RX1, RX2).
860 * Accepted values:
861 * RX1 (0)
862 * RX2 (1)
863 * @param gc_mode A variable to store the gain control mode.
864 * @return 0 in case of success, negative error code otherwise.
865 */
866 int32_t ad9361_get_rx_gain_control_mode (struct ad9361_rf_phy *phy,
867 uint8_t ch, uint8_t *gc_mode)
868 {
869 *gc_mode = phy->agc_mode[ch];
870
871 return 0;
872 }
873
874 /**
875 * Set the RX FIR filter configuration.
876 * @param phy The AD9361 current state structure.
877 * @param fir_cfg FIR filter configuration.
878 * @return 0 in case of success, negative error code otherwise.
879 *
880 * Note: This function will/may affect the data path.
881 */
882 int32_t ad9361_set_rx_fir_config (struct ad9361_rf_phy *phy,
883 AD9361_RXFIRConfig fir_cfg)
884 {
885 int32_t ret;
886
887 phy->rx_fir_dec = fir_cfg.rx_dec;
888 ret = ad9361_load_fir_filter_coef(phy, (enum fir_dest)(fir_cfg.rx | FIR_IS_RX),
889 fir_cfg.rx_gain, fir_cfg.rx_coef_size, fir_cfg.rx_coef);
890
891 return ret;
892 }
893
894 /**
895 * Get the RX FIR filter configuration.
896 * @param phy The AD9361 current state structure.
897 * @param tx_ch The selected RX channel (RX1, RX2).
898 * Accepted values:
899 * RX1 (0)
900 * RX2 (1)
901 * @param fir_cfg FIR filter configuration output file.
902 * @return 0 in case of success, negative error code otherwise.
903 */
904 int32_t ad9361_get_rx_fir_config(struct ad9361_rf_phy *phy, uint8_t rx_ch, AD9361_RXFIRConfig *fir_cfg)
905 {
906 int32_t ret;
907 uint32_t fir_conf;
908 uint8_t index;
909
910 rx_ch += 1;
911
912 ret = ad9361_spi_read(phy->spi, REG_RX_FILTER_CONFIG);
913 if(ret < 0)
914 return ret;
915 fir_conf = ret;
916
917 fir_cfg->rx_coef_size = (((fir_conf & FIR_NUM_TAPS(7)) >> 5) + 1) * 16;
918
919 ret = ad9361_spi_read(phy->spi, REG_RX_FILTER_GAIN);
920 if(ret < 0)
921 return ret;
922 fir_cfg->rx_gain = -6 * (ret & FILTER_GAIN(3)) + 6;
923 fir_cfg->rx = rx_ch;
924
925 fir_conf &= ~FIR_SELECT(3);
926 fir_conf |= FIR_SELECT(rx_ch) | FIR_START_CLK;
927 ad9361_spi_write(phy->spi, REG_RX_FILTER_CONFIG, fir_conf);
928
929 for(index = 0; index < 128; index++)
930 {
931 ad9361_spi_write(phy->spi, REG_RX_FILTER_COEF_ADDR, index);
932 ret = ad9361_spi_read(phy->spi, REG_RX_FILTER_COEF_READ_DATA_1);
933 if(ret < 0)
934 return ret;
935 fir_cfg->rx_coef[index] = ret;
936 ret = ad9361_spi_read(phy->spi, REG_RX_FILTER_COEF_READ_DATA_2);
937 if(ret < 0)
938 return ret;
939 fir_cfg->rx_coef[index] |= (ret << 8);
940 }
941
942 fir_conf &= ~FIR_START_CLK;
943 ad9361_spi_write(phy->spi, REG_RX_FILTER_CONFIG, fir_conf);
944
945 fir_cfg->rx_dec = phy->rx_fir_dec;
946
947 return 0;
948 }
949
950 /**
951 * Enable/disable the RX FIR filter.
952 * @param phy The AD9361 current state structure.
953 * @param en_dis The option (ENABLE, DISABLE).
954 * Accepted values:
955 * ENABLE (1)
956 * DISABLE (0)
957 * @return 0 in case of success, negative error code otherwise.
958 *
959 * Note: This function will/may affect the data path.
960 */
961 int32_t ad9361_set_rx_fir_en_dis (struct ad9361_rf_phy *phy,
962 uint8_t en_dis)
963 {
964 int32_t ret = 0;
965
966 if(phy->bypass_rx_fir == !en_dis)
967 return ret;
968
969 phy->bypass_rx_fir = !en_dis;
970 ret = ad9361_validate_enable_fir(phy);
971 if (ret < 0) {
972 phy->bypass_rx_fir = true;
973 }
974
975 return ret;
976 }
977
978 /**
979 * Get the status of the RX FIR filter.
980 * @param phy The AD9361 current state structure.
981 * @param en_dis The enable/disable status buffer.
982 * @return 0 in case of success, negative error code otherwise.
983 */
984 int32_t ad9361_get_rx_fir_en_dis (struct ad9361_rf_phy *phy,
985 uint8_t *en_dis)
986 {
987 *en_dis = !phy->bypass_rx_fir;
988
989 return 0;
990 }
991
992 /**
993 * Enable/disable the RX RFDC Tracking.
994 * @param phy The AD9361 current state structure.
995 * @param en_dis The option (ENABLE, DISABLE).
996 * Accepted values:
997 * ENABLE (1)
998 * DISABLE (0)
999 * @return 0 in case of success, negative error code otherwise.
1000 */
1001 int32_t ad9361_set_rx_rfdc_track_en_dis (struct ad9361_rf_phy *phy,
1002 uint8_t en_dis)
1003 {
1004 int32_t ret = 0;
1005
1006 if(phy->rfdc_track_en == en_dis)
1007 return ret;
1008
1009 phy->rfdc_track_en = en_dis;
1010 ret = ad9361_tracking_control(phy, phy->bbdc_track_en,
1011 phy->rfdc_track_en, phy->quad_track_en);
1012
1013 return ret;
1014 }
1015
1016 /**
1017 * Get the status of the RX RFDC Tracking.
1018 * @param phy The AD9361 current state structure.
1019 * @param en_dis The enable/disable status buffer.
1020 * @return 0 in case of success, negative error code otherwise.
1021 */
1022 int32_t ad9361_get_rx_rfdc_track_en_dis (struct ad9361_rf_phy *phy,
1023 uint8_t *en_dis)
1024 {
1025 *en_dis = phy->rfdc_track_en;
1026
1027 return 0;
1028 }
1029
1030 /**
1031 * Enable/disable the RX BasebandDC Tracking.
1032 * @param phy The AD9361 current state structure.
1033 * @param en_dis The option (ENABLE, DISABLE).
1034 * Accepted values:
1035 * ENABLE (1)
1036 * DISABLE (0)
1037 * @return 0 in case of success, negative error code otherwise.
1038 */
1039 int32_t ad9361_set_rx_bbdc_track_en_dis (struct ad9361_rf_phy *phy,
1040 uint8_t en_dis)
1041 {
1042 int32_t ret = 0;
1043
1044 if(phy->bbdc_track_en == en_dis)
1045 return ret;
1046
1047 phy->bbdc_track_en = en_dis;
1048 ret = ad9361_tracking_control(phy, phy->bbdc_track_en,
1049 phy->rfdc_track_en, phy->quad_track_en);
1050
1051 return ret;
1052 }
1053
1054 /**
1055 * Get the status of the RX BasebandDC Tracking.
1056 * @param phy The AD9361 current state structure.
1057 * @param en_dis The enable/disable status buffer.
1058 * @return 0 in case of success, negative error code otherwise.
1059 */
1060 int32_t ad9361_get_rx_bbdc_track_en_dis (struct ad9361_rf_phy *phy,
1061 uint8_t *en_dis)
1062 {
1063 *en_dis = phy->bbdc_track_en;
1064
1065 return 0;
1066 }
1067
1068 /**
1069 * Enable/disable the RX Quadrature Tracking.
1070 * @param phy The AD9361 current state structure.
1071 * @param en_dis The option (ENABLE, DISABLE).
1072 * Accepted values:
1073 * ENABLE (1)
1074 * DISABLE (0)
1075 * @return 0 in case of success, negative error code otherwise.
1076 */
1077 int32_t ad9361_set_rx_quad_track_en_dis (struct ad9361_rf_phy *phy,
1078 uint8_t en_dis)
1079 {
1080 int32_t ret = 0;
1081
1082 if(phy->quad_track_en == en_dis)
1083 return ret;
1084
1085 phy->quad_track_en = en_dis;
1086 ret = ad9361_tracking_control(phy, phy->bbdc_track_en,
1087 phy->rfdc_track_en, phy->quad_track_en);
1088
1089 return ret;
1090 }
1091
1092 /**
1093 * Get the status of the RX Quadrature Tracking.
1094 * @param phy The AD9361 current state structure.
1095 * @param en_dis The enable/disable status buffer.
1096 * @return 0 in case of success, negative error code otherwise.
1097 */
1098 int32_t ad9361_get_rx_quad_track_en_dis (struct ad9361_rf_phy *phy,
1099 uint8_t *en_dis)
1100 {
1101 *en_dis = phy->quad_track_en;
1102
1103 return 0;
1104 }
1105
1106 /**
1107 * Set the RX RF input port.
1108 * @param phy The AD9361 current state structure.
1109 * @param mode The RF port.
1110 * Accepted values:
1111 * A_BALANCED (0 - (RX1A_N & RX1A_P) and (RX2A_N & RX2A_P) enabled; balanced)
1112 * B_BALANCED (1 - (RX1B_N & RX1B_P) and (RX2B_N & RX2B_P) enabled; balanced)
1113 * C_BALANCED (2 - (RX1C_N & RX1C_P) and (RX2C_N & RX2C_P) enabled; balanced)
1114 * A_N (3 - RX1A_N and RX2A_N enabled; unbalanced)
1115 * A_P (4 - RX1A_P and RX2A_P enabled; unbalanced)
1116 * B_N (5 - RX1B_N and RX2B_N enabled; unbalanced)
1117 * B_P (6 - RX1B_P and RX2B_P enabled; unbalanced)
1118 * C_N (7 - RX1C_N and RX2C_N enabled; unbalanced)
1119 * C_P (8 - RX1C_P and RX2C_P enabled; unbalanced)
1120 * TX_MON1 (9 - TX_MONITOR1)
1121 * TX_MON2 (10 - TX_MONITOR2)
1122 * TX_MON1_2 (11 - TX_MONITOR1 & TX_MONITOR2)
1123 * @return 0 in case of success, negative error code otherwise.
1124 */
1125 int32_t ad9361_set_rx_rf_port_input (struct ad9361_rf_phy *phy,
1126 uint32_t mode)
1127 {
1128 int32_t ret;
1129
1130 phy->pdata->rf_rx_input_sel = mode;
1131
1132 ret = ad9361_rf_port_setup(phy, false,
1133 phy->pdata->rf_rx_input_sel,
1134 phy->pdata->rf_tx_output_sel);
1135
1136 return ret;
1137 }
1138
1139 /**
1140 * Get the selected RX RF input port.
1141 * @param phy The AD9361 current state structure.
1142 * @param mode The RF port.
1143 * @return 0 in case of success, negative error code otherwise.
1144 */
1145 int32_t ad9361_get_rx_rf_port_input (struct ad9361_rf_phy *phy,
1146 uint32_t *mode)
1147 {
1148 *mode = phy->pdata->rf_rx_input_sel;
1149
1150 return 0;
1151 }
1152
1153 /**
1154 * Store RX fastlock profile.
1155 * To create a profile tune the synthesizer (ad9361_set_rx_lo_freq()) and then
1156 * call this function specifying the target profile number.
1157 * @param phy The AD9361 state structure.
1158 * @param profile The profile number (0 - 7).
1159 * Accepted values:
1160 * 0 - 7
1161 * @return 0 in case of success, negative error code otherwise.
1162 */
1163 int32_t ad9361_rx_fastlock_store(struct ad9361_rf_phy *phy, uint32_t profile)
1164 {
1165 return ad9361_fastlock_store(phy, 0, profile);
1166 }
1167
1168 /**
1169 * Recall specified RX fastlock profile.
1170 * When in fastlock pin select mode (init_param->rx_fastlock_pincontrol_enable),
1171 * the function needs to be called before then the pin-control can be used.
1172 * @param phy The AD9361 state structure.
1173 * @param profile The profile number (0 - 7).
1174 * Accepted values:
1175 * 0 - 7
1176 * @return 0 in case of success, negative error code otherwise.
1177 */
1178 int32_t ad9361_rx_fastlock_recall(struct ad9361_rf_phy *phy, uint32_t profile)
1179 {
1180 return ad9361_fastlock_recall(phy, 0, profile);
1181 }
1182
1183 /**
1184 * Load RX fastlock profile. A previously saved profile can be loaded in any
1185 * of the 8 available slots.
1186 * @param phy The AD9361 state structure.
1187 * @param profile The profile number (0 - 7).
1188 * Accepted values:
1189 * 0 - 7
1190 * @param values Fastlock profile program data.
1191 * Example:
1192 * val0,val1,val2,…,val15
1193 * @return 0 in case of success, negative error code otherwise.
1194 */
1195 int32_t ad9361_rx_fastlock_load(struct ad9361_rf_phy *phy, uint32_t profile, uint8_t *values)
1196 {
1197 return ad9361_fastlock_load(phy, 0, profile, values);
1198 }
1199
1200 /**
1201 * Save RX fastlock profile. In order to use more than 8 Profiles, an existing
1202 * profile can be read back and stored by the user application.
1203 * @param phy The AD9361 state structure.
1204 * @param profile The profile number (0 - 7).
1205 * Accepted values:
1206 * 0 - 7
1207 * @param values Fastlock profile program data.
1208 * @return 0 in case of success, negative error code otherwise.
1209 */
1210 int32_t ad9361_rx_fastlock_save(struct ad9361_rf_phy *phy, uint32_t profile, uint8_t *values)
1211 {
1212 return ad9361_fastlock_save(phy, 0, profile, values);
1213 }
1214
1215 /**
1216 * Set the transmit attenuation for the selected channel.
1217 * @param phy The AD9361 current state structure.
1218 * @param ch The desired channel number (TX1, TX2).
1219 * Accepted values in 2x2 mode:
1220 * TX1 (0)
1221 * TX2 (1)
1222 * Accepted values in 1x1 mode:
1223 * TX1 (0)
1224 * @param attenuation_mdb The attenuation (mdB).
1225 * Example:
1226 * 10000 (10 dB)
1227 * @return 0 in case of success, negative error code otherwise.
1228 */
1229 int32_t ad9361_set_tx_attenuation (struct ad9361_rf_phy *phy,
1230 uint8_t ch, uint32_t attenuation_mdb)
1231 {
1232 int32_t ret;
1233 int32_t channel;
1234
1235 if ((phy->pdata->rx2tx2 == 0) && (ch == TX2)) {
1236 printf("%s : TX2 is an invalid option in 1x1 mode!\n", __func__);
1237 return -1;
1238 }
1239
1240 channel = ad9361_1rx1tx_channel_map(phy, true, ch);
1241 ret = ad9361_set_tx_atten(phy, attenuation_mdb,
1242 channel == 0, channel == 1,
1243 !phy->pdata->update_tx_gain_via_alert);
1244
1245 return ret;
1246 }
1247
1248 /**
1249 * Get current transmit attenuation for the selected channel.
1250 * @param phy The AD9361 current state structure.
1251 * @param ch The desired channel number (TX1, TX2).
1252 * Accepted values in 2x2 mode:
1253 * TX1 (0)
1254 * TX2 (1)
1255 * Accepted values in 1x1 mode:
1256 * TX1 (0)
1257 * @param attenuation_mdb A variable to store the attenuation value (mdB).
1258 * @return 0 in case of success, negative error code otherwise.
1259 */
1260 int32_t ad9361_get_tx_attenuation (struct ad9361_rf_phy *phy,
1261 uint8_t ch, uint32_t *attenuation_db)
1262 {
1263 int32_t ret;
1264
1265 if ((phy->pdata->rx2tx2 == 0) && (ch == TX2)) {
1266 printf("%s : TX2 is an invalid option in 1x1 mode!\n", __func__);
1267 return -1;
1268 }
1269
1270 ret = ad9361_get_tx_atten(phy,
1271 ad9361_1rx1tx_channel_map(phy, true,
1272 ch + 1));
1273
1274 if(ret < 0)
1275 return ret;
1276 *attenuation_db = ret;
1277
1278 return 0;
1279 }
1280
1281 /**
1282 * Set the TX RF bandwidth.
1283 * @param phy The AD9361 current state structure.
1284 * @param bandwidth_hz The desired bandwidth (Hz).
1285 * Example:
1286 * 18000000 (18 MHz)
1287 * @return 0 in case of success, negative error code otherwise.
1288 *
1289 * Note: This function will/may affect the data path.
1290 */
1291 int32_t ad9361_set_tx_rf_bandwidth (struct ad9361_rf_phy *phy,
1292 uint32_t bandwidth_hz)
1293 {
1294 int32_t ret = 0;
1295
1296 bandwidth_hz = ad9361_validate_rf_bw(phy, bandwidth_hz);
1297
1298 if (phy->current_tx_bw_Hz != bandwidth_hz)
1299 ret = ad9361_update_rf_bandwidth(phy,
1300 phy->current_rx_bw_Hz, bandwidth_hz);
1301 else
1302 ret = 0;
1303
1304 return ret;
1305 }
1306
1307 /**
1308 * Get the TX RF bandwidth.
1309 * @param phy The AD9361 current state structure.
1310 * @param bandwidth_hz A variable to store the bandwidth value (Hz).
1311 * @return 0 in case of success, negative error code otherwise.
1312 */
1313 int32_t ad9361_get_tx_rf_bandwidth (struct ad9361_rf_phy *phy,
1314 uint32_t *bandwidth_hz)
1315 {
1316 *bandwidth_hz = phy->current_tx_bw_Hz;
1317
1318 return 0;
1319 }
1320
1321 /**
1322 * Set the TX sampling frequency.
1323 * @param phy The AD9361 current state structure.
1324 * @param sampling_freq_hz The desired frequency (Hz).
1325 * Example:
1326 * 30720000 (30.72 MHz)
1327 * @return 0 in case of success, negative error code otherwise.
1328 *
1329 * Note: This function will/may affect the data path.
1330 */
1331 int32_t ad9361_set_tx_sampling_freq (struct ad9361_rf_phy *phy,
1332 uint32_t sampling_freq_hz)
1333 {
1334 int32_t ret;
1335 uint32_t rx[6], tx[6];
1336
1337 ret = ad9361_calculate_rf_clock_chain(phy, sampling_freq_hz,
1338 phy->rate_governor, rx, tx);
1339 if (ret < 0)
1340 return ret;
1341
1342 ad9361_set_trx_clock_chain(phy, rx, tx);
1343
1344 ret = ad9361_update_rf_bandwidth(phy, phy->current_rx_bw_Hz,
1345 phy->current_tx_bw_Hz);
1346
1347 return ret;
1348 }
1349
1350 /**
1351 * Get current TX sampling frequency.
1352 * @param phy The AD9361 current state structure.
1353 * @param sampling_freq_hz A variable to store the frequency value (Hz).
1354 * @return 0 in case of success, negative error code otherwise.
1355 */
1356 int32_t ad9361_get_tx_sampling_freq (struct ad9361_rf_phy *phy,
1357 uint32_t *sampling_freq_hz)
1358 {
1359 *sampling_freq_hz = (uint32_t)clk_get_rate(phy,
1360 phy->ref_clk_scale[TX_SAMPL_CLK]);
1361
1362 return 0;
1363 }
1364
1365 /**
1366 * Set the TX LO frequency.
1367 * @param phy The AD9361 current state structure.
1368 * @param lo_freq_hz The desired frequency (Hz).
1369 * Example:
1370 * 2400000000 (2.4 GHz)
1371 * @return 0 in case of success, negative error code otherwise.
1372 *
1373 * Note: This function will/may affect the data path.
1374 */
1375 int32_t ad9361_set_tx_lo_freq (struct ad9361_rf_phy *phy,
1376 uint64_t lo_freq_hz)
1377 {
1378 int32_t ret;
1379
1380 ret = clk_set_rate(phy, phy->ref_clk_scale[TX_RFPLL],
1381 ad9361_to_clk(lo_freq_hz));
1382
1383 return ret;
1384 }
1385
1386 /**
1387 * Get current TX LO frequency.
1388 * @param phy The AD9361 current state structure.
1389 * @param lo_freq_hz A variable to store the frequency value (Hz).
1390 * @return 0 in case of success, negative error code otherwise.
1391 */
1392 int32_t ad9361_get_tx_lo_freq (struct ad9361_rf_phy *phy,
1393 uint64_t *lo_freq_hz)
1394 {
1395 *lo_freq_hz = ad9361_from_clk(clk_get_rate(phy,
1396 phy->ref_clk_scale[TX_RFPLL]));
1397
1398 return 0;
1399 }
1400
1401 /**
1402 * Switch between internal and external LO.
1403 * @param phy The AD9361 state structure.
1404 * @param int_ext The selected lo (INT_LO, EXT_LO).
1405 * Accepted values:
1406 * INT_LO
1407 * EXT_LO
1408 * @return 0 in case of success, negative error code otherwise.
1409 */
1410 int32_t ad9361_set_tx_lo_int_ext(struct ad9361_rf_phy *phy, uint8_t int_ext)
1411 {
1412 if ((phy->dev_sel == ID_AD9363A) && (int_ext = EXT_LO)) {
1413 printf("%s : EXT_LO is not supported by AD9363!\n", __func__);
1414 return -1;
1415 }
1416
1417 phy->pdata->use_ext_tx_lo = int_ext;
1418
1419 return ad9361_clk_mux_set_parent(phy->ref_clk_scale[TX_RFPLL], int_ext);
1420 }
1421
1422 /**
1423 * Set the TX FIR filter configuration.
1424 * @param phy The AD9361 current state structure.
1425 * @param fir_cfg FIR filter configuration.
1426 * @return 0 in case of success, negative error code otherwise.
1427 *
1428 * Note: This function will/may affect the data path.
1429 */
1430 int32_t ad9361_set_tx_fir_config (struct ad9361_rf_phy *phy,
1431 AD9361_TXFIRConfig fir_cfg)
1432 {
1433 int32_t ret;
1434
1435 phy->tx_fir_int = fir_cfg.tx_int;
1436 ret = ad9361_load_fir_filter_coef(phy, (enum fir_dest)fir_cfg.tx,
1437 fir_cfg.tx_gain, fir_cfg.tx_coef_size, fir_cfg.tx_coef);
1438
1439 return ret;
1440 }
1441
1442 /**
1443 * Get the TX FIR filter configuration.
1444 * @param phy The AD9361 current state structure.
1445 * @param tx_ch The selected TX channel (TX1, TX2).
1446 * Accepted values:
1447 * TX1 (0)
1448 * TX2 (1)
1449 * @param fir_cfg FIR filter configuration output file.
1450 * @return 0 in case of success, negative error code otherwise.
1451 */
1452 int32_t ad9361_get_tx_fir_config(struct ad9361_rf_phy *phy, uint8_t tx_ch, AD9361_TXFIRConfig *fir_cfg)
1453 {
1454 int32_t ret;
1455 uint32_t fir_conf;
1456 uint8_t index;
1457
1458 tx_ch += 1;
1459
1460 ret = ad9361_spi_read(phy->spi, REG_TX_FILTER_CONF);
1461 if(ret < 0)
1462 return ret;
1463 fir_conf = ret;
1464 fir_cfg->tx_coef_size = (((fir_conf & FIR_NUM_TAPS(7)) >> 5) + 1) * 16;
1465 fir_cfg->tx_gain = -6 * (fir_conf & TX_FIR_GAIN_6DB);
1466 fir_cfg->tx = tx_ch;
1467
1468 fir_conf &= ~FIR_SELECT(3);
1469 fir_conf |= FIR_SELECT(tx_ch) | FIR_START_CLK;
1470 ad9361_spi_write(phy->spi, REG_TX_FILTER_CONF, fir_conf);
1471
1472 for(index = 0; index < 128; index++)
1473 {
1474 ad9361_spi_write(phy->spi, REG_TX_FILTER_COEF_ADDR, index);
1475 ret = ad9361_spi_read(phy->spi, REG_TX_FILTER_COEF_READ_DATA_1);
1476 if(ret < 0)
1477 return ret;
1478 fir_cfg->tx_coef[index] = ret;
1479 ret = ad9361_spi_read(phy->spi, REG_TX_FILTER_COEF_READ_DATA_2);
1480 if(ret < 0)
1481 return ret;
1482 fir_cfg->tx_coef[index] |= (ret << 8);
1483 }
1484
1485 fir_conf &= ~FIR_START_CLK;
1486 ad9361_spi_write(phy->spi, REG_TX_FILTER_CONF, fir_conf);
1487
1488 fir_cfg->tx_int = phy->tx_fir_int;
1489
1490 return 0;
1491 }
1492
1493 /**
1494 * Enable/disable the TX FIR filter.
1495 * @param phy The AD9361 current state structure.
1496 * @param en_dis The option (ENABLE, DISABLE).
1497 * Accepted values:
1498 * ENABLE (1)
1499 * DISABLE (0)
1500 * @return 0 in case of success, negative error code otherwise.
1501 *
1502 * Note: This function will/may affect the data path.
1503 */
1504 int32_t ad9361_set_tx_fir_en_dis (struct ad9361_rf_phy *phy,
1505 uint8_t en_dis)
1506 {
1507 int32_t ret = 0;
1508
1509 if(phy->bypass_tx_fir == !en_dis)
1510 return ret;
1511
1512 phy->bypass_tx_fir = !en_dis;
1513 ret = ad9361_validate_enable_fir(phy);
1514 if (ret < 0) {
1515 phy->bypass_tx_fir = true;
1516 }
1517
1518 return ret;
1519 }
1520
1521 /**
1522 * Get the status of the TX FIR filter.
1523 * @param phy The AD9361 current state structure.
1524 * @param en_dis The enable/disable status buffer.
1525 * @return 0 in case of success, negative error code otherwise.
1526 */
1527 int32_t ad9361_get_tx_fir_en_dis (struct ad9361_rf_phy *phy,
1528 uint8_t *en_dis)
1529 {
1530 *en_dis = !phy->bypass_tx_fir;
1531
1532 return 0;
1533 }
1534
1535 /**
1536 * Get the TX RSSI for the selected channel (TX_MON should be enabled).
1537 * @param phy The AD9361 current state structure.
1538 * @param ch The desired channel (TX1, TX2).
1539 * Accepted values:
1540 * TX1 (0)
1541 * TX2 (1)
1542 * @param rssi_db_x_1000 A variable to store the RSSI.
1543 * @return 0 in case of success, negative error code otherwise.
1544 */
1545 int32_t ad9361_get_tx_rssi (struct ad9361_rf_phy *phy,
1546 uint8_t ch,
1547 uint32_t *rssi_db_x_1000)
1548 {
1549 uint8_t reg_val_buf[3];
1550 uint32_t val;
1551 int32_t ret;
1552
1553 ret = ad9361_spi_readm(phy->spi, REG_TX_RSSI_LSB,
1554 reg_val_buf, ARRAY_SIZE(reg_val_buf));
1555 if (ret < 0) {
1556 return ret;
1557 }
1558
1559 switch (ch) {
1560 case 0:
1561 val = (reg_val_buf[2] << 1) | (reg_val_buf[0] & TX_RSSI_1);
1562 break;
1563 case 1:
1564 val = (reg_val_buf[1] << 1) | ((reg_val_buf[0] & TX_RSSI_2) >> 1);
1565 break;
1566 default:
1567 return -EINVAL;
1568 }
1569
1570 val *= RSSI_RESOLUTION;
1571
1572 *rssi_db_x_1000 = ((val / RSSI_MULTIPLIER) * 1000) +
1573 (val % RSSI_MULTIPLIER);
1574
1575 return 0;
1576 }
1577
1578 /**
1579 * Set the TX RF output port.
1580 * @param phy The AD9361 current state structure.
1581 * @param mode The RF port.
1582 * Accepted values:
1583 * TXA (0)
1584 * TXB (1)
1585 * @return 0 in case of success, negative error code otherwise.
1586 */
1587 int32_t ad9361_set_tx_rf_port_output (struct ad9361_rf_phy *phy,
1588 uint32_t mode)
1589 {
1590 int32_t ret;
1591
1592 phy->pdata->rf_tx_output_sel = mode;
1593
1594 ret = ad9361_rf_port_setup(phy, true,
1595 phy->pdata->rf_rx_input_sel,
1596 phy->pdata->rf_tx_output_sel);
1597
1598 return ret;
1599 }
1600
1601 /**
1602 * Get the selected TX RF output port.
1603 * @param phy The AD9361 current state structure.
1604 * @param mode The RF port.
1605 * @return 0 in case of success, negative error code otherwise.
1606 */
1607 int32_t ad9361_get_tx_rf_port_output (struct ad9361_rf_phy *phy,
1608 uint32_t *mode)
1609 {
1610 *mode = phy->pdata->rf_tx_output_sel;
1611
1612 return 0;
1613 }
1614
1615 /**
1616 * Enable/disable the auto calibration.
1617 * @param phy The AD9361 current state structure.
1618 * @param en_dis The option (ENABLE, DISABLE).
1619 * Accepted values:
1620 * ENABLE (1)
1621 * DISABLE (0)
1622 * @return 0 in case of success, negative error code otherwise.
1623 */
1624 int32_t ad9361_set_tx_auto_cal_en_dis (struct ad9361_rf_phy *phy, uint8_t en_dis)
1625 {
1626 if (en_dis == 0)
1627 phy->auto_cal_en = 0;
1628 else
1629 phy->auto_cal_en = 1;
1630
1631 return 0;
1632 }
1633
1634 /**
1635 * Get the status of the auto calibration flag.
1636 * @param phy The AD9361 current state structure.
1637 * @param en_dis The enable/disable status buffer.
1638 * @return 0 in case of success, negative error code otherwise.
1639 */
1640 int32_t ad9361_get_tx_auto_cal_en_dis (struct ad9361_rf_phy *phy, uint8_t *en_dis)
1641 {
1642 *en_dis = phy->auto_cal_en;
1643
1644 return 0;
1645 }
1646
1647 /**
1648 * Store TX fastlock profile.
1649 * To create a profile tune the synthesizer (ad9361_set_tx_lo_freq()) and then
1650 * call this function specifying the target profile number.
1651 * @param phy The AD9361 state structure.
1652 * @param profile The profile number (0 - 7).
1653 * Accepted values:
1654 * 0 - 7
1655 * @return 0 in case of success, negative error code otherwise.
1656 */
1657 int32_t ad9361_tx_fastlock_store(struct ad9361_rf_phy *phy, uint32_t profile)
1658 {
1659 return ad9361_fastlock_store(phy, 1, profile);
1660 }
1661
1662 /**
1663 * Recall specified TX fastlock profile.
1664 * When in fastlock pin select mode (init_param->tx_fastlock_pincontrol_enable),
1665 * the function needs to be called before then the pin-control can be used.
1666 * @param phy The AD9361 state structure.
1667 * @param profile The profile number (0 - 7).
1668 * Accepted values:
1669 * 0 - 7
1670 * @return 0 in case of success, negative error code otherwise.
1671 */
1672 int32_t ad9361_tx_fastlock_recall(struct ad9361_rf_phy *phy, uint32_t profile)
1673 {
1674 return ad9361_fastlock_recall(phy, 1, profile);
1675 }
1676
1677 /**
1678 * Load TX fastlock profile. A previously saved profile can be loaded in any
1679 * of the 8 available slots.
1680 * @param phy The AD9361 state structure.
1681 * @param profile The profile number (0 - 7).
1682 * Accepted values:
1683 * 0 - 7
1684 * @param values Fastlock profile program data.
1685 * Example:
1686 * val0,val1,val2,…,val15
1687 * @return 0 in case of success, negative error code otherwise.
1688 */
1689 int32_t ad9361_tx_fastlock_load(struct ad9361_rf_phy *phy, uint32_t profile, uint8_t *values)
1690 {
1691 return ad9361_fastlock_load(phy, 1, profile, values);
1692 }
1693
1694 /**
1695 * Save TX fastlock profile. In order to use more than 8 Profiles, an existing
1696 * profile can be read back and stored by the user application.
1697 * @param phy The AD9361 state structure.
1698 * @param profile The profile number (0 - 7).
1699 * Accepted values:
1700 * 0 - 7
1701 * @param values Fastlock profile program data.
1702 * @return 0 in case of success, negative error code otherwise.
1703 */
1704 int32_t ad9361_tx_fastlock_save(struct ad9361_rf_phy *phy, uint32_t profile, uint8_t *values)
1705 {
1706 return ad9361_fastlock_save(phy, 1, profile, values);
1707 }
1708
1709 /**
1710 * Set the RX and TX path rates.
1711 * @param phy The AD9361 state structure.
1712 * @param rx_path_clks RX path rates buffer.
1713 * @param tx_path_clks TX path rates buffer.
1714 * @return 0 in case of success, negative error code otherwise.
1715 *
1716 * Note: This function will/may affect the data path.
1717 */
1718 int32_t ad9361_set_trx_path_clks(struct ad9361_rf_phy *phy,
1719 uint32_t *rx_path_clks,
1720 uint32_t *tx_path_clks)
1721 {
1722 int32_t ret;
1723
1724 ret = ad9361_set_trx_clock_chain(phy, rx_path_clks, tx_path_clks);
1725 if (ret < 0)
1726 return ret;
1727
1728 ret = ad9361_update_rf_bandwidth(phy, phy->current_rx_bw_Hz,
1729 phy->current_tx_bw_Hz);
1730
1731 return ret;
1732 }
1733
1734 /**
1735 * Get the RX and TX path rates.
1736 * @param phy The AD9361 state structure.
1737 * @param rx_path_clks RX path rates buffer.
1738 * @param tx_path_clks TX path rates buffer.
1739 * @return 0 in case of success, negative error code otherwise.
1740 */
1741 int32_t ad9361_get_trx_path_clks(struct ad9361_rf_phy *phy,
1742 uint32_t *rx_path_clks,
1743 uint32_t *tx_path_clks)
1744 {
1745 return ad9361_get_trx_clock_chain(phy, rx_path_clks, tx_path_clks);
1746 }
1747
1748 /**
1749 * Set the number of channels mode.
1750 * @param phy The AD9361 state structure.
1751 * @param ch_mode Number of channels mode (MODE_1x1, MODE_2x2).
1752 * Accepted values:
1753 * MODE_1x1 (1)
1754 * MODE_2x2 (2)
1755 * @return 0 in case of success, negative error code otherwise.
1756 */
1757 int32_t ad9361_set_no_ch_mode(struct ad9361_rf_phy *phy, uint8_t no_ch_mode)
1758 {
1759 switch (no_ch_mode) {
1760 case 1:
1761 phy->pdata->rx2tx2 = 0;
1762 break;
1763 case 2:
1764 phy->pdata->rx2tx2 = 1;
1765 break;
1766 default:
1767 return -EINVAL;
1768 }
1769
1770 #ifndef AXI_ADC_NOT_PRESENT
1771 phy->adc_conv->chip_info = &axiadc_chip_info_tbl[phy->pdata->rx2tx2 ? ID_AD9361 : ID_AD9364];
1772 #endif
1773 ad9361_reset(phy);
1774 ad9361_spi_write(phy->spi, REG_SPI_CONF, SOFT_RESET | _SOFT_RESET);
1775 ad9361_spi_write(phy->spi, REG_SPI_CONF, 0x0);
1776
1777 phy->clks[TX_REFCLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[TX_REFCLK], phy->clk_refin->rate);
1778 phy->clks[TX_REFCLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[TX_REFCLK], phy->clk_refin->rate);
1779 phy->clks[RX_REFCLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[RX_REFCLK], phy->clk_refin->rate);
1780 phy->clks[BB_REFCLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[BB_REFCLK], phy->clk_refin->rate);
1781 phy->clks[BBPLL_CLK]->rate = ad9361_bbpll_recalc_rate(phy->ref_clk_scale[BBPLL_CLK], phy->clks[BB_REFCLK]->rate);
1782 phy->clks[ADC_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[ADC_CLK], phy->clks[BBPLL_CLK]->rate);
1783 phy->clks[R2_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[R2_CLK], phy->clks[ADC_CLK]->rate);
1784 phy->clks[R1_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[R1_CLK], phy->clks[R2_CLK]->rate);
1785 phy->clks[CLKRF_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[CLKRF_CLK], phy->clks[R1_CLK]->rate);
1786 phy->clks[RX_SAMPL_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[RX_SAMPL_CLK], phy->clks[CLKRF_CLK]->rate);
1787 phy->clks[DAC_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[DAC_CLK], phy->clks[ADC_CLK]->rate);
1788 phy->clks[T2_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[T2_CLK], phy->clks[DAC_CLK]->rate);
1789 phy->clks[T1_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[T1_CLK], phy->clks[T2_CLK]->rate);
1790 phy->clks[CLKTF_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[CLKTF_CLK], phy->clks[T1_CLK]->rate);
1791 phy->clks[TX_SAMPL_CLK]->rate = ad9361_clk_factor_recalc_rate(phy->ref_clk_scale[TX_SAMPL_CLK], phy->clks[CLKTF_CLK]->rate);
1792 phy->clks[RX_RFPLL_INT]->rate = ad9361_rfpll_int_recalc_rate(phy->ref_clk_scale[RX_RFPLL_INT], phy->clks[RX_REFCLK]->rate);
1793 phy->clks[TX_RFPLL_INT]->rate = ad9361_rfpll_int_recalc_rate(phy->ref_clk_scale[TX_RFPLL_INT], phy->clks[TX_REFCLK]->rate);
1794 phy->clks[RX_RFPLL_DUMMY]->rate = ad9361_rfpll_dummy_recalc_rate(phy->ref_clk_scale[RX_RFPLL_DUMMY]);
1795 phy->clks[TX_RFPLL_DUMMY]->rate = ad9361_rfpll_dummy_recalc_rate(phy->ref_clk_scale[TX_RFPLL_DUMMY]);
1796 phy->clks[RX_RFPLL]->rate = ad9361_rfpll_recalc_rate(phy->ref_clk_scale[RX_RFPLL]);
1797 phy->clks[TX_RFPLL]->rate = ad9361_rfpll_recalc_rate(phy->ref_clk_scale[TX_RFPLL]);
1798
1799 #ifndef AXI_ADC_NOT_PRESENT
1800 axiadc_init(phy);
1801 #endif
1802 ad9361_setup(phy);
1803 #ifndef AXI_ADC_NOT_PRESENT
1804 /* platform specific wrapper to call ad9361_post_setup() */
1805 axiadc_post_setup(phy);
1806 #endif
1807
1808 return 0;
1809 }
1810
1811 /**
1812 * Do multi chip synchronization.
1813 * @param phy_master The AD9361 Master state structure.
1814 * @param phy_slave The AD9361 Slave state structure.
1815 * @return 0 in case of success, negative error code otherwise.
1816 *
1817 * Note: This function will/may affect the data path.
1818 */
1819 int32_t ad9361_do_mcs(struct ad9361_rf_phy *phy_master, struct ad9361_rf_phy *phy_slave)
1820 {
1821 uint32_t ensm_mode;
1822 int32_t step;
1823 int32_t reg;
1824
1825 if ((phy_master->dev_sel == ID_AD9363A) ||
1826 (phy_slave->dev_sel == ID_AD9363A)) {
1827 printf("%s : MCS is not supported by AD9363!\n", __func__);
1828 return -1;
1829 }
1830
1831 reg = ad9361_spi_read(phy_master->spi, REG_RX_CLOCK_DATA_DELAY);
1832 ad9361_spi_write(phy_slave->spi, REG_RX_CLOCK_DATA_DELAY, reg);
1833 reg = ad9361_spi_read(phy_master->spi, REG_TX_CLOCK_DATA_DELAY);
1834 ad9361_spi_write(phy_slave->spi, REG_TX_CLOCK_DATA_DELAY, reg);
1835
1836 ad9361_get_en_state_machine_mode(phy_master, &ensm_mode);
1837
1838 ad9361_set_en_state_machine_mode(phy_master, ENSM_MODE_ALERT);
1839 ad9361_set_en_state_machine_mode(phy_slave, ENSM_MODE_ALERT);
1840
1841 for (step = 0; step <= 5; step++)
1842 {
1843 ad9361_mcs(phy_slave, step);
1844 ad9361_mcs(phy_master, step);
1845 mdelay(100);
1846 }
1847
1848 ad9361_set_en_state_machine_mode(phy_master, ensm_mode);
1849 ad9361_set_en_state_machine_mode(phy_slave, ensm_mode);
1850
1851 return 0;
1852 }
1853
1854 /**
1855 * Enable/disable the TRX FIR filters.
1856 * @param phy The AD9361 current state structure.
1857 * @param en_dis The option (ENABLE, DISABLE).
1858 * Accepted values:
1859 * ENABLE (1)
1860 * DISABLE (0)
1861 * @return 0 in case of success, negative error code otherwise.
1862 *
1863 * Note: This function will/may affect the data path.
1864 */
1865 int32_t ad9361_set_trx_fir_en_dis (struct ad9361_rf_phy *phy,
1866 uint8_t en_dis)
1867 {
1868 int32_t ret = 0;
1869
1870 if ((phy->bypass_rx_fir == phy->bypass_tx_fir) &&
1871 (phy->bypass_rx_fir == !en_dis))
1872 return ret;
1873
1874 phy->bypass_rx_fir = !en_dis;
1875 phy->bypass_tx_fir = !en_dis;
1876 ret = ad9361_validate_enable_fir(phy);
1877 if (ret < 0) {
1878 phy->bypass_rx_fir = true;
1879 phy->bypass_tx_fir = true;
1880 }
1881
1882 return ret;
1883 }
1884
1885 /**
1886 * Set the OSR rate governor.
1887 * @param phy The AD9361 current state structure.
1888 * @param rate_gov OSR rate governor (highest, nominal).
1889 * Accepted values:
1890 * HIGHEST_OSR (0 - highest OSR)
1891 * NOMINAL_OSR (1 - nominal)
1892 * @return 0 in case of success, negative error code otherwise.
1893 */
1894 int32_t ad9361_set_trx_rate_gov (struct ad9361_rf_phy *phy, uint32_t rate_gov)
1895 {
1896 if (rate_gov == 0)
1897 phy->rate_governor = 0;
1898 else
1899 phy->rate_governor = 1;
1900
1901 return 0;
1902 }
1903
1904 /**
1905 * Get the OSR rate governor.
1906 * @param phy The AD9361 current state structure.
1907 * @param rate_gov Option buffer.
1908 * @return 0 in case of success, negative error code otherwise.
1909 */
1910 int32_t ad9361_get_trx_rate_gov (struct ad9361_rf_phy *phy, uint32_t *rate_gov)
1911 {
1912 *rate_gov = phy->rate_governor;
1913
1914 return 0;
1915 }
1916
1917 /**
1918 * Perform the selected calibration.
1919 * @param phy The AD9361 state structure.
1920 * @param cal The selected calibration (TX_QUAD_CAL, RFDC_CAL).
1921 * Accepted values:
1922 * TX_QUAD_CAL
1923 * RFDC_CAL
1924 * @param arg For TX_QUAD_CAL - the optional RX phase value overwrite (set to zero).
1925 * @return 0 in case of success, negative error code otherwise.
1926 *
1927 * Note: This function will/may affect the data path.
1928 */
1929 int32_t ad9361_do_calib(struct ad9361_rf_phy *phy, uint32_t cal, int32_t arg)
1930 {
1931 return ad9361_do_calib_run(phy, cal, arg);
1932 }
1933
1934 /**
1935 * Load and enable TRX FIR filters configurations.
1936 * @param phy The AD9361 current state structure.
1937 * @param rx_fir_cfg RX FIR filter configuration.
1938 * @param tx_fir_cfg TX FIR filter configuration.
1939 * @return 0 in case of success, negative error code otherwise.
1940 *
1941 * Note: This function will/may affect the data path.
1942 */
1943 int32_t ad9361_trx_load_enable_fir(struct ad9361_rf_phy *phy,
1944 AD9361_RXFIRConfig rx_fir_cfg,
1945 AD9361_TXFIRConfig tx_fir_cfg)
1946 {
1947 int32_t rtx = -1, rrx = -1;
1948
1949 phy->filt_rx_bw_Hz = 0;
1950 phy->filt_tx_bw_Hz = 0;
1951 phy->filt_valid = false;
1952
1953 if (tx_fir_cfg.tx_path_clks[TX_SAMPL_FREQ]) {
1954 memcpy(phy->filt_tx_path_clks, tx_fir_cfg.tx_path_clks,
1955 sizeof(phy->filt_tx_path_clks));
1956 rtx = 0;
1957 }
1958
1959 if (rx_fir_cfg.rx_path_clks[RX_SAMPL_FREQ]) {
1960 memcpy(phy->filt_rx_path_clks, rx_fir_cfg.rx_path_clks,
1961 sizeof(phy->filt_rx_path_clks));
1962 rrx = 0;
1963 }
1964
1965 if (tx_fir_cfg.tx_bandwidth) {
1966 phy->filt_tx_bw_Hz = tx_fir_cfg.tx_bandwidth;
1967 }
1968
1969 if (rx_fir_cfg.rx_bandwidth) {
1970 phy->filt_rx_bw_Hz = rx_fir_cfg.rx_bandwidth;
1971 }
1972
1973 ad9361_set_tx_fir_config(phy, tx_fir_cfg);
1974 ad9361_set_rx_fir_config(phy, rx_fir_cfg);
1975
1976 if (!(rrx | rtx))
1977 phy->filt_valid = true;
1978
1979 ad9361_set_trx_fir_en_dis(phy, 1);
1980
1981 return 0;
1982 }
1983
1984 /**
1985 * Do DCXO coarse tuning.
1986 * @param phy The AD9361 current state structure.
1987 * @param coarse The DCXO coarse tuning value.
1988 * @return 0 in case of success, negative error code otherwise.
1989 */
1990 int32_t ad9361_do_dcxo_tune_coarse(struct ad9361_rf_phy *phy,
1991 uint32_t coarse)
1992 {
1993 phy->pdata->dcxo_coarse = coarse;
1994
1995 return ad9361_set_dcxo_tune(phy, phy->pdata->dcxo_coarse,
1996 phy->pdata->dcxo_fine);
1997 }
1998
1999 /**
2000 * Do DCXO fine tuning.
2001 * @param phy The AD9361 current state structure.
2002 * @param fine The DCXO fine tuning value.
2003 * @return 0 in case of success, negative error code otherwise.
2004 */
2005 int32_t ad9361_do_dcxo_tune_fine(struct ad9361_rf_phy *phy,
2006 uint32_t fine)
2007 {
2008 phy->pdata->dcxo_fine = fine;
2009
2010 return ad9361_set_dcxo_tune(phy, phy->pdata->dcxo_coarse,
2011 phy->pdata->dcxo_fine);
2012 }