Linux 3.12.39
[linux/fpc-iii.git] / drivers / media / dvb-frontends / tda10071.c
blob287b977862e27052f366befc5b1a3dc5dc860e92
1 /*
2 * NXP TDA10071 + Conexant CX24118A DVB-S/S2 demodulator + tuner driver
4 * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "tda10071_priv.h"
23 /* Max transfer size done by I2C transfer functions */
24 #define MAX_XFER_SIZE 64
26 static struct dvb_frontend_ops tda10071_ops;
28 /* write multiple registers */
29 static int tda10071_wr_regs(struct tda10071_priv *priv, u8 reg, u8 *val,
30 int len)
32 int ret;
33 u8 buf[MAX_XFER_SIZE];
34 struct i2c_msg msg[1] = {
36 .addr = priv->cfg.demod_i2c_addr,
37 .flags = 0,
38 .len = 1 + len,
39 .buf = buf,
43 if (1 + len > sizeof(buf)) {
44 dev_warn(&priv->i2c->dev,
45 "%s: i2c wr reg=%04x: len=%d is too big!\n",
46 KBUILD_MODNAME, reg, len);
47 return -EINVAL;
50 buf[0] = reg;
51 memcpy(&buf[1], val, len);
53 ret = i2c_transfer(priv->i2c, msg, 1);
54 if (ret == 1) {
55 ret = 0;
56 } else {
57 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x " \
58 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
59 ret = -EREMOTEIO;
61 return ret;
64 /* read multiple registers */
65 static int tda10071_rd_regs(struct tda10071_priv *priv, u8 reg, u8 *val,
66 int len)
68 int ret;
69 u8 buf[MAX_XFER_SIZE];
70 struct i2c_msg msg[2] = {
72 .addr = priv->cfg.demod_i2c_addr,
73 .flags = 0,
74 .len = 1,
75 .buf = &reg,
76 }, {
77 .addr = priv->cfg.demod_i2c_addr,
78 .flags = I2C_M_RD,
79 .len = len,
80 .buf = buf,
84 if (len > sizeof(buf)) {
85 dev_warn(&priv->i2c->dev,
86 "%s: i2c wr reg=%04x: len=%d is too big!\n",
87 KBUILD_MODNAME, reg, len);
88 return -EINVAL;
91 ret = i2c_transfer(priv->i2c, msg, 2);
92 if (ret == 2) {
93 memcpy(val, buf, len);
94 ret = 0;
95 } else {
96 dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%02x " \
97 "len=%d\n", KBUILD_MODNAME, ret, reg, len);
98 ret = -EREMOTEIO;
100 return ret;
103 /* write single register */
104 static int tda10071_wr_reg(struct tda10071_priv *priv, u8 reg, u8 val)
106 return tda10071_wr_regs(priv, reg, &val, 1);
109 /* read single register */
110 static int tda10071_rd_reg(struct tda10071_priv *priv, u8 reg, u8 *val)
112 return tda10071_rd_regs(priv, reg, val, 1);
115 /* write single register with mask */
116 static int tda10071_wr_reg_mask(struct tda10071_priv *priv,
117 u8 reg, u8 val, u8 mask)
119 int ret;
120 u8 tmp;
122 /* no need for read if whole reg is written */
123 if (mask != 0xff) {
124 ret = tda10071_rd_regs(priv, reg, &tmp, 1);
125 if (ret)
126 return ret;
128 val &= mask;
129 tmp &= ~mask;
130 val |= tmp;
133 return tda10071_wr_regs(priv, reg, &val, 1);
136 /* read single register with mask */
137 static int tda10071_rd_reg_mask(struct tda10071_priv *priv,
138 u8 reg, u8 *val, u8 mask)
140 int ret, i;
141 u8 tmp;
143 ret = tda10071_rd_regs(priv, reg, &tmp, 1);
144 if (ret)
145 return ret;
147 tmp &= mask;
149 /* find position of the first bit */
150 for (i = 0; i < 8; i++) {
151 if ((mask >> i) & 0x01)
152 break;
154 *val = tmp >> i;
156 return 0;
159 /* execute firmware command */
160 static int tda10071_cmd_execute(struct tda10071_priv *priv,
161 struct tda10071_cmd *cmd)
163 int ret, i;
164 u8 tmp;
166 if (!priv->warm) {
167 ret = -EFAULT;
168 goto error;
171 /* write cmd and args for firmware */
172 ret = tda10071_wr_regs(priv, 0x00, cmd->args, cmd->len);
173 if (ret)
174 goto error;
176 /* start cmd execution */
177 ret = tda10071_wr_reg(priv, 0x1f, 1);
178 if (ret)
179 goto error;
181 /* wait cmd execution terminate */
182 for (i = 1000, tmp = 1; i && tmp; i--) {
183 ret = tda10071_rd_reg(priv, 0x1f, &tmp);
184 if (ret)
185 goto error;
187 usleep_range(200, 5000);
190 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
192 if (i == 0) {
193 ret = -ETIMEDOUT;
194 goto error;
197 return ret;
198 error:
199 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
200 return ret;
203 static int tda10071_set_tone(struct dvb_frontend *fe,
204 fe_sec_tone_mode_t fe_sec_tone_mode)
206 struct tda10071_priv *priv = fe->demodulator_priv;
207 struct tda10071_cmd cmd;
208 int ret;
209 u8 tone;
211 if (!priv->warm) {
212 ret = -EFAULT;
213 goto error;
216 dev_dbg(&priv->i2c->dev, "%s: tone_mode=%d\n", __func__,
217 fe_sec_tone_mode);
219 switch (fe_sec_tone_mode) {
220 case SEC_TONE_ON:
221 tone = 1;
222 break;
223 case SEC_TONE_OFF:
224 tone = 0;
225 break;
226 default:
227 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_tone_mode\n",
228 __func__);
229 ret = -EINVAL;
230 goto error;
233 cmd.args[0] = CMD_LNB_PCB_CONFIG;
234 cmd.args[1] = 0;
235 cmd.args[2] = 0x00;
236 cmd.args[3] = 0x00;
237 cmd.args[4] = tone;
238 cmd.len = 5;
239 ret = tda10071_cmd_execute(priv, &cmd);
240 if (ret)
241 goto error;
243 return ret;
244 error:
245 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
246 return ret;
249 static int tda10071_set_voltage(struct dvb_frontend *fe,
250 fe_sec_voltage_t fe_sec_voltage)
252 struct tda10071_priv *priv = fe->demodulator_priv;
253 struct tda10071_cmd cmd;
254 int ret;
255 u8 voltage;
257 if (!priv->warm) {
258 ret = -EFAULT;
259 goto error;
262 dev_dbg(&priv->i2c->dev, "%s: voltage=%d\n", __func__, fe_sec_voltage);
264 switch (fe_sec_voltage) {
265 case SEC_VOLTAGE_13:
266 voltage = 0;
267 break;
268 case SEC_VOLTAGE_18:
269 voltage = 1;
270 break;
271 case SEC_VOLTAGE_OFF:
272 voltage = 0;
273 break;
274 default:
275 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_voltage\n",
276 __func__);
277 ret = -EINVAL;
278 goto error;
281 cmd.args[0] = CMD_LNB_SET_DC_LEVEL;
282 cmd.args[1] = 0;
283 cmd.args[2] = voltage;
284 cmd.len = 3;
285 ret = tda10071_cmd_execute(priv, &cmd);
286 if (ret)
287 goto error;
289 return ret;
290 error:
291 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
292 return ret;
295 static int tda10071_diseqc_send_master_cmd(struct dvb_frontend *fe,
296 struct dvb_diseqc_master_cmd *diseqc_cmd)
298 struct tda10071_priv *priv = fe->demodulator_priv;
299 struct tda10071_cmd cmd;
300 int ret, i;
301 u8 tmp;
303 if (!priv->warm) {
304 ret = -EFAULT;
305 goto error;
308 dev_dbg(&priv->i2c->dev, "%s: msg_len=%d\n", __func__,
309 diseqc_cmd->msg_len);
311 if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) {
312 ret = -EINVAL;
313 goto error;
316 /* wait LNB TX */
317 for (i = 500, tmp = 0; i && !tmp; i--) {
318 ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x01);
319 if (ret)
320 goto error;
322 usleep_range(10000, 20000);
325 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
327 if (i == 0) {
328 ret = -ETIMEDOUT;
329 goto error;
332 ret = tda10071_wr_reg_mask(priv, 0x47, 0x00, 0x01);
333 if (ret)
334 goto error;
336 cmd.args[0] = CMD_LNB_SEND_DISEQC;
337 cmd.args[1] = 0;
338 cmd.args[2] = 0;
339 cmd.args[3] = 0;
340 cmd.args[4] = 2;
341 cmd.args[5] = 0;
342 cmd.args[6] = diseqc_cmd->msg_len;
343 memcpy(&cmd.args[7], diseqc_cmd->msg, diseqc_cmd->msg_len);
344 cmd.len = 7 + diseqc_cmd->msg_len;
345 ret = tda10071_cmd_execute(priv, &cmd);
346 if (ret)
347 goto error;
349 return ret;
350 error:
351 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
352 return ret;
355 static int tda10071_diseqc_recv_slave_reply(struct dvb_frontend *fe,
356 struct dvb_diseqc_slave_reply *reply)
358 struct tda10071_priv *priv = fe->demodulator_priv;
359 struct tda10071_cmd cmd;
360 int ret, i;
361 u8 tmp;
363 if (!priv->warm) {
364 ret = -EFAULT;
365 goto error;
368 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
370 /* wait LNB RX */
371 for (i = 500, tmp = 0; i && !tmp; i--) {
372 ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x02);
373 if (ret)
374 goto error;
376 usleep_range(10000, 20000);
379 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
381 if (i == 0) {
382 ret = -ETIMEDOUT;
383 goto error;
386 /* reply len */
387 ret = tda10071_rd_reg(priv, 0x46, &tmp);
388 if (ret)
389 goto error;
391 reply->msg_len = tmp & 0x1f; /* [4:0] */
392 if (reply->msg_len > sizeof(reply->msg))
393 reply->msg_len = sizeof(reply->msg); /* truncate API max */
395 /* read reply */
396 cmd.args[0] = CMD_LNB_UPDATE_REPLY;
397 cmd.args[1] = 0;
398 cmd.len = 2;
399 ret = tda10071_cmd_execute(priv, &cmd);
400 if (ret)
401 goto error;
403 ret = tda10071_rd_regs(priv, cmd.len, reply->msg, reply->msg_len);
404 if (ret)
405 goto error;
407 return ret;
408 error:
409 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
410 return ret;
413 static int tda10071_diseqc_send_burst(struct dvb_frontend *fe,
414 fe_sec_mini_cmd_t fe_sec_mini_cmd)
416 struct tda10071_priv *priv = fe->demodulator_priv;
417 struct tda10071_cmd cmd;
418 int ret, i;
419 u8 tmp, burst;
421 if (!priv->warm) {
422 ret = -EFAULT;
423 goto error;
426 dev_dbg(&priv->i2c->dev, "%s: fe_sec_mini_cmd=%d\n", __func__,
427 fe_sec_mini_cmd);
429 switch (fe_sec_mini_cmd) {
430 case SEC_MINI_A:
431 burst = 0;
432 break;
433 case SEC_MINI_B:
434 burst = 1;
435 break;
436 default:
437 dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_mini_cmd\n",
438 __func__);
439 ret = -EINVAL;
440 goto error;
443 /* wait LNB TX */
444 for (i = 500, tmp = 0; i && !tmp; i--) {
445 ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x01);
446 if (ret)
447 goto error;
449 usleep_range(10000, 20000);
452 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
454 if (i == 0) {
455 ret = -ETIMEDOUT;
456 goto error;
459 ret = tda10071_wr_reg_mask(priv, 0x47, 0x00, 0x01);
460 if (ret)
461 goto error;
463 cmd.args[0] = CMD_LNB_SEND_TONEBURST;
464 cmd.args[1] = 0;
465 cmd.args[2] = burst;
466 cmd.len = 3;
467 ret = tda10071_cmd_execute(priv, &cmd);
468 if (ret)
469 goto error;
471 return ret;
472 error:
473 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
474 return ret;
477 static int tda10071_read_status(struct dvb_frontend *fe, fe_status_t *status)
479 struct tda10071_priv *priv = fe->demodulator_priv;
480 int ret;
481 u8 tmp;
483 *status = 0;
485 if (!priv->warm) {
486 ret = 0;
487 goto error;
490 ret = tda10071_rd_reg(priv, 0x39, &tmp);
491 if (ret)
492 goto error;
494 if (tmp & 0x01) /* tuner PLL */
495 *status |= FE_HAS_SIGNAL;
496 if (tmp & 0x02) /* demod PLL */
497 *status |= FE_HAS_CARRIER;
498 if (tmp & 0x04) /* viterbi or LDPC*/
499 *status |= FE_HAS_VITERBI;
500 if (tmp & 0x08) /* RS or BCH */
501 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
503 priv->fe_status = *status;
505 return ret;
506 error:
507 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
508 return ret;
511 static int tda10071_read_snr(struct dvb_frontend *fe, u16 *snr)
513 struct tda10071_priv *priv = fe->demodulator_priv;
514 int ret;
515 u8 buf[2];
517 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
518 *snr = 0;
519 ret = 0;
520 goto error;
523 ret = tda10071_rd_regs(priv, 0x3a, buf, 2);
524 if (ret)
525 goto error;
527 /* Es/No dBx10 */
528 *snr = buf[0] << 8 | buf[1];
530 return ret;
531 error:
532 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
533 return ret;
536 static int tda10071_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
538 struct tda10071_priv *priv = fe->demodulator_priv;
539 struct tda10071_cmd cmd;
540 int ret;
541 u8 tmp;
543 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
544 *strength = 0;
545 ret = 0;
546 goto error;
549 cmd.args[0] = CMD_GET_AGCACC;
550 cmd.args[1] = 0;
551 cmd.len = 2;
552 ret = tda10071_cmd_execute(priv, &cmd);
553 if (ret)
554 goto error;
556 /* input power estimate dBm */
557 ret = tda10071_rd_reg(priv, 0x50, &tmp);
558 if (ret)
559 goto error;
561 if (tmp < 181)
562 tmp = 181; /* -75 dBm */
563 else if (tmp > 236)
564 tmp = 236; /* -20 dBm */
566 /* scale value to 0x0000-0xffff */
567 *strength = (tmp-181) * 0xffff / (236-181);
569 return ret;
570 error:
571 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
572 return ret;
575 static int tda10071_read_ber(struct dvb_frontend *fe, u32 *ber)
577 struct tda10071_priv *priv = fe->demodulator_priv;
578 struct tda10071_cmd cmd;
579 int ret, i, len;
580 u8 tmp, reg, buf[8];
582 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
583 *ber = priv->ber = 0;
584 ret = 0;
585 goto error;
588 switch (priv->delivery_system) {
589 case SYS_DVBS:
590 reg = 0x4c;
591 len = 8;
592 i = 1;
593 break;
594 case SYS_DVBS2:
595 reg = 0x4d;
596 len = 4;
597 i = 0;
598 break;
599 default:
600 *ber = priv->ber = 0;
601 return 0;
604 ret = tda10071_rd_reg(priv, reg, &tmp);
605 if (ret)
606 goto error;
608 if (priv->meas_count[i] == tmp) {
609 dev_dbg(&priv->i2c->dev, "%s: meas not ready=%02x\n", __func__,
610 tmp);
611 *ber = priv->ber;
612 return 0;
613 } else {
614 priv->meas_count[i] = tmp;
617 cmd.args[0] = CMD_BER_UPDATE_COUNTERS;
618 cmd.args[1] = 0;
619 cmd.args[2] = i;
620 cmd.len = 3;
621 ret = tda10071_cmd_execute(priv, &cmd);
622 if (ret)
623 goto error;
625 ret = tda10071_rd_regs(priv, cmd.len, buf, len);
626 if (ret)
627 goto error;
629 if (priv->delivery_system == SYS_DVBS) {
630 *ber = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
631 priv->ucb += (buf[4] << 8) | buf[5];
632 } else {
633 *ber = (buf[0] << 8) | buf[1];
635 priv->ber = *ber;
637 return ret;
638 error:
639 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
640 return ret;
643 static int tda10071_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
645 struct tda10071_priv *priv = fe->demodulator_priv;
646 int ret = 0;
648 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
649 *ucblocks = 0;
650 goto error;
653 /* UCB is updated when BER is read. Assume BER is read anyway. */
655 *ucblocks = priv->ucb;
657 return ret;
658 error:
659 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
660 return ret;
663 static int tda10071_set_frontend(struct dvb_frontend *fe)
665 struct tda10071_priv *priv = fe->demodulator_priv;
666 struct tda10071_cmd cmd;
667 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
668 int ret, i;
669 u8 mode, rolloff, pilot, inversion, div;
670 fe_modulation_t modulation;
672 dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d modulation=%d " \
673 "frequency=%d symbol_rate=%d inversion=%d pilot=%d " \
674 "rolloff=%d\n", __func__, c->delivery_system, c->modulation,
675 c->frequency, c->symbol_rate, c->inversion, c->pilot,
676 c->rolloff);
678 priv->delivery_system = SYS_UNDEFINED;
680 if (!priv->warm) {
681 ret = -EFAULT;
682 goto error;
685 switch (c->inversion) {
686 case INVERSION_OFF:
687 inversion = 1;
688 break;
689 case INVERSION_ON:
690 inversion = 0;
691 break;
692 case INVERSION_AUTO:
693 /* 2 = auto; try first on then off
694 * 3 = auto; try first off then on */
695 inversion = 3;
696 break;
697 default:
698 dev_dbg(&priv->i2c->dev, "%s: invalid inversion\n", __func__);
699 ret = -EINVAL;
700 goto error;
703 switch (c->delivery_system) {
704 case SYS_DVBS:
705 modulation = QPSK;
706 rolloff = 0;
707 pilot = 2;
708 break;
709 case SYS_DVBS2:
710 modulation = c->modulation;
712 switch (c->rolloff) {
713 case ROLLOFF_20:
714 rolloff = 2;
715 break;
716 case ROLLOFF_25:
717 rolloff = 1;
718 break;
719 case ROLLOFF_35:
720 rolloff = 0;
721 break;
722 case ROLLOFF_AUTO:
723 default:
724 dev_dbg(&priv->i2c->dev, "%s: invalid rolloff\n",
725 __func__);
726 ret = -EINVAL;
727 goto error;
730 switch (c->pilot) {
731 case PILOT_OFF:
732 pilot = 0;
733 break;
734 case PILOT_ON:
735 pilot = 1;
736 break;
737 case PILOT_AUTO:
738 pilot = 2;
739 break;
740 default:
741 dev_dbg(&priv->i2c->dev, "%s: invalid pilot\n",
742 __func__);
743 ret = -EINVAL;
744 goto error;
746 break;
747 default:
748 dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n",
749 __func__);
750 ret = -EINVAL;
751 goto error;
754 for (i = 0, mode = 0xff; i < ARRAY_SIZE(TDA10071_MODCOD); i++) {
755 if (c->delivery_system == TDA10071_MODCOD[i].delivery_system &&
756 modulation == TDA10071_MODCOD[i].modulation &&
757 c->fec_inner == TDA10071_MODCOD[i].fec) {
758 mode = TDA10071_MODCOD[i].val;
759 dev_dbg(&priv->i2c->dev, "%s: mode found=%02x\n",
760 __func__, mode);
761 break;
765 if (mode == 0xff) {
766 dev_dbg(&priv->i2c->dev, "%s: invalid parameter combination\n",
767 __func__);
768 ret = -EINVAL;
769 goto error;
772 if (c->symbol_rate <= 5000000)
773 div = 14;
774 else
775 div = 4;
777 ret = tda10071_wr_reg(priv, 0x81, div);
778 if (ret)
779 goto error;
781 ret = tda10071_wr_reg(priv, 0xe3, div);
782 if (ret)
783 goto error;
785 cmd.args[0] = CMD_CHANGE_CHANNEL;
786 cmd.args[1] = 0;
787 cmd.args[2] = mode;
788 cmd.args[3] = (c->frequency >> 16) & 0xff;
789 cmd.args[4] = (c->frequency >> 8) & 0xff;
790 cmd.args[5] = (c->frequency >> 0) & 0xff;
791 cmd.args[6] = ((c->symbol_rate / 1000) >> 8) & 0xff;
792 cmd.args[7] = ((c->symbol_rate / 1000) >> 0) & 0xff;
793 cmd.args[8] = (tda10071_ops.info.frequency_tolerance >> 8) & 0xff;
794 cmd.args[9] = (tda10071_ops.info.frequency_tolerance >> 0) & 0xff;
795 cmd.args[10] = rolloff;
796 cmd.args[11] = inversion;
797 cmd.args[12] = pilot;
798 cmd.args[13] = 0x00;
799 cmd.args[14] = 0x00;
800 cmd.len = 15;
801 ret = tda10071_cmd_execute(priv, &cmd);
802 if (ret)
803 goto error;
805 priv->delivery_system = c->delivery_system;
807 return ret;
808 error:
809 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
810 return ret;
813 static int tda10071_get_frontend(struct dvb_frontend *fe)
815 struct tda10071_priv *priv = fe->demodulator_priv;
816 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
817 int ret, i;
818 u8 buf[5], tmp;
820 if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
821 ret = -EFAULT;
822 goto error;
825 ret = tda10071_rd_regs(priv, 0x30, buf, 5);
826 if (ret)
827 goto error;
829 tmp = buf[0] & 0x3f;
830 for (i = 0; i < ARRAY_SIZE(TDA10071_MODCOD); i++) {
831 if (tmp == TDA10071_MODCOD[i].val) {
832 c->modulation = TDA10071_MODCOD[i].modulation;
833 c->fec_inner = TDA10071_MODCOD[i].fec;
834 c->delivery_system = TDA10071_MODCOD[i].delivery_system;
838 switch ((buf[1] >> 0) & 0x01) {
839 case 0:
840 c->inversion = INVERSION_OFF;
841 break;
842 case 1:
843 c->inversion = INVERSION_ON;
844 break;
847 switch ((buf[1] >> 7) & 0x01) {
848 case 0:
849 c->pilot = PILOT_OFF;
850 break;
851 case 1:
852 c->pilot = PILOT_ON;
853 break;
856 c->frequency = (buf[2] << 16) | (buf[3] << 8) | (buf[4] << 0);
858 ret = tda10071_rd_regs(priv, 0x52, buf, 3);
859 if (ret)
860 goto error;
862 c->symbol_rate = (buf[0] << 16) | (buf[1] << 8) | (buf[2] << 0);
864 return ret;
865 error:
866 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
867 return ret;
870 static int tda10071_init(struct dvb_frontend *fe)
872 struct tda10071_priv *priv = fe->demodulator_priv;
873 struct tda10071_cmd cmd;
874 int ret, i, len, remaining, fw_size;
875 const struct firmware *fw;
876 u8 *fw_file = TDA10071_FIRMWARE;
877 u8 tmp, buf[4];
878 struct tda10071_reg_val_mask tab[] = {
879 { 0xcd, 0x00, 0x07 },
880 { 0x80, 0x00, 0x02 },
881 { 0xcd, 0x00, 0xc0 },
882 { 0xce, 0x00, 0x1b },
883 { 0x9d, 0x00, 0x01 },
884 { 0x9d, 0x00, 0x02 },
885 { 0x9e, 0x00, 0x01 },
886 { 0x87, 0x00, 0x80 },
887 { 0xce, 0x00, 0x08 },
888 { 0xce, 0x00, 0x10 },
890 struct tda10071_reg_val_mask tab2[] = {
891 { 0xf1, 0x70, 0xff },
892 { 0x88, priv->cfg.pll_multiplier, 0x3f },
893 { 0x89, 0x00, 0x10 },
894 { 0x89, 0x10, 0x10 },
895 { 0xc0, 0x01, 0x01 },
896 { 0xc0, 0x00, 0x01 },
897 { 0xe0, 0xff, 0xff },
898 { 0xe0, 0x00, 0xff },
899 { 0x96, 0x1e, 0x7e },
900 { 0x8b, 0x08, 0x08 },
901 { 0x8b, 0x00, 0x08 },
902 { 0x8f, 0x1a, 0x7e },
903 { 0x8c, 0x68, 0xff },
904 { 0x8d, 0x08, 0xff },
905 { 0x8e, 0x4c, 0xff },
906 { 0x8f, 0x01, 0x01 },
907 { 0x8b, 0x04, 0x04 },
908 { 0x8b, 0x00, 0x04 },
909 { 0x87, 0x05, 0x07 },
910 { 0x80, 0x00, 0x20 },
911 { 0xc8, 0x01, 0xff },
912 { 0xb4, 0x47, 0xff },
913 { 0xb5, 0x9c, 0xff },
914 { 0xb6, 0x7d, 0xff },
915 { 0xba, 0x00, 0x03 },
916 { 0xb7, 0x47, 0xff },
917 { 0xb8, 0x9c, 0xff },
918 { 0xb9, 0x7d, 0xff },
919 { 0xba, 0x00, 0x0c },
920 { 0xc8, 0x00, 0xff },
921 { 0xcd, 0x00, 0x04 },
922 { 0xcd, 0x00, 0x20 },
923 { 0xe8, 0x02, 0xff },
924 { 0xcf, 0x20, 0xff },
925 { 0x9b, 0xd7, 0xff },
926 { 0x9a, 0x01, 0x03 },
927 { 0xa8, 0x05, 0x0f },
928 { 0xa8, 0x65, 0xf0 },
929 { 0xa6, 0xa0, 0xf0 },
930 { 0x9d, 0x50, 0xfc },
931 { 0x9e, 0x20, 0xe0 },
932 { 0xa3, 0x1c, 0x7c },
933 { 0xd5, 0x03, 0x03 },
936 if (priv->warm) {
937 /* warm state - wake up device from sleep */
939 for (i = 0; i < ARRAY_SIZE(tab); i++) {
940 ret = tda10071_wr_reg_mask(priv, tab[i].reg,
941 tab[i].val, tab[i].mask);
942 if (ret)
943 goto error;
946 cmd.args[0] = CMD_SET_SLEEP_MODE;
947 cmd.args[1] = 0;
948 cmd.args[2] = 0;
949 cmd.len = 3;
950 ret = tda10071_cmd_execute(priv, &cmd);
951 if (ret)
952 goto error;
953 } else {
954 /* cold state - try to download firmware */
956 /* request the firmware, this will block and timeout */
957 ret = request_firmware(&fw, fw_file, priv->i2c->dev.parent);
958 if (ret) {
959 dev_err(&priv->i2c->dev, "%s: did not find the " \
960 "firmware file. (%s) Please see " \
961 "linux/Documentation/dvb/ for more " \
962 "details on firmware-problems. (%d)\n",
963 KBUILD_MODNAME, fw_file, ret);
964 goto error;
967 /* init */
968 for (i = 0; i < ARRAY_SIZE(tab2); i++) {
969 ret = tda10071_wr_reg_mask(priv, tab2[i].reg,
970 tab2[i].val, tab2[i].mask);
971 if (ret)
972 goto error_release_firmware;
975 /* download firmware */
976 ret = tda10071_wr_reg(priv, 0xe0, 0x7f);
977 if (ret)
978 goto error_release_firmware;
980 ret = tda10071_wr_reg(priv, 0xf7, 0x81);
981 if (ret)
982 goto error_release_firmware;
984 ret = tda10071_wr_reg(priv, 0xf8, 0x00);
985 if (ret)
986 goto error_release_firmware;
988 ret = tda10071_wr_reg(priv, 0xf9, 0x00);
989 if (ret)
990 goto error_release_firmware;
992 dev_info(&priv->i2c->dev, "%s: found a '%s' in cold state, " \
993 "will try to load a firmware\n", KBUILD_MODNAME,
994 tda10071_ops.info.name);
995 dev_info(&priv->i2c->dev, "%s: downloading firmware from " \
996 "file '%s'\n", KBUILD_MODNAME, fw_file);
998 /* do not download last byte */
999 fw_size = fw->size - 1;
1001 for (remaining = fw_size; remaining > 0;
1002 remaining -= (priv->cfg.i2c_wr_max - 1)) {
1003 len = remaining;
1004 if (len > (priv->cfg.i2c_wr_max - 1))
1005 len = (priv->cfg.i2c_wr_max - 1);
1007 ret = tda10071_wr_regs(priv, 0xfa,
1008 (u8 *) &fw->data[fw_size - remaining], len);
1009 if (ret) {
1010 dev_err(&priv->i2c->dev, "%s: firmware " \
1011 "download failed=%d\n",
1012 KBUILD_MODNAME, ret);
1013 if (ret)
1014 goto error_release_firmware;
1017 release_firmware(fw);
1019 ret = tda10071_wr_reg(priv, 0xf7, 0x0c);
1020 if (ret)
1021 goto error;
1023 ret = tda10071_wr_reg(priv, 0xe0, 0x00);
1024 if (ret)
1025 goto error;
1027 /* wait firmware start */
1028 msleep(250);
1030 /* firmware status */
1031 ret = tda10071_rd_reg(priv, 0x51, &tmp);
1032 if (ret)
1033 goto error;
1035 if (tmp) {
1036 dev_info(&priv->i2c->dev, "%s: firmware did not run\n",
1037 KBUILD_MODNAME);
1038 ret = -EFAULT;
1039 goto error;
1040 } else {
1041 priv->warm = 1;
1044 cmd.args[0] = CMD_GET_FW_VERSION;
1045 cmd.len = 1;
1046 ret = tda10071_cmd_execute(priv, &cmd);
1047 if (ret)
1048 goto error;
1050 ret = tda10071_rd_regs(priv, cmd.len, buf, 4);
1051 if (ret)
1052 goto error;
1054 dev_info(&priv->i2c->dev, "%s: firmware version %d.%d.%d.%d\n",
1055 KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3]);
1056 dev_info(&priv->i2c->dev, "%s: found a '%s' in warm state\n",
1057 KBUILD_MODNAME, tda10071_ops.info.name);
1059 ret = tda10071_rd_regs(priv, 0x81, buf, 2);
1060 if (ret)
1061 goto error;
1063 cmd.args[0] = CMD_DEMOD_INIT;
1064 cmd.args[1] = ((priv->cfg.xtal / 1000) >> 8) & 0xff;
1065 cmd.args[2] = ((priv->cfg.xtal / 1000) >> 0) & 0xff;
1066 cmd.args[3] = buf[0];
1067 cmd.args[4] = buf[1];
1068 cmd.args[5] = priv->cfg.pll_multiplier;
1069 cmd.args[6] = priv->cfg.spec_inv;
1070 cmd.args[7] = 0x00;
1071 cmd.len = 8;
1072 ret = tda10071_cmd_execute(priv, &cmd);
1073 if (ret)
1074 goto error;
1076 cmd.args[0] = CMD_TUNER_INIT;
1077 cmd.args[1] = 0x00;
1078 cmd.args[2] = 0x00;
1079 cmd.args[3] = 0x00;
1080 cmd.args[4] = 0x00;
1081 cmd.args[5] = (priv->cfg.tuner_i2c_addr) ? priv->cfg.tuner_i2c_addr : 0x14;
1082 cmd.args[6] = 0x00;
1083 cmd.args[7] = 0x03;
1084 cmd.args[8] = 0x02;
1085 cmd.args[9] = 0x02;
1086 cmd.args[10] = 0x00;
1087 cmd.args[11] = 0x00;
1088 cmd.args[12] = 0x00;
1089 cmd.args[13] = 0x00;
1090 cmd.args[14] = 0x00;
1091 cmd.len = 15;
1092 ret = tda10071_cmd_execute(priv, &cmd);
1093 if (ret)
1094 goto error;
1096 cmd.args[0] = CMD_MPEG_CONFIG;
1097 cmd.args[1] = 0;
1098 cmd.args[2] = priv->cfg.ts_mode;
1099 cmd.args[3] = 0x00;
1100 cmd.args[4] = 0x04;
1101 cmd.args[5] = 0x00;
1102 cmd.len = 6;
1103 ret = tda10071_cmd_execute(priv, &cmd);
1104 if (ret)
1105 goto error;
1107 ret = tda10071_wr_reg_mask(priv, 0xf0, 0x01, 0x01);
1108 if (ret)
1109 goto error;
1111 cmd.args[0] = CMD_LNB_CONFIG;
1112 cmd.args[1] = 0;
1113 cmd.args[2] = 150;
1114 cmd.args[3] = 3;
1115 cmd.args[4] = 22;
1116 cmd.args[5] = 1;
1117 cmd.args[6] = 1;
1118 cmd.args[7] = 30;
1119 cmd.args[8] = 30;
1120 cmd.args[9] = 30;
1121 cmd.args[10] = 30;
1122 cmd.len = 11;
1123 ret = tda10071_cmd_execute(priv, &cmd);
1124 if (ret)
1125 goto error;
1127 cmd.args[0] = CMD_BER_CONTROL;
1128 cmd.args[1] = 0;
1129 cmd.args[2] = 14;
1130 cmd.args[3] = 14;
1131 cmd.len = 4;
1132 ret = tda10071_cmd_execute(priv, &cmd);
1133 if (ret)
1134 goto error;
1137 return ret;
1138 error_release_firmware:
1139 release_firmware(fw);
1140 error:
1141 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1142 return ret;
1145 static int tda10071_sleep(struct dvb_frontend *fe)
1147 struct tda10071_priv *priv = fe->demodulator_priv;
1148 struct tda10071_cmd cmd;
1149 int ret, i;
1150 struct tda10071_reg_val_mask tab[] = {
1151 { 0xcd, 0x07, 0x07 },
1152 { 0x80, 0x02, 0x02 },
1153 { 0xcd, 0xc0, 0xc0 },
1154 { 0xce, 0x1b, 0x1b },
1155 { 0x9d, 0x01, 0x01 },
1156 { 0x9d, 0x02, 0x02 },
1157 { 0x9e, 0x01, 0x01 },
1158 { 0x87, 0x80, 0x80 },
1159 { 0xce, 0x08, 0x08 },
1160 { 0xce, 0x10, 0x10 },
1163 if (!priv->warm) {
1164 ret = -EFAULT;
1165 goto error;
1168 cmd.args[0] = CMD_SET_SLEEP_MODE;
1169 cmd.args[1] = 0;
1170 cmd.args[2] = 1;
1171 cmd.len = 3;
1172 ret = tda10071_cmd_execute(priv, &cmd);
1173 if (ret)
1174 goto error;
1176 for (i = 0; i < ARRAY_SIZE(tab); i++) {
1177 ret = tda10071_wr_reg_mask(priv, tab[i].reg, tab[i].val,
1178 tab[i].mask);
1179 if (ret)
1180 goto error;
1183 return ret;
1184 error:
1185 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1186 return ret;
1189 static int tda10071_get_tune_settings(struct dvb_frontend *fe,
1190 struct dvb_frontend_tune_settings *s)
1192 s->min_delay_ms = 8000;
1193 s->step_size = 0;
1194 s->max_drift = 0;
1196 return 0;
1199 static void tda10071_release(struct dvb_frontend *fe)
1201 struct tda10071_priv *priv = fe->demodulator_priv;
1202 kfree(priv);
1205 struct dvb_frontend *tda10071_attach(const struct tda10071_config *config,
1206 struct i2c_adapter *i2c)
1208 int ret;
1209 struct tda10071_priv *priv = NULL;
1210 u8 tmp;
1212 /* allocate memory for the internal priv */
1213 priv = kzalloc(sizeof(struct tda10071_priv), GFP_KERNEL);
1214 if (priv == NULL) {
1215 ret = -ENOMEM;
1216 goto error;
1219 /* make sure demod i2c address is specified */
1220 if (!config->demod_i2c_addr) {
1221 dev_dbg(&i2c->dev, "%s: invalid demod i2c address!\n", __func__);
1222 ret = -EINVAL;
1223 goto error;
1226 /* make sure tuner i2c address is specified */
1227 if (!config->tuner_i2c_addr) {
1228 dev_dbg(&i2c->dev, "%s: invalid tuner i2c address!\n", __func__);
1229 ret = -EINVAL;
1230 goto error;
1233 /* setup the priv */
1234 priv->i2c = i2c;
1235 memcpy(&priv->cfg, config, sizeof(struct tda10071_config));
1237 /* chip ID */
1238 ret = tda10071_rd_reg(priv, 0xff, &tmp);
1239 if (ret || tmp != 0x0f)
1240 goto error;
1242 /* chip type */
1243 ret = tda10071_rd_reg(priv, 0xdd, &tmp);
1244 if (ret || tmp != 0x00)
1245 goto error;
1247 /* chip version */
1248 ret = tda10071_rd_reg(priv, 0xfe, &tmp);
1249 if (ret || tmp != 0x01)
1250 goto error;
1252 /* create dvb_frontend */
1253 memcpy(&priv->fe.ops, &tda10071_ops, sizeof(struct dvb_frontend_ops));
1254 priv->fe.demodulator_priv = priv;
1256 return &priv->fe;
1257 error:
1258 dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
1259 kfree(priv);
1260 return NULL;
1262 EXPORT_SYMBOL(tda10071_attach);
1264 static struct dvb_frontend_ops tda10071_ops = {
1265 .delsys = { SYS_DVBS, SYS_DVBS2 },
1266 .info = {
1267 .name = "NXP TDA10071",
1268 .frequency_min = 950000,
1269 .frequency_max = 2150000,
1270 .frequency_tolerance = 5000,
1271 .symbol_rate_min = 1000000,
1272 .symbol_rate_max = 45000000,
1273 .caps = FE_CAN_INVERSION_AUTO |
1274 FE_CAN_FEC_1_2 |
1275 FE_CAN_FEC_2_3 |
1276 FE_CAN_FEC_3_4 |
1277 FE_CAN_FEC_4_5 |
1278 FE_CAN_FEC_5_6 |
1279 FE_CAN_FEC_6_7 |
1280 FE_CAN_FEC_7_8 |
1281 FE_CAN_FEC_8_9 |
1282 FE_CAN_FEC_AUTO |
1283 FE_CAN_QPSK |
1284 FE_CAN_RECOVER |
1285 FE_CAN_2G_MODULATION
1288 .release = tda10071_release,
1290 .get_tune_settings = tda10071_get_tune_settings,
1292 .init = tda10071_init,
1293 .sleep = tda10071_sleep,
1295 .set_frontend = tda10071_set_frontend,
1296 .get_frontend = tda10071_get_frontend,
1298 .read_status = tda10071_read_status,
1299 .read_snr = tda10071_read_snr,
1300 .read_signal_strength = tda10071_read_signal_strength,
1301 .read_ber = tda10071_read_ber,
1302 .read_ucblocks = tda10071_read_ucblocks,
1304 .diseqc_send_master_cmd = tda10071_diseqc_send_master_cmd,
1305 .diseqc_recv_slave_reply = tda10071_diseqc_recv_slave_reply,
1306 .diseqc_send_burst = tda10071_diseqc_send_burst,
1308 .set_tone = tda10071_set_tone,
1309 .set_voltage = tda10071_set_voltage,
1312 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1313 MODULE_DESCRIPTION("NXP TDA10071 DVB-S/S2 demodulator driver");
1314 MODULE_LICENSE("GPL");
1315 MODULE_FIRMWARE(TDA10071_FIRMWARE);