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