blob: 07607264bd41d990a42de2961e7faec0ce9d44c1 [file] [log] [blame]
Hans Verkuilbd985162005-11-13 16:07:56 -08001/* cx25840 - Conexant CX25840 audio/video decoder driver
2 *
3 * Copyright (C) 2004 Ulf Eklund
4 *
5 * Based on the saa7115 driver and on the first verison of Chris Kennedy's
6 * cx25840 driver.
7 *
8 * Changes by Tyler Trafford <tatrafford@comcast.net>
9 * - cleanup/rewrite for V4L2 API (2005)
10 *
11 * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
12 *
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>
Hans Verkuilbd985162005-11-13 16:07:56 -080035#include <media/v4l2-common.h>
36
37#include "cx25840.h"
38
39MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
Hans Verkuil1f4b3362005-11-13 16:08:05 -080040MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
Hans Verkuilbd985162005-11-13 16:07:56 -080041MODULE_LICENSE("GPL");
42
43static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
44
45
46int cx25840_debug = 0;
47
48module_param(cx25840_debug, bool, 0644);
49
50MODULE_PARM_DESC(cx25840_debug, "Debugging messages [0=Off (default) 1=On]");
51
52I2C_CLIENT_INSMOD;
53
54/* ----------------------------------------------------------------------- */
55
56int 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
65int 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
77u8 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
92u32 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
108int 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
Hans Verkuila8bbf122006-01-09 15:25:42 -0200118static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
119 enum cx25840_audio_input aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800120static void log_status(struct i2c_client *client);
121
122/* ----------------------------------------------------------------------- */
123
124static 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
138static 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
150static 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
Hans Verkuila8bbf122006-01-09 15:25:42 -0200198 /* (re)set input */
199 set_input(client, state->vid_input, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800200
201 /* start microcontroller */
202 cx25840_and_or(client, 0x803, ~0x10, 0x10);
203}
204
205/* ----------------------------------------------------------------------- */
206
207static void input_change(struct i2c_client *client)
208{
Hans Verkuilf95006f2005-12-01 00:51:42 -0800209 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800210 v4l2_std_id std = cx25840_get_v4lstd(client);
211
Hans Verkuilf95006f2005-12-01 00:51:42 -0800212 /* Note: perhaps V4L2_STD_PAL_M should be handled as V4L2_STD_NTSC
213 instead of V4L2_STD_PAL. Someone needs to test this. */
Hans Verkuilbd985162005-11-13 16:07:56 -0800214 if (std & V4L2_STD_PAL) {
215 /* Follow tuner change procedure for PAL */
216 cx25840_write(client, 0x808, 0xff);
217 cx25840_write(client, 0x80b, 0x10);
218 } else if (std & V4L2_STD_SECAM) {
219 /* Select autodetect for SECAM */
220 cx25840_write(client, 0x808, 0xff);
221 cx25840_write(client, 0x80b, 0x10);
222 } else if (std & V4L2_STD_NTSC) {
223 /* NTSC */
Hans Verkuila8bbf122006-01-09 15:25:42 -0200224 if (state->pvr150_workaround) {
Hans Verkuilf95006f2005-12-01 00:51:42 -0800225 /* Certain Hauppauge PVR150 models have a hardware bug
226 that causes audio to drop out. For these models the
227 audio standard must be set explicitly.
228 To be precise: it affects cards with tuner models
229 85, 99 and 112 (model numbers from tveeprom). */
230 if (std == V4L2_STD_NTSC_M_JP) {
231 /* Japan uses EIAJ audio standard */
232 cx25840_write(client, 0x808, 0x2f);
233 } else {
234 /* Others use the BTSC audio standard */
235 cx25840_write(client, 0x808, 0x1f);
236 }
237 /* South Korea uses the A2-M (aka Zweiton M) audio
238 standard, and should set 0x808 to 0x3f, but I don't
239 know how to detect this. */
240 } else if (std == V4L2_STD_NTSC_M_JP) {
241 /* Japan uses EIAJ audio standard */
242 cx25840_write(client, 0x808, 0xf7);
243 } else {
244 /* Others use the BTSC audio standard */
245 cx25840_write(client, 0x808, 0xf6);
246 }
247 /* South Korea uses the A2-M (aka Zweiton M) audio standard,
248 and should set 0x808 to 0xf8, but I don't know how to
249 detect this. */
Hans Verkuilbd985162005-11-13 16:07:56 -0800250 cx25840_write(client, 0x80b, 0x00);
251 }
252
253 if (cx25840_read(client, 0x803) & 0x10) {
254 /* restart audio decoder microcontroller */
255 cx25840_and_or(client, 0x803, ~0x10, 0x00);
256 cx25840_and_or(client, 0x803, ~0x10, 0x10);
257 }
258}
259
Hans Verkuila8bbf122006-01-09 15:25:42 -0200260static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
261 enum cx25840_audio_input aud_input)
Hans Verkuilbd985162005-11-13 16:07:56 -0800262{
263 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200264 u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
265 vid_input <= CX25840_COMPOSITE8);
266 u8 reg;
Hans Verkuilbd985162005-11-13 16:07:56 -0800267
Hans Verkuila8bbf122006-01-09 15:25:42 -0200268 cx25840_dbg("decoder set video input %d, audio input %d\n",
269 vid_input, aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800270
Hans Verkuila8bbf122006-01-09 15:25:42 -0200271 if (is_composite) {
272 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
273 } else {
274 int luma = vid_input & 0xf0;
275 int chroma = vid_input & 0xf00;
Hans Verkuilbd985162005-11-13 16:07:56 -0800276
Hans Verkuila8bbf122006-01-09 15:25:42 -0200277 if ((vid_input & ~0xff0) ||
278 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
279 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
280 cx25840_err("0x%04x is not a valid video input!\n", vid_input);
281 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800282 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200283 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
284 if (chroma >= CX25840_SVIDEO_CHROMA7) {
285 reg &= 0x3f;
286 reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
Hans Verkuilbd985162005-11-13 16:07:56 -0800287 } else {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200288 reg &= 0xcf;
289 reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
Hans Verkuilbd985162005-11-13 16:07:56 -0800290 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200291 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800292
Hans Verkuila8bbf122006-01-09 15:25:42 -0200293 switch (aud_input) {
294 case CX25840_AUDIO_SERIAL:
295 /* do nothing, use serial audio input */
Hans Verkuilbd985162005-11-13 16:07:56 -0800296 break;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200297 case CX25840_AUDIO4: reg &= ~0x30; break;
298 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
299 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
300 case CX25840_AUDIO7: reg &= ~0xc0; break;
301 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
Hans Verkuilbd985162005-11-13 16:07:56 -0800302
303 default:
Hans Verkuila8bbf122006-01-09 15:25:42 -0200304 cx25840_err("0x%04x is not a valid audio input!\n", aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800305 return -EINVAL;
306 }
307
Hans Verkuila8bbf122006-01-09 15:25:42 -0200308 cx25840_write(client, 0x103, reg);
309 /* Set INPUT_MODE to Composite (0) or S-Video (1) */
310 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
311 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
312 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
313 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
314 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
315 cx25840_and_or(client, 0x102, ~0x4, 4);
316 else
317 cx25840_and_or(client, 0x102, ~0x4, 0);
318
319 state->vid_input = vid_input;
320 state->aud_input = aud_input;
321 cx25840_audio_set_path(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800322 input_change(client);
323 return 0;
324}
325
326/* ----------------------------------------------------------------------- */
327
328static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
329{
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200330 u8 fmt=0; /* zero is autodetect */
Hans Verkuilbd985162005-11-13 16:07:56 -0800331
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200332 /* First tests should be against specific std */
333 if (std & V4L2_STD_NTSC_M_JP) {
334 fmt=0x2;
335 } else if (std & V4L2_STD_NTSC_443) {
336 fmt=0x3;
337 } else if (std & V4L2_STD_PAL_M) {
338 fmt=0x5;
339 } else if (std & V4L2_STD_PAL_N) {
340 fmt=0x6;
341 } else if (std & V4L2_STD_PAL_Nc) {
342 fmt=0x7;
343 } else if (std & V4L2_STD_PAL_60) {
344 fmt=0x8;
345 } else {
346 /* Then, test against generic ones */
347 if (std & V4L2_STD_NTSC) {
348 fmt=0x1;
349 } else if (std & V4L2_STD_PAL) {
350 fmt=0x4;
351 } else if (std & V4L2_STD_SECAM) {
352 fmt=0xc;
353 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800354 }
355
356 cx25840_and_or(client, 0x400, ~0xf, fmt);
357 cx25840_vbi_setup(client);
358 return 0;
359}
360
361v4l2_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
387static 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) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200392 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
393 state->pvr150_workaround = ctrl->value;
394 set_input(client, state->vid_input, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800395 break;
396
397 case V4L2_CID_BRIGHTNESS:
398 if (ctrl->value < 0 || ctrl->value > 255) {
399 cx25840_err("invalid brightness setting %d\n",
400 ctrl->value);
401 return -ERANGE;
402 }
403
404 cx25840_write(client, 0x414, ctrl->value - 128);
405 break;
406
407 case V4L2_CID_CONTRAST:
408 if (ctrl->value < 0 || ctrl->value > 127) {
409 cx25840_err("invalid contrast setting %d\n",
410 ctrl->value);
411 return -ERANGE;
412 }
413
414 cx25840_write(client, 0x415, ctrl->value << 1);
415 break;
416
417 case V4L2_CID_SATURATION:
418 if (ctrl->value < 0 || ctrl->value > 127) {
419 cx25840_err("invalid saturation setting %d\n",
420 ctrl->value);
421 return -ERANGE;
422 }
423
424 cx25840_write(client, 0x420, ctrl->value << 1);
425 cx25840_write(client, 0x421, ctrl->value << 1);
426 break;
427
428 case V4L2_CID_HUE:
429 if (ctrl->value < -127 || ctrl->value > 127) {
430 cx25840_err("invalid hue setting %d\n", ctrl->value);
431 return -ERANGE;
432 }
433
434 cx25840_write(client, 0x422, ctrl->value);
435 break;
436
437 case V4L2_CID_AUDIO_VOLUME:
438 case V4L2_CID_AUDIO_BASS:
439 case V4L2_CID_AUDIO_TREBLE:
440 case V4L2_CID_AUDIO_BALANCE:
441 case V4L2_CID_AUDIO_MUTE:
442 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200443
444 default:
445 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800446 }
447
448 return 0;
449}
450
451static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
452{
453 struct cx25840_state *state = i2c_get_clientdata(client);
454
455 switch (ctrl->id) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200456 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
457 ctrl->value = state->pvr150_workaround;
Hans Verkuilbd985162005-11-13 16:07:56 -0800458 break;
459 case V4L2_CID_BRIGHTNESS:
460 ctrl->value = cx25840_read(client, 0x414) + 128;
461 break;
462 case V4L2_CID_CONTRAST:
463 ctrl->value = cx25840_read(client, 0x415) >> 1;
464 break;
465 case V4L2_CID_SATURATION:
466 ctrl->value = cx25840_read(client, 0x420) >> 1;
467 break;
468 case V4L2_CID_HUE:
469 ctrl->value = cx25840_read(client, 0x422);
470 break;
471 case V4L2_CID_AUDIO_VOLUME:
472 case V4L2_CID_AUDIO_BASS:
473 case V4L2_CID_AUDIO_TREBLE:
474 case V4L2_CID_AUDIO_BALANCE:
475 case V4L2_CID_AUDIO_MUTE:
476 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
477 default:
478 return -EINVAL;
479 }
480
481 return 0;
482}
483
484/* ----------------------------------------------------------------------- */
485
486static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
487{
488 switch (fmt->type) {
489 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
490 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
491 default:
492 return -EINVAL;
493 }
494
495 return 0;
496}
497
498static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
499{
500 struct v4l2_pix_format *pix;
501 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
502 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
503
504 switch (fmt->type) {
505 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
506 pix = &(fmt->fmt.pix);
507
508 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
509 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
510
511 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
512 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
513
514 Vlines = pix->height + (is_pal ? 4 : 7);
515
516 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
517 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
518 cx25840_err("%dx%d is not a valid size!\n",
519 pix->width, pix->height);
520 return -ERANGE;
521 }
522
523 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
524 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
525 VSC &= 0x1fff;
526
527 if (pix->width >= 385)
528 filter = 0;
529 else if (pix->width > 192)
530 filter = 1;
531 else if (pix->width > 96)
532 filter = 2;
533 else
534 filter = 3;
535
536 cx25840_dbg("decoder set size %dx%d -> scale %ux%u\n",
537 pix->width, pix->height, HSC, VSC);
538
539 /* HSCALE=HSC */
540 cx25840_write(client, 0x418, HSC & 0xff);
541 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
542 cx25840_write(client, 0x41a, HSC >> 16);
543 /* VSCALE=VSC */
544 cx25840_write(client, 0x41c, VSC & 0xff);
545 cx25840_write(client, 0x41d, VSC >> 8);
546 /* VS_INTRLACE=1 VFILT=filter */
547 cx25840_write(client, 0x41e, 0x8 | filter);
548 break;
549
550 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
551 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
552
553 case V4L2_BUF_TYPE_VBI_CAPTURE:
554 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
555
556 default:
557 return -EINVAL;
558 }
559
560 return 0;
561}
562
563/* ----------------------------------------------------------------------- */
564
565static int cx25840_command(struct i2c_client *client, unsigned int cmd,
566 void *arg)
567{
568 struct cx25840_state *state = i2c_get_clientdata(client);
569 struct v4l2_tuner *vt = arg;
Hans Verkuilbd985162005-11-13 16:07:56 -0800570
571 switch (cmd) {
Hans Verkuilbd985162005-11-13 16:07:56 -0800572#ifdef CONFIG_VIDEO_ADV_DEBUG
573 /* ioctls to allow direct access to the
574 * cx25840 registers for testing */
575 case VIDIOC_INT_G_REGISTER:
576 {
577 struct v4l2_register *reg = arg;
578
579 if (reg->i2c_id != I2C_DRIVERID_CX25840)
580 return -EINVAL;
581 reg->val = cx25840_read(client, reg->reg & 0x0fff);
582 break;
583 }
584
585 case VIDIOC_INT_S_REGISTER:
586 {
587 struct v4l2_register *reg = arg;
588
589 if (reg->i2c_id != I2C_DRIVERID_CX25840)
590 return -EINVAL;
591 if (!capable(CAP_SYS_ADMIN))
592 return -EPERM;
593 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
594 break;
595 }
596#endif
597
598 case VIDIOC_INT_DECODE_VBI_LINE:
599 return cx25840_vbi(client, cmd, arg);
600
601 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200602 return cx25840_audio(client, cmd, arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800603
604 case VIDIOC_STREAMON:
605 cx25840_dbg("enable output\n");
606 cx25840_write(client, 0x115, 0x8c);
607 cx25840_write(client, 0x116, 0x07);
608 break;
609
610 case VIDIOC_STREAMOFF:
611 cx25840_dbg("disable output\n");
612 cx25840_write(client, 0x115, 0x00);
613 cx25840_write(client, 0x116, 0x00);
614 break;
615
616 case VIDIOC_LOG_STATUS:
617 log_status(client);
618 break;
619
620 case VIDIOC_G_CTRL:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200621 return get_v4lctrl(client, (struct v4l2_control *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800622
623 case VIDIOC_S_CTRL:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200624 return set_v4lctrl(client, (struct v4l2_control *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800625
626 case VIDIOC_G_STD:
627 *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
628 break;
629
630 case VIDIOC_S_STD:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200631 state->radio = 0;
632 return set_v4lstd(client, *(v4l2_std_id *)arg);
633
634 case AUDC_SET_RADIO:
635 state->radio = 1;
Hans Verkuilbd985162005-11-13 16:07:56 -0800636 break;
637
638 case VIDIOC_G_INPUT:
Hans Verkuila8bbf122006-01-09 15:25:42 -0200639 *(int *)arg = state->vid_input;
Hans Verkuilbd985162005-11-13 16:07:56 -0800640 break;
641
642 case VIDIOC_S_INPUT:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200643 return set_input(client, *(enum cx25840_video_input *)arg, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800644
Hans Verkuila8bbf122006-01-09 15:25:42 -0200645 case VIDIOC_S_AUDIO:
646 {
647 struct v4l2_audio *input = arg;
648
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200649 return set_input(client, state->vid_input, input->index);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200650 }
651
652 case VIDIOC_G_AUDIO:
653 {
654 struct v4l2_audio *input = arg;
655
656 memset(input, 0, sizeof(*input));
657 input->index = state->aud_input;
658 break;
659 }
660
Hans Verkuilbd985162005-11-13 16:07:56 -0800661 case VIDIOC_S_FREQUENCY:
662 input_change(client);
663 break;
664
665 case VIDIOC_G_TUNER:
666 {
667 u8 mode = cx25840_read(client, 0x804);
668 u8 pref = cx25840_read(client, 0x809) & 0xf;
669 u8 vpres = cx25840_read(client, 0x80a) & 0x10;
670 int val = 0;
671
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200672 if (state->radio)
673 break;
674
Hans Verkuilbd985162005-11-13 16:07:56 -0800675 vt->capability |=
676 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
677 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
678
679 vt->signal = vpres ? 0xffff : 0x0;
680
681 /* get rxsubchans and audmode */
682 if ((mode & 0xf) == 1)
683 val |= V4L2_TUNER_SUB_STEREO;
684 else
685 val |= V4L2_TUNER_SUB_MONO;
686
687 if (mode == 2 || mode == 4)
688 val |= V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
689
690 if (mode & 0x10)
691 val |= V4L2_TUNER_SUB_SAP;
692
693 vt->rxsubchans = val;
694
695 switch (pref) {
696 case 0:
697 vt->audmode = V4L2_TUNER_MODE_MONO;
698 break;
699 case 1:
700 case 2:
701 vt->audmode = V4L2_TUNER_MODE_LANG2;
702 break;
703 case 4:
704 default:
705 vt->audmode = V4L2_TUNER_MODE_STEREO;
706 }
707 break;
708 }
709
710 case VIDIOC_S_TUNER:
711 switch (vt->audmode) {
712 case V4L2_TUNER_MODE_MONO:
713 case V4L2_TUNER_MODE_LANG1:
714 /* Force PREF_MODE to MONO */
715 cx25840_and_or(client, 0x809, ~0xf, 0x00);
716 break;
717 case V4L2_TUNER_MODE_STEREO:
718 /* Force PREF_MODE to STEREO */
719 cx25840_and_or(client, 0x809, ~0xf, 0x04);
720 break;
721 case V4L2_TUNER_MODE_LANG2:
722 /* Force PREF_MODE to LANG2 */
723 cx25840_and_or(client, 0x809, ~0xf, 0x01);
724 break;
725 }
726 break;
727
728 case VIDIOC_G_FMT:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200729 return get_v4lfmt(client, (struct v4l2_format *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800730
731 case VIDIOC_S_FMT:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200732 return set_v4lfmt(client, (struct v4l2_format *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800733
734 case VIDIOC_INT_RESET:
735 cx25840_initialize(client, 0);
736 break;
737
738 case VIDIOC_INT_G_CHIP_IDENT:
739 *(enum v4l2_chip_ident *)arg =
740 V4L2_IDENT_CX25840 + ((cx25840_read(client, 0x100) >> 4) & 0xf);
741 break;
742
743 default:
Hans Verkuilbd985162005-11-13 16:07:56 -0800744 return -EINVAL;
745 }
746
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200747 return 0;
Hans Verkuilbd985162005-11-13 16:07:56 -0800748}
749
750/* ----------------------------------------------------------------------- */
751
Mauro Carvalho Chehab769e2432005-12-01 00:51:35 -0800752static struct i2c_driver i2c_driver_cx25840;
Hans Verkuilbd985162005-11-13 16:07:56 -0800753
754static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
755 int kind)
756{
757 struct i2c_client *client;
758 struct cx25840_state *state;
759 u16 device_id;
760
761 /* Check if the adapter supports the needed features
762 * Not until kernel version 2.6.11 did the bit-algo
763 * correctly report that it would do an I2C-level xfer */
764 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
765 return 0;
766
767 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
768 if (client == 0)
769 return -ENOMEM;
770
771 memset(client, 0, sizeof(struct i2c_client));
772 client->addr = address;
773 client->adapter = adapter;
774 client->driver = &i2c_driver_cx25840;
Hans Verkuilbd985162005-11-13 16:07:56 -0800775 snprintf(client->name, sizeof(client->name) - 1, "cx25840");
776
777 cx25840_dbg("detecting cx25840 client on address 0x%x\n", address << 1);
778
779 device_id = cx25840_read(client, 0x101) << 8;
780 device_id |= cx25840_read(client, 0x100);
781
782 /* The high byte of the device ID should be
783 * 0x84 if chip is present */
784 if ((device_id & 0xff00) != 0x8400) {
785 cx25840_dbg("cx25840 not found\n");
786 kfree(client);
787 return 0;
788 }
789
790 cx25840_info("cx25%3x-2%x found @ 0x%x (%s)\n",
791 (device_id & 0xfff0) >> 4,
792 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
793 address << 1, adapter->name);
794
795 state = kmalloc(sizeof(struct cx25840_state), GFP_KERNEL);
796 if (state == NULL) {
797 kfree(client);
798 return -ENOMEM;
799 }
800
801 i2c_set_clientdata(client, state);
802 memset(state, 0, sizeof(struct cx25840_state));
Hans Verkuila8bbf122006-01-09 15:25:42 -0200803 state->vid_input = CX25840_COMPOSITE7;
804 state->aud_input = CX25840_AUDIO8;
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200805 state->audclk_freq = 48000;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200806 state->pvr150_workaround = 0;
Hans Verkuilbd985162005-11-13 16:07:56 -0800807
808 cx25840_initialize(client, 1);
809
810 i2c_attach_client(client);
811
812 return 0;
813}
814
815static int cx25840_attach_adapter(struct i2c_adapter *adapter)
816{
Hans Verkuilbd985162005-11-13 16:07:56 -0800817 if (adapter->class & I2C_CLASS_TV_ANALOG)
Hans Verkuilbd985162005-11-13 16:07:56 -0800818 return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
819 return 0;
820}
821
822static 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
Mauro Carvalho Chehab769e2432005-12-01 00:51:35 -0800840static struct i2c_driver i2c_driver_cx25840 = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100841 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100842 .name = "cx25840",
843 },
Hans Verkuilbd985162005-11-13 16:07:56 -0800844
845 .id = I2C_DRIVERID_CX25840,
Hans Verkuilbd985162005-11-13 16:07:56 -0800846
847 .attach_adapter = cx25840_attach_adapter,
848 .detach_client = cx25840_detach_client,
849 .command = cx25840_command,
Hans Verkuilbd985162005-11-13 16:07:56 -0800850};
851
852
853static int __init m__init(void)
854{
855 return i2c_add_driver(&i2c_driver_cx25840);
856}
857
858static void __exit m__exit(void)
859{
860 i2c_del_driver(&i2c_driver_cx25840);
861}
862
863module_init(m__init);
864module_exit(m__exit);
865
866/* ----------------------------------------------------------------------- */
867
868static 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);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200890 int vid_input = state->vid_input;
891 int aud_input = state->aud_input;
Hans Verkuilbd985162005-11-13 16:07:56 -0800892 char *p;
893
894 cx25840_info("Video signal: %spresent\n",
895 (microctrl_vidfmt & 0x10) ? "" : "not ");
896 cx25840_info("Detected format: %s\n",
897 fmt_strs[gen_stat1 & 0xf]);
898
899 switch (mod_det_stat0) {
900 case 0x00: p = "mono"; break;
901 case 0x01: p = "stereo"; break;
902 case 0x02: p = "dual"; break;
903 case 0x04: p = "tri"; break;
904 case 0x10: p = "mono with SAP"; break;
905 case 0x11: p = "stereo with SAP"; break;
906 case 0x12: p = "dual with SAP"; break;
907 case 0x14: p = "tri with SAP"; break;
908 case 0xfe: p = "forced mode"; break;
909 default: p = "not defined";
910 }
911 cx25840_info("Detected audio mode: %s\n", p);
912
913 switch (mod_det_stat1) {
914 case 0x00: p = "not defined"; break;
915 case 0x01: p = "EIAJ"; break;
916 case 0x02: p = "A2-M"; break;
917 case 0x03: p = "A2-BG"; break;
918 case 0x04: p = "A2-DK1"; break;
919 case 0x05: p = "A2-DK2"; break;
920 case 0x06: p = "A2-DK3"; break;
921 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
922 case 0x08: p = "AM-L"; break;
923 case 0x09: p = "NICAM-BG"; break;
924 case 0x0a: p = "NICAM-DK"; break;
925 case 0x0b: p = "NICAM-I"; break;
926 case 0x0c: p = "NICAM-L"; break;
927 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
928 case 0x0e: p = "IF FM Radio"; break;
929 case 0x0f: p = "BTSC"; break;
930 case 0x10: p = "high-deviation FM"; break;
931 case 0x11: p = "very high-deviation FM"; break;
932 case 0xfd: p = "unknown audio standard"; break;
933 case 0xfe: p = "forced audio standard"; break;
934 case 0xff: p = "no detected audio standard"; break;
935 default: p = "not defined";
936 }
937 cx25840_info("Detected audio standard: %s\n", p);
938 cx25840_info("Audio muted: %s\n",
939 (mute_ctl & 0x2) ? "yes" : "no");
940 cx25840_info("Audio microcontroller: %s\n",
941 (download_ctl & 0x10) ? "running" : "stopped");
942
943 switch (audio_config >> 4) {
944 case 0x00: p = "undefined"; break;
945 case 0x01: p = "BTSC"; break;
946 case 0x02: p = "EIAJ"; break;
947 case 0x03: p = "A2-M"; break;
948 case 0x04: p = "A2-BG"; break;
949 case 0x05: p = "A2-DK1"; break;
950 case 0x06: p = "A2-DK2"; break;
951 case 0x07: p = "A2-DK3"; break;
952 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
953 case 0x09: p = "AM-L"; break;
954 case 0x0a: p = "NICAM-BG"; break;
955 case 0x0b: p = "NICAM-DK"; break;
956 case 0x0c: p = "NICAM-I"; break;
957 case 0x0d: p = "NICAM-L"; break;
958 case 0x0e: p = "FM radio"; break;
959 case 0x0f: p = "automatic detection"; break;
960 default: p = "undefined";
961 }
962 cx25840_info("Configured audio standard: %s\n", p);
963
964 if ((audio_config >> 4) < 0xF) {
965 switch (audio_config & 0xF) {
966 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
967 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
968 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
969 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
970 case 0x04: p = "STEREO"; break;
971 case 0x05: p = "DUAL1 (AB)"; break;
972 case 0x06: p = "DUAL2 (AC) (FM)"; break;
973 case 0x07: p = "DUAL3 (BC) (FM)"; break;
974 case 0x08: p = "DUAL4 (AC) (AM)"; break;
975 case 0x09: p = "DUAL5 (BC) (AM)"; break;
976 case 0x0a: p = "SAP"; break;
977 default: p = "undefined";
978 }
979 cx25840_info("Configured audio mode: %s\n", p);
980 } else {
981 switch (audio_config & 0xF) {
982 case 0x00: p = "BG"; break;
983 case 0x01: p = "DK1"; break;
984 case 0x02: p = "DK2"; break;
985 case 0x03: p = "DK3"; break;
986 case 0x04: p = "I"; break;
987 case 0x05: p = "L"; break;
988 case 0x06: p = "BTSC"; break;
989 case 0x07: p = "EIAJ"; break;
990 case 0x08: p = "A2-M"; break;
991 case 0x09: p = "FM Radio"; break;
992 case 0x0f: p = "automatic standard and mode detection"; break;
993 default: p = "undefined";
994 }
995 cx25840_info("Configured audio system: %s\n", p);
996 }
997
998 cx25840_info("Specified standard: %s\n",
999 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
1000
Hans Verkuila8bbf122006-01-09 15:25:42 -02001001 if (vid_input >= CX25840_COMPOSITE1 &&
1002 vid_input <= CX25840_COMPOSITE8) {
1003 cx25840_info("Specified video input: Composite %d\n",
1004 vid_input - CX25840_COMPOSITE1 + 1);
1005 } else {
1006 cx25840_info("Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
1007 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
Hans Verkuilbd985162005-11-13 16:07:56 -08001008 }
Hans Verkuila8bbf122006-01-09 15:25:42 -02001009 if (aud_input) {
1010 cx25840_info("Specified audio input: Tuner (In%d)\n", aud_input);
1011 } else {
1012 cx25840_info("Specified audio input: External\n");
1013 }
Hans Verkuilbd985162005-11-13 16:07:56 -08001014
Hans Verkuil3578d3d2006-01-09 15:25:41 -02001015 cx25840_info("Specified audioclock freq: %d Hz\n", state->audclk_freq);
Hans Verkuilbd985162005-11-13 16:07:56 -08001016
1017 switch (pref_mode & 0xf) {
1018 case 0: p = "mono/language A"; break;
1019 case 1: p = "language B"; break;
1020 case 2: p = "language C"; break;
1021 case 3: p = "analog fallback"; break;
1022 case 4: p = "stereo"; break;
1023 case 5: p = "language AC"; break;
1024 case 6: p = "language BC"; break;
1025 case 7: p = "language AB"; break;
1026 default: p = "undefined";
1027 }
1028 cx25840_info("Preferred audio mode: %s\n", p);
1029
1030 if ((audio_config & 0xf) == 0xf) {
1031 switch ((afc0 >> 3) & 0x3) {
1032 case 0: p = "system DK"; break;
1033 case 1: p = "system L"; break;
1034 case 2: p = "autodetect"; break;
1035 default: p = "undefined";
1036 }
1037 cx25840_info("Selected 65 MHz format: %s\n", p);
1038
1039 switch (afc0 & 0x7) {
1040 case 0: p = "chroma"; break;
1041 case 1: p = "BTSC"; break;
1042 case 2: p = "EIAJ"; break;
1043 case 3: p = "A2-M"; break;
1044 case 4: p = "autodetect"; break;
1045 default: p = "undefined";
1046 }
1047 cx25840_info("Selected 45 MHz format: %s\n", p);
1048 }
1049}