saa7191.c 15.5 KB
Newer Older
R
Ralf Baechle 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  saa7191.c - Philips SAA7191 video decoder driver
 *
 *  Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
 *  Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/fs.h>
15
#include <linux/init.h>
R
Ralf Baechle 已提交
16 17
#include <linux/kernel.h>
#include <linux/major.h>
18
#include <linux/module.h>
R
Ralf Baechle 已提交
19
#include <linux/mm.h>
20
#include <linux/slab.h>
R
Ralf Baechle 已提交
21

22
#include <linux/videodev2.h>
R
Ralf Baechle 已提交
23
#include <linux/i2c.h>
24 25
#include <media/v4l2-common.h>
#include <media/v4l2-i2c-drv-legacy.h>
R
Ralf Baechle 已提交
26 27 28

#include "saa7191.h"

L
Ladislav Michl 已提交
29
#define SAA7191_MODULE_VERSION	"0.0.5"
R
Ralf Baechle 已提交
30 31 32 33 34 35

MODULE_DESCRIPTION("Philips SAA7191 video decoder driver");
MODULE_VERSION(SAA7191_MODULE_VERSION);
MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
MODULE_LICENSE("GPL");

36 37 38 39
static unsigned short normal_i2c[] = { 0x8a >> 1, 0x8e >> 1, I2C_CLIENT_END };

I2C_CLIENT_INSMOD;

L
Ladislav Michl 已提交
40 41 42 43 44 45 46 47 48 49 50
// #define SAA7191_DEBUG

#ifdef SAA7191_DEBUG
#define dprintk(x...) printk("SAA7191: " x);
#else
#define dprintk(x...)
#endif

#define SAA7191_SYNC_COUNT	30
#define SAA7191_SYNC_DELAY	100	/* milliseconds */

R
Ralf Baechle 已提交
51 52 53 54 55
struct saa7191 {
	struct i2c_client *client;

	/* the register values are stored here as the actual
	 * I2C-registers are write-only */
L
Ladislav Michl 已提交
56
	u8 reg[25];
R
Ralf Baechle 已提交
57

L
Ladislav Michl 已提交
58
	int input;
59
	v4l2_std_id norm;
R
Ralf Baechle 已提交
60 61
};

L
Ladislav Michl 已提交
62
static const u8 initseq[] = {
R
Ralf Baechle 已提交
63
	0,	/* Subaddress */
L
Ladislav Michl 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

	0x50,	/* (0x50) SAA7191_REG_IDEL */

	/* 50 Hz signal timing */
	0x30,	/* (0x30) SAA7191_REG_HSYB */
	0x00,	/* (0x00) SAA7191_REG_HSYS */
	0xe8,	/* (0xe8) SAA7191_REG_HCLB */
	0xb6,	/* (0xb6) SAA7191_REG_HCLS */
	0xf4,	/* (0xf4) SAA7191_REG_HPHI */

	/* control */
	SAA7191_LUMA_APER_1,	/* (0x01) SAA7191_REG_LUMA - CVBS mode */
	0x00,	/* (0x00) SAA7191_REG_HUEC */
	0xf8,	/* (0xf8) SAA7191_REG_CKTQ */
	0xf8,	/* (0xf8) SAA7191_REG_CKTS */
	0x90,	/* (0x90) SAA7191_REG_PLSE */
	0x90,	/* (0x90) SAA7191_REG_SESE */
	0x00,	/* (0x00) SAA7191_REG_GAIN */
	SAA7191_STDC_NFEN | SAA7191_STDC_HRMV,	/* (0x0c) SAA7191_REG_STDC
						 * - not SECAM,
						 * slow time constant */
	SAA7191_IOCK_OEDC | SAA7191_IOCK_OEHS | SAA7191_IOCK_OEVS
	| SAA7191_IOCK_OEDY,	/* (0x78) SAA7191_REG_IOCK
				 * - chroma from CVBS, GPSW1 & 2 off */
	SAA7191_CTL3_AUFD | SAA7191_CTL3_SCEN | SAA7191_CTL3_OFTS
	| SAA7191_CTL3_YDEL0,	/* (0x99) SAA7191_REG_CTL3
				 * - automatic field detection */
	0x00,	/* (0x00) SAA7191_REG_CTL4 */
	0x2c,	/* (0x2c) SAA7191_REG_CHCV - PAL nominal value */
R
Ralf Baechle 已提交
93 94
	0x00,	/* unused */
	0x00,	/* unused */
L
Ladislav Michl 已提交
95 96 97 98 99 100 101

	/* 60 Hz signal timing */
	0x34,	/* (0x34) SAA7191_REG_HS6B */
	0x0a,	/* (0x0a) SAA7191_REG_HS6S */
	0xf4,	/* (0xf4) SAA7191_REG_HC6B */
	0xce,	/* (0xce) SAA7191_REG_HC6S */
	0xf4,	/* (0xf4) SAA7191_REG_HP6I */
R
Ralf Baechle 已提交
102 103 104 105
};

