adv7180.c 26.9 KB
Newer Older
1 2 3
/*
 * adv7180.c Analog Devices ADV7180 video decoder driver
 * Copyright (c) 2009 Intel Corporation
4 5
 * Copyright (C) 2013 Cogent Embedded, Inc.
 * Copyright (C) 2013 Renesas Solutions Corp.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/interrupt.h>
#include <linux/i2c.h>
27
#include <linux/slab.h>
28 29 30
#include <media/v4l2-ioctl.h>
#include <linux/videodev2.h>
#include <media/v4l2-device.h>
31
#include <media/v4l2-ctrls.h>
32
#include <linux/mutex.h>
33
#include <linux/delay.h>
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#define ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM		0x0
#define ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM_PED		0x1
#define ADV7180_STD_AD_PAL_N_NTSC_J_SECAM		0x2
#define ADV7180_STD_AD_PAL_N_NTSC_M_SECAM		0x3
#define ADV7180_STD_NTSC_J				0x4
#define ADV7180_STD_NTSC_M				0x5
#define ADV7180_STD_PAL60				0x6
#define ADV7180_STD_NTSC_443				0x7
#define ADV7180_STD_PAL_BG				0x8
#define ADV7180_STD_PAL_N				0x9
#define ADV7180_STD_PAL_M				0xa
#define ADV7180_STD_PAL_M_PED				0xb
#define ADV7180_STD_PAL_COMB_N				0xc
#define ADV7180_STD_PAL_COMB_N_PED			0xd
#define ADV7180_STD_PAL_SECAM				0xe
#define ADV7180_STD_PAL_SECAM_PED			0xf

52
#define ADV7180_REG_INPUT_CONTROL			0x0000
53
#define ADV7180_INPUT_CONTROL_INSEL_MASK		0x0f
54

55 56
#define ADV7182_REG_INPUT_VIDSEL			0x0002

57
#define ADV7180_REG_EXTENDED_OUTPUT_CONTROL		0x0004
58
#define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS		0xC5
59

60
#define ADV7180_REG_AUTODETECT_ENABLE			0x07
61
#define ADV7180_AUTODETECT_DEFAULT			0x7f
62
/* Contrast */
63
#define ADV7180_REG_CON		0x0008	/*Unsigned */
64 65 66 67
#define ADV7180_CON_MIN		0
#define ADV7180_CON_DEF		128
#define ADV7180_CON_MAX		255
/* Brightness*/
68
#define ADV7180_REG_BRI		0x000a	/*Signed */
69 70 71 72
#define ADV7180_BRI_MIN		-128
#define ADV7180_BRI_DEF		0
#define ADV7180_BRI_MAX		127
/* Hue */
73
#define ADV7180_REG_HUE		0x000b	/*Signed, inverted */
74 75 76
#define ADV7180_HUE_MIN		-127
#define ADV7180_HUE_DEF		0
#define ADV7180_HUE_MAX		128
77

78
#define ADV7180_REG_CTRL		0x000e
79
#define ADV7180_CTRL_IRQ_SPACE		0x20
80

81
#define ADV7180_REG_PWR_MAN		0x0f
82 83 84 85
#define ADV7180_PWR_MAN_ON		0x04
#define ADV7180_PWR_MAN_OFF		0x24
#define ADV7180_PWR_MAN_RES		0x80

86
#define ADV7180_REG_STATUS1		0x0010
87 88
#define ADV7180_STATUS1_IN_LOCK		0x01
#define ADV7180_STATUS1_AUTOD_MASK	0x70
89 90 91 92 93 94 95 96 97
#define ADV7180_STATUS1_AUTOD_NTSM_M_J	0x00
#define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
#define ADV7180_STATUS1_AUTOD_PAL_M	0x20
#define ADV7180_STATUS1_AUTOD_PAL_60	0x30
#define ADV7180_STATUS1_AUTOD_PAL_B_G	0x40
#define ADV7180_STATUS1_AUTOD_SECAM	0x50
#define ADV7180_STATUS1_AUTOD_PAL_COMB	0x60
#define ADV7180_STATUS1_AUTOD_SECAM_525	0x70

98
#define ADV7180_REG_IDENT 0x0011
99 100
#define ADV7180_ID_7180 0x18

101
#define ADV7180_REG_ICONF1		0x0040
102 103 104
#define ADV7180_ICONF1_ACTIVE_LOW	0x01
#define ADV7180_ICONF1_PSYNC_ONLY	0x10
#define ADV7180_ICONF1_ACTIVE_TO_CLR	0xC0
105
/* Saturation */
106 107
#define ADV7180_REG_SD_SAT_CB	0x00e3	/*Unsigned */
#define ADV7180_REG_SD_SAT_CR	0x00e4	/*Unsigned */
108 109 110
#define ADV7180_SAT_MIN		0
#define ADV7180_SAT_DEF		128
#define ADV7180_SAT_MAX		255
111

