v4l-dvb: remove legacy checks to allow support for kernels < 2.6.10
[safe/jmp/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  * CX23885 support by Steven Toth <stoth@hauppauge.com>.
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version 2
21  * of the License, or (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
31  */
32
33
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/slab.h>
37 #include <linux/videodev2.h>
38 #include <linux/i2c.h>
39 #include <linux/delay.h>
40 #include <media/v4l2-common.h>
41 #include <media/v4l2-chip-ident.h>
42 #include <media/v4l2-i2c-drv-legacy.h>
43 #include <media/cx25840.h>
44
45 #include "cx25840-core.h"
46
47 MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
48 MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
49 MODULE_LICENSE("GPL");
50
51 static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
52
53 int cx25840_debug;
54
55 module_param_named(debug,cx25840_debug, int, 0644);
56
57 MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
58
59 I2C_CLIENT_INSMOD;
60
61 /* ----------------------------------------------------------------------- */
62
63 int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
64 {
65         u8 buffer[3];
66         buffer[0] = addr >> 8;
67         buffer[1] = addr & 0xff;
68         buffer[2] = value;
69         return i2c_master_send(client, buffer, 3);
70 }
71
72 int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
73 {
74         u8 buffer[6];
75         buffer[0] = addr >> 8;
76         buffer[1] = addr & 0xff;
77         buffer[2] = value & 0xff;
78         buffer[3] = (value >> 8) & 0xff;
79         buffer[4] = (value >> 16) & 0xff;
80         buffer[5] = value >> 24;
81         return i2c_master_send(client, buffer, 6);
82 }
83
84 u8 cx25840_read(struct i2c_client * client, u16 addr)
85 {
86         u8 buffer[2];
87         buffer[0] = addr >> 8;
88         buffer[1] = addr & 0xff;
89
90         if (i2c_master_send(client, buffer, 2) < 2)
91                 return 0;
92
93         if (i2c_master_recv(client, buffer, 1) < 1)
94                 return 0;
95
96         return buffer[0];
97 }
98
99 u32 cx25840_read4(struct i2c_client * client, u16 addr)
100 {
101         u8 buffer[4];
102         buffer[0] = addr >> 8;
103         buffer[1] = addr & 0xff;
104
105         if (i2c_master_send(client, buffer, 2) < 2)
106                 return 0;
107
108         if (i2c_master_recv(client, buffer, 4) < 4)
109                 return 0;
110
111         return (buffer[3] << 24) | (buffer[2] << 16) |
112             (buffer[1] << 8) | buffer[0];
113 }
114
115 int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
116                    u8 or_value)
117 {
118         return cx25840_write(client, addr,
119                              (cx25840_read(client, addr) & and_mask) |
120                              or_value);
121 }
122
123 /* ----------------------------------------------------------------------- */
124
125 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
126                                                 enum cx25840_audio_input aud_input);
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 static void cx23885_initialize(struct i2c_client *client)
260 {
261         DEFINE_WAIT(wait);
262         struct cx25840_state *state = i2c_get_clientdata(client);
263         struct workqueue_struct *q;
264
265         /* Internal Reset */
266         cx25840_and_or(client, 0x102, ~0x01, 0x01);
267         cx25840_and_or(client, 0x102, ~0x01, 0x00);
268
269         /* Stop microcontroller */
270         cx25840_and_or(client, 0x803, ~0x10, 0x00);
271
272         /* DIF in reset? */
273         cx25840_write(client, 0x398, 0);
274
275         /* Trust the default xtal, no division */
276         /* This changes for the cx23888 products */
277         cx25840_write(client, 0x2, 0x76);
278
279         /* Bring down the regulator for AUX clk */
280         cx25840_write(client, 0x1, 0x40);
281
282         /* Sys PLL frac */
283         cx25840_write4(client, 0x11c, 0x01d1744c);
284
285         /* Sys PLL int */
286         cx25840_write4(client, 0x118, 0x00000416);
287
288         /* Disable DIF bypass */
289         cx25840_write4(client, 0x33c, 0x00000001);
290
291         /* DIF Src phase inc */
292         cx25840_write4(client, 0x340, 0x0df7df83);
293
294         /* Vid PLL frac */
295         cx25840_write4(client, 0x10c, 0x01b6db7b);
296
297         /* Vid PLL int */
298         cx25840_write4(client, 0x108, 0x00000512);
299
300         /* Luma */
301         cx25840_write4(client, 0x414, 0x00107d12);
302
303         /* Chroma */
304         cx25840_write4(client, 0x420, 0x3d008282);
305
306         /* Aux PLL frac */
307         cx25840_write4(client, 0x114, 0x017dbf48);
308
309         /* Aux PLL int */
310         cx25840_write4(client, 0x110, 0x000a030e);
311
312         /* ADC2 input select */
313         cx25840_write(client, 0x102, 0x10);
314
315         /* VIN1 & VIN5 */
316         cx25840_write(client, 0x103, 0x11);
317
318         /* Enable format auto detect */
319         cx25840_write(client, 0x400, 0);
320         /* Fast subchroma lock */
321         /* White crush, Chroma AGC & Chroma Killer enabled */
322         cx25840_write(client, 0x401, 0xe8);
323
324         /* Select AFE clock pad output source */
325         cx25840_write(client, 0x144, 0x05);
326
327         /* Do the firmware load in a work handler to prevent.
328            Otherwise the kernel is blocked waiting for the
329            bit-banging i2c interface to finish uploading the
330            firmware. */
331         INIT_WORK(&state->fw_work, cx25840_work_handler);
332         init_waitqueue_head(&state->fw_wait);
333         q = create_singlethread_workqueue("cx25840_fw");
334         prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
335         queue_work(q, &state->fw_work);
336         schedule();
337         finish_wait(&state->fw_wait, &wait);
338         destroy_workqueue(q);
339
340         cx25840_vbi_setup(client);
341
342         /* (re)set input */
343         set_input(client, state->vid_input, state->aud_input);
344
345         /* start microcontroller */
346         cx25840_and_or(client, 0x803, ~0x10, 0x10);
347 }
348
349 /* ----------------------------------------------------------------------- */
350
351 static void input_change(struct i2c_client *client)
352 {
353         struct cx25840_state *state = i2c_get_clientdata(client);
354         v4l2_std_id std = state->std;
355
356         /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
357         if (std & V4L2_STD_SECAM) {
358                 cx25840_write(client, 0x402, 0);
359         }
360         else {
361                 cx25840_write(client, 0x402, 0x04);
362                 cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
363         }
364         cx25840_and_or(client, 0x401, ~0x60, 0);
365         cx25840_and_or(client, 0x401, ~0x60, 0x60);
366         cx25840_and_or(client, 0x810, ~0x01, 1);
367
368         if (state->radio) {
369                 cx25840_write(client, 0x808, 0xf9);
370                 cx25840_write(client, 0x80b, 0x00);
371         }
372         else if (std & V4L2_STD_525_60) {
373                 /* Certain Hauppauge PVR150 models have a hardware bug
374                    that causes audio to drop out. For these models the
375                    audio standard must be set explicitly.
376                    To be precise: it affects cards with tuner models
377                    85, 99 and 112 (model numbers from tveeprom). */
378                 int hw_fix = state->pvr150_workaround;
379
380                 if (std == V4L2_STD_NTSC_M_JP) {
381                         /* Japan uses EIAJ audio standard */
382                         cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
383                 } else if (std == V4L2_STD_NTSC_M_KR) {
384                         /* South Korea uses A2 audio standard */
385                         cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
386                 } else {
387                         /* Others use the BTSC audio standard */
388                         cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
389                 }
390                 cx25840_write(client, 0x80b, 0x00);
391         } else if (std & V4L2_STD_PAL) {
392                 /* Follow tuner change procedure for PAL */
393                 cx25840_write(client, 0x808, 0xff);
394                 cx25840_write(client, 0x80b, 0x10);
395         } else if (std & V4L2_STD_SECAM) {
396                 /* Select autodetect for SECAM */
397                 cx25840_write(client, 0x808, 0xff);
398                 cx25840_write(client, 0x80b, 0x10);
399         }
400
401         cx25840_and_or(client, 0x810, ~0x01, 0);
402 }
403
404 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
405                                                 enum cx25840_audio_input aud_input)
406 {
407         struct cx25840_state *state = i2c_get_clientdata(client);
408         u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
409                            vid_input <= CX25840_COMPOSITE8);
410         u8 reg;
411
412         v4l_dbg(1, cx25840_debug, client,
413                 "decoder set video input %d, audio input %d\n",
414                 vid_input, aud_input);
415
416         if (vid_input >= CX25840_VIN1_CH1) {
417                 v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
418                         vid_input);
419                 reg = vid_input & 0xff;
420                 if ((vid_input & CX25840_SVIDEO_ON) == CX25840_SVIDEO_ON)
421                         is_composite = 0;
422                 else
423                         is_composite = 1;
424
425                 v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
426                         reg, is_composite);
427         } else
428         if (is_composite) {
429                 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
430         } else {
431                 int luma = vid_input & 0xf0;
432                 int chroma = vid_input & 0xf00;
433
434                 if ((vid_input & ~0xff0) ||
435                     luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||
436                     chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
437                         v4l_err(client, "0x%04x is not a valid video input!\n",
438                                 vid_input);
439                         return -EINVAL;
440                 }
441                 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
442                 if (chroma >= CX25840_SVIDEO_CHROMA7) {
443                         reg &= 0x3f;
444                         reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
445                 } else {
446                         reg &= 0xcf;
447                         reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
448                 }
449         }
450
451         /* The caller has previously prepared the correct routing
452          * configuration in reg (for the cx23885) so we have no
453          * need to attempt to flip bits for earlier av decoders.
454          */
455         if (!state->is_cx23885) {
456                 switch (aud_input) {
457                 case CX25840_AUDIO_SERIAL:
458                         /* do nothing, use serial audio input */
459                         break;
460                 case CX25840_AUDIO4: reg &= ~0x30; break;
461                 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
462                 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
463                 case CX25840_AUDIO7: reg &= ~0xc0; break;
464                 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
465
466                 default:
467                         v4l_err(client, "0x%04x is not a valid audio input!\n",
468                                 aud_input);
469                         return -EINVAL;
470                 }
471         }
472
473         cx25840_write(client, 0x103, reg);
474
475         /* Set INPUT_MODE to Composite (0) or S-Video (1) */
476         cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
477
478         if (!state->is_cx23885) {
479                 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
480                 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
481                 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
482                 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
483                         cx25840_and_or(client, 0x102, ~0x4, 4);
484                 else
485                         cx25840_and_or(client, 0x102, ~0x4, 0);
486         } else {
487                 if (is_composite)
488                         /* ADC2 input select channel 2 */
489                         cx25840_and_or(client, 0x102, ~0x2, 0);
490                 else
491                         /* ADC2 input select channel 3 */
492                         cx25840_and_or(client, 0x102, ~0x2, 2);
493         }
494
495         state->vid_input = vid_input;
496         state->aud_input = aud_input;
497         if (!state->is_cx25836) {
498                 cx25840_audio_set_path(client);
499                 input_change(client);
500         }
501
502         if (state->is_cx23885) {
503                 /* Audio channel 1 src : Parallel 1 */
504                 cx25840_write(client, 0x124, 0x03);
505
506                 /* Select AFE clock pad output source */
507                 cx25840_write(client, 0x144, 0x05);
508
509                 /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
510                 cx25840_write(client, 0x914, 0xa0);
511
512                 /* I2S_OUT_CTL:
513                  * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
514                  * I2S_OUT_MASTER_MODE = Master
515                  */
516                 cx25840_write(client, 0x918, 0xa0);
517                 cx25840_write(client, 0x919, 0x01);
518         }
519
520         return 0;
521 }
522
523 /* ----------------------------------------------------------------------- */
524
525 static int set_v4lstd(struct i2c_client *client)
526 {
527         struct cx25840_state *state = i2c_get_clientdata(client);
528         u8 fmt = 0;     /* zero is autodetect */
529         u8 pal_m = 0;
530
531         /* First tests should be against specific std */
532         if (state->std == V4L2_STD_NTSC_M_JP) {
533                 fmt = 0x2;
534         } else if (state->std == V4L2_STD_NTSC_443) {
535                 fmt = 0x3;
536         } else if (state->std == V4L2_STD_PAL_M) {
537                 pal_m = 1;
538                 fmt = 0x5;
539         } else if (state->std == V4L2_STD_PAL_N) {
540                 fmt = 0x6;
541         } else if (state->std == V4L2_STD_PAL_Nc) {
542                 fmt = 0x7;
543         } else if (state->std == V4L2_STD_PAL_60) {
544                 fmt = 0x8;
545         } else {
546                 /* Then, test against generic ones */
547                 if (state->std & V4L2_STD_NTSC)
548                         fmt = 0x1;
549                 else if (state->std & V4L2_STD_PAL)
550                         fmt = 0x4;
551                 else if (state->std & V4L2_STD_SECAM)
552                         fmt = 0xc;
553         }
554
555         v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
556
557         /* Follow step 9 of section 3.16 in the cx25840 datasheet.
558            Without this PAL may display a vertical ghosting effect.
559            This happens for example with the Yuan MPC622. */
560         if (fmt >= 4 && fmt < 8) {
561                 /* Set format to NTSC-M */
562                 cx25840_and_or(client, 0x400, ~0xf, 1);
563                 /* Turn off LCOMB */
564                 cx25840_and_or(client, 0x47b, ~6, 0);
565         }
566         cx25840_and_or(client, 0x400, ~0xf, fmt);
567         cx25840_and_or(client, 0x403, ~0x3, pal_m);
568         cx25840_vbi_setup(client);
569         if (!state->is_cx25836)
570                 input_change(client);
571         return 0;
572 }
573
574 /* ----------------------------------------------------------------------- */
575
576 static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
577 {
578         struct cx25840_state *state = i2c_get_clientdata(client);
579
580         switch (ctrl->id) {
581         case CX25840_CID_ENABLE_PVR150_WORKAROUND:
582                 state->pvr150_workaround = ctrl->value;
583                 set_input(client, state->vid_input, state->aud_input);
584                 break;
585
586         case V4L2_CID_BRIGHTNESS:
587                 if (ctrl->value < 0 || ctrl->value > 255) {
588                         v4l_err(client, "invalid brightness setting %d\n",
589                                     ctrl->value);
590                         return -ERANGE;
591                 }
592
593                 cx25840_write(client, 0x414, ctrl->value - 128);
594                 break;
595
596         case V4L2_CID_CONTRAST:
597                 if (ctrl->value < 0 || ctrl->value > 127) {
598                         v4l_err(client, "invalid contrast setting %d\n",
599                                     ctrl->value);
600                         return -ERANGE;
601                 }
602
603                 cx25840_write(client, 0x415, ctrl->value << 1);
604                 break;
605
606         case V4L2_CID_SATURATION:
607                 if (ctrl->value < 0 || ctrl->value > 127) {
608                         v4l_err(client, "invalid saturation setting %d\n",
609                                     ctrl->value);
610                         return -ERANGE;
611                 }
612
613                 cx25840_write(client, 0x420, ctrl->value << 1);
614                 cx25840_write(client, 0x421, ctrl->value << 1);
615                 break;
616
617         case V4L2_CID_HUE:
618                 if (ctrl->value < -127 || ctrl->value > 127) {
619                         v4l_err(client, "invalid hue setting %d\n", ctrl->value);
620                         return -ERANGE;
621                 }
622
623                 cx25840_write(client, 0x422, ctrl->value);
624                 break;
625
626         case V4L2_CID_AUDIO_VOLUME:
627         case V4L2_CID_AUDIO_BASS:
628         case V4L2_CID_AUDIO_TREBLE:
629         case V4L2_CID_AUDIO_BALANCE:
630         case V4L2_CID_AUDIO_MUTE:
631                 if (state->is_cx25836)
632                         return -EINVAL;
633                 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
634
635         default:
636                 return -EINVAL;
637         }
638
639         return 0;
640 }
641
642 static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
643 {
644         struct cx25840_state *state = i2c_get_clientdata(client);
645
646         switch (ctrl->id) {
647         case CX25840_CID_ENABLE_PVR150_WORKAROUND:
648                 ctrl->value = state->pvr150_workaround;
649                 break;
650         case V4L2_CID_BRIGHTNESS:
651                 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
652                 break;
653         case V4L2_CID_CONTRAST:
654                 ctrl->value = cx25840_read(client, 0x415) >> 1;
655                 break;
656         case V4L2_CID_SATURATION:
657                 ctrl->value = cx25840_read(client, 0x420) >> 1;
658                 break;
659         case V4L2_CID_HUE:
660                 ctrl->value = (s8)cx25840_read(client, 0x422);
661                 break;
662         case V4L2_CID_AUDIO_VOLUME:
663         case V4L2_CID_AUDIO_BASS:
664         case V4L2_CID_AUDIO_TREBLE:
665         case V4L2_CID_AUDIO_BALANCE:
666         case V4L2_CID_AUDIO_MUTE:
667                 if (state->is_cx25836)
668                         return -EINVAL;
669                 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
670         default:
671                 return -EINVAL;
672         }
673
674         return 0;
675 }
676
677 /* ----------------------------------------------------------------------- */
678
679 static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
680 {
681         switch (fmt->type) {
682         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
683                 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
684         default:
685                 return -EINVAL;
686         }
687
688         return 0;
689 }
690
691 static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
692 {
693         struct cx25840_state *state = i2c_get_clientdata(client);
694         struct v4l2_pix_format *pix;
695         int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
696         int is_50Hz = !(state->std & V4L2_STD_525_60);
697
698         switch (fmt->type) {
699         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
700                 pix = &(fmt->fmt.pix);
701
702                 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
703                 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
704
705                 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
706                 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
707
708                 Vlines = pix->height + (is_50Hz ? 4 : 7);
709
710                 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
711                     (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
712                         v4l_err(client, "%dx%d is not a valid size!\n",
713                                     pix->width, pix->height);
714                         return -ERANGE;
715                 }
716
717                 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
718                 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
719                 VSC &= 0x1fff;
720
721                 if (pix->width >= 385)
722                         filter = 0;
723                 else if (pix->width > 192)
724                         filter = 1;
725                 else if (pix->width > 96)
726                         filter = 2;
727                 else
728                         filter = 3;
729
730                 v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale  %ux%u\n",
731                             pix->width, pix->height, HSC, VSC);
732
733                 /* HSCALE=HSC */
734                 cx25840_write(client, 0x418, HSC & 0xff);
735                 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
736                 cx25840_write(client, 0x41a, HSC >> 16);
737                 /* VSCALE=VSC */
738                 cx25840_write(client, 0x41c, VSC & 0xff);
739                 cx25840_write(client, 0x41d, VSC >> 8);
740                 /* VS_INTRLACE=1 VFILT=filter */
741                 cx25840_write(client, 0x41e, 0x8 | filter);
742                 break;
743
744         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
745                 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
746
747         case V4L2_BUF_TYPE_VBI_CAPTURE:
748                 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
749
750         default:
751                 return -EINVAL;
752         }
753
754         return 0;
755 }
756
757 /* ----------------------------------------------------------------------- */
758
759 static void log_video_status(struct i2c_client *client)
760 {
761         static const char *const fmt_strs[] = {
762                 "0x0",
763                 "NTSC-M", "NTSC-J", "NTSC-4.43",
764                 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
765                 "0x9", "0xA", "0xB",
766                 "SECAM",
767                 "0xD", "0xE", "0xF"
768         };
769
770         struct cx25840_state *state = i2c_get_clientdata(client);
771         u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
772         u8 gen_stat1 = cx25840_read(client, 0x40d);
773         u8 gen_stat2 = cx25840_read(client, 0x40e);
774         int vid_input = state->vid_input;
775
776         v4l_info(client, "Video signal:              %spresent\n",
777                     (gen_stat2 & 0x20) ? "" : "not ");
778         v4l_info(client, "Detected format:           %s\n",
779                     fmt_strs[gen_stat1 & 0xf]);
780
781         v4l_info(client, "Specified standard:        %s\n",
782                     vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
783
784         if (vid_input >= CX25840_COMPOSITE1 &&
785             vid_input <= CX25840_COMPOSITE8) {
786                 v4l_info(client, "Specified video input:     Composite %d\n",
787                         vid_input - CX25840_COMPOSITE1 + 1);
788         } else {
789                 v4l_info(client, "Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
790                         (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
791         }
792
793         v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
794 }
795
796 /* ----------------------------------------------------------------------- */
797
798 static void log_audio_status(struct i2c_client *client)
799 {
800         struct cx25840_state *state = i2c_get_clientdata(client);
801         u8 download_ctl = cx25840_read(client, 0x803);
802         u8 mod_det_stat0 = cx25840_read(client, 0x804);
803         u8 mod_det_stat1 = cx25840_read(client, 0x805);
804         u8 audio_config = cx25840_read(client, 0x808);
805         u8 pref_mode = cx25840_read(client, 0x809);
806         u8 afc0 = cx25840_read(client, 0x80b);
807         u8 mute_ctl = cx25840_read(client, 0x8d3);
808         int aud_input = state->aud_input;
809         char *p;
810
811         switch (mod_det_stat0) {
812         case 0x00: p = "mono"; break;
813         case 0x01: p = "stereo"; break;
814         case 0x02: p = "dual"; break;
815         case 0x04: p = "tri"; break;
816         case 0x10: p = "mono with SAP"; break;
817         case 0x11: p = "stereo with SAP"; break;
818         case 0x12: p = "dual with SAP"; break;
819         case 0x14: p = "tri with SAP"; break;
820         case 0xfe: p = "forced mode"; break;
821         default: p = "not defined";
822         }
823         v4l_info(client, "Detected audio mode:       %s\n", p);
824
825         switch (mod_det_stat1) {
826         case 0x00: p = "not defined"; break;
827         case 0x01: p = "EIAJ"; break;
828         case 0x02: p = "A2-M"; break;
829         case 0x03: p = "A2-BG"; break;
830         case 0x04: p = "A2-DK1"; break;
831         case 0x05: p = "A2-DK2"; break;
832         case 0x06: p = "A2-DK3"; break;
833         case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
834         case 0x08: p = "AM-L"; break;
835         case 0x09: p = "NICAM-BG"; break;
836         case 0x0a: p = "NICAM-DK"; break;
837         case 0x0b: p = "NICAM-I"; break;
838         case 0x0c: p = "NICAM-L"; break;
839         case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
840         case 0x0e: p = "IF FM Radio"; break;
841         case 0x0f: p = "BTSC"; break;
842         case 0x10: p = "high-deviation FM"; break;
843         case 0x11: p = "very high-deviation FM"; break;
844         case 0xfd: p = "unknown audio standard"; break;
845         case 0xfe: p = "forced audio standard"; break;
846         case 0xff: p = "no detected audio standard"; break;
847         default: p = "not defined";
848         }
849         v4l_info(client, "Detected audio standard:   %s\n", p);
850         v4l_info(client, "Audio muted:               %s\n",
851                     (state->unmute_volume >= 0) ? "yes" : "no");
852         v4l_info(client, "Audio microcontroller:     %s\n",
853                     (download_ctl & 0x10) ?
854                                 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
855
856         switch (audio_config >> 4) {
857         case 0x00: p = "undefined"; break;
858         case 0x01: p = "BTSC"; break;
859         case 0x02: p = "EIAJ"; break;
860         case 0x03: p = "A2-M"; break;
861         case 0x04: p = "A2-BG"; break;
862         case 0x05: p = "A2-DK1"; break;
863         case 0x06: p = "A2-DK2"; break;
864         case 0x07: p = "A2-DK3"; break;
865         case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
866         case 0x09: p = "AM-L"; break;
867         case 0x0a: p = "NICAM-BG"; break;
868         case 0x0b: p = "NICAM-DK"; break;
869         case 0x0c: p = "NICAM-I"; break;
870         case 0x0d: p = "NICAM-L"; break;
871         case 0x0e: p = "FM radio"; break;
872         case 0x0f: p = "automatic detection"; break;
873         default: p = "undefined";
874         }
875         v4l_info(client, "Configured audio standard: %s\n", p);
876
877         if ((audio_config >> 4) < 0xF) {
878                 switch (audio_config & 0xF) {
879                 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
880                 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
881                 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
882                 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
883                 case 0x04: p = "STEREO"; break;
884                 case 0x05: p = "DUAL1 (AB)"; break;
885                 case 0x06: p = "DUAL2 (AC) (FM)"; break;
886                 case 0x07: p = "DUAL3 (BC) (FM)"; break;
887                 case 0x08: p = "DUAL4 (AC) (AM)"; break;
888                 case 0x09: p = "DUAL5 (BC) (AM)"; break;
889                 case 0x0a: p = "SAP"; break;
890                 default: p = "undefined";
891                 }
892                 v4l_info(client, "Configured audio mode:     %s\n", p);
893         } else {
894                 switch (audio_config & 0xF) {
895                 case 0x00: p = "BG"; break;
896                 case 0x01: p = "DK1"; break;
897                 case 0x02: p = "DK2"; break;
898                 case 0x03: p = "DK3"; break;
899                 case 0x04: p = "I"; break;
900                 case 0x05: p = "L"; break;
901                 case 0x06: p = "BTSC"; break;
902                 case 0x07: p = "EIAJ"; break;
903                 case 0x08: p = "A2-M"; break;
904                 case 0x09: p = "FM Radio"; break;
905                 case 0x0f: p = "automatic standard and mode detection"; break;
906                 default: p = "undefined";
907                 }
908                 v4l_info(client, "Configured audio system:   %s\n", p);
909         }
910
911         if (aud_input) {
912                 v4l_info(client, "Specified audio input:     Tuner (In%d)\n", aud_input);
913         } else {
914                 v4l_info(client, "Specified audio input:     External\n");
915         }
916
917         switch (pref_mode & 0xf) {
918         case 0: p = "mono/language A"; break;
919         case 1: p = "language B"; break;
920         case 2: p = "language C"; break;
921         case 3: p = "analog fallback"; break;
922         case 4: p = "stereo"; break;
923         case 5: p = "language AC"; break;
924         case 6: p = "language BC"; break;
925         case 7: p = "language AB"; break;
926         default: p = "undefined";
927         }
928         v4l_info(client, "Preferred audio mode:      %s\n", p);
929
930         if ((audio_config & 0xf) == 0xf) {
931                 switch ((afc0 >> 3) & 0x3) {
932                 case 0: p = "system DK"; break;
933                 case 1: p = "system L"; break;
934                 case 2: p = "autodetect"; break;
935                 default: p = "undefined";
936                 }
937                 v4l_info(client, "Selected 65 MHz format:    %s\n", p);
938
939                 switch (afc0 & 0x7) {
940                 case 0: p = "chroma"; break;
941                 case 1: p = "BTSC"; break;
942                 case 2: p = "EIAJ"; break;
943                 case 3: p = "A2-M"; break;
944                 case 4: p = "autodetect"; break;
945                 default: p = "undefined";
946                 }
947                 v4l_info(client, "Selected 45 MHz format:    %s\n", p);
948         }
949 }
950
951 /* ----------------------------------------------------------------------- */
952
953 static int cx25840_command(struct i2c_client *client, unsigned int cmd,
954                            void *arg)
955 {
956         struct cx25840_state *state = i2c_get_clientdata(client);
957         struct v4l2_tuner *vt = arg;
958         struct v4l2_routing *route = arg;
959
960         /* ignore these commands */
961         switch (cmd) {
962                 case TUNER_SET_TYPE_ADDR:
963                         return 0;
964         }
965
966         if (!state->is_initialized) {
967                 v4l_dbg(1, cx25840_debug, client, "cmd %08x triggered fw load\n", cmd);
968                 /* initialize on first use */
969                 state->is_initialized = 1;
970                 if (state->is_cx25836)
971                         cx25836_initialize(client);
972                 else if (state->is_cx23885)
973                         cx23885_initialize(client);
974                 else
975                         cx25840_initialize(client);
976         }
977
978         switch (cmd) {
979 #ifdef CONFIG_VIDEO_ADV_DEBUG
980         /* ioctls to allow direct access to the
981          * cx25840 registers for testing */
982         case VIDIOC_DBG_G_REGISTER:
983         case VIDIOC_DBG_S_REGISTER:
984         {
985                 struct v4l2_register *reg = arg;
986
987                 if (!v4l2_chip_match_i2c_client(client, reg->match_type, reg->match_chip))
988                         return -EINVAL;
989                 if (!capable(CAP_SYS_ADMIN))
990                         return -EPERM;
991
992                 if (cmd == VIDIOC_DBG_G_REGISTER)
993                         reg->val = cx25840_read(client, reg->reg & 0x0fff);
994                 else
995                         cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
996                 break;
997         }
998 #endif
999
1000         case VIDIOC_INT_DECODE_VBI_LINE:
1001                 return cx25840_vbi(client, cmd, arg);
1002
1003         case VIDIOC_INT_AUDIO_CLOCK_FREQ:
1004                 return cx25840_audio(client, cmd, arg);
1005
1006         case VIDIOC_STREAMON:
1007                 v4l_dbg(1, cx25840_debug, client, "enable output\n");
1008                 if (state->is_cx23885) {
1009                         u8 v = (cx25840_read(client, 0x421) | 0x0b);
1010                         cx25840_write(client, 0x421, v);
1011                 } else {
1012                         cx25840_write(client, 0x115,
1013                                 state->is_cx25836 ? 0x0c : 0x8c);
1014                         cx25840_write(client, 0x116,
1015                                 state->is_cx25836 ? 0x04 : 0x07);
1016                 }
1017                 break;
1018
1019         case VIDIOC_STREAMOFF:
1020                 v4l_dbg(1, cx25840_debug, client, "disable output\n");
1021                 if (state->is_cx23885) {
1022                         u8 v = cx25840_read(client, 0x421) & ~(0x0b);
1023                         cx25840_write(client, 0x421, v);
1024                 } else {
1025                         cx25840_write(client, 0x115, 0x00);
1026                         cx25840_write(client, 0x116, 0x00);
1027                 }
1028                 break;
1029
1030         case VIDIOC_LOG_STATUS:
1031                 log_video_status(client);
1032                 if (!state->is_cx25836)
1033                         log_audio_status(client);
1034                 break;
1035
1036         case VIDIOC_G_CTRL:
1037                 return get_v4lctrl(client, (struct v4l2_control *)arg);
1038
1039         case VIDIOC_S_CTRL:
1040                 return set_v4lctrl(client, (struct v4l2_control *)arg);
1041
1042         case VIDIOC_QUERYCTRL:
1043         {
1044                 struct v4l2_queryctrl *qc = arg;
1045
1046                 switch (qc->id) {
1047                         case V4L2_CID_BRIGHTNESS:
1048                         case V4L2_CID_CONTRAST:
1049                         case V4L2_CID_SATURATION:
1050                         case V4L2_CID_HUE:
1051                                 return v4l2_ctrl_query_fill_std(qc);
1052                         default:
1053                                 break;
1054                 }
1055                 if (state->is_cx25836)
1056                         return -EINVAL;
1057
1058                 switch (qc->id) {
1059                         case V4L2_CID_AUDIO_VOLUME:
1060                         case V4L2_CID_AUDIO_MUTE:
1061                         case V4L2_CID_AUDIO_BALANCE:
1062                         case V4L2_CID_AUDIO_BASS:
1063                         case V4L2_CID_AUDIO_TREBLE:
1064                                 return v4l2_ctrl_query_fill_std(qc);
1065                         default:
1066                                 return -EINVAL;
1067                 }
1068                 return -EINVAL;
1069         }
1070
1071         case VIDIOC_G_STD:
1072                 *(v4l2_std_id *)arg = state->std;
1073                 break;
1074
1075         case VIDIOC_S_STD:
1076                 if (state->radio == 0 && state->std == *(v4l2_std_id *)arg)
1077                         return 0;
1078                 state->radio = 0;
1079                 state->std = *(v4l2_std_id *)arg;
1080                 return set_v4lstd(client);
1081
1082         case AUDC_SET_RADIO:
1083                 state->radio = 1;
1084                 break;
1085
1086         case VIDIOC_INT_G_VIDEO_ROUTING:
1087                 route->input = state->vid_input;
1088                 route->output = 0;
1089                 break;
1090
1091         case VIDIOC_INT_S_VIDEO_ROUTING:
1092                 return set_input(client, route->input, state->aud_input);
1093
1094         case VIDIOC_INT_G_AUDIO_ROUTING:
1095                 if (state->is_cx25836)
1096                         return -EINVAL;
1097                 route->input = state->aud_input;
1098                 route->output = 0;
1099                 break;
1100
1101         case VIDIOC_INT_S_AUDIO_ROUTING:
1102                 if (state->is_cx25836)
1103                         return -EINVAL;
1104                 return set_input(client, state->vid_input, route->input);
1105
1106         case VIDIOC_S_FREQUENCY:
1107                 if (!state->is_cx25836) {
1108                         input_change(client);
1109                 }
1110                 break;
1111
1112         case VIDIOC_G_TUNER:
1113         {
1114                 u8 vpres = cx25840_read(client, 0x40e) & 0x20;
1115                 u8 mode;
1116                 int val = 0;
1117
1118                 if (state->radio)
1119                         break;
1120
1121                 vt->signal = vpres ? 0xffff : 0x0;
1122                 if (state->is_cx25836)
1123                         break;
1124
1125                 vt->capability |=
1126                     V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
1127                     V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
1128
1129                 mode = cx25840_read(client, 0x804);
1130
1131                 /* get rxsubchans and audmode */
1132                 if ((mode & 0xf) == 1)
1133                         val |= V4L2_TUNER_SUB_STEREO;
1134                 else
1135                         val |= V4L2_TUNER_SUB_MONO;
1136
1137                 if (mode == 2 || mode == 4)
1138                         val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1139
1140                 if (mode & 0x10)
1141                         val |= V4L2_TUNER_SUB_SAP;
1142
1143                 vt->rxsubchans = val;
1144                 vt->audmode = state->audmode;
1145                 break;
1146         }
1147
1148         case VIDIOC_S_TUNER:
1149                 if (state->radio || state->is_cx25836)
1150                         break;
1151
1152                 switch (vt->audmode) {
1153                 case V4L2_TUNER_MODE_MONO:
1154                         /* mono      -> mono
1155                            stereo    -> mono
1156                            bilingual -> lang1 */
1157                         cx25840_and_or(client, 0x809, ~0xf, 0x00);
1158                         break;
1159                 case V4L2_TUNER_MODE_STEREO:
1160                 case V4L2_TUNER_MODE_LANG1:
1161                         /* mono      -> mono
1162                            stereo    -> stereo
1163                            bilingual -> lang1 */
1164                         cx25840_and_or(client, 0x809, ~0xf, 0x04);
1165                         break;
1166                 case V4L2_TUNER_MODE_LANG1_LANG2:
1167                         /* mono      -> mono
1168                            stereo    -> stereo
1169                            bilingual -> lang1/lang2 */
1170                         cx25840_and_or(client, 0x809, ~0xf, 0x07);
1171                         break;
1172                 case V4L2_TUNER_MODE_LANG2:
1173                         /* mono      -> mono
1174                            stereo    -> stereo
1175                            bilingual -> lang2 */
1176                         cx25840_and_or(client, 0x809, ~0xf, 0x01);
1177                         break;
1178                 default:
1179                         return -EINVAL;
1180                 }
1181                 state->audmode = vt->audmode;
1182                 break;
1183
1184         case VIDIOC_G_FMT:
1185                 return get_v4lfmt(client, (struct v4l2_format *)arg);
1186
1187         case VIDIOC_S_FMT:
1188                 return set_v4lfmt(client, (struct v4l2_format *)arg);
1189
1190         case VIDIOC_INT_RESET:
1191                 if (state->is_cx25836)
1192                         cx25836_initialize(client);
1193                 else if (state->is_cx23885)
1194                         cx23885_initialize(client);
1195                 else
1196                         cx25840_initialize(client);
1197                 break;
1198
1199         case VIDIOC_G_CHIP_IDENT:
1200                 return v4l2_chip_ident_i2c_client(client, arg, state->id, state->rev);
1201
1202         default:
1203                 return -EINVAL;
1204         }
1205
1206         return 0;
1207 }
1208
1209 /* ----------------------------------------------------------------------- */
1210
1211 static int cx25840_probe(struct i2c_client *client,
1212                          const struct i2c_device_id *did)
1213 {
1214         struct cx25840_state *state;
1215         u32 id;
1216         u16 device_id;
1217
1218         /* Check if the adapter supports the needed features */
1219         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1220                 return -EIO;
1221
1222         v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
1223
1224         device_id = cx25840_read(client, 0x101) << 8;
1225         device_id |= cx25840_read(client, 0x100);
1226         v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
1227
1228         /* The high byte of the device ID should be
1229          * 0x83 for the cx2583x and 0x84 for the cx2584x */
1230         if ((device_id & 0xff00) == 0x8300) {
1231                 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
1232         }
1233         else if ((device_id & 0xff00) == 0x8400) {
1234                 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
1235         } else if (device_id == 0x0000) {
1236                 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
1237         } else if (device_id == 0x1313) {
1238                 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
1239         }
1240         else {
1241                 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
1242                 return -ENODEV;
1243         }
1244
1245         state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
1246         if (state == NULL) {
1247                 return -ENOMEM;
1248         }
1249
1250         /* Note: revision '(device_id & 0x0f) == 2' was never built. The
1251            marking skips from 0x1 == 22 to 0x3 == 23. */
1252         v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
1253                     (device_id & 0xfff0) >> 4,
1254                     (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
1255                     client->addr << 1, client->adapter->name);
1256
1257         i2c_set_clientdata(client, state);
1258         state->c = client;
1259         state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
1260         state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
1261         state->vid_input = CX25840_COMPOSITE7;
1262         state->aud_input = CX25840_AUDIO8;
1263         state->audclk_freq = 48000;
1264         state->pvr150_workaround = 0;
1265         state->audmode = V4L2_TUNER_MODE_LANG1;
1266         state->unmute_volume = -1;
1267         state->vbi_line_offset = 8;
1268         state->id = id;
1269         state->rev = device_id;
1270
1271         if (state->is_cx23885) {
1272                 /* Drive GPIO2 direction and values */
1273                 cx25840_write(client, 0x160, 0x1d);
1274                 cx25840_write(client, 0x164, 0x00);
1275         }
1276
1277         return 0;
1278 }
1279
1280 static int cx25840_remove(struct i2c_client *client)
1281 {
1282         kfree(i2c_get_clientdata(client));
1283         return 0;
1284 }
1285
1286 static const struct i2c_device_id cx25840_id[] = {
1287         { "cx25840", 0 },
1288         { }
1289 };
1290 MODULE_DEVICE_TABLE(i2c, cx25840_id);
1291
1292 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1293         .name = "cx25840",
1294         .driverid = I2C_DRIVERID_CX25840,
1295         .command = cx25840_command,
1296         .probe = cx25840_probe,
1297         .remove = cx25840_remove,
1298         .id_table = cx25840_id,
1299 };