xref: /openbmc/linux/drivers/media/i2c/adv7180.c (revision 39b6f3aa)
1 /*
2  * adv7180.c Analog Devices ADV7180 video decoder driver
3  * Copyright (c) 2009 Intel Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/interrupt.h>
24 #include <linux/i2c.h>
25 #include <linux/slab.h>
26 #include <media/v4l2-ioctl.h>
27 #include <linux/videodev2.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-chip-ident.h>
31 #include <linux/mutex.h>
32 
33 #define ADV7180_INPUT_CONTROL_REG			0x00
34 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM	0x00
35 #define ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM_PED 0x10
36 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_J_SECAM	0x20
37 #define ADV7180_INPUT_CONTROL_AD_PAL_N_NTSC_M_SECAM	0x30
38 #define ADV7180_INPUT_CONTROL_NTSC_J			0x40
39 #define ADV7180_INPUT_CONTROL_NTSC_M			0x50
40 #define ADV7180_INPUT_CONTROL_PAL60			0x60
41 #define ADV7180_INPUT_CONTROL_NTSC_443			0x70
42 #define ADV7180_INPUT_CONTROL_PAL_BG			0x80
43 #define ADV7180_INPUT_CONTROL_PAL_N			0x90
44 #define ADV7180_INPUT_CONTROL_PAL_M			0xa0
45 #define ADV7180_INPUT_CONTROL_PAL_M_PED			0xb0
46 #define ADV7180_INPUT_CONTROL_PAL_COMB_N		0xc0
47 #define ADV7180_INPUT_CONTROL_PAL_COMB_N_PED		0xd0
48 #define ADV7180_INPUT_CONTROL_PAL_SECAM			0xe0
49 #define ADV7180_INPUT_CONTROL_PAL_SECAM_PED		0xf0
50 #define ADV7180_INPUT_CONTROL_INSEL_MASK		0x0f
51 
52 #define ADV7180_EXTENDED_OUTPUT_CONTROL_REG		0x04
53 #define ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS		0xC5
54 
55 #define ADV7180_AUTODETECT_ENABLE_REG			0x07
56 #define ADV7180_AUTODETECT_DEFAULT			0x7f
57 /* Contrast */
58 #define ADV7180_CON_REG		0x08	/*Unsigned */
59 #define ADV7180_CON_MIN		0
60 #define ADV7180_CON_DEF		128
61 #define ADV7180_CON_MAX		255
62 /* Brightness*/
63 #define ADV7180_BRI_REG		0x0a	/*Signed */
64 #define ADV7180_BRI_MIN		-128
65 #define ADV7180_BRI_DEF		0
66 #define ADV7180_BRI_MAX		127
67 /* Hue */
68 #define ADV7180_HUE_REG		0x0b	/*Signed, inverted */
69 #define ADV7180_HUE_MIN		-127
70 #define ADV7180_HUE_DEF		0
71 #define ADV7180_HUE_MAX		128
72 
73 #define ADV7180_ADI_CTRL_REG				0x0e
74 #define ADV7180_ADI_CTRL_IRQ_SPACE			0x20
75 
76 #define ADV7180_PWR_MAN_REG		0x0f
77 #define ADV7180_PWR_MAN_ON		0x04
78 #define ADV7180_PWR_MAN_OFF		0x24
79 #define ADV7180_PWR_MAN_RES		0x80
80 
81 #define ADV7180_STATUS1_REG				0x10
82 #define ADV7180_STATUS1_IN_LOCK		0x01
83 #define ADV7180_STATUS1_AUTOD_MASK	0x70
84 #define ADV7180_STATUS1_AUTOD_NTSM_M_J	0x00
85 #define ADV7180_STATUS1_AUTOD_NTSC_4_43 0x10
86 #define ADV7180_STATUS1_AUTOD_PAL_M	0x20
87 #define ADV7180_STATUS1_AUTOD_PAL_60	0x30
88 #define ADV7180_STATUS1_AUTOD_PAL_B_G	0x40
89 #define ADV7180_STATUS1_AUTOD_SECAM	0x50
90 #define ADV7180_STATUS1_AUTOD_PAL_COMB	0x60
91 #define ADV7180_STATUS1_AUTOD_SECAM_525	0x70
92 
93 #define ADV7180_IDENT_REG 0x11
94 #define ADV7180_ID_7180 0x18
95 
96 #define ADV7180_ICONF1_ADI		0x40
97 #define ADV7180_ICONF1_ACTIVE_LOW	0x01
98 #define ADV7180_ICONF1_PSYNC_ONLY	0x10
99 #define ADV7180_ICONF1_ACTIVE_TO_CLR	0xC0
100 /* Saturation */
101 #define ADV7180_SD_SAT_CB_REG	0xe3	/*Unsigned */
102 #define ADV7180_SD_SAT_CR_REG	0xe4	/*Unsigned */
103 #define ADV7180_SAT_MIN		0
104 #define ADV7180_SAT_DEF		128
105 #define ADV7180_SAT_MAX		255
106 
107 #define ADV7180_IRQ1_LOCK	0x01
108 #define ADV7180_IRQ1_UNLOCK	0x02
109 #define ADV7180_ISR1_ADI	0x42
110 #define ADV7180_ICR1_ADI	0x43
111 #define ADV7180_IMR1_ADI	0x44
112 #define ADV7180_IMR2_ADI	0x48
113 #define ADV7180_IRQ3_AD_CHANGE	0x08
114 #define ADV7180_ISR3_ADI	0x4A
115 #define ADV7180_ICR3_ADI	0x4B
116 #define ADV7180_IMR3_ADI	0x4C
117 #define ADV7180_IMR4_ADI	0x50
118 
119 #define ADV7180_NTSC_V_BIT_END_REG	0xE6
120 #define ADV7180_NTSC_V_BIT_END_MANUAL_NVEND	0x4F
121 
122 struct adv7180_state {
123 	struct v4l2_ctrl_handler ctrl_hdl;
124 	struct v4l2_subdev	sd;
125 	struct work_struct	work;
126 	struct mutex		mutex; /* mutual excl. when accessing chip */
127 	int			irq;
128 	v4l2_std_id		curr_norm;
129 	bool			autodetect;
130 	u8			input;
131 };
132 #define to_adv7180_sd(_ctrl) (&container_of(_ctrl->handler,		\
133 					    struct adv7180_state,	\
134 					    ctrl_hdl)->sd)
135 
136 static v4l2_std_id adv7180_std_to_v4l2(u8 status1)
137 {
138 	/* in case V4L2_IN_ST_NO_SIGNAL */
139 	if (!(status1 & ADV7180_STATUS1_IN_LOCK))
140 		return V4L2_STD_UNKNOWN;
141 
142 	switch (status1 & ADV7180_STATUS1_AUTOD_MASK) {
143 	case ADV7180_STATUS1_AUTOD_NTSM_M_J:
144 		return V4L2_STD_NTSC;
145 	case ADV7180_STATUS1_AUTOD_NTSC_4_43:
146 		return V4L2_STD_NTSC_443;
147 	case ADV7180_STATUS1_AUTOD_PAL_M:
148 		return V4L2_STD_PAL_M;
149 	case ADV7180_STATUS1_AUTOD_PAL_60:
150 		return V4L2_STD_PAL_60;
151 	case ADV7180_STATUS1_AUTOD_PAL_B_G:
152 		return V4L2_STD_PAL;
153 	case ADV7180_STATUS1_AUTOD_SECAM:
154 		return V4L2_STD_SECAM;
155 	case ADV7180_STATUS1_AUTOD_PAL_COMB:
156 		return V4L2_STD_PAL_Nc | V4L2_STD_PAL_N;
157 	case ADV7180_STATUS1_AUTOD_SECAM_525:
158 		return V4L2_STD_SECAM;
159 	default:
160 		return V4L2_STD_UNKNOWN;
161 	}
162 }
163 
164 static int v4l2_std_to_adv7180(v4l2_std_id std)
165 {
166 	if (std == V4L2_STD_PAL_60)
167 		return ADV7180_INPUT_CONTROL_PAL60;
168 	if (std == V4L2_STD_NTSC_443)
169 		return ADV7180_INPUT_CONTROL_NTSC_443;
170 	if (std == V4L2_STD_PAL_N)
171 		return ADV7180_INPUT_CONTROL_PAL_N;
172 	if (std == V4L2_STD_PAL_M)
173 		return ADV7180_INPUT_CONTROL_PAL_M;
174 	if (std == V4L2_STD_PAL_Nc)
175 		return ADV7180_INPUT_CONTROL_PAL_COMB_N;
176 
177 	if (std & V4L2_STD_PAL)
178 		return ADV7180_INPUT_CONTROL_PAL_BG;
179 	if (std & V4L2_STD_NTSC)
180 		return ADV7180_INPUT_CONTROL_NTSC_M;
181 	if (std & V4L2_STD_SECAM)
182 		return ADV7180_INPUT_CONTROL_PAL_SECAM;
183 
184 	return -EINVAL;
185 }
186 
187 static u32 adv7180_status_to_v4l2(u8 status1)
188 {
189 	if (!(status1 & ADV7180_STATUS1_IN_LOCK))
190 		return V4L2_IN_ST_NO_SIGNAL;
191 
192 	return 0;
193 }
194 
195 static int __adv7180_status(struct i2c_client *client, u32 *status,
196 			    v4l2_std_id *std)
197 {
198 	int status1 = i2c_smbus_read_byte_data(client, ADV7180_STATUS1_REG);
199 
200 	if (status1 < 0)
201 		return status1;
202 
203 	if (status)
204 		*status = adv7180_status_to_v4l2(status1);
205 	if (std)
206 		*std = adv7180_std_to_v4l2(status1);
207 
208 	return 0;
209 }
210 
211 static inline struct adv7180_state *to_state(struct v4l2_subdev *sd)
212 {
213 	return container_of(sd, struct adv7180_state, sd);
214 }
215 
216 static int adv7180_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
217 {
218 	struct adv7180_state *state = to_state(sd);
219 	int err = mutex_lock_interruptible(&state->mutex);
220 	if (err)
221 		return err;
222 
223 	/* when we are interrupt driven we know the state */
224 	if (!state->autodetect || state->irq > 0)
225 		*std = state->curr_norm;
226 	else
227 		err = __adv7180_status(v4l2_get_subdevdata(sd), NULL, std);
228 
229 	mutex_unlock(&state->mutex);
230 	return err;
231 }
232 
233 static int adv7180_s_routing(struct v4l2_subdev *sd, u32 input,
234 			     u32 output, u32 config)
235 {
236 	struct adv7180_state *state = to_state(sd);
237 	int ret = mutex_lock_interruptible(&state->mutex);
238 	struct i2c_client *client = v4l2_get_subdevdata(sd);
239 
240 	if (ret)
241 		return ret;
242 
243 	/* We cannot discriminate between LQFP and 40-pin LFCSP, so accept
244 	 * all inputs and let the card driver take care of validation
245 	 */
246 	if ((input & ADV7180_INPUT_CONTROL_INSEL_MASK) != input)
247 		goto out;
248 
249 	ret = i2c_smbus_read_byte_data(client, ADV7180_INPUT_CONTROL_REG);
250 
251 	if (ret < 0)
252 		goto out;
253 
254 	ret &= ~ADV7180_INPUT_CONTROL_INSEL_MASK;
255 	ret = i2c_smbus_write_byte_data(client,
256 					ADV7180_INPUT_CONTROL_REG, ret | input);
257 	state->input = input;
258 out:
259 	mutex_unlock(&state->mutex);
260 	return ret;
261 }
262 
263 static int adv7180_g_input_status(struct v4l2_subdev *sd, u32 *status)
264 {
265 	struct adv7180_state *state = to_state(sd);
266 	int ret = mutex_lock_interruptible(&state->mutex);
267 	if (ret)
268 		return ret;
269 
270 	ret = __adv7180_status(v4l2_get_subdevdata(sd), status, NULL);
271 	mutex_unlock(&state->mutex);
272 	return ret;
273 }
274 
275 static int adv7180_g_chip_ident(struct v4l2_subdev *sd,
276 				struct v4l2_dbg_chip_ident *chip)
277 {
278 	struct i2c_client *client = v4l2_get_subdevdata(sd);
279 
280 	return v4l2_chip_ident_i2c_client(client, chip, V4L2_IDENT_ADV7180, 0);
281 }
282 
283 static int adv7180_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
284 {
285 	struct adv7180_state *state = to_state(sd);
286 	struct i2c_client *client = v4l2_get_subdevdata(sd);
287 	int ret = mutex_lock_interruptible(&state->mutex);
288 	if (ret)
289 		return ret;
290 
291 	/* all standards -> autodetect */
292 	if (std == V4L2_STD_ALL) {
293 		ret =
294 		    i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
295 				ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
296 					      | state->input);
297 		if (ret < 0)
298 			goto out;
299 
300 		__adv7180_status(client, NULL, &state->curr_norm);
301 		state->autodetect = true;
302 	} else {
303 		ret = v4l2_std_to_adv7180(std);
304 		if (ret < 0)
305 			goto out;
306 
307 		ret = i2c_smbus_write_byte_data(client,
308 						ADV7180_INPUT_CONTROL_REG,
309 						ret | state->input);
310 		if (ret < 0)
311 			goto out;
312 
313 		state->curr_norm = std;
314 		state->autodetect = false;
315 	}
316 	ret = 0;
317 out:
318 	mutex_unlock(&state->mutex);
319 	return ret;
320 }
321 
322 static int adv7180_s_ctrl(struct v4l2_ctrl *ctrl)
323 {
324 	struct v4l2_subdev *sd = to_adv7180_sd(ctrl);
325 	struct adv7180_state *state = to_state(sd);
326 	struct i2c_client *client = v4l2_get_subdevdata(sd);
327 	int ret = mutex_lock_interruptible(&state->mutex);
328 	int val;
329 
330 	if (ret)
331 		return ret;
332 	val = ctrl->val;
333 	switch (ctrl->id) {
334 	case V4L2_CID_BRIGHTNESS:
335 		ret = i2c_smbus_write_byte_data(client, ADV7180_BRI_REG, val);
336 		break;
337 	case V4L2_CID_HUE:
338 		/*Hue is inverted according to HSL chart */
339 		ret = i2c_smbus_write_byte_data(client, ADV7180_HUE_REG, -val);
340 		break;
341 	case V4L2_CID_CONTRAST:
342 		ret = i2c_smbus_write_byte_data(client, ADV7180_CON_REG, val);
343 		break;
344 	case V4L2_CID_SATURATION:
345 		/*
346 		 *This could be V4L2_CID_BLUE_BALANCE/V4L2_CID_RED_BALANCE
347 		 *Let's not confuse the user, everybody understands saturation
348 		 */
349 		ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CB_REG,
350 						val);
351 		if (ret < 0)
352 			break;
353 		ret = i2c_smbus_write_byte_data(client, ADV7180_SD_SAT_CR_REG,
354 						val);
355 		break;
356 	default:
357 		ret = -EINVAL;
358 	}
359 
360 	mutex_unlock(&state->mutex);
361 	return ret;
362 }
363 
364 static const struct v4l2_ctrl_ops adv7180_ctrl_ops = {
365 	.s_ctrl = adv7180_s_ctrl,
366 };
367 
368 static int adv7180_init_controls(struct adv7180_state *state)
369 {
370 	v4l2_ctrl_handler_init(&state->ctrl_hdl, 4);
371 
372 	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
373 			  V4L2_CID_BRIGHTNESS, ADV7180_BRI_MIN,
374 			  ADV7180_BRI_MAX, 1, ADV7180_BRI_DEF);
375 	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
376 			  V4L2_CID_CONTRAST, ADV7180_CON_MIN,
377 			  ADV7180_CON_MAX, 1, ADV7180_CON_DEF);
378 	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
379 			  V4L2_CID_SATURATION, ADV7180_SAT_MIN,
380 			  ADV7180_SAT_MAX, 1, ADV7180_SAT_DEF);
381 	v4l2_ctrl_new_std(&state->ctrl_hdl, &adv7180_ctrl_ops,
382 			  V4L2_CID_HUE, ADV7180_HUE_MIN,
383 			  ADV7180_HUE_MAX, 1, ADV7180_HUE_DEF);
384 	state->sd.ctrl_handler = &state->ctrl_hdl;
385 	if (state->ctrl_hdl.error) {
386 		int err = state->ctrl_hdl.error;
387 
388 		v4l2_ctrl_handler_free(&state->ctrl_hdl);
389 		return err;
390 	}
391 	v4l2_ctrl_handler_setup(&state->ctrl_hdl);
392 
393 	return 0;
394 }
395 static void adv7180_exit_controls(struct adv7180_state *state)
396 {
397 	v4l2_ctrl_handler_free(&state->ctrl_hdl);
398 }
399 
400 static const struct v4l2_subdev_video_ops adv7180_video_ops = {
401 	.querystd = adv7180_querystd,
402 	.g_input_status = adv7180_g_input_status,
403 	.s_routing = adv7180_s_routing,
404 };
405 
406 static const struct v4l2_subdev_core_ops adv7180_core_ops = {
407 	.g_chip_ident = adv7180_g_chip_ident,
408 	.s_std = adv7180_s_std,
409 };
410 
411 static const struct v4l2_subdev_ops adv7180_ops = {
412 	.core = &adv7180_core_ops,
413 	.video = &adv7180_video_ops,
414 };
415 
416 static void adv7180_work(struct work_struct *work)
417 {
418 	struct adv7180_state *state = container_of(work, struct adv7180_state,
419 						   work);
420 	struct i2c_client *client = v4l2_get_subdevdata(&state->sd);
421 	u8 isr3;
422 
423 	mutex_lock(&state->mutex);
424 	i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
425 				  ADV7180_ADI_CTRL_IRQ_SPACE);
426 	isr3 = i2c_smbus_read_byte_data(client, ADV7180_ISR3_ADI);
427 	/* clear */
428 	i2c_smbus_write_byte_data(client, ADV7180_ICR3_ADI, isr3);
429 	i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG, 0);
430 
431 	if (isr3 & ADV7180_IRQ3_AD_CHANGE && state->autodetect)
432 		__adv7180_status(client, NULL, &state->curr_norm);
433 	mutex_unlock(&state->mutex);
434 
435 	enable_irq(state->irq);
436 }
437 
438 static irqreturn_t adv7180_irq(int irq, void *devid)
439 {
440 	struct adv7180_state *state = devid;
441 
442 	schedule_work(&state->work);
443 
444 	disable_irq_nosync(state->irq);
445 
446 	return IRQ_HANDLED;
447 }
448 
449 static int init_device(struct i2c_client *client, struct adv7180_state *state)
450 {
451 	int ret;
452 
453 	/* Initialize adv7180 */
454 	/* Enable autodetection */
455 	if (state->autodetect) {
456 		ret =
457 		    i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
458 				ADV7180_INPUT_CONTROL_AD_PAL_BG_NTSC_J_SECAM
459 					      | state->input);
460 		if (ret < 0)
461 			return ret;
462 
463 		ret =
464 		    i2c_smbus_write_byte_data(client,
465 					      ADV7180_AUTODETECT_ENABLE_REG,
466 					      ADV7180_AUTODETECT_DEFAULT);
467 		if (ret < 0)
468 			return ret;
469 	} else {
470 		ret = v4l2_std_to_adv7180(state->curr_norm);
471 		if (ret < 0)
472 			return ret;
473 
474 		ret =
475 		    i2c_smbus_write_byte_data(client, ADV7180_INPUT_CONTROL_REG,
476 					      ret | state->input);
477 		if (ret < 0)
478 			return ret;
479 
480 	}
481 	/* ITU-R BT.656-4 compatible */
482 	ret = i2c_smbus_write_byte_data(client,
483 			ADV7180_EXTENDED_OUTPUT_CONTROL_REG,
484 			ADV7180_EXTENDED_OUTPUT_CONTROL_NTSCDIS);
485 	if (ret < 0)
486 		return ret;
487 
488 	/* Manually set V bit end position in NTSC mode */
489 	ret = i2c_smbus_write_byte_data(client,
490 					ADV7180_NTSC_V_BIT_END_REG,
491 					ADV7180_NTSC_V_BIT_END_MANUAL_NVEND);
492 	if (ret < 0)
493 		return ret;
494 
495 	/* read current norm */
496 	__adv7180_status(client, NULL, &state->curr_norm);
497 
498 	/* register for interrupts */
499 	if (state->irq > 0) {
500 		ret = request_irq(state->irq, adv7180_irq, 0, KBUILD_MODNAME,
501 				  state);
502 		if (ret)
503 			return ret;
504 
505 		ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
506 						ADV7180_ADI_CTRL_IRQ_SPACE);
507 		if (ret < 0)
508 			return ret;
509 
510 		/* config the Interrupt pin to be active low */
511 		ret = i2c_smbus_write_byte_data(client, ADV7180_ICONF1_ADI,
512 						ADV7180_ICONF1_ACTIVE_LOW |
513 						ADV7180_ICONF1_PSYNC_ONLY);
514 		if (ret < 0)
515 			return ret;
516 
517 		ret = i2c_smbus_write_byte_data(client, ADV7180_IMR1_ADI, 0);
518 		if (ret < 0)
519 			return ret;
520 
521 		ret = i2c_smbus_write_byte_data(client, ADV7180_IMR2_ADI, 0);
522 		if (ret < 0)
523 			return ret;
524 
525 		/* enable AD change interrupts interrupts */
526 		ret = i2c_smbus_write_byte_data(client, ADV7180_IMR3_ADI,
527 						ADV7180_IRQ3_AD_CHANGE);
528 		if (ret < 0)
529 			return ret;
530 
531 		ret = i2c_smbus_write_byte_data(client, ADV7180_IMR4_ADI, 0);
532 		if (ret < 0)
533 			return ret;
534 
535 		ret = i2c_smbus_write_byte_data(client, ADV7180_ADI_CTRL_REG,
536 						0);
537 		if (ret < 0)
538 			return ret;
539 	}
540 
541 	return 0;
542 }
543 
544 static int adv7180_probe(struct i2c_client *client,
545 			 const struct i2c_device_id *id)
546 {
547 	struct adv7180_state *state;
548 	struct v4l2_subdev *sd;
549 	int ret;
550 
551 	/* Check if the adapter supports the needed features */
552 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
553 		return -EIO;
554 
555 	v4l_info(client, "chip found @ 0x%02x (%s)\n",
556 		 client->addr, client->adapter->name);
557 
558 	state = kzalloc(sizeof(struct adv7180_state), GFP_KERNEL);
559 	if (state == NULL) {
560 		ret = -ENOMEM;
561 		goto err;
562 	}
563 
564 	state->irq = client->irq;
565 	INIT_WORK(&state->work, adv7180_work);
566 	mutex_init(&state->mutex);
567 	state->autodetect = true;
568 	state->input = 0;
569 	sd = &state->sd;
570 	v4l2_i2c_subdev_init(sd, client, &adv7180_ops);
571 
572 	ret = adv7180_init_controls(state);
573 	if (ret)
574 		goto err_unreg_subdev;
575 	ret = init_device(client, state);
576 	if (ret)
577 		goto err_free_ctrl;
578 	return 0;
579 
580 err_free_ctrl:
581 	adv7180_exit_controls(state);
582 err_unreg_subdev:
583 	mutex_destroy(&state->mutex);
584 	v4l2_device_unregister_subdev(sd);
585 	kfree(state);
586 err:
587 	printk(KERN_ERR KBUILD_MODNAME ": Failed to probe: %d\n", ret);
588 	return ret;
589 }
590 
591 static int adv7180_remove(struct i2c_client *client)
592 {
593 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
594 	struct adv7180_state *state = to_state(sd);
595 
596 	if (state->irq > 0) {
597 		free_irq(client->irq, state);
598 		if (cancel_work_sync(&state->work)) {
599 			/*
600 			 * Work was pending, therefore we need to enable
601 			 * IRQ here to balance the disable_irq() done in the
602 			 * interrupt handler.
603 			 */
604 			enable_irq(state->irq);
605 		}
606 	}
607 
608 	mutex_destroy(&state->mutex);
609 	v4l2_device_unregister_subdev(sd);
610 	kfree(to_state(sd));
611 	return 0;
612 }
613 
614 static const struct i2c_device_id adv7180_id[] = {
615 	{KBUILD_MODNAME, 0},
616 	{},
617 };
618 
619 #ifdef CONFIG_PM
620 static int adv7180_suspend(struct i2c_client *client, pm_message_t state)
621 {
622 	int ret;
623 
624 	ret = i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG,
625 					ADV7180_PWR_MAN_OFF);
626 	if (ret < 0)
627 		return ret;
628 	return 0;
629 }
630 
631 static int adv7180_resume(struct i2c_client *client)
632 {
633 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
634 	struct adv7180_state *state = to_state(sd);
635 	int ret;
636 
637 	ret = i2c_smbus_write_byte_data(client, ADV7180_PWR_MAN_REG,
638 					ADV7180_PWR_MAN_ON);
639 	if (ret < 0)
640 		return ret;
641 	ret = init_device(client, state);
642 	if (ret < 0)
643 		return ret;
644 	return 0;
645 }
646 #endif
647 
648 MODULE_DEVICE_TABLE(i2c, adv7180_id);
649 
650 static struct i2c_driver adv7180_driver = {
651 	.driver = {
652 		   .owner = THIS_MODULE,
653 		   .name = KBUILD_MODNAME,
654 		   },
655 	.probe = adv7180_probe,
656 	.remove = adv7180_remove,
657 #ifdef CONFIG_PM
658 	.suspend = adv7180_suspend,
659 	.resume = adv7180_resume,
660 #endif
661 	.id_table = adv7180_id,
662 };
663 
664 module_i2c_driver(adv7180_driver);
665 
666 MODULE_DESCRIPTION("Analog Devices ADV7180 video decoder driver");
667 MODULE_AUTHOR("Mocean Laboratories");
668 MODULE_LICENSE("GPL v2");
669