/* SAA7191 register handling */

L
Ladislav Michl 已提交
106 107
static u8 saa7191_read_reg(struct i2c_client *client,
			   u8 reg)
R
Ralf Baechle 已提交
108 109 110 111 112
{
	return ((struct saa7191 *)i2c_get_clientdata(client))->reg[reg];
}

static int saa7191_read_status(struct i2c_client *client,
L
Ladislav Michl 已提交
113
			       u8 *value)
R
Ralf Baechle 已提交
114 115 116 117 118
{
	int ret;

	ret = i2c_master_recv(client, value, 1);
	if (ret < 0) {
L
Ladislav Michl 已提交
119
		printk(KERN_ERR "SAA7191: saa7191_read_status(): read failed\n");
R
Ralf Baechle 已提交
120 121 122 123 124 125 126
		return ret;
	}

	return 0;
}


L
Ladislav Michl 已提交
127 128
static int saa7191_write_reg(struct i2c_client *client, u8 reg,
			     u8 value)
R
Ralf Baechle 已提交
129 130 131 132 133 134 135
{
	((struct saa7191 *)i2c_get_clientdata(client))->reg[reg] = value;
	return i2c_smbus_write_byte_data(client, reg, value);
}

/* the first byte of data must be the first subaddress number (register) */
static int saa7191_write_block(struct i2c_client *client,
136
			       u8 length, const u8 *data)
R
Ralf Baechle 已提交
137 138 139 140 141 142 143 144 145 146 147 148
{
	int i;
	int ret;

	struct saa7191 *decoder = (struct saa7191 *)i2c_get_clientdata(client);
	for (i = 0; i < (length - 1); i++) {
		decoder->reg[data[0] + i] = data[i + 1];
	}

	ret = i2c_master_send(client, data, length);
	if (ret < 0) {
		printk(KERN_ERR "SAA7191: saa7191_write_block(): "
L
Ladislav Michl 已提交
149
		       "write failed\n");
R
Ralf Baechle 已提交
150 151 152 153 154 155 156 157 158 159
		return ret;
	}

	return 0;
}

/* Helper functions */

static int saa7191_set_input(struct i2c_client *client, int input)
{
L
Ladislav Michl 已提交
160 161 162
	struct saa7191 *decoder = i2c_get_clientdata(client);
	u8 luma = saa7191_read_reg(client, SAA7191_REG_LUMA);
	u8 iock = saa7191_read_reg(client, SAA7191_REG_IOCK);
R
Ralf Baechle 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	int err;

	switch (input) {
	case SAA7191_INPUT_COMPOSITE: /* Set Composite input */
		iock &= ~(SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW1
			  | SAA7191_IOCK_GPSW2);
		/* Chrominance trap active */
		luma &= ~SAA7191_LUMA_BYPS;
		break;
	case SAA7191_INPUT_SVIDEO: /* Set S-Video input */
		iock |= SAA7191_IOCK_CHRS | SAA7191_IOCK_GPSW2;
		/* Chrominance trap bypassed */
		luma |= SAA7191_LUMA_BYPS;
		break;
	default:
		return -EINVAL;
	}

	err = saa7191_write_reg(client, SAA7191_REG_LUMA, luma);
	if (err)
		return -EIO;
	err = saa7191_write_reg(client, SAA7191_REG_IOCK, iock);
	if (err)
		return -EIO;

L
Ladislav Michl 已提交
188 189
	decoder->input = input;

R
Ralf Baechle 已提交
190 191 192
	return 0;
}

