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/videodev.h>
23 #include <linux/video_decoder.h>
24 #include <linux/i2c.h>
28 #define SAA7191_MODULE_VERSION "0.0.5"
30 MODULE_DESCRIPTION("Philips SAA7191 video decoder driver");
31 MODULE_VERSION(SAA7191_MODULE_VERSION);
32 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
33 MODULE_LICENSE("GPL");
35 // #define SAA7191_DEBUG
38 #define dprintk(x...) printk("SAA7191: " x);
43 #define SAA7191_SYNC_COUNT 30
44 #define SAA7191_SYNC_DELAY 100 /* milliseconds */
47 struct i2c_client *client;
49 /* the register values are stored here as the actual
50 * I2C-registers are write-only */
57 static struct i2c_driver i2c_driver_saa7191;
59 static const u8 initseq[] = {
62 0x50, /* (0x50) SAA7191_REG_IDEL */
64 /* 50 Hz signal timing */
65 0x30, /* (0x30) SAA7191_REG_HSYB */
66 0x00, /* (0x00) SAA7191_REG_HSYS */
67 0xe8, /* (0xe8) SAA7191_REG_HCLB */
68 0xb6, /* (0xb6) SAA7191_REG_HCLS */
69 0xf4, /* (0xf4) SAA7191_REG_HPHI */
72 SAA7191_LUMA_APER_1, /* (0x01) SAA7191_REG_LUMA - CVBS mode */
73 0x00, /* (0x00) SAA7191_REG_HUEC */
74 0xf8, /* (0xf8) SAA7191_REG_CKTQ */
75 0xf8, /* (0xf8) SAA7191_REG_CKTS */
76 0x90, /* (0x90) SAA7191_REG_PLSE */
77 0x90, /* (0x90) SAA7191_REG_SESE */
78 0x00, /* (0x00) SAA7191_REG_GAIN */
79 SAA7191_STDC_NFEN | SAA7191_STDC_HRMV, /* (0x0c) SAA7191_REG_STDC
81 * slow time constant */
82 SAA7191_IOCK_OEDC | SAA7191_IOCK_OEHS | SAA7191_IOCK_OEVS
83 | SAA7191_IOCK_OEDY, /* (0x78) SAA7191_REG_IOCK
84 * - chroma from CVBS, GPSW1 & 2 off */
85 SAA7191_CTL3_AUFD | SAA7191_CTL3_SCEN | SAA7191_CTL3_OFTS
86 | SAA7191_CTL3_YDEL0, /* (0x99) SAA7191_REG_CTL3
87 * - automatic field detection */
88 0x00, /* (0x00) SAA7191_REG_CTL4 */
89 0x2c, /* (0x2c) SAA7191_REG_CHCV - PAL nominal value */
93 /* 60 Hz signal timing */
94 0x34, /* (0x34) SAA7191_REG_HS6B */
95 0x0a, /* (0x0a) SAA7191_REG_HS6S */
96 0xf4, /* (0xf4) SAA7191_REG_HC6B */
97 0xce, /* (0xce) SAA7191_REG_HC6S */
98 0xf4, /* (0xf4) SAA7191_REG_HP6I */
101 /* SAA7191 register handling */
103 static u8 saa7191_read_reg(struct i2c_client *client,
106 return ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg];
109 static int saa7191_read_status(struct i2c_client *client,
114 ret = i2c_master_recv(client, value, 1);
116 printk(KERN_ERR "SAA7191: saa7191_read_status(): read failed\n");
124 static int saa7191_write_reg(struct i2c_client *client, u8 reg,
127 ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg] = value;
128 return i2c_smbus_write_byte_data(client, reg, value);
131 /* the first byte of data must be the first subaddress number (register) */
132 static int saa7191_write_block(struct i2c_client *client,
133 u8 length, const u8 *data)
138 struct saa7191 *decoder = (struct saa7191 *)i2c_get_clientdata(client);
139 for (i = 0; i < (length - 1); i++) {
140 decoder->reg[data[0] + i] = data[i + 1];
143 ret = i2c_master_send(client, data, length);
145 printk(KERN_ERR "SAA7191: saa7191_write_block(): "
153 /* Helper functions */
155 static int saa7191_set_input(struct i2c_client *client, int input)
157 struct saa7191 *decoder = i2c_get_clientdata(client);
158 u8 luma = saa7191_read_reg(client, SAA7191_REG_LUMA);
159 u8 iock = saa7191_read_reg(client, SAA7191_REG_IOCK);
163 case SAA7191_INPUT_COMPOSITE: /* Set Composite input */
164 iock &= ~(SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW1
165 | SAA7191_IOCK_GPSW2);
166 /* Chrominance trap active */
167 luma &= ~SAA7191_LUMA_BYPS;
169 case SAA7191_INPUT_SVIDEO: /* Set S-Video input */
170 iock |= SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW2;
171 /* Chrominance trap bypassed */
172 luma |= SAA7191_LUMA_BYPS;
178 err = saa7191_write_reg(client, SAA7191_REG_LUMA, luma);
181 err = saa7191_write_reg(client, SAA7191_REG_IOCK, iock);
185 decoder->input = input;
190 static int saa7191_set_norm(struct i2c_client *client, int norm)
192 struct saa7191 *decoder = i2c_get_clientdata(client);
193 u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC);
194 u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3);
195 u8 chcv = saa7191_read_reg(client, SAA7191_REG_CHCV);
199 case SAA7191_NORM_PAL:
200 stdc &= ~SAA7191_STDC_SECS;
201 ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
202 chcv = SAA7191_CHCV_PAL;
204 case SAA7191_NORM_NTSC:
205 stdc &= ~SAA7191_STDC_SECS;
206 ctl3 &= ~SAA7191_CTL3_AUFD;
207 ctl3 |= SAA7191_CTL3_FSEL;
208 chcv = SAA7191_CHCV_NTSC;
210 case SAA7191_NORM_SECAM:
211 stdc |= SAA7191_STDC_SECS;
212 ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
213 chcv = SAA7191_CHCV_PAL;
219 err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
222 err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc);
225 err = saa7191_write_reg(client, SAA7191_REG_CHCV, chcv);
229 decoder->norm = norm;
231 dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3,
233 dprintk("norm: %d\n", norm);
238 static int saa7191_wait_for_signal(struct i2c_client *client, u8 *status)
242 dprintk("Checking for signal...\n");
244 for (i = 0; i < SAA7191_SYNC_COUNT; i++) {
245 if (saa7191_read_status(client, status))
248 if (((*status) & SAA7191_STATUS_HLCK) == 0) {
249 dprintk("Signal found\n");
253 msleep(SAA7191_SYNC_DELAY);
256 dprintk("No signal\n");
261 static int saa7191_autodetect_norm_extended(struct i2c_client *client)
263 u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC);
264 u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3);
268 dprintk("SAA7191 extended signal auto-detection...\n");
270 stdc &= ~SAA7191_STDC_SECS;
271 ctl3 &= ~(SAA7191_CTL3_FSEL);
273 err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc);
278 err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
284 ctl3 |= SAA7191_CTL3_AUFD;
285 err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
291 msleep(SAA7191_SYNC_DELAY);
293 err = saa7191_wait_for_signal(client, &status);
297 if (status & SAA7191_STATUS_FIDT) {
298 /* 60Hz signal -> NTSC */
299 dprintk("60Hz signal: NTSC\n");
300 return saa7191_set_norm(client, SAA7191_NORM_NTSC);
304 dprintk("50Hz signal: Trying PAL...\n");
307 err = saa7191_set_norm(client, SAA7191_NORM_PAL);
311 msleep(SAA7191_SYNC_DELAY);
313 err = saa7191_wait_for_signal(client, &status);
318 if (status & SAA7191_STATUS_FIDT) {
319 dprintk("No 50Hz signal\n");
324 if (status & SAA7191_STATUS_CODE) {
329 dprintk("No color detected with PAL - Trying SECAM...\n");
331 /* no color detected ? -> try SECAM */
332 err = saa7191_set_norm(client,
337 msleep(SAA7191_SYNC_DELAY);
339 err = saa7191_wait_for_signal(client, &status);
344 if (status & SAA7191_STATUS_FIDT) {
345 dprintk("No 50Hz signal\n");
350 if (status & SAA7191_STATUS_CODE) {
351 /* Color detected -> SECAM */
356 dprintk("No color detected with SECAM - Going back to PAL.\n");
358 /* still no color detected ?
359 * -> set norm back to PAL */
360 err = saa7191_set_norm(client,
366 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3);
367 if (ctl3 & SAA7191_CTL3_AUFD) {
368 ctl3 &= ~(SAA7191_CTL3_AUFD);
369 err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
378 static int saa7191_autodetect_norm(struct i2c_client *client)
382 dprintk("SAA7191 signal auto-detection...\n");
384 dprintk("Reading status...\n");
386 if (saa7191_read_status(client, &status))
389 dprintk("Checking for signal...\n");
392 if (status & SAA7191_STATUS_HLCK) {
393 dprintk("No signal\n");
397 dprintk("Signal found\n");
399 if (status & SAA7191_STATUS_FIDT) {
400 /* 60hz signal -> NTSC */
402 return saa7191_set_norm(client, SAA7191_NORM_NTSC);
404 /* 50hz signal -> PAL */
406 return saa7191_set_norm(client, SAA7191_NORM_PAL);
410 static int saa7191_get_control(struct i2c_client *client,
411 struct saa7191_control *ctrl)
416 switch (ctrl->type) {
417 case SAA7191_CONTROL_BANDPASS:
418 case SAA7191_CONTROL_BANDPASS_WEIGHT:
419 case SAA7191_CONTROL_CORING:
420 reg = saa7191_read_reg(client, SAA7191_REG_LUMA);
421 switch (ctrl->type) {
422 case SAA7191_CONTROL_BANDPASS:
423 ctrl->value = ((s32)reg & SAA7191_LUMA_BPSS_MASK)
424 >> SAA7191_LUMA_BPSS_SHIFT;
426 case SAA7191_CONTROL_BANDPASS_WEIGHT:
427 ctrl->value = ((s32)reg & SAA7191_LUMA_APER_MASK)
428 >> SAA7191_LUMA_APER_SHIFT;
430 case SAA7191_CONTROL_CORING:
431 ctrl->value = ((s32)reg & SAA7191_LUMA_CORI_MASK)
432 >> SAA7191_LUMA_CORI_SHIFT;
436 case SAA7191_CONTROL_FORCE_COLOUR:
437 case SAA7191_CONTROL_CHROMA_GAIN:
438 reg = saa7191_read_reg(client, SAA7191_REG_GAIN);
439 if (ctrl->type == SAA7191_CONTROL_FORCE_COLOUR)
440 ctrl->value = ((s32)reg & SAA7191_GAIN_COLO) ? 1 : 0;
442 ctrl->value = ((s32)reg & SAA7191_GAIN_LFIS_MASK)
443 >> SAA7191_GAIN_LFIS_SHIFT;
445 case SAA7191_CONTROL_HUE:
446 reg = saa7191_read_reg(client, SAA7191_REG_HUEC);
451 ctrl->value = (s32)reg;
453 case SAA7191_CONTROL_VTRC:
454 reg = saa7191_read_reg(client, SAA7191_REG_STDC);
455 ctrl->value = ((s32)reg & SAA7191_STDC_VTRC) ? 1 : 0;
457 case SAA7191_CONTROL_LUMA_DELAY:
458 reg = saa7191_read_reg(client, SAA7191_REG_CTL3);
459 ctrl->value = ((s32)reg & SAA7191_CTL3_YDEL_MASK)
460 >> SAA7191_CTL3_YDEL_SHIFT;
461 if (ctrl->value >= 4)
464 case SAA7191_CONTROL_VNR:
465 reg = saa7191_read_reg(client, SAA7191_REG_CTL4);
466 ctrl->value = ((s32)reg & SAA7191_CTL4_VNOI_MASK)
467 >> SAA7191_CTL4_VNOI_SHIFT;
476 static int saa7191_set_control(struct i2c_client *client,
477 struct saa7191_control *ctrl)
482 switch (ctrl->type) {
483 case SAA7191_CONTROL_BANDPASS:
484 case SAA7191_CONTROL_BANDPASS_WEIGHT:
485 case SAA7191_CONTROL_CORING:
486 reg = saa7191_read_reg(client, SAA7191_REG_LUMA);
487 switch (ctrl->type) {
488 case SAA7191_CONTROL_BANDPASS:
489 reg &= ~SAA7191_LUMA_BPSS_MASK;
490 reg |= (ctrl->value << SAA7191_LUMA_BPSS_SHIFT)
491 & SAA7191_LUMA_BPSS_MASK;
493 case SAA7191_CONTROL_BANDPASS_WEIGHT:
494 reg &= ~SAA7191_LUMA_APER_MASK;
495 reg |= (ctrl->value << SAA7191_LUMA_APER_SHIFT)
496 & SAA7191_LUMA_APER_MASK;
498 case SAA7191_CONTROL_CORING:
499 reg &= ~SAA7191_LUMA_CORI_MASK;
500 reg |= (ctrl->value << SAA7191_LUMA_CORI_SHIFT)
501 & SAA7191_LUMA_CORI_MASK;
504 ret = saa7191_write_reg(client, SAA7191_REG_LUMA, reg);
506 case SAA7191_CONTROL_FORCE_COLOUR:
507 case SAA7191_CONTROL_CHROMA_GAIN:
508 reg = saa7191_read_reg(client, SAA7191_REG_GAIN);
509 if (ctrl->type == SAA7191_CONTROL_FORCE_COLOUR) {
511 reg |= SAA7191_GAIN_COLO;
513 reg &= ~SAA7191_GAIN_COLO;
515 reg &= ~SAA7191_GAIN_LFIS_MASK;
516 reg |= (ctrl->value << SAA7191_GAIN_LFIS_SHIFT)
517 & SAA7191_GAIN_LFIS_MASK;
519 ret = saa7191_write_reg(client, SAA7191_REG_GAIN, reg);
521 case SAA7191_CONTROL_HUE:
522 reg = ctrl->value & 0xff;
527 ret = saa7191_write_reg(client, SAA7191_REG_HUEC, reg);
529 case SAA7191_CONTROL_VTRC:
530 reg = saa7191_read_reg(client, SAA7191_REG_STDC);
532 reg |= SAA7191_STDC_VTRC;
534 reg &= ~SAA7191_STDC_VTRC;
535 ret = saa7191_write_reg(client, SAA7191_REG_STDC, reg);
537 case SAA7191_CONTROL_LUMA_DELAY: {
538 s32 value = ctrl->value;
541 reg = saa7191_read_reg(client, SAA7191_REG_CTL3);
542 reg &= ~SAA7191_CTL3_YDEL_MASK;
543 reg |= (value << SAA7191_CTL3_YDEL_SHIFT)
544 & SAA7191_CTL3_YDEL_MASK;
545 ret = saa7191_write_reg(client, SAA7191_REG_CTL3, reg);
548 case SAA7191_CONTROL_VNR:
549 reg = saa7191_read_reg(client, SAA7191_REG_CTL4);
550 reg &= ~SAA7191_CTL4_VNOI_MASK;
551 reg |= (ctrl->value << SAA7191_CTL4_VNOI_SHIFT)
552 & SAA7191_CTL4_VNOI_MASK;
553 ret = saa7191_write_reg(client, SAA7191_REG_CTL4, reg);
564 static int saa7191_attach(struct i2c_adapter *adap, int addr, int kind)
567 struct saa7191 *decoder;
568 struct i2c_client *client;
570 printk(KERN_INFO "Philips SAA7191 driver version %s\n",
571 SAA7191_MODULE_VERSION);
573 client = kzalloc(sizeof(*client), GFP_KERNEL);
576 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
579 goto out_free_client;
583 client->adapter = adap;
584 client->driver = &i2c_driver_saa7191;
586 strcpy(client->name, "saa7191 client");
587 i2c_set_clientdata(client, decoder);
589 decoder->client = client;
591 err = i2c_attach_client(client);
593 goto out_free_decoder;
595 err = saa7191_write_block(client, sizeof(initseq), initseq);
597 printk(KERN_ERR "SAA7191 initialization failed\n");
598 goto out_detach_client;
601 printk(KERN_INFO "SAA7191 initialized\n");
603 decoder->input = SAA7191_INPUT_COMPOSITE;
604 decoder->norm = SAA7191_NORM_PAL;
606 err = saa7191_autodetect_norm(client);
607 if (err && (err != -EBUSY)) {
608 printk(KERN_ERR "SAA7191: Signal auto-detection failed\n");
614 i2c_detach_client(client);
622 static int saa7191_probe(struct i2c_adapter *adap)
624 /* Always connected to VINO */
625 if (adap->id == I2C_HW_SGI_VINO)
626 return saa7191_attach(adap, SAA7191_ADDR, 0);
627 /* Feel free to add probe here :-) */
631 static int saa7191_detach(struct i2c_client *client)
633 struct saa7191 *decoder = i2c_get_clientdata(client);
635 i2c_detach_client(client);
641 static int saa7191_command(struct i2c_client *client, unsigned int cmd,
644 struct saa7191 *decoder = i2c_get_clientdata(client);
647 case DECODER_GET_CAPABILITIES: {
648 struct video_decoder_capability *cap = arg;
650 cap->flags = VIDEO_DECODER_PAL | VIDEO_DECODER_NTSC |
651 VIDEO_DECODER_SECAM | VIDEO_DECODER_AUTO;
652 cap->inputs = (client->adapter->id == I2C_HW_SGI_VINO) ? 2 : 1;
656 case DECODER_GET_STATUS: {
661 if (saa7191_read_status(client, &status)) {
664 if ((status & SAA7191_STATUS_HLCK) == 0)
665 res |= DECODER_STATUS_GOOD;
666 if (status & SAA7191_STATUS_CODE)
667 res |= DECODER_STATUS_COLOR;
668 switch (decoder->norm) {
669 case SAA7191_NORM_NTSC:
670 res |= DECODER_STATUS_NTSC;
672 case SAA7191_NORM_PAL:
673 res |= DECODER_STATUS_PAL;
675 case SAA7191_NORM_SECAM:
676 res |= DECODER_STATUS_SECAM;
678 case SAA7191_NORM_AUTO:
680 if (status & SAA7191_STATUS_FIDT)
681 res |= DECODER_STATUS_NTSC;
683 res |= DECODER_STATUS_PAL;
689 case DECODER_SET_NORM: {
693 case VIDEO_MODE_AUTO:
694 return saa7191_autodetect_norm(client);
696 return saa7191_set_norm(client, SAA7191_NORM_PAL);
697 case VIDEO_MODE_NTSC:
698 return saa7191_set_norm(client, SAA7191_NORM_NTSC);
699 case VIDEO_MODE_SECAM:
700 return saa7191_set_norm(client, SAA7191_NORM_SECAM);
706 case DECODER_SET_INPUT: {
709 switch (client->adapter->id) {
710 case I2C_HW_SGI_VINO:
711 return saa7191_set_input(client, *iarg);
718 case DECODER_SET_OUTPUT: {
721 /* not much choice of outputs */
726 case DECODER_ENABLE_OUTPUT: {
730 case DECODER_SET_PICTURE: {
731 struct video_picture *pic = arg;
735 val = (pic->hue >> 8) - 0x80;
737 err = saa7191_write_reg(client, SAA7191_REG_HUEC, val);
743 case DECODER_SAA7191_GET_STATUS: {
744 struct saa7191_status *status = arg;
747 if (saa7191_read_status(client, &status_reg))
750 status->signal = ((status_reg & SAA7191_STATUS_HLCK) == 0)
752 status->signal_60hz = (status_reg & SAA7191_STATUS_FIDT)
754 status->color = (status_reg & SAA7191_STATUS_CODE) ? 1 : 0;
756 status->input = decoder->input;
757 status->norm = decoder->norm;
761 case DECODER_SAA7191_SET_NORM: {
765 case SAA7191_NORM_AUTO:
766 return saa7191_autodetect_norm(client);
767 case SAA7191_NORM_AUTO_EXT:
768 return saa7191_autodetect_norm_extended(client);
770 return saa7191_set_norm(client, *norm);
773 case DECODER_SAA7191_GET_CONTROL: {
774 return saa7191_get_control(client, arg);
776 case DECODER_SAA7191_SET_CONTROL: {
777 return saa7191_set_control(client, arg);
786 static struct i2c_driver i2c_driver_saa7191 = {
790 .id = I2C_DRIVERID_SAA7191,
791 .attach_adapter = saa7191_probe,
792 .detach_client = saa7191_detach,
793 .command = saa7191_command
796 static int saa7191_init(void)
798 return i2c_add_driver(&i2c_driver_saa7191);
801 static void saa7191_exit(void)
803 i2c_del_driver(&i2c_driver_saa7191);
806 module_init(saa7191_init);
807 module_exit(saa7191_exit);