112 113
#define ADV7180_IRQ1_LOCK	0x01
#define ADV7180_IRQ1_UNLOCK	0x02
114 115 116 117
#define ADV7180_REG_ISR1	0x0042
#define ADV7180_REG_ICR1	0x0043
#define ADV7180_REG_IMR1	0x0044
#define ADV7180_REG_IMR2	0x0048
118
#define ADV7180_IRQ3_AD_CHANGE	0x08
119 120 121
#define ADV7180_REG_ISR3	0x004A
#define ADV7180_REG_ICR3	0x004B
#define ADV7180_REG_IMR3	0x004C
122
#define ADV7180_REG_IMR4	0x50
123

124
#define ADV7180_REG_NTSC_V_BIT_END	0x00E6
125 126
#define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND	0x4F

127 128 129 130 131 132 133 134 135 136 137 138
#define ADV7180_INPUT_CVBS_AIN1 0x00
#define ADV7180_INPUT_CVBS_AIN2 0x01
#define ADV7180_INPUT_CVBS_AIN3 0x02
#define ADV7180_INPUT_CVBS_AIN4 0x03
#define ADV7180_INPUT_CVBS_AIN5 0x04
#define ADV7180_INPUT_CVBS_AIN6 0x05
#define ADV7180_INPUT_SVIDEO_AIN1_AIN2 0x06
#define ADV7180_INPUT_SVIDEO_AIN3_AIN4 0x07
#define ADV7180_INPUT_SVIDEO_AIN5_AIN6 0x08
#define ADV7180_INPUT_YPRPB_AIN1_AIN2_AIN3 0x09
#define ADV7180_INPUT_YPRPB_AIN4_AIN5_AIN6 0x0a

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#define ADV7182_INPUT_CVBS_AIN1 0x00
#define ADV7182_INPUT_CVBS_AIN2 0x01
#define ADV7182_INPUT_CVBS_AIN3 0x02
#define ADV7182_INPUT_CVBS_AIN4 0x03
#define ADV7182_INPUT_CVBS_AIN5 0x04
#define ADV7182_INPUT_CVBS_AIN6 0x05
#define ADV7182_INPUT_CVBS_AIN7 0x06
#define ADV7182_INPUT_CVBS_AIN8 0x07
#define ADV7182_INPUT_SVIDEO_AIN1_AIN2 0x08
#define ADV7182_INPUT_SVIDEO_AIN3_AIN4 0x09
#define ADV7182_INPUT_SVIDEO_AIN5_AIN6 0x0a
#define ADV7182_INPUT_SVIDEO_AIN7_AIN8 0x0b
#define ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3 0x0c
#define ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6 0x0d
#define ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2 0x0e
#define ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4 0x0f
#define ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6 0x10
#define ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8 0x11

158 159 160
struct adv7180_state;

#define ADV7180_FLAG_RESET_POWERED	BIT(0)
161
#define ADV7180_FLAG_V2			BIT(1)
162 163 164 165 166 167 168 169 170

struct adv7180_chip_info {
	unsigned int flags;
	unsigned int valid_input_mask;
	int (*set_std)(struct adv7180_state *st, unsigned int std);
	int (*select_input)(struct adv7180_state *st, unsigned int input);
	int (*init)(struct adv7180_state *state);
};

171
struct adv7180_state {
172
	struct v4l2_ctrl_handler ctrl_hdl;
173
	struct v4l2_subdev	sd;
174
	struct media_pad	pad;
175 176
	struct mutex		mutex; /* mutual excl. when accessing chip */
	int			irq;
177 178
	v4l2_std_id		curr_norm;
	bool			autodetect;
179
	bool			powered;
180
	u8			input;
181 182 183

	struct i2c_client	*client;
	unsigned int		register_page;
184
	const struct adv7180_chip_info *chip_info;
185
};
186 187 188
#define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler,		\
					    struct adv7180_state,	\
					    ctrl_hdl)->sd)
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
static int adv7180_select_page(struct adv7180_state *state, unsigned int page)
{
	if (state->register_page != page) {
		i2c_smbus_write_byte_data(state->client, ADV7180_REG_CTRL,
			page);
		state->register_page = page;
	}

	return 0;
}

static int adv7180_write(struct adv7180_state *state, unsigned int reg,
	unsigned int value)
{
	lockdep_assert_held(&state->mutex);
	adv7180_select_page(state, reg >> 8);
	return i2c_smbus_write_byte_data(state->client, reg & 0xff, value);
}

static int adv7180_read(struct adv7180_state *state, unsigned int reg)
{
	lockdep_assert_held(&state->mutex);
	adv7180_select_page(state, reg >> 8);
	return i2c_smbus_read_byte_data(state->client, reg & 0xff);
}

216 217 218 219 220
static int adv7180_set_video_standard(struct adv7180_state *state,
	unsigned int std)
{
	return state->chip_info->set_std(state, std);
}
221

