tw9910.c 22.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * tw9910 Video Driver
 *
 * Copyright (C) 2008 Renesas Solutions Corp.
 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
 *
 * Based on ov772x driver,
 *
 * Copyright (C) 2008 Kuninori Morimoto <morimoto.kuninori@renesas.com>
 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
 * Copyright (C) 2008 Magnus Damm
 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 *
 * 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/init.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/videodev2.h>
#include <media/v4l2-chip-ident.h>
27
#include <media/v4l2-subdev.h>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
#include <media/soc_camera.h>
#include <media/tw9910.h>

#define GET_ID(val)  ((val & 0xF8) >> 3)
#define GET_ReV(val) (val & 0x07)

/*
 * register offset
 */
#define ID		0x00 /* Product ID Code Register */
#define STATUS1		0x01 /* Chip Status Register I */
#define INFORM		0x02 /* Input Format */
#define OPFORM		0x03 /* Output Format Control Register */
#define DLYCTR		0x04 /* Hysteresis and HSYNC Delay Control */
#define OUTCTR1		0x05 /* Output Control I */
#define ACNTL1		0x06 /* Analog Control Register 1 */
#define CROP_HI		0x07 /* Cropping Register, High */
#define VDELAY_LO	0x08 /* Vertical Delay Register, Low */
#define VACTIVE_LO	0x09 /* Vertical Active Register, Low */
#define HDELAY_LO	0x0A /* Horizontal Delay Register, Low */
#define HACTIVE_LO	0x0B /* Horizontal Active Register, Low */
#define CNTRL1		0x0C /* Control Register I */
#define VSCALE_LO	0x0D /* Vertical Scaling Register, Low */
#define SCALE_HI	0x0E /* Scaling Register, High */
#define HSCALE_LO	0x0F /* Horizontal Scaling Register, Low */
#define BRIGHT		0x10 /* BRIGHTNESS Control Register */
#define CONTRAST	0x11 /* CONTRAST Control Register */
#define SHARPNESS	0x12 /* SHARPNESS Control Register I */
#define SAT_U		0x13 /* Chroma (U) Gain Register */
#define SAT_V		0x14 /* Chroma (V) Gain Register */
#define HUE		0x15 /* Hue Control Register */
#define CORING1		0x17
#define CORING2		0x18 /* Coring and IF compensation */
#define VBICNTL		0x19 /* VBI Control Register */
#define ACNTL2		0x1A /* Analog Control 2 */
#define OUTCTR2		0x1B /* Output Control 2 */
#define SDT		0x1C /* Standard Selection */
#define SDTR		0x1D /* Standard Recognition */
#define TEST		0x1F /* Test Control Register */
#define CLMPG		0x20 /* Clamping Gain */
#define IAGC		0x21 /* Individual AGC Gain */
#define AGCGAIN		0x22 /* AGC Gain */
#define PEAKWT		0x23 /* White Peak Threshold */
#define CLMPL		0x24 /* Clamp level */
#define SYNCT		0x25 /* Sync Amplitude */
#define MISSCNT		0x26 /* Sync Miss Count Register */
#define PCLAMP		0x27 /* Clamp Position Register */
#define VCNTL1		0x28 /* Vertical Control I */
#define VCNTL2		0x29 /* Vertical Control II */
#define CKILL		0x2A /* Color Killer Level Control */
#define COMB		0x2B /* Comb Filter Control */
#define LDLY		0x2C /* Luma Delay and H Filter Control */
#define MISC1		0x2D /* Miscellaneous Control I */
#define LOOP		0x2E /* LOOP Control Register */
#define MISC2		0x2F /* Miscellaneous Control II */
#define MVSN		0x30 /* Macrovision Detection */
#define STATUS2		0x31 /* Chip STATUS II */
#define HFREF		0x32 /* H monitor */
#define CLMD		0x33 /* CLAMP MODE */
#define IDCNTL		0x34 /* ID Detection Control */
#define CLCNTL1		0x35 /* Clamp Control I */
#define ANAPLLCTL	0x4C
#define VBIMIN		0x4D
#define HSLOWCTL	0x4E
#define WSS3		0x4F
#define FILLDATA	0x50
#define SDID		0x51
#define DID		0x52
#define WSS1		0x53
#define WSS2		0x54
#define VVBI		0x55
#define LCTL6		0x56
#define LCTL7		0x57
#define LCTL8		0x58
#define LCTL9		0x59
#define LCTL10		0x5A
#define LCTL11		0x5B
#define LCTL12		0x5C
#define LCTL13		0x5D
#define LCTL14		0x5E
#define LCTL15		0x5F
#define LCTL16		0x60
#define LCTL17		0x61
#define LCTL18		0x62
#define LCTL19		0x63
#define LCTL20		0x64
#define LCTL21		0x65
#define LCTL22		0x66
#define LCTL23		0x67
#define LCTL24		0x68
#define LCTL25		0x69
#define LCTL26		0x6A
#define HSGEGIN		0x6B
#define HSEND		0x6C
#define OVSDLY		0x6D
#define OVSEND		0x6E
#define VBIDELAY	0x6F

