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/audiochip.h>
35 #include <media/v4l2-common.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 };
48 module_param(debug, bool, 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, u8 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_status(struct i2c_client *client);
122 /* ----------------------------------------------------------------------- */
124 static void init_dll1(struct i2c_client *client)
126 /* This is the Hauppauge sequence used to
127 * initialize the Delay Lock Loop 1 (ADC DLL). */
128 cx25840_write(client, 0x159, 0x23);
129 cx25840_write(client, 0x15a, 0x87);
130 cx25840_write(client, 0x15b, 0x06);
131 cx25840_write(client, 0x159, 0xe1);
132 cx25840_write(client, 0x15a, 0x86);
133 cx25840_write(client, 0x159, 0xe0);
134 cx25840_write(client, 0x159, 0xe1);
135 cx25840_write(client, 0x15b, 0x10);
138 static void init_dll2(struct i2c_client *client)
140 /* This is the Hauppauge sequence used to
141 * initialize the Delay Lock Loop 2 (ADC DLL). */
142 cx25840_write(client, 0x15d, 0xe3);
143 cx25840_write(client, 0x15e, 0x86);
144 cx25840_write(client, 0x15f, 0x06);
145 cx25840_write(client, 0x15d, 0xe1);
146 cx25840_write(client, 0x15d, 0xe0);
147 cx25840_write(client, 0x15d, 0xe1);
150 static void cx25840_initialize(struct i2c_client *client, int loadfw)
152 struct cx25840_state *state = i2c_get_clientdata(client);
154 /* datasheet startup in numbered steps, refer to page 3-77 */
156 cx25840_and_or(client, 0x803, ~0x10, 0x00);
157 /* The default of this register should be 4, but I get 0 instead.
158 * Set this register to 4 manually. */
159 cx25840_write(client, 0x000, 0x04);
163 cx25840_write(client, 0x136, 0x0a);
165 cx25840_write(client, 0x13c, 0x01);
166 cx25840_write(client, 0x13c, 0x00);
169 cx25840_loadfw(client);
171 cx25840_write(client, 0x115, 0x8c);
172 cx25840_write(client, 0x116, 0x07);
173 cx25840_write(client, 0x118, 0x02);
175 cx25840_write(client, 0x4a5, 0x80);
176 cx25840_write(client, 0x4a5, 0x00);
177 cx25840_write(client, 0x402, 0x00);
179 cx25840_write(client, 0x401, 0x18);
180 cx25840_write(client, 0x4a2, 0x10);
181 cx25840_write(client, 0x402, 0x04);
183 cx25840_write(client, 0x8d3, 0x1f);
184 cx25840_write(client, 0x8e3, 0x03);
186 cx25840_vbi_setup(client);
188 /* trial and error says these are needed to get audio */
189 cx25840_write(client, 0x914, 0xa0);
190 cx25840_write(client, 0x918, 0xa0);
191 cx25840_write(client, 0x919, 0x01);
193 /* stereo prefered */
194 cx25840_write(client, 0x809, 0x04);
196 cx25840_write(client, 0x8cf, 0x0f);
199 set_input(client, state->vid_input, state->aud_input);
201 /* start microcontroller */
202 cx25840_and_or(client, 0x803, ~0x10, 0x10);
205 /* ----------------------------------------------------------------------- */
207 static void input_change(struct i2c_client *client)
209 struct cx25840_state *state = i2c_get_clientdata(client);
210 v4l2_std_id std = cx25840_get_v4lstd(client);
212 /* Note: perhaps V4L2_STD_PAL_M should be handled as V4L2_STD_NTSC
213 instead of V4L2_STD_PAL. Someone needs to test this. */
214 if (std & V4L2_STD_PAL) {
215 /* Follow tuner change procedure for PAL */
216 cx25840_write(client, 0x808, 0xff);
217 cx25840_write(client, 0x80b, 0x10);
218 } else if (std & V4L2_STD_SECAM) {
219 /* Select autodetect for SECAM */
220 cx25840_write(client, 0x808, 0xff);
221 cx25840_write(client, 0x80b, 0x10);
222 } else if (std & V4L2_STD_NTSC) {
224 if (state->pvr150_workaround) {
225 /* Certain Hauppauge PVR150 models have a hardware bug
226 that causes audio to drop out. For these models the
227 audio standard must be set explicitly.
228 To be precise: it affects cards with tuner models
229 85, 99 and 112 (model numbers from tveeprom). */
230 if (std == V4L2_STD_NTSC_M_JP) {
231 /* Japan uses EIAJ audio standard */
232 cx25840_write(client, 0x808, 0x2f);
234 /* Others use the BTSC audio standard */
235 cx25840_write(client, 0x808, 0x1f);
237 /* South Korea uses the A2-M (aka Zweiton M) audio
238 standard, and should set 0x808 to 0x3f, but I don't
239 know how to detect this. */
240 } else if (std == V4L2_STD_NTSC_M_JP) {
241 /* Japan uses EIAJ audio standard */
242 cx25840_write(client, 0x808, 0xf7);
244 /* Others use the BTSC audio standard */
245 cx25840_write(client, 0x808, 0xf6);
247 /* South Korea uses the A2-M (aka Zweiton M) audio standard,
248 and should set 0x808 to 0xf8, but I don't know how to
250 cx25840_write(client, 0x80b, 0x00);
253 if (cx25840_read(client, 0x803) & 0x10) {
254 /* restart audio decoder microcontroller */
255 cx25840_and_or(client, 0x803, ~0x10, 0x00);
256 cx25840_and_or(client, 0x803, ~0x10, 0x10);
260 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
261 enum cx25840_audio_input aud_input)
263 struct cx25840_state *state = i2c_get_clientdata(client);
264 u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
265 vid_input <= CX25840_COMPOSITE8);
268 v4l_dbg(1, client, "decoder set video input %d, audio input %d\n",
269 vid_input, aud_input);
272 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
274 int luma = vid_input & 0xf0;
275 int chroma = vid_input & 0xf00;
277 if ((vid_input & ~0xff0) ||
278 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
279 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
280 v4l_err(client, "0x%04x is not a valid video input!\n", vid_input);
283 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
284 if (chroma >= CX25840_SVIDEO_CHROMA7) {
286 reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
289 reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
294 case CX25840_AUDIO_SERIAL:
295 /* do nothing, use serial audio input */
297 case CX25840_AUDIO4: reg &= ~0x30; break;
298 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
299 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
300 case CX25840_AUDIO7: reg &= ~0xc0; break;
301 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
304 v4l_err(client, "0x%04x is not a valid audio input!\n", aud_input);
308 cx25840_write(client, 0x103, reg);
309 /* Set INPUT_MODE to Composite (0) or S-Video (1) */
310 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
311 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
312 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
313 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
314 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
315 cx25840_and_or(client, 0x102, ~0x4, 4);
317 cx25840_and_or(client, 0x102, ~0x4, 0);
319 state->vid_input = vid_input;
320 state->aud_input = aud_input;
321 cx25840_audio_set_path(client);
322 input_change(client);
326 /* ----------------------------------------------------------------------- */
328 static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
330 u8 fmt=0; /* zero is autodetect */
332 /* First tests should be against specific std */
333 if (std & V4L2_STD_NTSC_M_JP) {
335 } else if (std & V4L2_STD_NTSC_443) {
337 } else if (std & V4L2_STD_PAL_M) {
339 } else if (std & V4L2_STD_PAL_N) {
341 } else if (std & V4L2_STD_PAL_Nc) {
343 } else if (std & V4L2_STD_PAL_60) {
346 /* Then, test against generic ones */
347 if (std & V4L2_STD_NTSC) {
349 } else if (std & V4L2_STD_PAL) {
351 } else if (std & V4L2_STD_SECAM) {
356 cx25840_and_or(client, 0x400, ~0xf, fmt);
357 cx25840_vbi_setup(client);
361 v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
363 /* check VID_FMT_SEL first */
364 u8 fmt = cx25840_read(client, 0x400) & 0xf;
367 /* check AFD_FMT_STAT if set to autodetect */
368 fmt = cx25840_read(client, 0x40d) & 0xf;
372 case 0x1: return V4L2_STD_NTSC_M;
373 case 0x2: return V4L2_STD_NTSC_M_JP;
374 case 0x3: return V4L2_STD_NTSC_443;
375 case 0x4: return V4L2_STD_PAL;
376 case 0x5: return V4L2_STD_PAL_M;
377 case 0x6: return V4L2_STD_PAL_N;
378 case 0x7: return V4L2_STD_PAL_Nc;
379 case 0x8: return V4L2_STD_PAL_60;
380 case 0xc: return V4L2_STD_SECAM;
381 default: return V4L2_STD_UNKNOWN;
385 /* ----------------------------------------------------------------------- */
387 static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
389 struct cx25840_state *state = i2c_get_clientdata(client);
392 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
393 state->pvr150_workaround = ctrl->value;
394 set_input(client, state->vid_input, state->aud_input);
397 case V4L2_CID_BRIGHTNESS:
398 if (ctrl->value < 0 || ctrl->value > 255) {
399 v4l_err(client, "invalid brightness setting %d\n",
404 cx25840_write(client, 0x414, ctrl->value - 128);
407 case V4L2_CID_CONTRAST:
408 if (ctrl->value < 0 || ctrl->value > 127) {
409 v4l_err(client, "invalid contrast setting %d\n",
414 cx25840_write(client, 0x415, ctrl->value << 1);
417 case V4L2_CID_SATURATION:
418 if (ctrl->value < 0 || ctrl->value > 127) {
419 v4l_err(client, "invalid saturation setting %d\n",
424 cx25840_write(client, 0x420, ctrl->value << 1);
425 cx25840_write(client, 0x421, ctrl->value << 1);
429 if (ctrl->value < -127 || ctrl->value > 127) {
430 v4l_err(client, "invalid hue setting %d\n", ctrl->value);
434 cx25840_write(client, 0x422, ctrl->value);
437 case V4L2_CID_AUDIO_VOLUME:
438 case V4L2_CID_AUDIO_BASS:
439 case V4L2_CID_AUDIO_TREBLE:
440 case V4L2_CID_AUDIO_BALANCE:
441 case V4L2_CID_AUDIO_MUTE:
442 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
451 static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
453 struct cx25840_state *state = i2c_get_clientdata(client);
456 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
457 ctrl->value = state->pvr150_workaround;
459 case V4L2_CID_BRIGHTNESS:
460 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
462 case V4L2_CID_CONTRAST:
463 ctrl->value = cx25840_read(client, 0x415) >> 1;
465 case V4L2_CID_SATURATION:
466 ctrl->value = cx25840_read(client, 0x420) >> 1;
469 ctrl->value = (s8)cx25840_read(client, 0x422);
471 case V4L2_CID_AUDIO_VOLUME:
472 case V4L2_CID_AUDIO_BASS:
473 case V4L2_CID_AUDIO_TREBLE:
474 case V4L2_CID_AUDIO_BALANCE:
475 case V4L2_CID_AUDIO_MUTE:
476 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
484 /* ----------------------------------------------------------------------- */
486 static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
489 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
490 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
498 static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
500 struct v4l2_pix_format *pix;
501 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
502 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
505 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
506 pix = &(fmt->fmt.pix);
508 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
509 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
511 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
512 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
514 Vlines = pix->height + (is_pal ? 4 : 7);
516 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
517 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
518 v4l_err(client, "%dx%d is not a valid size!\n",
519 pix->width, pix->height);
523 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
524 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
527 if (pix->width >= 385)
529 else if (pix->width > 192)
531 else if (pix->width > 96)
536 v4l_dbg(1, client, "decoder set size %dx%d -> scale %ux%u\n",
537 pix->width, pix->height, HSC, VSC);
540 cx25840_write(client, 0x418, HSC & 0xff);
541 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
542 cx25840_write(client, 0x41a, HSC >> 16);
544 cx25840_write(client, 0x41c, VSC & 0xff);
545 cx25840_write(client, 0x41d, VSC >> 8);
546 /* VS_INTRLACE=1 VFILT=filter */
547 cx25840_write(client, 0x41e, 0x8 | filter);
550 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
551 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
553 case V4L2_BUF_TYPE_VBI_CAPTURE:
554 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
563 /* ----------------------------------------------------------------------- */
565 static struct v4l2_queryctrl cx25840_qctrl[] = {
567 .id = V4L2_CID_BRIGHTNESS,
568 .type = V4L2_CTRL_TYPE_INTEGER,
569 .name = "Brightness",
573 .default_value = 128,
576 .id = V4L2_CID_CONTRAST,
577 .type = V4L2_CTRL_TYPE_INTEGER,
585 .id = V4L2_CID_SATURATION,
586 .type = V4L2_CTRL_TYPE_INTEGER,
587 .name = "Saturation",
595 .type = V4L2_CTRL_TYPE_INTEGER,
603 .id = V4L2_CID_AUDIO_VOLUME,
604 .type = V4L2_CTRL_TYPE_INTEGER,
609 .default_value = 58880,
612 .id = V4L2_CID_AUDIO_BALANCE,
613 .type = V4L2_CTRL_TYPE_INTEGER,
618 .default_value = 32768,
621 .id = V4L2_CID_AUDIO_MUTE,
622 .type = V4L2_CTRL_TYPE_BOOLEAN,
630 .id = V4L2_CID_AUDIO_BASS,
631 .type = V4L2_CTRL_TYPE_INTEGER,
636 .default_value = 32768,
638 .id = V4L2_CID_AUDIO_TREBLE,
639 .type = V4L2_CTRL_TYPE_INTEGER,
644 .default_value = 32768,
648 /* ----------------------------------------------------------------------- */
650 static int cx25840_command(struct i2c_client *client, unsigned int cmd,
653 struct cx25840_state *state = i2c_get_clientdata(client);
654 struct v4l2_tuner *vt = arg;
657 #ifdef CONFIG_VIDEO_ADV_DEBUG
658 /* ioctls to allow direct access to the
659 * cx25840 registers for testing */
660 case VIDIOC_INT_G_REGISTER:
662 struct v4l2_register *reg = arg;
664 if (reg->i2c_id != I2C_DRIVERID_CX25840)
666 reg->val = cx25840_read(client, reg->reg & 0x0fff);
670 case VIDIOC_INT_S_REGISTER:
672 struct v4l2_register *reg = arg;
674 if (reg->i2c_id != I2C_DRIVERID_CX25840)
676 if (!capable(CAP_SYS_ADMIN))
678 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
683 case VIDIOC_INT_DECODE_VBI_LINE:
684 return cx25840_vbi(client, cmd, arg);
686 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
687 return cx25840_audio(client, cmd, arg);
689 case VIDIOC_STREAMON:
690 v4l_dbg(1, client, "enable output\n");
691 cx25840_write(client, 0x115, 0x8c);
692 cx25840_write(client, 0x116, 0x07);
695 case VIDIOC_STREAMOFF:
696 v4l_dbg(1, client, "disable output\n");
697 cx25840_write(client, 0x115, 0x00);
698 cx25840_write(client, 0x116, 0x00);
701 case VIDIOC_LOG_STATUS:
706 return get_v4lctrl(client, (struct v4l2_control *)arg);
709 return set_v4lctrl(client, (struct v4l2_control *)arg);
711 case VIDIOC_QUERYCTRL:
713 struct v4l2_queryctrl *qc = arg;
716 for (i = 0; i < ARRAY_SIZE(cx25840_qctrl); i++)
717 if (qc->id && qc->id == cx25840_qctrl[i].id) {
718 memcpy(qc, &cx25840_qctrl[i], sizeof(*qc));
725 *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
730 return set_v4lstd(client, *(v4l2_std_id *)arg);
737 *(int *)arg = state->vid_input;
741 return set_input(client, *(enum cx25840_video_input *)arg, state->aud_input);
745 struct v4l2_audio *input = arg;
747 return set_input(client, state->vid_input, input->index);
752 struct v4l2_audio *input = arg;
754 memset(input, 0, sizeof(*input));
755 input->index = state->aud_input;
759 case VIDIOC_S_FREQUENCY:
760 input_change(client);
765 u8 mode = cx25840_read(client, 0x804);
766 u8 pref = cx25840_read(client, 0x809) & 0xf;
767 u8 vpres = cx25840_read(client, 0x80a) & 0x10;
774 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
775 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
777 vt->signal = vpres ? 0xffff : 0x0;
779 /* get rxsubchans and audmode */
780 if ((mode & 0xf) == 1)
781 val |= V4L2_TUNER_SUB_STEREO;
783 val |= V4L2_TUNER_SUB_MONO;
785 if (mode == 2 || mode == 4)
786 val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
789 val |= V4L2_TUNER_SUB_SAP;
791 vt->rxsubchans = val;
795 vt->audmode = V4L2_TUNER_MODE_MONO;
799 vt->audmode = V4L2_TUNER_MODE_LANG2;
803 vt->audmode = V4L2_TUNER_MODE_STEREO;
809 switch (vt->audmode) {
810 case V4L2_TUNER_MODE_MONO:
811 case V4L2_TUNER_MODE_LANG1:
812 /* Force PREF_MODE to MONO */
813 cx25840_and_or(client, 0x809, ~0xf, 0x00);
815 case V4L2_TUNER_MODE_STEREO:
816 /* Force PREF_MODE to STEREO */
817 cx25840_and_or(client, 0x809, ~0xf, 0x04);
819 case V4L2_TUNER_MODE_LANG2:
820 /* Force PREF_MODE to LANG2 */
821 cx25840_and_or(client, 0x809, ~0xf, 0x01);
827 return get_v4lfmt(client, (struct v4l2_format *)arg);
830 return set_v4lfmt(client, (struct v4l2_format *)arg);
832 case VIDIOC_INT_RESET:
833 cx25840_initialize(client, 0);
836 case VIDIOC_INT_G_CHIP_IDENT:
837 *(enum v4l2_chip_ident *)arg =
838 V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
848 /* ----------------------------------------------------------------------- */
850 static struct i2c_driver i2c_driver_cx25840;
852 static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
855 struct i2c_client *client;
856 struct cx25840_state *state;
859 /* Check if the adapter supports the needed features
860 * Not until kernel version 2.6.11 did the bit-algo
861 * correctly report that it would do an I2C-level xfer */
862 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
865 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
869 memset(client, 0, sizeof(struct i2c_client));
870 client->addr = address;
871 client->adapter = adapter;
872 client->driver = &i2c_driver_cx25840;
873 snprintf(client->name, sizeof(client->name) - 1, "cx25840");
875 v4l_dbg(1, client, "detecting cx25840 client on address 0x%x\n", address << 1);
877 device_id = cx25840_read(client, 0x101) << 8;
878 device_id |= cx25840_read(client, 0x100);
880 /* The high byte of the device ID should be
881 * 0x84 if chip is present */
882 if ((device_id & 0xff00) != 0x8400) {
883 v4l_dbg(1, client, "cx25840 not found\n");
888 v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
889 (device_id & 0xfff0) >> 4,
890 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
891 address << 1, adapter->name);
893 state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
899 i2c_set_clientdata(client, state);
900 memset(state, 0, sizeof(struct cx25840_state));
901 state->vid_input = CX25840_COMPOSITE7;
902 state->aud_input = CX25840_AUDIO8;
903 state->audclk_freq = 48000;
904 state->pvr150_workaround = 0;
906 cx25840_initialize(client, 1);
908 i2c_attach_client(client);
913 static int cx25840_attach_adapter(struct i2c_adapter *adapter)
915 if (adapter->class & I2C_CLASS_TV_ANALOG)
916 return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
920 static int cx25840_detach_client(struct i2c_client *client)
922 struct cx25840_state *state = i2c_get_clientdata(client);
925 err = i2c_detach_client(client);
936 /* ----------------------------------------------------------------------- */
938 static struct i2c_driver i2c_driver_cx25840 = {
942 .id = I2C_DRIVERID_CX25840,
943 .attach_adapter = cx25840_attach_adapter,
944 .detach_client = cx25840_detach_client,
945 .command = cx25840_command,
949 static int __init m__init(void)
951 return i2c_add_driver(&i2c_driver_cx25840);
954 static void __exit m__exit(void)
956 i2c_del_driver(&i2c_driver_cx25840);
959 module_init(m__init);
960 module_exit(m__exit);
962 /* ----------------------------------------------------------------------- */
964 static void log_status(struct i2c_client *client)
966 static const char *const fmt_strs[] = {
968 "NTSC-M", "NTSC-J", "NTSC-4.43",
969 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
975 struct cx25840_state *state = i2c_get_clientdata(client);
976 u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
977 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
978 u8 gen_stat1 = cx25840_read(client, 0x40d);
979 u8 download_ctl = cx25840_read(client, 0x803);
980 u8 mod_det_stat0 = cx25840_read(client, 0x804);
981 u8 mod_det_stat1 = cx25840_read(client, 0x805);
982 u8 audio_config = cx25840_read(client, 0x808);
983 u8 pref_mode = cx25840_read(client, 0x809);
984 u8 afc0 = cx25840_read(client, 0x80b);
985 u8 mute_ctl = cx25840_read(client, 0x8d3);
986 int vid_input = state->vid_input;
987 int aud_input = state->aud_input;
990 v4l_info(client, "Video signal: %spresent\n",
991 (microctrl_vidfmt & 0x10) ? "" : "not ");
992 v4l_info(client, "Detected format: %s\n",
993 fmt_strs[gen_stat1 & 0xf]);
995 switch (mod_det_stat0) {
996 case 0x00: p = "mono"; break;
997 case 0x01: p = "stereo"; break;
998 case 0x02: p = "dual"; break;
999 case 0x04: p = "tri"; break;
1000 case 0x10: p = "mono with SAP"; break;
1001 case 0x11: p = "stereo with SAP"; break;
1002 case 0x12: p = "dual with SAP"; break;
1003 case 0x14: p = "tri with SAP"; break;
1004 case 0xfe: p = "forced mode"; break;
1005 default: p = "not defined";
1007 v4l_info(client, "Detected audio mode: %s\n", p);
1009 switch (mod_det_stat1) {
1010 case 0x00: p = "not defined"; break;
1011 case 0x01: p = "EIAJ"; break;
1012 case 0x02: p = "A2-M"; break;
1013 case 0x03: p = "A2-BG"; break;
1014 case 0x04: p = "A2-DK1"; break;
1015 case 0x05: p = "A2-DK2"; break;
1016 case 0x06: p = "A2-DK3"; break;
1017 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1018 case 0x08: p = "AM-L"; break;
1019 case 0x09: p = "NICAM-BG"; break;
1020 case 0x0a: p = "NICAM-DK"; break;
1021 case 0x0b: p = "NICAM-I"; break;
1022 case 0x0c: p = "NICAM-L"; break;
1023 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1024 case 0x0e: p = "IF FM Radio"; break;
1025 case 0x0f: p = "BTSC"; break;
1026 case 0x10: p = "high-deviation FM"; break;
1027 case 0x11: p = "very high-deviation FM"; break;
1028 case 0xfd: p = "unknown audio standard"; break;
1029 case 0xfe: p = "forced audio standard"; break;
1030 case 0xff: p = "no detected audio standard"; break;
1031 default: p = "not defined";
1033 v4l_info(client, "Detected audio standard: %s\n", p);
1034 v4l_info(client, "Audio muted: %s\n",
1035 (mute_ctl & 0x2) ? "yes" : "no");
1036 v4l_info(client, "Audio microcontroller: %s\n",
1037 (download_ctl & 0x10) ? "running" : "stopped");
1039 switch (audio_config >> 4) {
1040 case 0x00: p = "undefined"; break;
1041 case 0x01: p = "BTSC"; break;
1042 case 0x02: p = "EIAJ"; break;
1043 case 0x03: p = "A2-M"; break;
1044 case 0x04: p = "A2-BG"; break;
1045 case 0x05: p = "A2-DK1"; break;
1046 case 0x06: p = "A2-DK2"; break;
1047 case 0x07: p = "A2-DK3"; break;
1048 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1049 case 0x09: p = "AM-L"; break;
1050 case 0x0a: p = "NICAM-BG"; break;
1051 case 0x0b: p = "NICAM-DK"; break;
1052 case 0x0c: p = "NICAM-I"; break;
1053 case 0x0d: p = "NICAM-L"; break;
1054 case 0x0e: p = "FM radio"; break;
1055 case 0x0f: p = "automatic detection"; break;
1056 default: p = "undefined";
1058 v4l_info(client, "Configured audio standard: %s\n", p);
1060 if ((audio_config >> 4) < 0xF) {
1061 switch (audio_config & 0xF) {
1062 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1063 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1064 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1065 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1066 case 0x04: p = "STEREO"; break;
1067 case 0x05: p = "DUAL1 (AB)"; break;
1068 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1069 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1070 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1071 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1072 case 0x0a: p = "SAP"; break;
1073 default: p = "undefined";
1075 v4l_info(client, "Configured audio mode: %s\n", p);
1077 switch (audio_config & 0xF) {
1078 case 0x00: p = "BG"; break;
1079 case 0x01: p = "DK1"; break;
1080 case 0x02: p = "DK2"; break;
1081 case 0x03: p = "DK3"; break;
1082 case 0x04: p = "I"; break;
1083 case 0x05: p = "L"; break;
1084 case 0x06: p = "BTSC"; break;
1085 case 0x07: p = "EIAJ"; break;
1086 case 0x08: p = "A2-M"; break;
1087 case 0x09: p = "FM Radio"; break;
1088 case 0x0f: p = "automatic standard and mode detection"; break;
1089 default: p = "undefined";
1091 v4l_info(client, "Configured audio system: %s\n", p);
1094 v4l_info(client, "Specified standard: %s\n",
1095 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
1097 if (vid_input >= CX25840_COMPOSITE1 &&
1098 vid_input <= CX25840_COMPOSITE8) {
1099 v4l_info(client, "Specified video input: Composite %d\n",
1100 vid_input - CX25840_COMPOSITE1 + 1);
1102 v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
1103 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
1106 v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
1108 v4l_info(client, "Specified audio input: External\n");
1111 v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
1113 switch (pref_mode & 0xf) {
1114 case 0: p = "mono/language A"; break;
1115 case 1: p = "language B"; break;
1116 case 2: p = "language C"; break;
1117 case 3: p = "analog fallback"; break;
1118 case 4: p = "stereo"; break;
1119 case 5: p = "language AC"; break;
1120 case 6: p = "language BC"; break;
1121 case 7: p = "language AB"; break;
1122 default: p = "undefined";
1124 v4l_info(client, "Preferred audio mode: %s\n", p);
1126 if ((audio_config & 0xf) == 0xf) {
1127 switch ((afc0 >> 3) & 0x3) {
1128 case 0: p = "system DK"; break;
1129 case 1: p = "system L"; break;
1130 case 2: p = "autodetect"; break;
1131 default: p = "undefined";
1133 v4l_info(client, "Selected 65 MHz format: %s\n", p);
1135 switch (afc0 & 0x7) {
1136 case 0: p = "chroma"; break;
1137 case 1: p = "BTSC"; break;
1138 case 2: p = "EIAJ"; break;
1139 case 3: p = "A2-M"; break;
1140 case 4: p = "autodetect"; break;
1141 default: p = "undefined";
1143 v4l_info(client, "Selected 45 MHz format: %s\n", p);