222
static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
223
{
224 225 226 227
	/* in case V4L2_IN_ST_NO_SIGNAL */
	if (!(status1 & ADV7180_STATUS1_IN_LOCK))
		return V4L2_STD_UNKNOWN;

228 229
	switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
	case ADV7180_STATUS1_AUTOD_NTSM_M_J:
230
		return V4L2_STD_NTSC;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	case ADV7180_STATUS1_AUTOD_NTSC_4_43:
		return V4L2_STD_NTSC_443;
	case ADV7180_STATUS1_AUTOD_PAL_M:
		return V4L2_STD_PAL_M;
	case ADV7180_STATUS1_AUTOD_PAL_60:
		return V4L2_STD_PAL_60;
	case ADV7180_STATUS1_AUTOD_PAL_B_G:
		return V4L2_STD_PAL;
	case ADV7180_STATUS1_AUTOD_SECAM:
		return V4L2_STD_SECAM;
	case ADV7180_STATUS1_AUTOD_PAL_COMB:
		return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
	case ADV7180_STATUS1_AUTOD_SECAM_525:
		return V4L2_STD_SECAM;
	default:
		return V4L2_STD_UNKNOWN;
	}
}

250 251 252
static int v4l2_std_to_adv7180(v4l2_std_id std)
{
	if (std == V4L2_STD_PAL_60)
253
		return ADV7180_STD_PAL60;
254
	if (std == V4L2_STD_NTSC_443)
255
		return ADV7180_STD_NTSC_443;
256
	if (std == V4L2_STD_PAL_N)
257
		return ADV7180_STD_PAL_N;
258
	if (std == V4L2_STD_PAL_M)
259
		return ADV7180_STD_PAL_M;
260
	if (std == V4L2_STD_PAL_Nc)
261
		return ADV7180_STD_PAL_COMB_N;
262 263

	if (std & V4L2_STD_PAL)
264
		return ADV7180_STD_PAL_BG;
265
	if (std & V4L2_STD_NTSC)
266
		return ADV7180_STD_NTSC_M;
267
	if (std & V4L2_STD_SECAM)
268
		return ADV7180_STD_PAL_SECAM;
269 270 271 272

	return -EINVAL;
}

273 274 275 276 277 278 279 280
static u32 adv7180_status_to_v4l2(u8 status1)
{
	if (!(status1 & ADV7180_STATUS1_IN_LOCK))
		return V4L2_IN_ST_NO_SIGNAL;

	return 0;
}

281
static int __adv7180_status(struct adv7180_state *state, u32 *status,
282
			    v4l2_std_id *std)
283
{
284
	int status1 = adv7180_read(state, ADV7180_REG_STATUS1);
285 286 287 288 289 290 291 292 293 294 295 296

	if (status1 < 0)
		return status1;

	if (status)
		*status = adv7180_status_to_v4l2(status1);
	if (std)
		*std = adv7180_std_to_v4l2(status1);

	return 0;
}

297 298 299 300 301 302 303
static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
{
	return container_of(sd, struct adv7180_state, sd);
}

static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
{
304
	struct adv7180_state *state = to_state(sd);
305 306 307
	int err = mutex_lock_interruptible(&state->mutex);
	if (err)
		return err;
308

309 310
	/* when we are interrupt driven we know the state */
	if (!state->autodetect || state->irq > 0)
311 312
		*std = state->curr_norm;
	else
313
		err = __adv7180_status(state, NULL, std);
314

315
	mutex_unlock(&state->mutex);
316
	return err;
317
}
318

319 320 321 322 323 324 325 326 327
static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
			     u32 output, u32 config)
{
	struct adv7180_state *state = to_state(sd);
	int ret = mutex_lock_interruptible(&state->mutex);

	if (ret)
		return ret;

328 329
	if (input > 31 || !(BIT(input) & state->chip_info->valid_input_mask)) {
		ret = -EINVAL;
330
		goto out;
331
	}
332

333
	ret = state->chip_info->select_input(state, input);
334

335 336
	if (ret == 0)
		state->input = input;
337 338 339 340 341
out:
	mutex_unlock(&state->mutex);
	return ret;
}

342 343
static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
{
344 345 346 347 348
	struct adv7180_state *state = to_state(sd);
	int ret = mutex_lock_interruptible(&state->mutex);
	if (ret)
		return ret;

349
	ret = __adv7180_status(state, status, NULL);
350 351
	mutex_unlock(&state->mutex);
	return ret;
352 353
}

354
static int adv7180_program_std(struct adv7180_state *state)
355
{
356
	int ret;
357

358
	if (state->autodetect) {
359 360
		ret = adv7180_set_video_standard(state,
			ADV7180_STD_AD_PAL_BG_NTSC_J_SECAM);
361
		if (ret < 0)
362
			return ret;
363

364
		__adv7180_status(state, NULL, &state->curr_norm);
365
	} else {
366
		ret = v4l2_std_to_adv7180(state->curr_norm);
367
		if (ret < 0)
368
			return ret;
369

370
		ret = adv7180_set_video_standard(state, ret);
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
		if (ret < 0)
			return ret;
	}

	return 0;
}

