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.h>
41 MODULE_DESCRIPTION("Philips SAA7110 video decoder driver");
42 MODULE_AUTHOR("Pauline Middelink");
43 MODULE_LICENSE("GPL");
47 module_param(debug, int, 0);
48 MODULE_PARM_DESC(debug, "Debug level (0-1)");
50 #define SAA7110_MAX_INPUT 9 /* 6 CVBS, 3 SVHS */
51 #define SAA7110_MAX_OUTPUT 1 /* 1 YUV */
53 #define SAA7110_NR_REG 0x35
56 struct v4l2_subdev sd;
57 u8 reg[SAA7110_NR_REG];
70 static inline struct saa7110 *to_saa7110(struct v4l2_subdev *sd)
72 return container_of(sd, struct saa7110, sd);
75 /* ----------------------------------------------------------------------- */
76 /* I2C support functions */
77 /* ----------------------------------------------------------------------- */
79 static int saa7110_write(struct v4l2_subdev *sd, u8 reg, u8 value)
81 struct i2c_client *client = v4l2_get_subdevdata(sd);
82 struct saa7110 *decoder = to_saa7110(sd);
84 decoder->reg[reg] = value;
85 return i2c_smbus_write_byte_data(client, reg, value);
88 static int saa7110_write_block(struct v4l2_subdev *sd, const u8 *data, unsigned int len)
90 struct i2c_client *client = v4l2_get_subdevdata(sd);
91 struct saa7110 *decoder = to_saa7110(sd);
93 u8 reg = *data; /* first register to write to */
96 if (reg + (len - 1) > SAA7110_NR_REG)
99 /* the saa7110 has an autoincrement function, use it if
100 * the adapter understands raw I2C */
101 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
102 ret = i2c_master_send(client, data, len);
104 /* Cache the written data */
105 memcpy(decoder->reg + reg, data + 1, len - 1);
107 for (++data, --len; len; len--) {
108 ret = saa7110_write(sd, reg++, *data++);
117 static inline int saa7110_read(struct v4l2_subdev *sd)
119 struct i2c_client *client = v4l2_get_subdevdata(sd);
121 return i2c_smbus_read_byte(client);
124 /* ----------------------------------------------------------------------- */
125 /* SAA7110 functions */
126 /* ----------------------------------------------------------------------- */
128 #define FRESP_06H_COMPST 0x03 /*0x13*/
129 #define FRESP_06H_SVIDEO 0x83 /*0xC0*/
132 static int saa7110_selmux(struct v4l2_subdev *sd, int chan)
134 static const unsigned char modes[9][8] = {
136 {FRESP_06H_COMPST, 0xD9, 0x17, 0x40, 0x03,
139 {FRESP_06H_COMPST, 0xD8, 0x17, 0x40, 0x03,
142 {FRESP_06H_COMPST, 0xBA, 0x07, 0x91, 0x03,
145 {FRESP_06H_COMPST, 0xB8, 0x07, 0x91, 0x03,
148 {FRESP_06H_COMPST, 0x7C, 0x07, 0xD2, 0x83,
151 {FRESP_06H_COMPST, 0x78, 0x07, 0xD2, 0x83,
154 {FRESP_06H_SVIDEO, 0x59, 0x17, 0x42, 0xA3,
157 {FRESP_06H_SVIDEO, 0x9A, 0x17, 0xB1, 0x13,
160 {FRESP_06H_SVIDEO, 0x3C, 0x27, 0xC1, 0x23,
163 struct saa7110 *decoder = to_saa7110(sd);
164 const unsigned char *ptr = modes[chan];
166 saa7110_write(sd, 0x06, ptr[0]); /* Luminance control */
167 saa7110_write(sd, 0x20, ptr[1]); /* Analog Control #1 */
168 saa7110_write(sd, 0x21, ptr[2]); /* Analog Control #2 */
169 saa7110_write(sd, 0x22, ptr[3]); /* Mixer Control #1 */
170 saa7110_write(sd, 0x2C, ptr[4]); /* Mixer Control #2 */
171 saa7110_write(sd, 0x30, ptr[5]); /* ADCs gain control */
172 saa7110_write(sd, 0x31, ptr[6]); /* Mixer Control #3 */
173 saa7110_write(sd, 0x21, ptr[7]); /* Analog Control #2 */
174 decoder->input = chan;
179 static const unsigned char initseq[1 + SAA7110_NR_REG] = {
180 0, 0x4C, 0x3C, 0x0D, 0xEF, 0xBD, 0xF2, 0x03, 0x00,
181 /* 0x08 */ 0xF8, 0xF8, 0x60, 0x60, 0x00, 0x86, 0x18, 0x90,
182 /* 0x10 */ 0x00, 0x59, 0x40, 0x46, 0x42, 0x1A, 0xFF, 0xDA,
183 /* 0x18 */ 0xF2, 0x8B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
184 /* 0x20 */ 0xD9, 0x16, 0x40, 0x41, 0x80, 0x41, 0x80, 0x4F,
185 /* 0x28 */ 0xFE, 0x01, 0xCF, 0x0F, 0x03, 0x01, 0x03, 0x0C,
186 /* 0x30 */ 0x44, 0x71, 0x02, 0x8C, 0x02
189 static v4l2_std_id determine_norm(struct v4l2_subdev *sd)
192 struct saa7110 *decoder = to_saa7110(sd);
195 /* mode changed, start automatic detection */
196 saa7110_write_block(sd, initseq, sizeof(initseq));
197 saa7110_selmux(sd, decoder->input);
198 prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
199 schedule_timeout(msecs_to_jiffies(250));
200 finish_wait(&decoder->wq, &wait);
201 status = saa7110_read(sd);
203 v4l2_dbg(1, debug, sd, "status=0x%02x (no signal)\n", status);
204 return decoder->norm; /* no change*/
206 if ((status & 3) == 0) {
207 saa7110_write(sd, 0x06, 0x83);
209 v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC/no color)\n", status);
210 /*saa7110_write(sd,0x2E,0x81);*/
211 return V4L2_STD_NTSC;
213 v4l2_dbg(1, debug, sd, "status=0x%02x (PAL/no color)\n", status);
214 /*saa7110_write(sd,0x2E,0x9A);*/
217 /*saa7110_write(sd,0x06,0x03);*/
218 if (status & 0x20) { /* 60Hz */
219 v4l2_dbg(1, debug, sd, "status=0x%02x (NTSC)\n", status);
220 saa7110_write(sd, 0x0D, 0x86);
221 saa7110_write(sd, 0x0F, 0x50);
222 saa7110_write(sd, 0x11, 0x2C);
223 /*saa7110_write(sd,0x2E,0x81);*/
224 return V4L2_STD_NTSC;
227 /* 50Hz -> PAL/SECAM */
228 saa7110_write(sd, 0x0D, 0x86);
229 saa7110_write(sd, 0x0F, 0x10);
230 saa7110_write(sd, 0x11, 0x59);
231 /*saa7110_write(sd,0x2E,0x9A);*/
233 prepare_to_wait(&decoder->wq, &wait, TASK_UNINTERRUPTIBLE);
234 schedule_timeout(msecs_to_jiffies(250));
235 finish_wait(&decoder->wq, &wait);
237 status = saa7110_read(sd);
238 if ((status & 0x03) == 0x01) {
239 v4l2_dbg(1, debug, sd, "status=0x%02x (SECAM)\n", status);
240 saa7110_write(sd, 0x0D, 0x87);
241 return V4L2_STD_SECAM;
243 v4l2_dbg(1, debug, sd, "status=0x%02x (PAL)\n", status);
247 static int saa7110_g_input_status(struct v4l2_subdev *sd, u32 *pstatus)
249 struct saa7110 *decoder = to_saa7110(sd);
250 int res = V4L2_IN_ST_NO_SIGNAL;
251 int status = saa7110_read(sd);
253 v4l2_dbg(1, debug, sd, "status=0x%02x norm=%llx\n",
254 status, (unsigned long long)decoder->norm);
255 if (!(status & 0x40))
257 if (!(status & 0x03))
258 res |= V4L2_IN_ST_NO_COLOR;
264 static int saa7110_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
266 *(v4l2_std_id *)std = determine_norm(sd);
270 static int saa7110_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
272 struct saa7110 *decoder = to_saa7110(sd);
274 if (decoder->norm != std) {
276 /*saa7110_write(sd, 0x06, 0x03);*/
277 if (std & V4L2_STD_NTSC) {
278 saa7110_write(sd, 0x0D, 0x86);
279 saa7110_write(sd, 0x0F, 0x50);
280 saa7110_write(sd, 0x11, 0x2C);
281 /*saa7110_write(sd, 0x2E, 0x81);*/
282 v4l2_dbg(1, debug, sd, "switched to NTSC\n");
283 } else if (std & V4L2_STD_PAL) {
284 saa7110_write(sd, 0x0D, 0x86);
285 saa7110_write(sd, 0x0F, 0x10);
286 saa7110_write(sd, 0x11, 0x59);
287 /*saa7110_write(sd, 0x2E, 0x9A);*/
288 v4l2_dbg(1, debug, sd, "switched to PAL\n");
289 } else if (std & V4L2_STD_SECAM) {
290 saa7110_write(sd, 0x0D, 0x87);
291 saa7110_write(sd, 0x0F, 0x10);
292 saa7110_write(sd, 0x11, 0x59);
293 /*saa7110_write(sd, 0x2E, 0x9A);*/
294 v4l2_dbg(1, debug, sd, "switched to SECAM\n");
302 static int saa7110_s_routing(struct v4l2_subdev *sd, const struct v4l2_routing *route)
304 struct saa7110 *decoder = to_saa7110(sd);
306 if (route->input < 0 || route->input >= SAA7110_MAX_INPUT) {
307 v4l2_dbg(1, debug, sd, "input=%d not available\n", route->input);
310 if (decoder->input != route->input) {
311 saa7110_selmux(sd, route->input);
312 v4l2_dbg(1, debug, sd, "switched to input=%d\n", route->input);
317 static int saa7110_s_stream(struct v4l2_subdev *sd, int enable)
319 struct saa7110 *decoder = to_saa7110(sd);
321 if (decoder->enable != enable) {
322 decoder->enable = enable;
323 saa7110_write(sd, 0x0E, enable ? 0x18 : 0x80);
324 v4l2_dbg(1, debug, sd, "YUV %s\n", enable ? "on" : "off");
329 static int saa7110_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
332 case V4L2_CID_BRIGHTNESS:
333 return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
334 case V4L2_CID_CONTRAST:
335 case V4L2_CID_SATURATION:
336 return v4l2_ctrl_query_fill(qc, 0, 127, 1, 64);
338 return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
345 static int saa7110_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
347 struct saa7110 *decoder = to_saa7110(sd);
350 case V4L2_CID_BRIGHTNESS:
351 ctrl->value = decoder->bright;
353 case V4L2_CID_CONTRAST:
354 ctrl->value = decoder->contrast;
356 case V4L2_CID_SATURATION:
357 ctrl->value = decoder->sat;
360 ctrl->value = decoder->hue;
368 static int saa7110_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
370 struct saa7110 *decoder = to_saa7110(sd);
373 case V4L2_CID_BRIGHTNESS:
374 if (decoder->bright != ctrl->value) {
375 decoder->bright = ctrl->value;
376 saa7110_write(sd, 0x19, decoder->bright);
379 case V4L2_CID_CONTRAST:
380 if (decoder->contrast != ctrl->value) {
381 decoder->contrast = ctrl->value;
382 saa7110_write(sd, 0x13, decoder->contrast);
385 case V4L2_CID_SATURATION:
386 if (decoder->sat != ctrl->value) {
387 decoder->sat = ctrl->value;
388 saa7110_write(sd, 0x12, decoder->sat);
392 if (decoder->hue != ctrl->value) {
393 decoder->hue = ctrl->value;
394 saa7110_write(sd, 0x07, decoder->hue);
403 static int saa7110_g_chip_ident(struct v4l2_subdev *sd, struct v4l2_dbg_chip_ident *chip)
405 struct i2c_client *client = v4l2_get_subdevdata(sd);
407 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7110, 0);
410 /* ----------------------------------------------------------------------- */
412 static const struct v4l2_subdev_core_ops saa7110_core_ops = {
413 .g_chip_ident = saa7110_g_chip_ident,
414 .g_ctrl = saa7110_g_ctrl,
415 .s_ctrl = saa7110_s_ctrl,
416 .queryctrl = saa7110_queryctrl,
419 static const struct v4l2_subdev_tuner_ops saa7110_tuner_ops = {
420 .s_std = saa7110_s_std,
423 static const struct v4l2_subdev_video_ops saa7110_video_ops = {
424 .s_routing = saa7110_s_routing,
425 .s_stream = saa7110_s_stream,
426 .querystd = saa7110_querystd,
427 .g_input_status = saa7110_g_input_status,
430 static const struct v4l2_subdev_ops saa7110_ops = {
431 .core = &saa7110_core_ops,
432 .tuner = &saa7110_tuner_ops,
433 .video = &saa7110_video_ops,
436 /* ----------------------------------------------------------------------- */
438 static int saa7110_probe(struct i2c_client *client,
439 const struct i2c_device_id *id)
441 struct saa7110 *decoder;
442 struct v4l2_subdev *sd;
445 /* Check if the adapter supports the needed features */
446 if (!i2c_check_functionality(client->adapter,
447 I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
450 v4l_info(client, "chip found @ 0x%x (%s)\n",
451 client->addr << 1, client->adapter->name);
453 decoder = kzalloc(sizeof(struct saa7110), GFP_KERNEL);
457 v4l2_i2c_subdev_init(sd, client, &saa7110_ops);
458 decoder->norm = V4L2_STD_PAL;
461 decoder->bright = 32768;
462 decoder->contrast = 32768;
463 decoder->hue = 32768;
464 decoder->sat = 32768;
465 init_waitqueue_head(&decoder->wq);
467 rv = saa7110_write_block(sd, initseq, sizeof(initseq));
469 v4l2_dbg(1, debug, sd, "init status %d\n", rv);
472 saa7110_write(sd, 0x21, 0x10);
473 saa7110_write(sd, 0x0e, 0x18);
474 saa7110_write(sd, 0x0D, 0x04);
475 ver = saa7110_read(sd);
476 saa7110_write(sd, 0x0D, 0x06);
478 status = saa7110_read(sd);
479 v4l2_dbg(1, debug, sd, "version %x, status=0x%02x\n",
481 saa7110_write(sd, 0x0D, 0x86);
482 saa7110_write(sd, 0x0F, 0x10);
483 saa7110_write(sd, 0x11, 0x59);
484 /*saa7110_write(sd, 0x2E, 0x9A);*/
487 /*saa7110_selmux(sd,0);*/
488 /*determine_norm(sd);*/
489 /* setup and implicit mode 0 select has been performed */
494 static int saa7110_remove(struct i2c_client *client)
496 struct v4l2_subdev *sd = i2c_get_clientdata(client);
498 v4l2_device_unregister_subdev(sd);
499 kfree(to_saa7110(sd));
503 /* ----------------------------------------------------------------------- */
505 static const struct i2c_device_id saa7110_id[] = {
509 MODULE_DEVICE_TABLE(i2c, saa7110_id);
511 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
513 .probe = saa7110_probe,
514 .remove = saa7110_remove,
515 .id_table = saa7110_id,