/*
 * register detail
 */

/* INFORM */
#define FC27_ON     0x40 /* 1 : Input crystal clock frequency is 27MHz */
#define FC27_FF     0x00 /* 0 : Square pixel mode. */
			 /*     Must use 24.54MHz for 60Hz field rate */
			 /*     source or 29.5MHz for 50Hz field rate */
#define IFSEL_S     0x10 /* 01 : S-video decoding */
#define IFSEL_C     0x00 /* 00 : Composite video decoding */
			 /* Y input video selection */
#define YSEL_M0     0x00 /*  00 : Mux0 selected */
#define YSEL_M1     0x04 /*  01 : Mux1 selected */
#define YSEL_M2     0x08 /*  10 : Mux2 selected */
#define YSEL_M3     0x10 /*  11 : Mux3 selected */

/* OPFORM */
#define MODE        0x80 /* 0 : CCIR601 compatible YCrCb 4:2:2 format */
			 /* 1 : ITU-R-656 compatible data sequence format */
#define LEN         0x40 /* 0 : 8-bit YCrCb 4:2:2 output format */
			 /* 1 : 16-bit YCrCb 4:2:2 output format.*/
#define LLCMODE     0x20 /* 1 : LLC output mode. */
			 /* 0 : free-run output mode */
#define AINC        0x10 /* Serial interface auto-indexing control */
			 /* 0 : auto-increment */
			 /* 1 : non-auto */
#define VSCTL       0x08 /* 1 : Vertical out ctrl by DVALID */
			 /* 0 : Vertical out ctrl by HACTIVE and DVALID */
#define OEN         0x04 /* Output Enable together with TRI_SEL. */

/* OUTCTR1 */
#define VSP_LO      0x00 /* 0 : VS pin output polarity is active low */
#define VSP_HI      0x80 /* 1 : VS pin output polarity is active high. */
			 /* VS pin output control */
#define VSSL_VSYNC  0x00 /*   0 : VSYNC  */
#define VSSL_VACT   0x10 /*   1 : VACT   */
#define VSSL_FIELD  0x20 /*   2 : FIELD  */
#define VSSL_VVALID 0x30 /*   3 : VVALID */
#define VSSL_ZERO   0x70 /*   7 : 0      */
#define HSP_LOW     0x00 /* 0 : HS pin output polarity is active low */
#define HSP_HI      0x08 /* 1 : HS pin output polarity is active high.*/
			 /* HS pin output control */
#define HSSL_HACT   0x00 /*   0 : HACT   */
#define HSSL_HSYNC  0x01 /*   1 : HSYNC  */
#define HSSL_DVALID 0x02 /*   2 : DVALID */
#define HSSL_HLOCK  0x03 /*   3 : HLOCK  */
#define HSSL_ASYNCW 0x04 /*   4 : ASYNCW */
#define HSSL_ZERO   0x07 /*   7 : 0      */

