]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/video/cx25840/cx25840-core.c
Merge branch 'upstream-fixes'
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / cx25840 / cx25840-core.c
1 /* cx25840 - Conexant CX25840 audio/video decoder driver
2 *
3 * Copyright (C) 2004 Ulf Eklund
4 *
5 * Based on the saa7115 driver and on the first verison of Chris Kennedy's
6 * cx25840 driver.
7 *
8 * Changes by Tyler Trafford <tatrafford@comcast.net>
9 * - cleanup/rewrite for V4L2 API (2005)
10 *
11 * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 */
27
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/videodev2.h>
33 #include <linux/i2c.h>
34 #include <media/audiochip.h>
35 #include <media/v4l2-common.h>
36
37 #include "cx25840.h"
38
39 MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
40 MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
41 MODULE_LICENSE("GPL");
42
43 static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
44
45
46 int cx25840_debug = 0;
47
48 module_param(cx25840_debug, bool, 0644);
49
50 MODULE_PARM_DESC(cx25840_debug, "Debugging messages [0=Off (default) 1=On]");
51
52 I2C_CLIENT_INSMOD;
53
54 /* ----------------------------------------------------------------------- */
55
56 int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
57 {
58 u8 buffer[3];
59 buffer[0] = addr >> 8;
60 buffer[1] = addr & 0xff;
61 buffer[2] = value;
62 return i2c_master_send(client, buffer, 3);
63 }
64
65 int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
66 {
67 u8 buffer[6];
68 buffer[0] = addr >> 8;
69 buffer[1] = addr & 0xff;
70 buffer[2] = value >> 24;
71 buffer[3] = (value >> 16) & 0xff;
72 buffer[4] = (value >> 8) & 0xff;
73 buffer[5] = value & 0xff;
74 return i2c_master_send(client, buffer, 6);
75 }
76
77 u8 cx25840_read(struct i2c_client * client, u16 addr)
78 {
79 u8 buffer[2];
80 buffer[0] = addr >> 8;
81 buffer[1] = addr & 0xff;
82
83 if (i2c_master_send(client, buffer, 2) < 2)
84 return 0;
85
86 if (i2c_master_recv(client, buffer, 1) < 1)
87 return 0;
88
89 return buffer[0];
90 }
91
92 u32 cx25840_read4(struct i2c_client * client, u16 addr)
93 {
94 u8 buffer[4];
95 buffer[0] = addr >> 8;
96 buffer[1] = addr & 0xff;
97
98 if (i2c_master_send(client, buffer, 2) < 2)
99 return 0;
100
101 if (i2c_master_recv(client, buffer, 4) < 4)
102 return 0;
103
104 return (buffer[0] << 24) | (buffer[1] << 16) |
105 (buffer[2] << 8) | buffer[3];
106 }
107
108 int cx25840_and_or(struct i2c_client *client, u16 addr, u8 and_mask,
109 u8 or_value)
110 {
111 return cx25840_write(client, addr,
112 (cx25840_read(client, addr) & and_mask) |
113 or_value);
114 }
115
116 /* ----------------------------------------------------------------------- */
117
118 static int set_input(struct i2c_client *, enum cx25840_input);
119 static void input_change(struct i2c_client *);
120 static void log_status(struct i2c_client *client);
121
122 /* ----------------------------------------------------------------------- */
123
124 static inline void init_dll1(struct i2c_client *client)
125 {
126 /* This is the Hauppauge sequence used to
127 * initialize the Delay Lock Loop 1 (ADC DLL). */
128 cx25840_write(client, 0x159, 0x23);
129 cx25840_write(client, 0x15a, 0x87);
130 cx25840_write(client, 0x15b, 0x06);
131 cx25840_write(client, 0x159, 0xe1);
132 cx25840_write(client, 0x15a, 0x86);
133 cx25840_write(client, 0x159, 0xe0);
134 cx25840_write(client, 0x159, 0xe1);
135 cx25840_write(client, 0x15b, 0x10);
136 }
137
138 static inline void init_dll2(struct i2c_client *client)
139 {
140 /* This is the Hauppauge sequence used to
141 * initialize the Delay Lock Loop 2 (ADC DLL). */
142 cx25840_write(client, 0x15d, 0xe3);
143 cx25840_write(client, 0x15e, 0x86);
144 cx25840_write(client, 0x15f, 0x06);
145 cx25840_write(client, 0x15d, 0xe1);
146 cx25840_write(client, 0x15d, 0xe0);
147 cx25840_write(client, 0x15d, 0xe1);
148 }
149
150 static void cx25840_initialize(struct i2c_client *client, int loadfw)
151 {
152 struct cx25840_state *state = i2c_get_clientdata(client);
153
154 /* datasheet startup in numbered steps, refer to page 3-77 */
155 /* 2. */
156 cx25840_and_or(client, 0x803, ~0x10, 0x00);
157 /* The default of this register should be 4, but I get 0 instead.
158 * Set this register to 4 manually. */
159 cx25840_write(client, 0x000, 0x04);
160 /* 3. */
161 init_dll1(client);
162 init_dll2(client);
163 cx25840_write(client, 0x136, 0x0a);
164 /* 4. */
165 cx25840_write(client, 0x13c, 0x01);
166 cx25840_write(client, 0x13c, 0x00);
167 /* 5. */
168 if (loadfw)
169 cx25840_loadfw(client);
170 /* 6. */
171 cx25840_write(client, 0x115, 0x8c);
172 cx25840_write(client, 0x116, 0x07);
173 cx25840_write(client, 0x118, 0x02);
174 /* 7. */
175 cx25840_write(client, 0x4a5, 0x80);
176 cx25840_write(client, 0x4a5, 0x00);
177 cx25840_write(client, 0x402, 0x00);
178 /* 8. */
179 cx25840_write(client, 0x401, 0x18);
180 cx25840_write(client, 0x4a2, 0x10);
181 cx25840_write(client, 0x402, 0x04);
182 /* 10. */
183 cx25840_write(client, 0x8d3, 0x1f);
184 cx25840_write(client, 0x8e3, 0x03);
185
186 cx25840_vbi_setup(client);
187
188 /* trial and error says these are needed to get audio */
189 cx25840_write(client, 0x914, 0xa0);
190 cx25840_write(client, 0x918, 0xa0);
191 cx25840_write(client, 0x919, 0x01);
192
193 /* stereo prefered */
194 cx25840_write(client, 0x809, 0x04);
195 /* AC97 shift */
196 cx25840_write(client, 0x8cf, 0x0f);
197
198 /* (re)set video input */
199 set_input(client, state->input);
200 /* (re)set audio input */
201 cx25840_audio(client, AUDC_SET_INPUT, &state->audio_input);
202
203 /* start microcontroller */
204 cx25840_and_or(client, 0x803, ~0x10, 0x10);
205 }
206
207 /* ----------------------------------------------------------------------- */
208
209 static void input_change(struct i2c_client *client)
210 {
211 struct cx25840_state *state = i2c_get_clientdata(client);
212 v4l2_std_id std = cx25840_get_v4lstd(client);
213
214 /* Note: perhaps V4L2_STD_PAL_M should be handled as V4L2_STD_NTSC
215 instead of V4L2_STD_PAL. Someone needs to test this. */
216 if (std & V4L2_STD_PAL) {
217 /* Follow tuner change procedure for PAL */
218 cx25840_write(client, 0x808, 0xff);
219 cx25840_write(client, 0x80b, 0x10);
220 } else if (std & V4L2_STD_SECAM) {
221 /* Select autodetect for SECAM */
222 cx25840_write(client, 0x808, 0xff);
223 cx25840_write(client, 0x80b, 0x10);
224 } else if (std & V4L2_STD_NTSC) {
225 /* NTSC */
226 if (state->cardtype == CARDTYPE_PVR150_WORKAROUND) {
227 /* Certain Hauppauge PVR150 models have a hardware bug
228 that causes audio to drop out. For these models the
229 audio standard must be set explicitly.
230 To be precise: it affects cards with tuner models
231 85, 99 and 112 (model numbers from tveeprom). */
232 if (std == V4L2_STD_NTSC_M_JP) {
233 /* Japan uses EIAJ audio standard */
234 cx25840_write(client, 0x808, 0x2f);
235 } else {
236 /* Others use the BTSC audio standard */
237 cx25840_write(client, 0x808, 0x1f);
238 }
239 /* South Korea uses the A2-M (aka Zweiton M) audio
240 standard, and should set 0x808 to 0x3f, but I don't
241 know how to detect this. */
242 } else if (std == V4L2_STD_NTSC_M_JP) {
243 /* Japan uses EIAJ audio standard */
244 cx25840_write(client, 0x808, 0xf7);
245 } else {
246 /* Others use the BTSC audio standard */
247 cx25840_write(client, 0x808, 0xf6);
248 }
249 /* South Korea uses the A2-M (aka Zweiton M) audio standard,
250 and should set 0x808 to 0xf8, but I don't know how to
251 detect this. */
252 cx25840_write(client, 0x80b, 0x00);
253 }
254
255 if (cx25840_read(client, 0x803) & 0x10) {
256 /* restart audio decoder microcontroller */
257 cx25840_and_or(client, 0x803, ~0x10, 0x00);
258 cx25840_and_or(client, 0x803, ~0x10, 0x10);
259 }
260 }
261
262 static int set_input(struct i2c_client *client, enum cx25840_input input)
263 {
264 struct cx25840_state *state = i2c_get_clientdata(client);
265
266 cx25840_dbg("decoder set input (%d)\n", input);
267
268 switch (input) {
269 case CX25840_TUNER:
270 cx25840_dbg("now setting Tuner input\n");
271
272 if (state->cardtype == CARDTYPE_PVR150 ||
273 state->cardtype == CARDTYPE_PVR150_WORKAROUND) {
274 /* CH_SEL_ADC2=1 */
275 cx25840_and_or(client, 0x102, ~0x2, 0x02);
276 }
277
278 /* Video Input Control */
279 if (state->cardtype == CARDTYPE_PG600) {
280 cx25840_write(client, 0x103, 0x11);
281 } else {
282 cx25840_write(client, 0x103, 0x46);
283 }
284
285 /* INPUT_MODE=0 */
286 cx25840_and_or(client, 0x401, ~0x6, 0x00);
287 break;
288
289 case CX25840_COMPOSITE0:
290 case CX25840_COMPOSITE1:
291 cx25840_dbg("now setting Composite input\n");
292
293 /* Video Input Control */
294 if (state->cardtype == CARDTYPE_PG600) {
295 cx25840_write(client, 0x103, 0x00);
296 } else {
297 cx25840_write(client, 0x103, 0x02);
298 }
299
300 /* INPUT_MODE=0 */
301 cx25840_and_or(client, 0x401, ~0x6, 0x00);
302 break;
303
304 case CX25840_SVIDEO0:
305 case CX25840_SVIDEO1:
306 cx25840_dbg("now setting S-Video input\n");
307
308 /* CH_SEL_ADC2=0 */
309 cx25840_and_or(client, 0x102, ~0x2, 0x00);
310
311 /* Video Input Control */
312 if (state->cardtype == CARDTYPE_PG600) {
313 cx25840_write(client, 0x103, 0x02);
314 } else {
315 cx25840_write(client, 0x103, 0x10);
316 }
317
318 /* INPUT_MODE=1 */
319 cx25840_and_or(client, 0x401, ~0x6, 0x02);
320 break;
321
322 default:
323 cx25840_err("%d is not a valid input!\n", input);
324 return -EINVAL;
325 }
326
327 state->input = input;
328 input_change(client);
329 return 0;
330 }
331
332 /* ----------------------------------------------------------------------- */
333
334 static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
335 {
336 u8 fmt;
337
338 switch (std) {
339 /* zero is autodetect */
340 case 0: fmt = 0x0; break;
341 /* default ntsc to ntsc-m */
342 case V4L2_STD_NTSC:
343 case V4L2_STD_NTSC_M: fmt = 0x1; break;
344 case V4L2_STD_NTSC_M_JP: fmt = 0x2; break;
345 case V4L2_STD_NTSC_443: fmt = 0x3; break;
346 case V4L2_STD_PAL: fmt = 0x4; break;
347 case V4L2_STD_PAL_M: fmt = 0x5; break;
348 case V4L2_STD_PAL_N: fmt = 0x6; break;
349 case V4L2_STD_PAL_Nc: fmt = 0x7; break;
350 case V4L2_STD_PAL_60: fmt = 0x8; break;
351 case V4L2_STD_SECAM: fmt = 0xc; break;
352 default:
353 return -ERANGE;
354 }
355
356 cx25840_and_or(client, 0x400, ~0xf, fmt);
357 cx25840_vbi_setup(client);
358 return 0;
359 }
360
361 v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
362 {
363 /* check VID_FMT_SEL first */
364 u8 fmt = cx25840_read(client, 0x400) & 0xf;
365
366 if (!fmt) {
367 /* check AFD_FMT_STAT if set to autodetect */
368 fmt = cx25840_read(client, 0x40d) & 0xf;
369 }
370
371 switch (fmt) {
372 case 0x1: return V4L2_STD_NTSC_M;
373 case 0x2: return V4L2_STD_NTSC_M_JP;
374 case 0x3: return V4L2_STD_NTSC_443;
375 case 0x4: return V4L2_STD_PAL;
376 case 0x5: return V4L2_STD_PAL_M;
377 case 0x6: return V4L2_STD_PAL_N;
378 case 0x7: return V4L2_STD_PAL_Nc;
379 case 0x8: return V4L2_STD_PAL_60;
380 case 0xc: return V4L2_STD_SECAM;
381 default: return V4L2_STD_UNKNOWN;
382 }
383 }
384
385 /* ----------------------------------------------------------------------- */
386
387 static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
388 {
389 struct cx25840_state *state = i2c_get_clientdata(client);
390
391 switch (ctrl->id) {
392 case CX25840_CID_CARDTYPE:
393 switch (ctrl->value) {
394 case CARDTYPE_PVR150:
395 case CARDTYPE_PVR150_WORKAROUND:
396 case CARDTYPE_PG600:
397 state->cardtype = ctrl->value;
398 break;
399 default:
400 return -ERANGE;
401 }
402
403 set_input(client, state->input);
404 break;
405
406 case V4L2_CID_BRIGHTNESS:
407 if (ctrl->value < 0 || ctrl->value > 255) {
408 cx25840_err("invalid brightness setting %d\n",
409 ctrl->value);
410 return -ERANGE;
411 }
412
413 cx25840_write(client, 0x414, ctrl->value - 128);
414 break;
415
416 case V4L2_CID_CONTRAST:
417 if (ctrl->value < 0 || ctrl->value > 127) {
418 cx25840_err("invalid contrast setting %d\n",
419 ctrl->value);
420 return -ERANGE;
421 }
422
423 cx25840_write(client, 0x415, ctrl->value << 1);
424 break;
425
426 case V4L2_CID_SATURATION:
427 if (ctrl->value < 0 || ctrl->value > 127) {
428 cx25840_err("invalid saturation setting %d\n",
429 ctrl->value);
430 return -ERANGE;
431 }
432
433 cx25840_write(client, 0x420, ctrl->value << 1);
434 cx25840_write(client, 0x421, ctrl->value << 1);
435 break;
436
437 case V4L2_CID_HUE:
438 if (ctrl->value < -127 || ctrl->value > 127) {
439 cx25840_err("invalid hue setting %d\n", ctrl->value);
440 return -ERANGE;
441 }
442
443 cx25840_write(client, 0x422, ctrl->value);
444 break;
445
446 case V4L2_CID_AUDIO_VOLUME:
447 case V4L2_CID_AUDIO_BASS:
448 case V4L2_CID_AUDIO_TREBLE:
449 case V4L2_CID_AUDIO_BALANCE:
450 case V4L2_CID_AUDIO_MUTE:
451 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
452 }
453
454 return 0;
455 }
456
457 static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
458 {
459 struct cx25840_state *state = i2c_get_clientdata(client);
460
461 switch (ctrl->id) {
462 case CX25840_CID_CARDTYPE:
463 ctrl->value = state->cardtype;
464 break;
465 case V4L2_CID_BRIGHTNESS:
466 ctrl->value = cx25840_read(client, 0x414) + 128;
467 break;
468 case V4L2_CID_CONTRAST:
469 ctrl->value = cx25840_read(client, 0x415) >> 1;
470 break;
471 case V4L2_CID_SATURATION:
472 ctrl->value = cx25840_read(client, 0x420) >> 1;
473 break;
474 case V4L2_CID_HUE:
475 ctrl->value = cx25840_read(client, 0x422);
476 break;
477 case V4L2_CID_AUDIO_VOLUME:
478 case V4L2_CID_AUDIO_BASS:
479 case V4L2_CID_AUDIO_TREBLE:
480 case V4L2_CID_AUDIO_BALANCE:
481 case V4L2_CID_AUDIO_MUTE:
482 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
483 default:
484 return -EINVAL;
485 }
486
487 return 0;
488 }
489
490 /* ----------------------------------------------------------------------- */
491
492 static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
493 {
494 switch (fmt->type) {
495 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
496 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
497 default:
498 return -EINVAL;
499 }
500
501 return 0;
502 }
503
504 static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
505 {
506 struct v4l2_pix_format *pix;
507 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
508 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
509
510 switch (fmt->type) {
511 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
512 pix = &(fmt->fmt.pix);
513
514 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
515 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
516
517 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
518 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
519
520 Vlines = pix->height + (is_pal ? 4 : 7);
521
522 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
523 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
524 cx25840_err("%dx%d is not a valid size!\n",
525 pix->width, pix->height);
526 return -ERANGE;
527 }
528
529 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
530 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
531 VSC &= 0x1fff;
532
533 if (pix->width >= 385)
534 filter = 0;
535 else if (pix->width > 192)
536 filter = 1;
537 else if (pix->width > 96)
538 filter = 2;
539 else
540 filter = 3;
541
542 cx25840_dbg("decoder set size %dx%d -> scale %ux%u\n",
543 pix->width, pix->height, HSC, VSC);
544
545 /* HSCALE=HSC */
546 cx25840_write(client, 0x418, HSC & 0xff);
547 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
548 cx25840_write(client, 0x41a, HSC >> 16);
549 /* VSCALE=VSC */
550 cx25840_write(client, 0x41c, VSC & 0xff);
551 cx25840_write(client, 0x41d, VSC >> 8);
552 /* VS_INTRLACE=1 VFILT=filter */
553 cx25840_write(client, 0x41e, 0x8 | filter);
554 break;
555
556 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
557 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
558
559 case V4L2_BUF_TYPE_VBI_CAPTURE:
560 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
561
562 default:
563 return -EINVAL;
564 }
565
566 return 0;
567 }
568
569 /* ----------------------------------------------------------------------- */
570
571 static int cx25840_command(struct i2c_client *client, unsigned int cmd,
572 void *arg)
573 {
574 struct cx25840_state *state = i2c_get_clientdata(client);
575 struct v4l2_tuner *vt = arg;
576 int result = 0;
577
578 switch (cmd) {
579 case 0:
580 break;
581
582 #ifdef CONFIG_VIDEO_ADV_DEBUG
583 /* ioctls to allow direct access to the
584 * cx25840 registers for testing */
585 case VIDIOC_INT_G_REGISTER:
586 {
587 struct v4l2_register *reg = arg;
588
589 if (reg->i2c_id != I2C_DRIVERID_CX25840)
590 return -EINVAL;
591 reg->val = cx25840_read(client, reg->reg & 0x0fff);
592 break;
593 }
594
595 case VIDIOC_INT_S_REGISTER:
596 {
597 struct v4l2_register *reg = arg;
598
599 if (reg->i2c_id != I2C_DRIVERID_CX25840)
600 return -EINVAL;
601 if (!capable(CAP_SYS_ADMIN))
602 return -EPERM;
603 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
604 break;
605 }
606 #endif
607
608 case VIDIOC_INT_DECODE_VBI_LINE:
609 return cx25840_vbi(client, cmd, arg);
610
611 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
612 case AUDC_SET_INPUT:
613 result = cx25840_audio(client, cmd, arg);
614 break;
615
616 case VIDIOC_STREAMON:
617 cx25840_dbg("enable output\n");
618 cx25840_write(client, 0x115, 0x8c);
619 cx25840_write(client, 0x116, 0x07);
620 break;
621
622 case VIDIOC_STREAMOFF:
623 cx25840_dbg("disable output\n");
624 cx25840_write(client, 0x115, 0x00);
625 cx25840_write(client, 0x116, 0x00);
626 break;
627
628 case VIDIOC_LOG_STATUS:
629 log_status(client);
630 break;
631
632 case VIDIOC_G_CTRL:
633 result = get_v4lctrl(client, (struct v4l2_control *)arg);
634 break;
635
636 case VIDIOC_S_CTRL:
637 result = set_v4lctrl(client, (struct v4l2_control *)arg);
638 break;
639
640 case VIDIOC_G_STD:
641 *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
642 break;
643
644 case VIDIOC_S_STD:
645 result = set_v4lstd(client, *(v4l2_std_id *)arg);
646 break;
647
648 case VIDIOC_G_INPUT:
649 *(int *)arg = state->input;
650 break;
651
652 case VIDIOC_S_INPUT:
653 result = set_input(client, *(int *)arg);
654 break;
655
656 case VIDIOC_S_FREQUENCY:
657 input_change(client);
658 break;
659
660 case VIDIOC_G_TUNER:
661 {
662 u8 mode = cx25840_read(client, 0x804);
663 u8 pref = cx25840_read(client, 0x809) & 0xf;
664 u8 vpres = cx25840_read(client, 0x80a) & 0x10;
665 int val = 0;
666
667 vt->capability |=
668 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
669 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
670
671 vt->signal = vpres ? 0xffff : 0x0;
672
673 /* get rxsubchans and audmode */
674 if ((mode & 0xf) == 1)
675 val |= V4L2_TUNER_SUB_STEREO;
676 else
677 val |= V4L2_TUNER_SUB_MONO;
678
679 if (mode == 2 || mode == 4)
680 val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
681
682 if (mode & 0x10)
683 val |= V4L2_TUNER_SUB_SAP;
684
685 vt->rxsubchans = val;
686
687 switch (pref) {
688 case 0:
689 vt->audmode = V4L2_TUNER_MODE_MONO;
690 break;
691 case 1:
692 case 2:
693 vt->audmode = V4L2_TUNER_MODE_LANG2;
694 break;
695 case 4:
696 default:
697 vt->audmode = V4L2_TUNER_MODE_STEREO;
698 }
699 break;
700 }
701
702 case VIDIOC_S_TUNER:
703 switch (vt->audmode) {
704 case V4L2_TUNER_MODE_MONO:
705 case V4L2_TUNER_MODE_LANG1:
706 /* Force PREF_MODE to MONO */
707 cx25840_and_or(client, 0x809, ~0xf, 0x00);
708 break;
709 case V4L2_TUNER_MODE_STEREO:
710 /* Force PREF_MODE to STEREO */
711 cx25840_and_or(client, 0x809, ~0xf, 0x04);
712 break;
713 case V4L2_TUNER_MODE_LANG2:
714 /* Force PREF_MODE to LANG2 */
715 cx25840_and_or(client, 0x809, ~0xf, 0x01);
716 break;
717 }
718 break;
719
720 case VIDIOC_G_FMT:
721 result = get_v4lfmt(client, (struct v4l2_format *)arg);
722 break;
723
724 case VIDIOC_S_FMT:
725 result = set_v4lfmt(client, (struct v4l2_format *)arg);
726 break;
727
728 case VIDIOC_INT_RESET:
729 cx25840_initialize(client, 0);
730 break;
731
732 case VIDIOC_INT_G_CHIP_IDENT:
733 *(enum v4l2_chip_ident *)arg =
734 V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
735 break;
736
737 default:
738 cx25840_err("invalid ioctl %x\n", cmd);
739 return -EINVAL;
740 }
741
742 return result;
743 }
744
745 /* ----------------------------------------------------------------------- */
746
747 static struct i2c_driver i2c_driver_cx25840;
748
749 static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
750 int kind)
751 {
752 struct i2c_client *client;
753 struct cx25840_state *state;
754 u16 device_id;
755
756 /* Check if the adapter supports the needed features
757 * Not until kernel version 2.6.11 did the bit-algo
758 * correctly report that it would do an I2C-level xfer */
759 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
760 return 0;
761
762 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
763 if (client == 0)
764 return -ENOMEM;
765
766 memset(client, 0, sizeof(struct i2c_client));
767 client->addr = address;
768 client->adapter = adapter;
769 client->driver = &i2c_driver_cx25840;
770 client->flags = I2C_CLIENT_ALLOW_USE;
771 snprintf(client->name, sizeof(client->name) - 1, "cx25840");
772
773 cx25840_dbg("detecting cx25840 client on address 0x%x\n", address << 1);
774
775 device_id = cx25840_read(client, 0x101) << 8;
776 device_id |= cx25840_read(client, 0x100);
777
778 /* The high byte of the device ID should be
779 * 0x84 if chip is present */
780 if ((device_id & 0xff00) != 0x8400) {
781 cx25840_dbg("cx25840 not found\n");
782 kfree(client);
783 return 0;
784 }
785
786 cx25840_info("cx25%3x-2%x found @ 0x%x (%s)\n",
787 (device_id & 0xfff0) >> 4,
788 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
789 address << 1, adapter->name);
790
791 state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
792 if (state == NULL) {
793 kfree(client);
794 return -ENOMEM;
795 }
796
797 i2c_set_clientdata(client, state);
798 memset(state, 0, sizeof(struct cx25840_state));
799 state->input = CX25840_TUNER;
800 state->audclk_freq = V4L2_AUDCLK_48_KHZ;
801 state->audio_input = AUDIO_TUNER;
802 state->cardtype = CARDTYPE_PVR150;
803
804 cx25840_initialize(client, 1);
805
806 i2c_attach_client(client);
807
808 return 0;
809 }
810
811 static int cx25840_attach_adapter(struct i2c_adapter *adapter)
812 {
813 #ifdef I2C_CLASS_TV_ANALOG
814 if (adapter->class & I2C_CLASS_TV_ANALOG)
815 #else
816 if (adapter->id == I2C_HW_B_BT848)
817 #endif
818 return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
819 return 0;
820 }
821
822 static int cx25840_detach_client(struct i2c_client *client)
823 {
824 struct cx25840_state *state = i2c_get_clientdata(client);
825 int err;
826
827 err = i2c_detach_client(client);
828 if (err) {
829 return err;
830 }
831
832 kfree(state);
833 kfree(client);
834
835 return 0;
836 }
837
838 /* ----------------------------------------------------------------------- */
839
840 static struct i2c_driver i2c_driver_cx25840 = {
841 .name = "cx25840",
842
843 .id = I2C_DRIVERID_CX25840,
844 .flags = I2C_DF_NOTIFY,
845
846 .attach_adapter = cx25840_attach_adapter,
847 .detach_client = cx25840_detach_client,
848 .command = cx25840_command,
849 .owner = THIS_MODULE,
850 };
851
852
853 static int __init m__init(void)
854 {
855 return i2c_add_driver(&i2c_driver_cx25840);
856 }
857
858 static void __exit m__exit(void)
859 {
860 i2c_del_driver(&i2c_driver_cx25840);
861 }
862
863 module_init(m__init);
864 module_exit(m__exit);
865
866 /* ----------------------------------------------------------------------- */
867
868 static void log_status(struct i2c_client *client)
869 {
870 static const char *const fmt_strs[] = {
871 "0x0",
872 "NTSC-M", "NTSC-J", "NTSC-4.43",
873 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
874 "0x9", "0xA", "0xB",
875 "SECAM",
876 "0xD", "0xE", "0xF"
877 };
878
879 struct cx25840_state *state = i2c_get_clientdata(client);
880 u8 microctrl_vidfmt = cx25840_read(client, 0x80a);
881 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
882 u8 gen_stat1 = cx25840_read(client, 0x40d);
883 u8 download_ctl = cx25840_read(client, 0x803);
884 u8 mod_det_stat0 = cx25840_read(client, 0x804);
885 u8 mod_det_stat1 = cx25840_read(client, 0x805);
886 u8 audio_config = cx25840_read(client, 0x808);
887 u8 pref_mode = cx25840_read(client, 0x809);
888 u8 afc0 = cx25840_read(client, 0x80b);
889 u8 mute_ctl = cx25840_read(client, 0x8d3);
890 char *p;
891
892 cx25840_info("Video signal: %spresent\n",
893 (microctrl_vidfmt & 0x10) ? "" : "not ");
894 cx25840_info("Detected format: %s\n",
895 fmt_strs[gen_stat1 & 0xf]);
896
897 switch (mod_det_stat0) {
898 case 0x00: p = "mono"; break;
899 case 0x01: p = "stereo"; break;
900 case 0x02: p = "dual"; break;
901 case 0x04: p = "tri"; break;
902 case 0x10: p = "mono with SAP"; break;
903 case 0x11: p = "stereo with SAP"; break;
904 case 0x12: p = "dual with SAP"; break;
905 case 0x14: p = "tri with SAP"; break;
906 case 0xfe: p = "forced mode"; break;
907 default: p = "not defined";
908 }
909 cx25840_info("Detected audio mode: %s\n", p);
910
911 switch (mod_det_stat1) {
912 case 0x00: p = "not defined"; break;
913 case 0x01: p = "EIAJ"; break;
914 case 0x02: p = "A2-M"; break;
915 case 0x03: p = "A2-BG"; break;
916 case 0x04: p = "A2-DK1"; break;
917 case 0x05: p = "A2-DK2"; break;
918 case 0x06: p = "A2-DK3"; break;
919 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
920 case 0x08: p = "AM-L"; break;
921 case 0x09: p = "NICAM-BG"; break;
922 case 0x0a: p = "NICAM-DK"; break;
923 case 0x0b: p = "NICAM-I"; break;
924 case 0x0c: p = "NICAM-L"; break;
925 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
926 case 0x0e: p = "IF FM Radio"; break;
927 case 0x0f: p = "BTSC"; break;
928 case 0x10: p = "high-deviation FM"; break;
929 case 0x11: p = "very high-deviation FM"; break;
930 case 0xfd: p = "unknown audio standard"; break;
931 case 0xfe: p = "forced audio standard"; break;
932 case 0xff: p = "no detected audio standard"; break;
933 default: p = "not defined";
934 }
935 cx25840_info("Detected audio standard: %s\n", p);
936 cx25840_info("Audio muted: %s\n",
937 (mute_ctl & 0x2) ? "yes" : "no");
938 cx25840_info("Audio microcontroller: %s\n",
939 (download_ctl & 0x10) ? "running" : "stopped");
940
941 switch (audio_config >> 4) {
942 case 0x00: p = "undefined"; break;
943 case 0x01: p = "BTSC"; break;
944 case 0x02: p = "EIAJ"; break;
945 case 0x03: p = "A2-M"; break;
946 case 0x04: p = "A2-BG"; break;
947 case 0x05: p = "A2-DK1"; break;
948 case 0x06: p = "A2-DK2"; break;
949 case 0x07: p = "A2-DK3"; break;
950 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
951 case 0x09: p = "AM-L"; break;
952 case 0x0a: p = "NICAM-BG"; break;
953 case 0x0b: p = "NICAM-DK"; break;
954 case 0x0c: p = "NICAM-I"; break;
955 case 0x0d: p = "NICAM-L"; break;
956 case 0x0e: p = "FM radio"; break;
957 case 0x0f: p = "automatic detection"; break;
958 default: p = "undefined";
959 }
960 cx25840_info("Configured audio standard: %s\n", p);
961
962 if ((audio_config >> 4) < 0xF) {
963 switch (audio_config & 0xF) {
964 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
965 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
966 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
967 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
968 case 0x04: p = "STEREO"; break;
969 case 0x05: p = "DUAL1 (AB)"; break;
970 case 0x06: p = "DUAL2 (AC) (FM)"; break;
971 case 0x07: p = "DUAL3 (BC) (FM)"; break;
972 case 0x08: p = "DUAL4 (AC) (AM)"; break;
973 case 0x09: p = "DUAL5 (BC) (AM)"; break;
974 case 0x0a: p = "SAP"; break;
975 default: p = "undefined";
976 }
977 cx25840_info("Configured audio mode: %s\n", p);
978 } else {
979 switch (audio_config & 0xF) {
980 case 0x00: p = "BG"; break;
981 case 0x01: p = "DK1"; break;
982 case 0x02: p = "DK2"; break;
983 case 0x03: p = "DK3"; break;
984 case 0x04: p = "I"; break;
985 case 0x05: p = "L"; break;
986 case 0x06: p = "BTSC"; break;
987 case 0x07: p = "EIAJ"; break;
988 case 0x08: p = "A2-M"; break;
989 case 0x09: p = "FM Radio"; break;
990 case 0x0f: p = "automatic standard and mode detection"; break;
991 default: p = "undefined";
992 }
993 cx25840_info("Configured audio system: %s\n", p);
994 }
995
996 cx25840_info("Specified standard: %s\n",
997 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
998
999 switch (state->input) {
1000 case CX25840_COMPOSITE0: p = "Composite 0"; break;
1001 case CX25840_COMPOSITE1: p = "Composite 1"; break;
1002 case CX25840_SVIDEO0: p = "S-Video 0"; break;
1003 case CX25840_SVIDEO1: p = "S-Video 1"; break;
1004 case CX25840_TUNER: p = "Tuner"; break;
1005 }
1006 cx25840_info("Specified input: %s\n", p);
1007 cx25840_info("Specified audio input: %s\n",
1008 state->audio_input == 0 ? "Tuner" : "External");
1009
1010 switch (state->audclk_freq) {
1011 case V4L2_AUDCLK_441_KHZ: p = "44.1 kHz"; break;
1012 case V4L2_AUDCLK_48_KHZ: p = "48 kHz"; break;
1013 case V4L2_AUDCLK_32_KHZ: p = "32 kHz"; break;
1014 default: p = "undefined";
1015 }
1016 cx25840_info("Specified audioclock freq: %s\n", p);
1017
1018 switch (pref_mode & 0xf) {
1019 case 0: p = "mono/language A"; break;
1020 case 1: p = "language B"; break;
1021 case 2: p = "language C"; break;
1022 case 3: p = "analog fallback"; break;
1023 case 4: p = "stereo"; break;
1024 case 5: p = "language AC"; break;
1025 case 6: p = "language BC"; break;
1026 case 7: p = "language AB"; break;
1027 default: p = "undefined";
1028 }
1029 cx25840_info("Preferred audio mode: %s\n", p);
1030
1031 if ((audio_config & 0xf) == 0xf) {
1032 switch ((afc0 >> 3) & 0x3) {
1033 case 0: p = "system DK"; break;
1034 case 1: p = "system L"; break;
1035 case 2: p = "autodetect"; break;
1036 default: p = "undefined";
1037 }
1038 cx25840_info("Selected 65 MHz format: %s\n", p);
1039
1040 switch (afc0 & 0x7) {
1041 case 0: p = "chroma"; break;
1042 case 1: p = "BTSC"; break;
1043 case 2: p = "EIAJ"; break;
1044 case 3: p = "A2-M"; break;
1045 case 4: p = "autodetect"; break;
1046 default: p = "undefined";
1047 }
1048 cx25840_info("Selected 45 MHz format: %s\n", p);
1049 }
1050 }