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 /*
403  * This returns the exposure time being used. This should only be used
404  * for filling in EXIF data, not for actual image processing.
405  */
ov2680_q_exposure(struct v4l2_subdev * sd,s32 * value)406 static int ov2680_q_exposure(struct v4l2_subdev *sd, s32 *value)
407 {
408 	struct i2c_client *client = v4l2_get_subdevdata(sd);
409 	u16 reg_v, reg_v2;
410 	int ret;
411 
412 	/* get exposure */
413 	ret = ov2680_read_reg(client, 1,
414 			      OV2680_EXPOSURE_L,
415 			      &reg_v);
416 	if (ret)
417 		goto err;
418 
419 	ret = ov2680_read_reg(client, 1,
420 			      OV2680_EXPOSURE_M,
421 			      &reg_v2);
422 	if (ret)
423 		goto err;
424 
425 	reg_v += reg_v2 << 8;
426 	ret = ov2680_read_reg(client, 1,
427 			      OV2680_EXPOSURE_H,
428 			      &reg_v2);
429 	if (ret)
430 		goto err;
431 
432 	*value = reg_v + (((u32)reg_v2 << 16));
433 err:
434 	return ret;
435 }
436 
ov2680_translate_bayer_order(enum atomisp_bayer_order code)437 static u32 ov2680_translate_bayer_order(enum atomisp_bayer_order code)
438 {
439 	switch (code) {
440 	case atomisp_bayer_order_rggb:
441 		return MEDIA_BUS_FMT_SRGGB10_1X10;
442 	case atomisp_bayer_order_grbg:
443 		return MEDIA_BUS_FMT_SGRBG10_1X10;
444 	case atomisp_bayer_order_bggr:
445 		return MEDIA_BUS_FMT_SBGGR10_1X10;
446 	case atomisp_bayer_order_gbrg:
447 		return MEDIA_BUS_FMT_SGBRG10_1X10;
448 	}
449 	return 0;
450 }
451 
ov2680_v_flip(struct v4l2_subdev * sd,s32 value)452 static int ov2680_v_flip(struct v4l2_subdev *sd, s32 value)
453 {
454 	struct ov2680_device *dev = to_ov2680_sensor(sd);
455 	struct camera_mipi_info *ov2680_info = NULL;
456 	struct i2c_client *client = v4l2_get_subdevdata(sd);
457 	int ret;
458 	u16 val;
459 	u8 index;
460 
461 	dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
462 	ret = ov2680_read_reg(client, 1, OV2680_FLIP_REG, &val);
463 	if (ret)
464 		return ret;
465 	if (value)
466 		val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
467 	else
468 		val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
469 
470 	ret = ov2680_write_reg(client, 1,
471 			       OV2680_FLIP_REG, val);
472 	if (ret)
473 		return ret;
474 	index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
475 		0);
476 	ov2680_info = v4l2_get_subdev_hostdata(sd);
477 	if (ov2680_info) {
478 		ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
479 		dev->format.code = ov2680_translate_bayer_order(
480 				       ov2680_info->raw_bayer_order);
481 	}
482 	return ret;
483 }
484 
ov2680_h_flip(struct v4l2_subdev * sd,s32 value)485 static int ov2680_h_flip(struct v4l2_subdev *sd, s32 value)
486 {
487 	struct ov2680_device *dev = to_ov2680_sensor(sd);
488 	struct camera_mipi_info *ov2680_info = NULL;
489 	struct i2c_client *client = v4l2_get_subdevdata(sd);
490 	int ret;
491 	u16 val;
492 	u8 index;
493 
494 	dev_dbg(&client->dev, "@%s: value:%d\n", __func__, value);
495 
496 	ret = ov2680_read_reg(client, 1, OV2680_MIRROR_REG, &val);
497 	if (ret)
498 		return ret;
499 	if (value)
500 		val |= OV2680_FLIP_MIRROR_BIT_ENABLE;
501 	else
502 		val &= ~OV2680_FLIP_MIRROR_BIT_ENABLE;
503 
504 	ret = ov2680_write_reg(client, 1,
505 			       OV2680_MIRROR_REG, val);
506 	if (ret)
507 		return ret;
508 	index = (v_flag > 0 ? OV2680_FLIP_BIT : 0) | (h_flag > 0 ? OV2680_MIRROR_BIT :
509 		0);
510 	ov2680_info = v4l2_get_subdev_hostdata(sd);
511 	if (ov2680_info) {
512 		ov2680_info->raw_bayer_order = ov2680_bayer_order_mapping[index];
513 		dev->format.code = ov2680_translate_bayer_order(
514 				       ov2680_info->raw_bayer_order);
515 	}
516 	return ret;
517 }
518 
ov2680_s_ctrl(struct v4l2_ctrl * ctrl)519 static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
520 {
521 	struct ov2680_device *dev =
522 	    container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
523 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
524 	int ret = 0;
525 
526 	switch (ctrl->id) {
527 	case V4L2_CID_VFLIP:
528 		dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
529 			__func__, ctrl->val);
530 		ret = ov2680_v_flip(&dev->sd, ctrl->val);
531 		break;
532 	case V4L2_CID_HFLIP:
533 		dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
534 			__func__, ctrl->val);
535 		ret = ov2680_h_flip(&dev->sd, ctrl->val);
536 		break;
537 	default:
538 		ret = -EINVAL;
539 	}
540 	return ret;
541 }
542 
ov2680_g_volatile_ctrl(struct v4l2_ctrl * ctrl)543 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
544 {
545 	struct ov2680_device *dev =
546 	    container_of(ctrl->handler, struct ov2680_device, ctrl_handler);
547 	int ret = 0;
548 
549 	switch (ctrl->id) {
550 	case V4L2_CID_EXPOSURE_ABSOLUTE:
551 		ret = ov2680_q_exposure(&dev->sd, &ctrl->val);
552 		break;
553 	case V4L2_CID_FOCAL_ABSOLUTE:
554 		ret = ov2680_g_focal(&dev->sd, &ctrl->val);
555 		break;
556 	case V4L2_CID_FNUMBER_ABSOLUTE:
557 		ret = ov2680_g_fnumber(&dev->sd, &ctrl->val);
558 		break;
559 	case V4L2_CID_FNUMBER_RANGE:
560 		ret = ov2680_g_fnumber_range(&dev->sd, &ctrl->val);
561 		break;
562 	case V4L2_CID_BIN_FACTOR_HORZ:
563 		ret = ov2680_g_bin_factor_x(&dev->sd, &ctrl->val);
564 		break;
565 	case V4L2_CID_BIN_FACTOR_VERT:
566 		ret = ov2680_g_bin_factor_y(&dev->sd, &ctrl->val);
567 		break;
568 	default:
569 		ret = -EINVAL;
570 	}
571 
572 	return ret;
573 }
574 
575 static const struct v4l2_ctrl_ops ctrl_ops = {
576 	.s_ctrl = ov2680_s_ctrl,
577 	.g_volatile_ctrl = ov2680_g_volatile_ctrl
578 };
579 
580 static const struct v4l2_ctrl_config ov2680_controls[] = {
581 	{
582 		.ops = &ctrl_ops,
583 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
584 		.type = V4L2_CTRL_TYPE_INTEGER,
585 		.name = "exposure",
586 		.min = 0x0,
587 		.max = 0xffff,
588 		.step = 0x01,
589 		.def = 0x00,
590 		.flags = 0,
591 	},
592 	{
593 		.ops = &ctrl_ops,
594 		.id = V4L2_CID_FOCAL_ABSOLUTE,
595 		.type = V4L2_CTRL_TYPE_INTEGER,
596 		.name = "focal length",
597 		.min = OV2680_FOCAL_LENGTH_DEFAULT,
598 		.max = OV2680_FOCAL_LENGTH_DEFAULT,
599 		.step = 0x01,
600 		.def = OV2680_FOCAL_LENGTH_DEFAULT,
601 		.flags = 0,
602 	},
603 	{
604 		.ops = &ctrl_ops,
605 		.id = V4L2_CID_FNUMBER_ABSOLUTE,
606 		.type = V4L2_CTRL_TYPE_INTEGER,
607 		.name = "f-number",
608 		.min = OV2680_F_NUMBER_DEFAULT,
609 		.max = OV2680_F_NUMBER_DEFAULT,
610 		.step = 0x01,
611 		.def = OV2680_F_NUMBER_DEFAULT,
612 		.flags = 0,
613 	},
614 	{
615 		.ops = &ctrl_ops,
616 		.id = V4L2_CID_FNUMBER_RANGE,
617 		.type = V4L2_CTRL_TYPE_INTEGER,
618 		.name = "f-number range",
619 		.min = OV2680_F_NUMBER_RANGE,
620 		.max = OV2680_F_NUMBER_RANGE,
621 		.step = 0x01,
622 		.def = OV2680_F_NUMBER_RANGE,
623 		.flags = 0,
624 	},
625 	{
626 		.ops = &ctrl_ops,
627 		.id = V4L2_CID_BIN_FACTOR_HORZ,
628 		.type = V4L2_CTRL_TYPE_INTEGER,
629 		.name = "horizontal binning factor",
630 		.min = 0,
631 		.max = OV2680_BIN_FACTOR_MAX,
632 		.step = 1,
633 		.def = 0,
634 		.flags = 0,
635 	},
636 	{
637 		.ops = &ctrl_ops,
638 		.id = V4L2_CID_BIN_FACTOR_VERT,
639 		.type = V4L2_CTRL_TYPE_INTEGER,
640 		.name = "vertical binning factor",
641 		.min = 0,
642 		.max = OV2680_BIN_FACTOR_MAX,
643 		.step = 1,
644 		.def = 0,
645 		.flags = 0,
646 	},
647 	{
648 		.ops = &ctrl_ops,
649 		.id = V4L2_CID_VFLIP,
650 		.type = V4L2_CTRL_TYPE_BOOLEAN,
651 		.name = "Flip",
652 		.min = 0,
653 		.max = 1,
654 		.step = 1,
655 		.def = 0,
656 	},
657 	{
658 		.ops = &ctrl_ops,
659 		.id = V4L2_CID_HFLIP,
660 		.type = V4L2_CTRL_TYPE_BOOLEAN,
661 		.name = "Mirror",
662 		.min = 0,
663 		.max = 1,
664 		.step = 1,
665 		.def = 0,
666 	},
667 };
668 
ov2680_init_registers(struct v4l2_subdev * sd)669 static int ov2680_init_registers(struct v4l2_subdev *sd)
670 {
671 	struct i2c_client *client = v4l2_get_subdevdata(sd);
672 	int ret;
673 
674 	ret = ov2680_write_reg(client, 1, OV2680_SW_RESET, 0x01);
675 	ret |= ov2680_write_reg_array(client, ov2680_global_setting);
676 
677 	return ret;
678 }
679 
ov2680_init(struct v4l2_subdev * sd)680 static int ov2680_init(struct v4l2_subdev *sd)
681 {
682 	struct ov2680_device *dev = to_ov2680_sensor(sd);
683 
684 	int ret;
685 
686 	mutex_lock(&dev->input_lock);
687 
688 	/* restore settings */
689 	ov2680_res = ov2680_res_preview;
690 	N_RES = N_RES_PREVIEW;
691 
692 	ret = ov2680_init_registers(sd);
693 
694 	mutex_unlock(&dev->input_lock);
695 
696 	return ret;
697 }
698 
power_ctrl(struct v4l2_subdev * sd,bool flag)699 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
700 {
701 	int ret = 0;
702 	struct ov2680_device *dev = to_ov2680_sensor(sd);
703 	struct i2c_client *client = v4l2_get_subdevdata(sd);
704 
705 	if (!dev || !dev->platform_data)
706 		return -ENODEV;
707 
708 	dev_dbg(&client->dev, "%s: %s", __func__, flag ? "on" : "off");
709 
710 	if (flag) {
711 		ret |= dev->platform_data->v1p8_ctrl(sd, 1);
712 		ret |= dev->platform_data->v2p8_ctrl(sd, 1);
713 		usleep_range(10000, 15000);
714 	}
715 
716 	if (!flag || ret) {
717 		ret |= dev->platform_data->v1p8_ctrl(sd, 0);
718 		ret |= dev->platform_data->v2p8_ctrl(sd, 0);
719 	}
720 	return ret;
721 }
722 
gpio_ctrl(struct v4l2_subdev * sd,bool flag)723 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
724 {
725 	int ret;
726 	struct ov2680_device *dev = to_ov2680_sensor(sd);
727 
728 	if (!dev || !dev->platform_data)
729 		return -ENODEV;
730 
731 	/*
732 	 * The OV2680 documents only one GPIO input (#XSHUTDN), but
733 	 * existing integrations often wire two (reset/power_down)
734 	 * because that is the way other sensors work.  There is no
735 	 * way to tell how it is wired internally, so existing
736 	 * firmwares expose both and we drive them symmetrically.
737 	 */
738 	if (flag) {
739 		ret = dev->platform_data->gpio0_ctrl(sd, 1);
740 		usleep_range(10000, 15000);
741 		/* Ignore return from second gpio, it may not be there */
742 		dev->platform_data->gpio1_ctrl(sd, 1);
743 		usleep_range(10000, 15000);
744 	} else {
745 		dev->platform_data->gpio1_ctrl(sd, 0);
746 		ret = dev->platform_data->gpio0_ctrl(sd, 0);
747 	}
748 	return ret;
749 }
750 
power_up(struct v4l2_subdev * sd)751 static int power_up(struct v4l2_subdev *sd)
752 {
753 	struct ov2680_device *dev = to_ov2680_sensor(sd);
754 	struct i2c_client *client = v4l2_get_subdevdata(sd);
755 	int ret;
756 
757 	if (!dev->platform_data) {
758 		dev_err(&client->dev,
759 			"no camera_sensor_platform_data");
760 		return -ENODEV;
761 	}
762 
763 	/* power control */
764 	ret = power_ctrl(sd, 1);
765 	if (ret)
766 		goto fail_power;
767 
768 	/* according to DS, at least 5ms is needed between DOVDD and PWDN */
769 	usleep_range(5000, 6000);
770 
771 	/* gpio ctrl */
772 	ret = gpio_ctrl(sd, 1);
773 	if (ret) {
774 		ret = gpio_ctrl(sd, 1);
775 		if (ret)
776 			goto fail_power;
777 	}
778 
779 	/* flis clock control */
780 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
781 	if (ret)
782 		goto fail_clk;
783 
784 	/* according to DS, 20ms is needed between PWDN and i2c access */
785 	msleep(20);
786 
787 	return 0;
788 
789 fail_clk:
790 	gpio_ctrl(sd, 0);
791 fail_power:
792 	power_ctrl(sd, 0);
793 	dev_err(&client->dev, "sensor power-up failed\n");
794 
795 	return ret;
796 }
797 
power_down(struct v4l2_subdev * sd)798 static int power_down(struct v4l2_subdev *sd)
799 {
800 	struct ov2680_device *dev = to_ov2680_sensor(sd);
801 	struct i2c_client *client = v4l2_get_subdevdata(sd);
802 	int ret = 0;
803 
804 	h_flag = 0;
805 	v_flag = 0;
806 	if (!dev->platform_data) {
807 		dev_err(&client->dev,
808 			"no camera_sensor_platform_data");
809 		return -ENODEV;
810 	}
811 
812 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
813 	if (ret)
814 		dev_err(&client->dev, "flisclk failed\n");
815 
816 	/* gpio ctrl */
817 	ret = gpio_ctrl(sd, 0);
818 	if (ret) {
819 		ret = gpio_ctrl(sd, 0);
820 		if (ret)
821 			dev_err(&client->dev, "gpio failed 2\n");
822 	}
823 
824 	/* power control */
825 	ret = power_ctrl(sd, 0);
826 	if (ret)
827 		dev_err(&client->dev, "vprog failed.\n");
828 
829 	return ret;
830 }
831 
ov2680_s_power(struct v4l2_subdev * sd,int on)832 static int ov2680_s_power(struct v4l2_subdev *sd, int on)
833 {
834 	int ret;
835 
836 	if (on == 0) {
837 		ret = power_down(sd);
838 	} else {
839 		ret = power_up(sd);
840 		if (!ret)
841 			return ov2680_init(sd);
842 	}
843 	return ret;
844 }
845 
846 /*
847  * distance - calculate the distance
848  * @res: resolution
849  * @w: width
850  * @h: height
851  *
852  * Get the gap between resolution and w/h.
853  * res->width/height smaller than w/h wouldn't be considered.
854  * Returns the value of gap or -1 if fail.
855  */
856 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
distance(struct ov2680_resolution * res,u32 w,u32 h)857 static int distance(struct ov2680_resolution *res, u32 w, u32 h)
858 {
859 	unsigned int w_ratio = (res->width << 13) / w;
860 	unsigned int h_ratio;
861 	int match;
862 
863 	if (h == 0)
864 		return -1;
865 	h_ratio = (res->height << 13) / h;
866 	if (h_ratio == 0)
867 		return -1;
868 	match   = abs(((w_ratio << 13) / h_ratio) - 8192);
869 
870 	if ((w_ratio < 8192) || (h_ratio < 8192)  ||
871 	    (match > LARGEST_ALLOWED_RATIO_MISMATCH))
872 		return -1;
873 
874 	return w_ratio + h_ratio;
875 }
876 
877 /* Return the nearest higher resolution index */
nearest_resolution_index(int w,int h)878 static int nearest_resolution_index(int w, int h)
879 {
880 	int i;
881 	int idx = -1;
882 	int dist;
883 	int min_dist = INT_MAX;
884 	struct ov2680_resolution *tmp_res = NULL;
885 
886 	for (i = 0; i < N_RES; i++) {
887 		tmp_res = &ov2680_res[i];
888 		dist = distance(tmp_res, w, h);
889 		if (dist == -1)
890 			continue;
891 		if (dist < min_dist) {
892 			min_dist = dist;
893 			idx = i;
894 		}
895 	}
896 
897 	return idx;
898 }
899 
get_resolution_index(int w,int h)900 static int get_resolution_index(int w, int h)
901 {
902 	int i;
903 
904 	for (i = 0; i < N_RES; i++) {
905 		if (w != ov2680_res[i].width)
906 			continue;
907 		if (h != ov2680_res[i].height)
908 			continue;
909 
910 		return i;
911 	}
912 
913 	return -1;
914 }
915 
ov2680_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)916 static int ov2680_set_fmt(struct v4l2_subdev *sd,
917 			  struct v4l2_subdev_state *sd_state,
918 			  struct v4l2_subdev_format *format)
919 {
920 	struct v4l2_mbus_framefmt *fmt = &format->format;
921 	struct ov2680_device *dev = to_ov2680_sensor(sd);
922 	struct i2c_client *client = v4l2_get_subdevdata(sd);
923 	struct camera_mipi_info *ov2680_info = NULL;
924 	int ret = 0;
925 	int idx = 0;
926 
927 	dev_dbg(&client->dev, "%s: %s: pad: %d, fmt: %p\n",
928 		__func__,
929 		(format->which == V4L2_SUBDEV_FORMAT_TRY) ? "try" : "set",
930 		format->pad, fmt);
931 
932 	if (format->pad)
933 		return -EINVAL;
934 
935 	if (!fmt)
936 		return -EINVAL;
937 
938 	ov2680_info = v4l2_get_subdev_hostdata(sd);
939 	if (!ov2680_info)
940 		return -EINVAL;
941 
942 	mutex_lock(&dev->input_lock);
943 	idx = nearest_resolution_index(fmt->width, fmt->height);
944 	if (idx == -1) {
945 		/* return the largest resolution */
946 		fmt->width = ov2680_res[N_RES - 1].width;
947 		fmt->height = ov2680_res[N_RES - 1].height;
948 	} else {
949 		fmt->width = ov2680_res[idx].width;
950 		fmt->height = ov2680_res[idx].height;
951 	}
952 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
953 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
954 		sd_state->pads->try_fmt = *fmt;
955 		mutex_unlock(&dev->input_lock);
956 		return 0;
957 	}
958 	dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
959 	dev_dbg(&client->dev, "%s: Resolution index: %d\n",
960 		__func__, dev->fmt_idx);
961 	if (dev->fmt_idx == -1) {
962 		dev_err(&client->dev, "get resolution fail\n");
963 		mutex_unlock(&dev->input_lock);
964 		return -EINVAL;
965 	}
966 	dev_dbg(&client->dev, "%s: i=%d, w=%d, h=%d\n",
967 		__func__, dev->fmt_idx, fmt->width, fmt->height);
968 
969 	// IS IT NEEDED?
970 	power_up(sd);
971 	ret = ov2680_write_reg_array(client, ov2680_res[dev->fmt_idx].regs);
972 	if (ret)
973 		dev_err(&client->dev,
974 			"ov2680 write resolution register err: %d\n", ret);
975 
976 	ret = ov2680_get_intg_factor(client, ov2680_info,
977 				     &ov2680_res[dev->fmt_idx]);
978 	if (ret) {
979 		dev_err(&client->dev, "failed to get integration factor\n");
980 		goto err;
981 	}
982 
983 	/*
984 	 * recall flip functions to avoid flip registers
985 	 * were overridden by default setting
986 	 */
987 	if (h_flag)
988 		ov2680_h_flip(sd, h_flag);
989 	if (v_flag)
990 		ov2680_v_flip(sd, v_flag);
991 
992 	v4l2_info(client, "\n%s idx %d\n", __func__, dev->fmt_idx);
993 
994 	/*
995 	 * ret = startup(sd);
996 	 * if (ret)
997 	 * dev_err(&client->dev, "ov2680 startup err\n");
998 	 */
999 err:
1000 	mutex_unlock(&dev->input_lock);
1001 	return ret;
1002 }
1003 
ov2680_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)1004 static int ov2680_get_fmt(struct v4l2_subdev *sd,
1005 			  struct v4l2_subdev_state *sd_state,
1006 			  struct v4l2_subdev_format *format)
1007 {
1008 	struct v4l2_mbus_framefmt *fmt = &format->format;
1009 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1010 
1011 	if (format->pad)
1012 		return -EINVAL;
1013 
1014 	if (!fmt)
1015 		return -EINVAL;
1016 
1017 	fmt->width = ov2680_res[dev->fmt_idx].width;
1018 	fmt->height = ov2680_res[dev->fmt_idx].height;
1019 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1020 
1021 	return 0;
1022 }
1023 
ov2680_detect(struct i2c_client * client)1024 static int ov2680_detect(struct i2c_client *client)
1025 {
1026 	struct i2c_adapter *adapter = client->adapter;
1027 	u16 high, low;
1028 	int ret;
1029 	u16 id;
1030 	u8 revision;
1031 
1032 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1033 		return -ENODEV;
1034 
1035 	ret = ov2680_read_reg(client, 1,
1036 			      OV2680_SC_CMMN_CHIP_ID_H, &high);
1037 	if (ret) {
1038 		dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1039 		return -ENODEV;
1040 	}
1041 	ret = ov2680_read_reg(client, 1,
1042 			      OV2680_SC_CMMN_CHIP_ID_L, &low);
1043 	id = ((((u16)high) << 8) | (u16)low);
1044 
1045 	if (id != OV2680_ID) {
1046 		dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1047 		return -ENODEV;
1048 	}
1049 
1050 	ret = ov2680_read_reg(client, 1,
1051 			      OV2680_SC_CMMN_SUB_ID, &high);
1052 	revision = (u8)high & 0x0f;
1053 
1054 	dev_info(&client->dev, "sensor_revision id = 0x%x, rev= %d\n",
1055 		 id, revision);
1056 
1057 	return 0;
1058 }
1059 
ov2680_s_stream(struct v4l2_subdev * sd,int enable)1060 static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
1061 {
1062 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1063 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1064 	int ret;
1065 
1066 	mutex_lock(&dev->input_lock);
1067 	if (enable)
1068 		dev_dbg(&client->dev, "ov2680_s_stream one\n");
1069 	else
1070 		dev_dbg(&client->dev, "ov2680_s_stream off\n");
1071 
1072 	ret = ov2680_write_reg(client, 1, OV2680_SW_STREAM,
1073 			       enable ? OV2680_START_STREAMING :
1074 			       OV2680_STOP_STREAMING);
1075 #if 0
1076 	/* restore settings */
1077 	ov2680_res = ov2680_res_preview;
1078 	N_RES = N_RES_PREVIEW;
1079 #endif
1080 
1081 	//otp valid at stream on state
1082 	//if(!dev->otp_data)
1083 	//	dev->otp_data = ov2680_otp_read(sd);
1084 
1085 	mutex_unlock(&dev->input_lock);
1086 
1087 	return ret;
1088 }
1089 
ov2680_s_config(struct v4l2_subdev * sd,int irq,void * platform_data)1090 static int ov2680_s_config(struct v4l2_subdev *sd,
1091 			   int irq, void *platform_data)
1092 {
1093 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1094 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1095 	int ret = 0;
1096 
1097 	if (!platform_data)
1098 		return -ENODEV;
1099 
1100 	dev->platform_data =
1101 	    (struct camera_sensor_platform_data *)platform_data;
1102 
1103 	mutex_lock(&dev->input_lock);
1104 	/*
1105 	 * power off the module, then power on it in future
1106 	 * as first power on by board may not fulfill the
1107 	 * power on sequqence needed by the module
1108 	 */
1109 	ret = power_down(sd);
1110 	if (ret) {
1111 		dev_err(&client->dev, "ov2680 power-off err.\n");
1112 		goto fail_power_off;
1113 	}
1114 
1115 	ret = power_up(sd);
1116 	if (ret) {
1117 		dev_err(&client->dev, "ov2680 power-up err.\n");
1118 		goto fail_power_on;
1119 	}
1120 
1121 	ret = dev->platform_data->csi_cfg(sd, 1);
1122 	if (ret)
1123 		goto fail_csi_cfg;
1124 
1125 	/* config & detect sensor */
1126 	ret = ov2680_detect(client);
1127 	if (ret) {
1128 		dev_err(&client->dev, "ov2680_detect err s_config.\n");
1129 		goto fail_csi_cfg;
1130 	}
1131 
1132 	/* turn off sensor, after probed */
1133 	ret = power_down(sd);
1134 	if (ret) {
1135 		dev_err(&client->dev, "ov2680 power-off err.\n");
1136 		goto fail_csi_cfg;
1137 	}
1138 	mutex_unlock(&dev->input_lock);
1139 
1140 	return 0;
1141 
1142 fail_csi_cfg:
1143 	dev->platform_data->csi_cfg(sd, 0);
1144 fail_power_on:
1145 	power_down(sd);
1146 	dev_err(&client->dev, "sensor power-gating failed\n");
1147 fail_power_off:
1148 	mutex_unlock(&dev->input_lock);
1149 	return ret;
1150 }
1151 
ov2680_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * interval)1152 static int ov2680_g_frame_interval(struct v4l2_subdev *sd,
1153 				   struct v4l2_subdev_frame_interval *interval)
1154 {
1155 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1156 
1157 	interval->interval.numerator = 1;
1158 	interval->interval.denominator = ov2680_res[dev->fmt_idx].fps;
1159 
1160 	return 0;
1161 }
1162 
ov2680_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)1163 static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
1164 				 struct v4l2_subdev_state *sd_state,
1165 				 struct v4l2_subdev_mbus_code_enum *code)
1166 {
1167 	if (code->index >= MAX_FMTS)
1168 		return -EINVAL;
1169 
1170 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1171 	return 0;
1172 }
1173 
ov2680_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)1174 static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
1175 				  struct v4l2_subdev_state *sd_state,
1176 				  struct v4l2_subdev_frame_size_enum *fse)
1177 {
1178 	int index = fse->index;
1179 
1180 	if (index >= N_RES)
1181 		return -EINVAL;
1182 
1183 	fse->min_width = ov2680_res[index].width;
1184 	fse->min_height = ov2680_res[index].height;
1185 	fse->max_width = ov2680_res[index].width;
1186 	fse->max_height = ov2680_res[index].height;
1187 
1188 	return 0;
1189 }
1190 
ov2680_g_skip_frames(struct v4l2_subdev * sd,u32 * frames)1191 static int ov2680_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1192 {
1193 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1194 
1195 	mutex_lock(&dev->input_lock);
1196 	*frames = ov2680_res[dev->fmt_idx].skip_frames;
1197 	mutex_unlock(&dev->input_lock);
1198 
1199 	return 0;
1200 }
1201 
1202 static const struct v4l2_subdev_video_ops ov2680_video_ops = {
1203 	.s_stream = ov2680_s_stream,
1204 	.g_frame_interval = ov2680_g_frame_interval,
1205 };
1206 
1207 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops = {
1208 	.g_skip_frames	= ov2680_g_skip_frames,
1209 };
1210 
1211 static const struct v4l2_subdev_core_ops ov2680_core_ops = {
1212 	.s_power = ov2680_s_power,
1213 	.ioctl = ov2680_ioctl,
1214 };
1215 
1216 static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
1217 	.enum_mbus_code = ov2680_enum_mbus_code,
1218 	.enum_frame_size = ov2680_enum_frame_size,
1219 	.get_fmt = ov2680_get_fmt,
1220 	.set_fmt = ov2680_set_fmt,
1221 };
1222 
1223 static const struct v4l2_subdev_ops ov2680_ops = {
1224 	.core = &ov2680_core_ops,
1225 	.video = &ov2680_video_ops,
1226 	.pad = &ov2680_pad_ops,
1227 	.sensor = &ov2680_sensor_ops,
1228 };
1229 
ov2680_remove(struct i2c_client * client)1230 static int ov2680_remove(struct i2c_client *client)
1231 {
1232 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1233 	struct ov2680_device *dev = to_ov2680_sensor(sd);
1234 
1235 	dev_dbg(&client->dev, "ov2680_remove...\n");
1236 
1237 	dev->platform_data->csi_cfg(sd, 0);
1238 
1239 	v4l2_device_unregister_subdev(sd);
1240 	media_entity_cleanup(&dev->sd.entity);
1241 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1242 	kfree(dev);
1243 
1244 	return 0;
1245 }
1246 
ov2680_probe(struct i2c_client * client)1247 static int ov2680_probe(struct i2c_client *client)
1248 {
1249 	struct ov2680_device *dev;
1250 	int ret;
1251 	void *pdata;
1252 	unsigned int i;
1253 
1254 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1255 	if (!dev)
1256 		return -ENOMEM;
1257 
1258 	mutex_init(&dev->input_lock);
1259 
1260 	dev->fmt_idx = 0;
1261 	v4l2_i2c_subdev_init(&dev->sd, client, &ov2680_ops);
1262 
1263 	pdata = gmin_camera_platform_data(&dev->sd,
1264 					  ATOMISP_INPUT_FORMAT_RAW_10,
1265 					  atomisp_bayer_order_bggr);
1266 	if (!pdata) {
1267 		ret = -EINVAL;
1268 		goto out_free;
1269 	}
1270 
1271 	ret = ov2680_s_config(&dev->sd, client->irq, pdata);
1272 	if (ret)
1273 		goto out_free;
1274 
1275 	ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1276 	if (ret)
1277 		goto out_free;
1278 
1279 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1280 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1281 	dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1282 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1283 	ret =
1284 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1285 				   ARRAY_SIZE(ov2680_controls));
1286 	if (ret) {
1287 		ov2680_remove(client);
1288 		return ret;
1289 	}
1290 
1291 	for (i = 0; i < ARRAY_SIZE(ov2680_controls); i++)
1292 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov2680_controls[i],
1293 				     NULL);
1294 
1295 	if (dev->ctrl_handler.error) {
1296 		ov2680_remove(client);
1297 		return dev->ctrl_handler.error;
1298 	}
1299 
1300 	/* Use same lock for controls as for everything else. */
1301 	dev->ctrl_handler.lock = &dev->input_lock;
1302 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1303 
1304 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1305 	if (ret) {
1306 		ov2680_remove(client);
1307 		dev_dbg(&client->dev, "+++ remove ov2680\n");
1308 	}
1309 	return ret;
1310 out_free:
1311 	dev_dbg(&client->dev, "+++ out free\n");
1312 	v4l2_device_unregister_subdev(&dev->sd);
1313 	kfree(dev);
1314 	return ret;
1315 }
1316 
1317 static const struct acpi_device_id ov2680_acpi_match[] = {
1318 	{"XXOV2680"},
1319 	{"OVTI2680"},
1320 	{},
1321 };
1322 MODULE_DEVICE_TABLE(acpi, ov2680_acpi_match);
1323 
1324 static struct i2c_driver ov2680_driver = {
1325 	.driver = {
1326 		.name = "ov2680",
1327 		.acpi_match_table = ov2680_acpi_match,
1328 	},
1329 	.probe_new = ov2680_probe,
1330 	.remove = ov2680_remove,
1331 };
1332 module_i2c_driver(ov2680_driver);
1333 
1334 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1335 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1336 MODULE_LICENSE("GPL");
1337