static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
{
	struct adv7180_state *state = to_state(sd);
	int ret = mutex_lock_interruptible(&state->mutex);

	if (ret)
		return ret;

	/* all standards -> autodetect */
	if (std == V4L2_STD_ALL) {
		state->autodetect = true;
	} else {
		/* Make sure we can support this std */
		ret = v4l2_std_to_adv7180(std);
392 393 394 395 396 397
		if (ret < 0)
			goto out;

		state->curr_norm = std;
		state->autodetect = false;
	}
398 399

	ret = adv7180_program_std(state);
400
out:
401
	mutex_unlock(&state->mutex);
402 403 404
	return ret;
}

405
static int adv7180_set_power(struct adv7180_state *state, bool on)
406 407 408 409 410 411 412 413
{
	u8 val;

	if (on)
		val = ADV7180_PWR_MAN_ON;
	else
		val = ADV7180_PWR_MAN_OFF;

414
	return adv7180_write(state, ADV7180_REG_PWR_MAN, val);
415 416 417 418 419 420 421 422 423 424 425
}

static int adv7180_s_power(struct v4l2_subdev *sd, int on)
{
	struct adv7180_state *state = to_state(sd);
	int ret;

	ret = mutex_lock_interruptible(&state->mutex);
	if (ret)
		return ret;

426
	ret = adv7180_set_power(state, on);
427 428 429 430 431 432 433
	if (ret == 0)
		state->powered = on;

	mutex_unlock(&state->mutex);
	return ret;
}

434
static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
435
{
436
	struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
437 438
	struct adv7180_state *state = to_state(sd);
	int ret = mutex_lock_interruptible(&state->mutex);
439 440
	int val;

441 442
	if (ret)
		return ret;
443
	val = ctrl->val;
444 445
	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
446
		ret = adv7180_write(state, ADV7180_REG_BRI, val);
447 448 449
		break;
	case V4L2_CID_HUE:
		/*Hue is inverted according to HSL chart */
450
		ret = adv7180_write(state, ADV7180_REG_HUE, -val);
451 452
		break;
	case V4L2_CID_CONTRAST:
453
		ret = adv7180_write(state, ADV7180_REG_CON, val);
454 455 456 457 458 459
		break;
	case V4L2_CID_SATURATION:
		/*
		 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
		 *Let's not confuse the user, everybody understands saturation
		 */
460
		ret = adv7180_write(state, ADV7180_REG_SD_SAT_CB, val);
461 462
		if (ret < 0)
			break;
463
		ret = adv7180_write(state, ADV7180_REG_SD_SAT_CR, val);
464 465 466 467 468 469 470 471 472
		break;
	default:
		ret = -EINVAL;
	}

	mutex_unlock(&state->mutex);
	return ret;
}

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
static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
	.s_ctrl = adv7180_s_ctrl,
};

static int adv7180_init_controls(struct adv7180_state *state)
{
	v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);

	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
			  V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
			  ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
			  V4L2_CID_CONTRAST, ADV7180_CON_MIN,
			  ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
			  V4L2_CID_SATURATION, ADV7180_SAT_MIN,
			  ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
			  V4L2_CID_HUE, ADV7180_HUE_MIN,
			  ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
	state->sd.ctrl_handler = &state->ctrl_hdl;
	if (state->ctrl_hdl.error) {
		int err = state->ctrl_hdl.error;

		v4l2_ctrl_handler_free(&state->ctrl_hdl);
		return err;
	}
	v4l2_ctrl_handler_setup(&state->ctrl_hdl);

	return 0;
}
static void adv7180_exit_controls(struct adv7180_state *state)
{
	v4l2_ctrl_handler_free(&state->ctrl_hdl);
}

509 510 511
static int adv7180_enum_mbus_code(struct v4l2_subdev *sd,
				  struct v4l2_subdev_fh *fh,
				  struct v4l2_subdev_mbus_code_enum *code)
512
{
513
	if (code->index != 0)
514 515
		return -EINVAL;

516
	code->code = MEDIA_BUS_FMT_YUYV8_2X8;
517 518 519 520 521 522 523 524 525

	return 0;
}

static int adv7180_mbus_fmt(struct v4l2_subdev *sd,
			    struct v4l2_mbus_framefmt *fmt)
{
	struct adv7180_state *state = to_state(sd);

526
	fmt->code = MEDIA_BUS_FMT_YUYV8_2X8;
527 528 529 530 531 532 533 534
	fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
	fmt->field = V4L2_FIELD_INTERLACED;
	fmt->width = 720;
	fmt->height = state->curr_norm & V4L2_STD_525_60 ? 480 : 576;

	return 0;
}

