]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/tuner-core.c
V4L/DVB (6429): Avoid having two tuner commands happening at the same time
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / tuner-core.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 *
3 * i2c tv tuner chip device driver
4 * core core, i.e. kernel interfaces, registering and so on
5 */
6
7#include <linux/module.h>
1da177e4 8#include <linux/kernel.h>
1da177e4
LT
9#include <linux/string.h>
10#include <linux/timer.h>
11#include <linux/delay.h>
12#include <linux/errno.h>
13#include <linux/slab.h>
14#include <linux/poll.h>
15#include <linux/i2c.h>
16#include <linux/types.h>
1da177e4 17#include <linux/init.h>
ffbb807c 18#include <linux/videodev.h>
1da177e4 19#include <media/tuner.h>
4adad287 20#include <media/tuner-types.h>
5e453dc7 21#include <media/v4l2-common.h>
8218b0b2 22#include "tuner-driver.h"
96c0b7cf 23#include "mt20xx.h"
910bb3e3 24#include "tda8290.h"
7ab10bf7 25#include "tea5761.h"
8d0936ed 26#include "tea5767.h"
4adad287 27#include "tuner-simple.h"
1da177e4
LT
28
29#define UNSET (-1U)
30
31/* standard i2c insmod options */
32static unsigned short normal_i2c[] = {
04d934ff 33#if defined(CONFIG_TUNER_TEA5761) || (defined(CONFIG_TUNER_TEA5761_MODULE) && defined(MODULE))
8573a9e6
MCC
34 0x10,
35#endif
de48eebc 36 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
f5bec396
MCC
37 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
38 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
1da177e4
LT
39 I2C_CLIENT_END
40};
f7ce3cc6 41
1da177e4
LT
42I2C_CLIENT_INSMOD;
43
44/* insmod options used at init time => read/only */
f7ce3cc6 45static unsigned int addr = 0;
c5287ba1 46static unsigned int no_autodetect = 0;
fd3113e8 47static unsigned int show_i2c = 0;
fd3113e8 48
1da177e4 49/* insmod options used at runtime => read/write */
f9195ded 50int tuner_debug = 0;
1da177e4 51
f7ce3cc6 52static unsigned int tv_range[2] = { 44, 958 };
1da177e4
LT
53static unsigned int radio_range[2] = { 65, 108 };
54
7e578191
MCC
55static char pal[] = "--";
56static char secam[] = "--";
57static char ntsc[] = "-";
58
f9195ded 59
7e578191
MCC
60module_param(addr, int, 0444);
61module_param(no_autodetect, int, 0444);
62module_param(show_i2c, int, 0444);
f9195ded 63module_param_named(debug,tuner_debug, int, 0644);
7e578191
MCC
64module_param_string(pal, pal, sizeof(pal), 0644);
65module_param_string(secam, secam, sizeof(secam), 0644);
66module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
f7ce3cc6 67module_param_array(tv_range, int, NULL, 0644);
1da177e4
LT
68module_param_array(radio_range, int, NULL, 0644);
69
70MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
71MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
72MODULE_LICENSE("GPL");
73
1da177e4
LT
74static struct i2c_driver driver;
75static struct i2c_client client_template;
76
77/* ---------------------------------------------------------------------- */
78
e18f9444
MK
79static void fe_set_freq(struct tuner *t, unsigned int freq)
80{
81 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
82
83 struct analog_parameters params = {
84 .frequency = freq,
85 .mode = t->mode,
86 .audmode = t->audmode,
87 .std = t->std
88 };
89
90 if (NULL == fe_tuner_ops->set_analog_params) {
91 tuner_warn("Tuner frontend module has no way to set freq\n");
92 return;
93 }
94 fe_tuner_ops->set_analog_params(&t->fe, &params);
95}
96
97static void fe_release(struct tuner *t)
98{
99 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
100
101 if (fe_tuner_ops->release)
102 fe_tuner_ops->release(&t->fe);
103}
104
105static void fe_standby(struct tuner *t)
106{
107 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
108
109 if (fe_tuner_ops->sleep)
110 fe_tuner_ops->sleep(&t->fe);
111}
112
1f5ef197
MK
113static int fe_has_signal(struct tuner *t)
114{
115 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1419683d 116 u16 strength = 0;
1f5ef197
MK
117
118 if (fe_tuner_ops->get_rf_strength)
119 fe_tuner_ops->get_rf_strength(&t->fe, &strength);
120
121 return strength;
122}
123
56fc08ca 124/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
1da177e4
LT
125static void set_tv_freq(struct i2c_client *c, unsigned int freq)
126{
127 struct tuner *t = i2c_get_clientdata(c);
128
129 if (t->type == UNSET) {
f7ce3cc6 130 tuner_warn ("tuner type not set\n");
1da177e4
LT
131 return;
132 }
7a91a80a 133 if (NULL == t->ops.set_tv_freq) {
f7ce3cc6 134 tuner_warn ("Tuner has no way to set tv freq\n");
1da177e4
LT
135 return;
136 }
f7ce3cc6
MCC
137 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
138 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
139 freq / 16, freq % 16 * 100 / 16, tv_range[0],
140 tv_range[1]);
27487d44
HV
141 /* V4L2 spec: if the freq is not possible then the closest
142 possible value should be selected */
143 if (freq < tv_range[0] * 16)
144 freq = tv_range[0] * 16;
145 else
146 freq = tv_range[1] * 16;
1da177e4 147 }
db8a6956 148 t->ops.set_tv_freq(t, freq);
1da177e4
LT
149}
150
151static void set_radio_freq(struct i2c_client *c, unsigned int freq)
152{
153 struct tuner *t = i2c_get_clientdata(c);
154
155 if (t->type == UNSET) {
f7ce3cc6 156 tuner_warn ("tuner type not set\n");
1da177e4
LT
157 return;
158 }
7a91a80a 159 if (NULL == t->ops.set_radio_freq) {
f7ce3cc6 160 tuner_warn ("tuner has no way to set radio frequency\n");
1da177e4
LT
161 return;
162 }
27487d44 163 if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
f7ce3cc6
MCC
164 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
165 freq / 16000, freq % 16000 * 100 / 16000,
166 radio_range[0], radio_range[1]);
27487d44
HV
167 /* V4L2 spec: if the freq is not possible then the closest
168 possible value should be selected */
169 if (freq < radio_range[0] * 16000)
170 freq = radio_range[0] * 16000;
171 else
172 freq = radio_range[1] * 16000;
1da177e4 173 }
586b0cab 174
db8a6956 175 t->ops.set_radio_freq(t, freq);
1da177e4
LT
176}
177
178static void set_freq(struct i2c_client *c, unsigned long freq)
179{
180 struct tuner *t = i2c_get_clientdata(c);
181
182 switch (t->mode) {
183 case V4L2_TUNER_RADIO:
184 tuner_dbg("radio freq set to %lu.%02lu\n",
f7ce3cc6
MCC
185 freq / 16000, freq % 16000 * 100 / 16000);
186 set_radio_freq(c, freq);
27487d44 187 t->radio_freq = freq;
1da177e4
LT
188 break;
189 case V4L2_TUNER_ANALOG_TV:
190 case V4L2_TUNER_DIGITAL_TV:
191 tuner_dbg("tv freq set to %lu.%02lu\n",
f7ce3cc6 192 freq / 16, freq % 16 * 100 / 16);
1da177e4 193 set_tv_freq(c, freq);
27487d44 194 t->tv_freq = freq;
1da177e4 195 break;
6cb45879
MCC
196 default:
197 tuner_dbg("freq set: unknown mode: 0x%04x!\n",t->mode);
1da177e4 198 }
1da177e4
LT
199}
200
293197cd
MK
201static void tuner_i2c_address_check(struct tuner *t)
202{
203 if ((t->type == UNSET || t->type == TUNER_ABSENT) ||
204 ((t->i2c.addr < 0x64) || (t->i2c.addr > 0x6f)))
205 return;
206
207 tuner_warn("====================== WARNING! ======================\n");
208 tuner_warn("Support for tuners in i2c address range 0x64 thru 0x6f\n");
209 tuner_warn("will soon be dropped. This message indicates that your\n");
210 tuner_warn("hardware has a %s tuner at i2c address 0x%02x.\n",
211 t->i2c.name, t->i2c.addr);
212 tuner_warn("To ensure continued support for your device, please\n");
213 tuner_warn("send a copy of this message, along with full dmesg\n");
214 tuner_warn("output to v4l-dvb-maintainer@linuxtv.org\n");
215 tuner_warn("Please use subject line: \"obsolete tuner i2c address.\"\n");
216 tuner_warn("driver: %s, addr: 0x%02x, type: %d (%s)\n",
217 t->i2c.adapter->name, t->i2c.addr, t->type,
218 tuners[t->type].name);
219 tuner_warn("====================== WARNING! ======================\n");
220}
221
910bb3e3
MK
222static void attach_tda8290(struct tuner *t)
223{
224 struct tda8290_config cfg = {
225 .lna_cfg = &t->config,
226 .tuner_callback = t->tuner_callback
227 };
228 tda8290_attach(&t->fe, t->i2c.adapter, t->i2c.addr, &cfg);
229}
230
4adad287
MK
231static void attach_simple_tuner(struct tuner *t)
232{
233 struct simple_tuner_config cfg = {
234 .type = t->type,
235 .tun = &tuners[t->type]
236 };
237 simple_tuner_attach(&t->fe, t->i2c.adapter, t->i2c.addr, &cfg);
238}
239
f7ce3cc6 240static void set_type(struct i2c_client *c, unsigned int type,
de956c1e 241 unsigned int new_mode_mask, unsigned int new_config,
cfeb8839 242 int (*tuner_callback) (void *dev, int command,int arg))
1da177e4
LT
243{
244 struct tuner *t = i2c_get_clientdata(c);
e18f9444 245 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
586b0cab 246 unsigned char buffer[4];
1da177e4 247
f7ce3cc6
MCC
248 if (type == UNSET || type == TUNER_ABSENT) {
249 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
1da177e4 250 return;
f7ce3cc6
MCC
251 }
252
253 if (type >= tuner_count) {
254 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
1da177e4 255 return;
f7ce3cc6 256 }
1da177e4 257
80f90fba
HH
258 t->type = type;
259 t->config = new_config;
260 if (tuner_callback != NULL) {
261 tuner_dbg("defining GPIO callback\n");
262 t->tuner_callback = tuner_callback;
263 }
264
f7ce3cc6 265 /* This code detects calls by card attach_inform */
1da177e4 266 if (NULL == t->i2c.dev.driver) {
f7ce3cc6
MCC
267 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
268
1da177e4
LT
269 return;
270 }
56fc08ca 271
b2083199 272 /* discard private data, in case set_type() was previously called */
7a91a80a 273 if (t->ops.release)
db8a6956 274 t->ops.release(t);
052c50d9
MK
275 else {
276 kfree(t->priv);
277 t->priv = NULL;
278 }
be2b85a1 279
1da177e4
LT
280 switch (t->type) {
281 case TUNER_MT2032:
96c0b7cf 282 microtune_attach(&t->fe, t->i2c.adapter, t->i2c.addr);
1da177e4
LT
283 break;
284 case TUNER_PHILIPS_TDA8290:
910bb3e3
MK
285 {
286 attach_tda8290(t);
1da177e4 287 break;
910bb3e3 288 }
586b0cab 289 case TUNER_TEA5767:
8d0936ed 290 if (tea5767_attach(&t->fe, t->i2c.adapter, t->i2c.addr) == NULL) {
f7ce3cc6
MCC
291 t->type = TUNER_ABSENT;
292 t->mode_mask = T_UNINITIALIZED;
293 return;
294 }
295 t->mode_mask = T_RADIO;
586b0cab 296 break;
8573a9e6 297 case TUNER_TEA5761:
7ab10bf7 298 if (tea5761_attach(&t->fe, t->i2c.adapter, t->i2c.addr) == NULL) {
8573a9e6
MCC
299 t->type = TUNER_ABSENT;
300 t->mode_mask = T_UNINITIALIZED;
9ee476a5 301 return;
8573a9e6
MCC
302 }
303 t->mode_mask = T_RADIO;
304 break;
586b0cab
MCC
305 case TUNER_PHILIPS_FMD1216ME_MK3:
306 buffer[0] = 0x0b;
307 buffer[1] = 0xdc;
308 buffer[2] = 0x9c;
309 buffer[3] = 0x60;
f7ce3cc6 310 i2c_master_send(c, buffer, 4);
586b0cab
MCC
311 mdelay(1);
312 buffer[2] = 0x86;
313 buffer[3] = 0x54;
f7ce3cc6 314 i2c_master_send(c, buffer, 4);
4adad287 315 attach_simple_tuner(t);
586b0cab 316 break;
93df3413
HH
317 case TUNER_PHILIPS_TD1316:
318 buffer[0] = 0x0b;
319 buffer[1] = 0xdc;
320 buffer[2] = 0x86;
321 buffer[3] = 0xa4;
322 i2c_master_send(c,buffer,4);
4adad287 323 attach_simple_tuner(t);
ac272ed7 324 break;
6cb45879
MCC
325 case TUNER_XC2028:
326 xc2028_tuner_init(c);
327 break;
15396236 328 case TUNER_TDA9887:
db8a6956 329 tda9887_tuner_init(t);
15396236 330 break;
1da177e4 331 default:
4adad287 332 attach_simple_tuner(t);
1da177e4
LT
333 break;
334 }
f7ce3cc6 335
e18f9444
MK
336 if (fe_tuner_ops->set_analog_params) {
337 strlcpy(t->i2c.name, fe_tuner_ops->info.name, sizeof(t->i2c.name));
338
339 t->ops.set_tv_freq = fe_set_freq;
340 t->ops.set_radio_freq = fe_set_freq;
341 t->ops.standby = fe_standby;
342 t->ops.release = fe_release;
1f5ef197 343 t->ops.has_signal = fe_has_signal;
e18f9444
MK
344 }
345
346 tuner_info("type set to %s\n", t->i2c.name);
347
f7ce3cc6
MCC
348 if (t->mode_mask == T_UNINITIALIZED)
349 t->mode_mask = new_mode_mask;
350
27487d44 351 set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? t->radio_freq : t->tv_freq);
f7ce3cc6 352 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
604f28e2 353 c->adapter->name, c->driver->driver.name, c->addr << 1, type,
f7ce3cc6 354 t->mode_mask);
293197cd 355 tuner_i2c_address_check(t);
1da177e4
LT
356}
357
f7ce3cc6
MCC
358/*
359 * This function apply tuner config to tuner specified
360 * by tun_setup structure. I addr is unset, then admin status
361 * and tun addr status is more precise then current status,
362 * it's applied. Otherwise status and type are applied only to
363 * tuner with exactly the same addr.
364*/
365
366static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
367{
368 struct tuner *t = i2c_get_clientdata(c);
369
15396236
MCC
370 tuner_dbg("set addr for type %i\n", t->type);
371
de956c1e
HH
372 if ( (t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
373 (t->mode_mask & tun_setup->mode_mask))) ||
374 (tun_setup->addr == c->addr)) {
375 set_type(c, tun_setup->type, tun_setup->mode_mask,
cfeb8839 376 tun_setup->config, tun_setup->tuner_callback);
f7ce3cc6
MCC
377 }
378}
56fc08ca 379
f7ce3cc6 380static inline int check_mode(struct tuner *t, char *cmd)
56fc08ca 381{
793cf9e6
MCC
382 if ((1 << t->mode & t->mode_mask) == 0) {
383 return EINVAL;
384 }
385
386 switch (t->mode) {
387 case V4L2_TUNER_RADIO:
388 tuner_dbg("Cmd %s accepted for radio\n", cmd);
389 break;
390 case V4L2_TUNER_ANALOG_TV:
391 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
392 break;
393 case V4L2_TUNER_DIGITAL_TV:
394 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
395 break;
56fc08ca 396 }
793cf9e6 397 return 0;
56fc08ca 398}
56fc08ca 399
f7ce3cc6 400/* get more precise norm info from insmod option */
1da177e4
LT
401static int tuner_fixup_std(struct tuner *t)
402{
403 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
1da177e4 404 switch (pal[0]) {
e71ced1a
HV
405 case '6':
406 tuner_dbg ("insmod fixup: PAL => PAL-60\n");
407 t->std = V4L2_STD_PAL_60;
408 break;
1da177e4
LT
409 case 'b':
410 case 'B':
411 case 'g':
412 case 'G':
f7ce3cc6 413 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
1da177e4
LT
414 t->std = V4L2_STD_PAL_BG;
415 break;
416 case 'i':
417 case 'I':
f7ce3cc6 418 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
1da177e4
LT
419 t->std = V4L2_STD_PAL_I;
420 break;
421 case 'd':
422 case 'D':
423 case 'k':
424 case 'K':
f7ce3cc6 425 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
1da177e4
LT
426 t->std = V4L2_STD_PAL_DK;
427 break;
f7ce3cc6
MCC
428 case 'M':
429 case 'm':
430 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
431 t->std = V4L2_STD_PAL_M;
432 break;
433 case 'N':
434 case 'n':
7e578191
MCC
435 if (pal[1] == 'c' || pal[1] == 'C') {
436 tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
437 t->std = V4L2_STD_PAL_Nc;
438 } else {
439 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
440 t->std = V4L2_STD_PAL_N;
441 }
f7ce3cc6 442 break;
21d4df37
MCC
443 case '-':
444 /* default parameter, do nothing */
445 break;
446 default:
447 tuner_warn ("pal= argument not recognised\n");
448 break;
1da177e4
LT
449 }
450 }
f7ce3cc6
MCC
451 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
452 switch (secam[0]) {
7e578191
MCC
453 case 'b':
454 case 'B':
455 case 'g':
456 case 'G':
457 case 'h':
458 case 'H':
459 tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
460 t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
461 break;
f7ce3cc6
MCC
462 case 'd':
463 case 'D':
464 case 'k':
465 case 'K':
466 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
467 t->std = V4L2_STD_SECAM_DK;
468 break;
469 case 'l':
470 case 'L':
800d3c6f
MCC
471 if ((secam[1]=='C')||(secam[1]=='c')) {
472 tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
473 t->std = V4L2_STD_SECAM_LC;
474 } else {
475 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
476 t->std = V4L2_STD_SECAM_L;
477 }
f7ce3cc6 478 break;
21d4df37
MCC
479 case '-':
480 /* default parameter, do nothing */
481 break;
482 default:
483 tuner_warn ("secam= argument not recognised\n");
484 break;
f7ce3cc6
MCC
485 }
486 }
487
7e578191
MCC
488 if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
489 switch (ntsc[0]) {
490 case 'm':
491 case 'M':
492 tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
493 t->std = V4L2_STD_NTSC_M;
494 break;
495 case 'j':
496 case 'J':
497 tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
498 t->std = V4L2_STD_NTSC_M_JP;
499 break;
d97a11e0
HV
500 case 'k':
501 case 'K':
502 tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
503 t->std = V4L2_STD_NTSC_M_KR;
504 break;
7e578191
MCC
505 case '-':
506 /* default parameter, do nothing */
507 break;
508 default:
509 tuner_info("ntsc= argument not recognised\n");
510 break;
511 }
512 }
1da177e4
LT
513 return 0;
514}
515
db8a6956 516static void tuner_status(struct tuner *t)
7e578191 517{
7e578191 518 unsigned long freq, freq_fraction;
e18f9444 519 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
7e578191
MCC
520 const char *p;
521
522 switch (t->mode) {
523 case V4L2_TUNER_RADIO: p = "radio"; break;
524 case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
525 case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
526 default: p = "undefined"; break;
527 }
528 if (t->mode == V4L2_TUNER_RADIO) {
27487d44
HV
529 freq = t->radio_freq / 16000;
530 freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
7e578191 531 } else {
27487d44
HV
532 freq = t->tv_freq / 16;
533 freq_fraction = (t->tv_freq % 16) * 100 / 16;
7e578191
MCC
534 }
535 tuner_info("Tuner mode: %s\n", p);
536 tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
4ae5c2e5 537 tuner_info("Standard: 0x%08lx\n", (unsigned long)t->std);
8a4b275f
HV
538 if (t->mode != V4L2_TUNER_RADIO)
539 return;
e18f9444
MK
540 if (fe_tuner_ops->get_status) {
541 u32 tuner_status;
542
543 fe_tuner_ops->get_status(&t->fe, &tuner_status);
544 if (tuner_status & TUNER_STATUS_LOCKED)
545 tuner_info("Tuner is locked.\n");
546 if (tuner_status & TUNER_STATUS_STEREO)
547 tuner_info("Stereo: yes\n");
548 }
7a91a80a 549 if (t->ops.has_signal) {
db8a6956 550 tuner_info("Signal strength: %d\n", t->ops.has_signal(t));
8a4b275f 551 }
7a91a80a 552 if (t->ops.is_stereo) {
db8a6956 553 tuner_info("Stereo: %s\n", t->ops.is_stereo(t) ? "yes" : "no");
7e578191
MCC
554 }
555}
8a4b275f 556
1da177e4
LT
557/* ---------------------------------------------------------------------- */
558
ba8fc399 559/* static vars: used only in tuner_attach and tuner_probe */
f7ce3cc6
MCC
560static unsigned default_mode_mask;
561
562/* During client attach, set_type is called by adapter's attach_inform callback.
563 set_type must then be completed by tuner_attach.
564 */
1da177e4
LT
565static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
566{
567 struct tuner *t;
568
f7ce3cc6
MCC
569 client_template.adapter = adap;
570 client_template.addr = addr;
1da177e4 571
7408187d 572 t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
f7ce3cc6
MCC
573 if (NULL == t)
574 return -ENOMEM;
f7ce3cc6 575 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
1da177e4 576 i2c_set_clientdata(&t->i2c, t);
f7ce3cc6 577 t->type = UNSET;
f7ce3cc6
MCC
578 t->audmode = V4L2_TUNER_MODE_STEREO;
579 t->mode_mask = T_UNINITIALIZED;
7a91a80a 580 t->ops.tuner_status = tuner_status;
f7ce3cc6 581
fd3113e8
MCC
582 if (show_i2c) {
583 unsigned char buffer[16];
584 int i,rc;
585
586 memset(buffer, 0, sizeof(buffer));
587 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
67678360 588 tuner_info("I2C RECV = ");
fd3113e8
MCC
589 for (i=0;i<rc;i++)
590 printk("%02x ",buffer[i]);
591 printk("\n");
592 }
c28089a6
MH
593 /* HACK: This test were added to avoid tuner to probe tda9840 and tea6415c on the MXB card */
594 if (adap->id == I2C_HW_SAA7146 && addr < 0x4a)
595 return -ENODEV;
596
257c645d 597 /* autodetection code based on the i2c addr */
c5287ba1 598 if (!no_autodetect) {
13dd38d0 599 switch (addr) {
8573a9e6 600 case 0x10:
7ab10bf7 601 if (tea5761_autodetection(t->i2c.adapter, t->i2c.addr) != EINVAL) {
8573a9e6
MCC
602 t->type = TUNER_TEA5761;
603 t->mode_mask = T_RADIO;
604 t->mode = T_STANDBY;
605 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
606 default_mode_mask &= ~T_RADIO;
607
608 goto register_client;
609 }
610 break;
13dd38d0
MCC
611 case 0x42:
612 case 0x43:
613 case 0x4a:
95736034 614 case 0x4b:
67678360
MCC
615 /* If chip is not tda8290, don't register.
616 since it can be tda9887*/
910bb3e3 617 if (tda8290_probe(t->i2c.adapter, t->i2c.addr) == 0) {
15396236
MCC
618 tuner_dbg("chip at addr %x is a tda8290\n", addr);
619 } else {
620 /* Default is being tda9887 */
621 t->type = TUNER_TDA9887;
622 t->mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
623 t->mode = T_STANDBY;
624 goto register_client;
13dd38d0 625 }
07345f5d
HH
626 break;
627 case 0x60:
8d0936ed 628 if (tea5767_autodetection(t->i2c.adapter, t->i2c.addr) != EINVAL) {
07345f5d
HH
629 t->type = TUNER_TEA5767;
630 t->mode_mask = T_RADIO;
631 t->mode = T_STANDBY;
27487d44 632 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
07345f5d 633 default_mode_mask &= ~T_RADIO;
13dd38d0 634
07345f5d
HH
635 goto register_client;
636 }
637 break;
f7ce3cc6
MCC
638 }
639 }
1da177e4 640
f7ce3cc6
MCC
641 /* Initializes only the first adapter found */
642 if (default_mode_mask != T_UNINITIALIZED) {
643 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
644 t->mode_mask = default_mode_mask;
27487d44
HV
645 t->tv_freq = 400 * 16; /* Sets freq to VHF High */
646 t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
f7ce3cc6
MCC
647 default_mode_mask = T_UNINITIALIZED;
648 }
56fc08ca 649
f7ce3cc6 650 /* Should be just before return */
67678360
MCC
651register_client:
652 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
f7ce3cc6 653 i2c_attach_client (&t->i2c);
cfeb8839 654 set_type (&t->i2c,t->type, t->mode_mask, t->config, t->tuner_callback);
1da177e4
LT
655 return 0;
656}
657
658static int tuner_probe(struct i2c_adapter *adap)
659{
660 if (0 != addr) {
f5bec396
MCC
661 normal_i2c[0] = addr;
662 normal_i2c[1] = I2C_CLIENT_END;
1da177e4 663 }
1da177e4 664
a1dec516
MK
665 /* HACK: Ignore 0x6b and 0x6f on cx88 boards.
666 * FusionHDTV5 RT Gold has an ir receiver at 0x6b
667 * and an RTC at 0x6f which can get corrupted if probed.
668 */
9bc37caa
MK
669 if ((adap->id == I2C_HW_B_CX2388x) ||
670 (adap->id == I2C_HW_B_CX23885)) {
a1dec516
MK
671 unsigned int i = 0;
672
673 while (i < I2C_CLIENT_MAX_OPTS && ignore[i] != I2C_CLIENT_END)
674 i += 2;
675 if (i + 4 < I2C_CLIENT_MAX_OPTS) {
676 ignore[i+0] = adap->nr;
677 ignore[i+1] = 0x6b;
678 ignore[i+2] = adap->nr;
679 ignore[i+3] = 0x6f;
680 ignore[i+4] = I2C_CLIENT_END;
681 } else
682 printk(KERN_WARNING "tuner: "
683 "too many options specified "
684 "in i2c probe ignore list!\n");
685 }
686
f7ce3cc6 687 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
391cd727 688
1da177e4
LT
689 if (adap->class & I2C_CLASS_TV_ANALOG)
690 return i2c_probe(adap, &addr_data, tuner_attach);
691 return 0;
692}
693
694static int tuner_detach(struct i2c_client *client)
695{
696 struct tuner *t = i2c_get_clientdata(client);
391cd727
MCC
697 int err;
698
f7ce3cc6 699 err = i2c_detach_client(&t->i2c);
391cd727 700 if (err) {
f7ce3cc6
MCC
701 tuner_warn
702 ("Client deregistration failed, client not detached.\n");
391cd727
MCC
703 return err;
704 }
1da177e4 705
7a91a80a 706 if (t->ops.release)
db8a6956 707 t->ops.release(t);
052c50d9
MK
708 else {
709 kfree(t->priv);
710 }
1da177e4
LT
711 kfree(t);
712 return 0;
713}
714
f7ce3cc6
MCC
715/*
716 * Switch tuner to other mode. If tuner support both tv and radio,
717 * set another frequency to some value (This is needed for some pal
718 * tuners to avoid locking). Otherwise, just put second tuner in
719 * standby mode.
720 */
721
722static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
723{
4ac97914
MCC
724 if (mode == t->mode)
725 return 0;
726
727 t->mode = mode;
728
729 if (check_mode(t, cmd) == EINVAL) {
730 t->mode = T_STANDBY;
7a91a80a 731 if (t->ops.standby)
db8a6956 732 t->ops.standby(t);
4ac97914
MCC
733 return EINVAL;
734 }
735 return 0;
f7ce3cc6
MCC
736}
737
738#define switch_v4l2() if (!t->using_v4l2) \
4ac97914
MCC
739 tuner_dbg("switching to v4l2\n"); \
740 t->using_v4l2 = 1;
f7ce3cc6
MCC
741
742static inline int check_v4l2(struct tuner *t)
743{
3bbe5a83
HV
744 /* bttv still uses both v4l1 and v4l2 calls to the tuner (v4l2 for
745 TV, v4l1 for radio), until that is fixed this code is disabled.
746 Otherwise the radio (v4l1) wouldn't tune after using the TV (v4l2)
747 first. */
f7ce3cc6
MCC
748 return 0;
749}
1da177e4 750
f7ce3cc6 751static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
1da177e4
LT
752{
753 struct tuner *t = i2c_get_clientdata(client);
e18f9444 754 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
1da177e4 755
f9195ded 756 if (tuner_debug>1)
5e453dc7
MK
757 v4l_i2c_print_ioctl(&(t->i2c),cmd);
758
f7ce3cc6 759 switch (cmd) {
1da177e4 760 /* --- configuration --- */
56fc08ca 761 case TUNER_SET_TYPE_ADDR:
de956c1e 762 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=0x%02x\n",
f7ce3cc6
MCC
763 ((struct tuner_setup *)arg)->type,
764 ((struct tuner_setup *)arg)->addr,
de956c1e
HH
765 ((struct tuner_setup *)arg)->mode_mask,
766 ((struct tuner_setup *)arg)->config);
f7ce3cc6
MCC
767
768 set_addr(client, (struct tuner_setup *)arg);
391cd727 769 break;
1da177e4 770 case AUDC_SET_RADIO:
27487d44
HV
771 if (set_mode(client, t, V4L2_TUNER_RADIO, "AUDC_SET_RADIO")
772 == EINVAL)
773 return 0;
774 if (t->radio_freq)
775 set_freq(client, t->radio_freq);
1da177e4 776 break;
793cf9e6 777 case TUNER_SET_STANDBY:
27487d44
HV
778 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
779 return 0;
15396236 780 t->mode = T_STANDBY;
7a91a80a 781 if (t->ops.standby)
db8a6956 782 t->ops.standby(t);
27487d44 783 break;
985bc96e 784#ifdef CONFIG_VIDEO_V4L1
fd3113e8
MCC
785 case VIDIOCSAUDIO:
786 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
787 return 0;
788 if (check_v4l2(t) == EINVAL)
789 return 0;
790
791 /* Should be implemented, since bttv calls it */
792 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
f7ce3cc6 793 break;
1da177e4 794 case VIDIOCSCHAN:
f7ce3cc6
MCC
795 {
796 static const v4l2_std_id map[] = {
797 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
798 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
799 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
800 [4 /* bttv */ ] = V4L2_STD_PAL_M,
801 [5 /* bttv */ ] = V4L2_STD_PAL_N,
802 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
803 };
804 struct video_channel *vc = arg;
805
806 if (check_v4l2(t) == EINVAL)
807 return 0;
808
809 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
810 return 0;
811
812 if (vc->norm < ARRAY_SIZE(map))
813 t->std = map[vc->norm];
814 tuner_fixup_std(t);
27487d44
HV
815 if (t->tv_freq)
816 set_tv_freq(client, t->tv_freq);
f7ce3cc6
MCC
817 return 0;
818 }
1da177e4 819 case VIDIOCSFREQ:
f7ce3cc6
MCC
820 {
821 unsigned long *v = arg;
1da177e4 822
f7ce3cc6
MCC
823 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
824 return 0;
825 if (check_v4l2(t) == EINVAL)
826 return 0;
827
828 set_freq(client, *v);
829 return 0;
830 }
1da177e4 831 case VIDIOCGTUNER:
f7ce3cc6
MCC
832 {
833 struct video_tuner *vt = arg;
834
835 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
836 return 0;
837 if (check_v4l2(t) == EINVAL)
838 return 0;
839
840 if (V4L2_TUNER_RADIO == t->mode) {
e18f9444
MK
841 if (fe_tuner_ops->get_status) {
842 u32 tuner_status;
843
844 fe_tuner_ops->get_status(&t->fe, &tuner_status);
1f5ef197
MK
845 if (tuner_status & TUNER_STATUS_STEREO)
846 vt->flags |= VIDEO_TUNER_STEREO_ON;
847 else
848 vt->flags &= ~VIDEO_TUNER_STEREO_ON;
e18f9444
MK
849 } else {
850 if (t->ops.is_stereo) {
851 if (t->ops.is_stereo(t))
852 vt->flags |=
853 VIDEO_TUNER_STEREO_ON;
854 else
855 vt->flags &=
856 ~VIDEO_TUNER_STEREO_ON;
857 }
f7ce3cc6 858 }
1f5ef197
MK
859 if (t->ops.has_signal)
860 vt->signal = t->ops.has_signal(t);
861
f7ce3cc6 862 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
586b0cab 863
f7ce3cc6
MCC
864 vt->rangelow = radio_range[0] * 16000;
865 vt->rangehigh = radio_range[1] * 16000;
586b0cab 866
f7ce3cc6
MCC
867 } else {
868 vt->rangelow = tv_range[0] * 16;
869 vt->rangehigh = tv_range[1] * 16;
870 }
56fc08ca 871
f7ce3cc6
MCC
872 return 0;
873 }
1da177e4 874 case VIDIOCGAUDIO:
f7ce3cc6
MCC
875 {
876 struct video_audio *va = arg;
877
878 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
879 return 0;
880 if (check_v4l2(t) == EINVAL)
881 return 0;
882
e18f9444
MK
883 if (V4L2_TUNER_RADIO == t->mode) {
884 if (fe_tuner_ops->get_status) {
885 u32 tuner_status;
886
887 fe_tuner_ops->get_status(&t->fe, &tuner_status);
888 va->mode = (tuner_status & TUNER_STATUS_STEREO)
889 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
890 } else if (t->ops.is_stereo)
891 va->mode = t->ops.is_stereo(t)
892 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
893 }
f7ce3cc6
MCC
894 return 0;
895 }
985bc96e 896#endif
7f171123
MCC
897 case TUNER_SET_CONFIG:
898 {
899 struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
900 struct v4l2_priv_tun_config *cfg = arg;
901
902 if (t->type != cfg->tuner)
903 break;
1da177e4 904
7f171123
MCC
905 if (t->type == TUNER_TDA9887) {
906 t->tda9887_config = *(unsigned int *)cfg->priv;
985bc96e 907 set_freq(client, t->tv_freq);
7f171123 908 break;
985bc96e 909 }
7f171123
MCC
910
911 if (NULL == fe_tuner_ops->set_config) {
912 tuner_warn("Tuner frontend module has no way to "
913 "set config\n");
914 break;
915 }
916 fe_tuner_ops->set_config(&t->fe, cfg->priv);
917
985bc96e 918 break;
7f171123 919 }
985bc96e
MCC
920 /* --- v4l ioctls --- */
921 /* take care: bttv does userspace copying, we'll get a
922 kernel pointer here... */
1da177e4 923 case VIDIOC_S_STD:
f7ce3cc6
MCC
924 {
925 v4l2_std_id *id = arg;
1da177e4 926
f7ce3cc6
MCC
927 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
928 == EINVAL)
929 return 0;
56fc08ca 930
f7ce3cc6
MCC
931 switch_v4l2();
932
933 t->std = *id;
934 tuner_fixup_std(t);
27487d44
HV
935 if (t->tv_freq)
936 set_freq(client, t->tv_freq);
f7ce3cc6
MCC
937 break;
938 }
1da177e4 939 case VIDIOC_S_FREQUENCY:
f7ce3cc6
MCC
940 {
941 struct v4l2_frequency *f = arg;
942
4f725cb3
HV
943 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
944 == EINVAL)
945 return 0;
f7ce3cc6 946 switch_v4l2();
27487d44 947 set_freq(client,f->frequency);
c184ca36 948
f7ce3cc6
MCC
949 break;
950 }
951 case VIDIOC_G_FREQUENCY:
952 {
953 struct v4l2_frequency *f = arg;
954
955 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
956 return 0;
957 switch_v4l2();
958 f->type = t->mode;
e18f9444
MK
959 if (fe_tuner_ops->get_frequency) {
960 u32 abs_freq;
961
962 fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
963 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
964 (abs_freq * 2 + 125/2) / 125 :
965 (abs_freq + 62500/2) / 62500;
966 break;
967 }
27487d44
HV
968 f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
969 t->radio_freq : t->tv_freq;
f7ce3cc6
MCC
970 break;
971 }
1da177e4 972 case VIDIOC_G_TUNER:
f7ce3cc6
MCC
973 {
974 struct v4l2_tuner *tuner = arg;
975
976 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
977 return 0;
978 switch_v4l2();
979
8a4b275f 980 tuner->type = t->mode;
7a91a80a 981 if (t->ops.get_afc)
db8a6956 982 tuner->afc=t->ops.get_afc(t);
ab4cecf9
HV
983 if (t->mode == V4L2_TUNER_ANALOG_TV)
984 tuner->capability |= V4L2_TUNER_CAP_NORM;
8a4b275f 985 if (t->mode != V4L2_TUNER_RADIO) {
f7ce3cc6
MCC
986 tuner->rangelow = tv_range[0] * 16;
987 tuner->rangehigh = tv_range[1] * 16;
8a4b275f
HV
988 break;
989 }
990
991 /* radio mode */
8a4b275f
HV
992 tuner->rxsubchans =
993 V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
e18f9444
MK
994 if (fe_tuner_ops->get_status) {
995 u32 tuner_status;
996
997 fe_tuner_ops->get_status(&t->fe, &tuner_status);
998 tuner->rxsubchans = (tuner_status & TUNER_STATUS_STEREO) ?
8a4b275f 999 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
e18f9444
MK
1000 } else {
1001 if (t->ops.is_stereo) {
1002 tuner->rxsubchans = t->ops.is_stereo(t) ?
1003 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1004 }
56fc08ca 1005 }
1f5ef197
MK
1006 if (t->ops.has_signal)
1007 tuner->signal = t->ops.has_signal(t);
8a4b275f
HV
1008 tuner->capability |=
1009 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
1010 tuner->audmode = t->audmode;
1011 tuner->rangelow = radio_range[0] * 16000;
1012 tuner->rangehigh = radio_range[1] * 16000;
f7ce3cc6
MCC
1013 break;
1014 }
1015 case VIDIOC_S_TUNER:
1016 {
1017 struct v4l2_tuner *tuner = arg;
1018
1019 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
1020 return 0;
1021
1022 switch_v4l2();
1023
8a4b275f
HV
1024 /* do nothing unless we're a radio tuner */
1025 if (t->mode != V4L2_TUNER_RADIO)
1026 break;
1027 t->audmode = tuner->audmode;
1028 set_radio_freq(client, t->radio_freq);
f7ce3cc6 1029 break;
56fc08ca 1030 }
cd43c3f6 1031 case VIDIOC_LOG_STATUS:
7a91a80a 1032 if (t->ops.tuner_status)
db8a6956 1033 t->ops.tuner_status(t);
cd43c3f6 1034 break;
1da177e4
LT
1035 }
1036
1037 return 0;
1038}
1039
21b48a70 1040static int tuner_suspend(struct i2c_client *c, pm_message_t state)
1da177e4 1041{
f7ce3cc6 1042 struct tuner *t = i2c_get_clientdata (c);
1da177e4 1043
f7ce3cc6 1044 tuner_dbg ("suspend\n");
1da177e4
LT
1045 /* FIXME: power down ??? */
1046 return 0;
1047}
1048
21b48a70 1049static int tuner_resume(struct i2c_client *c)
1da177e4 1050{
f7ce3cc6 1051 struct tuner *t = i2c_get_clientdata (c);
1da177e4 1052
f7ce3cc6 1053 tuner_dbg ("resume\n");
27487d44
HV
1054 if (V4L2_TUNER_RADIO == t->mode) {
1055 if (t->radio_freq)
1056 set_freq(c, t->radio_freq);
1057 } else {
1058 if (t->tv_freq)
1059 set_freq(c, t->tv_freq);
1060 }
1da177e4
LT
1061 return 0;
1062}
1063
1064/* ----------------------------------------------------------------------- */
1065
1066static struct i2c_driver driver = {
f7ce3cc6 1067 .id = I2C_DRIVERID_TUNER,
f7ce3cc6
MCC
1068 .attach_adapter = tuner_probe,
1069 .detach_client = tuner_detach,
1070 .command = tuner_command,
21b48a70
JD
1071 .suspend = tuner_suspend,
1072 .resume = tuner_resume,
1da177e4 1073 .driver = {
cab462f7 1074 .name = "tuner",
cab462f7 1075 },
1da177e4 1076};
f7ce3cc6 1077static struct i2c_client client_template = {
fae91e72 1078 .name = "(tuner unset)",
f7ce3cc6 1079 .driver = &driver,
1da177e4
LT
1080};
1081
1082static int __init tuner_init_module(void)
1083{
1084 return i2c_add_driver(&driver);
1085}
1086
1087static void __exit tuner_cleanup_module(void)
1088{
1089 i2c_del_driver(&driver);
1090}
1091
1092module_init(tuner_init_module);
1093module_exit(tuner_cleanup_module);
1094
1095/*
1096 * Overrides for Emacs so that we follow Linus's tabbing style.
1097 * ---------------------------------------------------------------------------
1098 * Local variables:
1099 * c-basic-offset: 8
1100 * End:
1101 */