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