2 * Afatech AF9033 demodulator driver
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "af9033_priv.h"
24 /* Max transfer size done by I2C transfer functions */
25 #define MAX_XFER_SIZE 64
28 struct i2c_client *client;
29 struct dvb_frontend fe;
30 struct af9033_config cfg;
35 bool ts_mode_parallel;
38 enum fe_status fe_status;
39 u64 post_bit_error_prev; /* for old read_ber we return (curr - prev) */
42 u64 error_block_count;
43 u64 total_block_count;
44 struct delayed_work stat_work;
47 /* write multiple registers */
48 static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val,
52 u8 buf[MAX_XFER_SIZE];
53 struct i2c_msg msg[1] = {
55 .addr = dev->client->addr,
62 if (3 + len > sizeof(buf)) {
63 dev_warn(&dev->client->dev,
64 "i2c wr reg=%04x: len=%d is too big!\n",
69 buf[0] = (reg >> 16) & 0xff;
70 buf[1] = (reg >> 8) & 0xff;
71 buf[2] = (reg >> 0) & 0xff;
72 memcpy(&buf[3], val, len);
74 ret = i2c_transfer(dev->client->adapter, msg, 1);
78 dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n",
86 /* read multiple registers */
87 static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len)
90 u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
92 struct i2c_msg msg[2] = {
94 .addr = dev->client->addr,
99 .addr = dev->client->addr,
106 ret = i2c_transfer(dev->client->adapter, msg, 2);
110 dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n",
119 /* write single register */
120 static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val)
122 return af9033_wr_regs(dev, reg, &val, 1);
125 /* read single register */
126 static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val)
128 return af9033_rd_regs(dev, reg, val, 1);
131 /* write single register with mask */
132 static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val,
138 /* no need for read if whole reg is written */
140 ret = af9033_rd_regs(dev, reg, &tmp, 1);
149 return af9033_wr_regs(dev, reg, &val, 1);
152 /* read single register with mask */
153 static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val,
159 ret = af9033_rd_regs(dev, reg, &tmp, 1);
165 /* find position of the first bit */
166 for (i = 0; i < 8; i++) {
167 if ((mask >> i) & 0x01)
175 /* write reg val table using reg addr auto increment */
176 static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
177 const struct reg_val *tab, int tab_len)
179 #define MAX_TAB_LEN 212
181 u8 buf[1 + MAX_TAB_LEN];
183 dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len);
185 if (tab_len > sizeof(buf)) {
186 dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len);
190 for (i = 0, j = 0; i < tab_len; i++) {
193 if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
194 ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1);
207 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
212 static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x)
216 dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x);
223 for (i = 0; i < x; i++) {
231 r = (c << (u32)x) + r;
233 dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r);
238 static int af9033_init(struct dvb_frontend *fe)
240 struct af9033_dev *dev = fe->demodulator_priv;
241 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
243 const struct reg_val *init;
245 u32 adc_cw, clock_cw;
246 struct reg_val_mask tab[] = {
247 { 0x80fb24, 0x00, 0x08 },
248 { 0x80004c, 0x00, 0xff },
249 { 0x00f641, dev->cfg.tuner, 0xff },
250 { 0x80f5ca, 0x01, 0x01 },
251 { 0x80f715, 0x01, 0x01 },
252 { 0x00f41f, 0x04, 0x04 },
253 { 0x00f41a, 0x01, 0x01 },
254 { 0x80f731, 0x00, 0x01 },
255 { 0x00d91e, 0x00, 0x01 },
256 { 0x00d919, 0x00, 0x01 },
257 { 0x80f732, 0x00, 0x01 },
258 { 0x00d91f, 0x00, 0x01 },
259 { 0x00d91a, 0x00, 0x01 },
260 { 0x80f730, 0x00, 0x01 },
261 { 0x80f778, 0x00, 0xff },
262 { 0x80f73c, 0x01, 0x01 },
263 { 0x80f776, 0x00, 0x01 },
264 { 0x00d8fd, 0x01, 0xff },
265 { 0x00d830, 0x01, 0xff },
266 { 0x00d831, 0x00, 0xff },
267 { 0x00d832, 0x00, 0xff },
268 { 0x80f985, dev->ts_mode_serial, 0x01 },
269 { 0x80f986, dev->ts_mode_parallel, 0x01 },
270 { 0x00d827, 0x00, 0xff },
271 { 0x00d829, 0x00, 0xff },
272 { 0x800045, dev->cfg.adc_multiplier, 0xff },
275 /* program clock control */
276 clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul);
277 buf[0] = (clock_cw >> 0) & 0xff;
278 buf[1] = (clock_cw >> 8) & 0xff;
279 buf[2] = (clock_cw >> 16) & 0xff;
280 buf[3] = (clock_cw >> 24) & 0xff;
282 dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n",
283 dev->cfg.clock, clock_cw);
285 ret = af9033_wr_regs(dev, 0x800025, buf, 4);
289 /* program ADC control */
290 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
291 if (clock_adc_lut[i].clock == dev->cfg.clock)
294 if (i == ARRAY_SIZE(clock_adc_lut)) {
295 dev_err(&dev->client->dev,
296 "Couldn't find ADC config for clock=%d\n",
301 adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul);
302 buf[0] = (adc_cw >> 0) & 0xff;
303 buf[1] = (adc_cw >> 8) & 0xff;
304 buf[2] = (adc_cw >> 16) & 0xff;
306 dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n",
307 clock_adc_lut[i].adc, adc_cw);
309 ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3);
313 /* program register table */
314 for (i = 0; i < ARRAY_SIZE(tab); i++) {
315 ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val,
322 if (dev->cfg.dyn0_clk) {
323 ret = af9033_wr_reg(dev, 0x80fba8, 0x00);
328 /* settings for TS interface */
329 if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
330 ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01);
334 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01);
338 ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01);
342 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01);
347 /* load OFSM settings */
348 dev_dbg(&dev->client->dev, "load ofsm settings\n");
349 switch (dev->cfg.tuner) {
350 case AF9033_TUNER_IT9135_38:
351 case AF9033_TUNER_IT9135_51:
352 case AF9033_TUNER_IT9135_52:
353 len = ARRAY_SIZE(ofsm_init_it9135_v1);
354 init = ofsm_init_it9135_v1;
356 case AF9033_TUNER_IT9135_60:
357 case AF9033_TUNER_IT9135_61:
358 case AF9033_TUNER_IT9135_62:
359 len = ARRAY_SIZE(ofsm_init_it9135_v2);
360 init = ofsm_init_it9135_v2;
363 len = ARRAY_SIZE(ofsm_init);
368 ret = af9033_wr_reg_val_tab(dev, init, len);
372 /* load tuner specific settings */
373 dev_dbg(&dev->client->dev, "load tuner specific settings\n");
374 switch (dev->cfg.tuner) {
375 case AF9033_TUNER_TUA9001:
376 len = ARRAY_SIZE(tuner_init_tua9001);
377 init = tuner_init_tua9001;
379 case AF9033_TUNER_FC0011:
380 len = ARRAY_SIZE(tuner_init_fc0011);
381 init = tuner_init_fc0011;
383 case AF9033_TUNER_MXL5007T:
384 len = ARRAY_SIZE(tuner_init_mxl5007t);
385 init = tuner_init_mxl5007t;
387 case AF9033_TUNER_TDA18218:
388 len = ARRAY_SIZE(tuner_init_tda18218);
389 init = tuner_init_tda18218;
391 case AF9033_TUNER_FC2580:
392 len = ARRAY_SIZE(tuner_init_fc2580);
393 init = tuner_init_fc2580;
395 case AF9033_TUNER_FC0012:
396 len = ARRAY_SIZE(tuner_init_fc0012);
397 init = tuner_init_fc0012;
399 case AF9033_TUNER_IT9135_38:
400 len = ARRAY_SIZE(tuner_init_it9135_38);
401 init = tuner_init_it9135_38;
403 case AF9033_TUNER_IT9135_51:
404 len = ARRAY_SIZE(tuner_init_it9135_51);
405 init = tuner_init_it9135_51;
407 case AF9033_TUNER_IT9135_52:
408 len = ARRAY_SIZE(tuner_init_it9135_52);
409 init = tuner_init_it9135_52;
411 case AF9033_TUNER_IT9135_60:
412 len = ARRAY_SIZE(tuner_init_it9135_60);
413 init = tuner_init_it9135_60;
415 case AF9033_TUNER_IT9135_61:
416 len = ARRAY_SIZE(tuner_init_it9135_61);
417 init = tuner_init_it9135_61;
419 case AF9033_TUNER_IT9135_62:
420 len = ARRAY_SIZE(tuner_init_it9135_62);
421 init = tuner_init_it9135_62;
424 dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n",
430 ret = af9033_wr_reg_val_tab(dev, init, len);
434 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
435 ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01);
439 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
443 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01);
448 switch (dev->cfg.tuner) {
449 case AF9033_TUNER_IT9135_60:
450 case AF9033_TUNER_IT9135_61:
451 case AF9033_TUNER_IT9135_62:
452 ret = af9033_wr_reg(dev, 0x800000, 0x01);
457 dev->bandwidth_hz = 0; /* force to program all parameters */
458 /* init stats here in order signal app which stats are supported */
460 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
462 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
463 c->block_count.len = 1;
464 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
465 c->block_error.len = 1;
466 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
467 c->post_bit_count.len = 1;
468 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
469 c->post_bit_error.len = 1;
470 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
471 /* start statistics polling */
472 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
477 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
482 static int af9033_sleep(struct dvb_frontend *fe)
484 struct af9033_dev *dev = fe->demodulator_priv;
488 /* stop statistics polling */
489 cancel_delayed_work_sync(&dev->stat_work);
491 ret = af9033_wr_reg(dev, 0x80004c, 1);
495 ret = af9033_wr_reg(dev, 0x800000, 0);
499 for (i = 100, tmp = 1; i && tmp; i--) {
500 ret = af9033_rd_reg(dev, 0x80004c, &tmp);
504 usleep_range(200, 10000);
507 dev_dbg(&dev->client->dev, "loop=%d\n", i);
514 ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08);
518 /* prevent current leak (?) */
519 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
520 /* enable parallel TS */
521 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
525 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01);
533 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
538 static int af9033_get_tune_settings(struct dvb_frontend *fe,
539 struct dvb_frontend_tune_settings *fesettings)
541 /* 800 => 2000 because IT9135 v2 is slow to gain lock */
542 fesettings->min_delay_ms = 2000;
543 fesettings->step_size = 0;
544 fesettings->max_drift = 0;
549 static int af9033_set_frontend(struct dvb_frontend *fe)
551 struct af9033_dev *dev = fe->demodulator_priv;
552 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
553 int ret, i, spec_inv, sampling_freq;
554 u8 tmp, buf[3], bandwidth_reg_val;
555 u32 if_frequency, freq_cw, adc_freq;
557 dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n",
558 c->frequency, c->bandwidth_hz);
560 /* check bandwidth */
561 switch (c->bandwidth_hz) {
563 bandwidth_reg_val = 0x00;
566 bandwidth_reg_val = 0x01;
569 bandwidth_reg_val = 0x02;
572 dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n");
578 if (fe->ops.tuner_ops.set_params)
579 fe->ops.tuner_ops.set_params(fe);
581 /* program CFOE coefficients */
582 if (c->bandwidth_hz != dev->bandwidth_hz) {
583 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
584 if (coeff_lut[i].clock == dev->cfg.clock &&
585 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
589 if (i == ARRAY_SIZE(coeff_lut)) {
590 dev_err(&dev->client->dev,
591 "Couldn't find LUT config for clock=%d\n",
597 ret = af9033_wr_regs(dev, 0x800001,
598 coeff_lut[i].val, sizeof(coeff_lut[i].val));
601 /* program frequency control */
602 if (c->bandwidth_hz != dev->bandwidth_hz) {
603 spec_inv = dev->cfg.spec_inv ? -1 : 1;
605 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
606 if (clock_adc_lut[i].clock == dev->cfg.clock)
609 if (i == ARRAY_SIZE(clock_adc_lut)) {
610 dev_err(&dev->client->dev,
611 "Couldn't find ADC clock for clock=%d\n",
616 adc_freq = clock_adc_lut[i].adc;
618 /* get used IF frequency */
619 if (fe->ops.tuner_ops.get_if_frequency)
620 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
624 sampling_freq = if_frequency;
626 while (sampling_freq > (adc_freq / 2))
627 sampling_freq -= adc_freq;
629 if (sampling_freq >= 0)
634 freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul);
637 freq_cw = 0x800000 - freq_cw;
639 if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
642 buf[0] = (freq_cw >> 0) & 0xff;
643 buf[1] = (freq_cw >> 8) & 0xff;
644 buf[2] = (freq_cw >> 16) & 0x7f;
646 /* FIXME: there seems to be calculation error here... */
647 if (if_frequency == 0)
650 ret = af9033_wr_regs(dev, 0x800029, buf, 3);
654 dev->bandwidth_hz = c->bandwidth_hz;
657 ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03);
661 ret = af9033_wr_reg(dev, 0x800040, 0x00);
665 ret = af9033_wr_reg(dev, 0x800047, 0x00);
669 ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01);
673 if (c->frequency <= 230000000)
674 tmp = 0x00; /* VHF */
676 tmp = 0x01; /* UHF */
678 ret = af9033_wr_reg(dev, 0x80004b, tmp);
682 ret = af9033_wr_reg(dev, 0x800000, 0x00);
689 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
694 static int af9033_get_frontend(struct dvb_frontend *fe,
695 struct dtv_frontend_properties *c)
697 struct af9033_dev *dev = fe->demodulator_priv;
701 dev_dbg(&dev->client->dev, "\n");
703 /* read all needed registers */
704 ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf));
708 switch ((buf[0] >> 0) & 3) {
710 c->transmission_mode = TRANSMISSION_MODE_2K;
713 c->transmission_mode = TRANSMISSION_MODE_8K;
717 switch ((buf[1] >> 0) & 3) {
719 c->guard_interval = GUARD_INTERVAL_1_32;
722 c->guard_interval = GUARD_INTERVAL_1_16;
725 c->guard_interval = GUARD_INTERVAL_1_8;
728 c->guard_interval = GUARD_INTERVAL_1_4;
732 switch ((buf[2] >> 0) & 7) {
734 c->hierarchy = HIERARCHY_NONE;
737 c->hierarchy = HIERARCHY_1;
740 c->hierarchy = HIERARCHY_2;
743 c->hierarchy = HIERARCHY_4;
747 switch ((buf[3] >> 0) & 3) {
749 c->modulation = QPSK;
752 c->modulation = QAM_16;
755 c->modulation = QAM_64;
759 switch ((buf[4] >> 0) & 3) {
761 c->bandwidth_hz = 6000000;
764 c->bandwidth_hz = 7000000;
767 c->bandwidth_hz = 8000000;
771 switch ((buf[6] >> 0) & 7) {
773 c->code_rate_HP = FEC_1_2;
776 c->code_rate_HP = FEC_2_3;
779 c->code_rate_HP = FEC_3_4;
782 c->code_rate_HP = FEC_5_6;
785 c->code_rate_HP = FEC_7_8;
788 c->code_rate_HP = FEC_NONE;
792 switch ((buf[7] >> 0) & 7) {
794 c->code_rate_LP = FEC_1_2;
797 c->code_rate_LP = FEC_2_3;
800 c->code_rate_LP = FEC_3_4;
803 c->code_rate_LP = FEC_5_6;
806 c->code_rate_LP = FEC_7_8;
809 c->code_rate_LP = FEC_NONE;
816 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
821 static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status)
823 struct af9033_dev *dev = fe->demodulator_priv;
829 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
830 ret = af9033_rd_reg(dev, 0x800047, &tmp);
836 *status |= FE_HAS_SIGNAL;
840 ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01);
845 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
849 ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01);
854 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
855 FE_HAS_VITERBI | FE_HAS_SYNC |
859 dev->fe_status = *status;
864 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
869 static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
871 struct af9033_dev *dev = fe->demodulator_priv;
872 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
877 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) {
878 /* Return 0.1 dB for AF9030 and 0-0xffff for IT9130. */
879 if (dev->is_af9035) {
880 /* 1000x => 10x (0.1 dB) */
881 *snr = div_s64(c->cnr.stat[0].svalue, 100);
883 /* 1000x => 1x (1 dB) */
884 *snr = div_s64(c->cnr.stat[0].svalue, 1000);
886 /* read current modulation */
887 ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
891 /* scale value to 0x0000-0xffff */
892 switch ((u8tmp >> 0) & 3) {
894 *snr = *snr * 0xffff / 23;
897 *snr = *snr * 0xffff / 26;
900 *snr = *snr * 0xffff / 32;
913 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
918 static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
920 struct af9033_dev *dev = fe->demodulator_priv;
921 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
922 int ret, tmp, power_real;
923 u8 u8tmp, gain_offset, buf[7];
925 if (dev->is_af9035) {
926 /* read signal strength of 0-100 scale */
927 ret = af9033_rd_reg(dev, 0x800048, &u8tmp);
931 /* scale value to 0x0000-0xffff */
932 *strength = u8tmp * 0xffff / 100;
934 ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
938 ret = af9033_rd_regs(dev, 0x80f900, buf, 7);
942 if (c->frequency <= 300000000)
943 gain_offset = 7; /* VHF */
945 gain_offset = 4; /* UHF */
947 power_real = (u8tmp - 100 - gain_offset) -
948 power_reference[((buf[3] >> 0) & 3)][((buf[6] >> 0) & 7)];
950 if (power_real < -15)
952 else if ((power_real >= -15) && (power_real < 0))
953 tmp = (2 * (power_real + 15)) / 3;
954 else if ((power_real >= 0) && (power_real < 20))
955 tmp = 4 * power_real + 10;
956 else if ((power_real >= 20) && (power_real < 35))
957 tmp = (2 * (power_real - 20)) / 3 + 90;
961 /* scale value to 0x0000-0xffff */
962 *strength = tmp * 0xffff / 100;
968 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
973 static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
975 struct af9033_dev *dev = fe->demodulator_priv;
977 *ber = (dev->post_bit_error - dev->post_bit_error_prev);
978 dev->post_bit_error_prev = dev->post_bit_error;
983 static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
985 struct af9033_dev *dev = fe->demodulator_priv;
987 *ucblocks = dev->error_block_count;
991 static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
993 struct af9033_dev *dev = fe->demodulator_priv;
996 dev_dbg(&dev->client->dev, "enable=%d\n", enable);
998 ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01);
1005 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1010 static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
1012 struct af9033_dev *dev = fe->demodulator_priv;
1015 dev_dbg(&dev->client->dev, "onoff=%d\n", onoff);
1017 ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01);
1024 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1029 static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
1032 struct af9033_dev *dev = fe->demodulator_priv;
1034 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
1036 dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n",
1042 ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2);
1046 ret = af9033_wr_reg(dev, 0x80f994, onoff);
1050 ret = af9033_wr_reg(dev, 0x80f995, index);
1057 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1062 static void af9033_stat_work(struct work_struct *work)
1064 struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work);
1065 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
1066 int ret, tmp, i, len;
1069 dev_dbg(&dev->client->dev, "\n");
1071 /* signal strength */
1072 if (dev->fe_status & FE_HAS_SIGNAL) {
1073 if (dev->is_af9035) {
1074 ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
1075 tmp = -u8tmp * 1000;
1077 ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
1078 tmp = (u8tmp - 100) * 1000;
1083 c->strength.len = 1;
1084 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
1085 c->strength.stat[0].svalue = tmp;
1087 c->strength.len = 1;
1088 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1092 if (dev->fe_status & FE_HAS_VITERBI) {
1094 const struct val_snr *snr_lut;
1097 ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
1101 snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
1103 /* read superframe number */
1104 ret = af9033_rd_reg(dev, 0x80f78b, &u8tmp);
1111 /* read current transmission mode */
1112 ret = af9033_rd_reg(dev, 0x80f900, &u8tmp);
1116 switch ((u8tmp >> 0) & 3) {
1127 goto err_schedule_delayed_work;
1130 /* read current modulation */
1131 ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
1135 switch ((u8tmp >> 0) & 3) {
1137 len = ARRAY_SIZE(qpsk_snr_lut);
1138 snr_lut = qpsk_snr_lut;
1141 len = ARRAY_SIZE(qam16_snr_lut);
1142 snr_lut = qam16_snr_lut;
1145 len = ARRAY_SIZE(qam64_snr_lut);
1146 snr_lut = qam64_snr_lut;
1149 goto err_schedule_delayed_work;
1152 for (i = 0; i < len; i++) {
1153 tmp = snr_lut[i].snr * 1000;
1154 if (snr_val < snr_lut[i].val)
1159 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1160 c->cnr.stat[0].svalue = tmp;
1163 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1167 if (dev->fe_status & FE_HAS_LOCK) {
1168 /* outer FEC, 204 byte packets */
1169 u16 abort_packet_count, rsd_packet_count;
1170 /* inner FEC, bits */
1171 u32 rsd_bit_err_count;
1174 * Packet count used for measurement is 10000
1175 * (rsd_packet_count). Maybe it should be increased?
1178 ret = af9033_rd_regs(dev, 0x800032, buf, 7);
1182 abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
1183 rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
1184 rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
1186 dev->error_block_count += abort_packet_count;
1187 dev->total_block_count += rsd_packet_count;
1188 dev->post_bit_error += rsd_bit_err_count;
1189 dev->post_bit_count += rsd_packet_count * 204 * 8;
1191 c->block_count.len = 1;
1192 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1193 c->block_count.stat[0].uvalue = dev->total_block_count;
1195 c->block_error.len = 1;
1196 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1197 c->block_error.stat[0].uvalue = dev->error_block_count;
1199 c->post_bit_count.len = 1;
1200 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1201 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
1203 c->post_bit_error.len = 1;
1204 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1205 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
1208 err_schedule_delayed_work:
1209 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
1212 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1215 static struct dvb_frontend_ops af9033_ops = {
1216 .delsys = { SYS_DVBT },
1218 .name = "Afatech AF9033 (DVB-T)",
1219 .frequency_min = 174000000,
1220 .frequency_max = 862000000,
1221 .frequency_stepsize = 250000,
1222 .frequency_tolerance = 0,
1223 .caps = FE_CAN_FEC_1_2 |
1233 FE_CAN_TRANSMISSION_MODE_AUTO |
1234 FE_CAN_GUARD_INTERVAL_AUTO |
1235 FE_CAN_HIERARCHY_AUTO |
1240 .init = af9033_init,
1241 .sleep = af9033_sleep,
1243 .get_tune_settings = af9033_get_tune_settings,
1244 .set_frontend = af9033_set_frontend,
1245 .get_frontend = af9033_get_frontend,
1247 .read_status = af9033_read_status,
1248 .read_snr = af9033_read_snr,
1249 .read_signal_strength = af9033_read_signal_strength,
1250 .read_ber = af9033_read_ber,
1251 .read_ucblocks = af9033_read_ucblocks,
1253 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1256 static int af9033_probe(struct i2c_client *client,
1257 const struct i2c_device_id *id)
1259 struct af9033_config *cfg = client->dev.platform_data;
1260 struct af9033_dev *dev;
1265 /* allocate memory for the internal state */
1266 dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL);
1269 dev_err(&client->dev, "Could not allocate memory for state\n");
1273 /* setup the state */
1274 dev->client = client;
1275 INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work);
1276 memcpy(&dev->cfg, cfg, sizeof(struct af9033_config));
1278 if (dev->cfg.clock != 12000000) {
1280 dev_err(&dev->client->dev,
1281 "unsupported clock %d Hz, only 12000000 Hz is supported currently\n",
1286 /* firmware version */
1287 switch (dev->cfg.tuner) {
1288 case AF9033_TUNER_IT9135_38:
1289 case AF9033_TUNER_IT9135_51:
1290 case AF9033_TUNER_IT9135_52:
1291 case AF9033_TUNER_IT9135_60:
1292 case AF9033_TUNER_IT9135_61:
1293 case AF9033_TUNER_IT9135_62:
1294 dev->is_it9135 = true;
1298 dev->is_af9035 = true;
1303 ret = af9033_rd_regs(dev, reg, &buf[0], 4);
1307 ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4);
1311 dev_info(&dev->client->dev,
1312 "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
1313 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
1317 switch (dev->cfg.tuner) {
1318 case AF9033_TUNER_IT9135_38:
1319 case AF9033_TUNER_IT9135_51:
1320 case AF9033_TUNER_IT9135_52:
1321 case AF9033_TUNER_IT9135_60:
1322 case AF9033_TUNER_IT9135_61:
1323 case AF9033_TUNER_IT9135_62:
1324 /* IT9135 did not like to sleep at that early */
1327 ret = af9033_wr_reg(dev, 0x80004c, 1);
1331 ret = af9033_wr_reg(dev, 0x800000, 0);
1336 /* configure internal TS mode */
1337 switch (dev->cfg.ts_mode) {
1338 case AF9033_TS_MODE_PARALLEL:
1339 dev->ts_mode_parallel = true;
1341 case AF9033_TS_MODE_SERIAL:
1342 dev->ts_mode_serial = true;
1344 case AF9033_TS_MODE_USB:
1345 /* usb mode for AF9035 */
1350 /* create dvb_frontend */
1351 memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
1352 dev->fe.demodulator_priv = dev;
1353 *cfg->fe = &dev->fe;
1355 cfg->ops->pid_filter = af9033_pid_filter;
1356 cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1358 i2c_set_clientdata(client, dev);
1360 dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n");
1365 dev_dbg(&client->dev, "failed=%d\n", ret);
1369 static int af9033_remove(struct i2c_client *client)
1371 struct af9033_dev *dev = i2c_get_clientdata(client);
1373 dev_dbg(&dev->client->dev, "\n");
1375 /* stop statistics polling */
1376 cancel_delayed_work_sync(&dev->stat_work);
1378 dev->fe.ops.release = NULL;
1379 dev->fe.demodulator_priv = NULL;
1385 static const struct i2c_device_id af9033_id_table[] = {
1389 MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1391 static struct i2c_driver af9033_driver = {
1395 .probe = af9033_probe,
1396 .remove = af9033_remove,
1397 .id_table = af9033_id_table,
1400 module_i2c_driver(af9033_driver);
1403 MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1404 MODULE_LICENSE("GPL");