blob: 5c2036b40ea11e8373b101a3a9c2844ddbcf0dd5 [file] [log] [blame]
Hans Verkuilbd985162005-11-13 16:07:56 -08001/* cx25840 - Conexant CX25840 audio/video decoder driver
2 *
3 * Copyright (C) 2004 Ulf Eklund
4 *
5 * Based on the saa7115 driver and on the first verison of Chris Kennedy's
6 * cx25840 driver.
7 *
8 * Changes by Tyler Trafford <tatrafford@comcast.net>
9 * - cleanup/rewrite for V4L2 API (2005)
10 *
11 * VBI support by Hans Verkuil <hverkuil@xs4all.nl>.
12 *
Christopher Neufeld3e3bf272006-05-24 10:16:45 -030013 * NTSC sliced VBI support by Christopher Neufeld <television@cneufeld.ca>
14 * with additional fixes by Hans Verkuil <hverkuil@xs4all.nl>.
15 *
Hans Verkuilbd985162005-11-13 16:07:56 -080016 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 */
30
31
32#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/slab.h>
35#include <linux/videodev2.h>
36#include <linux/i2c.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080037#include <media/v4l2-common.h>
Hans Verkuil31bc09b2006-03-25 10:26:09 -030038#include <media/cx25840.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080039
Hans Verkuil31bc09b2006-03-25 10:26:09 -030040#include "cx25840-core.h"
Hans Verkuilbd985162005-11-13 16:07:56 -080041
42MODULE_DESCRIPTION("Conexant CX25840 audio/video decoder driver");
Hans Verkuil1f4b3362005-11-13 16:08:05 -080043MODULE_AUTHOR("Ulf Eklund, Chris Kennedy, Hans Verkuil, Tyler Trafford");
Hans Verkuilbd985162005-11-13 16:07:56 -080044MODULE_LICENSE("GPL");
45
46static unsigned short normal_i2c[] = { 0x88 >> 1, I2C_CLIENT_END };
47
48
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -030049int cx25840_debug;
Hans Verkuilbd985162005-11-13 16:07:56 -080050
Hans Verkuilb5fc7142006-01-11 22:41:36 -020051module_param_named(debug,cx25840_debug, int, 0644);
Hans Verkuilbd985162005-11-13 16:07:56 -080052
Hans Verkuilfac9e892006-01-09 15:32:40 -020053MODULE_PARM_DESC(debug, "Debugging messages [0=Off (default) 1=On]");
Hans Verkuilbd985162005-11-13 16:07:56 -080054
55I2C_CLIENT_INSMOD;
56
57/* ----------------------------------------------------------------------- */
58
59int cx25840_write(struct i2c_client *client, u16 addr, u8 value)
60{
61 u8 buffer[3];
62 buffer[0] = addr >> 8;
63 buffer[1] = addr & 0xff;
64 buffer[2] = value;
65 return i2c_master_send(client, buffer, 3);
66}
67
68int cx25840_write4(struct i2c_client *client, u16 addr, u32 value)
69{
70 u8 buffer[6];
71 buffer[0] = addr >> 8;
72 buffer[1] = addr & 0xff;
73 buffer[2] = value >> 24;
74 buffer[3] = (value >> 16) & 0xff;
75 buffer[4] = (value >> 8) & 0xff;
76 buffer[5] = value & 0xff;
77 return i2c_master_send(client, buffer, 6);
78}
79
80u8 cx25840_read(struct i2c_client * client, u16 addr)
81{
82 u8 buffer[2];
83 buffer[0] = addr >> 8;
84 buffer[1] = addr & 0xff;
85
86 if (i2c_master_send(client, buffer, 2) < 2)
87 return 0;
88
89 if (i2c_master_recv(client, buffer, 1) < 1)
90 return 0;
91
92 return buffer[0];
93}
94
95u32 cx25840_read4(struct i2c_client * client, u16 addr)
96{
97 u8 buffer[4];
98 buffer[0] = addr >> 8;
99 buffer[1] = addr & 0xff;
100
101 if (i2c_master_send(client, buffer, 2) < 2)
102 return 0;
103
104 if (i2c_master_recv(client, buffer, 4) < 4)
105 return 0;
106
107 return (buffer[0] << 24) | (buffer[1] << 16) |
108 (buffer[2] << 8) | buffer[3];
109}
110
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300111int cx25840_and_or(struct i2c_client *client, u16 addr, unsigned and_mask,
Hans Verkuilbd985162005-11-13 16:07:56 -0800112 u8 or_value)
113{
114 return cx25840_write(client, addr,
115 (cx25840_read(client, addr) & and_mask) |
116 or_value);
117}
118
119/* ----------------------------------------------------------------------- */
120
Hans Verkuila8bbf122006-01-09 15:25:42 -0200121static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
122 enum cx25840_audio_input aud_input);
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300123static void log_audio_status(struct i2c_client *client);
124static void log_video_status(struct i2c_client *client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800125
126/* ----------------------------------------------------------------------- */
127
Hans Verkuild92c20e2006-01-09 15:32:41 -0200128static void init_dll1(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800129{
130 /* This is the Hauppauge sequence used to
131 * initialize the Delay Lock Loop 1 (ADC DLL). */
132 cx25840_write(client, 0x159, 0x23);
133 cx25840_write(client, 0x15a, 0x87);
134 cx25840_write(client, 0x15b, 0x06);
135 cx25840_write(client, 0x159, 0xe1);
136 cx25840_write(client, 0x15a, 0x86);
137 cx25840_write(client, 0x159, 0xe0);
138 cx25840_write(client, 0x159, 0xe1);
139 cx25840_write(client, 0x15b, 0x10);
140}
141
Hans Verkuild92c20e2006-01-09 15:32:41 -0200142static void init_dll2(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800143{
144 /* This is the Hauppauge sequence used to
145 * initialize the Delay Lock Loop 2 (ADC DLL). */
146 cx25840_write(client, 0x15d, 0xe3);
147 cx25840_write(client, 0x15e, 0x86);
148 cx25840_write(client, 0x15f, 0x06);
149 cx25840_write(client, 0x15d, 0xe1);
150 cx25840_write(client, 0x15d, 0xe0);
151 cx25840_write(client, 0x15d, 0xe1);
152}
153
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300154static void cx25836_initialize(struct i2c_client *client)
155{
156 /* reset configuration is described on page 3-77 of the CX25836 datasheet */
157 /* 2. */
158 cx25840_and_or(client, 0x000, ~0x01, 0x01);
159 cx25840_and_or(client, 0x000, ~0x01, 0x00);
160 /* 3a. */
161 cx25840_and_or(client, 0x15a, ~0x70, 0x00);
162 /* 3b. */
163 cx25840_and_or(client, 0x15b, ~0x1e, 0x06);
164 /* 3c. */
165 cx25840_and_or(client, 0x159, ~0x02, 0x02);
166 /* 3d. */
167 /* There should be a 10-us delay here, but since the
168 i2c bus already has a 10-us delay we don't need to do
169 anything */
170 /* 3e. */
171 cx25840_and_or(client, 0x159, ~0x02, 0x00);
172 /* 3f. */
173 cx25840_and_or(client, 0x159, ~0xc0, 0xc0);
174 /* 3g. */
175 cx25840_and_or(client, 0x159, ~0x01, 0x00);
176 cx25840_and_or(client, 0x159, ~0x01, 0x01);
177 /* 3h. */
178 cx25840_and_or(client, 0x15b, ~0x1e, 0x10);
179}
180
Hans Verkuilbd985162005-11-13 16:07:56 -0800181static void cx25840_initialize(struct i2c_client *client, int loadfw)
182{
183 struct cx25840_state *state = i2c_get_clientdata(client);
184
185 /* datasheet startup in numbered steps, refer to page 3-77 */
186 /* 2. */
187 cx25840_and_or(client, 0x803, ~0x10, 0x00);
188 /* The default of this register should be 4, but I get 0 instead.
189 * Set this register to 4 manually. */
190 cx25840_write(client, 0x000, 0x04);
191 /* 3. */
192 init_dll1(client);
193 init_dll2(client);
194 cx25840_write(client, 0x136, 0x0a);
195 /* 4. */
196 cx25840_write(client, 0x13c, 0x01);
197 cx25840_write(client, 0x13c, 0x00);
198 /* 5. */
199 if (loadfw)
200 cx25840_loadfw(client);
201 /* 6. */
202 cx25840_write(client, 0x115, 0x8c);
203 cx25840_write(client, 0x116, 0x07);
204 cx25840_write(client, 0x118, 0x02);
205 /* 7. */
206 cx25840_write(client, 0x4a5, 0x80);
207 cx25840_write(client, 0x4a5, 0x00);
208 cx25840_write(client, 0x402, 0x00);
209 /* 8. */
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300210 cx25840_and_or(client, 0x401, ~0x18, 0);
211 cx25840_and_or(client, 0x4a2, ~0x10, 0x10);
212 /* steps 8c and 8d are done in change_input() */
Hans Verkuilbd985162005-11-13 16:07:56 -0800213 /* 10. */
214 cx25840_write(client, 0x8d3, 0x1f);
215 cx25840_write(client, 0x8e3, 0x03);
216
217 cx25840_vbi_setup(client);
218
219 /* trial and error says these are needed to get audio */
220 cx25840_write(client, 0x914, 0xa0);
221 cx25840_write(client, 0x918, 0xa0);
222 cx25840_write(client, 0x919, 0x01);
223
224 /* stereo prefered */
225 cx25840_write(client, 0x809, 0x04);
226 /* AC97 shift */
227 cx25840_write(client, 0x8cf, 0x0f);
228
Hans Verkuila8bbf122006-01-09 15:25:42 -0200229 /* (re)set input */
230 set_input(client, state->vid_input, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800231
232 /* start microcontroller */
233 cx25840_and_or(client, 0x803, ~0x10, 0x10);
234}
235
236/* ----------------------------------------------------------------------- */
237
238static void input_change(struct i2c_client *client)
239{
Hans Verkuilf95006f2005-12-01 00:51:42 -0800240 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800241 v4l2_std_id std = cx25840_get_v4lstd(client);
242
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300243 /* Follow step 8c and 8d of section 3.16 in the cx25840 datasheet */
244 if (std & V4L2_STD_SECAM) {
245 cx25840_write(client, 0x402, 0);
246 }
247 else {
248 cx25840_write(client, 0x402, 0x04);
249 cx25840_write(client, 0x49f, (std & V4L2_STD_NTSC) ? 0x14 : 0x11);
250 }
251 cx25840_and_or(client, 0x401, ~0x60, 0);
252 cx25840_and_or(client, 0x401, ~0x60, 0x60);
253
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300254 if (std & V4L2_STD_525_60) {
Hans Verkuild97a11e2006-02-07 06:48:40 -0200255 /* Certain Hauppauge PVR150 models have a hardware bug
256 that causes audio to drop out. For these models the
257 audio standard must be set explicitly.
258 To be precise: it affects cards with tuner models
259 85, 99 and 112 (model numbers from tveeprom). */
260 int hw_fix = state->pvr150_workaround;
261
262 if (std == V4L2_STD_NTSC_M_JP) {
Hans Verkuilf95006f2005-12-01 00:51:42 -0800263 /* Japan uses EIAJ audio standard */
Hans Verkuild97a11e2006-02-07 06:48:40 -0200264 cx25840_write(client, 0x808, hw_fix ? 0x2f : 0xf7);
265 } else if (std == V4L2_STD_NTSC_M_KR) {
266 /* South Korea uses A2 audio standard */
267 cx25840_write(client, 0x808, hw_fix ? 0x3f : 0xf8);
Hans Verkuilf95006f2005-12-01 00:51:42 -0800268 } else {
269 /* Others use the BTSC audio standard */
Hans Verkuild97a11e2006-02-07 06:48:40 -0200270 cx25840_write(client, 0x808, hw_fix ? 0x1f : 0xf6);
Hans Verkuilf95006f2005-12-01 00:51:42 -0800271 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800272 cx25840_write(client, 0x80b, 0x00);
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300273 } else if (std & V4L2_STD_PAL) {
274 /* Follow tuner change procedure for PAL */
275 cx25840_write(client, 0x808, 0xff);
276 cx25840_write(client, 0x80b, 0x10);
277 } else if (std & V4L2_STD_SECAM) {
278 /* Select autodetect for SECAM */
279 cx25840_write(client, 0x808, 0xff);
280 cx25840_write(client, 0x80b, 0x10);
Hans Verkuilbd985162005-11-13 16:07:56 -0800281 }
282
283 if (cx25840_read(client, 0x803) & 0x10) {
284 /* restart audio decoder microcontroller */
285 cx25840_and_or(client, 0x803, ~0x10, 0x00);
286 cx25840_and_or(client, 0x803, ~0x10, 0x10);
287 }
288}
289
Hans Verkuila8bbf122006-01-09 15:25:42 -0200290static int set_input(struct i2c_client *client, enum cx25840_video_input vid_input,
291 enum cx25840_audio_input aud_input)
Hans Verkuilbd985162005-11-13 16:07:56 -0800292{
293 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200294 u8 is_composite = (vid_input >= CX25840_COMPOSITE1 &&
295 vid_input <= CX25840_COMPOSITE8);
296 u8 reg;
Hans Verkuilbd985162005-11-13 16:07:56 -0800297
Hans Verkuilb5fc7142006-01-11 22:41:36 -0200298 v4l_dbg(1, cx25840_debug, client, "decoder set video input %d, audio input %d\n",
Hans Verkuila8bbf122006-01-09 15:25:42 -0200299 vid_input, aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800300
Hans Verkuila8bbf122006-01-09 15:25:42 -0200301 if (is_composite) {
302 reg = 0xf0 + (vid_input - CX25840_COMPOSITE1);
303 } else {
304 int luma = vid_input & 0xf0;
305 int chroma = vid_input & 0xf00;
Hans Verkuilbd985162005-11-13 16:07:56 -0800306
Hans Verkuila8bbf122006-01-09 15:25:42 -0200307 if ((vid_input & ~0xff0) ||
308 luma < CX25840_SVIDEO_LUMA1 || luma > CX25840_SVIDEO_LUMA4 ||
309 chroma < CX25840_SVIDEO_CHROMA4 || chroma > CX25840_SVIDEO_CHROMA8) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200310 v4l_err(client, "0x%04x is not a valid video input!\n", vid_input);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200311 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800312 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200313 reg = 0xf0 + ((luma - CX25840_SVIDEO_LUMA1) >> 4);
314 if (chroma >= CX25840_SVIDEO_CHROMA7) {
315 reg &= 0x3f;
316 reg |= (chroma - CX25840_SVIDEO_CHROMA7) >> 2;
Hans Verkuilbd985162005-11-13 16:07:56 -0800317 } else {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200318 reg &= 0xcf;
319 reg |= (chroma - CX25840_SVIDEO_CHROMA4) >> 4;
Hans Verkuilbd985162005-11-13 16:07:56 -0800320 }
Hans Verkuila8bbf122006-01-09 15:25:42 -0200321 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800322
Hans Verkuila8bbf122006-01-09 15:25:42 -0200323 switch (aud_input) {
324 case CX25840_AUDIO_SERIAL:
325 /* do nothing, use serial audio input */
Hans Verkuilbd985162005-11-13 16:07:56 -0800326 break;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200327 case CX25840_AUDIO4: reg &= ~0x30; break;
328 case CX25840_AUDIO5: reg &= ~0x30; reg |= 0x10; break;
329 case CX25840_AUDIO6: reg &= ~0x30; reg |= 0x20; break;
330 case CX25840_AUDIO7: reg &= ~0xc0; break;
331 case CX25840_AUDIO8: reg &= ~0xc0; reg |= 0x40; break;
Hans Verkuilbd985162005-11-13 16:07:56 -0800332
333 default:
Hans Verkuilfac9e892006-01-09 15:32:40 -0200334 v4l_err(client, "0x%04x is not a valid audio input!\n", aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800335 return -EINVAL;
336 }
337
Hans Verkuila8bbf122006-01-09 15:25:42 -0200338 cx25840_write(client, 0x103, reg);
339 /* Set INPUT_MODE to Composite (0) or S-Video (1) */
340 cx25840_and_or(client, 0x401, ~0x6, is_composite ? 0 : 0x02);
341 /* Set CH_SEL_ADC2 to 1 if input comes from CH3 */
342 cx25840_and_or(client, 0x102, ~0x2, (reg & 0x80) == 0 ? 2 : 0);
343 /* Set DUAL_MODE_ADC2 to 1 if input comes from both CH2 and CH3 */
344 if ((reg & 0xc0) != 0xc0 && (reg & 0x30) != 0x30)
345 cx25840_and_or(client, 0x102, ~0x4, 4);
346 else
347 cx25840_and_or(client, 0x102, ~0x4, 0);
348
349 state->vid_input = vid_input;
350 state->aud_input = aud_input;
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300351 if (!state->is_cx25836) {
352 cx25840_audio_set_path(client);
353 input_change(client);
354 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800355 return 0;
356}
357
358/* ----------------------------------------------------------------------- */
359
360static int set_v4lstd(struct i2c_client *client, v4l2_std_id std)
361{
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200362 u8 fmt=0; /* zero is autodetect */
Hans Verkuilbd985162005-11-13 16:07:56 -0800363
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200364 /* First tests should be against specific std */
Hans Verkuild97a11e2006-02-07 06:48:40 -0200365 if (std == V4L2_STD_NTSC_M_JP) {
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200366 fmt=0x2;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200367 } else if (std == V4L2_STD_NTSC_443) {
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200368 fmt=0x3;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200369 } else if (std == V4L2_STD_PAL_M) {
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200370 fmt=0x5;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200371 } else if (std == V4L2_STD_PAL_N) {
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200372 fmt=0x6;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200373 } else if (std == V4L2_STD_PAL_Nc) {
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200374 fmt=0x7;
Hans Verkuild97a11e2006-02-07 06:48:40 -0200375 } else if (std == V4L2_STD_PAL_60) {
Mauro Carvalho Chehab468a0a52005-12-19 08:54:11 -0200376 fmt=0x8;
377 } else {
378 /* Then, test against generic ones */
379 if (std & V4L2_STD_NTSC) {
380 fmt=0x1;
381 } else if (std & V4L2_STD_PAL) {
382 fmt=0x4;
383 } else if (std & V4L2_STD_SECAM) {
384 fmt=0xc;
385 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800386 }
387
Mauro Carvalho Chehab839e4a42006-06-04 12:15:55 -0300388 v4l_dbg(1, cx25840_debug, client, "changing video std to fmt %i\n",fmt);
389
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300390 /* Follow step 9 of section 3.16 in the cx25840 datasheet.
391 Without this PAL may display a vertical ghosting effect.
392 This happens for example with the Yuan MPC622. */
393 if (fmt >= 4 && fmt < 8) {
394 /* Set format to NTSC-M */
395 cx25840_and_or(client, 0x400, ~0xf, 1);
396 /* Turn off LCOMB */
397 cx25840_and_or(client, 0x47b, ~6, 0);
398 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800399 cx25840_and_or(client, 0x400, ~0xf, fmt);
400 cx25840_vbi_setup(client);
401 return 0;
402}
403
404v4l2_std_id cx25840_get_v4lstd(struct i2c_client * client)
405{
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300406 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800407 /* check VID_FMT_SEL first */
408 u8 fmt = cx25840_read(client, 0x400) & 0xf;
409
410 if (!fmt) {
411 /* check AFD_FMT_STAT if set to autodetect */
412 fmt = cx25840_read(client, 0x40d) & 0xf;
413 }
414
415 switch (fmt) {
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300416 case 0x1:
417 {
418 /* if the audio std is A2-M, then this is the South Korean
419 NTSC standard */
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300420 if (!state->is_cx25836 && cx25840_read(client, 0x805) == 2)
Hans Verkuil73dcddc2006-03-16 20:23:47 -0300421 return V4L2_STD_NTSC_M_KR;
422 return V4L2_STD_NTSC_M;
423 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800424 case 0x2: return V4L2_STD_NTSC_M_JP;
425 case 0x3: return V4L2_STD_NTSC_443;
426 case 0x4: return V4L2_STD_PAL;
427 case 0x5: return V4L2_STD_PAL_M;
428 case 0x6: return V4L2_STD_PAL_N;
429 case 0x7: return V4L2_STD_PAL_Nc;
430 case 0x8: return V4L2_STD_PAL_60;
431 case 0xc: return V4L2_STD_SECAM;
432 default: return V4L2_STD_UNKNOWN;
433 }
434}
435
436/* ----------------------------------------------------------------------- */
437
438static int set_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
439{
440 struct cx25840_state *state = i2c_get_clientdata(client);
441
442 switch (ctrl->id) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200443 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
444 state->pvr150_workaround = ctrl->value;
445 set_input(client, state->vid_input, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800446 break;
447
448 case V4L2_CID_BRIGHTNESS:
449 if (ctrl->value < 0 || ctrl->value > 255) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200450 v4l_err(client, "invalid brightness setting %d\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800451 ctrl->value);
452 return -ERANGE;
453 }
454
455 cx25840_write(client, 0x414, ctrl->value - 128);
456 break;
457
458 case V4L2_CID_CONTRAST:
459 if (ctrl->value < 0 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200460 v4l_err(client, "invalid contrast setting %d\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800461 ctrl->value);
462 return -ERANGE;
463 }
464
465 cx25840_write(client, 0x415, ctrl->value << 1);
466 break;
467
468 case V4L2_CID_SATURATION:
469 if (ctrl->value < 0 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200470 v4l_err(client, "invalid saturation setting %d\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800471 ctrl->value);
472 return -ERANGE;
473 }
474
475 cx25840_write(client, 0x420, ctrl->value << 1);
476 cx25840_write(client, 0x421, ctrl->value << 1);
477 break;
478
479 case V4L2_CID_HUE:
480 if (ctrl->value < -127 || ctrl->value > 127) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200481 v4l_err(client, "invalid hue setting %d\n", ctrl->value);
Hans Verkuilbd985162005-11-13 16:07:56 -0800482 return -ERANGE;
483 }
484
485 cx25840_write(client, 0x422, ctrl->value);
486 break;
487
488 case V4L2_CID_AUDIO_VOLUME:
489 case V4L2_CID_AUDIO_BASS:
490 case V4L2_CID_AUDIO_TREBLE:
491 case V4L2_CID_AUDIO_BALANCE:
492 case V4L2_CID_AUDIO_MUTE:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300493 if (state->is_cx25836)
494 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800495 return cx25840_audio(client, VIDIOC_S_CTRL, ctrl);
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200496
497 default:
498 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800499 }
500
501 return 0;
502}
503
504static int get_v4lctrl(struct i2c_client *client, struct v4l2_control *ctrl)
505{
506 struct cx25840_state *state = i2c_get_clientdata(client);
507
508 switch (ctrl->id) {
Hans Verkuila8bbf122006-01-09 15:25:42 -0200509 case CX25840_CID_ENABLE_PVR150_WORKAROUND:
510 ctrl->value = state->pvr150_workaround;
Hans Verkuilbd985162005-11-13 16:07:56 -0800511 break;
512 case V4L2_CID_BRIGHTNESS:
Hans Verkuil0de71222006-01-09 15:32:44 -0200513 ctrl->value = (s8)cx25840_read(client, 0x414) + 128;
Hans Verkuilbd985162005-11-13 16:07:56 -0800514 break;
515 case V4L2_CID_CONTRAST:
516 ctrl->value = cx25840_read(client, 0x415) >> 1;
517 break;
518 case V4L2_CID_SATURATION:
519 ctrl->value = cx25840_read(client, 0x420) >> 1;
520 break;
521 case V4L2_CID_HUE:
Hans Verkuilc5099a62006-01-09 15:32:43 -0200522 ctrl->value = (s8)cx25840_read(client, 0x422);
Hans Verkuilbd985162005-11-13 16:07:56 -0800523 break;
524 case V4L2_CID_AUDIO_VOLUME:
525 case V4L2_CID_AUDIO_BASS:
526 case V4L2_CID_AUDIO_TREBLE:
527 case V4L2_CID_AUDIO_BALANCE:
528 case V4L2_CID_AUDIO_MUTE:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300529 if (state->is_cx25836)
530 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800531 return cx25840_audio(client, VIDIOC_G_CTRL, ctrl);
532 default:
533 return -EINVAL;
534 }
535
536 return 0;
537}
538
539/* ----------------------------------------------------------------------- */
540
541static int get_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
542{
543 switch (fmt->type) {
544 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
545 return cx25840_vbi(client, VIDIOC_G_FMT, fmt);
546 default:
547 return -EINVAL;
548 }
549
550 return 0;
551}
552
553static int set_v4lfmt(struct i2c_client *client, struct v4l2_format *fmt)
554{
555 struct v4l2_pix_format *pix;
556 int HSC, VSC, Vsrc, Hsrc, filter, Vlines;
557 int is_pal = !(cx25840_get_v4lstd(client) & V4L2_STD_NTSC);
558
559 switch (fmt->type) {
560 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
561 pix = &(fmt->fmt.pix);
562
563 Vsrc = (cx25840_read(client, 0x476) & 0x3f) << 4;
564 Vsrc |= (cx25840_read(client, 0x475) & 0xf0) >> 4;
565
566 Hsrc = (cx25840_read(client, 0x472) & 0x3f) << 4;
567 Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4;
568
569 Vlines = pix->height + (is_pal ? 4 : 7);
570
571 if ((pix->width * 16 < Hsrc) || (Hsrc < pix->width) ||
572 (Vlines * 8 < Vsrc) || (Vsrc < Vlines)) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200573 v4l_err(client, "%dx%d is not a valid size!\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800574 pix->width, pix->height);
575 return -ERANGE;
576 }
577
578 HSC = (Hsrc * (1 << 20)) / pix->width - (1 << 20);
579 VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9));
580 VSC &= 0x1fff;
581
582 if (pix->width >= 385)
583 filter = 0;
584 else if (pix->width > 192)
585 filter = 1;
586 else if (pix->width > 96)
587 filter = 2;
588 else
589 filter = 3;
590
Hans Verkuilb5fc7142006-01-11 22:41:36 -0200591 v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800592 pix->width, pix->height, HSC, VSC);
593
594 /* HSCALE=HSC */
595 cx25840_write(client, 0x418, HSC & 0xff);
596 cx25840_write(client, 0x419, (HSC >> 8) & 0xff);
597 cx25840_write(client, 0x41a, HSC >> 16);
598 /* VSCALE=VSC */
599 cx25840_write(client, 0x41c, VSC & 0xff);
600 cx25840_write(client, 0x41d, VSC >> 8);
601 /* VS_INTRLACE=1 VFILT=filter */
602 cx25840_write(client, 0x41e, 0x8 | filter);
603 break;
604
605 case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
606 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
607
608 case V4L2_BUF_TYPE_VBI_CAPTURE:
609 return cx25840_vbi(client, VIDIOC_S_FMT, fmt);
610
611 default:
612 return -EINVAL;
613 }
614
615 return 0;
616}
617
618/* ----------------------------------------------------------------------- */
619
620static int cx25840_command(struct i2c_client *client, unsigned int cmd,
621 void *arg)
622{
623 struct cx25840_state *state = i2c_get_clientdata(client);
624 struct v4l2_tuner *vt = arg;
Hans Verkuil31bc09b2006-03-25 10:26:09 -0300625 struct v4l2_routing *route = arg;
Hans Verkuilbd985162005-11-13 16:07:56 -0800626
627 switch (cmd) {
Hans Verkuilbd985162005-11-13 16:07:56 -0800628#ifdef CONFIG_VIDEO_ADV_DEBUG
629 /* ioctls to allow direct access to the
630 * cx25840 registers for testing */
631 case VIDIOC_INT_G_REGISTER:
632 {
633 struct v4l2_register *reg = arg;
634
635 if (reg->i2c_id != I2C_DRIVERID_CX25840)
636 return -EINVAL;
637 reg->val = cx25840_read(client, reg->reg & 0x0fff);
638 break;
639 }
640
641 case VIDIOC_INT_S_REGISTER:
642 {
643 struct v4l2_register *reg = arg;
644
645 if (reg->i2c_id != I2C_DRIVERID_CX25840)
646 return -EINVAL;
647 if (!capable(CAP_SYS_ADMIN))
648 return -EPERM;
649 cx25840_write(client, reg->reg & 0x0fff, reg->val & 0xff);
650 break;
651 }
652#endif
653
654 case VIDIOC_INT_DECODE_VBI_LINE:
655 return cx25840_vbi(client, cmd, arg);
656
657 case VIDIOC_INT_AUDIO_CLOCK_FREQ:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200658 return cx25840_audio(client, cmd, arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800659
660 case VIDIOC_STREAMON:
Hans Verkuilb5fc7142006-01-11 22:41:36 -0200661 v4l_dbg(1, cx25840_debug, client, "enable output\n");
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300662 cx25840_write(client, 0x115, state->is_cx25836 ? 0x0c : 0x8c);
663 cx25840_write(client, 0x116, state->is_cx25836 ? 0x04 : 0x07);
Hans Verkuilbd985162005-11-13 16:07:56 -0800664 break;
665
666 case VIDIOC_STREAMOFF:
Hans Verkuilb5fc7142006-01-11 22:41:36 -0200667 v4l_dbg(1, cx25840_debug, client, "disable output\n");
Hans Verkuilbd985162005-11-13 16:07:56 -0800668 cx25840_write(client, 0x115, 0x00);
669 cx25840_write(client, 0x116, 0x00);
670 break;
671
672 case VIDIOC_LOG_STATUS:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300673 log_video_status(client);
674 if (!state->is_cx25836)
675 log_audio_status(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800676 break;
677
678 case VIDIOC_G_CTRL:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200679 return get_v4lctrl(client, (struct v4l2_control *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800680
681 case VIDIOC_S_CTRL:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200682 return set_v4lctrl(client, (struct v4l2_control *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800683
Hans Verkuild92c20e2006-01-09 15:32:41 -0200684 case VIDIOC_QUERYCTRL:
685 {
686 struct v4l2_queryctrl *qc = arg;
Hans Verkuild92c20e2006-01-09 15:32:41 -0200687
Hans Verkuil18318e02006-06-18 14:49:52 -0300688 switch (qc->id) {
689 case V4L2_CID_BRIGHTNESS:
690 case V4L2_CID_CONTRAST:
691 case V4L2_CID_SATURATION:
692 case V4L2_CID_HUE:
693 return v4l2_ctrl_query_fill_std(qc);
694 default:
695 break;
696 }
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300697 if (state->is_cx25836)
698 return -EINVAL;
699
Hans Verkuil18318e02006-06-18 14:49:52 -0300700 switch (qc->id) {
701 case V4L2_CID_AUDIO_VOLUME:
702 case V4L2_CID_AUDIO_MUTE:
703 case V4L2_CID_AUDIO_BALANCE:
704 case V4L2_CID_AUDIO_BASS:
705 case V4L2_CID_AUDIO_TREBLE:
706 return v4l2_ctrl_query_fill_std(qc);
707 default:
708 return -EINVAL;
709 }
Hans Verkuild92c20e2006-01-09 15:32:41 -0200710 return -EINVAL;
711 }
712
Hans Verkuilbd985162005-11-13 16:07:56 -0800713 case VIDIOC_G_STD:
714 *(v4l2_std_id *)arg = cx25840_get_v4lstd(client);
715 break;
716
717 case VIDIOC_S_STD:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200718 state->radio = 0;
719 return set_v4lstd(client, *(v4l2_std_id *)arg);
720
721 case AUDC_SET_RADIO:
722 state->radio = 1;
Hans Verkuilbd985162005-11-13 16:07:56 -0800723 break;
724
Hans Verkuil31bc09b2006-03-25 10:26:09 -0300725 case VIDIOC_INT_G_VIDEO_ROUTING:
726 route->input = state->vid_input;
727 route->output = 0;
Hans Verkuilbd985162005-11-13 16:07:56 -0800728 break;
729
Hans Verkuil31bc09b2006-03-25 10:26:09 -0300730 case VIDIOC_INT_S_VIDEO_ROUTING:
731 return set_input(client, route->input, state->aud_input);
Hans Verkuilbd985162005-11-13 16:07:56 -0800732
Hans Verkuil31bc09b2006-03-25 10:26:09 -0300733 case VIDIOC_INT_G_AUDIO_ROUTING:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300734 if (state->is_cx25836)
735 return -EINVAL;
Hans Verkuil31bc09b2006-03-25 10:26:09 -0300736 route->input = state->aud_input;
737 route->output = 0;
738 break;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200739
Hans Verkuil31bc09b2006-03-25 10:26:09 -0300740 case VIDIOC_INT_S_AUDIO_ROUTING:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300741 if (state->is_cx25836)
742 return -EINVAL;
Hans Verkuil31bc09b2006-03-25 10:26:09 -0300743 return set_input(client, state->vid_input, route->input);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200744
Hans Verkuilbd985162005-11-13 16:07:56 -0800745 case VIDIOC_S_FREQUENCY:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300746 if (!state->is_cx25836) {
747 input_change(client);
748 }
Hans Verkuilbd985162005-11-13 16:07:56 -0800749 break;
750
751 case VIDIOC_G_TUNER:
752 {
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300753 u8 vpres = cx25840_read(client, 0x40e) & 0x20;
754 u8 mode;
Hans Verkuilbd985162005-11-13 16:07:56 -0800755 int val = 0;
756
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200757 if (state->radio)
758 break;
759
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300760 vt->signal = vpres ? 0xffff : 0x0;
761 if (state->is_cx25836)
762 break;
763
Hans Verkuilbd985162005-11-13 16:07:56 -0800764 vt->capability |=
765 V4L2_TUNER_CAP_STEREO | V4L2_TUNER_CAP_LANG1 |
766 V4L2_TUNER_CAP_LANG2 | V4L2_TUNER_CAP_SAP;
767
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300768 mode = cx25840_read(client, 0x804);
Hans Verkuilbd985162005-11-13 16:07:56 -0800769
770 /* get rxsubchans and audmode */
771 if ((mode & 0xf) == 1)
772 val |= V4L2_TUNER_SUB_STEREO;
773 else
774 val |= V4L2_TUNER_SUB_MONO;
775
776 if (mode == 2 || mode == 4)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200777 val = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
Hans Verkuilbd985162005-11-13 16:07:56 -0800778
779 if (mode & 0x10)
780 val |= V4L2_TUNER_SUB_SAP;
781
782 vt->rxsubchans = val;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200783 vt->audmode = state->audmode;
Hans Verkuilbd985162005-11-13 16:07:56 -0800784 break;
785 }
786
787 case VIDIOC_S_TUNER:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300788 if (state->radio || state->is_cx25836)
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200789 break;
790
Hans Verkuilbd985162005-11-13 16:07:56 -0800791 switch (vt->audmode) {
792 case V4L2_TUNER_MODE_MONO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200793 /* mono -> mono
794 stereo -> mono
795 bilingual -> lang1 */
Hans Verkuilbd985162005-11-13 16:07:56 -0800796 cx25840_and_or(client, 0x809, ~0xf, 0x00);
797 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -0300798 case V4L2_TUNER_MODE_STEREO:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200799 case V4L2_TUNER_MODE_LANG1:
800 /* mono -> mono
801 stereo -> stereo
802 bilingual -> lang1 */
Hans Verkuilbd985162005-11-13 16:07:56 -0800803 cx25840_and_or(client, 0x809, ~0xf, 0x04);
804 break;
Hans Verkuil301e22d2006-03-18 17:15:00 -0300805 case V4L2_TUNER_MODE_LANG1_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200806 /* mono -> mono
807 stereo -> stereo
808 bilingual -> lang1/lang2 */
809 cx25840_and_or(client, 0x809, ~0xf, 0x07);
810 break;
Hans Verkuilbd985162005-11-13 16:07:56 -0800811 case V4L2_TUNER_MODE_LANG2:
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200812 /* mono -> mono
Hans Verkuil301e22d2006-03-18 17:15:00 -0300813 stereo -> stereo
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200814 bilingual -> lang2 */
Hans Verkuilbd985162005-11-13 16:07:56 -0800815 cx25840_and_or(client, 0x809, ~0xf, 0x01);
816 break;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200817 default:
818 return -EINVAL;
Hans Verkuilbd985162005-11-13 16:07:56 -0800819 }
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200820 state->audmode = vt->audmode;
Hans Verkuilbd985162005-11-13 16:07:56 -0800821 break;
822
823 case VIDIOC_G_FMT:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200824 return get_v4lfmt(client, (struct v4l2_format *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800825
826 case VIDIOC_S_FMT:
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200827 return set_v4lfmt(client, (struct v4l2_format *)arg);
Hans Verkuilbd985162005-11-13 16:07:56 -0800828
829 case VIDIOC_INT_RESET:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300830 if (state->is_cx25836)
831 cx25836_initialize(client);
832 else
833 cx25840_initialize(client, 0);
Hans Verkuilbd985162005-11-13 16:07:56 -0800834 break;
835
836 case VIDIOC_INT_G_CHIP_IDENT:
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300837 *(enum v4l2_chip_ident *)arg = state->id;
Hans Verkuilbd985162005-11-13 16:07:56 -0800838 break;
839
840 default:
Hans Verkuilbd985162005-11-13 16:07:56 -0800841 return -EINVAL;
842 }
843
Hans Verkuil3faeeae2006-01-09 15:25:44 -0200844 return 0;
Hans Verkuilbd985162005-11-13 16:07:56 -0800845}
846
847/* ----------------------------------------------------------------------- */
848
Mauro Carvalho Chehab769e2432005-12-01 00:51:35 -0800849static struct i2c_driver i2c_driver_cx25840;
Hans Verkuilbd985162005-11-13 16:07:56 -0800850
851static int cx25840_detect_client(struct i2c_adapter *adapter, int address,
852 int kind)
853{
854 struct i2c_client *client;
855 struct cx25840_state *state;
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300856 enum v4l2_chip_ident id;
Hans Verkuilbd985162005-11-13 16:07:56 -0800857 u16 device_id;
858
859 /* Check if the adapter supports the needed features
860 * Not until kernel version 2.6.11 did the bit-algo
861 * correctly report that it would do an I2C-level xfer */
862 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
863 return 0;
864
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300865 state = kzalloc(sizeof(struct cx25840_state), GFP_KERNEL);
866 if (state == 0)
Hans Verkuilbd985162005-11-13 16:07:56 -0800867 return -ENOMEM;
868
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300869 client = &state->c;
Hans Verkuilbd985162005-11-13 16:07:56 -0800870 client->addr = address;
871 client->adapter = adapter;
872 client->driver = &i2c_driver_cx25840;
Hans Verkuilbd985162005-11-13 16:07:56 -0800873 snprintf(client->name, sizeof(client->name) - 1, "cx25840");
874
Hans Verkuilb5fc7142006-01-11 22:41:36 -0200875 v4l_dbg(1, cx25840_debug, client, "detecting cx25840 client on address 0x%x\n", address << 1);
Hans Verkuilbd985162005-11-13 16:07:56 -0800876
877 device_id = cx25840_read(client, 0x101) << 8;
878 device_id |= cx25840_read(client, 0x100);
879
880 /* The high byte of the device ID should be
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300881 * 0x83 for the cx2583x and 0x84 for the cx2584x */
882 if ((device_id & 0xff00) == 0x8300) {
883 id = V4L2_IDENT_CX25836 + ((device_id >> 4) & 0xf) - 6;
884 state->is_cx25836 = 1;
885 }
886 else if ((device_id & 0xff00) == 0x8400) {
887 id = V4L2_IDENT_CX25840 + ((device_id >> 4) & 0xf);
888 state->is_cx25836 = 0;
889 }
890 else {
Hans Verkuilb5fc7142006-01-11 22:41:36 -0200891 v4l_dbg(1, cx25840_debug, client, "cx25840 not found\n");
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300892 kfree(state);
Hans Verkuilbd985162005-11-13 16:07:56 -0800893 return 0;
894 }
895
Hans Verkuilfac9e892006-01-09 15:32:40 -0200896 v4l_info(client, "cx25%3x-2%x found @ 0x%x (%s)\n",
Hans Verkuilbd985162005-11-13 16:07:56 -0800897 (device_id & 0xfff0) >> 4,
898 (device_id & 0x0f) < 3 ? (device_id & 0x0f) + 1 : 3,
899 address << 1, adapter->name);
900
Hans Verkuilbd985162005-11-13 16:07:56 -0800901 i2c_set_clientdata(client, state);
Hans Verkuila8bbf122006-01-09 15:25:42 -0200902 state->vid_input = CX25840_COMPOSITE7;
903 state->aud_input = CX25840_AUDIO8;
Hans Verkuil3578d3d2006-01-09 15:25:41 -0200904 state->audclk_freq = 48000;
Hans Verkuila8bbf122006-01-09 15:25:42 -0200905 state->pvr150_workaround = 0;
Hans Verkuil8a4b2752006-01-23 17:11:09 -0200906 state->audmode = V4L2_TUNER_MODE_LANG1;
Christopher Neufeld3e3bf272006-05-24 10:16:45 -0300907 state->vbi_line_offset = 8;
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300908 state->id = id;
Hans Verkuilbd985162005-11-13 16:07:56 -0800909
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300910 if (state->is_cx25836)
911 cx25836_initialize(client);
912 else
913 cx25840_initialize(client, 1);
Hans Verkuilbd985162005-11-13 16:07:56 -0800914
915 i2c_attach_client(client);
916
917 return 0;
918}
919
920static int cx25840_attach_adapter(struct i2c_adapter *adapter)
921{
Hans Verkuilbd985162005-11-13 16:07:56 -0800922 if (adapter->class & I2C_CLASS_TV_ANALOG)
Hans Verkuilbd985162005-11-13 16:07:56 -0800923 return i2c_probe(adapter, &addr_data, &cx25840_detect_client);
924 return 0;
925}
926
927static int cx25840_detach_client(struct i2c_client *client)
928{
929 struct cx25840_state *state = i2c_get_clientdata(client);
930 int err;
931
932 err = i2c_detach_client(client);
933 if (err) {
934 return err;
935 }
936
937 kfree(state);
Hans Verkuilbd985162005-11-13 16:07:56 -0800938
939 return 0;
940}
941
942/* ----------------------------------------------------------------------- */
943
Mauro Carvalho Chehab769e2432005-12-01 00:51:35 -0800944static struct i2c_driver i2c_driver_cx25840 = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100945 .driver = {
Laurent Riffard604f28e2005-11-26 20:43:39 +0100946 .name = "cx25840",
947 },
Hans Verkuilbd985162005-11-13 16:07:56 -0800948 .id = I2C_DRIVERID_CX25840,
Hans Verkuilbd985162005-11-13 16:07:56 -0800949 .attach_adapter = cx25840_attach_adapter,
950 .detach_client = cx25840_detach_client,
951 .command = cx25840_command,
Hans Verkuilbd985162005-11-13 16:07:56 -0800952};
953
954
955static int __init m__init(void)
956{
957 return i2c_add_driver(&i2c_driver_cx25840);
958}
959
960static void __exit m__exit(void)
961{
962 i2c_del_driver(&i2c_driver_cx25840);
963}
964
965module_init(m__init);
966module_exit(m__exit);
967
968/* ----------------------------------------------------------------------- */
969
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300970static void log_video_status(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -0800971{
972 static const char *const fmt_strs[] = {
973 "0x0",
974 "NTSC-M", "NTSC-J", "NTSC-4.43",
975 "PAL-BDGHI", "PAL-M", "PAL-N", "PAL-Nc", "PAL-60",
976 "0x9", "0xA", "0xB",
977 "SECAM",
978 "0xD", "0xE", "0xF"
979 };
980
981 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuilbd985162005-11-13 16:07:56 -0800982 u8 vidfmt_sel = cx25840_read(client, 0x400) & 0xf;
983 u8 gen_stat1 = cx25840_read(client, 0x40d);
Hans Verkuile2b8cf42006-04-22 10:22:46 -0300984 u8 gen_stat2 = cx25840_read(client, 0x40e);
985 int vid_input = state->vid_input;
986
987 v4l_info(client, "Video signal: %spresent\n",
988 (gen_stat2 & 0x20) ? "" : "not ");
989 v4l_info(client, "Detected format: %s\n",
990 fmt_strs[gen_stat1 & 0xf]);
991
992 v4l_info(client, "Specified standard: %s\n",
993 vidfmt_sel ? fmt_strs[vidfmt_sel] : "automatic detection");
994
995 if (vid_input >= CX25840_COMPOSITE1 &&
996 vid_input <= CX25840_COMPOSITE8) {
997 v4l_info(client, "Specified video input: Composite %d\n",
998 vid_input - CX25840_COMPOSITE1 + 1);
999 } else {
1000 v4l_info(client, "Specified video input: S-Video (Luma In%d, Chroma In%d)\n",
1001 (vid_input & 0xf0) >> 4, (vid_input & 0xf00) >> 8);
1002 }
1003
1004 v4l_info(client, "Specified audioclock freq: %d Hz\n", state->audclk_freq);
1005}
1006
1007/* ----------------------------------------------------------------------- */
1008
1009static void log_audio_status(struct i2c_client *client)
1010{
1011 struct cx25840_state *state = i2c_get_clientdata(client);
Hans Verkuilbd985162005-11-13 16:07:56 -08001012 u8 download_ctl = cx25840_read(client, 0x803);
1013 u8 mod_det_stat0 = cx25840_read(client, 0x804);
1014 u8 mod_det_stat1 = cx25840_read(client, 0x805);
1015 u8 audio_config = cx25840_read(client, 0x808);
1016 u8 pref_mode = cx25840_read(client, 0x809);
1017 u8 afc0 = cx25840_read(client, 0x80b);
1018 u8 mute_ctl = cx25840_read(client, 0x8d3);
Hans Verkuila8bbf122006-01-09 15:25:42 -02001019 int aud_input = state->aud_input;
Hans Verkuilbd985162005-11-13 16:07:56 -08001020 char *p;
1021
Hans Verkuilbd985162005-11-13 16:07:56 -08001022 switch (mod_det_stat0) {
1023 case 0x00: p = "mono"; break;
1024 case 0x01: p = "stereo"; break;
1025 case 0x02: p = "dual"; break;
1026 case 0x04: p = "tri"; break;
1027 case 0x10: p = "mono with SAP"; break;
1028 case 0x11: p = "stereo with SAP"; break;
1029 case 0x12: p = "dual with SAP"; break;
1030 case 0x14: p = "tri with SAP"; break;
1031 case 0xfe: p = "forced mode"; break;
1032 default: p = "not defined";
1033 }
Hans Verkuilfac9e892006-01-09 15:32:40 -02001034 v4l_info(client, "Detected audio mode: %s\n", p);
Hans Verkuilbd985162005-11-13 16:07:56 -08001035
1036 switch (mod_det_stat1) {
1037 case 0x00: p = "not defined"; break;
1038 case 0x01: p = "EIAJ"; break;
1039 case 0x02: p = "A2-M"; break;
1040 case 0x03: p = "A2-BG"; break;
1041 case 0x04: p = "A2-DK1"; break;
1042 case 0x05: p = "A2-DK2"; break;
1043 case 0x06: p = "A2-DK3"; break;
1044 case 0x07: p = "A1 (6.0 MHz FM Mono)"; break;
1045 case 0x08: p = "AM-L"; break;
1046 case 0x09: p = "NICAM-BG"; break;
1047 case 0x0a: p = "NICAM-DK"; break;
1048 case 0x0b: p = "NICAM-I"; break;
1049 case 0x0c: p = "NICAM-L"; break;
1050 case 0x0d: p = "BTSC/EIAJ/A2-M Mono (4.5 MHz FMMono)"; break;
1051 case 0x0e: p = "IF FM Radio"; break;
1052 case 0x0f: p = "BTSC"; break;
1053 case 0x10: p = "high-deviation FM"; break;
1054 case 0x11: p = "very high-deviation FM"; break;
1055 case 0xfd: p = "unknown audio standard"; break;
1056 case 0xfe: p = "forced audio standard"; break;
1057 case 0xff: p = "no detected audio standard"; break;
1058 default: p = "not defined";
1059 }
Hans Verkuilfac9e892006-01-09 15:32:40 -02001060 v4l_info(client, "Detected audio standard: %s\n", p);
1061 v4l_info(client, "Audio muted: %s\n",
Hans Verkuilbd985162005-11-13 16:07:56 -08001062 (mute_ctl & 0x2) ? "yes" : "no");
Hans Verkuilfac9e892006-01-09 15:32:40 -02001063 v4l_info(client, "Audio microcontroller: %s\n",
Hans Verkuilbd985162005-11-13 16:07:56 -08001064 (download_ctl & 0x10) ? "running" : "stopped");
1065
1066 switch (audio_config >> 4) {
1067 case 0x00: p = "undefined"; break;
1068 case 0x01: p = "BTSC"; break;
1069 case 0x02: p = "EIAJ"; break;
1070 case 0x03: p = "A2-M"; break;
1071 case 0x04: p = "A2-BG"; break;
1072 case 0x05: p = "A2-DK1"; break;
1073 case 0x06: p = "A2-DK2"; break;
1074 case 0x07: p = "A2-DK3"; break;
1075 case 0x08: p = "A1 (6.0 MHz FM Mono)"; break;
1076 case 0x09: p = "AM-L"; break;
1077 case 0x0a: p = "NICAM-BG"; break;
1078 case 0x0b: p = "NICAM-DK"; break;
1079 case 0x0c: p = "NICAM-I"; break;
1080 case 0x0d: p = "NICAM-L"; break;
1081 case 0x0e: p = "FM radio"; break;
1082 case 0x0f: p = "automatic detection"; break;
1083 default: p = "undefined";
1084 }
Hans Verkuilfac9e892006-01-09 15:32:40 -02001085 v4l_info(client, "Configured audio standard: %s\n", p);
Hans Verkuilbd985162005-11-13 16:07:56 -08001086
1087 if ((audio_config >> 4) < 0xF) {
1088 switch (audio_config & 0xF) {
1089 case 0x00: p = "MONO1 (LANGUAGE A/Mono L+R channel for BTSC, EIAJ, A2)"; break;
1090 case 0x01: p = "MONO2 (LANGUAGE B)"; break;
1091 case 0x02: p = "MONO3 (STEREO forced MONO)"; break;
1092 case 0x03: p = "MONO4 (NICAM ANALOG-Language C/Analog Fallback)"; break;
1093 case 0x04: p = "STEREO"; break;
1094 case 0x05: p = "DUAL1 (AB)"; break;
1095 case 0x06: p = "DUAL2 (AC) (FM)"; break;
1096 case 0x07: p = "DUAL3 (BC) (FM)"; break;
1097 case 0x08: p = "DUAL4 (AC) (AM)"; break;
1098 case 0x09: p = "DUAL5 (BC) (AM)"; break;
1099 case 0x0a: p = "SAP"; break;
1100 default: p = "undefined";
1101 }
Hans Verkuilfac9e892006-01-09 15:32:40 -02001102 v4l_info(client, "Configured audio mode: %s\n", p);
Hans Verkuilbd985162005-11-13 16:07:56 -08001103 } else {
1104 switch (audio_config & 0xF) {
1105 case 0x00: p = "BG"; break;
1106 case 0x01: p = "DK1"; break;
1107 case 0x02: p = "DK2"; break;
1108 case 0x03: p = "DK3"; break;
1109 case 0x04: p = "I"; break;
1110 case 0x05: p = "L"; break;
1111 case 0x06: p = "BTSC"; break;
1112 case 0x07: p = "EIAJ"; break;
1113 case 0x08: p = "A2-M"; break;
1114 case 0x09: p = "FM Radio"; break;
1115 case 0x0f: p = "automatic standard and mode detection"; break;
1116 default: p = "undefined";
1117 }
Hans Verkuilfac9e892006-01-09 15:32:40 -02001118 v4l_info(client, "Configured audio system: %s\n", p);
Hans Verkuilbd985162005-11-13 16:07:56 -08001119 }
1120
Hans Verkuila8bbf122006-01-09 15:25:42 -02001121 if (aud_input) {
Hans Verkuilfac9e892006-01-09 15:32:40 -02001122 v4l_info(client, "Specified audio input: Tuner (In%d)\n", aud_input);
Hans Verkuila8bbf122006-01-09 15:25:42 -02001123 } else {
Hans Verkuilfac9e892006-01-09 15:32:40 -02001124 v4l_info(client, "Specified audio input: External\n");
Hans Verkuila8bbf122006-01-09 15:25:42 -02001125 }
Hans Verkuilbd985162005-11-13 16:07:56 -08001126
Hans Verkuilbd985162005-11-13 16:07:56 -08001127 switch (pref_mode & 0xf) {
1128 case 0: p = "mono/language A"; break;
1129 case 1: p = "language B"; break;
1130 case 2: p = "language C"; break;
1131 case 3: p = "analog fallback"; break;
1132 case 4: p = "stereo"; break;
1133 case 5: p = "language AC"; break;
1134 case 6: p = "language BC"; break;
1135 case 7: p = "language AB"; break;
1136 default: p = "undefined";
1137 }
Hans Verkuilfac9e892006-01-09 15:32:40 -02001138 v4l_info(client, "Preferred audio mode: %s\n", p);
Hans Verkuilbd985162005-11-13 16:07:56 -08001139
1140 if ((audio_config & 0xf) == 0xf) {
1141 switch ((afc0 >> 3) & 0x3) {
1142 case 0: p = "system DK"; break;
1143 case 1: p = "system L"; break;
1144 case 2: p = "autodetect"; break;
1145 default: p = "undefined";
1146 }
Hans Verkuilfac9e892006-01-09 15:32:40 -02001147 v4l_info(client, "Selected 65 MHz format: %s\n", p);
Hans Verkuilbd985162005-11-13 16:07:56 -08001148
1149 switch (afc0 & 0x7) {
1150 case 0: p = "chroma"; break;
1151 case 1: p = "BTSC"; break;
1152 case 2: p = "EIAJ"; break;
1153 case 3: p = "A2-M"; break;
1154 case 4: p = "autodetect"; break;
1155 default: p = "undefined";
1156 }
Hans Verkuilfac9e892006-01-09 15:32:40 -02001157 v4l_info(client, "Selected 45 MHz format: %s\n", p);
Hans Verkuilbd985162005-11-13 16:07:56 -08001158 }
1159}