ov6650.c 27.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * V4L2 subdevice driver for OmniVision OV6650 Camera Sensor
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Copyright (C) 2010 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
 *
 * Based on OmniVision OV96xx Camera Driver
 * Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com>
 *
 * Based on ov772x camera driver:
 * Copyright (C) 2008 Renesas Solutions Corp.
 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
 *
 * Based on ov7670 and soc_camera_platform driver,
 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
 * Copyright (C) 2008 Magnus Damm
 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
 *
19
 * Hardware specific bits initially based on former work by Matt Callow
20 21 22 23 24 25 26 27
 * drivers/media/video/omap/sensor_ov6650.c
 * Copyright (C) 2006 Matt Callow
 */

#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/slab.h>
28
#include <linux/v4l2-mediabus.h>
29
#include <linux/module.h>
30

31
#include <media/v4l2-clk.h>
32
#include <media/v4l2-ctrls.h>
33
#include <media/v4l2-device.h>
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

/* Register definitions */
#define REG_GAIN		0x00	/* range 00 - 3F */
#define REG_BLUE		0x01
#define REG_RED			0x02
#define REG_SAT			0x03	/* [7:4] saturation [0:3] reserved */
#define REG_HUE			0x04	/* [7:6] rsrvd [5] hue en [4:0] hue */

#define REG_BRT			0x06

#define REG_PIDH		0x0a
#define REG_PIDL		0x0b

#define REG_AECH		0x10
#define REG_CLKRC		0x11	/* Data Format and Internal Clock */
					/* [7:6] Input system clock (MHz)*/
					/*   00=8, 01=12, 10=16, 11=24 */
					/* [5:0]: Internal Clock Pre-Scaler */
#define REG_COMA		0x12	/* [7] Reset */
#define REG_COMB		0x13
#define REG_COMC		0x14
#define REG_COMD		0x15
#define REG_COML		0x16
#define REG_HSTRT		0x17
#define REG_HSTOP		0x18
#define REG_VSTRT		0x19
#define REG_VSTOP		0x1a
#define REG_PSHFT		0x1b
#define REG_MIDH		0x1c
#define REG_MIDL		0x1d
#define REG_HSYNS		0x1e
#define REG_HSYNE		0x1f
#define REG_COME		0x20
#define REG_YOFF		0x21
#define REG_UOFF		0x22
#define REG_VOFF		0x23
#define REG_AEW			0x24
#define REG_AEB			0x25
#define REG_COMF		0x26
#define REG_COMG		0x27
#define REG_COMH		0x28
#define REG_COMI		0x29

#define REG_FRARL		0x2b
#define REG_COMJ		0x2c
#define REG_COMK		0x2d
#define REG_AVGY		0x2e
#define REG_REF0		0x2f
#define REG_REF1		0x30
#define REG_REF2		0x31
#define REG_FRAJH		0x32
#define REG_FRAJL		0x33
#define REG_FACT		0x34
#define REG_L1AEC		0x35
#define REG_AVGU		0x36
#define REG_AVGV		0x37

#define REG_SPCB		0x60
#define REG_SPCC		0x61
#define REG_GAM1		0x62
#define REG_GAM2		0x63
#define REG_GAM3		0x64
#define REG_SPCD		0x65

#define REG_SPCE		0x68
#define REG_ADCL		0x69

#define REG_RMCO		0x6c
#define REG_GMCO		0x6d
#define REG_BMCO		0x6e


/* Register bits, values, etc. */
#define OV6650_PIDH		0x66	/* high byte of product ID number */
#define OV6650_PIDL		0x50	/* low byte of product ID number */
#define OV6650_MIDH		0x7F	/* high byte of mfg ID */
#define OV6650_MIDL		0xA2	/* low byte of mfg ID */

#define DEF_GAIN		0x00
#define DEF_BLUE		0x80
#define DEF_RED			0x80

#define SAT_SHIFT		4
#define SAT_MASK		(0xf << SAT_SHIFT)
#define SET_SAT(x)		(((x) << SAT_SHIFT) & SAT_MASK)

#define HUE_EN			BIT(5)
#define HUE_MASK		0x1f
#define DEF_HUE			0x10
#define SET_HUE(x)		(HUE_EN | ((x) & HUE_MASK))

#define DEF_AECH		0x4D

127
#define CLKRC_8MHz		0x00
128 129 130 131 132
#define CLKRC_12MHz		0x40
#define CLKRC_16MHz		0x80
#define CLKRC_24MHz		0xc0
#define CLKRC_DIV_MASK		0x3f
#define GET_CLKRC_DIV(x)	(((x) & CLKRC_DIV_MASK) + 1)
133
#define DEF_CLKRC		0x00
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

#define COMA_RESET		BIT(7)
#define COMA_QCIF		BIT(5)
#define COMA_RAW_RGB		BIT(4)
#define COMA_RGB		BIT(3)
#define COMA_BW			BIT(2)
#define COMA_WORD_SWAP		BIT(1)
#define COMA_BYTE_SWAP		BIT(0)
#define DEF_COMA		0x00