/* ACNTL1 */
#define SRESET      0x80 /* resets the device to its default state
			  * but all register content remain unchanged.
			  * This bit is self-resetting.
			  */

/* VBICNTL */
/* RTSEL : control the real time signal
*          output from the MPOUT pin
*/
#define RTSEL_MASK  0x07
#define RTSEL_VLOSS 0x00 /* 0000 = Video loss */
#define RTSEL_HLOCK 0x01 /* 0001 = H-lock */
#define RTSEL_SLOCK 0x02 /* 0010 = S-lock */
#define RTSEL_VLOCK 0x03 /* 0011 = V-lock */
#define RTSEL_MONO  0x04 /* 0100 = MONO */
#define RTSEL_DET50 0x05 /* 0101 = DET50 */
#define RTSEL_FIELD 0x06 /* 0110 = FIELD */
#define RTSEL_RTCO  0x07 /* 0111 = RTCO ( Real Time Control ) */

/*
 * structure
 */

struct regval_list {
	unsigned char reg_num;
	unsigned char value;
};

struct tw9910_scale_ctrl {
	char           *name;
	unsigned short  width;
	unsigned short  height;
	u16             hscale;
	u16             vscale;
};

struct tw9910_cropping_ctrl {
	u16 vdelay;
	u16 vactive;
	u16 hdelay;
	u16 hactive;
};

struct tw9910_hsync_ctrl {
	u16 start;
	u16 end;
};

struct tw9910_priv {
226
	struct v4l2_subdev                subdev;
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	struct tw9910_video_info       *info;
	const struct tw9910_scale_ctrl *scale;
};

/*
 * register settings
 */

#define ENDMARKER { 0xff, 0xff }

static const struct regval_list tw9910_default_regs[] =
{
	{ OPFORM,  0x00 },
	{ OUTCTR1, VSP_LO | VSSL_VVALID | HSP_HI | HSSL_HSYNC },
	ENDMARKER,
};

static const struct soc_camera_data_format tw9910_color_fmt[] = {
	{
		.name       = "VYUY",
		.fourcc     = V4L2_PIX_FMT_VYUY,
		.depth      = 16,
		.colorspace = V4L2_COLORSPACE_SMPTE170M,
	}
};

static const struct tw9910_scale_ctrl tw9910_ntsc_scales[] = {
	{
		.name   = "NTSC SQ",
		.width  = 640,
		.height = 480,
		.hscale = 0x0100,
		.vscale = 0x0100,
	},
	{
		.name   = "NTSC CCIR601",
		.width  = 720,
		.height = 480,
		.hscale = 0x0100,
		.vscale = 0x0100,
	},
	{
		.name   = "NTSC SQ (CIF)",
		.width  = 320,
		.height = 240,
		.hscale = 0x0200,
		.vscale = 0x0200,
	},
	{
		.name   = "NTSC CCIR601 (CIF)",
		.width  = 360,
		.height = 240,
		.hscale = 0x0200,
		.vscale = 0x0200,
	},
	{
		.name   = "NTSC SQ (QCIF)",
		.width  = 160,
		.height = 120,
		.hscale = 0x0400,
		.vscale = 0x0400,
	},
	{
		.name   = "NTSC CCIR601 (QCIF)",
		.width  = 180,
		.height = 120,
		.hscale = 0x0400,
		.vscale = 0x0400,
	},
};

static const struct tw9910_scale_ctrl tw9910_pal_scales[] = {
	{
		.name   = "PAL SQ",
		.width  = 768,
		.height = 576,
		.hscale = 0x0100,
		.vscale = 0x0100,
	},
	{
		.name   = "PAL CCIR601",
		.width  = 720,
		.height = 576,
		.hscale = 0x0100,
		.vscale = 0x0100,
	},
	{
		.name   = "PAL SQ (CIF)",
		.width  = 384,
		.height = 288,
		.hscale = 0x0200,
		.vscale = 0x0200,
	},
	{
		.name   = "PAL CCIR601 (CIF)",
		.width  = 360,
		.height = 288,
		.hscale = 0x0200,
		.vscale = 0x0200,
	},
	{
		.name   = "PAL SQ (QCIF)",
		.width  = 192,
		.height = 144,
		.hscale = 0x0400,
		.vscale = 0x0400,
	},
	{
		.name   = "PAL CCIR601 (QCIF)",
		.width  = 180,
		.height = 144,
		.hscale = 0x0400,
		.vscale = 0x0400,
	},
};