193
static int saa7191_set_norm(struct i2c_client *client, v4l2_std_id norm)
R
Ralf Baechle 已提交
194 195
{
	struct saa7191 *decoder = i2c_get_clientdata(client);
L
Ladislav Michl 已提交
196 197 198
	u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC);
	u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3);
	u8 chcv = saa7191_read_reg(client, SAA7191_REG_CHCV);
R
Ralf Baechle 已提交
199 200
	int err;

201
	if (norm & V4L2_STD_PAL) {
R
Ralf Baechle 已提交
202 203 204
		stdc &= ~SAA7191_STDC_SECS;
		ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
		chcv = SAA7191_CHCV_PAL;
205
	} else if (norm & V4L2_STD_NTSC) {
R
Ralf Baechle 已提交
206 207 208 209
		stdc &= ~SAA7191_STDC_SECS;
		ctl3 &= ~SAA7191_CTL3_AUFD;
		ctl3 |= SAA7191_CTL3_FSEL;
		chcv = SAA7191_CHCV_NTSC;
210
	} else if (norm & V4L2_STD_SECAM) {
R
Ralf Baechle 已提交
211 212 213
		stdc |= SAA7191_STDC_SECS;
		ctl3 &= ~(SAA7191_CTL3_AUFD | SAA7191_CTL3_FSEL);
		chcv = SAA7191_CHCV_PAL;
214
	} else {
R
Ralf Baechle 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		return -EINVAL;
	}

	err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
	if (err)
		return -EIO;
	err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc);
	if (err)
		return -EIO;
	err = saa7191_write_reg(client, SAA7191_REG_CHCV, chcv);
	if (err)
		return -EIO;

	decoder->norm = norm;

L
Ladislav Michl 已提交
230 231
	dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3,
		stdc, chcv);
232
	dprintk("norm: %llx\n", norm);
L
Ladislav Michl 已提交
233

R
Ralf Baechle 已提交
234 235 236
	return 0;
}

L
Ladislav Michl 已提交
237
static int saa7191_wait_for_signal(struct i2c_client *client, u8 *status)
R
Ralf Baechle 已提交
238
{
L
Ladislav Michl 已提交
239
	int i = 0;
R
Ralf Baechle 已提交
240

L
Ladislav Michl 已提交
241 242 243 244 245 246 247 248 249 250 251 252
	dprintk("Checking for signal...\n");

	for (i = 0; i < SAA7191_SYNC_COUNT; i++) {
		if (saa7191_read_status(client, status))
			return -EIO;

		if (((*status) & SAA7191_STATUS_HLCK) == 0) {
			dprintk("Signal found\n");
			return 0;
		}

		msleep(SAA7191_SYNC_DELAY);
R
Ralf Baechle 已提交
253 254
	}

L
Ladislav Michl 已提交
255
	dprintk("No signal\n");
R
Ralf Baechle 已提交
256

L
Ladislav Michl 已提交
257
	return -EBUSY;
R
Ralf Baechle 已提交
258 259
}

260 261
static int saa7191_autodetect_norm_extended(struct i2c_client *client,
		v4l2_std_id *norm)
