]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/cx25840/cx25840-core.c
V4L/DVB (11298): cx25840: remove legacy code for old-style i2c API
[linux-2.6-omap-h63xx.git] / drivers / media / video / cx25840 / cx25840-core.c
1 /* cx25840 - Conexant CX25840 audio/video decoder driver
2  *
3  * Copyright (C) 2004 Ulf Eklund
4  *
5  * Based on the saa7115 driver and on the first verison of Chris Kennedy's
6  * cx25840 driver.
7  *
8  * Changes by Tyler Trafford <tatrafford@comcast.net>
9  *    - cleanup/rewrite for V4L2 API (2005)
10  *
11  * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
12  *
13  * 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@linuxtv.org>.
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.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 int cx25840_debug;
52
53 module_param_named(debug,cx25840_debug, int, 0644);
54
55 MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
56
57
58 /* ----------------------------------------------------------------------- */
59
60 int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
61 {
62         u8 buffer[3];
63         buffer[0] = addr >> 8;
64         buffer[1] = addr & 0xff;
65         buffer[2] = value;
66         return i2c_master_send(client, buffer, 3);
67 }
68
69 int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
70 {
71         u8 buffer[6];
72         buffer[0] = addr >> 8;
73         buffer[1] = addr & 0xff;
74         buffer[2] = value & 0xff;
75         buffer[3] = (value >> 8) & 0xff;
76         buffer[4] = (value >> 16) & 0xff;
77         buffer[5] = value >> 24;
78         return i2c_master_send(client, buffer, 6);
79 }
80
81 u8 cx25840_read(struct i2c_client * client, u16 addr)
82 {
83         u8 buffer[2];
84         buffer[0] = addr >> 8;
85         buffer[1] = addr & 0xff;
86
87         if (i2c_master_send(client, buffer, 2) < 2)
88                 return 0;
89
90         if (i2c_master_recv(client, buffer, 1) < 1)
91                 return 0;
92
93         return buffer[0];
94 }
95
96 u32 cx25840_read4(struct i2c_client * client, u16 addr)
97 {
98         u8 buffer[4];
99         buffer[0] = addr >> 8;
100         buffer[1] = addr & 0xff;
101
102         if (i2c_master_send(client, buffer, 2) < 2)
103                 return 0;
104
105         if (i2c_master_recv(client, buffer, 4) < 4)
106                 return 0;
107
108         return (buffer[3] << 24) | (buffer[2] << 16) |
109             (buffer[1] << 8) | buffer[0];
110 }
111
112 int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
113                    u8 or_value)
114 {
115         return cx25840_write(client, addr,
116                              (cx25840_read(client, addr) & and_mask) |
117                              or_value);
118 }
119
120 /* ----------------------------------------------------------------------- */
121
122 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
123                                                 enum cx25840_audio_input aud_input);
124
125 /* ----------------------------------------------------------------------- */
126
127 static void init_dll1(struct i2c_client *client)
128 {
129         /* This is the Hauppauge sequence used to
130          * initialize the Delay Lock Loop 1 (ADC DLL). */
131         cx25840_write(client, 0x159, 0x23);
132         cx25840_write(client, 0x15a, 0x87);
133         cx25840_write(client, 0x15b, 0x06);
134         udelay(10);
135         cx25840_write(client, 0x159, 0xe1);
136         udelay(10);
137         cx25840_write(client, 0x15a, 0x86);
138         cx25840_write(client, 0x159, 0xe0);
139         cx25840_write(client, 0x159, 0xe1);
140         cx25840_write(client, 0x15b, 0x10);
141 }
142
143 static void init_dll2(struct i2c_client *client)
144 {
145         /* This is the Hauppauge sequence used to
146          * initialize the Delay Lock Loop 2 (ADC DLL). */
147         cx25840_write(client, 0x15d, 0xe3);
148         cx25840_write(client, 0x15e, 0x86);
149         cx25840_write(client, 0x15f, 0x06);
150         udelay(10);
151         cx25840_write(client, 0x15d, 0xe1);
152         cx25840_write(client, 0x15d, 0xe0);
153         cx25840_write(client, 0x15d, 0xe1);
154 }
155
156 static void cx25836_initialize(struct i2c_client *client)
157 {
158         /* reset configuration is described on page 3-77 of the CX25836 datasheet */
159         /* 2. */
160         cx25840_and_or(client, 0x000, ~0x01, 0x01);
161         cx25840_and_or(client, 0x000, ~0x01, 0x00);
162         /* 3a. */
163         cx25840_and_or(client, 0x15a, ~0x70, 0x00);
164         /* 3b. */
165         cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
166         /* 3c. */
167         cx25840_and_or(client, 0x159, ~0x02, 0x02);
168         /* 3d. */
169         udelay(10);
170         /* 3e. */
171         cx25840_and_or(client, 0x159, ~0x02, 0x00);
172         /* 3f. */
173         cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
174         /* 3g. */
175         cx25840_and_or(client, 0x159, ~0x01, 0x00);
176         cx25840_and_or(client, 0x159, ~0x01, 0x01);
177         /* 3h. */
178         cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
179 }
180
181 static void cx25840_work_handler(struct work_struct *work)
182 {
183         struct cx25840_state *state = container_of(work, struct cx25840_state, fw_work);
184         cx25840_loadfw(state->c);
185         wake_up(&state->fw_wait);
186 }
187
188 static void cx25840_initialize(struct i2c_client *client)
189 {
190         DEFINE_WAIT(wait);
191         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
192         struct workqueue_struct *q;
193
194         /* datasheet startup in numbered steps, refer to page 3-77 */
195         /* 2. */
196         cx25840_and_or(client, 0x803, ~0x10, 0x00);
197         /* The default of this register should be 4, but I get 0 instead.
198          * Set this register to 4 manually. */
199         cx25840_write(client, 0x000, 0x04);
200         /* 3. */
201         init_dll1(client);
202         init_dll2(client);
203         cx25840_write(client, 0x136, 0x0a);
204         /* 4. */
205         cx25840_write(client, 0x13c, 0x01);
206         cx25840_write(client, 0x13c, 0x00);
207         /* 5. */
208         /* Do the firmware load in a work handler to prevent.
209            Otherwise the kernel is blocked waiting for the
210            bit-banging i2c interface to finish uploading the
211            firmware. */
212         INIT_WORK(&state->fw_work, cx25840_work_handler);
213         init_waitqueue_head(&state->fw_wait);
214         q = create_singlethread_workqueue("cx25840_fw");
215         prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
216         queue_work(q, &state->fw_work);
217         schedule();
218         finish_wait(&state->fw_wait, &wait);
219         destroy_workqueue(q);
220
221         /* 6. */
222         cx25840_write(client, 0x115, 0x8c);
223         cx25840_write(client, 0x116, 0x07);
224         cx25840_write(client, 0x118, 0x02);
225         /* 7. */
226         cx25840_write(client, 0x4a5, 0x80);
227         cx25840_write(client, 0x4a5, 0x00);
228         cx25840_write(client, 0x402, 0x00);
229         /* 8. */
230         cx25840_and_or(client, 0x401, ~0x18, 0);
231         cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
232         /* steps 8c and 8d are done in change_input() */
233         /* 10. */
234         cx25840_write(client, 0x8d3, 0x1f);
235         cx25840_write(client, 0x8e3, 0x03);
236
237         cx25840_std_setup(client);
238
239         /* trial and error says these are needed to get audio */
240         cx25840_write(client, 0x914, 0xa0);
241         cx25840_write(client, 0x918, 0xa0);
242         cx25840_write(client, 0x919, 0x01);
243
244         /* stereo prefered */
245         cx25840_write(client, 0x809, 0x04);
246         /* AC97 shift */
247         cx25840_write(client, 0x8cf, 0x0f);
248
249         /* (re)set input */
250         set_input(client, state->vid_input, state->aud_input);
251
252         /* start microcontroller */
253         cx25840_and_or(client, 0x803, ~0x10, 0x10);
254 }
255
256 static void cx23885_initialize(struct i2c_client *client)
257 {
258         DEFINE_WAIT(wait);
259         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
260         struct workqueue_struct *q;
261
262         /* Internal Reset */
263         cx25840_and_or(client, 0x102, ~0x01, 0x01);
264         cx25840_and_or(client, 0x102, ~0x01, 0x00);
265
266         /* Stop microcontroller */
267         cx25840_and_or(client, 0x803, ~0x10, 0x00);
268
269         /* DIF in reset? */
270         cx25840_write(client, 0x398, 0);
271
272         /* Trust the default xtal, no division */
273         /* This changes for the cx23888 products */
274         cx25840_write(client, 0x2, 0x76);
275
276         /* Bring down the regulator for AUX clk */
277         cx25840_write(client, 0x1, 0x40);
278
279         /* Sys PLL frac */
280         cx25840_write4(client, 0x11c, 0x01d1744c);
281
282         /* Sys PLL int */
283         cx25840_write4(client, 0x118, 0x00000416);
284
285         /* Disable DIF bypass */
286         cx25840_write4(client, 0x33c, 0x00000001);
287
288         /* DIF Src phase inc */
289         cx25840_write4(client, 0x340, 0x0df7df83);
290
291         /* Vid PLL frac */
292         cx25840_write4(client, 0x10c, 0x01b6db7b);
293
294         /* Vid PLL int */
295         cx25840_write4(client, 0x108, 0x00000512);
296
297         /* Luma */
298         cx25840_write4(client, 0x414, 0x00107d12);
299
300         /* Chroma */
301         cx25840_write4(client, 0x420, 0x3d008282);
302
303         /* Aux PLL frac */
304         cx25840_write4(client, 0x114, 0x017dbf48);
305
306         /* Aux PLL int */
307         cx25840_write4(client, 0x110, 0x000a030e);
308
309         /* ADC2 input select */
310         cx25840_write(client, 0x102, 0x10);
311
312         /* VIN1 & VIN5 */
313         cx25840_write(client, 0x103, 0x11);
314
315         /* Enable format auto detect */
316         cx25840_write(client, 0x400, 0);
317         /* Fast subchroma lock */
318         /* White crush, Chroma AGC & Chroma Killer enabled */
319         cx25840_write(client, 0x401, 0xe8);
320
321         /* Select AFE clock pad output source */
322         cx25840_write(client, 0x144, 0x05);
323
324         /* Do the firmware load in a work handler to prevent.
325            Otherwise the kernel is blocked waiting for the
326            bit-banging i2c interface to finish uploading the
327            firmware. */
328         INIT_WORK(&state->fw_work, cx25840_work_handler);
329         init_waitqueue_head(&state->fw_wait);
330         q = create_singlethread_workqueue("cx25840_fw");
331         prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
332         queue_work(q, &state->fw_work);
333         schedule();
334         finish_wait(&state->fw_wait, &wait);
335         destroy_workqueue(q);
336
337         cx25840_std_setup(client);
338
339         /* (re)set input */
340         set_input(client, state->vid_input, state->aud_input);
341
342         /* start microcontroller */
343         cx25840_and_or(client, 0x803, ~0x10, 0x10);
344 }
345
346 /* ----------------------------------------------------------------------- */
347
348 void cx25840_std_setup(struct i2c_client *client)
349 {
350         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
351         v4l2_std_id std = state->std;
352         int hblank, hactive, burst, vblank, vactive, sc;
353         int vblank656, src_decimation;
354         int luma_lpf, uv_lpf, comb;
355         u32 pll_int, pll_frac, pll_post;
356
357         /* datasheet startup, step 8d */
358         if (std & ~V4L2_STD_NTSC)
359                 cx25840_write(client, 0x49f, 0x11);
360         else
361                 cx25840_write(client, 0x49f, 0x14);
362
363         if (std & V4L2_STD_625_50) {
364                 hblank = 132;
365                 hactive = 720;
366                 burst = 93;
367                 vblank = 36;
368                 vactive = 580;
369                 vblank656 = 40;
370                 src_decimation = 0x21f;
371                 luma_lpf = 2;
372
373                 if (std & V4L2_STD_SECAM) {
374                         uv_lpf = 0;
375                         comb = 0;
376                         sc = 0x0a425f;
377                 } else if (std == V4L2_STD_PAL_Nc) {
378                         uv_lpf = 1;
379                         comb = 0x20;
380                         sc = 556453;
381                 } else {
382                         uv_lpf = 1;
383                         comb = 0x20;
384                         sc = 688739;
385                 }
386         } else {
387                 hactive = 720;
388                 hblank = 122;
389                 vactive = 487;
390                 luma_lpf = 1;
391                 uv_lpf = 1;
392
393                 src_decimation = 0x21f;
394                 if (std == V4L2_STD_PAL_60) {
395                         vblank = 26;
396                         vblank656 = 26;
397                         burst = 0x5b;
398                         luma_lpf = 2;
399                         comb = 0x20;
400                         sc = 688739;
401                 } else if (std == V4L2_STD_PAL_M) {
402                         vblank = 20;
403                         vblank656 = 24;
404                         burst = 0x61;
405                         comb = 0x20;
406                         sc = 555452;
407                 } else {
408                         vblank = 26;
409                         vblank656 = 26;
410                         burst = 0x5b;
411                         comb = 0x66;
412                         sc = 556063;
413                 }
414         }
415
416         /* DEBUG: Displays configured PLL frequency */
417         pll_int = cx25840_read(client, 0x108);
418         pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
419         pll_post = cx25840_read(client, 0x109);
420         v4l_dbg(1, cx25840_debug, client,
421                                 "PLL regs = int: %u, frac: %u, post: %u\n",
422                                 pll_int, pll_frac, pll_post);
423
424         if (pll_post) {
425                 int fin, fsc;
426                 int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
427
428                 pll /= pll_post;
429                 v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",
430                                 pll / 1000000, pll % 1000000);
431                 v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",
432                                 pll / 8000000, (pll / 8) % 1000000);
433
434                 fin = ((u64)src_decimation * pll) >> 12;
435                 v4l_dbg(1, cx25840_debug, client,
436                                 "ADC Sampling freq = %d.%06d MHz\n",
437                                 fin / 1000000, fin % 1000000);
438
439                 fsc = (((u64)sc) * pll) >> 24L;
440                 v4l_dbg(1, cx25840_debug, client,
441                                 "Chroma sub-carrier freq = %d.%06d MHz\n",
442                                 fsc / 1000000, fsc % 1000000);
443
444                 v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
445                         "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
446                         "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "
447                         "sc 0x%06x\n",
448                         hblank, hactive, vblank, vactive, vblank656,
449                         src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
450         }
451
452         /* Sets horizontal blanking delay and active lines */
453         cx25840_write(client, 0x470, hblank);
454         cx25840_write(client, 0x471,
455                         0xff & (((hblank >> 8) & 0x3) | (hactive << 4)));
456         cx25840_write(client, 0x472, hactive >> 4);
457
458         /* Sets burst gate delay */
459         cx25840_write(client, 0x473, burst);
460
461         /* Sets vertical blanking delay and active duration */
462         cx25840_write(client, 0x474, vblank);
463         cx25840_write(client, 0x475,
464                         0xff & (((vblank >> 8) & 0x3) | (vactive << 4)));
465         cx25840_write(client, 0x476, vactive >> 4);
466         cx25840_write(client, 0x477, vblank656);
467
468         /* Sets src decimation rate */
469         cx25840_write(client, 0x478, 0xff & src_decimation);
470         cx25840_write(client, 0x479, 0xff & (src_decimation >> 8));
471
472         /* Sets Luma and UV Low pass filters */
473         cx25840_write(client, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
474
475         /* Enables comb filters */
476         cx25840_write(client, 0x47b, comb);
477
478         /* Sets SC Step*/
479         cx25840_write(client, 0x47c, sc);
480         cx25840_write(client, 0x47d, 0xff & sc >> 8);
481         cx25840_write(client, 0x47e, 0xff & sc >> 16);
482
483         /* Sets VBI parameters */
484         if (std & V4L2_STD_625_50) {
485                 cx25840_write(client, 0x47f, 0x01);
486                 state->vbi_line_offset = 5;
487         } else {
488                 cx25840_write(client, 0x47f, 0x00);
489                 state->vbi_line_offset = 8;
490         }
491 }
492
493 /* ----------------------------------------------------------------------- */
494
495 static void input_change(struct i2c_client *client)
496 {
497         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
498         v4l2_std_id std = state->std;
499
500         /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
501         if (std & V4L2_STD_SECAM) {
502                 cx25840_write(client, 0x402, 0);
503         }
504         else {
505                 cx25840_write(client, 0x402, 0x04);
506                 cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
507         }
508         cx25840_and_or(client, 0x401, ~0x60, 0);
509         cx25840_and_or(client, 0x401, ~0x60, 0x60);
510         cx25840_and_or(client, 0x810, ~0x01, 1);
511
512         if (state->radio) {
513                 cx25840_write(client, 0x808, 0xf9);
514                 cx25840_write(client, 0x80b, 0x00);
515         }
516         else if (std & V4L2_STD_525_60) {
517                 /* Certain Hauppauge PVR150 models have a hardware bug
518                    that causes audio to drop out. For these models the
519                    audio standard must be set explicitly.
520                    To be precise: it affects cards with tuner models
521                    85, 99 and 112 (model numbers from tveeprom). */
522                 int hw_fix = state->pvr150_workaround;
523
524                 if (std == V4L2_STD_NTSC_M_JP) {
525                         /* Japan uses EIAJ audio standard */
526                         cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
527                 } else if (std == V4L2_STD_NTSC_M_KR) {
528                         /* South Korea uses A2 audio standard */
529                         cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
530                 } else {
531                         /* Others use the BTSC audio standard */
532                         cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
533                 }
534                 cx25840_write(client, 0x80b, 0x00);
535         } else if (std & V4L2_STD_PAL) {
536                 /* Follow tuner change procedure for PAL */
537                 cx25840_write(client, 0x808, 0xff);
538                 cx25840_write(client, 0x80b, 0x10);
539         } else if (std & V4L2_STD_SECAM) {
540                 /* Select autodetect for SECAM */
541                 cx25840_write(client, 0x808, 0xff);
542                 cx25840_write(client, 0x80b, 0x10);
543         }
544
545         cx25840_and_or(client, 0x810, ~0x01, 0);
546 }
547
548 static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
549                                                 enum cx25840_audio_input aud_input)
550 {
551         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
552         u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
553                            vid_input <= CX25840_COMPOSITE8);
554         u8 reg;
555
556         v4l_dbg(1, cx25840_debug, client,
557                 "decoder set video input %d, audio input %d\n",
558                 vid_input, aud_input);
559
560         if (vid_input >= CX25840_VIN1_CH1) {
561                 v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
562                         vid_input);
563                 reg = vid_input & 0xff;
564                 if ((vid_input & CX25840_SVIDEO_ON) == CX25840_SVIDEO_ON)
565                         is_composite = 0;
566                 else
567                         is_composite = 1;
568
569                 v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
570                         reg, is_composite);
571         } else
572         if (is_composite) {
573                 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
574         } else {
575                 int luma = vid_input & 0xf0;
576                 int chroma = vid_input & 0xf00;
577
578                 if ((vid_input & ~0xff0) ||
579                     luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||
580                     chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
581                         v4l_err(client, "0x%04x is not a valid video input!\n",
582                                 vid_input);
583                         return -EINVAL;
584                 }
585                 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
586                 if (chroma >= CX25840_SVIDEO_CHROMA7) {
587                         reg &= 0x3f;
588                         reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
589                 } else {
590                         reg &= 0xcf;
591                         reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
592                 }
593         }
594
595         /* The caller has previously prepared the correct routing
596          * configuration in reg (for the cx23885) so we have no
597          * need to attempt to flip bits for earlier av decoders.
598          */
599         if (!state->is_cx23885) {
600                 switch (aud_input) {
601                 case CX25840_AUDIO_SERIAL:
602                         /* do nothing, use serial audio input */
603                         break;
604                 case CX25840_AUDIO4: reg &= ~0x30; break;
605                 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
606                 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
607                 case CX25840_AUDIO7: reg &= ~0xc0; break;
608                 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
609
610                 default:
611                         v4l_err(client, "0x%04x is not a valid audio input!\n",
612                                 aud_input);
613                         return -EINVAL;
614                 }
615         }
616
617         cx25840_write(client, 0x103, reg);
618
619         /* Set INPUT_MODE to Composite (0) or S-Video (1) */
620         cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
621
622         if (!state->is_cx23885) {
623                 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
624                 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
625                 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
626                 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
627                         cx25840_and_or(client, 0x102, ~0x4, 4);
628                 else
629                         cx25840_and_or(client, 0x102, ~0x4, 0);
630         } else {
631                 if (is_composite)
632                         /* ADC2 input select channel 2 */
633                         cx25840_and_or(client, 0x102, ~0x2, 0);
634                 else
635                         /* ADC2 input select channel 3 */
636                         cx25840_and_or(client, 0x102, ~0x2, 2);
637         }
638
639         state->vid_input = vid_input;
640         state->aud_input = aud_input;
641         if (!state->is_cx25836) {
642                 cx25840_audio_set_path(client);
643                 input_change(client);
644         }
645
646         if (state->is_cx23885) {
647                 /* Audio channel 1 src : Parallel 1 */
648                 cx25840_write(client, 0x124, 0x03);
649
650                 /* Select AFE clock pad output source */
651                 cx25840_write(client, 0x144, 0x05);
652
653                 /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
654                 cx25840_write(client, 0x914, 0xa0);
655
656                 /* I2S_OUT_CTL:
657                  * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
658                  * I2S_OUT_MASTER_MODE = Master
659                  */
660                 cx25840_write(client, 0x918, 0xa0);
661                 cx25840_write(client, 0x919, 0x01);
662         }
663
664         return 0;
665 }
666
667 /* ----------------------------------------------------------------------- */
668
669 static int set_v4lstd(struct i2c_client *client)
670 {
671         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
672         u8 fmt = 0;     /* zero is autodetect */
673         u8 pal_m = 0;
674
675         /* First tests should be against specific std */
676         if (state->std == V4L2_STD_NTSC_M_JP) {
677                 fmt = 0x2;
678         } else if (state->std == V4L2_STD_NTSC_443) {
679                 fmt = 0x3;
680         } else if (state->std == V4L2_STD_PAL_M) {
681                 pal_m = 1;
682                 fmt = 0x5;
683         } else if (state->std == V4L2_STD_PAL_N) {
684                 fmt = 0x6;
685         } else if (state->std == V4L2_STD_PAL_Nc) {
686                 fmt = 0x7;
687         } else if (state->std == V4L2_STD_PAL_60) {
688                 fmt = 0x8;
689         } else {
690                 /* Then, test against generic ones */
691                 if (state->std & V4L2_STD_NTSC)
692                         fmt = 0x1;
693                 else if (state->std & V4L2_STD_PAL)
694                         fmt = 0x4;
695                 else if (state->std & V4L2_STD_SECAM)
696                         fmt = 0xc;
697         }
698
699         v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
700
701         /* Follow step 9 of section 3.16 in the cx25840 datasheet.
702            Without this PAL may display a vertical ghosting effect.
703            This happens for example with the Yuan MPC622. */
704         if (fmt >= 4 && fmt < 8) {
705                 /* Set format to NTSC-M */
706                 cx25840_and_or(client, 0x400, ~0xf, 1);
707                 /* Turn off LCOMB */
708                 cx25840_and_or(client, 0x47b, ~6, 0);
709         }
710         cx25840_and_or(client, 0x400, ~0xf, fmt);
711         cx25840_and_or(client, 0x403, ~0x3, pal_m);
712         cx25840_std_setup(client);
713         if (!state->is_cx25836)
714                 input_change(client);
715         return 0;
716 }
717
718 /* ----------------------------------------------------------------------- */
719
720 static int cx25840_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
721 {
722         struct cx25840_state *state = to_state(sd);
723         struct i2c_client *client = v4l2_get_subdevdata(sd);
724
725         switch (ctrl->id) {
726         case CX25840_CID_ENABLE_PVR150_WORKAROUND:
727                 state->pvr150_workaround = ctrl->value;
728                 set_input(client, state->vid_input, state->aud_input);
729                 break;
730
731         case V4L2_CID_BRIGHTNESS:
732                 if (ctrl->value < 0 || ctrl->value > 255) {
733                         v4l_err(client, "invalid brightness setting %d\n",
734                                     ctrl->value);
735                         return -ERANGE;
736                 }
737
738                 cx25840_write(client, 0x414, ctrl->value - 128);
739                 break;
740
741         case V4L2_CID_CONTRAST:
742                 if (ctrl->value < 0 || ctrl->value > 127) {
743                         v4l_err(client, "invalid contrast setting %d\n",
744                                     ctrl->value);
745                         return -ERANGE;
746                 }
747
748                 cx25840_write(client, 0x415, ctrl->value << 1);
749                 break;
750
751         case V4L2_CID_SATURATION:
752                 if (ctrl->value < 0 || ctrl->value > 127) {
753                         v4l_err(client, "invalid saturation setting %d\n",
754                                     ctrl->value);
755                         return -ERANGE;
756                 }
757
758                 cx25840_write(client, 0x420, ctrl->value << 1);
759                 cx25840_write(client, 0x421, ctrl->value << 1);
760                 break;
761
762         case V4L2_CID_HUE:
763                 if (ctrl->value < -128 || ctrl->value > 127) {
764                         v4l_err(client, "invalid hue setting %d\n", ctrl->value);
765                         return -ERANGE;
766                 }
767
768                 cx25840_write(client, 0x422, ctrl->value);
769                 break;
770
771         case V4L2_CID_AUDIO_VOLUME:
772         case V4L2_CID_AUDIO_BASS:
773         case V4L2_CID_AUDIO_TREBLE:
774         case V4L2_CID_AUDIO_BALANCE:
775         case V4L2_CID_AUDIO_MUTE:
776                 if (state->is_cx25836)
777                         return -EINVAL;
778                 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
779
780         default:
781                 return -EINVAL;
782         }
783
784         return 0;
785 }
786
787 static int cx25840_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
788 {
789         struct cx25840_state *state = to_state(sd);
790         struct i2c_client *client = v4l2_get_subdevdata(sd);
791
792         switch (ctrl->id) {
793         case CX25840_CID_ENABLE_PVR150_WORKAROUND:
794                 ctrl->value = state->pvr150_workaround;
795                 break;
796         case V4L2_CID_BRIGHTNESS:
797                 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
798                 break;
799         case V4L2_CID_CONTRAST:
800                 ctrl->value = cx25840_read(client, 0x415) >> 1;
801                 break;
802         case V4L2_CID_SATURATION:
803                 ctrl->value = cx25840_read(client, 0x420) >> 1;
804                 break;
805         case V4L2_CID_HUE:
806                 ctrl->value = (s8)cx25840_read(client, 0x422);
807                 break;
808         case V4L2_CID_AUDIO_VOLUME:
809         case V4L2_CID_AUDIO_BASS:
810         case V4L2_CID_AUDIO_TREBLE:
811         case V4L2_CID_AUDIO_BALANCE:
812         case V4L2_CID_AUDIO_MUTE:
813                 if (state->is_cx25836)
814                         return -EINVAL;
815                 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
816         default:
817                 return -EINVAL;
818         }
819
820         return 0;
821 }
822
823 /* ----------------------------------------------------------------------- */
824
825 static int cx25840_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
826 {
827         struct i2c_client *client = v4l2_get_subdevdata(sd);
828
829         switch (fmt->type) {
830         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
831                 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
832         default:
833                 return -EINVAL;
834         }
835         return 0;
836 }
837
838 static int cx25840_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
839 {
840         struct cx25840_state *state = to_state(sd);
841         struct i2c_client *client = v4l2_get_subdevdata(sd);
842         struct v4l2_pix_format *pix;
843         int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
844         int is_50Hz = !(state->std & V4L2_STD_525_60);
845
846         switch (fmt->type) {
847         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
848                 pix = &(fmt->fmt.pix);
849
850                 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
851                 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
852
853                 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
854                 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
855
856                 Vlines = pix->height + (is_50Hz ? 4 : 7);
857
858                 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
859                     (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
860                         v4l_err(client, "%dx%d is not a valid size!\n",
861                                     pix->width, pix->height);
862                         return -ERANGE;
863                 }
864
865                 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
866                 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
867                 VSC &= 0x1fff;
868
869                 if (pix->width >= 385)
870                         filter = 0;
871                 else if (pix->width > 192)
872                         filter = 1;
873                 else if (pix->width > 96)
874                         filter = 2;
875                 else
876                         filter = 3;
877
878                 v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale  %ux%u\n",
879                             pix->width, pix->height, HSC, VSC);
880
881                 /* HSCALE=HSC */
882                 cx25840_write(client, 0x418, HSC & 0xff);
883                 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
884                 cx25840_write(client, 0x41a, HSC >> 16);
885                 /* VSCALE=VSC */
886                 cx25840_write(client, 0x41c, VSC & 0xff);
887                 cx25840_write(client, 0x41d, VSC >> 8);
888                 /* VS_INTRLACE=1 VFILT=filter */
889                 cx25840_write(client, 0x41e, 0x8 | filter);
890                 break;
891
892         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
893                 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
894
895         case V4L2_BUF_TYPE_VBI_CAPTURE:
896                 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
897
898         default:
899                 return -EINVAL;
900         }
901
902         return 0;
903 }
904
905 /* ----------------------------------------------------------------------- */
906
907 static void log_video_status(struct i2c_client *client)
908 {
909         static const char *const fmt_strs[] = {
910                 "0x0",
911                 "NTSC-M", "NTSC-J", "NTSC-4.43",
912                 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
913                 "0x9", "0xA", "0xB",
914                 "SECAM",
915                 "0xD", "0xE", "0xF"
916         };
917
918         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
919         u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
920         u8 gen_stat1 = cx25840_read(client, 0x40d);
921         u8 gen_stat2 = cx25840_read(client, 0x40e);
922         int vid_input = state->vid_input;
923
924         v4l_info(client, "Video signal:              %spresent\n",
925                     (gen_stat2 & 0x20) ? "" : "not ");
926         v4l_info(client, "Detected format:           %s\n",
927                     fmt_strs[gen_stat1 & 0xf]);
928
929         v4l_info(client, "Specified standard:        %s\n",
930                     vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
931
932         if (vid_input >= CX25840_COMPOSITE1 &&
933             vid_input <= CX25840_COMPOSITE8) {
934                 v4l_info(client, "Specified video input:     Composite %d\n",
935                         vid_input - CX25840_COMPOSITE1 + 1);
936         } else {
937                 v4l_info(client, "Specified video input:     S-Video (Luma In%d, Chroma In%d)\n",
938                         (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
939         }
940
941         v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
942 }
943
944 /* ----------------------------------------------------------------------- */
945
946 static void log_audio_status(struct i2c_client *client)
947 {
948         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
949         u8 download_ctl = cx25840_read(client, 0x803);
950         u8 mod_det_stat0 = cx25840_read(client, 0x804);
951         u8 mod_det_stat1 = cx25840_read(client, 0x805);
952         u8 audio_config = cx25840_read(client, 0x808);
953         u8 pref_mode = cx25840_read(client, 0x809);
954         u8 afc0 = cx25840_read(client, 0x80b);
955         u8 mute_ctl = cx25840_read(client, 0x8d3);
956         int aud_input = state->aud_input;
957         char *p;
958
959         switch (mod_det_stat0) {
960         case 0x00: p = "mono"; break;
961         case 0x01: p = "stereo"; break;
962         case 0x02: p = "dual"; break;
963         case 0x04: p = "tri"; break;
964         case 0x10: p = "mono with SAP"; break;
965         case 0x11: p = "stereo with SAP"; break;
966         case 0x12: p = "dual with SAP"; break;
967         case 0x14: p = "tri with SAP"; break;
968         case 0xfe: p = "forced mode"; break;
969         default: p = "not defined";
970         }
971         v4l_info(client, "Detected audio mode:       %s\n", p);
972
973         switch (mod_det_stat1) {
974         case 0x00: p = "not defined"; break;
975         case 0x01: p = "EIAJ"; break;
976         case 0x02: p = "A2-M"; break;
977         case 0x03: p = "A2-BG"; break;
978         case 0x04: p = "A2-DK1"; break;
979         case 0x05: p = "A2-DK2"; break;
980         case 0x06: p = "A2-DK3"; break;
981         case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
982         case 0x08: p = "AM-L"; break;
983         case 0x09: p = "NICAM-BG"; break;
984         case 0x0a: p = "NICAM-DK"; break;
985         case 0x0b: p = "NICAM-I"; break;
986         case 0x0c: p = "NICAM-L"; break;
987         case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
988         case 0x0e: p = "IF FM Radio"; break;
989         case 0x0f: p = "BTSC"; break;
990         case 0x10: p = "high-deviation FM"; break;
991         case 0x11: p = "very high-deviation FM"; break;
992         case 0xfd: p = "unknown audio standard"; break;
993         case 0xfe: p = "forced audio standard"; break;
994         case 0xff: p = "no detected audio standard"; break;
995         default: p = "not defined";
996         }
997         v4l_info(client, "Detected audio standard:   %s\n", p);
998         v4l_info(client, "Audio muted:               %s\n",
999                     (state->unmute_volume >= 0) ? "yes" : "no");
1000         v4l_info(client, "Audio microcontroller:     %s\n",
1001                     (download_ctl & 0x10) ?
1002                                 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
1003
1004         switch (audio_config >> 4) {
1005         case 0x00: p = "undefined"; break;
1006         case 0x01: p = "BTSC"; break;
1007         case 0x02: p = "EIAJ"; break;
1008         case 0x03: p = "A2-M"; break;
1009         case 0x04: p = "A2-BG"; break;
1010         case 0x05: p = "A2-DK1"; break;
1011         case 0x06: p = "A2-DK2"; break;
1012         case 0x07: p = "A2-DK3"; break;
1013         case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1014         case 0x09: p = "AM-L"; break;
1015         case 0x0a: p = "NICAM-BG"; break;
1016         case 0x0b: p = "NICAM-DK"; break;
1017         case 0x0c: p = "NICAM-I"; break;
1018         case 0x0d: p = "NICAM-L"; break;
1019         case 0x0e: p = "FM radio"; break;
1020         case 0x0f: p = "automatic detection"; break;
1021         default: p = "undefined";
1022         }
1023         v4l_info(client, "Configured audio standard: %s\n", p);
1024
1025         if ((audio_config >> 4) < 0xF) {
1026                 switch (audio_config & 0xF) {
1027                 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1028                 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1029                 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1030                 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1031                 case 0x04: p = "STEREO"; break;
1032                 case 0x05: p = "DUAL1 (AB)"; break;
1033                 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1034                 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1035                 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1036                 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1037                 case 0x0a: p = "SAP"; break;
1038                 default: p = "undefined";
1039                 }
1040                 v4l_info(client, "Configured audio mode:     %s\n", p);
1041         } else {
1042                 switch (audio_config & 0xF) {
1043                 case 0x00: p = "BG"; break;
1044                 case 0x01: p = "DK1"; break;
1045                 case 0x02: p = "DK2"; break;
1046                 case 0x03: p = "DK3"; break;
1047                 case 0x04: p = "I"; break;
1048                 case 0x05: p = "L"; break;
1049                 case 0x06: p = "BTSC"; break;
1050                 case 0x07: p = "EIAJ"; break;
1051                 case 0x08: p = "A2-M"; break;
1052                 case 0x09: p = "FM Radio"; break;
1053                 case 0x0f: p = "automatic standard and mode detection"; break;
1054                 default: p = "undefined";
1055                 }
1056                 v4l_info(client, "Configured audio system:   %s\n", p);
1057         }
1058
1059         if (aud_input) {
1060                 v4l_info(client, "Specified audio input:     Tuner (In%d)\n", aud_input);
1061         } else {
1062                 v4l_info(client, "Specified audio input:     External\n");
1063         }
1064
1065         switch (pref_mode & 0xf) {
1066         case 0: p = "mono/language A"; break;
1067         case 1: p = "language B"; break;
1068         case 2: p = "language C"; break;
1069         case 3: p = "analog fallback"; break;
1070         case 4: p = "stereo"; break;
1071         case 5: p = "language AC"; break;
1072         case 6: p = "language BC"; break;
1073         case 7: p = "language AB"; break;
1074         default: p = "undefined";
1075         }
1076         v4l_info(client, "Preferred audio mode:      %s\n", p);
1077
1078         if ((audio_config & 0xf) == 0xf) {
1079                 switch ((afc0 >> 3) & 0x3) {
1080                 case 0: p = "system DK"; break;
1081                 case 1: p = "system L"; break;
1082                 case 2: p = "autodetect"; break;
1083                 default: p = "undefined";
1084                 }
1085                 v4l_info(client, "Selected 65 MHz format:    %s\n", p);
1086
1087                 switch (afc0 & 0x7) {
1088                 case 0: p = "chroma"; break;
1089                 case 1: p = "BTSC"; break;
1090                 case 2: p = "EIAJ"; break;
1091                 case 3: p = "A2-M"; break;
1092                 case 4: p = "autodetect"; break;
1093                 default: p = "undefined";
1094                 }
1095                 v4l_info(client, "Selected 45 MHz format:    %s\n", p);
1096         }
1097 }
1098
1099 /* ----------------------------------------------------------------------- */
1100
1101 /* This init operation must be called to load the driver's firmware.
1102    Without this the audio standard detection will fail and you will
1103    only get mono.
1104
1105    Since loading the firmware is often problematic when the driver is
1106    compiled into the kernel I recommend postponing calling this function
1107    until the first open of the video device. Another reason for
1108    postponing it is that loading this firmware takes a long time (seconds)
1109    due to the slow i2c bus speed. So it will speed up the boot process if
1110    you can avoid loading the fw as long as the video device isn't used.  */
1111 static int cx25840_init(struct v4l2_subdev *sd, u32 val)
1112 {
1113         struct cx25840_state *state = to_state(sd);
1114         struct i2c_client *client = v4l2_get_subdevdata(sd);
1115
1116         if (!state->is_initialized) {
1117                 /* initialize on first use */
1118                 state->is_initialized = 1;
1119                 if (state->is_cx25836)
1120                         cx25836_initialize(client);
1121                 else if (state->is_cx23885)
1122                         cx23885_initialize(client);
1123                 else
1124                         cx25840_initialize(client);
1125         }
1126         return 0;
1127 }
1128
1129 #ifdef CONFIG_VIDEO_ADV_DEBUG
1130 static int cx25840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1131 {
1132         struct i2c_client *client = v4l2_get_subdevdata(sd);
1133
1134         if (!v4l2_chip_match_i2c_client(client, &reg->match))
1135                 return -EINVAL;
1136         if (!capable(CAP_SYS_ADMIN))
1137                 return -EPERM;
1138         reg->size = 1;
1139         reg->val = cx25840_read(client, reg->reg & 0x0fff);
1140         return 0;
1141 }
1142
1143 static int cx25840_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
1144 {
1145         struct i2c_client *client = v4l2_get_subdevdata(sd);
1146
1147         if (!v4l2_chip_match_i2c_client(client, &reg->match))
1148                 return -EINVAL;
1149         if (!capable(CAP_SYS_ADMIN))
1150                 return -EPERM;
1151         cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
1152         return 0;
1153 }
1154 #endif
1155
1156 static int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi)
1157 {
1158         struct i2c_client *client = v4l2_get_subdevdata(sd);
1159
1160         return cx25840_vbi(client, VIDIOC_INT_DECODE_VBI_LINE, vbi);
1161 }
1162
1163 static int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
1164 {
1165         struct i2c_client *client = v4l2_get_subdevdata(sd);
1166
1167         return cx25840_audio(client, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freq);
1168 }
1169
1170 static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
1171 {
1172         struct cx25840_state *state = to_state(sd);
1173         struct i2c_client *client = v4l2_get_subdevdata(sd);
1174
1175         v4l_dbg(1, cx25840_debug, client, "%s output\n",
1176                         enable ? "enable" : "disable");
1177         if (enable) {
1178                 if (state->is_cx23885) {
1179                         u8 v = (cx25840_read(client, 0x421) | 0x0b);
1180                         cx25840_write(client, 0x421, v);
1181                 } else {
1182                         cx25840_write(client, 0x115,
1183                                         state->is_cx25836 ? 0x0c : 0x8c);
1184                         cx25840_write(client, 0x116,
1185                                         state->is_cx25836 ? 0x04 : 0x07);
1186                 }
1187         } else {
1188                 if (state->is_cx23885) {
1189                         u8 v = cx25840_read(client, 0x421) & ~(0x0b);
1190                         cx25840_write(client, 0x421, v);
1191                 } else {
1192                         cx25840_write(client, 0x115, 0x00);
1193                         cx25840_write(client, 0x116, 0x00);
1194                 }
1195         }
1196         return 0;
1197 }
1198
1199 static int cx25840_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1200 {
1201         struct cx25840_state *state = to_state(sd);
1202
1203         switch (qc->id) {
1204         case V4L2_CID_BRIGHTNESS:
1205                 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
1206         case V4L2_CID_CONTRAST:
1207         case V4L2_CID_SATURATION:
1208                 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
1209         case V4L2_CID_HUE:
1210                 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
1211         default:
1212                 break;
1213         }
1214         if (state->is_cx25836)
1215                 return -EINVAL;
1216
1217         switch (qc->id) {
1218         case V4L2_CID_AUDIO_VOLUME:
1219                 return v4l2_ctrl_query_fill(qc, 0, 65535,
1220                                 65535 / 100, state->default_volume);
1221         case V4L2_CID_AUDIO_MUTE:
1222                 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
1223         case V4L2_CID_AUDIO_BALANCE:
1224         case V4L2_CID_AUDIO_BASS:
1225         case V4L2_CID_AUDIO_TREBLE:
1226                 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
1227         default:
1228                 return -EINVAL;
1229         }
1230         return -EINVAL;
1231 }
1232
1233 static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1234 {
1235         struct cx25840_state *state = to_state(sd);
1236         struct i2c_client *client = v4l2_get_subdevdata(sd);
1237
1238         if (state->radio == 0 && state->std == std)
1239                 return 0;
1240         state->radio = 0;
1241         state->std = std;
1242         return set_v4lstd(client);
1243 }
1244
1245 static int cx25840_s_radio(struct v4l2_subdev *sd)
1246 {
1247         struct cx25840_state *state = to_state(sd);
1248
1249         state->radio = 1;
1250         return 0;
1251 }
1252
1253 static int cx25840_s_video_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
1254 {
1255         struct cx25840_state *state = to_state(sd);
1256         struct i2c_client *client = v4l2_get_subdevdata(sd);
1257
1258         return set_input(client, route->input, state->aud_input);
1259 }
1260
1261 static int cx25840_s_audio_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
1262 {
1263         struct cx25840_state *state = to_state(sd);
1264         struct i2c_client *client = v4l2_get_subdevdata(sd);
1265
1266         if (state->is_cx25836)
1267                 return -EINVAL;
1268         return set_input(client, state->vid_input, route->input);
1269 }
1270
1271 static int cx25840_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
1272 {
1273         struct cx25840_state *state = to_state(sd);
1274         struct i2c_client *client = v4l2_get_subdevdata(sd);
1275
1276         if (!state->is_cx25836)
1277                 input_change(client);
1278         return 0;
1279 }
1280
1281 static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1282 {
1283         struct cx25840_state *state = to_state(sd);
1284         struct i2c_client *client = v4l2_get_subdevdata(sd);
1285         u8 vpres = cx25840_read(client, 0x40e) & 0x20;
1286         u8 mode;
1287         int val = 0;
1288
1289         if (state->radio)
1290                 return 0;
1291
1292         vt->signal = vpres ? 0xffff : 0x0;
1293         if (state->is_cx25836)
1294                 return 0;
1295
1296         vt->capability |=
1297                 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
1298                 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
1299
1300         mode = cx25840_read(client, 0x804);
1301
1302         /* get rxsubchans and audmode */
1303         if ((mode & 0xf) == 1)
1304                 val |= V4L2_TUNER_SUB_STEREO;
1305         else
1306                 val |= V4L2_TUNER_SUB_MONO;
1307
1308         if (mode == 2 || mode == 4)
1309                 val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1310
1311         if (mode & 0x10)
1312                 val |= V4L2_TUNER_SUB_SAP;
1313
1314         vt->rxsubchans = val;
1315         vt->audmode = state->audmode;
1316         return 0;
1317 }
1318
1319 static int cx25840_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1320 {
1321         struct cx25840_state *state = to_state(sd);
1322         struct i2c_client *client = v4l2_get_subdevdata(sd);
1323
1324         if (state->radio || state->is_cx25836)
1325                 return 0;
1326
1327         switch (vt->audmode) {
1328                 case V4L2_TUNER_MODE_MONO:
1329                         /* mono      -> mono
1330                            stereo    -> mono
1331                            bilingual -> lang1 */
1332                         cx25840_and_or(client, 0x809, ~0xf, 0x00);
1333                         break;
1334                 case V4L2_TUNER_MODE_STEREO:
1335                 case V4L2_TUNER_MODE_LANG1:
1336                         /* mono      -> mono
1337                            stereo    -> stereo
1338                            bilingual -> lang1 */
1339                         cx25840_and_or(client, 0x809, ~0xf, 0x04);
1340                         break;
1341                 case V4L2_TUNER_MODE_LANG1_LANG2:
1342                         /* mono      -> mono
1343                            stereo    -> stereo
1344                            bilingual -> lang1/lang2 */
1345                         cx25840_and_or(client, 0x809, ~0xf, 0x07);
1346                         break;
1347                 case V4L2_TUNER_MODE_LANG2:
1348                         /* mono      -> mono
1349                            stereo    -> stereo
1350                            bilingual -> lang2 */
1351                         cx25840_and_or(client, 0x809, ~0xf, 0x01);
1352                         break;
1353                 default:
1354                         return -EINVAL;
1355         }
1356         state->audmode = vt->audmode;
1357         return 0;
1358 }
1359
1360 static int cx25840_reset(struct v4l2_subdev *sd, u32 val)
1361 {
1362         struct cx25840_state *state = to_state(sd);
1363         struct i2c_client *client = v4l2_get_subdevdata(sd);
1364
1365         if (state->is_cx25836)
1366                 cx25836_initialize(client);
1367         else if (state->is_cx23885)
1368                 cx23885_initialize(client);
1369         else
1370                 cx25840_initialize(client);
1371         return 0;
1372 }
1373
1374 static int cx25840_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
1375 {
1376         struct cx25840_state *state = to_state(sd);
1377         struct i2c_client *client = v4l2_get_subdevdata(sd);
1378
1379         return v4l2_chip_ident_i2c_client(client, chip, state->id, state->rev);
1380 }
1381
1382 static int cx25840_log_status(struct v4l2_subdev *sd)
1383 {
1384         struct cx25840_state *state = to_state(sd);
1385         struct i2c_client *client = v4l2_get_subdevdata(sd);
1386
1387         log_video_status(client);
1388         if (!state->is_cx25836)
1389                 log_audio_status(client);
1390         return 0;
1391 }
1392
1393 /* ----------------------------------------------------------------------- */
1394
1395 static const struct v4l2_subdev_core_ops cx25840_core_ops = {
1396         .log_status = cx25840_log_status,
1397         .g_chip_ident = cx25840_g_chip_ident,
1398         .g_ctrl = cx25840_g_ctrl,
1399         .s_ctrl = cx25840_s_ctrl,
1400         .queryctrl = cx25840_queryctrl,
1401         .reset = cx25840_reset,
1402         .init = cx25840_init,
1403 #ifdef CONFIG_VIDEO_ADV_DEBUG
1404         .g_register = cx25840_g_register,
1405         .s_register = cx25840_s_register,
1406 #endif
1407 };
1408
1409 static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
1410         .s_frequency = cx25840_s_frequency,
1411         .s_std = cx25840_s_std,
1412         .s_radio = cx25840_s_radio,
1413         .g_tuner = cx25840_g_tuner,
1414         .s_tuner = cx25840_s_tuner,
1415 };
1416
1417 static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {
1418         .s_clock_freq = cx25840_s_clock_freq,
1419         .s_routing = cx25840_s_audio_routing,
1420 };
1421
1422 static const struct v4l2_subdev_video_ops cx25840_video_ops = {
1423         .s_routing = cx25840_s_video_routing,
1424         .g_fmt = cx25840_g_fmt,
1425         .s_fmt = cx25840_s_fmt,
1426         .decode_vbi_line = cx25840_decode_vbi_line,
1427         .s_stream = cx25840_s_stream,
1428 };
1429
1430 static const struct v4l2_subdev_ops cx25840_ops = {
1431         .core = &cx25840_core_ops,
1432         .tuner = &cx25840_tuner_ops,
1433         .audio = &cx25840_audio_ops,
1434         .video = &cx25840_video_ops,
1435 };
1436
1437 /* ----------------------------------------------------------------------- */
1438
1439 static int cx25840_probe(struct i2c_client *client,
1440                          const struct i2c_device_id *did)
1441 {
1442         struct cx25840_state *state;
1443         struct v4l2_subdev *sd;
1444         u32 id;
1445         u16 device_id;
1446
1447         /* Check if the adapter supports the needed features */
1448         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1449                 return -EIO;
1450
1451         v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
1452
1453         device_id = cx25840_read(client, 0x101) << 8;
1454         device_id |= cx25840_read(client, 0x100);
1455         v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
1456
1457         /* The high byte of the device ID should be
1458          * 0x83 for the cx2583x and 0x84 for the cx2584x */
1459         if ((device_id & 0xff00) == 0x8300) {
1460                 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
1461         }
1462         else if ((device_id & 0xff00) == 0x8400) {
1463                 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
1464         } else if (device_id == 0x0000) {
1465                 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
1466         } else if (device_id == 0x1313) {
1467                 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
1468         }
1469         else {
1470                 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
1471                 return -ENODEV;
1472         }
1473
1474         state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
1475         if (state == NULL)
1476                 return -ENOMEM;
1477
1478         sd = &state->sd;
1479         v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
1480         /* Note: revision '(device_id & 0x0f) == 2' was never built. The
1481            marking skips from 0x1 == 22 to 0x3 == 23. */
1482         v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
1483                     (device_id & 0xfff0) >> 4,
1484                     (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
1485                     client->addr << 1, client->adapter->name);
1486
1487         state->c = client;
1488         state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
1489         state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
1490         state->vid_input = CX25840_COMPOSITE7;
1491         state->aud_input = CX25840_AUDIO8;
1492         state->audclk_freq = 48000;
1493         state->pvr150_workaround = 0;
1494         state->audmode = V4L2_TUNER_MODE_LANG1;
1495         state->unmute_volume = -1;
1496         state->default_volume = 228 - cx25840_read(client, 0x8d4);
1497         state->default_volume = ((state->default_volume / 2) + 23) << 9;
1498         state->vbi_line_offset = 8;
1499         state->id = id;
1500         state->rev = device_id;
1501
1502         if (state->is_cx23885) {
1503                 /* Drive GPIO2 direction and values */
1504                 cx25840_write(client, 0x160, 0x1d);
1505                 cx25840_write(client, 0x164, 0x00);
1506         }
1507
1508         return 0;
1509 }
1510
1511 static int cx25840_remove(struct i2c_client *client)
1512 {
1513         struct v4l2_subdev *sd = i2c_get_clientdata(client);
1514
1515         v4l2_device_unregister_subdev(sd);
1516         kfree(to_state(sd));
1517         return 0;
1518 }
1519
1520 static const struct i2c_device_id cx25840_id[] = {
1521         { "cx25840", 0 },
1522         { }
1523 };
1524 MODULE_DEVICE_TABLE(i2c, cx25840_id);
1525
1526 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1527         .name = "cx25840",
1528         .probe = cx25840_probe,
1529         .remove = cx25840_remove,
1530         .id_table = cx25840_id,
1531 };