blob: 88f2fd32bfe357617f82f011df18a9c31ddf7997 [file] [log] [blame]
Hans Verkuilbd985162005-11-13 16:07:56 -08001/* 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 *
Christopher Neufeld3e3bf272006-05-24 10:16:45 -030013 * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
14 * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
15 *
Steven Toth6d897612008-09-03 17:12:12 -030016 * CX23885 support by Steven Toth <stoth@linuxtv.org>.
Steven Tothf2340812008-01-10 01:22:39 -030017 *
Hans Verkuilbd985162005-11-13 16:07:56 -080018 * 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>
Michael Krufkyf61b48f2007-09-01 02:02:51 -030039#include <linux/delay.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080040#include <media/v4l2-common.h>
Hans Verkuil3434eb72007-04-27 12:31:08 -030041#include <media/v4l2-chip-ident.h>
Hans Verkuil1a392752007-09-13 11:44:47 -030042#include <media/v4l2-i2c-drv-legacy.h>
Hans Verkuil31bc09b2006-03-25 10:26:09 -030043#include <media/cx25840.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080044
Hans Verkuil31bc09b2006-03-25 10:26:09 -030045#include "cx25840-core.h"
Hans Verkuilbd985162005-11-13 16:07:56 -080046
47MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
Hans Verkuil1f4b3362005-11-13 16:08:05 -080048MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
Hans Verkuilbd985162005-11-13 16:07:56 -080049MODULE_LICENSE("GPL");
50
51static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
52
Adrian Bunkfe0d3df2008-07-21 16:33:48 -030053static int cx25840_debug;
Hans Verkuilbd985162005-11-13 16:07:56 -080054
Hans Verkuilb5fc7142006-01-11 22:41:36 -020055module_param_named(debug,cx25840_debug, int, 0644);
Hans Verkuilbd985162005-11-13 16:07:56 -080056
Hans Verkuilfac9e892006-01-09 15:32:40 -020057MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
Hans Verkuilbd985162005-11-13 16:07:56 -080058
59I2C_CLIENT_INSMOD;
60
61/* ----------------------------------------------------------------------- */
62
63int 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
72int 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;
Hans Verkuil4a56eb32007-12-02 07:03:45 -030077 buffer[2] = value & 0xff;
78 buffer[3] = (value >> 8) & 0xff;
79 buffer[4] = (value >> 16) & 0xff;
80 buffer[5] = value >> 24;
Hans Verkuilbd985162005-11-13 16:07:56 -080081 return i2c_master_send(client, buffer, 6);
82}
83
84u8 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
99u32 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
Hans Verkuil17531c12006-08-08 09:10:12 -0300111 return (buffer[3] << 24) | (buffer[2] << 16) |
112 (buffer[1] << 8) | buffer[0];
Hans Verkuilbd985162005-11-13 16:07:56 -0800113}
114
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300115int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
Hans Verkuilbd985162005-11-13 16:07:56 -0800116 u8 or_value)
117{
118 return cx25840_write(client, addr,
119 (cx25840_read(client, addr) & and_mask) |
120 or_value);
121}
122
123/* ----------------------------------------------------------------------- */
124
Hans Verkuila8bbf122006-01-09 15:25:42 -0200125static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
126 enum cx25840_audio_input aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800127
128/* ----------------------------------------------------------------------- */
129
Hans Verkuild92c20e2006-01-09 15:32:41 -0200130static void init_dll1(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800131{
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);
Tyler Trafford38051452007-08-28 17:56:47 -0300137 udelay(10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800138 cx25840_write(client, 0x159, 0xe1);
Tyler Trafford38051452007-08-28 17:56:47 -0300139 udelay(10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800140 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
Hans Verkuild92c20e2006-01-09 15:32:41 -0200146static void init_dll2(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800147{
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);
Tyler Trafford38051452007-08-28 17:56:47 -0300153 udelay(10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800154 cx25840_write(client, 0x15d, 0xe1);
155 cx25840_write(client, 0x15d, 0xe0);
156 cx25840_write(client, 0x15d, 0xe1);
157}
158
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300159static 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. */
Tyler Trafford38051452007-08-28 17:56:47 -0300172 udelay(10);
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300173 /* 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
Hans Verkuil21340ae2007-08-26 10:53:16 -0300184static 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
Hans Verkuil89fc4eb2007-08-04 05:00:07 -0300191static void cx25840_initialize(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800192{
Hans Verkuil21340ae2007-08-26 10:53:16 -0300193 DEFINE_WAIT(wait);
Hans Verkuil9357b312008-11-29 12:50:06 -0300194 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil21340ae2007-08-26 10:53:16 -0300195 struct workqueue_struct *q;
Hans Verkuilbd985162005-11-13 16:07:56 -0800196
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. */
Hans Verkuil21340ae2007-08-26 10:53:16 -0300211 /* 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
Hans Verkuilbd985162005-11-13 16:07:56 -0800224 /* 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. */
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300233 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() */
Hans Verkuilbd985162005-11-13 16:07:56 -0800236 /* 10. */
237 cx25840_write(client, 0x8d3, 0x1f);
238 cx25840_write(client, 0x8e3, 0x03);
239
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300240 cx25840_std_setup(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800241
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
Hans Verkuila8bbf122006-01-09 15:25:42 -0200252 /* (re)set input */
253 set_input(client, state->vid_input, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800254
255 /* start microcontroller */
256 cx25840_and_or(client, 0x803, ~0x10, 0x10);
257}
258
Steven Tothf2340812008-01-10 01:22:39 -0300259static void cx23885_initialize(struct i2c_client *client)
260{
261 DEFINE_WAIT(wait);
Hans Verkuil9357b312008-11-29 12:50:06 -0300262 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Steven Tothf2340812008-01-10 01:22:39 -0300263 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
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300340 cx25840_std_setup(client);
Steven Tothf2340812008-01-10 01:22:39 -0300341
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
Hans Verkuilbd985162005-11-13 16:07:56 -0800349/* ----------------------------------------------------------------------- */
350
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300351void cx25840_std_setup(struct i2c_client *client)
352{
Hans Verkuil9357b312008-11-29 12:50:06 -0300353 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300354 v4l2_std_id std = state->std;
355 int hblank, hactive, burst, vblank, vactive, sc;
356 int vblank656, src_decimation;
357 int luma_lpf, uv_lpf, comb;
358 u32 pll_int, pll_frac, pll_post;
359
360 /* datasheet startup, step 8d */
361 if (std & ~V4L2_STD_NTSC)
362 cx25840_write(client, 0x49f, 0x11);
363 else
364 cx25840_write(client, 0x49f, 0x14);
365
366 if (std & V4L2_STD_625_50) {
367 hblank = 132;
368 hactive = 720;
369 burst = 93;
370 vblank = 36;
371 vactive = 580;
372 vblank656 = 40;
373 src_decimation = 0x21f;
374 luma_lpf = 2;
375
376 if (std & V4L2_STD_SECAM) {
377 uv_lpf = 0;
378 comb = 0;
379 sc = 0x0a425f;
380 } else if (std == V4L2_STD_PAL_Nc) {
381 uv_lpf = 1;
382 comb = 0x20;
383 sc = 556453;
384 } else {
385 uv_lpf = 1;
386 comb = 0x20;
387 sc = 688739;
388 }
389 } else {
390 hactive = 720;
391 hblank = 122;
392 vactive = 487;
393 luma_lpf = 1;
394 uv_lpf = 1;
395
396 src_decimation = 0x21f;
397 if (std == V4L2_STD_PAL_60) {
398 vblank = 26;
399 vblank656 = 26;
400 burst = 0x5b;
401 luma_lpf = 2;
402 comb = 0x20;
403 sc = 688739;
404 } else if (std == V4L2_STD_PAL_M) {
405 vblank = 20;
406 vblank656 = 24;
407 burst = 0x61;
408 comb = 0x20;
409 sc = 555452;
410 } else {
411 vblank = 26;
412 vblank656 = 26;
413 burst = 0x5b;
414 comb = 0x66;
415 sc = 556063;
416 }
417 }
418
419 /* DEBUG: Displays configured PLL frequency */
420 pll_int = cx25840_read(client, 0x108);
421 pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
422 pll_post = cx25840_read(client, 0x109);
423 v4l_dbg(1, cx25840_debug, client,
424 "PLL regs = int: %u, frac: %u, post: %u\n",
425 pll_int, pll_frac, pll_post);
426
427 if (pll_post) {
428 int fin, fsc;
429 int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
430
431 pll /= pll_post;
432 v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",
433 pll / 1000000, pll % 1000000);
434 v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",
435 pll / 8000000, (pll / 8) % 1000000);
436
437 fin = ((u64)src_decimation * pll) >> 12;
438 v4l_dbg(1, cx25840_debug, client,
439 "ADC Sampling freq = %d.%06d MHz\n",
440 fin / 1000000, fin % 1000000);
441
442 fsc = (((u64)sc) * pll) >> 24L;
443 v4l_dbg(1, cx25840_debug, client,
444 "Chroma sub-carrier freq = %d.%06d MHz\n",
445 fsc / 1000000, fsc % 1000000);
446
447 v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
448 "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
449 "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "
450 "sc 0x%06x\n",
451 hblank, hactive, vblank, vactive, vblank656,
452 src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
453 }
454
455 /* Sets horizontal blanking delay and active lines */
456 cx25840_write(client, 0x470, hblank);
457 cx25840_write(client, 0x471,
458 0xff & (((hblank >> 8) & 0x3) | (hactive << 4)));
459 cx25840_write(client, 0x472, hactive >> 4);
460
461 /* Sets burst gate delay */
462 cx25840_write(client, 0x473, burst);
463
464 /* Sets vertical blanking delay and active duration */
465 cx25840_write(client, 0x474, vblank);
466 cx25840_write(client, 0x475,
467 0xff & (((vblank >> 8) & 0x3) | (vactive << 4)));
468 cx25840_write(client, 0x476, vactive >> 4);
469 cx25840_write(client, 0x477, vblank656);
470
471 /* Sets src decimation rate */
472 cx25840_write(client, 0x478, 0xff & src_decimation);
473 cx25840_write(client, 0x479, 0xff & (src_decimation >> 8));
474
475 /* Sets Luma and UV Low pass filters */
476 cx25840_write(client, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
477
478 /* Enables comb filters */
479 cx25840_write(client, 0x47b, comb);
480
481 /* Sets SC Step*/
482 cx25840_write(client, 0x47c, sc);
483 cx25840_write(client, 0x47d, 0xff & sc >> 8);
484 cx25840_write(client, 0x47e, 0xff & sc >> 16);
485
486 /* Sets VBI parameters */
487 if (std & V4L2_STD_625_50) {
488 cx25840_write(client, 0x47f, 0x01);
489 state->vbi_line_offset = 5;
490 } else {
491 cx25840_write(client, 0x47f, 0x00);
492 state->vbi_line_offset = 8;
493 }
494}
495
496/* ----------------------------------------------------------------------- */
497
Hans Verkuilbd985162005-11-13 16:07:56 -0800498static void input_change(struct i2c_client *client)
499{
Hans Verkuil9357b312008-11-29 12:50:06 -0300500 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil081b4962008-04-22 14:45:51 -0300501 v4l2_std_id std = state->std;
Hans Verkuilbd985162005-11-13 16:07:56 -0800502
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300503 /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
504 if (std & V4L2_STD_SECAM) {
505 cx25840_write(client, 0x402, 0);
506 }
507 else {
508 cx25840_write(client, 0x402, 0x04);
509 cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
510 }
511 cx25840_and_or(client, 0x401, ~0x60, 0);
512 cx25840_and_or(client, 0x401, ~0x60, 0x60);
Hans Verkuil82677612007-08-07 07:16:07 -0300513 cx25840_and_or(client, 0x810, ~0x01, 1);
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300514
Hans Verkuil39c4ad62007-08-05 14:24:17 -0300515 if (state->radio) {
516 cx25840_write(client, 0x808, 0xf9);
517 cx25840_write(client, 0x80b, 0x00);
518 }
519 else if (std & V4L2_STD_525_60) {
Hans Verkuild97a11e2006-02-07 06:48:40 -0200520 /* Certain Hauppauge PVR150 models have a hardware bug
521 that causes audio to drop out. For these models the
522 audio standard must be set explicitly.
523 To be precise: it affects cards with tuner models
524 85, 99 and 112 (model numbers from tveeprom). */
525 int hw_fix = state->pvr150_workaround;
526
527 if (std == V4L2_STD_NTSC_M_JP) {
Hans Verkuilf95006f2005-12-01 00:51:42 -0800528 /* Japan uses EIAJ audio standard */
Hans Verkuild97a11e2006-02-07 06:48:40 -0200529 cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
530 } else if (std == V4L2_STD_NTSC_M_KR) {
531 /* South Korea uses A2 audio standard */
532 cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
Hans Verkuilf95006f2005-12-01 00:51:42 -0800533 } else {
534 /* Others use the BTSC audio standard */
Hans Verkuild97a11e2006-02-07 06:48:40 -0200535 cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
Hans Verkuilf95006f2005-12-01 00:51:42 -0800536 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800537 cx25840_write(client, 0x80b, 0x00);
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300538 } else if (std & V4L2_STD_PAL) {
539 /* Follow tuner change procedure for PAL */
540 cx25840_write(client, 0x808, 0xff);
541 cx25840_write(client, 0x80b, 0x10);
542 } else if (std & V4L2_STD_SECAM) {
543 /* Select autodetect for SECAM */
544 cx25840_write(client, 0x808, 0xff);
545 cx25840_write(client, 0x80b, 0x10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800546 }
547
Hans Verkuil82677612007-08-07 07:16:07 -0300548 cx25840_and_or(client, 0x810, ~0x01, 0);
Hans Verkuilbd985162005-11-13 16:07:56 -0800549}
550
Hans Verkuila8bbf122006-01-09 15:25:42 -0200551static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
552 enum cx25840_audio_input aud_input)
Hans Verkuilbd985162005-11-13 16:07:56 -0800553{
Hans Verkuil9357b312008-11-29 12:50:06 -0300554 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuila8bbf122006-01-09 15:25:42 -0200555 u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
556 vid_input <= CX25840_COMPOSITE8);
557 u8 reg;
Hans Verkuilbd985162005-11-13 16:07:56 -0800558
Steven Tothf2340812008-01-10 01:22:39 -0300559 v4l_dbg(1, cx25840_debug, client,
560 "decoder set video input %d, audio input %d\n",
561 vid_input, aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800562
Steven Tothf2340812008-01-10 01:22:39 -0300563 if (vid_input >= CX25840_VIN1_CH1) {
564 v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
565 vid_input);
566 reg = vid_input & 0xff;
567 if ((vid_input & CX25840_SVIDEO_ON) == CX25840_SVIDEO_ON)
568 is_composite = 0;
569 else
570 is_composite = 1;
571
572 v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
573 reg, is_composite);
574 } else
Hans Verkuila8bbf122006-01-09 15:25:42 -0200575 if (is_composite) {
576 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
577 } else {
578 int luma = vid_input & 0xf0;
579 int chroma = vid_input & 0xf00;
Hans Verkuilbd985162005-11-13 16:07:56 -0800580
Hans Verkuila8bbf122006-01-09 15:25:42 -0200581 if ((vid_input & ~0xff0) ||
Hans Verkuil45270a12008-06-07 11:18:17 -0300582 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||
Hans Verkuila8bbf122006-01-09 15:25:42 -0200583 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
Steven Tothf2340812008-01-10 01:22:39 -0300584 v4l_err(client, "0x%04x is not a valid video input!\n",
585 vid_input);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200586 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800587 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200588 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
589 if (chroma >= CX25840_SVIDEO_CHROMA7) {
590 reg &= 0x3f;
591 reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
Hans Verkuilbd985162005-11-13 16:07:56 -0800592 } else {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200593 reg &= 0xcf;
594 reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
Hans Verkuilbd985162005-11-13 16:07:56 -0800595 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200596 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800597
Steven Tothf2340812008-01-10 01:22:39 -0300598 /* The caller has previously prepared the correct routing
599 * configuration in reg (for the cx23885) so we have no
600 * need to attempt to flip bits for earlier av decoders.
601 */
602 if (!state->is_cx23885) {
603 switch (aud_input) {
604 case CX25840_AUDIO_SERIAL:
605 /* do nothing, use serial audio input */
606 break;
607 case CX25840_AUDIO4: reg &= ~0x30; break;
608 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
609 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
610 case CX25840_AUDIO7: reg &= ~0xc0; break;
611 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
Hans Verkuilbd985162005-11-13 16:07:56 -0800612
Steven Tothf2340812008-01-10 01:22:39 -0300613 default:
614 v4l_err(client, "0x%04x is not a valid audio input!\n",
615 aud_input);
616 return -EINVAL;
617 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800618 }
619
Hans Verkuila8bbf122006-01-09 15:25:42 -0200620 cx25840_write(client, 0x103, reg);
Steven Tothf2340812008-01-10 01:22:39 -0300621
Hans Verkuila8bbf122006-01-09 15:25:42 -0200622 /* Set INPUT_MODE to Composite (0) or S-Video (1) */
623 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
Steven Tothf2340812008-01-10 01:22:39 -0300624
625 if (!state->is_cx23885) {
626 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
627 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
628 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
629 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
630 cx25840_and_or(client, 0x102, ~0x4, 4);
631 else
632 cx25840_and_or(client, 0x102, ~0x4, 0);
633 } else {
634 if (is_composite)
635 /* ADC2 input select channel 2 */
636 cx25840_and_or(client, 0x102, ~0x2, 0);
637 else
638 /* ADC2 input select channel 3 */
639 cx25840_and_or(client, 0x102, ~0x2, 2);
640 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200641
642 state->vid_input = vid_input;
643 state->aud_input = aud_input;
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300644 if (!state->is_cx25836) {
645 cx25840_audio_set_path(client);
646 input_change(client);
647 }
Steven Tothf2340812008-01-10 01:22:39 -0300648
649 if (state->is_cx23885) {
650 /* Audio channel 1 src : Parallel 1 */
651 cx25840_write(client, 0x124, 0x03);
652
653 /* Select AFE clock pad output source */
654 cx25840_write(client, 0x144, 0x05);
655
656 /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
657 cx25840_write(client, 0x914, 0xa0);
658
659 /* I2S_OUT_CTL:
660 * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
661 * I2S_OUT_MASTER_MODE = Master
662 */
663 cx25840_write(client, 0x918, 0xa0);
664 cx25840_write(client, 0x919, 0x01);
665 }
666
Hans Verkuilbd985162005-11-13 16:07:56 -0800667 return 0;
668}
669
670/* ----------------------------------------------------------------------- */
671
Hans Verkuil081b4962008-04-22 14:45:51 -0300672static int set_v4lstd(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800673{
Hans Verkuil9357b312008-11-29 12:50:06 -0300674 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil081b4962008-04-22 14:45:51 -0300675 u8 fmt = 0; /* zero is autodetect */
676 u8 pal_m = 0;
Hans Verkuilbd985162005-11-13 16:07:56 -0800677
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200678 /* First tests should be against specific std */
Hans Verkuil081b4962008-04-22 14:45:51 -0300679 if (state->std == V4L2_STD_NTSC_M_JP) {
680 fmt = 0x2;
681 } else if (state->std == V4L2_STD_NTSC_443) {
682 fmt = 0x3;
683 } else if (state->std == V4L2_STD_PAL_M) {
684 pal_m = 1;
685 fmt = 0x5;
686 } else if (state->std == V4L2_STD_PAL_N) {
687 fmt = 0x6;
688 } else if (state->std == V4L2_STD_PAL_Nc) {
689 fmt = 0x7;
690 } else if (state->std == V4L2_STD_PAL_60) {
691 fmt = 0x8;
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200692 } else {
693 /* Then, test against generic ones */
Hans Verkuil081b4962008-04-22 14:45:51 -0300694 if (state->std & V4L2_STD_NTSC)
695 fmt = 0x1;
696 else if (state->std & V4L2_STD_PAL)
697 fmt = 0x4;
698 else if (state->std & V4L2_STD_SECAM)
699 fmt = 0xc;
Hans Verkuilbd985162005-11-13 16:07:56 -0800700 }
701
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300702 v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
703
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300704 /* Follow step 9 of section 3.16 in the cx25840 datasheet.
705 Without this PAL may display a vertical ghosting effect.
706 This happens for example with the Yuan MPC622. */
707 if (fmt >= 4 && fmt < 8) {
708 /* Set format to NTSC-M */
709 cx25840_and_or(client, 0x400, ~0xf, 1);
710 /* Turn off LCOMB */
711 cx25840_and_or(client, 0x47b, ~6, 0);
712 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800713 cx25840_and_or(client, 0x400, ~0xf, fmt);
Hans Verkuil081b4962008-04-22 14:45:51 -0300714 cx25840_and_or(client, 0x403, ~0x3, pal_m);
Hans Verkuilcb5aa1c2008-07-17 11:08:40 -0300715 cx25840_std_setup(client);
Hans Verkuil081b4962008-04-22 14:45:51 -0300716 if (!state->is_cx25836)
717 input_change(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800718 return 0;
719}
720
Hans Verkuilbd985162005-11-13 16:07:56 -0800721/* ----------------------------------------------------------------------- */
722
Hans Verkuil9357b312008-11-29 12:50:06 -0300723static int cx25840_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Hans Verkuilbd985162005-11-13 16:07:56 -0800724{
Hans Verkuil9357b312008-11-29 12:50:06 -0300725 struct cx25840_state *state = to_state(sd);
726 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -0800727
728 switch (ctrl->id) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200729 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
730 state->pvr150_workaround = ctrl->value;
731 set_input(client, state->vid_input, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800732 break;
733
734 case V4L2_CID_BRIGHTNESS:
735 if (ctrl->value < 0 || ctrl->value > 255) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200736 v4l_err(client, "invalid brightness setting %d\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800737 ctrl->value);
738 return -ERANGE;
739 }
740
741 cx25840_write(client, 0x414, ctrl->value - 128);
742 break;
743
744 case V4L2_CID_CONTRAST:
745 if (ctrl->value < 0 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200746 v4l_err(client, "invalid contrast setting %d\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800747 ctrl->value);
748 return -ERANGE;
749 }
750
751 cx25840_write(client, 0x415, ctrl->value << 1);
752 break;
753
754 case V4L2_CID_SATURATION:
755 if (ctrl->value < 0 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200756 v4l_err(client, "invalid saturation setting %d\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800757 ctrl->value);
758 return -ERANGE;
759 }
760
761 cx25840_write(client, 0x420, ctrl->value << 1);
762 cx25840_write(client, 0x421, ctrl->value << 1);
763 break;
764
765 case V4L2_CID_HUE:
766 if (ctrl->value < -127 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200767 v4l_err(client, "invalid hue setting %d\n", ctrl->value);
Hans Verkuilbd985162005-11-13 16:07:56 -0800768 return -ERANGE;
769 }
770
771 cx25840_write(client, 0x422, ctrl->value);
772 break;
773
774 case V4L2_CID_AUDIO_VOLUME:
775 case V4L2_CID_AUDIO_BASS:
776 case V4L2_CID_AUDIO_TREBLE:
777 case V4L2_CID_AUDIO_BALANCE:
778 case V4L2_CID_AUDIO_MUTE:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300779 if (state->is_cx25836)
780 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800781 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200782
783 default:
784 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800785 }
786
787 return 0;
788}
789
Hans Verkuil9357b312008-11-29 12:50:06 -0300790static int cx25840_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Hans Verkuilbd985162005-11-13 16:07:56 -0800791{
Hans Verkuil9357b312008-11-29 12:50:06 -0300792 struct cx25840_state *state = to_state(sd);
793 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -0800794
795 switch (ctrl->id) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200796 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
797 ctrl->value = state->pvr150_workaround;
Hans Verkuilbd985162005-11-13 16:07:56 -0800798 break;
799 case V4L2_CID_BRIGHTNESS:
Hans Verkuil0de71222006-01-09 15:32:44 -0200800 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
Hans Verkuilbd985162005-11-13 16:07:56 -0800801 break;
802 case V4L2_CID_CONTRAST:
803 ctrl->value = cx25840_read(client, 0x415) >> 1;
804 break;
805 case V4L2_CID_SATURATION:
806 ctrl->value = cx25840_read(client, 0x420) >> 1;
807 break;
808 case V4L2_CID_HUE:
Hans Verkuilc5099a62006-01-09 15:32:43 -0200809 ctrl->value = (s8)cx25840_read(client, 0x422);
Hans Verkuilbd985162005-11-13 16:07:56 -0800810 break;
811 case V4L2_CID_AUDIO_VOLUME:
812 case V4L2_CID_AUDIO_BASS:
813 case V4L2_CID_AUDIO_TREBLE:
814 case V4L2_CID_AUDIO_BALANCE:
815 case V4L2_CID_AUDIO_MUTE:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300816 if (state->is_cx25836)
817 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800818 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
819 default:
820 return -EINVAL;
821 }
822
823 return 0;
824}
825
826/* ----------------------------------------------------------------------- */
827
Hans Verkuil9357b312008-11-29 12:50:06 -0300828static int cx25840_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
Hans Verkuilbd985162005-11-13 16:07:56 -0800829{
Hans Verkuil9357b312008-11-29 12:50:06 -0300830 struct i2c_client *client = v4l2_get_subdevdata(sd);
831
Hans Verkuilbd985162005-11-13 16:07:56 -0800832 switch (fmt->type) {
833 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
834 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
835 default:
836 return -EINVAL;
837 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800838 return 0;
839}
840
Hans Verkuil9357b312008-11-29 12:50:06 -0300841static int cx25840_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *fmt)
Hans Verkuilbd985162005-11-13 16:07:56 -0800842{
Hans Verkuil9357b312008-11-29 12:50:06 -0300843 struct cx25840_state *state = to_state(sd);
844 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -0800845 struct v4l2_pix_format *pix;
846 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
Hans Verkuil081b4962008-04-22 14:45:51 -0300847 int is_50Hz = !(state->std & V4L2_STD_525_60);
Hans Verkuilbd985162005-11-13 16:07:56 -0800848
849 switch (fmt->type) {
850 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
851 pix = &(fmt->fmt.pix);
852
853 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
854 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
855
856 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
857 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
858
Servaas Vandenbergheba70d592007-04-29 16:27:30 -0300859 Vlines = pix->height + (is_50Hz ? 4 : 7);
Hans Verkuilbd985162005-11-13 16:07:56 -0800860
861 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
862 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200863 v4l_err(client, "%dx%d is not a valid size!\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800864 pix->width, pix->height);
865 return -ERANGE;
866 }
867
868 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
869 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
870 VSC &= 0x1fff;
871
872 if (pix->width >= 385)
873 filter = 0;
874 else if (pix->width > 192)
875 filter = 1;
876 else if (pix->width > 96)
877 filter = 2;
878 else
879 filter = 3;
880
Hans Verkuilb5fc7142006-01-11 22:41:36 -0200881 v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800882 pix->width, pix->height, HSC, VSC);
883
884 /* HSCALE=HSC */
885 cx25840_write(client, 0x418, HSC & 0xff);
886 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
887 cx25840_write(client, 0x41a, HSC >> 16);
888 /* VSCALE=VSC */
889 cx25840_write(client, 0x41c, VSC & 0xff);
890 cx25840_write(client, 0x41d, VSC >> 8);
891 /* VS_INTRLACE=1 VFILT=filter */
892 cx25840_write(client, 0x41e, 0x8 | filter);
893 break;
894
895 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
896 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
897
898 case V4L2_BUF_TYPE_VBI_CAPTURE:
899 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
900
901 default:
902 return -EINVAL;
903 }
904
905 return 0;
906}
907
908/* ----------------------------------------------------------------------- */
909
Hans Verkuil1a392752007-09-13 11:44:47 -0300910static void log_video_status(struct i2c_client *client)
911{
912 static const char *const fmt_strs[] = {
913 "0x0",
914 "NTSC-M", "NTSC-J", "NTSC-4.43",
915 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
916 "0x9", "0xA", "0xB",
917 "SECAM",
918 "0xD", "0xE", "0xF"
919 };
920
Hans Verkuil9357b312008-11-29 12:50:06 -0300921 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil1a392752007-09-13 11:44:47 -0300922 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
923 u8 gen_stat1 = cx25840_read(client, 0x40d);
924 u8 gen_stat2 = cx25840_read(client, 0x40e);
925 int vid_input = state->vid_input;
926
927 v4l_info(client, "Video signal: %spresent\n",
928 (gen_stat2 & 0x20) ? "" : "not ");
929 v4l_info(client, "Detected format: %s\n",
930 fmt_strs[gen_stat1 & 0xf]);
931
932 v4l_info(client, "Specified standard: %s\n",
933 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
934
935 if (vid_input >= CX25840_COMPOSITE1 &&
936 vid_input <= CX25840_COMPOSITE8) {
937 v4l_info(client, "Specified video input: Composite %d\n",
938 vid_input - CX25840_COMPOSITE1 + 1);
939 } else {
940 v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
941 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
942 }
943
944 v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
945}
946
947/* ----------------------------------------------------------------------- */
948
949static void log_audio_status(struct i2c_client *client)
950{
Hans Verkuil9357b312008-11-29 12:50:06 -0300951 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuil1a392752007-09-13 11:44:47 -0300952 u8 download_ctl = cx25840_read(client, 0x803);
953 u8 mod_det_stat0 = cx25840_read(client, 0x804);
954 u8 mod_det_stat1 = cx25840_read(client, 0x805);
955 u8 audio_config = cx25840_read(client, 0x808);
956 u8 pref_mode = cx25840_read(client, 0x809);
957 u8 afc0 = cx25840_read(client, 0x80b);
958 u8 mute_ctl = cx25840_read(client, 0x8d3);
959 int aud_input = state->aud_input;
960 char *p;
961
962 switch (mod_det_stat0) {
963 case 0x00: p = "mono"; break;
964 case 0x01: p = "stereo"; break;
965 case 0x02: p = "dual"; break;
966 case 0x04: p = "tri"; break;
967 case 0x10: p = "mono with SAP"; break;
968 case 0x11: p = "stereo with SAP"; break;
969 case 0x12: p = "dual with SAP"; break;
970 case 0x14: p = "tri with SAP"; break;
971 case 0xfe: p = "forced mode"; break;
972 default: p = "not defined";
973 }
974 v4l_info(client, "Detected audio mode: %s\n", p);
975
976 switch (mod_det_stat1) {
977 case 0x00: p = "not defined"; break;
978 case 0x01: p = "EIAJ"; break;
979 case 0x02: p = "A2-M"; break;
980 case 0x03: p = "A2-BG"; break;
981 case 0x04: p = "A2-DK1"; break;
982 case 0x05: p = "A2-DK2"; break;
983 case 0x06: p = "A2-DK3"; break;
984 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
985 case 0x08: p = "AM-L"; break;
986 case 0x09: p = "NICAM-BG"; break;
987 case 0x0a: p = "NICAM-DK"; break;
988 case 0x0b: p = "NICAM-I"; break;
989 case 0x0c: p = "NICAM-L"; break;
990 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
991 case 0x0e: p = "IF FM Radio"; break;
992 case 0x0f: p = "BTSC"; break;
993 case 0x10: p = "high-deviation FM"; break;
994 case 0x11: p = "very high-deviation FM"; break;
995 case 0xfd: p = "unknown audio standard"; break;
996 case 0xfe: p = "forced audio standard"; break;
997 case 0xff: p = "no detected audio standard"; break;
998 default: p = "not defined";
999 }
1000 v4l_info(client, "Detected audio standard: %s\n", p);
1001 v4l_info(client, "Audio muted: %s\n",
1002 (state->unmute_volume >= 0) ? "yes" : "no");
1003 v4l_info(client, "Audio microcontroller: %s\n",
1004 (download_ctl & 0x10) ?
1005 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
1006
1007 switch (audio_config >> 4) {
1008 case 0x00: p = "undefined"; break;
1009 case 0x01: p = "BTSC"; break;
1010 case 0x02: p = "EIAJ"; break;
1011 case 0x03: p = "A2-M"; break;
1012 case 0x04: p = "A2-BG"; break;
1013 case 0x05: p = "A2-DK1"; break;
1014 case 0x06: p = "A2-DK2"; break;
1015 case 0x07: p = "A2-DK3"; break;
1016 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1017 case 0x09: p = "AM-L"; break;
1018 case 0x0a: p = "NICAM-BG"; break;
1019 case 0x0b: p = "NICAM-DK"; break;
1020 case 0x0c: p = "NICAM-I"; break;
1021 case 0x0d: p = "NICAM-L"; break;
1022 case 0x0e: p = "FM radio"; break;
1023 case 0x0f: p = "automatic detection"; break;
1024 default: p = "undefined";
1025 }
1026 v4l_info(client, "Configured audio standard: %s\n", p);
1027
1028 if ((audio_config >> 4) < 0xF) {
1029 switch (audio_config & 0xF) {
1030 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1031 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1032 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1033 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1034 case 0x04: p = "STEREO"; break;
1035 case 0x05: p = "DUAL1 (AB)"; break;
1036 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1037 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1038 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1039 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1040 case 0x0a: p = "SAP"; break;
1041 default: p = "undefined";
1042 }
1043 v4l_info(client, "Configured audio mode: %s\n", p);
1044 } else {
1045 switch (audio_config & 0xF) {
1046 case 0x00: p = "BG"; break;
1047 case 0x01: p = "DK1"; break;
1048 case 0x02: p = "DK2"; break;
1049 case 0x03: p = "DK3"; break;
1050 case 0x04: p = "I"; break;
1051 case 0x05: p = "L"; break;
1052 case 0x06: p = "BTSC"; break;
1053 case 0x07: p = "EIAJ"; break;
1054 case 0x08: p = "A2-M"; break;
1055 case 0x09: p = "FM Radio"; break;
1056 case 0x0f: p = "automatic standard and mode detection"; break;
1057 default: p = "undefined";
1058 }
1059 v4l_info(client, "Configured audio system: %s\n", p);
1060 }
1061
1062 if (aud_input) {
1063 v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
1064 } else {
1065 v4l_info(client, "Specified audio input: External\n");
1066 }
1067
1068 switch (pref_mode & 0xf) {
1069 case 0: p = "mono/language A"; break;
1070 case 1: p = "language B"; break;
1071 case 2: p = "language C"; break;
1072 case 3: p = "analog fallback"; break;
1073 case 4: p = "stereo"; break;
1074 case 5: p = "language AC"; break;
1075 case 6: p = "language BC"; break;
1076 case 7: p = "language AB"; break;
1077 default: p = "undefined";
1078 }
1079 v4l_info(client, "Preferred audio mode: %s\n", p);
1080
1081 if ((audio_config & 0xf) == 0xf) {
1082 switch ((afc0 >> 3) & 0x3) {
1083 case 0: p = "system DK"; break;
1084 case 1: p = "system L"; break;
1085 case 2: p = "autodetect"; break;
1086 default: p = "undefined";
1087 }
1088 v4l_info(client, "Selected 65 MHz format: %s\n", p);
1089
1090 switch (afc0 & 0x7) {
1091 case 0: p = "chroma"; break;
1092 case 1: p = "BTSC"; break;
1093 case 2: p = "EIAJ"; break;
1094 case 3: p = "A2-M"; break;
1095 case 4: p = "autodetect"; break;
1096 default: p = "undefined";
1097 }
1098 v4l_info(client, "Selected 45 MHz format: %s\n", p);
1099 }
1100}
1101
1102/* ----------------------------------------------------------------------- */
1103
Hans Verkuil9357b312008-11-29 12:50:06 -03001104static int cx25840_init(struct v4l2_subdev *sd, u32 val)
Hans Verkuilbd985162005-11-13 16:07:56 -08001105{
Hans Verkuil9357b312008-11-29 12:50:06 -03001106 struct cx25840_state *state = to_state(sd);
1107 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilc976bc82007-07-22 12:52:40 -03001108
1109 if (!state->is_initialized) {
Hans Verkuilc976bc82007-07-22 12:52:40 -03001110 /* initialize on first use */
1111 state->is_initialized = 1;
1112 if (state->is_cx25836)
1113 cx25836_initialize(client);
Steven Tothf2340812008-01-10 01:22:39 -03001114 else if (state->is_cx23885)
1115 cx23885_initialize(client);
Hans Verkuilc976bc82007-07-22 12:52:40 -03001116 else
Hans Verkuil89fc4eb2007-08-04 05:00:07 -03001117 cx25840_initialize(client);
Hans Verkuilc976bc82007-07-22 12:52:40 -03001118 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001119 return 0;
1120}
Hans Verkuilc976bc82007-07-22 12:52:40 -03001121
Hans Verkuilbd985162005-11-13 16:07:56 -08001122#ifdef CONFIG_VIDEO_ADV_DEBUG
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001123static int cx25840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil9357b312008-11-29 12:50:06 -03001124{
1125 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001126
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001127 if (!v4l2_chip_match_i2c_client(client, &reg->match))
Hans Verkuil9357b312008-11-29 12:50:06 -03001128 return -EINVAL;
1129 if (!capable(CAP_SYS_ADMIN))
1130 return -EPERM;
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001131 reg->size = 1;
Hans Verkuil9357b312008-11-29 12:50:06 -03001132 reg->val = cx25840_read(client, reg->reg & 0x0fff);
1133 return 0;
1134}
Steven Tothf2340812008-01-10 01:22:39 -03001135
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001136static int cx25840_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
Hans Verkuil9357b312008-11-29 12:50:06 -03001137{
1138 struct i2c_client *client = v4l2_get_subdevdata(sd);
1139
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001140 if (!v4l2_chip_match_i2c_client(client, &reg->match))
Hans Verkuil9357b312008-11-29 12:50:06 -03001141 return -EINVAL;
1142 if (!capable(CAP_SYS_ADMIN))
1143 return -EPERM;
1144 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
1145 return 0;
1146}
Hans Verkuilbd985162005-11-13 16:07:56 -08001147#endif
1148
Hans Verkuil9357b312008-11-29 12:50:06 -03001149static int cx25840_decode_vbi_line(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi)
1150{
1151 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001152
Hans Verkuil9357b312008-11-29 12:50:06 -03001153 return cx25840_vbi(client, VIDIOC_INT_DECODE_VBI_LINE, vbi);
1154}
Hans Verkuilbd985162005-11-13 16:07:56 -08001155
Hans Verkuil9357b312008-11-29 12:50:06 -03001156static int cx25840_s_clock_freq(struct v4l2_subdev *sd, u32 freq)
1157{
1158 struct i2c_client *client = v4l2_get_subdevdata(sd);
1159
1160 return cx25840_audio(client, VIDIOC_INT_AUDIO_CLOCK_FREQ, &freq);
1161}
1162
1163static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
1164{
1165 struct cx25840_state *state = to_state(sd);
1166 struct i2c_client *client = v4l2_get_subdevdata(sd);
1167
1168 v4l_dbg(1, cx25840_debug, client, "%s output\n",
1169 enable ? "enable" : "disable");
1170 if (enable) {
Steven Tothf2340812008-01-10 01:22:39 -03001171 if (state->is_cx23885) {
1172 u8 v = (cx25840_read(client, 0x421) | 0x0b);
1173 cx25840_write(client, 0x421, v);
1174 } else {
1175 cx25840_write(client, 0x115,
Hans Verkuil9357b312008-11-29 12:50:06 -03001176 state->is_cx25836 ? 0x0c : 0x8c);
Steven Tothf2340812008-01-10 01:22:39 -03001177 cx25840_write(client, 0x116,
Hans Verkuil9357b312008-11-29 12:50:06 -03001178 state->is_cx25836 ? 0x04 : 0x07);
Steven Tothf2340812008-01-10 01:22:39 -03001179 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001180 } else {
Steven Tothf2340812008-01-10 01:22:39 -03001181 if (state->is_cx23885) {
1182 u8 v = cx25840_read(client, 0x421) & ~(0x0b);
1183 cx25840_write(client, 0x421, v);
1184 } else {
1185 cx25840_write(client, 0x115, 0x00);
1186 cx25840_write(client, 0x116, 0x00);
1187 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001188 }
1189 return 0;
1190}
1191
1192static int cx25840_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1193{
1194 struct cx25840_state *state = to_state(sd);
1195
1196 switch (qc->id) {
1197 case V4L2_CID_BRIGHTNESS:
1198 case V4L2_CID_CONTRAST:
1199 case V4L2_CID_SATURATION:
1200 case V4L2_CID_HUE:
1201 return v4l2_ctrl_query_fill_std(qc);
1202 default:
Hans Verkuilbd985162005-11-13 16:07:56 -08001203 break;
Hans Verkuil9357b312008-11-29 12:50:06 -03001204 }
1205 if (state->is_cx25836)
1206 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -08001207
Hans Verkuil9357b312008-11-29 12:50:06 -03001208 switch (qc->id) {
1209 case V4L2_CID_AUDIO_VOLUME:
1210 return v4l2_ctrl_query_fill(qc, 0, 65535,
1211 65535 / 100, state->default_volume);
1212 case V4L2_CID_AUDIO_MUTE:
1213 case V4L2_CID_AUDIO_BALANCE:
1214 case V4L2_CID_AUDIO_BASS:
1215 case V4L2_CID_AUDIO_TREBLE:
1216 return v4l2_ctrl_query_fill_std(qc);
1217 default:
Hans Verkuild92c20e2006-01-09 15:32:41 -02001218 return -EINVAL;
1219 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001220 return -EINVAL;
1221}
Hans Verkuild92c20e2006-01-09 15:32:41 -02001222
Hans Verkuil9357b312008-11-29 12:50:06 -03001223static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1224{
1225 struct cx25840_state *state = to_state(sd);
1226 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001227
Hans Verkuil9357b312008-11-29 12:50:06 -03001228 if (state->radio == 0 && state->std == std)
1229 return 0;
1230 state->radio = 0;
1231 state->std = std;
1232 return set_v4lstd(client);
1233}
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001234
Hans Verkuil9357b312008-11-29 12:50:06 -03001235static int cx25840_s_radio(struct v4l2_subdev *sd)
1236{
1237 struct cx25840_state *state = to_state(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001238
Hans Verkuil9357b312008-11-29 12:50:06 -03001239 state->radio = 1;
1240 return 0;
1241}
Hans Verkuilbd985162005-11-13 16:07:56 -08001242
Hans Verkuil9357b312008-11-29 12:50:06 -03001243static int cx25840_s_video_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
1244{
1245 struct cx25840_state *state = to_state(sd);
1246 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001247
Hans Verkuil9357b312008-11-29 12:50:06 -03001248 return set_input(client, route->input, state->aud_input);
1249}
Hans Verkuila8bbf122006-01-09 15:25:42 -02001250
Hans Verkuil9357b312008-11-29 12:50:06 -03001251static int cx25840_s_audio_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
1252{
1253 struct cx25840_state *state = to_state(sd);
1254 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuila8bbf122006-01-09 15:25:42 -02001255
Hans Verkuil9357b312008-11-29 12:50:06 -03001256 if (state->is_cx25836)
1257 return -EINVAL;
1258 return set_input(client, state->vid_input, route->input);
1259}
Hans Verkuilbd985162005-11-13 16:07:56 -08001260
Hans Verkuil9357b312008-11-29 12:50:06 -03001261static int cx25840_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
1262{
1263 struct cx25840_state *state = to_state(sd);
1264 struct i2c_client *client = v4l2_get_subdevdata(sd);
Hans Verkuilbd985162005-11-13 16:07:56 -08001265
Hans Verkuil9357b312008-11-29 12:50:06 -03001266 if (!state->is_cx25836)
1267 input_change(client);
1268 return 0;
1269}
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001270
Hans Verkuil9357b312008-11-29 12:50:06 -03001271static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1272{
1273 struct cx25840_state *state = to_state(sd);
1274 struct i2c_client *client = v4l2_get_subdevdata(sd);
1275 u8 vpres = cx25840_read(client, 0x40e) & 0x20;
1276 u8 mode;
1277 int val = 0;
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001278
Hans Verkuil9357b312008-11-29 12:50:06 -03001279 if (state->radio)
1280 return 0;
Hans Verkuilbd985162005-11-13 16:07:56 -08001281
Hans Verkuil9357b312008-11-29 12:50:06 -03001282 vt->signal = vpres ? 0xffff : 0x0;
1283 if (state->is_cx25836)
1284 return 0;
Hans Verkuilbd985162005-11-13 16:07:56 -08001285
Hans Verkuil9357b312008-11-29 12:50:06 -03001286 vt->capability |=
1287 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
1288 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
Hans Verkuilbd985162005-11-13 16:07:56 -08001289
Hans Verkuil9357b312008-11-29 12:50:06 -03001290 mode = cx25840_read(client, 0x804);
Hans Verkuilbd985162005-11-13 16:07:56 -08001291
Hans Verkuil9357b312008-11-29 12:50:06 -03001292 /* get rxsubchans and audmode */
1293 if ((mode & 0xf) == 1)
1294 val |= V4L2_TUNER_SUB_STEREO;
1295 else
1296 val |= V4L2_TUNER_SUB_MONO;
Hans Verkuilbd985162005-11-13 16:07:56 -08001297
Hans Verkuil9357b312008-11-29 12:50:06 -03001298 if (mode == 2 || mode == 4)
1299 val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuilbd985162005-11-13 16:07:56 -08001300
Hans Verkuil9357b312008-11-29 12:50:06 -03001301 if (mode & 0x10)
1302 val |= V4L2_TUNER_SUB_SAP;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001303
Hans Verkuil9357b312008-11-29 12:50:06 -03001304 vt->rxsubchans = val;
1305 vt->audmode = state->audmode;
1306 return 0;
1307}
1308
1309static int cx25840_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1310{
1311 struct cx25840_state *state = to_state(sd);
1312 struct i2c_client *client = v4l2_get_subdevdata(sd);
1313
1314 if (state->radio || state->is_cx25836)
1315 return 0;
1316
1317 switch (vt->audmode) {
Hans Verkuilbd985162005-11-13 16:07:56 -08001318 case V4L2_TUNER_MODE_MONO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001319 /* mono -> mono
1320 stereo -> mono
1321 bilingual -> lang1 */
Hans Verkuilbd985162005-11-13 16:07:56 -08001322 cx25840_and_or(client, 0x809, ~0xf, 0x00);
1323 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -03001324 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001325 case V4L2_TUNER_MODE_LANG1:
1326 /* mono -> mono
1327 stereo -> stereo
1328 bilingual -> lang1 */
Hans Verkuilbd985162005-11-13 16:07:56 -08001329 cx25840_and_or(client, 0x809, ~0xf, 0x04);
1330 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -03001331 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001332 /* mono -> mono
1333 stereo -> stereo
1334 bilingual -> lang1/lang2 */
1335 cx25840_and_or(client, 0x809, ~0xf, 0x07);
1336 break;
Hans Verkuilbd985162005-11-13 16:07:56 -08001337 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001338 /* mono -> mono
Hans Verkuil301e22d2006-03-18 17:15:00 -03001339 stereo -> stereo
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001340 bilingual -> lang2 */
Hans Verkuilbd985162005-11-13 16:07:56 -08001341 cx25840_and_or(client, 0x809, ~0xf, 0x01);
1342 break;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001343 default:
1344 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -08001345 }
Hans Verkuil9357b312008-11-29 12:50:06 -03001346 state->audmode = vt->audmode;
Hans Verkuil3faeeae2006-01-09 15:25:44 -02001347 return 0;
Hans Verkuilbd985162005-11-13 16:07:56 -08001348}
1349
Hans Verkuil9357b312008-11-29 12:50:06 -03001350static int cx25840_reset(struct v4l2_subdev *sd, u32 val)
1351{
1352 struct cx25840_state *state = to_state(sd);
1353 struct i2c_client *client = v4l2_get_subdevdata(sd);
1354
1355 if (state->is_cx25836)
1356 cx25836_initialize(client);
1357 else if (state->is_cx23885)
1358 cx23885_initialize(client);
1359 else
1360 cx25840_initialize(client);
1361 return 0;
1362}
1363
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001364static int cx25840_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
Hans Verkuil9357b312008-11-29 12:50:06 -03001365{
1366 struct cx25840_state *state = to_state(sd);
1367 struct i2c_client *client = v4l2_get_subdevdata(sd);
1368
1369 return v4l2_chip_ident_i2c_client(client, chip, state->id, state->rev);
1370}
1371
1372static int cx25840_log_status(struct v4l2_subdev *sd)
1373{
1374 struct cx25840_state *state = to_state(sd);
1375 struct i2c_client *client = v4l2_get_subdevdata(sd);
1376
1377 log_video_status(client);
1378 if (!state->is_cx25836)
1379 log_audio_status(client);
1380 return 0;
1381}
1382
1383static int cx25840_command(struct i2c_client *client, unsigned cmd, void *arg)
1384{
1385 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
1386}
1387
1388/* ----------------------------------------------------------------------- */
1389
1390static const struct v4l2_subdev_core_ops cx25840_core_ops = {
1391 .log_status = cx25840_log_status,
1392 .g_chip_ident = cx25840_g_chip_ident,
1393 .g_ctrl = cx25840_g_ctrl,
1394 .s_ctrl = cx25840_s_ctrl,
1395 .queryctrl = cx25840_queryctrl,
1396 .reset = cx25840_reset,
1397 .init = cx25840_init,
1398#ifdef CONFIG_VIDEO_ADV_DEBUG
1399 .g_register = cx25840_g_register,
1400 .s_register = cx25840_s_register,
1401#endif
1402};
1403
1404static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
1405 .s_frequency = cx25840_s_frequency,
1406 .s_std = cx25840_s_std,
1407 .s_radio = cx25840_s_radio,
1408 .g_tuner = cx25840_g_tuner,
1409 .s_tuner = cx25840_s_tuner,
1410};
1411
1412static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {
1413 .s_clock_freq = cx25840_s_clock_freq,
1414 .s_routing = cx25840_s_audio_routing,
1415};
1416
1417static const struct v4l2_subdev_video_ops cx25840_video_ops = {
1418 .s_routing = cx25840_s_video_routing,
1419 .g_fmt = cx25840_g_fmt,
1420 .s_fmt = cx25840_s_fmt,
1421 .decode_vbi_line = cx25840_decode_vbi_line,
1422 .s_stream = cx25840_s_stream,
1423};
1424
1425static const struct v4l2_subdev_ops cx25840_ops = {
1426 .core = &cx25840_core_ops,
1427 .tuner = &cx25840_tuner_ops,
1428 .audio = &cx25840_audio_ops,
1429 .video = &cx25840_video_ops,
1430};
1431
Hans Verkuilbd985162005-11-13 16:07:56 -08001432/* ----------------------------------------------------------------------- */
1433
Jean Delvared2653e92008-04-29 23:11:39 +02001434static int cx25840_probe(struct i2c_client *client,
1435 const struct i2c_device_id *did)
Hans Verkuilbd985162005-11-13 16:07:56 -08001436{
Hans Verkuilbd985162005-11-13 16:07:56 -08001437 struct cx25840_state *state;
Hans Verkuil9357b312008-11-29 12:50:06 -03001438 struct v4l2_subdev *sd;
Hans Verkuil3434eb72007-04-27 12:31:08 -03001439 u32 id;
Hans Verkuilbd985162005-11-13 16:07:56 -08001440 u16 device_id;
1441
Hans Verkuil188f3452007-09-16 10:47:15 -03001442 /* Check if the adapter supports the needed features */
1443 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1444 return -EIO;
1445
Hans Verkuil21340ae2007-08-26 10:53:16 -03001446 v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
Hans Verkuilbd985162005-11-13 16:07:56 -08001447
1448 device_id = cx25840_read(client, 0x101) << 8;
1449 device_id |= cx25840_read(client, 0x100);
Steven Tothf2340812008-01-10 01:22:39 -03001450 v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
Hans Verkuilbd985162005-11-13 16:07:56 -08001451
1452 /* The high byte of the device ID should be
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001453 * 0x83 for the cx2583x and 0x84 for the cx2584x */
1454 if ((device_id & 0xff00) == 0x8300) {
1455 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001456 }
1457 else if ((device_id & 0xff00) == 0x8400) {
1458 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
Steven Tothf2340812008-01-10 01:22:39 -03001459 } else if (device_id == 0x0000) {
1460 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
1461 } else if (device_id == 0x1313) {
1462 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001463 }
1464 else {
Hans Verkuilb5fc7142006-01-11 22:41:36 -02001465 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
Hans Verkuil188f3452007-09-16 10:47:15 -03001466 return -ENODEV;
Hans Verkuilbd985162005-11-13 16:07:56 -08001467 }
1468
Hans Verkuil21340ae2007-08-26 10:53:16 -03001469 state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
Hans Verkuil9357b312008-11-29 12:50:06 -03001470 if (state == NULL)
Hans Verkuil21340ae2007-08-26 10:53:16 -03001471 return -ENOMEM;
Hans Verkuil21340ae2007-08-26 10:53:16 -03001472
Hans Verkuil9357b312008-11-29 12:50:06 -03001473 sd = &state->sd;
1474 v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
Hans Verkuilb7a01e72007-02-02 20:49:54 -03001475 /* Note: revision '(device_id & 0x0f) == 2' was never built. The
1476 marking skips from 0x1 == 22 to 0x3 == 23. */
Hans Verkuilfac9e892006-01-09 15:32:40 -02001477 v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
Hans Verkuilbd985162005-11-13 16:07:56 -08001478 (device_id & 0xfff0) >> 4,
Hans Verkuilb7a01e72007-02-02 20:49:54 -03001479 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : (device_id & 0x0f),
Hans Verkuil21340ae2007-08-26 10:53:16 -03001480 client->addr << 1, client->adapter->name);
Hans Verkuilbd985162005-11-13 16:07:56 -08001481
Hans Verkuil21340ae2007-08-26 10:53:16 -03001482 state->c = client;
1483 state->is_cx25836 = ((device_id & 0xff00) == 0x8300);
Steven Tothf2340812008-01-10 01:22:39 -03001484 state->is_cx23885 = (device_id == 0x0000) || (device_id == 0x1313);
Hans Verkuila8bbf122006-01-09 15:25:42 -02001485 state->vid_input = CX25840_COMPOSITE7;
1486 state->aud_input = CX25840_AUDIO8;
Hans Verkuil3578d3d2006-01-09 15:25:41 -02001487 state->audclk_freq = 48000;
Hans Verkuila8bbf122006-01-09 15:25:42 -02001488 state->pvr150_workaround = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -02001489 state->audmode = V4L2_TUNER_MODE_LANG1;
Hans Verkuil87410da2007-08-05 08:00:36 -03001490 state->unmute_volume = -1;
Hans Verkuilca130ee2008-07-17 12:26:45 -03001491 state->default_volume = 228 - cx25840_read(client, 0x8d4);
1492 state->default_volume = ((state->default_volume / 2) + 23) << 9;
Christopher Neufeld3e3bf272006-05-24 10:16:45 -03001493 state->vbi_line_offset = 8;
Hans Verkuile2b8cf42006-04-22 10:22:46 -03001494 state->id = id;
Hans Verkuil3434eb72007-04-27 12:31:08 -03001495 state->rev = device_id;
Steven Tothf2340812008-01-10 01:22:39 -03001496
Steven Toth2770b7d2008-04-19 01:18:47 -03001497 if (state->is_cx23885) {
1498 /* Drive GPIO2 direction and values */
1499 cx25840_write(client, 0x160, 0x1d);
1500 cx25840_write(client, 0x164, 0x00);
1501 }
1502
Hans Verkuilbd985162005-11-13 16:07:56 -08001503 return 0;
1504}
1505
Hans Verkuil1a392752007-09-13 11:44:47 -03001506static int cx25840_remove(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -08001507{
Hans Verkuil9357b312008-11-29 12:50:06 -03001508 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1509
1510 v4l2_device_unregister_subdev(sd);
1511 kfree(to_state(sd));
Hans Verkuilbd985162005-11-13 16:07:56 -08001512 return 0;
1513}
1514
Jean Delvareaf294862008-05-18 20:49:40 +02001515static const struct i2c_device_id cx25840_id[] = {
1516 { "cx25840", 0 },
1517 { }
1518};
1519MODULE_DEVICE_TABLE(i2c, cx25840_id);
1520
Hans Verkuil1a392752007-09-13 11:44:47 -03001521static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1522 .name = "cx25840",
1523 .driverid = I2C_DRIVERID_CX25840,
Hans Verkuilbd985162005-11-13 16:07:56 -08001524 .command = cx25840_command,
Hans Verkuil1a392752007-09-13 11:44:47 -03001525 .probe = cx25840_probe,
1526 .remove = cx25840_remove,
Jean Delvareaf294862008-05-18 20:49:40 +02001527 .id_table = cx25840_id,
Hans Verkuilbd985162005-11-13 16:07:56 -08001528};