Lines Matching refs:ctx
30 #define mxm_dbg(ctx, fmt, arg...) dev_dbg(&ctx->client->dev, fmt, ## arg)
31 #define mxm_err(ctx, fmt, arg...) dev_err(&ctx->client->dev, fmt, ## arg)
316 static int max2175_read(struct max2175 *ctx, u8 idx, u8 *val)
321 ret = regmap_read(ctx->regmap, idx, ®val);
323 mxm_err(ctx, "read ret(%d): idx 0x%02x\n", ret, idx);
330 static int max2175_write(struct max2175 *ctx, u8 idx, u8 val)
334 ret = regmap_write(ctx->regmap, idx, val);
336 mxm_err(ctx, "write ret(%d): idx 0x%02x val 0x%02x\n",
342 static u8 max2175_read_bits(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb)
346 if (max2175_read(ctx, idx, &val))
352 static int max2175_write_bits(struct max2175 *ctx, u8 idx,
355 int ret = regmap_update_bits(ctx->regmap, idx, GENMASK(msb, lsb),
359 mxm_err(ctx, "wbits ret(%d): idx 0x%02x\n", ret, idx);
364 static int max2175_write_bit(struct max2175 *ctx, u8 idx, u8 bit, u8 newval)
366 return max2175_write_bits(ctx, idx, bit, bit, newval);
370 static int max2175_poll_timeout(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb,
375 return regmap_read_poll_timeout(ctx->regmap, idx, val,
380 static int max2175_poll_csm_ready(struct max2175 *ctx)
384 ret = max2175_poll_timeout(ctx, 69, 1, 1, 0, 50000);
386 mxm_err(ctx, "csm not ready\n");
391 #define MAX2175_IS_BAND_AM(ctx) \
392 (max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_AM)
394 #define MAX2175_IS_BAND_VHF(ctx) \
395 (max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_VHF)
397 #define MAX2175_IS_FM_MODE(ctx) \
398 (max2175_read_bits(ctx, 12, 5, 4) == 0)
400 #define MAX2175_IS_FMHD_MODE(ctx) \
401 (max2175_read_bits(ctx, 12, 5, 4) == 1)
403 #define MAX2175_IS_DAB_MODE(ctx) \
404 (max2175_read_bits(ctx, 12, 5, 4) == 2)
416 static void max2175_i2s_enable(struct max2175 *ctx, bool enable)
420 max2175_write_bits(ctx, 104, 3, 0, 2);
423 max2175_write_bits(ctx, 104, 3, 0, 9);
424 mxm_dbg(ctx, "i2s %sabled\n", enable ? "en" : "dis");
427 static void max2175_set_filter_coeffs(struct max2175 *ctx, u8 m_sel,
433 mxm_dbg(ctx, "set_filter_coeffs: m_sel %d bank %d\n", m_sel, bank);
434 max2175_write_bits(ctx, 114, 5, 4, m_sel);
441 max2175_write(ctx, 115, coeffs[i] >> 8);
442 max2175_write(ctx, 116, coeffs[i]);
443 max2175_write(ctx, 117, coeff_addr | 1 << 7);
445 max2175_write_bit(ctx, 117, 7, 0);
448 static void max2175_load_fmeu_1p2(struct max2175 *ctx)
453 max2175_write(ctx, fmeu1p2_map[i].idx, fmeu1p2_map[i].val);
455 ctx->decim_ratio = 36;
458 max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmeu);
459 max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
463 static void max2175_load_dab_1p2(struct max2175 *ctx)
468 max2175_write(ctx, dab12_map[i].idx, dab12_map[i].val);
470 ctx->decim_ratio = 1;
473 max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 2, ch_coeff_dab1);
476 static void max2175_load_fmna_1p0(struct max2175 *ctx)
481 max2175_write(ctx, fmna1p0_map[i].idx, fmna1p0_map[i].val);
484 static void max2175_load_fmna_2p0(struct max2175 *ctx)
489 max2175_write(ctx, fmna2p0_map[i].idx, fmna2p0_map[i].val);
492 static void max2175_set_bbfilter(struct max2175 *ctx)
494 if (MAX2175_IS_BAND_AM(ctx)) {
495 max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_am);
496 mxm_dbg(ctx, "set_bbfilter AM: rom %d\n", ctx->rom_bbf_bw_am);
497 } else if (MAX2175_IS_DAB_MODE(ctx)) {
498 max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_dab);
499 mxm_dbg(ctx, "set_bbfilter DAB: rom %d\n", ctx->rom_bbf_bw_dab);
501 max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_fm);
502 mxm_dbg(ctx, "set_bbfilter FM: rom %d\n", ctx->rom_bbf_bw_fm);
506 static int max2175_set_csm_mode(struct max2175 *ctx,
509 int ret = max2175_poll_csm_ready(ctx);
514 max2175_write_bits(ctx, 0, 2, 0, new_mode);
515 mxm_dbg(ctx, "set csm new mode %d\n", new_mode);
530 return max2175_poll_csm_ready(ctx);
533 static int max2175_csm_action(struct max2175 *ctx,
538 mxm_dbg(ctx, "csm_action: %d\n", action);
541 ret = max2175_set_csm_mode(ctx, MAX2175_LOAD_TO_BUFFER);
545 return max2175_set_csm_mode(ctx, MAX2175_PRESET_TUNE);
548 static int max2175_set_lo_freq(struct max2175 *ctx, u32 lo_freq)
555 band = max2175_read_bits(ctx, 5, 1, 0);
593 int_desired = lo_freq / ctx->xtal_freq;
594 frac_desired = div64_ul((u64)(lo_freq % ctx->xtal_freq) << 20,
595 ctx->xtal_freq);
598 ret = max2175_poll_csm_ready(ctx);
602 mxm_dbg(ctx, "lo_mult %u int %u frac %u\n",
606 max2175_write(ctx, 1, int_desired);
607 max2175_write_bits(ctx, 2, 3, 0, (frac_desired >> 16) & 0xf);
608 max2175_write(ctx, 3, frac_desired >> 8);
609 max2175_write(ctx, 4, frac_desired);
610 max2175_write_bits(ctx, 5, 3, 2, loband_bits);
611 max2175_write_bits(ctx, 6, 7, 6, vcodiv_bits);
628 static int max2175_set_nco_freq(struct max2175 *ctx, s32 nco_freq)
630 s32 clock_rate = ctx->xtal_freq / ctx->decim_ratio;
649 ret = max2175_poll_csm_ready(ctx);
653 mxm_dbg(ctx, "freq %d desired %lld reg %u\n",
657 max2175_write_bits(ctx, 7, 4, 0, (nco_reg >> 16) & 0x1f);
658 max2175_write(ctx, 8, nco_reg >> 8);
659 max2175_write(ctx, 9, nco_reg);
664 static int max2175_set_rf_freq_non_am_bands(struct max2175 *ctx, u64 freq,
670 mxm_dbg(ctx, "rf_freq: non AM bands\n");
672 if (MAX2175_IS_FM_MODE(ctx))
674 else if (MAX2175_IS_FMHD_MODE(ctx))
677 return max2175_set_lo_freq(ctx, freq);
679 if (MAX2175_IS_BAND_VHF(ctx) == (lo_pos == MAX2175_LO_ABOVE_DESIRED))
684 ret = max2175_set_lo_freq(ctx, adj_freq);
688 return max2175_set_nco_freq(ctx, -low_if_freq);
691 static int max2175_set_rf_freq(struct max2175 *ctx, u64 freq, u32 lo_pos)
695 if (MAX2175_IS_BAND_AM(ctx))
696 ret = max2175_set_nco_freq(ctx, freq);
698 ret = max2175_set_rf_freq_non_am_bands(ctx, freq, lo_pos);
700 mxm_dbg(ctx, "set_rf_freq: ret %d freq %llu\n", ret, freq);
705 static int max2175_tune_rf_freq(struct max2175 *ctx, u64 freq, u32 hsls)
709 ret = max2175_set_rf_freq(ctx, freq, hsls);
713 ret = max2175_csm_action(ctx, MAX2175_BUFFER_PLUS_PRESET_TUNE);
717 mxm_dbg(ctx, "tune_rf_freq: old %u new %llu\n", ctx->freq, freq);
718 ctx->freq = freq;
723 static void max2175_set_hsls(struct max2175 *ctx, u32 lo_pos)
725 mxm_dbg(ctx, "set_hsls: lo_pos %u\n", lo_pos);
727 if ((lo_pos == MAX2175_LO_BELOW_DESIRED) == MAX2175_IS_BAND_VHF(ctx))
728 max2175_write_bit(ctx, 5, 4, 1);
730 max2175_write_bit(ctx, 5, 4, 0);
733 static void max2175_set_eu_rx_mode(struct max2175 *ctx, u32 rx_mode)
737 max2175_load_fmeu_1p2(ctx);
741 max2175_load_dab_1p2(ctx);
745 if (!ctx->master)
746 max2175_write_bit(ctx, 30, 7, 1);
749 static void max2175_set_na_rx_mode(struct max2175 *ctx, u32 rx_mode)
753 max2175_load_fmna_1p0(ctx);
756 max2175_load_fmna_2p0(ctx);
760 if (!ctx->master)
761 max2175_write_bit(ctx, 30, 7, 1);
763 ctx->decim_ratio = 27;
766 max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmna);
767 max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
771 static int max2175_set_rx_mode(struct max2175 *ctx, u32 rx_mode)
773 mxm_dbg(ctx, "set_rx_mode: %u am_hiz %u\n", rx_mode, ctx->am_hiz);
774 if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
775 max2175_set_eu_rx_mode(ctx, rx_mode);
777 max2175_set_na_rx_mode(ctx, rx_mode);
779 if (ctx->am_hiz) {
780 mxm_dbg(ctx, "setting AM HiZ related config\n");
781 max2175_write_bit(ctx, 50, 5, 1);
782 max2175_write_bit(ctx, 90, 7, 1);
783 max2175_write_bits(ctx, 73, 1, 0, 2);
784 max2175_write_bits(ctx, 80, 5, 0, 33);
788 max2175_set_bbfilter(ctx);
791 max2175_set_hsls(ctx, ctx->hsls->cur.val);
794 max2175_i2s_enable(ctx, ctx->i2s_en->cur.val);
796 ctx->mode_resolved = true;
801 static int max2175_rx_mode_from_freq(struct max2175 *ctx, u32 freq, u32 *mode)
807 for (i = 0; i <= ctx->rx_mode->maximum; i++) {
808 if (ctx->rx_modes[i].band == band) {
810 mxm_dbg(ctx, "rx_mode_from_freq: freq %u mode %d\n",
819 static bool max2175_freq_rx_mode_valid(struct max2175 *ctx,
824 return (ctx->rx_modes[mode].band == band);
827 static void max2175_load_adc_presets(struct max2175 *ctx)
833 max2175_write(ctx, 146 + j + i * 55, adc_presets[i][j]);
836 static int max2175_init_power_manager(struct max2175 *ctx)
841 max2175_write_bit(ctx, 99, 2, 0);
843 max2175_write_bit(ctx, 99, 2, 1);
846 ret = max2175_poll_timeout(ctx, 69, 7, 7, 1, 50000);
848 mxm_err(ctx, "init pm failed\n");
853 static int max2175_recalibrate_adc(struct max2175 *ctx)
858 max2175_write(ctx, 150, 0xff);
859 max2175_write(ctx, 205, 0xff);
860 max2175_write(ctx, 147, 0x20);
861 max2175_write(ctx, 147, 0x00);
862 max2175_write(ctx, 202, 0x20);
863 max2175_write(ctx, 202, 0x00);
865 ret = max2175_poll_timeout(ctx, 69, 4, 3, 3, 50000);
867 mxm_err(ctx, "adc recalibration failed\n");
872 static u8 max2175_read_rom(struct max2175 *ctx, u8 row)
876 max2175_write_bit(ctx, 56, 4, 0);
877 max2175_write_bits(ctx, 56, 3, 0, row);
880 max2175_read(ctx, 58, &data);
882 max2175_write_bits(ctx, 56, 3, 0, 0);
884 mxm_dbg(ctx, "read_rom: row %d data 0x%02x\n", row, data);
889 static void max2175_load_from_rom(struct max2175 *ctx)
893 data = max2175_read_rom(ctx, 0);
894 ctx->rom_bbf_bw_am = data & 0x0f;
895 max2175_write_bits(ctx, 81, 3, 0, data >> 4);
897 data = max2175_read_rom(ctx, 1);
898 ctx->rom_bbf_bw_fm = data & 0x0f;
899 ctx->rom_bbf_bw_dab = data >> 4;
901 data = max2175_read_rom(ctx, 2);
902 max2175_write_bits(ctx, 82, 4, 0, data & 0x1f);
903 max2175_write_bits(ctx, 82, 7, 5, data >> 5);
905 data = max2175_read_rom(ctx, 3);
906 if (ctx->am_hiz) {
908 data |= (max2175_read_rom(ctx, 7) & 0x40) >> 2;
913 data |= (max2175_read_rom(ctx, 7) & 0x80) >> 3;
917 max2175_write_bits(ctx, 80, 5, 0, data + 31);
919 data = max2175_read_rom(ctx, 6);
920 max2175_write_bits(ctx, 81, 7, 6, data >> 6);
923 static void max2175_load_full_fm_eu_1p0(struct max2175 *ctx)
928 max2175_write(ctx, i + 1, full_fm_eu_1p0[i]);
931 ctx->decim_ratio = 36;
934 static void max2175_load_full_fm_na_1p0(struct max2175 *ctx)
939 max2175_write(ctx, i + 1, full_fm_na_1p0[i]);
942 ctx->decim_ratio = 27;
945 static int max2175_core_init(struct max2175 *ctx, u32 refout_bits)
950 if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
951 max2175_load_full_fm_eu_1p0(ctx);
953 max2175_load_full_fm_na_1p0(ctx);
956 if (!ctx->master)
957 max2175_write_bit(ctx, 30, 7, 1);
959 mxm_dbg(ctx, "refout_bits %u\n", refout_bits);
962 max2175_write_bits(ctx, 56, 7, 5, refout_bits);
965 max2175_write_bit(ctx, 99, 1, 0);
967 max2175_write_bit(ctx, 99, 1, 1);
970 max2175_load_adc_presets(ctx);
973 ret = max2175_init_power_manager(ctx);
978 ret = max2175_recalibrate_adc(ctx);
983 max2175_load_from_rom(ctx);
985 if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
987 max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
989 max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
993 max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
995 max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
998 mxm_dbg(ctx, "core initialized\n");
1003 static void max2175_s_ctrl_rx_mode(struct max2175 *ctx, u32 rx_mode)
1006 max2175_set_rx_mode(ctx, rx_mode);
1008 mxm_dbg(ctx, "s_ctrl_rx_mode: %u curr freq %u\n", rx_mode, ctx->freq);
1011 if (max2175_freq_rx_mode_valid(ctx, rx_mode, ctx->freq))
1012 max2175_tune_rf_freq(ctx, ctx->freq, ctx->hsls->cur.val);
1015 max2175_tune_rf_freq(ctx, ctx->rx_modes[rx_mode].freq,
1016 ctx->hsls->cur.val);
1021 struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
1023 mxm_dbg(ctx, "s_ctrl: id 0x%x, val %u\n", ctrl->id, ctrl->val);
1026 max2175_i2s_enable(ctx, ctrl->val);
1029 max2175_set_hsls(ctx, ctrl->val);
1032 max2175_s_ctrl_rx_mode(ctx, ctrl->val);
1039 static u32 max2175_get_lna_gain(struct max2175 *ctx)
1041 enum max2175_band band = max2175_read_bits(ctx, 5, 1, 0);
1045 return max2175_read_bits(ctx, 51, 3, 0);
1047 return max2175_read_bits(ctx, 50, 3, 0);
1049 return max2175_read_bits(ctx, 52, 5, 0);
1057 struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
1061 ctrl->val = max2175_get_lna_gain(ctx);
1064 ctrl->val = max2175_read_bits(ctx, 49, 4, 0);
1067 ctrl->val = (max2175_read_bits(ctx, 60, 7, 6) == 3);
1074 static int max2175_set_freq_and_mode(struct max2175 *ctx, u32 freq)
1080 ret = max2175_rx_mode_from_freq(ctx, freq, &rx_mode);
1084 mxm_dbg(ctx, "set_freq_and_mode: freq %u rx_mode %d\n", freq, rx_mode);
1087 max2175_set_rx_mode(ctx, rx_mode);
1088 ctx->rx_mode->cur.val = rx_mode;
1091 return max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
1097 struct max2175 *ctx = max2175_from_sd(sd);
1101 mxm_dbg(ctx, "s_freq: new %u curr %u, mode_resolved %d\n",
1102 vf->frequency, ctx->freq, ctx->mode_resolved);
1107 freq = clamp(vf->frequency, ctx->bands_rf->rangelow,
1108 ctx->bands_rf->rangehigh);
1111 if (ctx->mode_resolved &&
1112 max2175_freq_rx_mode_valid(ctx, ctx->rx_mode->cur.val, freq))
1113 ret = max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
1116 ret = max2175_set_freq_and_mode(ctx, freq);
1118 mxm_dbg(ctx, "s_freq: ret %d curr %u mode_resolved %d mode %u\n",
1119 ret, ctx->freq, ctx->mode_resolved, ctx->rx_mode->cur.val);
1127 struct max2175 *ctx = max2175_from_sd(sd);
1134 vf->frequency = ctx->freq;
1142 struct max2175 *ctx = max2175_from_sd(sd);
1147 *band = *ctx->bands_rf;
1154 struct max2175 *ctx = max2175_from_sd(sd);
1162 vt->rangelow = ctx->bands_rf->rangelow;
1163 vt->rangehigh = ctx->bands_rf->rangehigh;
1282 struct max2175 *ctx;
1323 ctx = devm_kzalloc(&client->dev, sizeof(*ctx), GFP_KERNEL);
1324 if (ctx == NULL)
1327 sd = &ctx->sd;
1328 ctx->master = master;
1329 ctx->am_hiz = am_hiz;
1330 ctx->mode_resolved = false;
1331 ctx->regmap = regmap;
1332 ctx->xtal_freq = clk_get_rate(clk);
1333 dev_info(&client->dev, "xtal freq %luHz\n", ctx->xtal_freq);
1336 ctx->client = client;
1341 hdl = &ctx->ctrl_hdl;
1346 ctx->lna_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
1349 ctx->lna_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
1351 ctx->if_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
1354 ctx->if_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
1356 ctx->pll_lock = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
1359 ctx->pll_lock->flags |= (V4L2_CTRL_FLAG_VOLATILE |
1361 ctx->i2s_en = v4l2_ctrl_new_custom(hdl, &max2175_i2s_en, NULL);
1362 ctx->hsls = v4l2_ctrl_new_custom(hdl, &max2175_hsls, NULL);
1364 if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
1365 ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
1367 ctx->rx_modes = eu_rx_modes;
1368 ctx->bands_rf = &eu_bands_rf;
1370 ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
1372 ctx->rx_modes = na_rx_modes;
1373 ctx->bands_rf = &na_bands_rf;
1375 ctx->sd.ctrl_handler = &ctx->ctrl_hdl;
1378 ctx->freq = ctx->bands_rf->rangelow;
1388 ret = max2175_core_init(ctx, refout_bits);
1401 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
1409 struct max2175 *ctx = max2175_from_sd(sd);
1411 v4l2_ctrl_handler_free(&ctx->ctrl_hdl);