1 /*
2  * i2c tv tuner chip device driver
3  * controls all those simple 4-control-bytes style tuners.
4  *
5  * This "tuner-simple" module was split apart from the original "tuner" module.
6  */
7 #include <linux/delay.h>
8 #include <linux/i2c.h>
9 #include <linux/videodev2.h>
10 #include <media/tuner.h>
11 #include <media/v4l2-common.h>
12 #include <media/tuner-types.h>
13 #include "tuner-i2c.h"
14 #include "tuner-simple.h"
15 
16 static int debug;
17 module_param(debug, int, 0644);
18 MODULE_PARM_DESC(debug, "enable verbose debug messages");
19 
20 #define TUNER_SIMPLE_MAX 64
21 static unsigned int simple_devcount;
22 
23 static int offset;
24 module_param(offset, int, 0664);
25 MODULE_PARM_DESC(offset, "Allows to specify an offset for tuner");
26 
27 static unsigned int atv_input[TUNER_SIMPLE_MAX] = \
28 			{ [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
29 static unsigned int dtv_input[TUNER_SIMPLE_MAX] = \
30 			{ [0 ... (TUNER_SIMPLE_MAX-1)] = 0 };
31 module_param_array(atv_input, int, NULL, 0644);
32 module_param_array(dtv_input, int, NULL, 0644);
33 MODULE_PARM_DESC(atv_input, "specify atv rf input, 0 for autoselect");
34 MODULE_PARM_DESC(dtv_input, "specify dtv rf input, 0 for autoselect");
35 
36 /* ---------------------------------------------------------------------- */
37 
38 /* tv standard selection for Temic 4046 FM5
39    this value takes the low bits of control byte 2
40    from datasheet Rev.01, Feb.00
41      standard     BG      I       L       L2      D
42      picture IF   38.9    38.9    38.9    33.95   38.9
43      sound 1      33.4    32.9    32.4    40.45   32.4
44      sound 2      33.16
45      NICAM        33.05   32.348  33.05           33.05
46  */
47 #define TEMIC_SET_PAL_I         0x05
48 #define TEMIC_SET_PAL_DK        0x09
49 #define TEMIC_SET_PAL_L         0x0a /* SECAM ? */
50 #define TEMIC_SET_PAL_L2        0x0b /* change IF ! */
51 #define TEMIC_SET_PAL_BG        0x0c
52 
53 /* tv tuner system standard selection for Philips FQ1216ME
54    this value takes the low bits of control byte 2
55    from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
56      standard		BG	DK	I	L	L`
57      picture carrier	38.90	38.90	38.90	38.90	33.95
58      colour		34.47	34.47	34.47	34.47	38.38
59      sound 1		33.40	32.40	32.90	32.40	40.45
60      sound 2		33.16	-	-	-	-
61      NICAM		33.05	33.05	32.35	33.05	39.80
62  */
63 #define PHILIPS_SET_PAL_I	0x01 /* Bit 2 always zero !*/
64 #define PHILIPS_SET_PAL_BGDK	0x09
65 #define PHILIPS_SET_PAL_L2	0x0a
66 #define PHILIPS_SET_PAL_L	0x0b
67 
68 /* system switching for Philips FI1216MF MK2
69    from datasheet "1996 Jul 09",
70     standard         BG     L      L'
71     picture carrier  38.90  38.90  33.95
72     colour	     34.47  34.37  38.38
73     sound 1          33.40  32.40  40.45
74     sound 2          33.16  -      -
75     NICAM            33.05  33.05  39.80
76  */
77 #define PHILIPS_MF_SET_STD_BG	0x01 /* Bit 2 must be zero, Bit 3 is system output */
78 #define PHILIPS_MF_SET_STD_L	0x03 /* Used on Secam France */
79 #define PHILIPS_MF_SET_STD_LC	0x02 /* Used on SECAM L' */
80 
81 /* Control byte */
82 
83 #define TUNER_RATIO_MASK        0x06 /* Bit cb1:cb2 */
84 #define TUNER_RATIO_SELECT_50   0x00
85 #define TUNER_RATIO_SELECT_32   0x02
86 #define TUNER_RATIO_SELECT_166  0x04
87 #define TUNER_RATIO_SELECT_62   0x06
88 
89 #define TUNER_CHARGE_PUMP       0x40  /* Bit cb6 */
90 
91 /* Status byte */
92 
93 #define TUNER_POR	  0x80
94 #define TUNER_FL          0x40
95 #define TUNER_MODE        0x38
96 #define TUNER_AFC         0x07
97 #define TUNER_SIGNAL      0x07
98 #define TUNER_STEREO      0x10
99 
100 #define TUNER_PLL_LOCKED   0x40
101 #define TUNER_STEREO_MK3   0x04
102 
103 static DEFINE_MUTEX(tuner_simple_list_mutex);
104 static LIST_HEAD(hybrid_tuner_instance_list);
105 
106 struct tuner_simple_priv {
107 	unsigned int nr;
108 	u16 last_div;
109 
110 	struct tuner_i2c_props i2c_props;
111 	struct list_head hybrid_tuner_instance_list;
112 
113 	unsigned int type;
114 	struct tunertype *tun;
115 
116 	u32 frequency;
117 	u32 bandwidth;
118 	bool radio_mode;
119 };
120 
121 /* ---------------------------------------------------------------------- */
122 
123 static int tuner_read_status(struct dvb_frontend *fe)
124 {
125 	struct tuner_simple_priv *priv = fe->tuner_priv;
126 	unsigned char byte;
127 
128 	if (1 != tuner_i2c_xfer_recv(&priv->i2c_props, &byte, 1))
129 		return 0;
130 
131 	return byte;
132 }
133 
134 static inline int tuner_signal(const int status)
135 {
136 	return (status & TUNER_SIGNAL) << 13;
137 }
138 
139 static inline int tuner_stereo(const int type, const int status)
140 {
141 	switch (type) {
142 	case TUNER_PHILIPS_FM1216ME_MK3:
143 	case TUNER_PHILIPS_FM1236_MK3:
144 	case TUNER_PHILIPS_FM1256_IH3:
145 	case TUNER_LG_NTSC_TAPE:
146 	case TUNER_TCL_MF02GIP_5N:
147 		return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
148 	case TUNER_PHILIPS_FM1216MK5:
149 		return status | TUNER_STEREO;
150 	default:
151 		return status & TUNER_STEREO;
152 	}
153 }
154 
155 static inline int tuner_islocked(const int status)
156 {
157 	return (status & TUNER_FL);
158 }
159 
160 static inline int tuner_afcstatus(const int status)
161 {
162 	return (status & TUNER_AFC) - 2;
163 }
164 
165 
166 static int simple_get_status(struct dvb_frontend *fe, u32 *status)
167 {
168 	struct tuner_simple_priv *priv = fe->tuner_priv;
169 	int tuner_status;
170 
171 	if (priv->i2c_props.adap == NULL)
172 		return -EINVAL;
173 
174 	tuner_status = tuner_read_status(fe);
175 
176 	*status = 0;
177 
178 	if (tuner_islocked(tuner_status))
179 		*status = TUNER_STATUS_LOCKED;
180 	if (tuner_stereo(priv->type, tuner_status))
181 		*status |= TUNER_STATUS_STEREO;
182 
183 	tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
184 
185 	return 0;
186 }
187 
188 static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
189 {
190 	struct tuner_simple_priv *priv = fe->tuner_priv;
191 	int signal;
192 
193 	if (priv->i2c_props.adap == NULL || !priv->radio_mode)
194 		return -EINVAL;
195 
196 	signal = tuner_signal(tuner_read_status(fe));
197 
198 	*strength = signal;
199 
200 	tuner_dbg("Signal strength: %d\n", signal);
201 
202 	return 0;
203 }
204 
205 /* ---------------------------------------------------------------------- */
206 
207 static inline char *tuner_param_name(enum param_type type)
208 {
209 	char *name;
210 
211 	switch (type) {
212 	case TUNER_PARAM_TYPE_RADIO:
213 		name = "radio";
214 		break;
215 	case TUNER_PARAM_TYPE_PAL:
216 		name = "pal";
217 		break;
218 	case TUNER_PARAM_TYPE_SECAM:
219 		name = "secam";
220 		break;
221 	case TUNER_PARAM_TYPE_NTSC:
222 		name = "ntsc";
223 		break;
224 	case TUNER_PARAM_TYPE_DIGITAL:
225 		name = "digital";
226 		break;
227 	default:
228 		name = "unknown";
229 		break;
230 	}
231 	return name;
232 }
233 
234 static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
235 						enum param_type desired_type)
236 {
237 	struct tuner_simple_priv *priv = fe->tuner_priv;
238 	struct tunertype *tun = priv->tun;
239 	int i;
240 
241 	for (i = 0; i < tun->count; i++)
242 		if (desired_type == tun->params[i].type)
243 			break;
244 
245 	/* use default tuner params if desired_type not available */
246 	if (i == tun->count) {
247 		tuner_dbg("desired params (%s) undefined for tuner %d\n",
248 			  tuner_param_name(desired_type), priv->type);
249 		i = 0;
250 	}
251 
252 	tuner_dbg("using tuner params #%d (%s)\n", i,
253 		  tuner_param_name(tun->params[i].type));
254 
255 	return &tun->params[i];
256 }
257 
258 static int simple_config_lookup(struct dvb_frontend *fe,
259 				struct tuner_params *t_params,
260 				unsigned *frequency, u8 *config, u8 *cb)
261 {
262 	struct tuner_simple_priv *priv = fe->tuner_priv;
263 	int i;
264 
265 	for (i = 0; i < t_params->count; i++) {
266 		if (*frequency > t_params->ranges[i].limit)
267 			continue;
268 		break;
269 	}
270 	if (i == t_params->count) {
271 		tuner_dbg("frequency out of range (%d > %d)\n",
272 			  *frequency, t_params->ranges[i - 1].limit);
273 		*frequency = t_params->ranges[--i].limit;
274 	}
275 	*config = t_params->ranges[i].config;
276 	*cb     = t_params->ranges[i].cb;
277 
278 	tuner_dbg("freq = %d.%02d (%d), range = %d, config = 0x%02x, cb = 0x%02x\n",
279 		  *frequency / 16, *frequency % 16 * 100 / 16, *frequency,
280 		  i, *config, *cb);
281 
282 	return i;
283 }
284 
285 /* ---------------------------------------------------------------------- */
286 
287 static void simple_set_rf_input(struct dvb_frontend *fe,
288 				u8 *config, u8 *cb, unsigned int rf)
289 {
290 	struct tuner_simple_priv *priv = fe->tuner_priv;
291 
292 	switch (priv->type) {
293 	case TUNER_PHILIPS_TUV1236D:
294 		switch (rf) {
295 		case 1:
296 			*cb |= 0x08;
297 			break;
298 		default:
299 			*cb &= ~0x08;
300 			break;
301 		}
302 		break;
303 	case TUNER_PHILIPS_FCV1236D:
304 		switch (rf) {
305 		case 1:
306 			*cb |= 0x01;
307 			break;
308 		default:
309 			*cb &= ~0x01;
310 			break;
311 		}
312 		break;
313 	default:
314 		break;
315 	}
316 }
317 
318 static int simple_std_setup(struct dvb_frontend *fe,
319 			    struct analog_parameters *params,
320 			    u8 *config, u8 *cb)
321 {
322 	struct tuner_simple_priv *priv = fe->tuner_priv;
323 	int rc;
324 
325 	/* tv norm specific stuff for multi-norm tuners */
326 	switch (priv->type) {
327 	case TUNER_PHILIPS_SECAM: /* FI1216MF */
328 		/* 0x01 -> ??? no change ??? */
329 		/* 0x02 -> PAL BDGHI / SECAM L */
330 		/* 0x04 -> ??? PAL others / SECAM others ??? */
331 		*cb &= ~0x03;
332 		if (params->std & V4L2_STD_SECAM_L)
333 			/* also valid for V4L2_STD_SECAM */
334 			*cb |= PHILIPS_MF_SET_STD_L;
335 		else if (params->std & V4L2_STD_SECAM_LC)
336 			*cb |= PHILIPS_MF_SET_STD_LC;
337 		else /* V4L2_STD_B|V4L2_STD_GH */
338 			*cb |= PHILIPS_MF_SET_STD_BG;
339 		break;
340 
341 	case TUNER_TEMIC_4046FM5:
342 		*cb &= ~0x0f;
343 
344 		if (params->std & V4L2_STD_PAL_BG) {
345 			*cb |= TEMIC_SET_PAL_BG;
346 
347 		} else if (params->std & V4L2_STD_PAL_I) {
348 			*cb |= TEMIC_SET_PAL_I;
349 
350 		} else if (params->std & V4L2_STD_PAL_DK) {
351 			*cb |= TEMIC_SET_PAL_DK;
352 
353 		} else if (params->std & V4L2_STD_SECAM_L) {
354 			*cb |= TEMIC_SET_PAL_L;
355 
356 		}
357 		break;
358 
359 	case TUNER_PHILIPS_FQ1216ME:
360 		*cb &= ~0x0f;
361 
362 		if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
363 			*cb |= PHILIPS_SET_PAL_BGDK;
364 
365 		} else if (params->std & V4L2_STD_PAL_I) {
366 			*cb |= PHILIPS_SET_PAL_I;
367 
368 		} else if (params->std & V4L2_STD_SECAM_L) {
369 			*cb |= PHILIPS_SET_PAL_L;
370 
371 		}
372 		break;
373 
374 	case TUNER_PHILIPS_FCV1236D:
375 		/* 0x00 -> ATSC antenna input 1 */
376 		/* 0x01 -> ATSC antenna input 2 */
377 		/* 0x02 -> NTSC antenna input 1 */
378 		/* 0x03 -> NTSC antenna input 2 */
379 		*cb &= ~0x03;
380 		if (!(params->std & V4L2_STD_ATSC))
381 			*cb |= 2;
382 		break;
383 
384 	case TUNER_MICROTUNE_4042FI5:
385 		/* Set the charge pump for fast tuning */
386 		*config |= TUNER_CHARGE_PUMP;
387 		break;
388 
389 	case TUNER_PHILIPS_TUV1236D:
390 	{
391 		struct tuner_i2c_props i2c = priv->i2c_props;
392 		/* 0x40 -> ATSC antenna input 1 */
393 		/* 0x48 -> ATSC antenna input 2 */
394 		/* 0x00 -> NTSC antenna input 1 */
395 		/* 0x08 -> NTSC antenna input 2 */
396 		u8 buffer[4] = { 0x14, 0x00, 0x17, 0x00};
397 		*cb &= ~0x40;
398 		if (params->std & V4L2_STD_ATSC) {
399 			*cb |= 0x40;
400 			buffer[1] = 0x04;
401 		}
402 		/* set to the correct mode (analog or digital) */
403 		i2c.addr = 0x0a;
404 		rc = tuner_i2c_xfer_send(&i2c, &buffer[0], 2);
405 		if (2 != rc)
406 			tuner_warn("i2c i/o error: rc == %d (should be 2)\n",
407 				   rc);
408 		rc = tuner_i2c_xfer_send(&i2c, &buffer[2], 2);
409 		if (2 != rc)
410 			tuner_warn("i2c i/o error: rc == %d (should be 2)\n",
411 				   rc);
412 		break;
413 	}
414 	}
415 	if (atv_input[priv->nr])
416 		simple_set_rf_input(fe, config, cb, atv_input[priv->nr]);
417 
418 	return 0;
419 }
420 
421 static int simple_set_aux_byte(struct dvb_frontend *fe, u8 config, u8 aux)
422 {
423 	struct tuner_simple_priv *priv = fe->tuner_priv;
424 	int rc;
425 	u8 buffer[2];
426 
427 	buffer[0] = (config & ~0x38) | 0x18;
428 	buffer[1] = aux;
429 
430 	tuner_dbg("setting aux byte: 0x%02x 0x%02x\n", buffer[0], buffer[1]);
431 
432 	rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
433 	if (2 != rc)
434 		tuner_warn("i2c i/o error: rc == %d (should be 2)\n", rc);
435 
436 	return rc == 2 ? 0 : rc;
437 }
438 
439 static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
440 			    u16 div, u8 config, u8 cb)
441 {
442 	struct tuner_simple_priv *priv = fe->tuner_priv;
443 	int rc;
444 
445 	switch (priv->type) {
446 	case TUNER_LG_TDVS_H06XF:
447 		simple_set_aux_byte(fe, config, 0x20);
448 		break;
449 	case TUNER_PHILIPS_FQ1216LME_MK3:
450 		simple_set_aux_byte(fe, config, 0x60); /* External AGC */
451 		break;
452 	case TUNER_MICROTUNE_4042FI5:
453 	{
454 		/* FIXME - this may also work for other tuners */
455 		unsigned long timeout = jiffies + msecs_to_jiffies(1);
456 		u8 status_byte = 0;
457 
458 		/* Wait until the PLL locks */
459 		for (;;) {
460 			if (time_after(jiffies, timeout))
461 				return 0;
462 			rc = tuner_i2c_xfer_recv(&priv->i2c_props,
463 						 &status_byte, 1);
464 			if (1 != rc) {
465 				tuner_warn("i2c i/o read error: rc == %d (should be 1)\n",
466 					   rc);
467 				break;
468 			}
469 			if (status_byte & TUNER_PLL_LOCKED)
470 				break;
471 			udelay(10);
472 		}
473 
474 		/* Set the charge pump for optimized phase noise figure */
475 		config &= ~TUNER_CHARGE_PUMP;
476 		buffer[0] = (div>>8) & 0x7f;
477 		buffer[1] = div      & 0xff;
478 		buffer[2] = config;
479 		buffer[3] = cb;
480 		tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
481 			  buffer[0], buffer[1], buffer[2], buffer[3]);
482 
483 		rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
484 		if (4 != rc)
485 			tuner_warn("i2c i/o error: rc == %d (should be 4)\n",
486 				   rc);
487 		break;
488 	}
489 	}
490 
491 	return 0;
492 }
493 
494 static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
495 {
496 	struct tuner_simple_priv *priv = fe->tuner_priv;
497 
498 	switch (priv->type) {
499 	case TUNER_TENA_9533_DI:
500 	case TUNER_YMEC_TVF_5533MF:
501 		tuner_dbg("This tuner doesn't have FM. Most cards have a TEA5767 for FM\n");
502 		return 0;
503 	case TUNER_PHILIPS_FM1216ME_MK3:
504 	case TUNER_PHILIPS_FM1236_MK3:
505 	case TUNER_PHILIPS_FMD1216ME_MK3:
506 	case TUNER_PHILIPS_FMD1216MEX_MK3:
507 	case TUNER_LG_NTSC_TAPE:
508 	case TUNER_PHILIPS_FM1256_IH3:
509 	case TUNER_TCL_MF02GIP_5N:
510 		buffer[3] = 0x19;
511 		break;
512 	case TUNER_PHILIPS_FM1216MK5:
513 		buffer[2] = 0x88;
514 		buffer[3] = 0x09;
515 		break;
516 	case TUNER_TNF_5335MF:
517 		buffer[3] = 0x11;
518 		break;
519 	case TUNER_LG_PAL_FM:
520 		buffer[3] = 0xa5;
521 		break;
522 	case TUNER_THOMSON_DTT761X:
523 		buffer[3] = 0x39;
524 		break;
525 	case TUNER_PHILIPS_FQ1216LME_MK3:
526 	case TUNER_PHILIPS_FQ1236_MK5:
527 		tuner_err("This tuner doesn't have FM\n");
528 		/* Set the low band for sanity, since it covers 88-108 MHz */
529 		buffer[3] = 0x01;
530 		break;
531 	case TUNER_MICROTUNE_4049FM5:
532 	default:
533 		buffer[3] = 0xa4;
534 		break;
535 	}
536 
537 	return 0;
538 }
539 
540 /* ---------------------------------------------------------------------- */
541 
542 static int simple_set_tv_freq(struct dvb_frontend *fe,
543 			      struct analog_parameters *params)
544 {
545 	struct tuner_simple_priv *priv = fe->tuner_priv;
546 	u8 config, cb;
547 	u16 div;
548 	u8 buffer[4];
549 	int rc, IFPCoff, i;
550 	enum param_type desired_type;
551 	struct tuner_params *t_params;
552 
553 	/* IFPCoff = Video Intermediate Frequency - Vif:
554 		940  =16*58.75  NTSC/J (Japan)
555 		732  =16*45.75  M/N STD
556 		704  =16*44     ATSC (at DVB code)
557 		632  =16*39.50  I U.K.
558 		622.4=16*38.90  B/G D/K I, L STD
559 		592  =16*37.00  D China
560 		590  =16.36.875 B Australia
561 		543.2=16*33.95  L' STD
562 		171.2=16*10.70  FM Radio (at set_radio_freq)
563 	*/
564 
565 	if (params->std == V4L2_STD_NTSC_M_JP) {
566 		IFPCoff      = 940;
567 		desired_type = TUNER_PARAM_TYPE_NTSC;
568 	} else if ((params->std & V4L2_STD_MN) &&
569 		  !(params->std & ~V4L2_STD_MN)) {
570 		IFPCoff      = 732;
571 		desired_type = TUNER_PARAM_TYPE_NTSC;
572 	} else if (params->std == V4L2_STD_SECAM_LC) {
573 		IFPCoff      = 543;
574 		desired_type = TUNER_PARAM_TYPE_SECAM;
575 	} else {
576 		IFPCoff      = 623;
577 		desired_type = TUNER_PARAM_TYPE_PAL;
578 	}
579 
580 	t_params = simple_tuner_params(fe, desired_type);
581 
582 	i = simple_config_lookup(fe, t_params, &params->frequency,
583 				 &config, &cb);
584 
585 	div = params->frequency + IFPCoff + offset;
586 
587 	tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, Offset=%d.%02d MHz, div=%0d\n",
588 		  params->frequency / 16, params->frequency % 16 * 100 / 16,
589 		  IFPCoff / 16, IFPCoff % 16 * 100 / 16,
590 		  offset / 16, offset % 16 * 100 / 16, div);
591 
592 	/* tv norm specific stuff for multi-norm tuners */
593 	simple_std_setup(fe, params, &config, &cb);
594 
595 	if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
596 		buffer[0] = config;
597 		buffer[1] = cb;
598 		buffer[2] = (div>>8) & 0x7f;
599 		buffer[3] = div      & 0xff;
600 	} else {
601 		buffer[0] = (div>>8) & 0x7f;
602 		buffer[1] = div      & 0xff;
603 		buffer[2] = config;
604 		buffer[3] = cb;
605 	}
606 	priv->last_div = div;
607 	if (t_params->has_tda9887) {
608 		struct v4l2_priv_tun_config tda9887_cfg;
609 		int tda_config = 0;
610 		int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
611 						 V4L2_STD_SECAM_LC)) &&
612 			!(params->std & ~(V4L2_STD_SECAM_L |
613 					  V4L2_STD_SECAM_LC));
614 
615 		tda9887_cfg.tuner = TUNER_TDA9887;
616 		tda9887_cfg.priv  = &tda_config;
617 
618 		if (params->std == V4L2_STD_SECAM_LC) {
619 			if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
620 				tda_config |= TDA9887_PORT1_ACTIVE;
621 			if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
622 				tda_config |= TDA9887_PORT2_ACTIVE;
623 		} else {
624 			if (t_params->port1_active)
625 				tda_config |= TDA9887_PORT1_ACTIVE;
626 			if (t_params->port2_active)
627 				tda_config |= TDA9887_PORT2_ACTIVE;
628 		}
629 		if (t_params->intercarrier_mode)
630 			tda_config |= TDA9887_INTERCARRIER;
631 		if (is_secam_l) {
632 			if (i == 0 && t_params->default_top_secam_low)
633 				tda_config |= TDA9887_TOP(t_params->default_top_secam_low);
634 			else if (i == 1 && t_params->default_top_secam_mid)
635 				tda_config |= TDA9887_TOP(t_params->default_top_secam_mid);
636 			else if (t_params->default_top_secam_high)
637 				tda_config |= TDA9887_TOP(t_params->default_top_secam_high);
638 		} else {
639 			if (i == 0 && t_params->default_top_low)
640 				tda_config |= TDA9887_TOP(t_params->default_top_low);
641 			else if (i == 1 && t_params->default_top_mid)
642 				tda_config |= TDA9887_TOP(t_params->default_top_mid);
643 			else if (t_params->default_top_high)
644 				tda_config |= TDA9887_TOP(t_params->default_top_high);
645 		}
646 		if (t_params->default_pll_gating_18)
647 			tda_config |= TDA9887_GATING_18;
648 		i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
649 				    &tda9887_cfg);
650 	}
651 	tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
652 		  buffer[0], buffer[1], buffer[2], buffer[3]);
653 
654 	rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
655 	if (4 != rc)
656 		tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
657 
658 	simple_post_tune(fe, &buffer[0], div, config, cb);
659 
660 	return 0;
661 }
662 
663 static int simple_set_radio_freq(struct dvb_frontend *fe,
664 				 struct analog_parameters *params)
665 {
666 	struct tunertype *tun;
667 	struct tuner_simple_priv *priv = fe->tuner_priv;
668 	u8 buffer[4];
669 	u16 div;
670 	int rc, j;
671 	struct tuner_params *t_params;
672 	unsigned int freq = params->frequency;
673 
674 	tun = priv->tun;
675 
676 	for (j = tun->count-1; j > 0; j--)
677 		if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
678 			break;
679 	/* default t_params (j=0) will be used if desired type wasn't found */
680 	t_params = &tun->params[j];
681 
682 	/* Select Radio 1st IF used */
683 	switch (t_params->radio_if) {
684 	case 0: /* 10.7 MHz */
685 		freq += (unsigned int)(10.7*16000);
686 		break;
687 	case 1: /* 33.3 MHz */
688 		freq += (unsigned int)(33.3*16000);
689 		break;
690 	case 2: /* 41.3 MHz */
691 		freq += (unsigned int)(41.3*16000);
692 		break;
693 	default:
694 		tuner_warn("Unsupported radio_if value %d\n",
695 			   t_params->radio_if);
696 		return 0;
697 	}
698 
699 	buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
700 		    TUNER_RATIO_SELECT_50; /* 50 kHz step */
701 
702 	/* Bandswitch byte */
703 	simple_radio_bandswitch(fe, &buffer[0]);
704 
705 	/* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
706 	   freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
707 	   freq * (1/800) */
708 	div = (freq + 400) / 800;
709 
710 	if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
711 		buffer[0] = buffer[2];
712 		buffer[1] = buffer[3];
713 		buffer[2] = (div>>8) & 0x7f;
714 		buffer[3] = div      & 0xff;
715 	} else {
716 		buffer[0] = (div>>8) & 0x7f;
717 		buffer[1] = div      & 0xff;
718 	}
719 
720 	tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
721 	       buffer[0], buffer[1], buffer[2], buffer[3]);
722 	priv->last_div = div;
723 
724 	if (t_params->has_tda9887) {
725 		int config = 0;
726 		struct v4l2_priv_tun_config tda9887_cfg;
727 
728 		tda9887_cfg.tuner = TUNER_TDA9887;
729 		tda9887_cfg.priv = &config;
730 
731 		if (t_params->port1_active &&
732 		    !t_params->port1_fm_high_sensitivity)
733 			config |= TDA9887_PORT1_ACTIVE;
734 		if (t_params->port2_active &&
735 		    !t_params->port2_fm_high_sensitivity)
736 			config |= TDA9887_PORT2_ACTIVE;
737 		if (t_params->intercarrier_mode)
738 			config |= TDA9887_INTERCARRIER;
739 /*		if (t_params->port1_set_for_fm_mono)
740 			config &= ~TDA9887_PORT1_ACTIVE;*/
741 		if (t_params->fm_gain_normal)
742 			config |= TDA9887_GAIN_NORMAL;
743 		if (t_params->radio_if == 2)
744 			config |= TDA9887_RIF_41_3;
745 		i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
746 				    &tda9887_cfg);
747 	}
748 	rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
749 	if (4 != rc)
750 		tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
751 
752 	/* Write AUX byte */
753 	switch (priv->type) {
754 	case TUNER_PHILIPS_FM1216ME_MK3:
755 		buffer[2] = 0x98;
756 		buffer[3] = 0x20; /* set TOP AGC */
757 		rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
758 		if (4 != rc)
759 			tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
760 		break;
761 	}
762 
763 	return 0;
764 }
765 
766 static int simple_set_params(struct dvb_frontend *fe,
767 			     struct analog_parameters *params)
768 {
769 	struct tuner_simple_priv *priv = fe->tuner_priv;
770 	int ret = -EINVAL;
771 
772 	if (priv->i2c_props.adap == NULL)
773 		return -EINVAL;
774 
775 	switch (params->mode) {
776 	case V4L2_TUNER_RADIO:
777 		priv->radio_mode = true;
778 		ret = simple_set_radio_freq(fe, params);
779 		priv->frequency = params->frequency * 125 / 2;
780 		break;
781 	case V4L2_TUNER_ANALOG_TV:
782 	case V4L2_TUNER_DIGITAL_TV:
783 		priv->radio_mode = false;
784 		ret = simple_set_tv_freq(fe, params);
785 		priv->frequency = params->frequency * 62500;
786 		break;
787 	}
788 	priv->bandwidth = 0;
789 
790 	return ret;
791 }
792 
793 static void simple_set_dvb(struct dvb_frontend *fe, u8 *buf,
794 			   const u32 delsys,
795 			   const u32 frequency,
796 			   const u32 bandwidth)
797 {
798 	struct tuner_simple_priv *priv = fe->tuner_priv;
799 
800 	switch (priv->type) {
801 	case TUNER_PHILIPS_FMD1216ME_MK3:
802 	case TUNER_PHILIPS_FMD1216MEX_MK3:
803 		if (bandwidth == 8000000 &&
804 		    frequency >= 158870000)
805 			buf[3] |= 0x08;
806 		break;
807 	case TUNER_PHILIPS_TD1316:
808 		/* determine band */
809 		buf[3] |= (frequency < 161000000) ? 1 :
810 			  (frequency < 444000000) ? 2 : 4;
811 
812 		/* setup PLL filter */
813 		if (bandwidth == 8000000)
814 			buf[3] |= 1 << 3;
815 		break;
816 	case TUNER_PHILIPS_TUV1236D:
817 	case TUNER_PHILIPS_FCV1236D:
818 	{
819 		unsigned int new_rf;
820 
821 		if (dtv_input[priv->nr])
822 			new_rf = dtv_input[priv->nr];
823 		else
824 			switch (delsys) {
825 			case SYS_DVBC_ANNEX_B:
826 				new_rf = 1;
827 				break;
828 			case SYS_ATSC:
829 			default:
830 				new_rf = 0;
831 				break;
832 			}
833 		simple_set_rf_input(fe, &buf[2], &buf[3], new_rf);
834 		break;
835 	}
836 	default:
837 		break;
838 	}
839 }
840 
841 static u32 simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
842 				const u32 delsys,
843 				const u32 freq,
844 				const u32 bw)
845 {
846 	/* This function returns the tuned frequency on success, 0 on error */
847 	struct tuner_simple_priv *priv = fe->tuner_priv;
848 	struct tunertype *tun = priv->tun;
849 	struct tuner_params *t_params;
850 	u8 config, cb;
851 	u32 div;
852 	int ret;
853 	u32 frequency = freq / 62500;
854 
855 	if (!tun->stepsize) {
856 		/* tuner-core was loaded before the digital tuner was
857 		 * configured and somehow picked the wrong tuner type */
858 		tuner_err("attempt to treat tuner %d (%s) as digital tuner without stepsize defined.\n",
859 			  priv->type, priv->tun->name);
860 		return 0; /* failure */
861 	}
862 
863 	t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
864 	ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
865 	if (ret < 0)
866 		return 0; /* failure */
867 
868 	div = ((frequency + t_params->iffreq) * 62500 + offset +
869 	       tun->stepsize/2) / tun->stepsize;
870 
871 	buf[0] = div >> 8;
872 	buf[1] = div & 0xff;
873 	buf[2] = config;
874 	buf[3] = cb;
875 
876 	simple_set_dvb(fe, buf, delsys, freq, bw);
877 
878 	tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
879 		  tun->name, div, buf[0], buf[1], buf[2], buf[3]);
880 
881 	/* calculate the frequency we set it to */
882 	return (div * tun->stepsize) - t_params->iffreq;
883 }
884 
885 static int simple_dvb_calc_regs(struct dvb_frontend *fe,
886 				u8 *buf, int buf_len)
887 {
888 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
889 	u32 delsys = c->delivery_system;
890 	u32 bw = c->bandwidth_hz;
891 	struct tuner_simple_priv *priv = fe->tuner_priv;
892 	u32 frequency;
893 
894 	if (buf_len < 5)
895 		return -EINVAL;
896 
897 	frequency = simple_dvb_configure(fe, buf+1, delsys, c->frequency, bw);
898 	if (frequency == 0)
899 		return -EINVAL;
900 
901 	buf[0] = priv->i2c_props.addr;
902 
903 	priv->frequency = frequency;
904 	priv->bandwidth = c->bandwidth_hz;
905 
906 	return 5;
907 }
908 
909 static int simple_dvb_set_params(struct dvb_frontend *fe)
910 {
911 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
912 	u32 delsys = c->delivery_system;
913 	u32 bw = c->bandwidth_hz;
914 	u32 freq = c->frequency;
915 	struct tuner_simple_priv *priv = fe->tuner_priv;
916 	u32 frequency;
917 	u32 prev_freq, prev_bw;
918 	int ret;
919 	u8 buf[5];
920 
921 	if (priv->i2c_props.adap == NULL)
922 		return -EINVAL;
923 
924 	prev_freq = priv->frequency;
925 	prev_bw   = priv->bandwidth;
926 
927 	frequency = simple_dvb_configure(fe, buf+1, delsys, freq, bw);
928 	if (frequency == 0)
929 		return -EINVAL;
930 
931 	buf[0] = priv->i2c_props.addr;
932 
933 	priv->frequency = frequency;
934 	priv->bandwidth = bw;
935 
936 	/* put analog demod in standby when tuning digital */
937 	if (fe->ops.analog_ops.standby)
938 		fe->ops.analog_ops.standby(fe);
939 
940 	if (fe->ops.i2c_gate_ctrl)
941 		fe->ops.i2c_gate_ctrl(fe, 1);
942 
943 	/* buf[0] contains the i2c address, but *
944 	 * we already have it in i2c_props.addr */
945 	ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
946 	if (ret != 4)
947 		goto fail;
948 
949 	return 0;
950 fail:
951 	/* calc_regs sets frequency and bandwidth. if we failed, unset them */
952 	priv->frequency = prev_freq;
953 	priv->bandwidth = prev_bw;
954 
955 	return ret;
956 }
957 
958 static int simple_init(struct dvb_frontend *fe)
959 {
960 	struct tuner_simple_priv *priv = fe->tuner_priv;
961 
962 	if (priv->i2c_props.adap == NULL)
963 		return -EINVAL;
964 
965 	if (priv->tun->initdata) {
966 		int ret;
967 
968 		if (fe->ops.i2c_gate_ctrl)
969 			fe->ops.i2c_gate_ctrl(fe, 1);
970 
971 		ret = tuner_i2c_xfer_send(&priv->i2c_props,
972 					  priv->tun->initdata + 1,
973 					  priv->tun->initdata[0]);
974 		if (ret != priv->tun->initdata[0])
975 			return ret;
976 	}
977 
978 	return 0;
979 }
980 
981 static int simple_sleep(struct dvb_frontend *fe)
982 {
983 	struct tuner_simple_priv *priv = fe->tuner_priv;
984 
985 	if (priv->i2c_props.adap == NULL)
986 		return -EINVAL;
987 
988 	if (priv->tun->sleepdata) {
989 		int ret;
990 
991 		if (fe->ops.i2c_gate_ctrl)
992 			fe->ops.i2c_gate_ctrl(fe, 1);
993 
994 		ret = tuner_i2c_xfer_send(&priv->i2c_props,
995 					  priv->tun->sleepdata + 1,
996 					  priv->tun->sleepdata[0]);
997 		if (ret != priv->tun->sleepdata[0])
998 			return ret;
999 	}
1000 
1001 	return 0;
1002 }
1003 
1004 static void simple_release(struct dvb_frontend *fe)
1005 {
1006 	struct tuner_simple_priv *priv = fe->tuner_priv;
1007 
1008 	mutex_lock(&tuner_simple_list_mutex);
1009 
1010 	if (priv)
1011 		hybrid_tuner_release_state(priv);
1012 
1013 	mutex_unlock(&tuner_simple_list_mutex);
1014 
1015 	fe->tuner_priv = NULL;
1016 }
1017 
1018 static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1019 {
1020 	struct tuner_simple_priv *priv = fe->tuner_priv;
1021 	*frequency = priv->frequency;
1022 	return 0;
1023 }
1024 
1025 static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1026 {
1027 	struct tuner_simple_priv *priv = fe->tuner_priv;
1028 	*bandwidth = priv->bandwidth;
1029 	return 0;
1030 }
1031 
1032 static const struct dvb_tuner_ops simple_tuner_ops = {
1033 	.init              = simple_init,
1034 	.sleep             = simple_sleep,
1035 	.set_analog_params = simple_set_params,
1036 	.set_params        = simple_dvb_set_params,
1037 	.calc_regs         = simple_dvb_calc_regs,
1038 	.release           = simple_release,
1039 	.get_frequency     = simple_get_frequency,
1040 	.get_bandwidth     = simple_get_bandwidth,
1041 	.get_status        = simple_get_status,
1042 	.get_rf_strength   = simple_get_rf_strength,
1043 };
1044 
1045 struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
1046 					 struct i2c_adapter *i2c_adap,
1047 					 u8 i2c_addr,
1048 					 unsigned int type)
1049 {
1050 	struct tuner_simple_priv *priv = NULL;
1051 	int instance;
1052 
1053 	if (type >= tuner_count) {
1054 		printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
1055 		       __func__, type, tuner_count-1);
1056 		return NULL;
1057 	}
1058 
1059 	/* If i2c_adap is set, check that the tuner is at the correct address.
1060 	 * Otherwise, if i2c_adap is NULL, the tuner will be programmed directly
1061 	 * by the digital demod via calc_regs.
1062 	 */
1063 	if (i2c_adap != NULL) {
1064 		u8 b[1];
1065 		struct i2c_msg msg = {
1066 			.addr = i2c_addr, .flags = I2C_M_RD,
1067 			.buf = b, .len = 1,
1068 		};
1069 
1070 		if (fe->ops.i2c_gate_ctrl)
1071 			fe->ops.i2c_gate_ctrl(fe, 1);
1072 
1073 		if (1 != i2c_transfer(i2c_adap, &msg, 1))
1074 			printk(KERN_WARNING "tuner-simple %d-%04x: unable to probe %s, proceeding anyway.",
1075 			       i2c_adapter_id(i2c_adap), i2c_addr,
1076 			       tuners[type].name);
1077 
1078 		if (fe->ops.i2c_gate_ctrl)
1079 			fe->ops.i2c_gate_ctrl(fe, 0);
1080 	}
1081 
1082 	mutex_lock(&tuner_simple_list_mutex);
1083 
1084 	instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
1085 					      hybrid_tuner_instance_list,
1086 					      i2c_adap, i2c_addr,
1087 					      "tuner-simple");
1088 	switch (instance) {
1089 	case 0:
1090 		mutex_unlock(&tuner_simple_list_mutex);
1091 		return NULL;
1092 	case 1:
1093 		fe->tuner_priv = priv;
1094 
1095 		priv->type = type;
1096 		priv->tun  = &tuners[type];
1097 		priv->nr   = simple_devcount++;
1098 		break;
1099 	default:
1100 		fe->tuner_priv = priv;
1101 		break;
1102 	}
1103 
1104 	mutex_unlock(&tuner_simple_list_mutex);
1105 
1106 	memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
1107 	       sizeof(struct dvb_tuner_ops));
1108 
1109 	if (type != priv->type)
1110 		tuner_warn("couldn't set type to %d. Using %d (%s) instead\n",
1111 			    type, priv->type, priv->tun->name);
1112 	else
1113 		tuner_info("type set to %d (%s)\n",
1114 			   priv->type, priv->tun->name);
1115 
1116 	if ((debug) || ((atv_input[priv->nr] > 0) ||
1117 			(dtv_input[priv->nr] > 0))) {
1118 		if (0 == atv_input[priv->nr])
1119 			tuner_info("tuner %d atv rf input will be autoselected\n",
1120 				   priv->nr);
1121 		else
1122 			tuner_info("tuner %d atv rf input will be set to input %d (insmod option)\n",
1123 				   priv->nr, atv_input[priv->nr]);
1124 		if (0 == dtv_input[priv->nr])
1125 			tuner_info("tuner %d dtv rf input will be autoselected\n",
1126 				   priv->nr);
1127 		else
1128 			tuner_info("tuner %d dtv rf input will be set to input %d (insmod option)\n",
1129 				   priv->nr, dtv_input[priv->nr]);
1130 	}
1131 
1132 	strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
1133 		sizeof(fe->ops.tuner_ops.info.name));
1134 
1135 	return fe;
1136 }
1137 EXPORT_SYMBOL_GPL(simple_tuner_attach);
1138 
1139 MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
1140 MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
1141 MODULE_LICENSE("GPL");
1142