2 * Afatech AF9033 demodulator driver
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
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 fe_status_t fe_status;
43 u64 error_block_count;
44 u64 total_block_count;
45 struct delayed_work stat_work;
46 unsigned long last_stat_check;
49 /* write multiple registers */
50 static int af9033_wr_regs(struct af9033_dev *dev, u32 reg, const u8 *val,
54 u8 buf[MAX_XFER_SIZE];
55 struct i2c_msg msg[1] = {
57 .addr = dev->client->addr,
64 if (3 + len > sizeof(buf)) {
65 dev_warn(&dev->client->dev,
66 "i2c wr reg=%04x: len=%d is too big!\n",
71 buf[0] = (reg >> 16) & 0xff;
72 buf[1] = (reg >> 8) & 0xff;
73 buf[2] = (reg >> 0) & 0xff;
74 memcpy(&buf[3], val, len);
76 ret = i2c_transfer(dev->client->adapter, msg, 1);
80 dev_warn(&dev->client->dev, "i2c wr failed=%d reg=%06x len=%d\n",
88 /* read multiple registers */
89 static int af9033_rd_regs(struct af9033_dev *dev, u32 reg, u8 *val, int len)
92 u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
94 struct i2c_msg msg[2] = {
96 .addr = dev->client->addr,
101 .addr = dev->client->addr,
108 ret = i2c_transfer(dev->client->adapter, msg, 2);
112 dev_warn(&dev->client->dev, "i2c rd failed=%d reg=%06x len=%d\n",
121 /* write single register */
122 static int af9033_wr_reg(struct af9033_dev *dev, u32 reg, u8 val)
124 return af9033_wr_regs(dev, reg, &val, 1);
127 /* read single register */
128 static int af9033_rd_reg(struct af9033_dev *dev, u32 reg, u8 *val)
130 return af9033_rd_regs(dev, reg, val, 1);
133 /* write single register with mask */
134 static int af9033_wr_reg_mask(struct af9033_dev *dev, u32 reg, u8 val,
140 /* no need for read if whole reg is written */
142 ret = af9033_rd_regs(dev, reg, &tmp, 1);
151 return af9033_wr_regs(dev, reg, &val, 1);
154 /* read single register with mask */
155 static int af9033_rd_reg_mask(struct af9033_dev *dev, u32 reg, u8 *val,
161 ret = af9033_rd_regs(dev, reg, &tmp, 1);
167 /* find position of the first bit */
168 for (i = 0; i < 8; i++) {
169 if ((mask >> i) & 0x01)
177 /* write reg val table using reg addr auto increment */
178 static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
179 const struct reg_val *tab, int tab_len)
181 #define MAX_TAB_LEN 212
183 u8 buf[1 + MAX_TAB_LEN];
185 dev_dbg(&dev->client->dev, "tab_len=%d\n", tab_len);
187 if (tab_len > sizeof(buf)) {
188 dev_warn(&dev->client->dev, "tab len %d is too big\n", tab_len);
192 for (i = 0, j = 0; i < tab_len; i++) {
195 if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
196 ret = af9033_wr_regs(dev, tab[i].reg - j, buf, j + 1);
209 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
214 static u32 af9033_div(struct af9033_dev *dev, u32 a, u32 b, u32 x)
218 dev_dbg(&dev->client->dev, "a=%d b=%d x=%d\n", a, b, x);
225 for (i = 0; i < x; i++) {
233 r = (c << (u32)x) + r;
235 dev_dbg(&dev->client->dev, "a=%d b=%d x=%d r=%d r=%x\n", a, b, x, r, r);
240 static int af9033_init(struct dvb_frontend *fe)
242 struct af9033_dev *dev = fe->demodulator_priv;
244 const struct reg_val *init;
246 u32 adc_cw, clock_cw;
247 struct reg_val_mask tab[] = {
248 { 0x80fb24, 0x00, 0x08 },
249 { 0x80004c, 0x00, 0xff },
250 { 0x00f641, dev->cfg.tuner, 0xff },
251 { 0x80f5ca, 0x01, 0x01 },
252 { 0x80f715, 0x01, 0x01 },
253 { 0x00f41f, 0x04, 0x04 },
254 { 0x00f41a, 0x01, 0x01 },
255 { 0x80f731, 0x00, 0x01 },
256 { 0x00d91e, 0x00, 0x01 },
257 { 0x00d919, 0x00, 0x01 },
258 { 0x80f732, 0x00, 0x01 },
259 { 0x00d91f, 0x00, 0x01 },
260 { 0x00d91a, 0x00, 0x01 },
261 { 0x80f730, 0x00, 0x01 },
262 { 0x80f778, 0x00, 0xff },
263 { 0x80f73c, 0x01, 0x01 },
264 { 0x80f776, 0x00, 0x01 },
265 { 0x00d8fd, 0x01, 0xff },
266 { 0x00d830, 0x01, 0xff },
267 { 0x00d831, 0x00, 0xff },
268 { 0x00d832, 0x00, 0xff },
269 { 0x80f985, dev->ts_mode_serial, 0x01 },
270 { 0x80f986, dev->ts_mode_parallel, 0x01 },
271 { 0x00d827, 0x00, 0xff },
272 { 0x00d829, 0x00, 0xff },
273 { 0x800045, dev->cfg.adc_multiplier, 0xff },
276 /* program clock control */
277 clock_cw = af9033_div(dev, dev->cfg.clock, 1000000ul, 19ul);
278 buf[0] = (clock_cw >> 0) & 0xff;
279 buf[1] = (clock_cw >> 8) & 0xff;
280 buf[2] = (clock_cw >> 16) & 0xff;
281 buf[3] = (clock_cw >> 24) & 0xff;
283 dev_dbg(&dev->client->dev, "clock=%d clock_cw=%08x\n",
284 dev->cfg.clock, clock_cw);
286 ret = af9033_wr_regs(dev, 0x800025, buf, 4);
290 /* program ADC control */
291 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
292 if (clock_adc_lut[i].clock == dev->cfg.clock)
296 adc_cw = af9033_div(dev, clock_adc_lut[i].adc, 1000000ul, 19ul);
297 buf[0] = (adc_cw >> 0) & 0xff;
298 buf[1] = (adc_cw >> 8) & 0xff;
299 buf[2] = (adc_cw >> 16) & 0xff;
301 dev_dbg(&dev->client->dev, "adc=%d adc_cw=%06x\n",
302 clock_adc_lut[i].adc, adc_cw);
304 ret = af9033_wr_regs(dev, 0x80f1cd, buf, 3);
308 /* program register table */
309 for (i = 0; i < ARRAY_SIZE(tab); i++) {
310 ret = af9033_wr_reg_mask(dev, tab[i].reg, tab[i].val,
317 if (dev->cfg.dyn0_clk) {
318 ret = af9033_wr_reg(dev, 0x80fba8, 0x00);
323 /* settings for TS interface */
324 if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
325 ret = af9033_wr_reg_mask(dev, 0x80f9a5, 0x00, 0x01);
329 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x01, 0x01);
333 ret = af9033_wr_reg_mask(dev, 0x80f990, 0x00, 0x01);
337 ret = af9033_wr_reg_mask(dev, 0x80f9b5, 0x00, 0x01);
342 /* load OFSM settings */
343 dev_dbg(&dev->client->dev, "load ofsm settings\n");
344 switch (dev->cfg.tuner) {
345 case AF9033_TUNER_IT9135_38:
346 case AF9033_TUNER_IT9135_51:
347 case AF9033_TUNER_IT9135_52:
348 len = ARRAY_SIZE(ofsm_init_it9135_v1);
349 init = ofsm_init_it9135_v1;
351 case AF9033_TUNER_IT9135_60:
352 case AF9033_TUNER_IT9135_61:
353 case AF9033_TUNER_IT9135_62:
354 len = ARRAY_SIZE(ofsm_init_it9135_v2);
355 init = ofsm_init_it9135_v2;
358 len = ARRAY_SIZE(ofsm_init);
363 ret = af9033_wr_reg_val_tab(dev, init, len);
367 /* load tuner specific settings */
368 dev_dbg(&dev->client->dev, "load tuner specific settings\n");
369 switch (dev->cfg.tuner) {
370 case AF9033_TUNER_TUA9001:
371 len = ARRAY_SIZE(tuner_init_tua9001);
372 init = tuner_init_tua9001;
374 case AF9033_TUNER_FC0011:
375 len = ARRAY_SIZE(tuner_init_fc0011);
376 init = tuner_init_fc0011;
378 case AF9033_TUNER_MXL5007T:
379 len = ARRAY_SIZE(tuner_init_mxl5007t);
380 init = tuner_init_mxl5007t;
382 case AF9033_TUNER_TDA18218:
383 len = ARRAY_SIZE(tuner_init_tda18218);
384 init = tuner_init_tda18218;
386 case AF9033_TUNER_FC2580:
387 len = ARRAY_SIZE(tuner_init_fc2580);
388 init = tuner_init_fc2580;
390 case AF9033_TUNER_FC0012:
391 len = ARRAY_SIZE(tuner_init_fc0012);
392 init = tuner_init_fc0012;
394 case AF9033_TUNER_IT9135_38:
395 len = ARRAY_SIZE(tuner_init_it9135_38);
396 init = tuner_init_it9135_38;
398 case AF9033_TUNER_IT9135_51:
399 len = ARRAY_SIZE(tuner_init_it9135_51);
400 init = tuner_init_it9135_51;
402 case AF9033_TUNER_IT9135_52:
403 len = ARRAY_SIZE(tuner_init_it9135_52);
404 init = tuner_init_it9135_52;
406 case AF9033_TUNER_IT9135_60:
407 len = ARRAY_SIZE(tuner_init_it9135_60);
408 init = tuner_init_it9135_60;
410 case AF9033_TUNER_IT9135_61:
411 len = ARRAY_SIZE(tuner_init_it9135_61);
412 init = tuner_init_it9135_61;
414 case AF9033_TUNER_IT9135_62:
415 len = ARRAY_SIZE(tuner_init_it9135_62);
416 init = tuner_init_it9135_62;
419 dev_dbg(&dev->client->dev, "unsupported tuner ID=%d\n",
425 ret = af9033_wr_reg_val_tab(dev, init, len);
429 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
430 ret = af9033_wr_reg_mask(dev, 0x00d91c, 0x01, 0x01);
434 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
438 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x00, 0x01);
443 switch (dev->cfg.tuner) {
444 case AF9033_TUNER_IT9135_60:
445 case AF9033_TUNER_IT9135_61:
446 case AF9033_TUNER_IT9135_62:
447 ret = af9033_wr_reg(dev, 0x800000, 0x01);
452 dev->bandwidth_hz = 0; /* force to program all parameters */
453 /* start statistics polling */
454 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
459 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
464 static int af9033_sleep(struct dvb_frontend *fe)
466 struct af9033_dev *dev = fe->demodulator_priv;
470 /* stop statistics polling */
471 cancel_delayed_work_sync(&dev->stat_work);
473 ret = af9033_wr_reg(dev, 0x80004c, 1);
477 ret = af9033_wr_reg(dev, 0x800000, 0);
481 for (i = 100, tmp = 1; i && tmp; i--) {
482 ret = af9033_rd_reg(dev, 0x80004c, &tmp);
486 usleep_range(200, 10000);
489 dev_dbg(&dev->client->dev, "loop=%d\n", i);
496 ret = af9033_wr_reg_mask(dev, 0x80fb24, 0x08, 0x08);
500 /* prevent current leak (?) */
501 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
502 /* enable parallel TS */
503 ret = af9033_wr_reg_mask(dev, 0x00d917, 0x00, 0x01);
507 ret = af9033_wr_reg_mask(dev, 0x00d916, 0x01, 0x01);
515 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
520 static int af9033_get_tune_settings(struct dvb_frontend *fe,
521 struct dvb_frontend_tune_settings *fesettings)
523 /* 800 => 2000 because IT9135 v2 is slow to gain lock */
524 fesettings->min_delay_ms = 2000;
525 fesettings->step_size = 0;
526 fesettings->max_drift = 0;
531 static int af9033_set_frontend(struct dvb_frontend *fe)
533 struct af9033_dev *dev = fe->demodulator_priv;
534 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
535 int ret, i, spec_inv, sampling_freq;
536 u8 tmp, buf[3], bandwidth_reg_val;
537 u32 if_frequency, freq_cw, adc_freq;
539 dev_dbg(&dev->client->dev, "frequency=%d bandwidth_hz=%d\n",
540 c->frequency, c->bandwidth_hz);
542 /* check bandwidth */
543 switch (c->bandwidth_hz) {
545 bandwidth_reg_val = 0x00;
548 bandwidth_reg_val = 0x01;
551 bandwidth_reg_val = 0x02;
554 dev_dbg(&dev->client->dev, "invalid bandwidth_hz\n");
560 if (fe->ops.tuner_ops.set_params)
561 fe->ops.tuner_ops.set_params(fe);
563 /* program CFOE coefficients */
564 if (c->bandwidth_hz != dev->bandwidth_hz) {
565 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
566 if (coeff_lut[i].clock == dev->cfg.clock &&
567 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
571 ret = af9033_wr_regs(dev, 0x800001,
572 coeff_lut[i].val, sizeof(coeff_lut[i].val));
575 /* program frequency control */
576 if (c->bandwidth_hz != dev->bandwidth_hz) {
577 spec_inv = dev->cfg.spec_inv ? -1 : 1;
579 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
580 if (clock_adc_lut[i].clock == dev->cfg.clock)
583 adc_freq = clock_adc_lut[i].adc;
585 /* get used IF frequency */
586 if (fe->ops.tuner_ops.get_if_frequency)
587 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
591 sampling_freq = if_frequency;
593 while (sampling_freq > (adc_freq / 2))
594 sampling_freq -= adc_freq;
596 if (sampling_freq >= 0)
601 freq_cw = af9033_div(dev, sampling_freq, adc_freq, 23ul);
604 freq_cw = 0x800000 - freq_cw;
606 if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
609 buf[0] = (freq_cw >> 0) & 0xff;
610 buf[1] = (freq_cw >> 8) & 0xff;
611 buf[2] = (freq_cw >> 16) & 0x7f;
613 /* FIXME: there seems to be calculation error here... */
614 if (if_frequency == 0)
617 ret = af9033_wr_regs(dev, 0x800029, buf, 3);
621 dev->bandwidth_hz = c->bandwidth_hz;
624 ret = af9033_wr_reg_mask(dev, 0x80f904, bandwidth_reg_val, 0x03);
628 ret = af9033_wr_reg(dev, 0x800040, 0x00);
632 ret = af9033_wr_reg(dev, 0x800047, 0x00);
636 ret = af9033_wr_reg_mask(dev, 0x80f999, 0x00, 0x01);
640 if (c->frequency <= 230000000)
641 tmp = 0x00; /* VHF */
643 tmp = 0x01; /* UHF */
645 ret = af9033_wr_reg(dev, 0x80004b, tmp);
649 ret = af9033_wr_reg(dev, 0x800000, 0x00);
656 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
661 static int af9033_get_frontend(struct dvb_frontend *fe)
663 struct af9033_dev *dev = fe->demodulator_priv;
664 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
668 dev_dbg(&dev->client->dev, "\n");
670 /* read all needed registers */
671 ret = af9033_rd_regs(dev, 0x80f900, buf, sizeof(buf));
675 switch ((buf[0] >> 0) & 3) {
677 c->transmission_mode = TRANSMISSION_MODE_2K;
680 c->transmission_mode = TRANSMISSION_MODE_8K;
684 switch ((buf[1] >> 0) & 3) {
686 c->guard_interval = GUARD_INTERVAL_1_32;
689 c->guard_interval = GUARD_INTERVAL_1_16;
692 c->guard_interval = GUARD_INTERVAL_1_8;
695 c->guard_interval = GUARD_INTERVAL_1_4;
699 switch ((buf[2] >> 0) & 7) {
701 c->hierarchy = HIERARCHY_NONE;
704 c->hierarchy = HIERARCHY_1;
707 c->hierarchy = HIERARCHY_2;
710 c->hierarchy = HIERARCHY_4;
714 switch ((buf[3] >> 0) & 3) {
716 c->modulation = QPSK;
719 c->modulation = QAM_16;
722 c->modulation = QAM_64;
726 switch ((buf[4] >> 0) & 3) {
728 c->bandwidth_hz = 6000000;
731 c->bandwidth_hz = 7000000;
734 c->bandwidth_hz = 8000000;
738 switch ((buf[6] >> 0) & 7) {
740 c->code_rate_HP = FEC_1_2;
743 c->code_rate_HP = FEC_2_3;
746 c->code_rate_HP = FEC_3_4;
749 c->code_rate_HP = FEC_5_6;
752 c->code_rate_HP = FEC_7_8;
755 c->code_rate_HP = FEC_NONE;
759 switch ((buf[7] >> 0) & 7) {
761 c->code_rate_LP = FEC_1_2;
764 c->code_rate_LP = FEC_2_3;
767 c->code_rate_LP = FEC_3_4;
770 c->code_rate_LP = FEC_5_6;
773 c->code_rate_LP = FEC_7_8;
776 c->code_rate_LP = FEC_NONE;
783 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
788 static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
790 struct af9033_dev *dev = fe->demodulator_priv;
796 /* radio channel status, 0=no result, 1=has signal, 2=no signal */
797 ret = af9033_rd_reg(dev, 0x800047, &tmp);
803 *status |= FE_HAS_SIGNAL;
807 ret = af9033_rd_reg_mask(dev, 0x80f5a9, &tmp, 0x01);
812 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
816 ret = af9033_rd_reg_mask(dev, 0x80f999, &tmp, 0x01);
821 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
822 FE_HAS_VITERBI | FE_HAS_SYNC |
826 dev->fe_status = *status;
831 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
836 static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
838 struct af9033_dev *dev = fe->demodulator_priv;
839 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
842 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
843 *snr = div_s64(c->cnr.stat[0].svalue, 100); /* 1000x => 10x */
850 static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
852 struct af9033_dev *dev = fe->demodulator_priv;
856 /* read signal strength of 0-100 scale */
857 ret = af9033_rd_reg(dev, 0x800048, &strength2);
861 /* scale value to 0x0000-0xffff */
862 *strength = strength2 * 0xffff / 100;
867 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
872 static int af9033_update_ch_stat(struct af9033_dev *dev)
875 u32 err_cnt, bit_cnt;
879 /* only update data every half second */
880 if (time_after(jiffies, dev->last_stat_check + msecs_to_jiffies(500))) {
881 ret = af9033_rd_regs(dev, 0x800032, buf, sizeof(buf));
884 /* in 8 byte packets? */
885 abort_cnt = (buf[1] << 8) + buf[0];
887 err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
888 /* in 8 byte packets? always(?) 0x2710 = 10000 */
889 bit_cnt = (buf[6] << 8) + buf[5];
891 if (bit_cnt < abort_cnt) {
893 dev->ber = 0xffffffff;
896 * 8 byte packets, that have not been rejected already
898 bit_cnt -= (u32)abort_cnt;
900 dev->ber = 0xffffffff;
902 err_cnt -= (u32)abort_cnt * 8 * 8;
904 dev->ber = err_cnt * (0xffffffff / bit_cnt);
907 dev->ucb += abort_cnt;
908 dev->last_stat_check = jiffies;
913 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
918 static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
920 struct af9033_dev *dev = fe->demodulator_priv;
923 ret = af9033_update_ch_stat(dev);
932 static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
934 struct af9033_dev *dev = fe->demodulator_priv;
936 *ucblocks = dev->error_block_count;
940 static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
942 struct af9033_dev *dev = fe->demodulator_priv;
945 dev_dbg(&dev->client->dev, "enable=%d\n", enable);
947 ret = af9033_wr_reg_mask(dev, 0x00fa04, enable, 0x01);
954 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
959 static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
961 struct af9033_dev *dev = fe->demodulator_priv;
964 dev_dbg(&dev->client->dev, "onoff=%d\n", onoff);
966 ret = af9033_wr_reg_mask(dev, 0x80f993, onoff, 0x01);
973 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
978 static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
981 struct af9033_dev *dev = fe->demodulator_priv;
983 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
985 dev_dbg(&dev->client->dev, "index=%d pid=%04x onoff=%d\n",
991 ret = af9033_wr_regs(dev, 0x80f996, wbuf, 2);
995 ret = af9033_wr_reg(dev, 0x80f994, onoff);
999 ret = af9033_wr_reg(dev, 0x80f995, index);
1006 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1011 static void af9033_stat_work(struct work_struct *work)
1013 struct af9033_dev *dev = container_of(work, struct af9033_dev, stat_work.work);
1014 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
1015 int ret, tmp, i, len;
1018 dev_dbg(&dev->client->dev, "\n");
1020 /* signal strength */
1021 if (dev->fe_status & FE_HAS_SIGNAL) {
1022 if (dev->is_af9035) {
1023 ret = af9033_rd_reg(dev, 0x80004a, &u8tmp);
1024 tmp = -u8tmp * 1000;
1026 ret = af9033_rd_reg(dev, 0x8000f7, &u8tmp);
1027 tmp = (u8tmp - 100) * 1000;
1032 c->strength.len = 1;
1033 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
1034 c->strength.stat[0].svalue = tmp;
1036 c->strength.len = 1;
1037 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1041 if (dev->fe_status & FE_HAS_VITERBI) {
1043 const struct val_snr *snr_lut;
1046 ret = af9033_rd_regs(dev, 0x80002c, buf, 3);
1050 snr_val = (buf[2] << 16) | (buf[1] << 8) | (buf[0] << 0);
1052 /* read current modulation */
1053 ret = af9033_rd_reg(dev, 0x80f903, &u8tmp);
1057 switch ((u8tmp >> 0) & 3) {
1059 len = ARRAY_SIZE(qpsk_snr_lut);
1060 snr_lut = qpsk_snr_lut;
1063 len = ARRAY_SIZE(qam16_snr_lut);
1064 snr_lut = qam16_snr_lut;
1067 len = ARRAY_SIZE(qam64_snr_lut);
1068 snr_lut = qam64_snr_lut;
1071 goto err_schedule_delayed_work;
1074 for (i = 0; i < len; i++) {
1075 tmp = snr_lut[i].snr * 1000;
1076 if (snr_val < snr_lut[i].val)
1081 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1082 c->cnr.stat[0].svalue = tmp;
1085 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1089 if (dev->fe_status & FE_HAS_LOCK) {
1090 /* outer FEC, 204 byte packets */
1091 u16 abort_packet_count, rsd_packet_count;
1092 /* inner FEC, bits */
1093 u32 rsd_bit_err_count;
1096 * Packet count used for measurement is 10000
1097 * (rsd_packet_count). Maybe it should be increased?
1100 ret = af9033_rd_regs(dev, 0x800032, buf, 7);
1104 abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
1105 rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
1106 rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
1108 dev->error_block_count += abort_packet_count;
1109 dev->total_block_count += rsd_packet_count;
1110 dev->post_bit_error += rsd_bit_err_count;
1111 dev->post_bit_count += rsd_packet_count * 204 * 8;
1113 c->block_count.len = 1;
1114 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1115 c->block_count.stat[0].uvalue = dev->total_block_count;
1117 c->block_error.len = 1;
1118 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1119 c->block_error.stat[0].uvalue = dev->error_block_count;
1121 c->post_bit_count.len = 1;
1122 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1123 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
1125 c->post_bit_error.len = 1;
1126 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1127 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
1130 err_schedule_delayed_work:
1131 schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
1134 dev_dbg(&dev->client->dev, "failed=%d\n", ret);
1137 static struct dvb_frontend_ops af9033_ops = {
1138 .delsys = { SYS_DVBT },
1140 .name = "Afatech AF9033 (DVB-T)",
1141 .frequency_min = 174000000,
1142 .frequency_max = 862000000,
1143 .frequency_stepsize = 250000,
1144 .frequency_tolerance = 0,
1145 .caps = FE_CAN_FEC_1_2 |
1155 FE_CAN_TRANSMISSION_MODE_AUTO |
1156 FE_CAN_GUARD_INTERVAL_AUTO |
1157 FE_CAN_HIERARCHY_AUTO |
1162 .init = af9033_init,
1163 .sleep = af9033_sleep,
1165 .get_tune_settings = af9033_get_tune_settings,
1166 .set_frontend = af9033_set_frontend,
1167 .get_frontend = af9033_get_frontend,
1169 .read_status = af9033_read_status,
1170 .read_snr = af9033_read_snr,
1171 .read_signal_strength = af9033_read_signal_strength,
1172 .read_ber = af9033_read_ber,
1173 .read_ucblocks = af9033_read_ucblocks,
1175 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1178 static int af9033_probe(struct i2c_client *client,
1179 const struct i2c_device_id *id)
1181 struct af9033_config *cfg = client->dev.platform_data;
1182 struct af9033_dev *dev;
1187 /* allocate memory for the internal state */
1188 dev = kzalloc(sizeof(struct af9033_dev), GFP_KERNEL);
1191 dev_err(&client->dev, "Could not allocate memory for state\n");
1195 /* setup the state */
1196 dev->client = client;
1197 INIT_DELAYED_WORK(&dev->stat_work, af9033_stat_work);
1198 memcpy(&dev->cfg, cfg, sizeof(struct af9033_config));
1200 if (dev->cfg.clock != 12000000) {
1202 dev_err(&dev->client->dev,
1203 "unsupported clock %d Hz, only 12000000 Hz is supported currently\n",
1208 /* firmware version */
1209 switch (dev->cfg.tuner) {
1210 case AF9033_TUNER_IT9135_38:
1211 case AF9033_TUNER_IT9135_51:
1212 case AF9033_TUNER_IT9135_52:
1213 case AF9033_TUNER_IT9135_60:
1214 case AF9033_TUNER_IT9135_61:
1215 case AF9033_TUNER_IT9135_62:
1216 dev->is_it9135 = true;
1220 dev->is_af9035 = true;
1225 ret = af9033_rd_regs(dev, reg, &buf[0], 4);
1229 ret = af9033_rd_regs(dev, 0x804191, &buf[4], 4);
1233 dev_info(&dev->client->dev,
1234 "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
1235 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
1239 switch (dev->cfg.tuner) {
1240 case AF9033_TUNER_IT9135_38:
1241 case AF9033_TUNER_IT9135_51:
1242 case AF9033_TUNER_IT9135_52:
1243 case AF9033_TUNER_IT9135_60:
1244 case AF9033_TUNER_IT9135_61:
1245 case AF9033_TUNER_IT9135_62:
1246 /* IT9135 did not like to sleep at that early */
1249 ret = af9033_wr_reg(dev, 0x80004c, 1);
1253 ret = af9033_wr_reg(dev, 0x800000, 0);
1258 /* configure internal TS mode */
1259 switch (dev->cfg.ts_mode) {
1260 case AF9033_TS_MODE_PARALLEL:
1261 dev->ts_mode_parallel = true;
1263 case AF9033_TS_MODE_SERIAL:
1264 dev->ts_mode_serial = true;
1266 case AF9033_TS_MODE_USB:
1267 /* usb mode for AF9035 */
1272 /* create dvb_frontend */
1273 memcpy(&dev->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
1274 dev->fe.demodulator_priv = dev;
1275 *cfg->fe = &dev->fe;
1277 cfg->ops->pid_filter = af9033_pid_filter;
1278 cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1280 i2c_set_clientdata(client, dev);
1282 dev_info(&dev->client->dev, "Afatech AF9033 successfully attached\n");
1287 dev_dbg(&client->dev, "failed=%d\n", ret);
1291 static int af9033_remove(struct i2c_client *client)
1293 struct af9033_dev *dev = i2c_get_clientdata(client);
1295 dev_dbg(&dev->client->dev, "\n");
1297 dev->fe.ops.release = NULL;
1298 dev->fe.demodulator_priv = NULL;
1304 static const struct i2c_device_id af9033_id_table[] = {
1308 MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1310 static struct i2c_driver af9033_driver = {
1312 .owner = THIS_MODULE,
1315 .probe = af9033_probe,
1316 .remove = af9033_remove,
1317 .id_table = af9033_id_table,
1320 module_i2c_driver(af9033_driver);
1322 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1323 MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1324 MODULE_LICENSE("GPL");