1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Support for GalaxyCore GC2235 2M camera sensor.
4 *
5 * Copyright (c) 2014 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 "../include/linux/atomisp_gmin_platform.h"
33#include <linux/acpi.h>
34#include <linux/io.h>
35
36#include "gc2235.h"
37
38/* i2c read/write stuff */
39static int gc2235_read_reg(struct i2c_client *client,
40			   u16 data_length, u16 reg, u16 *val)
41{
42	int err;
43	struct i2c_msg msg[2];
44	unsigned char data[6];
45
46	if (!client->adapter) {
47		dev_err(&client->dev, "%s error, no client->adapter\n",
48			__func__);
49		return -ENODEV;
50	}
51
52	if (data_length != GC2235_8BIT) {
53		dev_err(&client->dev, "%s error, invalid data length\n",
54			__func__);
55		return -EINVAL;
56	}
57
58	memset(msg, 0, sizeof(msg));
59
60	msg[0].addr = client->addr;
61	msg[0].flags = 0;
62	msg[0].len = 1;
63	msg[0].buf = data;
64
65	/* high byte goes out first */
66	data[0] = (u8)(reg & 0xff);
67
68	msg[1].addr = client->addr;
69	msg[1].len = data_length;
70	msg[1].flags = I2C_M_RD;
71	msg[1].buf = data;
72
73	err = i2c_transfer(client->adapter, msg, 2);
74	if (err != 2) {
75		if (err >= 0)
76			err = -EIO;
77		dev_err(&client->dev,
78			"read from offset 0x%x error %d", reg, err);
79		return err;
80	}
81
82	*val = 0;
83	/* high byte comes first */
84	if (data_length == GC2235_8BIT)
85		*val = (u8)data[0];
86
87	return 0;
88}
89
90static int gc2235_i2c_write(struct i2c_client *client, u16 len, u8 *data)
91{
92	struct i2c_msg msg;
93	const int num_msg = 1;
94	int ret;
95
96	msg.addr = client->addr;
97	msg.flags = 0;
98	msg.len = len;
99	msg.buf = data;
100	ret = i2c_transfer(client->adapter, &msg, 1);
101
102	return ret == num_msg ? 0 : -EIO;
103}
104
105static int gc2235_write_reg(struct i2c_client *client, u16 data_length,
106			    u8 reg, u8 val)
107{
108	int ret;
109	unsigned char data[4] = {0};
110	const u16 len = data_length + sizeof(u8); /* 16-bit address + data */
111
112	if (data_length != GC2235_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	data[0] = reg;
120	data[1] = val;
121
122	ret = gc2235_i2c_write(client, len, data);
123	if (ret)
124		dev_err(&client->dev,
125			"write error: wrote 0x%x to offset 0x%x error %d",
126			val, reg, ret);
127
128	return ret;
129}
130
131static int __gc2235_flush_reg_array(struct i2c_client *client,
132				    struct gc2235_write_ctrl *ctrl)
133{
134	u16 size;
135
136	if (ctrl->index == 0)
137		return 0;
138
139	size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
140	ctrl->index = 0;
141
142	return gc2235_i2c_write(client, size, (u8 *)&ctrl->buffer);
143}
144
145static int __gc2235_buf_reg_array(struct i2c_client *client,
146				  struct gc2235_write_ctrl *ctrl,
147				  const struct gc2235_reg *next)
148{
149	int size;
150
151	if (next->type != GC2235_8BIT)
152		return -EINVAL;
153
154	size = 1;
155	ctrl->buffer.data[ctrl->index] = (u8)next->val;
156
157	/* When first item is added, we need to store its starting address */
158	if (ctrl->index == 0)
159		ctrl->buffer.addr = next->reg;
160
161	ctrl->index += size;
162
163	/*
164	 * Buffer cannot guarantee free space for u32? Better flush it to avoid
165	 * possible lack of memory for next item.
166	 */
167	if (ctrl->index + sizeof(u8) >= GC2235_MAX_WRITE_BUF_SIZE)
168		return __gc2235_flush_reg_array(client, ctrl);
169
170	return 0;
171}
172
173static int __gc2235_write_reg_is_consecutive(struct i2c_client *client,
174					     struct gc2235_write_ctrl *ctrl,
175					     const struct gc2235_reg *next)
176{
177	if (ctrl->index == 0)
178		return 1;
179
180	return ctrl->buffer.addr + ctrl->index == next->reg;
181}
182
183static int gc2235_write_reg_array(struct i2c_client *client,
184				  const struct gc2235_reg *reglist)
185{
186	const struct gc2235_reg *next = reglist;
187	struct gc2235_write_ctrl ctrl;
188	int err;
189
190	ctrl.index = 0;
191	for (; next->type != GC2235_TOK_TERM; next++) {
192		switch (next->type & GC2235_TOK_MASK) {
193		case GC2235_TOK_DELAY:
194			err = __gc2235_flush_reg_array(client, &ctrl);
195			if (err)
196				return err;
197			msleep(next->val);
198			break;
199		default:
200			/*
201			 * If next address is not consecutive, data needs to be
202			 * flushed before proceed.
203			 */
204			if (!__gc2235_write_reg_is_consecutive(client, &ctrl,
205							       next)) {
206				err = __gc2235_flush_reg_array(client, &ctrl);
207				if (err)
208					return err;
209			}
210			err = __gc2235_buf_reg_array(client, &ctrl, next);
211			if (err) {
212				dev_err(&client->dev, "%s: write error, aborted\n",
213					__func__);
214				return err;
215			}
216			break;
217		}
218	}
219
220	return __gc2235_flush_reg_array(client, &ctrl);
221}
222
223static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
224				  int gain, int digitgain)
225
226{
227	struct i2c_client *client = v4l2_get_subdevdata(sd);
228	u16 coarse_integration = (u16)coarse_itg;
229	int ret = 0;
230	u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0;
231
232	expo_coarse_h = coarse_integration >> 8;
233	expo_coarse_l = coarse_integration & 0xff;
234
235	ret = gc2235_write_reg(client, GC2235_8BIT,
236			       GC2235_EXPOSURE_H, expo_coarse_h);
237	ret = gc2235_write_reg(client, GC2235_8BIT,
238			       GC2235_EXPOSURE_L, expo_coarse_l);
239
240	if (gain <= 0x58) {
241		gain_val = 0x40;
242		gain_val2 = 0x58;
243	} else if (gain < 256) {
244		gain_val = 0x40;
245		gain_val2 = gain;
246	} else {
247		gain_val2 = 64 * gain / 256;
248		gain_val = 0xff;
249	}
250
251	ret = gc2235_write_reg(client, GC2235_8BIT,
252			       GC2235_GLOBAL_GAIN, (u8)gain_val);
253	ret = gc2235_write_reg(client, GC2235_8BIT,
254			       GC2235_PRE_GAIN, (u8)gain_val2);
255
256	return ret;
257}
258
259static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure,
260			       int gain, int digitgain)
261{
262	struct gc2235_device *dev = to_gc2235_sensor(sd);
263	int ret;
264
265	mutex_lock(&dev->input_lock);
266	ret = __gc2235_set_exposure(sd, exposure, gain, digitgain);
267	mutex_unlock(&dev->input_lock);
268
269	return ret;
270}
271
272static long gc2235_s_exposure(struct v4l2_subdev *sd,
273			      struct atomisp_exposure *exposure)
274{
275	int exp = exposure->integration_time[0];
276	int gain = exposure->gain[0];
277	int digitgain = exposure->gain[1];
278
279	/* we should not accept the invalid value below. */
280	if (gain == 0) {
281		struct i2c_client *client = v4l2_get_subdevdata(sd);
282
283		v4l2_err(client, "%s: invalid value\n", __func__);
284		return -EINVAL;
285	}
286
287	return gc2235_set_exposure(sd, exp, gain, digitgain);
288}
289
290static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
291{
292	switch (cmd) {
293	case ATOMISP_IOC_S_EXPOSURE:
294		return gc2235_s_exposure(sd, arg);
295	default:
296		return -EINVAL;
297	}
298	return 0;
299}
300
301/*
302 * This returns the exposure time being used. This should only be used
303 * for filling in EXIF data, not for actual image processing.
304 */
305static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value)
306{
307	struct i2c_client *client = v4l2_get_subdevdata(sd);
308	u16 reg_v, reg_v2;
309	int ret;
310
311	/* get exposure */
312	ret = gc2235_read_reg(client, GC2235_8BIT,
313			      GC2235_EXPOSURE_L,
314			      &reg_v);
315	if (ret)
316		goto err;
317
318	ret = gc2235_read_reg(client, GC2235_8BIT,
319			      GC2235_EXPOSURE_H,
320			      &reg_v2);
321	if (ret)
322		goto err;
323
324	reg_v += reg_v2 << 8;
325
326	*value = reg_v;
327err:
328	return ret;
329}
330
331static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
332{
333	struct gc2235_device *dev =
334	    container_of(ctrl->handler, struct gc2235_device, ctrl_handler);
335	int ret = 0;
336
337	switch (ctrl->id) {
338	case V4L2_CID_EXPOSURE_ABSOLUTE:
339		ret = gc2235_q_exposure(&dev->sd, &ctrl->val);
340		break;
341	default:
342		ret = -EINVAL;
343	}
344
345	return ret;
346}
347
348static const struct v4l2_ctrl_ops ctrl_ops = {
349	.g_volatile_ctrl = gc2235_g_volatile_ctrl
350};
351
352static struct v4l2_ctrl_config gc2235_controls[] = {
353	{
354		.ops = &ctrl_ops,
355		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
356		.type = V4L2_CTRL_TYPE_INTEGER,
357		.name = "exposure",
358		.min = 0x0,
359		.max = 0xffff,
360		.step = 0x01,
361		.def = 0x00,
362		.flags = 0,
363	},
364};
365
366static int __gc2235_init(struct v4l2_subdev *sd)
367{
368	struct i2c_client *client = v4l2_get_subdevdata(sd);
369
370	/* restore settings */
371	gc2235_res = gc2235_res_preview;
372	N_RES = N_RES_PREVIEW;
373
374	return gc2235_write_reg_array(client, gc2235_init_settings);
375}
376
377static int is_init;
378
379static int power_ctrl(struct v4l2_subdev *sd, bool flag)
380{
381	int ret = -1;
382	struct gc2235_device *dev = to_gc2235_sensor(sd);
383
384	if (!dev || !dev->platform_data)
385		return -ENODEV;
386
387	if (flag) {
388		ret = dev->platform_data->v1p8_ctrl(sd, 1);
389		usleep_range(60, 90);
390		if (ret == 0)
391			ret |= dev->platform_data->v2p8_ctrl(sd, 1);
392	} else {
393		ret = dev->platform_data->v1p8_ctrl(sd, 0);
394		ret |= dev->platform_data->v2p8_ctrl(sd, 0);
395	}
396	return ret;
397}
398
399static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
400{
401	struct gc2235_device *dev = to_gc2235_sensor(sd);
402	int ret;
403
404	if (!dev || !dev->platform_data)
405		return -ENODEV;
406
407	ret = dev->platform_data->gpio1_ctrl(sd, !flag);
408	usleep_range(60, 90);
409	ret |= dev->platform_data->gpio0_ctrl(sd, flag);
410
411	return ret;
412}
413
414static int power_up(struct v4l2_subdev *sd)
415{
416	struct gc2235_device *dev = to_gc2235_sensor(sd);
417	struct i2c_client *client = v4l2_get_subdevdata(sd);
418	int ret;
419
420	if (!dev->platform_data) {
421		dev_err(&client->dev,
422			"no camera_sensor_platform_data");
423		return -ENODEV;
424	}
425	/* power control */
426	ret = power_ctrl(sd, 1);
427	if (ret)
428		goto fail_power;
429
430	/* according to DS, at least 5ms is needed between DOVDD and PWDN */
431	usleep_range(5000, 6000);
432
433	ret = dev->platform_data->flisclk_ctrl(sd, 1);
434	if (ret)
435		goto fail_clk;
436	usleep_range(5000, 6000);
437
438	/* gpio ctrl */
439	ret = gpio_ctrl(sd, 1);
440	if (ret) {
441		ret = gpio_ctrl(sd, 1);
442		if (ret)
443			goto fail_power;
444	}
445
446	msleep(5);
447	return 0;
448
449fail_clk:
450	gpio_ctrl(sd, 0);
451fail_power:
452	power_ctrl(sd, 0);
453	dev_err(&client->dev, "sensor power-up failed\n");
454
455	return ret;
456}
457
458static int power_down(struct v4l2_subdev *sd)
459{
460	struct gc2235_device *dev = to_gc2235_sensor(sd);
461	struct i2c_client *client = v4l2_get_subdevdata(sd);
462	int ret = 0;
463
464	if (!dev->platform_data) {
465		dev_err(&client->dev,
466			"no camera_sensor_platform_data");
467		return -ENODEV;
468	}
469	/* gpio ctrl */
470	ret = gpio_ctrl(sd, 0);
471	if (ret) {
472		ret = gpio_ctrl(sd, 0);
473		if (ret)
474			dev_err(&client->dev, "gpio failed 2\n");
475	}
476
477	ret = dev->platform_data->flisclk_ctrl(sd, 0);
478	if (ret)
479		dev_err(&client->dev, "flisclk failed\n");
480
481	/* power control */
482	ret = power_ctrl(sd, 0);
483	if (ret)
484		dev_err(&client->dev, "vprog failed.\n");
485
486	return ret;
487}
488
489static int gc2235_s_power(struct v4l2_subdev *sd, int on)
490{
491	int ret;
492
493	if (on == 0) {
494		ret = power_down(sd);
495	} else {
496		ret = power_up(sd);
497		if (!ret)
498			ret = __gc2235_init(sd);
499		is_init = 1;
500	}
501	return ret;
502}
503
504static int startup(struct v4l2_subdev *sd)
505{
506	struct gc2235_device *dev = to_gc2235_sensor(sd);
507	struct i2c_client *client = v4l2_get_subdevdata(sd);
508	int ret = 0;
509
510	if (is_init == 0) {
511		/*
512		 * force gc2235 to do a reset in res change, otherwise it
513		 * can not output normal after switching res. and it is not
514		 * necessary for first time run up after power on, for the sack
515		 * of performance
516		 */
517		power_down(sd);
518		power_up(sd);
519		gc2235_write_reg_array(client, gc2235_init_settings);
520	}
521
522	ret = gc2235_write_reg_array(client, dev->res->regs);
523	if (ret) {
524		dev_err(&client->dev, "gc2235 write register err.\n");
525		return ret;
526	}
527	is_init = 0;
528
529	return ret;
530}
531
532static int gc2235_set_fmt(struct v4l2_subdev *sd,
533			  struct v4l2_subdev_state *sd_state,
534			  struct v4l2_subdev_format *format)
535{
536	struct v4l2_mbus_framefmt *fmt = &format->format;
537	struct gc2235_device *dev = to_gc2235_sensor(sd);
538	struct i2c_client *client = v4l2_get_subdevdata(sd);
539	struct camera_mipi_info *gc2235_info = NULL;
540	struct gc2235_resolution *res;
541	int ret = 0;
542
543	gc2235_info = v4l2_get_subdev_hostdata(sd);
544	if (!gc2235_info)
545		return -EINVAL;
546	if (format->pad)
547		return -EINVAL;
548	if (!fmt)
549		return -EINVAL;
550
551	mutex_lock(&dev->input_lock);
552	res = v4l2_find_nearest_size(gc2235_res_preview,
553				     ARRAY_SIZE(gc2235_res_preview), width,
554				     height, fmt->width, fmt->height);
555	if (!res)
556		res = &gc2235_res_preview[N_RES - 1];
557
558	fmt->width = res->width;
559	fmt->height = res->height;
560	dev->res = res;
561
562	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
563	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
564		*v4l2_subdev_state_get_format(sd_state, 0) = *fmt;
565		mutex_unlock(&dev->input_lock);
566		return 0;
567	}
568
569	ret = startup(sd);
570	if (ret) {
571		dev_err(&client->dev, "gc2235 startup err\n");
572		goto err;
573	}
574
575err:
576	mutex_unlock(&dev->input_lock);
577	return ret;
578}
579
580static int gc2235_get_fmt(struct v4l2_subdev *sd,
581			  struct v4l2_subdev_state *sd_state,
582			  struct v4l2_subdev_format *format)
583{
584	struct v4l2_mbus_framefmt *fmt = &format->format;
585	struct gc2235_device *dev = to_gc2235_sensor(sd);
586
587	if (format->pad)
588		return -EINVAL;
589
590	if (!fmt)
591		return -EINVAL;
592
593	fmt->width = dev->res->width;
594	fmt->height = dev->res->height;
595	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
596
597	return 0;
598}
599
600static int gc2235_detect(struct i2c_client *client)
601{
602	struct i2c_adapter *adapter = client->adapter;
603	u16 high = 0, low = 0;
604	u16 id;
605
606	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
607		return -ENODEV;
608
609	gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_H, &high);
610	gc2235_read_reg(client, GC2235_8BIT, GC2235_SENSOR_ID_L, &low);
611	id = ((high << 8) | low);
612
613	if (id != GC2235_ID) {
614		dev_err(&client->dev, "sensor ID error, 0x%x\n", id);
615		return -ENODEV;
616	}
617
618	dev_info(&client->dev, "detect gc2235 success\n");
619	return 0;
620}
621
622static int gc2235_s_stream(struct v4l2_subdev *sd, int enable)
623{
624	struct gc2235_device *dev = to_gc2235_sensor(sd);
625	struct i2c_client *client = v4l2_get_subdevdata(sd);
626	int ret;
627
628	mutex_lock(&dev->input_lock);
629
630	if (enable)
631		ret = gc2235_write_reg_array(client, gc2235_stream_on);
632	else
633		ret = gc2235_write_reg_array(client, gc2235_stream_off);
634
635	mutex_unlock(&dev->input_lock);
636	return ret;
637}
638
639static int gc2235_s_config(struct v4l2_subdev *sd,
640			   int irq, void *platform_data)
641{
642	struct gc2235_device *dev = to_gc2235_sensor(sd);
643	struct i2c_client *client = v4l2_get_subdevdata(sd);
644	int ret = 0;
645
646	if (!platform_data)
647		return -ENODEV;
648
649	dev->platform_data =
650	    (struct camera_sensor_platform_data *)platform_data;
651
652	mutex_lock(&dev->input_lock);
653	/*
654	 * power off the module, then power on it in future
655	 * as first power on by board may not fulfill the
656	 * power on sequqence needed by the module
657	 */
658	ret = power_down(sd);
659	if (ret) {
660		dev_err(&client->dev, "gc2235 power-off err.\n");
661		goto fail_power_off;
662	}
663
664	ret = power_up(sd);
665	if (ret) {
666		dev_err(&client->dev, "gc2235 power-up err.\n");
667		goto fail_power_on;
668	}
669
670	ret = dev->platform_data->csi_cfg(sd, 1);
671	if (ret)
672		goto fail_csi_cfg;
673
674	/* config & detect sensor */
675	ret = gc2235_detect(client);
676	if (ret) {
677		dev_err(&client->dev, "gc2235_detect err s_config.\n");
678		goto fail_csi_cfg;
679	}
680
681	/* turn off sensor, after probed */
682	ret = power_down(sd);
683	if (ret) {
684		dev_err(&client->dev, "gc2235 power-off err.\n");
685		goto fail_csi_cfg;
686	}
687	mutex_unlock(&dev->input_lock);
688
689	return 0;
690
691fail_csi_cfg:
692	dev->platform_data->csi_cfg(sd, 0);
693fail_power_on:
694	power_down(sd);
695	dev_err(&client->dev, "sensor power-gating failed\n");
696fail_power_off:
697	mutex_unlock(&dev->input_lock);
698	return ret;
699}
700
701static int gc2235_get_frame_interval(struct v4l2_subdev *sd,
702				     struct v4l2_subdev_state *sd_state,
703				     struct v4l2_subdev_frame_interval *interval)
704{
705	struct gc2235_device *dev = to_gc2235_sensor(sd);
706
707	/*
708	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
709	 * subdev active state API.
710	 */
711	if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE)
712		return -EINVAL;
713
714	interval->interval.numerator = 1;
715	interval->interval.denominator = dev->res->fps;
716
717	return 0;
718}
719
720static int gc2235_enum_mbus_code(struct v4l2_subdev *sd,
721				 struct v4l2_subdev_state *sd_state,
722				 struct v4l2_subdev_mbus_code_enum *code)
723{
724	if (code->index >= MAX_FMTS)
725		return -EINVAL;
726
727	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
728	return 0;
729}
730
731static int gc2235_enum_frame_size(struct v4l2_subdev *sd,
732				  struct v4l2_subdev_state *sd_state,
733				  struct v4l2_subdev_frame_size_enum *fse)
734{
735	int index = fse->index;
736
737	if (index >= N_RES)
738		return -EINVAL;
739
740	fse->min_width = gc2235_res[index].width;
741	fse->min_height = gc2235_res[index].height;
742	fse->max_width = gc2235_res[index].width;
743	fse->max_height = gc2235_res[index].height;
744
745	return 0;
746}
747
748static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
749{
750	struct gc2235_device *dev = to_gc2235_sensor(sd);
751
752	mutex_lock(&dev->input_lock);
753	*frames = dev->res->skip_frames;
754	mutex_unlock(&dev->input_lock);
755
756	return 0;
757}
758
759static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = {
760	.g_skip_frames	= gc2235_g_skip_frames,
761};
762
763static const struct v4l2_subdev_video_ops gc2235_video_ops = {
764	.s_stream = gc2235_s_stream,
765};
766
767static const struct v4l2_subdev_core_ops gc2235_core_ops = {
768	.s_power = gc2235_s_power,
769	.ioctl = gc2235_ioctl,
770};
771
772static const struct v4l2_subdev_pad_ops gc2235_pad_ops = {
773	.enum_mbus_code = gc2235_enum_mbus_code,
774	.enum_frame_size = gc2235_enum_frame_size,
775	.get_fmt = gc2235_get_fmt,
776	.set_fmt = gc2235_set_fmt,
777	.get_frame_interval = gc2235_get_frame_interval,
778};
779
780static const struct v4l2_subdev_ops gc2235_ops = {
781	.core = &gc2235_core_ops,
782	.video = &gc2235_video_ops,
783	.pad = &gc2235_pad_ops,
784	.sensor = &gc2235_sensor_ops,
785};
786
787static void gc2235_remove(struct i2c_client *client)
788{
789	struct v4l2_subdev *sd = i2c_get_clientdata(client);
790	struct gc2235_device *dev = to_gc2235_sensor(sd);
791
792	dev_dbg(&client->dev, "gc2235_remove...\n");
793
794	dev->platform_data->csi_cfg(sd, 0);
795
796	v4l2_device_unregister_subdev(sd);
797	media_entity_cleanup(&dev->sd.entity);
798	v4l2_ctrl_handler_free(&dev->ctrl_handler);
799	kfree(dev);
800}
801
802static int gc2235_probe(struct i2c_client *client)
803{
804	struct gc2235_device *dev;
805	void *gcpdev;
806	int ret;
807	unsigned int i;
808
809	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
810	if (!dev)
811		return -ENOMEM;
812
813	mutex_init(&dev->input_lock);
814
815	dev->res = &gc2235_res_preview[0];
816	v4l2_i2c_subdev_init(&dev->sd, client, &gc2235_ops);
817
818	gcpdev = gmin_camera_platform_data(&dev->sd,
819					   ATOMISP_INPUT_FORMAT_RAW_10,
820					   atomisp_bayer_order_grbg);
821
822	ret = gc2235_s_config(&dev->sd, client->irq, gcpdev);
823	if (ret)
824		goto out_free;
825
826	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
827	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
828	dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
829	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
830	ret =
831	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
832				   ARRAY_SIZE(gc2235_controls));
833	if (ret) {
834		gc2235_remove(client);
835		return ret;
836	}
837
838	for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++)
839		v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i],
840				     NULL);
841
842	if (dev->ctrl_handler.error) {
843		gc2235_remove(client);
844		return dev->ctrl_handler.error;
845	}
846
847	/* Use same lock for controls as for everything else. */
848	dev->ctrl_handler.lock = &dev->input_lock;
849	dev->sd.ctrl_handler = &dev->ctrl_handler;
850
851	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
852	if (ret)
853		gc2235_remove(client);
854
855	return atomisp_register_i2c_module(&dev->sd, gcpdev, RAW_CAMERA);
856
857out_free:
858	v4l2_device_unregister_subdev(&dev->sd);
859	kfree(dev);
860
861	return ret;
862}
863
864static const struct acpi_device_id gc2235_acpi_match[] = {
865	{ "INT33F8" },
866	{},
867};
868MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match);
869
870static struct i2c_driver gc2235_driver = {
871	.driver = {
872		.name = "gc2235",
873		.acpi_match_table = gc2235_acpi_match,
874	},
875	.probe = gc2235_probe,
876	.remove = gc2235_remove,
877};
878module_i2c_driver(gc2235_driver);
879
880MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
881MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
882MODULE_LICENSE("GPL");
883