R
Ralf Baechle 已提交
262
{
263
	struct saa7191 *decoder = i2c_get_clientdata(client);
L
Ladislav Michl 已提交
264 265 266
	u8 stdc = saa7191_read_reg(client, SAA7191_REG_STDC);
	u8 ctl3 = saa7191_read_reg(client, SAA7191_REG_CTL3);
	u8 status;
267
	v4l2_std_id old_norm = decoder->norm;
L
Ladislav Michl 已提交
268
	int err = 0;
R
Ralf Baechle 已提交
269

L
Ladislav Michl 已提交
270 271
	dprintk("SAA7191 extended signal auto-detection...\n");

272
	*norm = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
L
Ladislav Michl 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	stdc &= ~SAA7191_STDC_SECS;
	ctl3 &= ~(SAA7191_CTL3_FSEL);

	err = saa7191_write_reg(client, SAA7191_REG_STDC, stdc);
	if (err) {
		err = -EIO;
		goto out;
	}
	err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
	if (err) {
		err = -EIO;
		goto out;
	}

	ctl3 |= SAA7191_CTL3_AUFD;
	err = saa7191_write_reg(client, SAA7191_REG_CTL3, ctl3);
	if (err) {
		err = -EIO;
		goto out;
	}

	msleep(SAA7191_SYNC_DELAY);

	err = saa7191_wait_for_signal(client, &status);
	if (err)
		goto out;

	if (status & SAA7191_STATUS_FIDT) {
		/* 60Hz signal -> NTSC */
		dprintk("60Hz signal: NTSC\n");
303 304
		*norm = V4L2_STD_NTSC;
		return 0;
L
Ladislav Michl 已提交
305 306 307 308 309 310
	}

	/* 50Hz signal */
	dprintk("50Hz signal: Trying PAL...\n");

	/* try PAL first */
311
	err = saa7191_set_norm(client, V4L2_STD_PAL);
L
Ladislav Michl 已提交
312 313 314 315 316 317 318 319 320 321 322 323
	if (err)
		goto out;

	msleep(SAA7191_SYNC_DELAY);

	err = saa7191_wait_for_signal(client, &status);
	if (err)
		goto out;

	/* not 50Hz ? */
	if (status & SAA7191_STATUS_FIDT) {
		dprintk("No 50Hz signal\n");
324 325
		saa7191_set_norm(client, old_norm);
		return -EAGAIN;
L
Ladislav Michl 已提交
326 327 328 329
	}

	if (status & SAA7191_STATUS_CODE) {
		dprintk("PAL\n");
330 331
		*norm = V4L2_STD_PAL;
		return saa7191_set_norm(client, old_norm);
L
Ladislav Michl 已提交
332 333 334 335 336
	}

	dprintk("No color detected with PAL - Trying SECAM...\n");

	/* no color detected ? -> try SECAM */
337
	err = saa7191_set_norm(client, V4L2_STD_SECAM);
L
Ladislav Michl 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	if (err)
		goto out;

	msleep(SAA7191_SYNC_DELAY);

	err = saa7191_wait_for_signal(client, &status);
	if (err)
		goto out;

	/* not 50Hz ? */
	if (status & SAA7191_STATUS_FIDT) {
		dprintk("No 50Hz signal\n");
		err = -EAGAIN;
		goto out;
	}

	if (status & SAA7191_STATUS_CODE) {
		/* Color detected -> SECAM */
		dprintk("SECAM\n");
357 358
		*norm = V4L2_STD_SECAM;
		return saa7191_set_norm(client, old_norm);
L
Ladislav Michl 已提交
359 360 361 362 363
	}

	dprintk("No color detected with SECAM - Going back to PAL.\n");

out:
364
	return saa7191_set_norm(client, old_norm);
L
Ladislav Michl 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
}

static int saa7191_autodetect_norm(struct i2c_client *client)
{
	u8 status;

	dprintk("SAA7191 signal auto-detection...\n");

	dprintk("Reading status...\n");

	if (saa7191_read_status(client, &status))
		return -EIO;

	dprintk("Checking for signal...\n");

	/* no signal ? */
	if (status & SAA7191_STATUS_HLCK) {
		dprintk("No signal\n");
		return -EBUSY;
	}

	dprintk("Signal found\n");

	if (status & SAA7191_STATUS_FIDT) {
		/* 60hz signal -> NTSC */
		dprintk("NTSC\n");
391
		return saa7191_set_norm(client, V4L2_STD_NTSC);
L
Ladislav Michl 已提交
392 393 394
	} else {
		/* 50hz signal -> PAL */
		dprintk("PAL\n");
395
		return saa7191_set_norm(client, V4L2_STD_PAL);
L
Ladislav Michl 已提交
396 397 398 399
	}
}