static const struct tw9910_cropping_ctrl tw9910_cropping_ctrl = {
	.vdelay  = 0x0012,
	.vactive = 0x00F0,
	.hdelay  = 0x0010,
	.hactive = 0x02D0,
};

static const struct tw9910_hsync_ctrl tw9910_hsync_ctrl = {
	.start = 0x0260,
	.end   = 0x0300,
};

/*
 * general function
 */
358 359 360 361 362
static struct tw9910_priv *to_tw9910(const struct i2c_client *client)
{
	return container_of(i2c_get_clientdata(client), struct tw9910_priv, subdev);
}

363 364 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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 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 461 462 463 464 465 466
static int tw9910_set_scale(struct i2c_client *client,
			    const struct tw9910_scale_ctrl *scale)
{
	int ret;

	ret = i2c_smbus_write_byte_data(client, SCALE_HI,
					(scale->vscale & 0x0F00) >> 4 |
					(scale->hscale & 0x0F00) >> 8);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(client, HSCALE_LO,
					scale->hscale & 0x00FF);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(client, VSCALE_LO,
					scale->vscale & 0x00FF);

	return ret;
}

static int tw9910_set_cropping(struct i2c_client *client,
			       const struct tw9910_cropping_ctrl *cropping)
{
	int ret;

	ret = i2c_smbus_write_byte_data(client, CROP_HI,
					(cropping->vdelay  & 0x0300) >> 2 |
					(cropping->vactive & 0x0300) >> 4 |
					(cropping->hdelay  & 0x0300) >> 6 |
					(cropping->hactive & 0x0300) >> 8);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(client, VDELAY_LO,
					cropping->vdelay & 0x00FF);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(client, VACTIVE_LO,
					cropping->vactive & 0x00FF);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(client, HDELAY_LO,
					cropping->hdelay & 0x00FF);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(client, HACTIVE_LO,
					cropping->hactive & 0x00FF);

	return ret;
}

static int tw9910_set_hsync(struct i2c_client *client,
			    const struct tw9910_hsync_ctrl *hsync)
{
	int ret;

	/* bit 10 - 3 */
	ret = i2c_smbus_write_byte_data(client, HSGEGIN,
					(hsync->start & 0x07F8) >> 3);
	if (ret < 0)
		return ret;

	/* bit 10 - 3 */
	ret = i2c_smbus_write_byte_data(client, HSEND,
					(hsync->end & 0x07F8) >> 3);
	if (ret < 0)
		return ret;

	/* bit 2 - 0 */
	ret = i2c_smbus_read_byte_data(client, HSLOWCTL);
	if (ret < 0)
		return ret;

	ret = i2c_smbus_write_byte_data(client, HSLOWCTL,
					(ret & 0x88)                 |
					(hsync->start & 0x0007) << 4 |
					(hsync->end   & 0x0007));

	return ret;
}

static int tw9910_write_array(struct i2c_client *client,
			      const struct regval_list *vals)
{
	while (vals->reg_num != 0xff) {
		int ret = i2c_smbus_write_byte_data(client,
						    vals->reg_num,
						    vals->value);
		if (ret < 0)
			return ret;
		vals++;
	}
	return 0;
}

static int tw9910_mask_set(struct i2c_client *client, u8 command,
			   u8 mask, u8 set)
{
	s32 val = i2c_smbus_read_byte_data(client, command);
467 468
	if (val < 0)
		return val;
469 470

	val &= ~mask;
471
	val |= set & mask;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

	return i2c_smbus_write_byte_data(client, command, val);
}

