saa7191.c 15.4 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-device.h>
#include <media/v4l2-chip-ident.h>
26
#include <media/v4l2-i2c-drv.h>
R
Ralf Baechle 已提交
27 28 29

#include "saa7191.h"

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

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

37

L
Ladislav Michl 已提交
38 39 40 41 42 43 44 45 46 47 48
// #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 已提交
49
struct saa7191 {
50
	struct v4l2_subdev sd;
R
Ralf Baechle 已提交
51 52 53

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

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

60 61 62 63 64
static inline struct saa7191 *to_saa7191(struct v4l2_subdev *sd)
{
	return container_of(sd, struct saa7191, sd);
}

L
Ladislav Michl 已提交
65
static const u8 initseq[] = {
R
Ralf Baechle 已提交
66
	0,	/* Subaddress */
L
Ladislav Michl 已提交
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 93 94 95

	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 已提交
96 97
	0x00,	/* unused */
	0x00,	/* unused */
L
Ladislav Michl 已提交
98 99 100 101 102 103 104

	/* 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 已提交
105 106 107 108
};

/* SAA7191 register handling */

109
static u8 saa7191_read_reg(struct v4l2_subdev *sd, u8 reg)
R
Ralf Baechle 已提交
110
{
111
	return to_saa7191(sd)->reg[reg];
R
Ralf Baechle 已提交
112 113
}

114
static int saa7191_read_status(struct v4l2_subdev *sd, u8 *value)
R
Ralf Baechle 已提交
115
{
116
	struct i2c_client *client = v4l2_get_subdevdata(sd);
R
Ralf Baechle 已提交
117 118 119 120
	int ret;

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

	return 0;
}


129
static int saa7191_write_reg(struct v4l2_subdev *sd, u8 reg, u8 value)
R
Ralf Baechle 已提交
130
{
131 132 133
	struct i2c_client *client = v4l2_get_subdevdata(sd);

	to_saa7191(sd)->reg[reg] = value;
R
Ralf Baechle 已提交
134 135 136 137
	return i2c_smbus_write_byte_data(client, reg, value);
}

/* the first byte of data must be the first subaddress number (register) */
138
static int saa7191_write_block(struct v4l2_subdev *sd,
139
			       u8 length, const u8 *data)
R
Ralf Baechle 已提交
140
{
141 142
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct saa7191 *decoder = to_saa7191(sd);
R
Ralf Baechle 已提交
143 144 145 146 147 148 149 150 151 152
	int i;
	int ret;

	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 已提交
153
		       "write failed\n");
R
Ralf Baechle 已提交
154 155 156 157 158 159 160 161
		return ret;
	}

	return 0;
}

/* Helper functions */

162
static int saa7191_s_routing(struct v4l2_subdev *sd,
163
			     u32 input, u32 output, u32 config)
R
Ralf Baechle 已提交
164
{
165 166 167
	struct saa7191 *decoder = to_saa7191(sd);
	u8 luma = saa7191_read_reg(sd, SAA7191_REG_LUMA);
	u8 iock = saa7191_read_reg(sd, SAA7191_REG_IOCK);
R
Ralf Baechle 已提交
168 169
	int err;

170
	switch (input) {
R
Ralf Baechle 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	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;
	}

186
	err = saa7191_write_reg(sd, SAA7191_REG_LUMA, luma);
R
Ralf Baechle 已提交
187 188
	if (err)
		return -EIO;
189
	err = saa7191_write_reg(sd, SAA7191_REG_IOCK, iock);
R
Ralf Baechle 已提交
190 191 192
	if (err)
		return -EIO;

193
	decoder->input = input;
L
Ladislav Michl 已提交
194

R
Ralf Baechle 已提交
195 196 197
	return 0;
}

198
static int saa7191_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
R
Ralf Baechle 已提交
199
{
200 201 202 203
	struct saa7191 *decoder = to_saa7191(sd);
	u8 stdc = saa7191_read_reg(sd, SAA7191_REG_STDC);
	u8 ctl3 = saa7191_read_reg(sd, SAA7191_REG_CTL3);
	u8 chcv = saa7191_read_reg(sd, SAA7191_REG_CHCV);
R
Ralf Baechle 已提交
204 205
	int err;

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

223
	err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
R
Ralf Baechle 已提交
224 225
	if (err)
		return -EIO;
226
	err = saa7191_write_reg(sd, SAA7191_REG_STDC, stdc);
R
Ralf Baechle 已提交
227 228
	if (err)
		return -EIO;
229
	err = saa7191_write_reg(sd, SAA7191_REG_CHCV, chcv);
R
Ralf Baechle 已提交
230 231 232 233 234
	if (err)
		return -EIO;

	decoder->norm = norm;

L
Ladislav Michl 已提交
235 236
	dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3,
		stdc, chcv);
