1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for GalaxyCore GC0310 VGA 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 <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/mm.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/moduleparam.h>
31 #include <media/v4l2-device.h>
32 #include <linux/io.h>
33 #include "../include/linux/atomisp_gmin_platform.h"
34 
35 #include "gc0310.h"
36 
37 /* i2c read/write stuff */
38 static int gc0310_read_reg(struct i2c_client *client,
39 			   u16 data_length, u8 reg, u8 *val)
40 {
41 	int err;
42 	struct i2c_msg msg[2];
43 	unsigned char data[1];
44 
45 	if (!client->adapter) {
46 		dev_err(&client->dev, "%s error, no client->adapter\n",
47 			__func__);
48 		return -ENODEV;
49 	}
50 
51 	if (data_length != GC0310_8BIT) {
52 		dev_err(&client->dev, "%s error, invalid data length\n",
53 			__func__);
54 		return -EINVAL;
55 	}
56 
57 	memset(msg, 0, sizeof(msg));
58 
59 	msg[0].addr = client->addr;
60 	msg[0].flags = 0;
61 	msg[0].len = I2C_MSG_LENGTH;
62 	msg[0].buf = data;
63 
64 	/* high byte goes out first */
65 	data[0] = (u8)(reg & 0xff);
66 
67 	msg[1].addr = client->addr;
68 	msg[1].len = data_length;
69 	msg[1].flags = I2C_M_RD;
70 	msg[1].buf = data;
71 
72 	err = i2c_transfer(client->adapter, msg, 2);
73 	if (err != 2) {
74 		if (err >= 0)
75 			err = -EIO;
76 		dev_err(&client->dev,
77 			"read from offset 0x%x error %d", reg, err);
78 		return err;
79 	}
80 
81 	*val = 0;
82 	/* high byte comes first */
83 	if (data_length == GC0310_8BIT)
84 		*val = (u8)data[0];
85 
86 	return 0;
87 }
88 
89 static int gc0310_i2c_write(struct i2c_client *client, u16 len, u8 *data)
90 {
91 	struct i2c_msg msg;
92 	const int num_msg = 1;
93 	int ret;
94 
95 	msg.addr = client->addr;
96 	msg.flags = 0;
97 	msg.len = len;
98 	msg.buf = data;
99 	ret = i2c_transfer(client->adapter, &msg, 1);
100 
101 	return ret == num_msg ? 0 : -EIO;
102 }
103 
104 static int gc0310_write_reg(struct i2c_client *client, u16 data_length,
105 			    u8 reg, u8 val)
106 {
107 	int ret;
108 	unsigned char data[2] = {0};
109 	u8 *wreg = (u8 *)data;
110 	const u16 len = data_length + sizeof(u8); /* 8-bit address + data */
111 
112 	if (data_length != GC0310_8BIT) {
113 		dev_err(&client->dev,
114 			"%s error, invalid data_length\n", __func__);
115 		return -EINVAL;
116 	}
117 
118 	/* high byte goes out first */
119 	*wreg = (u8)(reg & 0xff);
120 
121 	if (data_length == GC0310_8BIT)
122 		data[1] = (u8)(val);
123 
124 	ret = gc0310_i2c_write(client, len, data);
125 	if (ret)
126 		dev_err(&client->dev,
127 			"write error: wrote 0x%x to offset 0x%x error %d",
128 			val, reg, ret);
129 
130 	return ret;
131 }
132 
133 /*
134  * gc0310_write_reg_array - Initializes a list of GC0310 registers
135  * @client: i2c driver client structure
136  * @reglist: list of registers to be written
137  *
138  * This function initializes a list of registers. When consecutive addresses
139  * are found in a row on the list, this function creates a buffer and sends
140  * consecutive data in a single i2c_transfer().
141  *
142  * __gc0310_flush_reg_array, __gc0310_buf_reg_array() and
143  * __gc0310_write_reg_is_consecutive() are internal functions to
144  * gc0310_write_reg_array_fast() and should be not used anywhere else.
145  *
146  */
147 
148 static int __gc0310_flush_reg_array(struct i2c_client *client,
149 				    struct gc0310_write_ctrl *ctrl)
150 {
151 	u16 size;
152 
153 	if (ctrl->index == 0)
154 		return 0;
155 
156 	size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
157 	ctrl->buffer.addr = (u8)(ctrl->buffer.addr);
158 	ctrl->index = 0;
159 
160 	return gc0310_i2c_write(client, size, (u8 *)&ctrl->buffer);
161 }
162 
163 static int __gc0310_buf_reg_array(struct i2c_client *client,
164 				  struct gc0310_write_ctrl *ctrl,
165 				  const struct gc0310_reg *next)
166 {
167 	int size;
168 
169 	switch (next->type) {
170 	case GC0310_8BIT:
171 		size = 1;
172 		ctrl->buffer.data[ctrl->index] = (u8)next->val;
173 		break;
174 	default:
175 		return -EINVAL;
176 	}
177 
178 	/* When first item is added, we need to store its starting address */
179 	if (ctrl->index == 0)
180 		ctrl->buffer.addr = next->reg;
181 
182 	ctrl->index += size;
183 
184 	/*
185 	 * Buffer cannot guarantee free space for u32? Better flush it to avoid
186 	 * possible lack of memory for next item.
187 	 */
188 	if (ctrl->index + sizeof(u8) >= GC0310_MAX_WRITE_BUF_SIZE)
189 		return __gc0310_flush_reg_array(client, ctrl);
190 
191 	return 0;
192 }
193 
194 static int __gc0310_write_reg_is_consecutive(struct i2c_client *client,
195 					     struct gc0310_write_ctrl *ctrl,
196 					     const struct gc0310_reg *next)
197 {
198 	if (ctrl->index == 0)
199 		return 1;
200 
201 	return ctrl->buffer.addr + ctrl->index == next->reg;
202 }
203 
204 static int gc0310_write_reg_array(struct i2c_client *client,
205 				  const struct gc0310_reg *reglist)
206 {
207 	const struct gc0310_reg *next = reglist;
208 	struct gc0310_write_ctrl ctrl;
209 	int err;
210 
211 	ctrl.index = 0;
212 	for (; next->type != GC0310_TOK_TERM; next++) {
213 		switch (next->type & GC0310_TOK_MASK) {
214 		case GC0310_TOK_DELAY:
215 			err = __gc0310_flush_reg_array(client, &ctrl);
216 			if (err)
217 				return err;
218 			msleep(next->val);
219 			break;
220 		default:
221 			/*
222 			 * If next address is not consecutive, data needs to be
223 			 * flushed before proceed.
224 			 */
225 			if (!__gc0310_write_reg_is_consecutive(client, &ctrl,
226 							       next)) {
227 				err = __gc0310_flush_reg_array(client, &ctrl);
228 				if (err)
229 					return err;
230 			}
231 			err = __gc0310_buf_reg_array(client, &ctrl, next);
232 			if (err) {
233 				dev_err(&client->dev, "%s: write error, aborted\n",
234 					__func__);
235 				return err;
236 			}
237 			break;
238 		}
239 	}
240 
241 	return __gc0310_flush_reg_array(client, &ctrl);
242 }
243 
244 static int gc0310_set_gain(struct v4l2_subdev *sd, int gain)
245 
246 {
247 	struct i2c_client *client = v4l2_get_subdevdata(sd);
248 	int ret;
249 	u8 again, dgain;
250 
251 	if (gain < 0x20)
252 		gain = 0x20;
253 	if (gain > 0x80)
254 		gain = 0x80;
255 
256 	if (gain >= 0x20 && gain < 0x40) {
257 		again = 0x0; /* sqrt(2) */
258 		dgain = gain;
259 	} else {
260 		again = 0x2; /* 2 * sqrt(2) */
261 		dgain = gain / 2;
262 	}
263 
264 	dev_dbg(&client->dev, "gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain);
265 
266 	/* set analog gain */
267 	ret = gc0310_write_reg(client, GC0310_8BIT,
268 			       GC0310_AGC_ADJ, again);
269 	if (ret)
270 		return ret;
271 
272 	/* set digital gain */
273 	ret = gc0310_write_reg(client, GC0310_8BIT,
274 			       GC0310_DGC_ADJ, dgain);
275 	if (ret)
276 		return ret;
277 
278 	return 0;
279 }
280 
281 static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
282 				 int gain, int digitgain)
283 
284 {
285 	struct i2c_client *client = v4l2_get_subdevdata(sd);
286 	int ret;
287 
288 	dev_dbg(&client->dev, "coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain);
289 
290 	/* set exposure */
291 	ret = gc0310_write_reg(client, GC0310_8BIT,
292 			       GC0310_AEC_PK_EXPO_L,
293 			       coarse_itg & 0xff);
294 	if (ret)
295 		return ret;
296 
297 	ret = gc0310_write_reg(client, GC0310_8BIT,
298 			       GC0310_AEC_PK_EXPO_H,
299 			       (coarse_itg >> 8) & 0x0f);
300 	if (ret)
301 		return ret;
302 
303 	ret = gc0310_set_gain(sd, gain);
304 	if (ret)
305 		return ret;
306 
307 	return ret;
308 }
309 
310 static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure,
311 			       int gain, int digitgain)
312 {
313 	struct gc0310_device *dev = to_gc0310_sensor(sd);
314 	int ret;
315 
316 	mutex_lock(&dev->input_lock);
317 	ret = __gc0310_set_exposure(sd, exposure, gain, digitgain);
318 	mutex_unlock(&dev->input_lock);
319 
320 	return ret;
321 }
322 
323 static long gc0310_s_exposure(struct v4l2_subdev *sd,
324 			      struct atomisp_exposure *exposure)
325 {
326 	int exp = exposure->integration_time[0];
327 	int gain = exposure->gain[0];
328 	int digitgain = exposure->gain[1];
329 
330 	/* we should not accept the invalid value below. */
331 	if (gain == 0) {
332 		struct i2c_client *client = v4l2_get_subdevdata(sd);
333 
334 		v4l2_err(client, "%s: invalid value\n", __func__);
335 		return -EINVAL;
336 	}
337 
338 	return gc0310_set_exposure(sd, exp, gain, digitgain);
339 }
340 
341 /* TO DO */
342 static int gc0310_v_flip(struct v4l2_subdev *sd, s32 value)
343 {
344 	return 0;
345 }
346 
347 /* TO DO */
348 static int gc0310_h_flip(struct v4l2_subdev *sd, s32 value)
349 {
350 	return 0;
351 }
352 
353 static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
354 {
355 	switch (cmd) {
356 	case ATOMISP_IOC_S_EXPOSURE:
357 		return gc0310_s_exposure(sd, arg);
358 	default:
359 		return -EINVAL;
360 	}
361 	return 0;
362 }
363 
364 /* This returns the exposure time being used. This should only be used
365  * for filling in EXIF data, not for actual image processing.
366  */
367 static int gc0310_q_exposure(struct v4l2_subdev *sd, s32 *value)
368 {
369 	struct i2c_client *client = v4l2_get_subdevdata(sd);
370 	u8 reg_v;
371 	int ret;
372 
373 	/* get exposure */
374 	ret = gc0310_read_reg(client, GC0310_8BIT,
375 			      GC0310_AEC_PK_EXPO_L,
376 			      &reg_v);
377 	if (ret)
378 		goto err;
379 
380 	*value = reg_v;
381 	ret = gc0310_read_reg(client, GC0310_8BIT,
382 			      GC0310_AEC_PK_EXPO_H,
383 			      &reg_v);
384 	if (ret)
385 		goto err;
386 
387 	*value = *value + (reg_v << 8);
388 err:
389 	return ret;
390 }
391 
392 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl)
393 {
394 	struct gc0310_device *dev =
395 	    container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
396 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
397 	int ret = 0;
398 
399 	switch (ctrl->id) {
400 	case V4L2_CID_VFLIP:
401 		dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
402 			__func__, ctrl->val);
403 		ret = gc0310_v_flip(&dev->sd, ctrl->val);
404 		break;
405 	case V4L2_CID_HFLIP:
406 		dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
407 			__func__, ctrl->val);
408 		ret = gc0310_h_flip(&dev->sd, ctrl->val);
409 		break;
410 	default:
411 		ret = -EINVAL;
412 	}
413 	return ret;
414 }
415 
416 static int gc0310_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
417 {
418 	struct gc0310_device *dev =
419 	    container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
420 	int ret = 0;
421 
422 	switch (ctrl->id) {
423 	case V4L2_CID_EXPOSURE_ABSOLUTE:
424 		ret = gc0310_q_exposure(&dev->sd, &ctrl->val);
425 		break;
426 	default:
427 		ret = -EINVAL;
428 	}
429 
430 	return ret;
431 }
432 
433 static const struct v4l2_ctrl_ops ctrl_ops = {
434 	.s_ctrl = gc0310_s_ctrl,
435 	.g_volatile_ctrl = gc0310_g_volatile_ctrl
436 };
437 
438 static const struct v4l2_ctrl_config gc0310_controls[] = {
439 	{
440 		.ops = &ctrl_ops,
441 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
442 		.type = V4L2_CTRL_TYPE_INTEGER,
443 		.name = "exposure",
444 		.min = 0x0,
445 		.max = 0xffff,
446 		.step = 0x01,
447 		.def = 0x00,
448 		.flags = 0,
449 	},
450 	{
451 		.ops = &ctrl_ops,
452 		.id = V4L2_CID_VFLIP,
453 		.type = V4L2_CTRL_TYPE_BOOLEAN,
454 		.name = "Flip",
455 		.min = 0,
456 		.max = 1,
457 		.step = 1,
458 		.def = 0,
459 	},
460 	{
461 		.ops = &ctrl_ops,
462 		.id = V4L2_CID_HFLIP,
463 		.type = V4L2_CTRL_TYPE_BOOLEAN,
464 		.name = "Mirror",
465 		.min = 0,
466 		.max = 1,
467 		.step = 1,
468 		.def = 0,
469 	},
470 };
471 
472 static int gc0310_init(struct v4l2_subdev *sd)
473 {
474 	int ret;
475 	struct i2c_client *client = v4l2_get_subdevdata(sd);
476 	struct gc0310_device *dev = to_gc0310_sensor(sd);
477 
478 	mutex_lock(&dev->input_lock);
479 
480 	/* set initial registers */
481 	ret  = gc0310_write_reg_array(client, gc0310_reset_register);
482 
483 	/* restore settings */
484 	gc0310_res = gc0310_res_preview;
485 	N_RES = N_RES_PREVIEW;
486 
487 	mutex_unlock(&dev->input_lock);
488 
489 	return ret;
490 }
491 
492 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
493 {
494 	int ret = 0;
495 	struct gc0310_device *dev = to_gc0310_sensor(sd);
496 
497 	if (!dev || !dev->platform_data)
498 		return -ENODEV;
499 
500 	if (flag) {
501 		/* The upstream module driver (written to Crystal
502 		 * Cove) had this logic to pulse the rails low first.
503 		 * This appears to break things on the MRD7 with the
504 		 * X-Powers PMIC...
505 		 *
506 		 *     ret = dev->platform_data->v1p8_ctrl(sd, 0);
507 		 *     ret |= dev->platform_data->v2p8_ctrl(sd, 0);
508 		 *     mdelay(50);
509 		 */
510 		ret |= dev->platform_data->v1p8_ctrl(sd, 1);
511 		ret |= dev->platform_data->v2p8_ctrl(sd, 1);
512 		usleep_range(10000, 15000);
513 	}
514 
515 	if (!flag || ret) {
516 		ret |= dev->platform_data->v1p8_ctrl(sd, 0);
517 		ret |= dev->platform_data->v2p8_ctrl(sd, 0);
518 	}
519 	return ret;
520 }
521 
522 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
523 {
524 	int ret;
525 	struct gc0310_device *dev = to_gc0310_sensor(sd);
526 
527 	if (!dev || !dev->platform_data)
528 		return -ENODEV;
529 
530 	/* GPIO0 == "reset" (active low), GPIO1 == "power down" */
531 	if (flag) {
532 		/* Pulse reset, then release power down */
533 		ret = dev->platform_data->gpio0_ctrl(sd, 0);
534 		usleep_range(5000, 10000);
535 		ret |= dev->platform_data->gpio0_ctrl(sd, 1);
536 		usleep_range(10000, 15000);
537 		ret |= dev->platform_data->gpio1_ctrl(sd, 0);
538 		usleep_range(10000, 15000);
539 	} else {
540 		ret = dev->platform_data->gpio1_ctrl(sd, 1);
541 		ret |= dev->platform_data->gpio0_ctrl(sd, 0);
542 	}
543 	return ret;
544 }
545 
546 static int power_up(struct v4l2_subdev *sd)
547 {
548 	struct gc0310_device *dev = to_gc0310_sensor(sd);
549 	struct i2c_client *client = v4l2_get_subdevdata(sd);
550 	int ret;
551 
552 	if (!dev->platform_data) {
553 		dev_err(&client->dev,
554 			"no camera_sensor_platform_data");
555 		return -ENODEV;
556 	}
557 
558 	if (dev->power_on)
559 		return 0; /* Already on */
560 
561 	/* power control */
562 	ret = power_ctrl(sd, 1);
563 	if (ret)
564 		goto fail_power;
565 
566 	/* flis clock control */
567 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
568 	if (ret)
569 		goto fail_clk;
570 
571 	/* gpio ctrl */
572 	ret = gpio_ctrl(sd, 1);
573 	if (ret) {
574 		ret = gpio_ctrl(sd, 1);
575 		if (ret)
576 			goto fail_gpio;
577 	}
578 
579 	msleep(100);
580 
581 	dev->power_on = true;
582 	return 0;
583 
584 fail_gpio:
585 	dev->platform_data->flisclk_ctrl(sd, 0);
586 fail_clk:
587 	power_ctrl(sd, 0);
588 fail_power:
589 	dev_err(&client->dev, "sensor power-up failed\n");
590 
591 	return ret;
592 }
593 
594 static int power_down(struct v4l2_subdev *sd)
595 {
596 	struct gc0310_device *dev = to_gc0310_sensor(sd);
597 	struct i2c_client *client = v4l2_get_subdevdata(sd);
598 	int ret = 0;
599 
600 	if (!dev->platform_data) {
601 		dev_err(&client->dev,
602 			"no camera_sensor_platform_data");
603 		return -ENODEV;
604 	}
605 
606 	if (!dev->power_on)
607 		return 0; /* Already off */
608 
609 	/* gpio ctrl */
610 	ret = gpio_ctrl(sd, 0);
611 	if (ret) {
612 		ret = gpio_ctrl(sd, 0);
613 		if (ret)
614 			dev_err(&client->dev, "gpio failed 2\n");
615 	}
616 
617 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
618 	if (ret)
619 		dev_err(&client->dev, "flisclk failed\n");
620 
621 	/* power control */
622 	ret = power_ctrl(sd, 0);
623 	if (ret)
624 		dev_err(&client->dev, "vprog failed.\n");
625 
626 	dev->power_on = false;
627 	return ret;
628 }
629 
630 static int gc0310_s_power(struct v4l2_subdev *sd, int on)
631 {
632 	int ret;
633 
634 	if (on == 0)
635 		return power_down(sd);
636 
637 	ret = power_up(sd);
638 	if (ret)
639 		return ret;
640 
641 	return gc0310_init(sd);
642 }
643 
644 /* TODO: remove it. */
645 static int startup(struct v4l2_subdev *sd)
646 {
647 	struct gc0310_device *dev = to_gc0310_sensor(sd);
648 	struct i2c_client *client = v4l2_get_subdevdata(sd);
649 	int ret = 0;
650 
651 	ret = gc0310_write_reg_array(client, dev->res->regs);
652 	if (ret) {
653 		dev_err(&client->dev, "gc0310 write register err.\n");
654 		return ret;
655 	}
656 
657 	return ret;
658 }
659 
660 static int gc0310_set_fmt(struct v4l2_subdev *sd,
661 			  struct v4l2_subdev_state *sd_state,
662 			  struct v4l2_subdev_format *format)
663 {
664 	struct v4l2_mbus_framefmt *fmt = &format->format;
665 	struct gc0310_device *dev = to_gc0310_sensor(sd);
666 	struct i2c_client *client = v4l2_get_subdevdata(sd);
667 	struct camera_mipi_info *gc0310_info = NULL;
668 	struct gc0310_resolution *res;
669 	int ret = 0;
670 
671 	if (format->pad)
672 		return -EINVAL;
673 
674 	if (!fmt)
675 		return -EINVAL;
676 
677 	gc0310_info = v4l2_get_subdev_hostdata(sd);
678 	if (!gc0310_info)
679 		return -EINVAL;
680 
681 	mutex_lock(&dev->input_lock);
682 
683 	res = v4l2_find_nearest_size(gc0310_res_preview,
684 				     ARRAY_SIZE(gc0310_res_preview), width,
685 				     height, fmt->width, fmt->height);
686 	if (!res)
687 		res = &gc0310_res_preview[N_RES - 1];
688 
689 	fmt->width = res->width;
690 	fmt->height = res->height;
691 	dev->res = res;
692 
693 	fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
694 
695 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
696 		sd_state->pads->try_fmt = *fmt;
697 		mutex_unlock(&dev->input_lock);
698 		return 0;
699 	}
700 
701 	/* s_power has not been called yet for std v4l2 clients (camorama) */
702 	power_up(sd);
703 
704 	dev_dbg(&client->dev, "%s: before gc0310_write_reg_array %s\n",
705 		__func__, dev->res->desc);
706 	ret = startup(sd);
707 	if (ret) {
708 		dev_err(&client->dev, "gc0310 startup err\n");
709 		goto err;
710 	}
711 
712 err:
713 	mutex_unlock(&dev->input_lock);
714 	return ret;
715 }
716 
717 static int gc0310_get_fmt(struct v4l2_subdev *sd,
718 			  struct v4l2_subdev_state *sd_state,
719 			  struct v4l2_subdev_format *format)
720 {
721 	struct v4l2_mbus_framefmt *fmt = &format->format;
722 	struct gc0310_device *dev = to_gc0310_sensor(sd);
723 
724 	if (format->pad)
725 		return -EINVAL;
726 
727 	if (!fmt)
728 		return -EINVAL;
729 
730 	fmt->width = dev->res->width;
731 	fmt->height = dev->res->height;
732 	fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
733 
734 	return 0;
735 }
736 
737 static int gc0310_detect(struct i2c_client *client)
738 {
739 	struct i2c_adapter *adapter = client->adapter;
740 	u8 high, low;
741 	int ret;
742 	u16 id;
743 
744 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
745 		return -ENODEV;
746 
747 	ret = gc0310_read_reg(client, GC0310_8BIT,
748 			      GC0310_SC_CMMN_CHIP_ID_H, &high);
749 	if (ret) {
750 		dev_err(&client->dev, "read sensor_id_high failed\n");
751 		return -ENODEV;
752 	}
753 	ret = gc0310_read_reg(client, GC0310_8BIT,
754 			      GC0310_SC_CMMN_CHIP_ID_L, &low);
755 	if (ret) {
756 		dev_err(&client->dev, "read sensor_id_low failed\n");
757 		return -ENODEV;
758 	}
759 	id = ((((u16)high) << 8) | (u16)low);
760 	dev_dbg(&client->dev, "sensor ID = 0x%x\n", id);
761 
762 	if (id != GC0310_ID) {
763 		dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id,
764 			GC0310_ID);
765 		return -ENODEV;
766 	}
767 
768 	dev_dbg(&client->dev, "detect gc0310 success\n");
769 
770 	return 0;
771 }
772 
773 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable)
774 {
775 	struct gc0310_device *dev = to_gc0310_sensor(sd);
776 	struct i2c_client *client = v4l2_get_subdevdata(sd);
777 	int ret;
778 
779 	dev_dbg(&client->dev, "%s S enable=%d\n", __func__, enable);
780 	mutex_lock(&dev->input_lock);
781 
782 	if (enable) {
783 		/* enable per frame MIPI and sensor ctrl reset  */
784 		ret = gc0310_write_reg(client, GC0310_8BIT,
785 				       0xFE, 0x30);
786 		if (ret) {
787 			mutex_unlock(&dev->input_lock);
788 			return ret;
789 		}
790 	}
791 
792 	ret = gc0310_write_reg(client, GC0310_8BIT,
793 			       GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3);
794 	if (ret) {
795 		mutex_unlock(&dev->input_lock);
796 		return ret;
797 	}
798 
799 	ret = gc0310_write_reg(client, GC0310_8BIT, GC0310_SW_STREAM,
800 			       enable ? GC0310_START_STREAMING :
801 			       GC0310_STOP_STREAMING);
802 	if (ret) {
803 		mutex_unlock(&dev->input_lock);
804 		return ret;
805 	}
806 
807 	ret = gc0310_write_reg(client, GC0310_8BIT,
808 			       GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0);
809 	if (ret) {
810 		mutex_unlock(&dev->input_lock);
811 		return ret;
812 	}
813 
814 	mutex_unlock(&dev->input_lock);
815 	return ret;
816 }
817 
818 static int gc0310_s_config(struct v4l2_subdev *sd,
819 			   int irq, void *platform_data)
820 {
821 	struct gc0310_device *dev = to_gc0310_sensor(sd);
822 	struct i2c_client *client = v4l2_get_subdevdata(sd);
823 	int ret = 0;
824 
825 	if (!platform_data)
826 		return -ENODEV;
827 
828 	dev->platform_data =
829 	    (struct camera_sensor_platform_data *)platform_data;
830 
831 	mutex_lock(&dev->input_lock);
832 	/* power off the module, then power on it in future
833 	 * as first power on by board may not fulfill the
834 	 * power on sequqence needed by the module
835 	 */
836 	dev->power_on = true; /* force power_down() to run */
837 	ret = power_down(sd);
838 	if (ret) {
839 		dev_err(&client->dev, "gc0310 power-off err.\n");
840 		goto fail_power_off;
841 	}
842 
843 	ret = power_up(sd);
844 	if (ret) {
845 		dev_err(&client->dev, "gc0310 power-up err.\n");
846 		goto fail_power_on;
847 	}
848 
849 	ret = dev->platform_data->csi_cfg(sd, 1);
850 	if (ret)
851 		goto fail_csi_cfg;
852 
853 	/* config & detect sensor */
854 	ret = gc0310_detect(client);
855 	if (ret) {
856 		dev_err(&client->dev, "gc0310_detect err s_config.\n");
857 		goto fail_csi_cfg;
858 	}
859 
860 	/* turn off sensor, after probed */
861 	ret = power_down(sd);
862 	if (ret) {
863 		dev_err(&client->dev, "gc0310 power-off err.\n");
864 		goto fail_csi_cfg;
865 	}
866 	mutex_unlock(&dev->input_lock);
867 
868 	return 0;
869 
870 fail_csi_cfg:
871 	dev->platform_data->csi_cfg(sd, 0);
872 fail_power_on:
873 	power_down(sd);
874 	dev_err(&client->dev, "sensor power-gating failed\n");
875 fail_power_off:
876 	mutex_unlock(&dev->input_lock);
877 	return ret;
878 }
879 
880 static int gc0310_g_frame_interval(struct v4l2_subdev *sd,
881 				   struct v4l2_subdev_frame_interval *interval)
882 {
883 	struct gc0310_device *dev = to_gc0310_sensor(sd);
884 
885 	interval->interval.numerator = 1;
886 	interval->interval.denominator = dev->res->fps;
887 
888 	return 0;
889 }
890 
891 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd,
892 				 struct v4l2_subdev_state *sd_state,
893 				 struct v4l2_subdev_mbus_code_enum *code)
894 {
895 	if (code->index >= MAX_FMTS)
896 		return -EINVAL;
897 
898 	code->code = MEDIA_BUS_FMT_SGRBG8_1X8;
899 	return 0;
900 }
901 
902 static int gc0310_enum_frame_size(struct v4l2_subdev *sd,
903 				  struct v4l2_subdev_state *sd_state,
904 				  struct v4l2_subdev_frame_size_enum *fse)
905 {
906 	int index = fse->index;
907 
908 	if (index >= N_RES)
909 		return -EINVAL;
910 
911 	fse->min_width = gc0310_res[index].width;
912 	fse->min_height = gc0310_res[index].height;
913 	fse->max_width = gc0310_res[index].width;
914 	fse->max_height = gc0310_res[index].height;
915 
916 	return 0;
917 }
918 
919 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
920 {
921 	struct gc0310_device *dev = to_gc0310_sensor(sd);
922 
923 	mutex_lock(&dev->input_lock);
924 	*frames = dev->res->skip_frames;
925 	mutex_unlock(&dev->input_lock);
926 
927 	return 0;
928 }
929 
930 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = {
931 	.g_skip_frames	= gc0310_g_skip_frames,
932 };
933 
934 static const struct v4l2_subdev_video_ops gc0310_video_ops = {
935 	.s_stream = gc0310_s_stream,
936 	.g_frame_interval = gc0310_g_frame_interval,
937 };
938 
939 static const struct v4l2_subdev_core_ops gc0310_core_ops = {
940 	.s_power = gc0310_s_power,
941 	.ioctl = gc0310_ioctl,
942 };
943 
944 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = {
945 	.enum_mbus_code = gc0310_enum_mbus_code,
946 	.enum_frame_size = gc0310_enum_frame_size,
947 	.get_fmt = gc0310_get_fmt,
948 	.set_fmt = gc0310_set_fmt,
949 };
950 
951 static const struct v4l2_subdev_ops gc0310_ops = {
952 	.core = &gc0310_core_ops,
953 	.video = &gc0310_video_ops,
954 	.pad = &gc0310_pad_ops,
955 	.sensor = &gc0310_sensor_ops,
956 };
957 
958 static void gc0310_remove(struct i2c_client *client)
959 {
960 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
961 	struct gc0310_device *dev = to_gc0310_sensor(sd);
962 
963 	dev_dbg(&client->dev, "gc0310_remove...\n");
964 
965 	dev->platform_data->csi_cfg(sd, 0);
966 
967 	v4l2_device_unregister_subdev(sd);
968 	media_entity_cleanup(&dev->sd.entity);
969 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
970 	kfree(dev);
971 }
972 
973 static int gc0310_probe(struct i2c_client *client)
974 {
975 	struct gc0310_device *dev;
976 	int ret;
977 	void *pdata;
978 	unsigned int i;
979 
980 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
981 	if (!dev)
982 		return -ENOMEM;
983 
984 	mutex_init(&dev->input_lock);
985 
986 	dev->res = &gc0310_res_preview[0];
987 	v4l2_i2c_subdev_init(&dev->sd, client, &gc0310_ops);
988 
989 	pdata = gmin_camera_platform_data(&dev->sd,
990 					  ATOMISP_INPUT_FORMAT_RAW_8,
991 					  atomisp_bayer_order_grbg);
992 	if (!pdata) {
993 		ret = -EINVAL;
994 		goto out_free;
995 	}
996 
997 	ret = gc0310_s_config(&dev->sd, client->irq, pdata);
998 	if (ret)
999 		goto out_free;
1000 
1001 	ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1002 	if (ret)
1003 		goto out_free;
1004 
1005 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1006 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1007 	dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8;
1008 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1009 	ret =
1010 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1011 				   ARRAY_SIZE(gc0310_controls));
1012 	if (ret) {
1013 		gc0310_remove(client);
1014 		return ret;
1015 	}
1016 
1017 	for (i = 0; i < ARRAY_SIZE(gc0310_controls); i++)
1018 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc0310_controls[i],
1019 				     NULL);
1020 
1021 	if (dev->ctrl_handler.error) {
1022 		gc0310_remove(client);
1023 		return dev->ctrl_handler.error;
1024 	}
1025 
1026 	/* Use same lock for controls as for everything else. */
1027 	dev->ctrl_handler.lock = &dev->input_lock;
1028 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1029 
1030 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1031 	if (ret)
1032 		gc0310_remove(client);
1033 
1034 	return ret;
1035 out_free:
1036 	v4l2_device_unregister_subdev(&dev->sd);
1037 	kfree(dev);
1038 	return ret;
1039 }
1040 
1041 static const struct acpi_device_id gc0310_acpi_match[] = {
1042 	{"XXGC0310"},
1043 	{"INT0310"},
1044 	{},
1045 };
1046 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match);
1047 
1048 static struct i2c_driver gc0310_driver = {
1049 	.driver = {
1050 		.name = "gc0310",
1051 		.acpi_match_table = gc0310_acpi_match,
1052 	},
1053 	.probe_new = gc0310_probe,
1054 	.remove = gc0310_remove,
1055 };
1056 module_i2c_driver(gc0310_driver);
1057 
1058 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>");
1059 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors");
1060 MODULE_LICENSE("GPL");
1061