static void tw9910_reset(struct i2c_client *client)
{
	i2c_smbus_write_byte_data(client, ACNTL1, SRESET);
	msleep(1);
}

static const struct tw9910_scale_ctrl*
tw9910_select_norm(struct soc_camera_device *icd, u32 width, u32 height)
{
	const struct tw9910_scale_ctrl *scale;
	const struct tw9910_scale_ctrl *ret = NULL;
	v4l2_std_id norm = icd->vdev->current_norm;
	__u32 diff = 0xffffffff, tmp;
	int size, i;

	if (norm & V4L2_STD_NTSC) {
		scale = tw9910_ntsc_scales;
		size = ARRAY_SIZE(tw9910_ntsc_scales);
	} else if (norm & V4L2_STD_PAL) {
		scale = tw9910_pal_scales;
		size = ARRAY_SIZE(tw9910_pal_scales);
	} else {
		return NULL;
	}

	for (i = 0; i < size; i++) {
		tmp = abs(width - scale[i].width) +
			abs(height - scale[i].height);
		if (tmp < diff) {
			diff = tmp;
			ret = scale + i;
		}
	}

	return ret;
}

/*
 * soc_camera_ops function
 */
516
static int tw9910_s_stream(struct v4l2_subdev *sd, int enable)
517
{
518 519
	struct i2c_client *client = sd->priv;
	struct tw9910_priv *priv = to_tw9910(client);
520

521 522
	if (!enable)
		return 0;
523 524

	if (!priv->scale) {
525
		dev_err(&client->dev, "norm select error\n");
526 527 528
		return -EPERM;
	}

529
	dev_dbg(&client->dev, "%s %dx%d\n",
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
		 priv->scale->name,
		 priv->scale->width,
		 priv->scale->height);

	return 0;
}

static int tw9910_set_bus_param(struct soc_camera_device *icd,
				unsigned long flags)
{
	return 0;
}

static unsigned long tw9910_query_bus_param(struct soc_camera_device *icd)
{
545
	struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
546
	struct tw9910_priv *priv = to_tw9910(client);
547
	struct soc_camera_link *icl = to_soc_camera_link(icd);
548 549 550 551 552 553 554
	unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
		SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
		SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;

	return soc_camera_apply_sensor_flags(icl, flags);
}

555
static int tw9910_s_std(struct v4l2_subdev *sd, v4l2_std_id norm)
556 557 558
{
	int ret = -EINVAL;

559
	if (norm & (V4L2_STD_NTSC | V4L2_STD_PAL))
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
		ret = 0;

	return ret;
}

static int tw9910_enum_input(struct soc_camera_device *icd,
			     struct v4l2_input *inp)
{
	inp->type = V4L2_INPUT_TYPE_TUNER;
	inp->std  = V4L2_STD_UNKNOWN;
	strcpy(inp->name, "Video");

	return 0;
}

575 576 577 578 579 580 581 582 583
static int tw9910_g_chip_ident(struct v4l2_subdev *sd,
			       struct v4l2_dbg_chip_ident *id)
{
	id->ident = V4L2_IDENT_TW9910;
	id->revision = 0;

	return 0;
}

584
#ifdef CONFIG_VIDEO_ADV_DEBUG
585 586
static int tw9910_g_register(struct v4l2_subdev *sd,
			     struct v4l2_dbg_register *reg)
587
{
588
	struct i2c_client *client = sd->priv;
589 590 591 592 593
	int ret;

	if (reg->reg > 0xff)
		return -EINVAL;

594
	ret = i2c_smbus_read_byte_data(client, reg->reg);
595 596 597 598 599 600 601 602 603 604 605
	if (ret < 0)
		return ret;

	/* ret      = int
	 * reg->val = __u64
	 */
	reg->val = (__u64)ret;

	return 0;
}

606 607
static int tw9910_s_register(struct v4l2_subdev *sd,
			     struct v4l2_dbg_register *reg)
