blob: 67aa0db4b81a1049d21b7a096650d86362325a41 [file] [log] [blame]
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -03001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * vpx3220a, vpx3216b & vpx3214c video decoder driver version 0.0.1
3 *
4 * Copyright (C) 2001 Laurent Pinchart <lpinchart@freegates.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/delay.h>
24#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/i2c.h>
Mauro Carvalho Chehab5e87efa2006-06-05 10:26:32 -030027#include <media/v4l2-common.h>
Hans Verkuil23848b62008-09-07 08:01:39 -030028#include <media/v4l2-i2c-drv-legacy.h>
29#include <linux/videodev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/video_decoder.h>
31
Hans Verkuil23848b62008-09-07 08:01:39 -030032MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
33MODULE_AUTHOR("Laurent Pinchart");
34MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030036static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037module_param(debug, int, 0);
38MODULE_PARM_DESC(debug, "Debug level (0-1)");
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define VPX_TIMEOUT_COUNT 10
41
42/* ----------------------------------------------------------------------- */
43
44struct vpx3220 {
45 unsigned char reg[255];
46
47 int norm;
48 int input;
49 int enable;
50 int bright;
51 int contrast;
52 int hue;
53 int sat;
54};
55
56static char *inputs[] = { "internal", "composite", "svideo" };
57
58/* ----------------------------------------------------------------------- */
Hans Verkuil23848b62008-09-07 08:01:39 -030059
60static inline int vpx3220_write(struct i2c_client *client, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 struct vpx3220 *decoder = i2c_get_clientdata(client);
63
64 decoder->reg[reg] = value;
65 return i2c_smbus_write_byte_data(client, reg, value);
66}
67
Hans Verkuil23848b62008-09-07 08:01:39 -030068static inline int vpx3220_read(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 return i2c_smbus_read_byte_data(client, reg);
71}
72
Hans Verkuil23848b62008-09-07 08:01:39 -030073static int vpx3220_fp_status(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 unsigned char status;
76 unsigned int i;
77
78 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
79 status = vpx3220_read(client, 0x29);
80
81 if (!(status & 4))
82 return 0;
83
84 udelay(10);
85
86 if (need_resched())
87 cond_resched();
88 }
89
90 return -1;
91}
92
Hans Verkuil23848b62008-09-07 08:01:39 -030093static int vpx3220_fp_write(struct i2c_client *client, u8 fpaddr, u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 /* Write the 16-bit address to the FPWR register */
96 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
Hans Verkuil23848b62008-09-07 08:01:39 -030097 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -1;
99 }
100
101 if (vpx3220_fp_status(client) < 0)
102 return -1;
103
104 /* Write the 16-bit data to the FPDAT register */
105 if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
Hans Verkuil23848b62008-09-07 08:01:39 -0300106 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -1;
108 }
109
110 return 0;
111}
112
Hans Verkuil23848b62008-09-07 08:01:39 -0300113static u16 vpx3220_fp_read(struct i2c_client *client, u16 fpaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 s16 data;
116
117 /* Write the 16-bit address to the FPRD register */
118 if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
Hans Verkuil23848b62008-09-07 08:01:39 -0300119 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return -1;
121 }
122
123 if (vpx3220_fp_status(client) < 0)
124 return -1;
125
126 /* Read the 16-bit data from the FPDAT register */
127 data = i2c_smbus_read_word_data(client, 0x28);
128 if (data == -1) {
Hans Verkuil23848b62008-09-07 08:01:39 -0300129 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -1;
131 }
132
133 return swab16(data);
134}
135
Hans Verkuil23848b62008-09-07 08:01:39 -0300136static int vpx3220_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 u8 reg;
139 int ret = -1;
140
141 while (len >= 2) {
142 reg = *data++;
Hans Verkuil23848b62008-09-07 08:01:39 -0300143 ret = vpx3220_write(client, reg, *data++);
144 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 break;
146 len -= 2;
147 }
148
149 return ret;
150}
151
Hans Verkuil23848b62008-09-07 08:01:39 -0300152static int vpx3220_write_fp_block(struct i2c_client *client,
153 const u16 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 u8 reg;
156 int ret = 0;
157
158 while (len > 1) {
159 reg = *data++;
160 ret |= vpx3220_fp_write(client, reg, *data++);
161 len -= 2;
162 }
163
164 return ret;
165}
166
167/* ---------------------------------------------------------------------- */
168
169static const unsigned short init_ntsc[] = {
170 0x1c, 0x00, /* NTSC tint angle */
171 0x88, 17, /* Window 1 vertical */
172 0x89, 240, /* Vertical lines in */
173 0x8a, 240, /* Vertical lines out */
174 0x8b, 000, /* Horizontal begin */
175 0x8c, 640, /* Horizontal length */
176 0x8d, 640, /* Number of pixels */
177 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700178 0xf0, 0x73, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * mode, latch windows */
180 0xf2, 0x13, /* NTSC M, composite input */
181 0xe7, 0x1e1, /* Enable vertical standard
182 * locking @ 240 lines */
183};
184
185static const unsigned short init_pal[] = {
186 0x88, 23, /* Window 1 vertical begin */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700187 0x89, 288, /* Vertical lines in (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * skipped by the VFE) */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700189 0x8a, 288, /* Vertical lines out (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * skipped by the VFE) */
191 0x8b, 16, /* Horizontal begin */
192 0x8c, 768, /* Horizontal length */
193 0x8d, 784, /* Number of pixels
194 * Must be >= Horizontal begin + Horizontal length */
195 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700196 0xf0, 0x77, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * mode, latch windows */
198 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700199 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
202static const unsigned short init_secam[] = {
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700203 0x88, 23, /* Window 1 vertical begin */
204 0x89, 288, /* Vertical lines in (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * skipped by the VFE) */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700206 0x8a, 288, /* Vertical lines out (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * skipped by the VFE) */
208 0x8b, 16, /* Horizontal begin */
209 0x8c, 768, /* Horizontal length */
210 0x8d, 784, /* Number of pixels
211 * Must be >= Horizontal begin + Horizontal length */
212 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700213 0xf0, 0x77, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 * mode, latch windows */
215 0xf2, 0x3d5, /* SECAM, composite input */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700216 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
219static const unsigned char init_common[] = {
220 0xf2, 0x00, /* Disable all outputs */
221 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
222 * (clamp off) */
223 0xd8, 0xa8, /* HREF/VREF active high, VREF
224 * pulse = 2, Odd/Even flag */
225 0x20, 0x03, /* IF compensation 0dB/oct */
226 0xe0, 0xff, /* Open up all comparators */
227 0xe1, 0x00,
228 0xe2, 0x7f,
229 0xe3, 0x80,
230 0xe4, 0x7f,
231 0xe5, 0x80,
232 0xe6, 0x00, /* Brightness set to 0 */
233 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
234 * 10 to 8 2-bit error diffusion */
235 0xe8, 0xf8, /* YUV422, CbCr binary offset,
236 * ... (p.32) */
237 0xea, 0x18, /* LLC2 connected, output FIFO
238 * reset with VACTintern */
239 0xf0, 0x8a, /* Half full level to 10, bus
240 * shuffler [7:0, 23:16, 15:8] */
241 0xf1, 0x18, /* Single clock, sync mode, no
242 * FE delay, no HLEN counter */
243 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
244 * strength to 2 */
245 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
246 * ALPHA strength to 4 */
247};
248
249static const unsigned short init_fp[] = {
250 0x59, 0,
251 0xa0, 2070, /* ACC reference */
252 0xa3, 0,
253 0xa4, 0,
254 0xa8, 30,
255 0xb2, 768,
256 0xbe, 27,
257 0x58, 0,
258 0x26, 0,
259 0x4b, 0x298, /* PLL gain */
260};
261
Hans Verkuil23848b62008-09-07 08:01:39 -0300262static void vpx3220_dump_i2c(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int len = sizeof(init_common);
265 const unsigned char *data = init_common;
266
267 while (len > 1) {
Hans Verkuil23848b62008-09-07 08:01:39 -0300268 v4l_dbg(1, debug, client, "i2c reg 0x%02x data 0x%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *data, vpx3220_read(client, *data));
270 data += 2;
271 len -= 2;
272 }
273}
274
Hans Verkuil23848b62008-09-07 08:01:39 -0300275static int vpx3220_command(struct i2c_client *client, unsigned cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct vpx3220 *decoder = i2c_get_clientdata(client);
278
279 switch (cmd) {
280 case 0:
281 {
282 vpx3220_write_block(client, init_common,
283 sizeof(init_common));
284 vpx3220_write_fp_block(client, init_fp,
285 sizeof(init_fp) >> 1);
286 switch (decoder->norm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 case VIDEO_MODE_NTSC:
288 vpx3220_write_fp_block(client, init_ntsc,
289 sizeof(init_ntsc) >> 1);
290 break;
291
292 case VIDEO_MODE_PAL:
293 vpx3220_write_fp_block(client, init_pal,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300294 sizeof(init_pal) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 break;
296 case VIDEO_MODE_SECAM:
297 vpx3220_write_fp_block(client, init_secam,
298 sizeof(init_secam) >> 1);
299 break;
300 default:
301 vpx3220_write_fp_block(client, init_pal,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300302 sizeof(init_pal) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 case DECODER_DUMP:
309 {
310 vpx3220_dump_i2c(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 case DECODER_GET_CAPABILITIES:
315 {
316 struct video_decoder_capability *cap = arg;
317
Hans Verkuil23848b62008-09-07 08:01:39 -0300318 v4l_dbg(1, debug, client, "DECODER_GET_CAPABILITIES\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 cap->flags = VIDEO_DECODER_PAL |
321 VIDEO_DECODER_NTSC |
322 VIDEO_DECODER_SECAM |
323 VIDEO_DECODER_AUTO |
324 VIDEO_DECODER_CCIR;
325 cap->inputs = 3;
326 cap->outputs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 case DECODER_GET_STATUS:
331 {
332 int res = 0, status;
333
Hans Verkuil23848b62008-09-07 08:01:39 -0300334 v4l_dbg(1, debug, client, "DECODER_GET_STATUS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 status = vpx3220_fp_read(client, 0x0f3);
337
Hans Verkuil23848b62008-09-07 08:01:39 -0300338 v4l_dbg(1, debug, client, "status: 0x%04x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 if (status < 0)
341 return status;
342
343 if ((status & 0x20) == 0) {
344 res |= DECODER_STATUS_GOOD | DECODER_STATUS_COLOR;
345
346 switch (status & 0x18) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 case 0x00:
348 case 0x10:
349 case 0x14:
350 case 0x18:
351 res |= DECODER_STATUS_PAL;
352 break;
353
354 case 0x08:
355 res |= DECODER_STATUS_SECAM;
356 break;
357
358 case 0x04:
359 case 0x0c:
360 case 0x1c:
361 res |= DECODER_STATUS_NTSC;
362 break;
363 }
364 }
365
366 *(int *) arg = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 case DECODER_SET_NORM:
371 {
372 int *iarg = arg, data;
Ronald S. Bultjede21eb62005-10-16 20:29:25 -0700373 int temp_input;
374
375 /* Here we back up the input selection because it gets
376 overwritten when we fill the registers with the
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300377 choosen video norm */
Ronald S. Bultjede21eb62005-10-16 20:29:25 -0700378 temp_input = vpx3220_fp_read(client, 0xf2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Hans Verkuil23848b62008-09-07 08:01:39 -0300380 v4l_dbg(1, debug, client, "DECODER_SET_NORM %d\n", *iarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 switch (*iarg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 case VIDEO_MODE_NTSC:
383 vpx3220_write_fp_block(client, init_ntsc,
384 sizeof(init_ntsc) >> 1);
Hans Verkuil23848b62008-09-07 08:01:39 -0300385 v4l_dbg(1, debug, client, "norm switched to NTSC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387
388 case VIDEO_MODE_PAL:
389 vpx3220_write_fp_block(client, init_pal,
390 sizeof(init_pal) >> 1);
Hans Verkuil23848b62008-09-07 08:01:39 -0300391 v4l_dbg(1, debug, client, "norm switched to PAL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 break;
393
394 case VIDEO_MODE_SECAM:
395 vpx3220_write_fp_block(client, init_secam,
396 sizeof(init_secam) >> 1);
Hans Verkuil23848b62008-09-07 08:01:39 -0300397 v4l_dbg(1, debug, client, "norm switched to SECAM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 break;
399
400 case VIDEO_MODE_AUTO:
401 /* FIXME This is only preliminary support */
402 data = vpx3220_fp_read(client, 0xf2) & 0x20;
403 vpx3220_fp_write(client, 0xf2, 0x00c0 | data);
Hans Verkuil23848b62008-09-07 08:01:39 -0300404 v4l_dbg(1, debug, client, "norm switched to AUTO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
406
407 default:
408 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410 decoder->norm = *iarg;
Ronald S. Bultjede21eb62005-10-16 20:29:25 -0700411
412 /* And here we set the backed up video input again */
413 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
414 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 case DECODER_SET_INPUT:
419 {
420 int *iarg = arg, data;
421
422 /* RJ: *iarg = 0: ST8 (PCTV) input
423 *iarg = 1: COMPOSITE input
424 *iarg = 2: SVHS input */
425
426 const int input[3][2] = {
427 {0x0c, 0},
428 {0x0d, 0},
429 {0x0e, 1}
430 };
431
432 if (*iarg < 0 || *iarg > 2)
433 return -EINVAL;
434
Hans Verkuil23848b62008-09-07 08:01:39 -0300435 v4l_dbg(1, debug, client, "input switched to %s\n", inputs[*iarg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 vpx3220_write(client, 0x33, input[*iarg][0]);
438
439 data = vpx3220_fp_read(client, 0xf2) & ~(0x0020);
440 if (data < 0)
441 return data;
442 /* 0x0010 is required to latch the setting */
443 vpx3220_fp_write(client, 0xf2,
444 data | (input[*iarg][1] << 5) | 0x0010);
445
446 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 case DECODER_SET_OUTPUT:
451 {
452 int *iarg = arg;
453
454 /* not much choice of outputs */
455 if (*iarg != 0) {
456 return -EINVAL;
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 case DECODER_ENABLE_OUTPUT:
462 {
463 int *iarg = arg;
464
Hans Verkuil23848b62008-09-07 08:01:39 -0300465 v4l_dbg(1, debug, client, "DECODER_ENABLE_OUTPUT %d\n", *iarg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 vpx3220_write(client, 0xf2, (*iarg ? 0x1b : 0x00));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 case DECODER_SET_PICTURE:
472 {
473 struct video_picture *pic = arg;
474
475 if (decoder->bright != pic->brightness) {
476 /* We want -128 to 128 we get 0-65535 */
477 decoder->bright = pic->brightness;
478 vpx3220_write(client, 0xe6,
479 (decoder->bright - 32768) >> 8);
480 }
481 if (decoder->contrast != pic->contrast) {
482 /* We want 0 to 64 we get 0-65535 */
483 /* Bit 7 and 8 is for noise shaping */
484 decoder->contrast = pic->contrast;
485 vpx3220_write(client, 0xe7,
486 (decoder->contrast >> 10) + 192);
487 }
488 if (decoder->sat != pic->colour) {
489 /* We want 0 to 4096 we get 0-65535 */
490 decoder->sat = pic->colour;
491 vpx3220_fp_write(client, 0xa0,
492 decoder->sat >> 4);
493 }
494 if (decoder->hue != pic->hue) {
495 /* We want -512 to 512 we get 0-65535 */
496 decoder->hue = pic->hue;
497 vpx3220_fp_write(client, 0x1c,
498 ((decoder->hue - 32768) >> 6) & 0xFFF);
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 default:
504 return -EINVAL;
505 }
506
507 return 0;
508}
509
Hans Verkuil23848b62008-09-07 08:01:39 -0300510static int vpx3220_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 vpx3220_write_block(client, init_common, sizeof(init_common));
513 vpx3220_write_fp_block(client, init_fp, sizeof(init_fp) >> 1);
514 /* Default to PAL */
515 vpx3220_write_fp_block(client, init_pal, sizeof(init_pal) >> 1);
516
517 return 0;
518}
519
520/* -----------------------------------------------------------------------
Joe Perchesc84e6032008-02-03 17:18:59 +0200521 * Client management code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
523
Hans Verkuil23848b62008-09-07 08:01:39 -0300524static unsigned short normal_i2c[] = { 0x86 >> 1, 0x8e >> 1, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Hans Verkuil23848b62008-09-07 08:01:39 -0300526I2C_CLIENT_INSMOD;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300527
Hans Verkuil23848b62008-09-07 08:01:39 -0300528static int vpx3220_probe(struct i2c_client *client,
529 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct vpx3220 *decoder;
Hans Verkuil23848b62008-09-07 08:01:39 -0300532 const char *name = NULL;
533 u8 ver;
534 u16 pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 /* Check if the adapter supports the needed features */
Hans Verkuil23848b62008-09-07 08:01:39 -0300537 if (!i2c_check_functionality(client->adapter,
538 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
539 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Panagiotis Issaris74081872006-01-11 19:40:56 -0200541 decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
Hans Verkuil23848b62008-09-07 08:01:39 -0300542 if (decoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 decoder->norm = VIDEO_MODE_PAL;
545 decoder->input = 0;
546 decoder->enable = 1;
547 decoder->bright = 32768;
548 decoder->contrast = 32768;
549 decoder->hue = 32768;
550 decoder->sat = 32768;
551 i2c_set_clientdata(client, decoder);
552
Hans Verkuil23848b62008-09-07 08:01:39 -0300553 ver = i2c_smbus_read_byte_data(client, 0x00);
554 pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
555 i2c_smbus_read_byte_data(client, 0x01);
556 if (ver == 0xec) {
557 switch (pn) {
558 case 0x4680:
559 name = "vpx3220a";
560 break;
561 case 0x4260:
562 name = "vpx3216b";
563 break;
564 case 0x4280:
565 name = "vpx3214c";
566 break;
567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Hans Verkuil23848b62008-09-07 08:01:39 -0300569 if (name)
570 v4l_info(client, "%s found @ 0x%x (%s)\n", name,
571 client->addr << 1, client->adapter->name);
572 else
573 v4l_info(client, "chip (%02x:%04x) found @ 0x%x (%s)\n",
574 ver, pn, client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 vpx3220_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578}
579
Hans Verkuil23848b62008-09-07 08:01:39 -0300580static int vpx3220_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Hans Verkuil23848b62008-09-07 08:01:39 -0300582 kfree(i2c_get_clientdata(client));
583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Hans Verkuil23848b62008-09-07 08:01:39 -0300586static const struct i2c_device_id vpx3220_id[] = {
587 { "vpx3220a", 0 },
588 { "vpx3216b", 0 },
589 { "vpx3214c", 0 },
590 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591};
Hans Verkuil23848b62008-09-07 08:01:39 -0300592MODULE_DEVICE_TABLE(i2c, vpx3220_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Hans Verkuil23848b62008-09-07 08:01:39 -0300594static struct v4l2_i2c_driver_data v4l2_i2c_data = {
595 .name = "vpx3220",
596 .driverid = I2C_DRIVERID_VPX3220,
597 .command = vpx3220_command,
598 .probe = vpx3220_probe,
599 .remove = vpx3220_remove,
600 .id_table = vpx3220_id,
601};