sched: Remove double_rq_lock() from __migrate_task()
[linux/fpc-iii.git] / drivers / media / dvb-frontends / af9013.c
blobfb504f1e912500c3f8c04a9e1e0f0472255597a6
1 /*
2 * Afatech AF9013 demodulator driver
4 * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
7 * Thanks to Afatech who kindly provided information.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "af9013_priv.h"
27 /* Max transfer size done by I2C transfer functions */
28 #define MAX_XFER_SIZE 64
30 struct af9013_state {
31 struct i2c_adapter *i2c;
32 struct dvb_frontend fe;
33 struct af9013_config config;
35 /* tuner/demod RF and IF AGC limits used for signal strength calc */
36 u8 signal_strength_en, rf_50, rf_80, if_50, if_80;
37 u16 signal_strength;
38 u32 ber;
39 u32 ucblocks;
40 u16 snr;
41 u32 bandwidth_hz;
42 fe_status_t fe_status;
43 unsigned long set_frontend_jiffies;
44 unsigned long read_status_jiffies;
45 bool first_tune;
46 bool i2c_gate_state;
47 unsigned int statistics_step:3;
48 struct delayed_work statistics_work;
51 /* write multiple registers */
52 static int af9013_wr_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg,
53 const u8 *val, int len)
55 int ret;
56 u8 buf[MAX_XFER_SIZE];
57 struct i2c_msg msg[1] = {
59 .addr = priv->config.i2c_addr,
60 .flags = 0,
61 .len = 3 + len,
62 .buf = buf,
66 if (3 + len > sizeof(buf)) {
67 dev_warn(&priv->i2c->dev,
68 "%s: i2c wr reg=%04x: len=%d is too big!\n",
69 KBUILD_MODNAME, reg, len);
70 return -EINVAL;
73 buf[0] = (reg >> 8) & 0xff;
74 buf[1] = (reg >> 0) & 0xff;
75 buf[2] = mbox;
76 memcpy(&buf[3], val, len);
78 ret = i2c_transfer(priv->i2c, msg, 1);
79 if (ret == 1) {
80 ret = 0;
81 } else {
82 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%04x " \
83 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
84 ret = -EREMOTEIO;
86 return ret;
89 /* read multiple registers */
90 static int af9013_rd_regs_i2c(struct af9013_state *priv, u8 mbox, u16 reg,
91 u8 *val, int len)
93 int ret;
94 u8 buf[3];
95 struct i2c_msg msg[2] = {
97 .addr = priv->config.i2c_addr,
98 .flags = 0,
99 .len = 3,
100 .buf = buf,
101 }, {
102 .addr = priv->config.i2c_addr,
103 .flags = I2C_M_RD,
104 .len = len,
105 .buf = val,
109 buf[0] = (reg >> 8) & 0xff;
110 buf[1] = (reg >> 0) & 0xff;
111 buf[2] = mbox;
113 ret = i2c_transfer(priv->i2c, msg, 2);
114 if (ret == 2) {
115 ret = 0;
116 } else {
117 dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%04x " \
118 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
119 ret = -EREMOTEIO;
121 return ret;
124 /* write multiple registers */
125 static int af9013_wr_regs(struct af9013_state *priv, u16 reg, const u8 *val,
126 int len)
128 int ret, i;
129 u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(1 << 0);
131 if ((priv->config.ts_mode == AF9013_TS_USB) &&
132 ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) {
133 mbox |= ((len - 1) << 2);
134 ret = af9013_wr_regs_i2c(priv, mbox, reg, val, len);
135 } else {
136 for (i = 0; i < len; i++) {
137 ret = af9013_wr_regs_i2c(priv, mbox, reg+i, val+i, 1);
138 if (ret)
139 goto err;
143 err:
144 return 0;
147 /* read multiple registers */
148 static int af9013_rd_regs(struct af9013_state *priv, u16 reg, u8 *val, int len)
150 int ret, i;
151 u8 mbox = (0 << 7)|(0 << 6)|(1 << 1)|(0 << 0);
153 if ((priv->config.ts_mode == AF9013_TS_USB) &&
154 ((reg & 0xff00) != 0xff00) && ((reg & 0xff00) != 0xae00)) {
155 mbox |= ((len - 1) << 2);
156 ret = af9013_rd_regs_i2c(priv, mbox, reg, val, len);
157 } else {
158 for (i = 0; i < len; i++) {
159 ret = af9013_rd_regs_i2c(priv, mbox, reg+i, val+i, 1);
160 if (ret)
161 goto err;
165 err:
166 return 0;
169 /* write single register */
170 static int af9013_wr_reg(struct af9013_state *priv, u16 reg, u8 val)
172 return af9013_wr_regs(priv, reg, &val, 1);
175 /* read single register */
176 static int af9013_rd_reg(struct af9013_state *priv, u16 reg, u8 *val)
178 return af9013_rd_regs(priv, reg, val, 1);
181 static int af9013_write_ofsm_regs(struct af9013_state *state, u16 reg, u8 *val,
182 u8 len)
184 u8 mbox = (1 << 7)|(1 << 6)|((len - 1) << 2)|(1 << 1)|(1 << 0);
185 return af9013_wr_regs_i2c(state, mbox, reg, val, len);
188 static int af9013_wr_reg_bits(struct af9013_state *state, u16 reg, int pos,
189 int len, u8 val)
191 int ret;
192 u8 tmp, mask;
194 /* no need for read if whole reg is written */
195 if (len != 8) {
196 ret = af9013_rd_reg(state, reg, &tmp);
197 if (ret)
198 return ret;
200 mask = (0xff >> (8 - len)) << pos;
201 val <<= pos;
202 tmp &= ~mask;
203 val |= tmp;
206 return af9013_wr_reg(state, reg, val);
209 static int af9013_rd_reg_bits(struct af9013_state *state, u16 reg, int pos,
210 int len, u8 *val)
212 int ret;
213 u8 tmp;
215 ret = af9013_rd_reg(state, reg, &tmp);
216 if (ret)
217 return ret;
219 *val = (tmp >> pos);
220 *val &= (0xff >> (8 - len));
222 return 0;
225 static int af9013_set_gpio(struct af9013_state *state, u8 gpio, u8 gpioval)
227 int ret;
228 u8 pos;
229 u16 addr;
231 dev_dbg(&state->i2c->dev, "%s: gpio=%d gpioval=%02x\n",
232 __func__, gpio, gpioval);
235 * GPIO0 & GPIO1 0xd735
236 * GPIO2 & GPIO3 0xd736
239 switch (gpio) {
240 case 0:
241 case 1:
242 addr = 0xd735;
243 break;
244 case 2:
245 case 3:
246 addr = 0xd736;
247 break;
249 default:
250 dev_err(&state->i2c->dev, "%s: invalid gpio=%d\n",
251 KBUILD_MODNAME, gpio);
252 ret = -EINVAL;
253 goto err;
256 switch (gpio) {
257 case 0:
258 case 2:
259 pos = 0;
260 break;
261 case 1:
262 case 3:
263 default:
264 pos = 4;
265 break;
268 ret = af9013_wr_reg_bits(state, addr, pos, 4, gpioval);
269 if (ret)
270 goto err;
272 return ret;
273 err:
274 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
275 return ret;
278 static u32 af9013_div(struct af9013_state *state, u32 a, u32 b, u32 x)
280 u32 r = 0, c = 0, i;
282 dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x);
284 if (a > b) {
285 c = a / b;
286 a = a - c * b;
289 for (i = 0; i < x; i++) {
290 if (a >= b) {
291 r += 1;
292 a -= b;
294 a <<= 1;
295 r <<= 1;
297 r = (c << (u32)x) + r;
299 dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n",
300 __func__, a, b, x, r, r);
302 return r;
305 static int af9013_power_ctrl(struct af9013_state *state, u8 onoff)
307 int ret, i;
308 u8 tmp;
310 dev_dbg(&state->i2c->dev, "%s: onoff=%d\n", __func__, onoff);
312 /* enable reset */
313 ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 1);
314 if (ret)
315 goto err;
317 /* start reset mechanism */
318 ret = af9013_wr_reg(state, 0xaeff, 1);
319 if (ret)
320 goto err;
322 /* wait reset performs */
323 for (i = 0; i < 150; i++) {
324 ret = af9013_rd_reg_bits(state, 0xd417, 1, 1, &tmp);
325 if (ret)
326 goto err;
328 if (tmp)
329 break; /* reset done */
331 usleep_range(5000, 25000);
334 if (!tmp)
335 return -ETIMEDOUT;
337 if (onoff) {
338 /* clear reset */
339 ret = af9013_wr_reg_bits(state, 0xd417, 1, 1, 0);
340 if (ret)
341 goto err;
343 /* disable reset */
344 ret = af9013_wr_reg_bits(state, 0xd417, 4, 1, 0);
346 /* power on */
347 ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 0);
348 } else {
349 /* power off */
350 ret = af9013_wr_reg_bits(state, 0xd73a, 3, 1, 1);
353 return ret;
354 err:
355 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
356 return ret;
359 static int af9013_statistics_ber_unc_start(struct dvb_frontend *fe)
361 struct af9013_state *state = fe->demodulator_priv;
362 int ret;
364 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
366 /* reset and start BER counter */
367 ret = af9013_wr_reg_bits(state, 0xd391, 4, 1, 1);
368 if (ret)
369 goto err;
371 return ret;
372 err:
373 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
374 return ret;
377 static int af9013_statistics_ber_unc_result(struct dvb_frontend *fe)
379 struct af9013_state *state = fe->demodulator_priv;
380 int ret;
381 u8 buf[5];
383 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
385 /* check if error bit count is ready */
386 ret = af9013_rd_reg_bits(state, 0xd391, 4, 1, &buf[0]);
387 if (ret)
388 goto err;
390 if (!buf[0]) {
391 dev_dbg(&state->i2c->dev, "%s: not ready\n", __func__);
392 return 0;
395 ret = af9013_rd_regs(state, 0xd387, buf, 5);
396 if (ret)
397 goto err;
399 state->ber = (buf[2] << 16) | (buf[1] << 8) | buf[0];
400 state->ucblocks += (buf[4] << 8) | buf[3];
402 return ret;
403 err:
404 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
405 return ret;
408 static int af9013_statistics_snr_start(struct dvb_frontend *fe)
410 struct af9013_state *state = fe->demodulator_priv;
411 int ret;
413 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
415 /* start SNR meas */
416 ret = af9013_wr_reg_bits(state, 0xd2e1, 3, 1, 1);
417 if (ret)
418 goto err;
420 return ret;
421 err:
422 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
423 return ret;
426 static int af9013_statistics_snr_result(struct dvb_frontend *fe)
428 struct af9013_state *state = fe->demodulator_priv;
429 int ret, i, len;
430 u8 buf[3], tmp;
431 u32 snr_val;
432 const struct af9013_snr *uninitialized_var(snr_lut);
434 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
436 /* check if SNR ready */
437 ret = af9013_rd_reg_bits(state, 0xd2e1, 3, 1, &tmp);
438 if (ret)
439 goto err;
441 if (!tmp) {
442 dev_dbg(&state->i2c->dev, "%s: not ready\n", __func__);
443 return 0;
446 /* read value */
447 ret = af9013_rd_regs(state, 0xd2e3, buf, 3);
448 if (ret)
449 goto err;
451 snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];
453 /* read current modulation */
454 ret = af9013_rd_reg(state, 0xd3c1, &tmp);
455 if (ret)
456 goto err;
458 switch ((tmp >> 6) & 3) {
459 case 0:
460 len = ARRAY_SIZE(qpsk_snr_lut);
461 snr_lut = qpsk_snr_lut;
462 break;
463 case 1:
464 len = ARRAY_SIZE(qam16_snr_lut);
465 snr_lut = qam16_snr_lut;
466 break;
467 case 2:
468 len = ARRAY_SIZE(qam64_snr_lut);
469 snr_lut = qam64_snr_lut;
470 break;
471 default:
472 goto err;
473 break;
476 for (i = 0; i < len; i++) {
477 tmp = snr_lut[i].snr;
479 if (snr_val < snr_lut[i].val)
480 break;
482 state->snr = tmp * 10; /* dB/10 */
484 return ret;
485 err:
486 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
487 return ret;
490 static int af9013_statistics_signal_strength(struct dvb_frontend *fe)
492 struct af9013_state *state = fe->demodulator_priv;
493 int ret = 0;
494 u8 buf[2], rf_gain, if_gain;
495 int signal_strength;
497 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
499 if (!state->signal_strength_en)
500 return 0;
502 ret = af9013_rd_regs(state, 0xd07c, buf, 2);
503 if (ret)
504 goto err;
506 rf_gain = buf[0];
507 if_gain = buf[1];
509 signal_strength = (0xffff / \
510 (9 * (state->rf_50 + state->if_50) - \
511 11 * (state->rf_80 + state->if_80))) * \
512 (10 * (rf_gain + if_gain) - \
513 11 * (state->rf_80 + state->if_80));
514 if (signal_strength < 0)
515 signal_strength = 0;
516 else if (signal_strength > 0xffff)
517 signal_strength = 0xffff;
519 state->signal_strength = signal_strength;
521 return ret;
522 err:
523 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
524 return ret;
527 static void af9013_statistics_work(struct work_struct *work)
529 struct af9013_state *state = container_of(work,
530 struct af9013_state, statistics_work.work);
531 unsigned int next_msec;
533 /* update only signal strength when demod is not locked */
534 if (!(state->fe_status & FE_HAS_LOCK)) {
535 state->statistics_step = 0;
536 state->ber = 0;
537 state->snr = 0;
540 switch (state->statistics_step) {
541 default:
542 state->statistics_step = 0;
543 case 0:
544 af9013_statistics_signal_strength(&state->fe);
545 state->statistics_step++;
546 next_msec = 300;
547 break;
548 case 1:
549 af9013_statistics_snr_start(&state->fe);
550 state->statistics_step++;
551 next_msec = 200;
552 break;
553 case 2:
554 af9013_statistics_ber_unc_start(&state->fe);
555 state->statistics_step++;
556 next_msec = 1000;
557 break;
558 case 3:
559 af9013_statistics_snr_result(&state->fe);
560 state->statistics_step++;
561 next_msec = 400;
562 break;
563 case 4:
564 af9013_statistics_ber_unc_result(&state->fe);
565 state->statistics_step++;
566 next_msec = 100;
567 break;
570 schedule_delayed_work(&state->statistics_work,
571 msecs_to_jiffies(next_msec));
574 static int af9013_get_tune_settings(struct dvb_frontend *fe,
575 struct dvb_frontend_tune_settings *fesettings)
577 fesettings->min_delay_ms = 800;
578 fesettings->step_size = 0;
579 fesettings->max_drift = 0;
581 return 0;
584 static int af9013_set_frontend(struct dvb_frontend *fe)
586 struct af9013_state *state = fe->demodulator_priv;
587 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
588 int ret, i, sampling_freq;
589 bool auto_mode, spec_inv;
590 u8 buf[6];
591 u32 if_frequency, freq_cw;
593 dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
594 __func__, c->frequency, c->bandwidth_hz);
596 /* program tuner */
597 if (fe->ops.tuner_ops.set_params)
598 fe->ops.tuner_ops.set_params(fe);
600 /* program CFOE coefficients */
601 if (c->bandwidth_hz != state->bandwidth_hz) {
602 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
603 if (coeff_lut[i].clock == state->config.clock &&
604 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
605 break;
609 ret = af9013_wr_regs(state, 0xae00, coeff_lut[i].val,
610 sizeof(coeff_lut[i].val));
613 /* program frequency control */
614 if (c->bandwidth_hz != state->bandwidth_hz || state->first_tune) {
615 /* get used IF frequency */
616 if (fe->ops.tuner_ops.get_if_frequency)
617 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
618 else
619 if_frequency = state->config.if_frequency;
621 dev_dbg(&state->i2c->dev, "%s: if_frequency=%d\n",
622 __func__, if_frequency);
624 sampling_freq = if_frequency;
626 while (sampling_freq > (state->config.clock / 2))
627 sampling_freq -= state->config.clock;
629 if (sampling_freq < 0) {
630 sampling_freq *= -1;
631 spec_inv = state->config.spec_inv;
632 } else {
633 spec_inv = !state->config.spec_inv;
636 freq_cw = af9013_div(state, sampling_freq, state->config.clock,
637 23);
639 if (spec_inv)
640 freq_cw = 0x800000 - freq_cw;
642 buf[0] = (freq_cw >> 0) & 0xff;
643 buf[1] = (freq_cw >> 8) & 0xff;
644 buf[2] = (freq_cw >> 16) & 0x7f;
646 freq_cw = 0x800000 - freq_cw;
648 buf[3] = (freq_cw >> 0) & 0xff;
649 buf[4] = (freq_cw >> 8) & 0xff;
650 buf[5] = (freq_cw >> 16) & 0x7f;
652 ret = af9013_wr_regs(state, 0xd140, buf, 3);
653 if (ret)
654 goto err;
656 ret = af9013_wr_regs(state, 0x9be7, buf, 6);
657 if (ret)
658 goto err;
661 /* clear TPS lock flag */
662 ret = af9013_wr_reg_bits(state, 0xd330, 3, 1, 1);
663 if (ret)
664 goto err;
666 /* clear MPEG2 lock flag */
667 ret = af9013_wr_reg_bits(state, 0xd507, 6, 1, 0);
668 if (ret)
669 goto err;
671 /* empty channel function */
672 ret = af9013_wr_reg_bits(state, 0x9bfe, 0, 1, 0);
673 if (ret)
674 goto err;
676 /* empty DVB-T channel function */
677 ret = af9013_wr_reg_bits(state, 0x9bc2, 0, 1, 0);
678 if (ret)
679 goto err;
681 /* transmission parameters */
682 auto_mode = false;
683 memset(buf, 0, 3);
685 switch (c->transmission_mode) {
686 case TRANSMISSION_MODE_AUTO:
687 auto_mode = 1;
688 break;
689 case TRANSMISSION_MODE_2K:
690 break;
691 case TRANSMISSION_MODE_8K:
692 buf[0] |= (1 << 0);
693 break;
694 default:
695 dev_dbg(&state->i2c->dev, "%s: invalid transmission_mode\n",
696 __func__);
697 auto_mode = 1;
700 switch (c->guard_interval) {
701 case GUARD_INTERVAL_AUTO:
702 auto_mode = 1;
703 break;
704 case GUARD_INTERVAL_1_32:
705 break;
706 case GUARD_INTERVAL_1_16:
707 buf[0] |= (1 << 2);
708 break;
709 case GUARD_INTERVAL_1_8:
710 buf[0] |= (2 << 2);
711 break;
712 case GUARD_INTERVAL_1_4:
713 buf[0] |= (3 << 2);
714 break;
715 default:
716 dev_dbg(&state->i2c->dev, "%s: invalid guard_interval\n",
717 __func__);
718 auto_mode = 1;
721 switch (c->hierarchy) {
722 case HIERARCHY_AUTO:
723 auto_mode = 1;
724 break;
725 case HIERARCHY_NONE:
726 break;
727 case HIERARCHY_1:
728 buf[0] |= (1 << 4);
729 break;
730 case HIERARCHY_2:
731 buf[0] |= (2 << 4);
732 break;
733 case HIERARCHY_4:
734 buf[0] |= (3 << 4);
735 break;
736 default:
737 dev_dbg(&state->i2c->dev, "%s: invalid hierarchy\n", __func__);
738 auto_mode = 1;
741 switch (c->modulation) {
742 case QAM_AUTO:
743 auto_mode = 1;
744 break;
745 case QPSK:
746 break;
747 case QAM_16:
748 buf[1] |= (1 << 6);
749 break;
750 case QAM_64:
751 buf[1] |= (2 << 6);
752 break;
753 default:
754 dev_dbg(&state->i2c->dev, "%s: invalid modulation\n", __func__);
755 auto_mode = 1;
758 /* Use HP. How and which case we can switch to LP? */
759 buf[1] |= (1 << 4);
761 switch (c->code_rate_HP) {
762 case FEC_AUTO:
763 auto_mode = 1;
764 break;
765 case FEC_1_2:
766 break;
767 case FEC_2_3:
768 buf[2] |= (1 << 0);
769 break;
770 case FEC_3_4:
771 buf[2] |= (2 << 0);
772 break;
773 case FEC_5_6:
774 buf[2] |= (3 << 0);
775 break;
776 case FEC_7_8:
777 buf[2] |= (4 << 0);
778 break;
779 default:
780 dev_dbg(&state->i2c->dev, "%s: invalid code_rate_HP\n",
781 __func__);
782 auto_mode = 1;
785 switch (c->code_rate_LP) {
786 case FEC_AUTO:
787 auto_mode = 1;
788 break;
789 case FEC_1_2:
790 break;
791 case FEC_2_3:
792 buf[2] |= (1 << 3);
793 break;
794 case FEC_3_4:
795 buf[2] |= (2 << 3);
796 break;
797 case FEC_5_6:
798 buf[2] |= (3 << 3);
799 break;
800 case FEC_7_8:
801 buf[2] |= (4 << 3);
802 break;
803 case FEC_NONE:
804 break;
805 default:
806 dev_dbg(&state->i2c->dev, "%s: invalid code_rate_LP\n",
807 __func__);
808 auto_mode = 1;
811 switch (c->bandwidth_hz) {
812 case 6000000:
813 break;
814 case 7000000:
815 buf[1] |= (1 << 2);
816 break;
817 case 8000000:
818 buf[1] |= (2 << 2);
819 break;
820 default:
821 dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n",
822 __func__);
823 ret = -EINVAL;
824 goto err;
827 ret = af9013_wr_regs(state, 0xd3c0, buf, 3);
828 if (ret)
829 goto err;
831 if (auto_mode) {
832 /* clear easy mode flag */
833 ret = af9013_wr_reg(state, 0xaefd, 0);
834 if (ret)
835 goto err;
837 dev_dbg(&state->i2c->dev, "%s: auto params\n", __func__);
838 } else {
839 /* set easy mode flag */
840 ret = af9013_wr_reg(state, 0xaefd, 1);
841 if (ret)
842 goto err;
844 ret = af9013_wr_reg(state, 0xaefe, 0);
845 if (ret)
846 goto err;
848 dev_dbg(&state->i2c->dev, "%s: manual params\n", __func__);
851 /* tune */
852 ret = af9013_wr_reg(state, 0xffff, 0);
853 if (ret)
854 goto err;
856 state->bandwidth_hz = c->bandwidth_hz;
857 state->set_frontend_jiffies = jiffies;
858 state->first_tune = false;
860 return ret;
861 err:
862 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
863 return ret;
866 static int af9013_get_frontend(struct dvb_frontend *fe)
868 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
869 struct af9013_state *state = fe->demodulator_priv;
870 int ret;
871 u8 buf[3];
873 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
875 ret = af9013_rd_regs(state, 0xd3c0, buf, 3);
876 if (ret)
877 goto err;
879 switch ((buf[1] >> 6) & 3) {
880 case 0:
881 c->modulation = QPSK;
882 break;
883 case 1:
884 c->modulation = QAM_16;
885 break;
886 case 2:
887 c->modulation = QAM_64;
888 break;
891 switch ((buf[0] >> 0) & 3) {
892 case 0:
893 c->transmission_mode = TRANSMISSION_MODE_2K;
894 break;
895 case 1:
896 c->transmission_mode = TRANSMISSION_MODE_8K;
899 switch ((buf[0] >> 2) & 3) {
900 case 0:
901 c->guard_interval = GUARD_INTERVAL_1_32;
902 break;
903 case 1:
904 c->guard_interval = GUARD_INTERVAL_1_16;
905 break;
906 case 2:
907 c->guard_interval = GUARD_INTERVAL_1_8;
908 break;
909 case 3:
910 c->guard_interval = GUARD_INTERVAL_1_4;
911 break;
914 switch ((buf[0] >> 4) & 7) {
915 case 0:
916 c->hierarchy = HIERARCHY_NONE;
917 break;
918 case 1:
919 c->hierarchy = HIERARCHY_1;
920 break;
921 case 2:
922 c->hierarchy = HIERARCHY_2;
923 break;
924 case 3:
925 c->hierarchy = HIERARCHY_4;
926 break;
929 switch ((buf[2] >> 0) & 7) {
930 case 0:
931 c->code_rate_HP = FEC_1_2;
932 break;
933 case 1:
934 c->code_rate_HP = FEC_2_3;
935 break;
936 case 2:
937 c->code_rate_HP = FEC_3_4;
938 break;
939 case 3:
940 c->code_rate_HP = FEC_5_6;
941 break;
942 case 4:
943 c->code_rate_HP = FEC_7_8;
944 break;
947 switch ((buf[2] >> 3) & 7) {
948 case 0:
949 c->code_rate_LP = FEC_1_2;
950 break;
951 case 1:
952 c->code_rate_LP = FEC_2_3;
953 break;
954 case 2:
955 c->code_rate_LP = FEC_3_4;
956 break;
957 case 3:
958 c->code_rate_LP = FEC_5_6;
959 break;
960 case 4:
961 c->code_rate_LP = FEC_7_8;
962 break;
965 switch ((buf[1] >> 2) & 3) {
966 case 0:
967 c->bandwidth_hz = 6000000;
968 break;
969 case 1:
970 c->bandwidth_hz = 7000000;
971 break;
972 case 2:
973 c->bandwidth_hz = 8000000;
974 break;
977 return ret;
978 err:
979 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
980 return ret;
983 static int af9013_read_status(struct dvb_frontend *fe, fe_status_t *status)
985 struct af9013_state *state = fe->demodulator_priv;
986 int ret;
987 u8 tmp;
990 * Return status from the cache if it is younger than 2000ms with the
991 * exception of last tune is done during 4000ms.
993 if (time_is_after_jiffies(
994 state->read_status_jiffies + msecs_to_jiffies(2000)) &&
995 time_is_before_jiffies(
996 state->set_frontend_jiffies + msecs_to_jiffies(4000))
998 *status = state->fe_status;
999 return 0;
1000 } else {
1001 *status = 0;
1004 /* MPEG2 lock */
1005 ret = af9013_rd_reg_bits(state, 0xd507, 6, 1, &tmp);
1006 if (ret)
1007 goto err;
1009 if (tmp)
1010 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
1011 FE_HAS_SYNC | FE_HAS_LOCK;
1013 if (!*status) {
1014 /* TPS lock */
1015 ret = af9013_rd_reg_bits(state, 0xd330, 3, 1, &tmp);
1016 if (ret)
1017 goto err;
1019 if (tmp)
1020 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
1021 FE_HAS_VITERBI;
1024 state->fe_status = *status;
1025 state->read_status_jiffies = jiffies;
1027 return ret;
1028 err:
1029 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
1030 return ret;
1033 static int af9013_read_snr(struct dvb_frontend *fe, u16 *snr)
1035 struct af9013_state *state = fe->demodulator_priv;
1036 *snr = state->snr;
1037 return 0;
1040 static int af9013_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
1042 struct af9013_state *state = fe->demodulator_priv;
1043 *strength = state->signal_strength;
1044 return 0;
1047 static int af9013_read_ber(struct dvb_frontend *fe, u32 *ber)
1049 struct af9013_state *state = fe->demodulator_priv;
1050 *ber = state->ber;
1051 return 0;
1054 static int af9013_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1056 struct af9013_state *state = fe->demodulator_priv;
1057 *ucblocks = state->ucblocks;
1058 return 0;
1061 static int af9013_init(struct dvb_frontend *fe)
1063 struct af9013_state *state = fe->demodulator_priv;
1064 int ret, i, len;
1065 u8 buf[3], tmp;
1066 u32 adc_cw;
1067 const struct af9013_reg_bit *init;
1069 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
1071 /* power on */
1072 ret = af9013_power_ctrl(state, 1);
1073 if (ret)
1074 goto err;
1076 /* enable ADC */
1077 ret = af9013_wr_reg(state, 0xd73a, 0xa4);
1078 if (ret)
1079 goto err;
1081 /* write API version to firmware */
1082 ret = af9013_wr_regs(state, 0x9bf2, state->config.api_version, 4);
1083 if (ret)
1084 goto err;
1086 /* program ADC control */
1087 switch (state->config.clock) {
1088 case 28800000: /* 28.800 MHz */
1089 tmp = 0;
1090 break;
1091 case 20480000: /* 20.480 MHz */
1092 tmp = 1;
1093 break;
1094 case 28000000: /* 28.000 MHz */
1095 tmp = 2;
1096 break;
1097 case 25000000: /* 25.000 MHz */
1098 tmp = 3;
1099 break;
1100 default:
1101 dev_err(&state->i2c->dev, "%s: invalid clock\n",
1102 KBUILD_MODNAME);
1103 return -EINVAL;
1106 adc_cw = af9013_div(state, state->config.clock, 1000000ul, 19);
1107 buf[0] = (adc_cw >> 0) & 0xff;
1108 buf[1] = (adc_cw >> 8) & 0xff;
1109 buf[2] = (adc_cw >> 16) & 0xff;
1111 ret = af9013_wr_regs(state, 0xd180, buf, 3);
1112 if (ret)
1113 goto err;
1115 ret = af9013_wr_reg_bits(state, 0x9bd2, 0, 4, tmp);
1116 if (ret)
1117 goto err;
1119 /* set I2C master clock */
1120 ret = af9013_wr_reg(state, 0xd416, 0x14);
1121 if (ret)
1122 goto err;
1124 /* set 16 embx */
1125 ret = af9013_wr_reg_bits(state, 0xd700, 1, 1, 1);
1126 if (ret)
1127 goto err;
1129 /* set no trigger */
1130 ret = af9013_wr_reg_bits(state, 0xd700, 2, 1, 0);
1131 if (ret)
1132 goto err;
1134 /* set read-update bit for constellation */
1135 ret = af9013_wr_reg_bits(state, 0xd371, 1, 1, 1);
1136 if (ret)
1137 goto err;
1139 /* settings for mp2if */
1140 if (state->config.ts_mode == AF9013_TS_USB) {
1141 /* AF9015 split PSB to 1.5k + 0.5k */
1142 ret = af9013_wr_reg_bits(state, 0xd50b, 2, 1, 1);
1143 if (ret)
1144 goto err;
1145 } else {
1146 /* AF9013 change the output bit to data7 */
1147 ret = af9013_wr_reg_bits(state, 0xd500, 3, 1, 1);
1148 if (ret)
1149 goto err;
1151 /* AF9013 set mpeg to full speed */
1152 ret = af9013_wr_reg_bits(state, 0xd502, 4, 1, 1);
1153 if (ret)
1154 goto err;
1157 ret = af9013_wr_reg_bits(state, 0xd520, 4, 1, 1);
1158 if (ret)
1159 goto err;
1161 /* load OFSM settings */
1162 dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__);
1163 len = ARRAY_SIZE(ofsm_init);
1164 init = ofsm_init;
1165 for (i = 0; i < len; i++) {
1166 ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
1167 init[i].len, init[i].val);
1168 if (ret)
1169 goto err;
1172 /* load tuner specific settings */
1173 dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n",
1174 __func__);
1175 switch (state->config.tuner) {
1176 case AF9013_TUNER_MXL5003D:
1177 len = ARRAY_SIZE(tuner_init_mxl5003d);
1178 init = tuner_init_mxl5003d;
1179 break;
1180 case AF9013_TUNER_MXL5005D:
1181 case AF9013_TUNER_MXL5005R:
1182 case AF9013_TUNER_MXL5007T:
1183 len = ARRAY_SIZE(tuner_init_mxl5005);
1184 init = tuner_init_mxl5005;
1185 break;
1186 case AF9013_TUNER_ENV77H11D5:
1187 len = ARRAY_SIZE(tuner_init_env77h11d5);
1188 init = tuner_init_env77h11d5;
1189 break;
1190 case AF9013_TUNER_MT2060:
1191 len = ARRAY_SIZE(tuner_init_mt2060);
1192 init = tuner_init_mt2060;
1193 break;
1194 case AF9013_TUNER_MC44S803:
1195 len = ARRAY_SIZE(tuner_init_mc44s803);
1196 init = tuner_init_mc44s803;
1197 break;
1198 case AF9013_TUNER_QT1010:
1199 case AF9013_TUNER_QT1010A:
1200 len = ARRAY_SIZE(tuner_init_qt1010);
1201 init = tuner_init_qt1010;
1202 break;
1203 case AF9013_TUNER_MT2060_2:
1204 len = ARRAY_SIZE(tuner_init_mt2060_2);
1205 init = tuner_init_mt2060_2;
1206 break;
1207 case AF9013_TUNER_TDA18271:
1208 case AF9013_TUNER_TDA18218:
1209 len = ARRAY_SIZE(tuner_init_tda18271);
1210 init = tuner_init_tda18271;
1211 break;
1212 case AF9013_TUNER_UNKNOWN:
1213 default:
1214 len = ARRAY_SIZE(tuner_init_unknown);
1215 init = tuner_init_unknown;
1216 break;
1219 for (i = 0; i < len; i++) {
1220 ret = af9013_wr_reg_bits(state, init[i].addr, init[i].pos,
1221 init[i].len, init[i].val);
1222 if (ret)
1223 goto err;
1226 /* TS mode */
1227 ret = af9013_wr_reg_bits(state, 0xd500, 1, 2, state->config.ts_mode);
1228 if (ret)
1229 goto err;
1231 /* enable lock led */
1232 ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 1);
1233 if (ret)
1234 goto err;
1236 /* check if we support signal strength */
1237 if (!state->signal_strength_en) {
1238 ret = af9013_rd_reg_bits(state, 0x9bee, 0, 1,
1239 &state->signal_strength_en);
1240 if (ret)
1241 goto err;
1244 /* read values needed for signal strength calculation */
1245 if (state->signal_strength_en && !state->rf_50) {
1246 ret = af9013_rd_reg(state, 0x9bbd, &state->rf_50);
1247 if (ret)
1248 goto err;
1250 ret = af9013_rd_reg(state, 0x9bd0, &state->rf_80);
1251 if (ret)
1252 goto err;
1254 ret = af9013_rd_reg(state, 0x9be2, &state->if_50);
1255 if (ret)
1256 goto err;
1258 ret = af9013_rd_reg(state, 0x9be4, &state->if_80);
1259 if (ret)
1260 goto err;
1263 /* SNR */
1264 ret = af9013_wr_reg(state, 0xd2e2, 1);
1265 if (ret)
1266 goto err;
1268 /* BER / UCB */
1269 buf[0] = (10000 >> 0) & 0xff;
1270 buf[1] = (10000 >> 8) & 0xff;
1271 ret = af9013_wr_regs(state, 0xd385, buf, 2);
1272 if (ret)
1273 goto err;
1275 /* enable FEC monitor */
1276 ret = af9013_wr_reg_bits(state, 0xd392, 1, 1, 1);
1277 if (ret)
1278 goto err;
1280 state->first_tune = true;
1281 schedule_delayed_work(&state->statistics_work, msecs_to_jiffies(400));
1283 return ret;
1284 err:
1285 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
1286 return ret;
1289 static int af9013_sleep(struct dvb_frontend *fe)
1291 struct af9013_state *state = fe->demodulator_priv;
1292 int ret;
1294 dev_dbg(&state->i2c->dev, "%s:\n", __func__);
1296 /* stop statistics polling */
1297 cancel_delayed_work_sync(&state->statistics_work);
1299 /* disable lock led */
1300 ret = af9013_wr_reg_bits(state, 0xd730, 0, 1, 0);
1301 if (ret)
1302 goto err;
1304 /* power off */
1305 ret = af9013_power_ctrl(state, 0);
1306 if (ret)
1307 goto err;
1309 return ret;
1310 err:
1311 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
1312 return ret;
1315 static int af9013_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
1317 int ret;
1318 struct af9013_state *state = fe->demodulator_priv;
1320 dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable);
1322 /* gate already open or close */
1323 if (state->i2c_gate_state == enable)
1324 return 0;
1326 if (state->config.ts_mode == AF9013_TS_USB)
1327 ret = af9013_wr_reg_bits(state, 0xd417, 3, 1, enable);
1328 else
1329 ret = af9013_wr_reg_bits(state, 0xd607, 2, 1, enable);
1330 if (ret)
1331 goto err;
1333 state->i2c_gate_state = enable;
1335 return ret;
1336 err:
1337 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
1338 return ret;
1341 static void af9013_release(struct dvb_frontend *fe)
1343 struct af9013_state *state = fe->demodulator_priv;
1344 kfree(state);
1347 static struct dvb_frontend_ops af9013_ops;
1349 static int af9013_download_firmware(struct af9013_state *state)
1351 int i, len, remaining, ret;
1352 const struct firmware *fw;
1353 u16 checksum = 0;
1354 u8 val;
1355 u8 fw_params[4];
1356 u8 *fw_file = AF9013_FIRMWARE;
1358 msleep(100);
1359 /* check whether firmware is already running */
1360 ret = af9013_rd_reg(state, 0x98be, &val);
1361 if (ret)
1362 goto err;
1363 else
1364 dev_dbg(&state->i2c->dev, "%s: firmware status=%02x\n",
1365 __func__, val);
1367 if (val == 0x0c) /* fw is running, no need for download */
1368 goto exit;
1370 dev_info(&state->i2c->dev, "%s: found a '%s' in cold state, will try " \
1371 "to load a firmware\n",
1372 KBUILD_MODNAME, af9013_ops.info.name);
1374 /* request the firmware, this will block and timeout */
1375 ret = request_firmware(&fw, fw_file, state->i2c->dev.parent);
1376 if (ret) {
1377 dev_info(&state->i2c->dev, "%s: did not find the firmware " \
1378 "file. (%s) Please see linux/Documentation/dvb/ for " \
1379 "more details on firmware-problems. (%d)\n",
1380 KBUILD_MODNAME, fw_file, ret);
1381 goto err;
1384 dev_info(&state->i2c->dev, "%s: downloading firmware from file '%s'\n",
1385 KBUILD_MODNAME, fw_file);
1387 /* calc checksum */
1388 for (i = 0; i < fw->size; i++)
1389 checksum += fw->data[i];
1391 fw_params[0] = checksum >> 8;
1392 fw_params[1] = checksum & 0xff;
1393 fw_params[2] = fw->size >> 8;
1394 fw_params[3] = fw->size & 0xff;
1396 /* write fw checksum & size */
1397 ret = af9013_write_ofsm_regs(state, 0x50fc,
1398 fw_params, sizeof(fw_params));
1399 if (ret)
1400 goto err_release;
1402 #define FW_ADDR 0x5100 /* firmware start address */
1403 #define LEN_MAX 16 /* max packet size */
1404 for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
1405 len = remaining;
1406 if (len > LEN_MAX)
1407 len = LEN_MAX;
1409 ret = af9013_write_ofsm_regs(state,
1410 FW_ADDR + fw->size - remaining,
1411 (u8 *) &fw->data[fw->size - remaining], len);
1412 if (ret) {
1413 dev_err(&state->i2c->dev,
1414 "%s: firmware download failed=%d\n",
1415 KBUILD_MODNAME, ret);
1416 goto err_release;
1420 /* request boot firmware */
1421 ret = af9013_wr_reg(state, 0xe205, 1);
1422 if (ret)
1423 goto err_release;
1425 for (i = 0; i < 15; i++) {
1426 msleep(100);
1428 /* check firmware status */
1429 ret = af9013_rd_reg(state, 0x98be, &val);
1430 if (ret)
1431 goto err_release;
1433 dev_dbg(&state->i2c->dev, "%s: firmware status=%02x\n",
1434 __func__, val);
1436 if (val == 0x0c || val == 0x04) /* success or fail */
1437 break;
1440 if (val == 0x04) {
1441 dev_err(&state->i2c->dev, "%s: firmware did not run\n",
1442 KBUILD_MODNAME);
1443 ret = -ENODEV;
1444 } else if (val != 0x0c) {
1445 dev_err(&state->i2c->dev, "%s: firmware boot timeout\n",
1446 KBUILD_MODNAME);
1447 ret = -ENODEV;
1450 err_release:
1451 release_firmware(fw);
1452 err:
1453 exit:
1454 if (!ret)
1455 dev_info(&state->i2c->dev, "%s: found a '%s' in warm state\n",
1456 KBUILD_MODNAME, af9013_ops.info.name);
1457 return ret;
1460 struct dvb_frontend *af9013_attach(const struct af9013_config *config,
1461 struct i2c_adapter *i2c)
1463 int ret;
1464 struct af9013_state *state = NULL;
1465 u8 buf[4], i;
1467 /* allocate memory for the internal state */
1468 state = kzalloc(sizeof(struct af9013_state), GFP_KERNEL);
1469 if (state == NULL)
1470 goto err;
1472 /* setup the state */
1473 state->i2c = i2c;
1474 memcpy(&state->config, config, sizeof(struct af9013_config));
1476 /* download firmware */
1477 if (state->config.ts_mode != AF9013_TS_USB) {
1478 ret = af9013_download_firmware(state);
1479 if (ret)
1480 goto err;
1483 /* firmware version */
1484 ret = af9013_rd_regs(state, 0x5103, buf, 4);
1485 if (ret)
1486 goto err;
1488 dev_info(&state->i2c->dev, "%s: firmware version %d.%d.%d.%d\n",
1489 KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3]);
1491 /* set GPIOs */
1492 for (i = 0; i < sizeof(state->config.gpio); i++) {
1493 ret = af9013_set_gpio(state, i, state->config.gpio[i]);
1494 if (ret)
1495 goto err;
1498 /* create dvb_frontend */
1499 memcpy(&state->fe.ops, &af9013_ops,
1500 sizeof(struct dvb_frontend_ops));
1501 state->fe.demodulator_priv = state;
1503 INIT_DELAYED_WORK(&state->statistics_work, af9013_statistics_work);
1505 return &state->fe;
1506 err:
1507 kfree(state);
1508 return NULL;
1510 EXPORT_SYMBOL(af9013_attach);
1512 static struct dvb_frontend_ops af9013_ops = {
1513 .delsys = { SYS_DVBT },
1514 .info = {
1515 .name = "Afatech AF9013",
1516 .frequency_min = 174000000,
1517 .frequency_max = 862000000,
1518 .frequency_stepsize = 250000,
1519 .frequency_tolerance = 0,
1520 .caps = FE_CAN_FEC_1_2 |
1521 FE_CAN_FEC_2_3 |
1522 FE_CAN_FEC_3_4 |
1523 FE_CAN_FEC_5_6 |
1524 FE_CAN_FEC_7_8 |
1525 FE_CAN_FEC_AUTO |
1526 FE_CAN_QPSK |
1527 FE_CAN_QAM_16 |
1528 FE_CAN_QAM_64 |
1529 FE_CAN_QAM_AUTO |
1530 FE_CAN_TRANSMISSION_MODE_AUTO |
1531 FE_CAN_GUARD_INTERVAL_AUTO |
1532 FE_CAN_HIERARCHY_AUTO |
1533 FE_CAN_RECOVER |
1534 FE_CAN_MUTE_TS
1537 .release = af9013_release,
1539 .init = af9013_init,
1540 .sleep = af9013_sleep,
1542 .get_tune_settings = af9013_get_tune_settings,
1543 .set_frontend = af9013_set_frontend,
1544 .get_frontend = af9013_get_frontend,
1546 .read_status = af9013_read_status,
1547 .read_snr = af9013_read_snr,
1548 .read_signal_strength = af9013_read_signal_strength,
1549 .read_ber = af9013_read_ber,
1550 .read_ucblocks = af9013_read_ucblocks,
1552 .i2c_gate_ctrl = af9013_i2c_gate_ctrl,
1555 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1556 MODULE_DESCRIPTION("Afatech AF9013 DVB-T demodulator driver");
1557 MODULE_LICENSE("GPL");
1558 MODULE_FIRMWARE(AF9013_FIRMWARE);