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