xref: /linux/drivers/media/dvb-frontends/tda10071.c (revision 6eb2fb3170549737207974c2c6ad34bcc2f3025e)
1 /*
2  * NXP TDA10071 + Conexant CX24118A DVB-S/S2 demodulator + tuner driver
3  *
4  * Copyright (C) 2011 Antti Palosaari <crope@iki.fi>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "tda10071_priv.h"
22 
23 static struct dvb_frontend_ops tda10071_ops;
24 
25 /* write multiple registers */
26 static int tda10071_wr_regs(struct tda10071_priv *priv, u8 reg, u8 *val,
27 	int len)
28 {
29 	int ret;
30 	u8 buf[len+1];
31 	struct i2c_msg msg[1] = {
32 		{
33 			.addr = priv->cfg.demod_i2c_addr,
34 			.flags = 0,
35 			.len = sizeof(buf),
36 			.buf = buf,
37 		}
38 	};
39 
40 	buf[0] = reg;
41 	memcpy(&buf[1], val, len);
42 
43 	ret = i2c_transfer(priv->i2c, msg, 1);
44 	if (ret == 1) {
45 		ret = 0;
46 	} else {
47 		dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x " \
48 				"len=%d\n", KBUILD_MODNAME, ret, reg, len);
49 		ret = -EREMOTEIO;
50 	}
51 	return ret;
52 }
53 
54 /* read multiple registers */
55 static int tda10071_rd_regs(struct tda10071_priv *priv, u8 reg, u8 *val,
56 	int len)
57 {
58 	int ret;
59 	u8 buf[len];
60 	struct i2c_msg msg[2] = {
61 		{
62 			.addr = priv->cfg.demod_i2c_addr,
63 			.flags = 0,
64 			.len = 1,
65 			.buf = &reg,
66 		}, {
67 			.addr = priv->cfg.demod_i2c_addr,
68 			.flags = I2C_M_RD,
69 			.len = sizeof(buf),
70 			.buf = buf,
71 		}
72 	};
73 
74 	ret = i2c_transfer(priv->i2c, msg, 2);
75 	if (ret == 2) {
76 		memcpy(val, buf, len);
77 		ret = 0;
78 	} else {
79 		dev_warn(&priv->i2c->dev, "%s: i2c rd failed=%d reg=%02x " \
80 				"len=%d\n", KBUILD_MODNAME, ret, reg, len);
81 		ret = -EREMOTEIO;
82 	}
83 	return ret;
84 }
85 
86 /* write single register */
87 static int tda10071_wr_reg(struct tda10071_priv *priv, u8 reg, u8 val)
88 {
89 	return tda10071_wr_regs(priv, reg, &val, 1);
90 }
91 
92 /* read single register */
93 static int tda10071_rd_reg(struct tda10071_priv *priv, u8 reg, u8 *val)
94 {
95 	return tda10071_rd_regs(priv, reg, val, 1);
96 }
97 
98 /* write single register with mask */
99 static int tda10071_wr_reg_mask(struct tda10071_priv *priv,
100 				u8 reg, u8 val, u8 mask)
101 {
102 	int ret;
103 	u8 tmp;
104 
105 	/* no need for read if whole reg is written */
106 	if (mask != 0xff) {
107 		ret = tda10071_rd_regs(priv, reg, &tmp, 1);
108 		if (ret)
109 			return ret;
110 
111 		val &= mask;
112 		tmp &= ~mask;
113 		val |= tmp;
114 	}
115 
116 	return tda10071_wr_regs(priv, reg, &val, 1);
117 }
118 
119 /* read single register with mask */
120 static int tda10071_rd_reg_mask(struct tda10071_priv *priv,
121 				u8 reg, u8 *val, u8 mask)
122 {
123 	int ret, i;
124 	u8 tmp;
125 
126 	ret = tda10071_rd_regs(priv, reg, &tmp, 1);
127 	if (ret)
128 		return ret;
129 
130 	tmp &= mask;
131 
132 	/* find position of the first bit */
133 	for (i = 0; i < 8; i++) {
134 		if ((mask >> i) & 0x01)
135 			break;
136 	}
137 	*val = tmp >> i;
138 
139 	return 0;
140 }
141 
142 /* execute firmware command */
143 static int tda10071_cmd_execute(struct tda10071_priv *priv,
144 	struct tda10071_cmd *cmd)
145 {
146 	int ret, i;
147 	u8 tmp;
148 
149 	if (!priv->warm) {
150 		ret = -EFAULT;
151 		goto error;
152 	}
153 
154 	/* write cmd and args for firmware */
155 	ret = tda10071_wr_regs(priv, 0x00, cmd->args, cmd->len);
156 	if (ret)
157 		goto error;
158 
159 	/* start cmd execution */
160 	ret = tda10071_wr_reg(priv, 0x1f, 1);
161 	if (ret)
162 		goto error;
163 
164 	/* wait cmd execution terminate */
165 	for (i = 1000, tmp = 1; i && tmp; i--) {
166 		ret = tda10071_rd_reg(priv, 0x1f, &tmp);
167 		if (ret)
168 			goto error;
169 
170 		usleep_range(200, 5000);
171 	}
172 
173 	dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
174 
175 	if (i == 0) {
176 		ret = -ETIMEDOUT;
177 		goto error;
178 	}
179 
180 	return ret;
181 error:
182 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
183 	return ret;
184 }
185 
186 static int tda10071_set_tone(struct dvb_frontend *fe,
187 	fe_sec_tone_mode_t fe_sec_tone_mode)
188 {
189 	struct tda10071_priv *priv = fe->demodulator_priv;
190 	struct tda10071_cmd cmd;
191 	int ret;
192 	u8 tone;
193 
194 	if (!priv->warm) {
195 		ret = -EFAULT;
196 		goto error;
197 	}
198 
199 	dev_dbg(&priv->i2c->dev, "%s: tone_mode=%d\n", __func__,
200 			fe_sec_tone_mode);
201 
202 	switch (fe_sec_tone_mode) {
203 	case SEC_TONE_ON:
204 		tone = 1;
205 		break;
206 	case SEC_TONE_OFF:
207 		tone = 0;
208 		break;
209 	default:
210 		dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_tone_mode\n",
211 				__func__);
212 		ret = -EINVAL;
213 		goto error;
214 	}
215 
216 	cmd.args[0] = CMD_LNB_PCB_CONFIG;
217 	cmd.args[1] = 0;
218 	cmd.args[2] = 0x00;
219 	cmd.args[3] = 0x00;
220 	cmd.args[4] = tone;
221 	cmd.len = 5;
222 	ret = tda10071_cmd_execute(priv, &cmd);
223 	if (ret)
224 		goto error;
225 
226 	return ret;
227 error:
228 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
229 	return ret;
230 }
231 
232 static int tda10071_set_voltage(struct dvb_frontend *fe,
233 	fe_sec_voltage_t fe_sec_voltage)
234 {
235 	struct tda10071_priv *priv = fe->demodulator_priv;
236 	struct tda10071_cmd cmd;
237 	int ret;
238 	u8 voltage;
239 
240 	if (!priv->warm) {
241 		ret = -EFAULT;
242 		goto error;
243 	}
244 
245 	dev_dbg(&priv->i2c->dev, "%s: voltage=%d\n", __func__, fe_sec_voltage);
246 
247 	switch (fe_sec_voltage) {
248 	case SEC_VOLTAGE_13:
249 		voltage = 0;
250 		break;
251 	case SEC_VOLTAGE_18:
252 		voltage = 1;
253 		break;
254 	case SEC_VOLTAGE_OFF:
255 		voltage = 0;
256 		break;
257 	default:
258 		dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_voltage\n",
259 				__func__);
260 		ret = -EINVAL;
261 		goto error;
262 	}
263 
264 	cmd.args[0] = CMD_LNB_SET_DC_LEVEL;
265 	cmd.args[1] = 0;
266 	cmd.args[2] = voltage;
267 	cmd.len = 3;
268 	ret = tda10071_cmd_execute(priv, &cmd);
269 	if (ret)
270 		goto error;
271 
272 	return ret;
273 error:
274 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
275 	return ret;
276 }
277 
278 static int tda10071_diseqc_send_master_cmd(struct dvb_frontend *fe,
279 	struct dvb_diseqc_master_cmd *diseqc_cmd)
280 {
281 	struct tda10071_priv *priv = fe->demodulator_priv;
282 	struct tda10071_cmd cmd;
283 	int ret, i;
284 	u8 tmp;
285 
286 	if (!priv->warm) {
287 		ret = -EFAULT;
288 		goto error;
289 	}
290 
291 	dev_dbg(&priv->i2c->dev, "%s: msg_len=%d\n", __func__,
292 			diseqc_cmd->msg_len);
293 
294 	if (diseqc_cmd->msg_len < 3 || diseqc_cmd->msg_len > 6) {
295 		ret = -EINVAL;
296 		goto error;
297 	}
298 
299 	/* wait LNB TX */
300 	for (i = 500, tmp = 0; i && !tmp; i--) {
301 		ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x01);
302 		if (ret)
303 			goto error;
304 
305 		usleep_range(10000, 20000);
306 	}
307 
308 	dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
309 
310 	if (i == 0) {
311 		ret = -ETIMEDOUT;
312 		goto error;
313 	}
314 
315 	ret = tda10071_wr_reg_mask(priv, 0x47, 0x00, 0x01);
316 	if (ret)
317 		goto error;
318 
319 	cmd.args[0] = CMD_LNB_SEND_DISEQC;
320 	cmd.args[1] = 0;
321 	cmd.args[2] = 0;
322 	cmd.args[3] = 0;
323 	cmd.args[4] = 2;
324 	cmd.args[5] = 0;
325 	cmd.args[6] = diseqc_cmd->msg_len;
326 	memcpy(&cmd.args[7], diseqc_cmd->msg, diseqc_cmd->msg_len);
327 	cmd.len = 7 + diseqc_cmd->msg_len;
328 	ret = tda10071_cmd_execute(priv, &cmd);
329 	if (ret)
330 		goto error;
331 
332 	return ret;
333 error:
334 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
335 	return ret;
336 }
337 
338 static int tda10071_diseqc_recv_slave_reply(struct dvb_frontend *fe,
339 	struct dvb_diseqc_slave_reply *reply)
340 {
341 	struct tda10071_priv *priv = fe->demodulator_priv;
342 	struct tda10071_cmd cmd;
343 	int ret, i;
344 	u8 tmp;
345 
346 	if (!priv->warm) {
347 		ret = -EFAULT;
348 		goto error;
349 	}
350 
351 	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
352 
353 	/* wait LNB RX */
354 	for (i = 500, tmp = 0; i && !tmp; i--) {
355 		ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x02);
356 		if (ret)
357 			goto error;
358 
359 		usleep_range(10000, 20000);
360 	}
361 
362 	dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
363 
364 	if (i == 0) {
365 		ret = -ETIMEDOUT;
366 		goto error;
367 	}
368 
369 	/* reply len */
370 	ret = tda10071_rd_reg(priv, 0x46, &tmp);
371 	if (ret)
372 		goto error;
373 
374 	reply->msg_len = tmp & 0x1f; /* [4:0] */
375 	if (reply->msg_len > sizeof(reply->msg))
376 		reply->msg_len = sizeof(reply->msg); /* truncate API max */
377 
378 	/* read reply */
379 	cmd.args[0] = CMD_LNB_UPDATE_REPLY;
380 	cmd.args[1] = 0;
381 	cmd.len = 2;
382 	ret = tda10071_cmd_execute(priv, &cmd);
383 	if (ret)
384 		goto error;
385 
386 	ret = tda10071_rd_regs(priv, cmd.len, reply->msg, reply->msg_len);
387 	if (ret)
388 		goto error;
389 
390 	return ret;
391 error:
392 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
393 	return ret;
394 }
395 
396 static int tda10071_diseqc_send_burst(struct dvb_frontend *fe,
397 	fe_sec_mini_cmd_t fe_sec_mini_cmd)
398 {
399 	struct tda10071_priv *priv = fe->demodulator_priv;
400 	struct tda10071_cmd cmd;
401 	int ret, i;
402 	u8 tmp, burst;
403 
404 	if (!priv->warm) {
405 		ret = -EFAULT;
406 		goto error;
407 	}
408 
409 	dev_dbg(&priv->i2c->dev, "%s: fe_sec_mini_cmd=%d\n", __func__,
410 			fe_sec_mini_cmd);
411 
412 	switch (fe_sec_mini_cmd) {
413 	case SEC_MINI_A:
414 		burst = 0;
415 		break;
416 	case SEC_MINI_B:
417 		burst = 1;
418 		break;
419 	default:
420 		dev_dbg(&priv->i2c->dev, "%s: invalid fe_sec_mini_cmd\n",
421 				__func__);
422 		ret = -EINVAL;
423 		goto error;
424 	}
425 
426 	/* wait LNB TX */
427 	for (i = 500, tmp = 0; i && !tmp; i--) {
428 		ret = tda10071_rd_reg_mask(priv, 0x47, &tmp, 0x01);
429 		if (ret)
430 			goto error;
431 
432 		usleep_range(10000, 20000);
433 	}
434 
435 	dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
436 
437 	if (i == 0) {
438 		ret = -ETIMEDOUT;
439 		goto error;
440 	}
441 
442 	ret = tda10071_wr_reg_mask(priv, 0x47, 0x00, 0x01);
443 	if (ret)
444 		goto error;
445 
446 	cmd.args[0] = CMD_LNB_SEND_TONEBURST;
447 	cmd.args[1] = 0;
448 	cmd.args[2] = burst;
449 	cmd.len = 3;
450 	ret = tda10071_cmd_execute(priv, &cmd);
451 	if (ret)
452 		goto error;
453 
454 	return ret;
455 error:
456 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
457 	return ret;
458 }
459 
460 static int tda10071_read_status(struct dvb_frontend *fe, fe_status_t *status)
461 {
462 	struct tda10071_priv *priv = fe->demodulator_priv;
463 	int ret;
464 	u8 tmp;
465 
466 	*status = 0;
467 
468 	if (!priv->warm) {
469 		ret = 0;
470 		goto error;
471 	}
472 
473 	ret = tda10071_rd_reg(priv, 0x39, &tmp);
474 	if (ret)
475 		goto error;
476 
477 	if (tmp & 0x01) /* tuner PLL */
478 		*status |= FE_HAS_SIGNAL;
479 	if (tmp & 0x02) /* demod PLL */
480 		*status |= FE_HAS_CARRIER;
481 	if (tmp & 0x04) /* viterbi or LDPC*/
482 		*status |= FE_HAS_VITERBI;
483 	if (tmp & 0x08) /* RS or BCH */
484 		*status |= FE_HAS_SYNC | FE_HAS_LOCK;
485 
486 	priv->fe_status = *status;
487 
488 	return ret;
489 error:
490 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
491 	return ret;
492 }
493 
494 static int tda10071_read_snr(struct dvb_frontend *fe, u16 *snr)
495 {
496 	struct tda10071_priv *priv = fe->demodulator_priv;
497 	int ret;
498 	u8 buf[2];
499 
500 	if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
501 		*snr = 0;
502 		ret = 0;
503 		goto error;
504 	}
505 
506 	ret = tda10071_rd_regs(priv, 0x3a, buf, 2);
507 	if (ret)
508 		goto error;
509 
510 	/* Es/No dBx10 */
511 	*snr = buf[0] << 8 | buf[1];
512 
513 	return ret;
514 error:
515 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
516 	return ret;
517 }
518 
519 static int tda10071_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
520 {
521 	struct tda10071_priv *priv = fe->demodulator_priv;
522 	struct tda10071_cmd cmd;
523 	int ret;
524 	u8 tmp;
525 
526 	if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
527 		*strength = 0;
528 		ret = 0;
529 		goto error;
530 	}
531 
532 	cmd.args[0] = CMD_GET_AGCACC;
533 	cmd.args[1] = 0;
534 	cmd.len = 2;
535 	ret = tda10071_cmd_execute(priv, &cmd);
536 	if (ret)
537 		goto error;
538 
539 	/* input power estimate dBm */
540 	ret = tda10071_rd_reg(priv, 0x50, &tmp);
541 	if (ret)
542 		goto error;
543 
544 	if (tmp < 181)
545 		tmp = 181; /* -75 dBm */
546 	else if (tmp > 236)
547 		tmp = 236; /* -20 dBm */
548 
549 	/* scale value to 0x0000-0xffff */
550 	*strength = (tmp-181) * 0xffff / (236-181);
551 
552 	return ret;
553 error:
554 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
555 	return ret;
556 }
557 
558 static int tda10071_read_ber(struct dvb_frontend *fe, u32 *ber)
559 {
560 	struct tda10071_priv *priv = fe->demodulator_priv;
561 	struct tda10071_cmd cmd;
562 	int ret, i, len;
563 	u8 tmp, reg, buf[8];
564 
565 	if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
566 		*ber = priv->ber = 0;
567 		ret = 0;
568 		goto error;
569 	}
570 
571 	switch (priv->delivery_system) {
572 	case SYS_DVBS:
573 		reg = 0x4c;
574 		len = 8;
575 		i = 1;
576 		break;
577 	case SYS_DVBS2:
578 		reg = 0x4d;
579 		len = 4;
580 		i = 0;
581 		break;
582 	default:
583 		*ber = priv->ber = 0;
584 		return 0;
585 	}
586 
587 	ret = tda10071_rd_reg(priv, reg, &tmp);
588 	if (ret)
589 		goto error;
590 
591 	if (priv->meas_count[i] == tmp) {
592 		dev_dbg(&priv->i2c->dev, "%s: meas not ready=%02x\n", __func__,
593 				tmp);
594 		*ber = priv->ber;
595 		return 0;
596 	} else {
597 		priv->meas_count[i] = tmp;
598 	}
599 
600 	cmd.args[0] = CMD_BER_UPDATE_COUNTERS;
601 	cmd.args[1] = 0;
602 	cmd.args[2] = i;
603 	cmd.len = 3;
604 	ret = tda10071_cmd_execute(priv, &cmd);
605 	if (ret)
606 		goto error;
607 
608 	ret = tda10071_rd_regs(priv, cmd.len, buf, len);
609 	if (ret)
610 		goto error;
611 
612 	if (priv->delivery_system == SYS_DVBS) {
613 		*ber = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
614 		priv->ucb += (buf[4] << 8) | buf[5];
615 	} else {
616 		*ber = (buf[0] << 8) | buf[1];
617 	}
618 	priv->ber = *ber;
619 
620 	return ret;
621 error:
622 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
623 	return ret;
624 }
625 
626 static int tda10071_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
627 {
628 	struct tda10071_priv *priv = fe->demodulator_priv;
629 	int ret = 0;
630 
631 	if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
632 		*ucblocks = 0;
633 		goto error;
634 	}
635 
636 	/* UCB is updated when BER is read. Assume BER is read anyway. */
637 
638 	*ucblocks = priv->ucb;
639 
640 	return ret;
641 error:
642 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
643 	return ret;
644 }
645 
646 static int tda10071_set_frontend(struct dvb_frontend *fe)
647 {
648 	struct tda10071_priv *priv = fe->demodulator_priv;
649 	struct tda10071_cmd cmd;
650 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
651 	int ret, i;
652 	u8 mode, rolloff, pilot, inversion, div;
653 
654 	dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d modulation=%d " \
655 		"frequency=%d symbol_rate=%d inversion=%d pilot=%d " \
656 		"rolloff=%d\n", __func__, c->delivery_system, c->modulation,
657 		c->frequency, c->symbol_rate, c->inversion, c->pilot,
658 		c->rolloff);
659 
660 	priv->delivery_system = SYS_UNDEFINED;
661 
662 	if (!priv->warm) {
663 		ret = -EFAULT;
664 		goto error;
665 	}
666 
667 	switch (c->inversion) {
668 	case INVERSION_OFF:
669 		inversion = 1;
670 		break;
671 	case INVERSION_ON:
672 		inversion = 0;
673 		break;
674 	case INVERSION_AUTO:
675 		/* 2 = auto; try first on then off
676 		 * 3 = auto; try first off then on */
677 		inversion = 3;
678 		break;
679 	default:
680 		dev_dbg(&priv->i2c->dev, "%s: invalid inversion\n", __func__);
681 		ret = -EINVAL;
682 		goto error;
683 	}
684 
685 	switch (c->delivery_system) {
686 	case SYS_DVBS:
687 		rolloff = 0;
688 		pilot = 2;
689 		break;
690 	case SYS_DVBS2:
691 		switch (c->rolloff) {
692 		case ROLLOFF_20:
693 			rolloff = 2;
694 			break;
695 		case ROLLOFF_25:
696 			rolloff = 1;
697 			break;
698 		case ROLLOFF_35:
699 			rolloff = 0;
700 			break;
701 		case ROLLOFF_AUTO:
702 		default:
703 			dev_dbg(&priv->i2c->dev, "%s: invalid rolloff\n",
704 					__func__);
705 			ret = -EINVAL;
706 			goto error;
707 		}
708 
709 		switch (c->pilot) {
710 		case PILOT_OFF:
711 			pilot = 0;
712 			break;
713 		case PILOT_ON:
714 			pilot = 1;
715 			break;
716 		case PILOT_AUTO:
717 			pilot = 2;
718 			break;
719 		default:
720 			dev_dbg(&priv->i2c->dev, "%s: invalid pilot\n",
721 					__func__);
722 			ret = -EINVAL;
723 			goto error;
724 		}
725 		break;
726 	default:
727 		dev_dbg(&priv->i2c->dev, "%s: invalid delivery_system\n",
728 				__func__);
729 		ret = -EINVAL;
730 		goto error;
731 	}
732 
733 	for (i = 0, mode = 0xff; i < ARRAY_SIZE(TDA10071_MODCOD); i++) {
734 		if (c->delivery_system == TDA10071_MODCOD[i].delivery_system &&
735 			c->modulation == TDA10071_MODCOD[i].modulation &&
736 			c->fec_inner == TDA10071_MODCOD[i].fec) {
737 			mode = TDA10071_MODCOD[i].val;
738 			dev_dbg(&priv->i2c->dev, "%s: mode found=%02x\n",
739 					__func__, mode);
740 			break;
741 		}
742 	}
743 
744 	if (mode == 0xff) {
745 		dev_dbg(&priv->i2c->dev, "%s: invalid parameter combination\n",
746 				__func__);
747 		ret = -EINVAL;
748 		goto error;
749 	}
750 
751 	if (c->symbol_rate <= 5000000)
752 		div = 14;
753 	else
754 		div = 4;
755 
756 	ret = tda10071_wr_reg(priv, 0x81, div);
757 	if (ret)
758 		goto error;
759 
760 	ret = tda10071_wr_reg(priv, 0xe3, div);
761 	if (ret)
762 		goto error;
763 
764 	cmd.args[0] = CMD_CHANGE_CHANNEL;
765 	cmd.args[1] = 0;
766 	cmd.args[2] = mode;
767 	cmd.args[3] = (c->frequency >> 16) & 0xff;
768 	cmd.args[4] = (c->frequency >>  8) & 0xff;
769 	cmd.args[5] = (c->frequency >>  0) & 0xff;
770 	cmd.args[6] = ((c->symbol_rate / 1000) >> 8) & 0xff;
771 	cmd.args[7] = ((c->symbol_rate / 1000) >> 0) & 0xff;
772 	cmd.args[8] = (tda10071_ops.info.frequency_tolerance >> 8) & 0xff;
773 	cmd.args[9] = (tda10071_ops.info.frequency_tolerance >> 0) & 0xff;
774 	cmd.args[10] = rolloff;
775 	cmd.args[11] = inversion;
776 	cmd.args[12] = pilot;
777 	cmd.args[13] = 0x00;
778 	cmd.args[14] = 0x00;
779 	cmd.len = 15;
780 	ret = tda10071_cmd_execute(priv, &cmd);
781 	if (ret)
782 		goto error;
783 
784 	priv->delivery_system = c->delivery_system;
785 
786 	return ret;
787 error:
788 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
789 	return ret;
790 }
791 
792 static int tda10071_get_frontend(struct dvb_frontend *fe)
793 {
794 	struct tda10071_priv *priv = fe->demodulator_priv;
795 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
796 	int ret, i;
797 	u8 buf[5], tmp;
798 
799 	if (!priv->warm || !(priv->fe_status & FE_HAS_LOCK)) {
800 		ret = -EFAULT;
801 		goto error;
802 	}
803 
804 	ret = tda10071_rd_regs(priv, 0x30, buf, 5);
805 	if (ret)
806 		goto error;
807 
808 	tmp = buf[0] & 0x3f;
809 	for (i = 0; i < ARRAY_SIZE(TDA10071_MODCOD); i++) {
810 		if (tmp == TDA10071_MODCOD[i].val) {
811 			c->modulation = TDA10071_MODCOD[i].modulation;
812 			c->fec_inner = TDA10071_MODCOD[i].fec;
813 			c->delivery_system = TDA10071_MODCOD[i].delivery_system;
814 		}
815 	}
816 
817 	switch ((buf[1] >> 0) & 0x01) {
818 	case 0:
819 		c->inversion = INVERSION_OFF;
820 		break;
821 	case 1:
822 		c->inversion = INVERSION_ON;
823 		break;
824 	}
825 
826 	switch ((buf[1] >> 7) & 0x01) {
827 	case 0:
828 		c->pilot = PILOT_OFF;
829 		break;
830 	case 1:
831 		c->pilot = PILOT_ON;
832 		break;
833 	}
834 
835 	c->frequency = (buf[2] << 16) | (buf[3] << 8) | (buf[4] << 0);
836 
837 	ret = tda10071_rd_regs(priv, 0x52, buf, 3);
838 	if (ret)
839 		goto error;
840 
841 	c->symbol_rate = (buf[0] << 16) | (buf[1] << 8) | (buf[2] << 0);
842 
843 	return ret;
844 error:
845 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
846 	return ret;
847 }
848 
849 static int tda10071_init(struct dvb_frontend *fe)
850 {
851 	struct tda10071_priv *priv = fe->demodulator_priv;
852 	struct tda10071_cmd cmd;
853 	int ret, i, len, remaining, fw_size;
854 	const struct firmware *fw;
855 	u8 *fw_file = TDA10071_FIRMWARE;
856 	u8 tmp, buf[4];
857 	struct tda10071_reg_val_mask tab[] = {
858 		{ 0xcd, 0x00, 0x07 },
859 		{ 0x80, 0x00, 0x02 },
860 		{ 0xcd, 0x00, 0xc0 },
861 		{ 0xce, 0x00, 0x1b },
862 		{ 0x9d, 0x00, 0x01 },
863 		{ 0x9d, 0x00, 0x02 },
864 		{ 0x9e, 0x00, 0x01 },
865 		{ 0x87, 0x00, 0x80 },
866 		{ 0xce, 0x00, 0x08 },
867 		{ 0xce, 0x00, 0x10 },
868 	};
869 	struct tda10071_reg_val_mask tab2[] = {
870 		{ 0xf1, 0x70, 0xff },
871 		{ 0x88, priv->cfg.pll_multiplier, 0x3f },
872 		{ 0x89, 0x00, 0x10 },
873 		{ 0x89, 0x10, 0x10 },
874 		{ 0xc0, 0x01, 0x01 },
875 		{ 0xc0, 0x00, 0x01 },
876 		{ 0xe0, 0xff, 0xff },
877 		{ 0xe0, 0x00, 0xff },
878 		{ 0x96, 0x1e, 0x7e },
879 		{ 0x8b, 0x08, 0x08 },
880 		{ 0x8b, 0x00, 0x08 },
881 		{ 0x8f, 0x1a, 0x7e },
882 		{ 0x8c, 0x68, 0xff },
883 		{ 0x8d, 0x08, 0xff },
884 		{ 0x8e, 0x4c, 0xff },
885 		{ 0x8f, 0x01, 0x01 },
886 		{ 0x8b, 0x04, 0x04 },
887 		{ 0x8b, 0x00, 0x04 },
888 		{ 0x87, 0x05, 0x07 },
889 		{ 0x80, 0x00, 0x20 },
890 		{ 0xc8, 0x01, 0xff },
891 		{ 0xb4, 0x47, 0xff },
892 		{ 0xb5, 0x9c, 0xff },
893 		{ 0xb6, 0x7d, 0xff },
894 		{ 0xba, 0x00, 0x03 },
895 		{ 0xb7, 0x47, 0xff },
896 		{ 0xb8, 0x9c, 0xff },
897 		{ 0xb9, 0x7d, 0xff },
898 		{ 0xba, 0x00, 0x0c },
899 		{ 0xc8, 0x00, 0xff },
900 		{ 0xcd, 0x00, 0x04 },
901 		{ 0xcd, 0x00, 0x20 },
902 		{ 0xe8, 0x02, 0xff },
903 		{ 0xcf, 0x20, 0xff },
904 		{ 0x9b, 0xd7, 0xff },
905 		{ 0x9a, 0x01, 0x03 },
906 		{ 0xa8, 0x05, 0x0f },
907 		{ 0xa8, 0x65, 0xf0 },
908 		{ 0xa6, 0xa0, 0xf0 },
909 		{ 0x9d, 0x50, 0xfc },
910 		{ 0x9e, 0x20, 0xe0 },
911 		{ 0xa3, 0x1c, 0x7c },
912 		{ 0xd5, 0x03, 0x03 },
913 	};
914 
915 	/* firmware status */
916 	ret = tda10071_rd_reg(priv, 0x51, &tmp);
917 	if (ret)
918 		goto error;
919 
920 	if (!tmp) {
921 		/* warm state - wake up device from sleep */
922 		priv->warm = 1;
923 
924 		for (i = 0; i < ARRAY_SIZE(tab); i++) {
925 			ret = tda10071_wr_reg_mask(priv, tab[i].reg,
926 				tab[i].val, tab[i].mask);
927 			if (ret)
928 				goto error;
929 		}
930 
931 		cmd.args[0] = CMD_SET_SLEEP_MODE;
932 		cmd.args[1] = 0;
933 		cmd.args[2] = 0;
934 		cmd.len = 3;
935 		ret = tda10071_cmd_execute(priv, &cmd);
936 		if (ret)
937 			goto error;
938 	} else {
939 		/* cold state - try to download firmware */
940 		priv->warm = 0;
941 
942 		/* request the firmware, this will block and timeout */
943 		ret = request_firmware(&fw, fw_file, priv->i2c->dev.parent);
944 		if (ret) {
945 			dev_err(&priv->i2c->dev, "%s: did not find the " \
946 					"firmware file. (%s) Please see " \
947 					"linux/Documentation/dvb/ for more " \
948 					"details on firmware-problems. (%d)\n",
949 					KBUILD_MODNAME, fw_file, ret);
950 			goto error;
951 		}
952 
953 		/* init */
954 		for (i = 0; i < ARRAY_SIZE(tab2); i++) {
955 			ret = tda10071_wr_reg_mask(priv, tab2[i].reg,
956 				tab2[i].val, tab2[i].mask);
957 			if (ret)
958 				goto error_release_firmware;
959 		}
960 
961 		/*  download firmware */
962 		ret = tda10071_wr_reg(priv, 0xe0, 0x7f);
963 		if (ret)
964 			goto error_release_firmware;
965 
966 		ret = tda10071_wr_reg(priv, 0xf7, 0x81);
967 		if (ret)
968 			goto error_release_firmware;
969 
970 		ret = tda10071_wr_reg(priv, 0xf8, 0x00);
971 		if (ret)
972 			goto error_release_firmware;
973 
974 		ret = tda10071_wr_reg(priv, 0xf9, 0x00);
975 		if (ret)
976 			goto error_release_firmware;
977 
978 		dev_info(&priv->i2c->dev, "%s: found a '%s' in cold state, " \
979 				"will try to load a firmware\n", KBUILD_MODNAME,
980 				tda10071_ops.info.name);
981 		dev_info(&priv->i2c->dev, "%s: downloading firmware from " \
982 				"file '%s'\n", KBUILD_MODNAME, fw_file);
983 
984 		/* do not download last byte */
985 		fw_size = fw->size - 1;
986 
987 		for (remaining = fw_size; remaining > 0;
988 			remaining -= (priv->cfg.i2c_wr_max - 1)) {
989 			len = remaining;
990 			if (len > (priv->cfg.i2c_wr_max - 1))
991 				len = (priv->cfg.i2c_wr_max - 1);
992 
993 			ret = tda10071_wr_regs(priv, 0xfa,
994 				(u8 *) &fw->data[fw_size - remaining], len);
995 			if (ret) {
996 				dev_err(&priv->i2c->dev, "%s: firmware " \
997 						"download failed=%d\n",
998 						KBUILD_MODNAME, ret);
999 				if (ret)
1000 					goto error_release_firmware;
1001 			}
1002 		}
1003 		release_firmware(fw);
1004 
1005 		ret = tda10071_wr_reg(priv, 0xf7, 0x0c);
1006 		if (ret)
1007 			goto error;
1008 
1009 		ret = tda10071_wr_reg(priv, 0xe0, 0x00);
1010 		if (ret)
1011 			goto error;
1012 
1013 		/* wait firmware start */
1014 		msleep(250);
1015 
1016 		/* firmware status */
1017 		ret = tda10071_rd_reg(priv, 0x51, &tmp);
1018 		if (ret)
1019 			goto error;
1020 
1021 		if (tmp) {
1022 			dev_info(&priv->i2c->dev, "%s: firmware did not run\n",
1023 					KBUILD_MODNAME);
1024 			ret = -EFAULT;
1025 			goto error;
1026 		} else {
1027 			priv->warm = 1;
1028 		}
1029 
1030 		cmd.args[0] = CMD_GET_FW_VERSION;
1031 		cmd.len = 1;
1032 		ret = tda10071_cmd_execute(priv, &cmd);
1033 		if (ret)
1034 			goto error;
1035 
1036 		ret = tda10071_rd_regs(priv, cmd.len, buf, 4);
1037 		if (ret)
1038 			goto error;
1039 
1040 		dev_info(&priv->i2c->dev, "%s: firmware version %d.%d.%d.%d\n",
1041 				KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3]);
1042 		dev_info(&priv->i2c->dev, "%s: found a '%s' in warm state\n",
1043 				KBUILD_MODNAME, tda10071_ops.info.name);
1044 
1045 		ret = tda10071_rd_regs(priv, 0x81, buf, 2);
1046 		if (ret)
1047 			goto error;
1048 
1049 		cmd.args[0] = CMD_DEMOD_INIT;
1050 		cmd.args[1] = ((priv->cfg.xtal / 1000) >> 8) & 0xff;
1051 		cmd.args[2] = ((priv->cfg.xtal / 1000) >> 0) & 0xff;
1052 		cmd.args[3] = buf[0];
1053 		cmd.args[4] = buf[1];
1054 		cmd.args[5] = priv->cfg.pll_multiplier;
1055 		cmd.args[6] = priv->cfg.spec_inv;
1056 		cmd.args[7] = 0x00;
1057 		cmd.len = 8;
1058 		ret = tda10071_cmd_execute(priv, &cmd);
1059 		if (ret)
1060 			goto error;
1061 
1062 		cmd.args[0] = CMD_TUNER_INIT;
1063 		cmd.args[1] = 0x00;
1064 		cmd.args[2] = 0x00;
1065 		cmd.args[3] = 0x00;
1066 		cmd.args[4] = 0x00;
1067 		cmd.args[5] = (priv->cfg.tuner_i2c_addr) ? priv->cfg.tuner_i2c_addr : 0x14;
1068 		cmd.args[6] = 0x00;
1069 		cmd.args[7] = 0x03;
1070 		cmd.args[8] = 0x02;
1071 		cmd.args[9] = 0x02;
1072 		cmd.args[10] = 0x00;
1073 		cmd.args[11] = 0x00;
1074 		cmd.args[12] = 0x00;
1075 		cmd.args[13] = 0x00;
1076 		cmd.args[14] = 0x00;
1077 		cmd.len = 15;
1078 		ret = tda10071_cmd_execute(priv, &cmd);
1079 		if (ret)
1080 			goto error;
1081 
1082 		cmd.args[0] = CMD_MPEG_CONFIG;
1083 		cmd.args[1] = 0;
1084 		cmd.args[2] = priv->cfg.ts_mode;
1085 		cmd.args[3] = 0x00;
1086 		cmd.args[4] = 0x04;
1087 		cmd.args[5] = 0x00;
1088 		cmd.len = 6;
1089 		ret = tda10071_cmd_execute(priv, &cmd);
1090 		if (ret)
1091 			goto error;
1092 
1093 		ret = tda10071_wr_reg_mask(priv, 0xf0, 0x01, 0x01);
1094 		if (ret)
1095 			goto error;
1096 
1097 		cmd.args[0] = CMD_LNB_CONFIG;
1098 		cmd.args[1] = 0;
1099 		cmd.args[2] = 150;
1100 		cmd.args[3] = 3;
1101 		cmd.args[4] = 22;
1102 		cmd.args[5] = 1;
1103 		cmd.args[6] = 1;
1104 		cmd.args[7] = 30;
1105 		cmd.args[8] = 30;
1106 		cmd.args[9] = 30;
1107 		cmd.args[10] = 30;
1108 		cmd.len = 11;
1109 		ret = tda10071_cmd_execute(priv, &cmd);
1110 		if (ret)
1111 			goto error;
1112 
1113 		cmd.args[0] = CMD_BER_CONTROL;
1114 		cmd.args[1] = 0;
1115 		cmd.args[2] = 14;
1116 		cmd.args[3] = 14;
1117 		cmd.len = 4;
1118 		ret = tda10071_cmd_execute(priv, &cmd);
1119 		if (ret)
1120 			goto error;
1121 	}
1122 
1123 	return ret;
1124 error_release_firmware:
1125 	release_firmware(fw);
1126 error:
1127 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1128 	return ret;
1129 }
1130 
1131 static int tda10071_sleep(struct dvb_frontend *fe)
1132 {
1133 	struct tda10071_priv *priv = fe->demodulator_priv;
1134 	struct tda10071_cmd cmd;
1135 	int ret, i;
1136 	struct tda10071_reg_val_mask tab[] = {
1137 		{ 0xcd, 0x07, 0x07 },
1138 		{ 0x80, 0x02, 0x02 },
1139 		{ 0xcd, 0xc0, 0xc0 },
1140 		{ 0xce, 0x1b, 0x1b },
1141 		{ 0x9d, 0x01, 0x01 },
1142 		{ 0x9d, 0x02, 0x02 },
1143 		{ 0x9e, 0x01, 0x01 },
1144 		{ 0x87, 0x80, 0x80 },
1145 		{ 0xce, 0x08, 0x08 },
1146 		{ 0xce, 0x10, 0x10 },
1147 	};
1148 
1149 	if (!priv->warm) {
1150 		ret = -EFAULT;
1151 		goto error;
1152 	}
1153 
1154 	cmd.args[0] = CMD_SET_SLEEP_MODE;
1155 	cmd.args[1] = 0;
1156 	cmd.args[2] = 1;
1157 	cmd.len = 3;
1158 	ret = tda10071_cmd_execute(priv, &cmd);
1159 	if (ret)
1160 		goto error;
1161 
1162 	for (i = 0; i < ARRAY_SIZE(tab); i++) {
1163 		ret = tda10071_wr_reg_mask(priv, tab[i].reg, tab[i].val,
1164 			tab[i].mask);
1165 		if (ret)
1166 			goto error;
1167 	}
1168 
1169 	return ret;
1170 error:
1171 	dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
1172 	return ret;
1173 }
1174 
1175 static int tda10071_get_tune_settings(struct dvb_frontend *fe,
1176 	struct dvb_frontend_tune_settings *s)
1177 {
1178 	s->min_delay_ms = 8000;
1179 	s->step_size = 0;
1180 	s->max_drift = 0;
1181 
1182 	return 0;
1183 }
1184 
1185 static void tda10071_release(struct dvb_frontend *fe)
1186 {
1187 	struct tda10071_priv *priv = fe->demodulator_priv;
1188 	kfree(priv);
1189 }
1190 
1191 struct dvb_frontend *tda10071_attach(const struct tda10071_config *config,
1192 	struct i2c_adapter *i2c)
1193 {
1194 	int ret;
1195 	struct tda10071_priv *priv = NULL;
1196 	u8 tmp;
1197 
1198 	/* allocate memory for the internal priv */
1199 	priv = kzalloc(sizeof(struct tda10071_priv), GFP_KERNEL);
1200 	if (priv == NULL) {
1201 		ret = -ENOMEM;
1202 		goto error;
1203 	}
1204 
1205 	/* make sure demod i2c address is specified */
1206 	if (!config->demod_i2c_addr) {
1207 		dev_dbg(&i2c->dev, "%s: invalid demod i2c address!\n", __func__);
1208 		ret = -EINVAL;
1209 		goto error;
1210 	}
1211 
1212 	/* make sure tuner i2c address is specified */
1213 	if (!config->tuner_i2c_addr) {
1214 		dev_dbg(&i2c->dev, "%s: invalid tuner i2c address!\n", __func__);
1215 		ret = -EINVAL;
1216 		goto error;
1217 	}
1218 
1219 	/* setup the priv */
1220 	priv->i2c = i2c;
1221 	memcpy(&priv->cfg, config, sizeof(struct tda10071_config));
1222 
1223 	/* chip ID */
1224 	ret = tda10071_rd_reg(priv, 0xff, &tmp);
1225 	if (ret || tmp != 0x0f)
1226 		goto error;
1227 
1228 	/* chip type */
1229 	ret = tda10071_rd_reg(priv, 0xdd, &tmp);
1230 	if (ret || tmp != 0x00)
1231 		goto error;
1232 
1233 	/* chip version */
1234 	ret = tda10071_rd_reg(priv, 0xfe, &tmp);
1235 	if (ret || tmp != 0x01)
1236 		goto error;
1237 
1238 	/* create dvb_frontend */
1239 	memcpy(&priv->fe.ops, &tda10071_ops, sizeof(struct dvb_frontend_ops));
1240 	priv->fe.demodulator_priv = priv;
1241 
1242 	return &priv->fe;
1243 error:
1244 	dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
1245 	kfree(priv);
1246 	return NULL;
1247 }
1248 EXPORT_SYMBOL(tda10071_attach);
1249 
1250 static struct dvb_frontend_ops tda10071_ops = {
1251 	.delsys = { SYS_DVBS, SYS_DVBS2 },
1252 	.info = {
1253 		.name = "NXP TDA10071",
1254 		.frequency_min = 950000,
1255 		.frequency_max = 2150000,
1256 		.frequency_tolerance = 5000,
1257 		.symbol_rate_min = 1000000,
1258 		.symbol_rate_max = 45000000,
1259 		.caps = FE_CAN_INVERSION_AUTO |
1260 			FE_CAN_FEC_1_2 |
1261 			FE_CAN_FEC_2_3 |
1262 			FE_CAN_FEC_3_4 |
1263 			FE_CAN_FEC_4_5 |
1264 			FE_CAN_FEC_5_6 |
1265 			FE_CAN_FEC_6_7 |
1266 			FE_CAN_FEC_7_8 |
1267 			FE_CAN_FEC_8_9 |
1268 			FE_CAN_FEC_AUTO |
1269 			FE_CAN_QPSK |
1270 			FE_CAN_RECOVER |
1271 			FE_CAN_2G_MODULATION
1272 	},
1273 
1274 	.release = tda10071_release,
1275 
1276 	.get_tune_settings = tda10071_get_tune_settings,
1277 
1278 	.init = tda10071_init,
1279 	.sleep = tda10071_sleep,
1280 
1281 	.set_frontend = tda10071_set_frontend,
1282 	.get_frontend = tda10071_get_frontend,
1283 
1284 	.read_status = tda10071_read_status,
1285 	.read_snr = tda10071_read_snr,
1286 	.read_signal_strength = tda10071_read_signal_strength,
1287 	.read_ber = tda10071_read_ber,
1288 	.read_ucblocks = tda10071_read_ucblocks,
1289 
1290 	.diseqc_send_master_cmd = tda10071_diseqc_send_master_cmd,
1291 	.diseqc_recv_slave_reply = tda10071_diseqc_recv_slave_reply,
1292 	.diseqc_send_burst = tda10071_diseqc_send_burst,
1293 
1294 	.set_tone = tda10071_set_tone,
1295 	.set_voltage = tda10071_set_voltage,
1296 };
1297 
1298 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1299 MODULE_DESCRIPTION("NXP TDA10071 DVB-S/S2 demodulator driver");
1300 MODULE_LICENSE("GPL");
1301 MODULE_FIRMWARE(TDA10071_FIRMWARE);
1302