V4L/DVB (10189): dm1105: Fix build with INPUT=m and DVB_DM1105=y
[deliverable/linux.git] / drivers / media / video / vpx3220.c
CommitLineData
d56410e0 1/*
1da177e4
LT
2 * 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>
1da177e4 25#include <asm/uaccess.h>
1da177e4 26#include <linux/i2c.h>
5e87efa3 27#include <media/v4l2-common.h>
23848b65
HV
28#include <media/v4l2-i2c-drv-legacy.h>
29#include <linux/videodev.h>
1da177e4
LT
30#include <linux/video_decoder.h>
31
23848b65
HV
32MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
33MODULE_AUTHOR("Laurent Pinchart");
34MODULE_LICENSE("GPL");
1da177e4 35
ff699e6b 36static int debug;
1da177e4
LT
37module_param(debug, int, 0);
38MODULE_PARM_DESC(debug, "Debug level (0-1)");
39
1da177e4
LT
40#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/* ----------------------------------------------------------------------- */
23848b65
HV
59
60static inline int vpx3220_write(struct i2c_client *client, u8 reg, u8 value)
1da177e4
LT
61{
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
23848b65 68static inline int vpx3220_read(struct i2c_client *client, u8 reg)
1da177e4
LT
69{
70 return i2c_smbus_read_byte_data(client, reg);
71}
72
23848b65 73static int vpx3220_fp_status(struct i2c_client *client)
1da177e4
LT
74{
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
23848b65 93static int vpx3220_fp_write(struct i2c_client *client, u8 fpaddr, u16 data)
1da177e4
LT
94{
95 /* Write the 16-bit address to the FPWR register */
96 if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
23848b65 97 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
1da177e4
LT
98 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) {
23848b65 106 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
1da177e4
LT
107 return -1;
108 }
109
110 return 0;
111}
112
23848b65 113static u16 vpx3220_fp_read(struct i2c_client *client, u16 fpaddr)
1da177e4
LT
114{
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) {
23848b65 119 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
1da177e4
LT
120 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) {
23848b65 129 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
1da177e4
LT
130 return -1;
131 }
132
133 return swab16(data);
134}
135
23848b65 136static int vpx3220_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
1da177e4
LT
137{
138 u8 reg;
139 int ret = -1;
140
141 while (len >= 2) {
142 reg = *data++;
23848b65
HV
143 ret = vpx3220_write(client, reg, *data++);
144 if (ret < 0)
1da177e4
LT
145 break;
146 len -= 2;
147 }
148
149 return ret;
150}
151
23848b65
HV
152static int vpx3220_write_fp_block(struct i2c_client *client,
153 const u16 *data, unsigned int len)
1da177e4
LT
154{
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 */
9b3acc21 178 0xf0, 0x73, /* 13.5 MHz transport, Forced
1da177e4
LT
179 * 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 */
9b3acc21 187 0x89, 288, /* Vertical lines in (16 lines
1da177e4 188 * skipped by the VFE) */
9b3acc21 189 0x8a, 288, /* Vertical lines out (16 lines
1da177e4
LT
190 * 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 */
9b3acc21 196 0xf0, 0x77, /* 13.5 MHz transport, Forced
1da177e4
LT
197 * mode, latch windows */
198 0xf2, 0x3d1, /* PAL B,G,H,I, composite input */
9b3acc21 199 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
1da177e4
LT
200};
201
202static const unsigned short init_secam[] = {
9b3acc21
RB
203 0x88, 23, /* Window 1 vertical begin */
204 0x89, 288, /* Vertical lines in (16 lines
1da177e4 205 * skipped by the VFE) */
9b3acc21 206 0x8a, 288, /* Vertical lines out (16 lines
1da177e4
LT
207 * 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 */
9b3acc21 213 0xf0, 0x77, /* 13.5 MHz transport, Forced
1da177e4
LT
214 * mode, latch windows */
215 0xf2, 0x3d5, /* SECAM, composite input */
9b3acc21 216 0xe7, 0x241, /* PAL/SECAM set to 288 lines */
1da177e4
LT
217};
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
23848b65 262static void vpx3220_dump_i2c(struct i2c_client *client)
1da177e4
LT
263{
264 int len = sizeof(init_common);
265 const unsigned char *data = init_common;
266
267 while (len > 1) {
23848b65 268 v4l_dbg(1, debug, client, "i2c reg 0x%02x data 0x%02x\n",
1da177e4
LT
269 *data, vpx3220_read(client, *data));
270 data += 2;
271 len -= 2;
272 }
273}
274
23848b65 275static int vpx3220_command(struct i2c_client *client, unsigned cmd, void *arg)
1da177e4
LT
276{
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) {
1da177e4
LT
287 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,
d56410e0 294 sizeof(init_pal) >> 1);
1da177e4
LT
295 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,
d56410e0 302 sizeof(init_pal) >> 1);
1da177e4
LT
303 break;
304 }
1da177e4 305 break;
23848b65 306 }
1da177e4
LT
307
308 case DECODER_DUMP:
309 {
310 vpx3220_dump_i2c(client);
1da177e4 311 break;
23848b65 312 }
1da177e4
LT
313
314 case DECODER_GET_CAPABILITIES:
315 {
316 struct video_decoder_capability *cap = arg;
317
23848b65 318 v4l_dbg(1, debug, client, "DECODER_GET_CAPABILITIES\n");
1da177e4
LT
319
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;
1da177e4 327 break;
23848b65 328 }
1da177e4
LT
329
330 case DECODER_GET_STATUS:
331 {
332 int res = 0, status;
333
23848b65 334 v4l_dbg(1, debug, client, "DECODER_GET_STATUS\n");
1da177e4
LT
335
336 status = vpx3220_fp_read(client, 0x0f3);
337
23848b65 338 v4l_dbg(1, debug, client, "status: 0x%04x\n", status);
1da177e4
LT
339
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) {
1da177e4
LT
347 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;
1da177e4 367 break;
23848b65 368 }
1da177e4
LT
369
370 case DECODER_SET_NORM:
371 {
372 int *iarg = arg, data;
de21eb63
RB
373 int temp_input;
374
375 /* Here we back up the input selection because it gets
376 overwritten when we fill the registers with the
d56410e0 377 choosen video norm */
de21eb63 378 temp_input = vpx3220_fp_read(client, 0xf2);
1da177e4 379
23848b65 380 v4l_dbg(1, debug, client, "DECODER_SET_NORM %d\n", *iarg);
1da177e4 381 switch (*iarg) {
1da177e4
LT
382 case VIDEO_MODE_NTSC:
383 vpx3220_write_fp_block(client, init_ntsc,
384 sizeof(init_ntsc) >> 1);
23848b65 385 v4l_dbg(1, debug, client, "norm switched to NTSC\n");
1da177e4
LT
386 break;
387
388 case VIDEO_MODE_PAL:
389 vpx3220_write_fp_block(client, init_pal,
390 sizeof(init_pal) >> 1);
23848b65 391 v4l_dbg(1, debug, client, "norm switched to PAL\n");
1da177e4
LT
392 break;
393
394 case VIDEO_MODE_SECAM:
395 vpx3220_write_fp_block(client, init_secam,
396 sizeof(init_secam) >> 1);
23848b65 397 v4l_dbg(1, debug, client, "norm switched to SECAM\n");
1da177e4
LT
398 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);
23848b65 404 v4l_dbg(1, debug, client, "norm switched to AUTO\n");
1da177e4
LT
405 break;
406
407 default:
408 return -EINVAL;
1da177e4
LT
409 }
410 decoder->norm = *iarg;
de21eb63
RB
411
412 /* And here we set the backed up video input again */
413 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
414 udelay(10);
1da177e4 415 break;
23848b65 416 }
1da177e4
LT
417
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
23848b65 435 v4l_dbg(1, debug, client, "input switched to %s\n", inputs[*iarg]);
1da177e4
LT
436
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);
1da177e4 447 break;
23848b65 448 }
1da177e4
LT
449
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 }
1da177e4 458 break;
23848b65 459 }
1da177e4
LT
460
461 case DECODER_ENABLE_OUTPUT:
462 {
463 int *iarg = arg;
464
23848b65 465 v4l_dbg(1, debug, client, "DECODER_ENABLE_OUTPUT %d\n", *iarg);
1da177e4
LT
466
467 vpx3220_write(client, 0xf2, (*iarg ? 0x1b : 0x00));
1da177e4 468 break;
23848b65 469 }
1da177e4
LT
470
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 }
1da177e4 500 break;
23848b65 501 }
1da177e4
LT
502
503 default:
504 return -EINVAL;
505 }
506
507 return 0;
508}
509
23848b65 510static int vpx3220_init_client(struct i2c_client *client)
1da177e4
LT
511{
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/* -----------------------------------------------------------------------
c84e6036 521 * Client management code
1da177e4
LT
522 */
523
23848b65 524static unsigned short normal_i2c[] = { 0x86 >> 1, 0x8e >> 1, I2C_CLIENT_END };
d56410e0 525
23848b65 526I2C_CLIENT_INSMOD;
1da177e4 527
23848b65
HV
528static int vpx3220_probe(struct i2c_client *client,
529 const struct i2c_device_id *id)
1da177e4 530{
1da177e4 531 struct vpx3220 *decoder;
23848b65
HV
532 const char *name = NULL;
533 u8 ver;
534 u16 pn;
1da177e4
LT
535
536 /* Check if the adapter supports the needed features */
23848b65
HV
537 if (!i2c_check_functionality(client->adapter,
538 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
539 return -ENODEV;
1da177e4 540
7408187d 541 decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
23848b65 542 if (decoder == NULL)
1da177e4 543 return -ENOMEM;
1da177e4
LT
544 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
23848b65
HV
553 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 }
1da177e4 568 }
23848b65
HV
569 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);
1da177e4
LT
575
576 vpx3220_init_client(client);
1da177e4
LT
577 return 0;
578}
579
23848b65 580static int vpx3220_remove(struct i2c_client *client)
1da177e4 581{
23848b65
HV
582 kfree(i2c_get_clientdata(client));
583 return 0;
1da177e4
LT
584}
585
23848b65
HV
586static const struct i2c_device_id vpx3220_id[] = {
587 { "vpx3220a", 0 },
588 { "vpx3216b", 0 },
589 { "vpx3214c", 0 },
590 { }
591};
592MODULE_DEVICE_TABLE(i2c, vpx3220_id);
1da177e4 593
23848b65
HV
594static struct v4l2_i2c_driver_data v4l2_i2c_data = {
595 .name = "vpx3220",
596 .driverid = I2C_DRIVERID_VPX3220,
1da177e4 597 .command = vpx3220_command,
23848b65
HV
598 .probe = vpx3220_probe,
599 .remove = vpx3220_remove,
600 .id_table = vpx3220_id,
1da177e4 601};
This page took 0.501167 seconds and 5 git commands to generate.