Lines Matching refs:st
73 static int adf4350_sync_config(struct adf4350_state *st) in adf4350_sync_config() argument
78 if ((st->regs_hw[i] != st->regs[i]) || in adf4350_sync_config()
87 st->val = cpu_to_be32(st->regs[i] | i); in adf4350_sync_config()
88 ret = spi_write(st->spi, &st->val, 4); in adf4350_sync_config()
91 st->regs_hw[i] = st->regs[i]; in adf4350_sync_config()
92 dev_dbg(&st->spi->dev, "[%d] 0x%X\n", in adf4350_sync_config()
93 i, (u32)st->regs[i] | i); in adf4350_sync_config()
103 struct adf4350_state *st = iio_priv(indio_dev); in adf4350_reg_access() local
109 mutex_lock(&st->lock); in adf4350_reg_access()
111 st->regs[reg] = writeval & ~(BIT(0) | BIT(1) | BIT(2)); in adf4350_reg_access()
112 ret = adf4350_sync_config(st); in adf4350_reg_access()
114 *readval = st->regs_hw[reg]; in adf4350_reg_access()
117 mutex_unlock(&st->lock); in adf4350_reg_access()
122 static int adf4350_tune_r_cnt(struct adf4350_state *st, unsigned short r_cnt) in adf4350_tune_r_cnt() argument
124 struct adf4350_platform_data *pdata = st->pdata; in adf4350_tune_r_cnt()
128 st->fpfd = (st->clkin * (pdata->ref_doubler_en ? 2 : 1)) / in adf4350_tune_r_cnt()
130 } while (st->fpfd > ADF4350_MAX_FREQ_PFD); in adf4350_tune_r_cnt()
135 static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq) in adf4350_set_freq() argument
137 struct adf4350_platform_data *pdata = st->pdata; in adf4350_set_freq()
143 if (freq > ADF4350_MAX_OUT_FREQ || freq < st->min_out_freq) in adf4350_set_freq()
154 st->r4_rf_div_sel = 0; in adf4350_set_freq()
158 st->r4_rf_div_sel++; in adf4350_set_freq()
168 chspc = st->chspc; in adf4350_set_freq()
173 r_cnt = adf4350_tune_r_cnt(st, r_cnt); in adf4350_set_freq()
174 st->r1_mod = st->fpfd / chspc; in adf4350_set_freq()
180 } while ((st->r1_mod > ADF4350_MAX_MODULUS) && r_cnt); in adf4350_set_freq()
183 tmp = freq * (u64)st->r1_mod + (st->fpfd >> 1); in adf4350_set_freq()
184 do_div(tmp, st->fpfd); /* Div round closest (n + d/2)/d */ in adf4350_set_freq()
185 st->r0_fract = do_div(tmp, st->r1_mod); in adf4350_set_freq()
186 st->r0_int = tmp; in adf4350_set_freq()
187 } while (mdiv > st->r0_int); in adf4350_set_freq()
189 band_sel_div = DIV_ROUND_UP(st->fpfd, ADF4350_MAX_BANDSEL_CLK); in adf4350_set_freq()
191 if (st->r0_fract && st->r1_mod) { in adf4350_set_freq()
192 div_gcd = gcd(st->r1_mod, st->r0_fract); in adf4350_set_freq()
193 st->r1_mod /= div_gcd; in adf4350_set_freq()
194 st->r0_fract /= div_gcd; in adf4350_set_freq()
196 st->r0_fract = 0; in adf4350_set_freq()
197 st->r1_mod = 1; in adf4350_set_freq()
200 dev_dbg(&st->spi->dev, "VCO: %llu Hz, PFD %lu Hz\n" in adf4350_set_freq()
203 freq, st->fpfd, r_cnt, st->r0_int, st->r0_fract, st->r1_mod, in adf4350_set_freq()
204 1 << st->r4_rf_div_sel, prescaler ? "8/9" : "4/5", in adf4350_set_freq()
207 st->regs[ADF4350_REG0] = ADF4350_REG0_INT(st->r0_int) | in adf4350_set_freq()
208 ADF4350_REG0_FRACT(st->r0_fract); in adf4350_set_freq()
210 st->regs[ADF4350_REG1] = ADF4350_REG1_PHASE(1) | in adf4350_set_freq()
211 ADF4350_REG1_MOD(st->r1_mod) | in adf4350_set_freq()
214 st->regs[ADF4350_REG2] = in adf4350_set_freq()
224 st->regs[ADF4350_REG3] = pdata->r3_user_settings & in adf4350_set_freq()
232 st->regs[ADF4350_REG4] = in adf4350_set_freq()
234 ADF4350_REG4_RF_DIV_SEL(st->r4_rf_div_sel) | in adf4350_set_freq()
244 st->regs[ADF4350_REG5] = ADF4350_REG5_LD_PIN_MODE_DIGITAL; in adf4350_set_freq()
245 st->freq_req = freq; in adf4350_set_freq()
247 return adf4350_sync_config(st); in adf4350_set_freq()
255 struct adf4350_state *st = iio_priv(indio_dev); in adf4350_write() local
264 mutex_lock(&st->lock); in adf4350_write()
267 ret = adf4350_set_freq(st, readin); in adf4350_write()
275 if (st->clk) { in adf4350_write()
276 tmp = clk_round_rate(st->clk, readin); in adf4350_write()
281 ret = clk_set_rate(st->clk, tmp); in adf4350_write()
285 st->clkin = readin; in adf4350_write()
286 ret = adf4350_set_freq(st, st->freq_req); in adf4350_write()
292 st->chspc = readin; in adf4350_write()
296 st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN; in adf4350_write()
298 st->regs[ADF4350_REG2] &= ~ADF4350_REG2_POWER_DOWN_EN; in adf4350_write()
300 adf4350_sync_config(st); in adf4350_write()
305 mutex_unlock(&st->lock); in adf4350_write()
315 struct adf4350_state *st = iio_priv(indio_dev); in adf4350_read() local
319 mutex_lock(&st->lock); in adf4350_read()
322 val = (u64)((st->r0_int * st->r1_mod) + st->r0_fract) * in adf4350_read()
323 (u64)st->fpfd; in adf4350_read()
324 do_div(val, st->r1_mod * (1 << st->r4_rf_div_sel)); in adf4350_read()
326 if (st->lock_detect_gpiod) in adf4350_read()
327 if (!gpiod_get_value(st->lock_detect_gpiod)) { in adf4350_read()
328 dev_dbg(&st->spi->dev, "PLL un-locked\n"); in adf4350_read()
333 if (st->clk) in adf4350_read()
334 st->clkin = clk_get_rate(st->clk); in adf4350_read()
336 val = st->clkin; in adf4350_read()
339 val = st->chspc; in adf4350_read()
342 val = !!(st->regs[ADF4350_REG2] & ADF4350_REG2_POWER_DOWN_EN); in adf4350_read()
348 mutex_unlock(&st->lock); in adf4350_read()
474 struct adf4350_state *st = iio_priv(indio_dev); in adf4350_power_down() local
476 st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN; in adf4350_power_down()
477 adf4350_sync_config(st); in adf4350_power_down()
484 struct adf4350_state *st; in adf4350_probe() local
507 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in adf4350_probe()
511 st = iio_priv(indio_dev); in adf4350_probe()
517 st->spi = spi; in adf4350_probe()
518 st->pdata = pdata; in adf4350_probe()
528 mutex_init(&st->lock); in adf4350_probe()
530 st->chspc = pdata->channel_spacing; in adf4350_probe()
532 st->clk = clk; in adf4350_probe()
533 st->clkin = clk_get_rate(clk); in adf4350_probe()
535 st->clkin = pdata->clkin; in adf4350_probe()
538 st->min_out_freq = spi_get_device_id(spi)->driver_data == 4351 ? in adf4350_probe()
541 memset(st->regs_hw, 0xFF, sizeof(st->regs_hw)); in adf4350_probe()
543 st->lock_detect_gpiod = devm_gpiod_get_optional(&spi->dev, NULL, in adf4350_probe()
545 if (IS_ERR(st->lock_detect_gpiod)) in adf4350_probe()
546 return PTR_ERR(st->lock_detect_gpiod); in adf4350_probe()
549 ret = adf4350_set_freq(st, pdata->power_up_frequency); in adf4350_probe()