V4L/DVB (10714): zoran et al: convert zoran i2c modules to V4L2.
[safe/jmp/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/videodev2.h>
31 #include <linux/video_decoder.h>
32
33 MODULE_DESCRIPTION("vpx3220a/vpx3216b/vpx3214c video decoder driver");
34 MODULE_AUTHOR("Laurent Pinchart");
35 MODULE_LICENSE("GPL");
36
37 static int debug;
38 module_param(debug, int, 0);
39 MODULE_PARM_DESC(debug, "Debug level (0-1)");
40
41 #define VPX_TIMEOUT_COUNT  10
42
43 /* ----------------------------------------------------------------------- */
44
45 struct vpx3220 {
46         unsigned char reg[255];
47
48         v4l2_std_id norm;
49         int input;
50         int enable;
51         int bright;
52         int contrast;
53         int hue;
54         int sat;
55 };
56
57 static char *inputs[] = { "internal", "composite", "svideo" };
58
59 /* ----------------------------------------------------------------------- */
60
61 static inline int vpx3220_write(struct i2c_client *client, u8 reg, u8 value)
62 {
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
69 static inline int vpx3220_read(struct i2c_client *client, u8 reg)
70 {
71         return i2c_smbus_read_byte_data(client, reg);
72 }
73
74 static int vpx3220_fp_status(struct i2c_client *client)
75 {
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
94 static int vpx3220_fp_write(struct i2c_client *client, u8 fpaddr, u16 data)
95 {
96         /* Write the 16-bit address to the FPWR register */
97         if (i2c_smbus_write_word_data(client, 0x27, swab16(fpaddr)) == -1) {
98                 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
99                 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) {
107                 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
108                 return -1;
109         }
110
111         return 0;
112 }
113
114 static u16 vpx3220_fp_read(struct i2c_client *client, u16 fpaddr)
115 {
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) {
120                 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
121                 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) {
130                 v4l_dbg(1, debug, client, "%s: failed\n", __func__);
131                 return -1;
132         }
133
134         return swab16(data);
135 }
136
137 static int vpx3220_write_block(struct i2c_client *client, const u8 *data, unsigned int len)
138 {
139         u8 reg;
140         int ret = -1;
141
142         while (len >= 2) {
143                 reg = *data++;
144                 ret = vpx3220_write(client, reg, *data++);
145                 if (ret < 0)
146                         break;
147                 len -= 2;
148         }
149
150         return ret;
151 }
152
153 static int vpx3220_write_fp_block(struct i2c_client *client,
154                 const u16 *data, unsigned int len)
155 {
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
170 static 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 */
179         0xf0, 0x73,             /* 13.5 MHz transport, Forced
180                                  * mode, latch windows */
181         0xf2, 0x13,             /* NTSC M, composite input */
182         0xe7, 0x1e1,            /* Enable vertical standard
183                                  * locking @ 240 lines */
184 };
185
186 static const unsigned short init_pal[] = {
187         0x88, 23,               /* Window 1 vertical begin */
188         0x89, 288,              /* Vertical lines in (16 lines
189                                  * skipped by the VFE) */
190         0x8a, 288,              /* Vertical lines out (16 lines
191                                  * 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 */
197         0xf0, 0x77,             /* 13.5 MHz transport, Forced
198                                  * mode, latch windows */
199         0xf2, 0x3d1,            /* PAL B,G,H,I, composite input */
200         0xe7, 0x241,            /* PAL/SECAM set to 288 lines */
201 };
202
203 static const unsigned short init_secam[] = {
204         0x88, 23,               /* Window 1 vertical begin */
205         0x89, 288,              /* Vertical lines in (16 lines
206                                  * skipped by the VFE) */
207         0x8a, 288,              /* Vertical lines out (16 lines
208                                  * 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 */
214         0xf0, 0x77,             /* 13.5 MHz transport, Forced
215                                  * mode, latch windows */
216         0xf2, 0x3d5,            /* SECAM, composite input */
217         0xe7, 0x241,            /* PAL/SECAM set to 288 lines */
218 };
219
220 static 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
250 static 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
263
264 static int vpx3220_command(struct i2c_client *client, unsigned cmd, void *arg)
265 {
266         struct vpx3220 *decoder = i2c_get_clientdata(client);
267
268         switch (cmd) {
269         case VIDIOC_INT_INIT:
270         {
271                 vpx3220_write_block(client, init_common,
272                                     sizeof(init_common));
273                 vpx3220_write_fp_block(client, init_fp,
274                                        sizeof(init_fp) >> 1);
275                 if (decoder->norm & V4L2_STD_NTSC) {
276                         vpx3220_write_fp_block(client, init_ntsc,
277                                                sizeof(init_ntsc) >> 1);
278                 } else if (decoder->norm & V4L2_STD_PAL) {
279                         vpx3220_write_fp_block(client, init_pal,
280                                                sizeof(init_pal) >> 1);
281                 } else if (decoder->norm & V4L2_STD_SECAM) {
282                         vpx3220_write_fp_block(client, init_secam,
283                                                sizeof(init_secam) >> 1);
284                 } else {
285                         vpx3220_write_fp_block(client, init_pal,
286                                                sizeof(init_pal) >> 1);
287                 }
288                 break;
289         }
290
291         case VIDIOC_QUERYSTD:
292         case VIDIOC_INT_G_INPUT_STATUS:
293         {
294                 int res = V4L2_IN_ST_NO_SIGNAL, status;
295                 v4l2_std_id std = 0;
296
297                 v4l_dbg(1, debug, client, "VIDIOC_QUERYSTD/VIDIOC_INT_G_INPUT_STATUS\n");
298
299                 status = vpx3220_fp_read(client, 0x0f3);
300
301                 v4l_dbg(1, debug, client, "status: 0x%04x\n", status);
302
303                 if (status < 0)
304                         return status;
305
306                 if ((status & 0x20) == 0) {
307                         res = 0;
308
309                         switch (status & 0x18) {
310                         case 0x00:
311                         case 0x10:
312                         case 0x14:
313                         case 0x18:
314                                 std = V4L2_STD_PAL;
315                                 break;
316
317                         case 0x08:
318                                 std = V4L2_STD_SECAM;
319                                 break;
320
321                         case 0x04:
322                         case 0x0c:
323                         case 0x1c:
324                                 std = V4L2_STD_NTSC;
325                                 break;
326                         }
327                 }
328
329                 if (cmd == VIDIOC_QUERYSTD)
330                         *(v4l2_std_id *)arg = std;
331                 else
332                         *(int *)arg = res;
333                 break;
334         }
335
336         case VIDIOC_S_STD:
337         {
338                 v4l2_std_id *iarg = arg;
339                 int temp_input;
340
341                 /* Here we back up the input selection because it gets
342                    overwritten when we fill the registers with the
343                    choosen video norm */
344                 temp_input = vpx3220_fp_read(client, 0xf2);
345
346                 v4l_dbg(1, debug, client, "VIDIOC_S_STD %llx\n", *iarg);
347                 if (*iarg & V4L2_STD_NTSC) {
348                         vpx3220_write_fp_block(client, init_ntsc,
349                                                sizeof(init_ntsc) >> 1);
350                         v4l_dbg(1, debug, client, "norm switched to NTSC\n");
351                 } else if (*iarg & V4L2_STD_PAL) {
352                         vpx3220_write_fp_block(client, init_pal,
353                                                sizeof(init_pal) >> 1);
354                         v4l_dbg(1, debug, client, "norm switched to PAL\n");
355                 } else if (*iarg & V4L2_STD_SECAM) {
356                         vpx3220_write_fp_block(client, init_secam,
357                                                sizeof(init_secam) >> 1);
358                         v4l_dbg(1, debug, client, "norm switched to SECAM\n");
359                 } else {
360                         return -EINVAL;
361                 }
362
363                 decoder->norm = *iarg;
364
365                 /* And here we set the backed up video input again */
366                 vpx3220_fp_write(client, 0xf2, temp_input | 0x0010);
367                 udelay(10);
368                 break;
369         }
370
371         case VIDIOC_INT_S_VIDEO_ROUTING:
372         {
373                 struct v4l2_routing *route = arg;
374                 int data;
375
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
386                 if (route->input < 0 || route->input > 2)
387                         return -EINVAL;
388
389                 v4l_dbg(1, debug, client, "input switched to %s\n", inputs[route->input]);
390
391                 vpx3220_write(client, 0x33, input[route->input][0]);
392
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,
398                                  data | (input[route->input][1] << 5) | 0x0010);
399
400                 udelay(10);
401                 break;
402         }
403
404         case VIDIOC_STREAMON:
405         case VIDIOC_STREAMOFF:
406         {
407                 int on = cmd == VIDIOC_STREAMON;
408                 v4l_dbg(1, debug, client, "VIDIOC_STREAM%s\n", on ? "ON" : "OFF");
409
410                 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:
436                         return -EINVAL;
437                 }
438                 break;
439         }
440
441         case VIDIOC_G_CTRL:
442         {
443                 struct v4l2_control *ctrl = arg;
444
445                 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                 }
461                 break;
462         }
463
464         case VIDIOC_S_CTRL:
465         {
466                 struct v4l2_control *ctrl = arg;
467
468                 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;
497                 }
498                 break;
499         }
500
501         default:
502                 return -EINVAL;
503         }
504
505         return 0;
506 }
507
508 static int vpx3220_init_client(struct i2c_client *client)
509 {
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 /* -----------------------------------------------------------------------
519  * Client management code
520  */
521
522 static unsigned short normal_i2c[] = { 0x86 >> 1, 0x8e >> 1, I2C_CLIENT_END };
523
524 I2C_CLIENT_INSMOD;
525
526 static int vpx3220_probe(struct i2c_client *client,
527                         const struct i2c_device_id *id)
528 {
529         struct vpx3220 *decoder;
530         const char *name = NULL;
531         u8 ver;
532         u16 pn;
533
534         /* Check if the adapter supports the needed features */
535         if (!i2c_check_functionality(client->adapter,
536                 I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
537                 return -ENODEV;
538
539         decoder = kzalloc(sizeof(struct vpx3220), GFP_KERNEL);
540         if (decoder == NULL)
541                 return -ENOMEM;
542         decoder->norm = V4L2_STD_PAL;
543         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
551         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                 }
566         }
567         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);
573
574         vpx3220_init_client(client);
575         return 0;
576 }
577
578 static int vpx3220_remove(struct i2c_client *client)
579 {
580         kfree(i2c_get_clientdata(client));
581         return 0;
582 }
583
584 static const struct i2c_device_id vpx3220_id[] = {
585         { "vpx3220a", 0 },
586         { "vpx3216b", 0 },
587         { "vpx3214c", 0 },
588         { }
589 };
590 MODULE_DEVICE_TABLE(i2c, vpx3220_id);
591
592 static 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 };