adv7180.c 18.6 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

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#define ADV7180_INPUT_CONTROL_REG			0x00
#define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM	0x00
#define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
#define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM	0x20
#define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM	0x30
#define ADV7180_INPUT_CONTROL_NTSC_J			0x40
#define ADV7180_INPUT_CONTROL_NTSC_M			0x50
#define ADV7180_INPUT_CONTROL_PAL60			0x60
#define ADV7180_INPUT_CONTROL_NTSC_443			0x70
#define ADV7180_INPUT_CONTROL_PAL_BG			0x80
#define ADV7180_INPUT_CONTROL_PAL_N			0x90
#define ADV7180_INPUT_CONTROL_PAL_M			0xa0
#define ADV7180_INPUT_CONTROL_PAL_M_PED			0xb0
#define ADV7180_INPUT_CONTROL_PAL_COMB_N		0xc0
#define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED		0xd0
#define ADV7180_INPUT_CONTROL_PAL_SECAM			0xe0
#define ADV7180_INPUT_CONTROL_PAL_SECAM_PED		0xf0
51
#define ADV7180_INPUT_CONTROL_INSEL_MASK		0x0f
52

53 54
#define ADV7180_EXTENDED_OUTPUT_CONTROL_REG		0x04
#define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS		0xC5
55

56 57
#define ADV7180_AUTODETECT_ENABLE_REG			0x07
#define ADV7180_AUTODETECT_DEFAULT			0x7f
58
/* Contrast */
59
#define ADV7180_CON_REG		0x08	/*Unsigned */
60 61 62 63
#define ADV7180_CON_MIN		0
#define ADV7180_CON_DEF		128
#define ADV7180_CON_MAX		255
/* Brightness*/
64
#define ADV7180_BRI_REG		0x0a	/*Signed */
65 66 67 68
#define ADV7180_BRI_MIN		-128
#define ADV7180_BRI_DEF		0
#define ADV7180_BRI_MAX		127
/* Hue */
69
#define ADV7180_HUE_REG		0x0b	/*Signed, inverted */
70 71 72
#define ADV7180_HUE_MIN		-127
#define ADV7180_HUE_DEF		0
#define ADV7180_HUE_MAX		128
73

74 75
#define ADV7180_ADI_CTRL_REG				0x0e
#define ADV7180_ADI_CTRL_IRQ_SPACE			0x20
76

77 78 79 80 81
#define ADV7180_PWR_MAN_REG		0x0f
#define ADV7180_PWR_MAN_ON		0x04
#define ADV7180_PWR_MAN_OFF		0x24
#define ADV7180_PWR_MAN_RES		0x80

82 83 84
#define ADV7180_STATUS1_REG				0x10
#define ADV7180_STATUS1_IN_LOCK		0x01
#define ADV7180_STATUS1_AUTOD_MASK	0x70
85 86 87 88 89 90 91 92 93 94 95 96
#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

#define ADV7180_IDENT_REG 0x11
#define ADV7180_ID_7180 0x18

97 98 99 100
#define ADV7180_ICONF1_ADI		0x40
#define ADV7180_ICONF1_ACTIVE_LOW	0x01
#define ADV7180_ICONF1_PSYNC_ONLY	0x10
#define ADV7180_ICONF1_ACTIVE_TO_CLR	0xC0
101
/* Saturation */
102 103
#define ADV7180_SD_SAT_CB_REG	0xe3	/*Unsigned */
#define ADV7180_SD_SAT_CR_REG	0xe4	/*Unsigned */
104 105 106
#define ADV7180_SAT_MIN		0
#define ADV7180_SAT_DEF		128
#define ADV7180_SAT_MAX		255
107

108 109 110 111 112 113 114 115 116 117 118
#define ADV7180_IRQ1_LOCK	0x01
#define ADV7180_IRQ1_UNLOCK	0x02
#define ADV7180_ISR1_ADI	0x42
#define ADV7180_ICR1_ADI	0x43
#define ADV7180_IMR1_ADI	0x44
#define ADV7180_IMR2_ADI	0x48
#define ADV7180_IRQ3_AD_CHANGE	0x08
#define ADV7180_ISR3_ADI	0x4A
#define ADV7180_ICR3_ADI	0x4B
#define ADV7180_IMR3_ADI	0x4C
#define ADV7180_IMR4_ADI	0x50
119

120 121 122
#define ADV7180_NTSC_V_BIT_END_REG	0xE6
#define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND	0x4F