static int saa7191_get_control(struct i2c_client *client,
400
			       struct v4l2_control *ctrl)
L
Ladislav Michl 已提交
401 402 403 404
{
	u8 reg;
	int ret = 0;

405
	switch (ctrl->id) {
L
Ladislav Michl 已提交
406 407 408 409
	case SAA7191_CONTROL_BANDPASS:
	case SAA7191_CONTROL_BANDPASS_WEIGHT:
	case SAA7191_CONTROL_CORING:
		reg = saa7191_read_reg(client, SAA7191_REG_LUMA);
410
		switch (ctrl->id) {
L
Ladislav Michl 已提交
411 412 413 414 415 416 417 418 419 420 421 422
		case SAA7191_CONTROL_BANDPASS:
			ctrl->value = ((s32)reg & SAA7191_LUMA_BPSS_MASK)
				>> SAA7191_LUMA_BPSS_SHIFT;
			break;
		case SAA7191_CONTROL_BANDPASS_WEIGHT:
			ctrl->value = ((s32)reg & SAA7191_LUMA_APER_MASK)
				>> SAA7191_LUMA_APER_SHIFT;
			break;
		case SAA7191_CONTROL_CORING:
			ctrl->value = ((s32)reg & SAA7191_LUMA_CORI_MASK)
				>> SAA7191_LUMA_CORI_SHIFT;
			break;
R
Ralf Baechle 已提交
423
		}
L
Ladislav Michl 已提交
424 425 426 427
		break;
	case SAA7191_CONTROL_FORCE_COLOUR:
	case SAA7191_CONTROL_CHROMA_GAIN:
		reg = saa7191_read_reg(client, SAA7191_REG_GAIN);
428
		if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR)
L
Ladislav Michl 已提交
429 430 431 432 433
			ctrl->value = ((s32)reg & SAA7191_GAIN_COLO) ? 1 : 0;
		else
			ctrl->value = ((s32)reg & SAA7191_GAIN_LFIS_MASK)
				>> SAA7191_GAIN_LFIS_SHIFT;
		break;
434
	case V4L2_CID_HUE:
L
Ladislav Michl 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
		reg = saa7191_read_reg(client, SAA7191_REG_HUEC);
		if (reg < 0x80)
			reg += 0x80;
		else
			reg -= 0x80;
		ctrl->value = (s32)reg;
		break;
	case SAA7191_CONTROL_VTRC:
		reg = saa7191_read_reg(client, SAA7191_REG_STDC);
		ctrl->value = ((s32)reg & SAA7191_STDC_VTRC) ? 1 : 0;
		break;
	case SAA7191_CONTROL_LUMA_DELAY:
		reg = saa7191_read_reg(client, SAA7191_REG_CTL3);
		ctrl->value = ((s32)reg & SAA7191_CTL3_YDEL_MASK)
			>> SAA7191_CTL3_YDEL_SHIFT;
		if (ctrl->value >= 4)
			ctrl->value -= 8;
		break;
	case SAA7191_CONTROL_VNR:
		reg = saa7191_read_reg(client, SAA7191_REG_CTL4);
		ctrl->value = ((s32)reg & SAA7191_CTL4_VNOI_MASK)
			>> SAA7191_CTL4_VNOI_SHIFT;
		break;
	default:
		ret = -EINVAL;
	}
R
Ralf Baechle 已提交
461

L
Ladislav Michl 已提交
462 463 464 465
	return ret;
}

static int saa7191_set_control(struct i2c_client *client,
466
			       struct v4l2_control *ctrl)
