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  * Copyright (c) 2023 Hans de Goede <hdegoede@redhat.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version
10  * 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  */
18 
19 #include <linux/delay.h>
20 #include <linux/errno.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/i2c.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/string.h>
27 #include <linux/types.h>
28 
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-device.h>
31 
32 #include "../include/linux/atomisp_gmin_platform.h"
33 
34 #define GC0310_NATIVE_WIDTH			656
35 #define GC0310_NATIVE_HEIGHT			496
36 
37 #define GC0310_FPS				30
38 #define GC0310_SKIP_FRAMES			3
39 
40 #define GC0310_FOCAL_LENGTH_NUM			278 /* 2.78mm */
41 
42 #define GC0310_ID				0xa310
43 
44 #define GC0310_RESET_RELATED			0xFE
45 #define GC0310_REGISTER_PAGE_0			0x0
46 #define GC0310_REGISTER_PAGE_3			0x3
47 
48 /*
49  * GC0310 System control registers
50  */
51 #define GC0310_SW_STREAM			0x10
52 
53 #define GC0310_SC_CMMN_CHIP_ID_H		0xf0
54 #define GC0310_SC_CMMN_CHIP_ID_L		0xf1
55 
56 #define GC0310_AEC_PK_EXPO_H			0x03
57 #define GC0310_AEC_PK_EXPO_L			0x04
58 #define GC0310_AGC_ADJ				0x48
59 #define GC0310_DGC_ADJ				0x71
60 #define GC0310_GROUP_ACCESS			0x3208
61 
62 #define GC0310_H_CROP_START_H			0x09
63 #define GC0310_H_CROP_START_L			0x0A
64 #define GC0310_V_CROP_START_H			0x0B
65 #define GC0310_V_CROP_START_L			0x0C
66 #define GC0310_H_OUTSIZE_H			0x0F
67 #define GC0310_H_OUTSIZE_L			0x10
68 #define GC0310_V_OUTSIZE_H			0x0D
69 #define GC0310_V_OUTSIZE_L			0x0E
70 #define GC0310_H_BLANKING_H			0x05
71 #define GC0310_H_BLANKING_L			0x06
72 #define GC0310_V_BLANKING_H			0x07
73 #define GC0310_V_BLANKING_L			0x08
74 #define GC0310_SH_DELAY				0x11
75 
76 #define GC0310_START_STREAMING			0x94 /* 8-bit enable */
77 #define GC0310_STOP_STREAMING			0x0 /* 8-bit disable */
78 
79 #define to_gc0310_sensor(x) container_of(x, struct gc0310_device, sd)
80 
81 struct gc0310_device {
82 	struct v4l2_subdev sd;
83 	struct media_pad pad;
84 	/* Protect against concurrent changes to controls */
85 	struct mutex input_lock;
86 	bool is_streaming;
87 
88 	struct gpio_desc *reset;
89 	struct gpio_desc *powerdown;
90 
91 	struct gc0310_mode {
92 		struct v4l2_mbus_framefmt fmt;
93 	} mode;
94 
95 	struct gc0310_ctrls {
96 		struct v4l2_ctrl_handler handler;
97 		struct v4l2_ctrl *exposure;
98 		struct v4l2_ctrl *gain;
99 	} ctrls;
100 };
101 
102 struct gc0310_reg {
103 	u8 reg;
104 	u8 val;
105 };
106 
107 static const struct gc0310_reg gc0310_reset_register[] = {
108 	/* System registers */
109 	{ 0xfe, 0xf0 },
110 	{ 0xfe, 0xf0 },
111 	{ 0xfe, 0x00 },
112 
113 	{ 0xfc, 0x0e }, /* 4e */
114 	{ 0xfc, 0x0e }, /* 16//4e // [0]apwd [6]regf_clk_gate */
115 	{ 0xf2, 0x80 }, /* sync output */
116 	{ 0xf3, 0x00 }, /* 1f//01 data output */
117 	{ 0xf7, 0x33 }, /* f9 */
118 	{ 0xf8, 0x05 }, /* 00 */
119 	{ 0xf9, 0x0e }, /* 0x8e //0f */
120 	{ 0xfa, 0x11 },
121 
122 	/* MIPI */
123 	{ 0xfe, 0x03 },
124 	{ 0x01, 0x03 }, /* mipi 1lane */
125 	{ 0x02, 0x22 }, /* 0x33 */
126 	{ 0x03, 0x94 },
127 	{ 0x04, 0x01 }, /* fifo_prog */
128 	{ 0x05, 0x00 }, /* fifo_prog */
129 	{ 0x06, 0x80 }, /* b0  //YUV ISP data */
130 	{ 0x11, 0x2a }, /* 1e //LDI set YUV422 */
131 	{ 0x12, 0x90 }, /* 00 //04 //00 //04//00 //LWC[7:0] */
132 	{ 0x13, 0x02 }, /* 05 //05 //LWC[15:8] */
133 	{ 0x15, 0x12 }, /* 0x10 //DPHYY_MODE read_ready */
134 	{ 0x17, 0x01 },
135 	{ 0x40, 0x08 },
136 	{ 0x41, 0x00 },
137 	{ 0x42, 0x00 },
138 	{ 0x43, 0x00 },
139 	{ 0x21, 0x02 }, /* 0x01 */
140 	{ 0x22, 0x02 }, /* 0x01 */
141 	{ 0x23, 0x01 }, /* 0x05 //Nor:0x05 DOU:0x06 */
142 	{ 0x29, 0x00 },
143 	{ 0x2A, 0x25 }, /* 0x05 //data zero 0x7a de */
144 	{ 0x2B, 0x02 },
145 
146 	{ 0xfe, 0x00 },
147 
148 	/* CISCTL */
149 	{ 0x00, 0x2f }, /* 2f//0f//02//01 */
150 	{ 0x01, 0x0f }, /* 06 */
151 	{ 0x02, 0x04 },
152 	{ 0x4f, 0x00 }, /* AEC 0FF */
153 	{ 0x03, 0x01 }, /* 0x03 //04 */
154 	{ 0x04, 0xc0 }, /* 0xe8 //58 */
155 	{ 0x05, 0x00 },
156 	{ 0x06, 0xb2 }, /* 0x0a //HB */
157 	{ 0x07, 0x00 },
158 	{ 0x08, 0x0c }, /* 0x89 //VB */
159 	{ 0x09, 0x00 }, /* row start */
160 	{ 0x0a, 0x00 },
161 	{ 0x0b, 0x00 }, /* col start */
162 	{ 0x0c, 0x00 },
163 	{ 0x0d, 0x01 }, /* height */
164 	{ 0x0e, 0xf2 }, /* 0xf7 //height */
165 	{ 0x0f, 0x02 }, /* width */
166 	{ 0x10, 0x94 }, /* 0xa0 //height */
167 	{ 0x17, 0x14 },
168 	{ 0x18, 0x1a }, /* 0a//[4]double reset */
169 	{ 0x19, 0x14 }, /* AD pipeline */
170 	{ 0x1b, 0x48 },
171 	{ 0x1e, 0x6b }, /* 3b//col bias */
172 	{ 0x1f, 0x28 }, /* 20//00//08//txlow */
173 	{ 0x20, 0x89 }, /* 88//0c//[3:2]DA15 */
174 	{ 0x21, 0x49 }, /* 48//[3] txhigh */
175 	{ 0x22, 0xb0 },
176 	{ 0x23, 0x04 }, /* [1:0]vcm_r */
177 	{ 0x24, 0x16 }, /* 15 */
178 	{ 0x34, 0x20 }, /* [6:4] rsg high//range */
179 
180 	/* BLK */
181 	{ 0x26, 0x23 }, /* [1]dark_current_en [0]offset_en */
182 	{ 0x28, 0xff }, /* BLK_limie_value */
183 	{ 0x29, 0x00 }, /* global offset */
184 	{ 0x33, 0x18 }, /* offset_ratio */
185 	{ 0x37, 0x20 }, /* dark_current_ratio */
186 	{ 0x2a, 0x00 },
187 	{ 0x2b, 0x00 },
188 	{ 0x2c, 0x00 },
189 	{ 0x2d, 0x00 },
190 	{ 0x2e, 0x00 },
191 	{ 0x2f, 0x00 },
192 	{ 0x30, 0x00 },
193 	{ 0x31, 0x00 },
194 	{ 0x47, 0x80 }, /* a7 */
195 	{ 0x4e, 0x66 }, /* select_row */
196 	{ 0xa8, 0x02 }, /* win_width_dark, same with crop_win_width */
197 	{ 0xa9, 0x80 },
198 
199 	/* ISP */
200 	{ 0x40, 0x06 }, /* 0xff //ff //48 */
201 	{ 0x41, 0x00 }, /* 0x21 //00//[0]curve_en */
202 	{ 0x42, 0x04 }, /* 0xcf //0a//[1]awn_en */
203 	{ 0x44, 0x18 }, /* 0x18 //02 */
204 	{ 0x46, 0x02 }, /* 0x03 //sync */
205 	{ 0x49, 0x03 },
206 	{ 0x4c, 0x20 }, /* 00[5]pretect exp */
207 	{ 0x50, 0x01 }, /* crop enable */
208 	{ 0x51, 0x00 },
209 	{ 0x52, 0x00 },
210 	{ 0x53, 0x00 },
211 	{ 0x54, 0x01 },
212 	{ 0x55, 0x01 }, /* crop window height */
213 	{ 0x56, 0xf0 },
214 	{ 0x57, 0x02 }, /* crop window width */
215 	{ 0x58, 0x90 },
216 
217 	/* Gain */
218 	{ 0x70, 0x70 }, /* 70 //80//global gain */
219 	{ 0x71, 0x20 }, /* pregain gain */
220 	{ 0x72, 0x40 }, /* post gain */
221 	{ 0x5a, 0x84 }, /* 84//analog gain 0  */
222 	{ 0x5b, 0xc9 }, /* c9 */
223 	{ 0x5c, 0xed }, /* ed//not use pga gain highest level */
224 	{ 0x77, 0x40 }, /* R gain 0x74 //awb gain */
225 	{ 0x78, 0x40 }, /* G gain */
226 	{ 0x79, 0x40 }, /* B gain 0x5f */
227 
228 	{ 0x48, 0x00 },
229 	{ 0xfe, 0x01 },
230 	{ 0x0a, 0x45 }, /* [7]col gain mode */
231 
232 	{ 0x3e, 0x40 },
233 	{ 0x3f, 0x5c },
234 	{ 0x40, 0x7b },
235 	{ 0x41, 0xbd },
236 	{ 0x42, 0xf6 },
237 	{ 0x43, 0x63 },
238 	{ 0x03, 0x60 },
239 	{ 0x44, 0x03 },
240 
241 	/* Dark / Sun mode related */
242 	{ 0xfe, 0x01 },
243 	{ 0x45, 0xa4 }, /* 0xf7 */
244 	{ 0x46, 0xf0 }, /* 0xff //f0//sun value th */
245 	{ 0x48, 0x03 }, /* sun mode */
246 	{ 0x4f, 0x60 }, /* sun_clamp */
247 	{ 0xfe, 0x00 },
248 };
249 
250 static const struct gc0310_reg gc0310_VGA_30fps[] = {
251 	{ 0xfe, 0x00 },
252 	{ 0x0d, 0x01 }, /* height */
253 	{ 0x0e, 0xf2 }, /* 0xf7 //height */
254 	{ 0x0f, 0x02 }, /* width */
255 	{ 0x10, 0x94 }, /* 0xa0 //height */
256 
257 	{ 0x50, 0x01 }, /* crop enable */
258 	{ 0x51, 0x00 },
259 	{ 0x52, 0x00 },
260 	{ 0x53, 0x00 },
261 	{ 0x54, 0x01 },
262 	{ 0x55, 0x01 }, /* crop window height */
263 	{ 0x56, 0xf0 },
264 	{ 0x57, 0x02 }, /* crop window width */
265 	{ 0x58, 0x90 },
266 
267 	{ 0xfe, 0x03 },
268 	{ 0x12, 0x90 }, /* 00 //04 //00 //04//00 //LWC[7:0]  */
269 	{ 0x13, 0x02 }, /* 05 //05 //LWC[15:8] */
270 
271 	{ 0xfe, 0x00 },
272 };
273 
274 /*
275  * gc0310_write_reg_array - Initializes a list of GC0310 registers
276  * @client: i2c driver client structure
277  * @reglist: list of registers to be written
278  * @count: number of register, value pairs in the list
279  */
280 static int gc0310_write_reg_array(struct i2c_client *client,
281 				  const struct gc0310_reg *reglist, int count)
282 {
283 	int i, err;
284 
285 	for (i = 0; i < count; i++) {
286 		err = i2c_smbus_write_byte_data(client, reglist[i].reg, reglist[i].val);
287 		if (err) {
288 			dev_err(&client->dev, "write error: wrote 0x%x to offset 0x%x error %d",
289 				reglist[i].val, reglist[i].reg, err);
290 			return err;
291 		}
292 	}
293 
294 	return 0;
295 }
296 
297 static int gc0310_exposure_set(struct gc0310_device *dev, u32 exp)
298 {
299 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
300 
301 	return i2c_smbus_write_word_swapped(client, GC0310_AEC_PK_EXPO_H, exp);
302 }
303 
304 static int gc0310_gain_set(struct gc0310_device *dev, u32 gain)
305 {
306 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
307 	u8 again, dgain;
308 	int ret;
309 
310 	/* Taken from original driver, this never sets dgain lower then 32? */
311 
312 	/* Change 0 - 95 to 32 - 127 */
313 	gain += 32;
314 
315 	if (gain < 64) {
316 		again = 0x0; /* sqrt(2) */
317 		dgain = gain;
318 	} else {
319 		again = 0x2; /* 2 * sqrt(2) */
320 		dgain = gain / 2;
321 	}
322 
323 	ret = i2c_smbus_write_byte_data(client, GC0310_AGC_ADJ, again);
324 	if (ret)
325 		return ret;
326 
327 	return i2c_smbus_write_byte_data(client, GC0310_DGC_ADJ, dgain);
328 }
329 
330 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl)
331 {
332 	struct gc0310_device *dev =
333 		container_of(ctrl->handler, struct gc0310_device, ctrls.handler);
334 	int ret;
335 
336 	/* Only apply changes to the controls if the device is powered up */
337 	if (!pm_runtime_get_if_in_use(dev->sd.dev))
338 		return 0;
339 
340 	switch (ctrl->id) {
341 	case V4L2_CID_EXPOSURE:
342 		ret = gc0310_exposure_set(dev, ctrl->val);
343 		break;
344 	case V4L2_CID_GAIN:
345 		ret = gc0310_gain_set(dev, ctrl->val);
346 		break;
347 	default:
348 		ret = -EINVAL;
349 		break;
350 	}
351 
352 	pm_runtime_put(dev->sd.dev);
353 	return ret;
354 }
355 
356 static const struct v4l2_ctrl_ops ctrl_ops = {
357 	.s_ctrl = gc0310_s_ctrl,
358 };
359 
360 static struct v4l2_mbus_framefmt *
361 gc0310_get_pad_format(struct gc0310_device *dev,
362 		      struct v4l2_subdev_state *state,
363 		      unsigned int pad, enum v4l2_subdev_format_whence which)
364 {
365 	if (which == V4L2_SUBDEV_FORMAT_TRY)
366 		return v4l2_subdev_get_try_format(&dev->sd, state, pad);
367 
368 	return &dev->mode.fmt;
369 }
370 
371 /* The GC0310 currently only supports 1 fixed fmt */
372 static void gc0310_fill_format(struct v4l2_mbus_framefmt *fmt)
373 {
374 	memset(fmt, 0, sizeof(*fmt));
375 	fmt->width = GC0310_NATIVE_WIDTH;
376 	fmt->height = GC0310_NATIVE_HEIGHT;
377 	fmt->field = V4L2_FIELD_NONE;
378 	fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
379 }
380 
381 static int gc0310_set_fmt(struct v4l2_subdev *sd,
382 			  struct v4l2_subdev_state *sd_state,
383 			  struct v4l2_subdev_format *format)
384 {
385 	struct gc0310_device *dev = to_gc0310_sensor(sd);
386 	struct v4l2_mbus_framefmt *fmt;
387 
388 	fmt = gc0310_get_pad_format(dev, sd_state, format->pad, format->which);
389 	gc0310_fill_format(fmt);
390 
391 	format->format = *fmt;
392 	return 0;
393 }
394 
395 static int gc0310_get_fmt(struct v4l2_subdev *sd,
396 			  struct v4l2_subdev_state *sd_state,
397 			  struct v4l2_subdev_format *format)
398 {
399 	struct gc0310_device *dev = to_gc0310_sensor(sd);
400 	struct v4l2_mbus_framefmt *fmt;
401 
402 	fmt = gc0310_get_pad_format(dev, sd_state, format->pad, format->which);
403 	format->format = *fmt;
404 	return 0;
405 }
406 
407 static int gc0310_detect(struct i2c_client *client)
408 {
409 	struct i2c_adapter *adapter = client->adapter;
410 	int ret;
411 
412 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
413 		return -ENODEV;
414 
415 	ret = pm_runtime_get_sync(&client->dev);
416 	if (ret >= 0)
417 		ret = i2c_smbus_read_word_swapped(client, GC0310_SC_CMMN_CHIP_ID_H);
418 	pm_runtime_put(&client->dev);
419 	if (ret < 0) {
420 		dev_err(&client->dev, "read sensor_id failed: %d\n", ret);
421 		return -ENODEV;
422 	}
423 
424 	dev_dbg(&client->dev, "sensor ID = 0x%x\n", ret);
425 
426 	if (ret != GC0310_ID) {
427 		dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n",
428 			ret, GC0310_ID);
429 		return -ENODEV;
430 	}
431 
432 	dev_dbg(&client->dev, "detect gc0310 success\n");
433 
434 	return 0;
435 }
436 
437 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable)
438 {
439 	struct gc0310_device *dev = to_gc0310_sensor(sd);
440 	struct i2c_client *client = v4l2_get_subdevdata(sd);
441 	int ret = 0;
442 
443 	dev_dbg(&client->dev, "%s S enable=%d\n", __func__, enable);
444 	mutex_lock(&dev->input_lock);
445 
446 	if (dev->is_streaming == enable) {
447 		dev_warn(&client->dev, "stream already %s\n", enable ? "started" : "stopped");
448 		goto error_unlock;
449 	}
450 
451 	if (enable) {
452 		ret = pm_runtime_get_sync(&client->dev);
453 		if (ret < 0)
454 			goto error_power_down;
455 
456 		msleep(100);
457 
458 		ret = gc0310_write_reg_array(client, gc0310_reset_register,
459 					     ARRAY_SIZE(gc0310_reset_register));
460 		if (ret)
461 			goto error_power_down;
462 
463 		ret = gc0310_write_reg_array(client, gc0310_VGA_30fps,
464 					     ARRAY_SIZE(gc0310_VGA_30fps));
465 		if (ret)
466 			goto error_power_down;
467 
468 		/* restore value of all ctrls */
469 		ret = __v4l2_ctrl_handler_setup(&dev->ctrls.handler);
470 		if (ret)
471 			goto error_power_down;
472 
473 		/* enable per frame MIPI and sensor ctrl reset  */
474 		ret = i2c_smbus_write_byte_data(client, 0xFE, 0x30);
475 		if (ret)
476 			goto error_power_down;
477 	}
478 
479 	ret = i2c_smbus_write_byte_data(client, GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3);
480 	if (ret)
481 		goto error_power_down;
482 
483 	ret = i2c_smbus_write_byte_data(client, GC0310_SW_STREAM,
484 					enable ? GC0310_START_STREAMING : GC0310_STOP_STREAMING);
485 	if (ret)
486 		goto error_power_down;
487 
488 	ret = i2c_smbus_write_byte_data(client, GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0);
489 	if (ret)
490 		goto error_power_down;
491 
492 	if (!enable)
493 		pm_runtime_put(&client->dev);
494 
495 	dev->is_streaming = enable;
496 	mutex_unlock(&dev->input_lock);
497 	return 0;
498 
499 error_power_down:
500 	pm_runtime_put(&client->dev);
501 	dev->is_streaming = false;
502 error_unlock:
503 	mutex_unlock(&dev->input_lock);
504 	return ret;
505 }
506 
507 static int gc0310_g_frame_interval(struct v4l2_subdev *sd,
508 				   struct v4l2_subdev_frame_interval *interval)
509 {
510 	interval->interval.numerator = 1;
511 	interval->interval.denominator = GC0310_FPS;
512 
513 	return 0;
514 }
515 
516 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd,
517 				 struct v4l2_subdev_state *sd_state,
518 				 struct v4l2_subdev_mbus_code_enum *code)
519 {
520 	/* We support only a single format */
521 	if (code->index)
522 		return -EINVAL;
523 
524 	code->code = MEDIA_BUS_FMT_SGRBG8_1X8;
525 	return 0;
526 }
527 
528 static int gc0310_enum_frame_size(struct v4l2_subdev *sd,
529 				  struct v4l2_subdev_state *sd_state,
530 				  struct v4l2_subdev_frame_size_enum *fse)
531 {
532 	/* We support only a single resolution */
533 	if (fse->index)
534 		return -EINVAL;
535 
536 	fse->min_width = GC0310_NATIVE_WIDTH;
537 	fse->max_width = GC0310_NATIVE_WIDTH;
538 	fse->min_height = GC0310_NATIVE_HEIGHT;
539 	fse->max_height = GC0310_NATIVE_HEIGHT;
540 
541 	return 0;
542 }
543 
544 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
545 {
546 	*frames = GC0310_SKIP_FRAMES;
547 	return 0;
548 }
549 
550 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = {
551 	.g_skip_frames	= gc0310_g_skip_frames,
552 };
553 
554 static const struct v4l2_subdev_video_ops gc0310_video_ops = {
555 	.s_stream = gc0310_s_stream,
556 	.g_frame_interval = gc0310_g_frame_interval,
557 };
558 
559 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = {
560 	.enum_mbus_code = gc0310_enum_mbus_code,
561 	.enum_frame_size = gc0310_enum_frame_size,
562 	.get_fmt = gc0310_get_fmt,
563 	.set_fmt = gc0310_set_fmt,
564 };
565 
566 static const struct v4l2_subdev_ops gc0310_ops = {
567 	.video = &gc0310_video_ops,
568 	.pad = &gc0310_pad_ops,
569 	.sensor = &gc0310_sensor_ops,
570 };
571 
572 static int gc0310_init_controls(struct gc0310_device *dev)
573 {
574 	struct v4l2_ctrl_handler *hdl = &dev->ctrls.handler;
575 
576 	v4l2_ctrl_handler_init(hdl, 2);
577 
578 	/* Use the same lock for controls as for everything else */
579 	hdl->lock = &dev->input_lock;
580 	dev->sd.ctrl_handler = hdl;
581 
582 	dev->ctrls.exposure =
583 		v4l2_ctrl_new_std(hdl, &ctrl_ops, V4L2_CID_EXPOSURE, 0, 4095, 1, 1023);
584 
585 	/* 32 steps at base gain 1 + 64 half steps at base gain 2 */
586 	dev->ctrls.gain =
587 		v4l2_ctrl_new_std(hdl, &ctrl_ops, V4L2_CID_GAIN, 0, 95, 1, 31);
588 
589 	return hdl->error;
590 }
591 
592 static void gc0310_remove(struct i2c_client *client)
593 {
594 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
595 	struct gc0310_device *dev = to_gc0310_sensor(sd);
596 
597 	dev_dbg(&client->dev, "gc0310_remove...\n");
598 
599 	atomisp_unregister_subdev(sd);
600 	v4l2_device_unregister_subdev(sd);
601 	media_entity_cleanup(&dev->sd.entity);
602 	v4l2_ctrl_handler_free(&dev->ctrls.handler);
603 	mutex_destroy(&dev->input_lock);
604 	pm_runtime_disable(&client->dev);
605 }
606 
607 static int gc0310_probe(struct i2c_client *client)
608 {
609 	struct gc0310_device *dev;
610 	int ret;
611 
612 	dev = devm_kzalloc(&client->dev, sizeof(*dev), GFP_KERNEL);
613 	if (!dev)
614 		return -ENOMEM;
615 
616 	ret = v4l2_get_acpi_sensor_info(&client->dev, NULL);
617 	if (ret)
618 		return ret;
619 
620 	dev->reset = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_HIGH);
621 	if (IS_ERR(dev->reset))
622 		return dev_err_probe(&client->dev, PTR_ERR(dev->reset),
623 				     "getting reset GPIO\n");
624 
625 	dev->powerdown = devm_gpiod_get(&client->dev, "powerdown", GPIOD_OUT_HIGH);
626 	if (IS_ERR(dev->powerdown))
627 		return dev_err_probe(&client->dev, PTR_ERR(dev->powerdown),
628 				     "getting powerdown GPIO\n");
629 
630 	mutex_init(&dev->input_lock);
631 	v4l2_i2c_subdev_init(&dev->sd, client, &gc0310_ops);
632 	gc0310_fill_format(&dev->mode.fmt);
633 
634 	pm_runtime_set_suspended(&client->dev);
635 	pm_runtime_enable(&client->dev);
636 	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
637 	pm_runtime_use_autosuspend(&client->dev);
638 
639 	ret = gc0310_detect(client);
640 	if (ret) {
641 		gc0310_remove(client);
642 		return ret;
643 	}
644 
645 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
646 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
647 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
648 
649 	ret = gc0310_init_controls(dev);
650 	if (ret) {
651 		gc0310_remove(client);
652 		return ret;
653 	}
654 
655 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
656 	if (ret) {
657 		gc0310_remove(client);
658 		return ret;
659 	}
660 
661 	ret = atomisp_register_sensor_no_gmin(&dev->sd, 1, ATOMISP_INPUT_FORMAT_RAW_8,
662 					      atomisp_bayer_order_grbg);
663 	if (ret) {
664 		gc0310_remove(client);
665 		return ret;
666 	}
667 
668 	return 0;
669 }
670 
671 static int gc0310_suspend(struct device *dev)
672 {
673 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
674 	struct gc0310_device *gc0310_dev = to_gc0310_sensor(sd);
675 
676 	gpiod_set_value_cansleep(gc0310_dev->powerdown, 1);
677 	gpiod_set_value_cansleep(gc0310_dev->reset, 1);
678 	return 0;
679 }
680 
681 static int gc0310_resume(struct device *dev)
682 {
683 	struct v4l2_subdev *sd = dev_get_drvdata(dev);
684 	struct gc0310_device *gc0310_dev = to_gc0310_sensor(sd);
685 
686 	usleep_range(10000, 15000);
687 	gpiod_set_value_cansleep(gc0310_dev->reset, 0);
688 	usleep_range(10000, 15000);
689 	gpiod_set_value_cansleep(gc0310_dev->powerdown, 0);
690 
691 	return 0;
692 }
693 
694 static DEFINE_RUNTIME_DEV_PM_OPS(gc0310_pm_ops, gc0310_suspend, gc0310_resume, NULL);
695 
696 static const struct acpi_device_id gc0310_acpi_match[] = {
697 	{"INT0310"},
698 	{},
699 };
700 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match);
701 
702 static struct i2c_driver gc0310_driver = {
703 	.driver = {
704 		.name = "gc0310",
705 		.pm = pm_sleep_ptr(&gc0310_pm_ops),
706 		.acpi_match_table = gc0310_acpi_match,
707 	},
708 	.probe = gc0310_probe,
709 	.remove = gc0310_remove,
710 };
711 module_i2c_driver(gc0310_driver);
712 
713 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>");
714 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors");
715 MODULE_LICENSE("GPL");
716