123
struct adv7180_state {
124
	struct v4l2_ctrl_handler ctrl_hdl;
125
	struct v4l2_subdev	sd;
126 127
	struct mutex		mutex; /* mutual excl. when accessing chip */
	int			irq;
128 129
	v4l2_std_id		curr_norm;
	bool			autodetect;
130
	bool			powered;
131
	u8			input;
132
};
133 134 135
#define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler,		\
					    struct adv7180_state,	\
					    ctrl_hdl)->sd)
136

137
static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
138
{
139 140 141 142
	/* in case V4L2_IN_ST_NO_SIGNAL */
	if (!(status1 & ADV7180_STATUS1_IN_LOCK))
		return V4L2_STD_UNKNOWN;

143 144
	switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
	case ADV7180_STATUS1_AUTOD_NTSM_M_J:
145
		return V4L2_STD_NTSC;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	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;
	}
}

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static int v4l2_std_to_adv7180(v4l2_std_id std)
{
	if (std == V4L2_STD_PAL_60)
		return ADV7180_INPUT_CONTROL_PAL60;
	if (std == V4L2_STD_NTSC_443)
		return ADV7180_INPUT_CONTROL_NTSC_443;
	if (std == V4L2_STD_PAL_N)
		return ADV7180_INPUT_CONTROL_PAL_N;
	if (std == V4L2_STD_PAL_M)
		return ADV7180_INPUT_CONTROL_PAL_M;
	if (std == V4L2_STD_PAL_Nc)
		return ADV7180_INPUT_CONTROL_PAL_COMB_N;

	if (std & V4L2_STD_PAL)
		return ADV7180_INPUT_CONTROL_PAL_BG;
	if (std & V4L2_STD_NTSC)
		return ADV7180_INPUT_CONTROL_NTSC_M;
	if (std & V4L2_STD_SECAM)
		return ADV7180_INPUT_CONTROL_PAL_SECAM;

	return -EINVAL;
}

188 189 190 191 192 193 194 195 196
static u32 adv7180_status_to_v4l2(u8 status1)
{
	if (!(status1 & ADV7180_STATUS1_IN_LOCK))
		return V4L2_IN_ST_NO_SIGNAL;

	return 0;
}

static int __adv7180_status(struct i2c_client *client, u32 *status,
197
			    v4l2_std_id *std)
198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
	int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);

	if (status1 < 0)
		return status1;

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

	return 0;
}

212 213 214 215 216 217 218
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)
{
219
	struct adv7180_state *state = to_state(sd);
220 221 222
	int err = mutex_lock_interruptible(&state->mutex);
	if (err)
		return err;
223

224 225
	/* when we are interrupt driven we know the state */
	if (!state->autodetect || state->irq > 0)
226 227 228 229
		*std = state->curr_norm;
	else
		err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);

230
	mutex_unlock(&state->mutex);
231
	return err;
232
}
233

234 235 236 237 238 239 240 241 242 243
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);
	struct i2c_client *client = v4l2_get_subdevdata(sd);

	if (ret)
		return ret;

244
	/* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	 * all inputs and let the card driver take care of validation
	 */
	if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
		goto out;

	ret = i2c_smbus_read_byte_data(client, ADV7180_INPUT_CONTROL_REG);

	if (ret < 0)
		goto out;

	ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
	ret = i2c_smbus_write_byte_data(client,
					ADV7180_INPUT_CONTROL_REG, ret | input);
	state->input = input;
out:
	mutex_unlock(&state->mutex);
	return ret;
}

264 265
static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
{
266 267 268 269 270 271 272 273
	struct adv7180_state *state = to_state(sd);
	int ret = mutex_lock_interruptible(&state->mutex);
	if (ret)
		return ret;

	ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
	mutex_unlock(&state->mutex);
	return ret;
274 275
}

276 277 278 279
static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
{
	struct adv7180_state *state = to_state(sd);
	struct i2c_client *client = v4l2_get_subdevdata(sd);
280 281 282
	int ret = mutex_lock_interruptible(&state->mutex);
	if (ret)
		return ret;
283 284 285

	/* all standards -> autodetect */
	if (std == V4L2_STD_ALL) {
286 287 288 289
		ret =
		    i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
				ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
					      | state->input);
290 291 292
		if (ret < 0)
			goto out;

293
		__adv7180_status(client, NULL, &state->curr_norm);
294 295 296 297 298 299 300
		state->autodetect = true;
	} else {
		ret = v4l2_std_to_adv7180(std);
		if (ret < 0)
			goto out;

		ret = i2c_smbus_write_byte_data(client,
301 302
						ADV7180_INPUT_CONTROL_REG,
						ret | state->input);
303 304 305 306 307 308 309 310
		if (ret < 0)
			goto out;

		state->curr_norm = std;
		state->autodetect = false;
	}
	ret = 0;