L
Ladislav Michl 已提交
467 468 469 470
{
	u8 reg;
	int ret = 0;

471
	switch (ctrl->id) {
L
Ladislav Michl 已提交
472 473 474 475
	case SAA7191_CONTROL_BANDPASS:
	case SAA7191_CONTROL_BANDPASS_WEIGHT:
	case SAA7191_CONTROL_CORING:
		reg = saa7191_read_reg(client, SAA7191_REG_LUMA);
476
		switch (ctrl->id) {
L
Ladislav Michl 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
		case SAA7191_CONTROL_BANDPASS:
			reg &= ~SAA7191_LUMA_BPSS_MASK;
			reg |= (ctrl->value << SAA7191_LUMA_BPSS_SHIFT)
				& SAA7191_LUMA_BPSS_MASK;
			break;
		case SAA7191_CONTROL_BANDPASS_WEIGHT:
			reg &= ~SAA7191_LUMA_APER_MASK;
			reg |= (ctrl->value << SAA7191_LUMA_APER_SHIFT)
				& SAA7191_LUMA_APER_MASK;
			break;
		case SAA7191_CONTROL_CORING:
			reg &= ~SAA7191_LUMA_CORI_MASK;
			reg |= (ctrl->value << SAA7191_LUMA_CORI_SHIFT)
				& SAA7191_LUMA_CORI_MASK;
			break;
		}
		ret = saa7191_write_reg(client, SAA7191_REG_LUMA, reg);
		break;
	case SAA7191_CONTROL_FORCE_COLOUR:
	case SAA7191_CONTROL_CHROMA_GAIN:
		reg = saa7191_read_reg(client, SAA7191_REG_GAIN);
498
		if (ctrl->id == SAA7191_CONTROL_FORCE_COLOUR) {
L
Ladislav Michl 已提交
499 500 501 502 503 504 505 506 507 508 509
			if (ctrl->value)
				reg |= SAA7191_GAIN_COLO;
			else
				reg &= ~SAA7191_GAIN_COLO;
		} else {
			reg &= ~SAA7191_GAIN_LFIS_MASK;
			reg |= (ctrl->value << SAA7191_GAIN_LFIS_SHIFT)
				& SAA7191_GAIN_LFIS_MASK;
		}
		ret = saa7191_write_reg(client, SAA7191_REG_GAIN, reg);
		break;
510
	case V4L2_CID_HUE:
L
Ladislav Michl 已提交
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
		reg = ctrl->value & 0xff;
		if (reg < 0x80)
			reg += 0x80;
		else
			reg -= 0x80;
		ret = saa7191_write_reg(client, SAA7191_REG_HUEC, reg);
		break;
	case SAA7191_CONTROL_VTRC:
		reg = saa7191_read_reg(client, SAA7191_REG_STDC);
		if (ctrl->value)
			reg |= SAA7191_STDC_VTRC;
		else
			reg &= ~SAA7191_STDC_VTRC;
		ret = saa7191_write_reg(client, SAA7191_REG_STDC, reg);
		break;
	case SAA7191_CONTROL_LUMA_DELAY: {
		s32 value = ctrl->value;
		if (value < 0)
			value += 8;
		reg = saa7191_read_reg(client, SAA7191_REG_CTL3);
		reg &= ~SAA7191_CTL3_YDEL_MASK;
		reg |= (value << SAA7191_CTL3_YDEL_SHIFT)
			& SAA7191_CTL3_YDEL_MASK;
		ret = saa7191_write_reg(client, SAA7191_REG_CTL3, reg);
		break;
	}
	case SAA7191_CONTROL_VNR:
		reg = saa7191_read_reg(client, SAA7191_REG_CTL4);
		reg &= ~SAA7191_CTL4_VNOI_MASK;
		reg |= (ctrl->value << SAA7191_CTL4_VNOI_SHIFT)
			& SAA7191_CTL4_VNOI_MASK;
		ret = saa7191_write_reg(client, SAA7191_REG_CTL4, reg);
		break;
	default:
		ret = -EINVAL;
R
Ralf Baechle 已提交
546 547
	}

L
Ladislav Michl 已提交
548
	return ret;
R
Ralf Baechle 已提交
549 550 551 552 553 554 555 556
}

/* I2C-interface */

