blob: d6d922c32bcef63a8780cc3b1cfa2adf92d2e34b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * i2c tv tuner chip device driver
3 * controls all those simple 4-control-bytes style tuners.
Michael Krufky4adad282007-08-27 21:59:08 -03004 *
5 * This "tuner-simple" module was split apart from the original "tuner" module.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7#include <linux/delay.h>
8#include <linux/i2c.h>
9#include <linux/videodev.h>
10#include <media/tuner.h>
Hans Verkuilba8fc392006-06-25 15:34:39 -030011#include <media/v4l2-common.h>
Michael Krufky8218b0b2007-06-26 13:12:08 -030012#include <media/tuner-types.h>
Michael Krufky4adad282007-08-27 21:59:08 -030013#include "tuner-i2c.h"
14#include "tuner-simple.h"
15
Michael Krufky9ba0a3c02008-04-22 14:41:44 -030016static int debug;
Michael Krufky4adad282007-08-27 21:59:08 -030017module_param(debug, int, 0644);
18MODULE_PARM_DESC(debug, "enable verbose debug messages");
19
Michael Krufky9ba0a3c02008-04-22 14:41:44 -030020static int offset;
Mauro Carvalho Chehab4d988162006-08-12 21:59:19 -030021module_param(offset, int, 0664);
Michael Krufky9ba0a3c02008-04-22 14:41:44 -030022MODULE_PARM_DESC(offset, "Allows to specify an offset for tuner");
Mauro Carvalho Chehab5c07db02006-01-09 15:25:11 -020023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/* ---------------------------------------------------------------------- */
25
26/* tv standard selection for Temic 4046 FM5
27 this value takes the low bits of control byte 2
28 from datasheet Rev.01, Feb.00
29 standard BG I L L2 D
30 picture IF 38.9 38.9 38.9 33.95 38.9
31 sound 1 33.4 32.9 32.4 40.45 32.4
32 sound 2 33.16
33 NICAM 33.05 32.348 33.05 33.05
34 */
35#define TEMIC_SET_PAL_I 0x05
36#define TEMIC_SET_PAL_DK 0x09
Michael Krufky9ba0a3c02008-04-22 14:41:44 -030037#define TEMIC_SET_PAL_L 0x0a /* SECAM ? */
38#define TEMIC_SET_PAL_L2 0x0b /* change IF ! */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define TEMIC_SET_PAL_BG 0x0c
40
41/* tv tuner system standard selection for Philips FQ1216ME
42 this value takes the low bits of control byte 2
43 from datasheet "1999 Nov 16" (supersedes "1999 Mar 23")
44 standard BG DK I L L`
45 picture carrier 38.90 38.90 38.90 38.90 33.95
46 colour 34.47 34.47 34.47 34.47 38.38
47 sound 1 33.40 32.40 32.90 32.40 40.45
48 sound 2 33.16 - - - -
49 NICAM 33.05 33.05 32.35 33.05 39.80
50 */
51#define PHILIPS_SET_PAL_I 0x01 /* Bit 2 always zero !*/
52#define PHILIPS_SET_PAL_BGDK 0x09
53#define PHILIPS_SET_PAL_L2 0x0a
54#define PHILIPS_SET_PAL_L 0x0b
55
56/* system switching for Philips FI1216MF MK2
57 from datasheet "1996 Jul 09",
58 standard BG L L'
59 picture carrier 38.90 38.90 33.95
60 colour 34.47 34.37 38.38
61 sound 1 33.40 32.40 40.45
62 sound 2 33.16 - -
63 NICAM 33.05 33.05 39.80
64 */
Mauro Carvalho Chehabc57032d2007-05-21 11:39:21 -030065#define PHILIPS_MF_SET_STD_BG 0x01 /* Bit 2 must be zero, Bit 3 is system output */
66#define PHILIPS_MF_SET_STD_L 0x03 /* Used on Secam France */
67#define PHILIPS_MF_SET_STD_LC 0x02 /* Used on SECAM L' */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Mauro Carvalho Chehabf7ce3cc2005-07-12 13:58:55 -070069/* Control byte */
70
71#define TUNER_RATIO_MASK 0x06 /* Bit cb1:cb2 */
72#define TUNER_RATIO_SELECT_50 0x00
73#define TUNER_RATIO_SELECT_32 0x02
74#define TUNER_RATIO_SELECT_166 0x04
75#define TUNER_RATIO_SELECT_62 0x06
76
77#define TUNER_CHARGE_PUMP 0x40 /* Bit cb6 */
78
79/* Status byte */
80
81#define TUNER_POR 0x80
82#define TUNER_FL 0x40
83#define TUNER_MODE 0x38
84#define TUNER_AFC 0x07
85#define TUNER_SIGNAL 0x07
86#define TUNER_STEREO 0x10
87
88#define TUNER_PLL_LOCKED 0x40
89#define TUNER_STEREO_MK3 0x04
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Michael Krufky62325492008-04-22 14:45:52 -030091static DEFINE_MUTEX(tuner_simple_list_mutex);
92static LIST_HEAD(hybrid_tuner_instance_list);
93
Michael Krufky6b1dde92007-08-11 15:42:12 -030094struct tuner_simple_priv {
95 u16 last_div;
Michael Krufky62325492008-04-22 14:45:52 -030096
Michael Krufkydb8a6952007-08-21 01:24:42 -030097 struct tuner_i2c_props i2c_props;
Michael Krufky62325492008-04-22 14:45:52 -030098 struct list_head hybrid_tuner_instance_list;
Michael Krufky4adad282007-08-27 21:59:08 -030099
100 unsigned int type;
101 struct tunertype *tun;
102
103 u32 frequency;
Michael Krufky62325492008-04-22 14:45:52 -0300104 u32 bandwidth;
Michael Krufky6b1dde92007-08-11 15:42:12 -0300105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* ---------------------------------------------------------------------- */
108
Michael Krufky735f0b92007-08-31 16:39:39 -0300109static int tuner_read_status(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Michael Krufky4adad282007-08-27 21:59:08 -0300111 struct tuner_simple_priv *priv = fe->tuner_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned char byte;
113
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300114 if (1 != tuner_i2c_xfer_recv(&priv->i2c_props, &byte, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return byte;
118}
119
Michael Krufky735f0b92007-08-31 16:39:39 -0300120static inline int tuner_signal(const int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Michael Krufky735f0b92007-08-31 16:39:39 -0300122 return (status & TUNER_SIGNAL) << 13;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Michael Krufky735f0b92007-08-31 16:39:39 -0300125static inline int tuner_stereo(const int type, const int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Michael Krufky735f0b92007-08-31 16:39:39 -0300127 switch (type) {
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300128 case TUNER_PHILIPS_FM1216ME_MK3:
129 case TUNER_PHILIPS_FM1236_MK3:
130 case TUNER_PHILIPS_FM1256_IH3:
131 case TUNER_LG_NTSC_TAPE:
132 return ((status & TUNER_SIGNAL) == TUNER_STEREO_MK3);
133 default:
134 return status & TUNER_STEREO;
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700135 }
Michael Krufky735f0b92007-08-31 16:39:39 -0300136}
Mauro Carvalho Chehab56fc08c2005-06-23 22:05:07 -0700137
Michael Krufky735f0b92007-08-31 16:39:39 -0300138static inline int tuner_islocked(const int status)
139{
140 return (status & TUNER_FL);
141}
142
143static inline int tuner_afcstatus(const int status)
144{
145 return (status & TUNER_AFC) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Michael Krufky4adad282007-08-27 21:59:08 -0300149static int simple_get_status(struct dvb_frontend *fe, u32 *status)
150{
151 struct tuner_simple_priv *priv = fe->tuner_priv;
Michael Krufky735f0b92007-08-31 16:39:39 -0300152 int tuner_status = tuner_read_status(fe);
Michael Krufky4adad282007-08-27 21:59:08 -0300153
154 *status = 0;
155
Michael Krufky735f0b92007-08-31 16:39:39 -0300156 if (tuner_islocked(tuner_status))
Michael Krufky4adad282007-08-27 21:59:08 -0300157 *status = TUNER_STATUS_LOCKED;
Michael Krufky735f0b92007-08-31 16:39:39 -0300158 if (tuner_stereo(priv->type, tuner_status))
Michael Krufky4adad282007-08-27 21:59:08 -0300159 *status |= TUNER_STATUS_STEREO;
160
Michael Krufky735f0b92007-08-31 16:39:39 -0300161 tuner_dbg("AFC Status: %d\n", tuner_afcstatus(tuner_status));
162
163 return 0;
164}
165
166static int simple_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
167{
168 struct tuner_simple_priv *priv = fe->tuner_priv;
169 int signal = tuner_signal(tuner_read_status(fe));
170
171 *strength = signal;
172
173 tuner_dbg("Signal strength: %d\n", signal);
Michael Krufky4adad282007-08-27 21:59:08 -0300174
175 return 0;
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* ---------------------------------------------------------------------- */
179
Michael Krufkybe71f7d2008-04-22 14:41:48 -0300180static inline char *tuner_param_name(enum param_type type)
181{
182 char *name;
183
184 switch (type) {
185 case TUNER_PARAM_TYPE_RADIO:
186 name = "radio";
187 break;
188 case TUNER_PARAM_TYPE_PAL:
189 name = "pal";
190 break;
191 case TUNER_PARAM_TYPE_SECAM:
192 name = "secam";
193 break;
194 case TUNER_PARAM_TYPE_NTSC:
195 name = "ntsc";
196 break;
Michael Krufky62325492008-04-22 14:45:52 -0300197 case TUNER_PARAM_TYPE_DIGITAL:
198 name = "digital";
199 break;
Michael Krufkybe71f7d2008-04-22 14:41:48 -0300200 default:
201 name = "unknown";
202 break;
203 }
204 return name;
205}
206
207static struct tuner_params *simple_tuner_params(struct dvb_frontend *fe,
208 enum param_type desired_type)
209{
210 struct tuner_simple_priv *priv = fe->tuner_priv;
211 struct tunertype *tun = priv->tun;
212 int i;
213
214 for (i = 0; i < tun->count; i++)
215 if (desired_type == tun->params[i].type)
216 break;
217
218 /* use default tuner params if desired_type not available */
219 if (i == tun->count) {
220 tuner_dbg("desired params (%s) undefined for tuner %d\n",
221 tuner_param_name(desired_type), priv->type);
222 i = 0;
223 }
224
225 tuner_dbg("using tuner params #%d (%s)\n", i,
226 tuner_param_name(tun->params[i].type));
227
228 return &tun->params[i];
229}
230
231static int simple_config_lookup(struct dvb_frontend *fe,
232 struct tuner_params *t_params,
233 int *frequency, u8 *config, u8 *cb)
234{
235 struct tuner_simple_priv *priv = fe->tuner_priv;
236 int i;
237
238 for (i = 0; i < t_params->count; i++) {
239 if (*frequency > t_params->ranges[i].limit)
240 continue;
241 break;
242 }
243 if (i == t_params->count) {
244 tuner_dbg("frequency out of range (%d > %d)\n",
245 *frequency, t_params->ranges[i - 1].limit);
246 *frequency = t_params->ranges[--i].limit;
247 }
248 *config = t_params->ranges[i].config;
249 *cb = t_params->ranges[i].cb;
250
Michael Krufky7f8447d2008-04-22 14:41:49 -0300251 tuner_dbg("freq = %d.%02d (%d), range = %d, "
252 "config = 0x%02x, cb = 0x%02x\n",
253 *frequency / 16, *frequency % 16 * 100 / 16, *frequency,
254 i, *config, *cb);
Michael Krufkybe71f7d2008-04-22 14:41:48 -0300255
256 return i;
257}
258
259/* ---------------------------------------------------------------------- */
260
Michael Krufkyc7a9f3a2008-04-22 14:41:51 -0300261static int simple_std_setup(struct dvb_frontend *fe,
262 struct analog_parameters *params,
Mauro Carvalho Chehabf13613a2008-04-22 14:42:13 -0300263 u8 *config, u8 *cb)
Michael Krufkyc7a9f3a2008-04-22 14:41:51 -0300264{
265 struct tuner_simple_priv *priv = fe->tuner_priv;
266 u8 tuneraddr;
267 int rc;
268
269 /* tv norm specific stuff for multi-norm tuners */
270 switch (priv->type) {
271 case TUNER_PHILIPS_SECAM: /* FI1216MF */
272 /* 0x01 -> ??? no change ??? */
273 /* 0x02 -> PAL BDGHI / SECAM L */
274 /* 0x04 -> ??? PAL others / SECAM others ??? */
275 *cb &= ~0x03;
276 if (params->std & V4L2_STD_SECAM_L)
277 /* also valid for V4L2_STD_SECAM */
278 *cb |= PHILIPS_MF_SET_STD_L;
279 else if (params->std & V4L2_STD_SECAM_LC)
280 *cb |= PHILIPS_MF_SET_STD_LC;
281 else /* V4L2_STD_B|V4L2_STD_GH */
282 *cb |= PHILIPS_MF_SET_STD_BG;
283 break;
284
285 case TUNER_TEMIC_4046FM5:
286 *cb &= ~0x0f;
287
288 if (params->std & V4L2_STD_PAL_BG) {
289 *cb |= TEMIC_SET_PAL_BG;
290
291 } else if (params->std & V4L2_STD_PAL_I) {
292 *cb |= TEMIC_SET_PAL_I;
293
294 } else if (params->std & V4L2_STD_PAL_DK) {
295 *cb |= TEMIC_SET_PAL_DK;
296
297 } else if (params->std & V4L2_STD_SECAM_L) {
298 *cb |= TEMIC_SET_PAL_L;
299
300 }
301 break;
302
303 case TUNER_PHILIPS_FQ1216ME:
304 *cb &= ~0x0f;
305
306 if (params->std & (V4L2_STD_PAL_BG|V4L2_STD_PAL_DK)) {
307 *cb |= PHILIPS_SET_PAL_BGDK;
308
309 } else if (params->std & V4L2_STD_PAL_I) {
310 *cb |= PHILIPS_SET_PAL_I;
311
312 } else if (params->std & V4L2_STD_SECAM_L) {
313 *cb |= PHILIPS_SET_PAL_L;
314
315 }
316 break;
317
318 case TUNER_PHILIPS_ATSC:
319 /* 0x00 -> ATSC antenna input 1 */
320 /* 0x01 -> ATSC antenna input 2 */
321 /* 0x02 -> NTSC antenna input 1 */
322 /* 0x03 -> NTSC antenna input 2 */
323 *cb &= ~0x03;
324 if (!(params->std & V4L2_STD_ATSC))
325 *cb |= 2;
326 /* FIXME: input */
327 break;
328
329 case TUNER_MICROTUNE_4042FI5:
330 /* Set the charge pump for fast tuning */
331 *config |= TUNER_CHARGE_PUMP;
332 break;
333
334 case TUNER_PHILIPS_TUV1236D:
Mauro Carvalho Chehabf13613a2008-04-22 14:42:13 -0300335 {
Michael Krufkyc7a9f3a2008-04-22 14:41:51 -0300336 /* 0x40 -> ATSC antenna input 1 */
337 /* 0x48 -> ATSC antenna input 2 */
338 /* 0x00 -> NTSC antenna input 1 */
339 /* 0x08 -> NTSC antenna input 2 */
Mauro Carvalho Chehabf13613a2008-04-22 14:42:13 -0300340 u8 buffer[4] = { 0x14, 0x00, 0x17, 0x00};
Michael Krufkyc7a9f3a2008-04-22 14:41:51 -0300341 *cb &= ~0x40;
342 if (params->std & V4L2_STD_ATSC) {
343 *cb |= 0x40;
344 buffer[1] = 0x04;
345 }
346 /* set to the correct mode (analog or digital) */
347 tuneraddr = priv->i2c_props.addr;
348 priv->i2c_props.addr = 0x0a;
349 rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[0], 2);
350 if (2 != rc)
351 tuner_warn("i2c i/o error: rc == %d "
352 "(should be 2)\n", rc);
353 rc = tuner_i2c_xfer_send(&priv->i2c_props, &buffer[2], 2);
354 if (2 != rc)
355 tuner_warn("i2c i/o error: rc == %d "
356 "(should be 2)\n", rc);
357 priv->i2c_props.addr = tuneraddr;
358 /* FIXME: input */
359 break;
360 }
Mauro Carvalho Chehabf13613a2008-04-22 14:42:13 -0300361 }
Michael Krufkyc7a9f3a2008-04-22 14:41:51 -0300362
363 return 0;
364}
365
366static int simple_post_tune(struct dvb_frontend *fe, u8 *buffer,
367 u16 div, u8 config, u8 cb)
368{
369 struct tuner_simple_priv *priv = fe->tuner_priv;
370 int rc;
371
372 switch (priv->type) {
373 case TUNER_LG_TDVS_H06XF:
374 /* Set the Auxiliary Byte. */
375 buffer[0] = buffer[2];
376 buffer[0] &= ~0x20;
377 buffer[0] |= 0x18;
378 buffer[1] = 0x20;
379 tuner_dbg("tv 0x%02x 0x%02x\n", buffer[0], buffer[1]);
380
381 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 2);
382 if (2 != rc)
383 tuner_warn("i2c i/o error: rc == %d "
384 "(should be 2)\n", rc);
385 break;
386 case TUNER_MICROTUNE_4042FI5:
387 {
388 /* FIXME - this may also work for other tuners */
389 unsigned long timeout = jiffies + msecs_to_jiffies(1);
390 u8 status_byte = 0;
391
392 /* Wait until the PLL locks */
393 for (;;) {
394 if (time_after(jiffies, timeout))
395 return 0;
396 rc = tuner_i2c_xfer_recv(&priv->i2c_props,
397 &status_byte, 1);
398 if (1 != rc) {
399 tuner_warn("i2c i/o read error: rc == %d "
400 "(should be 1)\n", rc);
401 break;
402 }
403 if (status_byte & TUNER_PLL_LOCKED)
404 break;
405 udelay(10);
406 }
407
408 /* Set the charge pump for optimized phase noise figure */
409 config &= ~TUNER_CHARGE_PUMP;
410 buffer[0] = (div>>8) & 0x7f;
411 buffer[1] = div & 0xff;
412 buffer[2] = config;
413 buffer[3] = cb;
414 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
415 buffer[0], buffer[1], buffer[2], buffer[3]);
416
417 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
418 if (4 != rc)
419 tuner_warn("i2c i/o error: rc == %d "
420 "(should be 4)\n", rc);
421 break;
422 }
423 }
424
425 return 0;
426}
427
428static int simple_radio_bandswitch(struct dvb_frontend *fe, u8 *buffer)
429{
430 struct tuner_simple_priv *priv = fe->tuner_priv;
431
432 switch (priv->type) {
433 case TUNER_TENA_9533_DI:
434 case TUNER_YMEC_TVF_5533MF:
435 tuner_dbg("This tuner doesn't have FM. "
436 "Most cards have a TEA5767 for FM\n");
437 return 0;
438 case TUNER_PHILIPS_FM1216ME_MK3:
439 case TUNER_PHILIPS_FM1236_MK3:
440 case TUNER_PHILIPS_FMD1216ME_MK3:
441 case TUNER_LG_NTSC_TAPE:
442 case TUNER_PHILIPS_FM1256_IH3:
443 buffer[3] = 0x19;
444 break;
445 case TUNER_TNF_5335MF:
446 buffer[3] = 0x11;
447 break;
448 case TUNER_LG_PAL_FM:
449 buffer[3] = 0xa5;
450 break;
451 case TUNER_THOMSON_DTT761X:
452 buffer[3] = 0x39;
453 break;
454 case TUNER_MICROTUNE_4049FM5:
455 default:
456 buffer[3] = 0xa4;
457 break;
458 }
459
460 return 0;
461}
462
463/* ---------------------------------------------------------------------- */
464
Michael Krufky4adad282007-08-27 21:59:08 -0300465static int simple_set_tv_freq(struct dvb_frontend *fe,
466 struct analog_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Michael Krufky4adad282007-08-27 21:59:08 -0300468 struct tuner_simple_priv *priv = fe->tuner_priv;
Michael Krufkyc7a9f3a2008-04-22 14:41:51 -0300469 u8 config, cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 u16 div;
471 struct tunertype *tun;
Hans Verkuil27487d42006-01-15 15:04:52 -0200472 u8 buffer[4];
Michael Krufkybe71f7d2008-04-22 14:41:48 -0300473 int rc, IFPCoff, i;
Michael Krufky476d63d2006-02-07 06:25:34 -0200474 enum param_type desired_type;
Michael Krufky4adad282007-08-27 21:59:08 -0300475 struct tuner_params *t_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Michael Krufky4adad282007-08-27 21:59:08 -0300477 tun = priv->tun;
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200478
Michael Krufky5f594182006-02-07 06:25:33 -0200479 /* IFPCoff = Video Intermediate Frequency - Vif:
480 940 =16*58.75 NTSC/J (Japan)
481 732 =16*45.75 M/N STD
482 704 =16*44 ATSC (at DVB code)
483 632 =16*39.50 I U.K.
484 622.4=16*38.90 B/G D/K I, L STD
485 592 =16*37.00 D China
486 590 =16.36.875 B Australia
487 543.2=16*33.95 L' STD
488 171.2=16*10.70 FM Radio (at set_radio_freq)
489 */
490
Michael Krufky4adad282007-08-27 21:59:08 -0300491 if (params->std == V4L2_STD_NTSC_M_JP) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200492 IFPCoff = 940;
493 desired_type = TUNER_PARAM_TYPE_NTSC;
Michael Krufky4adad282007-08-27 21:59:08 -0300494 } else if ((params->std & V4L2_STD_MN) &&
495 !(params->std & ~V4L2_STD_MN)) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200496 IFPCoff = 732;
497 desired_type = TUNER_PARAM_TYPE_NTSC;
Michael Krufky4adad282007-08-27 21:59:08 -0300498 } else if (params->std == V4L2_STD_SECAM_LC) {
Michael Krufky476d63d2006-02-07 06:25:34 -0200499 IFPCoff = 543;
500 desired_type = TUNER_PARAM_TYPE_SECAM;
Michael Krufky5f594182006-02-07 06:25:33 -0200501 } else {
Michael Krufky476d63d2006-02-07 06:25:34 -0200502 IFPCoff = 623;
503 desired_type = TUNER_PARAM_TYPE_PAL;
Michael Krufky5f594182006-02-07 06:25:33 -0200504 }
505
Michael Krufkybe71f7d2008-04-22 14:41:48 -0300506 t_params = simple_tuner_params(fe, desired_type);
Michael Krufky8f1a58d2006-02-07 06:25:39 -0200507
Michael Krufkybe71f7d2008-04-22 14:41:48 -0300508 i = simple_config_lookup(fe, t_params, &params->frequency,
509 &config, &cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300511 div = params->frequency + IFPCoff + offset;
Michael Krufky5f594182006-02-07 06:25:33 -0200512
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300513 tuner_dbg("Freq= %d.%02d MHz, V_IF=%d.%02d MHz, "
514 "Offset=%d.%02d MHz, div=%0d\n",
515 params->frequency / 16, params->frequency % 16 * 100 / 16,
516 IFPCoff / 16, IFPCoff % 16 * 100 / 16,
517 offset / 16, offset % 16 * 100 / 16, div);
Michael Krufky5f594182006-02-07 06:25:33 -0200518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* tv norm specific stuff for multi-norm tuners */
Mauro Carvalho Chehabf13613a2008-04-22 14:42:13 -0300520 simple_std_setup(fe, params, &config, &cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Michael Krufky4adad282007-08-27 21:59:08 -0300522 if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
Michael Krufky3fc46d32006-01-23 17:11:11 -0200523 buffer[0] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200524 buffer[1] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 buffer[2] = (div>>8) & 0x7f;
526 buffer[3] = div & 0xff;
527 } else {
528 buffer[0] = (div>>8) & 0x7f;
529 buffer[1] = div & 0xff;
Michael Krufky3fc46d32006-01-23 17:11:11 -0200530 buffer[2] = config;
Michael Krufkyab66b222006-01-23 17:11:11 -0200531 buffer[3] = cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Michael Krufky6b1dde92007-08-11 15:42:12 -0300533 priv->last_div = div;
Michael Krufky4adad282007-08-27 21:59:08 -0300534 if (t_params->has_tda9887) {
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300535 struct v4l2_priv_tun_config tda9887_cfg;
Hans Verkuilba8fc392006-06-25 15:34:39 -0300536 int config = 0;
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300537 int is_secam_l = (params->std & (V4L2_STD_SECAM_L |
538 V4L2_STD_SECAM_LC)) &&
539 !(params->std & ~(V4L2_STD_SECAM_L |
540 V4L2_STD_SECAM_LC));
Hans Verkuilba8fc392006-06-25 15:34:39 -0300541
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300542 tda9887_cfg.tuner = TUNER_TDA9887;
543 tda9887_cfg.priv = &config;
544
Michael Krufky4adad282007-08-27 21:59:08 -0300545 if (params->std == V4L2_STD_SECAM_LC) {
546 if (t_params->port1_active ^ t_params->port1_invert_for_secam_lc)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300547 config |= TDA9887_PORT1_ACTIVE;
Michael Krufky4adad282007-08-27 21:59:08 -0300548 if (t_params->port2_active ^ t_params->port2_invert_for_secam_lc)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300549 config |= TDA9887_PORT2_ACTIVE;
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300550 } else {
Michael Krufky4adad282007-08-27 21:59:08 -0300551 if (t_params->port1_active)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300552 config |= TDA9887_PORT1_ACTIVE;
Michael Krufky4adad282007-08-27 21:59:08 -0300553 if (t_params->port2_active)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300554 config |= TDA9887_PORT2_ACTIVE;
555 }
Michael Krufky4adad282007-08-27 21:59:08 -0300556 if (t_params->intercarrier_mode)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300557 config |= TDA9887_INTERCARRIER;
558 if (is_secam_l) {
Michael Krufky4adad282007-08-27 21:59:08 -0300559 if (i == 0 && t_params->default_top_secam_low)
560 config |= TDA9887_TOP(t_params->default_top_secam_low);
561 else if (i == 1 && t_params->default_top_secam_mid)
562 config |= TDA9887_TOP(t_params->default_top_secam_mid);
563 else if (t_params->default_top_secam_high)
564 config |= TDA9887_TOP(t_params->default_top_secam_high);
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300565 } else {
Michael Krufky4adad282007-08-27 21:59:08 -0300566 if (i == 0 && t_params->default_top_low)
567 config |= TDA9887_TOP(t_params->default_top_low);
568 else if (i == 1 && t_params->default_top_mid)
569 config |= TDA9887_TOP(t_params->default_top_mid);
570 else if (t_params->default_top_high)
571 config |= TDA9887_TOP(t_params->default_top_high);
Hans Verkuilba8fc392006-06-25 15:34:39 -0300572 }
Michael Krufky4adad282007-08-27 21:59:08 -0300573 if (t_params->default_pll_gating_18)
Trent Piephod7304de2006-08-24 22:43:45 -0300574 config |= TDA9887_GATING_18;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300575 i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
576 &tda9887_cfg);
Hans Verkuilba8fc392006-06-25 15:34:39 -0300577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 tuner_dbg("tv 0x%02x 0x%02x 0x%02x 0x%02x\n",
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300579 buffer[0], buffer[1], buffer[2], buffer[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300581 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
582 if (4 != rc)
583 tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Michael Krufkyc7a9f3a2008-04-22 14:41:51 -0300585 simple_post_tune(fe, &buffer[0], div, config, cb);
Michael Krufkyd9cd2d92006-07-17 17:15:26 -0300586
Michael Krufky4adad282007-08-27 21:59:08 -0300587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Michael Krufky4adad282007-08-27 21:59:08 -0300590static int simple_set_radio_freq(struct dvb_frontend *fe,
591 struct analog_parameters *params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct tunertype *tun;
Michael Krufky4adad282007-08-27 21:59:08 -0300594 struct tuner_simple_priv *priv = fe->tuner_priv;
Hans Verkuil27487d42006-01-15 15:04:52 -0200595 u8 buffer[4];
596 u16 div;
Michael Krufky7b0ac9c2006-01-13 14:10:25 -0200597 int rc, j;
Michael Krufky4adad282007-08-27 21:59:08 -0300598 struct tuner_params *t_params;
599 unsigned int freq = params->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Michael Krufky4adad282007-08-27 21:59:08 -0300601 tun = priv->tun;
Michael Krufky476d63d2006-02-07 06:25:34 -0200602
Trent Piepho5e082f12007-08-03 18:32:38 -0300603 for (j = tun->count-1; j > 0; j--)
604 if (tun->params[j].type == TUNER_PARAM_TYPE_RADIO)
605 break;
Michael Krufky4adad282007-08-27 21:59:08 -0300606 /* default t_params (j=0) will be used if desired type wasn't found */
607 t_params = &tun->params[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Trent Piepho5e082f12007-08-03 18:32:38 -0300609 /* Select Radio 1st IF used */
Michael Krufky4adad282007-08-27 21:59:08 -0300610 switch (t_params->radio_if) {
Trent Piepho5e082f12007-08-03 18:32:38 -0300611 case 0: /* 10.7 MHz */
612 freq += (unsigned int)(10.7*16000);
613 break;
614 case 1: /* 33.3 MHz */
615 freq += (unsigned int)(33.3*16000);
616 break;
617 case 2: /* 41.3 MHz */
618 freq += (unsigned int)(41.3*16000);
619 break;
620 default:
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300621 tuner_warn("Unsupported radio_if value %d\n",
622 t_params->radio_if);
Michael Krufky4adad282007-08-27 21:59:08 -0300623 return 0;
Trent Piepho5e082f12007-08-03 18:32:38 -0300624 }
625
626 /* Bandswitch byte */
Michael Krufkyc7a9f3a2008-04-22 14:41:51 -0300627 simple_radio_bandswitch(fe, &buffer[0]);
Trent Piepho5e082f12007-08-03 18:32:38 -0300628
Michael Krufky4adad282007-08-27 21:59:08 -0300629 buffer[2] = (t_params->ranges[0].config & ~TUNER_RATIO_MASK) |
Trent Piepho5e082f12007-08-03 18:32:38 -0300630 TUNER_RATIO_SELECT_50; /* 50 kHz step */
631
632 /* Convert from 1/16 kHz V4L steps to 1/20 MHz (=50 kHz) PLL steps
633 freq * (1 Mhz / 16000 V4L steps) * (20 PLL steps / 1 MHz) =
634 freq * (1/800) */
635 div = (freq + 400) / 800;
636
Michael Krufky4adad282007-08-27 21:59:08 -0300637 if (t_params->cb_first_if_lower_freq && div < priv->last_div) {
Hans Verkuil27487d42006-01-15 15:04:52 -0200638 buffer[0] = buffer[2];
639 buffer[1] = buffer[3];
640 buffer[2] = (div>>8) & 0x7f;
641 buffer[3] = div & 0xff;
642 } else {
643 buffer[0] = (div>>8) & 0x7f;
644 buffer[1] = div & 0xff;
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 tuner_dbg("radio 0x%02x 0x%02x 0x%02x 0x%02x\n",
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300648 buffer[0], buffer[1], buffer[2], buffer[3]);
Michael Krufky6b1dde92007-08-11 15:42:12 -0300649 priv->last_div = div;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Michael Krufky4adad282007-08-27 21:59:08 -0300651 if (t_params->has_tda9887) {
Hans Verkuilba8fc392006-06-25 15:34:39 -0300652 int config = 0;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300653 struct v4l2_priv_tun_config tda9887_cfg;
654
655 tda9887_cfg.tuner = TUNER_TDA9887;
656 tda9887_cfg.priv = &config;
657
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300658 if (t_params->port1_active &&
659 !t_params->port1_fm_high_sensitivity)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300660 config |= TDA9887_PORT1_ACTIVE;
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300661 if (t_params->port2_active &&
662 !t_params->port2_fm_high_sensitivity)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300663 config |= TDA9887_PORT2_ACTIVE;
Michael Krufky4adad282007-08-27 21:59:08 -0300664 if (t_params->intercarrier_mode)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300665 config |= TDA9887_INTERCARRIER;
Michael Krufky4adad282007-08-27 21:59:08 -0300666/* if (t_params->port1_set_for_fm_mono)
Hans Verkuilba8fc392006-06-25 15:34:39 -0300667 config &= ~TDA9887_PORT1_ACTIVE;*/
Michael Krufky4adad282007-08-27 21:59:08 -0300668 if (t_params->fm_gain_normal)
Mauro Carvalho Chehab483deb02006-12-04 08:31:38 -0300669 config |= TDA9887_GAIN_NORMAL;
Michael Krufky4adad282007-08-27 21:59:08 -0300670 if (t_params->radio_if == 2)
Trent Piepho5e082f12007-08-03 18:32:38 -0300671 config |= TDA9887_RIF_41_3;
Mauro Carvalho Chehab7f171122007-10-18 19:56:47 -0300672 i2c_clients_command(priv->i2c_props.adap, TUNER_SET_CONFIG,
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300673 &tda9887_cfg);
Hans Verkuilba8fc392006-06-25 15:34:39 -0300674 }
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300675 rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 4);
676 if (4 != rc)
677 tuner_warn("i2c i/o error: rc == %d (should be 4)\n", rc);
Michael Krufky4adad282007-08-27 21:59:08 -0300678
679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Michael Krufky4adad282007-08-27 21:59:08 -0300682static int simple_set_params(struct dvb_frontend *fe,
683 struct analog_parameters *params)
Michael Krufky6b1dde92007-08-11 15:42:12 -0300684{
Michael Krufky4adad282007-08-27 21:59:08 -0300685 struct tuner_simple_priv *priv = fe->tuner_priv;
686 int ret = -EINVAL;
687
688 switch (params->mode) {
689 case V4L2_TUNER_RADIO:
690 ret = simple_set_radio_freq(fe, params);
691 priv->frequency = params->frequency * 125 / 2;
692 break;
693 case V4L2_TUNER_ANALOG_TV:
694 case V4L2_TUNER_DIGITAL_TV:
695 ret = simple_set_tv_freq(fe, params);
696 priv->frequency = params->frequency * 62500;
697 break;
698 }
Michael Krufky62325492008-04-22 14:45:52 -0300699 priv->bandwidth = 0;
Michael Krufky4adad282007-08-27 21:59:08 -0300700
701 return ret;
Michael Krufky6b1dde92007-08-11 15:42:12 -0300702}
703
Michael Krufky62325492008-04-22 14:45:52 -0300704static int simple_dvb_configure(struct dvb_frontend *fe, u8 *buf,
705 const struct dvb_frontend_parameters *params)
706{
707 struct tuner_simple_priv *priv = fe->tuner_priv;
708 struct tunertype *tun = priv->tun;
709 static struct tuner_params *t_params;
710 u8 config, cb;
711 u32 div;
712 int ret, frequency = params->frequency / 62500;
713
714 t_params = simple_tuner_params(fe, TUNER_PARAM_TYPE_DIGITAL);
715 ret = simple_config_lookup(fe, t_params, &frequency, &config, &cb);
716 if (ret < 0)
717 return ret;
718
719 div = ((frequency + t_params->iffreq) * 62500 + offset +
720 tun->stepsize/2) / tun->stepsize;
721
722 buf[0] = div >> 8;
723 buf[1] = div & 0xff;
724 buf[2] = config;
725 buf[3] = cb;
726
727 tuner_dbg("%s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
728 tun->name, div, buf[0], buf[1], buf[2], buf[3]);
729
730 /* calculate the frequency we set it to */
731 return (div * tun->stepsize) - t_params->iffreq;
732}
733
734static int simple_dvb_calc_regs(struct dvb_frontend *fe,
735 struct dvb_frontend_parameters *params,
736 u8 *buf, int buf_len)
737{
738 struct tuner_simple_priv *priv = fe->tuner_priv;
739 int ret;
740 u32 frequency;
741
742 if (buf_len < 5)
743 return -EINVAL;
744
745 ret = simple_dvb_configure(fe, buf+1, params);
746 if (ret < 0)
747 return ret;
748 else
749 frequency = ret;
750
751 buf[0] = priv->i2c_props.addr;
752
753 priv->frequency = frequency;
754 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
755 params->u.ofdm.bandwidth : 0;
756
757 return 5;
758}
759
760static int simple_dvb_set_params(struct dvb_frontend *fe,
761 struct dvb_frontend_parameters *params)
762{
763 struct tuner_simple_priv *priv = fe->tuner_priv;
764 u32 prev_freq, prev_bw;
765 int ret;
766 u8 buf[5];
767
768 if (priv->i2c_props.adap == NULL)
769 return -EINVAL;
770
771 prev_freq = priv->frequency;
772 prev_bw = priv->bandwidth;
773
774 ret = simple_dvb_calc_regs(fe, params, buf, 5);
775 if (ret != 5)
776 goto fail;
777
778 /* put analog demod in standby when tuning digital */
779 if (fe->ops.analog_ops.standby)
780 fe->ops.analog_ops.standby(fe);
781
782 if (fe->ops.i2c_gate_ctrl)
783 fe->ops.i2c_gate_ctrl(fe, 1);
784
785 /* buf[0] contains the i2c address, but *
786 * we already have it in i2c_props.addr */
787 ret = tuner_i2c_xfer_send(&priv->i2c_props, buf+1, 4);
788 if (ret != 4)
789 goto fail;
790
791 return 0;
792fail:
793 /* calc_regs sets frequency and bandwidth. if we failed, unset them */
794 priv->frequency = prev_freq;
795 priv->bandwidth = prev_bw;
796
797 return ret;
798}
Michael Krufky4adad282007-08-27 21:59:08 -0300799
800static int simple_release(struct dvb_frontend *fe)
801{
Michael Krufky62325492008-04-22 14:45:52 -0300802 struct tuner_simple_priv *priv = fe->tuner_priv;
803
804 mutex_lock(&tuner_simple_list_mutex);
805
806 if (priv)
807 hybrid_tuner_release_state(priv);
808
809 mutex_unlock(&tuner_simple_list_mutex);
810
Michael Krufky4adad282007-08-27 21:59:08 -0300811 fe->tuner_priv = NULL;
812
813 return 0;
814}
815
816static int simple_get_frequency(struct dvb_frontend *fe, u32 *frequency)
817{
818 struct tuner_simple_priv *priv = fe->tuner_priv;
819 *frequency = priv->frequency;
820 return 0;
821}
822
Michael Krufky62325492008-04-22 14:45:52 -0300823static int simple_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
824{
825 struct tuner_simple_priv *priv = fe->tuner_priv;
826 *bandwidth = priv->bandwidth;
827 return 0;
828}
829
Michael Krufky4adad282007-08-27 21:59:08 -0300830static struct dvb_tuner_ops simple_tuner_ops = {
831 .set_analog_params = simple_set_params,
Michael Krufky62325492008-04-22 14:45:52 -0300832 .set_params = simple_dvb_set_params,
833 .calc_regs = simple_dvb_calc_regs,
Michael Krufky735f0b92007-08-31 16:39:39 -0300834 .release = simple_release,
835 .get_frequency = simple_get_frequency,
Michael Krufky62325492008-04-22 14:45:52 -0300836 .get_bandwidth = simple_get_bandwidth,
Michael Krufky735f0b92007-08-31 16:39:39 -0300837 .get_status = simple_get_status,
838 .get_rf_strength = simple_get_rf_strength,
Michael Krufky5d807c92007-06-06 16:17:57 -0300839};
840
Michael Krufky4adad282007-08-27 21:59:08 -0300841struct dvb_frontend *simple_tuner_attach(struct dvb_frontend *fe,
842 struct i2c_adapter *i2c_adap,
843 u8 i2c_addr,
Michael Krufky060a5bd2008-04-22 14:41:51 -0300844 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Michael Krufky6b1dde92007-08-11 15:42:12 -0300846 struct tuner_simple_priv *priv = NULL;
Michael Krufky62325492008-04-22 14:45:52 -0300847 int instance;
Michael Krufky6b1dde92007-08-11 15:42:12 -0300848
Michael Krufky65e8d292008-04-22 14:41:50 -0300849 if (type >= tuner_count) {
850 printk(KERN_WARNING "%s: invalid tuner type: %d (max: %d)\n",
851 __FUNCTION__, type, tuner_count-1);
852 return NULL;
853 }
854
Michael Krufky62325492008-04-22 14:45:52 -0300855 mutex_lock(&tuner_simple_list_mutex);
856
857 instance = hybrid_tuner_request_state(struct tuner_simple_priv, priv,
858 hybrid_tuner_instance_list,
859 i2c_adap, i2c_addr,
860 "tuner-simple");
861 switch (instance) {
862 case 0:
863 mutex_unlock(&tuner_simple_list_mutex);
Michael Krufky4adad282007-08-27 21:59:08 -0300864 return NULL;
Michael Krufky62325492008-04-22 14:45:52 -0300865 break;
866 case 1:
867 fe->tuner_priv = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Michael Krufky62325492008-04-22 14:45:52 -0300869 priv->type = type;
870 priv->tun = &tuners[type];
871 break;
872 default:
873 fe->tuner_priv = priv;
874 break;
875 }
Michael Krufky27566652008-04-22 14:41:53 -0300876
Michael Krufky62325492008-04-22 14:45:52 -0300877 mutex_unlock(&tuner_simple_list_mutex);
Michael Krufkydb8a6952007-08-21 01:24:42 -0300878
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300879 memcpy(&fe->ops.tuner_ops, &simple_tuner_ops,
880 sizeof(struct dvb_tuner_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Michael Krufky62325492008-04-22 14:45:52 -0300882 tuner_info("type set to %d (%s)\n", type, priv->tun->name);
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700883
Michael Krufky060a5bd2008-04-22 14:41:51 -0300884 strlcpy(fe->ops.tuner_ops.info.name, priv->tun->name,
Michael Krufky9ba0a3c02008-04-22 14:41:44 -0300885 sizeof(fe->ops.tuner_ops.info.name));
Michael Krufky4adad282007-08-27 21:59:08 -0300886
887 return fe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
Michael Krufky4adad282007-08-27 21:59:08 -0300889EXPORT_SYMBOL_GPL(simple_tuner_attach);
890
891MODULE_DESCRIPTION("Simple 4-control-bytes style tuner driver");
892MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
893MODULE_LICENSE("GPL");
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/*
896 * Overrides for Emacs so that we follow Linus's tabbing style.
897 * ---------------------------------------------------------------------------
898 * Local variables:
899 * c-basic-offset: 8
900 * End:
901 */