#define COMB_FLIP_V		BIT(7)
#define COMB_FLIP_H		BIT(5)
#define COMB_BAND_FILTER	BIT(4)
#define COMB_AWB		BIT(2)
#define COMB_AGC		BIT(1)
#define COMB_AEC		BIT(0)
#define DEF_COMB		0x5f

#define COML_ONE_CHANNEL	BIT(7)

#define DEF_HSTRT		0x24
#define DEF_HSTOP		0xd4
#define DEF_VSTRT		0x04
#define DEF_VSTOP		0x94

#define COMF_HREF_LOW		BIT(4)

#define COMJ_PCLK_RISING	BIT(4)
#define COMJ_VSYNC_HIGH		BIT(0)

/* supported resolutions */
#define W_QCIF			(DEF_HSTOP - DEF_HSTRT)
#define W_CIF			(W_QCIF << 1)
#define H_QCIF			(DEF_VSTOP - DEF_VSTRT)
#define H_CIF			(H_QCIF << 1)

#define FRAME_RATE_MAX		30


struct ov6650_reg {
	u8	reg;
	u8	val;
};

struct ov6650 {
	struct v4l2_subdev	subdev;
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	struct v4l2_ctrl_handler hdl;
	struct {
		/* exposure/autoexposure cluster */
		struct v4l2_ctrl *autoexposure;
		struct v4l2_ctrl *exposure;
	};
	struct {
		/* gain/autogain cluster */
		struct v4l2_ctrl *autogain;
		struct v4l2_ctrl *gain;
	};
	struct {
		/* blue/red/autowhitebalance cluster */
		struct v4l2_ctrl *autowb;
		struct v4l2_ctrl *blue;
		struct v4l2_ctrl *red;
	};
197
	struct v4l2_clk		*clk;
198 199
	bool			half_scale;	/* scale down output by 2 */
	struct v4l2_rect	rect;		/* sensor cropping window */
200
	struct v4l2_fract	tpf;		/* as requested with s_frame_interval */
201
	u32 code;
202 203
};

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
struct ov6650_xclk {
	unsigned long	rate;
	u8		clkrc;
};

static const struct ov6650_xclk ov6650_xclk[] = {
{
	.rate	= 8000000,
	.clkrc	= CLKRC_8MHz,
},
{
	.rate	= 12000000,
	.clkrc	= CLKRC_12MHz,
},
{
	.rate	= 16000000,
	.clkrc	= CLKRC_16MHz,
},
{
	.rate	= 24000000,
	.clkrc	= CLKRC_24MHz,
},
};
227

228 229 230 231 232 233 234
static u32 ov6650_codes[] = {
	MEDIA_BUS_FMT_YUYV8_2X8,
	MEDIA_BUS_FMT_UYVY8_2X8,
	MEDIA_BUS_FMT_YVYU8_2X8,
	MEDIA_BUS_FMT_VYUY8_2X8,
	MEDIA_BUS_FMT_SBGGR8_1X8,
	MEDIA_BUS_FMT_Y8_1X8,
235 236
};

237 238 239 240 241 242 243 244 245 246 247
static const struct v4l2_mbus_framefmt ov6650_def_fmt = {
	.width		= W_CIF,
	.height		= H_CIF,
	.code		= MEDIA_BUS_FMT_SBGGR8_1X8,
	.colorspace	= V4L2_COLORSPACE_SRGB,
	.field		= V4L2_FIELD_NONE,
	.ycbcr_enc	= V4L2_YCBCR_ENC_DEFAULT,
	.quantization	= V4L2_QUANTIZATION_DEFAULT,
	.xfer_func	= V4L2_XFER_FUNC_DEFAULT,
};

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
/* read a register */
static int ov6650_reg_read(struct i2c_client *client, u8 reg, u8 *val)
{
	int ret;
	u8 data = reg;
	struct i2c_msg msg = {
		.addr	= client->addr,
		.flags	= 0,
		.len	= 1,
		.buf	= &data,
	};

	ret = i2c_transfer(client->adapter, &msg, 1);
	if (ret < 0)
		goto err;

	msg.flags = I2C_M_RD;
	ret = i2c_transfer(client->adapter, &msg, 1);
	if (ret < 0)
		goto err;

	*val = data;
	return 0;

err:
	dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
	return ret;
}

/* write a register */
static int ov6650_reg_write(struct i2c_client *client, u8 reg, u8 val)
{
	int ret;
	unsigned char data[2] = { reg, val };
	struct i2c_msg msg = {
		.addr	= client->addr,
		.flags	= 0,
		.len	= 2,
		.buf	= data,
	};

	ret = i2c_transfer(client->adapter, &msg, 1);
	udelay(100);

	if (ret < 0) {
		dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
		return ret;
	}
	return 0;
}