out:
311
	mutex_unlock(&state->mutex);
312 313 314
	return ret;
}

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
static int adv7180_set_power(struct adv7180_state *state,
	struct i2c_client *client, bool on)
{
	u8 val;

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

	return i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG, val);
}

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

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

	ret = adv7180_set_power(state, client, on);
	if (ret == 0)
		state->powered = on;

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

346
static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
347
{
348
	struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
349 350 351
	struct adv7180_state *state = to_state(sd);
	struct i2c_client *client = v4l2_get_subdevdata(sd);
	int ret = mutex_lock_interruptible(&state->mutex);
352 353
	int val;

354 355
	if (ret)
		return ret;
356
	val = ctrl->val;
357 358
	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
359
		ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG, val);
360 361 362
		break;
	case V4L2_CID_HUE:
		/*Hue is inverted according to HSL chart */
363
		ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, -val);
364 365
		break;
	case V4L2_CID_CONTRAST:
366
		ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG, val);
367 368 369 370 371 372
		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
		 */
373 374
		ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
						val);
375 376
		if (ret < 0)
			break;
377 378
		ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
						val);
379 380 381 382 383 384 385 386 387
		break;
	default:
		ret = -EINVAL;
	}

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

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
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);
}

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
static int adv7180_enum_mbus_fmt(struct v4l2_subdev *sd, unsigned int index,
				 enum v4l2_mbus_pixelcode *code)
{
	if (index > 0)
		return -EINVAL;

	*code = V4L2_MBUS_FMT_YUYV8_2X8;

	return 0;
}

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

	fmt->code = V4L2_MBUS_FMT_YUYV8_2X8;
	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;
}

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;
}

463 464
static const struct v4l2_subdev_video_ops adv7180_video_ops = {
	.querystd = adv7180_querystd,
465
	.g_input_status = adv7180_g_input_status,
466
	.s_routing = adv7180_s_routing,
467 468 469 470 471
	.enum_mbus_fmt = adv7180_enum_mbus_fmt,
	.try_mbus_fmt = adv7180_mbus_fmt,
	.g_mbus_fmt = adv7180_mbus_fmt,
	.s_mbus_fmt = adv7180_mbus_fmt,
	.g_mbus_config = adv7180_g_mbus_config,
472 473 474
};

static const struct v4l2_subdev_core_ops adv7180_core_ops = {
475
	.s_std = adv7180_s_std,
476
	.s_power = adv7180_s_power,
477 478 479 480 481 482 483
};

static const struct v4l2_subdev_ops adv7180_ops = {
	.core = &adv7180_core_ops,
	.video = &adv7180_video_ops,
};

484
static irqreturn_t adv7180_irq(int irq, void *devid)
485
{
486
	struct adv7180_state *state = devid;
487 488 489 490 491
	struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
	u8 isr3;

	mutex_lock(&state->mutex);
	i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
492
				  ADV7180_ADI_CTRL_IRQ_SPACE);
493 494 495 496 497 498 499 500 501 502 503 504
	isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
	/* clear */
	i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
	i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);

	if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
		__adv7180_status(client, NULL, &state->curr_norm);
	mutex_unlock(&state->mutex);

	return IRQ_HANDLED;
}

505
static int init_device(struct i2c_client *client, struct adv7180_state *state)
506 507 508 509
{
	int ret;

	/* Initialize adv7180 */
510
	/* Enable autodetection */
511 512 513 514 515 516 517
	if (state->autodetect) {
		ret =
		    i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
				ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
					      | state->input);
		if (ret < 0)
			return ret;
518

519 520 521 522 523 524 525 526 527 528
		ret =
		    i2c_smbus_write_byte_data(client,
					      ADV7180_AUTODETECT_ENABLE_REG,
					      ADV7180_AUTODETECT_DEFAULT);
		if (ret < 0)
			return ret;
	} else {
		ret = v4l2_std_to_adv7180(state->curr_norm);
		if (ret < 0)
			return ret;
529

530 531 532 533 534 535 536
		ret =
		    i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
					      ret | state->input);
		if (ret < 0)
			return ret;

	}
