xref: /linux/drivers/media/dvb-frontends/lg2160.c (revision c0c914eca7f251c70facc37dfebeaf176601918d)
1 /*
2  *    Support for LG2160 - ATSC/MH
3  *
4  *    Copyright (C) 2010 Michael Krufky <mkrufky@linuxtv.org>
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
17  *    along with this program; if not, write to the Free Software
18  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21 
22 #include <linux/jiffies.h>
23 #include <linux/dvb/frontend.h>
24 #include "lg2160.h"
25 
26 static int debug;
27 module_param(debug, int, 0644);
28 MODULE_PARM_DESC(debug, "set debug level (info=1, reg=2 (or-able))");
29 
30 #define DBG_INFO 1
31 #define DBG_REG  2
32 
33 #define lg_printk(kern, fmt, arg...)					\
34 	printk(kern "%s: " fmt, __func__, ##arg)
35 
36 #define lg_info(fmt, arg...)	printk(KERN_INFO "lg2160: " fmt, ##arg)
37 #define lg_warn(fmt, arg...)	lg_printk(KERN_WARNING,       fmt, ##arg)
38 #define lg_err(fmt, arg...)	lg_printk(KERN_ERR,           fmt, ##arg)
39 #define lg_dbg(fmt, arg...) if (debug & DBG_INFO)			\
40 				lg_printk(KERN_DEBUG,         fmt, ##arg)
41 #define lg_reg(fmt, arg...) if (debug & DBG_REG)			\
42 				lg_printk(KERN_DEBUG,         fmt, ##arg)
43 
44 #define lg_fail(ret)							\
45 ({									\
46 	int __ret;							\
47 	__ret = (ret < 0);						\
48 	if (__ret)							\
49 		lg_err("error %d on line %d\n",	ret, __LINE__);		\
50 	__ret;								\
51 })
52 
53 struct lg216x_state {
54 	struct i2c_adapter *i2c_adap;
55 	const struct lg2160_config *cfg;
56 
57 	struct dvb_frontend frontend;
58 
59 	u32 current_frequency;
60 	u8 parade_id;
61 	u8 fic_ver;
62 	unsigned int last_reset;
63 };
64 
65 /* ------------------------------------------------------------------------ */
66 
67 static int lg216x_write_reg(struct lg216x_state *state, u16 reg, u8 val)
68 {
69 	int ret;
70 	u8 buf[] = { reg >> 8, reg & 0xff, val };
71 	struct i2c_msg msg = {
72 		.addr = state->cfg->i2c_addr, .flags = 0,
73 		.buf = buf, .len = 3,
74 	};
75 
76 	lg_reg("reg: 0x%04x, val: 0x%02x\n", reg, val);
77 
78 	ret = i2c_transfer(state->i2c_adap, &msg, 1);
79 
80 	if (ret != 1) {
81 		lg_err("error (addr %02x %02x <- %02x, err = %i)\n",
82 		       msg.buf[0], msg.buf[1], msg.buf[2], ret);
83 		if (ret < 0)
84 			return ret;
85 		else
86 			return -EREMOTEIO;
87 	}
88 	return 0;
89 }
90 
91 static int lg216x_read_reg(struct lg216x_state *state, u16 reg, u8 *val)
92 {
93 	int ret;
94 	u8 reg_buf[] = { reg >> 8, reg & 0xff };
95 	struct i2c_msg msg[] = {
96 		{ .addr = state->cfg->i2c_addr,
97 		  .flags = 0, .buf = reg_buf, .len = 2 },
98 		{ .addr = state->cfg->i2c_addr,
99 		  .flags = I2C_M_RD, .buf = val, .len = 1 },
100 	};
101 
102 	lg_reg("reg: 0x%04x\n", reg);
103 
104 	ret = i2c_transfer(state->i2c_adap, msg, 2);
105 
106 	if (ret != 2) {
107 		lg_err("error (addr %02x reg %04x error (ret == %i)\n",
108 		       state->cfg->i2c_addr, reg, ret);
109 		if (ret < 0)
110 			return ret;
111 		else
112 			return -EREMOTEIO;
113 	}
114 	return 0;
115 }
116 
117 struct lg216x_reg {
118 	u16 reg;
119 	u8 val;
120 };
121 
122 static int lg216x_write_regs(struct lg216x_state *state,
123 			     struct lg216x_reg *regs, int len)
124 {
125 	int i, ret;
126 
127 	lg_reg("writing %d registers...\n", len);
128 
129 	for (i = 0; i < len; i++) {
130 		ret = lg216x_write_reg(state, regs[i].reg, regs[i].val);
131 		if (lg_fail(ret))
132 			return ret;
133 	}
134 	return 0;
135 }
136 
137 static int lg216x_set_reg_bit(struct lg216x_state *state,
138 			      u16 reg, int bit, int onoff)
139 {
140 	u8 val;
141 	int ret;
142 
143 	lg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg, bit, onoff);
144 
145 	ret = lg216x_read_reg(state, reg, &val);
146 	if (lg_fail(ret))
147 		goto fail;
148 
149 	val &= ~(1 << bit);
150 	val |= (onoff & 1) << bit;
151 
152 	ret = lg216x_write_reg(state, reg, val);
153 	lg_fail(ret);
154 fail:
155 	return ret;
156 }
157 
158 /* ------------------------------------------------------------------------ */
159 
160 static int lg216x_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
161 {
162 	struct lg216x_state *state = fe->demodulator_priv;
163 	int ret;
164 
165 	if (state->cfg->deny_i2c_rptr)
166 		return 0;
167 
168 	lg_dbg("(%d)\n", enable);
169 
170 	ret = lg216x_set_reg_bit(state, 0x0000, 0, enable ? 0 : 1);
171 
172 	msleep(1);
173 
174 	return ret;
175 }
176 
177 static int lg216x_soft_reset(struct lg216x_state *state)
178 {
179 	int ret;
180 
181 	lg_dbg("\n");
182 
183 	ret = lg216x_write_reg(state, 0x0002, 0x00);
184 	if (lg_fail(ret))
185 		goto fail;
186 
187 	msleep(20);
188 	ret = lg216x_write_reg(state, 0x0002, 0x01);
189 	if (lg_fail(ret))
190 		goto fail;
191 
192 	state->last_reset = jiffies_to_msecs(jiffies);
193 fail:
194 	return ret;
195 }
196 
197 static int lg216x_initialize(struct lg216x_state *state)
198 {
199 	int ret;
200 
201 	static struct lg216x_reg lg2160_init[] = {
202 #if 0
203 		{ .reg = 0x0015, .val = 0xe6 },
204 #else
205 		{ .reg = 0x0015, .val = 0xf7 },
206 		{ .reg = 0x001b, .val = 0x52 },
207 		{ .reg = 0x0208, .val = 0x00 },
208 		{ .reg = 0x0209, .val = 0x82 },
209 		{ .reg = 0x0210, .val = 0xf9 },
210 		{ .reg = 0x020a, .val = 0x00 },
211 		{ .reg = 0x020b, .val = 0x82 },
212 		{ .reg = 0x020d, .val = 0x28 },
213 		{ .reg = 0x020f, .val = 0x14 },
214 #endif
215 	};
216 
217 	static struct lg216x_reg lg2161_init[] = {
218 		{ .reg = 0x0000, .val = 0x41 },
219 		{ .reg = 0x0001, .val = 0xfb },
220 		{ .reg = 0x0216, .val = 0x00 },
221 		{ .reg = 0x0219, .val = 0x00 },
222 		{ .reg = 0x021b, .val = 0x55 },
223 		{ .reg = 0x0606, .val = 0x0a },
224 	};
225 
226 	switch (state->cfg->lg_chip) {
227 	case LG2160:
228 		ret = lg216x_write_regs(state,
229 					lg2160_init, ARRAY_SIZE(lg2160_init));
230 		break;
231 	case LG2161:
232 		ret = lg216x_write_regs(state,
233 					lg2161_init, ARRAY_SIZE(lg2161_init));
234 		break;
235 	default:
236 		ret = -EINVAL;
237 		break;
238 	}
239 	if (lg_fail(ret))
240 		goto fail;
241 
242 	ret = lg216x_soft_reset(state);
243 	lg_fail(ret);
244 fail:
245 	return ret;
246 }
247 
248 /* ------------------------------------------------------------------------ */
249 
250 static int lg216x_set_if(struct lg216x_state *state)
251 {
252 	u8 val;
253 	int ret;
254 
255 	lg_dbg("%d KHz\n", state->cfg->if_khz);
256 
257 	ret = lg216x_read_reg(state, 0x0132, &val);
258 	if (lg_fail(ret))
259 		goto fail;
260 
261 	val &= 0xfb;
262 	val |= (0 == state->cfg->if_khz) ? 0x04 : 0x00;
263 
264 	ret = lg216x_write_reg(state, 0x0132, val);
265 	lg_fail(ret);
266 
267 	/* if NOT zero IF, 6 MHz is the default */
268 fail:
269 	return ret;
270 }
271 
272 /* ------------------------------------------------------------------------ */
273 
274 static int lg2160_agc_fix(struct lg216x_state *state,
275 			  int if_agc_fix, int rf_agc_fix)
276 {
277 	u8 val;
278 	int ret;
279 
280 	ret = lg216x_read_reg(state, 0x0100, &val);
281 	if (lg_fail(ret))
282 		goto fail;
283 
284 	val &= 0xf3;
285 	val |= (if_agc_fix) ? 0x08 : 0x00;
286 	val |= (rf_agc_fix) ? 0x04 : 0x00;
287 
288 	ret = lg216x_write_reg(state, 0x0100, val);
289 	lg_fail(ret);
290 fail:
291 	return ret;
292 }
293 
294 #if 0
295 static int lg2160_agc_freeze(struct lg216x_state *state,
296 			     int if_agc_freeze, int rf_agc_freeze)
297 {
298 	u8 val;
299 	int ret;
300 
301 	ret = lg216x_read_reg(state, 0x0100, &val);
302 	if (lg_fail(ret))
303 		goto fail;
304 
305 	val &= 0xcf;
306 	val |= (if_agc_freeze) ? 0x20 : 0x00;
307 	val |= (rf_agc_freeze) ? 0x10 : 0x00;
308 
309 	ret = lg216x_write_reg(state, 0x0100, val);
310 	lg_fail(ret);
311 fail:
312 	return ret;
313 }
314 #endif
315 
316 static int lg2160_agc_polarity(struct lg216x_state *state,
317 			       int if_agc_polarity, int rf_agc_polarity)
318 {
319 	u8 val;
320 	int ret;
321 
322 	ret = lg216x_read_reg(state, 0x0100, &val);
323 	if (lg_fail(ret))
324 		goto fail;
325 
326 	val &= 0xfc;
327 	val |= (if_agc_polarity) ? 0x02 : 0x00;
328 	val |= (rf_agc_polarity) ? 0x01 : 0x00;
329 
330 	ret = lg216x_write_reg(state, 0x0100, val);
331 	lg_fail(ret);
332 fail:
333 	return ret;
334 }
335 
336 static int lg2160_tuner_pwr_save_polarity(struct lg216x_state *state,
337 					  int polarity)
338 {
339 	u8 val;
340 	int ret;
341 
342 	ret = lg216x_read_reg(state, 0x0008, &val);
343 	if (lg_fail(ret))
344 		goto fail;
345 
346 	val &= 0xfe;
347 	val |= (polarity) ? 0x01 : 0x00;
348 
349 	ret = lg216x_write_reg(state, 0x0008, val);
350 	lg_fail(ret);
351 fail:
352 	return ret;
353 }
354 
355 static int lg2160_spectrum_polarity(struct lg216x_state *state,
356 				    int inverted)
357 {
358 	u8 val;
359 	int ret;
360 
361 	ret = lg216x_read_reg(state, 0x0132, &val);
362 	if (lg_fail(ret))
363 		goto fail;
364 
365 	val &= 0xfd;
366 	val |= (inverted) ? 0x02 : 0x00;
367 
368 	ret = lg216x_write_reg(state, 0x0132, val);
369 	lg_fail(ret);
370 fail:
371 	return lg216x_soft_reset(state);
372 }
373 
374 static int lg2160_tuner_pwr_save(struct lg216x_state *state, int onoff)
375 {
376 	u8 val;
377 	int ret;
378 
379 	ret = lg216x_read_reg(state, 0x0007, &val);
380 	if (lg_fail(ret))
381 		goto fail;
382 
383 	val &= 0xbf;
384 	val |= (onoff) ? 0x40 : 0x00;
385 
386 	ret = lg216x_write_reg(state, 0x0007, val);
387 	lg_fail(ret);
388 fail:
389 	return ret;
390 }
391 
392 static int lg216x_set_parade(struct lg216x_state *state, int id)
393 {
394 	int ret;
395 
396 	ret = lg216x_write_reg(state, 0x013e, id & 0x7f);
397 	if (lg_fail(ret))
398 		goto fail;
399 
400 	state->parade_id = id & 0x7f;
401 fail:
402 	return ret;
403 }
404 
405 static int lg216x_set_ensemble(struct lg216x_state *state, int id)
406 {
407 	int ret;
408 	u16 reg;
409 	u8 val;
410 
411 	switch (state->cfg->lg_chip) {
412 	case LG2160:
413 		reg = 0x0400;
414 		break;
415 	case LG2161:
416 	default:
417 		reg = 0x0500;
418 		break;
419 	}
420 
421 	ret = lg216x_read_reg(state, reg, &val);
422 	if (lg_fail(ret))
423 		goto fail;
424 
425 	val &= 0xfe;
426 	val |= (id) ? 0x01 : 0x00;
427 
428 	ret = lg216x_write_reg(state, reg, val);
429 	lg_fail(ret);
430 fail:
431 	return ret;
432 }
433 
434 static int lg2160_set_spi_clock(struct lg216x_state *state)
435 {
436 	u8 val;
437 	int ret;
438 
439 	ret = lg216x_read_reg(state, 0x0014, &val);
440 	if (lg_fail(ret))
441 		goto fail;
442 
443 	val &= 0xf3;
444 	val |= (state->cfg->spi_clock << 2);
445 
446 	ret = lg216x_write_reg(state, 0x0014, val);
447 	lg_fail(ret);
448 fail:
449 	return ret;
450 }
451 
452 static int lg2161_set_output_interface(struct lg216x_state *state)
453 {
454 	u8 val;
455 	int ret;
456 
457 	ret = lg216x_read_reg(state, 0x0014, &val);
458 	if (lg_fail(ret))
459 		goto fail;
460 
461 	val &= ~0x07;
462 	val |= state->cfg->output_if; /* FIXME: needs sanity check */
463 
464 	ret = lg216x_write_reg(state, 0x0014, val);
465 	lg_fail(ret);
466 fail:
467 	return ret;
468 }
469 
470 static int lg216x_enable_fic(struct lg216x_state *state, int onoff)
471 {
472 	int ret;
473 
474 	ret = lg216x_write_reg(state, 0x0017, 0x23);
475 	if (lg_fail(ret))
476 		goto fail;
477 
478 	ret = lg216x_write_reg(state, 0x0016, 0xfc);
479 	if (lg_fail(ret))
480 		goto fail;
481 
482 	switch (state->cfg->lg_chip) {
483 	case LG2160:
484 		ret = lg216x_write_reg(state, 0x0016,
485 				       0xfc | ((onoff) ? 0x02 : 0x00));
486 		break;
487 	case LG2161:
488 		ret = lg216x_write_reg(state, 0x0016, (onoff) ? 0x10 : 0x00);
489 		break;
490 	}
491 	if (lg_fail(ret))
492 		goto fail;
493 
494 	ret = lg216x_initialize(state);
495 	if (lg_fail(ret))
496 		goto fail;
497 
498 	if (onoff) {
499 		ret = lg216x_write_reg(state, 0x0017, 0x03);
500 		lg_fail(ret);
501 	}
502 fail:
503 	return ret;
504 }
505 
506 /* ------------------------------------------------------------------------ */
507 
508 static int lg216x_get_fic_version(struct lg216x_state *state, u8 *ficver)
509 {
510 	u8 val;
511 	int ret;
512 
513 	*ficver = 0xff; /* invalid value */
514 
515 	ret = lg216x_read_reg(state, 0x0128, &val);
516 	if (lg_fail(ret))
517 		goto fail;
518 
519 	*ficver = (val >> 3) & 0x1f;
520 fail:
521 	return ret;
522 }
523 
524 #if 0
525 static int lg2160_get_parade_id(struct lg216x_state *state, u8 *id)
526 {
527 	u8 val;
528 	int ret;
529 
530 	*id = 0xff; /* invalid value */
531 
532 	ret = lg216x_read_reg(state, 0x0123, &val);
533 	if (lg_fail(ret))
534 		goto fail;
535 
536 	*id = val & 0x7f;
537 fail:
538 	return ret;
539 }
540 #endif
541 
542 static int lg216x_get_nog(struct lg216x_state *state, u8 *nog)
543 {
544 	u8 val;
545 	int ret;
546 
547 	*nog = 0xff; /* invalid value */
548 
549 	ret = lg216x_read_reg(state, 0x0124, &val);
550 	if (lg_fail(ret))
551 		goto fail;
552 
553 	*nog = ((val >> 4) & 0x07) + 1;
554 fail:
555 	return ret;
556 }
557 
558 static int lg216x_get_tnog(struct lg216x_state *state, u8 *tnog)
559 {
560 	u8 val;
561 	int ret;
562 
563 	*tnog = 0xff; /* invalid value */
564 
565 	ret = lg216x_read_reg(state, 0x0125, &val);
566 	if (lg_fail(ret))
567 		goto fail;
568 
569 	*tnog = val & 0x1f;
570 fail:
571 	return ret;
572 }
573 
574 static int lg216x_get_sgn(struct lg216x_state *state, u8 *sgn)
575 {
576 	u8 val;
577 	int ret;
578 
579 	*sgn = 0xff; /* invalid value */
580 
581 	ret = lg216x_read_reg(state, 0x0124, &val);
582 	if (lg_fail(ret))
583 		goto fail;
584 
585 	*sgn = val & 0x0f;
586 fail:
587 	return ret;
588 }
589 
590 static int lg216x_get_prc(struct lg216x_state *state, u8 *prc)
591 {
592 	u8 val;
593 	int ret;
594 
595 	*prc = 0xff; /* invalid value */
596 
597 	ret = lg216x_read_reg(state, 0x0125, &val);
598 	if (lg_fail(ret))
599 		goto fail;
600 
601 	*prc = ((val >> 5) & 0x07) + 1;
602 fail:
603 	return ret;
604 }
605 
606 /* ------------------------------------------------------------------------ */
607 
608 static int lg216x_get_rs_frame_mode(struct lg216x_state *state,
609 				    enum atscmh_rs_frame_mode *rs_framemode)
610 {
611 	u8 val;
612 	int ret;
613 
614 	switch (state->cfg->lg_chip) {
615 	case LG2160:
616 		ret = lg216x_read_reg(state, 0x0410, &val);
617 		break;
618 	case LG2161:
619 		ret = lg216x_read_reg(state, 0x0513, &val);
620 		break;
621 	default:
622 		ret = -EINVAL;
623 	}
624 	if (lg_fail(ret))
625 		goto fail;
626 
627 	switch ((val >> 4) & 0x03) {
628 #if 1
629 	default:
630 #endif
631 	case 0x00:
632 		*rs_framemode = ATSCMH_RSFRAME_PRI_ONLY;
633 		break;
634 	case 0x01:
635 		*rs_framemode = ATSCMH_RSFRAME_PRI_SEC;
636 		break;
637 #if 0
638 	default:
639 		*rs_framemode = ATSCMH_RSFRAME_RES;
640 		break;
641 #endif
642 	}
643 fail:
644 	return ret;
645 }
646 
647 static
648 int lg216x_get_rs_frame_ensemble(struct lg216x_state *state,
649 				 enum atscmh_rs_frame_ensemble *rs_frame_ens)
650 {
651 	u8 val;
652 	int ret;
653 
654 	switch (state->cfg->lg_chip) {
655 	case LG2160:
656 		ret = lg216x_read_reg(state, 0x0400, &val);
657 		break;
658 	case LG2161:
659 		ret = lg216x_read_reg(state, 0x0500, &val);
660 		break;
661 	default:
662 		ret = -EINVAL;
663 	}
664 	if (lg_fail(ret))
665 		goto fail;
666 
667 	val &= 0x01;
668 	*rs_frame_ens = (enum atscmh_rs_frame_ensemble) val;
669 fail:
670 	return ret;
671 }
672 
673 static int lg216x_get_rs_code_mode(struct lg216x_state *state,
674 				   enum atscmh_rs_code_mode *rs_code_pri,
675 				   enum atscmh_rs_code_mode *rs_code_sec)
676 {
677 	u8 val;
678 	int ret;
679 
680 	switch (state->cfg->lg_chip) {
681 	case LG2160:
682 		ret = lg216x_read_reg(state, 0x0410, &val);
683 		break;
684 	case LG2161:
685 		ret = lg216x_read_reg(state, 0x0513, &val);
686 		break;
687 	default:
688 		ret = -EINVAL;
689 	}
690 	if (lg_fail(ret))
691 		goto fail;
692 
693 	*rs_code_pri = (enum atscmh_rs_code_mode) ((val >> 2) & 0x03);
694 	*rs_code_sec = (enum atscmh_rs_code_mode) (val & 0x03);
695 fail:
696 	return ret;
697 }
698 
699 static int lg216x_get_sccc_block_mode(struct lg216x_state *state,
700 				      enum atscmh_sccc_block_mode *sccc_block)
701 {
702 	u8 val;
703 	int ret;
704 
705 	switch (state->cfg->lg_chip) {
706 	case LG2160:
707 		ret = lg216x_read_reg(state, 0x0315, &val);
708 		break;
709 	case LG2161:
710 		ret = lg216x_read_reg(state, 0x0511, &val);
711 		break;
712 	default:
713 		ret = -EINVAL;
714 	}
715 	if (lg_fail(ret))
716 		goto fail;
717 
718 	switch (val & 0x03) {
719 	case 0x00:
720 		*sccc_block = ATSCMH_SCCC_BLK_SEP;
721 		break;
722 	case 0x01:
723 		*sccc_block = ATSCMH_SCCC_BLK_COMB;
724 		break;
725 	default:
726 		*sccc_block = ATSCMH_SCCC_BLK_RES;
727 		break;
728 	}
729 fail:
730 	return ret;
731 }
732 
733 static int lg216x_get_sccc_code_mode(struct lg216x_state *state,
734 				     enum atscmh_sccc_code_mode *mode_a,
735 				     enum atscmh_sccc_code_mode *mode_b,
736 				     enum atscmh_sccc_code_mode *mode_c,
737 				     enum atscmh_sccc_code_mode *mode_d)
738 {
739 	u8 val;
740 	int ret;
741 
742 	switch (state->cfg->lg_chip) {
743 	case LG2160:
744 		ret = lg216x_read_reg(state, 0x0316, &val);
745 		break;
746 	case LG2161:
747 		ret = lg216x_read_reg(state, 0x0512, &val);
748 		break;
749 	default:
750 		ret = -EINVAL;
751 	}
752 	if (lg_fail(ret))
753 		goto fail;
754 
755 	switch ((val >> 6) & 0x03) {
756 	case 0x00:
757 		*mode_a = ATSCMH_SCCC_CODE_HLF;
758 		break;
759 	case 0x01:
760 		*mode_a = ATSCMH_SCCC_CODE_QTR;
761 		break;
762 	default:
763 		*mode_a = ATSCMH_SCCC_CODE_RES;
764 		break;
765 	}
766 
767 	switch ((val >> 4) & 0x03) {
768 	case 0x00:
769 		*mode_b = ATSCMH_SCCC_CODE_HLF;
770 		break;
771 	case 0x01:
772 		*mode_b = ATSCMH_SCCC_CODE_QTR;
773 		break;
774 	default:
775 		*mode_b = ATSCMH_SCCC_CODE_RES;
776 		break;
777 	}
778 
779 	switch ((val >> 2) & 0x03) {
780 	case 0x00:
781 		*mode_c = ATSCMH_SCCC_CODE_HLF;
782 		break;
783 	case 0x01:
784 		*mode_c = ATSCMH_SCCC_CODE_QTR;
785 		break;
786 	default:
787 		*mode_c = ATSCMH_SCCC_CODE_RES;
788 		break;
789 	}
790 
791 	switch (val & 0x03) {
792 	case 0x00:
793 		*mode_d = ATSCMH_SCCC_CODE_HLF;
794 		break;
795 	case 0x01:
796 		*mode_d = ATSCMH_SCCC_CODE_QTR;
797 		break;
798 	default:
799 		*mode_d = ATSCMH_SCCC_CODE_RES;
800 		break;
801 	}
802 fail:
803 	return ret;
804 }
805 
806 /* ------------------------------------------------------------------------ */
807 
808 #if 0
809 static int lg216x_read_fic_err_count(struct lg216x_state *state, u8 *err)
810 {
811 	u8 fic_err;
812 	int ret;
813 
814 	*err = 0;
815 
816 	switch (state->cfg->lg_chip) {
817 	case LG2160:
818 		ret = lg216x_read_reg(state, 0x0012, &fic_err);
819 		break;
820 	case LG2161:
821 		ret = lg216x_read_reg(state, 0x001e, &fic_err);
822 		break;
823 	}
824 	if (lg_fail(ret))
825 		goto fail;
826 
827 	*err = fic_err;
828 fail:
829 	return ret;
830 }
831 
832 static int lg2160_read_crc_err_count(struct lg216x_state *state, u16 *err)
833 {
834 	u8 crc_err1, crc_err2;
835 	int ret;
836 
837 	*err = 0;
838 
839 	ret = lg216x_read_reg(state, 0x0411, &crc_err1);
840 	if (lg_fail(ret))
841 		goto fail;
842 
843 	ret = lg216x_read_reg(state, 0x0412, &crc_err2);
844 	if (lg_fail(ret))
845 		goto fail;
846 
847 	*err = (u16)(((crc_err2 & 0x0f) << 8) | crc_err1);
848 fail:
849 	return ret;
850 }
851 
852 static int lg2161_read_crc_err_count(struct lg216x_state *state, u16 *err)
853 {
854 	u8 crc_err;
855 	int ret;
856 
857 	*err = 0;
858 
859 	ret = lg216x_read_reg(state, 0x0612, &crc_err);
860 	if (lg_fail(ret))
861 		goto fail;
862 
863 	*err = (u16)crc_err;
864 fail:
865 	return ret;
866 }
867 
868 static int lg216x_read_crc_err_count(struct lg216x_state *state, u16 *err)
869 {
870 	int ret;
871 	switch (state->cfg->lg_chip) {
872 	case LG2160:
873 		ret = lg2160_read_crc_err_count(state, err);
874 		break;
875 	case LG2161:
876 		ret = lg2161_read_crc_err_count(state, err);
877 		break;
878 	default:
879 		ret = -EINVAL;
880 		break;
881 	}
882 	return ret;
883 }
884 
885 static int lg2160_read_rs_err_count(struct lg216x_state *state, u16 *err)
886 {
887 	u8 rs_err1, rs_err2;
888 	int ret;
889 
890 	*err = 0;
891 
892 	ret = lg216x_read_reg(state, 0x0413, &rs_err1);
893 	if (lg_fail(ret))
894 		goto fail;
895 
896 	ret = lg216x_read_reg(state, 0x0414, &rs_err2);
897 	if (lg_fail(ret))
898 		goto fail;
899 
900 	*err = (u16)(((rs_err2 & 0x0f) << 8) | rs_err1);
901 fail:
902 	return ret;
903 }
904 
905 static int lg2161_read_rs_err_count(struct lg216x_state *state, u16 *err)
906 {
907 	u8 rs_err1, rs_err2;
908 	int ret;
909 
910 	*err = 0;
911 
912 	ret = lg216x_read_reg(state, 0x0613, &rs_err1);
913 	if (lg_fail(ret))
914 		goto fail;
915 
916 	ret = lg216x_read_reg(state, 0x0614, &rs_err2);
917 	if (lg_fail(ret))
918 		goto fail;
919 
920 	*err = (u16)((rs_err1 << 8) | rs_err2);
921 fail:
922 	return ret;
923 }
924 
925 static int lg216x_read_rs_err_count(struct lg216x_state *state, u16 *err)
926 {
927 	int ret;
928 	switch (state->cfg->lg_chip) {
929 	case LG2160:
930 		ret = lg2160_read_rs_err_count(state, err);
931 		break;
932 	case LG2161:
933 		ret = lg2161_read_rs_err_count(state, err);
934 		break;
935 	default:
936 		ret = -EINVAL;
937 		break;
938 	}
939 	return ret;
940 }
941 #endif
942 
943 /* ------------------------------------------------------------------------ */
944 
945 static int lg216x_get_frontend(struct dvb_frontend *fe,
946 			       struct dtv_frontend_properties *c)
947 {
948 	struct lg216x_state *state = fe->demodulator_priv;
949 	int ret;
950 
951 	lg_dbg("\n");
952 
953 	c->modulation = VSB_8;
954 	c->frequency = state->current_frequency;
955 	c->delivery_system = SYS_ATSCMH;
956 
957 	ret = lg216x_get_fic_version(state,
958 				     &c->atscmh_fic_ver);
959 	if (lg_fail(ret))
960 		goto fail;
961 	if (state->fic_ver != c->atscmh_fic_ver) {
962 		state->fic_ver = c->atscmh_fic_ver;
963 
964 #if 0
965 		ret = lg2160_get_parade_id(state,
966 				&c->atscmh_parade_id);
967 		if (lg_fail(ret))
968 			goto fail;
969 /* #else */
970 		c->atscmh_parade_id = state->parade_id;
971 #endif
972 		ret = lg216x_get_nog(state,
973 				     &c->atscmh_nog);
974 		if (lg_fail(ret))
975 			goto fail;
976 		ret = lg216x_get_tnog(state,
977 				      &c->atscmh_tnog);
978 		if (lg_fail(ret))
979 			goto fail;
980 		ret = lg216x_get_sgn(state,
981 				     &c->atscmh_sgn);
982 		if (lg_fail(ret))
983 			goto fail;
984 		ret = lg216x_get_prc(state,
985 				     &c->atscmh_prc);
986 		if (lg_fail(ret))
987 			goto fail;
988 
989 		ret = lg216x_get_rs_frame_mode(state,
990 			(enum atscmh_rs_frame_mode *)
991 			&c->atscmh_rs_frame_mode);
992 		if (lg_fail(ret))
993 			goto fail;
994 		ret = lg216x_get_rs_frame_ensemble(state,
995 			(enum atscmh_rs_frame_ensemble *)
996 			&c->atscmh_rs_frame_ensemble);
997 		if (lg_fail(ret))
998 			goto fail;
999 		ret = lg216x_get_rs_code_mode(state,
1000 			(enum atscmh_rs_code_mode *)
1001 			&c->atscmh_rs_code_mode_pri,
1002 			(enum atscmh_rs_code_mode *)
1003 			&c->atscmh_rs_code_mode_sec);
1004 		if (lg_fail(ret))
1005 			goto fail;
1006 		ret = lg216x_get_sccc_block_mode(state,
1007 			(enum atscmh_sccc_block_mode *)
1008 			&c->atscmh_sccc_block_mode);
1009 		if (lg_fail(ret))
1010 			goto fail;
1011 		ret = lg216x_get_sccc_code_mode(state,
1012 			(enum atscmh_sccc_code_mode *)
1013 			&c->atscmh_sccc_code_mode_a,
1014 			(enum atscmh_sccc_code_mode *)
1015 			&c->atscmh_sccc_code_mode_b,
1016 			(enum atscmh_sccc_code_mode *)
1017 			&c->atscmh_sccc_code_mode_c,
1018 			(enum atscmh_sccc_code_mode *)
1019 			&c->atscmh_sccc_code_mode_d);
1020 		if (lg_fail(ret))
1021 			goto fail;
1022 	}
1023 #if 0
1024 	ret = lg216x_read_fic_err_count(state,
1025 				(u8 *)&c->atscmh_fic_err);
1026 	if (lg_fail(ret))
1027 		goto fail;
1028 	ret = lg216x_read_crc_err_count(state,
1029 				&c->atscmh_crc_err);
1030 	if (lg_fail(ret))
1031 		goto fail;
1032 	ret = lg216x_read_rs_err_count(state,
1033 				&c->atscmh_rs_err);
1034 	if (lg_fail(ret))
1035 		goto fail;
1036 
1037 	switch (state->cfg->lg_chip) {
1038 	case LG2160:
1039 		if (((c->atscmh_rs_err >= 240) &&
1040 		     (c->atscmh_crc_err >= 240)) &&
1041 		    ((jiffies_to_msecs(jiffies) - state->last_reset) > 6000))
1042 			ret = lg216x_soft_reset(state);
1043 		break;
1044 	case LG2161:
1045 		/* no fix needed here (as far as we know) */
1046 		ret = 0;
1047 		break;
1048 	}
1049 	lg_fail(ret);
1050 #endif
1051 fail:
1052 	return ret;
1053 }
1054 
1055 static int lg216x_get_property(struct dvb_frontend *fe,
1056 			       struct dtv_property *tvp)
1057 {
1058 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1059 
1060 	return (DTV_ATSCMH_FIC_VER == tvp->cmd) ?
1061 		lg216x_get_frontend(fe, c) : 0;
1062 }
1063 
1064 
1065 static int lg2160_set_frontend(struct dvb_frontend *fe)
1066 {
1067 	struct lg216x_state *state = fe->demodulator_priv;
1068 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1069 	int ret;
1070 
1071 	lg_dbg("(%d)\n", fe->dtv_property_cache.frequency);
1072 
1073 	if (fe->ops.tuner_ops.set_params) {
1074 		ret = fe->ops.tuner_ops.set_params(fe);
1075 		if (fe->ops.i2c_gate_ctrl)
1076 			fe->ops.i2c_gate_ctrl(fe, 0);
1077 		if (lg_fail(ret))
1078 			goto fail;
1079 		state->current_frequency = fe->dtv_property_cache.frequency;
1080 	}
1081 
1082 	ret = lg2160_agc_fix(state, 0, 0);
1083 	if (lg_fail(ret))
1084 		goto fail;
1085 	ret = lg2160_agc_polarity(state, 0, 0);
1086 	if (lg_fail(ret))
1087 		goto fail;
1088 	ret = lg2160_tuner_pwr_save_polarity(state, 1);
1089 	if (lg_fail(ret))
1090 		goto fail;
1091 	ret = lg216x_set_if(state);
1092 	if (lg_fail(ret))
1093 		goto fail;
1094 	ret = lg2160_spectrum_polarity(state, state->cfg->spectral_inversion);
1095 	if (lg_fail(ret))
1096 		goto fail;
1097 
1098 	/* be tuned before this point */
1099 	ret = lg216x_soft_reset(state);
1100 	if (lg_fail(ret))
1101 		goto fail;
1102 
1103 	ret = lg2160_tuner_pwr_save(state, 0);
1104 	if (lg_fail(ret))
1105 		goto fail;
1106 
1107 	switch (state->cfg->lg_chip) {
1108 	case LG2160:
1109 		ret = lg2160_set_spi_clock(state);
1110 		if (lg_fail(ret))
1111 			goto fail;
1112 		break;
1113 	case LG2161:
1114 		ret = lg2161_set_output_interface(state);
1115 		if (lg_fail(ret))
1116 			goto fail;
1117 		break;
1118 	}
1119 
1120 	ret = lg216x_set_parade(state, fe->dtv_property_cache.atscmh_parade_id);
1121 	if (lg_fail(ret))
1122 		goto fail;
1123 
1124 	ret = lg216x_set_ensemble(state,
1125 			fe->dtv_property_cache.atscmh_rs_frame_ensemble);
1126 	if (lg_fail(ret))
1127 		goto fail;
1128 
1129 	ret = lg216x_initialize(state);
1130 	if (lg_fail(ret))
1131 		goto fail;
1132 
1133 	ret = lg216x_enable_fic(state, 1);
1134 	lg_fail(ret);
1135 
1136 	lg216x_get_frontend(fe, c);
1137 fail:
1138 	return ret;
1139 }
1140 
1141 /* ------------------------------------------------------------------------ */
1142 
1143 static int lg2160_read_lock_status(struct lg216x_state *state,
1144 				   int *acq_lock, int *sync_lock)
1145 {
1146 	u8 val;
1147 	int ret;
1148 
1149 	*acq_lock = 0;
1150 	*sync_lock = 0;
1151 
1152 	ret = lg216x_read_reg(state, 0x011b, &val);
1153 	if (lg_fail(ret))
1154 		goto fail;
1155 
1156 	*sync_lock = (val & 0x20) ? 0 : 1;
1157 	*acq_lock  = (val & 0x40) ? 0 : 1;
1158 fail:
1159 	return ret;
1160 }
1161 
1162 #ifdef USE_LG2161_LOCK_BITS
1163 static int lg2161_read_lock_status(struct lg216x_state *state,
1164 				   int *acq_lock, int *sync_lock)
1165 {
1166 	u8 val;
1167 	int ret;
1168 
1169 	*acq_lock = 0;
1170 	*sync_lock = 0;
1171 
1172 	ret = lg216x_read_reg(state, 0x0304, &val);
1173 	if (lg_fail(ret))
1174 		goto fail;
1175 
1176 	*sync_lock = (val & 0x80) ? 0 : 1;
1177 
1178 	ret = lg216x_read_reg(state, 0x011b, &val);
1179 	if (lg_fail(ret))
1180 		goto fail;
1181 
1182 	*acq_lock  = (val & 0x40) ? 0 : 1;
1183 fail:
1184 	return ret;
1185 }
1186 #endif
1187 
1188 static int lg216x_read_lock_status(struct lg216x_state *state,
1189 				   int *acq_lock, int *sync_lock)
1190 {
1191 #ifdef USE_LG2161_LOCK_BITS
1192 	int ret;
1193 	switch (state->cfg->lg_chip) {
1194 	case LG2160:
1195 		ret = lg2160_read_lock_status(state, acq_lock, sync_lock);
1196 		break;
1197 	case LG2161:
1198 		ret = lg2161_read_lock_status(state, acq_lock, sync_lock);
1199 		break;
1200 	default:
1201 		ret = -EINVAL;
1202 		break;
1203 	}
1204 	return ret;
1205 #else
1206 	return lg2160_read_lock_status(state, acq_lock, sync_lock);
1207 #endif
1208 }
1209 
1210 static int lg216x_read_status(struct dvb_frontend *fe, enum fe_status *status)
1211 {
1212 	struct lg216x_state *state = fe->demodulator_priv;
1213 	int ret, acq_lock, sync_lock;
1214 
1215 	*status = 0;
1216 
1217 	ret = lg216x_read_lock_status(state, &acq_lock, &sync_lock);
1218 	if (lg_fail(ret))
1219 		goto fail;
1220 
1221 	lg_dbg("%s%s\n",
1222 	       acq_lock  ? "SIGNALEXIST " : "",
1223 	       sync_lock ? "SYNCLOCK"     : "");
1224 
1225 	if (acq_lock)
1226 		*status |= FE_HAS_SIGNAL;
1227 	if (sync_lock)
1228 		*status |= FE_HAS_SYNC;
1229 
1230 	if (*status)
1231 		*status |= FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_LOCK;
1232 
1233 fail:
1234 	return ret;
1235 }
1236 
1237 /* ------------------------------------------------------------------------ */
1238 
1239 static int lg2160_read_snr(struct dvb_frontend *fe, u16 *snr)
1240 {
1241 	struct lg216x_state *state = fe->demodulator_priv;
1242 	u8 snr1, snr2;
1243 	int ret;
1244 
1245 	*snr = 0;
1246 
1247 	ret = lg216x_read_reg(state, 0x0202, &snr1);
1248 	if (lg_fail(ret))
1249 		goto fail;
1250 
1251 	ret = lg216x_read_reg(state, 0x0203, &snr2);
1252 	if (lg_fail(ret))
1253 		goto fail;
1254 
1255 	if ((snr1 == 0xba) || (snr2 == 0xdf))
1256 		*snr = 0;
1257 	else
1258 #if 1
1259 	*snr =  ((snr1 >> 4) * 100) + ((snr1 & 0x0f) * 10) + (snr2 >> 4);
1260 #else /* BCD */
1261 	*snr =  (snr2 | (snr1 << 8));
1262 #endif
1263 fail:
1264 	return ret;
1265 }
1266 
1267 static int lg2161_read_snr(struct dvb_frontend *fe, u16 *snr)
1268 {
1269 	struct lg216x_state *state = fe->demodulator_priv;
1270 	u8 snr1, snr2;
1271 	int ret;
1272 
1273 	*snr = 0;
1274 
1275 	ret = lg216x_read_reg(state, 0x0302, &snr1);
1276 	if (lg_fail(ret))
1277 		goto fail;
1278 
1279 	ret = lg216x_read_reg(state, 0x0303, &snr2);
1280 	if (lg_fail(ret))
1281 		goto fail;
1282 
1283 	if ((snr1 == 0xba) || (snr2 == 0xfd))
1284 		*snr = 0;
1285 	else
1286 
1287 	*snr =  ((snr1 >> 4) * 100) + ((snr1 & 0x0f) * 10) + (snr2 & 0x0f);
1288 fail:
1289 	return ret;
1290 }
1291 
1292 static int lg216x_read_signal_strength(struct dvb_frontend *fe,
1293 				       u16 *strength)
1294 {
1295 #if 0
1296 	/* borrowed from lgdt330x.c
1297 	 *
1298 	 * Calculate strength from SNR up to 35dB
1299 	 * Even though the SNR can go higher than 35dB,
1300 	 * there is some comfort factor in having a range of
1301 	 * strong signals that can show at 100%
1302 	 */
1303 	struct lg216x_state *state = fe->demodulator_priv;
1304 	u16 snr;
1305 	int ret;
1306 #endif
1307 	*strength = 0;
1308 #if 0
1309 	ret = fe->ops.read_snr(fe, &snr);
1310 	if (lg_fail(ret))
1311 		goto fail;
1312 	/* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
1313 	/* scale the range 0 - 35*2^24 into 0 - 65535 */
1314 	if (state->snr >= 8960 * 0x10000)
1315 		*strength = 0xffff;
1316 	else
1317 		*strength = state->snr / 8960;
1318 fail:
1319 	return ret;
1320 #else
1321 	return 0;
1322 #endif
1323 }
1324 
1325 /* ------------------------------------------------------------------------ */
1326 
1327 static int lg216x_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
1328 {
1329 #if 0
1330 	struct lg216x_state *state = fe->demodulator_priv;
1331 	int ret;
1332 
1333 	ret = lg216x_read_rs_err_count(state,
1334 				       &fe->dtv_property_cache.atscmh_rs_err);
1335 	if (lg_fail(ret))
1336 		goto fail;
1337 
1338 	*ucblocks = fe->dtv_property_cache.atscmh_rs_err;
1339 fail:
1340 #else
1341 	*ucblocks = 0;
1342 #endif
1343 	return 0;
1344 }
1345 
1346 static int lg216x_get_tune_settings(struct dvb_frontend *fe,
1347 				    struct dvb_frontend_tune_settings
1348 				    *fe_tune_settings)
1349 {
1350 	fe_tune_settings->min_delay_ms = 500;
1351 	lg_dbg("\n");
1352 	return 0;
1353 }
1354 
1355 static void lg216x_release(struct dvb_frontend *fe)
1356 {
1357 	struct lg216x_state *state = fe->demodulator_priv;
1358 	lg_dbg("\n");
1359 	kfree(state);
1360 }
1361 
1362 static struct dvb_frontend_ops lg2160_ops = {
1363 	.delsys = { SYS_ATSCMH },
1364 	.info = {
1365 		.name = "LG Electronics LG2160 ATSC/MH Frontend",
1366 		.frequency_min      = 54000000,
1367 		.frequency_max      = 858000000,
1368 		.frequency_stepsize = 62500,
1369 	},
1370 	.i2c_gate_ctrl        = lg216x_i2c_gate_ctrl,
1371 #if 0
1372 	.init                 = lg216x_init,
1373 	.sleep                = lg216x_sleep,
1374 #endif
1375 	.get_property         = lg216x_get_property,
1376 
1377 	.set_frontend         = lg2160_set_frontend,
1378 	.get_frontend         = lg216x_get_frontend,
1379 	.get_tune_settings    = lg216x_get_tune_settings,
1380 	.read_status          = lg216x_read_status,
1381 #if 0
1382 	.read_ber             = lg216x_read_ber,
1383 #endif
1384 	.read_signal_strength = lg216x_read_signal_strength,
1385 	.read_snr             = lg2160_read_snr,
1386 	.read_ucblocks        = lg216x_read_ucblocks,
1387 	.release              = lg216x_release,
1388 };
1389 
1390 static struct dvb_frontend_ops lg2161_ops = {
1391 	.delsys = { SYS_ATSCMH },
1392 	.info = {
1393 		.name = "LG Electronics LG2161 ATSC/MH Frontend",
1394 		.frequency_min      = 54000000,
1395 		.frequency_max      = 858000000,
1396 		.frequency_stepsize = 62500,
1397 	},
1398 	.i2c_gate_ctrl        = lg216x_i2c_gate_ctrl,
1399 #if 0
1400 	.init                 = lg216x_init,
1401 	.sleep                = lg216x_sleep,
1402 #endif
1403 	.get_property         = lg216x_get_property,
1404 
1405 	.set_frontend         = lg2160_set_frontend,
1406 	.get_frontend         = lg216x_get_frontend,
1407 	.get_tune_settings    = lg216x_get_tune_settings,
1408 	.read_status          = lg216x_read_status,
1409 #if 0
1410 	.read_ber             = lg216x_read_ber,
1411 #endif
1412 	.read_signal_strength = lg216x_read_signal_strength,
1413 	.read_snr             = lg2161_read_snr,
1414 	.read_ucblocks        = lg216x_read_ucblocks,
1415 	.release              = lg216x_release,
1416 };
1417 
1418 struct dvb_frontend *lg2160_attach(const struct lg2160_config *config,
1419 				   struct i2c_adapter *i2c_adap)
1420 {
1421 	struct lg216x_state *state = NULL;
1422 
1423 	lg_dbg("(%d-%04x)\n",
1424 	       i2c_adap ? i2c_adapter_id(i2c_adap) : 0,
1425 	       config ? config->i2c_addr : 0);
1426 
1427 	state = kzalloc(sizeof(struct lg216x_state), GFP_KERNEL);
1428 	if (!state)
1429 		return NULL;
1430 
1431 	state->cfg = config;
1432 	state->i2c_adap = i2c_adap;
1433 	state->fic_ver = 0xff;
1434 	state->parade_id = 0xff;
1435 
1436 	switch (config->lg_chip) {
1437 	default:
1438 		lg_warn("invalid chip requested, defaulting to LG2160");
1439 		/* fall-thru */
1440 	case LG2160:
1441 		memcpy(&state->frontend.ops, &lg2160_ops,
1442 		       sizeof(struct dvb_frontend_ops));
1443 		break;
1444 	case LG2161:
1445 		memcpy(&state->frontend.ops, &lg2161_ops,
1446 		       sizeof(struct dvb_frontend_ops));
1447 		break;
1448 	}
1449 
1450 	state->frontend.demodulator_priv = state;
1451 	state->current_frequency = -1;
1452 	/* parade 1 by default */
1453 	state->frontend.dtv_property_cache.atscmh_parade_id = 1;
1454 
1455 	return &state->frontend;
1456 }
1457 EXPORT_SYMBOL(lg2160_attach);
1458 
1459 MODULE_DESCRIPTION("LG Electronics LG216x ATSC/MH Demodulator Driver");
1460 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1461 MODULE_LICENSE("GPL");
1462 MODULE_VERSION("0.3");
1463