]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/saa7110.c
V4L/DVB (10724): saa7110: convert to v4l2_subdev.
[linux-2.6-omap-h63xx.git] / drivers / media / video / saa7110.c
1 /*
2  * saa7110 - Philips SAA7110(A) video decoder driver
3  *
4  * Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl>
5  *
6  * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
7  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
8  *    - some corrections for Pinnacle Systems Inc. DC10plus card.
9  *
10  * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
11  *    - moved over to linux>=2.4.x i2c protocol (1/1/2003)
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/wait.h>
34 #include <asm/uaccess.h>
35 #include <linux/i2c.h>
36 #include <linux/videodev2.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-chip-ident.h>
39 #include <media/v4l2-i2c-drv-legacy.h>
40
41 MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
42 MODULE_AUTHOR("Pauline Middelink");
43 MODULE_LICENSE("GPL");
44
45 static unsigned short normal_i2c[] = { 0x9c >> 1, 0x9e >> 1, I2C_CLIENT_END };
46
47 I2C_CLIENT_INSMOD;
48
49 static int debug;
50 module_param(debug, int, 0);
51 MODULE_PARM_DESC(debug, "Debug level (0-1)");
52
53 #define SAA7110_MAX_INPUT       9       /* 6 CVBS, 3 SVHS */
54 #define SAA7110_MAX_OUTPUT      1       /* 1 YUV */
55
56 #define SAA7110_NR_REG          0x35
57
58 struct saa7110 {
59         struct v4l2_subdev sd;
60         u8 reg[SAA7110_NR_REG];
61
62         v4l2_std_id norm;
63         int input;
64         int enable;
65         int bright;
66         int contrast;
67         int hue;
68         int sat;
69
70         wait_queue_head_t wq;
71 };
72
73 static inline struct saa7110 *to_saa7110(struct v4l2_subdev *sd)
74 {
75         return container_of(sd, struct saa7110, sd);
76 }
77
78 /* ----------------------------------------------------------------------- */
79 /* I2C support functions                                                   */
80 /* ----------------------------------------------------------------------- */
81
82 static int saa7110_write(struct v4l2_subdev *sd, u8 reg, u8 value)
83 {
84         struct i2c_client *client = v4l2_get_subdevdata(sd);
85         struct saa7110 *decoder = to_saa7110(sd);
86
87         decoder->reg[reg] = value;
88         return i2c_smbus_write_byte_data(client, reg, value);
89 }
90
91 static int saa7110_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
92 {
93         struct i2c_client *client = v4l2_get_subdevdata(sd);
94         struct saa7110 *decoder = to_saa7110(sd);
95         int ret = -1;
96         u8 reg = *data;         /* first register to write to */
97
98         /* Sanity check */
99         if (reg + (len - 1) > SAA7110_NR_REG)
100                 return ret;
101
102         /* the saa7110 has an autoincrement function, use it if
103          * the adapter understands raw I2C */
104         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
105                 ret = i2c_master_send(client, data, len);
106
107                 /* Cache the written data */
108                 memcpy(decoder->reg + reg, data + 1, len - 1);
109         } else {
110                 for (++data, --len; len; len--) {
111                         ret = saa7110_write(sd, reg++, *data++);
112                         if (ret < 0)
113                                 break;
114                 }
115         }
116
117         return ret;
118 }
119
120 static inline int saa7110_read(struct v4l2_subdev *sd)
121 {
122         struct i2c_client *client = v4l2_get_subdevdata(sd);
123
124         return i2c_smbus_read_byte(client);
125 }
126
127 /* ----------------------------------------------------------------------- */
128 /* SAA7110 functions                                                       */
129 /* ----------------------------------------------------------------------- */
130
131 #define FRESP_06H_COMPST 0x03   /*0x13*/
132 #define FRESP_06H_SVIDEO 0x83   /*0xC0*/
133
134
135 static int saa7110_selmux(struct v4l2_subdev *sd, int chan)
136 {
137         static const unsigned char modes[9][8] = {
138                 /* mode 0 */
139                 {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
140                               0x44, 0x75, 0x16},
141                 /* mode 1 */
142                 {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
143                               0x44, 0x75, 0x16},
144                 /* mode 2 */
145                 {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
146                               0x60, 0xB5, 0x05},
147                 /* mode 3 */
148                 {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
149                               0x60, 0xB5, 0x05},
150                 /* mode 4 */
151                 {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
152                               0x60, 0xB5, 0x03},
153                 /* mode 5 */
154                 {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
155                               0x60, 0xB5, 0x03},
156                 /* mode 6 */
157                 {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
158                               0x44, 0x75, 0x12},
159                 /* mode 7 */
160                 {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
161                               0x60, 0xB5, 0x14},
162                 /* mode 8 */
163                 {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
164                               0x44, 0x75, 0x21}
165         };
166         struct saa7110 *decoder = to_saa7110(sd);
167         const unsigned char *ptr = modes[chan];
168
169         saa7110_write(sd, 0x06, ptr[0]);        /* Luminance control    */
170         saa7110_write(sd, 0x20, ptr[1]);        /* Analog Control #1    */
171         saa7110_write(sd, 0x21, ptr[2]);        /* Analog Control #2    */
172         saa7110_write(sd, 0x22, ptr[3]);        /* Mixer Control #1     */
173         saa7110_write(sd, 0x2C, ptr[4]);        /* Mixer Control #2     */
174         saa7110_write(sd, 0x30, ptr[5]);        /* ADCs gain control    */
175         saa7110_write(sd, 0x31, ptr[6]);        /* Mixer Control #3     */
176         saa7110_write(sd, 0x21, ptr[7]);        /* Analog Control #2    */
177         decoder->input = chan;
178
179         return 0;
180 }
181
182 static const unsigned char initseq[1 + SAA7110_NR_REG] = {
183         0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
184         /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
185         /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
186         /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
187         /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
188         /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
189         /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
190 };
191
192 static v4l2_std_id determine_norm(struct v4l2_subdev *sd)
193 {
194         DEFINE_WAIT(wait);
195         struct saa7110 *decoder = to_saa7110(sd);
196         int status;
197
198         /* mode changed, start automatic detection */
199         saa7110_write_block(sd, initseq, sizeof(initseq));
200         saa7110_selmux(sd, decoder->input);
201         prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
202         schedule_timeout(msecs_to_jiffies(250));
203         finish_wait(&decoder->wq, &wait);
204         status = saa7110_read(sd);
205         if (status & 0x40) {
206                 v4l2_dbg(1, debug, sd, "status=0x%02x (no signal)\n", status);
207                 return decoder->norm;   /* no change*/
208         }
209         if ((status & 3) == 0) {
210                 saa7110_write(sd, 0x06, 0x83);
211                 if (status & 0x20) {
212                         v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC/no color)\n", status);
213                         /*saa7110_write(sd,0x2E,0x81);*/
214                         return V4L2_STD_NTSC;
215                 }
216                 v4l2_dbg(1, debug, sd, "status=0x%02x (PAL/no color)\n", status);
217                 /*saa7110_write(sd,0x2E,0x9A);*/
218                 return V4L2_STD_PAL;
219         }
220         /*saa7110_write(sd,0x06,0x03);*/
221         if (status & 0x20) {    /* 60Hz */
222                 v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC)\n", status);
223                 saa7110_write(sd, 0x0D, 0x86);
224                 saa7110_write(sd, 0x0F, 0x50);
225                 saa7110_write(sd, 0x11, 0x2C);
226                 /*saa7110_write(sd,0x2E,0x81);*/
227                 return V4L2_STD_NTSC;
228         }
229
230         /* 50Hz -> PAL/SECAM */
231         saa7110_write(sd, 0x0D, 0x86);
232         saa7110_write(sd, 0x0F, 0x10);
233         saa7110_write(sd, 0x11, 0x59);
234         /*saa7110_write(sd,0x2E,0x9A);*/
235
236         prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
237         schedule_timeout(msecs_to_jiffies(250));
238         finish_wait(&decoder->wq, &wait);
239
240         status = saa7110_read(sd);
241         if ((status & 0x03) == 0x01) {
242                 v4l2_dbg(1, debug, sd, "status=0x%02x (SECAM)\n", status);
243                 saa7110_write(sd, 0x0D, 0x87);
244                 return V4L2_STD_SECAM;
245         }
246         v4l2_dbg(1, debug, sd, "status=0x%02x (PAL)\n", status);
247         return V4L2_STD_PAL;
248 }
249
250 static int saa7110_g_input_status(struct v4l2_subdev *sd, u32 *pstatus)
251 {
252         struct saa7110 *decoder = to_saa7110(sd);
253         int res = V4L2_IN_ST_NO_SIGNAL;
254         int status = saa7110_read(sd);
255
256         v4l2_dbg(1, debug, sd, "status=0x%02x norm=%llx\n",
257                        status, decoder->norm);
258         if (!(status & 0x40))
259                 res = 0;
260         if (!(status & 0x03))
261                 res |= V4L2_IN_ST_NO_COLOR;
262
263         *pstatus = res;
264         return 0;
265 }
266
267 static int saa7110_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
268 {
269         *(v4l2_std_id *)std = determine_norm(sd);
270         return 0;
271 }
272
273 static int saa7110_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
274 {
275         struct saa7110 *decoder = to_saa7110(sd);
276
277         if (decoder->norm != std) {
278                 decoder->norm = std;
279                 /*saa7110_write(sd, 0x06, 0x03);*/
280                 if (std & V4L2_STD_NTSC) {
281                         saa7110_write(sd, 0x0D, 0x86);
282                         saa7110_write(sd, 0x0F, 0x50);
283                         saa7110_write(sd, 0x11, 0x2C);
284                         /*saa7110_write(sd, 0x2E, 0x81);*/
285                         v4l2_dbg(1, debug, sd, "switched to NTSC\n");
286                 } else if (std & V4L2_STD_PAL) {
287                         saa7110_write(sd, 0x0D, 0x86);
288                         saa7110_write(sd, 0x0F, 0x10);
289                         saa7110_write(sd, 0x11, 0x59);
290                         /*saa7110_write(sd, 0x2E, 0x9A);*/
291                         v4l2_dbg(1, debug, sd, "switched to PAL\n");
292                 } else if (std & V4L2_STD_SECAM) {
293                         saa7110_write(sd, 0x0D, 0x87);
294                         saa7110_write(sd, 0x0F, 0x10);
295                         saa7110_write(sd, 0x11, 0x59);
296                         /*saa7110_write(sd, 0x2E, 0x9A);*/
297                         v4l2_dbg(1, debug, sd, "switched to SECAM\n");
298                 } else {
299                         return -EINVAL;
300                 }
301         }
302         return 0;
303 }
304
305 static int saa7110_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
306 {
307         struct saa7110 *decoder = to_saa7110(sd);
308
309         if (route->input < 0 || route->input >= SAA7110_MAX_INPUT) {
310                 v4l2_dbg(1, debug, sd, "input=%d not available\n", route->input);
311                 return -EINVAL;
312         }
313         if (decoder->input != route->input) {
314                 saa7110_selmux(sd, route->input);
315                 v4l2_dbg(1, debug, sd, "switched to input=%d\n", route->input);
316         }
317         return 0;
318 }
319
320 static int saa7110_s_stream(struct v4l2_subdev *sd, int enable)
321 {
322         struct saa7110 *decoder = to_saa7110(sd);
323
324         if (decoder->enable != enable) {
325                 decoder->enable = enable;
326                 saa7110_write(sd, 0x0E, enable ? 0x18 : 0x80);
327                 v4l2_dbg(1, debug, sd, "YUV %s\n", enable ? "on" : "off");
328         }
329         return 0;
330 }
331
332 static int saa7110_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
333 {
334         switch (qc->id) {
335         case V4L2_CID_BRIGHTNESS:
336                 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
337         case V4L2_CID_CONTRAST:
338         case V4L2_CID_SATURATION:
339                 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
340         case V4L2_CID_HUE:
341                 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
342         default:
343                 return -EINVAL;
344         }
345         return 0;
346 }
347
348 static int saa7110_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
349 {
350         struct saa7110 *decoder = to_saa7110(sd);
351
352         switch (ctrl->id) {
353         case V4L2_CID_BRIGHTNESS:
354                 ctrl->value = decoder->bright;
355                 break;
356         case V4L2_CID_CONTRAST:
357                 ctrl->value = decoder->contrast;
358                 break;
359         case V4L2_CID_SATURATION:
360                 ctrl->value = decoder->sat;
361                 break;
362         case V4L2_CID_HUE:
363                 ctrl->value = decoder->hue;
364                 break;
365         default:
366                 return -EINVAL;
367         }
368         return 0;
369 }
370
371 static int saa7110_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
372 {
373         struct saa7110 *decoder = to_saa7110(sd);
374
375         switch (ctrl->id) {
376         case V4L2_CID_BRIGHTNESS:
377                 if (decoder->bright != ctrl->value) {
378                         decoder->bright = ctrl->value;
379                         saa7110_write(sd, 0x19, decoder->bright);
380                 }
381                 break;
382         case V4L2_CID_CONTRAST:
383                 if (decoder->contrast != ctrl->value) {
384                         decoder->contrast = ctrl->value;
385                         saa7110_write(sd, 0x13, decoder->contrast);
386                 }
387                 break;
388         case V4L2_CID_SATURATION:
389                 if (decoder->sat != ctrl->value) {
390                         decoder->sat = ctrl->value;
391                         saa7110_write(sd, 0x12, decoder->sat);
392                 }
393                 break;
394         case V4L2_CID_HUE:
395                 if (decoder->hue != ctrl->value) {
396                         decoder->hue = ctrl->value;
397                         saa7110_write(sd, 0x07, decoder->hue);
398                 }
399                 break;
400         default:
401                 return -EINVAL;
402         }
403         return 0;
404 }
405
406 static int saa7110_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
407 {
408         struct i2c_client *client = v4l2_get_subdevdata(sd);
409
410         return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7110, 0);
411 }
412
413 static int saa7110_command(struct i2c_client *client, unsigned cmd, void *arg)
414 {
415         return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
416 }
417
418 /* ----------------------------------------------------------------------- */
419
420 static const struct v4l2_subdev_core_ops saa7110_core_ops = {
421         .g_chip_ident = saa7110_g_chip_ident,
422         .g_ctrl = saa7110_g_ctrl,
423         .s_ctrl = saa7110_s_ctrl,
424         .queryctrl = saa7110_queryctrl,
425 };
426
427 static const struct v4l2_subdev_tuner_ops saa7110_tuner_ops = {
428         .s_std = saa7110_s_std,
429 };
430
431 static const struct v4l2_subdev_video_ops saa7110_video_ops = {
432         .s_routing = saa7110_s_routing,
433         .s_stream = saa7110_s_stream,
434         .querystd = saa7110_querystd,
435         .g_input_status = saa7110_g_input_status,
436 };
437
438 static const struct v4l2_subdev_ops saa7110_ops = {
439         .core = &saa7110_core_ops,
440         .tuner = &saa7110_tuner_ops,
441         .video = &saa7110_video_ops,
442 };
443
444 /* ----------------------------------------------------------------------- */
445
446 static int saa7110_probe(struct i2c_client *client,
447                         const struct i2c_device_id *id)
448 {
449         struct saa7110 *decoder;
450         struct v4l2_subdev *sd;
451         int rv;
452
453         /* Check if the adapter supports the needed features */
454         if (!i2c_check_functionality(client->adapter,
455                 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
456                 return -ENODEV;
457
458         v4l_info(client, "chip found @ 0x%x (%s)\n",
459                         client->addr << 1, client->adapter->name);
460
461         decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
462         if (!decoder)
463                 return -ENOMEM;
464         sd = &decoder->sd;
465         v4l2_i2c_subdev_init(sd, client, &saa7110_ops);
466         decoder->norm = V4L2_STD_PAL;
467         decoder->input = 0;
468         decoder->enable = 1;
469         decoder->bright = 32768;
470         decoder->contrast = 32768;
471         decoder->hue = 32768;
472         decoder->sat = 32768;
473         init_waitqueue_head(&decoder->wq);
474
475         rv = saa7110_write_block(sd, initseq, sizeof(initseq));
476         if (rv < 0) {
477                 v4l2_dbg(1, debug, sd, "init status %d\n", rv);
478         } else {
479                 int ver, status;
480                 saa7110_write(sd, 0x21, 0x10);
481                 saa7110_write(sd, 0x0e, 0x18);
482                 saa7110_write(sd, 0x0D, 0x04);
483                 ver = saa7110_read(sd);
484                 saa7110_write(sd, 0x0D, 0x06);
485                 /*mdelay(150);*/
486                 status = saa7110_read(sd);
487                 v4l2_dbg(1, debug, sd, "version %x, status=0x%02x\n",
488                                ver, status);
489                 saa7110_write(sd, 0x0D, 0x86);
490                 saa7110_write(sd, 0x0F, 0x10);
491                 saa7110_write(sd, 0x11, 0x59);
492                 /*saa7110_write(sd, 0x2E, 0x9A);*/
493         }
494
495         /*saa7110_selmux(sd,0);*/
496         /*determine_norm(sd);*/
497         /* setup and implicit mode 0 select has been performed */
498
499         return 0;
500 }
501
502 static int saa7110_remove(struct i2c_client *client)
503 {
504         struct v4l2_subdev *sd = i2c_get_clientdata(client);
505
506         v4l2_device_unregister_subdev(sd);
507         kfree(to_saa7110(sd));
508         return 0;
509 }
510
511 /* ----------------------------------------------------------------------- */
512
513 static const struct i2c_device_id saa7110_id[] = {
514         { "saa7110", 0 },
515         { }
516 };
517 MODULE_DEVICE_TABLE(i2c, saa7110_id);
518
519 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
520         .name = "saa7110",
521         .driverid = I2C_DRIVERID_SAA7110,
522         .command = saa7110_command,
523         .probe = saa7110_probe,
524         .remove = saa7110_remove,
525         .id_table = saa7110_id,
526 };