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-device.h>
25 #include <media/v4l2-chip-ident.h>
26 #include <media/v4l2-i2c-drv-legacy.h>
30 #define SAA7191_MODULE_VERSION "0.0.5"
32 MODULE_DESCRIPTION("Philips SAA7191 video decoder driver");
33 MODULE_VERSION(SAA7191_MODULE_VERSION);
34 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
35 MODULE_LICENSE("GPL");
37 static unsigned short normal_i2c[] = { 0x8a >> 1, 0x8e >> 1, I2C_CLIENT_END };
41 // #define SAA7191_DEBUG
44 #define dprintk(x...) printk("SAA7191: " x);
49 #define SAA7191_SYNC_COUNT 30
50 #define SAA7191_SYNC_DELAY 100 /* milliseconds */
53 struct v4l2_subdev sd;
55 /* the register values are stored here as the actual
56 * I2C-registers are write-only */
63 static inline struct saa7191 *to_saa7191(struct v4l2_subdev *sd)
65 return container_of(sd, struct saa7191, sd);
68 static const u8 initseq[] = {
71 0x50, /* (0x50) SAA7191_REG_IDEL */
73 /* 50 Hz signal timing */
74 0x30, /* (0x30) SAA7191_REG_HSYB */
75 0x00, /* (0x00) SAA7191_REG_HSYS */
76 0xe8, /* (0xe8) SAA7191_REG_HCLB */
77 0xb6, /* (0xb6) SAA7191_REG_HCLS */
78 0xf4, /* (0xf4) SAA7191_REG_HPHI */
81 SAA7191_LUMA_APER_1, /* (0x01) SAA7191_REG_LUMA - CVBS mode */
82 0x00, /* (0x00) SAA7191_REG_HUEC */
83 0xf8, /* (0xf8) SAA7191_REG_CKTQ */
84 0xf8, /* (0xf8) SAA7191_REG_CKTS */
85 0x90, /* (0x90) SAA7191_REG_PLSE */
86 0x90, /* (0x90) SAA7191_REG_SESE */
87 0x00, /* (0x00) SAA7191_REG_GAIN */
88 SAA7191_STDC_NFEN | SAA7191_STDC_HRMV, /* (0x0c) SAA7191_REG_STDC
90 * slow time constant */
91 SAA7191_IOCK_OEDC | SAA7191_IOCK_OEHS | SAA7191_IOCK_OEVS
92 | SAA7191_IOCK_OEDY, /* (0x78) SAA7191_REG_IOCK
93 * - chroma from CVBS, GPSW1 & 2 off */
94 SAA7191_CTL3_AUFD | SAA7191_CTL3_SCEN | SAA7191_CTL3_OFTS
95 | SAA7191_CTL3_YDEL0, /* (0x99) SAA7191_REG_CTL3
96 * - automatic field detection */
97 0x00, /* (0x00) SAA7191_REG_CTL4 */
98 0x2c, /* (0x2c) SAA7191_REG_CHCV - PAL nominal value */
102 /* 60 Hz signal timing */
103 0x34, /* (0x34) SAA7191_REG_HS6B */
104 0x0a, /* (0x0a) SAA7191_REG_HS6S */
105 0xf4, /* (0xf4) SAA7191_REG_HC6B */
106 0xce, /* (0xce) SAA7191_REG_HC6S */
107 0xf4, /* (0xf4) SAA7191_REG_HP6I */
110 /* SAA7191 register handling */
112 static u8 saa7191_read_reg(struct v4l2_subdev *sd, u8 reg)
114 return to_saa7191(sd)->reg[reg];
117 static int saa7191_read_status(struct v4l2_subdev *sd, u8 *value)
119 struct i2c_client *client = v4l2_get_subdevdata(sd);
122 ret = i2c_master_recv(client, value, 1);
124 printk(KERN_ERR "SAA7191: saa7191_read_status(): read failed\n");
132 static int saa7191_write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
134 struct i2c_client *client = v4l2_get_subdevdata(sd);
136 to_saa7191(sd)->reg[reg] = value;
137 return i2c_smbus_write_byte_data(client, reg, value);
140 /* the first byte of data must be the first subaddress number (register) */
141 static int saa7191_write_block(struct v4l2_subdev *sd,
142 u8 length, const u8 *data)
144 struct i2c_client *client = v4l2_get_subdevdata(sd);
145 struct saa7191 *decoder = to_saa7191(sd);
149 for (i = 0; i < (length - 1); i++) {
150 decoder->reg[data[0] + i] = data[i + 1];
153 ret = i2c_master_send(client, data, length);
155 printk(KERN_ERR "SAA7191: saa7191_write_block(): "
163 /* Helper functions */
165 static int saa7191_s_routing(struct v4l2_subdev *sd,
166 const struct v4l2_routing *route)
168 struct saa7191 *decoder = to_saa7191(sd);
169 u8 luma = saa7191_read_reg(sd, SAA7191_REG_LUMA);
170 u8 iock = saa7191_read_reg(sd, SAA7191_REG_IOCK);
173 switch (route->input) {
174 case SAA7191_INPUT_COMPOSITE: /* Set Composite input */
175 iock &= ~(SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW1
176 | SAA7191_IOCK_GPSW2);
177 /* Chrominance trap active */
178 luma &= ~SAA7191_LUMA_BYPS;
180 case SAA7191_INPUT_SVIDEO: /* Set S-Video input */
181 iock |= SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW2;
182 /* Chrominance trap bypassed */
183 luma |= SAA7191_LUMA_BYPS;
189 err = saa7191_write_reg(sd, SAA7191_REG_LUMA, luma);
192 err = saa7191_write_reg(sd, SAA7191_REG_IOCK, iock);
196 decoder->input = route->input;
201 static int saa7191_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
203 struct saa7191 *decoder = to_saa7191(sd);
204 u8 stdc = saa7191_read_reg(sd, SAA7191_REG_STDC);
205 u8 ctl3 = saa7191_read_reg(sd, SAA7191_REG_CTL3);
206 u8 chcv = saa7191_read_reg(sd, SAA7191_REG_CHCV);
209 if (norm & V4L2_STD_PAL) {
210 stdc &= ~SAA7191_STDC_SECS;
211 ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
212 chcv = SAA7191_CHCV_PAL;
213 } else if (norm & V4L2_STD_NTSC) {
214 stdc &= ~SAA7191_STDC_SECS;
215 ctl3 &= ~SAA7191_CTL3_AUFD;
216 ctl3 |= SAA7191_CTL3_FSEL;
217 chcv = SAA7191_CHCV_NTSC;
218 } else if (norm & V4L2_STD_SECAM) {
219 stdc |= SAA7191_STDC_SECS;
220 ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
221 chcv = SAA7191_CHCV_PAL;
226 err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
229 err = saa7191_write_reg(sd, SAA7191_REG_STDC, stdc);
232 err = saa7191_write_reg(sd, SAA7191_REG_CHCV, chcv);
236 decoder->norm = norm;
238 dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3,
240 dprintk("norm: %llx\n", norm);
245 static int saa7191_wait_for_signal(struct v4l2_subdev *sd, u8 *status)
249 dprintk("Checking for signal...\n");
251 for (i = 0; i < SAA7191_SYNC_COUNT; i++) {
252 if (saa7191_read_status(sd, status))
255 if (((*status) & SAA7191_STATUS_HLCK) == 0) {
256 dprintk("Signal found\n");
260 msleep(SAA7191_SYNC_DELAY);
263 dprintk("No signal\n");
268 static int saa7191_querystd(struct v4l2_subdev *sd, v4l2_std_id *norm)
270 struct saa7191 *decoder = to_saa7191(sd);
271 u8 stdc = saa7191_read_reg(sd, SAA7191_REG_STDC);
272 u8 ctl3 = saa7191_read_reg(sd, SAA7191_REG_CTL3);
274 v4l2_std_id old_norm = decoder->norm;
277 dprintk("SAA7191 extended signal auto-detection...\n");
279 *norm = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
280 stdc &= ~SAA7191_STDC_SECS;
281 ctl3 &= ~(SAA7191_CTL3_FSEL);
283 err = saa7191_write_reg(sd, SAA7191_REG_STDC, stdc);
288 err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
294 ctl3 |= SAA7191_CTL3_AUFD;
295 err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
301 msleep(SAA7191_SYNC_DELAY);
303 err = saa7191_wait_for_signal(sd, &status);
307 if (status & SAA7191_STATUS_FIDT) {
308 /* 60Hz signal -> NTSC */
309 dprintk("60Hz signal: NTSC\n");
310 *norm = V4L2_STD_NTSC;
315 dprintk("50Hz signal: Trying PAL...\n");
318 err = saa7191_s_std(sd, V4L2_STD_PAL);
322 msleep(SAA7191_SYNC_DELAY);
324 err = saa7191_wait_for_signal(sd, &status);
329 if (status & SAA7191_STATUS_FIDT) {
330 dprintk("No 50Hz signal\n");
331 saa7191_s_std(sd, old_norm);
335 if (status & SAA7191_STATUS_CODE) {
337 *norm = V4L2_STD_PAL;
338 return saa7191_s_std(sd, old_norm);
341 dprintk("No color detected with PAL - Trying SECAM...\n");
343 /* no color detected ? -> try SECAM */
344 err = saa7191_s_std(sd, V4L2_STD_SECAM);
348 msleep(SAA7191_SYNC_DELAY);
350 err = saa7191_wait_for_signal(sd, &status);
355 if (status & SAA7191_STATUS_FIDT) {
356 dprintk("No 50Hz signal\n");
361 if (status & SAA7191_STATUS_CODE) {
362 /* Color detected -> SECAM */
364 *norm = V4L2_STD_SECAM;
365 return saa7191_s_std(sd, old_norm);
368 dprintk("No color detected with SECAM - Going back to PAL.\n");
371 return saa7191_s_std(sd, old_norm);
374 static int saa7191_autodetect_norm(struct v4l2_subdev *sd)
378 dprintk("SAA7191 signal auto-detection...\n");
380 dprintk("Reading status...\n");
382 if (saa7191_read_status(sd, &status))
385 dprintk("Checking for signal...\n");
388 if (status & SAA7191_STATUS_HLCK) {
389 dprintk("No signal\n");
393 dprintk("Signal found\n");
395 if (status & SAA7191_STATUS_FIDT) {
396 /* 60hz signal -> NTSC */
398 return saa7191_s_std(sd, V4L2_STD_NTSC);
400 /* 50hz signal -> PAL */
402 return saa7191_s_std(sd, V4L2_STD_PAL);
406 static int saa7191_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
412 case SAA7191_CONTROL_BANDPASS:
413 case SAA7191_CONTROL_BANDPASS_WEIGHT:
414 case SAA7191_CONTROL_CORING:
415 reg = saa7191_read_reg(sd, SAA7191_REG_LUMA);
417 case SAA7191_CONTROL_BANDPASS:
418 ctrl->value = ((s32)reg & SAA7191_LUMA_BPSS_MASK)
419 >> SAA7191_LUMA_BPSS_SHIFT;
421 case SAA7191_CONTROL_BANDPASS_WEIGHT:
422 ctrl->value = ((s32)reg & SAA7191_LUMA_APER_MASK)
423 >> SAA7191_LUMA_APER_SHIFT;
425 case SAA7191_CONTROL_CORING:
426 ctrl->value = ((s32)reg & SAA7191_LUMA_CORI_MASK)
427 >> SAA7191_LUMA_CORI_SHIFT;
431 case SAA7191_CONTROL_FORCE_COLOUR:
432 case SAA7191_CONTROL_CHROMA_GAIN:
433 reg = saa7191_read_reg(sd, SAA7191_REG_GAIN);
434 if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR)
435 ctrl->value = ((s32)reg & SAA7191_GAIN_COLO) ? 1 : 0;
437 ctrl->value = ((s32)reg & SAA7191_GAIN_LFIS_MASK)
438 >> SAA7191_GAIN_LFIS_SHIFT;
441 reg = saa7191_read_reg(sd, SAA7191_REG_HUEC);
446 ctrl->value = (s32)reg;
448 case SAA7191_CONTROL_VTRC:
449 reg = saa7191_read_reg(sd, SAA7191_REG_STDC);
450 ctrl->value = ((s32)reg & SAA7191_STDC_VTRC) ? 1 : 0;
452 case SAA7191_CONTROL_LUMA_DELAY:
453 reg = saa7191_read_reg(sd, SAA7191_REG_CTL3);
454 ctrl->value = ((s32)reg & SAA7191_CTL3_YDEL_MASK)
455 >> SAA7191_CTL3_YDEL_SHIFT;
456 if (ctrl->value >= 4)
459 case SAA7191_CONTROL_VNR:
460 reg = saa7191_read_reg(sd, SAA7191_REG_CTL4);
461 ctrl->value = ((s32)reg & SAA7191_CTL4_VNOI_MASK)
462 >> SAA7191_CTL4_VNOI_SHIFT;
471 static int saa7191_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
477 case SAA7191_CONTROL_BANDPASS:
478 case SAA7191_CONTROL_BANDPASS_WEIGHT:
479 case SAA7191_CONTROL_CORING:
480 reg = saa7191_read_reg(sd, SAA7191_REG_LUMA);
482 case SAA7191_CONTROL_BANDPASS:
483 reg &= ~SAA7191_LUMA_BPSS_MASK;
484 reg |= (ctrl->value << SAA7191_LUMA_BPSS_SHIFT)
485 & SAA7191_LUMA_BPSS_MASK;
487 case SAA7191_CONTROL_BANDPASS_WEIGHT:
488 reg &= ~SAA7191_LUMA_APER_MASK;
489 reg |= (ctrl->value << SAA7191_LUMA_APER_SHIFT)
490 & SAA7191_LUMA_APER_MASK;
492 case SAA7191_CONTROL_CORING:
493 reg &= ~SAA7191_LUMA_CORI_MASK;
494 reg |= (ctrl->value << SAA7191_LUMA_CORI_SHIFT)
495 & SAA7191_LUMA_CORI_MASK;
498 ret = saa7191_write_reg(sd, SAA7191_REG_LUMA, reg);
500 case SAA7191_CONTROL_FORCE_COLOUR:
501 case SAA7191_CONTROL_CHROMA_GAIN:
502 reg = saa7191_read_reg(sd, SAA7191_REG_GAIN);
503 if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR) {
505 reg |= SAA7191_GAIN_COLO;
507 reg &= ~SAA7191_GAIN_COLO;
509 reg &= ~SAA7191_GAIN_LFIS_MASK;
510 reg |= (ctrl->value << SAA7191_GAIN_LFIS_SHIFT)
511 & SAA7191_GAIN_LFIS_MASK;
513 ret = saa7191_write_reg(sd, SAA7191_REG_GAIN, reg);
516 reg = ctrl->value & 0xff;
521 ret = saa7191_write_reg(sd, SAA7191_REG_HUEC, reg);
523 case SAA7191_CONTROL_VTRC:
524 reg = saa7191_read_reg(sd, SAA7191_REG_STDC);
526 reg |= SAA7191_STDC_VTRC;
528 reg &= ~SAA7191_STDC_VTRC;
529 ret = saa7191_write_reg(sd, SAA7191_REG_STDC, reg);
531 case SAA7191_CONTROL_LUMA_DELAY: {
532 s32 value = ctrl->value;
535 reg = saa7191_read_reg(sd, SAA7191_REG_CTL3);
536 reg &= ~SAA7191_CTL3_YDEL_MASK;
537 reg |= (value << SAA7191_CTL3_YDEL_SHIFT)
538 & SAA7191_CTL3_YDEL_MASK;
539 ret = saa7191_write_reg(sd, SAA7191_REG_CTL3, reg);
542 case SAA7191_CONTROL_VNR:
543 reg = saa7191_read_reg(sd, SAA7191_REG_CTL4);
544 reg &= ~SAA7191_CTL4_VNOI_MASK;
545 reg |= (ctrl->value << SAA7191_CTL4_VNOI_SHIFT)
546 & SAA7191_CTL4_VNOI_MASK;
547 ret = saa7191_write_reg(sd, SAA7191_REG_CTL4, reg);
558 static int saa7191_g_input_status(struct v4l2_subdev *sd, u32 *status)
561 int res = V4L2_IN_ST_NO_SIGNAL;
563 if (saa7191_read_status(sd, &status_reg))
565 if ((status_reg & SAA7191_STATUS_HLCK) == 0)
567 if (!(status_reg & SAA7191_STATUS_CODE))
568 res |= V4L2_IN_ST_NO_COLOR;
574 static int saa7191_g_chip_ident(struct v4l2_subdev *sd,
575 struct v4l2_dbg_chip_ident *chip)
577 struct i2c_client *client = v4l2_get_subdevdata(sd);
579 return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7191, 0);
582 static int saa7191_command(struct i2c_client *client, unsigned cmd, void *arg)
584 return v4l2_subdev_command(i2c_get_clientdata(client), cmd, arg);
587 /* ----------------------------------------------------------------------- */
589 static const struct v4l2_subdev_core_ops saa7191_core_ops = {
590 .g_chip_ident = saa7191_g_chip_ident,
591 .g_ctrl = saa7191_g_ctrl,
592 .s_ctrl = saa7191_s_ctrl,
595 static const struct v4l2_subdev_tuner_ops saa7191_tuner_ops = {
596 .s_std = saa7191_s_std,
599 static const struct v4l2_subdev_video_ops saa7191_video_ops = {
600 .s_routing = saa7191_s_routing,
601 .querystd = saa7191_querystd,
602 .g_input_status = saa7191_g_input_status,
605 static const struct v4l2_subdev_ops saa7191_ops = {
606 .core = &saa7191_core_ops,
607 .video = &saa7191_video_ops,
610 static int saa7191_probe(struct i2c_client *client,
611 const struct i2c_device_id *id)
614 struct saa7191 *decoder;
615 struct v4l2_subdev *sd;
617 v4l_info(client, "chip found @ 0x%x (%s)\n",
618 client->addr << 1, client->adapter->name);
620 decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
625 v4l2_i2c_subdev_init(sd, client, &saa7191_ops);
627 err = saa7191_write_block(sd, sizeof(initseq), initseq);
629 printk(KERN_ERR "SAA7191 initialization failed\n");
634 printk(KERN_INFO "SAA7191 initialized\n");
636 decoder->input = SAA7191_INPUT_COMPOSITE;
637 decoder->norm = V4L2_STD_PAL;
639 err = saa7191_autodetect_norm(sd);
640 if (err && (err != -EBUSY))
641 printk(KERN_ERR "SAA7191: Signal auto-detection failed\n");
646 static int saa7191_remove(struct i2c_client *client)
648 struct v4l2_subdev *sd = i2c_get_clientdata(client);
650 v4l2_device_unregister_subdev(sd);
651 kfree(to_saa7191(sd));
655 static int saa7191_legacy_probe(struct i2c_adapter *adapter)
657 return adapter->id == I2C_HW_SGI_VINO;
660 static const struct i2c_device_id saa7191_id[] = {
664 MODULE_DEVICE_TABLE(i2c, saa7191_id);
666 static struct v4l2_i2c_driver_data v4l2_i2c_data = {
668 .driverid = I2C_DRIVERID_SAA7191,
669 .command = saa7191_command,
670 .probe = saa7191_probe,
671 .remove = saa7191_remove,
672 .legacy_probe = saa7191_legacy_probe,
673 .id_table = saa7191_id,