blob: 805273e5f91946a61247ac7e48e9302c061d77ca [file] [log] [blame]
Hans Verkuilbd985162005-11-13 16:07:56 -08001/* cx25840 - Conexant CX25840 audio/video decoder driver
2 *
3 * Copyright (C) 2004 Ulf Eklund
4 *
5 * Based on the saa7115 driver and on the first verison of Chris Kennedy's
6 * cx25840 driver.
7 *
8 * Changes by Tyler Trafford <tatrafford@comcast.net>
9 * - cleanup/rewrite for V4L2 API (2005)
10 *
11 * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/slab.h>
32#include <linux/videodev2.h>
33#include <linux/i2c.h>
34#include <media/audiochip.h>
35#include <media/i2c-compat.h>
36#include <media/v4l2-common.h>
37
38#include "cx25840.h"
39
40MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
41MODULE_AUTHOR("Ulf Eklund <ivtv@eklund.to>");
42MODULE_AUTHOR("Chris Kennedy <c@groovy.org>");
43MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
44MODULE_AUTHOR("Tyler Trafford <tatrafford@comcast.net>");
45MODULE_LICENSE("GPL");
46
47static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
48
49
50int cx25840_debug = 0;
51
52module_param(cx25840_debug, bool, 0644);
53
54MODULE_PARM_DESC(cx25840_debug, "Debugging messages [0=Off (default) 1=On]");
55
56I2C_CLIENT_INSMOD;
57
58/* ----------------------------------------------------------------------- */
59
60int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
61{
62 u8 buffer[3];
63 buffer[0] = addr >> 8;
64 buffer[1] = addr & 0xff;
65 buffer[2] = value;
66 return i2c_master_send(client, buffer, 3);
67}
68
69int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
70{
71 u8 buffer[6];
72 buffer[0] = addr >> 8;
73 buffer[1] = addr & 0xff;
74 buffer[2] = value >> 24;
75 buffer[3] = (value >> 16) & 0xff;
76 buffer[4] = (value >> 8) & 0xff;
77 buffer[5] = value & 0xff;
78 return i2c_master_send(client, buffer, 6);
79}
80
81u8 cx25840_read(struct i2c_client * client, u16 addr)
82{
83 u8 buffer[2];
84 buffer[0] = addr >> 8;
85 buffer[1] = addr & 0xff;
86
87 if (i2c_master_send(client, buffer, 2) < 2)
88 return 0;
89
90 if (i2c_master_recv(client, buffer, 1) < 1)
91 return 0;
92
93 return buffer[0];
94}
95
96u32 cx25840_read4(struct i2c_client * client, u16 addr)
97{
98 u8 buffer[4];
99 buffer[0] = addr >> 8;
100 buffer[1] = addr & 0xff;
101
102 if (i2c_master_send(client, buffer, 2) < 2)
103 return 0;
104
105 if (i2c_master_recv(client, buffer, 4) < 4)
106 return 0;
107
108 return (buffer[0] << 24) | (buffer[1] << 16) |
109 (buffer[2] << 8) | buffer[3];
110}
111
112int cx25840_and_or(struct i2c_client *client, u16 addr, u8 and_mask,
113 u8 or_value)
114{
115 return cx25840_write(client, addr,
116 (cx25840_read(client, addr) & and_mask) |
117 or_value);
118}
119
120/* ----------------------------------------------------------------------- */
121
122static int set_input(struct i2c_client *, enum cx25840_input);
123static void input_change(struct i2c_client *);
124static void log_status(struct i2c_client *client);
125
126/* ----------------------------------------------------------------------- */
127
128static inline void init_dll1(struct i2c_client *client)
129{
130 /* This is the Hauppauge sequence used to
131 * initialize the Delay Lock Loop 1 (ADC DLL). */
132 cx25840_write(client, 0x159, 0x23);
133 cx25840_write(client, 0x15a, 0x87);
134 cx25840_write(client, 0x15b, 0x06);
135 cx25840_write(client, 0x159, 0xe1);
136 cx25840_write(client, 0x15a, 0x86);
137 cx25840_write(client, 0x159, 0xe0);
138 cx25840_write(client, 0x159, 0xe1);
139 cx25840_write(client, 0x15b, 0x10);
140}
141
142static inline void init_dll2(struct i2c_client *client)
143{
144 /* This is the Hauppauge sequence used to
145 * initialize the Delay Lock Loop 2 (ADC DLL). */
146 cx25840_write(client, 0x15d, 0xe3);
147 cx25840_write(client, 0x15e, 0x86);
148 cx25840_write(client, 0x15f, 0x06);
149 cx25840_write(client, 0x15d, 0xe1);
150 cx25840_write(client, 0x15d, 0xe0);
151 cx25840_write(client, 0x15d, 0xe1);
152}
153
154static void cx25840_initialize(struct i2c_client *client, int loadfw)
155{
156 struct cx25840_state *state = i2c_get_clientdata(client);
157
158 /* datasheet startup in numbered steps, refer to page 3-77 */
159 /* 2. */
160 cx25840_and_or(client, 0x803, ~0x10, 0x00);
161 /* The default of this register should be 4, but I get 0 instead.
162 * Set this register to 4 manually. */
163 cx25840_write(client, 0x000, 0x04);
164 /* 3. */
165 init_dll1(client);
166 init_dll2(client);
167 cx25840_write(client, 0x136, 0x0a);
168 /* 4. */
169 cx25840_write(client, 0x13c, 0x01);
170 cx25840_write(client, 0x13c, 0x00);
171 /* 5. */
172 if (loadfw)
173 cx25840_loadfw(client);
174 /* 6. */
175 cx25840_write(client, 0x115, 0x8c);
176 cx25840_write(client, 0x116, 0x07);
177 cx25840_write(client, 0x118, 0x02);
178 /* 7. */
179 cx25840_write(client, 0x4a5, 0x80);
180 cx25840_write(client, 0x4a5, 0x00);
181 cx25840_write(client, 0x402, 0x00);
182 /* 8. */
183 cx25840_write(client, 0x401, 0x18);
184 cx25840_write(client, 0x4a2, 0x10);
185 cx25840_write(client, 0x402, 0x04);
186 /* 10. */
187 cx25840_write(client, 0x8d3, 0x1f);
188 cx25840_write(client, 0x8e3, 0x03);
189
190 cx25840_vbi_setup(client);
191
192 /* trial and error says these are needed to get audio */
193 cx25840_write(client, 0x914, 0xa0);
194 cx25840_write(client, 0x918, 0xa0);
195 cx25840_write(client, 0x919, 0x01);
196
197 /* stereo prefered */
198 cx25840_write(client, 0x809, 0x04);
199 /* AC97 shift */
200 cx25840_write(client, 0x8cf, 0x0f);
201
202 /* (re)set video input */
203 set_input(client, state->input);
204 /* (re)set audio input */
205 cx25840_audio(client, AUDC_SET_INPUT, &state->audio_input);
206
207 /* start microcontroller */
208 cx25840_and_or(client, 0x803, ~0x10, 0x10);
209}
210
211/* ----------------------------------------------------------------------- */
212
213static void input_change(struct i2c_client *client)
214{
215 v4l2_std_id std = cx25840_get_v4lstd(client);
216
217 if (std & V4L2_STD_PAL) {
218 /* Follow tuner change procedure for PAL */
219 cx25840_write(client, 0x808, 0xff);
220 cx25840_write(client, 0x80b, 0x10);
221 } else if (std & V4L2_STD_SECAM) {
222 /* Select autodetect for SECAM */
223 cx25840_write(client, 0x808, 0xff);
224 cx25840_write(client, 0x80b, 0x10);
225 } else if (std & V4L2_STD_NTSC) {
226 /* NTSC */
227 cx25840_write(client, 0x808, 0xf6);
228 cx25840_write(client, 0x80b, 0x00);
229 }
230
231 if (cx25840_read(client, 0x803) & 0x10) {
232 /* restart audio decoder microcontroller */
233 cx25840_and_or(client, 0x803, ~0x10, 0x00);
234 cx25840_and_or(client, 0x803, ~0x10, 0x10);
235 }
236}
237
238static int set_input(struct i2c_client *client, enum cx25840_input input)
239{
240 struct cx25840_state *state = i2c_get_clientdata(client);
241
242 cx25840_dbg("decoder set input (%d)\n", input);
243
244 switch (input) {
245 case CX25840_TUNER:
246 cx25840_dbg("now setting Tuner input\n");
247
248 if (state->cardtype == CARDTYPE_PVR150) {
249 /* CH_SEL_ADC2=1 */
250 cx25840_and_or(client, 0x102, ~0x2, 0x02);
251 }
252
253 /* Video Input Control */
254 if (state->cardtype == CARDTYPE_PG600) {
255 cx25840_write(client, 0x103, 0x11);
256 } else {
257 cx25840_write(client, 0x103, 0x46);
258 }
259
260 /* INPUT_MODE=0 */
261 cx25840_and_or(client, 0x401, ~0x6, 0x00);
262 break;
263
264 case CX25840_COMPOSITE0:
265 case CX25840_COMPOSITE1:
266 cx25840_dbg("now setting Composite input\n");
267
268 /* Video Input Control */
269 if (state->cardtype == CARDTYPE_PG600) {
270 cx25840_write(client, 0x103, 0x00);
271 } else {
272 cx25840_write(client, 0x103, 0x02);
273 }
274
275 /* INPUT_MODE=0 */
276 cx25840_and_or(client, 0x401, ~0x6, 0x00);
277 break;
278
279 case CX25840_SVIDEO0:
280 case CX25840_SVIDEO1:
281 cx25840_dbg("now setting S-Video input\n");
282
283 /* CH_SEL_ADC2=0 */
284 cx25840_and_or(client, 0x102, ~0x2, 0x00);
285
286 /* Video Input Control */
287 if (state->cardtype == CARDTYPE_PG600) {
288 cx25840_write(client, 0x103, 0x02);
289 } else {
290 cx25840_write(client, 0x103, 0x10);
291 }
292
293 /* INPUT_MODE=1 */
294 cx25840_and_or(client, 0x401, ~0x6, 0x02);
295 break;
296
297 default:
298 cx25840_err("%d is not a valid input!\n", input);
299 return -EINVAL;
300 }
301
302 state->input = input;
303 input_change(client);
304 return 0;
305}
306
307/* ----------------------------------------------------------------------- */
308
309static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
310{
311 u8 fmt;
312
313 switch (std) {
314 /* zero is autodetect */
315 case 0: fmt = 0x0; break;
316 /* default ntsc to ntsc-m */
317 case V4L2_STD_NTSC:
318 case V4L2_STD_NTSC_M: fmt = 0x1; break;
319 case V4L2_STD_NTSC_M_JP: fmt = 0x2; break;
320 case V4L2_STD_NTSC_443: fmt = 0x3; break;
321 case V4L2_STD_PAL: fmt = 0x4; break;
322 case V4L2_STD_PAL_M: fmt = 0x5; break;
323 case V4L2_STD_PAL_N: fmt = 0x6; break;
324 case V4L2_STD_PAL_Nc: fmt = 0x7; break;
325 case V4L2_STD_PAL_60: fmt = 0x8; break;
326 case V4L2_STD_SECAM: fmt = 0xc; break;
327 default:
328 return -ERANGE;
329 }
330
331 cx25840_and_or(client, 0x400, ~0xf, fmt);
332 cx25840_vbi_setup(client);
333 return 0;
334}
335
336v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
337{
338 /* check VID_FMT_SEL first */
339 u8 fmt = cx25840_read(client, 0x400) & 0xf;
340
341 if (!fmt) {
342 /* check AFD_FMT_STAT if set to autodetect */
343 fmt = cx25840_read(client, 0x40d) & 0xf;
344 }
345
346 switch (fmt) {
347 case 0x1: return V4L2_STD_NTSC_M;
348 case 0x2: return V4L2_STD_NTSC_M_JP;
349 case 0x3: return V4L2_STD_NTSC_443;
350 case 0x4: return V4L2_STD_PAL;
351 case 0x5: return V4L2_STD_PAL_M;
352 case 0x6: return V4L2_STD_PAL_N;
353 case 0x7: return V4L2_STD_PAL_Nc;
354 case 0x8: return V4L2_STD_PAL_60;
355 case 0xc: return V4L2_STD_SECAM;
356 default: return V4L2_STD_UNKNOWN;
357 }
358}
359
360/* ----------------------------------------------------------------------- */
361
362static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
363{
364 struct cx25840_state *state = i2c_get_clientdata(client);
365
366 switch (ctrl->id) {
367 case CX25840_CID_CARDTYPE:
368 switch (ctrl->value) {
369 case CARDTYPE_PVR150:
370 case CARDTYPE_PG600:
371 state->cardtype = ctrl->value;
372 break;
373 default:
374 return -ERANGE;
375 }
376
377 set_input(client, state->input);
378 break;
379
380 case V4L2_CID_BRIGHTNESS:
381 if (ctrl->value < 0 || ctrl->value > 255) {
382 cx25840_err("invalid brightness setting %d\n",
383 ctrl->value);
384 return -ERANGE;
385 }
386
387 cx25840_write(client, 0x414, ctrl->value - 128);
388 break;
389
390 case V4L2_CID_CONTRAST:
391 if (ctrl->value < 0 || ctrl->value > 127) {
392 cx25840_err("invalid contrast setting %d\n",
393 ctrl->value);
394 return -ERANGE;
395 }
396
397 cx25840_write(client, 0x415, ctrl->value << 1);
398 break;
399
400 case V4L2_CID_SATURATION:
401 if (ctrl->value < 0 || ctrl->value > 127) {
402 cx25840_err("invalid saturation setting %d\n",
403 ctrl->value);
404 return -ERANGE;
405 }
406
407 cx25840_write(client, 0x420, ctrl->value << 1);
408 cx25840_write(client, 0x421, ctrl->value << 1);
409 break;
410
411 case V4L2_CID_HUE:
412 if (ctrl->value < -127 || ctrl->value > 127) {
413 cx25840_err("invalid hue setting %d\n", ctrl->value);
414 return -ERANGE;
415 }
416
417 cx25840_write(client, 0x422, ctrl->value);
418 break;
419
420 case V4L2_CID_AUDIO_VOLUME:
421 case V4L2_CID_AUDIO_BASS:
422 case V4L2_CID_AUDIO_TREBLE:
423 case V4L2_CID_AUDIO_BALANCE:
424 case V4L2_CID_AUDIO_MUTE:
425 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
426 }
427
428 return 0;
429}
430
431static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
432{
433 struct cx25840_state *state = i2c_get_clientdata(client);
434
435 switch (ctrl->id) {
436 case CX25840_CID_CARDTYPE:
437 ctrl->value = state->cardtype;
438 break;
439 case V4L2_CID_BRIGHTNESS:
440 ctrl->value = cx25840_read(client, 0x414) + 128;
441 break;
442 case V4L2_CID_CONTRAST:
443 ctrl->value = cx25840_read(client, 0x415) >> 1;
444 break;
445 case V4L2_CID_SATURATION:
446 ctrl->value = cx25840_read(client, 0x420) >> 1;
447 break;
448 case V4L2_CID_HUE:
449 ctrl->value = cx25840_read(client, 0x422);
450 break;
451 case V4L2_CID_AUDIO_VOLUME:
452 case V4L2_CID_AUDIO_BASS:
453 case V4L2_CID_AUDIO_TREBLE:
454 case V4L2_CID_AUDIO_BALANCE:
455 case V4L2_CID_AUDIO_MUTE:
456 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
457 default:
458 return -EINVAL;
459 }
460
461 return 0;
462}
463
464/* ----------------------------------------------------------------------- */
465
466static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
467{
468 switch (fmt->type) {
469 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
470 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
471 default:
472 return -EINVAL;
473 }
474
475 return 0;
476}
477
478static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
479{
480 struct v4l2_pix_format *pix;
481 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
482 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
483
484 switch (fmt->type) {
485 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
486 pix = &(fmt->fmt.pix);
487
488 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
489 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
490
491 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
492 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
493
494 Vlines = pix->height + (is_pal ? 4 : 7);
495
496 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
497 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
498 cx25840_err("%dx%d is not a valid size!\n",
499 pix->width, pix->height);
500 return -ERANGE;
501 }
502
503 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
504 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
505 VSC &= 0x1fff;
506
507 if (pix->width >= 385)
508 filter = 0;
509 else if (pix->width > 192)
510 filter = 1;
511 else if (pix->width > 96)
512 filter = 2;
513 else
514 filter = 3;
515
516 cx25840_dbg("decoder set size %dx%d -> scale %ux%u\n",
517 pix->width, pix->height, HSC, VSC);
518
519 /* HSCALE=HSC */
520 cx25840_write(client, 0x418, HSC & 0xff);
521 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
522 cx25840_write(client, 0x41a, HSC >> 16);
523 /* VSCALE=VSC */
524 cx25840_write(client, 0x41c, VSC & 0xff);
525 cx25840_write(client, 0x41d, VSC >> 8);
526 /* VS_INTRLACE=1 VFILT=filter */
527 cx25840_write(client, 0x41e, 0x8 | filter);
528 break;
529
530 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
531 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
532
533 case V4L2_BUF_TYPE_VBI_CAPTURE:
534 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
535
536 default:
537 return -EINVAL;
538 }
539
540 return 0;
541}
542
543/* ----------------------------------------------------------------------- */
544
545static int cx25840_command(struct i2c_client *client, unsigned int cmd,
546 void *arg)
547{
548 struct cx25840_state *state = i2c_get_clientdata(client);
549 struct v4l2_tuner *vt = arg;
550 int result = 0;
551
552 switch (cmd) {
553 case 0:
554 break;
555
556#ifdef CONFIG_VIDEO_ADV_DEBUG
557 /* ioctls to allow direct access to the
558 * cx25840 registers for testing */
559 case VIDIOC_INT_G_REGISTER:
560 {
561 struct v4l2_register *reg = arg;
562
563 if (reg->i2c_id != I2C_DRIVERID_CX25840)
564 return -EINVAL;
565 reg->val = cx25840_read(client, reg->reg & 0x0fff);
566 break;
567 }
568
569 case VIDIOC_INT_S_REGISTER:
570 {
571 struct v4l2_register *reg = arg;
572
573 if (reg->i2c_id != I2C_DRIVERID_CX25840)
574 return -EINVAL;
575 if (!capable(CAP_SYS_ADMIN))
576 return -EPERM;
577 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
578 break;
579 }
580#endif
581
582 case VIDIOC_INT_DECODE_VBI_LINE:
583 return cx25840_vbi(client, cmd, arg);
584
585 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
586 case AUDC_SET_INPUT:
587 result = cx25840_audio(client, cmd, arg);
588 break;
589
590 case VIDIOC_STREAMON:
591 cx25840_dbg("enable output\n");
592 cx25840_write(client, 0x115, 0x8c);
593 cx25840_write(client, 0x116, 0x07);
594 break;
595
596 case VIDIOC_STREAMOFF:
597 cx25840_dbg("disable output\n");
598 cx25840_write(client, 0x115, 0x00);
599 cx25840_write(client, 0x116, 0x00);
600 break;
601
602 case VIDIOC_LOG_STATUS:
603 log_status(client);
604 break;
605
606 case VIDIOC_G_CTRL:
607 result = get_v4lctrl(client, (struct v4l2_control *)arg);
608 break;
609
610 case VIDIOC_S_CTRL:
611 result = set_v4lctrl(client, (struct v4l2_control *)arg);
612 break;
613
614 case VIDIOC_G_STD:
615 *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
616 break;
617
618 case VIDIOC_S_STD:
619 result = set_v4lstd(client, *(v4l2_std_id *)arg);
620 break;
621
622 case VIDIOC_G_INPUT:
623 *(int *)arg = state->input;
624 break;
625
626 case VIDIOC_S_INPUT:
627 result = set_input(client, *(int *)arg);
628 break;
629
630 case VIDIOC_S_FREQUENCY:
631 input_change(client);
632 break;
633
634 case VIDIOC_G_TUNER:
635 {
636 u8 mode = cx25840_read(client, 0x804);
637 u8 pref = cx25840_read(client, 0x809) & 0xf;
638 u8 vpres = cx25840_read(client, 0x80a) & 0x10;
639 int val = 0;
640
641 vt->capability |=
642 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
643 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
644
645 vt->signal = vpres ? 0xffff : 0x0;
646
647 /* get rxsubchans and audmode */
648 if ((mode & 0xf) == 1)
649 val |= V4L2_TUNER_SUB_STEREO;
650 else
651 val |= V4L2_TUNER_SUB_MONO;
652
653 if (mode == 2 || mode == 4)
654 val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
655
656 if (mode & 0x10)
657 val |= V4L2_TUNER_SUB_SAP;
658
659 vt->rxsubchans = val;
660
661 switch (pref) {
662 case 0:
663 vt->audmode = V4L2_TUNER_MODE_MONO;
664 break;
665 case 1:
666 case 2:
667 vt->audmode = V4L2_TUNER_MODE_LANG2;
668 break;
669 case 4:
670 default:
671 vt->audmode = V4L2_TUNER_MODE_STEREO;
672 }
673 break;
674 }
675
676 case VIDIOC_S_TUNER:
677 switch (vt->audmode) {
678 case V4L2_TUNER_MODE_MONO:
679 case V4L2_TUNER_MODE_LANG1:
680 /* Force PREF_MODE to MONO */
681 cx25840_and_or(client, 0x809, ~0xf, 0x00);
682 break;
683 case V4L2_TUNER_MODE_STEREO:
684 /* Force PREF_MODE to STEREO */
685 cx25840_and_or(client, 0x809, ~0xf, 0x04);
686 break;
687 case V4L2_TUNER_MODE_LANG2:
688 /* Force PREF_MODE to LANG2 */
689 cx25840_and_or(client, 0x809, ~0xf, 0x01);
690 break;
691 }
692 break;
693
694 case VIDIOC_G_FMT:
695 result = get_v4lfmt(client, (struct v4l2_format *)arg);
696 break;
697
698 case VIDIOC_S_FMT:
699 result = set_v4lfmt(client, (struct v4l2_format *)arg);
700 break;
701
702 case VIDIOC_INT_RESET:
703 cx25840_initialize(client, 0);
704 break;
705
706 case VIDIOC_INT_G_CHIP_IDENT:
707 *(enum v4l2_chip_ident *)arg =
708 V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
709 break;
710
711 default:
712 cx25840_err("invalid ioctl %x\n", cmd);
713 return -EINVAL;
714 }
715
716 return result;
717}
718
719/* ----------------------------------------------------------------------- */
720
721struct i2c_driver i2c_driver_cx25840;
722
723static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
724 int kind)
725{
726 struct i2c_client *client;
727 struct cx25840_state *state;
728 u16 device_id;
729
730 /* Check if the adapter supports the needed features
731 * Not until kernel version 2.6.11 did the bit-algo
732 * correctly report that it would do an I2C-level xfer */
733 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
734 return 0;
735
736 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
737 if (client == 0)
738 return -ENOMEM;
739
740 memset(client, 0, sizeof(struct i2c_client));
741 client->addr = address;
742 client->adapter = adapter;
743 client->driver = &i2c_driver_cx25840;
744 client->flags = I2C_CLIENT_ALLOW_USE;
745 snprintf(client->name, sizeof(client->name) - 1, "cx25840");
746
747 cx25840_dbg("detecting cx25840 client on address 0x%x\n", address << 1);
748
749 device_id = cx25840_read(client, 0x101) << 8;
750 device_id |= cx25840_read(client, 0x100);
751
752 /* The high byte of the device ID should be
753 * 0x84 if chip is present */
754 if ((device_id & 0xff00) != 0x8400) {
755 cx25840_dbg("cx25840 not found\n");
756 kfree(client);
757 return 0;
758 }
759
760 cx25840_info("cx25%3x-2%x found @ 0x%x (%s)\n",
761 (device_id & 0xfff0) >> 4,
762 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
763 address << 1, adapter->name);
764
765 state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
766 if (state == NULL) {
767 kfree(client);
768 return -ENOMEM;
769 }
770
771 i2c_set_clientdata(client, state);
772 memset(state, 0, sizeof(struct cx25840_state));
773 state->input = CX25840_TUNER;
774 state->audclk_freq = V4L2_AUDCLK_48_KHZ;
775 state->audio_input = AUDIO_TUNER;
776 state->cardtype = CARDTYPE_PVR150;
777
778 cx25840_initialize(client, 1);
779
780 i2c_attach_client(client);
781
782 return 0;
783}
784
785static int cx25840_attach_adapter(struct i2c_adapter *adapter)
786{
787#ifdef I2C_CLASS_TV_ANALOG
788 if (adapter->class & I2C_CLASS_TV_ANALOG)
789#else
790 if (adapter->id == I2C_HW_B_BT848)
791#endif
792 return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
793 return 0;
794}
795
796static int cx25840_detach_client(struct i2c_client *client)
797{
798 struct cx25840_state *state = i2c_get_clientdata(client);
799 int err;
800
801 err = i2c_detach_client(client);
802 if (err) {
803 return err;
804 }
805
806 kfree(state);
807 kfree(client);
808
809 return 0;
810}
811
812/* ----------------------------------------------------------------------- */
813
814struct i2c_driver i2c_driver_cx25840 = {
815 .name = "cx25840",
816
817 .id = I2C_DRIVERID_CX25840,
818 .flags = I2C_DF_NOTIFY,
819
820 .attach_adapter = cx25840_attach_adapter,
821 .detach_client = cx25840_detach_client,
822 .command = cx25840_command,
823 .owner = THIS_MODULE,
824};
825
826
827static int __init m__init(void)
828{
829 return i2c_add_driver(&i2c_driver_cx25840);
830}
831
832static void __exit m__exit(void)
833{
834 i2c_del_driver(&i2c_driver_cx25840);
835}
836
837module_init(m__init);
838module_exit(m__exit);
839
840/* ----------------------------------------------------------------------- */
841
842static void log_status(struct i2c_client *client)
843{
844 static const char *const fmt_strs[] = {
845 "0x0",
846 "NTSC-M", "NTSC-J", "NTSC-4.43",
847 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
848 "0x9", "0xA", "0xB",
849 "SECAM",
850 "0xD", "0xE", "0xF"
851 };
852
853 struct cx25840_state *state = i2c_get_clientdata(client);
854 u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
855 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
856 u8 gen_stat1 = cx25840_read(client, 0x40d);
857 u8 download_ctl = cx25840_read(client, 0x803);
858 u8 mod_det_stat0 = cx25840_read(client, 0x804);
859 u8 mod_det_stat1 = cx25840_read(client, 0x805);
860 u8 audio_config = cx25840_read(client, 0x808);
861 u8 pref_mode = cx25840_read(client, 0x809);
862 u8 afc0 = cx25840_read(client, 0x80b);
863 u8 mute_ctl = cx25840_read(client, 0x8d3);
864 char *p;
865
866 cx25840_info("Video signal: %spresent\n",
867 (microctrl_vidfmt & 0x10) ? "" : "not ");
868 cx25840_info("Detected format: %s\n",
869 fmt_strs[gen_stat1 & 0xf]);
870
871 switch (mod_det_stat0) {
872 case 0x00: p = "mono"; break;
873 case 0x01: p = "stereo"; break;
874 case 0x02: p = "dual"; break;
875 case 0x04: p = "tri"; break;
876 case 0x10: p = "mono with SAP"; break;
877 case 0x11: p = "stereo with SAP"; break;
878 case 0x12: p = "dual with SAP"; break;
879 case 0x14: p = "tri with SAP"; break;
880 case 0xfe: p = "forced mode"; break;
881 default: p = "not defined";
882 }
883 cx25840_info("Detected audio mode: %s\n", p);
884
885 switch (mod_det_stat1) {
886 case 0x00: p = "not defined"; break;
887 case 0x01: p = "EIAJ"; break;
888 case 0x02: p = "A2-M"; break;
889 case 0x03: p = "A2-BG"; break;
890 case 0x04: p = "A2-DK1"; break;
891 case 0x05: p = "A2-DK2"; break;
892 case 0x06: p = "A2-DK3"; break;
893 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
894 case 0x08: p = "AM-L"; break;
895 case 0x09: p = "NICAM-BG"; break;
896 case 0x0a: p = "NICAM-DK"; break;
897 case 0x0b: p = "NICAM-I"; break;
898 case 0x0c: p = "NICAM-L"; break;
899 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
900 case 0x0e: p = "IF FM Radio"; break;
901 case 0x0f: p = "BTSC"; break;
902 case 0x10: p = "high-deviation FM"; break;
903 case 0x11: p = "very high-deviation FM"; break;
904 case 0xfd: p = "unknown audio standard"; break;
905 case 0xfe: p = "forced audio standard"; break;
906 case 0xff: p = "no detected audio standard"; break;
907 default: p = "not defined";
908 }
909 cx25840_info("Detected audio standard: %s\n", p);
910 cx25840_info("Audio muted: %s\n",
911 (mute_ctl & 0x2) ? "yes" : "no");
912 cx25840_info("Audio microcontroller: %s\n",
913 (download_ctl & 0x10) ? "running" : "stopped");
914
915 switch (audio_config >> 4) {
916 case 0x00: p = "undefined"; break;
917 case 0x01: p = "BTSC"; break;
918 case 0x02: p = "EIAJ"; break;
919 case 0x03: p = "A2-M"; break;
920 case 0x04: p = "A2-BG"; break;
921 case 0x05: p = "A2-DK1"; break;
922 case 0x06: p = "A2-DK2"; break;
923 case 0x07: p = "A2-DK3"; break;
924 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
925 case 0x09: p = "AM-L"; break;
926 case 0x0a: p = "NICAM-BG"; break;
927 case 0x0b: p = "NICAM-DK"; break;
928 case 0x0c: p = "NICAM-I"; break;
929 case 0x0d: p = "NICAM-L"; break;
930 case 0x0e: p = "FM radio"; break;
931 case 0x0f: p = "automatic detection"; break;
932 default: p = "undefined";
933 }
934 cx25840_info("Configured audio standard: %s\n", p);
935
936 if ((audio_config >> 4) < 0xF) {
937 switch (audio_config & 0xF) {
938 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
939 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
940 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
941 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
942 case 0x04: p = "STEREO"; break;
943 case 0x05: p = "DUAL1 (AB)"; break;
944 case 0x06: p = "DUAL2 (AC) (FM)"; break;
945 case 0x07: p = "DUAL3 (BC) (FM)"; break;
946 case 0x08: p = "DUAL4 (AC) (AM)"; break;
947 case 0x09: p = "DUAL5 (BC) (AM)"; break;
948 case 0x0a: p = "SAP"; break;
949 default: p = "undefined";
950 }
951 cx25840_info("Configured audio mode: %s\n", p);
952 } else {
953 switch (audio_config & 0xF) {
954 case 0x00: p = "BG"; break;
955 case 0x01: p = "DK1"; break;
956 case 0x02: p = "DK2"; break;
957 case 0x03: p = "DK3"; break;
958 case 0x04: p = "I"; break;
959 case 0x05: p = "L"; break;
960 case 0x06: p = "BTSC"; break;
961 case 0x07: p = "EIAJ"; break;
962 case 0x08: p = "A2-M"; break;
963 case 0x09: p = "FM Radio"; break;
964 case 0x0f: p = "automatic standard and mode detection"; break;
965 default: p = "undefined";
966 }
967 cx25840_info("Configured audio system: %s\n", p);
968 }
969
970 cx25840_info("Specified standard: %s\n",
971 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
972
973 switch (state->input) {
974 case CX25840_COMPOSITE0: p = "Composite 0"; break;
975 case CX25840_COMPOSITE1: p = "Composite 1"; break;
976 case CX25840_SVIDEO0: p = "S-Video 0"; break;
977 case CX25840_SVIDEO1: p = "S-Video 1"; break;
978 case CX25840_TUNER: p = "Tuner"; break;
979 }
980 cx25840_info("Specified input: %s\n", p);
981 cx25840_info("Specified audio input: %s\n",
982 state->audio_input == 0 ? "Tuner" : "External");
983
984 switch (state->audclk_freq) {
985 case V4L2_AUDCLK_441_KHZ: p = "44.1 kHz"; break;
986 case V4L2_AUDCLK_48_KHZ: p = "48 kHz"; break;
987 case V4L2_AUDCLK_32_KHZ: p = "32 kHz"; break;
988 default: p = "undefined";
989 }
990 cx25840_info("Specified audioclock freq: %s\n", p);
991
992 switch (pref_mode & 0xf) {
993 case 0: p = "mono/language A"; break;
994 case 1: p = "language B"; break;
995 case 2: p = "language C"; break;
996 case 3: p = "analog fallback"; break;
997 case 4: p = "stereo"; break;
998 case 5: p = "language AC"; break;
999 case 6: p = "language BC"; break;
1000 case 7: p = "language AB"; break;
1001 default: p = "undefined";
1002 }
1003 cx25840_info("Preferred audio mode: %s\n", p);
1004
1005 if ((audio_config & 0xf) == 0xf) {
1006 switch ((afc0 >> 3) & 0x3) {
1007 case 0: p = "system DK"; break;
1008 case 1: p = "system L"; break;
1009 case 2: p = "autodetect"; break;
1010 default: p = "undefined";
1011 }
1012 cx25840_info("Selected 65 MHz format: %s\n", p);
1013
1014 switch (afc0 & 0x7) {
1015 case 0: p = "chroma"; break;
1016 case 1: p = "BTSC"; break;
1017 case 2: p = "EIAJ"; break;
1018 case 3: p = "A2-M"; break;
1019 case 4: p = "autodetect"; break;
1020 default: p = "undefined";
1021 }
1022 cx25840_info("Selected 45 MHz format: %s\n", p);
1023 }
1024}