V4L/DVB (5324): This patch fixes request_module_depend()
[linux-2.6] / drivers / media / video / cx25840 / cx25840-core.c
1 /* 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  * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
14  * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
29  */
30
31
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/videodev2.h>
36 #include <linux/i2c.h>
37 #include <media/v4l2-common.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/cx25840.h>
40
41 #include "cx25840-core.h"
42
43 MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
44 MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
45 MODULE_LICENSE("GPL");
46
47 static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
48
49
50 int cx25840_debug;
51
52 module_param_named(debug,cx25840_debug, int, 0644);
53
54 MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
55
56 I2C_CLIENT_INSMOD;
57
58 /* ----------------------------------------------------------------------- */
59
60 int 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
69 int 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
81 u8 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
96 u32 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[3] << 24) | (buffer[2] << 16) |
109             (buffer[1] << 8) | buffer[0];
110 }
111
112 int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned 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
122 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
123                                                 enum cx25840_audio_input aud_input);
124 static void log_audio_status(struct i2c_client *client);
125 static void log_video_status(struct i2c_client *client);
126
127 /* ----------------------------------------------------------------------- */
128
129 static void init_dll1(struct i2c_client *client)
130 {
131         /* This is the Hauppauge sequence used to
132          * initialize the Delay Lock Loop 1 (ADC DLL). */
133         cx25840_write(client, 0x159, 0x23);
134         cx25840_write(client, 0x15a, 0x87);
135         cx25840_write(client, 0x15b, 0x06);
136         cx25840_write(client, 0x159, 0xe1);
137         cx25840_write(client, 0x15a, 0x86);
138         cx25840_write(client, 0x159, 0xe0);
139         cx25840_write(client, 0x159, 0xe1);
140         cx25840_write(client, 0x15b, 0x10);
141 }
142
143 static void init_dll2(struct i2c_client *client)
144 {
145         /* This is the Hauppauge sequence used to
146          * initialize the Delay Lock Loop 2 (ADC DLL). */
147         cx25840_write(client, 0x15d, 0xe3);
148         cx25840_write(client, 0x15e, 0x86);
149         cx25840_write(client, 0x15f, 0x06);
150         cx25840_write(client, 0x15d, 0xe1);
151         cx25840_write(client, 0x15d, 0xe0);
152         cx25840_write(client, 0x15d, 0xe1);
153 }
154
155 static void cx25836_initialize(struct i2c_client *client)
156 {
157         /* reset configuration is described on page 3-77 of the CX25836 datasheet */
158         /* 2. */
159         cx25840_and_or(client, 0x000, ~0x01, 0x01);
160         cx25840_and_or(client, 0x000, ~0x01, 0x00);
161         /* 3a. */
162         cx25840_and_or(client, 0x15a, ~0x70, 0x00);
163         /* 3b. */
164         cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
165         /* 3c. */
166         cx25840_and_or(client, 0x159, ~0x02, 0x02);
167         /* 3d. */
168         /* There should be a 10-us delay here, but since the
169            i2c bus already has a 10-us delay we don't need to do
170            anything */
171         /* 3e. */
172         cx25840_and_or(client, 0x159, ~0x02, 0x00);
173         /* 3f. */
174         cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
175         /* 3g. */
176         cx25840_and_or(client, 0x159, ~0x01, 0x00);
177         cx25840_and_or(client, 0x159, ~0x01, 0x01);
178         /* 3h. */
179         cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
180 }
181
182 static void cx25840_initialize(struct i2c_client *client, int loadfw)
183 {
184         struct cx25840_state *state = i2c_get_clientdata(client);
185
186         /* datasheet startup in numbered steps, refer to page 3-77 */
187         /* 2. */
188         cx25840_and_or(client, 0x803, ~0x10, 0x00);
189         /* The default of this register should be 4, but I get 0 instead.
190          * Set this register to 4 manually. */
191         cx25840_write(client, 0x000, 0x04);
192         /* 3. */
193         init_dll1(client);
194         init_dll2(client);
195         cx25840_write(client, 0x136, 0x0a);
196         /* 4. */
197         cx25840_write(client, 0x13c, 0x01);
198         cx25840_write(client, 0x13c, 0x00);
199         /* 5. */
200         if (loadfw)
201                 cx25840_loadfw(client);
202         /* 6. */
203         cx25840_write(client, 0x115, 0x8c);
204         cx25840_write(client, 0x116, 0x07);
205         cx25840_write(client, 0x118, 0x02);
206         /* 7. */
207         cx25840_write(client, 0x4a5, 0x80);
208         cx25840_write(client, 0x4a5, 0x00);
209         cx25840_write(client, 0x402, 0x00);
210         /* 8. */
211         cx25840_and_or(client, 0x401, ~0x18, 0);
212         cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
213         /* steps 8c and 8d are done in change_input() */
214         /* 10. */
215         cx25840_write(client, 0x8d3, 0x1f);
216         cx25840_write(client, 0x8e3, 0x03);
217
218         cx25840_vbi_setup(client);
219
220         /* trial and error says these are needed to get audio */
221         cx25840_write(client, 0x914, 0xa0);
222         cx25840_write(client, 0x918, 0xa0);
223         cx25840_write(client, 0x919, 0x01);
224
225         /* stereo prefered */
226         cx25840_write(client, 0x809, 0x04);
227         /* AC97 shift */
228         cx25840_write(client, 0x8cf, 0x0f);
229
230         /* (re)set input */
231         set_input(client, state->vid_input, state->aud_input);
232
233         /* start microcontroller */
234         cx25840_and_or(client, 0x803, ~0x10, 0x10);
235 }
236
237 /* ----------------------------------------------------------------------- */
238
239 static void input_change(struct i2c_client *client)
240 {
241         struct cx25840_state *state = i2c_get_clientdata(client);
242         v4l2_std_id std = cx25840_get_v4lstd(client);
243
244         /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
245         if (std & V4L2_STD_SECAM) {
246                 cx25840_write(client, 0x402, 0);
247         }
248         else {
249                 cx25840_write(client, 0x402, 0x04);
250                 cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
251         }
252         cx25840_and_or(client, 0x401, ~0x60, 0);
253         cx25840_and_or(client, 0x401, ~0x60, 0x60);
254
255         if (std & V4L2_STD_525_60) {
256                 /* Certain Hauppauge PVR150 models have a hardware bug
257                    that causes audio to drop out. For these models the
258                    audio standard must be set explicitly.
259                    To be precise: it affects cards with tuner models
260                    85, 99 and 112 (model numbers from tveeprom). */
261                 int hw_fix = state->pvr150_workaround;
262
263                 if (std == V4L2_STD_NTSC_M_JP) {
264                         /* Japan uses EIAJ audio standard */
265                         cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
266                 } else if (std == V4L2_STD_NTSC_M_KR) {
267                         /* South Korea uses A2 audio standard */
268                         cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
269                 } else {
270                         /* Others use the BTSC audio standard */
271                         cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
272                 }
273                 cx25840_write(client, 0x80b, 0x00);
274         } else if (std & V4L2_STD_PAL) {
275                 /* Follow tuner change procedure for PAL */
276                 cx25840_write(client, 0x808, 0xff);
277                 cx25840_write(client, 0x80b, 0x10);
278         } else if (std & V4L2_STD_SECAM) {
279                 /* Select autodetect for SECAM */
280                 cx25840_write(client, 0x808, 0xff);
281                 cx25840_write(client, 0x80b, 0x10);
282         }
283
284         if (cx25840_read(client, 0x803) & 0x10) {
285                 /* restart audio decoder microcontroller */
286                 cx25840_and_or(client, 0x803, ~0x10, 0x00);
287                 cx25840_and_or(client, 0x803, ~0x10, 0x10);
288         }
289 }
290
291 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
292                                                 enum cx25840_audio_input aud_input)
293 {
294         struct cx25840_state *state = i2c_get_clientdata(client);
295         u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
296                            vid_input <= CX25840_COMPOSITE8);
297         u8 reg;
298
299         v4l_dbg(1, cx25840_debug, client, "decoder set video input %d, audio input %d\n",
300                         vid_input, aud_input);
301
302         if (is_composite) {
303                 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
304         } else {
305                 int luma = vid_input & 0xf0;
306                 int chroma = vid_input & 0xf00;
307
308                 if ((vid_input & ~0xff0) ||
309                     luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
310                     chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
311                         v4l_err(client, "0x%04x is not a valid video input!\n", vid_input);
312                         return -EINVAL;
313                 }
314                 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
315                 if (chroma >= CX25840_SVIDEO_CHROMA7) {
316                         reg &= 0x3f;
317                         reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
318                 } else {
319                         reg &= 0xcf;
320                         reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
321                 }
322         }
323
324         switch (aud_input) {
325         case CX25840_AUDIO_SERIAL:
326                 /* do nothing, use serial audio input */
327                 break;
328         case CX25840_AUDIO4: reg &= ~0x30; break;
329         case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
330         case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
331         case CX25840_AUDIO7: reg &= ~0xc0; break;
332         case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
333
334         default:
335                 v4l_err(client, "0x%04x is not a valid audio input!\n", aud_input);
336                 return -EINVAL;
337         }
338
339         cx25840_write(client, 0x103, reg);
340         /* Set INPUT_MODE to Composite (0) or S-Video (1) */
341         cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
342         /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
343         cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
344         /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
345         if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
346                 cx25840_and_or(client, 0x102, ~0x4, 4);
347         else
348                 cx25840_and_or(client, 0x102, ~0x4, 0);
349
350         state->vid_input = vid_input;
351         state->aud_input = aud_input;
352         if (!state->is_cx25836) {
353                 cx25840_audio_set_path(client);
354                 input_change(client);
355         }
356         return 0;
357 }
358
359 /* ----------------------------------------------------------------------- */
360
361 static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
362 {
363         u8 fmt=0;       /* zero is autodetect */
364
365         /* First tests should be against specific std */
366         if (std == V4L2_STD_NTSC_M_JP) {
367                 fmt=0x2;
368         } else if (std == V4L2_STD_NTSC_443) {
369                 fmt=0x3;
370         } else if (std == V4L2_STD_PAL_M) {
371                 fmt=0x5;
372         } else if (std == V4L2_STD_PAL_N) {
373                 fmt=0x6;
374         } else if (std == V4L2_STD_PAL_Nc) {
375                 fmt=0x7;
376         } else if (std == V4L2_STD_PAL_60) {
377                 fmt=0x8;
378         } else {
379                 /* Then, test against generic ones */
380                 if (std & V4L2_STD_NTSC) {
381                         fmt=0x1;
382                 } else if (std & V4L2_STD_PAL) {
383                         fmt=0x4;
384                 } else if (std & V4L2_STD_SECAM) {
385                         fmt=0xc;
386                 }
387         }
388
389         v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
390
391         /* Follow step 9 of section 3.16 in the cx25840 datasheet.
392            Without this PAL may display a vertical ghosting effect.
393            This happens for example with the Yuan MPC622. */
394         if (fmt >= 4 && fmt < 8) {
395                 /* Set format to NTSC-M */
396                 cx25840_and_or(client, 0x400, ~0xf, 1);
397                 /* Turn off LCOMB */
398                 cx25840_and_or(client, 0x47b, ~6, 0);
399         }
400         cx25840_and_or(client, 0x400, ~0xf, fmt);
401         cx25840_vbi_setup(client);
402         return 0;
403 }
404
405 v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
406 {
407         struct cx25840_state *state = i2c_get_clientdata(client);
408         /* check VID_FMT_SEL first */
409         u8 fmt = cx25840_read(client, 0x400) & 0xf;
410
411         if (!fmt) {
412                 /* check AFD_FMT_STAT if set to autodetect */
413                 fmt = cx25840_read(client, 0x40d) & 0xf;
414         }
415
416         switch (fmt) {
417         case 0x1:
418         {
419                 /* if the audio std is A2-M, then this is the South Korean
420                    NTSC standard */
421                 if (!state->is_cx25836 && cx25840_read(client, 0x805) == 2)
422                         return V4L2_STD_NTSC_M_KR;
423                 return V4L2_STD_NTSC_M;
424         }
425         case 0x2: return V4L2_STD_NTSC_M_JP;
426         case 0x3: return V4L2_STD_NTSC_443;
427         case 0x4: return V4L2_STD_PAL;
428         case 0x5: return V4L2_STD_PAL_M;
429         case 0x6: return V4L2_STD_PAL_N;
430         case 0x7: return V4L2_STD_PAL_Nc;
431         case 0x8: return V4L2_STD_PAL_60;
432         case 0xc: return V4L2_STD_SECAM;
433         default: return V4L2_STD_UNKNOWN;
434         }
435 }
436
437 /* ----------------------------------------------------------------------- */
438
439 static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
440 {
441         struct cx25840_state *state = i2c_get_clientdata(client);
442
443         switch (ctrl->id) {
444         case CX25840_CID_ENABLE_PVR150_WORKAROUND:
445                 state->pvr150_workaround = ctrl->value;
446                 set_input(client, state->vid_input, state->aud_input);
447                 break;
448
449         case V4L2_CID_BRIGHTNESS:
450                 if (ctrl->value < 0 || ctrl->value > 255) {
451                         v4l_err(client, "invalid brightness setting %d\n",
452                                     ctrl->value);
453                         return -ERANGE;
454                 }
455
456                 cx25840_write(client, 0x414, ctrl->value - 128);
457                 break;
458
459         case V4L2_CID_CONTRAST:
460                 if (ctrl->value < 0 || ctrl->value > 127) {
461                         v4l_err(client, "invalid contrast setting %d\n",
462                                     ctrl->value);
463                         return -ERANGE;
464                 }
465
466                 cx25840_write(client, 0x415, ctrl->value << 1);
467                 break;
468
469         case V4L2_CID_SATURATION:
470                 if (ctrl->value < 0 || ctrl->value > 127) {
471                         v4l_err(client, "invalid saturation setting %d\n",
472                                     ctrl->value);
473                         return -ERANGE;
474                 }
475
476                 cx25840_write(client, 0x420, ctrl->value << 1);
477                 cx25840_write(client, 0x421, ctrl->value << 1);
478                 break;
479
480         case V4L2_CID_HUE:
481                 if (ctrl->value < -127 || ctrl->value > 127) {
482                         v4l_err(client, "invalid hue setting %d\n", ctrl->value);
483                         return -ERANGE;
484                 }
485
486                 cx25840_write(client, 0x422, ctrl->value);
487                 break;
488
489         case V4L2_CID_AUDIO_VOLUME:
490         case V4L2_CID_AUDIO_BASS:
491         case V4L2_CID_AUDIO_TREBLE:
492         case V4L2_CID_AUDIO_BALANCE:
493         case V4L2_CID_AUDIO_MUTE:
494                 if (state->is_cx25836)
495                         return -EINVAL;
496                 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
497
498         default:
499                 return -EINVAL;
500         }
501
502         return 0;
503 }
504
505 static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
506 {
507         struct cx25840_state *state = i2c_get_clientdata(client);
508
509         switch (ctrl->id) {
510         case CX25840_CID_ENABLE_PVR150_WORKAROUND:
511                 ctrl->value = state->pvr150_workaround;
512                 break;
513         case V4L2_CID_BRIGHTNESS:
514                 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
515                 break;
516         case V4L2_CID_CONTRAST:
517                 ctrl->value = cx25840_read(client, 0x415) >> 1;
518                 break;
519         case V4L2_CID_SATURATION:
520                 ctrl->value = cx25840_read(client, 0x420) >> 1;
521                 break;
522         case V4L2_CID_HUE:
523                 ctrl->value = (s8)cx25840_read(client, 0x422);
524                 break;
525         case V4L2_CID_AUDIO_VOLUME:
526         case V4L2_CID_AUDIO_BASS:
527         case V4L2_CID_AUDIO_TREBLE:
528         case V4L2_CID_AUDIO_BALANCE:
529         case V4L2_CID_AUDIO_MUTE:
530                 if (state->is_cx25836)
531                         return -EINVAL;
532                 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
533         default:
534                 return -EINVAL;
535         }
536
537         return 0;
538 }
539
540 /* ----------------------------------------------------------------------- */
541
542 static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
543 {
544         switch (fmt->type) {
545         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
546                 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
547         default:
548                 return -EINVAL;
549         }
550
551         return 0;
552 }
553
554 static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
555 {
556         struct v4l2_pix_format *pix;
557         int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
558         int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
559
560         switch (fmt->type) {
561         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
562                 pix = &(fmt->fmt.pix);
563
564                 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
565                 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
566
567                 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
568                 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
569
570                 Vlines = pix->height + (is_pal ? 4 : 7);
571
572                 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
573                     (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
574                         v4l_err(client, "%dx%d is not a valid size!\n",
575                                     pix->width, pix->height);
576                         return -ERANGE;
577                 }
578
579                 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
580                 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
581                 VSC &= 0x1fff;
582
583                 if (pix->width >= 385)
584                         filter = 0;
585                 else if (pix->width > 192)
586                         filter = 1;
587                 else if (pix->width > 96)
588                         filter = 2;
589                 else
590                         filter = 3;
591
592                 v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale  %ux%u\n",
593                             pix->width, pix->height, HSC, VSC);
594
595                 /* HSCALE=HSC */
596                 cx25840_write(client, 0x418, HSC & 0xff);
597                 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
598                 cx25840_write(client, 0x41a, HSC >> 16);
599                 /* VSCALE=VSC */
600                 cx25840_write(client, 0x41c, VSC & 0xff);
601                 cx25840_write(client, 0x41d, VSC >> 8);
602                 /* VS_INTRLACE=1 VFILT=filter */
603                 cx25840_write(client, 0x41e, 0x8 | filter);
604                 break;
605
606         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
607                 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
608
609         case V4L2_BUF_TYPE_VBI_CAPTURE:
610                 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
611
612         default:
613                 return -EINVAL;
614         }
615
616         return 0;
617 }
618
619 /* ----------------------------------------------------------------------- */
620
621 static int cx25840_command(struct i2c_client *client, unsigned int cmd,
622                            void *arg)
623 {
624         struct cx25840_state *state = i2c_get_clientdata(client);
625         struct v4l2_tuner *vt = arg;
626         struct v4l2_routing *route = arg;
627
628         switch (cmd) {
629 #ifdef CONFIG_VIDEO_ADV_DEBUG
630         /* ioctls to allow direct access to the
631          * cx25840 registers for testing */
632         case VIDIOC_DBG_G_REGISTER:
633         case VIDIOC_DBG_S_REGISTER:
634         {
635                 struct v4l2_register *reg = arg;
636
637                 if (!v4l2_chip_match_i2c_client(client, reg->match_type, reg->match_chip))
638                         return -EINVAL;
639                 if (!capable(CAP_SYS_ADMIN))
640                         return -EPERM;
641                 if (cmd == VIDIOC_DBG_G_REGISTER)
642                         reg->val = cx25840_read(client, reg->reg & 0x0fff);
643                 else
644                         cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
645                 break;
646         }
647 #endif
648
649         case VIDIOC_INT_DECODE_VBI_LINE:
650                 return cx25840_vbi(client, cmd, arg);
651
652         case VIDIOC_INT_AUDIO_CLOCK_FREQ:
653                 return cx25840_audio(client, cmd, arg);
654
655         case VIDIOC_STREAMON:
656                 v4l_dbg(1, cx25840_debug, client, "enable output\n");
657                 cx25840_write(client, 0x115, state->is_cx25836 ? 0x0c : 0x8c);
658                 cx25840_write(client, 0x116, state->is_cx25836 ? 0x04 : 0x07);
659                 break;
660
661         case VIDIOC_STREAMOFF:
662                 v4l_dbg(1, cx25840_debug, client, "disable output\n");
663                 cx25840_write(client, 0x115, 0x00);
664                 cx25840_write(client, 0x116, 0x00);
665                 break;
666
667         case VIDIOC_LOG_STATUS:
668                 log_video_status(client);
669                 if (!state->is_cx25836)
670                         log_audio_status(client);
671                 break;
672
673         case VIDIOC_G_CTRL:
674                 return get_v4lctrl(client, (struct v4l2_control *)arg);
675
676         case VIDIOC_S_CTRL:
677                 return set_v4lctrl(client, (struct v4l2_control *)arg);
678
679         case VIDIOC_QUERYCTRL:
680         {
681                 struct v4l2_queryctrl *qc = arg;
682
683                 switch (qc->id) {
684                         case V4L2_CID_BRIGHTNESS:
685                         case V4L2_CID_CONTRAST:
686                         case V4L2_CID_SATURATION:
687                         case V4L2_CID_HUE:
688                                 return v4l2_ctrl_query_fill_std(qc);
689                         default:
690                                 break;
691                 }
692                 if (state->is_cx25836)
693                         return -EINVAL;
694
695                 switch (qc->id) {
696                         case V4L2_CID_AUDIO_VOLUME:
697                         case V4L2_CID_AUDIO_MUTE:
698                         case V4L2_CID_AUDIO_BALANCE:
699                         case V4L2_CID_AUDIO_BASS:
700                         case V4L2_CID_AUDIO_TREBLE:
701                                 return v4l2_ctrl_query_fill_std(qc);
702                         default:
703                                 return -EINVAL;
704                 }
705                 return -EINVAL;
706         }
707
708         case VIDIOC_G_STD:
709                 *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
710                 break;
711
712         case VIDIOC_S_STD:
713                 state->radio = 0;
714                 return set_v4lstd(client, *(v4l2_std_id *)arg);
715
716         case AUDC_SET_RADIO:
717                 state->radio = 1;
718                 break;
719
720         case VIDIOC_INT_G_VIDEO_ROUTING:
721                 route->input = state->vid_input;
722                 route->output = 0;
723                 break;
724
725         case VIDIOC_INT_S_VIDEO_ROUTING:
726                 return set_input(client, route->input, state->aud_input);
727
728         case VIDIOC_INT_G_AUDIO_ROUTING:
729                 if (state->is_cx25836)
730                         return -EINVAL;
731                 route->input = state->aud_input;
732                 route->output = 0;
733                 break;
734
735         case VIDIOC_INT_S_AUDIO_ROUTING:
736                 if (state->is_cx25836)
737                         return -EINVAL;
738                 return set_input(client, state->vid_input, route->input);
739
740         case VIDIOC_S_FREQUENCY:
741                 if (!state->is_cx25836) {
742                         input_change(client);
743                 }
744                 break;
745
746         case VIDIOC_G_TUNER:
747         {
748                 u8 vpres = cx25840_read(client, 0x40e) & 0x20;
749                 u8 mode;
750                 int val = 0;
751
752                 if (state->radio)
753                         break;
754
755                 vt->signal = vpres ? 0xffff : 0x0;
756                 if (state->is_cx25836)
757                         break;
758
759                 vt->capability |=
760                     V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
761                     V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
762
763                 mode = cx25840_read(client, 0x804);
764
765                 /* get rxsubchans and audmode */
766                 if ((mode & 0xf) == 1)
767                         val |= V4L2_TUNER_SUB_STEREO;
768                 else
769                         val |= V4L2_TUNER_SUB_MONO;
770
771                 if (mode == 2 || mode == 4)
772                         val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
773
774                 if (mode & 0x10)
775                         val |= V4L2_TUNER_SUB_SAP;
776
777                 vt->rxsubchans = val;
778                 vt->audmode = state->audmode;
779                 break;
780         }
781
782         case VIDIOC_S_TUNER:
783                 if (state->radio || state->is_cx25836)
784                         break;
785
786                 switch (vt->audmode) {
787                 case V4L2_TUNER_MODE_MONO:
788                         /* mono      -> mono
789                            stereo    -> mono
790                            bilingual -> lang1 */
791                         cx25840_and_or(client, 0x809, ~0xf, 0x00);
792                         break;
793                 case V4L2_TUNER_MODE_STEREO:
794                 case V4L2_TUNER_MODE_LANG1:
795                         /* mono      -> mono
796                            stereo    -> stereo
797                            bilingual -> lang1 */
798                         cx25840_and_or(client, 0x809, ~0xf, 0x04);
799                         break;
800                 case V4L2_TUNER_MODE_LANG1_LANG2:
801                         /* mono      -> mono
802                            stereo    -> stereo
803                            bilingual -> lang1/lang2 */
804                         cx25840_and_or(client, 0x809, ~0xf, 0x07);
805                         break;
806                 case V4L2_TUNER_MODE_LANG2:
807                         /* mono      -> mono
808                            stereo    -> stereo
809                            bilingual -> lang2 */
810                         cx25840_and_or(client, 0x809, ~0xf, 0x01);
811                         break;
812                 default:
813                         return -EINVAL;
814                 }
815                 state->audmode = vt->audmode;
816                 break;
817
818         case VIDIOC_G_FMT:
819                 return get_v4lfmt(client, (struct v4l2_format *)arg);
820
821         case VIDIOC_S_FMT:
822                 return set_v4lfmt(client, (struct v4l2_format *)arg);
823
824         case VIDIOC_INT_RESET:
825                 if (state->is_cx25836)
826                         cx25836_initialize(client);
827                 else
828                         cx25840_initialize(client, 0);
829                 break;
830
831         case VIDIOC_G_CHIP_IDENT:
832                 return v4l2_chip_ident_i2c_client(client, arg, state->id, state->rev);
833
834         default:
835                 return -EINVAL;
836         }
837
838         return 0;
839 }
840
841 /* ----------------------------------------------------------------------- */
842
843 static struct i2c_driver i2c_driver_cx25840;
844
845 static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
846                                  int kind)
847 {
848         struct i2c_client *client;
849         struct cx25840_state *state;
850         u32 id;
851         u16 device_id;
852
853         /* Check if the adapter supports the needed features
854          * Not until kernel version 2.6.11 did the bit-algo
855          * correctly report that it would do an I2C-level xfer */
856         if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
857                 return 0;
858
859         state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
860         if (state == 0)
861                 return -ENOMEM;
862
863         client = &state->c;
864         client->addr = address;
865         client->adapter = adapter;
866         client->driver = &i2c_driver_cx25840;
867         snprintf(client->name, sizeof(client->name) - 1, "cx25840");
868
869         v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", address << 1);
870
871         device_id = cx25840_read(client, 0x101) << 8;
872         device_id |= cx25840_read(client, 0x100);
873
874         /* The high byte of the device ID should be
875          * 0x83 for the cx2583x and 0x84 for the cx2584x */
876         if ((device_id & 0xff00) == 0x8300) {
877                 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
878                 state->is_cx25836 = 1;
879         }
880         else if ((device_id & 0xff00) == 0x8400) {
881                 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
882                 state->is_cx25836 = 0;
883         }
884         else {
885                 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
886                 kfree(state);
887                 return 0;
888         }
889
890         /* Note: revision '(device_id & 0x0f) == 2' was never built. The
891            marking skips from 0x1 == 22 to 0x3 == 23. */
892         v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
893                     (device_id & 0xfff0) >> 4,
894                     (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
895                     address << 1, adapter->name);
896
897         i2c_set_clientdata(client, state);
898         state->vid_input = CX25840_COMPOSITE7;
899         state->aud_input = CX25840_AUDIO8;
900         state->audclk_freq = 48000;
901         state->pvr150_workaround = 0;
902         state->audmode = V4L2_TUNER_MODE_LANG1;
903         state->vbi_line_offset = 8;
904         state->id = id;
905         state->rev = device_id;
906
907         i2c_attach_client(client);
908
909         if (state->is_cx25836)
910                 cx25836_initialize(client);
911         else
912                 cx25840_initialize(client, 1);
913
914         return 0;
915 }
916
917 static int cx25840_attach_adapter(struct i2c_adapter *adapter)
918 {
919         if (adapter->class & I2C_CLASS_TV_ANALOG)
920                 return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
921         return 0;
922 }
923
924 static int cx25840_detach_client(struct i2c_client *client)
925 {
926         struct cx25840_state *state = i2c_get_clientdata(client);
927         int err;
928
929         err = i2c_detach_client(client);
930         if (err) {
931                 return err;
932         }
933
934         kfree(state);
935
936         return 0;
937 }
938
939 /* ----------------------------------------------------------------------- */
940
941 static struct i2c_driver i2c_driver_cx25840 = {
942         .driver = {
943                 .name = "cx25840",
944         },
945         .id = I2C_DRIVERID_CX25840,
946         .attach_adapter = cx25840_attach_adapter,
947         .detach_client = cx25840_detach_client,
948         .command = cx25840_command,
949 };
950
951
952 static int __init m__init(void)
953 {
954         return i2c_add_driver(&i2c_driver_cx25840);
955 }
956
957 static void __exit m__exit(void)
958 {
959         i2c_del_driver(&i2c_driver_cx25840);
960 }
961
962 module_init(m__init);
963 module_exit(m__exit);
964
965 /* ----------------------------------------------------------------------- */
966
967 static void log_video_status(struct i2c_client *client)
968 {
969         static const char *const fmt_strs[] = {
970                 "0x0",
971                 "NTSC-M", "NTSC-J", "NTSC-4.43",
972                 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
973                 "0x9", "0xA", "0xB",
974                 "SECAM",
975                 "0xD", "0xE", "0xF"
976         };
977
978         struct cx25840_state *state = i2c_get_clientdata(client);
979         u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
980         u8 gen_stat1 = cx25840_read(client, 0x40d);
981         u8 gen_stat2 = cx25840_read(client, 0x40e);
982         int vid_input = state->vid_input;
983
984         v4l_info(client, "Video signal:              %spresent\n",
985                     (gen_stat2 & 0x20) ? "" : "not ");
986         v4l_info(client, "Detected format:           %s\n",
987                     fmt_strs[gen_stat1 & 0xf]);
988
989         v4l_info(client, "Specified standard:        %s\n",
990                     vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
991
992         if (vid_input >= CX25840_COMPOSITE1 &&
993             vid_input <= CX25840_COMPOSITE8) {
994                 v4l_info(client, "Specified video input:     Composite %d\n",
995                         vid_input - CX25840_COMPOSITE1 + 1);
996         } else {
997                 v4l_info(client, "Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
998                         (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
999         }
1000
1001         v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
1002 }
1003
1004 /* ----------------------------------------------------------------------- */
1005
1006 static void log_audio_status(struct i2c_client *client)
1007 {
1008         struct cx25840_state *state = i2c_get_clientdata(client);
1009         u8 download_ctl = cx25840_read(client, 0x803);
1010         u8 mod_det_stat0 = cx25840_read(client, 0x804);
1011         u8 mod_det_stat1 = cx25840_read(client, 0x805);
1012         u8 audio_config = cx25840_read(client, 0x808);
1013         u8 pref_mode = cx25840_read(client, 0x809);
1014         u8 afc0 = cx25840_read(client, 0x80b);
1015         u8 mute_ctl = cx25840_read(client, 0x8d3);
1016         int aud_input = state->aud_input;
1017         char *p;
1018
1019         switch (mod_det_stat0) {
1020         case 0x00: p = "mono"; break;
1021         case 0x01: p = "stereo"; break;
1022         case 0x02: p = "dual"; break;
1023         case 0x04: p = "tri"; break;
1024         case 0x10: p = "mono with SAP"; break;
1025         case 0x11: p = "stereo with SAP"; break;
1026         case 0x12: p = "dual with SAP"; break;
1027         case 0x14: p = "tri with SAP"; break;
1028         case 0xfe: p = "forced mode"; break;
1029         default: p = "not defined";
1030         }
1031         v4l_info(client, "Detected audio mode:       %s\n", p);
1032
1033         switch (mod_det_stat1) {
1034         case 0x00: p = "not defined"; break;
1035         case 0x01: p = "EIAJ"; break;
1036         case 0x02: p = "A2-M"; break;
1037         case 0x03: p = "A2-BG"; break;
1038         case 0x04: p = "A2-DK1"; break;
1039         case 0x05: p = "A2-DK2"; break;
1040         case 0x06: p = "A2-DK3"; break;
1041         case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1042         case 0x08: p = "AM-L"; break;
1043         case 0x09: p = "NICAM-BG"; break;
1044         case 0x0a: p = "NICAM-DK"; break;
1045         case 0x0b: p = "NICAM-I"; break;
1046         case 0x0c: p = "NICAM-L"; break;
1047         case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1048         case 0x0e: p = "IF FM Radio"; break;
1049         case 0x0f: p = "BTSC"; break;
1050         case 0x10: p = "high-deviation FM"; break;
1051         case 0x11: p = "very high-deviation FM"; break;
1052         case 0xfd: p = "unknown audio standard"; break;
1053         case 0xfe: p = "forced audio standard"; break;
1054         case 0xff: p = "no detected audio standard"; break;
1055         default: p = "not defined";
1056         }
1057         v4l_info(client, "Detected audio standard:   %s\n", p);
1058         v4l_info(client, "Audio muted:               %s\n",
1059                     (mute_ctl & 0x2) ? "yes" : "no");
1060         v4l_info(client, "Audio microcontroller:     %s\n",
1061                     (download_ctl & 0x10) ? "running" : "stopped");
1062
1063         switch (audio_config >> 4) {
1064         case 0x00: p = "undefined"; break;
1065         case 0x01: p = "BTSC"; break;
1066         case 0x02: p = "EIAJ"; break;
1067         case 0x03: p = "A2-M"; break;
1068         case 0x04: p = "A2-BG"; break;
1069         case 0x05: p = "A2-DK1"; break;
1070         case 0x06: p = "A2-DK2"; break;
1071         case 0x07: p = "A2-DK3"; break;
1072         case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1073         case 0x09: p = "AM-L"; break;
1074         case 0x0a: p = "NICAM-BG"; break;
1075         case 0x0b: p = "NICAM-DK"; break;
1076         case 0x0c: p = "NICAM-I"; break;
1077         case 0x0d: p = "NICAM-L"; break;
1078         case 0x0e: p = "FM radio"; break;
1079         case 0x0f: p = "automatic detection"; break;
1080         default: p = "undefined";
1081         }
1082         v4l_info(client, "Configured audio standard: %s\n", p);
1083
1084         if ((audio_config >> 4) < 0xF) {
1085                 switch (audio_config & 0xF) {
1086                 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1087                 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1088                 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1089                 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1090                 case 0x04: p = "STEREO"; break;
1091                 case 0x05: p = "DUAL1 (AB)"; break;
1092                 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1093                 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1094                 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1095                 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1096                 case 0x0a: p = "SAP"; break;
1097                 default: p = "undefined";
1098                 }
1099                 v4l_info(client, "Configured audio mode:     %s\n", p);
1100         } else {
1101                 switch (audio_config & 0xF) {
1102                 case 0x00: p = "BG"; break;
1103                 case 0x01: p = "DK1"; break;
1104                 case 0x02: p = "DK2"; break;
1105                 case 0x03: p = "DK3"; break;
1106                 case 0x04: p = "I"; break;
1107                 case 0x05: p = "L"; break;
1108                 case 0x06: p = "BTSC"; break;
1109                 case 0x07: p = "EIAJ"; break;
1110                 case 0x08: p = "A2-M"; break;
1111                 case 0x09: p = "FM Radio"; break;
1112                 case 0x0f: p = "automatic standard and mode detection"; break;
1113                 default: p = "undefined";
1114                 }
1115                 v4l_info(client, "Configured audio system:   %s\n", p);
1116         }
1117
1118         if (aud_input) {
1119                 v4l_info(client, "Specified audio input:     Tuner (In%d)\n", aud_input);
1120         } else {
1121                 v4l_info(client, "Specified audio input:     External\n");
1122         }
1123
1124         switch (pref_mode & 0xf) {
1125         case 0: p = "mono/language A"; break;
1126         case 1: p = "language B"; break;
1127         case 2: p = "language C"; break;
1128         case 3: p = "analog fallback"; break;
1129         case 4: p = "stereo"; break;
1130         case 5: p = "language AC"; break;
1131         case 6: p = "language BC"; break;
1132         case 7: p = "language AB"; break;
1133         default: p = "undefined";
1134         }
1135         v4l_info(client, "Preferred audio mode:      %s\n", p);
1136
1137         if ((audio_config & 0xf) == 0xf) {
1138                 switch ((afc0 >> 3) & 0x3) {
1139                 case 0: p = "system DK"; break;
1140                 case 1: p = "system L"; break;
1141                 case 2: p = "autodetect"; break;
1142                 default: p = "undefined";
1143                 }
1144                 v4l_info(client, "Selected 65 MHz format:    %s\n", p);
1145
1146                 switch (afc0 & 0x7) {
1147                 case 0: p = "chroma"; break;
1148                 case 1: p = "BTSC"; break;
1149                 case 2: p = "EIAJ"; break;
1150                 case 3: p = "A2-M"; break;
1151                 case 4: p = "autodetect"; break;
1152                 default: p = "undefined";
1153                 }
1154                 v4l_info(client, "Selected 45 MHz format:    %s\n", p);
1155         }
1156 }