608
{
609
	struct i2c_client *client = sd->priv;
610 611 612 613 614

	if (reg->reg > 0xff ||
	    reg->val > 0xff)
		return -EINVAL;

615
	return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
616 617 618
}
#endif

619 620
static int tw9910_set_crop(struct soc_camera_device *icd,
			   struct v4l2_rect *rect)
621
{
622
	struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
623
	struct tw9910_priv *priv = to_tw9910(client);
624 625 626 627 628 629 630 631
	int                 ret  = -EINVAL;
	u8                  val;

	/*
	 * select suitable norm
	 */
	priv->scale = tw9910_select_norm(icd, rect->width, rect->height);
	if (!priv->scale)
632
		goto tw9910_set_fmt_error;
633 634 635 636

	/*
	 * reset hardware
	 */
637 638
	tw9910_reset(client);
	ret = tw9910_write_array(client, tw9910_default_regs);
639
	if (ret < 0)
640 641
		goto tw9910_set_fmt_error;

642 643 644 645 646 647 648
	/*
	 * set bus width
	 */
	val = 0x00;
	if (SOCAM_DATAWIDTH_16 == priv->info->buswidth)
		val = LEN;

649
	ret = tw9910_mask_set(client, OPFORM, LEN, val);
650
	if (ret < 0)
651
		goto tw9910_set_fmt_error;
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676

	/*
	 * select MPOUT behavior
	 */
	switch (priv->info->mpout) {
	case TW9910_MPO_VLOSS:
		val = RTSEL_VLOSS; break;
	case TW9910_MPO_HLOCK:
		val = RTSEL_HLOCK; break;
	case TW9910_MPO_SLOCK:
		val = RTSEL_SLOCK; break;
	case TW9910_MPO_VLOCK:
		val = RTSEL_VLOCK; break;
	case TW9910_MPO_MONO:
		val = RTSEL_MONO;  break;
	case TW9910_MPO_DET50:
		val = RTSEL_DET50; break;
	case TW9910_MPO_FIELD:
		val = RTSEL_FIELD; break;
	case TW9910_MPO_RTCO:
		val = RTSEL_RTCO;  break;
	default:
		val = 0;
	}

677
	ret = tw9910_mask_set(client, VBICNTL, RTSEL_MASK, val);
678
	if (ret < 0)
679
		goto tw9910_set_fmt_error;
680 681 682 683

	/*
	 * set scale
	 */
684
	ret = tw9910_set_scale(client, priv->scale);
685
	if (ret < 0)
686
		goto tw9910_set_fmt_error;
687 688 689 690

	/*
	 * set cropping
	 */
691
	ret = tw9910_set_cropping(client, &tw9910_cropping_ctrl);
692
	if (ret < 0)
693
		goto tw9910_set_fmt_error;
694 695 696 697

	/*
	 * set hsync
	 */
698
	ret = tw9910_set_hsync(client, &tw9910_hsync_ctrl);
699 700 701 702 703 704 705
	if (ret < 0)
		goto tw9910_set_fmt_error;

	return ret;

tw9910_set_fmt_error:

706
	tw9910_reset(client);
707
	priv->scale = NULL;
708 709 710 711

	return ret;
}

712
static int tw9910_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
713
{
714 715
	struct i2c_client *client = sd->priv;
	struct soc_camera_device *icd = client->dev.platform_data;
716 717
	struct v4l2_pix_format *pix = &f->fmt.pix;
	struct v4l2_rect rect = {
718 719
		.left	= icd->rect_current.left,
		.top	= icd->rect_current.top,
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
		.width	= pix->width,
		.height	= pix->height,
	};
	int i;

	/*
	 * check color format
	 */
	for (i = 0; i < ARRAY_SIZE(tw9910_color_fmt); i++)
		if (pix->pixelformat == tw9910_color_fmt[i].fourcc)
			break;

	if (i == ARRAY_SIZE(tw9910_color_fmt))
		return -EINVAL;

	return tw9910_set_crop(icd, &rect);
}