/* Read a register, alter its bits, write it back */
static int ov6650_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 mask)
{
	u8 val;
	int ret;

	ret = ov6650_reg_read(client, reg, &val);
	if (ret) {
		dev_err(&client->dev,
			"[Read]-Modify-Write of register 0x%02x failed!\n",
			reg);
		return ret;
	}

	val &= ~mask;
	val |= set;

	ret = ov6650_reg_write(client, reg, val);
	if (ret)
		dev_err(&client->dev,
			"Read-Modify-[Write] of register 0x%02x failed!\n",
			reg);

	return ret;
}

static struct ov6650 *to_ov6650(const struct i2c_client *client)
{
	return container_of(i2c_get_clientdata(client), struct ov6650, subdev);
}

/* Start/Stop streaming from the device */
static int ov6650_s_stream(struct v4l2_subdev *sd, int enable)
{
	return 0;
}

/* Get status of additional camera capabilities */
338
static int ov6550_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
339
{
340 341
	struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl);
	struct v4l2_subdev *sd = &priv->subdev;
342
	struct i2c_client *client = v4l2_get_subdevdata(sd);
343
	uint8_t reg, reg2;
344
	int ret;
345 346 347

	switch (ctrl->id) {
	case V4L2_CID_AUTOGAIN:
348 349 350 351
		ret = ov6650_reg_read(client, REG_GAIN, &reg);
		if (!ret)
			priv->gain->val = reg;
		return ret;
352
	case V4L2_CID_AUTO_WHITE_BALANCE:
353 354 355 356 357 358
		ret = ov6650_reg_read(client, REG_BLUE, &reg);
		if (!ret)
			ret = ov6650_reg_read(client, REG_RED, &reg2);
		if (!ret) {
			priv->blue->val = reg;
			priv->red->val = reg2;
359
		}
360
		return ret;
361
	case V4L2_CID_EXPOSURE_AUTO:
362 363 364 365
		ret = ov6650_reg_read(client, REG_AECH, &reg);
		if (!ret)
			priv->exposure->val = reg;
		return ret;
366
	}
367
	return -EINVAL;
368 369 370
}

/* Set status of additional camera capabilities */
371
static int ov6550_s_ctrl(struct v4l2_ctrl *ctrl)
372
{
373 374
	struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl);
	struct v4l2_subdev *sd = &priv->subdev;
375
	struct i2c_client *client = v4l2_get_subdevdata(sd);
376
	int ret;
377 378 379 380

	switch (ctrl->id) {
	case V4L2_CID_AUTOGAIN:
		ret = ov6650_reg_rmw(client, REG_COMB,
381 382 383 384
				ctrl->val ? COMB_AGC : 0, COMB_AGC);
		if (!ret && !ctrl->val)
			ret = ov6650_reg_write(client, REG_GAIN, priv->gain->val);
		return ret;
385 386
	case V4L2_CID_AUTO_WHITE_BALANCE:
		ret = ov6650_reg_rmw(client, REG_COMB,
387 388 389 390 391 392 393 394
				ctrl->val ? COMB_AWB : 0, COMB_AWB);
		if (!ret && !ctrl->val) {
			ret = ov6650_reg_write(client, REG_BLUE, priv->blue->val);
			if (!ret)
				ret = ov6650_reg_write(client, REG_RED,
							priv->red->val);
		}
		return ret;
395
	case V4L2_CID_SATURATION:
396
		return ov6650_reg_rmw(client, REG_SAT, SET_SAT(ctrl->val),
397 398
				SAT_MASK);
	case V4L2_CID_HUE:
399
		return ov6650_reg_rmw(client, REG_HUE, SET_HUE(ctrl->val),
400 401
				HUE_MASK);
	case V4L2_CID_BRIGHTNESS:
402
		return ov6650_reg_write(client, REG_BRT, ctrl->val);
403
	case V4L2_CID_EXPOSURE_AUTO:
404 405
		ret = ov6650_reg_rmw(client, REG_COMB, ctrl->val ==
				V4L2_EXPOSURE_AUTO ? COMB_AEC : 0, COMB_AEC);
406 407 408 409
		if (!ret && ctrl->val == V4L2_EXPOSURE_MANUAL)
			ret = ov6650_reg_write(client, REG_AECH,
						priv->exposure->val);
		return ret;
410
	case V4L2_CID_GAMMA:
411
		return ov6650_reg_write(client, REG_GAM1, ctrl->val);
412
	case V4L2_CID_VFLIP:
413 414
		return ov6650_reg_rmw(client, REG_COMB,
				ctrl->val ? COMB_FLIP_V : 0, COMB_FLIP_V);
415
	case V4L2_CID_HFLIP:
416 417
		return ov6650_reg_rmw(client, REG_COMB,
				ctrl->val ? COMB_FLIP_H : 0, COMB_FLIP_H);
418 419
	}

420
	return -EINVAL;
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
}