537 538
	/* ITU-R BT.656-4 compatible */
	ret = i2c_smbus_write_byte_data(client,
539 540
			ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
			ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
541
	if (ret < 0)
542 543 544 545 546 547 548 549
		return ret;

	/* Manually set V bit end position in NTSC mode */
	ret = i2c_smbus_write_byte_data(client,
					ADV7180_NTSC_V_BIT_END_REG,
					ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
	if (ret < 0)
		return ret;
550 551 552 553 554 555

	/* read current norm */
	__adv7180_status(client, NULL, &state->curr_norm);

	/* register for interrupts */
	if (state->irq > 0) {
556 557
		ret = request_threaded_irq(state->irq, NULL, adv7180_irq,
					   IRQF_ONESHOT, KBUILD_MODNAME, state);
558
		if (ret)
559
			return ret;
560 561

		ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
562
						ADV7180_ADI_CTRL_IRQ_SPACE);
563
		if (ret < 0)
564
			goto err;
565 566 567

		/* config the Interrupt pin to be active low */
		ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
568 569
						ADV7180_ICONF1_ACTIVE_LOW |
						ADV7180_ICONF1_PSYNC_ONLY);
570
		if (ret < 0)
571
			goto err;
572 573 574

		ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
		if (ret < 0)
575
			goto err;
576 577 578

		ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
		if (ret < 0)
579
			goto err;
580 581 582

		/* enable AD change interrupts interrupts */
		ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
583
						ADV7180_IRQ3_AD_CHANGE);
584
		if (ret < 0)
585
			goto err;
586 587 588

		ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
		if (ret < 0)
589
			goto err;
590 591

		ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
592
						0);
593
		if (ret < 0)
594
			goto err;
595 596
	}

597
	return 0;
598 599 600 601

err:
	free_irq(state->irq, state);
	return ret;
602 603
}

604 605
static int adv7180_probe(struct i2c_client *client,
			 const struct i2c_device_id *id)
606 607 608 609 610 611 612 613 614 615 616 617
{
	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);

618
	state = devm_kzalloc(&client->dev, sizeof(*state), GFP_KERNEL);
619 620 621 622 623 624 625 626
	if (state == NULL) {
		ret = -ENOMEM;
		goto err;
	}

	state->irq = client->irq;
	mutex_init(&state->mutex);
	state->autodetect = true;
627
	state->powered = true;
628 629 630 631
	state->input = 0;
	sd = &state->sd;
	v4l2_i2c_subdev_init(sd, client, &adv7180_ops);

632 633
	ret = adv7180_init_controls(state);
	if (ret)
634
		goto err_unreg_subdev;
635 636 637
	ret = init_device(client, state);
	if (ret)
		goto err_free_ctrl;
638 639 640 641 642

	ret = v4l2_async_register_subdev(sd);
	if (ret)
		goto err_free_irq;

643
	return 0;
644

645 646 647
err_free_irq:
	if (state->irq > 0)
		free_irq(client->irq, state);
648 649
err_free_ctrl:
	adv7180_exit_controls(state);
650
err_unreg_subdev:
651
	mutex_destroy(&state->mutex);
652 653
err:
	return ret;
654 655
}

656
static int adv7180_remove(struct i2c_client *client)
657 658
{
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
659 660
	struct adv7180_state *state = to_state(sd);

661 662
	v4l2_async_unregister_subdev(sd);

663
	if (state->irq > 0)
664
		free_irq(client->irq, state);
665 666

	v4l2_device_unregister_subdev(sd);
667
	adv7180_exit_controls(state);
668
	mutex_destroy(&state->mutex);
669 670 671 672
	return 0;
}

static const struct i2c_device_id adv7180_id[] = {
673
	{KBUILD_MODNAME, 0},
674 675 676
	{},
};

677 678
#ifdef CONFIG_PM_SLEEP
static int adv7180_suspend(struct device *dev)
679
{
680
	struct i2c_client *client = to_i2c_client(dev);
681 682
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct adv7180_state *state = to_state(sd);
683

684
	return adv7180_set_power(state, client, false);
685 686
}

687
static int adv7180_resume(struct device *dev)
688
{
689
	struct i2c_client *client = to_i2c_client(dev);
690 691 692 693
	struct v4l2_subdev *sd = i2c_get_clientdata(client);
	struct adv7180_state *state = to_state(sd);
	int ret;

694 695 696 697 698
	if (state->powered) {
		ret = adv7180_set_power(state, client, true);
		if (ret)
			return ret;
	}
699 700 701 702 703
	ret = init_device(client, state);
	if (ret < 0)
		return ret;
	return 0;
}
704 705 706 707 708 709

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
710 711
#endif

712 713 714 715
MODULE_DEVICE_TABLE(i2c, adv7180_id);

static struct i2c_driver adv7180_driver = {
	.driver = {
716
		   .owner = THIS_MODULE,
717
		   .name = KBUILD_MODNAME,
718
		   .pm = ADV7180_PM_OPS,
719 720
		   },
	.probe = adv7180_probe,
721
	.remove = adv7180_remove,
722
	.id_table = adv7180_id,
723 724
};

725
module_i2c_driver(adv7180_driver);
726 727 728 729

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