237
	dprintk("norm: %llx\n", norm);
L
Ladislav Michl 已提交
238

R
Ralf Baechle 已提交
239 240 241
	return 0;
}

242
static int saa7191_wait_for_signal(struct v4l2_subdev *sd, u8 *status)
R
Ralf Baechle 已提交
243
{
L
Ladislav Michl 已提交
244
	int i = 0;
R
Ralf Baechle 已提交
245

L
Ladislav Michl 已提交
246 247 248
	dprintk("Checking for signal...\n");

	for (i = 0; i < SAA7191_SYNC_COUNT; i++) {
249
		if (saa7191_read_status(sd, status))
L
Ladislav Michl 已提交
250 251 252 253 254 255 256 257
			return -EIO;

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

		msleep(SAA7191_SYNC_DELAY);
R
Ralf Baechle 已提交
258 259
	}

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

L
Ladislav Michl 已提交
262
	return -EBUSY;
R
Ralf Baechle 已提交
263 264
}

265
static int saa7191_querystd(struct v4l2_subdev *sd, v4l2_std_id *norm)
R
Ralf Baechle 已提交
266
{
267 268 269
	struct saa7191 *decoder = to_saa7191(sd);
	u8 stdc = saa7191_read_reg(sd, SAA7191_REG_STDC);
	u8 ctl3 = saa7191_read_reg(sd, SAA7191_REG_CTL3);
L
Ladislav Michl 已提交
270
	u8 status;
271
	v4l2_std_id old_norm = decoder->norm;
L
Ladislav Michl 已提交
272
	int err = 0;
R
Ralf Baechle 已提交
273

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

276
	*norm = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
L
Ladislav Michl 已提交
277 278 279
	stdc &= ~SAA7191_STDC_SECS;
	ctl3 &= ~(SAA7191_CTL3_FSEL);

280
	err = saa7191_write_reg(sd, SAA7191_REG_STDC, stdc);
L
Ladislav Michl 已提交
281 282 283 284
	if (err) {
		err = -EIO;
		goto out;
	}
285
	err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
L
Ladislav Michl 已提交
286 287 288 289 290 291
	if (err) {
		err = -EIO;
		goto out;
	}

	ctl3 |= SAA7191_CTL3_AUFD;
292
	err = saa7191_write_reg(sd, SAA7191_REG_CTL3, ctl3);
L
Ladislav Michl 已提交
293 294 295 296 297 298 299
	if (err) {
		err = -EIO;
		goto out;
	}

	msleep(SAA7191_SYNC_DELAY);

300
	err = saa7191_wait_for_signal(sd, &status);
L
Ladislav Michl 已提交
301 302 303 304 305 306
	if (err)
		goto out;

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

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

	/* try PAL first */
315
	err = saa7191_s_std(sd, V4L2_STD_PAL);
L
Ladislav Michl 已提交
316 317 318 319 320
	if (err)
		goto out;

	msleep(SAA7191_SYNC_DELAY);

321
	err = saa7191_wait_for_signal(sd, &status);
L
Ladislav Michl 已提交
322 323 324 325 326 327
	if (err)
		goto out;

	/* not 50Hz ? */
	if (status & SAA7191_STATUS_FIDT) {
		dprintk("No 50Hz signal\n");
328
		saa7191_s_std(sd, old_norm);
329
		return -EAGAIN;
L
Ladislav Michl 已提交
330 331 332 333
	}

	if (status & SAA7191_STATUS_CODE) {
		dprintk("PAL\n");
334
		*norm = V4L2_STD_PAL;
335
		return saa7191_s_std(sd, old_norm);
L
Ladislav Michl 已提交
336 337 338 339 340
	}

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

	/* no color detected ? -> try SECAM */
341
	err = saa7191_s_std(sd, V4L2_STD_SECAM);
L
Ladislav Michl 已提交
342 343 344 345 346
	if (err)
		goto out;

	msleep(SAA7191_SYNC_DELAY);

347
	err = saa7191_wait_for_signal(sd, &status);
L
Ladislav Michl 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360
	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");
361
		*norm = V4L2_STD_SECAM;
362
		return saa7191_s_std(sd, old_norm);
L
Ladislav Michl 已提交
363 364 365 366 367
	}

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