#ifdef CONFIG_VIDEO_ADV_DEBUG
static int ov6650_get_register(struct v4l2_subdev *sd,
				struct v4l2_dbg_register *reg)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	int ret;
	u8 val;

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

	reg->size = 1;

	ret = ov6650_reg_read(client, reg->reg, &val);
	if (!ret)
		reg->val = (__u64)val;

	return ret;
}

static int ov6650_set_register(struct v4l2_subdev *sd,
444
				const struct v4l2_dbg_register *reg)
445 446 447 448 449 450 451 452 453 454
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);

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

	return ov6650_reg_write(client, reg->reg, reg->val);
}
#endif

455 456 457
static int ov6650_s_power(struct v4l2_subdev *sd, int on)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
458
	struct ov6650 *priv = to_ov6650(client);
459
	int ret = 0;
460

461 462 463 464 465 466
	if (on)
		ret = v4l2_clk_enable(priv->clk);
	else
		v4l2_clk_disable(priv->clk);

	return ret;
467 468
}

469 470 471
static int ov6650_get_selection(struct v4l2_subdev *sd,
		struct v4l2_subdev_pad_config *cfg,
		struct v4l2_subdev_selection *sel)
472 473 474 475
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct ov6650 *priv = to_ov6650(client);

476 477
	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
		return -EINVAL;
478

479 480 481 482 483 484 485 486 487 488 489 490 491
	switch (sel->target) {
	case V4L2_SEL_TGT_CROP_BOUNDS:
		sel->r.left = DEF_HSTRT << 1;
		sel->r.top = DEF_VSTRT << 1;
		sel->r.width = W_CIF;
		sel->r.height = H_CIF;
		return 0;
	case V4L2_SEL_TGT_CROP:
		sel->r = priv->rect;
		return 0;
	default:
		return -EINVAL;
	}
492 493
}

494 495 496
static int ov6650_set_selection(struct v4l2_subdev *sd,
		struct v4l2_subdev_pad_config *cfg,
		struct v4l2_subdev_selection *sel)
497 498 499 500 501
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct ov6650 *priv = to_ov6650(client);
	int ret;

502 503
	if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
	    sel->target != V4L2_SEL_TGT_CROP)
504 505
		return -EINVAL;

506 507 508 509 510 511 512
	v4l_bound_align_image(&sel->r.width, 2, W_CIF, 1,
			      &sel->r.height, 2, H_CIF, 1, 0);
	v4l_bound_align_image(&sel->r.left, DEF_HSTRT << 1,
			      (DEF_HSTRT << 1) + W_CIF - (__s32)sel->r.width, 1,
			      &sel->r.top, DEF_VSTRT << 1,
			      (DEF_VSTRT << 1) + H_CIF - (__s32)sel->r.height,
			      1, 0);
513

514
	ret = ov6650_reg_write(client, REG_HSTRT, sel->r.left >> 1);
515
	if (!ret) {
516
		priv->rect.width += priv->rect.left - sel->r.left;
517
		priv->rect.left = sel->r.left;
518
		ret = ov6650_reg_write(client, REG_HSTOP,
519
				       (sel->r.left + sel->r.width) >> 1);
520 521
	}
	if (!ret) {
522 523
		priv->rect.width = sel->r.width;
		ret = ov6650_reg_write(client, REG_VSTRT, sel->r.top >> 1);
524 525
	}
	if (!ret) {
526
		priv->rect.height += priv->rect.top - sel->r.top;
527
		priv->rect.top = sel->r.top;
528
		ret = ov6650_reg_write(client, REG_VSTOP,
529
				       (sel->r.top + sel->r.height) >> 1);
530 531
	}
	if (!ret)
532
		priv->rect.height = sel->r.height;
533 534 535 536

	return ret;
}

537 538 539
static int ov6650_get_fmt(struct v4l2_subdev *sd,
		struct v4l2_subdev_pad_config *cfg,
		struct v4l2_subdev_format *format)
540
{
541
	struct v4l2_mbus_framefmt *mf = &format->format;
542 543 544
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct ov6650 *priv = to_ov6650(client);

545 546 547
	if (format->pad)
		return -EINVAL;

548 549 550 551
	/* initialize response with default media bus frame format */
	*mf = ov6650_def_fmt;

	/* update media bus format code and frame size */
552 553 554 555 556 557 558 559 560 561
	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
		mf->width = cfg->try_fmt.width;
		mf->height = cfg->try_fmt.height;
		mf->code = cfg->try_fmt.code;

	} else {
		mf->width = priv->rect.width >> priv->half_scale;
		mf->height = priv->rect.height >> priv->half_scale;
		mf->code = priv->code;
	}
562 563 564 565 566
	return 0;
}

static bool is_unscaled_ok(int width, int height, struct v4l2_rect *rect)
{
567
	return width > rect->width >> 1 || height > rect->height >> 1;
568 569
}