535 536 537 538 539 540 541 542 543 544 545 546 547 548
static int adv7180_get_pad_format(struct v4l2_subdev *sd,
				  struct v4l2_subdev_fh *fh,
				  struct v4l2_subdev_format *format)
{
	return adv7180_mbus_fmt(sd, &format->format);
}

static int adv7180_set_pad_format(struct v4l2_subdev *sd,
				  struct v4l2_subdev_fh *fh,
				  struct v4l2_subdev_format *format)
{
	return adv7180_mbus_fmt(sd, &format->format);
}

549 550 551 552 553 554 555 556 557 558 559 560 561 562
static int adv7180_g_mbus_config(struct v4l2_subdev *sd,
				 struct v4l2_mbus_config *cfg)
{
	/*
	 * The ADV7180 sensor supports BT.601/656 output modes.
	 * The BT.656 is default and not yet configurable by s/w.
	 */
	cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
		     V4L2_MBUS_DATA_ACTIVE_HIGH;
	cfg->type = V4L2_MBUS_BT656;

	return 0;
}

563
static const struct v4l2_subdev_video_ops adv7180_video_ops = {
564
	.s_std = adv7180_s_std,
565
	.querystd = adv7180_querystd,
566
	.g_input_status = adv7180_g_input_status,
567
	.s_routing = adv7180_s_routing,
568
	.g_mbus_config = adv7180_g_mbus_config,
569 570
};

571

572
static const struct v4l2_subdev_core_ops adv7180_core_ops = {
573
	.s_power = adv7180_s_power,
574 575
};

576 577 578 579 580 581
static const struct v4l2_subdev_pad_ops adv7180_pad_ops = {
	.enum_mbus_code = adv7180_enum_mbus_code,
	.set_fmt = adv7180_set_pad_format,
	.get_fmt = adv7180_get_pad_format,
};

582 583 584
static const struct v4l2_subdev_ops adv7180_ops = {
	.core = &adv7180_core_ops,
	.video = &adv7180_video_ops,
585
	.pad = &adv7180_pad_ops,
586 587
};

588
static irqreturn_t adv7180_irq(int irq, void *devid)
589
{
590
	struct adv7180_state *state = devid;
591 592 593
	u8 isr3;

	mutex_lock(&state->mutex);
594
	isr3 = adv7180_read(state, ADV7180_REG_ISR3);
595
	/* clear */
596
	adv7180_write(state, ADV7180_REG_ICR3, isr3);
597 598

	if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
599
		__adv7180_status(state, NULL, &state->curr_norm);
600 601 602 603 604
	mutex_unlock(&state->mutex);

	return IRQ_HANDLED;
}

