]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/cx25840/cx25840-core.c
V4L/DVB: v4l2_subdev: Add s_io_pin_config to v4l2_subdev_core_ops
[mirror_ubuntu-artful-kernel.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 *
6d897616 16 * CX23885 support by Steven Toth <stoth@linuxtv.org>.
f234081b 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>
b6198ade 42#include <media/v4l2-i2c-drv.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
fe0d3dff 51static int cx25840_debug;
bd985160 52
b5fc7144 53module_param_named(debug,cx25840_debug, int, 0644);
bd985160 54
fac9e899 55MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
bd985160 56
bd985160
HV
57
58/* ----------------------------------------------------------------------- */
59
60int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
61{
62 u8 buffer[3];
63 buffer[0] = addr >> 8;
64 buffer[1] = addr & 0xff;
65 buffer[2] = value;
66 return i2c_master_send(client, buffer, 3);
67}
68
69int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
70{
71 u8 buffer[6];
72 buffer[0] = addr >> 8;
73 buffer[1] = addr & 0xff;
4a56eb3f
HV
74 buffer[2] = value & 0xff;
75 buffer[3] = (value >> 8) & 0xff;
76 buffer[4] = (value >> 16) & 0xff;
77 buffer[5] = value >> 24;
bd985160
HV
78 return i2c_master_send(client, buffer, 6);
79}
80
81u8 cx25840_read(struct i2c_client * client, u16 addr)
82{
5f272644
AW
83 struct i2c_msg msgs[2];
84 u8 tx_buf[2], rx_buf[1];
85
86 /* Write register address */
87 tx_buf[0] = addr >> 8;
88 tx_buf[1] = addr & 0xff;
89 msgs[0].addr = client->addr;
90 msgs[0].flags = 0;
91 msgs[0].len = 2;
92 msgs[0].buf = (char *) tx_buf;
93
94 /* Read data from register */
95 msgs[1].addr = client->addr;
96 msgs[1].flags = I2C_M_RD;
97 msgs[1].len = 1;
98 msgs[1].buf = (char *) rx_buf;
99
100 if (i2c_transfer(client->adapter, msgs, 2) < 2)
bd985160
HV
101 return 0;
102
5f272644 103 return rx_buf[0];
bd985160
HV
104}
105
106u32 cx25840_read4(struct i2c_client * client, u16 addr)
107{
5f272644
AW
108 struct i2c_msg msgs[2];
109 u8 tx_buf[2], rx_buf[4];
110
111 /* Write register address */
112 tx_buf[0] = addr >> 8;
113 tx_buf[1] = addr & 0xff;
114 msgs[0].addr = client->addr;
115 msgs[0].flags = 0;
116 msgs[0].len = 2;
117 msgs[0].buf = (char *) tx_buf;
118
119 /* Read data from registers */
120 msgs[1].addr = client->addr;
121 msgs[1].flags = I2C_M_RD;
122 msgs[1].len = 4;
123 msgs[1].buf = (char *) rx_buf;
124
125 if (i2c_transfer(client->adapter, msgs, 2) < 2)
bd985160
HV
126 return 0;
127
5f272644
AW
128 return (rx_buf[3] << 24) | (rx_buf[2] << 16) | (rx_buf[1] << 8) |
129 rx_buf[0];
bd985160
HV
130}
131
e2b8cf4c 132int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
bd985160
HV
133 u8 or_value)
134{
135 return cx25840_write(client, addr,
136 (cx25840_read(client, addr) & and_mask) |
137 or_value);
138}
139
140/* ----------------------------------------------------------------------- */
141
a8bbf12a
HV
142static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
143 enum cx25840_audio_input aud_input);
bd985160
HV
144
145/* ----------------------------------------------------------------------- */
146
d92c20e0 147static void init_dll1(struct i2c_client *client)
bd985160
HV
148{
149 /* This is the Hauppauge sequence used to
150 * initialize the Delay Lock Loop 1 (ADC DLL). */
151 cx25840_write(client, 0x159, 0x23);
152 cx25840_write(client, 0x15a, 0x87);
153 cx25840_write(client, 0x15b, 0x06);
38051450 154 udelay(10);
bd985160 155 cx25840_write(client, 0x159, 0xe1);
38051450 156 udelay(10);
bd985160
HV
157 cx25840_write(client, 0x15a, 0x86);
158 cx25840_write(client, 0x159, 0xe0);
159 cx25840_write(client, 0x159, 0xe1);
160 cx25840_write(client, 0x15b, 0x10);
161}
162
d92c20e0 163static void init_dll2(struct i2c_client *client)
bd985160
HV
164{
165 /* This is the Hauppauge sequence used to
166 * initialize the Delay Lock Loop 2 (ADC DLL). */
167 cx25840_write(client, 0x15d, 0xe3);
168 cx25840_write(client, 0x15e, 0x86);
169 cx25840_write(client, 0x15f, 0x06);
38051450 170 udelay(10);
bd985160
HV
171 cx25840_write(client, 0x15d, 0xe1);
172 cx25840_write(client, 0x15d, 0xe0);
173 cx25840_write(client, 0x15d, 0xe1);
174}
175
e2b8cf4c
HV
176static void cx25836_initialize(struct i2c_client *client)
177{
178 /* reset configuration is described on page 3-77 of the CX25836 datasheet */
179 /* 2. */
180 cx25840_and_or(client, 0x000, ~0x01, 0x01);
181 cx25840_and_or(client, 0x000, ~0x01, 0x00);
182 /* 3a. */
183 cx25840_and_or(client, 0x15a, ~0x70, 0x00);
184 /* 3b. */
185 cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
186 /* 3c. */
187 cx25840_and_or(client, 0x159, ~0x02, 0x02);
188 /* 3d. */
38051450 189 udelay(10);
e2b8cf4c
HV
190 /* 3e. */
191 cx25840_and_or(client, 0x159, ~0x02, 0x00);
192 /* 3f. */
193 cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
194 /* 3g. */
195 cx25840_and_or(client, 0x159, ~0x01, 0x00);
196 cx25840_and_or(client, 0x159, ~0x01, 0x01);
197 /* 3h. */
198 cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
199}
200
21340ae0
HV
201static void cx25840_work_handler(struct work_struct *work)
202{
203 struct cx25840_state *state = container_of(work, struct cx25840_state, fw_work);
204 cx25840_loadfw(state->c);
205 wake_up(&state->fw_wait);
206}
207
89fc4eb9 208static void cx25840_initialize(struct i2c_client *client)
bd985160 209{
21340ae0 210 DEFINE_WAIT(wait);
9357b31c 211 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
21340ae0 212 struct workqueue_struct *q;
bd985160
HV
213
214 /* datasheet startup in numbered steps, refer to page 3-77 */
215 /* 2. */
216 cx25840_and_or(client, 0x803, ~0x10, 0x00);
217 /* The default of this register should be 4, but I get 0 instead.
218 * Set this register to 4 manually. */
219 cx25840_write(client, 0x000, 0x04);
220 /* 3. */
221 init_dll1(client);
222 init_dll2(client);
223 cx25840_write(client, 0x136, 0x0a);
224 /* 4. */
225 cx25840_write(client, 0x13c, 0x01);
226 cx25840_write(client, 0x13c, 0x00);
227 /* 5. */
21340ae0
HV
228 /* Do the firmware load in a work handler to prevent.
229 Otherwise the kernel is blocked waiting for the
230 bit-banging i2c interface to finish uploading the
231 firmware. */
232 INIT_WORK(&state->fw_work, cx25840_work_handler);
233 init_waitqueue_head(&state->fw_wait);
234 q = create_singlethread_workqueue("cx25840_fw");
235 prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
236 queue_work(q, &state->fw_work);
237 schedule();
238 finish_wait(&state->fw_wait, &wait);
239 destroy_workqueue(q);
240
bd985160
HV
241 /* 6. */
242 cx25840_write(client, 0x115, 0x8c);
243 cx25840_write(client, 0x116, 0x07);
244 cx25840_write(client, 0x118, 0x02);
245 /* 7. */
246 cx25840_write(client, 0x4a5, 0x80);
247 cx25840_write(client, 0x4a5, 0x00);
248 cx25840_write(client, 0x402, 0x00);
249 /* 8. */
73dcddc5
HV
250 cx25840_and_or(client, 0x401, ~0x18, 0);
251 cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
252 /* steps 8c and 8d are done in change_input() */
bd985160
HV
253 /* 10. */
254 cx25840_write(client, 0x8d3, 0x1f);
255 cx25840_write(client, 0x8e3, 0x03);
256
cb5aa1c6 257 cx25840_std_setup(client);
bd985160
HV
258
259 /* trial and error says these are needed to get audio */
260 cx25840_write(client, 0x914, 0xa0);
261 cx25840_write(client, 0x918, 0xa0);
262 cx25840_write(client, 0x919, 0x01);
263
264 /* stereo prefered */
265 cx25840_write(client, 0x809, 0x04);
266 /* AC97 shift */
267 cx25840_write(client, 0x8cf, 0x0f);
268
a8bbf12a
HV
269 /* (re)set input */
270 set_input(client, state->vid_input, state->aud_input);
bd985160
HV
271
272 /* start microcontroller */
273 cx25840_and_or(client, 0x803, ~0x10, 0x10);
274}
275
f234081b
ST
276static void cx23885_initialize(struct i2c_client *client)
277{
278 DEFINE_WAIT(wait);
9357b31c 279 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
f234081b
ST
280 struct workqueue_struct *q;
281
e283d780
AW
282 /*
283 * Come out of digital power down
284 * The CX23888, at least, needs this, otherwise registers aside from
285 * 0x0-0x2 can't be read or written.
286 */
287 cx25840_write(client, 0x000, 0);
288
f234081b
ST
289 /* Internal Reset */
290 cx25840_and_or(client, 0x102, ~0x01, 0x01);
291 cx25840_and_or(client, 0x102, ~0x01, 0x00);
292
293 /* Stop microcontroller */
294 cx25840_and_or(client, 0x803, ~0x10, 0x00);
295
296 /* DIF in reset? */
297 cx25840_write(client, 0x398, 0);
298
e283d780
AW
299 /*
300 * Trust the default xtal, no division
301 * '885: 28.636363... MHz
302 * '887: 25.000000 MHz
303 * '888: 50.000000 MHz
304 */
f234081b
ST
305 cx25840_write(client, 0x2, 0x76);
306
e283d780 307 /* Power up all the PLL's and DLL */
f234081b
ST
308 cx25840_write(client, 0x1, 0x40);
309
e283d780
AW
310 /* Sys PLL */
311 switch (state->id) {
312 case V4L2_IDENT_CX23888_AV:
313 /*
314 * 50.0 MHz * (0xb + 0xe8ba26/0x2000000)/4 = 5 * 28.636363 MHz
315 * 572.73 MHz before post divide
316 */
317 cx25840_write4(client, 0x11c, 0x00e8ba26);
318 cx25840_write4(client, 0x118, 0x0000040b);
319 break;
320 case V4L2_IDENT_CX23887_AV:
321 /*
322 * 25.0 MHz * (0x16 + 0x1d1744c/0x2000000)/4 = 5 * 28.636363 MHz
323 * 572.73 MHz before post divide
324 */
325 cx25840_write4(client, 0x11c, 0x01d1744c);
326 cx25840_write4(client, 0x118, 0x00000416);
327 break;
328 case V4L2_IDENT_CX23885_AV:
329 default:
330 /*
331 * 28.636363 MHz * (0x14 + 0x0/0x2000000)/4 = 5 * 28.636363 MHz
332 * 572.73 MHz before post divide
333 */
334 cx25840_write4(client, 0x11c, 0x00000000);
335 cx25840_write4(client, 0x118, 0x00000414);
336 break;
337 }
f234081b
ST
338
339 /* Disable DIF bypass */
340 cx25840_write4(client, 0x33c, 0x00000001);
341
342 /* DIF Src phase inc */
343 cx25840_write4(client, 0x340, 0x0df7df83);
344
e283d780
AW
345 /*
346 * Vid PLL
347 * Setup for a BT.656 pixel clock of 13.5 Mpixels/second
348 *
349 * 28.636363 MHz * (0xf + 0x02be2c9/0x2000000)/4 = 8 * 13.5 MHz
350 * 432.0 MHz before post divide
351 */
352 cx25840_write4(client, 0x10c, 0x002be2c9);
353 cx25840_write4(client, 0x108, 0x0000040f);
f234081b
ST
354
355 /* Luma */
356 cx25840_write4(client, 0x414, 0x00107d12);
357
358 /* Chroma */
359 cx25840_write4(client, 0x420, 0x3d008282);
360
e283d780
AW
361 /*
362 * Aux PLL
363 * Initial setup for audio sample clock:
364 * 48 ksps, 16 bits/sample, x160 multiplier = 122.88 MHz
365 * Intial I2S output/master clock(?):
366 * 48 ksps, 16 bits/sample, x16 multiplier = 12.288 MHz
367 */
368 switch (state->id) {
369 case V4L2_IDENT_CX23888_AV:
370 /*
371 * 50.0 MHz * (0x7 + 0x0bedfa4/0x2000000)/3 = 122.88 MHz
372 * 368.64 MHz before post divide
373 * 122.88 MHz / 0xa = 12.288 MHz
374 */
375 cx25840_write4(client, 0x114, 0x00bedfa4);
376 cx25840_write4(client, 0x110, 0x000a0307);
377 break;
378 case V4L2_IDENT_CX23887_AV:
379 /*
380 * 25.0 MHz * (0xe + 0x17dbf48/0x2000000)/3 = 122.88 MHz
381 * 368.64 MHz before post divide
382 * 122.88 MHz / 0xa = 12.288 MHz
383 */
384 cx25840_write4(client, 0x114, 0x017dbf48);
385 cx25840_write4(client, 0x110, 0x000a030e);
386 break;
387 case V4L2_IDENT_CX23885_AV:
388 default:
389 /*
390 * 28.636363 MHz * (0xc + 0x1bf0c9e/0x2000000)/3 = 122.88 MHz
391 * 368.64 MHz before post divide
392 * 122.88 MHz / 0xa = 12.288 MHz
393 */
394 cx25840_write4(client, 0x114, 0x01bf0c9e);
395 cx25840_write4(client, 0x110, 0x000a030c);
396 break;
397 };
f234081b
ST
398
399 /* ADC2 input select */
400 cx25840_write(client, 0x102, 0x10);
401
402 /* VIN1 & VIN5 */
403 cx25840_write(client, 0x103, 0x11);
404
405 /* Enable format auto detect */
406 cx25840_write(client, 0x400, 0);
407 /* Fast subchroma lock */
408 /* White crush, Chroma AGC & Chroma Killer enabled */
409 cx25840_write(client, 0x401, 0xe8);
410
411 /* Select AFE clock pad output source */
412 cx25840_write(client, 0x144, 0x05);
413
f3d6f633
ST
414 /* Drive GPIO2 direction and values for HVR1700
415 * where an onboard mux selects the output of demodulator
416 * vs the 417. Failure to set this results in no DTV.
417 * It's safe to set this across all Hauppauge boards
418 * currently, regardless of the board type.
419 */
420 cx25840_write(client, 0x160, 0x1d);
421 cx25840_write(client, 0x164, 0x00);
422
f234081b
ST
423 /* Do the firmware load in a work handler to prevent.
424 Otherwise the kernel is blocked waiting for the
425 bit-banging i2c interface to finish uploading the
426 firmware. */
427 INIT_WORK(&state->fw_work, cx25840_work_handler);
428 init_waitqueue_head(&state->fw_wait);
429 q = create_singlethread_workqueue("cx25840_fw");
430 prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
431 queue_work(q, &state->fw_work);
432 schedule();
433 finish_wait(&state->fw_wait, &wait);
434 destroy_workqueue(q);
435
cb5aa1c6 436 cx25840_std_setup(client);
f234081b
ST
437
438 /* (re)set input */
439 set_input(client, state->vid_input, state->aud_input);
440
441 /* start microcontroller */
442 cx25840_and_or(client, 0x803, ~0x10, 0x10);
443}
444
bd985160
HV
445/* ----------------------------------------------------------------------- */
446
149783b5
SD
447static void cx231xx_initialize(struct i2c_client *client)
448{
449 DEFINE_WAIT(wait);
450 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
451 struct workqueue_struct *q;
452
453 /* Internal Reset */
454 cx25840_and_or(client, 0x102, ~0x01, 0x01);
455 cx25840_and_or(client, 0x102, ~0x01, 0x00);
456
457 /* Stop microcontroller */
458 cx25840_and_or(client, 0x803, ~0x10, 0x00);
459
460 /* DIF in reset? */
461 cx25840_write(client, 0x398, 0);
462
463 /* Trust the default xtal, no division */
464 /* This changes for the cx23888 products */
465 cx25840_write(client, 0x2, 0x76);
466
467 /* Bring down the regulator for AUX clk */
468 cx25840_write(client, 0x1, 0x40);
469
470 /* Disable DIF bypass */
471 cx25840_write4(client, 0x33c, 0x00000001);
472
473 /* DIF Src phase inc */
474 cx25840_write4(client, 0x340, 0x0df7df83);
475
149783b5
SD
476 /* Luma */
477 cx25840_write4(client, 0x414, 0x00107d12);
478
479 /* Chroma */
480 cx25840_write4(client, 0x420, 0x3d008282);
481
149783b5
SD
482 /* ADC2 input select */
483 cx25840_write(client, 0x102, 0x10);
484
485 /* VIN1 & VIN5 */
486 cx25840_write(client, 0x103, 0x11);
487
488 /* Enable format auto detect */
489 cx25840_write(client, 0x400, 0);
490 /* Fast subchroma lock */
491 /* White crush, Chroma AGC & Chroma Killer enabled */
492 cx25840_write(client, 0x401, 0xe8);
493
149783b5
SD
494 /* Do the firmware load in a work handler to prevent.
495 Otherwise the kernel is blocked waiting for the
496 bit-banging i2c interface to finish uploading the
497 firmware. */
498 INIT_WORK(&state->fw_work, cx25840_work_handler);
499 init_waitqueue_head(&state->fw_wait);
500 q = create_singlethread_workqueue("cx25840_fw");
501 prepare_to_wait(&state->fw_wait, &wait, TASK_UNINTERRUPTIBLE);
502 queue_work(q, &state->fw_work);
503 schedule();
504 finish_wait(&state->fw_wait, &wait);
505 destroy_workqueue(q);
506
507 cx25840_std_setup(client);
508
509 /* (re)set input */
510 set_input(client, state->vid_input, state->aud_input);
511
512 /* start microcontroller */
513 cx25840_and_or(client, 0x803, ~0x10, 0x10);
514}
515
516/* ----------------------------------------------------------------------- */
517
cb5aa1c6
HV
518void cx25840_std_setup(struct i2c_client *client)
519{
9357b31c 520 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
cb5aa1c6
HV
521 v4l2_std_id std = state->std;
522 int hblank, hactive, burst, vblank, vactive, sc;
523 int vblank656, src_decimation;
524 int luma_lpf, uv_lpf, comb;
525 u32 pll_int, pll_frac, pll_post;
526
527 /* datasheet startup, step 8d */
528 if (std & ~V4L2_STD_NTSC)
529 cx25840_write(client, 0x49f, 0x11);
530 else
531 cx25840_write(client, 0x49f, 0x14);
532
533 if (std & V4L2_STD_625_50) {
534 hblank = 132;
535 hactive = 720;
536 burst = 93;
537 vblank = 36;
538 vactive = 580;
539 vblank656 = 40;
540 src_decimation = 0x21f;
541 luma_lpf = 2;
542
543 if (std & V4L2_STD_SECAM) {
544 uv_lpf = 0;
545 comb = 0;
546 sc = 0x0a425f;
547 } else if (std == V4L2_STD_PAL_Nc) {
548 uv_lpf = 1;
549 comb = 0x20;
550 sc = 556453;
551 } else {
552 uv_lpf = 1;
553 comb = 0x20;
554 sc = 688739;
555 }
556 } else {
557 hactive = 720;
558 hblank = 122;
559 vactive = 487;
560 luma_lpf = 1;
561 uv_lpf = 1;
562
563 src_decimation = 0x21f;
564 if (std == V4L2_STD_PAL_60) {
565 vblank = 26;
566 vblank656 = 26;
567 burst = 0x5b;
568 luma_lpf = 2;
569 comb = 0x20;
570 sc = 688739;
571 } else if (std == V4L2_STD_PAL_M) {
572 vblank = 20;
573 vblank656 = 24;
574 burst = 0x61;
575 comb = 0x20;
576 sc = 555452;
577 } else {
578 vblank = 26;
579 vblank656 = 26;
580 burst = 0x5b;
581 comb = 0x66;
582 sc = 556063;
583 }
584 }
585
586 /* DEBUG: Displays configured PLL frequency */
2a03f034 587 if (!is_cx231xx(state)) {
95b14fb2
MCC
588 pll_int = cx25840_read(client, 0x108);
589 pll_frac = cx25840_read4(client, 0x10c) & 0x1ffffff;
590 pll_post = cx25840_read(client, 0x109);
cb5aa1c6 591 v4l_dbg(1, cx25840_debug, client,
95b14fb2
MCC
592 "PLL regs = int: %u, frac: %u, post: %u\n",
593 pll_int, pll_frac, pll_post);
594
595 if (pll_post) {
596 int fin, fsc;
597 int pll = (28636363L * ((((u64)pll_int) << 25L) + pll_frac)) >> 25L;
598
599 pll /= pll_post;
600 v4l_dbg(1, cx25840_debug, client, "PLL = %d.%06d MHz\n",
601 pll / 1000000, pll % 1000000);
602 v4l_dbg(1, cx25840_debug, client, "PLL/8 = %d.%06d MHz\n",
603 pll / 8000000, (pll / 8) % 1000000);
604
605 fin = ((u64)src_decimation * pll) >> 12;
606 v4l_dbg(1, cx25840_debug, client,
607 "ADC Sampling freq = %d.%06d MHz\n",
608 fin / 1000000, fin % 1000000);
609
610 fsc = (((u64)sc) * pll) >> 24L;
611 v4l_dbg(1, cx25840_debug, client,
612 "Chroma sub-carrier freq = %d.%06d MHz\n",
613 fsc / 1000000, fsc % 1000000);
614
615 v4l_dbg(1, cx25840_debug, client, "hblank %i, hactive %i, "
616 "vblank %i, vactive %i, vblank656 %i, src_dec %i, "
617 "burst 0x%02x, luma_lpf %i, uv_lpf %i, comb 0x%02x, "
618 "sc 0x%06x\n",
619 hblank, hactive, vblank, vactive, vblank656,
620 src_decimation, burst, luma_lpf, uv_lpf, comb, sc);
621 }
cb5aa1c6
HV
622 }
623
624 /* Sets horizontal blanking delay and active lines */
625 cx25840_write(client, 0x470, hblank);
626 cx25840_write(client, 0x471,
627 0xff & (((hblank >> 8) & 0x3) | (hactive << 4)));
628 cx25840_write(client, 0x472, hactive >> 4);
629
630 /* Sets burst gate delay */
631 cx25840_write(client, 0x473, burst);
632
633 /* Sets vertical blanking delay and active duration */
634 cx25840_write(client, 0x474, vblank);
635 cx25840_write(client, 0x475,
636 0xff & (((vblank >> 8) & 0x3) | (vactive << 4)));
637 cx25840_write(client, 0x476, vactive >> 4);
638 cx25840_write(client, 0x477, vblank656);
639
640 /* Sets src decimation rate */
641 cx25840_write(client, 0x478, 0xff & src_decimation);
642 cx25840_write(client, 0x479, 0xff & (src_decimation >> 8));
643
644 /* Sets Luma and UV Low pass filters */
645 cx25840_write(client, 0x47a, luma_lpf << 6 | ((uv_lpf << 4) & 0x30));
646
647 /* Enables comb filters */
648 cx25840_write(client, 0x47b, comb);
649
650 /* Sets SC Step*/
651 cx25840_write(client, 0x47c, sc);
652 cx25840_write(client, 0x47d, 0xff & sc >> 8);
653 cx25840_write(client, 0x47e, 0xff & sc >> 16);
654
655 /* Sets VBI parameters */
656 if (std & V4L2_STD_625_50) {
657 cx25840_write(client, 0x47f, 0x01);
658 state->vbi_line_offset = 5;
659 } else {
660 cx25840_write(client, 0x47f, 0x00);
661 state->vbi_line_offset = 8;
662 }
663}
664
665/* ----------------------------------------------------------------------- */
666
bd985160
HV
667static void input_change(struct i2c_client *client)
668{
9357b31c 669 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
081b496a 670 v4l2_std_id std = state->std;
bd985160 671
73dcddc5
HV
672 /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
673 if (std & V4L2_STD_SECAM) {
674 cx25840_write(client, 0x402, 0);
675 }
676 else {
677 cx25840_write(client, 0x402, 0x04);
678 cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
679 }
680 cx25840_and_or(client, 0x401, ~0x60, 0);
681 cx25840_and_or(client, 0x401, ~0x60, 0x60);
82677618 682 cx25840_and_or(client, 0x810, ~0x01, 1);
73dcddc5 683
39c4ad6a
HV
684 if (state->radio) {
685 cx25840_write(client, 0x808, 0xf9);
686 cx25840_write(client, 0x80b, 0x00);
687 }
688 else if (std & V4L2_STD_525_60) {
d97a11e0
HV
689 /* Certain Hauppauge PVR150 models have a hardware bug
690 that causes audio to drop out. For these models the
691 audio standard must be set explicitly.
692 To be precise: it affects cards with tuner models
693 85, 99 and 112 (model numbers from tveeprom). */
694 int hw_fix = state->pvr150_workaround;
695
696 if (std == V4L2_STD_NTSC_M_JP) {
f95006f8 697 /* Japan uses EIAJ audio standard */
d97a11e0
HV
698 cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
699 } else if (std == V4L2_STD_NTSC_M_KR) {
700 /* South Korea uses A2 audio standard */
701 cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
f95006f8
HV
702 } else {
703 /* Others use the BTSC audio standard */
d97a11e0 704 cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
f95006f8 705 }
bd985160 706 cx25840_write(client, 0x80b, 0x00);
839e4a4a 707 } else if (std & V4L2_STD_PAL) {
3c3099d5 708 /* Autodetect audio standard and audio system */
839e4a4a 709 cx25840_write(client, 0x808, 0xff);
3c3099d5
AP
710 /* Since system PAL-L is pretty much non-existant and
711 not used by any public broadcast network, force
712 6.5 MHz carrier to be interpreted as System DK,
713 this avoids DK audio detection instability */
714 cx25840_write(client, 0x80b, 0x00);
839e4a4a 715 } else if (std & V4L2_STD_SECAM) {
3c3099d5 716 /* Autodetect audio standard and audio system */
839e4a4a 717 cx25840_write(client, 0x808, 0xff);
3c3099d5
AP
718 /* If only one of SECAM-DK / SECAM-L is required, then force
719 6.5MHz carrier, else autodetect it */
720 if ((std & V4L2_STD_SECAM_DK) &&
721 !(std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
722 /* 6.5 MHz carrier to be interpreted as System DK */
723 cx25840_write(client, 0x80b, 0x00);
724 } else if (!(std & V4L2_STD_SECAM_DK) &&
725 (std & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))) {
726 /* 6.5 MHz carrier to be interpreted as System L */
727 cx25840_write(client, 0x80b, 0x08);
728 } else {
729 /* 6.5 MHz carrier to be autodetected */
730 cx25840_write(client, 0x80b, 0x10);
731 }
bd985160
HV
732 }
733
82677618 734 cx25840_and_or(client, 0x810, ~0x01, 0);
bd985160
HV
735}
736
a8bbf12a
HV
737static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
738 enum cx25840_audio_input aud_input)
bd985160 739{
9357b31c 740 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
a8bbf12a
HV
741 u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
742 vid_input <= CX25840_COMPOSITE8);
fb29ab96
DW
743 u8 is_component = (vid_input & CX25840_COMPONENT_ON) ==
744 CX25840_COMPONENT_ON;
745 int luma = vid_input & 0xf0;
746 int chroma = vid_input & 0xf00;
a8bbf12a 747 u8 reg;
bd985160 748
f234081b
ST
749 v4l_dbg(1, cx25840_debug, client,
750 "decoder set video input %d, audio input %d\n",
751 vid_input, aud_input);
bd985160 752
f234081b
ST
753 if (vid_input >= CX25840_VIN1_CH1) {
754 v4l_dbg(1, cx25840_debug, client, "vid_input 0x%x\n",
755 vid_input);
756 reg = vid_input & 0xff;
10e43d90
KK
757 is_composite = !is_component &&
758 ((vid_input & CX25840_SVIDEO_ON) != CX25840_SVIDEO_ON);
f234081b
ST
759
760 v4l_dbg(1, cx25840_debug, client, "mux cfg 0x%x comp=%d\n",
761 reg, is_composite);
fb29ab96 762 } else if (is_composite) {
a8bbf12a
HV
763 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
764 } else {
a8bbf12a 765 if ((vid_input & ~0xff0) ||
45270a15 766 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA8 ||
a8bbf12a 767 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
f234081b
ST
768 v4l_err(client, "0x%04x is not a valid video input!\n",
769 vid_input);
a8bbf12a 770 return -EINVAL;
bd985160 771 }
a8bbf12a
HV
772 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
773 if (chroma >= CX25840_SVIDEO_CHROMA7) {
774 reg &= 0x3f;
775 reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
bd985160 776 } else {
a8bbf12a
HV
777 reg &= 0xcf;
778 reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
bd985160 779 }
a8bbf12a 780 }
bd985160 781
f234081b
ST
782 /* The caller has previously prepared the correct routing
783 * configuration in reg (for the cx23885) so we have no
784 * need to attempt to flip bits for earlier av decoders.
785 */
2a03f034 786 if (!is_cx2388x(state) && !is_cx231xx(state)) {
f234081b
ST
787 switch (aud_input) {
788 case CX25840_AUDIO_SERIAL:
789 /* do nothing, use serial audio input */
790 break;
791 case CX25840_AUDIO4: reg &= ~0x30; break;
792 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
793 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
794 case CX25840_AUDIO7: reg &= ~0xc0; break;
795 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
bd985160 796
f234081b
ST
797 default:
798 v4l_err(client, "0x%04x is not a valid audio input!\n",
799 aud_input);
800 return -EINVAL;
801 }
bd985160
HV
802 }
803
a8bbf12a 804 cx25840_write(client, 0x103, reg);
f234081b 805
fb29ab96
DW
806 /* Set INPUT_MODE to Composite, S-Video or Component */
807 if (is_component)
808 cx25840_and_or(client, 0x401, ~0x6, 0x6);
809 else
810 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
f234081b 811
2a03f034 812 if (!is_cx2388x(state) && !is_cx231xx(state)) {
f234081b
ST
813 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
814 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
815 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2&CH3 */
816 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
817 cx25840_and_or(client, 0x102, ~0x4, 4);
818 else
819 cx25840_and_or(client, 0x102, ~0x4, 0);
820 } else {
fb29ab96
DW
821 /* Set DUAL_MODE_ADC2 to 1 if component*/
822 cx25840_and_or(client, 0x102, ~0x4, is_component ? 0x4 : 0x0);
823 if (is_composite) {
f234081b
ST
824 /* ADC2 input select channel 2 */
825 cx25840_and_or(client, 0x102, ~0x2, 0);
fb29ab96
DW
826 } else if (!is_component) {
827 /* S-Video */
828 if (chroma >= CX25840_SVIDEO_CHROMA7) {
829 /* ADC2 input select channel 3 */
830 cx25840_and_or(client, 0x102, ~0x2, 2);
831 } else {
832 /* ADC2 input select channel 2 */
833 cx25840_and_or(client, 0x102, ~0x2, 0);
834 }
835 }
f234081b 836 }
a8bbf12a
HV
837
838 state->vid_input = vid_input;
839 state->aud_input = aud_input;
2a03f034 840 if (!is_cx2583x(state)) {
e2b8cf4c
HV
841 cx25840_audio_set_path(client);
842 input_change(client);
843 }
f234081b 844
2a03f034 845 if (is_cx2388x(state)) {
f234081b
ST
846 /* Audio channel 1 src : Parallel 1 */
847 cx25840_write(client, 0x124, 0x03);
848
849 /* Select AFE clock pad output source */
850 cx25840_write(client, 0x144, 0x05);
851
852 /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
853 cx25840_write(client, 0x914, 0xa0);
854
149783b5
SD
855 /* I2S_OUT_CTL:
856 * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
857 * I2S_OUT_MASTER_MODE = Master
858 */
859 cx25840_write(client, 0x918, 0xa0);
860 cx25840_write(client, 0x919, 0x01);
2a03f034 861 } else if (is_cx231xx(state)) {
149783b5
SD
862 /* Audio channel 1 src : Parallel 1 */
863 cx25840_write(client, 0x124, 0x03);
864
865 /* I2S_IN_CTL: I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1 */
866 cx25840_write(client, 0x914, 0xa0);
867
f234081b
ST
868 /* I2S_OUT_CTL:
869 * I2S_IN_SONY_MODE, LEFT SAMPLE on WS=1
870 * I2S_OUT_MASTER_MODE = Master
871 */
872 cx25840_write(client, 0x918, 0xa0);
873 cx25840_write(client, 0x919, 0x01);
874 }
875
bd985160
HV
876 return 0;
877}
878
879/* ----------------------------------------------------------------------- */
880
081b496a 881static int set_v4lstd(struct i2c_client *client)
bd985160 882{
9357b31c 883 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
081b496a
HV
884 u8 fmt = 0; /* zero is autodetect */
885 u8 pal_m = 0;
468a0a54
MCC
886
887 /* First tests should be against specific std */
081b496a
HV
888 if (state->std == V4L2_STD_NTSC_M_JP) {
889 fmt = 0x2;
890 } else if (state->std == V4L2_STD_NTSC_443) {
891 fmt = 0x3;
892 } else if (state->std == V4L2_STD_PAL_M) {
893 pal_m = 1;
894 fmt = 0x5;
895 } else if (state->std == V4L2_STD_PAL_N) {
896 fmt = 0x6;
897 } else if (state->std == V4L2_STD_PAL_Nc) {
898 fmt = 0x7;
899 } else if (state->std == V4L2_STD_PAL_60) {
900 fmt = 0x8;
468a0a54
MCC
901 } else {
902 /* Then, test against generic ones */
081b496a
HV
903 if (state->std & V4L2_STD_NTSC)
904 fmt = 0x1;
905 else if (state->std & V4L2_STD_PAL)
906 fmt = 0x4;
907 else if (state->std & V4L2_STD_SECAM)
908 fmt = 0xc;
bd985160
HV
909 }
910
839e4a4a
MCC
911 v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
912
73dcddc5
HV
913 /* Follow step 9 of section 3.16 in the cx25840 datasheet.
914 Without this PAL may display a vertical ghosting effect.
915 This happens for example with the Yuan MPC622. */
916 if (fmt >= 4 && fmt < 8) {
917 /* Set format to NTSC-M */
918 cx25840_and_or(client, 0x400, ~0xf, 1);
919 /* Turn off LCOMB */
920 cx25840_and_or(client, 0x47b, ~6, 0);
921 }
bd985160 922 cx25840_and_or(client, 0x400, ~0xf, fmt);
081b496a 923 cx25840_and_or(client, 0x403, ~0x3, pal_m);
cb5aa1c6 924 cx25840_std_setup(client);
2a03f034 925 if (!is_cx2583x(state))
081b496a 926 input_change(client);
bd985160
HV
927 return 0;
928}
929
bd985160
HV
930/* ----------------------------------------------------------------------- */
931
9357b31c 932static int cx25840_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
bd985160 933{
95b14fb2 934 struct cx25840_state *state = to_state(sd);
9357b31c 935 struct i2c_client *client = v4l2_get_subdevdata(sd);
bd985160
HV
936
937 switch (ctrl->id) {
a8bbf12a
HV
938 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
939 state->pvr150_workaround = ctrl->value;
940 set_input(client, state->vid_input, state->aud_input);
bd985160
HV
941 break;
942
943 case V4L2_CID_BRIGHTNESS:
944 if (ctrl->value < 0 || ctrl->value > 255) {
fac9e899 945 v4l_err(client, "invalid brightness setting %d\n",
bd985160
HV
946 ctrl->value);
947 return -ERANGE;
948 }
949
950 cx25840_write(client, 0x414, ctrl->value - 128);
951 break;
952
953 case V4L2_CID_CONTRAST:
954 if (ctrl->value < 0 || ctrl->value > 127) {
fac9e899 955 v4l_err(client, "invalid contrast setting %d\n",
bd985160
HV
956 ctrl->value);
957 return -ERANGE;
958 }
959
960 cx25840_write(client, 0x415, ctrl->value << 1);
961 break;
962
963 case V4L2_CID_SATURATION:
964 if (ctrl->value < 0 || ctrl->value > 127) {
fac9e899 965 v4l_err(client, "invalid saturation setting %d\n",
bd985160
HV
966 ctrl->value);
967 return -ERANGE;
968 }
969
970 cx25840_write(client, 0x420, ctrl->value << 1);
971 cx25840_write(client, 0x421, ctrl->value << 1);
972 break;
973
974 case V4L2_CID_HUE:
de6476f5 975 if (ctrl->value < -128 || ctrl->value > 127) {
fac9e899 976 v4l_err(client, "invalid hue setting %d\n", ctrl->value);
bd985160
HV
977 return -ERANGE;
978 }
979
980 cx25840_write(client, 0x422, ctrl->value);
981 break;
982
983 case V4L2_CID_AUDIO_VOLUME:
984 case V4L2_CID_AUDIO_BASS:
985 case V4L2_CID_AUDIO_TREBLE:
986 case V4L2_CID_AUDIO_BALANCE:
987 case V4L2_CID_AUDIO_MUTE:
2a03f034 988 if (is_cx2583x(state))
e2b8cf4c 989 return -EINVAL;
df1d5ed8 990 return cx25840_audio_s_ctrl(sd, ctrl);
3faeeae4
HV
991
992 default:
993 return -EINVAL;
bd985160
HV
994 }
995
996 return 0;
997}
998
9357b31c 999static int cx25840_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
bd985160 1000{
95b14fb2 1001 struct cx25840_state *state = to_state(sd);
9357b31c 1002 struct i2c_client *client = v4l2_get_subdevdata(sd);
bd985160
HV
1003
1004 switch (ctrl->id) {
a8bbf12a
HV
1005 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
1006 ctrl->value = state->pvr150_workaround;
bd985160
HV
1007 break;
1008 case V4L2_CID_BRIGHTNESS:
0de71224 1009 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
bd985160
HV
1010 break;
1011 case V4L2_CID_CONTRAST:
1012 ctrl->value = cx25840_read(client, 0x415) >> 1;
1013 break;
1014 case V4L2_CID_SATURATION:
1015 ctrl->value = cx25840_read(client, 0x420) >> 1;
1016 break;
1017 case V4L2_CID_HUE:
c5099a64 1018 ctrl->value = (s8)cx25840_read(client, 0x422);
bd985160
HV
1019 break;
1020 case V4L2_CID_AUDIO_VOLUME:
1021 case V4L2_CID_AUDIO_BASS:
1022 case V4L2_CID_AUDIO_TREBLE:
1023 case V4L2_CID_AUDIO_BALANCE:
1024 case V4L2_CID_AUDIO_MUTE:
2a03f034 1025 if (is_cx2583x(state))
e2b8cf4c 1026 return -EINVAL;
df1d5ed8 1027 return cx25840_audio_g_ctrl(sd, ctrl);
bd985160
HV
1028 default:
1029 return -EINVAL;
1030 }
1031
1032 return 0;
1033}
1034
1035/* ----------------------------------------------------------------------- */
1036
96fd004f 1037static int cx25840_s_mbus_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt)
bd985160 1038{
9357b31c
HV
1039 struct cx25840_state *state = to_state(sd);
1040 struct i2c_client *client = v4l2_get_subdevdata(sd);
bd985160 1041 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
081b496a 1042 int is_50Hz = !(state->std & V4L2_STD_525_60);
bd985160 1043
96fd004f
HV
1044 if (fmt->code != V4L2_MBUS_FMT_FIXED)
1045 return -EINVAL;
bd985160 1046
96fd004f
HV
1047 fmt->field = V4L2_FIELD_INTERLACED;
1048 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
bd985160 1049
96fd004f
HV
1050 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
1051 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
bd985160 1052
96fd004f
HV
1053 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
1054 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
bd985160 1055
96fd004f 1056 Vlines = fmt->height + (is_50Hz ? 4 : 7);
bd985160 1057
96fd004f
HV
1058 if ((fmt->width * 16 < Hsrc) || (Hsrc < fmt->width) ||
1059 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
1060 v4l_err(client, "%dx%d is not a valid size!\n",
1061 fmt->width, fmt->height);
1062 return -ERANGE;
1063 }
bd985160 1064
96fd004f
HV
1065 HSC = (Hsrc * (1 << 20)) / fmt->width - (1 << 20);
1066 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
1067 VSC &= 0x1fff;
1068
1069 if (fmt->width >= 385)
1070 filter = 0;
1071 else if (fmt->width > 192)
1072 filter = 1;
1073 else if (fmt->width > 96)
1074 filter = 2;
1075 else
1076 filter = 3;
1077
1078 v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
1079 fmt->width, fmt->height, HSC, VSC);
1080
1081 /* HSCALE=HSC */
1082 cx25840_write(client, 0x418, HSC & 0xff);
1083 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
1084 cx25840_write(client, 0x41a, HSC >> 16);
1085 /* VSCALE=VSC */
1086 cx25840_write(client, 0x41c, VSC & 0xff);
1087 cx25840_write(client, 0x41d, VSC >> 8);
1088 /* VS_INTRLACE=1 VFILT=filter */
1089 cx25840_write(client, 0x41e, 0x8 | filter);
1090 return 0;
1091}
1092
bd985160
HV
1093/* ----------------------------------------------------------------------- */
1094
1a39275a
HV
1095static void log_video_status(struct i2c_client *client)
1096{
1097 static const char *const fmt_strs[] = {
1098 "0x0",
1099 "NTSC-M", "NTSC-J", "NTSC-4.43",
1100 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
1101 "0x9", "0xA", "0xB",
1102 "SECAM",
1103 "0xD", "0xE", "0xF"
1104 };
1105
9357b31c 1106 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1a39275a
HV
1107 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
1108 u8 gen_stat1 = cx25840_read(client, 0x40d);
1109 u8 gen_stat2 = cx25840_read(client, 0x40e);
1110 int vid_input = state->vid_input;
1111
1112 v4l_info(client, "Video signal: %spresent\n",
1113 (gen_stat2 & 0x20) ? "" : "not ");
1114 v4l_info(client, "Detected format: %s\n",
1115 fmt_strs[gen_stat1 & 0xf]);
1116
1117 v4l_info(client, "Specified standard: %s\n",
1118 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
1119
1120 if (vid_input >= CX25840_COMPOSITE1 &&
1121 vid_input <= CX25840_COMPOSITE8) {
1122 v4l_info(client, "Specified video input: Composite %d\n",
1123 vid_input - CX25840_COMPOSITE1 + 1);
1124 } else {
1125 v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
1126 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
1127 }
1128
1129 v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
1130}
1131
1132/* ----------------------------------------------------------------------- */
1133
1134static void log_audio_status(struct i2c_client *client)
1135{
9357b31c 1136 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
1a39275a
HV
1137 u8 download_ctl = cx25840_read(client, 0x803);
1138 u8 mod_det_stat0 = cx25840_read(client, 0x804);
1139 u8 mod_det_stat1 = cx25840_read(client, 0x805);
1140 u8 audio_config = cx25840_read(client, 0x808);
1141 u8 pref_mode = cx25840_read(client, 0x809);
1142 u8 afc0 = cx25840_read(client, 0x80b);
1143 u8 mute_ctl = cx25840_read(client, 0x8d3);
1144 int aud_input = state->aud_input;
1145 char *p;
1146
1147 switch (mod_det_stat0) {
1148 case 0x00: p = "mono"; break;
1149 case 0x01: p = "stereo"; break;
1150 case 0x02: p = "dual"; break;
1151 case 0x04: p = "tri"; break;
1152 case 0x10: p = "mono with SAP"; break;
1153 case 0x11: p = "stereo with SAP"; break;
1154 case 0x12: p = "dual with SAP"; break;
1155 case 0x14: p = "tri with SAP"; break;
1156 case 0xfe: p = "forced mode"; break;
1157 default: p = "not defined";
1158 }
1159 v4l_info(client, "Detected audio mode: %s\n", p);
1160
1161 switch (mod_det_stat1) {
1162 case 0x00: p = "not defined"; break;
1163 case 0x01: p = "EIAJ"; break;
1164 case 0x02: p = "A2-M"; break;
1165 case 0x03: p = "A2-BG"; break;
1166 case 0x04: p = "A2-DK1"; break;
1167 case 0x05: p = "A2-DK2"; break;
1168 case 0x06: p = "A2-DK3"; break;
1169 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1170 case 0x08: p = "AM-L"; break;
1171 case 0x09: p = "NICAM-BG"; break;
1172 case 0x0a: p = "NICAM-DK"; break;
1173 case 0x0b: p = "NICAM-I"; break;
1174 case 0x0c: p = "NICAM-L"; break;
1175 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1176 case 0x0e: p = "IF FM Radio"; break;
1177 case 0x0f: p = "BTSC"; break;
1178 case 0x10: p = "high-deviation FM"; break;
1179 case 0x11: p = "very high-deviation FM"; break;
1180 case 0xfd: p = "unknown audio standard"; break;
1181 case 0xfe: p = "forced audio standard"; break;
1182 case 0xff: p = "no detected audio standard"; break;
1183 default: p = "not defined";
1184 }
1185 v4l_info(client, "Detected audio standard: %s\n", p);
1186 v4l_info(client, "Audio muted: %s\n",
1187 (state->unmute_volume >= 0) ? "yes" : "no");
1188 v4l_info(client, "Audio microcontroller: %s\n",
1189 (download_ctl & 0x10) ?
1190 ((mute_ctl & 0x2) ? "detecting" : "running") : "stopped");
1191
1192 switch (audio_config >> 4) {
1193 case 0x00: p = "undefined"; break;
1194 case 0x01: p = "BTSC"; break;
1195 case 0x02: p = "EIAJ"; break;
1196 case 0x03: p = "A2-M"; break;
1197 case 0x04: p = "A2-BG"; break;
1198 case 0x05: p = "A2-DK1"; break;
1199 case 0x06: p = "A2-DK2"; break;
1200 case 0x07: p = "A2-DK3"; break;
1201 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1202 case 0x09: p = "AM-L"; break;
1203 case 0x0a: p = "NICAM-BG"; break;
1204 case 0x0b: p = "NICAM-DK"; break;
1205 case 0x0c: p = "NICAM-I"; break;
1206 case 0x0d: p = "NICAM-L"; break;
1207 case 0x0e: p = "FM radio"; break;
1208 case 0x0f: p = "automatic detection"; break;
1209 default: p = "undefined";
1210 }
1211 v4l_info(client, "Configured audio standard: %s\n", p);
1212
1213 if ((audio_config >> 4) < 0xF) {
1214 switch (audio_config & 0xF) {
1215 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1216 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1217 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1218 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1219 case 0x04: p = "STEREO"; break;
1220 case 0x05: p = "DUAL1 (AB)"; break;
1221 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1222 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1223 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1224 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1225 case 0x0a: p = "SAP"; break;
1226 default: p = "undefined";
1227 }
1228 v4l_info(client, "Configured audio mode: %s\n", p);
1229 } else {
1230 switch (audio_config & 0xF) {
1231 case 0x00: p = "BG"; break;
1232 case 0x01: p = "DK1"; break;
1233 case 0x02: p = "DK2"; break;
1234 case 0x03: p = "DK3"; break;
1235 case 0x04: p = "I"; break;
1236 case 0x05: p = "L"; break;
1237 case 0x06: p = "BTSC"; break;
1238 case 0x07: p = "EIAJ"; break;
1239 case 0x08: p = "A2-M"; break;
1240 case 0x09: p = "FM Radio"; break;
1241 case 0x0f: p = "automatic standard and mode detection"; break;
1242 default: p = "undefined";
1243 }
1244 v4l_info(client, "Configured audio system: %s\n", p);
1245 }
1246
1247 if (aud_input) {
1248 v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
1249 } else {
1250 v4l_info(client, "Specified audio input: External\n");
1251 }
1252
1253 switch (pref_mode & 0xf) {
1254 case 0: p = "mono/language A"; break;
1255 case 1: p = "language B"; break;
1256 case 2: p = "language C"; break;
1257 case 3: p = "analog fallback"; break;
1258 case 4: p = "stereo"; break;
1259 case 5: p = "language AC"; break;
1260 case 6: p = "language BC"; break;
1261 case 7: p = "language AB"; break;
1262 default: p = "undefined";
1263 }
1264 v4l_info(client, "Preferred audio mode: %s\n", p);
1265
1266 if ((audio_config & 0xf) == 0xf) {
1267 switch ((afc0 >> 3) & 0x3) {
1268 case 0: p = "system DK"; break;
1269 case 1: p = "system L"; break;
1270 case 2: p = "autodetect"; break;
1271 default: p = "undefined";
1272 }
1273 v4l_info(client, "Selected 65 MHz format: %s\n", p);
1274
1275 switch (afc0 & 0x7) {
1276 case 0: p = "chroma"; break;
1277 case 1: p = "BTSC"; break;
1278 case 2: p = "EIAJ"; break;
1279 case 3: p = "A2-M"; break;
1280 case 4: p = "autodetect"; break;
1281 default: p = "undefined";
1282 }
1283 v4l_info(client, "Selected 45 MHz format: %s\n", p);
1284 }
1285}
1286
1287/* ----------------------------------------------------------------------- */
1288
cc26b076 1289/* This load_fw operation must be called to load the driver's firmware.
6ca187ab
HV
1290 Without this the audio standard detection will fail and you will
1291 only get mono.
1292
1293 Since loading the firmware is often problematic when the driver is
1294 compiled into the kernel I recommend postponing calling this function
1295 until the first open of the video device. Another reason for
1296 postponing it is that loading this firmware takes a long time (seconds)
1297 due to the slow i2c bus speed. So it will speed up the boot process if
1298 you can avoid loading the fw as long as the video device isn't used. */
cc26b076 1299static int cx25840_load_fw(struct v4l2_subdev *sd)
bd985160 1300{
9357b31c
HV
1301 struct cx25840_state *state = to_state(sd);
1302 struct i2c_client *client = v4l2_get_subdevdata(sd);
c976bc82
HV
1303
1304 if (!state->is_initialized) {
cc26b076 1305 /* initialize and load firmware */
c976bc82 1306 state->is_initialized = 1;
2a03f034 1307 if (is_cx2583x(state))
c976bc82 1308 cx25836_initialize(client);
2a03f034 1309 else if (is_cx2388x(state))
f234081b 1310 cx23885_initialize(client);
2a03f034 1311 else if (is_cx231xx(state))
149783b5 1312 cx231xx_initialize(client);
c976bc82 1313 else
89fc4eb9 1314 cx25840_initialize(client);
c976bc82 1315 }
9357b31c
HV
1316 return 0;
1317}
c976bc82 1318
bd985160 1319#ifdef CONFIG_VIDEO_ADV_DEBUG
aecde8b5 1320static int cx25840_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
9357b31c
HV
1321{
1322 struct i2c_client *client = v4l2_get_subdevdata(sd);
f234081b 1323
aecde8b5 1324 if (!v4l2_chip_match_i2c_client(client, &reg->match))
9357b31c
HV
1325 return -EINVAL;
1326 if (!capable(CAP_SYS_ADMIN))
1327 return -EPERM;
aecde8b5 1328 reg->size = 1;
9357b31c
HV
1329 reg->val = cx25840_read(client, reg->reg & 0x0fff);
1330 return 0;
1331}
1332
aecde8b5 1333static int cx25840_s_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
9357b31c
HV
1334{
1335 struct i2c_client *client = v4l2_get_subdevdata(sd);
1336
aecde8b5 1337 if (!v4l2_chip_match_i2c_client(client, &reg->match))
9357b31c
HV
1338 return -EINVAL;
1339 if (!capable(CAP_SYS_ADMIN))
1340 return -EPERM;
1341 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
1342 return 0;
1343}
bd985160
HV
1344#endif
1345
3ccc646b
AW
1346static int cx25840_s_audio_stream(struct v4l2_subdev *sd, int enable)
1347{
1348 struct cx25840_state *state = to_state(sd);
1349 struct i2c_client *client = v4l2_get_subdevdata(sd);
1350 u8 v;
1351
1352 if (is_cx2583x(state) || is_cx2388x(state) || is_cx231xx(state))
1353 return 0;
1354
1355 v4l_dbg(1, cx25840_debug, client, "%s audio output\n",
1356 enable ? "enable" : "disable");
1357
1358 if (enable) {
1359 v = cx25840_read(client, 0x115) | 0x80;
1360 cx25840_write(client, 0x115, v);
1361 v = cx25840_read(client, 0x116) | 0x03;
1362 cx25840_write(client, 0x116, v);
1363 } else {
1364 v = cx25840_read(client, 0x115) & ~(0x80);
1365 cx25840_write(client, 0x115, v);
1366 v = cx25840_read(client, 0x116) & ~(0x03);
1367 cx25840_write(client, 0x116, v);
1368 }
1369 return 0;
1370}
1371
9357b31c
HV
1372static int cx25840_s_stream(struct v4l2_subdev *sd, int enable)
1373{
1374 struct cx25840_state *state = to_state(sd);
1375 struct i2c_client *client = v4l2_get_subdevdata(sd);
3ccc646b 1376 u8 v;
bd985160 1377
3ccc646b 1378 v4l_dbg(1, cx25840_debug, client, "%s video output\n",
9357b31c
HV
1379 enable ? "enable" : "disable");
1380 if (enable) {
2a03f034 1381 if (is_cx2388x(state) || is_cx231xx(state)) {
3ccc646b 1382 v = cx25840_read(client, 0x421) | 0x0b;
f234081b
ST
1383 cx25840_write(client, 0x421, v);
1384 } else {
3ccc646b
AW
1385 v = cx25840_read(client, 0x115) | 0x0c;
1386 cx25840_write(client, 0x115, v);
1387 v = cx25840_read(client, 0x116) | 0x04;
1388 cx25840_write(client, 0x116, v);
f234081b 1389 }
9357b31c 1390 } else {
2a03f034 1391 if (is_cx2388x(state) || is_cx231xx(state)) {
3ccc646b 1392 v = cx25840_read(client, 0x421) & ~(0x0b);
f234081b
ST
1393 cx25840_write(client, 0x421, v);
1394 } else {
3ccc646b
AW
1395 v = cx25840_read(client, 0x115) & ~(0x0c);
1396 cx25840_write(client, 0x115, v);
1397 v = cx25840_read(client, 0x116) & ~(0x04);
1398 cx25840_write(client, 0x116, v);
f234081b 1399 }
9357b31c
HV
1400 }
1401 return 0;
1402}
bd985160 1403
9357b31c
HV
1404static int cx25840_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
1405{
1406 struct cx25840_state *state = to_state(sd);
bd985160 1407
9357b31c
HV
1408 switch (qc->id) {
1409 case V4L2_CID_BRIGHTNESS:
10afbef1 1410 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
9357b31c
HV
1411 case V4L2_CID_CONTRAST:
1412 case V4L2_CID_SATURATION:
10afbef1 1413 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
9357b31c 1414 case V4L2_CID_HUE:
10afbef1 1415 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
9357b31c
HV
1416 default:
1417 break;
1418 }
2a03f034 1419 if (is_cx2583x(state))
9357b31c 1420 return -EINVAL;
bd985160 1421
9357b31c
HV
1422 switch (qc->id) {
1423 case V4L2_CID_AUDIO_VOLUME:
1424 return v4l2_ctrl_query_fill(qc, 0, 65535,
1425 65535 / 100, state->default_volume);
1426 case V4L2_CID_AUDIO_MUTE:
10afbef1 1427 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
9357b31c
HV
1428 case V4L2_CID_AUDIO_BALANCE:
1429 case V4L2_CID_AUDIO_BASS:
1430 case V4L2_CID_AUDIO_TREBLE:
10afbef1 1431 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
9357b31c
HV
1432 default:
1433 return -EINVAL;
1434 }
1435 return -EINVAL;
1436}
bd985160 1437
9357b31c
HV
1438static int cx25840_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
1439{
1440 struct cx25840_state *state = to_state(sd);
1441 struct i2c_client *client = v4l2_get_subdevdata(sd);
d92c20e0 1442
9357b31c
HV
1443 if (state->radio == 0 && state->std == std)
1444 return 0;
1445 state->radio = 0;
1446 state->std = std;
1447 return set_v4lstd(client);
1448}
e2b8cf4c 1449
9357b31c
HV
1450static int cx25840_s_radio(struct v4l2_subdev *sd)
1451{
1452 struct cx25840_state *state = to_state(sd);
d92c20e0 1453
9357b31c
HV
1454 state->radio = 1;
1455 return 0;
1456}
bd985160 1457
5325b427
HV
1458static int cx25840_s_video_routing(struct v4l2_subdev *sd,
1459 u32 input, u32 output, u32 config)
9357b31c
HV
1460{
1461 struct cx25840_state *state = to_state(sd);
1462 struct i2c_client *client = v4l2_get_subdevdata(sd);
3faeeae4 1463
5325b427 1464 return set_input(client, input, state->aud_input);
9357b31c 1465}
bd985160 1466
5325b427
HV
1467static int cx25840_s_audio_routing(struct v4l2_subdev *sd,
1468 u32 input, u32 output, u32 config)
9357b31c
HV
1469{
1470 struct cx25840_state *state = to_state(sd);
1471 struct i2c_client *client = v4l2_get_subdevdata(sd);
bd985160 1472
2a03f034 1473 if (is_cx2583x(state))
9357b31c 1474 return -EINVAL;
5325b427 1475 return set_input(client, state->vid_input, input);
9357b31c 1476}
bd985160 1477
9357b31c
HV
1478static int cx25840_s_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *freq)
1479{
1480 struct cx25840_state *state = to_state(sd);
1481 struct i2c_client *client = v4l2_get_subdevdata(sd);
a8bbf12a 1482
2a03f034 1483 if (!is_cx2583x(state))
9357b31c
HV
1484 input_change(client);
1485 return 0;
1486}
a8bbf12a 1487
9357b31c
HV
1488static int cx25840_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1489{
1490 struct cx25840_state *state = to_state(sd);
1491 struct i2c_client *client = v4l2_get_subdevdata(sd);
1492 u8 vpres = cx25840_read(client, 0x40e) & 0x20;
1493 u8 mode;
1494 int val = 0;
bd985160 1495
9357b31c
HV
1496 if (state->radio)
1497 return 0;
bd985160 1498
9357b31c 1499 vt->signal = vpres ? 0xffff : 0x0;
2a03f034 1500 if (is_cx2583x(state))
9357b31c 1501 return 0;
3faeeae4 1502
9357b31c
HV
1503 vt->capability |=
1504 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
1505 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
e2b8cf4c 1506
9357b31c 1507 mode = cx25840_read(client, 0x804);
bd985160 1508
9357b31c
HV
1509 /* get rxsubchans and audmode */
1510 if ((mode & 0xf) == 1)
1511 val |= V4L2_TUNER_SUB_STEREO;
1512 else
1513 val |= V4L2_TUNER_SUB_MONO;
bd985160 1514
9357b31c
HV
1515 if (mode == 2 || mode == 4)
1516 val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
bd985160 1517
9357b31c
HV
1518 if (mode & 0x10)
1519 val |= V4L2_TUNER_SUB_SAP;
bd985160 1520
9357b31c
HV
1521 vt->rxsubchans = val;
1522 vt->audmode = state->audmode;
1523 return 0;
1524}
bd985160 1525
9357b31c
HV
1526static int cx25840_s_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
1527{
1528 struct cx25840_state *state = to_state(sd);
1529 struct i2c_client *client = v4l2_get_subdevdata(sd);
bd985160 1530
2a03f034 1531 if (state->radio || is_cx2583x(state))
9357b31c 1532 return 0;
8a4b275f 1533
9357b31c 1534 switch (vt->audmode) {
bd985160 1535 case V4L2_TUNER_MODE_MONO:
8a4b275f
HV
1536 /* mono -> mono
1537 stereo -> mono
1538 bilingual -> lang1 */
bd985160
HV
1539 cx25840_and_or(client, 0x809, ~0xf, 0x00);
1540 break;
301e22d6 1541 case V4L2_TUNER_MODE_STEREO:
8a4b275f
HV
1542 case V4L2_TUNER_MODE_LANG1:
1543 /* mono -> mono
1544 stereo -> stereo
1545 bilingual -> lang1 */
bd985160
HV
1546 cx25840_and_or(client, 0x809, ~0xf, 0x04);
1547 break;
301e22d6 1548 case V4L2_TUNER_MODE_LANG1_LANG2:
8a4b275f
HV
1549 /* mono -> mono
1550 stereo -> stereo
1551 bilingual -> lang1/lang2 */
1552 cx25840_and_or(client, 0x809, ~0xf, 0x07);
1553 break;
bd985160 1554 case V4L2_TUNER_MODE_LANG2:
8a4b275f 1555 /* mono -> mono
301e22d6 1556 stereo -> stereo
8a4b275f 1557 bilingual -> lang2 */
bd985160
HV
1558 cx25840_and_or(client, 0x809, ~0xf, 0x01);
1559 break;
8a4b275f
HV
1560 default:
1561 return -EINVAL;
9357b31c
HV
1562 }
1563 state->audmode = vt->audmode;
1564 return 0;
1565}
bd985160 1566
9357b31c
HV
1567static int cx25840_reset(struct v4l2_subdev *sd, u32 val)
1568{
1569 struct cx25840_state *state = to_state(sd);
1570 struct i2c_client *client = v4l2_get_subdevdata(sd);
bd985160 1571
2a03f034 1572 if (is_cx2583x(state))
9357b31c 1573 cx25836_initialize(client);
2a03f034 1574 else if (is_cx2388x(state))
9357b31c 1575 cx23885_initialize(client);
2a03f034 1576 else if (is_cx231xx(state))
149783b5 1577 cx231xx_initialize(client);
9357b31c
HV
1578 else
1579 cx25840_initialize(client);
1580 return 0;
1581}
bd985160 1582
aecde8b5 1583static int cx25840_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
9357b31c
HV
1584{
1585 struct cx25840_state *state = to_state(sd);
1586 struct i2c_client *client = v4l2_get_subdevdata(sd);
bd985160 1587
9357b31c
HV
1588 return v4l2_chip_ident_i2c_client(client, chip, state->id, state->rev);
1589}
bd985160 1590
9357b31c
HV
1591static int cx25840_log_status(struct v4l2_subdev *sd)
1592{
1593 struct cx25840_state *state = to_state(sd);
1594 struct i2c_client *client = v4l2_get_subdevdata(sd);
bd985160 1595
9357b31c 1596 log_video_status(client);
2a03f034 1597 if (!is_cx2583x(state))
9357b31c 1598 log_audio_status(client);
3faeeae4 1599 return 0;
bd985160
HV
1600}
1601
9357b31c
HV
1602/* ----------------------------------------------------------------------- */
1603
1604static const struct v4l2_subdev_core_ops cx25840_core_ops = {
1605 .log_status = cx25840_log_status,
1606 .g_chip_ident = cx25840_g_chip_ident,
1607 .g_ctrl = cx25840_g_ctrl,
1608 .s_ctrl = cx25840_s_ctrl,
1609 .queryctrl = cx25840_queryctrl,
f41737ec 1610 .s_std = cx25840_s_std,
9357b31c 1611 .reset = cx25840_reset,
cc26b076 1612 .load_fw = cx25840_load_fw,
9357b31c
HV
1613#ifdef CONFIG_VIDEO_ADV_DEBUG
1614 .g_register = cx25840_g_register,
1615 .s_register = cx25840_s_register,
1616#endif
1617};
1618
1619static const struct v4l2_subdev_tuner_ops cx25840_tuner_ops = {
1620 .s_frequency = cx25840_s_frequency,
9357b31c
HV
1621 .s_radio = cx25840_s_radio,
1622 .g_tuner = cx25840_g_tuner,
1623 .s_tuner = cx25840_s_tuner,
1624};
1625
1626static const struct v4l2_subdev_audio_ops cx25840_audio_ops = {
1627 .s_clock_freq = cx25840_s_clock_freq,
1628 .s_routing = cx25840_s_audio_routing,
3ccc646b 1629 .s_stream = cx25840_s_audio_stream,
9357b31c
HV
1630};
1631
1632static const struct v4l2_subdev_video_ops cx25840_video_ops = {
1633 .s_routing = cx25840_s_video_routing,
96fd004f 1634 .s_mbus_fmt = cx25840_s_mbus_fmt,
9357b31c
HV
1635 .s_stream = cx25840_s_stream,
1636};
1637
32cd527f
HV
1638static const struct v4l2_subdev_vbi_ops cx25840_vbi_ops = {
1639 .decode_vbi_line = cx25840_decode_vbi_line,
5393db43
HV
1640 .s_raw_fmt = cx25840_s_raw_fmt,
1641 .s_sliced_fmt = cx25840_s_sliced_fmt,
1642 .g_sliced_fmt = cx25840_g_sliced_fmt,
32cd527f
HV
1643};
1644
9357b31c
HV
1645static const struct v4l2_subdev_ops cx25840_ops = {
1646 .core = &cx25840_core_ops,
1647 .tuner = &cx25840_tuner_ops,
1648 .audio = &cx25840_audio_ops,
1649 .video = &cx25840_video_ops,
32cd527f 1650 .vbi = &cx25840_vbi_ops,
9357b31c
HV
1651};
1652
bd985160
HV
1653/* ----------------------------------------------------------------------- */
1654
c7dd1ecd
AW
1655static u32 get_cx2388x_ident(struct i2c_client *client)
1656{
1657 u32 ret;
1658
1659 /* Come out of digital power down */
1660 cx25840_write(client, 0x000, 0);
1661
8c2d7821
ST
1662 /* Detecting whether the part is cx23885/7/8 is more
1663 * difficult than it needs to be. No ID register. Instead we
1664 * probe certain registers indicated in the datasheets to look
1665 * for specific defaults that differ between the silicon designs. */
1666
1667 /* It's either 885/7 if the IR Tx Clk Divider register exists */
c7dd1ecd 1668 if (cx25840_read4(client, 0x204) & 0xffff) {
8c2d7821
ST
1669 /* CX23885 returns bogus repetitive byte values for the DIF,
1670 * which doesn't exist for it. (Ex. 8a8a8a8a or 31313131) */
1671 ret = cx25840_read4(client, 0x300);
1672 if (((ret & 0xffff0000) >> 16) == (ret & 0xffff)) {
1673 /* No DIF */
1674 ret = V4L2_IDENT_CX23885_AV;
1675 } else {
1676 /* CX23887 has a broken DIF, but the registers
1677 * appear valid (but unsed), good enough to detect. */
1678 ret = V4L2_IDENT_CX23887_AV;
1679 }
c7dd1ecd
AW
1680 } else if (cx25840_read4(client, 0x300) & 0x0fffffff) {
1681 /* DIF PLL Freq Word reg exists; chip must be a CX23888 */
1682 ret = V4L2_IDENT_CX23888_AV;
1683 } else {
8c2d7821 1684 v4l_err(client, "Unable to detect h/w, assuming cx23887\n");
c7dd1ecd
AW
1685 ret = V4L2_IDENT_CX23887_AV;
1686 }
1687
1688 /* Back into digital power down */
1689 cx25840_write(client, 0x000, 2);
1690 return ret;
1691}
1692
d2653e92
JD
1693static int cx25840_probe(struct i2c_client *client,
1694 const struct i2c_device_id *did)
bd985160 1695{
bd985160 1696 struct cx25840_state *state;
9357b31c 1697 struct v4l2_subdev *sd;
c7dd1ecd 1698 u32 id = V4L2_IDENT_NONE;
bd985160
HV
1699 u16 device_id;
1700
188f3457
HV
1701 /* Check if the adapter supports the needed features */
1702 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1703 return -EIO;
1704
21340ae0 1705 v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", client->addr << 1);
bd985160
HV
1706
1707 device_id = cx25840_read(client, 0x101) << 8;
1708 device_id |= cx25840_read(client, 0x100);
f234081b 1709 v4l_dbg(1, cx25840_debug, client, "device_id = 0x%04x\n", device_id);
bd985160
HV
1710
1711 /* The high byte of the device ID should be
e2b8cf4c
HV
1712 * 0x83 for the cx2583x and 0x84 for the cx2584x */
1713 if ((device_id & 0xff00) == 0x8300) {
1714 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
c7dd1ecd 1715 } else if ((device_id & 0xff00) == 0x8400) {
e2b8cf4c 1716 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
00ca7324 1717 } else if (device_id == 0x0000) {
c7dd1ecd 1718 id = get_cx2388x_ident(client);
149783b5 1719 } else if ((device_id & 0xfff0) == 0x5A30) {
c7dd1ecd
AW
1720 /* The CX23100 (0x5A3C = 23100) doesn't have an A/V decoder */
1721 id = V4L2_IDENT_CX2310X_AV;
1722 } else if ((device_id & 0xff) == (device_id >> 8)) {
1723 v4l_err(client,
1724 "likely a confused/unresponsive cx2388[578] A/V decoder"
1725 " found @ 0x%x (%s)\n",
1726 client->addr << 1, client->adapter->name);
1727 v4l_err(client, "A method to reset it from the cx25840 driver"
1728 " software is not known at this time\n");
1729 return -ENODEV;
1730 } else {
b5fc7144 1731 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
188f3457 1732 return -ENODEV;
bd985160
HV
1733 }
1734
21340ae0 1735 state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
9357b31c 1736 if (state == NULL)
21340ae0 1737 return -ENOMEM;
21340ae0 1738
9357b31c
HV
1739 sd = &state->sd;
1740 v4l2_i2c_subdev_init(sd, client, &cx25840_ops);
c7dd1ecd
AW
1741 switch (id) {
1742 case V4L2_IDENT_CX23885_AV:
c7dd1ecd
AW
1743 v4l_info(client, "cx23885 A/V decoder found @ 0x%x (%s)\n",
1744 client->addr << 1, client->adapter->name);
1745 break;
1746 case V4L2_IDENT_CX23887_AV:
c7dd1ecd
AW
1747 v4l_info(client, "cx23887 A/V decoder found @ 0x%x (%s)\n",
1748 client->addr << 1, client->adapter->name);
1749 break;
1750 case V4L2_IDENT_CX23888_AV:
c7dd1ecd
AW
1751 v4l_info(client, "cx23888 A/V decoder found @ 0x%x (%s)\n",
1752 client->addr << 1, client->adapter->name);
1753 break;
1754 case V4L2_IDENT_CX2310X_AV:
c7dd1ecd
AW
1755 v4l_info(client, "cx%d A/V decoder found @ 0x%x (%s)\n",
1756 device_id, client->addr << 1, client->adapter->name);
1757 break;
1758 case V4L2_IDENT_CX25840:
1759 case V4L2_IDENT_CX25841:
1760 case V4L2_IDENT_CX25842:
1761 case V4L2_IDENT_CX25843:
1762 /* Note: revision '(device_id & 0x0f) == 2' was never built. The
1763 marking skips from 0x1 == 22 to 0x3 == 23. */
1764 v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
1765 (device_id & 0xfff0) >> 4,
1766 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1
1767 : (device_id & 0x0f),
1768 client->addr << 1, client->adapter->name);
1769 break;
1770 case V4L2_IDENT_CX25836:
1771 case V4L2_IDENT_CX25837:
c7dd1ecd
AW
1772 default:
1773 v4l_info(client, "cx25%3x-%x found @ 0x%x (%s)\n",
1774 (device_id & 0xfff0) >> 4, device_id & 0x0f,
1775 client->addr << 1, client->adapter->name);
1776 break;
1777 }
bd985160 1778
21340ae0 1779 state->c = client;
a8bbf12a
HV
1780 state->vid_input = CX25840_COMPOSITE7;
1781 state->aud_input = CX25840_AUDIO8;
3578d3dd 1782 state->audclk_freq = 48000;
a8bbf12a 1783 state->pvr150_workaround = 0;
8a4b275f 1784 state->audmode = V4L2_TUNER_MODE_LANG1;
87410dab 1785 state->unmute_volume = -1;
ca130eef
HV
1786 state->default_volume = 228 - cx25840_read(client, 0x8d4);
1787 state->default_volume = ((state->default_volume / 2) + 23) << 9;
3e3bf277 1788 state->vbi_line_offset = 8;
e2b8cf4c 1789 state->id = id;
3434eb7e 1790 state->rev = device_id;
f234081b 1791
bd985160
HV
1792 return 0;
1793}
1794
1a39275a 1795static int cx25840_remove(struct i2c_client *client)
bd985160 1796{
9357b31c
HV
1797 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1798
1799 v4l2_device_unregister_subdev(sd);
1800 kfree(to_state(sd));
bd985160
HV
1801 return 0;
1802}
1803
af294867
JD
1804static const struct i2c_device_id cx25840_id[] = {
1805 { "cx25840", 0 },
1806 { }
1807};
1808MODULE_DEVICE_TABLE(i2c, cx25840_id);
1809
1a39275a
HV
1810static struct v4l2_i2c_driver_data v4l2_i2c_data = {
1811 .name = "cx25840",
1a39275a
HV
1812 .probe = cx25840_probe,
1813 .remove = cx25840_remove,
af294867 1814 .id_table = cx25840_id,
bd985160 1815};