Lines Matching full:st
74 static int adf4350_sync_config(struct adf4350_state *st) in adf4350_sync_config() argument
79 if ((st->regs_hw[i] != st->regs[i]) || in adf4350_sync_config()
88 st->val = cpu_to_be32(st->regs[i] | i); in adf4350_sync_config()
89 ret = spi_write(st->spi, &st->val, 4); in adf4350_sync_config()
92 st->regs_hw[i] = st->regs[i]; in adf4350_sync_config()
93 dev_dbg(&st->spi->dev, "[%d] 0x%X\n", in adf4350_sync_config()
94 i, (u32)st->regs[i] | i); in adf4350_sync_config()
104 struct adf4350_state *st = iio_priv(indio_dev); in adf4350_reg_access() local
110 mutex_lock(&st->lock); in adf4350_reg_access()
112 st->regs[reg] = writeval & ~(BIT(0) | BIT(1) | BIT(2)); in adf4350_reg_access()
113 ret = adf4350_sync_config(st); in adf4350_reg_access()
115 *readval = st->regs_hw[reg]; in adf4350_reg_access()
118 mutex_unlock(&st->lock); in adf4350_reg_access()
123 static int adf4350_tune_r_cnt(struct adf4350_state *st, unsigned short r_cnt) in adf4350_tune_r_cnt() argument
125 struct adf4350_platform_data *pdata = st->pdata; in adf4350_tune_r_cnt()
129 st->fpfd = (st->clkin * (pdata->ref_doubler_en ? 2 : 1)) / in adf4350_tune_r_cnt()
131 } while (st->fpfd > ADF4350_MAX_FREQ_PFD); in adf4350_tune_r_cnt()
136 static int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq) in adf4350_set_freq() argument
138 struct adf4350_platform_data *pdata = st->pdata; in adf4350_set_freq()
144 if (freq > ADF4350_MAX_OUT_FREQ || freq < st->min_out_freq) in adf4350_set_freq()
155 st->r4_rf_div_sel = 0; in adf4350_set_freq()
159 st->r4_rf_div_sel++; in adf4350_set_freq()
169 chspc = st->chspc; in adf4350_set_freq()
174 r_cnt = adf4350_tune_r_cnt(st, r_cnt); in adf4350_set_freq()
175 st->r1_mod = st->fpfd / chspc; in adf4350_set_freq()
181 } while ((st->r1_mod > ADF4350_MAX_MODULUS) && r_cnt); in adf4350_set_freq()
184 tmp = freq * (u64)st->r1_mod + (st->fpfd >> 1); in adf4350_set_freq()
185 do_div(tmp, st->fpfd); /* Div round closest (n + d/2)/d */ in adf4350_set_freq()
186 st->r0_fract = do_div(tmp, st->r1_mod); in adf4350_set_freq()
187 st->r0_int = tmp; in adf4350_set_freq()
188 } while (mdiv > st->r0_int); in adf4350_set_freq()
190 band_sel_div = DIV_ROUND_UP(st->fpfd, ADF4350_MAX_BANDSEL_CLK); in adf4350_set_freq()
192 if (st->r0_fract && st->r1_mod) { in adf4350_set_freq()
193 div_gcd = gcd(st->r1_mod, st->r0_fract); in adf4350_set_freq()
194 st->r1_mod /= div_gcd; in adf4350_set_freq()
195 st->r0_fract /= div_gcd; in adf4350_set_freq()
197 st->r0_fract = 0; in adf4350_set_freq()
198 st->r1_mod = 1; in adf4350_set_freq()
201 dev_dbg(&st->spi->dev, "VCO: %llu Hz, PFD %lu Hz\n" in adf4350_set_freq()
204 freq, st->fpfd, r_cnt, st->r0_int, st->r0_fract, st->r1_mod, in adf4350_set_freq()
205 1 << st->r4_rf_div_sel, prescaler ? "8/9" : "4/5", in adf4350_set_freq()
208 st->regs[ADF4350_REG0] = ADF4350_REG0_INT(st->r0_int) | in adf4350_set_freq()
209 ADF4350_REG0_FRACT(st->r0_fract); in adf4350_set_freq()
211 st->regs[ADF4350_REG1] = ADF4350_REG1_PHASE(1) | in adf4350_set_freq()
212 ADF4350_REG1_MOD(st->r1_mod) | in adf4350_set_freq()
215 st->regs[ADF4350_REG2] = in adf4350_set_freq()
225 st->regs[ADF4350_REG3] = pdata->r3_user_settings & in adf4350_set_freq()
233 st->regs[ADF4350_REG4] = in adf4350_set_freq()
235 ADF4350_REG4_RF_DIV_SEL(st->r4_rf_div_sel) | in adf4350_set_freq()
245 st->regs[ADF4350_REG5] = ADF4350_REG5_LD_PIN_MODE_DIGITAL; in adf4350_set_freq()
246 st->freq_req = freq; in adf4350_set_freq()
248 return adf4350_sync_config(st); in adf4350_set_freq()
256 struct adf4350_state *st = iio_priv(indio_dev); in adf4350_write() local
265 mutex_lock(&st->lock); in adf4350_write()
268 ret = adf4350_set_freq(st, readin); in adf4350_write()
276 if (st->clk) { in adf4350_write()
277 tmp = clk_round_rate(st->clk, readin); in adf4350_write()
282 ret = clk_set_rate(st->clk, tmp); in adf4350_write()
286 st->clkin = readin; in adf4350_write()
287 ret = adf4350_set_freq(st, st->freq_req); in adf4350_write()
293 st->chspc = readin; in adf4350_write()
297 st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN; in adf4350_write()
299 st->regs[ADF4350_REG2] &= ~ADF4350_REG2_POWER_DOWN_EN; in adf4350_write()
301 adf4350_sync_config(st); in adf4350_write()
306 mutex_unlock(&st->lock); in adf4350_write()
316 struct adf4350_state *st = iio_priv(indio_dev); in adf4350_read() local
320 mutex_lock(&st->lock); in adf4350_read()
323 val = (u64)((st->r0_int * st->r1_mod) + st->r0_fract) * in adf4350_read()
324 (u64)st->fpfd; in adf4350_read()
325 do_div(val, st->r1_mod * (1 << st->r4_rf_div_sel)); in adf4350_read()
327 if (st->lock_detect_gpiod) in adf4350_read()
328 if (!gpiod_get_value(st->lock_detect_gpiod)) { in adf4350_read()
329 dev_dbg(&st->spi->dev, "PLL un-locked\n"); in adf4350_read()
334 if (st->clk) in adf4350_read()
335 st->clkin = clk_get_rate(st->clk); in adf4350_read()
337 val = st->clkin; in adf4350_read()
340 val = st->chspc; in adf4350_read()
343 val = !!(st->regs[ADF4350_REG2] & ADF4350_REG2_POWER_DOWN_EN); in adf4350_read()
349 mutex_unlock(&st->lock); in adf4350_read()
476 struct adf4350_state *st; in adf4350_probe() local
503 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); in adf4350_probe()
509 st = iio_priv(indio_dev); in adf4350_probe()
511 st->reg = devm_regulator_get(&spi->dev, "vcc"); in adf4350_probe()
512 if (!IS_ERR(st->reg)) { in adf4350_probe()
513 ret = regulator_enable(st->reg); in adf4350_probe()
519 st->spi = spi; in adf4350_probe()
520 st->pdata = pdata; in adf4350_probe()
530 mutex_init(&st->lock); in adf4350_probe()
532 st->chspc = pdata->channel_spacing; in adf4350_probe()
534 st->clk = clk; in adf4350_probe()
535 st->clkin = clk_get_rate(clk); in adf4350_probe()
537 st->clkin = pdata->clkin; in adf4350_probe()
540 st->min_out_freq = spi_get_device_id(spi)->driver_data == 4351 ? in adf4350_probe()
543 memset(st->regs_hw, 0xFF, sizeof(st->regs_hw)); in adf4350_probe()
545 st->lock_detect_gpiod = devm_gpiod_get_optional(&spi->dev, NULL, in adf4350_probe()
547 if (IS_ERR(st->lock_detect_gpiod)) { in adf4350_probe()
548 ret = PTR_ERR(st->lock_detect_gpiod); in adf4350_probe()
553 ret = adf4350_set_freq(st, pdata->power_up_frequency); in adf4350_probe()
565 if (!IS_ERR(st->reg)) in adf4350_probe()
566 regulator_disable(st->reg); in adf4350_probe()
576 struct adf4350_state *st = iio_priv(indio_dev); in adf4350_remove() local
577 struct regulator *reg = st->reg; in adf4350_remove()
579 st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN; in adf4350_remove()
580 adf4350_sync_config(st); in adf4350_remove()
584 clk_disable_unprepare(st->clk); in adf4350_remove()