738
static int tw9910_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
739
{
740 741
	struct i2c_client *client = sd->priv;
	struct soc_camera_device *icd = client->dev.platform_data;
742 743 744 745 746 747
	struct v4l2_pix_format *pix = &f->fmt.pix;
	const struct tw9910_scale_ctrl *scale;

	if (V4L2_FIELD_ANY == pix->field) {
		pix->field = V4L2_FIELD_INTERLACED;
	} else if (V4L2_FIELD_INTERLACED != pix->field) {
748
		dev_err(&client->dev, "Field type invalid.\n");
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
		return -EINVAL;
	}

	/*
	 * select suitable norm
	 */
	scale = tw9910_select_norm(icd, pix->width, pix->height);
	if (!scale)
		return -EINVAL;

	pix->width  = scale->width;
	pix->height = scale->height;

	return 0;
}

765 766
static int tw9910_video_probe(struct soc_camera_device *icd,
			      struct i2c_client *client)
767
{
768
	struct tw9910_priv *priv = to_tw9910(client);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	s32 val;

	/*
	 * We must have a parent by now. And it cannot be a wrong one.
	 * So this entire test is completely redundant.
	 */
	if (!icd->dev.parent ||
	    to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
		return -ENODEV;

	/*
	 * tw9910 only use 8 or 16 bit bus width
	 */
	if (SOCAM_DATAWIDTH_16 != priv->info->buswidth &&
	    SOCAM_DATAWIDTH_8  != priv->info->buswidth) {
		dev_err(&icd->dev, "bus width error\n");
		return -ENODEV;
	}

	icd->formats     = tw9910_color_fmt;
	icd->num_formats = ARRAY_SIZE(tw9910_color_fmt);

	/*
	 * check and show Product ID
	 */
794 795
	val = i2c_smbus_read_byte_data(client, ID);

796 797 798 799 800 801 802 803 804 805 806 807 808
	if (0x0B != GET_ID(val) ||
	    0x00 != GET_ReV(val)) {
		dev_err(&icd->dev,
			"Product ID error %x:%x\n", GET_ID(val), GET_ReV(val));
		return -ENODEV;
	}

	dev_info(&icd->dev,
		 "tw9910 Product ID %0x:%0x\n", GET_ID(val), GET_ReV(val));

	icd->vdev->tvnorms      = V4L2_STD_NTSC | V4L2_STD_PAL;
	icd->vdev->current_norm = V4L2_STD_NTSC;

809
	return 0;
810 811 812
}

static struct soc_camera_ops tw9910_ops = {
813
	.set_crop		= tw9910_set_crop,
814 815 816
	.set_bus_param		= tw9910_set_bus_param,
	.query_bus_param	= tw9910_query_bus_param,
	.enum_input		= tw9910_enum_input,
817 818 819 820 821
};

static struct v4l2_subdev_core_ops tw9910_subdev_core_ops = {
	.g_chip_ident	= tw9910_g_chip_ident,
	.s_std		= tw9910_s_std,
822
#ifdef CONFIG_VIDEO_ADV_DEBUG
823 824
	.g_register	= tw9910_g_register,
	.s_register	= tw9910_s_register,
825 826 827
#endif
};

828 829 830 831 832 833 834 835 836 837 838
static struct v4l2_subdev_video_ops tw9910_subdev_video_ops = {
	.s_stream	= tw9910_s_stream,
	.s_fmt		= tw9910_s_fmt,
	.try_fmt	= tw9910_try_fmt,
};

static struct v4l2_subdev_ops tw9910_subdev_ops = {
	.core	= &tw9910_subdev_core_ops,
	.video	= &tw9910_subdev_video_ops,
};

839 840 841 842
/*
 * i2c_driver function
 */

843 844 845 846 847 848 849 850 851 852 853 854 855
static void limit_to_scale(struct soc_camera_device *icd,
			   const struct tw9910_scale_ctrl *scale)
{
	if (scale->width > icd->rect_max.width)
		icd->rect_max.width  = scale->width;
	if (scale->width < icd->width_min)
		icd->width_min = scale->width;
	if (scale->height > icd->rect_max.height)
		icd->rect_max.height = scale->height;
	if (scale->height < icd->height_min)
		icd->height_min = scale->height;
}

856 857 858 859 860 861
static int tw9910_probe(struct i2c_client *client,
			const struct i2c_device_id *did)

{
	struct tw9910_priv             *priv;
	struct tw9910_video_info       *info;
862 863 864 865
	struct soc_camera_device       *icd = client->dev.platform_data;
	struct i2c_adapter             *adapter =
		to_i2c_adapter(client->dev.parent);
	struct soc_camera_link         *icl;
866 867 868
	const struct tw9910_scale_ctrl *scale;
	int                             i, ret;

869 870
	if (!icd) {
		dev_err(&client->dev, "TW9910: missing soc-camera data!\n");
871
		return -EINVAL;
872
	}
873

874 875 876
	icl = to_soc_camera_link(icd);
	if (!icl)
		return -EINVAL;
877

878 879 880
	info = container_of(icl, struct tw9910_video_info, link);

	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
881 882 883 884 885 886 887 888 889 890 891
		dev_err(&client->dev,
			"I2C-Adapter doesn't support "
			"I2C_FUNC_SMBUS_BYTE_DATA\n");
		return -EIO;
	}

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

	priv->info   = info;
892 893

	v4l2_i2c_subdev_init(&priv->subdev, client, &tw9910_subdev_ops);
894 895 896 897 898 899 900

	icd->ops     = &tw9910_ops;
	icd->iface   = info->link.bus_id;

	/*
	 * set width and height
	 */
901
	icd->rect_max.width  = tw9910_ntsc_scales[0].width; /* set default */
902
	icd->width_min  = tw9910_ntsc_scales[0].width;
903
	icd->rect_max.height = tw9910_ntsc_scales[0].height;
904 905 906
	icd->height_min = tw9910_ntsc_scales[0].height;

	scale = tw9910_ntsc_scales;
907 908 909
	for (i = 0; i < ARRAY_SIZE(tw9910_ntsc_scales); i++)
		limit_to_scale(icd, scale + i);

910
	scale = tw9910_pal_scales;
911 912
	for (i = 0; i < ARRAY_SIZE(tw9910_pal_scales); i++)
		limit_to_scale(icd, scale + i);
913

914
	ret = tw9910_video_probe(icd, client);
915
	if (ret) {
916
		icd->ops = NULL;
917 918 919 920 921 922 923 924 925
		i2c_set_clientdata(client, NULL);
		kfree(priv);
	}

	return ret;
}

static int tw9910_remove(struct i2c_client *client)
{
926
	struct tw9910_priv *priv = to_tw9910(client);
927
	struct soc_camera_device *icd = client->dev.platform_data;
928

929
	icd->ops = NULL;
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
	i2c_set_clientdata(client, NULL);
	kfree(priv);
	return 0;
}

static const struct i2c_device_id tw9910_id[] = {
	{ "tw9910", 0 },
	{ }
};
MODULE_DEVICE_TABLE(i2c, tw9910_id);

static struct i2c_driver tw9910_i2c_driver = {
	.driver = {
		.name = "tw9910",
	},
	.probe    = tw9910_probe,
	.remove   = tw9910_remove,
	.id_table = tw9910_id,
};

/*
 * module function
 */
static int __init tw9910_module_init(void)
{
	return i2c_add_driver(&tw9910_i2c_driver);
}

static void __exit tw9910_module_exit(void)
{
	i2c_del_driver(&tw9910_i2c_driver);
}

module_init(tw9910_module_init);
module_exit(tw9910_module_exit);

MODULE_DESCRIPTION("SoC Camera driver for tw9910");
MODULE_AUTHOR("Kuninori Morimoto");
MODULE_LICENSE("GPL v2");