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