2 * saa7110 - Philips SAA7110(A) video decoder driver
4 * Copyright (C) 1998 Pauline Middelink <middelin@polyware.nl>
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.
10 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
11 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
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.
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.
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.
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>
41 MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
42 MODULE_AUTHOR("Pauline Middelink");
43 MODULE_LICENSE("GPL");
45 static unsigned short normal_i2c[] = { 0x9c >> 1, 0x9e >> 1, I2C_CLIENT_END };
50 module_param(debug, int, 0);
51 MODULE_PARM_DESC(debug, "Debug level (0-1)");
53 #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
54 #define SAA7110_MAX_OUTPUT 1 /* 1 YUV */
56 #define SAA7110_NR_REG 0x35
59 struct v4l2_subdev sd;
60 u8 reg[SAA7110_NR_REG];
73 static inline struct saa7110 *to_saa7110(struct v4l2_subdev *sd)
75 return container_of(sd, struct saa7110, sd);
78 /* ----------------------------------------------------------------------- */
79 /* I2C support functions */
80 /* ----------------------------------------------------------------------- */
82 static int saa7110_write(struct v4l2_subdev *sd, u8 reg, u8 value)
84 struct i2c_client *client = v4l2_get_subdevdata(sd);
85 struct saa7110 *decoder = to_saa7110(sd);
87 decoder->reg[reg] = value;
88 return i2c_smbus_write_byte_data(client, reg, value);
91 static int saa7110_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
93 struct i2c_client *client = v4l2_get_subdevdata(sd);
94 struct saa7110 *decoder = to_saa7110(sd);
96 u8 reg = *data; /* first register to write to */
99 if (reg + (len - 1) > SAA7110_NR_REG)
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);
107 /* Cache the written data */
108 memcpy(decoder->reg + reg, data + 1, len - 1);
110 for (++data, --len; len; len--) {
111 ret = saa7110_write(sd, reg++, *data++);
120 static inline int saa7110_read(struct v4l2_subdev *sd)
122 struct i2c_client *client = v4l2_get_subdevdata(sd);
124 return i2c_smbus_read_byte(client);
127 /* ----------------------------------------------------------------------- */
128 /* SAA7110 functions */
129 /* ----------------------------------------------------------------------- */
131 #define FRESP_06H_COMPST 0x03 /*0x13*/
132 #define FRESP_06H_SVIDEO 0x83 /*0xC0*/
135 static int saa7110_selmux(struct v4l2_subdev *sd, int chan)
137 static const unsigned char modes[9][8] = {
139 {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
142 {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
145 {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
148 {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
151 {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
154 {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
157 {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
160 {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
163 {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
166 struct saa7110 *decoder = to_saa7110(sd);
167 const unsigned char *ptr = modes[chan];
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;
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
192 static v4l2_std_id determine_norm(struct v4l2_subdev *sd)
195 struct saa7110 *decoder = to_saa7110(sd);
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);
206 v4l2_dbg(1, debug, sd, "status=0x%02x (no signal)\n", status);
207 return decoder->norm; /* no change*/
209 if ((status & 3) == 0) {
210 saa7110_write(sd, 0x06, 0x83);
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;
216 v4l2_dbg(1, debug, sd, "status=0x%02x (PAL/no color)\n", status);
217 /*saa7110_write(sd,0x2E,0x9A);*/
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;
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);*/
236 prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
237 schedule_timeout(msecs_to_jiffies(250));
238 finish_wait(&decoder->wq, &wait);
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;
246 v4l2_dbg(1, debug, sd, "status=0x%02x (PAL)\n", status);
250 static int saa7110_g_input_status(struct v4l2_subdev *sd, u32 *pstatus)
252 struct saa7110 *decoder = to_saa7110(sd);
253 int res = V4L2_IN_ST_NO_SIGNAL;
254 int status = saa7110_read(sd);
256 v4l2_dbg(1, debug, sd, "status=0x%02x norm=%llx\n",
257 status, decoder->norm);
258 if (!(status & 0x40))
260 if (!(status & 0x03))
261 res |= V4L2_IN_ST_NO_COLOR;
267 static int saa7110_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
269 *(v4l2_std_id *)std = determine_norm(sd);
273 static int saa7110_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
275 struct saa7110 *decoder = to_saa7110(sd);
277 if (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");
305 static int saa7110_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
307 struct saa7110 *decoder = to_saa7110(sd);
309 if (route->input < 0 || route->input >= SAA7110_MAX_INPUT) {
310 v4l2_dbg(1, debug, sd, "input=%d not available\n", route->input);
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);
320 static int saa7110_s_stream(struct v4l2_subdev *sd, int enable)
322 struct saa7110 *decoder = to_saa7110(sd);
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");
332 static int saa7110_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
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);
341 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
348 static int saa7110_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
350 struct saa7110 *decoder = to_saa7110(sd);
353 case V4L2_CID_BRIGHTNESS:
354 ctrl->value = decoder->bright;
356 case V4L2_CID_CONTRAST:
357 ctrl->value = decoder->contrast;
359 case V4L2_CID_SATURATION:
360 ctrl->value = decoder->sat;
363 ctrl->value = decoder->hue;
371 static int saa7110_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
373 struct saa7110 *decoder = to_saa7110(sd);
376 case V4L2_CID_BRIGHTNESS:
377 if (decoder->bright != ctrl->value) {
378 decoder->bright = ctrl->value;
379 saa7110_write(sd, 0x19, decoder->bright);
382 case V4L2_CID_CONTRAST:
383 if (decoder->contrast != ctrl->value) {
384 decoder->contrast = ctrl->value;
385 saa7110_write(sd, 0x13, decoder->contrast);
388 case V4L2_CID_SATURATION:
389 if (decoder->sat != ctrl->value) {
390 decoder->sat = ctrl->value;
391 saa7110_write(sd, 0x12, decoder->sat);
395 if (decoder->hue != ctrl->value) {
396 decoder->hue = ctrl->value;
397 saa7110_write(sd, 0x07, decoder->hue);
406 static int saa7110_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
408 struct i2c_client *client = v4l2_get_subdevdata(sd);
410 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7110, 0);
413 static int saa7110_command(struct i2c_client *client, unsigned cmd, void *arg)
415 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
418 /* ----------------------------------------------------------------------- */
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,
427 static const struct v4l2_subdev_tuner_ops saa7110_tuner_ops = {
428 .s_std = saa7110_s_std,
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,
438 static const struct v4l2_subdev_ops saa7110_ops = {
439 .core = &saa7110_core_ops,
440 .tuner = &saa7110_tuner_ops,
441 .video = &saa7110_video_ops,
444 /* ----------------------------------------------------------------------- */
446 static int saa7110_probe(struct i2c_client *client,
447 const struct i2c_device_id *id)
449 struct saa7110 *decoder;
450 struct v4l2_subdev *sd;
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))
458 v4l_info(client, "chip found @ 0x%x (%s)\n",
459 client->addr << 1, client->adapter->name);
461 decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
465 v4l2_i2c_subdev_init(sd, client, &saa7110_ops);
466 decoder->norm = V4L2_STD_PAL;
469 decoder->bright = 32768;
470 decoder->contrast = 32768;
471 decoder->hue = 32768;
472 decoder->sat = 32768;
473 init_waitqueue_head(&decoder->wq);
475 rv = saa7110_write_block(sd, initseq, sizeof(initseq));
477 v4l2_dbg(1, debug, sd, "init status %d\n", rv);
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);
486 status = saa7110_read(sd);
487 v4l2_dbg(1, debug, sd, "version %x, status=0x%02x\n",
489 saa7110_write(sd, 0x0D, 0x86);
490 saa7110_write(sd, 0x0F, 0x10);
491 saa7110_write(sd, 0x11, 0x59);
492 /*saa7110_write(sd, 0x2E, 0x9A);*/
495 /*saa7110_selmux(sd,0);*/
496 /*determine_norm(sd);*/
497 /* setup and implicit mode 0 select has been performed */
502 static int saa7110_remove(struct i2c_client *client)
504 struct v4l2_subdev *sd = i2c_get_clientdata(client);
506 v4l2_device_unregister_subdev(sd);
507 kfree(to_saa7110(sd));
511 /* ----------------------------------------------------------------------- */
513 static const struct i2c_device_id saa7110_id[] = {
517 MODULE_DEVICE_TABLE(i2c, saa7110_id);
519 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
521 .driverid = I2C_DRIVERID_SAA7110,
522 .command = saa7110_command,
523 .probe = saa7110_probe,
524 .remove = saa7110_remove,
525 .id_table = saa7110_id,