1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for OmniVision OV2680 1080p HD camera sensor.
4  *
5  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17 
18 #include <asm/unaligned.h>
19 
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/string.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kmod.h>
28 #include <linux/device.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/i2c.h>
32 #include <linux/moduleparam.h>
33 #include <media/v4l2-device.h>
34 #include <linux/io.h>
35 #include <linux/acpi.h>
36 #include "../include/linux/atomisp_gmin_platform.h"
37 
38 #include "ov2680.h"
39 
40 static int h_flag;
41 static int v_flag;
42 static enum atomisp_bayer_order ov2680_bayer_order_mapping[] = {
43 	atomisp_bayer_order_bggr,
44 	atomisp_bayer_order_grbg,
45 	atomisp_bayer_order_gbrg,
46 	atomisp_bayer_order_rggb,
47 };
48 
49 /* i2c read/write stuff */
ov2680_read_reg(struct i2c_client * client,int len,u16 reg,u16 * val)50 static int ov2680_read_reg(struct i2c_client *client,
51 			   int len, u16 reg, u16 *val)
52 {
53 	struct i2c_msg msgs[2];
54 	u8 addr_buf[2] = { reg >> 8, reg & 0xff };
55 	u8 data_buf[4] = { 0, };
56 	int ret;
57 
58 	if (len > 4)
59 		return -EINVAL;
60 
61 	msgs[0].addr = client->addr;
62 	msgs[0].flags = 0;
63 	msgs[0].len = ARRAY_SIZE(addr_buf);
64 	msgs[0].buf = addr_buf;
65 
66 	msgs[1].addr = client->addr;
67 	msgs[1].flags = I2C_M_RD;
68 	msgs[1].len = len;
69 	msgs[1].buf = &data_buf[4 - len];
70 
71 	ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
72 	if (ret != ARRAY_SIZE(msgs)) {
73 		dev_err(&client->dev, "read error: reg=0x%4x: %d\n", reg, ret);
74 		return -EIO;
75 	}
76 
77 	*val = get_unaligned_be32(data_buf);
78 
79 	return 0;
80 }
81 
ov2680_write_reg(struct i2c_client * client,unsigned int len,u16 reg,u16 val)82 static int ov2680_write_reg(struct i2c_client *client, unsigned int len,
83 			    u16 reg, u16 val)
84 {
85 	u8 buf[6];
86 	int ret;
87 
88 	if (len == 2)
89 		put_unaligned_be16(val << (8 * (4 - len)), buf + 2);
90 	else if (len == 1)
91 		buf[2] = val;
92 	else
93 		return -EINVAL;
94 
95 	put_unaligned_be16(reg, buf);
96 
97 	ret = i2c_master_send(client, buf, len + 2);
98 	if (ret != len + 2) {
99 		dev_err(&client->dev, "write error %d reg 0x%04x, val 0x%02x: buf sent: %*ph\n",
100 			ret, reg, val, len + 2, &buf);
101 		return -EIO;
102 	}
103 
104 	return 0;
105 }
106 
ov2680_write_reg_array(struct i2c_client * client,const struct ov2680_reg * reglist)107 static int ov2680_write_reg_array(struct i2c_client *client,
108 				  const struct ov2680_reg *reglist)
109 {
110 	const struct ov2680_reg *next = reglist;
111 	int ret;
112 
113 	for (; next->reg != 0; next++) {
114 		ret = ov2680_write_reg(client, 1, next->reg, next->val);
115 		if (ret)
116 			return ret;
117 	}
118 
119 	return 0;
120 }
121 
ov2680_g_focal(struct v4l2_subdev * sd,s32 * val)122 static int ov2680_g_focal(struct v4l2_subdev *sd, s32 *val)
123 {
124 	*val = (OV2680_FOCAL_LENGTH_NUM << 16) | OV2680_FOCAL_LENGTH_DEM;
125 	return 0;
126 }
127 
ov2680_g_fnumber(struct v4l2_subdev * sd,s32 * val)128 static int ov2680_g_fnumber(struct v4l2_subdev *sd, s32 *val)
129 {
130 	/*const f number for ov2680*/
131 
132 	*val = (OV2680_F_NUMBER_DEFAULT_NUM << 16) | OV2680_F_NUMBER_DEM;
133 	return 0;
134 }
135 
ov2680_g_fnumber_range(struct v4l2_subdev * sd,s32 * val)136 static int ov2680_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
137 {
138 	*val = (OV2680_F_NUMBER_DEFAULT_NUM << 24) |
139 	       (OV2680_F_NUMBER_DEM << 16) |
140 	       (OV2680_F_NUMBER_DEFAULT_NUM << 8) | OV2680_F_NUMBER_DEM;
141 	return 0;
142 }
143 
ov2680_g_bin_factor_x(struct v4l2_subdev * sd,s32 * val)144 static int ov2680_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
145 {
146 	struct ov2680_device *dev = to_ov2680_sensor(sd);
147 	struct i2c_client *client = v4l2_get_subdevdata(sd);
148 
149 	dev_dbg(&client->dev,  "++++ov2680_g_bin_factor_x\n");
150 	*val = ov2680_res[dev->fmt_idx].bin_factor_x;
151 
152 	return 0;
153 }
154 
ov2680_g_bin_factor_y(struct v4l2_subdev * sd,s32 * val)155 static int ov2680_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
156 {
157 	struct ov2680_device *dev = to_ov2680_sensor(sd);
158 	struct i2c_client *client = v4l2_get_subdevdata(sd);
159 
160 	*val = ov2680_res[dev->fmt_idx].bin_factor_y;
161 	dev_dbg(&client->dev,  "++++ov2680_g_bin_factor_y\n");
162 	return 0;
163 }
164 
ov2680_get_intg_factor(struct i2c_client * client,struct camera_mipi_info * info,const struct ov2680_resolution * res)165 static int ov2680_get_intg_factor(struct i2c_client *client,
166 				  struct camera_mipi_info *info,
167 				  const struct ov2680_resolution *res)
168 {
169 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
170 	struct ov2680_device *dev = to_ov2680_sensor(sd);
171 	struct atomisp_sensor_mode_data *buf = &info->data;
172 	unsigned int pix_clk_freq_hz;
173 	u16 reg_val;
174 	int ret;
175 
176 	dev_dbg(&client->dev,  "++++ov2680_get_intg_factor\n");
177 	if (!info)
178 		return -EINVAL;
179 
180 	/* pixel clock */
181 	pix_clk_freq_hz = res->pix_clk_freq * 1000000;
182 
183 	dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
184 	buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
185 
186 	/* get integration time */
187 	buf->coarse_integration_time_min = OV2680_COARSE_INTG_TIME_MIN;
188 	buf->coarse_integration_time_max_margin =
189 	    OV2680_COARSE_INTG_TIME_MAX_MARGIN;
190 
191 	buf->fine_integration_time_min = OV2680_FINE_INTG_TIME_MIN;
192 	buf->fine_integration_time_max_margin =
193 	    OV2680_FINE_INTG_TIME_MAX_MARGIN;
194 
195 	buf->fine_integration_time_def = OV2680_FINE_INTG_TIME_MIN;
196 	buf->frame_length_lines = res->lines_per_frame;
197 	buf->line_length_pck = res->pixels_per_line;
198 	buf->read_mode = res->bin_mode;
199 
200 	/* get the cropping and output resolution to ISP for this mode. */
201 	ret =  ov2680_read_reg(client, 2,
202 			       OV2680_HORIZONTAL_START_H, &reg_val);
203 	if (ret)
204 		return ret;
205 	buf->crop_horizontal_start = reg_val;
206 
207 	ret =  ov2680_read_reg(client, 2,
208 			       OV2680_VERTICAL_START_H, &reg_val);
209 	if (ret)
210 		return ret;
211 	buf->crop_vertical_start = reg_val;
212 
213 	ret = ov2680_read_reg(client, 2,
214 			      OV2680_HORIZONTAL_END_H, &reg_val);
215 	if (ret)
216 		return ret;
217 	buf->crop_horizontal_end = reg_val;
218 
219 	ret = ov2680_read_reg(client, 2,
220 			      OV2680_VERTICAL_END_H, &reg_val);
221 	if (ret)
222 		return ret;
223 	buf->crop_vertical_end = reg_val;
224 
225 	ret = ov2680_read_reg(client, 2,
226 			      OV2680_HORIZONTAL_OUTPUT_SIZE_H, &reg_val);
227 	if (ret)
228 		return ret;
229 	buf->output_width = reg_val;
230 
231 	ret = ov2680_read_reg(client, 2,
232 			      OV2680_VERTICAL_OUTPUT_SIZE_H, &reg_val);
233 	if (ret)
234 		return ret;
235 	buf->output_height = reg_val;
236 
237 	buf->binning_factor_x = res->bin_factor_x ?
238 				(res->bin_factor_x * 2) : 1;
239 	buf->binning_factor_y = res->bin_factor_y ?
240 				(res->bin_factor_y * 2) : 1;
241 	return 0;
242 }
243 
__ov2680_set_exposure(struct v4l2_subdev * sd,int coarse_itg,int gain,int digitgain)244 static long __ov2680_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
245 				  int gain, int digitgain)
246 
247 {
248 	struct i2c_client *client = v4l2_get_subdevdata(sd);
249 	struct ov2680_device *dev = to_ov2680_sensor(sd);
250 	u16 vts;
251 	int ret, exp_val;
252 
253 	dev_dbg(&client->dev,
254 		"+++++++__ov2680_set_exposure coarse_itg %d, gain %d, digitgain %d++\n",
255 		coarse_itg, gain, digitgain);
256 
257 	vts = ov2680_res[dev->fmt_idx].lines_per_frame;
258 
259 	/* group hold */
260 	ret = ov2680_write_reg(client, 1,
261 			       OV2680_GROUP_ACCESS, 0x00);
262 	if (ret) {
263 		dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
264 			__func__, OV2680_GROUP_ACCESS);
265 		return ret;
266 	}
267 
268 	/* Increase the VTS to match exposure + MARGIN */
269 	if (coarse_itg > vts - OV2680_INTEGRATION_TIME_MARGIN)
270 		vts = (u16)coarse_itg + OV2680_INTEGRATION_TIME_MARGIN;
271 
272 	ret = ov2680_write_reg(client, 2, OV2680_TIMING_VTS_H, vts);
273 	if (ret) {
274 		dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
275 			__func__, OV2680_TIMING_VTS_H);
276 		return ret;
277 	}
278 
279 	/* set exposure */
280 
281 	/* Lower four bit should be 0*/
282 	exp_val = coarse_itg << 4;
283 	ret = ov2680_write_reg(client, 1,
284 			       OV2680_EXPOSURE_L, exp_val & 0xFF);
285 	if (ret) {
286 		dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
287 			__func__, OV2680_EXPOSURE_L);
288 		return ret;
289 	}
290 
291 	ret = ov2680_write_reg(client, 1,
292 			       OV2680_EXPOSURE_M, (exp_val >> 8) & 0xFF);
293 	if (ret) {
294 		dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
295 			__func__, OV2680_EXPOSURE_M);
296 		return ret;
297 	}
298 
299 	ret = ov2680_write_reg(client, 1,
300 			       OV2680_EXPOSURE_H, (exp_val >> 16) & 0x0F);
301 	if (ret) {
302 		dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
303 			__func__, OV2680_EXPOSURE_H);
304 		return ret;
305 	}
306 
307 	/* Analog gain */
308 	ret = ov2680_write_reg(client, 2, OV2680_AGC_H, gain);
309 	if (ret) {
310 		dev_err(&client->dev, "%s: write 0x%02x: error, aborted\n",
311 			__func__, OV2680_AGC_H);
312 		return ret;
313 	}
314 	/* Digital gain */
315 	if (digitgain) {
316 		ret = ov2680_write_reg(client, 2,
317 				       OV2680_MWB_RED_GAIN_H, digitgain);
318 		if (ret) {
319 			dev_err(&client->dev,
320 				"%s: write 0x%02x: error, aborted\n",
321 				__func__, OV2680_MWB_RED_GAIN_H);
322 			return ret;
323 		}
324 
325 		ret = ov2680_write_reg(client, 2,
326 				       OV2680_MWB_GREEN_GAIN_H, digitgain);
327 		if (ret) {
328 			dev_err(&client->dev,
329 				"%s: write 0x%02x: error, aborted\n",
330 				__func__, OV2680_MWB_RED_GAIN_H);
331 			return ret;
332 		}
333 
334 		ret = ov2680_write_reg(client, 2,
335 				       OV2680_MWB_BLUE_GAIN_H, digitgain);
336 		if (ret) {
337 			dev_err(&client->dev,
338 				"%s: write 0x%02x: error, aborted\n",
339 				__func__, OV2680_MWB_RED_GAIN_H);
340 			return ret;
341 		}
342 	}
343 
344 	/* End group */
345 	ret = ov2680_write_reg(client, 1,
346 			       OV2680_GROUP_ACCESS, 0x10);
347 	if (ret)
348 		return ret;
349 
350 	/* Delay launch group */
351 	ret = ov2680_write_reg(client, 1,
352 			       OV2680_GROUP_ACCESS, 0xa0);
353 	if (ret)
354 		return ret;
355 	return ret;
356 }
357 
ov2680_set_exposure(struct v4l2_subdev * sd,int exposure,int gain,int digitgain)358 static int ov2680_set_exposure(struct v4l2_subdev *sd, int exposure,
359 			       int gain, int digitgain)
360 {
361 	struct ov2680_device *dev = to_ov2680_sensor(sd);
362 	int ret;
363 
364 	mutex_lock(&dev->input_lock);
365 	ret = __ov2680_set_exposure(sd, exposure, gain, digitgain);
366 	mutex_unlock(&dev->input_lock);
367 
368 	return ret;
369 }
370 
ov2680_s_exposure(struct v4l2_subdev * sd,struct atomisp_exposure * exposure)371 static long ov2680_s_exposure(struct v4l2_subdev *sd,
372 			      struct atomisp_exposure *exposure)
373 {
374 	u16 coarse_itg = exposure->integration_time[0];
375 	u16 analog_gain = exposure->gain[0];
376 	u16 digital_gain = exposure->gain[1];
377 
378 	/* we should not accept the invalid value below */
379 	if (analog_gain == 0) {
380 		struct i2c_client *client = v4l2_get_subdevdata(sd);
381 
382 		v4l2_err(client, "%s: invalid value\n", __func__);
383 		return -EINVAL;
384 	}
385 
386 	// EXPOSURE CONTROL DISABLED FOR INITIAL CHECKIN, TUNING DOESN'T WORK
387 	return ov2680_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
388 }
389 
ov2680_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)390 static long ov2680_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
391 {
392 	switch (cmd) {
393 	case ATOMISP_IOC_S_EXPOSURE:
394 		return ov2680_s_exposure(sd, arg);
395 
396 	default:
397 		return -EINVAL;
398 	}
399 	return 0;
400 }
401 
402 /* This returns the exposure time being used. This should only be used
403  * for filling in EXIF data, not for actual image processing.
404  */
ov2680_q_exposure(struct v4l2_subdev * sd,s32 * value)405 static int ov2680_q_exposure(struct v4l2_subdev *sd, s32 *value)
406 {
407 	struct i2c_client *client = v4l2_get_subdevdata(sd);
408 	u16 reg_v, reg_v2;
409 	int ret;
410 
411 	/* get exposure */
412 	ret = ov2680_read_reg(client, 1,
413 			      OV2680_EXPOSURE_L,
414 			      &reg_v);
415 	if (ret)
416 		goto err;
417 
418 	ret = ov2680_read_reg(client, 1,
419 			      OV2680_EXPOSURE_M,
420 			      &reg_v2);
421 	if (ret)
422 		goto err;
423 
424 	reg_v += reg_v2 << 8;
425 	ret = ov2680_read_reg(client, 1,
426 			      OV2680_EXPOSURE_H,
427 			      &reg_v2);
428 	if (ret)
429 		goto err;
430 
431 	*value = reg_v + (((u32)reg_v2 << 16));
432 err:
433 	return ret;
434 }
435 
ov2680_translate_bayer_order(enum atomisp_bayer_order code)436 static u32 ov2680_translate_bayer_order(enum atomisp_bayer_order code)
437 {
438 	switch (code) {
439 	case atomisp_bayer_order_rggb:
440 		return MEDIA_BUS_FMT_SRGGB10_1X10;
441 	case atomisp_bayer_order_grbg:
442 		return MEDIA_BUS_FMT_SGRBG10_1X10;
443 	case atomisp_bayer_order_bggr:
444 		return MEDIA_BUS_FMT_SBGGR10_1X10;
445 	case atomisp_bayer_order_gbrg:
446 		return MEDIA_BUS_FMT_SGBRG10_1X10;
447 	}
448 	return 0;
449 }
450 
ov2680_v_flip(struct v4l2_subdev * sd,s32 value)451 static int ov2680_v_flip(struct v4l2_subdev *sd, s32 value)
452 {
453 	struct ov2680_device *dev = to_ov2680_sensor(sd);
454 	struct camera_mipi_info *ov2680_info = NULL;
455 	struct i2c_client *client = v4l2_get_subdevdata(sd);
456 	int ret;
457 	u16 val;
458 	u8 index;
459 
460 	dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
461 	ret = ov2680_read_reg(client, 1, OV2680_FLIP_REG, &val);
462 	if (ret)
463 		return ret;
464 	if (value) {
465 		val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
466 	} else {
467 		val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
468 	}
469 	ret = ov2680_write_reg(client, 1,
470 			       OV2680_FLIP_REG, val);
471 	if (ret)
472 		return ret;
473 	index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
474 		0);
475 	ov2680_info = v4l2_get_subdev_hostdata(sd);
476 	if (ov2680_info) {
477 		ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
478 		dev->format.code = ov2680_translate_bayer_order(
479 				       ov2680_info->raw_bayer_order);
480 	}
481 	return ret;
482 }
483 
ov2680_h_flip(struct v4l2_subdev * sd,s32 value)484 static int ov2680_h_flip(struct v4l2_subdev *sd, s32 value)
485 {
486 	struct ov2680_device *dev = to_ov2680_sensor(sd);
487 	struct camera_mipi_info *ov2680_info = NULL;
488 	struct i2c_client *client = v4l2_get_subdevdata(sd);
489 	int ret;
490 	u16 val;
491 	u8 index;
492 
493 	dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
494 
495 	ret = ov2680_read_reg(client, 1, OV2680_MIRROR_REG, &val);
496 	if (ret)
497 		return ret;
498 	if (value)
499 		val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
500 	else
501 		val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
502 
503 	ret = ov2680_write_reg(client, 1,
504 			       OV2680_MIRROR_REG, val);
505 	if (ret)
506 		return ret;
507 	index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
508 		0);
509 	ov2680_info = v4l2_get_subdev_hostdata(sd);
510 	if (ov2680_info) {
511 		ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
512 		dev->format.code = ov2680_translate_bayer_order(
513 				       ov2680_info->raw_bayer_order);
514 	}
515 	return ret;
516 }
517 
ov2680_s_ctrl(struct v4l2_ctrl * ctrl)518 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
519 {
520 	struct ov2680_device *dev =
521 	    container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
522 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
523 	int ret = 0;
524 
525 	switch (ctrl->id) {
526 	case V4L2_CID_VFLIP:
527 		dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
528 			__func__, ctrl->val);
529 		ret = ov2680_v_flip(&dev->sd, ctrl->val);
530 		break;
531 	case V4L2_CID_HFLIP:
532 		dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
533 			__func__, ctrl->val);
534 		ret = ov2680_h_flip(&dev->sd, ctrl->val);
535 		break;
536 	default:
537 		ret = -EINVAL;
538 	}
539 	return ret;
540 }
541 
ov2680_g_volatile_ctrl(struct v4l2_ctrl * ctrl)542 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
543 {
544 	struct ov2680_device *dev =
545 	    container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
546 	int ret = 0;
547 
548 	switch (ctrl->id) {
549 	case V4L2_CID_EXPOSURE_ABSOLUTE:
550 		ret = ov2680_q_exposure(&dev->sd, &ctrl->val);
551 		break;
552 	case V4L2_CID_FOCAL_ABSOLUTE:
553 		ret = ov2680_g_focal(&dev->sd, &ctrl->val);
554 		break;
555 	case V4L2_CID_FNUMBER_ABSOLUTE:
556 		ret = ov2680_g_fnumber(&dev->sd, &ctrl->val);
557 		break;
558 	case V4L2_CID_FNUMBER_RANGE:
559 		ret = ov2680_g_fnumber_range(&dev->sd, &ctrl->val);
560 		break;
561 	case V4L2_CID_BIN_FACTOR_HORZ:
562 		ret = ov2680_g_bin_factor_x(&dev->sd, &ctrl->val);
563 		break;
564 	case V4L2_CID_BIN_FACTOR_VERT:
565 		ret = ov2680_g_bin_factor_y(&dev->sd, &ctrl->val);
566 		break;
567 	default:
568 		ret = -EINVAL;
569 	}
570 
571 	return ret;
572 }
573 
574 static const struct v4l2_ctrl_ops ctrl_ops = {
575 	.s_ctrl = ov2680_s_ctrl,
576 	.g_volatile_ctrl = ov2680_g_volatile_ctrl
577 };
578 
579 static const struct v4l2_ctrl_config ov2680_controls[] = {
580 	{
581 		.ops = &ctrl_ops,
582 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
583 		.type = V4L2_CTRL_TYPE_INTEGER,
584 		.name = "exposure",
585 		.min = 0x0,
586 		.max = 0xffff,
587 		.step = 0x01,
588 		.def = 0x00,
589 		.flags = 0,
590 	},
591 	{
592 		.ops = &ctrl_ops,
593 		.id = V4L2_CID_FOCAL_ABSOLUTE,
594 		.type = V4L2_CTRL_TYPE_INTEGER,
595 		.name = "focal length",
596 		.min = OV2680_FOCAL_LENGTH_DEFAULT,
597 		.max = OV2680_FOCAL_LENGTH_DEFAULT,
598 		.step = 0x01,
599 		.def = OV2680_FOCAL_LENGTH_DEFAULT,
600 		.flags = 0,
601 	},
602 	{
603 		.ops = &ctrl_ops,
604 		.id = V4L2_CID_FNUMBER_ABSOLUTE,
605 		.type = V4L2_CTRL_TYPE_INTEGER,
606 		.name = "f-number",
607 		.min = OV2680_F_NUMBER_DEFAULT,
608 		.max = OV2680_F_NUMBER_DEFAULT,
609 		.step = 0x01,
610 		.def = OV2680_F_NUMBER_DEFAULT,
611 		.flags = 0,
612 	},
613 	{
614 		.ops = &ctrl_ops,
615 		.id = V4L2_CID_FNUMBER_RANGE,
616 		.type = V4L2_CTRL_TYPE_INTEGER,
617 		.name = "f-number range",
618 		.min = OV2680_F_NUMBER_RANGE,
619 		.max = OV2680_F_NUMBER_RANGE,
620 		.step = 0x01,
621 		.def = OV2680_F_NUMBER_RANGE,
622 		.flags = 0,
623 	},
624 	{
625 		.ops = &ctrl_ops,
626 		.id = V4L2_CID_BIN_FACTOR_HORZ,
627 		.type = V4L2_CTRL_TYPE_INTEGER,
628 		.name = "horizontal binning factor",
629 		.min = 0,
630 		.max = OV2680_BIN_FACTOR_MAX,
631 		.step = 1,
632 		.def = 0,
633 		.flags = 0,
634 	},
635 	{
636 		.ops = &ctrl_ops,
637 		.id = V4L2_CID_BIN_FACTOR_VERT,
638 		.type = V4L2_CTRL_TYPE_INTEGER,
639 		.name = "vertical binning factor",
640 		.min = 0,
641 		.max = OV2680_BIN_FACTOR_MAX,
642 		.step = 1,
643 		.def = 0,
644 		.flags = 0,
645 	},
646 	{
647 		.ops = &ctrl_ops,
648 		.id = V4L2_CID_VFLIP,
649 		.type = V4L2_CTRL_TYPE_BOOLEAN,
650 		.name = "Flip",
651 		.min = 0,
652 		.max = 1,
653 		.step = 1,
654 		.def = 0,
655 	},
656 	{
657 		.ops = &ctrl_ops,
658 		.id = V4L2_CID_HFLIP,
659 		.type = V4L2_CTRL_TYPE_BOOLEAN,
660 		.name = "Mirror",
661 		.min = 0,
662 		.max = 1,
663 		.step = 1,
664 		.def = 0,
665 	},
666 };
667 
ov2680_init_registers(struct v4l2_subdev * sd)668 static int ov2680_init_registers(struct v4l2_subdev *sd)
669 {
670 	struct i2c_client *client = v4l2_get_subdevdata(sd);
671 	int ret;
672 
673 	ret = ov2680_write_reg(client, 1, OV2680_SW_RESET, 0x01);
674 	ret |= ov2680_write_reg_array(client, ov2680_global_setting);
675 
676 	return ret;
677 }
678 
ov2680_init(struct v4l2_subdev * sd)679 static int ov2680_init(struct v4l2_subdev *sd)
680 {
681 	struct ov2680_device *dev = to_ov2680_sensor(sd);
682 
683 	int ret;
684 
685 	mutex_lock(&dev->input_lock);
686 
687 	/* restore settings */
688 	ov2680_res = ov2680_res_preview;
689 	N_RES = N_RES_PREVIEW;
690 
691 	ret = ov2680_init_registers(sd);
692 
693 	mutex_unlock(&dev->input_lock);
694 
695 	return ret;
696 }
697 
power_ctrl(struct v4l2_subdev * sd,bool flag)698 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
699 {
700 	int ret = 0;
701 	struct ov2680_device *dev = to_ov2680_sensor(sd);
702 	struct i2c_client *client = v4l2_get_subdevdata(sd);
703 
704 	if (!dev || !dev->platform_data)
705 		return -ENODEV;
706 
707 	dev_dbg(&client->dev, "%s: %s", __func__, flag ? "on" : "off");
708 
709 	if (flag) {
710 		ret |= dev->platform_data->v1p8_ctrl(sd, 1);
711 		ret |= dev->platform_data->v2p8_ctrl(sd, 1);
712 		usleep_range(10000, 15000);
713 	}
714 
715 	if (!flag || ret) {
716 		ret |= dev->platform_data->v1p8_ctrl(sd, 0);
717 		ret |= dev->platform_data->v2p8_ctrl(sd, 0);
718 	}
719 	return ret;
720 }
721 
gpio_ctrl(struct v4l2_subdev * sd,bool flag)722 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
723 {
724 	int ret;
725 	struct ov2680_device *dev = to_ov2680_sensor(sd);
726 
727 	if (!dev || !dev->platform_data)
728 		return -ENODEV;
729 
730 	/* The OV2680 documents only one GPIO input (#XSHUTDN), but
731 	 * existing integrations often wire two (reset/power_down)
732 	 * because that is the way other sensors work.  There is no
733 	 * way to tell how it is wired internally, so existing
734 	 * firmwares expose both and we drive them symmetrically. */
735 	if (flag) {
736 		ret = dev->platform_data->gpio0_ctrl(sd, 1);
737 		usleep_range(10000, 15000);
738 		/* Ignore return from second gpio, it may not be there */
739 		dev->platform_data->gpio1_ctrl(sd, 1);
740 		usleep_range(10000, 15000);
741 	} else {
742 		dev->platform_data->gpio1_ctrl(sd, 0);
743 		ret = dev->platform_data->gpio0_ctrl(sd, 0);
744 	}
745 	return ret;
746 }
747 
power_up(struct v4l2_subdev * sd)748 static int power_up(struct v4l2_subdev *sd)
749 {
750 	struct ov2680_device *dev = to_ov2680_sensor(sd);
751 	struct i2c_client *client = v4l2_get_subdevdata(sd);
752 	int ret;
753 
754 	if (!dev->platform_data) {
755 		dev_err(&client->dev,
756 			"no camera_sensor_platform_data");
757 		return -ENODEV;
758 	}
759 
760 	/* power control */
761 	ret = power_ctrl(sd, 1);
762 	if (ret)
763 		goto fail_power;
764 
765 	/* according to DS, at least 5ms is needed between DOVDD and PWDN */
766 	usleep_range(5000, 6000);
767 
768 	/* gpio ctrl */
769 	ret = gpio_ctrl(sd, 1);
770 	if (ret) {
771 		ret = gpio_ctrl(sd, 1);
772 		if (ret)
773 			goto fail_power;
774 	}
775 
776 	/* flis clock control */
777 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
778 	if (ret)
779 		goto fail_clk;
780 
781 	/* according to DS, 20ms is needed between PWDN and i2c access */
782 	msleep(20);
783 
784 	return 0;
785 
786 fail_clk:
787 	gpio_ctrl(sd, 0);
788 fail_power:
789 	power_ctrl(sd, 0);
790 	dev_err(&client->dev, "sensor power-up failed\n");
791 
792 	return ret;
793 }
794 
power_down(struct v4l2_subdev * sd)795 static int power_down(struct v4l2_subdev *sd)
796 {
797 	struct ov2680_device *dev = to_ov2680_sensor(sd);
798 	struct i2c_client *client = v4l2_get_subdevdata(sd);
799 	int ret = 0;
800 
801 	h_flag = 0;
802 	v_flag = 0;
803 	if (!dev->platform_data) {
804 		dev_err(&client->dev,
805 			"no camera_sensor_platform_data");
806 		return -ENODEV;
807 	}
808 
809 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
810 	if (ret)
811 		dev_err(&client->dev, "flisclk failed\n");
812 
813 	/* gpio ctrl */
814 	ret = gpio_ctrl(sd, 0);
815 	if (ret) {
816 		ret = gpio_ctrl(sd, 0);
817 		if (ret)
818 			dev_err(&client->dev, "gpio failed 2\n");
819 	}
820 
821 	/* power control */
822 	ret = power_ctrl(sd, 0);
823 	if (ret)
824 		dev_err(&client->dev, "vprog failed.\n");
825 
826 	return ret;
827 }
828 
ov2680_s_power(struct v4l2_subdev * sd,int on)829 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
830 {
831 	int ret;
832 
833 	if (on == 0) {
834 		ret = power_down(sd);
835 	} else {
836 		ret = power_up(sd);
837 		if (!ret)
838 			return ov2680_init(sd);
839 	}
840 	return ret;
841 }
842 
843 /*
844  * distance - calculate the distance
845  * @res: resolution
846  * @w: width
847  * @h: height
848  *
849  * Get the gap between resolution and w/h.
850  * res->width/height smaller than w/h wouldn't be considered.
851  * Returns the value of gap or -1 if fail.
852  */
853 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
distance(struct ov2680_resolution * res,u32 w,u32 h)854 static int distance(struct ov2680_resolution *res, u32 w, u32 h)
855 {
856 	unsigned int w_ratio = (res->width << 13) / w;
857 	unsigned int h_ratio;
858 	int match;
859 
860 	if (h == 0)
861 		return -1;
862 	h_ratio = (res->height << 13) / h;
863 	if (h_ratio == 0)
864 		return -1;
865 	match   = abs(((w_ratio << 13) / h_ratio) - 8192);
866 
867 	if ((w_ratio < 8192) || (h_ratio < 8192)  ||
868 	    (match > LARGEST_ALLOWED_RATIO_MISMATCH))
869 		return -1;
870 
871 	return w_ratio + h_ratio;
872 }
873 
874 /* Return the nearest higher resolution index */
nearest_resolution_index(int w,int h)875 static int nearest_resolution_index(int w, int h)
876 {
877 	int i;
878 	int idx = -1;
879 	int dist;
880 	int min_dist = INT_MAX;
881 	struct ov2680_resolution *tmp_res = NULL;
882 
883 	for (i = 0; i < N_RES; i++) {
884 		tmp_res = &ov2680_res[i];
885 		dist = distance(tmp_res, w, h);
886 		if (dist == -1)
887 			continue;
888 		if (dist < min_dist) {
889 			min_dist = dist;
890 			idx = i;
891 		}
892 	}
893 
894 	return idx;
895 }
896 
get_resolution_index(int w,int h)897 static int get_resolution_index(int w, int h)
898 {
899 	int i;
900 
901 	for (i = 0; i < N_RES; i++) {
902 		if (w != ov2680_res[i].width)
903 			continue;
904 		if (h != ov2680_res[i].height)
905 			continue;
906 
907 		return i;
908 	}
909 
910 	return -1;
911 }
912 
ov2680_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)913 static int ov2680_set_fmt(struct v4l2_subdev *sd,
914 			  struct v4l2_subdev_pad_config *cfg,
915 			  struct v4l2_subdev_format *format)
916 {
917 	struct v4l2_mbus_framefmt *fmt = &format->format;
918 	struct ov2680_device *dev = to_ov2680_sensor(sd);
919 	struct i2c_client *client = v4l2_get_subdevdata(sd);
920 	struct camera_mipi_info *ov2680_info = NULL;
921 	int ret = 0;
922 	int idx = 0;
923 
924 	dev_dbg(&client->dev, "%s: %s: pad: %d, fmt: %p\n",
925 		__func__,
926 		(format->which == V4L2_SUBDEV_FORMAT_TRY) ? "try" : "set",
927 		format->pad, fmt);
928 
929 	if (format->pad)
930 		return -EINVAL;
931 
932 	if (!fmt)
933 		return -EINVAL;
934 
935 	ov2680_info = v4l2_get_subdev_hostdata(sd);
936 	if (!ov2680_info)
937 		return -EINVAL;
938 
939 	mutex_lock(&dev->input_lock);
940 	idx = nearest_resolution_index(fmt->width, fmt->height);
941 	if (idx == -1) {
942 		/* return the largest resolution */
943 		fmt->width = ov2680_res[N_RES - 1].width;
944 		fmt->height = ov2680_res[N_RES - 1].height;
945 	} else {
946 		fmt->width = ov2680_res[idx].width;
947 		fmt->height = ov2680_res[idx].height;
948 	}
949 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
950 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
951 		cfg->try_fmt = *fmt;
952 		mutex_unlock(&dev->input_lock);
953 		return 0;
954 	}
955 	dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
956 	dev_dbg(&client->dev, "%s: Resolution index: %d\n",
957 		__func__, dev->fmt_idx);
958 	if (dev->fmt_idx == -1) {
959 		dev_err(&client->dev, "get resolution fail\n");
960 		mutex_unlock(&dev->input_lock);
961 		return -EINVAL;
962 	}
963 	dev_dbg(&client->dev, "%s: i=%d, w=%d, h=%d\n",
964 		__func__, dev->fmt_idx, fmt->width, fmt->height);
965 
966 	// IS IT NEEDED?
967 	power_up(sd);
968 	ret = ov2680_write_reg_array(client, ov2680_res[dev->fmt_idx].regs);
969 	if (ret)
970 		dev_err(&client->dev,
971 			"ov2680 write resolution register err: %d\n", ret);
972 
973 	ret = ov2680_get_intg_factor(client, ov2680_info,
974 				     &ov2680_res[dev->fmt_idx]);
975 	if (ret) {
976 		dev_err(&client->dev, "failed to get integration factor\n");
977 		goto err;
978 	}
979 
980 	/*recall flip functions to avoid flip registers
981 	 * were overridden by default setting
982 	 */
983 	if (h_flag)
984 		ov2680_h_flip(sd, h_flag);
985 	if (v_flag)
986 		ov2680_v_flip(sd, v_flag);
987 
988 	v4l2_info(client, "\n%s idx %d\n", __func__, dev->fmt_idx);
989 
990 	/*ret = startup(sd);
991 	 * if (ret)
992 	 * dev_err(&client->dev, "ov2680 startup err\n");
993 	 */
994 err:
995 	mutex_unlock(&dev->input_lock);
996 	return ret;
997 }
998 
ov2680_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * format)999 static int ov2680_get_fmt(struct v4l2_subdev *sd,
1000 			  struct v4l2_subdev_pad_config *cfg,
1001 			  struct v4l2_subdev_format *format)
1002 {
1003 	struct v4l2_mbus_framefmt *fmt = &format->format;
1004 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1005 
1006 	if (format->pad)
1007 		return -EINVAL;
1008 
1009 	if (!fmt)
1010 		return -EINVAL;
1011 
1012 	fmt->width = ov2680_res[dev->fmt_idx].width;
1013 	fmt->height = ov2680_res[dev->fmt_idx].height;
1014 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1015 
1016 	return 0;
1017 }
1018 
ov2680_detect(struct i2c_client * client)1019 static int ov2680_detect(struct i2c_client *client)
1020 {
1021 	struct i2c_adapter *adapter = client->adapter;
1022 	u16 high, low;
1023 	int ret;
1024 	u16 id;
1025 	u8 revision;
1026 
1027 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1028 		return -ENODEV;
1029 
1030 	ret = ov2680_read_reg(client, 1,
1031 			      OV2680_SC_CMMN_CHIP_ID_H, &high);
1032 	if (ret) {
1033 		dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1034 		return -ENODEV;
1035 	}
1036 	ret = ov2680_read_reg(client, 1,
1037 			      OV2680_SC_CMMN_CHIP_ID_L, &low);
1038 	id = ((((u16)high) << 8) | (u16)low);
1039 
1040 	if (id != OV2680_ID) {
1041 		dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1042 		return -ENODEV;
1043 	}
1044 
1045 	ret = ov2680_read_reg(client, 1,
1046 			      OV2680_SC_CMMN_SUB_ID, &high);
1047 	revision = (u8)high & 0x0f;
1048 
1049 	dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
1050 		 id, revision);
1051 
1052 	return 0;
1053 }
1054 
ov2680_s_stream(struct v4l2_subdev * sd,int enable)1055 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
1056 {
1057 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1058 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1059 	int ret;
1060 
1061 	mutex_lock(&dev->input_lock);
1062 	if (enable)
1063 		dev_dbg(&client->dev, "ov2680_s_stream one\n");
1064 	else
1065 		dev_dbg(&client->dev, "ov2680_s_stream off\n");
1066 
1067 	ret = ov2680_write_reg(client, 1, OV2680_SW_STREAM,
1068 			       enable ? OV2680_START_STREAMING :
1069 			       OV2680_STOP_STREAMING);
1070 #if 0
1071 	/* restore settings */
1072 	ov2680_res = ov2680_res_preview;
1073 	N_RES = N_RES_PREVIEW;
1074 #endif
1075 
1076 	//otp valid at stream on state
1077 	//if(!dev->otp_data)
1078 	//	dev->otp_data = ov2680_otp_read(sd);
1079 
1080 	mutex_unlock(&dev->input_lock);
1081 
1082 	return ret;
1083 }
1084 
ov2680_s_config(struct v4l2_subdev * sd,int irq,void * platform_data)1085 static int ov2680_s_config(struct v4l2_subdev *sd,
1086 			   int irq, void *platform_data)
1087 {
1088 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1089 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1090 	int ret = 0;
1091 
1092 	if (!platform_data)
1093 		return -ENODEV;
1094 
1095 	dev->platform_data =
1096 	    (struct camera_sensor_platform_data *)platform_data;
1097 
1098 	mutex_lock(&dev->input_lock);
1099 	/* power off the module, then power on it in future
1100 	 * as first power on by board may not fulfill the
1101 	 * power on sequqence needed by the module
1102 	 */
1103 	ret = power_down(sd);
1104 	if (ret) {
1105 		dev_err(&client->dev, "ov2680 power-off err.\n");
1106 		goto fail_power_off;
1107 	}
1108 
1109 	ret = power_up(sd);
1110 	if (ret) {
1111 		dev_err(&client->dev, "ov2680 power-up err.\n");
1112 		goto fail_power_on;
1113 	}
1114 
1115 	ret = dev->platform_data->csi_cfg(sd, 1);
1116 	if (ret)
1117 		goto fail_csi_cfg;
1118 
1119 	/* config & detect sensor */
1120 	ret = ov2680_detect(client);
1121 	if (ret) {
1122 		dev_err(&client->dev, "ov2680_detect err s_config.\n");
1123 		goto fail_csi_cfg;
1124 	}
1125 
1126 	/* turn off sensor, after probed */
1127 	ret = power_down(sd);
1128 	if (ret) {
1129 		dev_err(&client->dev, "ov2680 power-off err.\n");
1130 		goto fail_csi_cfg;
1131 	}
1132 	mutex_unlock(&dev->input_lock);
1133 
1134 	return 0;
1135 
1136 fail_csi_cfg:
1137 	dev->platform_data->csi_cfg(sd, 0);
1138 fail_power_on:
1139 	power_down(sd);
1140 	dev_err(&client->dev, "sensor power-gating failed\n");
1141 fail_power_off:
1142 	mutex_unlock(&dev->input_lock);
1143 	return ret;
1144 }
1145 
ov2680_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * interval)1146 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
1147 				   struct v4l2_subdev_frame_interval *interval)
1148 {
1149 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1150 
1151 	interval->interval.numerator = 1;
1152 	interval->interval.denominator = ov2680_res[dev->fmt_idx].fps;
1153 
1154 	return 0;
1155 }
1156 
ov2680_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1157 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
1158 				 struct v4l2_subdev_pad_config *cfg,
1159 				 struct v4l2_subdev_mbus_code_enum *code)
1160 {
1161 	if (code->index >= MAX_FMTS)
1162 		return -EINVAL;
1163 
1164 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1165 	return 0;
1166 }
1167 
ov2680_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1168 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
1169 				  struct v4l2_subdev_pad_config *cfg,
1170 				  struct v4l2_subdev_frame_size_enum *fse)
1171 {
1172 	int index = fse->index;
1173 
1174 	if (index >= N_RES)
1175 		return -EINVAL;
1176 
1177 	fse->min_width = ov2680_res[index].width;
1178 	fse->min_height = ov2680_res[index].height;
1179 	fse->max_width = ov2680_res[index].width;
1180 	fse->max_height = ov2680_res[index].height;
1181 
1182 	return 0;
1183 }
1184 
ov2680_g_skip_frames(struct v4l2_subdev * sd,u32 * frames)1185 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1186 {
1187 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1188 
1189 	mutex_lock(&dev->input_lock);
1190 	*frames = ov2680_res[dev->fmt_idx].skip_frames;
1191 	mutex_unlock(&dev->input_lock);
1192 
1193 	return 0;
1194 }
1195 
1196 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
1197 	.s_stream = ov2680_s_stream,
1198 	.g_frame_interval = ov2680_g_frame_interval,
1199 };
1200 
1201 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
1202 	.g_skip_frames	= ov2680_g_skip_frames,
1203 };
1204 
1205 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
1206 	.s_power = ov2680_s_power,
1207 	.ioctl = ov2680_ioctl,
1208 };
1209 
1210 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
1211 	.enum_mbus_code = ov2680_enum_mbus_code,
1212 	.enum_frame_size = ov2680_enum_frame_size,
1213 	.get_fmt = ov2680_get_fmt,
1214 	.set_fmt = ov2680_set_fmt,
1215 };
1216 
1217 static const struct v4l2_subdev_ops ov2680_ops = {
1218 	.core = &ov2680_core_ops,
1219 	.video = &ov2680_video_ops,
1220 	.pad = &ov2680_pad_ops,
1221 	.sensor = &ov2680_sensor_ops,
1222 };
1223 
ov2680_remove(struct i2c_client * client)1224 static int ov2680_remove(struct i2c_client *client)
1225 {
1226 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1227 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1228 
1229 	dev_dbg(&client->dev, "ov2680_remove...\n");
1230 
1231 	dev->platform_data->csi_cfg(sd, 0);
1232 
1233 	v4l2_device_unregister_subdev(sd);
1234 	media_entity_cleanup(&dev->sd.entity);
1235 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1236 	kfree(dev);
1237 
1238 	return 0;
1239 }
1240 
ov2680_probe(struct i2c_client * client)1241 static int ov2680_probe(struct i2c_client *client)
1242 {
1243 	struct ov2680_device *dev;
1244 	int ret;
1245 	void *pdata;
1246 	unsigned int i;
1247 
1248 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1249 	if (!dev)
1250 		return -ENOMEM;
1251 
1252 	mutex_init(&dev->input_lock);
1253 
1254 	dev->fmt_idx = 0;
1255 	v4l2_i2c_subdev_init(&dev->sd, client, &ov2680_ops);
1256 
1257 	pdata = gmin_camera_platform_data(&dev->sd,
1258 					  ATOMISP_INPUT_FORMAT_RAW_10,
1259 					  atomisp_bayer_order_bggr);
1260 	if (!pdata) {
1261 		ret = -EINVAL;
1262 		goto out_free;
1263 	}
1264 
1265 	ret = ov2680_s_config(&dev->sd, client->irq, pdata);
1266 	if (ret)
1267 		goto out_free;
1268 
1269 	ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1270 	if (ret)
1271 		goto out_free;
1272 
1273 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1274 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1275 	dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1276 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1277 	ret =
1278 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1279 				   ARRAY_SIZE(ov2680_controls));
1280 	if (ret) {
1281 		ov2680_remove(client);
1282 		return ret;
1283 	}
1284 
1285 	for (i = 0; i < ARRAY_SIZE(ov2680_controls); i++)
1286 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2680_controls[i],
1287 				     NULL);
1288 
1289 	if (dev->ctrl_handler.error) {
1290 		ov2680_remove(client);
1291 		return dev->ctrl_handler.error;
1292 	}
1293 
1294 	/* Use same lock for controls as for everything else. */
1295 	dev->ctrl_handler.lock = &dev->input_lock;
1296 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1297 
1298 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1299 	if (ret) {
1300 		ov2680_remove(client);
1301 		dev_dbg(&client->dev, "+++ remove ov2680\n");
1302 	}
1303 	return ret;
1304 out_free:
1305 	dev_dbg(&client->dev, "+++ out free\n");
1306 	v4l2_device_unregister_subdev(&dev->sd);
1307 	kfree(dev);
1308 	return ret;
1309 }
1310 
1311 static const struct acpi_device_id ov2680_acpi_match[] = {
1312 	{"XXOV2680"},
1313 	{"OVTI2680"},
1314 	{},
1315 };
1316 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
1317 
1318 static struct i2c_driver ov2680_driver = {
1319 	.driver = {
1320 		.name = "ov2680",
1321 		.acpi_match_table = ov2680_acpi_match,
1322 	},
1323 	.probe_new = ov2680_probe,
1324 	.remove = ov2680_remove,
1325 };
1326 module_i2c_driver(ov2680_driver);
1327 
1328 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1329 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1330 MODULE_LICENSE("GPL");
1331