570
#define to_clkrc(div)	((div) - 1)
571 572 573 574 575 576 577

/* set the format we will capture in */
static int ov6650_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct ov6650 *priv = to_ov6650(client);
	bool half_scale = !is_unscaled_ok(mf->width, mf->height, &priv->rect);
578 579 580 581 582 583 584 585 586
	struct v4l2_subdev_selection sel = {
		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
		.target = V4L2_SEL_TGT_CROP,
		.r.left = priv->rect.left + (priv->rect.width >> 1) -
			(mf->width >> (1 - half_scale)),
		.r.top = priv->rect.top + (priv->rect.height >> 1) -
			(mf->height >> (1 - half_scale)),
		.r.width = mf->width << half_scale,
		.r.height = mf->height << half_scale,
587
	};
588
	u32 code = mf->code;
589
	u8 coma_set = 0, coma_mask = 0, coml_set, coml_mask;
590 591 592 593
	int ret;

	/* select color matrix configuration for given color encoding */
	switch (code) {
594
	case MEDIA_BUS_FMT_Y8_1X8:
595 596 597 598
		dev_dbg(&client->dev, "pixel format GREY8_1X8\n");
		coma_mask |= COMA_RGB | COMA_WORD_SWAP | COMA_BYTE_SWAP;
		coma_set |= COMA_BW;
		break;
599
	case MEDIA_BUS_FMT_YUYV8_2X8:
600 601 602 603
		dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n");
		coma_mask |= COMA_RGB | COMA_BW | COMA_BYTE_SWAP;
		coma_set |= COMA_WORD_SWAP;
		break;
604
	case MEDIA_BUS_FMT_YVYU8_2X8:
605 606 607 608
		dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n");
		coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP |
				COMA_BYTE_SWAP;
		break;
609
	case MEDIA_BUS_FMT_UYVY8_2X8:
610 611 612 613 614 615 616 617 618
		dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n");
		if (half_scale) {
			coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
			coma_set |= COMA_BYTE_SWAP;
		} else {
			coma_mask |= COMA_RGB | COMA_BW;
			coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
		}
		break;
619
	case MEDIA_BUS_FMT_VYUY8_2X8:
620 621 622 623 624 625 626 627 628
		dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n");
		if (half_scale) {
			coma_mask |= COMA_RGB | COMA_BW;
			coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
		} else {
			coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
			coma_set |= COMA_BYTE_SWAP;
		}
		break;
629
	case MEDIA_BUS_FMT_SBGGR8_1X8:
630 631 632 633 634 635 636 637 638
		dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n");
		coma_mask |= COMA_BW | COMA_BYTE_SWAP | COMA_WORD_SWAP;
		coma_set |= COMA_RAW_RGB | COMA_RGB;
		break;
	default:
		dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code);
		return -EINVAL;
	}

639 640
	if (code == MEDIA_BUS_FMT_Y8_1X8 ||
			code == MEDIA_BUS_FMT_SBGGR8_1X8) {
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
		coml_mask = COML_ONE_CHANNEL;
		coml_set = 0;
	} else {
		coml_mask = 0;
		coml_set = COML_ONE_CHANNEL;
	}

	if (half_scale) {
		dev_dbg(&client->dev, "max resolution: QCIF\n");
		coma_set |= COMA_QCIF;
	} else {
		dev_dbg(&client->dev, "max resolution: CIF\n");
		coma_mask |= COMA_QCIF;
	}

656
	ret = ov6650_set_selection(sd, NULL, &sel);
657 658
	if (!ret)
		ret = ov6650_reg_rmw(client, REG_COMA, coma_set, coma_mask);
659 660 661
	if (!ret) {
		priv->half_scale = half_scale;

662
		ret = ov6650_reg_rmw(client, REG_COML, coml_set, coml_mask);
663 664 665
	}
	if (!ret)
		priv->code = code;
666 667 668 669

	return ret;
}

670 671 672
static int ov6650_set_fmt(struct v4l2_subdev *sd,
		struct v4l2_subdev_pad_config *cfg,
		struct v4l2_subdev_format *format)
