1 /* cx25840 - Conexant CX25840 audio/video decoder driver
3 * Copyright (C) 2004 Ulf Eklund
5 * Based on the saa7115 driver and on the first verison of Chris Kennedy's
8 * Changes by Tyler Trafford <tatrafford@comcast.net>
9 * - cleanup/rewrite for V4L2 API (2005)
11 * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
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.
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.
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.
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/v4l2-common.h>
35 #include <media/cx25840.h>
37 #include "cx25840-core.h"
39 MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
40 MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
41 MODULE_LICENSE("GPL");
43 static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
46 static int cx25840_debug;
48 module_param_named(debug,cx25840_debug, int, 0644);
50 MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
54 /* ----------------------------------------------------------------------- */
56 int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
59 buffer[0] = addr >> 8;
60 buffer[1] = addr & 0xff;
62 return i2c_master_send(client, buffer, 3);
65 int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
68 buffer[0] = addr >> 8;
69 buffer[1] = addr & 0xff;
70 buffer[2] = value >> 24;
71 buffer[3] = (value >> 16) & 0xff;
72 buffer[4] = (value >> 8) & 0xff;
73 buffer[5] = value & 0xff;
74 return i2c_master_send(client, buffer, 6);
77 u8 cx25840_read(struct i2c_client * client, u16 addr)
80 buffer[0] = addr >> 8;
81 buffer[1] = addr & 0xff;
83 if (i2c_master_send(client, buffer, 2) < 2)
86 if (i2c_master_recv(client, buffer, 1) < 1)
92 u32 cx25840_read4(struct i2c_client * client, u16 addr)
95 buffer[0] = addr >> 8;
96 buffer[1] = addr & 0xff;
98 if (i2c_master_send(client, buffer, 2) < 2)
101 if (i2c_master_recv(client, buffer, 4) < 4)
104 return (buffer[0] << 24) | (buffer[1] << 16) |
105 (buffer[2] << 8) | buffer[3];
108 int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
111 return cx25840_write(client, addr,
112 (cx25840_read(client, addr) & and_mask) |
116 /* ----------------------------------------------------------------------- */
118 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
119 enum cx25840_audio_input aud_input);
120 static void log_audio_status(struct i2c_client *client);
121 static void log_video_status(struct i2c_client *client);
123 /* ----------------------------------------------------------------------- */
125 static void init_dll1(struct i2c_client *client)
127 /* This is the Hauppauge sequence used to
128 * initialize the Delay Lock Loop 1 (ADC DLL). */
129 cx25840_write(client, 0x159, 0x23);
130 cx25840_write(client, 0x15a, 0x87);
131 cx25840_write(client, 0x15b, 0x06);
132 cx25840_write(client, 0x159, 0xe1);
133 cx25840_write(client, 0x15a, 0x86);
134 cx25840_write(client, 0x159, 0xe0);
135 cx25840_write(client, 0x159, 0xe1);
136 cx25840_write(client, 0x15b, 0x10);
139 static void init_dll2(struct i2c_client *client)
141 /* This is the Hauppauge sequence used to
142 * initialize the Delay Lock Loop 2 (ADC DLL). */
143 cx25840_write(client, 0x15d, 0xe3);
144 cx25840_write(client, 0x15e, 0x86);
145 cx25840_write(client, 0x15f, 0x06);
146 cx25840_write(client, 0x15d, 0xe1);
147 cx25840_write(client, 0x15d, 0xe0);
148 cx25840_write(client, 0x15d, 0xe1);
151 static void cx25836_initialize(struct i2c_client *client)
153 /* reset configuration is described on page 3-77 of the CX25836 datasheet */
155 cx25840_and_or(client, 0x000, ~0x01, 0x01);
156 cx25840_and_or(client, 0x000, ~0x01, 0x00);
158 cx25840_and_or(client, 0x15a, ~0x70, 0x00);
160 cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
162 cx25840_and_or(client, 0x159, ~0x02, 0x02);
164 /* There should be a 10-us delay here, but since the
165 i2c bus already has a 10-us delay we don't need to do
168 cx25840_and_or(client, 0x159, ~0x02, 0x00);
170 cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
172 cx25840_and_or(client, 0x159, ~0x01, 0x00);
173 cx25840_and_or(client, 0x159, ~0x01, 0x01);
175 cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
178 static void cx25840_initialize(struct i2c_client *client, int loadfw)
180 struct cx25840_state *state = i2c_get_clientdata(client);
182 /* datasheet startup in numbered steps, refer to page 3-77 */
184 cx25840_and_or(client, 0x803, ~0x10, 0x00);
185 /* The default of this register should be 4, but I get 0 instead.
186 * Set this register to 4 manually. */
187 cx25840_write(client, 0x000, 0x04);
191 cx25840_write(client, 0x136, 0x0a);
193 cx25840_write(client, 0x13c, 0x01);
194 cx25840_write(client, 0x13c, 0x00);
197 cx25840_loadfw(client);
199 cx25840_write(client, 0x115, 0x8c);
200 cx25840_write(client, 0x116, 0x07);
201 cx25840_write(client, 0x118, 0x02);
203 cx25840_write(client, 0x4a5, 0x80);
204 cx25840_write(client, 0x4a5, 0x00);
205 cx25840_write(client, 0x402, 0x00);
207 cx25840_and_or(client, 0x401, ~0x18, 0);
208 cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
209 /* steps 8c and 8d are done in change_input() */
211 cx25840_write(client, 0x8d3, 0x1f);
212 cx25840_write(client, 0x8e3, 0x03);
214 cx25840_vbi_setup(client);
216 /* trial and error says these are needed to get audio */
217 cx25840_write(client, 0x914, 0xa0);
218 cx25840_write(client, 0x918, 0xa0);
219 cx25840_write(client, 0x919, 0x01);
221 /* stereo prefered */
222 cx25840_write(client, 0x809, 0x04);
224 cx25840_write(client, 0x8cf, 0x0f);
227 set_input(client, state->vid_input, state->aud_input);
229 /* start microcontroller */
230 cx25840_and_or(client, 0x803, ~0x10, 0x10);
233 /* ----------------------------------------------------------------------- */
235 static void input_change(struct i2c_client *client)
237 struct cx25840_state *state = i2c_get_clientdata(client);
238 v4l2_std_id std = cx25840_get_v4lstd(client);
240 /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
241 if (std & V4L2_STD_SECAM) {
242 cx25840_write(client, 0x402, 0);
245 cx25840_write(client, 0x402, 0x04);
246 cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
248 cx25840_and_or(client, 0x401, ~0x60, 0);
249 cx25840_and_or(client, 0x401, ~0x60, 0x60);
251 /* Note: perhaps V4L2_STD_PAL_M should be handled as V4L2_STD_NTSC
252 instead of V4L2_STD_PAL. Someone needs to test this. */
253 if (std & V4L2_STD_PAL) {
254 /* Follow tuner change procedure for PAL */
255 cx25840_write(client, 0x808, 0xff);
256 cx25840_write(client, 0x80b, 0x10);
257 } else if (std & V4L2_STD_SECAM) {
258 /* Select autodetect for SECAM */
259 cx25840_write(client, 0x808, 0xff);
260 cx25840_write(client, 0x80b, 0x10);
261 } else if (std & V4L2_STD_NTSC) {
262 /* Certain Hauppauge PVR150 models have a hardware bug
263 that causes audio to drop out. For these models the
264 audio standard must be set explicitly.
265 To be precise: it affects cards with tuner models
266 85, 99 and 112 (model numbers from tveeprom). */
267 int hw_fix = state->pvr150_workaround;
269 if (std == V4L2_STD_NTSC_M_JP) {
270 /* Japan uses EIAJ audio standard */
271 cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
272 } else if (std == V4L2_STD_NTSC_M_KR) {
273 /* South Korea uses A2 audio standard */
274 cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
276 /* Others use the BTSC audio standard */
277 cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
279 cx25840_write(client, 0x80b, 0x00);
282 if (cx25840_read(client, 0x803) & 0x10) {
283 /* restart audio decoder microcontroller */
284 cx25840_and_or(client, 0x803, ~0x10, 0x00);
285 cx25840_and_or(client, 0x803, ~0x10, 0x10);
289 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
290 enum cx25840_audio_input aud_input)
292 struct cx25840_state *state = i2c_get_clientdata(client);
293 u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
294 vid_input <= CX25840_COMPOSITE8);
297 v4l_dbg(1, cx25840_debug, client, "decoder set video input %d, audio input %d\n",
298 vid_input, aud_input);
301 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
303 int luma = vid_input & 0xf0;
304 int chroma = vid_input & 0xf00;
306 if ((vid_input & ~0xff0) ||
307 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
308 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
309 v4l_err(client, "0x%04x is not a valid video input!\n", vid_input);
312 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
313 if (chroma >= CX25840_SVIDEO_CHROMA7) {
315 reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
318 reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
323 case CX25840_AUDIO_SERIAL:
324 /* do nothing, use serial audio input */
326 case CX25840_AUDIO4: reg &= ~0x30; break;
327 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
328 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
329 case CX25840_AUDIO7: reg &= ~0xc0; break;
330 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
333 v4l_err(client, "0x%04x is not a valid audio input!\n", aud_input);
337 cx25840_write(client, 0x103, reg);
338 /* Set INPUT_MODE to Composite (0) or S-Video (1) */
339 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
340 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
341 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
342 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
343 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
344 cx25840_and_or(client, 0x102, ~0x4, 4);
346 cx25840_and_or(client, 0x102, ~0x4, 0);
348 state->vid_input = vid_input;
349 state->aud_input = aud_input;
350 if (!state->is_cx25836) {
351 cx25840_audio_set_path(client);
352 input_change(client);
357 /* ----------------------------------------------------------------------- */
359 static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
361 u8 fmt=0; /* zero is autodetect */
363 /* First tests should be against specific std */
364 if (std == V4L2_STD_NTSC_M_JP) {
366 } else if (std == V4L2_STD_NTSC_443) {
368 } else if (std == V4L2_STD_PAL_M) {
370 } else if (std == V4L2_STD_PAL_N) {
372 } else if (std == V4L2_STD_PAL_Nc) {
374 } else if (std == V4L2_STD_PAL_60) {
377 /* Then, test against generic ones */
378 if (std & V4L2_STD_NTSC) {
380 } else if (std & V4L2_STD_PAL) {
382 } else if (std & V4L2_STD_SECAM) {
387 /* Follow step 9 of section 3.16 in the cx25840 datasheet.
388 Without this PAL may display a vertical ghosting effect.
389 This happens for example with the Yuan MPC622. */
390 if (fmt >= 4 && fmt < 8) {
391 /* Set format to NTSC-M */
392 cx25840_and_or(client, 0x400, ~0xf, 1);
394 cx25840_and_or(client, 0x47b, ~6, 0);
396 cx25840_and_or(client, 0x400, ~0xf, fmt);
397 cx25840_vbi_setup(client);
401 v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
403 struct cx25840_state *state = i2c_get_clientdata(client);
404 /* check VID_FMT_SEL first */
405 u8 fmt = cx25840_read(client, 0x400) & 0xf;
408 /* check AFD_FMT_STAT if set to autodetect */
409 fmt = cx25840_read(client, 0x40d) & 0xf;
415 /* if the audio std is A2-M, then this is the South Korean
417 if (!state->is_cx25836 && cx25840_read(client, 0x805) == 2)
418 return V4L2_STD_NTSC_M_KR;
419 return V4L2_STD_NTSC_M;
421 case 0x2: return V4L2_STD_NTSC_M_JP;
422 case 0x3: return V4L2_STD_NTSC_443;
423 case 0x4: return V4L2_STD_PAL;
424 case 0x5: return V4L2_STD_PAL_M;
425 case 0x6: return V4L2_STD_PAL_N;
426 case 0x7: return V4L2_STD_PAL_Nc;
427 case 0x8: return V4L2_STD_PAL_60;
428 case 0xc: return V4L2_STD_SECAM;
429 default: return V4L2_STD_UNKNOWN;
433 /* ----------------------------------------------------------------------- */
435 static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
437 struct cx25840_state *state = i2c_get_clientdata(client);
440 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
441 state->pvr150_workaround = ctrl->value;
442 set_input(client, state->vid_input, state->aud_input);
445 case V4L2_CID_BRIGHTNESS:
446 if (ctrl->value < 0 || ctrl->value > 255) {
447 v4l_err(client, "invalid brightness setting %d\n",
452 cx25840_write(client, 0x414, ctrl->value - 128);
455 case V4L2_CID_CONTRAST:
456 if (ctrl->value < 0 || ctrl->value > 127) {
457 v4l_err(client, "invalid contrast setting %d\n",
462 cx25840_write(client, 0x415, ctrl->value << 1);
465 case V4L2_CID_SATURATION:
466 if (ctrl->value < 0 || ctrl->value > 127) {
467 v4l_err(client, "invalid saturation setting %d\n",
472 cx25840_write(client, 0x420, ctrl->value << 1);
473 cx25840_write(client, 0x421, ctrl->value << 1);
477 if (ctrl->value < -127 || ctrl->value > 127) {
478 v4l_err(client, "invalid hue setting %d\n", ctrl->value);
482 cx25840_write(client, 0x422, ctrl->value);
485 case V4L2_CID_AUDIO_VOLUME:
486 case V4L2_CID_AUDIO_BASS:
487 case V4L2_CID_AUDIO_TREBLE:
488 case V4L2_CID_AUDIO_BALANCE:
489 case V4L2_CID_AUDIO_MUTE:
490 if (state->is_cx25836)
492 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
501 static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
503 struct cx25840_state *state = i2c_get_clientdata(client);
506 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
507 ctrl->value = state->pvr150_workaround;
509 case V4L2_CID_BRIGHTNESS:
510 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
512 case V4L2_CID_CONTRAST:
513 ctrl->value = cx25840_read(client, 0x415) >> 1;
515 case V4L2_CID_SATURATION:
516 ctrl->value = cx25840_read(client, 0x420) >> 1;
519 ctrl->value = (s8)cx25840_read(client, 0x422);
521 case V4L2_CID_AUDIO_VOLUME:
522 case V4L2_CID_AUDIO_BASS:
523 case V4L2_CID_AUDIO_TREBLE:
524 case V4L2_CID_AUDIO_BALANCE:
525 case V4L2_CID_AUDIO_MUTE:
526 if (state->is_cx25836)
528 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
536 /* ----------------------------------------------------------------------- */
538 static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
541 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
542 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
550 static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
552 struct v4l2_pix_format *pix;
553 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
554 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
557 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
558 pix = &(fmt->fmt.pix);
560 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
561 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
563 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
564 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
566 Vlines = pix->height + (is_pal ? 4 : 7);
568 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
569 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
570 v4l_err(client, "%dx%d is not a valid size!\n",
571 pix->width, pix->height);
575 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
576 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
579 if (pix->width >= 385)
581 else if (pix->width > 192)
583 else if (pix->width > 96)
588 v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
589 pix->width, pix->height, HSC, VSC);
592 cx25840_write(client, 0x418, HSC & 0xff);
593 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
594 cx25840_write(client, 0x41a, HSC >> 16);
596 cx25840_write(client, 0x41c, VSC & 0xff);
597 cx25840_write(client, 0x41d, VSC >> 8);
598 /* VS_INTRLACE=1 VFILT=filter */
599 cx25840_write(client, 0x41e, 0x8 | filter);
602 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
603 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
605 case V4L2_BUF_TYPE_VBI_CAPTURE:
606 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
615 /* ----------------------------------------------------------------------- */
617 static struct v4l2_queryctrl cx25836_qctrl[] = {
619 .id = V4L2_CID_BRIGHTNESS,
620 .type = V4L2_CTRL_TYPE_INTEGER,
621 .name = "Brightness",
625 .default_value = 128,
628 .id = V4L2_CID_CONTRAST,
629 .type = V4L2_CTRL_TYPE_INTEGER,
637 .id = V4L2_CID_SATURATION,
638 .type = V4L2_CTRL_TYPE_INTEGER,
639 .name = "Saturation",
647 .type = V4L2_CTRL_TYPE_INTEGER,
657 static struct v4l2_queryctrl cx25840_qctrl[] = {
659 .id = V4L2_CID_AUDIO_VOLUME,
660 .type = V4L2_CTRL_TYPE_INTEGER,
665 .default_value = 58880,
668 .id = V4L2_CID_AUDIO_BALANCE,
669 .type = V4L2_CTRL_TYPE_INTEGER,
674 .default_value = 32768,
677 .id = V4L2_CID_AUDIO_MUTE,
678 .type = V4L2_CTRL_TYPE_BOOLEAN,
686 .id = V4L2_CID_AUDIO_BASS,
687 .type = V4L2_CTRL_TYPE_INTEGER,
692 .default_value = 32768,
694 .id = V4L2_CID_AUDIO_TREBLE,
695 .type = V4L2_CTRL_TYPE_INTEGER,
700 .default_value = 32768,
704 /* ----------------------------------------------------------------------- */
706 static int cx25840_command(struct i2c_client *client, unsigned int cmd,
709 struct cx25840_state *state = i2c_get_clientdata(client);
710 struct v4l2_tuner *vt = arg;
711 struct v4l2_routing *route = arg;
714 #ifdef CONFIG_VIDEO_ADV_DEBUG
715 /* ioctls to allow direct access to the
716 * cx25840 registers for testing */
717 case VIDIOC_INT_G_REGISTER:
719 struct v4l2_register *reg = arg;
721 if (reg->i2c_id != I2C_DRIVERID_CX25840)
723 reg->val = cx25840_read(client, reg->reg & 0x0fff);
727 case VIDIOC_INT_S_REGISTER:
729 struct v4l2_register *reg = arg;
731 if (reg->i2c_id != I2C_DRIVERID_CX25840)
733 if (!capable(CAP_SYS_ADMIN))
735 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
740 case VIDIOC_INT_DECODE_VBI_LINE:
741 return cx25840_vbi(client, cmd, arg);
743 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
744 return cx25840_audio(client, cmd, arg);
746 case VIDIOC_STREAMON:
747 v4l_dbg(1, cx25840_debug, client, "enable output\n");
748 cx25840_write(client, 0x115, state->is_cx25836 ? 0x0c : 0x8c);
749 cx25840_write(client, 0x116, state->is_cx25836 ? 0x04 : 0x07);
752 case VIDIOC_STREAMOFF:
753 v4l_dbg(1, cx25840_debug, client, "disable output\n");
754 cx25840_write(client, 0x115, 0x00);
755 cx25840_write(client, 0x116, 0x00);
758 case VIDIOC_LOG_STATUS:
759 log_video_status(client);
760 if (!state->is_cx25836)
761 log_audio_status(client);
765 return get_v4lctrl(client, (struct v4l2_control *)arg);
768 return set_v4lctrl(client, (struct v4l2_control *)arg);
770 case VIDIOC_QUERYCTRL:
772 struct v4l2_queryctrl *qc = arg;
775 for (i = 0; i < ARRAY_SIZE(cx25836_qctrl); i++)
776 if (qc->id && qc->id == cx25836_qctrl[i].id) {
777 memcpy(qc, &cx25836_qctrl[i], sizeof(*qc));
780 if (state->is_cx25836)
783 for (i = 0; i < ARRAY_SIZE(cx25840_qctrl); i++)
784 if (qc->id && qc->id == cx25840_qctrl[i].id) {
785 memcpy(qc, &cx25840_qctrl[i], sizeof(*qc));
792 *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
797 return set_v4lstd(client, *(v4l2_std_id *)arg);
803 case VIDIOC_INT_G_VIDEO_ROUTING:
804 route->input = state->vid_input;
808 case VIDIOC_INT_S_VIDEO_ROUTING:
809 return set_input(client, route->input, state->aud_input);
811 case VIDIOC_INT_G_AUDIO_ROUTING:
812 if (state->is_cx25836)
814 route->input = state->aud_input;
818 case VIDIOC_INT_S_AUDIO_ROUTING:
819 if (state->is_cx25836)
821 return set_input(client, state->vid_input, route->input);
823 case VIDIOC_S_FREQUENCY:
824 if (!state->is_cx25836) {
825 input_change(client);
831 u8 vpres = cx25840_read(client, 0x40e) & 0x20;
838 vt->signal = vpres ? 0xffff : 0x0;
839 if (state->is_cx25836)
843 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
844 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
846 mode = cx25840_read(client, 0x804);
848 /* get rxsubchans and audmode */
849 if ((mode & 0xf) == 1)
850 val |= V4L2_TUNER_SUB_STEREO;
852 val |= V4L2_TUNER_SUB_MONO;
854 if (mode == 2 || mode == 4)
855 val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
858 val |= V4L2_TUNER_SUB_SAP;
860 vt->rxsubchans = val;
861 vt->audmode = state->audmode;
866 if (state->radio || state->is_cx25836)
869 switch (vt->audmode) {
870 case V4L2_TUNER_MODE_MONO:
873 bilingual -> lang1 */
874 cx25840_and_or(client, 0x809, ~0xf, 0x00);
876 case V4L2_TUNER_MODE_STEREO:
877 case V4L2_TUNER_MODE_LANG1:
880 bilingual -> lang1 */
881 cx25840_and_or(client, 0x809, ~0xf, 0x04);
883 case V4L2_TUNER_MODE_LANG1_LANG2:
886 bilingual -> lang1/lang2 */
887 cx25840_and_or(client, 0x809, ~0xf, 0x07);
889 case V4L2_TUNER_MODE_LANG2:
892 bilingual -> lang2 */
893 cx25840_and_or(client, 0x809, ~0xf, 0x01);
898 state->audmode = vt->audmode;
902 return get_v4lfmt(client, (struct v4l2_format *)arg);
905 return set_v4lfmt(client, (struct v4l2_format *)arg);
907 case VIDIOC_INT_RESET:
908 if (state->is_cx25836)
909 cx25836_initialize(client);
911 cx25840_initialize(client, 0);
914 case VIDIOC_INT_G_CHIP_IDENT:
915 *(enum v4l2_chip_ident *)arg = state->id;
925 /* ----------------------------------------------------------------------- */
927 static struct i2c_driver i2c_driver_cx25840;
929 static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
932 struct i2c_client *client;
933 struct cx25840_state *state;
934 enum v4l2_chip_ident id;
937 /* Check if the adapter supports the needed features
938 * Not until kernel version 2.6.11 did the bit-algo
939 * correctly report that it would do an I2C-level xfer */
940 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
943 state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
948 client->addr = address;
949 client->adapter = adapter;
950 client->driver = &i2c_driver_cx25840;
951 snprintf(client->name, sizeof(client->name) - 1, "cx25840");
953 v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", address << 1);
955 device_id = cx25840_read(client, 0x101) << 8;
956 device_id |= cx25840_read(client, 0x100);
958 /* The high byte of the device ID should be
959 * 0x83 for the cx2583x and 0x84 for the cx2584x */
960 if ((device_id & 0xff00) == 0x8300) {
961 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
962 state->is_cx25836 = 1;
964 else if ((device_id & 0xff00) == 0x8400) {
965 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
966 state->is_cx25836 = 0;
969 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
974 v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
975 (device_id & 0xfff0) >> 4,
976 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
977 address << 1, adapter->name);
979 i2c_set_clientdata(client, state);
980 state->vid_input = CX25840_COMPOSITE7;
981 state->aud_input = CX25840_AUDIO8;
982 state->audclk_freq = 48000;
983 state->pvr150_workaround = 0;
984 state->audmode = V4L2_TUNER_MODE_LANG1;
987 if (state->is_cx25836)
988 cx25836_initialize(client);
990 cx25840_initialize(client, 1);
992 i2c_attach_client(client);
997 static int cx25840_attach_adapter(struct i2c_adapter *adapter)
999 if (adapter->class & I2C_CLASS_TV_ANALOG)
1000 return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
1004 static int cx25840_detach_client(struct i2c_client *client)
1006 struct cx25840_state *state = i2c_get_clientdata(client);
1009 err = i2c_detach_client(client);
1019 /* ----------------------------------------------------------------------- */
1021 static struct i2c_driver i2c_driver_cx25840 = {
1025 .id = I2C_DRIVERID_CX25840,
1026 .attach_adapter = cx25840_attach_adapter,
1027 .detach_client = cx25840_detach_client,
1028 .command = cx25840_command,
1032 static int __init m__init(void)
1034 return i2c_add_driver(&i2c_driver_cx25840);
1037 static void __exit m__exit(void)
1039 i2c_del_driver(&i2c_driver_cx25840);
1042 module_init(m__init);
1043 module_exit(m__exit);
1045 /* ----------------------------------------------------------------------- */
1047 static void log_video_status(struct i2c_client *client)
1049 static const char *const fmt_strs[] = {
1051 "NTSC-M", "NTSC-J", "NTSC-4.43",
1052 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
1053 "0x9", "0xA", "0xB",
1058 struct cx25840_state *state = i2c_get_clientdata(client);
1059 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
1060 u8 gen_stat1 = cx25840_read(client, 0x40d);
1061 u8 gen_stat2 = cx25840_read(client, 0x40e);
1062 int vid_input = state->vid_input;
1064 v4l_info(client, "Video signal: %spresent\n",
1065 (gen_stat2 & 0x20) ? "" : "not ");
1066 v4l_info(client, "Detected format: %s\n",
1067 fmt_strs[gen_stat1 & 0xf]);
1069 v4l_info(client, "Specified standard: %s\n",
1070 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
1072 if (vid_input >= CX25840_COMPOSITE1 &&
1073 vid_input <= CX25840_COMPOSITE8) {
1074 v4l_info(client, "Specified video input: Composite %d\n",
1075 vid_input - CX25840_COMPOSITE1 + 1);
1077 v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
1078 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
1081 v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
1084 /* ----------------------------------------------------------------------- */
1086 static void log_audio_status(struct i2c_client *client)
1088 struct cx25840_state *state = i2c_get_clientdata(client);
1089 u8 download_ctl = cx25840_read(client, 0x803);
1090 u8 mod_det_stat0 = cx25840_read(client, 0x804);
1091 u8 mod_det_stat1 = cx25840_read(client, 0x805);
1092 u8 audio_config = cx25840_read(client, 0x808);
1093 u8 pref_mode = cx25840_read(client, 0x809);
1094 u8 afc0 = cx25840_read(client, 0x80b);
1095 u8 mute_ctl = cx25840_read(client, 0x8d3);
1096 int aud_input = state->aud_input;
1099 switch (mod_det_stat0) {
1100 case 0x00: p = "mono"; break;
1101 case 0x01: p = "stereo"; break;
1102 case 0x02: p = "dual"; break;
1103 case 0x04: p = "tri"; break;
1104 case 0x10: p = "mono with SAP"; break;
1105 case 0x11: p = "stereo with SAP"; break;
1106 case 0x12: p = "dual with SAP"; break;
1107 case 0x14: p = "tri with SAP"; break;
1108 case 0xfe: p = "forced mode"; break;
1109 default: p = "not defined";
1111 v4l_info(client, "Detected audio mode: %s\n", p);
1113 switch (mod_det_stat1) {
1114 case 0x00: p = "not defined"; break;
1115 case 0x01: p = "EIAJ"; break;
1116 case 0x02: p = "A2-M"; break;
1117 case 0x03: p = "A2-BG"; break;
1118 case 0x04: p = "A2-DK1"; break;
1119 case 0x05: p = "A2-DK2"; break;
1120 case 0x06: p = "A2-DK3"; break;
1121 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1122 case 0x08: p = "AM-L"; break;
1123 case 0x09: p = "NICAM-BG"; break;
1124 case 0x0a: p = "NICAM-DK"; break;
1125 case 0x0b: p = "NICAM-I"; break;
1126 case 0x0c: p = "NICAM-L"; break;
1127 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1128 case 0x0e: p = "IF FM Radio"; break;
1129 case 0x0f: p = "BTSC"; break;
1130 case 0x10: p = "high-deviation FM"; break;
1131 case 0x11: p = "very high-deviation FM"; break;
1132 case 0xfd: p = "unknown audio standard"; break;
1133 case 0xfe: p = "forced audio standard"; break;
1134 case 0xff: p = "no detected audio standard"; break;
1135 default: p = "not defined";
1137 v4l_info(client, "Detected audio standard: %s\n", p);
1138 v4l_info(client, "Audio muted: %s\n",
1139 (mute_ctl & 0x2) ? "yes" : "no");
1140 v4l_info(client, "Audio microcontroller: %s\n",
1141 (download_ctl & 0x10) ? "running" : "stopped");
1143 switch (audio_config >> 4) {
1144 case 0x00: p = "undefined"; break;
1145 case 0x01: p = "BTSC"; break;
1146 case 0x02: p = "EIAJ"; break;
1147 case 0x03: p = "A2-M"; break;
1148 case 0x04: p = "A2-BG"; break;
1149 case 0x05: p = "A2-DK1"; break;
1150 case 0x06: p = "A2-DK2"; break;
1151 case 0x07: p = "A2-DK3"; break;
1152 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1153 case 0x09: p = "AM-L"; break;
1154 case 0x0a: p = "NICAM-BG"; break;
1155 case 0x0b: p = "NICAM-DK"; break;
1156 case 0x0c: p = "NICAM-I"; break;
1157 case 0x0d: p = "NICAM-L"; break;
1158 case 0x0e: p = "FM radio"; break;
1159 case 0x0f: p = "automatic detection"; break;
1160 default: p = "undefined";
1162 v4l_info(client, "Configured audio standard: %s\n", p);
1164 if ((audio_config >> 4) < 0xF) {
1165 switch (audio_config & 0xF) {
1166 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1167 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1168 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1169 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1170 case 0x04: p = "STEREO"; break;
1171 case 0x05: p = "DUAL1 (AB)"; break;
1172 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1173 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1174 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1175 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1176 case 0x0a: p = "SAP"; break;
1177 default: p = "undefined";
1179 v4l_info(client, "Configured audio mode: %s\n", p);
1181 switch (audio_config & 0xF) {
1182 case 0x00: p = "BG"; break;
1183 case 0x01: p = "DK1"; break;
1184 case 0x02: p = "DK2"; break;
1185 case 0x03: p = "DK3"; break;
1186 case 0x04: p = "I"; break;
1187 case 0x05: p = "L"; break;
1188 case 0x06: p = "BTSC"; break;
1189 case 0x07: p = "EIAJ"; break;
1190 case 0x08: p = "A2-M"; break;
1191 case 0x09: p = "FM Radio"; break;
1192 case 0x0f: p = "automatic standard and mode detection"; break;
1193 default: p = "undefined";
1195 v4l_info(client, "Configured audio system: %s\n", p);
1199 v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
1201 v4l_info(client, "Specified audio input: External\n");
1204 switch (pref_mode & 0xf) {
1205 case 0: p = "mono/language A"; break;
1206 case 1: p = "language B"; break;
1207 case 2: p = "language C"; break;
1208 case 3: p = "analog fallback"; break;
1209 case 4: p = "stereo"; break;
1210 case 5: p = "language AC"; break;
1211 case 6: p = "language BC"; break;
1212 case 7: p = "language AB"; break;
1213 default: p = "undefined";
1215 v4l_info(client, "Preferred audio mode: %s\n", p);
1217 if ((audio_config & 0xf) == 0xf) {
1218 switch ((afc0 >> 3) & 0x3) {
1219 case 0: p = "system DK"; break;
1220 case 1: p = "system L"; break;
1221 case 2: p = "autodetect"; break;
1222 default: p = "undefined";
1224 v4l_info(client, "Selected 65 MHz format: %s\n", p);
1226 switch (afc0 & 0x7) {
1227 case 0: p = "chroma"; break;
1228 case 1: p = "BTSC"; break;
1229 case 2: p = "EIAJ"; break;
1230 case 3: p = "A2-M"; break;
1231 case 4: p = "autodetect"; break;
1232 default: p = "undefined";
1234 v4l_info(client, "Selected 45 MHz format: %s\n", p);