]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/tuner-core.c
[PATCH] V4L maintainer patch
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / tuner-core.c
CommitLineData
1da177e4 1/*
56fc08ca 2 * $Id: tuner-core.c,v 1.15 2005/06/12 01:36:14 mchehab Exp $
1da177e4
LT
3 *
4 * i2c tv tuner chip device driver
5 * core core, i.e. kernel interfaces, registering and so on
6 */
7
8#include <linux/module.h>
9#include <linux/moduleparam.h>
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/string.h>
13#include <linux/timer.h>
14#include <linux/delay.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
17#include <linux/poll.h>
18#include <linux/i2c.h>
19#include <linux/types.h>
20#include <linux/videodev.h>
21#include <linux/init.h>
22
23#include <media/tuner.h>
24#include <media/audiochip.h>
25
391cd727
MCC
26/*
27 * comment line bellow to return to old behavor, where only one I2C device is supported
28 */
56fc08ca 29#define CONFIG_TUNER_MULTI_I2C /**/
391cd727 30
1da177e4
LT
31#define UNSET (-1U)
32
33/* standard i2c insmod options */
34static unsigned short normal_i2c[] = {
35 0x4b, /* tda8290 */
f5bec396
MCC
36 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
37 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
1da177e4
LT
38 I2C_CLIENT_END
39};
40I2C_CLIENT_INSMOD;
41
42/* insmod options used at init time => read/only */
43static unsigned int addr = 0;
44module_param(addr, int, 0444);
45
46/* insmod options used at runtime => read/write */
47unsigned int tuner_debug = 0;
48module_param(tuner_debug, int, 0644);
49
50static unsigned int tv_range[2] = { 44, 958 };
51static unsigned int radio_range[2] = { 65, 108 };
52
53module_param_array(tv_range, int, NULL, 0644);
54module_param_array(radio_range, int, NULL, 0644);
55
56MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
57MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
58MODULE_LICENSE("GPL");
59
60static int this_adap;
391cd727 61#ifdef CONFIG_TUNER_MULTI_I2C
56fc08ca 62static unsigned short first_tuner, tv_tuner, radio_tuner;
391cd727 63#endif
1da177e4
LT
64
65static struct i2c_driver driver;
66static struct i2c_client client_template;
67
68/* ---------------------------------------------------------------------- */
69
56fc08ca 70/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
1da177e4
LT
71static void set_tv_freq(struct i2c_client *c, unsigned int freq)
72{
73 struct tuner *t = i2c_get_clientdata(c);
74
75 if (t->type == UNSET) {
76 tuner_info("tuner type not set\n");
77 return;
78 }
79 if (NULL == t->tv_freq) {
80 tuner_info("Huh? tv_set is NULL?\n");
81 return;
82 }
83 if (freq < tv_range[0]*16 || freq > tv_range[1]*16) {
56fc08ca
MCC
84
85 if (freq >= tv_range[0]*16364 && freq <= tv_range[1]*16384) {
86 /* V4L2_TUNER_CAP_LOW frequency */
87
88 tuner_dbg("V4L2_TUNER_CAP_LOW freq selected for TV. Tuners yet doesn't support converting it to valid freq.\n");
89
90 t->tv_freq(c,freq>>10);
91
92 return;
93 } else {
94 /* FIXME: better do that chip-specific, but
95 right now we don't have that in the config
96 struct and this way is still better than no
97 check at all */
98 tuner_info("TV freq (%d.%02d) out of range (%d-%d)\n",
99 freq/16,freq%16*100/16,tv_range[0],tv_range[1]);
100 return;
101 }
1da177e4 102 }
56fc08ca 103 tuner_dbg("62.5 Khz freq step selected for TV.\n");
1da177e4
LT
104 t->tv_freq(c,freq);
105}
106
107static void set_radio_freq(struct i2c_client *c, unsigned int freq)
108{
109 struct tuner *t = i2c_get_clientdata(c);
110
111 if (t->type == UNSET) {
112 tuner_info("tuner type not set\n");
113 return;
114 }
115 if (NULL == t->radio_freq) {
116 tuner_info("no radio tuning for this one, sorry.\n");
117 return;
118 }
119 if (freq < radio_range[0]*16 || freq > radio_range[1]*16) {
56fc08ca
MCC
120 if (freq >= tv_range[0]*16364 && freq <= tv_range[1]*16384) {
121 /* V4L2_TUNER_CAP_LOW frequency */
122 if (t->type == TUNER_TEA5767) {
123 tuner_info("radio freq step 62.5Hz (%d.%06d)\n",(freq>>14),freq%(1<<14)*10000);
124 t->radio_freq(c,freq>>10);
125 return;
126 }
127
128 tuner_dbg("V4L2_TUNER_CAP_LOW freq selected for Radio. Tuners yet doesn't support converting it to valid freq.\n");
129
130 tuner_info("radio freq (%d.%06d)\n",(freq>>14),freq%(1<<14)*10000);
131
132 t->radio_freq(c,freq>>10);
133 return;
134
135 } else {
136 tuner_info("radio freq (%d.%02d) out of range (%d-%d)\n",
1da177e4 137 freq/16,freq%16*100/16,
56fc08ca
MCC
138 radio_range[0],radio_range[1]);
139 return;
140 }
1da177e4 141 }
56fc08ca 142 tuner_dbg("62.5 Khz freq step selected for Radio.\n");
1da177e4
LT
143 t->radio_freq(c,freq);
144}
145
146static void set_freq(struct i2c_client *c, unsigned long freq)
147{
148 struct tuner *t = i2c_get_clientdata(c);
149
150 switch (t->mode) {
151 case V4L2_TUNER_RADIO:
152 tuner_dbg("radio freq set to %lu.%02lu\n",
153 freq/16,freq%16*100/16);
154 set_radio_freq(c,freq);
155 break;
156 case V4L2_TUNER_ANALOG_TV:
157 case V4L2_TUNER_DIGITAL_TV:
158 tuner_dbg("tv freq set to %lu.%02lu\n",
159 freq/16,freq%16*100/16);
160 set_tv_freq(c, freq);
161 break;
162 }
163 t->freq = freq;
164}
165
166static void set_type(struct i2c_client *c, unsigned int type)
167{
168 struct tuner *t = i2c_get_clientdata(c);
169
56fc08ca 170 tuner_dbg ("I2C addr 0x%02x with type %d\n",c->addr<<1,type);
1da177e4 171 /* sanity check */
56fc08ca 172 if (type == UNSET || type == TUNER_ABSENT)
1da177e4
LT
173 return;
174 if (type >= tuner_count)
175 return;
176
177 if (NULL == t->i2c.dev.driver) {
178 /* not registered yet */
179 t->type = type;
180 return;
181 }
182 if (t->initialized)
183 /* run only once */
184 return;
185
186 t->initialized = 1;
56fc08ca 187
1da177e4
LT
188 t->type = type;
189 switch (t->type) {
190 case TUNER_MT2032:
191 microtune_init(c);
192 break;
193 case TUNER_PHILIPS_TDA8290:
194 tda8290_init(c);
195 break;
196 default:
197 default_tuner_init(c);
198 break;
199 }
200}
201
56fc08ca
MCC
202#ifdef CONFIG_TUNER_MULTI_I2C
203#define CHECK_ADDR(tp,cmd,tun) if (client->addr!=tp) { \
204 return 0; } else \
205 tuner_info ("Cmd %s accepted to "tun"\n",cmd);
206#define CHECK_MODE(cmd) if (t->mode == V4L2_TUNER_RADIO) { \
207 CHECK_ADDR(radio_tuner,cmd,"radio") } else \
208 { CHECK_ADDR(tv_tuner,cmd,"TV"); }
209#else
210#define CHECK_ADDR(tp,cmd,tun) tuner_info ("Cmd %s accepted to "tun"\n",cmd);
211#define CHECK_MODE(cmd) tuner_info ("Cmd %s accepted\n",cmd);
212#endif
213
214#ifdef CONFIG_TUNER_MULTI_I2C
215
216static void set_addr(struct i2c_client *c, struct tuner_addr *tun_addr)
217{
218 /* ADDR_UNSET defaults to first available tuner */
219 if ( tun_addr->addr == ADDR_UNSET ) {
220 if (first_tuner != c->addr)
221 return;
222 switch (tun_addr->v4l2_tuner) {
223 case V4L2_TUNER_RADIO:
224 radio_tuner=c->addr;
225 break;
226 default:
227 tv_tuner=c->addr;
228 break;
229 }
230 } else {
231 /* Sets tuner to its configured value */
232 switch (tun_addr->v4l2_tuner) {
233 case V4L2_TUNER_RADIO:
234 radio_tuner=tun_addr->addr;
235 if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
236 return;
237 default:
238 tv_tuner=tun_addr->addr;
239 if ( tun_addr->addr == c->addr ) set_type(c,tun_addr->type);
240 return;
241 }
242 }
243 set_type(c,tun_addr->type);
244}
245#else
246#define set_addr(c,tun_addr) set_type(c,(tun_addr)->type)
247#endif
248
1da177e4 249static char pal[] = "-";
975e046c 250module_param_string(pal, pal, sizeof(pal), 0644);
1da177e4
LT
251
252static int tuner_fixup_std(struct tuner *t)
253{
254 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
255 /* get more precise norm info from insmod option */
256 switch (pal[0]) {
257 case 'b':
258 case 'B':
259 case 'g':
260 case 'G':
261 tuner_dbg("insmod fixup: PAL => PAL-BG\n");
262 t->std = V4L2_STD_PAL_BG;
263 break;
264 case 'i':
265 case 'I':
266 tuner_dbg("insmod fixup: PAL => PAL-I\n");
267 t->std = V4L2_STD_PAL_I;
268 break;
269 case 'd':
270 case 'D':
271 case 'k':
272 case 'K':
273 tuner_dbg("insmod fixup: PAL => PAL-DK\n");
274 t->std = V4L2_STD_PAL_DK;
275 break;
276 }
277 }
278 return 0;
279}
280
281/* ---------------------------------------------------------------------- */
282
283static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
284{
285 struct tuner *t;
286
391cd727 287#ifndef CONFIG_TUNER_MULTI_I2C
1da177e4
LT
288 if (this_adap > 0)
289 return -1;
391cd727
MCC
290#else
291 /* by default, first I2C card is both tv and radio tuner */
292 if (this_adap == 0) {
56fc08ca 293 first_tuner = addr;
391cd727
MCC
294 tv_tuner = addr;
295 radio_tuner = addr;
296 }
297#endif
1da177e4
LT
298 this_adap++;
299
300 client_template.adapter = adap;
301 client_template.addr = addr;
302
303 t = kmalloc(sizeof(struct tuner),GFP_KERNEL);
304 if (NULL == t)
305 return -ENOMEM;
306 memset(t,0,sizeof(struct tuner));
307 memcpy(&t->i2c,&client_template,sizeof(struct i2c_client));
308 i2c_set_clientdata(&t->i2c, t);
309 t->type = UNSET;
56fc08ca 310 t->radio_if2 = 10700*1000; /* 10.7MHz - FM radio */
1da177e4
LT
311
312 i2c_attach_client(&t->i2c);
313 tuner_info("chip found @ 0x%x (%s)\n",
314 addr << 1, adap->name);
56fc08ca 315
1da177e4
LT
316 set_type(&t->i2c, t->type);
317 return 0;
318}
319
320static int tuner_probe(struct i2c_adapter *adap)
321{
322 if (0 != addr) {
f5bec396
MCC
323 normal_i2c[0] = addr;
324 normal_i2c[1] = I2C_CLIENT_END;
1da177e4
LT
325 }
326 this_adap = 0;
327
391cd727 328#ifdef CONFIG_TUNER_MULTI_I2C
56fc08ca 329 first_tuner = 0;
391cd727
MCC
330 tv_tuner = 0;
331 radio_tuner = 0;
332#endif
333
1da177e4
LT
334 if (adap->class & I2C_CLASS_TV_ANALOG)
335 return i2c_probe(adap, &addr_data, tuner_attach);
336 return 0;
337}
338
339static int tuner_detach(struct i2c_client *client)
340{
341 struct tuner *t = i2c_get_clientdata(client);
391cd727
MCC
342 int err;
343
344 err=i2c_detach_client(&t->i2c);
345 if (err) {
346 tuner_warn ("Client deregistration failed, client not detached.\n");
347 return err;
348 }
1da177e4 349
1da177e4
LT
350 kfree(t);
351 return 0;
352}
353
354#define SWITCH_V4L2 if (!t->using_v4l2 && tuner_debug) \
355 tuner_info("switching to v4l2\n"); \
356 t->using_v4l2 = 1;
357#define CHECK_V4L2 if (t->using_v4l2) { if (tuner_debug) \
358 tuner_info("ignore v4l1 call\n"); \
359 return 0; }
360
361static int
362tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
363{
364 struct tuner *t = i2c_get_clientdata(client);
365 unsigned int *iarg = (int*)arg;
366
367 switch (cmd) {
1da177e4
LT
368 /* --- configuration --- */
369 case TUNER_SET_TYPE:
370 set_type(client,*iarg);
371 break;
56fc08ca 372 case TUNER_SET_TYPE_ADDR:
391cd727
MCC
373 set_addr(client,(struct tuner_addr *)arg);
374 break;
1da177e4 375 case AUDC_SET_RADIO:
56fc08ca
MCC
376 t->mode = V4L2_TUNER_RADIO;
377 CHECK_ADDR(tv_tuner,"AUDC_SET_RADIO","TV");
391cd727 378
1da177e4
LT
379 if (V4L2_TUNER_RADIO != t->mode) {
380 set_tv_freq(client,400 * 16);
1da177e4
LT
381 }
382 break;
383 case AUDC_CONFIG_PINNACLE:
56fc08ca 384 CHECK_ADDR(tv_tuner,"AUDC_CONFIG_PINNACLE","TV");
1da177e4
LT
385 switch (*iarg) {
386 case 2:
387 tuner_dbg("pinnacle pal\n");
388 t->radio_if2 = 33300 * 1000;
389 break;
390 case 3:
391 tuner_dbg("pinnacle ntsc\n");
392 t->radio_if2 = 41300 * 1000;
393 break;
394 }
395 break;
396
397 /* --- v4l ioctls --- */
398 /* take care: bttv does userspace copying, we'll get a
399 kernel pointer here... */
400 case VIDIOCSCHAN:
401 {
402 static const v4l2_std_id map[] = {
403 [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
404 [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
405 [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
406 [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
407 [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
408 [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
409 };
410 struct video_channel *vc = arg;
411
412 CHECK_V4L2;
413 t->mode = V4L2_TUNER_ANALOG_TV;
56fc08ca
MCC
414 CHECK_ADDR(tv_tuner,"VIDIOCSCHAN","TV");
415
1da177e4
LT
416 if (vc->norm < ARRAY_SIZE(map))
417 t->std = map[vc->norm];
418 tuner_fixup_std(t);
419 if (t->freq)
420 set_tv_freq(client,t->freq);
421 return 0;
422 }
423 case VIDIOCSFREQ:
424 {
425 unsigned long *v = arg;
426
391cd727 427 CHECK_MODE("VIDIOCSFREQ");
1da177e4
LT
428 CHECK_V4L2;
429 set_freq(client,*v);
430 return 0;
431 }
432 case VIDIOCGTUNER:
433 {
434 struct video_tuner *vt = arg;
435
56fc08ca 436 CHECK_ADDR(radio_tuner,"VIDIOCGTUNER","radio");
1da177e4 437 CHECK_V4L2;
56fc08ca
MCC
438 if (V4L2_TUNER_RADIO == t->mode) {
439 if (t->has_signal)
440 vt->signal = t->has_signal(client);
441 if (t->is_stereo) {
442 if (t->is_stereo(client))
443 vt-> flags |= VIDEO_TUNER_STEREO_ON;
444 else
445 vt-> flags &= 0xffff ^ VIDEO_TUNER_STEREO_ON;
446 }
447 vt->flags |= V4L2_TUNER_CAP_LOW; /* Allow freqs at 62.5 Hz */
448 }
449
1da177e4
LT
450 return 0;
451 }
452 case VIDIOCGAUDIO:
453 {
454 struct video_audio *va = arg;
455
56fc08ca 456 CHECK_ADDR(radio_tuner,"VIDIOCGAUDIO","radio");
1da177e4
LT
457 CHECK_V4L2;
458 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
459 va->mode = t->is_stereo(client)
460 ? VIDEO_SOUND_STEREO
461 : VIDEO_SOUND_MONO;
462 return 0;
463 }
464
465 case VIDIOC_S_STD:
466 {
467 v4l2_std_id *id = arg;
468
469 SWITCH_V4L2;
470 t->mode = V4L2_TUNER_ANALOG_TV;
56fc08ca
MCC
471 CHECK_ADDR(tv_tuner,"VIDIOC_S_STD","TV");
472
1da177e4
LT
473 t->std = *id;
474 tuner_fixup_std(t);
475 if (t->freq)
476 set_freq(client,t->freq);
477 break;
478 }
479 case VIDIOC_S_FREQUENCY:
480 {
481 struct v4l2_frequency *f = arg;
482
391cd727 483 CHECK_MODE("VIDIOC_S_FREQUENCY");
1da177e4
LT
484 SWITCH_V4L2;
485 if (V4L2_TUNER_RADIO == f->type &&
486 V4L2_TUNER_RADIO != t->mode)
487 set_tv_freq(client,400*16);
488 t->mode = f->type;
e99d3438 489 set_freq(client,f->frequency);
1da177e4
LT
490 break;
491 }
c184ca36
JB
492 case VIDIOC_G_FREQUENCY:
493 {
494 struct v4l2_frequency *f = arg;
495
391cd727 496 CHECK_MODE("VIDIOC_G_FREQUENCY");
c184ca36
JB
497 SWITCH_V4L2;
498 f->type = t->mode;
499 f->frequency = t->freq;
500 break;
501 }
1da177e4
LT
502 case VIDIOC_G_TUNER:
503 {
504 struct v4l2_tuner *tuner = arg;
505
391cd727 506 CHECK_MODE("VIDIOC_G_TUNER");
1da177e4 507 SWITCH_V4L2;
56fc08ca
MCC
508 if (V4L2_TUNER_RADIO == t->mode) {
509 if (t->has_signal)
510 tuner -> signal = t->has_signal(client);
511 if (t->is_stereo) {
512 if (t->is_stereo(client)) {
513 tuner -> capability |= V4L2_TUNER_CAP_STEREO;
514 tuner -> rxsubchans |= V4L2_TUNER_SUB_STEREO;
515 } else {
516 tuner -> rxsubchans &= 0xffff ^ V4L2_TUNER_SUB_STEREO;
517 }
518 }
519 }
520 /* Wow to deal with V4L2_TUNER_CAP_LOW ? For now, it accepts from low at 62.5KHz step to high at 62.5 Hz */
c184ca36 521 tuner->rangelow = tv_range[0] * 16;
56fc08ca
MCC
522// tuner->rangehigh = tv_range[1] * 16;
523// tuner->rangelow = tv_range[0] * 16384;
524 tuner->rangehigh = tv_range[1] * 16384;
1da177e4
LT
525 break;
526 }
527 default:
56fc08ca 528 tuner_dbg ("Unimplemented IOCTL 0x%08x called to tuner.\n", cmd);
1da177e4
LT
529 /* nothing */
530 break;
531 }
532
533 return 0;
534}
535
56fc08ca 536static int tuner_suspend(struct device * dev, u32 state, u32 level)
1da177e4
LT
537{
538 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
539 struct tuner *t = i2c_get_clientdata(c);
540
541 tuner_dbg("suspend\n");
542 /* FIXME: power down ??? */
543 return 0;
544}
545
546static int tuner_resume(struct device * dev, u32 level)
547{
548 struct i2c_client *c = container_of(dev, struct i2c_client, dev);
549 struct tuner *t = i2c_get_clientdata(c);
550
551 tuner_dbg("resume\n");
552 if (t->freq)
553 set_freq(c,t->freq);
554 return 0;
555}
556
557/* ----------------------------------------------------------------------- */
558
559static struct i2c_driver driver = {
560 .owner = THIS_MODULE,
561 .name = "tuner",
562 .id = I2C_DRIVERID_TUNER,
563 .flags = I2C_DF_NOTIFY,
564 .attach_adapter = tuner_probe,
565 .detach_client = tuner_detach,
566 .command = tuner_command,
567 .driver = {
568 .suspend = tuner_suspend,
569 .resume = tuner_resume,
570 },
571};
572static struct i2c_client client_template =
573{
574 I2C_DEVNAME("(tuner unset)"),
575 .flags = I2C_CLIENT_ALLOW_USE,
576 .driver = &driver,
577};
578
579static int __init tuner_init_module(void)
580{
581 return i2c_add_driver(&driver);
582}
583
584static void __exit tuner_cleanup_module(void)
585{
586 i2c_del_driver(&driver);
587}
588
589module_init(tuner_init_module);
590module_exit(tuner_cleanup_module);
591
592/*
593 * Overrides for Emacs so that we follow Linus's tabbing style.
594 * ---------------------------------------------------------------------------
595 * Local variables:
596 * c-basic-offset: 8
597 * End:
598 */