673
{
674
	struct v4l2_mbus_framefmt *mf = &format->format;
675 676 677
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct ov6650 *priv = to_ov6650(client);

678 679 680
	if (format->pad)
		return -EINVAL;

681 682 683 684 685
	if (is_unscaled_ok(mf->width, mf->height, &priv->rect))
		v4l_bound_align_image(&mf->width, 2, W_CIF, 1,
				&mf->height, 2, H_CIF, 1, 0);

	switch (mf->code) {
686 687
	case MEDIA_BUS_FMT_Y10_1X10:
		mf->code = MEDIA_BUS_FMT_Y8_1X8;
688
		/* fall through */
689 690 691 692 693
	case MEDIA_BUS_FMT_Y8_1X8:
	case MEDIA_BUS_FMT_YVYU8_2X8:
	case MEDIA_BUS_FMT_YUYV8_2X8:
	case MEDIA_BUS_FMT_VYUY8_2X8:
	case MEDIA_BUS_FMT_UYVY8_2X8:
694 695
		break;
	default:
696
		mf->code = MEDIA_BUS_FMT_SBGGR8_1X8;
697
		/* fall through */
698
	case MEDIA_BUS_FMT_SBGGR8_1X8:
699 700 701
		break;
	}

702 703 704 705 706
	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
		/* store media bus format code and frame size in pad config */
		cfg->try_fmt.width = mf->width;
		cfg->try_fmt.height = mf->height;
		cfg->try_fmt.code = mf->code;
707

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
		/* return default mbus frame format updated with pad config */
		*mf = ov6650_def_fmt;
		mf->width = cfg->try_fmt.width;
		mf->height = cfg->try_fmt.height;
		mf->code = cfg->try_fmt.code;

	} else {
		/* apply new media bus format code and frame size */
		int ret = ov6650_s_fmt(sd, mf);

		if (ret)
			return ret;

		/* return default format updated with active size and code */
		*mf = ov6650_def_fmt;
		mf->width = priv->rect.width >> priv->half_scale;
		mf->height = priv->rect.height >> priv->half_scale;
		mf->code = priv->code;
	}
727 728 729
	return 0;
}

730 731 732
static int ov6650_enum_mbus_code(struct v4l2_subdev *sd,
		struct v4l2_subdev_pad_config *cfg,
		struct v4l2_subdev_mbus_code_enum *code)
733
{
734
	if (code->pad || code->index >= ARRAY_SIZE(ov6650_codes))
735 736
		return -EINVAL;

737
	code->code = ov6650_codes[code->index];
738 739 740
	return 0;
}

741 742
static int ov6650_g_frame_interval(struct v4l2_subdev *sd,
				   struct v4l2_subdev_frame_interval *ival)
743 744 745 746
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct ov6650 *priv = to_ov6650(client);

747
	ival->interval = priv->tpf;
748 749

	dev_dbg(&client->dev, "Frame interval: %u/%u s\n",
750
		ival->interval.numerator, ival->interval.denominator);
751 752 753 754

	return 0;
}

755 756
static int ov6650_s_frame_interval(struct v4l2_subdev *sd,
				   struct v4l2_subdev_frame_interval *ival)
757 758 759
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	struct ov6650 *priv = to_ov6650(client);
760
	struct v4l2_fract *tpf = &ival->interval;
761 762 763 764 765 766 767 768 769 770 771 772
	int div, ret;

	if (tpf->numerator == 0 || tpf->denominator == 0)
		div = 1;  /* Reset to full rate */
	else
		div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator;

	if (div == 0)
		div = 1;
	else if (div > GET_CLKRC_DIV(CLKRC_DIV_MASK))
		div = GET_CLKRC_DIV(CLKRC_DIV_MASK);

773
	ret = ov6650_reg_rmw(client, REG_CLKRC, to_clkrc(div), CLKRC_DIV_MASK);
774
	if (!ret) {
775
		priv->tpf.numerator = div;
776 777 778
		priv->tpf.denominator = FRAME_RATE_MAX;

		*tpf = priv->tpf;
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	}

	return ret;
}

/* Soft reset the camera. This has nothing to do with the RESET pin! */
static int ov6650_reset(struct i2c_client *client)
{
	int ret;

	dev_dbg(&client->dev, "reset\n");

	ret = ov6650_reg_rmw(client, REG_COMA, COMA_RESET, 0);
	if (ret)
		dev_err(&client->dev,
L
Lucas De Marchi 已提交
794
			"An error occurred while entering soft reset!\n");
795 796 797 798 799

	return ret;
}

/* program default register values */
800
static int ov6650_prog_dflt(struct i2c_client *client, u8 clkrc)
801 802 803 804 805 806
{
	int ret;

	dev_dbg(&client->dev, "initializing\n");

	ret = ov6650_reg_write(client, REG_COMA, 0);	/* ~COMA_RESET */
807
	if (!ret)
808
		ret = ov6650_reg_write(client, REG_CLKRC, clkrc);
809 810 811 812 813 814
	if (!ret)
		ret = ov6650_reg_rmw(client, REG_COMB, 0, COMB_BAND_FILTER);

	return ret;
}

