[PATCH] v4l: 655: added support for the philips td1316 tuner
[linux-block.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>
8#include <linux/moduleparam.h>
9#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/string.h>
12#include <linux/timer.h>
13#include <linux/delay.h>
14#include <linux/errno.h>
15#include <linux/slab.h>
16#include <linux/poll.h>
17#include <linux/i2c.h>
18#include <linux/types.h>
19#include <linux/videodev.h>
20#include <linux/init.h>
21
22#include <media/tuner.h>
23#include <media/audiochip.h>
24
fd3113e8
MCC
25#include "msp3400.h"
26
1da177e4
LT
27#define UNSET (-1U)
28
29/* standard i2c insmod options */
30static unsigned short normal_i2c[] = {
f7ce3cc6 31 0x4b, /* tda8290 */
f5bec396
MCC
32 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
33 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
1da177e4
LT
34 I2C_CLIENT_END
35};
f7ce3cc6 36
1da177e4
LT
37I2C_CLIENT_INSMOD;
38
39/* insmod options used at init time => read/only */
f7ce3cc6 40static unsigned int addr = 0;
1da177e4
LT
41module_param(addr, int, 0444);
42
c5287ba1
MCC
43static unsigned int no_autodetect = 0;
44module_param(no_autodetect, int, 0444);
45
fd3113e8
MCC
46static unsigned int show_i2c = 0;
47module_param(show_i2c, int, 0444);
48
1da177e4 49/* insmod options used at runtime => read/write */
f7ce3cc6
MCC
50unsigned int tuner_debug = 0;
51module_param(tuner_debug, int, 0644);
1da177e4 52
f7ce3cc6 53static unsigned int tv_range[2] = { 44, 958 };
1da177e4
LT
54static unsigned int radio_range[2] = { 65, 108 };
55
f7ce3cc6 56module_param_array(tv_range, int, NULL, 0644);
1da177e4
LT
57module_param_array(radio_range, int, NULL, 0644);
58
59MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
60MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
61MODULE_LICENSE("GPL");
62
1da177e4
LT
63static struct i2c_driver driver;
64static struct i2c_client client_template;
65
66/* ---------------------------------------------------------------------- */
67
56fc08ca 68/* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
1da177e4
LT
69static void set_tv_freq(struct i2c_client *c, unsigned int freq)
70{
71 struct tuner *t = i2c_get_clientdata(c);
72
73 if (t->type == UNSET) {
f7ce3cc6 74 tuner_warn ("tuner type not set\n");
1da177e4
LT
75 return;
76 }
77 if (NULL == t->tv_freq) {
f7ce3cc6 78 tuner_warn ("Tuner has no way to set tv freq\n");
1da177e4
LT
79 return;
80 }
f7ce3cc6
MCC
81 if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
82 tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
83 freq / 16, freq % 16 * 100 / 16, tv_range[0],
84 tv_range[1]);
1da177e4 85 }
f7ce3cc6 86 t->tv_freq(c, freq);
1da177e4
LT
87}
88
89static void set_radio_freq(struct i2c_client *c, unsigned int freq)
90{
91 struct tuner *t = i2c_get_clientdata(c);
92
93 if (t->type == UNSET) {
f7ce3cc6 94 tuner_warn ("tuner type not set\n");
1da177e4
LT
95 return;
96 }
97 if (NULL == t->radio_freq) {
f7ce3cc6 98 tuner_warn ("tuner has no way to set radio frequency\n");
1da177e4
LT
99 return;
100 }
f7ce3cc6
MCC
101 if (freq <= radio_range[0] * 16000 || freq >= radio_range[1] * 16000) {
102 tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
103 freq / 16000, freq % 16000 * 100 / 16000,
104 radio_range[0], radio_range[1]);
1da177e4 105 }
586b0cab 106
f7ce3cc6 107 t->radio_freq(c, freq);
586b0cab 108 return;
1da177e4
LT
109}
110
111static void set_freq(struct i2c_client *c, unsigned long freq)
112{
113 struct tuner *t = i2c_get_clientdata(c);
114
115 switch (t->mode) {
116 case V4L2_TUNER_RADIO:
117 tuner_dbg("radio freq set to %lu.%02lu\n",
f7ce3cc6
MCC
118 freq / 16000, freq % 16000 * 100 / 16000);
119 set_radio_freq(c, freq);
1da177e4
LT
120 break;
121 case V4L2_TUNER_ANALOG_TV:
122 case V4L2_TUNER_DIGITAL_TV:
123 tuner_dbg("tv freq set to %lu.%02lu\n",
f7ce3cc6 124 freq / 16, freq % 16 * 100 / 16);
1da177e4
LT
125 set_tv_freq(c, freq);
126 break;
127 }
128 t->freq = freq;
129}
130
f7ce3cc6
MCC
131static void set_type(struct i2c_client *c, unsigned int type,
132 unsigned int new_mode_mask)
1da177e4
LT
133{
134 struct tuner *t = i2c_get_clientdata(c);
586b0cab 135 unsigned char buffer[4];
1da177e4 136
f7ce3cc6
MCC
137 if (type == UNSET || type == TUNER_ABSENT) {
138 tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
1da177e4 139 return;
f7ce3cc6
MCC
140 }
141
142 if (type >= tuner_count) {
143 tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
1da177e4 144 return;
f7ce3cc6 145 }
1da177e4 146
f7ce3cc6 147 /* This code detects calls by card attach_inform */
1da177e4 148 if (NULL == t->i2c.dev.driver) {
f7ce3cc6
MCC
149 tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
150
151 t->type=type;
1da177e4
LT
152 return;
153 }
56fc08ca 154
1da177e4 155 t->type = type;
f7ce3cc6 156
1da177e4
LT
157 switch (t->type) {
158 case TUNER_MT2032:
159 microtune_init(c);
160 break;
161 case TUNER_PHILIPS_TDA8290:
162 tda8290_init(c);
163 break;
586b0cab 164 case TUNER_TEA5767:
f7ce3cc6
MCC
165 if (tea5767_tuner_init(c) == EINVAL) {
166 t->type = TUNER_ABSENT;
167 t->mode_mask = T_UNINITIALIZED;
168 return;
169 }
170 t->mode_mask = T_RADIO;
586b0cab
MCC
171 break;
172 case TUNER_PHILIPS_FMD1216ME_MK3:
173 buffer[0] = 0x0b;
174 buffer[1] = 0xdc;
175 buffer[2] = 0x9c;
176 buffer[3] = 0x60;
f7ce3cc6 177 i2c_master_send(c, buffer, 4);
586b0cab
MCC
178 mdelay(1);
179 buffer[2] = 0x86;
180 buffer[3] = 0x54;
f7ce3cc6 181 i2c_master_send(c, buffer, 4);
586b0cab
MCC
182 default_tuner_init(c);
183 break;
793cf9e6
MCC
184 case TUNER_LG_TDVS_H062F:
185 /* Set the Auxiliary Byte. */
186 buffer[2] &= ~0x20;
187 buffer[2] |= 0x18;
188 buffer[3] = 0x20;
189 i2c_master_send(c, buffer, 4);
190 default_tuner_init(c);
191 break;
93df3413
HH
192 case TUNER_PHILIPS_TD1316:
193 buffer[0] = 0x0b;
194 buffer[1] = 0xdc;
195 buffer[2] = 0x86;
196 buffer[3] = 0xa4;
197 i2c_master_send(c,buffer,4);
198 default_tuner_init(c);
1da177e4
LT
199 default:
200 default_tuner_init(c);
201 break;
202 }
f7ce3cc6
MCC
203
204 if (t->mode_mask == T_UNINITIALIZED)
205 t->mode_mask = new_mode_mask;
206
207 set_freq(c, t->freq);
208 tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
209 c->adapter->name, c->driver->name, c->addr << 1, type,
210 t->mode_mask);
1da177e4
LT
211}
212
f7ce3cc6
MCC
213/*
214 * This function apply tuner config to tuner specified
215 * by tun_setup structure. I addr is unset, then admin status
216 * and tun addr status is more precise then current status,
217 * it's applied. Otherwise status and type are applied only to
218 * tuner with exactly the same addr.
219*/
220
221static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
222{
223 struct tuner *t = i2c_get_clientdata(c);
224
793cf9e6
MCC
225 if ((tun_setup->addr == ADDR_UNSET &&
226 (t->mode_mask & tun_setup->mode_mask)) ||
227 tun_setup->addr == c->addr) {
f7ce3cc6 228 set_type(c, tun_setup->type, tun_setup->mode_mask);
f7ce3cc6
MCC
229 }
230}
56fc08ca 231
f7ce3cc6 232static inline int check_mode(struct tuner *t, char *cmd)
56fc08ca 233{
793cf9e6
MCC
234 if ((1 << t->mode & t->mode_mask) == 0) {
235 return EINVAL;
236 }
237
238 switch (t->mode) {
239 case V4L2_TUNER_RADIO:
240 tuner_dbg("Cmd %s accepted for radio\n", cmd);
241 break;
242 case V4L2_TUNER_ANALOG_TV:
243 tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
244 break;
245 case V4L2_TUNER_DIGITAL_TV:
246 tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
247 break;
56fc08ca 248 }
793cf9e6 249 return 0;
56fc08ca 250}
56fc08ca 251
1da177e4 252static char pal[] = "-";
975e046c 253module_param_string(pal, pal, sizeof(pal), 0644);
f7ce3cc6
MCC
254static char secam[] = "-";
255module_param_string(secam, secam, sizeof(secam), 0644);
1da177e4 256
f7ce3cc6 257/* get more precise norm info from insmod option */
1da177e4
LT
258static int tuner_fixup_std(struct tuner *t)
259{
260 if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
1da177e4
LT
261 switch (pal[0]) {
262 case 'b':
263 case 'B':
264 case 'g':
265 case 'G':
f7ce3cc6 266 tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
1da177e4
LT
267 t->std = V4L2_STD_PAL_BG;
268 break;
269 case 'i':
270 case 'I':
f7ce3cc6 271 tuner_dbg ("insmod fixup: PAL => PAL-I\n");
1da177e4
LT
272 t->std = V4L2_STD_PAL_I;
273 break;
274 case 'd':
275 case 'D':
276 case 'k':
277 case 'K':
f7ce3cc6 278 tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
1da177e4
LT
279 t->std = V4L2_STD_PAL_DK;
280 break;
f7ce3cc6
MCC
281 case 'M':
282 case 'm':
283 tuner_dbg ("insmod fixup: PAL => PAL-M\n");
284 t->std = V4L2_STD_PAL_M;
285 break;
286 case 'N':
287 case 'n':
288 tuner_dbg ("insmod fixup: PAL => PAL-N\n");
289 t->std = V4L2_STD_PAL_N;
290 break;
21d4df37
MCC
291 case '-':
292 /* default parameter, do nothing */
293 break;
294 default:
295 tuner_warn ("pal= argument not recognised\n");
296 break;
1da177e4
LT
297 }
298 }
f7ce3cc6
MCC
299 if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
300 switch (secam[0]) {
301 case 'd':
302 case 'D':
303 case 'k':
304 case 'K':
305 tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
306 t->std = V4L2_STD_SECAM_DK;
307 break;
308 case 'l':
309 case 'L':
310 tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
311 t->std = V4L2_STD_SECAM_L;
312 break;
21d4df37
MCC
313 case '-':
314 /* default parameter, do nothing */
315 break;
316 default:
317 tuner_warn ("secam= argument not recognised\n");
318 break;
f7ce3cc6
MCC
319 }
320 }
321
1da177e4
LT
322 return 0;
323}
324
325/* ---------------------------------------------------------------------- */
326
f7ce3cc6
MCC
327/* static var Used only in tuner_attach and tuner_probe */
328static unsigned default_mode_mask;
329
330/* During client attach, set_type is called by adapter's attach_inform callback.
331 set_type must then be completed by tuner_attach.
332 */
1da177e4
LT
333static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
334{
335 struct tuner *t;
336
f7ce3cc6
MCC
337 client_template.adapter = adap;
338 client_template.addr = addr;
1da177e4 339
f7ce3cc6
MCC
340 t = kmalloc(sizeof(struct tuner), GFP_KERNEL);
341 if (NULL == t)
342 return -ENOMEM;
343 memset(t, 0, sizeof(struct tuner));
344 memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
1da177e4 345 i2c_set_clientdata(&t->i2c, t);
f7ce3cc6
MCC
346 t->type = UNSET;
347 t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
348 t->audmode = V4L2_TUNER_MODE_STEREO;
349 t->mode_mask = T_UNINITIALIZED;
350
351
352 tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
353
fd3113e8
MCC
354 if (show_i2c) {
355 unsigned char buffer[16];
356 int i,rc;
357
358 memset(buffer, 0, sizeof(buffer));
359 rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
360 printk("tuner-%04x I2C RECV = ",addr);
361 for (i=0;i<rc;i++)
362 printk("%02x ",buffer[i]);
363 printk("\n");
364 }
f7ce3cc6 365 /* TEA5767 autodetection code - only for addr = 0xc0 */
c5287ba1
MCC
366 if (!no_autodetect) {
367 if (addr == 0x60) {
368 if (tea5767_autodetection(&t->i2c) != EINVAL) {
369 t->type = TUNER_TEA5767;
370 t->mode_mask = T_RADIO;
371 t->mode = T_STANDBY;
372 t->freq = 87.5 * 16; /* Sets freq to FM range */
373 default_mode_mask &= ~T_RADIO;
374
375 i2c_attach_client (&t->i2c);
376 set_type(&t->i2c,t->type, t->mode_mask);
377 return 0;
378 }
f7ce3cc6
MCC
379 }
380 }
1da177e4 381
f7ce3cc6
MCC
382 /* Initializes only the first adapter found */
383 if (default_mode_mask != T_UNINITIALIZED) {
384 tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
385 t->mode_mask = default_mode_mask;
386 t->freq = 400 * 16; /* Sets freq to VHF High */
387 default_mode_mask = T_UNINITIALIZED;
388 }
56fc08ca 389
f7ce3cc6
MCC
390 /* Should be just before return */
391 i2c_attach_client (&t->i2c);
392 set_type (&t->i2c,t->type, t->mode_mask);
1da177e4
LT
393 return 0;
394}
395
396static int tuner_probe(struct i2c_adapter *adap)
397{
398 if (0 != addr) {
f5bec396
MCC
399 normal_i2c[0] = addr;
400 normal_i2c[1] = I2C_CLIENT_END;
1da177e4 401 }
1da177e4 402
f7ce3cc6 403 default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
391cd727 404
1da177e4
LT
405 if (adap->class & I2C_CLASS_TV_ANALOG)
406 return i2c_probe(adap, &addr_data, tuner_attach);
407 return 0;
408}
409
410static int tuner_detach(struct i2c_client *client)
411{
412 struct tuner *t = i2c_get_clientdata(client);
391cd727
MCC
413 int err;
414
f7ce3cc6 415 err = i2c_detach_client(&t->i2c);
391cd727 416 if (err) {
f7ce3cc6
MCC
417 tuner_warn
418 ("Client deregistration failed, client not detached.\n");
391cd727
MCC
419 return err;
420 }
1da177e4 421
1da177e4
LT
422 kfree(t);
423 return 0;
424}
425
f7ce3cc6
MCC
426/*
427 * Switch tuner to other mode. If tuner support both tv and radio,
428 * set another frequency to some value (This is needed for some pal
429 * tuners to avoid locking). Otherwise, just put second tuner in
430 * standby mode.
431 */
432
433static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
434{
793cf9e6
MCC
435 if (mode == t->mode)
436 return 0;
437
438 t->mode = mode;
439
440 if (check_mode(t, cmd) == EINVAL) {
441 t->mode = T_STANDBY;
442 if (t->standby)
443 t->standby (client);
444 return EINVAL;
445 }
446 return 0;
f7ce3cc6
MCC
447}
448
449#define switch_v4l2() if (!t->using_v4l2) \
450 tuner_dbg("switching to v4l2\n"); \
451 t->using_v4l2 = 1;
452
453static inline int check_v4l2(struct tuner *t)
454{
455 if (t->using_v4l2) {
456 tuner_dbg ("ignore v4l1 call\n");
457 return EINVAL;
458 }
459 return 0;
460}
1da177e4 461
f7ce3cc6 462static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
1da177e4
LT
463{
464 struct tuner *t = i2c_get_clientdata(client);
f7ce3cc6 465 unsigned int *iarg = (int *)arg;
1da177e4 466
f7ce3cc6 467 switch (cmd) {
1da177e4 468 /* --- configuration --- */
56fc08ca 469 case TUNER_SET_TYPE_ADDR:
f7ce3cc6
MCC
470 tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
471 ((struct tuner_setup *)arg)->type,
472 ((struct tuner_setup *)arg)->addr,
473 ((struct tuner_setup *)arg)->mode_mask);
474
475 set_addr(client, (struct tuner_setup *)arg);
391cd727 476 break;
1da177e4 477 case AUDC_SET_RADIO:
f7ce3cc6 478 set_mode(client,t,V4L2_TUNER_RADIO, "AUDC_SET_RADIO");
1da177e4 479 break;
793cf9e6
MCC
480 case TUNER_SET_STANDBY:
481 {
482 if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
483 return 0;
484 if (t->standby)
485 t->standby (client);
486 break;
487 }
1da177e4 488 case AUDC_CONFIG_PINNACLE:
f7ce3cc6
MCC
489 if (check_mode(t, "AUDC_CONFIG_PINNACLE") == EINVAL)
490 return 0;
1da177e4
LT
491 switch (*iarg) {
492 case 2:
493 tuner_dbg("pinnacle pal\n");
494 t->radio_if2 = 33300 * 1000;
495 break;
496 case 3:
497 tuner_dbg("pinnacle ntsc\n");
498 t->radio_if2 = 41300 * 1000;
499 break;
500 }
586b0cab 501 break;
fd3113e8
MCC
502 case VIDIOCSAUDIO:
503 if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
504 return 0;
505 if (check_v4l2(t) == EINVAL)
506 return 0;
507
508 /* Should be implemented, since bttv calls it */
509 tuner_dbg("VIDIOCSAUDIO not implemented.\n");
510
511 break;
512 case MSP_SET_MATRIX:
f7ce3cc6
MCC
513 case TDA9887_SET_CONFIG:
514 break;
1da177e4
LT
515 /* --- v4l ioctls --- */
516 /* take care: bttv does userspace copying, we'll get a
517 kernel pointer here... */
518 case VIDIOCSCHAN:
f7ce3cc6
MCC
519 {
520 static const v4l2_std_id map[] = {
521 [VIDEO_MODE_PAL] = V4L2_STD_PAL,
522 [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
523 [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
524 [4 /* bttv */ ] = V4L2_STD_PAL_M,
525 [5 /* bttv */ ] = V4L2_STD_PAL_N,
526 [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
527 };
528 struct video_channel *vc = arg;
529
530 if (check_v4l2(t) == EINVAL)
531 return 0;
532
533 if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
534 return 0;
535
536 if (vc->norm < ARRAY_SIZE(map))
537 t->std = map[vc->norm];
538 tuner_fixup_std(t);
539 if (t->freq)
540 set_tv_freq(client, t->freq);
541 return 0;
542 }
1da177e4 543 case VIDIOCSFREQ:
f7ce3cc6
MCC
544 {
545 unsigned long *v = arg;
1da177e4 546
f7ce3cc6
MCC
547 if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
548 return 0;
549 if (check_v4l2(t) == EINVAL)
550 return 0;
551
552 set_freq(client, *v);
553 return 0;
554 }
1da177e4 555 case VIDIOCGTUNER:
f7ce3cc6
MCC
556 {
557 struct video_tuner *vt = arg;
558
559 if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
560 return 0;
561 if (check_v4l2(t) == EINVAL)
562 return 0;
563
564 if (V4L2_TUNER_RADIO == t->mode) {
565 if (t->has_signal)
566 vt->signal = t->has_signal(client);
567 if (t->is_stereo) {
568 if (t->is_stereo(client))
569 vt->flags |=
570 VIDEO_TUNER_STEREO_ON;
571 else
572 vt->flags &=
573 ~VIDEO_TUNER_STEREO_ON;
574 }
575 vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
586b0cab 576
f7ce3cc6
MCC
577 vt->rangelow = radio_range[0] * 16000;
578 vt->rangehigh = radio_range[1] * 16000;
586b0cab 579
f7ce3cc6
MCC
580 } else {
581 vt->rangelow = tv_range[0] * 16;
582 vt->rangehigh = tv_range[1] * 16;
583 }
56fc08ca 584
f7ce3cc6
MCC
585 return 0;
586 }
1da177e4 587 case VIDIOCGAUDIO:
f7ce3cc6
MCC
588 {
589 struct video_audio *va = arg;
590
591 if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
592 return 0;
593 if (check_v4l2(t) == EINVAL)
594 return 0;
595
596 if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
597 va->mode = t->is_stereo(client)
598 ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
599 return 0;
600 }
1da177e4
LT
601
602 case VIDIOC_S_STD:
f7ce3cc6
MCC
603 {
604 v4l2_std_id *id = arg;
1da177e4 605
f7ce3cc6
MCC
606 if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
607 == EINVAL)
608 return 0;
56fc08ca 609
f7ce3cc6
MCC
610 switch_v4l2();
611
612 t->std = *id;
613 tuner_fixup_std(t);
614 if (t->freq)
615 set_freq(client, t->freq);
616 break;
617 }
1da177e4 618 case VIDIOC_S_FREQUENCY:
f7ce3cc6
MCC
619 {
620 struct v4l2_frequency *f = arg;
621
622 t->freq = f->frequency;
623 switch_v4l2();
624 if (V4L2_TUNER_RADIO == f->type &&
625 V4L2_TUNER_RADIO != t->mode) {
626 if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
627 == EINVAL)
628 return 0;
629 }
630 set_freq(client,t->freq);
c184ca36 631
f7ce3cc6
MCC
632 break;
633 }
634 case VIDIOC_G_FREQUENCY:
635 {
636 struct v4l2_frequency *f = arg;
637
638 if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
639 return 0;
640 switch_v4l2();
641 f->type = t->mode;
642 f->frequency = t->freq;
643 break;
644 }
1da177e4 645 case VIDIOC_G_TUNER:
f7ce3cc6
MCC
646 {
647 struct v4l2_tuner *tuner = arg;
648
649 if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
650 return 0;
651 switch_v4l2();
652
653 if (V4L2_TUNER_RADIO == t->mode) {
654
655 if (t->has_signal)
656 tuner->signal = t->has_signal(client);
657
658 if (t->is_stereo) {
659 if (t->is_stereo(client)) {
660 tuner->rxsubchans =
661 V4L2_TUNER_SUB_STEREO |
662 V4L2_TUNER_SUB_MONO;
663 } else {
664 tuner->rxsubchans =
665 V4L2_TUNER_SUB_MONO;
666 }
56fc08ca 667 }
f7ce3cc6
MCC
668
669 tuner->capability |=
670 V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
671
672 tuner->audmode = t->audmode;
673
674 tuner->rangelow = radio_range[0] * 16000;
675 tuner->rangehigh = radio_range[1] * 16000;
676 } else {
677 tuner->rangelow = tv_range[0] * 16;
678 tuner->rangehigh = tv_range[1] * 16;
56fc08ca 679 }
f7ce3cc6
MCC
680 break;
681 }
682 case VIDIOC_S_TUNER:
683 {
684 struct v4l2_tuner *tuner = arg;
685
686 if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
687 return 0;
688
689 switch_v4l2();
690
691 if (V4L2_TUNER_RADIO == t->mode) {
692 t->audmode = tuner->audmode;
693 set_radio_freq(client, t->freq);
694 }
695 break;
56fc08ca 696 }
1da177e4 697 default:
c5287ba1
MCC
698 tuner_dbg("Unimplemented IOCTL 0x%08x(dir=%d,tp=0x%02x,nr=%d,sz=%d)\n",
699 cmd, _IOC_DIR(cmd), _IOC_TYPE(cmd),
700 _IOC_NR(cmd), _IOC_SIZE(cmd));
1da177e4
LT
701 break;
702 }
703
704 return 0;
705}
706
9480e307 707static int tuner_suspend(struct device *dev, pm_message_t state)
1da177e4 708{
f7ce3cc6
MCC
709 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
710 struct tuner *t = i2c_get_clientdata (c);
1da177e4 711
f7ce3cc6 712 tuner_dbg ("suspend\n");
1da177e4
LT
713 /* FIXME: power down ??? */
714 return 0;
715}
716
9480e307 717static int tuner_resume(struct device *dev)
1da177e4 718{
f7ce3cc6
MCC
719 struct i2c_client *c = container_of (dev, struct i2c_client, dev);
720 struct tuner *t = i2c_get_clientdata (c);
1da177e4 721
f7ce3cc6 722 tuner_dbg ("resume\n");
1da177e4 723 if (t->freq)
f7ce3cc6 724 set_freq(c, t->freq);
1da177e4
LT
725 return 0;
726}
727
728/* ----------------------------------------------------------------------- */
729
730static struct i2c_driver driver = {
f7ce3cc6
MCC
731 .owner = THIS_MODULE,
732 .name = "tuner",
733 .id = I2C_DRIVERID_TUNER,
734 .flags = I2C_DF_NOTIFY,
735 .attach_adapter = tuner_probe,
736 .detach_client = tuner_detach,
737 .command = tuner_command,
1da177e4 738 .driver = {
f7ce3cc6
MCC
739 .suspend = tuner_suspend,
740 .resume = tuner_resume,
741 },
1da177e4 742};
f7ce3cc6 743static struct i2c_client client_template = {
fae91e72 744 .name = "(tuner unset)",
f7ce3cc6
MCC
745 .flags = I2C_CLIENT_ALLOW_USE,
746 .driver = &driver,
1da177e4
LT
747};
748
749static int __init tuner_init_module(void)
750{
751 return i2c_add_driver(&driver);
752}
753
754static void __exit tuner_cleanup_module(void)
755{
756 i2c_del_driver(&driver);
757}
758
759module_init(tuner_init_module);
760module_exit(tuner_cleanup_module);
761
762/*
763 * Overrides for Emacs so that we follow Linus's tabbing style.
764 * ---------------------------------------------------------------------------
765 * Local variables:
766 * c-basic-offset: 8
767 * End:
768 */