1 /*
2  * Support for GalaxyCore GC0310 VGA camera sensor.
3  *
4  * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version
8  * 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16 
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
20 #include <linux/mm.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/moduleparam.h>
30 #include <media/v4l2-device.h>
31 #include <linux/io.h>
32 #include "../include/linux/atomisp_gmin_platform.h"
33 
34 #include "gc0310.h"
35 
36 /* i2c read/write stuff */
37 static int gc0310_read_reg(struct i2c_client *client,
38 			   u16 data_length, u8 reg, u8 *val)
39 {
40 	int err;
41 	struct i2c_msg msg[2];
42 	unsigned char data[1];
43 
44 	if (!client->adapter) {
45 		dev_err(&client->dev, "%s error, no client->adapter\n",
46 			__func__);
47 		return -ENODEV;
48 	}
49 
50 	if (data_length != GC0310_8BIT) {
51 		dev_err(&client->dev, "%s error, invalid data length\n",
52 			__func__);
53 		return -EINVAL;
54 	}
55 
56 	memset(msg, 0, sizeof(msg));
57 
58 	msg[0].addr = client->addr;
59 	msg[0].flags = 0;
60 	msg[0].len = I2C_MSG_LENGTH;
61 	msg[0].buf = data;
62 
63 	/* high byte goes out first */
64 	data[0] = (u8)(reg & 0xff);
65 
66 	msg[1].addr = client->addr;
67 	msg[1].len = data_length;
68 	msg[1].flags = I2C_M_RD;
69 	msg[1].buf = data;
70 
71 	err = i2c_transfer(client->adapter, msg, 2);
72 	if (err != 2) {
73 		if (err >= 0)
74 			err = -EIO;
75 		dev_err(&client->dev,
76 			"read from offset 0x%x error %d", reg, err);
77 		return err;
78 	}
79 
80 	*val = 0;
81 	/* high byte comes first */
82 	if (data_length == GC0310_8BIT)
83 		*val = (u8)data[0];
84 
85 	return 0;
86 }
87 
88 static int gc0310_i2c_write(struct i2c_client *client, u16 len, u8 *data)
89 {
90 	struct i2c_msg msg;
91 	const int num_msg = 1;
92 	int ret;
93 
94 	msg.addr = client->addr;
95 	msg.flags = 0;
96 	msg.len = len;
97 	msg.buf = data;
98 	ret = i2c_transfer(client->adapter, &msg, 1);
99 
100 	return ret == num_msg ? 0 : -EIO;
101 }
102 
103 static int gc0310_write_reg(struct i2c_client *client, u16 data_length,
104 							u8 reg, u8 val)
105 {
106 	int ret;
107 	unsigned char data[2] = {0};
108 	u8 *wreg = (u8 *)data;
109 	const u16 len = data_length + sizeof(u8); /* 8-bit address + data */
110 
111 	if (data_length != GC0310_8BIT) {
112 		dev_err(&client->dev,
113 			"%s error, invalid data_length\n", __func__);
114 		return -EINVAL;
115 	}
116 
117 	/* high byte goes out first */
118 	*wreg = (u8)(reg & 0xff);
119 
120 	if (data_length == GC0310_8BIT)
121 		data[1] = (u8)(val);
122 
123 	ret = gc0310_i2c_write(client, len, data);
124 	if (ret)
125 		dev_err(&client->dev,
126 			"write error: wrote 0x%x to offset 0x%x error %d",
127 			val, reg, ret);
128 
129 	return ret;
130 }
131 
132 /*
133  * gc0310_write_reg_array - Initializes a list of GC0310 registers
134  * @client: i2c driver client structure
135  * @reglist: list of registers to be written
136  *
137  * This function initializes a list of registers. When consecutive addresses
138  * are found in a row on the list, this function creates a buffer and sends
139  * consecutive data in a single i2c_transfer().
140  *
141  * __gc0310_flush_reg_array, __gc0310_buf_reg_array() and
142  * __gc0310_write_reg_is_consecutive() are internal functions to
143  * gc0310_write_reg_array_fast() and should be not used anywhere else.
144  *
145  */
146 
147 static int __gc0310_flush_reg_array(struct i2c_client *client,
148 				    struct gc0310_write_ctrl *ctrl)
149 {
150 	u16 size;
151 
152 	if (ctrl->index == 0)
153 		return 0;
154 
155 	size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
156 	ctrl->buffer.addr = (u8)(ctrl->buffer.addr);
157 	ctrl->index = 0;
158 
159 	return gc0310_i2c_write(client, size, (u8 *)&ctrl->buffer);
160 }
161 
162 static int __gc0310_buf_reg_array(struct i2c_client *client,
163 				  struct gc0310_write_ctrl *ctrl,
164 				  const struct gc0310_reg *next)
165 {
166 	int size;
167 
168 	switch (next->type) {
169 	case GC0310_8BIT:
170 		size = 1;
171 		ctrl->buffer.data[ctrl->index] = (u8)next->val;
172 		break;
173 	default:
174 		return -EINVAL;
175 	}
176 
177 	/* When first item is added, we need to store its starting address */
178 	if (ctrl->index == 0)
179 		ctrl->buffer.addr = next->reg;
180 
181 	ctrl->index += size;
182 
183 	/*
184 	 * Buffer cannot guarantee free space for u32? Better flush it to avoid
185 	 * possible lack of memory for next item.
186 	 */
187 	if (ctrl->index + sizeof(u8) >= GC0310_MAX_WRITE_BUF_SIZE)
188 		return __gc0310_flush_reg_array(client, ctrl);
189 
190 	return 0;
191 }
192 
193 static int __gc0310_write_reg_is_consecutive(struct i2c_client *client,
194 					     struct gc0310_write_ctrl *ctrl,
195 					     const struct gc0310_reg *next)
196 {
197 	if (ctrl->index == 0)
198 		return 1;
199 
200 	return ctrl->buffer.addr + ctrl->index == next->reg;
201 }
202 
203 static int gc0310_write_reg_array(struct i2c_client *client,
204 				  const struct gc0310_reg *reglist)
205 {
206 	const struct gc0310_reg *next = reglist;
207 	struct gc0310_write_ctrl ctrl;
208 	int err;
209 
210 	ctrl.index = 0;
211 	for (; next->type != GC0310_TOK_TERM; next++) {
212 		switch (next->type & GC0310_TOK_MASK) {
213 		case GC0310_TOK_DELAY:
214 			err = __gc0310_flush_reg_array(client, &ctrl);
215 			if (err)
216 				return err;
217 			msleep(next->val);
218 			break;
219 		default:
220 			/*
221 			 * If next address is not consecutive, data needs to be
222 			 * flushed before proceed.
223 			 */
224 			if (!__gc0310_write_reg_is_consecutive(client, &ctrl,
225 								next)) {
226 				err = __gc0310_flush_reg_array(client, &ctrl);
227 				if (err)
228 					return err;
229 			}
230 			err = __gc0310_buf_reg_array(client, &ctrl, next);
231 			if (err) {
232 				dev_err(&client->dev, "%s: write error, aborted\n",
233 					 __func__);
234 				return err;
235 			}
236 			break;
237 		}
238 	}
239 
240 	return __gc0310_flush_reg_array(client, &ctrl);
241 }
242 
243 static int gc0310_g_focal(struct v4l2_subdev *sd, s32 *val)
244 {
245 	*val = (GC0310_FOCAL_LENGTH_NUM << 16) | GC0310_FOCAL_LENGTH_DEM;
246 	return 0;
247 }
248 
249 static int gc0310_g_fnumber(struct v4l2_subdev *sd, s32 *val)
250 {
251 	/*const f number for imx*/
252 	*val = (GC0310_F_NUMBER_DEFAULT_NUM << 16) | GC0310_F_NUMBER_DEM;
253 	return 0;
254 }
255 
256 static int gc0310_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
257 {
258 	*val = (GC0310_F_NUMBER_DEFAULT_NUM << 24) |
259 		(GC0310_F_NUMBER_DEM << 16) |
260 		(GC0310_F_NUMBER_DEFAULT_NUM << 8) | GC0310_F_NUMBER_DEM;
261 	return 0;
262 }
263 
264 static int gc0310_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
265 {
266 	struct gc0310_device *dev = to_gc0310_sensor(sd);
267 
268 	*val = gc0310_res[dev->fmt_idx].bin_factor_x;
269 
270 	return 0;
271 }
272 
273 static int gc0310_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
274 {
275 	struct gc0310_device *dev = to_gc0310_sensor(sd);
276 
277 	*val = gc0310_res[dev->fmt_idx].bin_factor_y;
278 
279 	return 0;
280 }
281 
282 static int gc0310_get_intg_factor(struct i2c_client *client,
283 				struct camera_mipi_info *info,
284 				const struct gc0310_resolution *res)
285 {
286 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
287 	struct gc0310_device *dev = to_gc0310_sensor(sd);
288 	struct atomisp_sensor_mode_data *buf = &info->data;
289 	u16 val;
290 	u8 reg_val;
291 	int ret;
292 	unsigned int hori_blanking;
293 	unsigned int vert_blanking;
294 	unsigned int sh_delay;
295 
296 	if (!info)
297 		return -EINVAL;
298 
299 	/* pixel clock calculattion */
300 	dev->vt_pix_clk_freq_mhz = 14400000; // 16.8MHz
301 	buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz;
302 	pr_info("vt_pix_clk_freq_mhz=%d\n", buf->vt_pix_clk_freq_mhz);
303 
304 	/* get integration time */
305 	buf->coarse_integration_time_min = GC0310_COARSE_INTG_TIME_MIN;
306 	buf->coarse_integration_time_max_margin =
307 					GC0310_COARSE_INTG_TIME_MAX_MARGIN;
308 
309 	buf->fine_integration_time_min = GC0310_FINE_INTG_TIME_MIN;
310 	buf->fine_integration_time_max_margin =
311 					GC0310_FINE_INTG_TIME_MAX_MARGIN;
312 
313 	buf->fine_integration_time_def = GC0310_FINE_INTG_TIME_MIN;
314 	buf->read_mode = res->bin_mode;
315 
316 	/* get the cropping and output resolution to ISP for this mode. */
317 	/* Getting crop_horizontal_start */
318 	ret =  gc0310_read_reg(client, GC0310_8BIT,
319 					GC0310_H_CROP_START_H, &reg_val);
320 	if (ret)
321 		return ret;
322 	val = (reg_val & 0xFF) << 8;
323 	ret =  gc0310_read_reg(client, GC0310_8BIT,
324 					GC0310_H_CROP_START_L, &reg_val);
325 	if (ret)
326 		return ret;
327 	buf->crop_horizontal_start = val | (reg_val & 0xFF);
328 	pr_info("crop_horizontal_start=%d\n", buf->crop_horizontal_start);
329 
330 	/* Getting crop_vertical_start */
331 	ret =  gc0310_read_reg(client, GC0310_8BIT,
332 					GC0310_V_CROP_START_H, &reg_val);
333 	if (ret)
334 		return ret;
335 	val = (reg_val & 0xFF) << 8;
336 	ret =  gc0310_read_reg(client, GC0310_8BIT,
337 					GC0310_V_CROP_START_L, &reg_val);
338 	if (ret)
339 		return ret;
340 	buf->crop_vertical_start = val | (reg_val & 0xFF);
341 	pr_info("crop_vertical_start=%d\n", buf->crop_vertical_start);
342 
343 	/* Getting output_width */
344 	ret = gc0310_read_reg(client, GC0310_8BIT,
345 					GC0310_H_OUTSIZE_H, &reg_val);
346 	if (ret)
347 		return ret;
348 	val = (reg_val & 0xFF) << 8;
349 	ret = gc0310_read_reg(client, GC0310_8BIT,
350 					GC0310_H_OUTSIZE_L, &reg_val);
351 	if (ret)
352 		return ret;
353 	buf->output_width = val | (reg_val & 0xFF);
354 	pr_info("output_width=%d\n", buf->output_width);
355 
356 	/* Getting output_height */
357 	ret = gc0310_read_reg(client, GC0310_8BIT,
358 					GC0310_V_OUTSIZE_H, &reg_val);
359 	if (ret)
360 		return ret;
361 	val = (reg_val & 0xFF) << 8;
362 	ret = gc0310_read_reg(client, GC0310_8BIT,
363 					GC0310_V_OUTSIZE_L, &reg_val);
364 	if (ret)
365 		return ret;
366 	buf->output_height = val | (reg_val & 0xFF);
367 	pr_info("output_height=%d\n", buf->output_height);
368 
369 	buf->crop_horizontal_end = buf->crop_horizontal_start + buf->output_width - 1;
370 	buf->crop_vertical_end = buf->crop_vertical_start + buf->output_height - 1;
371 	pr_info("crop_horizontal_end=%d\n", buf->crop_horizontal_end);
372 	pr_info("crop_vertical_end=%d\n", buf->crop_vertical_end);
373 
374 	/* Getting line_length_pck */
375 	ret = gc0310_read_reg(client, GC0310_8BIT,
376 					GC0310_H_BLANKING_H, &reg_val);
377 	if (ret)
378 		return ret;
379 	val = (reg_val & 0xFF) << 8;
380 	ret = gc0310_read_reg(client, GC0310_8BIT,
381 					GC0310_H_BLANKING_L, &reg_val);
382 	if (ret)
383 		return ret;
384 	hori_blanking = val | (reg_val & 0xFF);
385 	ret = gc0310_read_reg(client, GC0310_8BIT,
386 					GC0310_SH_DELAY, &reg_val);
387 	if (ret)
388 		return ret;
389 	sh_delay = reg_val;
390 	buf->line_length_pck = buf->output_width + hori_blanking + sh_delay + 4;
391 	pr_info("hori_blanking=%d sh_delay=%d line_length_pck=%d\n", hori_blanking, sh_delay, buf->line_length_pck);
392 
393 	/* Getting frame_length_lines */
394 	ret = gc0310_read_reg(client, GC0310_8BIT,
395 					GC0310_V_BLANKING_H, &reg_val);
396 	if (ret)
397 		return ret;
398 	val = (reg_val & 0xFF) << 8;
399 	ret = gc0310_read_reg(client, GC0310_8BIT,
400 					GC0310_V_BLANKING_L, &reg_val);
401 	if (ret)
402 		return ret;
403 	vert_blanking = val | (reg_val & 0xFF);
404 	buf->frame_length_lines = buf->output_height + vert_blanking;
405 	pr_info("vert_blanking=%d frame_length_lines=%d\n", vert_blanking, buf->frame_length_lines);
406 
407 	buf->binning_factor_x = res->bin_factor_x ?
408 					res->bin_factor_x : 1;
409 	buf->binning_factor_y = res->bin_factor_y ?
410 					res->bin_factor_y : 1;
411 	return 0;
412 }
413 
414 static int gc0310_set_gain(struct v4l2_subdev *sd, int gain)
415 
416 {
417 	struct i2c_client *client = v4l2_get_subdevdata(sd);
418 	int ret;
419 	u8 again, dgain;
420 
421 	if (gain < 0x20)
422 		gain = 0x20;
423 	if (gain > 0x80)
424 		gain = 0x80;
425 
426 	if (gain >= 0x20 && gain < 0x40) {
427 		again = 0x0; /* sqrt(2) */
428 		dgain = gain;
429 	} else {
430 		again = 0x2; /* 2 * sqrt(2) */
431 		dgain = gain / 2;
432 	}
433 
434 	pr_info("gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain);
435 
436 	/* set analog gain */
437 	ret = gc0310_write_reg(client, GC0310_8BIT,
438 					GC0310_AGC_ADJ, again);
439 	if (ret)
440 		return ret;
441 
442 	/* set digital gain */
443 	ret = gc0310_write_reg(client, GC0310_8BIT,
444 					GC0310_DGC_ADJ, dgain);
445 	if (ret)
446 		return ret;
447 
448 	return 0;
449 }
450 
451 static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
452 				 int gain, int digitgain)
453 
454 {
455 	struct i2c_client *client = v4l2_get_subdevdata(sd);
456 	int ret;
457 
458 	pr_info("coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain);
459 
460 	/* set exposure */
461 	ret = gc0310_write_reg(client, GC0310_8BIT,
462 					GC0310_AEC_PK_EXPO_L,
463 					coarse_itg & 0xff);
464 	if (ret)
465 		return ret;
466 
467 	ret = gc0310_write_reg(client, GC0310_8BIT,
468 					GC0310_AEC_PK_EXPO_H,
469 					(coarse_itg >> 8) & 0x0f);
470 	if (ret)
471 		return ret;
472 
473 	ret = gc0310_set_gain(sd, gain);
474 	if (ret)
475 		return ret;
476 
477 	return ret;
478 }
479 
480 static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure,
481 	int gain, int digitgain)
482 {
483 	struct gc0310_device *dev = to_gc0310_sensor(sd);
484 	int ret;
485 
486 	mutex_lock(&dev->input_lock);
487 	ret = __gc0310_set_exposure(sd, exposure, gain, digitgain);
488 	mutex_unlock(&dev->input_lock);
489 
490 	return ret;
491 }
492 
493 static long gc0310_s_exposure(struct v4l2_subdev *sd,
494 			       struct atomisp_exposure *exposure)
495 {
496 	int exp = exposure->integration_time[0];
497 	int gain = exposure->gain[0];
498 	int digitgain = exposure->gain[1];
499 
500 	/* we should not accept the invalid value below. */
501 	if (gain == 0) {
502 		struct i2c_client *client = v4l2_get_subdevdata(sd);
503 
504 		v4l2_err(client, "%s: invalid value\n", __func__);
505 		return -EINVAL;
506 	}
507 
508 	return gc0310_set_exposure(sd, exp, gain, digitgain);
509 }
510 
511 /* TO DO */
512 static int gc0310_v_flip(struct v4l2_subdev *sd, s32 value)
513 {
514 	return 0;
515 }
516 
517 /* TO DO */
518 static int gc0310_h_flip(struct v4l2_subdev *sd, s32 value)
519 {
520 	return 0;
521 }
522 
523 static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
524 {
525 	switch (cmd) {
526 	case ATOMISP_IOC_S_EXPOSURE:
527 		return gc0310_s_exposure(sd, arg);
528 	default:
529 		return -EINVAL;
530 	}
531 	return 0;
532 }
533 
534 /* This returns the exposure time being used. This should only be used
535  * for filling in EXIF data, not for actual image processing.
536  */
537 static int gc0310_q_exposure(struct v4l2_subdev *sd, s32 *value)
538 {
539 	struct i2c_client *client = v4l2_get_subdevdata(sd);
540 	u8 reg_v;
541 	int ret;
542 
543 	/* get exposure */
544 	ret = gc0310_read_reg(client, GC0310_8BIT,
545 					GC0310_AEC_PK_EXPO_L,
546 					&reg_v);
547 	if (ret)
548 		goto err;
549 
550 	*value = reg_v;
551 	ret = gc0310_read_reg(client, GC0310_8BIT,
552 					GC0310_AEC_PK_EXPO_H,
553 					&reg_v);
554 	if (ret)
555 		goto err;
556 
557 	*value = *value + (reg_v << 8);
558 err:
559 	return ret;
560 }
561 
562 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl)
563 {
564 	struct gc0310_device *dev =
565 	    container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
566 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
567 	int ret = 0;
568 
569 	switch (ctrl->id) {
570 	case V4L2_CID_VFLIP:
571 		dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
572 			__func__, ctrl->val);
573 		ret = gc0310_v_flip(&dev->sd, ctrl->val);
574 		break;
575 	case V4L2_CID_HFLIP:
576 		dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
577 			__func__, ctrl->val);
578 		ret = gc0310_h_flip(&dev->sd, ctrl->val);
579 		break;
580 	default:
581 		ret = -EINVAL;
582 	}
583 	return ret;
584 }
585 
586 static int gc0310_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
587 {
588 	struct gc0310_device *dev =
589 	    container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
590 	int ret = 0;
591 
592 	switch (ctrl->id) {
593 	case V4L2_CID_EXPOSURE_ABSOLUTE:
594 		ret = gc0310_q_exposure(&dev->sd, &ctrl->val);
595 		break;
596 	case V4L2_CID_FOCAL_ABSOLUTE:
597 		ret = gc0310_g_focal(&dev->sd, &ctrl->val);
598 		break;
599 	case V4L2_CID_FNUMBER_ABSOLUTE:
600 		ret = gc0310_g_fnumber(&dev->sd, &ctrl->val);
601 		break;
602 	case V4L2_CID_FNUMBER_RANGE:
603 		ret = gc0310_g_fnumber_range(&dev->sd, &ctrl->val);
604 		break;
605 	case V4L2_CID_BIN_FACTOR_HORZ:
606 		ret = gc0310_g_bin_factor_x(&dev->sd, &ctrl->val);
607 		break;
608 	case V4L2_CID_BIN_FACTOR_VERT:
609 		ret = gc0310_g_bin_factor_y(&dev->sd, &ctrl->val);
610 		break;
611 	default:
612 		ret = -EINVAL;
613 	}
614 
615 	return ret;
616 }
617 
618 static const struct v4l2_ctrl_ops ctrl_ops = {
619 	.s_ctrl = gc0310_s_ctrl,
620 	.g_volatile_ctrl = gc0310_g_volatile_ctrl
621 };
622 
623 static const struct v4l2_ctrl_config gc0310_controls[] = {
624 	{
625 	 .ops = &ctrl_ops,
626 	 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
627 	 .type = V4L2_CTRL_TYPE_INTEGER,
628 	 .name = "exposure",
629 	 .min = 0x0,
630 	 .max = 0xffff,
631 	 .step = 0x01,
632 	 .def = 0x00,
633 	 .flags = 0,
634 	 },
635 	{
636 	 .ops = &ctrl_ops,
637 	 .id = V4L2_CID_VFLIP,
638 	 .type = V4L2_CTRL_TYPE_BOOLEAN,
639 	 .name = "Flip",
640 	 .min = 0,
641 	 .max = 1,
642 	 .step = 1,
643 	 .def = 0,
644 	 },
645 	{
646 	 .ops = &ctrl_ops,
647 	 .id = V4L2_CID_HFLIP,
648 	 .type = V4L2_CTRL_TYPE_BOOLEAN,
649 	 .name = "Mirror",
650 	 .min = 0,
651 	 .max = 1,
652 	 .step = 1,
653 	 .def = 0,
654 	 },
655 	{
656 	 .ops = &ctrl_ops,
657 	 .id = V4L2_CID_FOCAL_ABSOLUTE,
658 	 .type = V4L2_CTRL_TYPE_INTEGER,
659 	 .name = "focal length",
660 	 .min = GC0310_FOCAL_LENGTH_DEFAULT,
661 	 .max = GC0310_FOCAL_LENGTH_DEFAULT,
662 	 .step = 0x01,
663 	 .def = GC0310_FOCAL_LENGTH_DEFAULT,
664 	 .flags = 0,
665 	 },
666 	{
667 	 .ops = &ctrl_ops,
668 	 .id = V4L2_CID_FNUMBER_ABSOLUTE,
669 	 .type = V4L2_CTRL_TYPE_INTEGER,
670 	 .name = "f-number",
671 	 .min = GC0310_F_NUMBER_DEFAULT,
672 	 .max = GC0310_F_NUMBER_DEFAULT,
673 	 .step = 0x01,
674 	 .def = GC0310_F_NUMBER_DEFAULT,
675 	 .flags = 0,
676 	 },
677 	{
678 	 .ops = &ctrl_ops,
679 	 .id = V4L2_CID_FNUMBER_RANGE,
680 	 .type = V4L2_CTRL_TYPE_INTEGER,
681 	 .name = "f-number range",
682 	 .min = GC0310_F_NUMBER_RANGE,
683 	 .max = GC0310_F_NUMBER_RANGE,
684 	 .step = 0x01,
685 	 .def = GC0310_F_NUMBER_RANGE,
686 	 .flags = 0,
687 	 },
688 	{
689 	 .ops = &ctrl_ops,
690 	 .id = V4L2_CID_BIN_FACTOR_HORZ,
691 	 .type = V4L2_CTRL_TYPE_INTEGER,
692 	 .name = "horizontal binning factor",
693 	 .min = 0,
694 	 .max = GC0310_BIN_FACTOR_MAX,
695 	 .step = 1,
696 	 .def = 0,
697 	 .flags = 0,
698 	 },
699 	{
700 	 .ops = &ctrl_ops,
701 	 .id = V4L2_CID_BIN_FACTOR_VERT,
702 	 .type = V4L2_CTRL_TYPE_INTEGER,
703 	 .name = "vertical binning factor",
704 	 .min = 0,
705 	 .max = GC0310_BIN_FACTOR_MAX,
706 	 .step = 1,
707 	 .def = 0,
708 	 .flags = 0,
709 	 },
710 };
711 
712 static int gc0310_init(struct v4l2_subdev *sd)
713 {
714 	int ret;
715 	struct i2c_client *client = v4l2_get_subdevdata(sd);
716 	struct gc0310_device *dev = to_gc0310_sensor(sd);
717 
718 	pr_info("%s S\n", __func__);
719 	mutex_lock(&dev->input_lock);
720 
721 	/* set initial registers */
722 	ret  = gc0310_write_reg_array(client, gc0310_reset_register);
723 
724 	/* restore settings */
725 	gc0310_res = gc0310_res_preview;
726 	N_RES = N_RES_PREVIEW;
727 
728 	mutex_unlock(&dev->input_lock);
729 
730 	pr_info("%s E\n", __func__);
731 	return ret;
732 }
733 
734 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
735 {
736 	int ret = 0;
737 	struct gc0310_device *dev = to_gc0310_sensor(sd);
738 
739 	if (!dev || !dev->platform_data)
740 		return -ENODEV;
741 
742 	if (flag) {
743 		/* The upstream module driver (written to Crystal
744 		 * Cove) had this logic to pulse the rails low first.
745 		 * This appears to break things on the MRD7 with the
746 		 * X-Powers PMIC...
747 		 *
748 		 *     ret = dev->platform_data->v1p8_ctrl(sd, 0);
749 		 *     ret |= dev->platform_data->v2p8_ctrl(sd, 0);
750 		 *     mdelay(50);
751 		 */
752 		ret |= dev->platform_data->v1p8_ctrl(sd, 1);
753 		ret |= dev->platform_data->v2p8_ctrl(sd, 1);
754 		usleep_range(10000, 15000);
755 	}
756 
757 	if (!flag || ret) {
758 		ret |= dev->platform_data->v1p8_ctrl(sd, 0);
759 		ret |= dev->platform_data->v2p8_ctrl(sd, 0);
760 	}
761 	return ret;
762 }
763 
764 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
765 {
766 	int ret;
767 	struct gc0310_device *dev = to_gc0310_sensor(sd);
768 
769 	if (!dev || !dev->platform_data)
770 		return -ENODEV;
771 
772 	/* GPIO0 == "reset" (active low), GPIO1 == "power down" */
773 	if (flag) {
774 		/* Pulse reset, then release power down */
775 		ret = dev->platform_data->gpio0_ctrl(sd, 0);
776 		usleep_range(5000, 10000);
777 		ret |= dev->platform_data->gpio0_ctrl(sd, 1);
778 		usleep_range(10000, 15000);
779 		ret |= dev->platform_data->gpio1_ctrl(sd, 0);
780 		usleep_range(10000, 15000);
781 	} else {
782 		ret = dev->platform_data->gpio1_ctrl(sd, 1);
783 		ret |= dev->platform_data->gpio0_ctrl(sd, 0);
784 	}
785 	return ret;
786 }
787 
788 static int power_down(struct v4l2_subdev *sd);
789 
790 static int power_up(struct v4l2_subdev *sd)
791 {
792 	struct gc0310_device *dev = to_gc0310_sensor(sd);
793 	struct i2c_client *client = v4l2_get_subdevdata(sd);
794 	int ret;
795 
796 	pr_info("%s S\n", __func__);
797 	if (!dev->platform_data) {
798 		dev_err(&client->dev,
799 			"no camera_sensor_platform_data");
800 		return -ENODEV;
801 	}
802 
803 	/* power control */
804 	ret = power_ctrl(sd, 1);
805 	if (ret)
806 		goto fail_power;
807 
808 	/* flis clock control */
809 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
810 	if (ret)
811 		goto fail_clk;
812 
813 	/* gpio ctrl */
814 	ret = gpio_ctrl(sd, 1);
815 	if (ret) {
816 		ret = gpio_ctrl(sd, 1);
817 		if (ret)
818 			goto fail_gpio;
819 	}
820 
821 	msleep(100);
822 
823 	pr_info("%s E\n", __func__);
824 	return 0;
825 
826 fail_gpio:
827 	dev->platform_data->flisclk_ctrl(sd, 0);
828 fail_clk:
829 	power_ctrl(sd, 0);
830 fail_power:
831 	dev_err(&client->dev, "sensor power-up failed\n");
832 
833 	return ret;
834 }
835 
836 static int power_down(struct v4l2_subdev *sd)
837 {
838 	struct gc0310_device *dev = to_gc0310_sensor(sd);
839 	struct i2c_client *client = v4l2_get_subdevdata(sd);
840 	int ret = 0;
841 
842 	if (!dev->platform_data) {
843 		dev_err(&client->dev,
844 			"no camera_sensor_platform_data");
845 		return -ENODEV;
846 	}
847 
848 	/* gpio ctrl */
849 	ret = gpio_ctrl(sd, 0);
850 	if (ret) {
851 		ret = gpio_ctrl(sd, 0);
852 		if (ret)
853 			dev_err(&client->dev, "gpio failed 2\n");
854 	}
855 
856 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
857 	if (ret)
858 		dev_err(&client->dev, "flisclk failed\n");
859 
860 	/* power control */
861 	ret = power_ctrl(sd, 0);
862 	if (ret)
863 		dev_err(&client->dev, "vprog failed.\n");
864 
865 	return ret;
866 }
867 
868 static int gc0310_s_power(struct v4l2_subdev *sd, int on)
869 {
870 	int ret;
871 
872 	if (on == 0)
873 		return power_down(sd);
874 	else {
875 		ret = power_up(sd);
876 		if (!ret)
877 			return gc0310_init(sd);
878 	}
879 	return ret;
880 }
881 
882 /*
883  * distance - calculate the distance
884  * @res: resolution
885  * @w: width
886  * @h: height
887  *
888  * Get the gap between resolution and w/h.
889  * res->width/height smaller than w/h wouldn't be considered.
890  * Returns the value of gap or -1 if fail.
891  */
892 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
893 static int distance(struct gc0310_resolution *res, u32 w, u32 h)
894 {
895 	unsigned int w_ratio = (res->width << 13) / w;
896 	unsigned int h_ratio;
897 	int match;
898 
899 	if (h == 0)
900 		return -1;
901 	h_ratio = (res->height << 13) / h;
902 	if (h_ratio == 0)
903 		return -1;
904 	match   = abs(((w_ratio << 13) / h_ratio) - 8192);
905 
906 	if ((w_ratio < 8192) || (h_ratio < 8192)  ||
907 		(match > LARGEST_ALLOWED_RATIO_MISMATCH))
908 		return -1;
909 
910 	return w_ratio + h_ratio;
911 }
912 
913 /* Return the nearest higher resolution index */
914 static int nearest_resolution_index(int w, int h)
915 {
916 	int i;
917 	int idx = -1;
918 	int dist;
919 	int min_dist = INT_MAX;
920 	struct gc0310_resolution *tmp_res = NULL;
921 
922 	for (i = 0; i < N_RES; i++) {
923 		tmp_res = &gc0310_res[i];
924 		dist = distance(tmp_res, w, h);
925 		if (dist == -1)
926 			continue;
927 		if (dist < min_dist) {
928 			min_dist = dist;
929 			idx = i;
930 		}
931 	}
932 
933 	return idx;
934 }
935 
936 static int get_resolution_index(int w, int h)
937 {
938 	int i;
939 
940 	for (i = 0; i < N_RES; i++) {
941 		if (w != gc0310_res[i].width)
942 			continue;
943 		if (h != gc0310_res[i].height)
944 			continue;
945 
946 		return i;
947 	}
948 
949 	return -1;
950 }
951 
952 /* TODO: remove it. */
953 static int startup(struct v4l2_subdev *sd)
954 {
955 	struct gc0310_device *dev = to_gc0310_sensor(sd);
956 	struct i2c_client *client = v4l2_get_subdevdata(sd);
957 	int ret = 0;
958 
959 	pr_info("%s S\n", __func__);
960 
961 	ret = gc0310_write_reg_array(client, gc0310_res[dev->fmt_idx].regs);
962 	if (ret) {
963 		dev_err(&client->dev, "gc0310 write register err.\n");
964 		return ret;
965 	}
966 
967 	pr_info("%s E\n", __func__);
968 	return ret;
969 }
970 
971 static int gc0310_set_fmt(struct v4l2_subdev *sd,
972 			  struct v4l2_subdev_pad_config *cfg,
973 			  struct v4l2_subdev_format *format)
974 {
975 	struct v4l2_mbus_framefmt *fmt = &format->format;
976 	struct gc0310_device *dev = to_gc0310_sensor(sd);
977 	struct i2c_client *client = v4l2_get_subdevdata(sd);
978 	struct camera_mipi_info *gc0310_info = NULL;
979 	int ret = 0;
980 	int idx = 0;
981 
982 	pr_info("%s S\n", __func__);
983 
984 	if (format->pad)
985 		return -EINVAL;
986 
987 	if (!fmt)
988 		return -EINVAL;
989 
990 	gc0310_info = v4l2_get_subdev_hostdata(sd);
991 	if (!gc0310_info)
992 		return -EINVAL;
993 
994 	mutex_lock(&dev->input_lock);
995 
996 	idx = nearest_resolution_index(fmt->width, fmt->height);
997 	if (idx == -1) {
998 		/* return the largest resolution */
999 		fmt->width = gc0310_res[N_RES - 1].width;
1000 		fmt->height = gc0310_res[N_RES - 1].height;
1001 	} else {
1002 		fmt->width = gc0310_res[idx].width;
1003 		fmt->height = gc0310_res[idx].height;
1004 	}
1005 	fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1006 
1007 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1008 		cfg->try_fmt = *fmt;
1009 		mutex_unlock(&dev->input_lock);
1010 		return 0;
1011 	}
1012 
1013 	dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1014 	if (dev->fmt_idx == -1) {
1015 		dev_err(&client->dev, "get resolution fail\n");
1016 		mutex_unlock(&dev->input_lock);
1017 		return -EINVAL;
1018 	}
1019 
1020 	printk("%s: before gc0310_write_reg_array %s\n", __func__,
1021 	       gc0310_res[dev->fmt_idx].desc);
1022 	ret = startup(sd);
1023 	if (ret) {
1024 		dev_err(&client->dev, "gc0310 startup err\n");
1025 		goto err;
1026 	}
1027 
1028 	ret = gc0310_get_intg_factor(client, gc0310_info,
1029 				     &gc0310_res[dev->fmt_idx]);
1030 	if (ret) {
1031 		dev_err(&client->dev, "failed to get integration_factor\n");
1032 		goto err;
1033 	}
1034 
1035 	pr_info("%s E\n", __func__);
1036 err:
1037 	mutex_unlock(&dev->input_lock);
1038 	return ret;
1039 }
1040 
1041 static int gc0310_get_fmt(struct v4l2_subdev *sd,
1042 			  struct v4l2_subdev_pad_config *cfg,
1043 			  struct v4l2_subdev_format *format)
1044 {
1045 	struct v4l2_mbus_framefmt *fmt = &format->format;
1046 	struct gc0310_device *dev = to_gc0310_sensor(sd);
1047 
1048 	if (format->pad)
1049 		return -EINVAL;
1050 
1051 	if (!fmt)
1052 		return -EINVAL;
1053 
1054 	fmt->width = gc0310_res[dev->fmt_idx].width;
1055 	fmt->height = gc0310_res[dev->fmt_idx].height;
1056 	fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1057 
1058 	return 0;
1059 }
1060 
1061 static int gc0310_detect(struct i2c_client *client)
1062 {
1063 	struct i2c_adapter *adapter = client->adapter;
1064 	u8 high, low;
1065 	int ret;
1066 	u16 id;
1067 
1068 	pr_info("%s S\n", __func__);
1069 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1070 		return -ENODEV;
1071 
1072 	ret = gc0310_read_reg(client, GC0310_8BIT,
1073 					GC0310_SC_CMMN_CHIP_ID_H, &high);
1074 	if (ret) {
1075 		dev_err(&client->dev, "read sensor_id_high failed\n");
1076 		return -ENODEV;
1077 	}
1078 	ret = gc0310_read_reg(client, GC0310_8BIT,
1079 					GC0310_SC_CMMN_CHIP_ID_L, &low);
1080 	if (ret) {
1081 		dev_err(&client->dev, "read sensor_id_low failed\n");
1082 		return -ENODEV;
1083 	}
1084 	id = ((((u16)high) << 8) | (u16)low);
1085 	pr_info("sensor ID = 0x%x\n", id);
1086 
1087 	if (id != GC0310_ID) {
1088 		dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id, GC0310_ID);
1089 		return -ENODEV;
1090 	}
1091 
1092 	dev_dbg(&client->dev, "detect gc0310 success\n");
1093 
1094 	pr_info("%s E\n", __func__);
1095 
1096 	return 0;
1097 }
1098 
1099 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable)
1100 {
1101 	struct gc0310_device *dev = to_gc0310_sensor(sd);
1102 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1103 	int ret;
1104 
1105 	pr_info("%s S enable=%d\n", __func__, enable);
1106 	mutex_lock(&dev->input_lock);
1107 
1108 	if (enable) {
1109 		/* enable per frame MIPI and sensor ctrl reset  */
1110 		ret = gc0310_write_reg(client, GC0310_8BIT,
1111 						0xFE, 0x30);
1112 		if (ret) {
1113 			mutex_unlock(&dev->input_lock);
1114 			return ret;
1115 		}
1116 	}
1117 
1118 	ret = gc0310_write_reg(client, GC0310_8BIT,
1119 				GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3);
1120 	if (ret) {
1121 		mutex_unlock(&dev->input_lock);
1122 		return ret;
1123 	}
1124 
1125 	ret = gc0310_write_reg(client, GC0310_8BIT, GC0310_SW_STREAM,
1126 				enable ? GC0310_START_STREAMING :
1127 				GC0310_STOP_STREAMING);
1128 	if (ret) {
1129 		mutex_unlock(&dev->input_lock);
1130 		return ret;
1131 	}
1132 
1133 	ret = gc0310_write_reg(client, GC0310_8BIT,
1134 				GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0);
1135 	if (ret) {
1136 		mutex_unlock(&dev->input_lock);
1137 		return ret;
1138 	}
1139 
1140 	mutex_unlock(&dev->input_lock);
1141 	pr_info("%s E\n", __func__);
1142 	return ret;
1143 }
1144 
1145 static int gc0310_s_config(struct v4l2_subdev *sd,
1146 			   int irq, void *platform_data)
1147 {
1148 	struct gc0310_device *dev = to_gc0310_sensor(sd);
1149 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1150 	int ret = 0;
1151 
1152 	pr_info("%s S\n", __func__);
1153 	if (!platform_data)
1154 		return -ENODEV;
1155 
1156 	dev->platform_data =
1157 		(struct camera_sensor_platform_data *)platform_data;
1158 
1159 	mutex_lock(&dev->input_lock);
1160 	/* power off the module, then power on it in future
1161 	 * as first power on by board may not fulfill the
1162 	 * power on sequqence needed by the module
1163 	 */
1164 	ret = power_down(sd);
1165 	if (ret) {
1166 		dev_err(&client->dev, "gc0310 power-off err.\n");
1167 		goto fail_power_off;
1168 	}
1169 
1170 	ret = power_up(sd);
1171 	if (ret) {
1172 		dev_err(&client->dev, "gc0310 power-up err.\n");
1173 		goto fail_power_on;
1174 	}
1175 
1176 	ret = dev->platform_data->csi_cfg(sd, 1);
1177 	if (ret)
1178 		goto fail_csi_cfg;
1179 
1180 	/* config & detect sensor */
1181 	ret = gc0310_detect(client);
1182 	if (ret) {
1183 		dev_err(&client->dev, "gc0310_detect err s_config.\n");
1184 		goto fail_csi_cfg;
1185 	}
1186 
1187 	/* turn off sensor, after probed */
1188 	ret = power_down(sd);
1189 	if (ret) {
1190 		dev_err(&client->dev, "gc0310 power-off err.\n");
1191 		goto fail_csi_cfg;
1192 	}
1193 	mutex_unlock(&dev->input_lock);
1194 
1195 	pr_info("%s E\n", __func__);
1196 	return 0;
1197 
1198 fail_csi_cfg:
1199 	dev->platform_data->csi_cfg(sd, 0);
1200 fail_power_on:
1201 	power_down(sd);
1202 	dev_err(&client->dev, "sensor power-gating failed\n");
1203 fail_power_off:
1204 	mutex_unlock(&dev->input_lock);
1205 	return ret;
1206 }
1207 
1208 static int gc0310_g_frame_interval(struct v4l2_subdev *sd,
1209 				   struct v4l2_subdev_frame_interval *interval)
1210 {
1211 	struct gc0310_device *dev = to_gc0310_sensor(sd);
1212 
1213 	interval->interval.numerator = 1;
1214 	interval->interval.denominator = gc0310_res[dev->fmt_idx].fps;
1215 
1216 	return 0;
1217 }
1218 
1219 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd,
1220 				 struct v4l2_subdev_pad_config *cfg,
1221 				 struct v4l2_subdev_mbus_code_enum *code)
1222 {
1223 	if (code->index >= MAX_FMTS)
1224 		return -EINVAL;
1225 
1226 	code->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1227 	return 0;
1228 }
1229 
1230 static int gc0310_enum_frame_size(struct v4l2_subdev *sd,
1231 				  struct v4l2_subdev_pad_config *cfg,
1232 				  struct v4l2_subdev_frame_size_enum *fse)
1233 {
1234 	int index = fse->index;
1235 
1236 	if (index >= N_RES)
1237 		return -EINVAL;
1238 
1239 	fse->min_width = gc0310_res[index].width;
1240 	fse->min_height = gc0310_res[index].height;
1241 	fse->max_width = gc0310_res[index].width;
1242 	fse->max_height = gc0310_res[index].height;
1243 
1244 	return 0;
1245 }
1246 
1247 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1248 {
1249 	struct gc0310_device *dev = to_gc0310_sensor(sd);
1250 
1251 	mutex_lock(&dev->input_lock);
1252 	*frames = gc0310_res[dev->fmt_idx].skip_frames;
1253 	mutex_unlock(&dev->input_lock);
1254 
1255 	return 0;
1256 }
1257 
1258 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = {
1259 	.g_skip_frames	= gc0310_g_skip_frames,
1260 };
1261 
1262 static const struct v4l2_subdev_video_ops gc0310_video_ops = {
1263 	.s_stream = gc0310_s_stream,
1264 	.g_frame_interval = gc0310_g_frame_interval,
1265 };
1266 
1267 static const struct v4l2_subdev_core_ops gc0310_core_ops = {
1268 	.s_power = gc0310_s_power,
1269 	.ioctl = gc0310_ioctl,
1270 };
1271 
1272 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = {
1273 	.enum_mbus_code = gc0310_enum_mbus_code,
1274 	.enum_frame_size = gc0310_enum_frame_size,
1275 	.get_fmt = gc0310_get_fmt,
1276 	.set_fmt = gc0310_set_fmt,
1277 };
1278 
1279 static const struct v4l2_subdev_ops gc0310_ops = {
1280 	.core = &gc0310_core_ops,
1281 	.video = &gc0310_video_ops,
1282 	.pad = &gc0310_pad_ops,
1283 	.sensor = &gc0310_sensor_ops,
1284 };
1285 
1286 static int gc0310_remove(struct i2c_client *client)
1287 {
1288 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1289 	struct gc0310_device *dev = to_gc0310_sensor(sd);
1290 
1291 	dev_dbg(&client->dev, "gc0310_remove...\n");
1292 
1293 	dev->platform_data->csi_cfg(sd, 0);
1294 
1295 	v4l2_device_unregister_subdev(sd);
1296 	media_entity_cleanup(&dev->sd.entity);
1297 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1298 	kfree(dev);
1299 
1300 	return 0;
1301 }
1302 
1303 static int gc0310_probe(struct i2c_client *client)
1304 {
1305 	struct gc0310_device *dev;
1306 	int ret;
1307 	void *pdata;
1308 	unsigned int i;
1309 
1310 	pr_info("%s S\n", __func__);
1311 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1312 	if (!dev)
1313 		return -ENOMEM;
1314 
1315 	mutex_init(&dev->input_lock);
1316 
1317 	dev->fmt_idx = 0;
1318 	v4l2_i2c_subdev_init(&dev->sd, client, &gc0310_ops);
1319 
1320 	pdata = gmin_camera_platform_data(&dev->sd,
1321 					  ATOMISP_INPUT_FORMAT_RAW_8,
1322 					  atomisp_bayer_order_grbg);
1323 	if (!pdata) {
1324 		ret = -EINVAL;
1325 		goto out_free;
1326 	}
1327 
1328 	ret = gc0310_s_config(&dev->sd, client->irq, pdata);
1329 	if (ret)
1330 		goto out_free;
1331 
1332 	ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1333 	if (ret)
1334 		goto out_free;
1335 
1336 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1337 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1338 	dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8;
1339 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1340 	ret =
1341 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1342 				   ARRAY_SIZE(gc0310_controls));
1343 	if (ret) {
1344 		gc0310_remove(client);
1345 		return ret;
1346 	}
1347 
1348 	for (i = 0; i < ARRAY_SIZE(gc0310_controls); i++)
1349 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc0310_controls[i],
1350 				     NULL);
1351 
1352 	if (dev->ctrl_handler.error) {
1353 		gc0310_remove(client);
1354 		return dev->ctrl_handler.error;
1355 	}
1356 
1357 	/* Use same lock for controls as for everything else. */
1358 	dev->ctrl_handler.lock = &dev->input_lock;
1359 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1360 
1361 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1362 	if (ret)
1363 		gc0310_remove(client);
1364 
1365 	pr_info("%s E\n", __func__);
1366 	return ret;
1367 out_free:
1368 	v4l2_device_unregister_subdev(&dev->sd);
1369 	kfree(dev);
1370 	return ret;
1371 }
1372 
1373 static const struct acpi_device_id gc0310_acpi_match[] = {
1374 	{"XXGC0310"},
1375 	{"INT0310"},
1376 	{},
1377 };
1378 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match);
1379 
1380 static struct i2c_driver gc0310_driver = {
1381 	.driver = {
1382 		.name = "gc0310",
1383 		.acpi_match_table = gc0310_acpi_match,
1384 	},
1385 	.probe_new = gc0310_probe,
1386 	.remove = gc0310_remove,
1387 };
1388 module_i2c_driver(gc0310_driver);
1389 
1390 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>");
1391 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors");
1392 MODULE_LICENSE("GPL");
1393