605
static int adv7180_init(struct adv7180_state *state)
606 607 608
{
	int ret;

609
	/* ITU-R BT.656-4 compatible */
610
	ret = adv7180_write(state, ADV7180_REG_EXTENDED_OUTPUT_CONTROL,
611
			ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
612
	if (ret < 0)
613
		return ret;
614 615

	/* Manually set V bit end position in NTSC mode */
616
	return adv7180_write(state, ADV7180_REG_NTSC_V_BIT_END,
617
					ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
618 619 620 621 622 623 624 625 626 627 628 629 630
}

static int adv7180_set_std(struct adv7180_state *state, unsigned int std)
{
	return adv7180_write(state, ADV7180_REG_INPUT_CONTROL,
		(std << 4) | state->input);
}

static int adv7180_select_input(struct adv7180_state *state, unsigned int input)
{
	int ret;

	ret = adv7180_read(state, ADV7180_REG_INPUT_CONTROL);
631
	if (ret < 0)
632 633 634 635 636 637 638
		return ret;

	ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
	ret |= input;
	return adv7180_write(state, ADV7180_REG_INPUT_CONTROL, ret);
}

639 640
static int adv7182_init(struct adv7180_state *state)
{
641 642 643 644 645 646 647 648 649 650
	if (state->chip_info->flags & ADV7180_FLAG_V2) {
		/* ADI recommended writes for improved video quality */
		adv7180_write(state, 0x0080, 0x51);
		adv7180_write(state, 0x0081, 0x51);
		adv7180_write(state, 0x0082, 0x68);
		adv7180_write(state, 0x0004, 0x17);
	} else {
		adv7180_write(state, 0x0004, 0x07);
	}

651 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 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
	/* ADI required writes */
	adv7180_write(state, 0x0003, 0x0c);
	adv7180_write(state, 0x0013, 0x00);
	adv7180_write(state, 0x001d, 0x40);

	return 0;
}

static int adv7182_set_std(struct adv7180_state *state, unsigned int std)
{
	return adv7180_write(state, ADV7182_REG_INPUT_VIDSEL, std << 4);
}

enum adv7182_input_type {
	ADV7182_INPUT_TYPE_CVBS,
	ADV7182_INPUT_TYPE_DIFF_CVBS,
	ADV7182_INPUT_TYPE_SVIDEO,
	ADV7182_INPUT_TYPE_YPBPR,
};

static enum adv7182_input_type adv7182_get_input_type(unsigned int input)
{
	switch (input) {
	case ADV7182_INPUT_CVBS_AIN1:
	case ADV7182_INPUT_CVBS_AIN2:
	case ADV7182_INPUT_CVBS_AIN3:
	case ADV7182_INPUT_CVBS_AIN4:
	case ADV7182_INPUT_CVBS_AIN5:
	case ADV7182_INPUT_CVBS_AIN6:
	case ADV7182_INPUT_CVBS_AIN7:
	case ADV7182_INPUT_CVBS_AIN8:
		return ADV7182_INPUT_TYPE_CVBS;
	case ADV7182_INPUT_SVIDEO_AIN1_AIN2:
	case ADV7182_INPUT_SVIDEO_AIN3_AIN4:
	case ADV7182_INPUT_SVIDEO_AIN5_AIN6:
	case ADV7182_INPUT_SVIDEO_AIN7_AIN8:
		return ADV7182_INPUT_TYPE_SVIDEO;
	case ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3:
	case ADV7182_INPUT_YPRPB_AIN4_AIN5_AIN6:
		return ADV7182_INPUT_TYPE_YPBPR;
	case ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2:
	case ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4:
	case ADV7182_INPUT_DIFF_CVBS_AIN5_AIN6:
	case ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8:
		return ADV7182_INPUT_TYPE_DIFF_CVBS;
	default: /* Will never happen */
		return 0;
	}
}

/* ADI recommended writes to registers 0x52, 0x53, 0x54 */
static unsigned int adv7182_lbias_settings[][3] = {
	[ADV7182_INPUT_TYPE_CVBS] = { 0xCB, 0x4E, 0x80 },
	[ADV7182_INPUT_TYPE_DIFF_CVBS] = { 0xC0, 0x4E, 0x80 },
	[ADV7182_INPUT_TYPE_SVIDEO] = { 0x0B, 0xCE, 0x80 },
	[ADV7182_INPUT_TYPE_YPBPR] = { 0x0B, 0x4E, 0xC0 },
};

709 710 711 712 713 714 715
static unsigned int adv7280_lbias_settings[][3] = {
	[ADV7182_INPUT_TYPE_CVBS] = { 0xCD, 0x4E, 0x80 },
	[ADV7182_INPUT_TYPE_DIFF_CVBS] = { 0xC0, 0x4E, 0x80 },
	[ADV7182_INPUT_TYPE_SVIDEO] = { 0x0B, 0xCE, 0x80 },
	[ADV7182_INPUT_TYPE_YPBPR] = { 0x0B, 0x4E, 0xC0 },
};

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
static int adv7182_select_input(struct adv7180_state *state, unsigned int input)
{
	enum adv7182_input_type input_type;
	unsigned int *lbias;
	unsigned int i;
	int ret;

	ret = adv7180_write(state, ADV7180_REG_INPUT_CONTROL, input);
	if (ret)
		return ret;

	/* Reset clamp circuitry - ADI recommended writes */
	adv7180_write(state, 0x809c, 0x00);
	adv7180_write(state, 0x809c, 0xff);

	input_type = adv7182_get_input_type(input);

	switch (input_type) {
	case ADV7182_INPUT_TYPE_CVBS:
	case ADV7182_INPUT_TYPE_DIFF_CVBS:
		/* ADI recommends to use the SH1 filter */
		adv7180_write(state, 0x0017, 0x41);
		break;
	default:
		adv7180_write(state, 0x0017, 0x01);
		break;
	}

744 745 746 747
	if (state->chip_info->flags & ADV7180_FLAG_V2)
		lbias = adv7280_lbias_settings[input_type];
	else
		lbias = adv7182_lbias_settings[input_type];
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

	for (i = 0; i < ARRAY_SIZE(adv7182_lbias_settings[0]); i++)
		adv7180_write(state, 0x0052 + i, lbias[i]);

	if (input_type == ADV7182_INPUT_TYPE_DIFF_CVBS) {
		/* ADI required writes to make differential CVBS work */
		adv7180_write(state, 0x005f, 0xa8);
		adv7180_write(state, 0x005a, 0x90);
		adv7180_write(state, 0x0060, 0xb0);
		adv7180_write(state, 0x80b6, 0x08);
		adv7180_write(state, 0x80c0, 0xa0);
	} else {
		adv7180_write(state, 0x005f, 0xf0);
		adv7180_write(state, 0x005a, 0xd0);
		adv7180_write(state, 0x0060, 0x10);
		adv7180_write(state, 0x80b6, 0x9c);
		adv7180_write(state, 0x80c0, 0x00);
	}

	return 0;
}

770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
static const struct adv7180_chip_info adv7180_info = {
	.flags = ADV7180_FLAG_RESET_POWERED,
	/* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
	 * all inputs and let the card driver take care of validation
	 */
	.valid_input_mask = BIT(ADV7180_INPUT_CVBS_AIN1) |
		BIT(ADV7180_INPUT_CVBS_AIN2) |
		BIT(ADV7180_INPUT_CVBS_AIN3) |
		BIT(ADV7180_INPUT_CVBS_AIN4) |
		BIT(ADV7180_INPUT_CVBS_AIN5) |
		BIT(ADV7180_INPUT_CVBS_AIN6) |
		BIT(ADV7180_INPUT_SVIDEO_AIN1_AIN2) |
		BIT(ADV7180_INPUT_SVIDEO_AIN3_AIN4) |
		BIT(ADV7180_INPUT_SVIDEO_AIN5_AIN6) |
		BIT(ADV7180_INPUT_YPRPB_AIN1_AIN2_AIN3) |
		BIT(ADV7180_INPUT_YPRPB_AIN4_AIN5_AIN6),
	.init = adv7180_init,
	.set_std = adv7180_set_std,
	.select_input = adv7180_select_input,
};

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
static const struct adv7180_chip_info adv7182_info = {
	.valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
		BIT(ADV7182_INPUT_CVBS_AIN2) |
		BIT(ADV7182_INPUT_CVBS_AIN3) |
		BIT(ADV7182_INPUT_CVBS_AIN4) |
		BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
		BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
		BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3) |
		BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
		BIT(ADV7182_INPUT_DIFF_CVBS_AIN3_AIN4),
	.init = adv7182_init,
	.set_std = adv7182_set_std,
	.select_input = adv7182_select_input,
};

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
static const struct adv7180_chip_info adv7280_info = {
	.flags = ADV7180_FLAG_V2,
	.valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
		BIT(ADV7182_INPUT_CVBS_AIN2) |
		BIT(ADV7182_INPUT_CVBS_AIN3) |
		BIT(ADV7182_INPUT_CVBS_AIN4) |
		BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
		BIT(ADV7182_INPUT_SVIDEO_AIN3_AIN4) |
		BIT(ADV7182_INPUT_YPRPB_AIN1_AIN2_AIN3),
	.init = adv7182_init,
	.set_std = adv7182_set_std,
	.select_input = adv7182_select_input,
};

static const struct adv7180_chip_info adv7281_info = {
	.flags = ADV7180_FLAG_V2,
	.valid_input_mask = BIT(ADV7182_INPUT_CVBS_AIN1) |
		BIT(ADV7182_INPUT_CVBS_AIN2) |
		BIT(ADV7182_INPUT_CVBS_AIN7) |
		BIT(ADV7182_INPUT_CVBS_AIN8) |
		BIT(ADV7182_INPUT_SVIDEO_AIN1_AIN2) |
		BIT(ADV7182_INPUT_SVIDEO_AIN7_AIN8) |
		BIT(ADV7182_INPUT_DIFF_CVBS_AIN1_AIN2) |
		BIT(ADV7182_INPUT_DIFF_CVBS_AIN7_AIN8),
	.init = adv7182_init,
	.set_std = adv7182_set_std,
	.select_input = adv7182_select_input,
};

835 836 837 838 839 840 841 842 843 844 845
static int init_device(struct adv7180_state *state)
{
	int ret;

	mutex_lock(&state->mutex);

	adv7180_write(state, ADV7180_REG_PWR_MAN, ADV7180_PWR_MAN_RES);
	usleep_range(2000, 10000);

	ret = state->chip_info->init(state);
	if (ret)
846
		goto out_unlock;
847

848 849 850
	ret = adv7180_program_std(state);
	if (ret)
		goto out_unlock;
851 852 853 854

	/* register for interrupts */
	if (state->irq > 0) {
		/* config the Interrupt pin to be active low */
855
		ret = adv7180_write(state, ADV7180_REG_ICONF1,
856 857
						ADV7180_ICONF1_ACTIVE_LOW |
						ADV7180_ICONF1_PSYNC_ONLY);
858
		if (ret < 0)
859
			goto out_unlock;
860

861
		ret = adv7180_write(state, ADV7180_REG_IMR1, 0);
862
		if (ret < 0)
863
			goto out_unlock;
864

865
		ret = adv7180_write(state, ADV7180_REG_IMR2, 0);
866
		if (ret < 0)
867
			goto out_unlock;
868 869

		/* enable AD change interrupts interrupts */
870
		ret = adv7180_write(state, ADV7180_REG_IMR3,
871
						ADV7180_IRQ3_AD_CHANGE);
872
		if (ret < 0)
873
			goto out_unlock;
874

875
		ret = adv7180_write(state, ADV7180_REG_IMR4, 0);
876
		if (ret < 0)
877
			goto out_unlock;
878 879
	}

880 881
out_unlock:
	mutex_unlock(&state->mutex);
882 883

	return ret;
884 885
}

886 887
static int adv7180_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
888 889 890 891 892 893 894 895 896 897 898 899
{
	struct adv7180_state *state;
	struct v4l2_subdev *sd;
	int ret;

	/* Check if the adapter supports the needed features */
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
		return -EIO;

	v4l_info(client, "chip found @ 0x%02x (%s)\n",
		 client->addr, client->adapter->name);

900
	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
901 902
	if (state == NULL)
		return -ENOMEM;
903

904
	state->client = client;
905
	state->chip_info = (struct adv7180_chip_info *)id->driver_data;
906

907 908 909
	state->irq = client->irq;
	mutex_init(&state->mutex);
	state->autodetect = true;
910 911 912 913
	if (state->chip_info->flags & ADV7180_FLAG_RESET_POWERED)
		state->powered = true;
	else
		state->powered = false;
914 915 916
	state->input = 0;
	sd = &state->sd;
	v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
917
	sd->flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
918

919 920
	ret = adv7180_init_controls(state);
	if (ret)
921
		goto err_unreg_subdev;
922 923 924 925

	state->pad.flags = MEDIA_PAD_FL_SOURCE;
	sd->entity.flags |= MEDIA_ENT_T_V4L2_SUBDEV_DECODER;
	ret = media_entity_init(&sd->entity, 1, &state->pad, 0);
926 927
	if (ret)
		goto err_free_ctrl;
928

929 930 931 932
	ret = init_device(state);
	if (ret)
		goto err_media_entity_cleanup;

933 934
	if (state->irq) {
		ret = request_threaded_irq(client->irq, NULL, adv7180_irq,
935 936
					   IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
					   KBUILD_MODNAME, state);
937
		if (ret)
938
			goto err_media_entity_cleanup;
939 940
	}

941 942 943 944
	ret = v4l2_async_register_subdev(sd);
	if (ret)
		goto err_free_irq;

945
	return 0;
946

947 948 949
err_free_irq:
	if (state->irq > 0)
		free_irq(client->irq, state);
950 951
err_media_entity_cleanup:
	media_entity_cleanup(&sd->entity);
952 953
err_free_ctrl:
	adv7180_exit_controls(state);
954
err_unreg_subdev:
955
	mutex_destroy(&state->mutex);
956
	return ret;
957 958
}

959
static int adv7180_remove(struct i2c_client *client)
960 961
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
962 963
	struct adv7180_state *state = to_state(sd);

964 965
	v4l2_async_unregister_subdev(sd);

966
	if (state->irq > 0)
967
		free_irq(client->irq, state);
968

969
	media_entity_cleanup(&sd->entity);
970
	adv7180_exit_controls(state);
971
	mutex_destroy(&state->mutex);
972 973 974 975
	return 0;
}

