V4L/DVB (10231): v4l2-subdev: add v4l2_ext_controls support
[linux-2.6] / drivers / media / video / vpx3220.c
1 /*
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>
25 #include <asm/uaccess.h>
26 #include <linux/i2c.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-i2c-drv-legacy.h>
29 #include <linux/videodev.h>
30 #include <linux/video_decoder.h>
31
32 MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
33 MODULE_AUTHOR("Laurent Pinchart");
34 MODULE_LICENSE("GPL");
35
36 static int debug;
37 module_param(debug, int, 0);
38 MODULE_PARM_DESC(debug, "Debug level (0-1)");
39
40 #define VPX_TIMEOUT_COUNT  10
41
42 /* ----------------------------------------------------------------------- */
43
44 struct 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
56 static char *inputs[] = { "internal", "composite", "svideo" };
57
58 /* ----------------------------------------------------------------------- */
59
60 static inline int vpx3220_write(struct i2c_client *client, u8 reg, u8 value)
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
68 static inline int vpx3220_read(struct i2c_client *client, u8 reg)
69 {
70         return i2c_smbus_read_byte_data(client, reg);
71 }
72
73 static int vpx3220_fp_status(struct i2c_client *client)
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
93 static int vpx3220_fp_write(struct i2c_client *client, u8 fpaddr, u16 data)
94 {
95         /* Write the 16-bit address to the FPWR register */
96         if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
97                 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
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) {
106                 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
107                 return -1;
108         }
109
110         return 0;
111 }
112
113 static u16 vpx3220_fp_read(struct i2c_client *client, u16 fpaddr)
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) {
119                 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
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) {
129                 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
130                 return -1;
131         }
132
133         return swab16(data);
134 }
135
136 static int vpx3220_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
137 {
138         u8 reg;
139         int ret = -1;
140
141         while (len >= 2) {
142                 reg = *data++;
143                 ret = vpx3220_write(client, reg, *data++);
144                 if (ret < 0)
145                         break;
146                 len -= 2;
147         }
148
149         return ret;
150 }
151
152 static int vpx3220_write_fp_block(struct i2c_client *client,
153                 const u16 *data, unsigned int len)
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
169 static 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 */
178         0xf0, 0x73,             /* 13.5 MHz transport, Forced
179                                  * mode, latch windows */
180         0xf2, 0x13,             /* NTSC M, composite input */
181         0xe7, 0x1e1,            /* Enable vertical standard
182                                  * locking @ 240 lines */
183 };
184
185 static const unsigned short init_pal[] = {
186         0x88, 23,               /* Window 1 vertical begin */
187         0x89, 288,              /* Vertical lines in (16 lines
188                                  * skipped by the VFE) */
189         0x8a, 288,              /* Vertical lines out (16 lines
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 */
196         0xf0, 0x77,             /* 13.5 MHz transport, Forced
197                                  * mode, latch windows */
198         0xf2, 0x3d1,            /* PAL B,G,H,I, composite input */
199         0xe7, 0x241,            /* PAL/SECAM set to 288 lines */
200 };
201
202 static const unsigned short init_secam[] = {
203         0x88, 23,               /* Window 1 vertical begin */
204         0x89, 288,              /* Vertical lines in (16 lines
205                                  * skipped by the VFE) */
206         0x8a, 288,              /* Vertical lines out (16 lines
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 */
213         0xf0, 0x77,             /* 13.5 MHz transport, Forced
214                                  * mode, latch windows */
215         0xf2, 0x3d5,            /* SECAM, composite input */
216         0xe7, 0x241,            /* PAL/SECAM set to 288 lines */
217 };
218
219 static 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
249 static 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
262 static void vpx3220_dump_i2c(struct i2c_client *client)
263 {
264         int len = sizeof(init_common);
265         const unsigned char *data = init_common;
266
267         while (len > 1) {
268                 v4l_dbg(1, debug, client, "i2c reg 0x%02x data 0x%02x\n",
269                         *data, vpx3220_read(client, *data));
270                 data += 2;
271                 len -= 2;
272         }
273 }
274
275 static int vpx3220_command(struct i2c_client *client, unsigned cmd, void *arg)
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) {
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,
294                                                sizeof(init_pal) >> 1);
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,
302                                                sizeof(init_pal) >> 1);
303                         break;
304                 }
305                 break;
306         }
307
308         case DECODER_DUMP:
309         {
310                 vpx3220_dump_i2c(client);
311                 break;
312         }
313
314         case DECODER_GET_CAPABILITIES:
315         {
316                 struct video_decoder_capability *cap = arg;
317
318                 v4l_dbg(1, debug, client, "DECODER_GET_CAPABILITIES\n");
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;
327                 break;
328         }
329
330         case DECODER_GET_STATUS:
331         {
332                 int res = 0, status;
333
334                 v4l_dbg(1, debug, client, "DECODER_GET_STATUS\n");
335
336                 status = vpx3220_fp_read(client, 0x0f3);
337
338                 v4l_dbg(1, debug, client, "status: 0x%04x\n", status);
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) {
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;
367                 break;
368         }
369
370         case DECODER_SET_NORM:
371         {
372                 int *iarg = arg, data;
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
377                    choosen video norm */
378                 temp_input = vpx3220_fp_read(client, 0xf2);
379
380                 v4l_dbg(1, debug, client, "DECODER_SET_NORM %d\n", *iarg);
381                 switch (*iarg) {
382                 case VIDEO_MODE_NTSC:
383                         vpx3220_write_fp_block(client, init_ntsc,
384                                                sizeof(init_ntsc) >> 1);
385                         v4l_dbg(1, debug, client, "norm switched to NTSC\n");
386                         break;
387
388                 case VIDEO_MODE_PAL:
389                         vpx3220_write_fp_block(client, init_pal,
390                                                sizeof(init_pal) >> 1);
391                         v4l_dbg(1, debug, client, "norm switched to PAL\n");
392                         break;
393
394                 case VIDEO_MODE_SECAM:
395                         vpx3220_write_fp_block(client, init_secam,
396                                                sizeof(init_secam) >> 1);
397                         v4l_dbg(1, debug, client, "norm switched to SECAM\n");
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);
404                         v4l_dbg(1, debug, client, "norm switched to AUTO\n");
405                         break;
406
407                 default:
408                         return -EINVAL;
409                 }
410                 decoder->norm = *iarg;
411
412                 /* And here we set the backed up video input again */
413                 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
414                 udelay(10);
415                 break;
416         }
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
435                 v4l_dbg(1, debug, client, "input switched to %s\n", inputs[*iarg]);
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);
447                 break;
448         }
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                 }
458                 break;
459         }
460
461         case DECODER_ENABLE_OUTPUT:
462         {
463                 int *iarg = arg;
464
465                 v4l_dbg(1, debug, client, "DECODER_ENABLE_OUTPUT %d\n", *iarg);
466
467                 vpx3220_write(client, 0xf2, (*iarg ? 0x1b : 0x00));
468                 break;
469         }
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                 }
500                 break;
501         }
502
503         default:
504                 return -EINVAL;
505         }
506
507         return 0;
508 }
509
510 static int vpx3220_init_client(struct i2c_client *client)
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 /* -----------------------------------------------------------------------
521  * Client management code
522  */
523
524 static unsigned short normal_i2c[] = { 0x86 >> 1, 0x8e >> 1, I2C_CLIENT_END };
525
526 I2C_CLIENT_INSMOD;
527
528 static int vpx3220_probe(struct i2c_client *client,
529                         const struct i2c_device_id *id)
530 {
531         struct vpx3220 *decoder;
532         const char *name = NULL;
533         u8 ver;
534         u16 pn;
535
536         /* Check if the adapter supports the needed features */
537         if (!i2c_check_functionality(client->adapter,
538                 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
539                 return -ENODEV;
540
541         decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
542         if (decoder == NULL)
543                 return -ENOMEM;
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
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                 }
568         }
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);
575
576         vpx3220_init_client(client);
577         return 0;
578 }
579
580 static int vpx3220_remove(struct i2c_client *client)
581 {
582         kfree(i2c_get_clientdata(client));
583         return 0;
584 }
585
586 static const struct i2c_device_id vpx3220_id[] = {
587         { "vpx3220a", 0 },
588         { "vpx3216b", 0 },
589         { "vpx3214c", 0 },
590         { }
591 };
592 MODULE_DEVICE_TABLE(i2c, vpx3220_id);
593
594 static 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 };