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