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