815
static int ov6650_video_probe(struct v4l2_subdev *sd)
816
{
817
	struct i2c_client *client = v4l2_get_subdevdata(sd);
818
	struct ov6650 *priv = to_ov6650(client);
819 820 821
	const struct ov6650_xclk *xclk = NULL;
	unsigned long rate;
	u8 pidh, pidl, midh, midl;
H
Hans Verkuil 已提交
822
	int i, ret = 0;
823

824 825 826 827 828 829 830
	priv->clk = v4l2_clk_get(&client->dev, NULL);
	if (IS_ERR(priv->clk)) {
		ret = PTR_ERR(priv->clk);
		dev_err(&client->dev, "v4l2_clk request err: %d\n", ret);
		return ret;
	}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
	rate = v4l2_clk_get_rate(priv->clk);
	for (i = 0; rate && i < ARRAY_SIZE(ov6650_xclk); i++) {
		if (rate != ov6650_xclk[i].rate)
			continue;

		xclk = &ov6650_xclk[i];
		dev_info(&client->dev, "using host default clock rate %lukHz\n",
			 rate / 1000);
		break;
	}
	for (i = 0; !xclk && i < ARRAY_SIZE(ov6650_xclk); i++) {
		ret = v4l2_clk_set_rate(priv->clk, ov6650_xclk[i].rate);
		if (ret || v4l2_clk_get_rate(priv->clk) != ov6650_xclk[i].rate)
			continue;

		xclk = &ov6650_xclk[i];
		dev_info(&client->dev, "using negotiated clock rate %lukHz\n",
			 xclk->rate / 1000);
		break;
	}
	if (!xclk) {
		dev_err(&client->dev, "unable to get supported clock rate\n");
		if (!ret)
			ret = -EINVAL;
		goto eclkput;
	}

858
	ret = ov6650_s_power(sd, 1);
859
	if (ret < 0)
860
		goto eclkput;
861

862 863
	msleep(20);

864 865 866 867 868 869 870 871 872 873 874 875
	/*
	 * check and show product ID and manufacturer ID
	 */
	ret = ov6650_reg_read(client, REG_PIDH, &pidh);
	if (!ret)
		ret = ov6650_reg_read(client, REG_PIDL, &pidl);
	if (!ret)
		ret = ov6650_reg_read(client, REG_MIDH, &midh);
	if (!ret)
		ret = ov6650_reg_read(client, REG_MIDL, &midl);

	if (ret)
876
		goto done;
877 878 879 880

	if ((pidh != OV6650_PIDH) || (pidl != OV6650_PIDL)) {
		dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n",
				pidh, pidl);
881 882
		ret = -ENODEV;
		goto done;
883 884 885 886 887 888 889 890
	}

	dev_info(&client->dev,
		"ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
		pidh, pidl, midh, midl);

	ret = ov6650_reset(client);
	if (!ret)
891
		ret = ov6650_prog_dflt(client, xclk->clkrc);
892 893 894 895 896
	if (!ret) {
		struct v4l2_mbus_framefmt mf = ov6650_def_fmt;

		ret = ov6650_s_fmt(sd, &mf);
	}
897 898
	if (!ret)
		ret = v4l2_ctrl_handler_setup(&priv->hdl);
899

900
done:
901
	ov6650_s_power(sd, 0);
902 903 904 905 906
	if (!ret)
		return 0;
eclkput:
	v4l2_clk_put(priv->clk);

907 908 909
	return ret;
}

910 911 912
static const struct v4l2_ctrl_ops ov6550_ctrl_ops = {
	.g_volatile_ctrl = ov6550_g_volatile_ctrl,
	.s_ctrl = ov6550_s_ctrl,
913 914
};

915
static const struct v4l2_subdev_core_ops ov6650_core_ops = {
916 917 918 919
#ifdef CONFIG_VIDEO_ADV_DEBUG
	.g_register		= ov6650_get_register,
	.s_register		= ov6650_set_register,
#endif
920
	.s_power		= ov6650_s_power,
921 922
};

923
/* Request bus settings on camera side */
924 925 926 927 928 929 930 931 932 933 934 935 936 937
static int ov6650_g_mbus_config(struct v4l2_subdev *sd,
				struct v4l2_mbus_config *cfg)
{

	cfg->flags = V4L2_MBUS_MASTER |
		V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
		V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
		V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
		V4L2_MBUS_DATA_ACTIVE_HIGH;
	cfg->type = V4L2_MBUS_PARALLEL;

	return 0;
}

938
/* Alter bus settings on camera side */
939 940 941 942 943 944
static int ov6650_s_mbus_config(struct v4l2_subdev *sd,
				const struct v4l2_mbus_config *cfg)
{
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	int ret;

945
	if (cfg->flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
946 947 948 949 950 951
		ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_PCLK_RISING, 0);
	else
		ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_PCLK_RISING);
	if (ret)
		return ret;

952
	if (cfg->flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
953 954 955 956 957 958
		ret = ov6650_reg_rmw(client, REG_COMF, COMF_HREF_LOW, 0);
	else
		ret = ov6650_reg_rmw(client, REG_COMF, 0, COMF_HREF_LOW);
	if (ret)
		return ret;

959
	if (cfg->flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
960 961 962 963 964 965 966
		ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_VSYNC_HIGH, 0);
	else
		ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_VSYNC_HIGH);

	return ret;
}

