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