static int saa7191_command(struct i2c_client *client, unsigned int cmd,
			   void *arg)
{
	switch (cmd) {
557 558
	case VIDIOC_INT_G_INPUT_STATUS: {
		u32 *iarg = arg;
L
Ladislav Michl 已提交
559
		u8 status;
560
		int res = V4L2_IN_ST_NO_SIGNAL;
R
Ralf Baechle 已提交
561

562
		if (saa7191_read_status(client, &status))
R
Ralf Baechle 已提交
563 564
			return -EIO;
		if ((status & SAA7191_STATUS_HLCK) == 0)
565 566 567
			res = 0;
		if (!(status & SAA7191_STATUS_CODE))
			res |= V4L2_IN_ST_NO_COLOR;
R
Ralf Baechle 已提交
568 569 570 571
		*iarg = res;
		break;
	}

572 573
	case VIDIOC_QUERYSTD:
		return saa7191_autodetect_norm_extended(client, arg);
L
Ladislav Michl 已提交
574

575 576
	case VIDIOC_S_STD: {
		v4l2_std_id *istd = arg;
L
Ladislav Michl 已提交
577

578
		return saa7191_set_norm(client, *istd);
R
Ralf Baechle 已提交
579
	}
580 581
	case VIDIOC_INT_S_VIDEO_ROUTING: {
		struct v4l2_routing *route = arg;
R
Ralf Baechle 已提交
582

583
		return saa7191_set_input(client, route->input);
R
Ralf Baechle 已提交
584
	}
585 586

	case VIDIOC_G_CTRL:
L
Ladislav Michl 已提交
587
		return saa7191_get_control(client, arg);
588 589

	case VIDIOC_S_CTRL:
L
Ladislav Michl 已提交
590
		return saa7191_set_control(client, arg);
591

R
Ralf Baechle 已提交
592 593 594 595 596 597 598
	default:
		return -EINVAL;
	}

	return 0;
}

599 600 601 602 603
static int saa7191_probe(struct i2c_client *client,
			  const struct i2c_device_id *id)
{
	int err = 0;
	struct saa7191 *decoder;
R
Ralf Baechle 已提交
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
	v4l_info(client, "chip found @ 0x%x (%s)\n",
			client->addr << 1, client->adapter->name);

	decoder = kzalloc(sizeof(*decoder), GFP_KERNEL);
	if (!decoder)
		return -ENOMEM;

	i2c_set_clientdata(client, decoder);

	decoder->client = client;

	err = saa7191_write_block(client, sizeof(initseq), initseq);
	if (err) {
		printk(KERN_ERR "SAA7191 initialization failed\n");
		kfree(decoder);
		return err;
	}

	printk(KERN_INFO "SAA7191 initialized\n");

	decoder->input = SAA7191_INPUT_COMPOSITE;
626
	decoder->norm = V4L2_STD_PAL;
627 628 629 630 631 632 633 634 635

	err = saa7191_autodetect_norm(client);
	if (err && (err != -EBUSY))
		printk(KERN_ERR "SAA7191: Signal auto-detection failed\n");

	return 0;
}

static int saa7191_remove(struct i2c_client *client)
R
Ralf Baechle 已提交
636
{
637 638 639 640
	struct saa7191 *decoder = i2c_get_clientdata(client);

	kfree(decoder);
	return 0;
R
Ralf Baechle 已提交
641 642
}

643
static int saa7191_legacy_probe(struct i2c_adapter *adapter)
R
Ralf Baechle 已提交
644
{
645
	return adapter->id == I2C_HW_SGI_VINO;
R
Ralf Baechle 已提交
646 647
}

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
static const struct i2c_device_id saa7191_id[] = {
	{ "saa7191", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, saa7191_id);

static struct v4l2_i2c_driver_data v4l2_i2c_data = {
	.name = "saa7191",
	.driverid = I2C_DRIVERID_SAA7191,
	.command = saa7191_command,
	.probe = saa7191_probe,
	.remove = saa7191_remove,
	.legacy_probe = saa7191_legacy_probe,
	.id_table = saa7191_id,
};