967
static const struct v4l2_subdev_video_ops ov6650_video_ops = {
968
	.s_stream	= ov6650_s_stream,
969 970
	.g_frame_interval = ov6650_g_frame_interval,
	.s_frame_interval = ov6650_s_frame_interval,
971 972
	.g_mbus_config	= ov6650_g_mbus_config,
	.s_mbus_config	= ov6650_s_mbus_config,
973 974
};

975 976
static const struct v4l2_subdev_pad_ops ov6650_pad_ops = {
	.enum_mbus_code = ov6650_enum_mbus_code,
977 978
	.get_selection	= ov6650_get_selection,
	.set_selection	= ov6650_set_selection,
979
	.get_fmt	= ov6650_get_fmt,
980
	.set_fmt	= ov6650_set_fmt,
981 982
};

983
static const struct v4l2_subdev_ops ov6650_subdev_ops = {
984 985
	.core	= &ov6650_core_ops,
	.video	= &ov6650_video_ops,
986
	.pad	= &ov6650_pad_ops,
987 988
};

989 990 991 992
static const struct v4l2_subdev_internal_ops ov6650_internal_ops = {
	.registered = ov6650_video_probe,
};

993 994 995 996 997 998 999 1000 1001
/*
 * i2c_driver function
 */
static int ov6650_probe(struct i2c_client *client,
			const struct i2c_device_id *did)
{
	struct ov6650 *priv;
	int ret;

1002
	priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
1003
	if (!priv)
1004 1005 1006
		return -ENOMEM;

	v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	v4l2_ctrl_handler_init(&priv->hdl, 13);
	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_VFLIP, 0, 1, 1, 0);
	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_HFLIP, 0, 1, 1, 0);
	priv->autogain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
	priv->gain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_GAIN, 0, 0x3f, 1, DEF_GAIN);
	priv->autowb = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
	priv->blue = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_BLUE_BALANCE, 0, 0xff, 1, DEF_BLUE);
	priv->red = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_RED_BALANCE, 0, 0xff, 1, DEF_RED);
	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_SATURATION, 0, 0xf, 1, 0x8);
	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_HUE, 0, HUE_MASK, 1, DEF_HUE);
	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_BRIGHTNESS, 0, 0xff, 1, 0x80);
	priv->autoexposure = v4l2_ctrl_new_std_menu(&priv->hdl,
1029 1030
			&ov6550_ctrl_ops, V4L2_CID_EXPOSURE_AUTO,
			V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
1031 1032 1033 1034 1035 1036
	priv->exposure = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_EXPOSURE, 0, 0xff, 1, DEF_AECH);
	v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
			V4L2_CID_GAMMA, 0, 0xff, 1, 0x12);

	priv->subdev.ctrl_handler = &priv->hdl;
1037 1038 1039 1040
	if (priv->hdl.error) {
		ret = priv->hdl.error;
		goto ectlhdlfree;
	}
1041

1042 1043 1044 1045
	v4l2_ctrl_auto_cluster(2, &priv->autogain, 0, true);
	v4l2_ctrl_auto_cluster(3, &priv->autowb, 0, true);
	v4l2_ctrl_auto_cluster(2, &priv->autoexposure,
				V4L2_EXPOSURE_MANUAL, true);
1046 1047 1048 1049 1050 1051

	priv->rect.left	  = DEF_HSTRT << 1;
	priv->rect.top	  = DEF_VSTRT << 1;
	priv->rect.width  = W_CIF;
	priv->rect.height = H_CIF;

1052 1053 1054 1055
	/* Hardware default frame interval */
	priv->tpf.numerator   = GET_CLKRC_DIV(DEF_CLKRC);
	priv->tpf.denominator = FRAME_RATE_MAX;

1056 1057 1058
	priv->subdev.internal_ops = &ov6650_internal_ops;

	ret = v4l2_async_register_subdev(&priv->subdev);
1059 1060 1061 1062
	if (!ret)
		return 0;
ectlhdlfree:
	v4l2_ctrl_handler_free(&priv->hdl);
1063 1064 1065 1066 1067 1068 1069 1070

	return ret;
}

static int ov6650_remove(struct i2c_client *client)
{
	struct ov6650 *priv = to_ov6650(client);

1071
	v4l2_clk_put(priv->clk);
1072
	v4l2_async_unregister_subdev(&priv->subdev);
1073
	v4l2_ctrl_handler_free(&priv->hdl);
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
	return 0;
}

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

static struct i2c_driver ov6650_i2c_driver = {
	.driver = {
		.name = "ov6650",
	},
	.probe    = ov6650_probe,
	.remove   = ov6650_remove,
	.id_table = ov6650_id,
};

1092
module_i2c_driver(ov6650_i2c_driver);
1093

1094 1095
MODULE_DESCRIPTION("V4L2 subdevice driver for OmniVision OV6650 camera sensor");
MODULE_AUTHOR("Janusz Krzysztofik <jmkrzyszt@gmail.com");
1096
MODULE_LICENSE("GPL v2");