blob: cd2064e7733b041f0ddb1031587d38e732968d6b [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>
Hans Verkuil107063c2009-02-18 17:26:06 -030030#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/video_decoder.h>
32
Hans Verkuil23848b62008-09-07 08:01:39 -030033MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
34MODULE_AUTHOR("Laurent Pinchart");
35MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030037static int debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038module_param(debug, int, 0);
39MODULE_PARM_DESC(debug, "Debug level (0-1)");
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define VPX_TIMEOUT_COUNT 10
42
43/* ----------------------------------------------------------------------- */
44
45struct vpx3220 {
46 unsigned char reg[255];
47
Hans Verkuil107063c2009-02-18 17:26:06 -030048 v4l2_std_id norm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int input;
50 int enable;
51 int bright;
52 int contrast;
53 int hue;
54 int sat;
55};
56
57static char *inputs[] = { "internal", "composite", "svideo" };
58
59/* ----------------------------------------------------------------------- */
Hans Verkuil23848b62008-09-07 08:01:39 -030060
61static inline int vpx3220_write(struct i2c_client *client, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct vpx3220 *decoder = i2c_get_clientdata(client);
64
65 decoder->reg[reg] = value;
66 return i2c_smbus_write_byte_data(client, reg, value);
67}
68
Hans Verkuil23848b62008-09-07 08:01:39 -030069static inline int vpx3220_read(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71 return i2c_smbus_read_byte_data(client, reg);
72}
73
Hans Verkuil23848b62008-09-07 08:01:39 -030074static int vpx3220_fp_status(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 unsigned char status;
77 unsigned int i;
78
79 for (i = 0; i < VPX_TIMEOUT_COUNT; i++) {
80 status = vpx3220_read(client, 0x29);
81
82 if (!(status & 4))
83 return 0;
84
85 udelay(10);
86
87 if (need_resched())
88 cond_resched();
89 }
90
91 return -1;
92}
93
Hans Verkuil23848b62008-09-07 08:01:39 -030094static int vpx3220_fp_write(struct i2c_client *client, u8 fpaddr, u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 /* Write the 16-bit address to the FPWR register */
97 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
Hans Verkuil23848b62008-09-07 08:01:39 -030098 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -1;
100 }
101
102 if (vpx3220_fp_status(client) < 0)
103 return -1;
104
105 /* Write the 16-bit data to the FPDAT register */
106 if (i2c_smbus_write_word_data(client, 0x28, swab16(data)) == -1) {
Hans Verkuil23848b62008-09-07 08:01:39 -0300107 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -1;
109 }
110
111 return 0;
112}
113
Hans Verkuil23848b62008-09-07 08:01:39 -0300114static u16 vpx3220_fp_read(struct i2c_client *client, u16 fpaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 s16 data;
117
118 /* Write the 16-bit address to the FPRD register */
119 if (i2c_smbus_write_word_data(client, 0x26, swab16(fpaddr)) == -1) {
Hans Verkuil23848b62008-09-07 08:01:39 -0300120 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return -1;
122 }
123
124 if (vpx3220_fp_status(client) < 0)
125 return -1;
126
127 /* Read the 16-bit data from the FPDAT register */
128 data = i2c_smbus_read_word_data(client, 0x28);
129 if (data == -1) {
Hans Verkuil23848b62008-09-07 08:01:39 -0300130 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return -1;
132 }
133
134 return swab16(data);
135}
136
Hans Verkuil23848b62008-09-07 08:01:39 -0300137static int vpx3220_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 u8 reg;
140 int ret = -1;
141
142 while (len >= 2) {
143 reg = *data++;
Hans Verkuil23848b62008-09-07 08:01:39 -0300144 ret = vpx3220_write(client, reg, *data++);
145 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 break;
147 len -= 2;
148 }
149
150 return ret;
151}
152
Hans Verkuil23848b62008-09-07 08:01:39 -0300153static int vpx3220_write_fp_block(struct i2c_client *client,
154 const u16 *data, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 u8 reg;
157 int ret = 0;
158
159 while (len > 1) {
160 reg = *data++;
161 ret |= vpx3220_fp_write(client, reg, *data++);
162 len -= 2;
163 }
164
165 return ret;
166}
167
168/* ---------------------------------------------------------------------- */
169
170static const unsigned short init_ntsc[] = {
171 0x1c, 0x00, /* NTSC tint angle */
172 0x88, 17, /* Window 1 vertical */
173 0x89, 240, /* Vertical lines in */
174 0x8a, 240, /* Vertical lines out */
175 0x8b, 000, /* Horizontal begin */
176 0x8c, 640, /* Horizontal length */
177 0x8d, 640, /* Number of pixels */
178 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700179 0xf0, 0x73, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * mode, latch windows */
181 0xf2, 0x13, /* NTSC M, composite input */
182 0xe7, 0x1e1, /* Enable vertical standard
183 * locking @ 240 lines */
184};
185
186static const unsigned short init_pal[] = {
187 0x88, 23, /* Window 1 vertical begin */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700188 0x89, 288, /* Vertical lines in (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * skipped by the VFE) */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700190 0x8a, 288, /* Vertical lines out (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * skipped by the VFE) */
192 0x8b, 16, /* Horizontal begin */
193 0x8c, 768, /* Horizontal length */
194 0x8d, 784, /* Number of pixels
195 * Must be >= Horizontal begin + Horizontal length */
196 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700197 0xf0, 0x77, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * mode, latch windows */
199 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700200 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201};
202
203static const unsigned short init_secam[] = {
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700204 0x88, 23, /* Window 1 vertical begin */
205 0x89, 288, /* Vertical lines in (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * skipped by the VFE) */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700207 0x8a, 288, /* Vertical lines out (16 lines
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * skipped by the VFE) */
209 0x8b, 16, /* Horizontal begin */
210 0x8c, 768, /* Horizontal length */
211 0x8d, 784, /* Number of pixels
212 * Must be >= Horizontal begin + Horizontal length */
213 0x8f, 0xc00, /* Disable window 2 */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700214 0xf0, 0x77, /* 13.5 MHz transport, Forced
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 * mode, latch windows */
216 0xf2, 0x3d5, /* SECAM, composite input */
Ronald S. Bultje9b3acc22005-10-16 20:29:24 -0700217 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
220static const unsigned char init_common[] = {
221 0xf2, 0x00, /* Disable all outputs */
222 0x33, 0x0d, /* Luma : VIN2, Chroma : CIN
223 * (clamp off) */
224 0xd8, 0xa8, /* HREF/VREF active high, VREF
225 * pulse = 2, Odd/Even flag */
226 0x20, 0x03, /* IF compensation 0dB/oct */
227 0xe0, 0xff, /* Open up all comparators */
228 0xe1, 0x00,
229 0xe2, 0x7f,
230 0xe3, 0x80,
231 0xe4, 0x7f,
232 0xe5, 0x80,
233 0xe6, 0x00, /* Brightness set to 0 */
234 0xe7, 0xe0, /* Contrast to 1.0, noise shaping
235 * 10 to 8 2-bit error diffusion */
236 0xe8, 0xf8, /* YUV422, CbCr binary offset,
237 * ... (p.32) */
238 0xea, 0x18, /* LLC2 connected, output FIFO
239 * reset with VACTintern */
240 0xf0, 0x8a, /* Half full level to 10, bus
241 * shuffler [7:0, 23:16, 15:8] */
242 0xf1, 0x18, /* Single clock, sync mode, no
243 * FE delay, no HLEN counter */
244 0xf8, 0x12, /* Port A, PIXCLK, HF# & FE#
245 * strength to 2 */
246 0xf9, 0x24, /* Port B, HREF, VREF, PREF &
247 * ALPHA strength to 4 */
248};
249
250static const unsigned short init_fp[] = {
251 0x59, 0,
252 0xa0, 2070, /* ACC reference */
253 0xa3, 0,
254 0xa4, 0,
255 0xa8, 30,
256 0xb2, 768,
257 0xbe, 27,
258 0x58, 0,
259 0x26, 0,
260 0x4b, 0x298, /* PLL gain */
261};
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Hans Verkuil23848b62008-09-07 08:01:39 -0300264static int vpx3220_command(struct i2c_client *client, unsigned cmd, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct vpx3220 *decoder = i2c_get_clientdata(client);
267
268 switch (cmd) {
Hans Verkuil107063c2009-02-18 17:26:06 -0300269 case VIDIOC_INT_INIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 {
271 vpx3220_write_block(client, init_common,
272 sizeof(init_common));
273 vpx3220_write_fp_block(client, init_fp,
274 sizeof(init_fp) >> 1);
Hans Verkuil107063c2009-02-18 17:26:06 -0300275 if (decoder->norm & V4L2_STD_NTSC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 vpx3220_write_fp_block(client, init_ntsc,
277 sizeof(init_ntsc) >> 1);
Hans Verkuil107063c2009-02-18 17:26:06 -0300278 } else if (decoder->norm & V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 vpx3220_write_fp_block(client, init_pal,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300280 sizeof(init_pal) >> 1);
Hans Verkuil107063c2009-02-18 17:26:06 -0300281 } else if (decoder->norm & V4L2_STD_SECAM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 vpx3220_write_fp_block(client, init_secam,
283 sizeof(init_secam) >> 1);
Hans Verkuil107063c2009-02-18 17:26:06 -0300284 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 vpx3220_write_fp_block(client, init_pal,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300286 sizeof(init_pal) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Hans Verkuil107063c2009-02-18 17:26:06 -0300291 case VIDIOC_QUERYSTD:
292 case VIDIOC_INT_G_INPUT_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300294 int res = V4L2_IN_ST_NO_SIGNAL, status;
295 v4l2_std_id std = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Hans Verkuil107063c2009-02-18 17:26:06 -0300297 v4l_dbg(1, debug, client, "VIDIOC_QUERYSTD/VIDIOC_INT_G_INPUT_STATUS\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 status = vpx3220_fp_read(client, 0x0f3);
300
Hans Verkuil23848b62008-09-07 08:01:39 -0300301 v4l_dbg(1, debug, client, "status: 0x%04x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (status < 0)
304 return status;
305
306 if ((status & 0x20) == 0) {
Hans Verkuil107063c2009-02-18 17:26:06 -0300307 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 switch (status & 0x18) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 case 0x00:
311 case 0x10:
312 case 0x14:
313 case 0x18:
Hans Verkuil107063c2009-02-18 17:26:06 -0300314 std = V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 break;
316
317 case 0x08:
Hans Verkuil107063c2009-02-18 17:26:06 -0300318 std = V4L2_STD_SECAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
320
321 case 0x04:
322 case 0x0c:
323 case 0x1c:
Hans Verkuil107063c2009-02-18 17:26:06 -0300324 std = V4L2_STD_NTSC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 break;
326 }
327 }
328
Hans Verkuil107063c2009-02-18 17:26:06 -0300329 if (cmd == VIDIOC_QUERYSTD)
330 *(v4l2_std_id *)arg = std;
331 else
332 *(int *)arg = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Hans Verkuil107063c2009-02-18 17:26:06 -0300336 case VIDIOC_S_STD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300338 v4l2_std_id *iarg = arg;
Ronald S. Bultjede21eb62005-10-16 20:29:25 -0700339 int temp_input;
340
341 /* Here we back up the input selection because it gets
342 overwritten when we fill the registers with the
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300343 choosen video norm */
Ronald S. Bultjede21eb62005-10-16 20:29:25 -0700344 temp_input = vpx3220_fp_read(client, 0xf2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Hans Verkuil107063c2009-02-18 17:26:06 -0300346 v4l_dbg(1, debug, client, "VIDIOC_S_STD %llx\n", *iarg);
347 if (*iarg & V4L2_STD_NTSC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 vpx3220_write_fp_block(client, init_ntsc,
349 sizeof(init_ntsc) >> 1);
Hans Verkuil23848b62008-09-07 08:01:39 -0300350 v4l_dbg(1, debug, client, "norm switched to NTSC\n");
Hans Verkuil107063c2009-02-18 17:26:06 -0300351 } else if (*iarg & V4L2_STD_PAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 vpx3220_write_fp_block(client, init_pal,
353 sizeof(init_pal) >> 1);
Hans Verkuil23848b62008-09-07 08:01:39 -0300354 v4l_dbg(1, debug, client, "norm switched to PAL\n");
Hans Verkuil107063c2009-02-18 17:26:06 -0300355 } else if (*iarg & V4L2_STD_SECAM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 vpx3220_write_fp_block(client, init_secam,
357 sizeof(init_secam) >> 1);
Hans Verkuil23848b62008-09-07 08:01:39 -0300358 v4l_dbg(1, debug, client, "norm switched to SECAM\n");
Hans Verkuil107063c2009-02-18 17:26:06 -0300359 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Hans Verkuil107063c2009-02-18 17:26:06 -0300362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 decoder->norm = *iarg;
Ronald S. Bultjede21eb62005-10-16 20:29:25 -0700364
365 /* And here we set the backed up video input again */
366 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
367 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Hans Verkuil107063c2009-02-18 17:26:06 -0300371 case VIDIOC_INT_S_VIDEO_ROUTING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300373 struct v4l2_routing *route = arg;
374 int data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 /* RJ: *iarg = 0: ST8 (PCTV) input
377 *iarg = 1: COMPOSITE input
378 *iarg = 2: SVHS input */
379
380 const int input[3][2] = {
381 {0x0c, 0},
382 {0x0d, 0},
383 {0x0e, 1}
384 };
385
Hans Verkuil107063c2009-02-18 17:26:06 -0300386 if (route->input < 0 || route->input > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return -EINVAL;
388
Hans Verkuil107063c2009-02-18 17:26:06 -0300389 v4l_dbg(1, debug, client, "input switched to %s\n", inputs[route->input]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Hans Verkuil107063c2009-02-18 17:26:06 -0300391 vpx3220_write(client, 0x33, input[route->input][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 data = vpx3220_fp_read(client, 0xf2) & ~(0x0020);
394 if (data < 0)
395 return data;
396 /* 0x0010 is required to latch the setting */
397 vpx3220_fp_write(client, 0xf2,
Hans Verkuil107063c2009-02-18 17:26:06 -0300398 data | (input[route->input][1] << 5) | 0x0010);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Hans Verkuil107063c2009-02-18 17:26:06 -0300404 case VIDIOC_STREAMON:
405 case VIDIOC_STREAMOFF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300407 int on = cmd == VIDIOC_STREAMON;
408 v4l_dbg(1, debug, client, "VIDIOC_STREAM%s\n", on ? "ON" : "OFF");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Hans Verkuil107063c2009-02-18 17:26:06 -0300410 vpx3220_write(client, 0xf2, (on ? 0x1b : 0x00));
411 break;
412 }
413
414 case VIDIOC_QUERYCTRL:
415 {
416 struct v4l2_queryctrl *qc = arg;
417
418 switch (qc->id) {
419 case V4L2_CID_BRIGHTNESS:
420 v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
421 break;
422
423 case V4L2_CID_CONTRAST:
424 v4l2_ctrl_query_fill(qc, 0, 63, 1, 32);
425 break;
426
427 case V4L2_CID_SATURATION:
428 v4l2_ctrl_query_fill(qc, 0, 4095, 1, 2048);
429 break;
430
431 case V4L2_CID_HUE:
432 v4l2_ctrl_query_fill(qc, -512, 511, 1, 0);
433 break;
434
435 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -EINVAL;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Hans Verkuil107063c2009-02-18 17:26:06 -0300441 case VIDIOC_G_CTRL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300443 struct v4l2_control *ctrl = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Hans Verkuil107063c2009-02-18 17:26:06 -0300445 switch (ctrl->id) {
446 case V4L2_CID_BRIGHTNESS:
447 ctrl->value = decoder->bright;
448 break;
449 case V4L2_CID_CONTRAST:
450 ctrl->value = decoder->contrast;
451 break;
452 case V4L2_CID_SATURATION:
453 ctrl->value = decoder->sat;
454 break;
455 case V4L2_CID_HUE:
456 ctrl->value = decoder->hue;
457 break;
458 default:
459 return -EINVAL;
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Hans Verkuil107063c2009-02-18 17:26:06 -0300464 case VIDIOC_S_CTRL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 {
Hans Verkuil107063c2009-02-18 17:26:06 -0300466 struct v4l2_control *ctrl = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Hans Verkuil107063c2009-02-18 17:26:06 -0300468 switch (ctrl->id) {
469 case V4L2_CID_BRIGHTNESS:
470 if (decoder->bright != ctrl->value) {
471 decoder->bright = ctrl->value;
472 vpx3220_write(client, 0xe6, decoder->bright);
473 }
474 break;
475 case V4L2_CID_CONTRAST:
476 if (decoder->contrast != ctrl->value) {
477 /* Bit 7 and 8 is for noise shaping */
478 decoder->contrast = ctrl->value;
479 vpx3220_write(client, 0xe7,
480 decoder->contrast + 192);
481 }
482 break;
483 case V4L2_CID_SATURATION:
484 if (decoder->sat != ctrl->value) {
485 decoder->sat = ctrl->value;
486 vpx3220_fp_write(client, 0xa0, decoder->sat);
487 }
488 break;
489 case V4L2_CID_HUE:
490 if (decoder->hue != ctrl->value) {
491 decoder->hue = ctrl->value;
492 vpx3220_fp_write(client, 0x1c, decoder->hue);
493 }
494 break;
495 default:
496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
Hans Verkuil23848b62008-09-07 08:01:39 -0300499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 default:
502 return -EINVAL;
503 }
504
505 return 0;
506}
507
Hans Verkuil23848b62008-09-07 08:01:39 -0300508static int vpx3220_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 vpx3220_write_block(client, init_common, sizeof(init_common));
511 vpx3220_write_fp_block(client, init_fp, sizeof(init_fp) >> 1);
512 /* Default to PAL */
513 vpx3220_write_fp_block(client, init_pal, sizeof(init_pal) >> 1);
514
515 return 0;
516}
517
518/* -----------------------------------------------------------------------
Joe Perchesc84e6032008-02-03 17:18:59 +0200519 * Client management code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
521
Hans Verkuil23848b62008-09-07 08:01:39 -0300522static unsigned short normal_i2c[] = { 0x86 >> 1, 0x8e >> 1, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Hans Verkuil23848b62008-09-07 08:01:39 -0300524I2C_CLIENT_INSMOD;
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300525
Hans Verkuil23848b62008-09-07 08:01:39 -0300526static int vpx3220_probe(struct i2c_client *client,
527 const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct vpx3220 *decoder;
Hans Verkuil23848b62008-09-07 08:01:39 -0300530 const char *name = NULL;
531 u8 ver;
532 u16 pn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 /* Check if the adapter supports the needed features */
Hans Verkuil23848b62008-09-07 08:01:39 -0300535 if (!i2c_check_functionality(client->adapter,
536 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
537 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Panagiotis Issaris74081872006-01-11 19:40:56 -0200539 decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
Hans Verkuil23848b62008-09-07 08:01:39 -0300540 if (decoder == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return -ENOMEM;
Hans Verkuil107063c2009-02-18 17:26:06 -0300542 decoder->norm = V4L2_STD_PAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 decoder->input = 0;
544 decoder->enable = 1;
545 decoder->bright = 32768;
546 decoder->contrast = 32768;
547 decoder->hue = 32768;
548 decoder->sat = 32768;
549 i2c_set_clientdata(client, decoder);
550
Hans Verkuil23848b62008-09-07 08:01:39 -0300551 ver = i2c_smbus_read_byte_data(client, 0x00);
552 pn = (i2c_smbus_read_byte_data(client, 0x02) << 8) +
553 i2c_smbus_read_byte_data(client, 0x01);
554 if (ver == 0xec) {
555 switch (pn) {
556 case 0x4680:
557 name = "vpx3220a";
558 break;
559 case 0x4260:
560 name = "vpx3216b";
561 break;
562 case 0x4280:
563 name = "vpx3214c";
564 break;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
Hans Verkuil23848b62008-09-07 08:01:39 -0300567 if (name)
568 v4l_info(client, "%s found @ 0x%x (%s)\n", name,
569 client->addr << 1, client->adapter->name);
570 else
571 v4l_info(client, "chip (%02x:%04x) found @ 0x%x (%s)\n",
572 ver, pn, client->addr << 1, client->adapter->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 vpx3220_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 0;
576}
577
Hans Verkuil23848b62008-09-07 08:01:39 -0300578static int vpx3220_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Hans Verkuil23848b62008-09-07 08:01:39 -0300580 kfree(i2c_get_clientdata(client));
581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Hans Verkuil23848b62008-09-07 08:01:39 -0300584static const struct i2c_device_id vpx3220_id[] = {
585 { "vpx3220a", 0 },
586 { "vpx3216b", 0 },
587 { "vpx3214c", 0 },
588 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589};
Hans Verkuil23848b62008-09-07 08:01:39 -0300590MODULE_DEVICE_TABLE(i2c, vpx3220_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Hans Verkuil23848b62008-09-07 08:01:39 -0300592static struct v4l2_i2c_driver_data v4l2_i2c_data = {
593 .name = "vpx3220",
594 .driverid = I2C_DRIVERID_VPX3220,
595 .command = vpx3220_command,
596 .probe = vpx3220_probe,
597 .remove = vpx3220_remove,
598 .id_table = vpx3220_id,
599};