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