static const struct i2c_device_id adv7180_id[] = {
976
	{ "adv7180", (kernel_ulong_t)&adv7180_info },
977
	{ "adv7182", (kernel_ulong_t)&adv7182_info },
978 979 980
	{ "adv7280", (kernel_ulong_t)&adv7280_info },
	{ "adv7281", (kernel_ulong_t)&adv7281_info },
	{ "adv7282", (kernel_ulong_t)&adv7281_info },
981 982
	{},
};
983
MODULE_DEVICE_TABLE(i2c, adv7180_id);
984

985 986
#ifdef CONFIG_PM_SLEEP
static int adv7180_suspend(struct device *dev)
987
{
988
	struct i2c_client *client = to_i2c_client(dev);
989 990
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct adv7180_state *state = to_state(sd);
991

992
	return adv7180_set_power(state, false);
993 994
}

995
static int adv7180_resume(struct device *dev)
996
{
997
	struct i2c_client *client = to_i2c_client(dev);
998 999 1000 1001
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct adv7180_state *state = to_state(sd);
	int ret;

1002
	ret = init_device(state);
1003 1004
	if (ret < 0)
		return ret;
1005 1006 1007 1008 1009

	ret = adv7180_set_power(state, state->powered);
	if (ret)
		return ret;

1010 1011
	return 0;
}
1012 1013 1014 1015 1016 1017

static SIMPLE_DEV_PM_OPS(adv7180_pm_ops, adv7180_suspend, adv7180_resume);
#define ADV7180_PM_OPS (&adv7180_pm_ops)

#else
#define ADV7180_PM_OPS NULL
1018 1019
#endif

1020 1021
static struct i2c_driver adv7180_driver = {
	.driver = {
1022
		   .owner = THIS_MODULE,
1023
		   .name = KBUILD_MODNAME,
1024
		   .pm = ADV7180_PM_OPS,
1025 1026
		   },
	.probe = adv7180_probe,
1027
	.remove = adv7180_remove,
1028
	.id_table = adv7180_id,
1029 1030
};

1031
module_i2c_driver(adv7180_driver);
1032 1033 1034 1035

MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
MODULE_AUTHOR("Mocean Laboratories");
MODULE_LICENSE("GPL v2");