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