out:
368
	return saa7191_s_std(sd, old_norm);
L
Ladislav Michl 已提交
369 370
}

371
static int saa7191_autodetect_norm(struct v4l2_subdev *sd)
L
Ladislav Michl 已提交
372 373 374 375 376 377 378
{
	u8 status;

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

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

379
	if (saa7191_read_status(sd, &status))
L
Ladislav Michl 已提交
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
		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");
395
		return saa7191_s_std(sd, V4L2_STD_NTSC);
L
Ladislav Michl 已提交
396 397 398
	} else {
		/* 50hz signal -> PAL */
		dprintk("PAL\n");
399
		return saa7191_s_std(sd, V4L2_STD_PAL);
L
Ladislav Michl 已提交
400 401 402
	}
}

403
static int saa7191_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
L
Ladislav Michl 已提交
404 405 406 407
{
	u8 reg;
	int ret = 0;

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

L
Ladislav Michl 已提交
465 466 467
	return ret;
}

468
static int saa7191_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
L
Ladislav Michl 已提交
469 470 471 472
{
	u8 reg;
	int ret = 0;

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

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

/* I2C-interface */

555
static int saa7191_g_input_status(struct v4l2_subdev *sd, u32 *status)
R
Ralf Baechle 已提交
556
{
557 558
	u8 status_reg;
	int res = V4L2_IN_ST_NO_SIGNAL;
R
Ralf Baechle 已提交
559

560 561 562 563 564 565 566 567 568
	if (saa7191_read_status(sd, &status_reg))
		return -EIO;
	if ((status_reg & SAA7191_STATUS_HLCK) == 0)
		res = 0;
	if (!(status_reg & SAA7191_STATUS_CODE))
		res |= V4L2_IN_ST_NO_COLOR;
	*status = res;
	return 0;
}
R
Ralf Baechle 已提交
569

L
Ladislav Michl 已提交
570

571 572 573 574
static int saa7191_g_chip_ident(struct v4l2_subdev *sd,
		struct v4l2_dbg_chip_ident *chip)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
L
Ladislav Michl 已提交
575

576 577
	return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_SAA7191, 0);
}
R
Ralf Baechle 已提交
578

579
/* ----------------------------------------------------------------------- */
580

581 582 583 584 585 586
static const struct v4l2_subdev_core_ops saa7191_core_ops = {
	.g_chip_ident = saa7191_g_chip_ident,
	.g_ctrl = saa7191_g_ctrl,
	.s_ctrl = saa7191_s_ctrl,
	.s_std = saa7191_s_std,
};
R
Ralf Baechle 已提交
587

588 589 590 591 592 593 594 595 596 597
static const struct v4l2_subdev_video_ops saa7191_video_ops = {
	.s_routing = saa7191_s_routing,
	.querystd = saa7191_querystd,
	.g_input_status = saa7191_g_input_status,
};

static const struct v4l2_subdev_ops saa7191_ops = {
	.core = &saa7191_core_ops,
	.video = &saa7191_video_ops,
};
R
Ralf Baechle 已提交
598

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;
604
	struct v4l2_subdev *sd;
R
Ralf Baechle 已提交
605

606 607 608 609 610 611 612
	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;

613 614
	sd = &decoder->sd;
	v4l2_i2c_subdev_init(sd, client, &saa7191_ops);
615

616
	err = saa7191_write_block(sd, sizeof(initseq), initseq);
617 618 619 620 621 622 623 624 625
	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
	err = saa7191_autodetect_norm(sd);
629 630 631 632 633 634 635
	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
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
638

639 640
	v4l2_device_unregister_subdev(sd);
	kfree(to_saa7191(sd));
641
	return 0;
R
Ralf Baechle 已提交
642 643
}

644 645 646 647 648 649 650 651 652 653 654 655
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",
	.probe = saa7191_probe,
	.remove = saa7191_remove,
	.id_table = saa7191_id,
};