1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux-DVB Driver for DiBcom's DiB0070 base-band RF Tuner.
5 * Copyright (C) 2005-9 DiBcom (http://www.dibcom.fr/)
7 * This code is more or less generated from another driver, please
8 * excuse some codingstyle oddities.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/i2c.h>
16 #include <linux/mutex.h>
18 #include <media/dvb_frontend.h>
21 #include "dibx000_common.h"
24 module_param(debug, int, 0644);
25 MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
27 #define dprintk(fmt, arg...) do { \
29 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
33 #define DIB0070_P1D 0x00
34 #define DIB0070_P1F 0x01
35 #define DIB0070_P1G 0x03
36 #define DIB0070S_P1A 0x02
38 struct dib0070_state {
39 struct i2c_adapter *i2c;
40 struct dvb_frontend *fe;
41 const struct dib0070_config *cfg;
45 enum frontend_tune_state tune_state;
48 /* for the captrim binary search */
56 const struct dib0070_tuning *current_tune_table_index;
57 const struct dib0070_lna_match *lna_match;
60 u16 wbd_offset_3_3[2];
62 /* for the I2C transfer */
63 struct i2c_msg msg[2];
64 u8 i2c_write_buffer[3];
65 u8 i2c_read_buffer[2];
66 struct mutex i2c_buffer_lock;
69 static u16 dib0070_read_reg(struct dib0070_state *state, u8 reg)
73 if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
74 dprintk("could not acquire lock\n");
78 state->i2c_write_buffer[0] = reg;
80 memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
81 state->msg[0].addr = state->cfg->i2c_address;
82 state->msg[0].flags = 0;
83 state->msg[0].buf = state->i2c_write_buffer;
84 state->msg[0].len = 1;
85 state->msg[1].addr = state->cfg->i2c_address;
86 state->msg[1].flags = I2C_M_RD;
87 state->msg[1].buf = state->i2c_read_buffer;
88 state->msg[1].len = 2;
90 if (i2c_transfer(state->i2c, state->msg, 2) != 2) {
91 pr_warn("DiB0070 I2C read failed\n");
94 ret = (state->i2c_read_buffer[0] << 8)
95 | state->i2c_read_buffer[1];
97 mutex_unlock(&state->i2c_buffer_lock);
101 static int dib0070_write_reg(struct dib0070_state *state, u8 reg, u16 val)
105 if (mutex_lock_interruptible(&state->i2c_buffer_lock) < 0) {
106 dprintk("could not acquire lock\n");
109 state->i2c_write_buffer[0] = reg;
110 state->i2c_write_buffer[1] = val >> 8;
111 state->i2c_write_buffer[2] = val & 0xff;
113 memset(state->msg, 0, sizeof(struct i2c_msg));
114 state->msg[0].addr = state->cfg->i2c_address;
115 state->msg[0].flags = 0;
116 state->msg[0].buf = state->i2c_write_buffer;
117 state->msg[0].len = 3;
119 if (i2c_transfer(state->i2c, state->msg, 1) != 1) {
120 pr_warn("DiB0070 I2C write failed\n");
125 mutex_unlock(&state->i2c_buffer_lock);
129 #define HARD_RESET(state) do { \
130 state->cfg->sleep(state->fe, 0); \
131 if (state->cfg->reset) { \
132 state->cfg->reset(state->fe,1); msleep(10); \
133 state->cfg->reset(state->fe,0); msleep(10); \
137 static int dib0070_set_bandwidth(struct dvb_frontend *fe)
139 struct dib0070_state *state = fe->tuner_priv;
140 u16 tmp = dib0070_read_reg(state, 0x02) & 0x3fff;
142 if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 7000)
144 else if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 6000)
146 else if (state->fe->dtv_property_cache.bandwidth_hz/1000 > 5000)
151 dib0070_write_reg(state, 0x02, tmp);
153 /* sharpen the BB filter in ISDB-T to have higher immunity to adjacent channels */
154 if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT) {
155 u16 value = dib0070_read_reg(state, 0x17);
157 dib0070_write_reg(state, 0x17, value & 0xfffc);
158 tmp = dib0070_read_reg(state, 0x01) & 0x01ff;
159 dib0070_write_reg(state, 0x01, tmp | (60 << 9));
161 dib0070_write_reg(state, 0x17, value);
166 static int dib0070_captrim(struct dib0070_state *state, enum frontend_tune_state *tune_state)
172 if (*tune_state == CT_TUNER_STEP_0) {
173 dib0070_write_reg(state, 0x0f, 0xed10);
174 dib0070_write_reg(state, 0x17, 0x0034);
176 dib0070_write_reg(state, 0x18, 0x0032);
177 state->step = state->captrim = state->fcaptrim = 64;
178 state->adc_diff = 3000;
181 *tune_state = CT_TUNER_STEP_1;
182 } else if (*tune_state == CT_TUNER_STEP_1) {
184 dib0070_write_reg(state, 0x14, state->lo4 | state->captrim);
187 *tune_state = CT_TUNER_STEP_2;
188 } else if (*tune_state == CT_TUNER_STEP_2) {
190 adc = dib0070_read_reg(state, 0x19);
192 dprintk("CAPTRIM=%d; ADC = %hd (ADC) & %dmV\n", state->captrim,
193 adc, (u32)adc * (u32)1800 / (u32)1024);
203 if (adc < state->adc_diff) {
204 dprintk("CAPTRIM=%d is closer to target (%hd/%hd)\n",
205 state->captrim, adc, state->adc_diff);
206 state->adc_diff = adc;
207 state->fcaptrim = state->captrim;
209 state->captrim += (step_sign * state->step);
211 if (state->step >= 1)
212 *tune_state = CT_TUNER_STEP_1;
214 *tune_state = CT_TUNER_STEP_3;
216 } else if (*tune_state == CT_TUNER_STEP_3) {
217 dib0070_write_reg(state, 0x14, state->lo4 | state->fcaptrim);
218 dib0070_write_reg(state, 0x18, 0x07ff);
219 *tune_state = CT_TUNER_STEP_4;
225 static int dib0070_set_ctrl_lo5(struct dvb_frontend *fe, u8 vco_bias_trim, u8 hf_div_trim, u8 cp_current, u8 third_order_filt)
227 struct dib0070_state *state = fe->tuner_priv;
228 u16 lo5 = (third_order_filt << 14) | (0 << 13) | (1 << 12) | (3 << 9) | (cp_current << 6) | (hf_div_trim << 3) | (vco_bias_trim << 0);
230 dprintk("CTRL_LO5: 0x%x\n", lo5);
231 return dib0070_write_reg(state, 0x15, lo5);
234 void dib0070_ctrl_agc_filter(struct dvb_frontend *fe, u8 open)
236 struct dib0070_state *state = fe->tuner_priv;
239 dib0070_write_reg(state, 0x1b, 0xff00);
240 dib0070_write_reg(state, 0x1a, 0x0000);
242 dib0070_write_reg(state, 0x1b, 0x4112);
243 if (state->cfg->vga_filter != 0) {
244 dib0070_write_reg(state, 0x1a, state->cfg->vga_filter);
245 dprintk("vga filter register is set to %x\n", state->cfg->vga_filter);
247 dib0070_write_reg(state, 0x1a, 0x0009);
251 EXPORT_SYMBOL(dib0070_ctrl_agc_filter);
252 struct dib0070_tuning {
253 u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
263 struct dib0070_lna_match {
264 u32 max_freq; /* for every frequency less than or equal to that field: this information is correct */
268 static const struct dib0070_tuning dib0070s_tuning_table[] = {
269 { 570000, 2, 1, 3, 6, 6, 2, 0x4000 | 0x0800 }, /* UHF */
270 { 700000, 2, 0, 2, 4, 2, 2, 0x4000 | 0x0800 },
271 { 863999, 2, 1, 2, 4, 2, 2, 0x4000 | 0x0800 },
272 { 1500000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 }, /* LBAND */
273 { 1600000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 },
274 { 2000000, 0, 1, 1, 2, 2, 4, 0x2000 | 0x0400 },
275 { 0xffffffff, 0, 0, 8, 1, 2, 1, 0x8000 | 0x1000 }, /* SBAND */
278 static const struct dib0070_tuning dib0070_tuning_table[] = {
279 { 115000, 1, 0, 7, 24, 2, 1, 0x8000 | 0x1000 }, /* FM below 92MHz cannot be tuned */
280 { 179500, 1, 0, 3, 16, 2, 1, 0x8000 | 0x1000 }, /* VHF */
281 { 189999, 1, 1, 3, 16, 2, 1, 0x8000 | 0x1000 },
282 { 250000, 1, 0, 6, 12, 2, 1, 0x8000 | 0x1000 },
283 { 569999, 2, 1, 5, 6, 2, 2, 0x4000 | 0x0800 }, /* UHF */
284 { 699999, 2, 0, 1, 4, 2, 2, 0x4000 | 0x0800 },
285 { 863999, 2, 1, 1, 4, 2, 2, 0x4000 | 0x0800 },
286 { 0xffffffff, 0, 1, 0, 2, 2, 4, 0x2000 | 0x0400 }, /* LBAND or everything higher than UHF */
289 static const struct dib0070_lna_match dib0070_lna_flip_chip[] = {
290 { 180000, 0 }, /* VHF */
294 { 550000, 0 }, /* UHF */
298 { 1500000, 0 }, /* LBAND or everything higher than UHF */
304 static const struct dib0070_lna_match dib0070_lna[] = {
305 { 180000, 0 }, /* VHF */
309 { 550000, 2 }, /* UHF */
314 { 1500000, 0 }, /* LBAND or everything higher than UHF */
321 static int dib0070_tune_digital(struct dvb_frontend *fe)
323 struct dib0070_state *state = fe->tuner_priv;
325 const struct dib0070_tuning *tune;
326 const struct dib0070_lna_match *lna_match;
328 enum frontend_tune_state *tune_state = &state->tune_state;
329 int ret = 10; /* 1ms is the default delay most of the time */
331 u8 band = (u8)BAND_OF_FREQUENCY(fe->dtv_property_cache.frequency/1000);
332 u32 freq = fe->dtv_property_cache.frequency/1000 + (band == BAND_VHF ? state->cfg->freq_offset_khz_vhf : state->cfg->freq_offset_khz_uhf);
334 #ifdef CONFIG_SYS_ISDBT
335 if (state->fe->dtv_property_cache.delivery_system == SYS_ISDBT && state->fe->dtv_property_cache.isdbt_sb_mode == 1)
336 if (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2)
337 && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1)))
338 || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
339 && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == (state->fe->dtv_property_cache.isdbt_sb_segment_count / 2)))
340 || (((state->fe->dtv_property_cache.isdbt_sb_segment_count % 2) == 0)
341 && (state->fe->dtv_property_cache.isdbt_sb_segment_idx == ((state->fe->dtv_property_cache.isdbt_sb_segment_count / 2) + 1))))
344 if (state->current_rf != freq) {
346 switch (state->revision) {
348 tune = dib0070s_tuning_table;
349 lna_match = dib0070_lna;
352 tune = dib0070_tuning_table;
353 if (state->cfg->flip_chip)
354 lna_match = dib0070_lna_flip_chip;
356 lna_match = dib0070_lna;
359 while (freq > tune->max_freq) /* find the right one */
361 while (freq > lna_match->max_freq) /* find the right one */
364 state->current_tune_table_index = tune;
365 state->lna_match = lna_match;
368 if (*tune_state == CT_TUNER_START) {
369 dprintk("Tuning for Band: %d (%d kHz)\n", band, freq);
370 if (state->current_rf != freq) {
372 u32 FBDiv, Rest, FREF, VCOF_kHz;
375 state->current_rf = freq;
376 state->lo4 = (state->current_tune_table_index->vco_band << 11) | (state->current_tune_table_index->hfdiv << 7);
379 dib0070_write_reg(state, 0x17, 0x30);
382 VCOF_kHz = state->current_tune_table_index->vco_multi * freq * 2;
386 REFDIV = (u8) ((state->cfg->clock_khz + 9999) / 10000);
389 REFDIV = (u8) ((state->cfg->clock_khz) / 1000);
392 REFDIV = (u8) (state->cfg->clock_khz / 10000);
395 FREF = state->cfg->clock_khz / REFDIV;
399 switch (state->revision) {
401 FBDiv = (VCOF_kHz / state->current_tune_table_index->presc / FREF);
402 Rest = (VCOF_kHz / state->current_tune_table_index->presc) - FBDiv * FREF;
408 FBDiv = (freq / (FREF / 2));
409 Rest = 2 * freq - FBDiv * FREF;
415 else if (Rest < 2 * LPF)
417 else if (Rest > (FREF - LPF)) {
420 } else if (Rest > (FREF - 2 * LPF))
421 Rest = FREF - 2 * LPF;
422 Rest = (Rest * 6528) / (FREF / 10);
426 state->lo4 |= (1 << 14) | (1 << 12);
431 dib0070_write_reg(state, 0x11, (u16)FBDiv);
432 dib0070_write_reg(state, 0x12, (Den << 8) | REFDIV);
433 dib0070_write_reg(state, 0x13, (u16) Rest);
435 if (state->revision == DIB0070S_P1A) {
437 if (band == BAND_SBAND) {
438 dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
439 dib0070_write_reg(state, 0x1d, 0xFFFF);
441 dib0070_set_ctrl_lo5(fe, 5, 4, 3, 1);
444 dib0070_write_reg(state, 0x20,
445 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001 | state->current_tune_table_index->tuner_enable);
447 dprintk("REFDIV: %u, FREF: %d\n", REFDIV, FREF);
448 dprintk("FBDIV: %d, Rest: %d\n", FBDiv, Rest);
449 dprintk("Num: %u, Den: %u, SD: %d\n", (u16)Rest, Den,
450 (state->lo4 >> 12) & 0x1);
451 dprintk("HFDIV code: %u\n",
452 state->current_tune_table_index->hfdiv);
453 dprintk("VCO = %u\n",
454 state->current_tune_table_index->vco_band);
455 dprintk("VCOF: ((%u*%d) << 1))\n",
456 state->current_tune_table_index->vco_multi,
459 *tune_state = CT_TUNER_STEP_0;
460 } else { /* we are already tuned to this frequency - the configuration is correct */
461 ret = 50; /* wakeup time */
462 *tune_state = CT_TUNER_STEP_5;
464 } else if ((*tune_state > CT_TUNER_START) && (*tune_state < CT_TUNER_STEP_4)) {
466 ret = dib0070_captrim(state, tune_state);
468 } else if (*tune_state == CT_TUNER_STEP_4) {
469 const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
471 while (freq/1000 > tmp->freq) /* find the right one */
473 dib0070_write_reg(state, 0x0f,
474 (0 << 15) | (1 << 14) | (3 << 12)
475 | (tmp->wbd_gain_val << 9) | (0 << 8) | (1 << 7)
476 | (state->current_tune_table_index->wbdmux << 0));
477 state->wbd_gain_current = tmp->wbd_gain_val;
479 dib0070_write_reg(state, 0x0f,
480 (0 << 15) | (1 << 14) | (3 << 12)
481 | (6 << 9) | (0 << 8) | (1 << 7)
482 | (state->current_tune_table_index->wbdmux << 0));
483 state->wbd_gain_current = 6;
486 dib0070_write_reg(state, 0x06, 0x3fff);
487 dib0070_write_reg(state, 0x07,
488 (state->current_tune_table_index->switch_trim << 11) | (7 << 8) | (state->lna_match->lna_band << 3) | (3 << 0));
489 dib0070_write_reg(state, 0x08, (state->lna_match->lna_band << 10) | (3 << 7) | (127));
490 dib0070_write_reg(state, 0x0d, 0x0d80);
493 dib0070_write_reg(state, 0x18, 0x07ff);
494 dib0070_write_reg(state, 0x17, 0x0033);
497 *tune_state = CT_TUNER_STEP_5;
498 } else if (*tune_state == CT_TUNER_STEP_5) {
499 dib0070_set_bandwidth(fe);
500 *tune_state = CT_TUNER_STOP;
502 ret = FE_CALLBACK_TIME_NEVER; /* tuner finished, time to call again infinite */
508 static int dib0070_tune(struct dvb_frontend *fe)
510 struct dib0070_state *state = fe->tuner_priv;
513 state->tune_state = CT_TUNER_START;
516 ret = dib0070_tune_digital(fe);
517 if (ret != FE_CALLBACK_TIME_NEVER)
521 } while (state->tune_state != CT_TUNER_STOP);
526 static int dib0070_wakeup(struct dvb_frontend *fe)
528 struct dib0070_state *state = fe->tuner_priv;
529 if (state->cfg->sleep)
530 state->cfg->sleep(fe, 0);
534 static int dib0070_sleep(struct dvb_frontend *fe)
536 struct dib0070_state *state = fe->tuner_priv;
537 if (state->cfg->sleep)
538 state->cfg->sleep(fe, 1);
542 u8 dib0070_get_rf_output(struct dvb_frontend *fe)
544 struct dib0070_state *state = fe->tuner_priv;
545 return (dib0070_read_reg(state, 0x07) >> 11) & 0x3;
547 EXPORT_SYMBOL(dib0070_get_rf_output);
549 int dib0070_set_rf_output(struct dvb_frontend *fe, u8 no)
551 struct dib0070_state *state = fe->tuner_priv;
552 u16 rxrf2 = dib0070_read_reg(state, 0x07) & 0xfe7ff;
557 return dib0070_write_reg(state, 0x07, rxrf2 | (no << 11));
559 EXPORT_SYMBOL(dib0070_set_rf_output);
561 static const u16 dib0070_p1f_defaults[] =
595 0x4000 | 0x0800 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001,
600 static u16 dib0070_read_wbd_offset(struct dib0070_state *state, u8 gain)
602 u16 tuner_en = dib0070_read_reg(state, 0x20);
605 dib0070_write_reg(state, 0x18, 0x07ff);
606 dib0070_write_reg(state, 0x20, 0x0800 | 0x4000 | 0x0040 | 0x0020 | 0x0010 | 0x0008 | 0x0002 | 0x0001);
607 dib0070_write_reg(state, 0x0f, (1 << 14) | (2 << 12) | (gain << 9) | (1 << 8) | (1 << 7) | (0 << 0));
609 offset = dib0070_read_reg(state, 0x19);
610 dib0070_write_reg(state, 0x20, tuner_en);
614 static void dib0070_wbd_offset_calibration(struct dib0070_state *state)
617 for (gain = 6; gain < 8; gain++) {
618 state->wbd_offset_3_3[gain - 6] = ((dib0070_read_wbd_offset(state, gain) * 8 * 18 / 33 + 1) / 2);
619 dprintk("Gain: %d, WBDOffset (3.3V) = %hd\n", gain, state->wbd_offset_3_3[gain-6]);
623 u16 dib0070_wbd_offset(struct dvb_frontend *fe)
625 struct dib0070_state *state = fe->tuner_priv;
626 const struct dib0070_wbd_gain_cfg *tmp = state->cfg->wbd_gain;
627 u32 freq = fe->dtv_property_cache.frequency/1000;
630 while (freq/1000 > tmp->freq) /* find the right one */
632 state->wbd_gain_current = tmp->wbd_gain_val;
634 state->wbd_gain_current = 6;
636 return state->wbd_offset_3_3[state->wbd_gain_current - 6];
638 EXPORT_SYMBOL(dib0070_wbd_offset);
640 #define pgm_read_word(w) (*w)
641 static int dib0070_reset(struct dvb_frontend *fe)
643 struct dib0070_state *state = fe->tuner_priv;
649 #ifndef FORCE_SBAND_TUNER
650 if ((dib0070_read_reg(state, 0x22) >> 9) & 0x1)
651 state->revision = (dib0070_read_reg(state, 0x1f) >> 8) & 0xff;
654 #warning forcing SBAND
656 state->revision = DIB0070S_P1A;
659 dprintk("Revision: %x\n", state->revision);
661 if (state->revision == DIB0070_P1D) {
662 dprintk("Error: this driver is not to be used meant for P1D or earlier\n");
666 n = (u16 *) dib0070_p1f_defaults;
667 l = pgm_read_word(n++);
669 r = pgm_read_word(n++);
671 dib0070_write_reg(state, (u8)r, pgm_read_word(n++));
674 l = pgm_read_word(n++);
677 if (state->cfg->force_crystal_mode != 0)
678 r = state->cfg->force_crystal_mode;
679 else if (state->cfg->clock_khz >= 24000)
685 r |= state->cfg->osc_buffer_state << 3;
687 dib0070_write_reg(state, 0x10, r);
688 dib0070_write_reg(state, 0x1f, (1 << 8) | ((state->cfg->clock_pad_drive & 0xf) << 5));
690 if (state->cfg->invert_iq) {
691 r = dib0070_read_reg(state, 0x02) & 0xffdf;
692 dib0070_write_reg(state, 0x02, r | (1 << 5));
695 if (state->revision == DIB0070S_P1A)
696 dib0070_set_ctrl_lo5(fe, 2, 4, 3, 0);
698 dib0070_set_ctrl_lo5(fe, 5, 4, state->cfg->charge_pump,
699 state->cfg->enable_third_order_filter);
701 dib0070_write_reg(state, 0x01, (54 << 9) | 0xc8);
703 dib0070_wbd_offset_calibration(state);
708 static int dib0070_get_frequency(struct dvb_frontend *fe, u32 *frequency)
710 struct dib0070_state *state = fe->tuner_priv;
712 *frequency = 1000 * state->current_rf;
716 static void dib0070_release(struct dvb_frontend *fe)
718 kfree(fe->tuner_priv);
719 fe->tuner_priv = NULL;
722 static const struct dvb_tuner_ops dib0070_ops = {
724 .name = "DiBcom DiB0070",
725 .frequency_min_hz = 45 * MHz,
726 .frequency_max_hz = 860 * MHz,
727 .frequency_step_hz = 1 * kHz,
729 .release = dib0070_release,
731 .init = dib0070_wakeup,
732 .sleep = dib0070_sleep,
733 .set_params = dib0070_tune,
735 .get_frequency = dib0070_get_frequency,
736 // .get_bandwidth = dib0070_get_bandwidth
739 struct dvb_frontend *dib0070_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct dib0070_config *cfg)
741 struct dib0070_state *state = kzalloc(sizeof(struct dib0070_state), GFP_KERNEL);
748 mutex_init(&state->i2c_buffer_lock);
749 fe->tuner_priv = state;
751 if (dib0070_reset(fe) != 0)
754 pr_info("DiB0070: successfully identified\n");
755 memcpy(&fe->ops.tuner_ops, &dib0070_ops, sizeof(struct dvb_tuner_ops));
757 fe->tuner_priv = state;
762 fe->tuner_priv = NULL;
765 EXPORT_SYMBOL(dib0070_attach);
768 MODULE_DESCRIPTION("Driver for the DiBcom 0070 base-band RF Tuner");
769 MODULE_LICENSE("GPL");