xref: /linux/drivers/media/i2c/ov2680.c (revision 2d7f3d1a5866705be2393150e1ffdf67030ab88d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Omnivision OV2680 CMOS Image Sensor driver
4  *
5  * Copyright (C) 2018 Linaro Ltd
6  *
7  * Based on OV5640 Sensor Driver
8  * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
9  * Copyright (C) 2014-2017 Mentor Graphics Inc.
10  *
11  */
12 
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 
25 #include <media/v4l2-cci.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-ctrls.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-subdev.h>
30 
31 #define OV2680_CHIP_ID				0x2680
32 
33 #define OV2680_REG_STREAM_CTRL			CCI_REG8(0x0100)
34 #define OV2680_REG_SOFT_RESET			CCI_REG8(0x0103)
35 
36 #define OV2680_REG_CHIP_ID			CCI_REG16(0x300a)
37 #define OV2680_REG_SC_CMMN_SUB_ID		CCI_REG8(0x302a)
38 #define OV2680_REG_PLL_MULTIPLIER		CCI_REG16(0x3081)
39 
40 #define OV2680_REG_EXPOSURE_PK			CCI_REG24(0x3500)
41 #define OV2680_REG_R_MANUAL			CCI_REG8(0x3503)
42 #define OV2680_REG_GAIN_PK			CCI_REG16(0x350a)
43 
44 #define OV2680_REG_SENSOR_CTRL_0A		CCI_REG8(0x370a)
45 
46 #define OV2680_REG_HORIZONTAL_START		CCI_REG16(0x3800)
47 #define OV2680_REG_VERTICAL_START		CCI_REG16(0x3802)
48 #define OV2680_REG_HORIZONTAL_END		CCI_REG16(0x3804)
49 #define OV2680_REG_VERTICAL_END			CCI_REG16(0x3806)
50 #define OV2680_REG_HORIZONTAL_OUTPUT_SIZE	CCI_REG16(0x3808)
51 #define OV2680_REG_VERTICAL_OUTPUT_SIZE		CCI_REG16(0x380a)
52 #define OV2680_REG_TIMING_HTS			CCI_REG16(0x380c)
53 #define OV2680_REG_TIMING_VTS			CCI_REG16(0x380e)
54 #define OV2680_REG_ISP_X_WIN			CCI_REG16(0x3810)
55 #define OV2680_REG_ISP_Y_WIN			CCI_REG16(0x3812)
56 #define OV2680_REG_X_INC			CCI_REG8(0x3814)
57 #define OV2680_REG_Y_INC			CCI_REG8(0x3815)
58 #define OV2680_REG_FORMAT1			CCI_REG8(0x3820)
59 #define OV2680_REG_FORMAT2			CCI_REG8(0x3821)
60 
61 #define OV2680_REG_ISP_CTRL00			CCI_REG8(0x5080)
62 
63 #define OV2680_REG_X_WIN			CCI_REG16(0x5704)
64 #define OV2680_REG_Y_WIN			CCI_REG16(0x5706)
65 
66 #define OV2680_FRAME_RATE			30
67 
68 #define OV2680_NATIVE_WIDTH			1616
69 #define OV2680_NATIVE_HEIGHT			1216
70 #define OV2680_NATIVE_START_LEFT		0
71 #define OV2680_NATIVE_START_TOP			0
72 #define OV2680_ACTIVE_WIDTH			1600
73 #define OV2680_ACTIVE_HEIGHT			1200
74 #define OV2680_ACTIVE_START_LEFT		8
75 #define OV2680_ACTIVE_START_TOP			8
76 #define OV2680_MIN_CROP_WIDTH			2
77 #define OV2680_MIN_CROP_HEIGHT			2
78 
79 /* Fixed pre-div of 1/2 */
80 #define OV2680_PLL_PREDIV0			2
81 
82 /* Pre-div configurable through reg 0x3080, left at its default of 0x02 : 1/2 */
83 #define OV2680_PLL_PREDIV			2
84 
85 /* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */
86 #define OV2680_PIXELS_PER_LINE			1704
87 #define OV2680_LINES_PER_FRAME			1294
88 
89 /* If possible send 16 extra rows / lines to the ISP as padding */
90 #define OV2680_END_MARGIN			16
91 
92 /* Max exposure time is VTS - 8 */
93 #define OV2680_INTEGRATION_TIME_MARGIN		8
94 
95 #define OV2680_DEFAULT_WIDTH			800
96 #define OV2680_DEFAULT_HEIGHT			600
97 
98 /* For enum_frame_size() full-size + binned-/quarter-size */
99 #define OV2680_FRAME_SIZES			2
100 
101 static const char * const ov2680_supply_name[] = {
102 	"DOVDD",
103 	"DVDD",
104 	"AVDD",
105 };
106 
107 #define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
108 
109 enum {
110 	OV2680_19_2_MHZ,
111 	OV2680_24_MHZ,
112 };
113 
114 static const unsigned long ov2680_xvclk_freqs[] = {
115 	[OV2680_19_2_MHZ] = 19200000,
116 	[OV2680_24_MHZ] = 24000000,
117 };
118 
119 static const u8 ov2680_pll_multipliers[] = {
120 	[OV2680_19_2_MHZ] = 69,
121 	[OV2680_24_MHZ] = 55,
122 };
123 
124 struct ov2680_ctrls {
125 	struct v4l2_ctrl_handler handler;
126 	struct v4l2_ctrl *exposure;
127 	struct v4l2_ctrl *gain;
128 	struct v4l2_ctrl *hflip;
129 	struct v4l2_ctrl *vflip;
130 	struct v4l2_ctrl *test_pattern;
131 	struct v4l2_ctrl *link_freq;
132 	struct v4l2_ctrl *pixel_rate;
133 };
134 
135 struct ov2680_mode {
136 	struct v4l2_rect		crop;
137 	struct v4l2_mbus_framefmt	fmt;
138 	struct v4l2_fract		frame_interval;
139 	bool				binning;
140 	u16				h_start;
141 	u16				v_start;
142 	u16				h_end;
143 	u16				v_end;
144 	u16				h_output_size;
145 	u16				v_output_size;
146 	u16				hts;
147 	u16				vts;
148 };
149 
150 struct ov2680_dev {
151 	struct device			*dev;
152 	struct regmap			*regmap;
153 	struct v4l2_subdev		sd;
154 
155 	struct media_pad		pad;
156 	struct clk			*xvclk;
157 	u32				xvclk_freq;
158 	u8				pll_mult;
159 	s64				link_freq[1];
160 	u64				pixel_rate;
161 	struct regulator_bulk_data	supplies[OV2680_NUM_SUPPLIES];
162 
163 	struct gpio_desc		*pwdn_gpio;
164 	struct mutex			lock; /* protect members */
165 
166 	bool				is_streaming;
167 
168 	struct ov2680_ctrls		ctrls;
169 	struct ov2680_mode		mode;
170 };
171 
172 static const struct v4l2_rect ov2680_default_crop = {
173 	.left = OV2680_ACTIVE_START_LEFT,
174 	.top = OV2680_ACTIVE_START_TOP,
175 	.width = OV2680_ACTIVE_WIDTH,
176 	.height = OV2680_ACTIVE_HEIGHT,
177 };
178 
179 static const char * const test_pattern_menu[] = {
180 	"Disabled",
181 	"Color Bars",
182 	"Random Data",
183 	"Square",
184 	"Black Image",
185 };
186 
187 static const int ov2680_hv_flip_bayer_order[] = {
188 	MEDIA_BUS_FMT_SBGGR10_1X10,
189 	MEDIA_BUS_FMT_SGRBG10_1X10,
190 	MEDIA_BUS_FMT_SGBRG10_1X10,
191 	MEDIA_BUS_FMT_SRGGB10_1X10,
192 };
193 
194 static const struct reg_sequence ov2680_global_setting[] = {
195 	/* MIPI PHY, 0x10 -> 0x1c enable bp_c_hs_en_lat and bp_d_hs_en_lat */
196 	{0x3016, 0x1c},
197 
198 	/* R MANUAL set exposure and gain to manual (hw does not do auto) */
199 	{0x3503, 0x03},
200 
201 	/* Analog control register tweaks */
202 	{0x3603, 0x39}, /* Reset value 0x99 */
203 	{0x3604, 0x24}, /* Reset value 0x74 */
204 	{0x3621, 0x37}, /* Reset value 0x44 */
205 
206 	/* Sensor control register tweaks */
207 	{0x3701, 0x64}, /* Reset value 0x61 */
208 	{0x3705, 0x3c}, /* Reset value 0x21 */
209 	{0x370c, 0x50}, /* Reset value 0x10 */
210 	{0x370d, 0xc0}, /* Reset value 0x00 */
211 	{0x3718, 0x88}, /* Reset value 0x80 */
212 
213 	/* PSRAM tweaks */
214 	{0x3781, 0x80}, /* Reset value 0x00 */
215 	{0x3784, 0x0c}, /* Reset value 0x00, based on OV2680_R1A_AM10.ovt */
216 	{0x3789, 0x60}, /* Reset value 0x50 */
217 
218 	/* BLC CTRL00 0x01 -> 0x81 set avg_weight to 8 */
219 	{0x4000, 0x81},
220 
221 	/* Set black level compensation range to 0 - 3 (default 0 - 11) */
222 	{0x4008, 0x00},
223 	{0x4009, 0x03},
224 
225 	/* VFIFO R2 0x00 -> 0x02 set Frame reset enable */
226 	{0x4602, 0x02},
227 
228 	/* MIPI ctrl CLK PREPARE MIN change from 0x26 (38) -> 0x36 (54) */
229 	{0x481f, 0x36},
230 
231 	/* MIPI ctrl CLK LPX P MIN change from 0x32 (50) -> 0x36 (54) */
232 	{0x4825, 0x36},
233 
234 	/* R ISP CTRL2 0x20 -> 0x30, set sof_sel bit */
235 	{0x5002, 0x30},
236 
237 	/*
238 	 * Window CONTROL 0x00 -> 0x01, enable manual window control,
239 	 * this is necessary for full size flip and mirror support.
240 	 */
241 	{0x5708, 0x01},
242 
243 	/*
244 	 * DPC CTRL0 0x14 -> 0x3e, set enable_tail, enable_3x3_cluster
245 	 * and enable_general_tail bits based OV2680_R1A_AM10.ovt.
246 	 */
247 	{0x5780, 0x3e},
248 
249 	/* DPC MORE CONNECTION CASE THRE 0x0c (12) -> 0x02 (2) */
250 	{0x5788, 0x02},
251 
252 	/* DPC GAIN LIST1 0x0f (15) -> 0x08 (8) */
253 	{0x578e, 0x08},
254 
255 	/* DPC GAIN LIST2 0x3f (63) -> 0x0c (12) */
256 	{0x578f, 0x0c},
257 
258 	/* DPC THRE RATIO 0x04 (4) -> 0x00 (0) */
259 	{0x5792, 0x00},
260 };
261 
262 static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
263 {
264 	return container_of(sd, struct ov2680_dev, sd);
265 }
266 
267 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
268 {
269 	return &container_of(ctrl->handler, struct ov2680_dev,
270 			     ctrls.handler)->sd;
271 }
272 
273 static void ov2680_power_up(struct ov2680_dev *sensor)
274 {
275 	if (!sensor->pwdn_gpio)
276 		return;
277 
278 	gpiod_set_value(sensor->pwdn_gpio, 0);
279 	usleep_range(5000, 10000);
280 }
281 
282 static void ov2680_power_down(struct ov2680_dev *sensor)
283 {
284 	if (!sensor->pwdn_gpio)
285 		return;
286 
287 	gpiod_set_value(sensor->pwdn_gpio, 1);
288 	usleep_range(5000, 10000);
289 }
290 
291 static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
292 				   struct v4l2_mbus_framefmt *fmt)
293 {
294 	int hv_flip = 0;
295 
296 	if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
297 		hv_flip += 1;
298 
299 	if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
300 		hv_flip += 2;
301 
302 	fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
303 }
304 
305 static struct v4l2_mbus_framefmt *
306 __ov2680_get_pad_format(struct ov2680_dev *sensor,
307 			struct v4l2_subdev_state *state,
308 			unsigned int pad,
309 			enum v4l2_subdev_format_whence which)
310 {
311 	if (which == V4L2_SUBDEV_FORMAT_TRY)
312 		return v4l2_subdev_state_get_format(state, pad);
313 
314 	return &sensor->mode.fmt;
315 }
316 
317 static struct v4l2_rect *
318 __ov2680_get_pad_crop(struct ov2680_dev *sensor,
319 		      struct v4l2_subdev_state *state,
320 		      unsigned int pad,
321 		      enum v4l2_subdev_format_whence which)
322 {
323 	if (which == V4L2_SUBDEV_FORMAT_TRY)
324 		return v4l2_subdev_state_get_crop(state, pad);
325 
326 	return &sensor->mode.crop;
327 }
328 
329 static void ov2680_fill_format(struct ov2680_dev *sensor,
330 			       struct v4l2_mbus_framefmt *fmt,
331 			       unsigned int width, unsigned int height)
332 {
333 	memset(fmt, 0, sizeof(*fmt));
334 	fmt->width = width;
335 	fmt->height = height;
336 	fmt->field = V4L2_FIELD_NONE;
337 	fmt->colorspace = V4L2_COLORSPACE_SRGB;
338 	ov2680_set_bayer_order(sensor, fmt);
339 }
340 
341 static void ov2680_calc_mode(struct ov2680_dev *sensor)
342 {
343 	int width = sensor->mode.fmt.width;
344 	int height = sensor->mode.fmt.height;
345 	int orig_width = width;
346 	int orig_height = height;
347 
348 	if (width  <= (sensor->mode.crop.width / 2) &&
349 	    height <= (sensor->mode.crop.height / 2)) {
350 		sensor->mode.binning = true;
351 		width *= 2;
352 		height *= 2;
353 	} else {
354 		sensor->mode.binning = false;
355 	}
356 
357 	sensor->mode.h_start = (sensor->mode.crop.left +
358 				(sensor->mode.crop.width - width) / 2) & ~1;
359 	sensor->mode.v_start = (sensor->mode.crop.top +
360 				(sensor->mode.crop.height - height) / 2) & ~1;
361 	sensor->mode.h_end =
362 		min(sensor->mode.h_start + width + OV2680_END_MARGIN - 1,
363 		    OV2680_NATIVE_WIDTH - 1);
364 	sensor->mode.v_end =
365 		min(sensor->mode.v_start + height + OV2680_END_MARGIN - 1,
366 		    OV2680_NATIVE_HEIGHT - 1);
367 	sensor->mode.h_output_size = orig_width;
368 	sensor->mode.v_output_size = orig_height;
369 	sensor->mode.hts = OV2680_PIXELS_PER_LINE;
370 	sensor->mode.vts = OV2680_LINES_PER_FRAME;
371 }
372 
373 static int ov2680_set_mode(struct ov2680_dev *sensor)
374 {
375 	u8 sensor_ctrl_0a, inc, fmt1, fmt2;
376 	int ret = 0;
377 
378 	if (sensor->mode.binning) {
379 		sensor_ctrl_0a = 0x23;
380 		inc = 0x31;
381 		fmt1 = 0xc2;
382 		fmt2 = 0x01;
383 	} else {
384 		sensor_ctrl_0a = 0x21;
385 		inc = 0x11;
386 		fmt1 = 0xc0;
387 		fmt2 = 0x00;
388 	}
389 
390 	cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A,
391 		  sensor_ctrl_0a, &ret);
392 	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START,
393 		  sensor->mode.h_start, &ret);
394 	cci_write(sensor->regmap, OV2680_REG_VERTICAL_START,
395 		  sensor->mode.v_start, &ret);
396 	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END,
397 		  sensor->mode.h_end, &ret);
398 	cci_write(sensor->regmap, OV2680_REG_VERTICAL_END,
399 		  sensor->mode.v_end, &ret);
400 	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE,
401 		  sensor->mode.h_output_size, &ret);
402 	cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE,
403 		  sensor->mode.v_output_size, &ret);
404 	cci_write(sensor->regmap, OV2680_REG_TIMING_HTS,
405 		  sensor->mode.hts, &ret);
406 	cci_write(sensor->regmap, OV2680_REG_TIMING_VTS,
407 		  sensor->mode.vts, &ret);
408 	cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret);
409 	cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret);
410 	cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret);
411 	cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret);
412 	cci_write(sensor->regmap, OV2680_REG_X_WIN,
413 		  sensor->mode.h_output_size, &ret);
414 	cci_write(sensor->regmap, OV2680_REG_Y_WIN,
415 		  sensor->mode.v_output_size, &ret);
416 	cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret);
417 	cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret);
418 
419 	return ret;
420 }
421 
422 static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
423 {
424 	int ret;
425 
426 	if (sensor->is_streaming)
427 		return -EBUSY;
428 
429 	ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
430 			      BIT(2), val ? BIT(2) : 0, NULL);
431 	if (ret < 0)
432 		return ret;
433 
434 	ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
435 	return 0;
436 }
437 
438 static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
439 {
440 	int ret;
441 
442 	if (sensor->is_streaming)
443 		return -EBUSY;
444 
445 	ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
446 			      BIT(2), val ? BIT(2) : 0, NULL);
447 	if (ret < 0)
448 		return ret;
449 
450 	ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
451 	return 0;
452 }
453 
454 static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
455 {
456 	int ret = 0;
457 
458 	if (!value)
459 		return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
460 				       BIT(7), 0, NULL);
461 
462 	cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
463 			0x03, value - 1, &ret);
464 	cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
465 			BIT(7), BIT(7), &ret);
466 
467 	return ret;
468 }
469 
470 static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
471 {
472 	return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
473 }
474 
475 static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
476 {
477 	return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
478 			 NULL);
479 }
480 
481 static int ov2680_stream_enable(struct ov2680_dev *sensor)
482 {
483 	int ret;
484 
485 	ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER,
486 			sensor->pll_mult, NULL);
487 	if (ret < 0)
488 		return ret;
489 
490 	ret = regmap_multi_reg_write(sensor->regmap,
491 				     ov2680_global_setting,
492 				     ARRAY_SIZE(ov2680_global_setting));
493 	if (ret < 0)
494 		return ret;
495 
496 	ret = ov2680_set_mode(sensor);
497 	if (ret < 0)
498 		return ret;
499 
500 	/* Restore value of all ctrls */
501 	ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
502 	if (ret < 0)
503 		return ret;
504 
505 	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
506 }
507 
508 static int ov2680_stream_disable(struct ov2680_dev *sensor)
509 {
510 	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
511 }
512 
513 static int ov2680_power_off(struct ov2680_dev *sensor)
514 {
515 	clk_disable_unprepare(sensor->xvclk);
516 	ov2680_power_down(sensor);
517 	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
518 	return 0;
519 }
520 
521 static int ov2680_power_on(struct ov2680_dev *sensor)
522 {
523 	int ret;
524 
525 	ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
526 	if (ret < 0) {
527 		dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
528 		return ret;
529 	}
530 
531 	if (!sensor->pwdn_gpio) {
532 		ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
533 				NULL);
534 		if (ret != 0) {
535 			dev_err(sensor->dev, "sensor soft reset failed\n");
536 			goto err_disable_regulators;
537 		}
538 		usleep_range(1000, 2000);
539 	} else {
540 		ov2680_power_down(sensor);
541 		ov2680_power_up(sensor);
542 	}
543 
544 	ret = clk_prepare_enable(sensor->xvclk);
545 	if (ret < 0)
546 		goto err_disable_regulators;
547 
548 	return 0;
549 
550 err_disable_regulators:
551 	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
552 	return ret;
553 }
554 
555 static int ov2680_get_frame_interval(struct v4l2_subdev *sd,
556 				     struct v4l2_subdev_state *sd_state,
557 				     struct v4l2_subdev_frame_interval *fi)
558 {
559 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
560 
561 	/*
562 	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
563 	 * subdev active state API.
564 	 */
565 	if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
566 		return -EINVAL;
567 
568 	mutex_lock(&sensor->lock);
569 	fi->interval = sensor->mode.frame_interval;
570 	mutex_unlock(&sensor->lock);
571 
572 	return 0;
573 }
574 
575 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
576 {
577 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
578 	int ret = 0;
579 
580 	mutex_lock(&sensor->lock);
581 
582 	if (sensor->is_streaming == !!enable)
583 		goto unlock;
584 
585 	if (enable) {
586 		ret = pm_runtime_resume_and_get(sensor->sd.dev);
587 		if (ret < 0)
588 			goto unlock;
589 
590 		ret = ov2680_stream_enable(sensor);
591 		if (ret < 0) {
592 			pm_runtime_put(sensor->sd.dev);
593 			goto unlock;
594 		}
595 	} else {
596 		ret = ov2680_stream_disable(sensor);
597 		pm_runtime_put(sensor->sd.dev);
598 	}
599 
600 	sensor->is_streaming = !!enable;
601 
602 unlock:
603 	mutex_unlock(&sensor->lock);
604 
605 	return ret;
606 }
607 
608 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
609 				 struct v4l2_subdev_state *sd_state,
610 				 struct v4l2_subdev_mbus_code_enum *code)
611 {
612 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
613 
614 	if (code->index != 0)
615 		return -EINVAL;
616 
617 	code->code = sensor->mode.fmt.code;
618 
619 	return 0;
620 }
621 
622 static int ov2680_get_fmt(struct v4l2_subdev *sd,
623 			  struct v4l2_subdev_state *sd_state,
624 			  struct v4l2_subdev_format *format)
625 {
626 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
627 	struct v4l2_mbus_framefmt *fmt;
628 
629 	fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad,
630 				      format->which);
631 
632 	mutex_lock(&sensor->lock);
633 	format->format = *fmt;
634 	mutex_unlock(&sensor->lock);
635 
636 	return 0;
637 }
638 
639 static int ov2680_set_fmt(struct v4l2_subdev *sd,
640 			  struct v4l2_subdev_state *sd_state,
641 			  struct v4l2_subdev_format *format)
642 {
643 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
644 	struct v4l2_mbus_framefmt *try_fmt;
645 	const struct v4l2_rect *crop;
646 	unsigned int width, height;
647 	int ret = 0;
648 
649 	crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad,
650 				     format->which);
651 
652 	/* Limit set_fmt max size to crop width / height */
653 	width = clamp_val(ALIGN(format->format.width, 2),
654 			  OV2680_MIN_CROP_WIDTH, crop->width);
655 	height = clamp_val(ALIGN(format->format.height, 2),
656 			   OV2680_MIN_CROP_HEIGHT, crop->height);
657 
658 	ov2680_fill_format(sensor, &format->format, width, height);
659 
660 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
661 		try_fmt = v4l2_subdev_state_get_format(sd_state, 0);
662 		*try_fmt = format->format;
663 		return 0;
664 	}
665 
666 	mutex_lock(&sensor->lock);
667 
668 	if (sensor->is_streaming) {
669 		ret = -EBUSY;
670 		goto unlock;
671 	}
672 
673 	sensor->mode.fmt = format->format;
674 	ov2680_calc_mode(sensor);
675 
676 unlock:
677 	mutex_unlock(&sensor->lock);
678 
679 	return ret;
680 }
681 
682 static int ov2680_get_selection(struct v4l2_subdev *sd,
683 				struct v4l2_subdev_state *state,
684 				struct v4l2_subdev_selection *sel)
685 {
686 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
687 
688 	switch (sel->target) {
689 	case V4L2_SEL_TGT_CROP:
690 		mutex_lock(&sensor->lock);
691 		sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad,
692 						sel->which);
693 		mutex_unlock(&sensor->lock);
694 		break;
695 	case V4L2_SEL_TGT_NATIVE_SIZE:
696 	case V4L2_SEL_TGT_CROP_BOUNDS:
697 		sel->r.top = 0;
698 		sel->r.left = 0;
699 		sel->r.width = OV2680_NATIVE_WIDTH;
700 		sel->r.height = OV2680_NATIVE_HEIGHT;
701 		break;
702 	case V4L2_SEL_TGT_CROP_DEFAULT:
703 		sel->r = ov2680_default_crop;
704 		break;
705 	default:
706 		return -EINVAL;
707 	}
708 
709 	return 0;
710 }
711 
712 static int ov2680_set_selection(struct v4l2_subdev *sd,
713 				struct v4l2_subdev_state *state,
714 				struct v4l2_subdev_selection *sel)
715 {
716 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
717 	struct v4l2_mbus_framefmt *format;
718 	struct v4l2_rect *crop;
719 	struct v4l2_rect rect;
720 
721 	if (sel->target != V4L2_SEL_TGT_CROP)
722 		return -EINVAL;
723 
724 	/*
725 	 * Clamp the boundaries of the crop rectangle to the size of the sensor
726 	 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
727 	 * disrupted.
728 	 */
729 	rect.left = clamp_val(ALIGN(sel->r.left, 2),
730 			      OV2680_NATIVE_START_LEFT, OV2680_NATIVE_WIDTH);
731 	rect.top = clamp_val(ALIGN(sel->r.top, 2),
732 			     OV2680_NATIVE_START_TOP, OV2680_NATIVE_HEIGHT);
733 	rect.width = clamp_val(ALIGN(sel->r.width, 2),
734 			       OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH);
735 	rect.height = clamp_val(ALIGN(sel->r.height, 2),
736 				OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT);
737 
738 	/* Make sure the crop rectangle isn't outside the bounds of the array */
739 	rect.width = min_t(unsigned int, rect.width,
740 			   OV2680_NATIVE_WIDTH - rect.left);
741 	rect.height = min_t(unsigned int, rect.height,
742 			    OV2680_NATIVE_HEIGHT - rect.top);
743 
744 	crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
745 
746 	mutex_lock(&sensor->lock);
747 	if (rect.width != crop->width || rect.height != crop->height) {
748 		/*
749 		 * Reset the output image size if the crop rectangle size has
750 		 * been modified.
751 		 */
752 		format = __ov2680_get_pad_format(sensor, state, sel->pad,
753 						 sel->which);
754 		format->width = rect.width;
755 		format->height = rect.height;
756 	}
757 
758 	*crop = rect;
759 	mutex_unlock(&sensor->lock);
760 
761 	sel->r = rect;
762 
763 	return 0;
764 }
765 
766 static int ov2680_init_state(struct v4l2_subdev *sd,
767 			     struct v4l2_subdev_state *sd_state)
768 {
769 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
770 
771 	*v4l2_subdev_state_get_crop(sd_state, 0) = ov2680_default_crop;
772 
773 	ov2680_fill_format(sensor, v4l2_subdev_state_get_format(sd_state, 0),
774 			   OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
775 	return 0;
776 }
777 
778 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
779 				  struct v4l2_subdev_state *sd_state,
780 				  struct v4l2_subdev_frame_size_enum *fse)
781 {
782 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
783 	struct v4l2_rect *crop;
784 
785 	if (fse->index >= OV2680_FRAME_SIZES)
786 		return -EINVAL;
787 
788 	crop = __ov2680_get_pad_crop(sensor, sd_state, fse->pad, fse->which);
789 	if (!crop)
790 		return -EINVAL;
791 
792 	fse->min_width = crop->width / (fse->index + 1);
793 	fse->min_height = crop->height / (fse->index + 1);
794 	fse->max_width = fse->min_width;
795 	fse->max_height = fse->min_height;
796 
797 	return 0;
798 }
799 
800 static bool ov2680_valid_frame_size(struct v4l2_subdev *sd,
801 				    struct v4l2_subdev_state *sd_state,
802 				    struct v4l2_subdev_frame_interval_enum *fie)
803 {
804 	struct v4l2_subdev_frame_size_enum fse = {
805 		.pad = fie->pad,
806 		.which = fie->which,
807 	};
808 	int i;
809 
810 	for (i = 0; i < OV2680_FRAME_SIZES; i++) {
811 		fse.index = i;
812 
813 		if (ov2680_enum_frame_size(sd, sd_state, &fse))
814 			return false;
815 
816 		if (fie->width == fse.min_width &&
817 		    fie->height == fse.min_height)
818 			return true;
819 	}
820 
821 	return false;
822 }
823 
824 static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
825 			      struct v4l2_subdev_state *sd_state,
826 			      struct v4l2_subdev_frame_interval_enum *fie)
827 {
828 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
829 
830 	/* Only 1 framerate */
831 	if (fie->index || !ov2680_valid_frame_size(sd, sd_state, fie))
832 		return -EINVAL;
833 
834 	fie->interval = sensor->mode.frame_interval;
835 
836 	return 0;
837 }
838 
839 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
840 {
841 	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
842 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
843 	int ret;
844 
845 	/* Only apply changes to the controls if the device is powered up */
846 	if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
847 		ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
848 		return 0;
849 	}
850 
851 	switch (ctrl->id) {
852 	case V4L2_CID_ANALOGUE_GAIN:
853 		ret = ov2680_gain_set(sensor, ctrl->val);
854 		break;
855 	case V4L2_CID_EXPOSURE:
856 		ret = ov2680_exposure_set(sensor, ctrl->val);
857 		break;
858 	case V4L2_CID_VFLIP:
859 		ret = ov2680_set_vflip(sensor, ctrl->val);
860 		break;
861 	case V4L2_CID_HFLIP:
862 		ret = ov2680_set_hflip(sensor, ctrl->val);
863 		break;
864 	case V4L2_CID_TEST_PATTERN:
865 		ret = ov2680_test_pattern_set(sensor, ctrl->val);
866 		break;
867 	default:
868 		ret = -EINVAL;
869 		break;
870 	}
871 
872 	pm_runtime_put(sensor->sd.dev);
873 	return ret;
874 }
875 
876 static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
877 	.s_ctrl = ov2680_s_ctrl,
878 };
879 
880 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
881 	.s_stream		= ov2680_s_stream,
882 };
883 
884 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
885 	.enum_mbus_code		= ov2680_enum_mbus_code,
886 	.enum_frame_size	= ov2680_enum_frame_size,
887 	.enum_frame_interval	= ov2680_enum_frame_interval,
888 	.get_fmt		= ov2680_get_fmt,
889 	.set_fmt		= ov2680_set_fmt,
890 	.get_selection		= ov2680_get_selection,
891 	.set_selection		= ov2680_set_selection,
892 	.get_frame_interval	= ov2680_get_frame_interval,
893 	.set_frame_interval	= ov2680_get_frame_interval,
894 };
895 
896 static const struct v4l2_subdev_ops ov2680_subdev_ops = {
897 	.video	= &ov2680_video_ops,
898 	.pad	= &ov2680_pad_ops,
899 };
900 
901 static const struct v4l2_subdev_internal_ops ov2680_internal_ops = {
902 	.init_state		= ov2680_init_state,
903 };
904 
905 static int ov2680_mode_init(struct ov2680_dev *sensor)
906 {
907 	/* set initial mode */
908 	sensor->mode.crop = ov2680_default_crop;
909 	ov2680_fill_format(sensor, &sensor->mode.fmt,
910 			   OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
911 	ov2680_calc_mode(sensor);
912 
913 	sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
914 	sensor->mode.frame_interval.numerator = 1;
915 
916 	return 0;
917 }
918 
919 static int ov2680_v4l2_register(struct ov2680_dev *sensor)
920 {
921 	struct i2c_client *client = to_i2c_client(sensor->dev);
922 	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
923 	struct ov2680_ctrls *ctrls = &sensor->ctrls;
924 	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
925 	int exp_max = OV2680_LINES_PER_FRAME - OV2680_INTEGRATION_TIME_MARGIN;
926 	int ret = 0;
927 
928 	v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
929 	sensor->sd.internal_ops = &ov2680_internal_ops;
930 
931 	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
932 	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
933 	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
934 
935 	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
936 	if (ret < 0)
937 		return ret;
938 
939 	v4l2_ctrl_handler_init(hdl, 5);
940 
941 	hdl->lock = &sensor->lock;
942 
943 	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
944 	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
945 
946 	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
947 					&ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
948 					ARRAY_SIZE(test_pattern_menu) - 1,
949 					0, 0, test_pattern_menu);
950 
951 	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
952 					    0, exp_max, 1, exp_max);
953 
954 	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
955 					0, 1023, 1, 250);
956 
957 	ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, NULL, V4L2_CID_LINK_FREQ,
958 						  0, 0, sensor->link_freq);
959 	ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, NULL, V4L2_CID_PIXEL_RATE,
960 					      0, sensor->pixel_rate,
961 					      1, sensor->pixel_rate);
962 
963 	if (hdl->error) {
964 		ret = hdl->error;
965 		goto cleanup_entity;
966 	}
967 
968 	ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
969 	ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
970 	ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
971 
972 	sensor->sd.ctrl_handler = hdl;
973 
974 	ret = v4l2_async_register_subdev(&sensor->sd);
975 	if (ret < 0)
976 		goto cleanup_entity;
977 
978 	return 0;
979 
980 cleanup_entity:
981 	media_entity_cleanup(&sensor->sd.entity);
982 	v4l2_ctrl_handler_free(hdl);
983 
984 	return ret;
985 }
986 
987 static int ov2680_get_regulators(struct ov2680_dev *sensor)
988 {
989 	int i;
990 
991 	for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
992 		sensor->supplies[i].supply = ov2680_supply_name[i];
993 
994 	return devm_regulator_bulk_get(sensor->dev,
995 				       OV2680_NUM_SUPPLIES, sensor->supplies);
996 }
997 
998 static int ov2680_check_id(struct ov2680_dev *sensor)
999 {
1000 	u64 chip_id, rev;
1001 	int ret = 0;
1002 
1003 	cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, &ret);
1004 	cci_read(sensor->regmap, OV2680_REG_SC_CMMN_SUB_ID, &rev, &ret);
1005 	if (ret < 0) {
1006 		dev_err(sensor->dev, "failed to read chip id\n");
1007 		return ret;
1008 	}
1009 
1010 	if (chip_id != OV2680_CHIP_ID) {
1011 		dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n",
1012 			chip_id, OV2680_CHIP_ID);
1013 		return -ENODEV;
1014 	}
1015 
1016 	dev_info(sensor->dev, "sensor_revision id = 0x%llx, rev= %lld\n",
1017 		 chip_id, rev & 0x0f);
1018 
1019 	return 0;
1020 }
1021 
1022 static int ov2680_parse_dt(struct ov2680_dev *sensor)
1023 {
1024 	struct v4l2_fwnode_endpoint bus_cfg = {
1025 		.bus_type = V4L2_MBUS_CSI2_DPHY,
1026 	};
1027 	struct device *dev = sensor->dev;
1028 	struct fwnode_handle *ep_fwnode;
1029 	struct gpio_desc *gpio;
1030 	unsigned int rate = 0;
1031 	int i, ret;
1032 
1033 	/*
1034 	 * Sometimes the fwnode graph is initialized by the bridge driver.
1035 	 * Bridge drivers doing this may also add GPIO mappings, wait for this.
1036 	 */
1037 	ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1038 	if (!ep_fwnode)
1039 		return dev_err_probe(dev, -EPROBE_DEFER,
1040 				     "waiting for fwnode graph endpoint\n");
1041 
1042 	ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &bus_cfg);
1043 	fwnode_handle_put(ep_fwnode);
1044 	if (ret)
1045 		return ret;
1046 
1047 	/*
1048 	 * The pin we want is named XSHUTDN in the datasheet. Linux sensor
1049 	 * drivers have standardized on using "powerdown" as con-id name
1050 	 * for powerdown or shutdown pins. Older DTB files use "reset",
1051 	 * so fallback to that if there is no "powerdown" pin.
1052 	 */
1053 	gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
1054 	if (!gpio)
1055 		gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1056 
1057 	ret = PTR_ERR_OR_ZERO(gpio);
1058 	if (ret < 0) {
1059 		dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
1060 		goto out_free_bus_cfg;
1061 	}
1062 
1063 	sensor->pwdn_gpio = gpio;
1064 
1065 	sensor->xvclk = devm_clk_get_optional(dev, "xvclk");
1066 	if (IS_ERR(sensor->xvclk)) {
1067 		ret = dev_err_probe(dev, PTR_ERR(sensor->xvclk),
1068 				    "xvclk clock missing or invalid\n");
1069 		goto out_free_bus_cfg;
1070 	}
1071 
1072 	/*
1073 	 * We could have either a 24MHz or 19.2MHz clock rate from either DT or
1074 	 * ACPI... but we also need to support the weird IPU3 case which will
1075 	 * have an external clock AND a clock-frequency property. Check for the
1076 	 * clock-frequency property and if found, set that rate if we managed
1077 	 * to acquire a clock. This should cover the ACPI case. If the system
1078 	 * uses devicetree then the configured rate should already be set, so
1079 	 * we can just read it.
1080 	 */
1081 	ret = fwnode_property_read_u32(dev_fwnode(dev), "clock-frequency",
1082 				       &rate);
1083 	if (ret && !sensor->xvclk) {
1084 		dev_err_probe(dev, ret, "invalid clock config\n");
1085 		goto out_free_bus_cfg;
1086 	}
1087 
1088 	if (!ret && sensor->xvclk) {
1089 		ret = clk_set_rate(sensor->xvclk, rate);
1090 		if (ret) {
1091 			dev_err_probe(dev, ret, "failed to set clock rate\n");
1092 			goto out_free_bus_cfg;
1093 		}
1094 	}
1095 
1096 	sensor->xvclk_freq = rate ?: clk_get_rate(sensor->xvclk);
1097 
1098 	for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) {
1099 		if (sensor->xvclk_freq == ov2680_xvclk_freqs[i])
1100 			break;
1101 	}
1102 
1103 	if (i == ARRAY_SIZE(ov2680_xvclk_freqs)) {
1104 		ret = dev_err_probe(dev, -EINVAL,
1105 				    "unsupported xvclk frequency %d Hz\n",
1106 				    sensor->xvclk_freq);
1107 		goto out_free_bus_cfg;
1108 	}
1109 
1110 	sensor->pll_mult = ov2680_pll_multipliers[i];
1111 
1112 	sensor->link_freq[0] = sensor->xvclk_freq / OV2680_PLL_PREDIV0 /
1113 			       OV2680_PLL_PREDIV * sensor->pll_mult;
1114 
1115 	/* CSI-2 is double data rate, bus-format is 10 bpp */
1116 	sensor->pixel_rate = sensor->link_freq[0] * 2;
1117 	do_div(sensor->pixel_rate, 10);
1118 
1119 	/* Verify bus cfg */
1120 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != 1) {
1121 		ret = dev_err_probe(dev, -EINVAL,
1122 				    "only a 1-lane CSI2 config is supported");
1123 		goto out_free_bus_cfg;
1124 	}
1125 
1126 	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1127 		if (bus_cfg.link_frequencies[i] == sensor->link_freq[0])
1128 			break;
1129 
1130 	if (bus_cfg.nr_of_link_frequencies == 0 ||
1131 	    bus_cfg.nr_of_link_frequencies == i) {
1132 		ret = dev_err_probe(dev, -EINVAL,
1133 				    "supported link freq %lld not found\n",
1134 				    sensor->link_freq[0]);
1135 		goto out_free_bus_cfg;
1136 	}
1137 
1138 out_free_bus_cfg:
1139 	v4l2_fwnode_endpoint_free(&bus_cfg);
1140 	return ret;
1141 }
1142 
1143 static int ov2680_probe(struct i2c_client *client)
1144 {
1145 	struct device *dev = &client->dev;
1146 	struct ov2680_dev *sensor;
1147 	int ret;
1148 
1149 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1150 	if (!sensor)
1151 		return -ENOMEM;
1152 
1153 	sensor->dev = &client->dev;
1154 
1155 	sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
1156 	if (IS_ERR(sensor->regmap))
1157 		return PTR_ERR(sensor->regmap);
1158 
1159 	ret = ov2680_parse_dt(sensor);
1160 	if (ret < 0)
1161 		return ret;
1162 
1163 	ret = ov2680_mode_init(sensor);
1164 	if (ret < 0)
1165 		return ret;
1166 
1167 	ret = ov2680_get_regulators(sensor);
1168 	if (ret < 0) {
1169 		dev_err(dev, "failed to get regulators\n");
1170 		return ret;
1171 	}
1172 
1173 	mutex_init(&sensor->lock);
1174 
1175 	/*
1176 	 * Power up and verify the chip now, so that if runtime pm is
1177 	 * disabled the chip is left on and streaming will work.
1178 	 */
1179 	ret = ov2680_power_on(sensor);
1180 	if (ret < 0)
1181 		goto lock_destroy;
1182 
1183 	ret = ov2680_check_id(sensor);
1184 	if (ret < 0)
1185 		goto err_powerdown;
1186 
1187 	pm_runtime_set_active(&client->dev);
1188 	pm_runtime_get_noresume(&client->dev);
1189 	pm_runtime_enable(&client->dev);
1190 
1191 	ret = ov2680_v4l2_register(sensor);
1192 	if (ret < 0)
1193 		goto err_pm_runtime;
1194 
1195 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
1196 	pm_runtime_use_autosuspend(&client->dev);
1197 	pm_runtime_put_autosuspend(&client->dev);
1198 
1199 	return 0;
1200 
1201 err_pm_runtime:
1202 	pm_runtime_disable(&client->dev);
1203 	pm_runtime_put_noidle(&client->dev);
1204 err_powerdown:
1205 	ov2680_power_off(sensor);
1206 lock_destroy:
1207 	dev_err(dev, "ov2680 init fail: %d\n", ret);
1208 	mutex_destroy(&sensor->lock);
1209 
1210 	return ret;
1211 }
1212 
1213 static void ov2680_remove(struct i2c_client *client)
1214 {
1215 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1216 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1217 
1218 	v4l2_async_unregister_subdev(&sensor->sd);
1219 	mutex_destroy(&sensor->lock);
1220 	media_entity_cleanup(&sensor->sd.entity);
1221 	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1222 
1223 	/*
1224 	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1225 	 * make sure to turn power off manually.
1226 	 */
1227 	pm_runtime_disable(&client->dev);
1228 	if (!pm_runtime_status_suspended(&client->dev))
1229 		ov2680_power_off(sensor);
1230 	pm_runtime_set_suspended(&client->dev);
1231 }
1232 
1233 static int ov2680_suspend(struct device *dev)
1234 {
1235 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1236 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1237 
1238 	if (sensor->is_streaming)
1239 		ov2680_stream_disable(sensor);
1240 
1241 	return ov2680_power_off(sensor);
1242 }
1243 
1244 static int ov2680_resume(struct device *dev)
1245 {
1246 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1247 	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1248 	int ret;
1249 
1250 	ret = ov2680_power_on(sensor);
1251 	if (ret < 0)
1252 		goto stream_disable;
1253 
1254 	if (sensor->is_streaming) {
1255 		ret = ov2680_stream_enable(sensor);
1256 		if (ret < 0)
1257 			goto stream_disable;
1258 	}
1259 
1260 	return 0;
1261 
1262 stream_disable:
1263 	ov2680_stream_disable(sensor);
1264 	sensor->is_streaming = false;
1265 
1266 	return ret;
1267 }
1268 
1269 static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
1270 				 NULL);
1271 
1272 static const struct of_device_id ov2680_dt_ids[] = {
1273 	{ .compatible = "ovti,ov2680" },
1274 	{ /* sentinel */ },
1275 };
1276 MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1277 
1278 static const struct acpi_device_id ov2680_acpi_ids[] = {
1279 	{ "OVTI2680" },
1280 	{ /* sentinel */ }
1281 };
1282 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids);
1283 
1284 static struct i2c_driver ov2680_i2c_driver = {
1285 	.driver = {
1286 		.name  = "ov2680",
1287 		.pm = pm_sleep_ptr(&ov2680_pm_ops),
1288 		.of_match_table	= ov2680_dt_ids,
1289 		.acpi_match_table = ov2680_acpi_ids,
1290 	},
1291 	.probe		= ov2680_probe,
1292 	.remove		= ov2680_remove,
1293 };
1294 module_i2c_driver(ov2680_i2c_driver);
1295 
1296 MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1297 MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1298 MODULE_LICENSE("GPL v2");
1299