53 buf[0] = (reg >> 16) & 0xff;
54 buf[1] = (reg >> 8) & 0xff;
55 buf[2] = (reg >> 0) & 0xff;
62 dev_warn(&state->
i2c->dev,
"%s: i2c wr failed=%d reg=%06x " \
63 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
74 u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
83 .addr = state->
cfg.i2c_addr,
94 dev_warn(&state->
i2c->dev,
"%s: i2c rd failed=%d reg=%06x " \
95 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
106 return af9033_wr_regs(state, reg, &val, 1);
112 return af9033_rd_regs(state, reg, val, 1);
124 ret = af9033_rd_regs(state, reg, &tmp, 1);
133 return af9033_wr_regs(state, reg, &val, 1);
143 ret = af9033_rd_regs(state, reg, &tmp, 1);
150 for (i = 0; i < 8; i++) {
151 if ((mask >> i) & 0x01)
163 dev_dbg(&state->
i2c->dev,
"%s: a=%d b=%d x=%d\n", __func__, a, b, x);
170 for (
i = 0;
i <
x;
i++) {
178 r = (
c << (
u32)x) +
r;
180 dev_dbg(&state->
i2c->dev,
"%s: a=%d b=%d x=%d r=%d r=%x\n",
181 __func__, a, b, x, r, r);
199 u32 adc_cw, clock_cw;
201 { 0x80fb24, 0x00, 0x08 },
202 { 0x80004c, 0x00, 0xff },
203 { 0x00f641, state->
cfg.tuner, 0xff },
204 { 0x80f5ca, 0x01, 0x01 },
205 { 0x80f715, 0x01, 0x01 },
206 { 0x00f41f, 0x04, 0x04 },
207 { 0x00f41a, 0x01, 0x01 },
208 { 0x80f731, 0x00, 0x01 },
209 { 0x00d91e, 0x00, 0x01 },
210 { 0x00d919, 0x00, 0x01 },
211 { 0x80f732, 0x00, 0x01 },
212 { 0x00d91f, 0x00, 0x01 },
213 { 0x00d91a, 0x00, 0x01 },
214 { 0x80f730, 0x00, 0x01 },
215 { 0x80f778, 0x00, 0xff },
216 { 0x80f73c, 0x01, 0x01 },
217 { 0x80f776, 0x00, 0x01 },
218 { 0x00d8fd, 0x01, 0xff },
219 { 0x00d830, 0x01, 0xff },
220 { 0x00d831, 0x00, 0xff },
221 { 0x00d832, 0x00, 0xff },
224 { 0x00d827, 0x00, 0xff },
225 { 0x00d829, 0x00, 0xff },
229 clock_cw = af9033_div(state, state->
cfg.clock, 1000000ul, 19ul);
230 buf[0] = (clock_cw >> 0) & 0xff;
231 buf[1] = (clock_cw >> 8) & 0xff;
232 buf[2] = (clock_cw >> 16) & 0xff;
233 buf[3] = (clock_cw >> 24) & 0xff;
235 dev_dbg(&state->
i2c->dev,
"%s: clock=%d clock_cw=%08x\n",
236 __func__, state->
cfg.clock, clock_cw);
238 ret = af9033_wr_regs(state, 0x800025, buf, 4);
243 for (i = 0; i <
ARRAY_SIZE(clock_adc_lut); i++) {
244 if (clock_adc_lut[i].
clock == state->
cfg.clock)
248 adc_cw = af9033_div(state, clock_adc_lut[i].
adc, 1000000ul, 19ul);
249 buf[0] = (adc_cw >> 0) & 0xff;
250 buf[1] = (adc_cw >> 8) & 0xff;
251 buf[2] = (adc_cw >> 16) & 0xff;
253 dev_dbg(&state->
i2c->dev,
"%s: adc=%d adc_cw=%06x\n",
254 __func__, clock_adc_lut[i].adc, adc_cw);
256 ret = af9033_wr_regs(state, 0x80f1cd, buf, 3);
262 ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val,
270 ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01);
274 ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01);
278 ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01);
282 ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01);
288 dev_dbg(&state->
i2c->dev,
"%s: load ofsm settings\n", __func__);
291 for (i = 0; i < len; i++) {
292 ret = af9033_wr_reg(state, init[i].reg, init[i].val);
298 dev_dbg(&state->
i2c->dev,
"%s: load tuner specific settings\n",
300 switch (state->
cfg.tuner) {
303 init = tuner_init_tua9001;
307 init = tuner_init_fc0011;
311 init = tuner_init_mxl5007t;
315 init = tuner_init_tda18218;
319 init = tuner_init_fc2580;
322 dev_dbg(&state->
i2c->dev,
"%s: unsupported tuner ID=%d\n",
323 __func__, state->
cfg.tuner);
328 for (i = 0; i < len; i++) {
329 ret = af9033_wr_reg(state, init[i].reg, init[i].val);
339 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
350 ret = af9033_wr_reg(state, 0x80004c, 1);
354 ret = af9033_wr_reg(state, 0x800000, 0);
358 for (i = 100, tmp = 1; i &&
tmp; i--) {
359 ret = af9033_rd_reg(state, 0x80004c, &tmp);
366 dev_dbg(&state->
i2c->dev,
"%s: loop=%d\n", __func__, i);
373 ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08);
380 ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
384 ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01);
392 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
397 static int af9033_get_tune_settings(
struct dvb_frontend *fe,
412 u8 tmp, buf[3], bandwidth_reg_val;
413 u32 if_frequency, freq_cw, adc_freq;
415 dev_dbg(&state->
i2c->dev,
"%s: frequency=%d bandwidth_hz=%d\n",
421 bandwidth_reg_val = 0x00;
424 bandwidth_reg_val = 0x01;
427 bandwidth_reg_val = 0x02;
430 dev_dbg(&state->
i2c->dev,
"%s: invalid bandwidth_hz\n",
437 if (fe->
ops.tuner_ops.set_params)
438 fe->
ops.tuner_ops.set_params(fe);
443 if (coeff_lut[i].
clock == state->
cfg.clock &&
448 ret = af9033_wr_regs(state, 0x800001,
449 coeff_lut[i].val,
sizeof(coeff_lut[i].val));
454 spec_inv = state->
cfg.spec_inv ? -1 : 1;
456 for (i = 0; i <
ARRAY_SIZE(clock_adc_lut); i++) {
457 if (clock_adc_lut[i].
clock == state->
cfg.clock)
460 adc_freq = clock_adc_lut[
i].adc;
463 if (fe->
ops.tuner_ops.get_if_frequency)
464 fe->
ops.tuner_ops.get_if_frequency(fe, &if_frequency);
468 sampling_freq = if_frequency;
470 while (sampling_freq > (adc_freq / 2))
471 sampling_freq -= adc_freq;
473 if (sampling_freq >= 0)
478 freq_cw = af9033_div(state, sampling_freq, adc_freq, 23ul);
481 freq_cw = 0x800000 - freq_cw;
484 ret = af9033_rd_reg(state, 0x800045, &tmp);
491 buf[0] = (freq_cw >> 0) & 0xff;
492 buf[1] = (freq_cw >> 8) & 0xff;
493 buf[2] = (freq_cw >> 16) & 0x7f;
494 ret = af9033_wr_regs(state, 0x800029, buf, 3);
501 ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03);
505 ret = af9033_wr_reg(state, 0x800040, 0x00);
509 ret = af9033_wr_reg(state, 0x800047, 0x00);
513 ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01);
522 ret = af9033_wr_reg(state, 0x80004b, tmp);
526 ret = af9033_wr_reg(state, 0x800000, 0x00);
533 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
548 ret = af9033_rd_regs(state, 0x80f900, buf,
sizeof(buf));
552 switch ((buf[0] >> 0) & 3) {
561 switch ((buf[1] >> 0) & 3) {
576 switch ((buf[2] >> 0) & 7) {
591 switch ((buf[3] >> 0) & 3) {
603 switch ((buf[4] >> 0) & 3) {
615 switch ((buf[6] >> 0) & 7) {
636 switch ((buf[7] >> 0) & 7) {
660 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
674 ret = af9033_rd_reg(state, 0x800047, &tmp);
684 ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01);
693 ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01);
706 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
720 ret = af9033_rd_regs(state, 0x80002c, buf, 3);
724 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
727 ret = af9033_rd_reg(state, 0x80f903, &tmp);
731 switch ((tmp >> 0) & 3) {
734 snr_lut = qpsk_snr_lut;
738 snr_lut = qam16_snr_lut;
742 snr_lut = qam64_snr_lut;
748 for (i = 0; i < len; i++) {
749 tmp = snr_lut[
i].snr;
751 if (snr_val < snr_lut[i].val)
760 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
765 static int af9033_read_signal_strength(
struct dvb_frontend *fe,
u16 *strength)
772 ret = af9033_rd_reg(state, 0x800048, &strength2);
777 *strength = strength2 * 0xffff / 100;
782 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
787 static int af9033_update_ch_stat(
struct af9033_state *state)
790 u32 err_cnt, bit_cnt;
796 ret = af9033_rd_regs(state, 0x800032, buf,
sizeof(buf));
800 abort_cnt = (buf[1] << 8) + buf[0];
802 err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
804 bit_cnt = (buf[6] << 8) + buf[5];
806 if (bit_cnt < abort_cnt) {
808 state->
ber = 0xffffffff;
811 bit_cnt -= (
u32)abort_cnt;
813 state->
ber = 0xffffffff;
815 err_cnt -= (
u32)abort_cnt * 8 * 8;
817 state->
ber = err_cnt * (0xffffffff / bit_cnt);
820 state->
ucb += abort_cnt;
826 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
836 ret = af9033_update_ch_stat(state);
845 static int af9033_read_ucblocks(
struct dvb_frontend *fe,
u32 *ucblocks)
850 ret = af9033_update_ch_stat(state);
854 *ucblocks = state->
ucb;
864 dev_dbg(&state->
i2c->dev,
"%s: enable=%d\n", __func__, enable);
866 ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01);
873 dev_dbg(&state->
i2c->dev,
"%s: failed=%d\n", __func__, ret);
898 if (state->
cfg.clock != 12000000) {
899 dev_err(&state->
i2c->dev,
"%s: af9033: unsupported clock=%d, " \
900 "only 12000000 Hz is supported currently\n",
901 KBUILD_MODNAME, state->
cfg.clock);
906 ret = af9033_rd_regs(state, 0x0083e9, &buf[0], 4);
910 ret = af9033_rd_regs(state, 0x804191, &buf[4], 4);
914 dev_info(&state->
i2c->dev,
"%s: firmware version: LINK=%d.%d.%d.%d " \
915 "OFDM=%d.%d.%d.%d\n", KBUILD_MODNAME, buf[0], buf[1],
916 buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
919 ret = af9033_wr_reg(state, 0x80004c, 1);
923 ret = af9033_wr_reg(state, 0x800000, 0);
928 switch (state->
cfg.ts_mode) {
943 state->
fe.demodulator_priv =
state;
956 .name =
"Afatech AF9033 (DVB-T)",
957 .frequency_min = 174000000,
958 .frequency_max = 862000000,
959 .frequency_stepsize = 250000,
960 .frequency_tolerance = 0,
978 .release = af9033_release,
981 .sleep = af9033_sleep,
983 .get_tune_settings = af9033_get_tune_settings,
984 .set_frontend = af9033_set_frontend,
985 .get_frontend = af9033_get_frontend,
987 .read_status = af9033_read_status,
988 .read_snr = af9033_read_snr,
989 .read_signal_strength = af9033_read_signal_strength,
990 .read_ber = af9033_read_ber,
991 .read_ucblocks = af9033_read_ucblocks,
993 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,