2 * saa7191.c - Philips SAA7191 video decoder driver
4 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
5 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/delay.h>
13 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/major.h>
18 #include <linux/module.h>
20 #include <linux/slab.h>
22 #include <linux/videodev2.h>
23 #include <linux/i2c.h>
24 #include <media/v4l2-common.h>
25 #include <media/v4l2-i2c-drv-legacy.h>
29 #define SAA7191_MODULE_VERSION "0.0.5"
31 MODULE_DESCRIPTION("Philips SAA7191 video decoder driver");
32 MODULE_VERSION(SAA7191_MODULE_VERSION);
33 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
34 MODULE_LICENSE("GPL");
36 static unsigned short normal_i2c[] = { 0x8a >> 1, 0x8e >> 1, I2C_CLIENT_END };
40 // #define SAA7191_DEBUG
43 #define dprintk(x...) printk("SAA7191: " x);
48 #define SAA7191_SYNC_COUNT 30
49 #define SAA7191_SYNC_DELAY 100 /* milliseconds */
52 struct i2c_client *client;
54 /* the register values are stored here as the actual
55 * I2C-registers are write-only */
62 static const u8 initseq[] = {
65 0x50, /* (0x50) SAA7191_REG_IDEL */
67 /* 50 Hz signal timing */
68 0x30, /* (0x30) SAA7191_REG_HSYB */
69 0x00, /* (0x00) SAA7191_REG_HSYS */
70 0xe8, /* (0xe8) SAA7191_REG_HCLB */
71 0xb6, /* (0xb6) SAA7191_REG_HCLS */
72 0xf4, /* (0xf4) SAA7191_REG_HPHI */
75 SAA7191_LUMA_APER_1, /* (0x01) SAA7191_REG_LUMA - CVBS mode */
76 0x00, /* (0x00) SAA7191_REG_HUEC */
77 0xf8, /* (0xf8) SAA7191_REG_CKTQ */
78 0xf8, /* (0xf8) SAA7191_REG_CKTS */
79 0x90, /* (0x90) SAA7191_REG_PLSE */
80 0x90, /* (0x90) SAA7191_REG_SESE */
81 0x00, /* (0x00) SAA7191_REG_GAIN */
82 SAA7191_STDC_NFEN | SAA7191_STDC_HRMV, /* (0x0c) SAA7191_REG_STDC
84 * slow time constant */
85 SAA7191_IOCK_OEDC | SAA7191_IOCK_OEHS | SAA7191_IOCK_OEVS
86 | SAA7191_IOCK_OEDY, /* (0x78) SAA7191_REG_IOCK
87 * - chroma from CVBS, GPSW1 & 2 off */
88 SAA7191_CTL3_AUFD | SAA7191_CTL3_SCEN | SAA7191_CTL3_OFTS
89 | SAA7191_CTL3_YDEL0, /* (0x99) SAA7191_REG_CTL3
90 * - automatic field detection */
91 0x00, /* (0x00) SAA7191_REG_CTL4 */
92 0x2c, /* (0x2c) SAA7191_REG_CHCV - PAL nominal value */
96 /* 60 Hz signal timing */
97 0x34, /* (0x34) SAA7191_REG_HS6B */
98 0x0a, /* (0x0a) SAA7191_REG_HS6S */
99 0xf4, /* (0xf4) SAA7191_REG_HC6B */
100 0xce, /* (0xce) SAA7191_REG_HC6S */
101 0xf4, /* (0xf4) SAA7191_REG_HP6I */
104 /* SAA7191 register handling */
106 static u8 saa7191_read_reg(struct i2c_client *client,
109 return ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg];
112 static int saa7191_read_status(struct i2c_client *client,
117 ret = i2c_master_recv(client, value, 1);
119 printk(KERN_ERR "SAA7191: saa7191_read_status(): read failed\n");
127 static int saa7191_write_reg(struct i2c_client *client, u8 reg,
130 ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg] = value;
131 return i2c_smbus_write_byte_data(client, reg, value);
134 /* the first byte of data must be the first subaddress number (register) */
135 static int saa7191_write_block(struct i2c_client *client,
136 u8 length, const u8 *data)
141 struct saa7191 *decoder = (struct saa7191 *)i2c_get_clientdata(client);
142 for (i = 0; i < (length - 1); i++) {
143 decoder->reg[data[0] + i] = data[i + 1];
146 ret = i2c_master_send(client, data, length);
148 printk(KERN_ERR "SAA7191: saa7191_write_block(): "
156 /* Helper functions */
158 static int saa7191_set_input(struct i2c_client *client, int input)
160 struct saa7191 *decoder = i2c_get_clientdata(client);
161 u8 luma = saa7191_read_reg(client, SAA7191_REG_LUMA);
162 u8 iock = saa7191_read_reg(client, SAA7191_REG_IOCK);
166 case SAA7191_INPUT_COMPOSITE: /* Set Composite input */
167 iock &= ~(SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW1
168 | SAA7191_IOCK_GPSW2);
169 /* Chrominance trap active */
170 luma &= ~SAA7191_LUMA_BYPS;
172 case SAA7191_INPUT_SVIDEO: /* Set S-Video input */
173 iock |= SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW2;
174 /* Chrominance trap bypassed */
175 luma |= SAA7191_LUMA_BYPS;
181 err = saa7191_write_reg(client, SAA7191_REG_LUMA, luma);
184 err = saa7191_write_reg(client, SAA7191_REG_IOCK, iock);
188 decoder->input = input;
193 static int saa7191_set_norm(struct i2c_client *client, v4l2_std_id norm)
195 struct saa7191 *decoder = i2c_get_clientdata(client);
196 u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC);
197 u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3);
198 u8 chcv = saa7191_read_reg(client, SAA7191_REG_CHCV);
201 if (norm & V4L2_STD_PAL) {
202 stdc &= ~SAA7191_STDC_SECS;
203 ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
204 chcv = SAA7191_CHCV_PAL;
205 } else if (norm & V4L2_STD_NTSC) {
206 stdc &= ~SAA7191_STDC_SECS;
207 ctl3 &= ~SAA7191_CTL3_AUFD;
208 ctl3 |= SAA7191_CTL3_FSEL;
209 chcv = SAA7191_CHCV_NTSC;
210 } else if (norm & V4L2_STD_SECAM) {
211 stdc |= SAA7191_STDC_SECS;
212 ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
213 chcv = SAA7191_CHCV_PAL;
218 err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
221 err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc);
224 err = saa7191_write_reg(client, SAA7191_REG_CHCV, chcv);
228 decoder->norm = norm;
230 dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3,
232 dprintk("norm: %llx\n", norm);
237 static int saa7191_wait_for_signal(struct i2c_client *client, u8 *status)
241 dprintk("Checking for signal...\n");
243 for (i = 0; i < SAA7191_SYNC_COUNT; i++) {
244 if (saa7191_read_status(client, status))
247 if (((*status) & SAA7191_STATUS_HLCK) == 0) {
248 dprintk("Signal found\n");
252 msleep(SAA7191_SYNC_DELAY);
255 dprintk("No signal\n");
260 static int saa7191_autodetect_norm_extended(struct i2c_client *client,
263 struct saa7191 *decoder = i2c_get_clientdata(client);
264 u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC);
265 u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3);
267 v4l2_std_id old_norm = decoder->norm;
270 dprintk("SAA7191 extended signal auto-detection...\n");
272 *norm = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
273 stdc &= ~SAA7191_STDC_SECS;
274 ctl3 &= ~(SAA7191_CTL3_FSEL);
276 err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc);
281 err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
287 ctl3 |= SAA7191_CTL3_AUFD;
288 err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
294 msleep(SAA7191_SYNC_DELAY);
296 err = saa7191_wait_for_signal(client, &status);
300 if (status & SAA7191_STATUS_FIDT) {
301 /* 60Hz signal -> NTSC */
302 dprintk("60Hz signal: NTSC\n");
303 *norm = V4L2_STD_NTSC;
308 dprintk("50Hz signal: Trying PAL...\n");
311 err = saa7191_set_norm(client, V4L2_STD_PAL);
315 msleep(SAA7191_SYNC_DELAY);
317 err = saa7191_wait_for_signal(client, &status);
322 if (status & SAA7191_STATUS_FIDT) {
323 dprintk("No 50Hz signal\n");
324 saa7191_set_norm(client, old_norm);
328 if (status & SAA7191_STATUS_CODE) {
330 *norm = V4L2_STD_PAL;
331 return saa7191_set_norm(client, old_norm);
334 dprintk("No color detected with PAL - Trying SECAM...\n");
336 /* no color detected ? -> try SECAM */
337 err = saa7191_set_norm(client, V4L2_STD_SECAM);
341 msleep(SAA7191_SYNC_DELAY);
343 err = saa7191_wait_for_signal(client, &status);
348 if (status & SAA7191_STATUS_FIDT) {
349 dprintk("No 50Hz signal\n");
354 if (status & SAA7191_STATUS_CODE) {
355 /* Color detected -> SECAM */
357 *norm = V4L2_STD_SECAM;
358 return saa7191_set_norm(client, old_norm);
361 dprintk("No color detected with SECAM - Going back to PAL.\n");
364 return saa7191_set_norm(client, old_norm);
367 static int saa7191_autodetect_norm(struct i2c_client *client)
371 dprintk("SAA7191 signal auto-detection...\n");
373 dprintk("Reading status...\n");
375 if (saa7191_read_status(client, &status))
378 dprintk("Checking for signal...\n");
381 if (status & SAA7191_STATUS_HLCK) {
382 dprintk("No signal\n");
386 dprintk("Signal found\n");
388 if (status & SAA7191_STATUS_FIDT) {
389 /* 60hz signal -> NTSC */
391 return saa7191_set_norm(client, V4L2_STD_NTSC);
393 /* 50hz signal -> PAL */
395 return saa7191_set_norm(client, V4L2_STD_PAL);
399 static int saa7191_get_control(struct i2c_client *client,
400 struct v4l2_control *ctrl)
406 case SAA7191_CONTROL_BANDPASS:
407 case SAA7191_CONTROL_BANDPASS_WEIGHT:
408 case SAA7191_CONTROL_CORING:
409 reg = saa7191_read_reg(client, SAA7191_REG_LUMA);
411 case SAA7191_CONTROL_BANDPASS:
412 ctrl->value = ((s32)reg & SAA7191_LUMA_BPSS_MASK)
413 >> SAA7191_LUMA_BPSS_SHIFT;
415 case SAA7191_CONTROL_BANDPASS_WEIGHT:
416 ctrl->value = ((s32)reg & SAA7191_LUMA_APER_MASK)
417 >> SAA7191_LUMA_APER_SHIFT;
419 case SAA7191_CONTROL_CORING:
420 ctrl->value = ((s32)reg & SAA7191_LUMA_CORI_MASK)
421 >> SAA7191_LUMA_CORI_SHIFT;
425 case SAA7191_CONTROL_FORCE_COLOUR:
426 case SAA7191_CONTROL_CHROMA_GAIN:
427 reg = saa7191_read_reg(client, SAA7191_REG_GAIN);
428 if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR)
429 ctrl->value = ((s32)reg & SAA7191_GAIN_COLO) ? 1 : 0;
431 ctrl->value = ((s32)reg & SAA7191_GAIN_LFIS_MASK)
432 >> SAA7191_GAIN_LFIS_SHIFT;
435 reg = saa7191_read_reg(client, SAA7191_REG_HUEC);
440 ctrl->value = (s32)reg;
442 case SAA7191_CONTROL_VTRC:
443 reg = saa7191_read_reg(client, SAA7191_REG_STDC);
444 ctrl->value = ((s32)reg & SAA7191_STDC_VTRC) ? 1 : 0;
446 case SAA7191_CONTROL_LUMA_DELAY:
447 reg = saa7191_read_reg(client, SAA7191_REG_CTL3);
448 ctrl->value = ((s32)reg & SAA7191_CTL3_YDEL_MASK)
449 >> SAA7191_CTL3_YDEL_SHIFT;
450 if (ctrl->value >= 4)
453 case SAA7191_CONTROL_VNR:
454 reg = saa7191_read_reg(client, SAA7191_REG_CTL4);
455 ctrl->value = ((s32)reg & SAA7191_CTL4_VNOI_MASK)
456 >> SAA7191_CTL4_VNOI_SHIFT;
465 static int saa7191_set_control(struct i2c_client *client,
466 struct v4l2_control *ctrl)
472 case SAA7191_CONTROL_BANDPASS:
473 case SAA7191_CONTROL_BANDPASS_WEIGHT:
474 case SAA7191_CONTROL_CORING:
475 reg = saa7191_read_reg(client, SAA7191_REG_LUMA);
477 case SAA7191_CONTROL_BANDPASS:
478 reg &= ~SAA7191_LUMA_BPSS_MASK;
479 reg |= (ctrl->value << SAA7191_LUMA_BPSS_SHIFT)
480 & SAA7191_LUMA_BPSS_MASK;
482 case SAA7191_CONTROL_BANDPASS_WEIGHT:
483 reg &= ~SAA7191_LUMA_APER_MASK;
484 reg |= (ctrl->value << SAA7191_LUMA_APER_SHIFT)
485 & SAA7191_LUMA_APER_MASK;
487 case SAA7191_CONTROL_CORING:
488 reg &= ~SAA7191_LUMA_CORI_MASK;
489 reg |= (ctrl->value << SAA7191_LUMA_CORI_SHIFT)
490 & SAA7191_LUMA_CORI_MASK;
493 ret = saa7191_write_reg(client, SAA7191_REG_LUMA, reg);
495 case SAA7191_CONTROL_FORCE_COLOUR:
496 case SAA7191_CONTROL_CHROMA_GAIN:
497 reg = saa7191_read_reg(client, SAA7191_REG_GAIN);
498 if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR) {
500 reg |= SAA7191_GAIN_COLO;
502 reg &= ~SAA7191_GAIN_COLO;
504 reg &= ~SAA7191_GAIN_LFIS_MASK;
505 reg |= (ctrl->value << SAA7191_GAIN_LFIS_SHIFT)
506 & SAA7191_GAIN_LFIS_MASK;
508 ret = saa7191_write_reg(client, SAA7191_REG_GAIN, reg);
511 reg = ctrl->value & 0xff;
516 ret = saa7191_write_reg(client, SAA7191_REG_HUEC, reg);
518 case SAA7191_CONTROL_VTRC:
519 reg = saa7191_read_reg(client, SAA7191_REG_STDC);
521 reg |= SAA7191_STDC_VTRC;
523 reg &= ~SAA7191_STDC_VTRC;
524 ret = saa7191_write_reg(client, SAA7191_REG_STDC, reg);
526 case SAA7191_CONTROL_LUMA_DELAY: {
527 s32 value = ctrl->value;
530 reg = saa7191_read_reg(client, SAA7191_REG_CTL3);
531 reg &= ~SAA7191_CTL3_YDEL_MASK;
532 reg |= (value << SAA7191_CTL3_YDEL_SHIFT)
533 & SAA7191_CTL3_YDEL_MASK;
534 ret = saa7191_write_reg(client, SAA7191_REG_CTL3, reg);
537 case SAA7191_CONTROL_VNR:
538 reg = saa7191_read_reg(client, SAA7191_REG_CTL4);
539 reg &= ~SAA7191_CTL4_VNOI_MASK;
540 reg |= (ctrl->value << SAA7191_CTL4_VNOI_SHIFT)
541 & SAA7191_CTL4_VNOI_MASK;
542 ret = saa7191_write_reg(client, SAA7191_REG_CTL4, reg);
553 static int saa7191_command(struct i2c_client *client, unsigned int cmd,
557 case VIDIOC_INT_G_INPUT_STATUS: {
560 int res = V4L2_IN_ST_NO_SIGNAL;
562 if (saa7191_read_status(client, &status))
564 if ((status & SAA7191_STATUS_HLCK) == 0)
566 if (!(status & SAA7191_STATUS_CODE))
567 res |= V4L2_IN_ST_NO_COLOR;
572 case VIDIOC_QUERYSTD:
573 return saa7191_autodetect_norm_extended(client, arg);
576 v4l2_std_id *istd = arg;
578 return saa7191_set_norm(client, *istd);
580 case VIDIOC_INT_S_VIDEO_ROUTING: {
581 struct v4l2_routing *route = arg;
583 return saa7191_set_input(client, route->input);
587 return saa7191_get_control(client, arg);
590 return saa7191_set_control(client, arg);
599 static int saa7191_probe(struct i2c_client *client,
600 const struct i2c_device_id *id)
603 struct saa7191 *decoder;
605 v4l_info(client, "chip found @ 0x%x (%s)\n",
606 client->addr << 1, client->adapter->name);
608 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
612 i2c_set_clientdata(client, decoder);
614 decoder->client = client;
616 err = saa7191_write_block(client, sizeof(initseq), initseq);
618 printk(KERN_ERR "SAA7191 initialization failed\n");
623 printk(KERN_INFO "SAA7191 initialized\n");
625 decoder->input = SAA7191_INPUT_COMPOSITE;
626 decoder->norm = V4L2_STD_PAL;
628 err = saa7191_autodetect_norm(client);
629 if (err && (err != -EBUSY))
630 printk(KERN_ERR "SAA7191: Signal auto-detection failed\n");
635 static int saa7191_remove(struct i2c_client *client)
637 struct saa7191 *decoder = i2c_get_clientdata(client);
643 static int saa7191_legacy_probe(struct i2c_adapter *adapter)
645 return adapter->id == I2C_HW_SGI_VINO;
648 static const struct i2c_device_id saa7191_id[] = {
652 MODULE_DEVICE_TABLE(i2c, saa7191_id);
654 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
656 .driverid = I2C_DRIVERID_SAA7191,
657 .command = saa7191_command,
658 .probe = saa7191_probe,
659 .remove = saa7191_remove,
660 .legacy_probe = saa7191_legacy_probe,
661 .id_table = saa7191_id,