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