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 */
39 static 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 
90 static 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 
105 static 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 
131 static 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 
145 static 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 
173 static 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 
183 static 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 
223 static int gc2235_g_focal(struct v4l2_subdev *sd, s32 *val)
224 {
225 	*val = (GC2235_FOCAL_LENGTH_NUM << 16) | GC2235_FOCAL_LENGTH_DEM;
226 	return 0;
227 }
228 
229 static int gc2235_g_fnumber(struct v4l2_subdev *sd, s32 *val)
230 {
231 	/*const f number for imx*/
232 	*val = (GC2235_F_NUMBER_DEFAULT_NUM << 16) | GC2235_F_NUMBER_DEM;
233 	return 0;
234 }
235 
236 static int gc2235_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
237 {
238 	*val = (GC2235_F_NUMBER_DEFAULT_NUM << 24) |
239 	       (GC2235_F_NUMBER_DEM << 16) |
240 	       (GC2235_F_NUMBER_DEFAULT_NUM << 8) | GC2235_F_NUMBER_DEM;
241 	return 0;
242 }
243 
244 static int gc2235_get_intg_factor(struct i2c_client *client,
245 				  struct camera_mipi_info *info,
246 				  const struct gc2235_resolution *res)
247 {
248 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
249 	struct gc2235_device *dev = to_gc2235_sensor(sd);
250 	struct atomisp_sensor_mode_data *buf = &info->data;
251 	u16 reg_val, reg_val_h;
252 	int ret;
253 
254 	if (!info)
255 		return -EINVAL;
256 
257 	/* pixel clock calculattion */
258 	buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz = 30000000;
259 
260 	/* get integration time */
261 	buf->coarse_integration_time_min = GC2235_COARSE_INTG_TIME_MIN;
262 	buf->coarse_integration_time_max_margin =
263 	    GC2235_COARSE_INTG_TIME_MAX_MARGIN;
264 
265 	buf->fine_integration_time_min = GC2235_FINE_INTG_TIME_MIN;
266 	buf->fine_integration_time_max_margin =
267 	    GC2235_FINE_INTG_TIME_MAX_MARGIN;
268 
269 	buf->fine_integration_time_def = GC2235_FINE_INTG_TIME_MIN;
270 	buf->frame_length_lines = res->lines_per_frame;
271 	buf->line_length_pck = res->pixels_per_line;
272 	buf->read_mode = res->bin_mode;
273 
274 	/* get the cropping and output resolution to ISP for this mode. */
275 	ret =  gc2235_read_reg(client, GC2235_8BIT,
276 			       GC2235_H_CROP_START_H, &reg_val_h);
277 	ret =  gc2235_read_reg(client, GC2235_8BIT,
278 			       GC2235_H_CROP_START_L, &reg_val);
279 	if (ret)
280 		return ret;
281 
282 	buf->crop_horizontal_start = (reg_val_h << 8) | reg_val;
283 
284 	ret =  gc2235_read_reg(client, GC2235_8BIT,
285 			       GC2235_V_CROP_START_H, &reg_val_h);
286 	ret =  gc2235_read_reg(client, GC2235_8BIT,
287 			       GC2235_V_CROP_START_L, &reg_val);
288 	if (ret)
289 		return ret;
290 
291 	buf->crop_vertical_start = (reg_val_h << 8) | reg_val;
292 
293 	ret = gc2235_read_reg(client, GC2235_8BIT,
294 			      GC2235_H_OUTSIZE_H, &reg_val_h);
295 	ret = gc2235_read_reg(client, GC2235_8BIT,
296 			      GC2235_H_OUTSIZE_L, &reg_val);
297 	if (ret)
298 		return ret;
299 	buf->output_width = (reg_val_h << 8) | reg_val;
300 
301 	ret = gc2235_read_reg(client, GC2235_8BIT,
302 			      GC2235_V_OUTSIZE_H, &reg_val_h);
303 	ret = gc2235_read_reg(client, GC2235_8BIT,
304 			      GC2235_V_OUTSIZE_L, &reg_val);
305 	if (ret)
306 		return ret;
307 	buf->output_height = (reg_val_h << 8) | reg_val;
308 
309 	buf->crop_horizontal_end = buf->crop_horizontal_start +
310 				   buf->output_width - 1;
311 	buf->crop_vertical_end = buf->crop_vertical_start +
312 				 buf->output_height - 1;
313 
314 	ret = gc2235_read_reg(client, GC2235_8BIT,
315 			      GC2235_HB_H, &reg_val_h);
316 	ret = gc2235_read_reg(client, GC2235_8BIT,
317 			      GC2235_HB_L, &reg_val);
318 	if (ret)
319 		return ret;
320 
321 #if 0
322 	u16 dummy = (reg_val_h << 8) | reg_val;
323 #endif
324 
325 	ret = gc2235_read_reg(client, GC2235_8BIT,
326 			      GC2235_SH_DELAY_H, &reg_val_h);
327 	ret = gc2235_read_reg(client, GC2235_8BIT,
328 			      GC2235_SH_DELAY_L, &reg_val);
329 
330 #if 0
331 	buf->line_length_pck = buf->output_width + 16 + dummy +
332 			       (((u16)reg_val_h << 8) | (u16)reg_val) + 4;
333 #endif
334 	ret = gc2235_read_reg(client, GC2235_8BIT,
335 			      GC2235_VB_H, &reg_val_h);
336 	ret = gc2235_read_reg(client, GC2235_8BIT,
337 			      GC2235_VB_L, &reg_val);
338 	if (ret)
339 		return ret;
340 
341 #if 0
342 	buf->frame_length_lines = buf->output_height + 32 +
343 				  (((u16)reg_val_h << 8) | (u16)reg_val);
344 #endif
345 	buf->binning_factor_x = res->bin_factor_x ?
346 				res->bin_factor_x : 1;
347 	buf->binning_factor_y = res->bin_factor_y ?
348 				res->bin_factor_y : 1;
349 	return 0;
350 }
351 
352 static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
353 				  int gain, int digitgain)
354 
355 {
356 	struct i2c_client *client = v4l2_get_subdevdata(sd);
357 	u16 coarse_integration = (u16)coarse_itg;
358 	int ret = 0;
359 	u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0;
360 
361 	expo_coarse_h = coarse_integration >> 8;
362 	expo_coarse_l = coarse_integration & 0xff;
363 
364 	ret = gc2235_write_reg(client, GC2235_8BIT,
365 			       GC2235_EXPOSURE_H, expo_coarse_h);
366 	ret = gc2235_write_reg(client, GC2235_8BIT,
367 			       GC2235_EXPOSURE_L, expo_coarse_l);
368 
369 	if (gain <= 0x58) {
370 		gain_val = 0x40;
371 		gain_val2 = 0x58;
372 	} else if (gain < 256) {
373 		gain_val = 0x40;
374 		gain_val2 = gain;
375 	} else {
376 		gain_val2 = 64 * gain / 256;
377 		gain_val = 0xff;
378 	}
379 
380 	ret = gc2235_write_reg(client, GC2235_8BIT,
381 			       GC2235_GLOBAL_GAIN, (u8)gain_val);
382 	ret = gc2235_write_reg(client, GC2235_8BIT,
383 			       GC2235_PRE_GAIN, (u8)gain_val2);
384 
385 	return ret;
386 }
387 
388 static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure,
389 			       int gain, int digitgain)
390 {
391 	struct gc2235_device *dev = to_gc2235_sensor(sd);
392 	int ret;
393 
394 	mutex_lock(&dev->input_lock);
395 	ret = __gc2235_set_exposure(sd, exposure, gain, digitgain);
396 	mutex_unlock(&dev->input_lock);
397 
398 	return ret;
399 }
400 
401 static long gc2235_s_exposure(struct v4l2_subdev *sd,
402 			      struct atomisp_exposure *exposure)
403 {
404 	int exp = exposure->integration_time[0];
405 	int gain = exposure->gain[0];
406 	int digitgain = exposure->gain[1];
407 
408 	/* we should not accept the invalid value below. */
409 	if (gain == 0) {
410 		struct i2c_client *client = v4l2_get_subdevdata(sd);
411 
412 		v4l2_err(client, "%s: invalid value\n", __func__);
413 		return -EINVAL;
414 	}
415 
416 	return gc2235_set_exposure(sd, exp, gain, digitgain);
417 }
418 
419 static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
420 {
421 	switch (cmd) {
422 	case ATOMISP_IOC_S_EXPOSURE:
423 		return gc2235_s_exposure(sd, arg);
424 	default:
425 		return -EINVAL;
426 	}
427 	return 0;
428 }
429 
430 /* This returns the exposure time being used. This should only be used
431  * for filling in EXIF data, not for actual image processing.
432  */
433 static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value)
434 {
435 	struct i2c_client *client = v4l2_get_subdevdata(sd);
436 	u16 reg_v, reg_v2;
437 	int ret;
438 
439 	/* get exposure */
440 	ret = gc2235_read_reg(client, GC2235_8BIT,
441 			      GC2235_EXPOSURE_L,
442 			      &reg_v);
443 	if (ret)
444 		goto err;
445 
446 	ret = gc2235_read_reg(client, GC2235_8BIT,
447 			      GC2235_EXPOSURE_H,
448 			      &reg_v2);
449 	if (ret)
450 		goto err;
451 
452 	reg_v += reg_v2 << 8;
453 
454 	*value = reg_v;
455 err:
456 	return ret;
457 }
458 
459 static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
460 {
461 	struct gc2235_device *dev =
462 	    container_of(ctrl->handler, struct gc2235_device, ctrl_handler);
463 	int ret = 0;
464 
465 	switch (ctrl->id) {
466 	case V4L2_CID_EXPOSURE_ABSOLUTE:
467 		ret = gc2235_q_exposure(&dev->sd, &ctrl->val);
468 		break;
469 	case V4L2_CID_FOCAL_ABSOLUTE:
470 		ret = gc2235_g_focal(&dev->sd, &ctrl->val);
471 		break;
472 	case V4L2_CID_FNUMBER_ABSOLUTE:
473 		ret = gc2235_g_fnumber(&dev->sd, &ctrl->val);
474 		break;
475 	case V4L2_CID_FNUMBER_RANGE:
476 		ret = gc2235_g_fnumber_range(&dev->sd, &ctrl->val);
477 		break;
478 	default:
479 		ret = -EINVAL;
480 	}
481 
482 	return ret;
483 }
484 
485 static const struct v4l2_ctrl_ops ctrl_ops = {
486 	.g_volatile_ctrl = gc2235_g_volatile_ctrl
487 };
488 
489 static struct v4l2_ctrl_config gc2235_controls[] = {
490 	{
491 		.ops = &ctrl_ops,
492 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
493 		.type = V4L2_CTRL_TYPE_INTEGER,
494 		.name = "exposure",
495 		.min = 0x0,
496 		.max = 0xffff,
497 		.step = 0x01,
498 		.def = 0x00,
499 		.flags = 0,
500 	},
501 	{
502 		.ops = &ctrl_ops,
503 		.id = V4L2_CID_FOCAL_ABSOLUTE,
504 		.type = V4L2_CTRL_TYPE_INTEGER,
505 		.name = "focal length",
506 		.min = GC2235_FOCAL_LENGTH_DEFAULT,
507 		.max = GC2235_FOCAL_LENGTH_DEFAULT,
508 		.step = 0x01,
509 		.def = GC2235_FOCAL_LENGTH_DEFAULT,
510 		.flags = 0,
511 	},
512 	{
513 		.ops = &ctrl_ops,
514 		.id = V4L2_CID_FNUMBER_ABSOLUTE,
515 		.type = V4L2_CTRL_TYPE_INTEGER,
516 		.name = "f-number",
517 		.min = GC2235_F_NUMBER_DEFAULT,
518 		.max = GC2235_F_NUMBER_DEFAULT,
519 		.step = 0x01,
520 		.def = GC2235_F_NUMBER_DEFAULT,
521 		.flags = 0,
522 	},
523 	{
524 		.ops = &ctrl_ops,
525 		.id = V4L2_CID_FNUMBER_RANGE,
526 		.type = V4L2_CTRL_TYPE_INTEGER,
527 		.name = "f-number range",
528 		.min = GC2235_F_NUMBER_RANGE,
529 		.max = GC2235_F_NUMBER_RANGE,
530 		.step = 0x01,
531 		.def = GC2235_F_NUMBER_RANGE,
532 		.flags = 0,
533 	},
534 };
535 
536 static int __gc2235_init(struct v4l2_subdev *sd)
537 {
538 	struct i2c_client *client = v4l2_get_subdevdata(sd);
539 
540 	/* restore settings */
541 	gc2235_res = gc2235_res_preview;
542 	N_RES = N_RES_PREVIEW;
543 
544 	return gc2235_write_reg_array(client, gc2235_init_settings);
545 }
546 
547 static int is_init;
548 
549 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
550 {
551 	int ret = -1;
552 	struct gc2235_device *dev = to_gc2235_sensor(sd);
553 
554 	if (!dev || !dev->platform_data)
555 		return -ENODEV;
556 
557 	if (flag) {
558 		ret = dev->platform_data->v1p8_ctrl(sd, 1);
559 		usleep_range(60, 90);
560 		if (ret == 0)
561 			ret |= dev->platform_data->v2p8_ctrl(sd, 1);
562 	} else {
563 		ret = dev->platform_data->v1p8_ctrl(sd, 0);
564 		ret |= dev->platform_data->v2p8_ctrl(sd, 0);
565 	}
566 	return ret;
567 }
568 
569 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
570 {
571 	struct gc2235_device *dev = to_gc2235_sensor(sd);
572 	int ret = -1;
573 
574 	if (!dev || !dev->platform_data)
575 		return -ENODEV;
576 
577 	ret |= dev->platform_data->gpio1_ctrl(sd, !flag);
578 	usleep_range(60, 90);
579 	return dev->platform_data->gpio0_ctrl(sd, flag);
580 }
581 
582 static int power_up(struct v4l2_subdev *sd)
583 {
584 	struct gc2235_device *dev = to_gc2235_sensor(sd);
585 	struct i2c_client *client = v4l2_get_subdevdata(sd);
586 	int ret;
587 
588 	if (!dev->platform_data) {
589 		dev_err(&client->dev,
590 			"no camera_sensor_platform_data");
591 		return -ENODEV;
592 	}
593 	/* power control */
594 	ret = power_ctrl(sd, 1);
595 	if (ret)
596 		goto fail_power;
597 
598 	/* according to DS, at least 5ms is needed between DOVDD and PWDN */
599 	usleep_range(5000, 6000);
600 
601 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
602 	if (ret)
603 		goto fail_clk;
604 	usleep_range(5000, 6000);
605 
606 	/* gpio ctrl */
607 	ret = gpio_ctrl(sd, 1);
608 	if (ret) {
609 		ret = gpio_ctrl(sd, 1);
610 		if (ret)
611 			goto fail_power;
612 	}
613 
614 	msleep(5);
615 	return 0;
616 
617 fail_clk:
618 	gpio_ctrl(sd, 0);
619 fail_power:
620 	power_ctrl(sd, 0);
621 	dev_err(&client->dev, "sensor power-up failed\n");
622 
623 	return ret;
624 }
625 
626 static int power_down(struct v4l2_subdev *sd)
627 {
628 	struct gc2235_device *dev = to_gc2235_sensor(sd);
629 	struct i2c_client *client = v4l2_get_subdevdata(sd);
630 	int ret = 0;
631 
632 	if (!dev->platform_data) {
633 		dev_err(&client->dev,
634 			"no camera_sensor_platform_data");
635 		return -ENODEV;
636 	}
637 	/* gpio ctrl */
638 	ret = gpio_ctrl(sd, 0);
639 	if (ret) {
640 		ret = gpio_ctrl(sd, 0);
641 		if (ret)
642 			dev_err(&client->dev, "gpio failed 2\n");
643 	}
644 
645 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
646 	if (ret)
647 		dev_err(&client->dev, "flisclk failed\n");
648 
649 	/* power control */
650 	ret = power_ctrl(sd, 0);
651 	if (ret)
652 		dev_err(&client->dev, "vprog failed.\n");
653 
654 	return ret;
655 }
656 
657 static int gc2235_s_power(struct v4l2_subdev *sd, int on)
658 {
659 	int ret;
660 
661 	if (on == 0)
662 		ret = power_down(sd);
663 	else {
664 		ret = power_up(sd);
665 		if (!ret)
666 			ret = __gc2235_init(sd);
667 		is_init = 1;
668 	}
669 	return ret;
670 }
671 
672 /*
673  * distance - calculate the distance
674  * @res: resolution
675  * @w: width
676  * @h: height
677  *
678  * Get the gap between resolution and w/h.
679  * res->width/height smaller than w/h wouldn't be considered.
680  * Returns the value of gap or -1 if fail.
681  */
682 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
683 static int distance(struct gc2235_resolution *res, u32 w, u32 h)
684 {
685 	unsigned int w_ratio = (res->width << 13) / w;
686 	unsigned int h_ratio;
687 	int match;
688 
689 	if (h == 0)
690 		return -1;
691 	h_ratio = (res->height << 13) / h;
692 	if (h_ratio == 0)
693 		return -1;
694 	match   = abs(((w_ratio << 13) / h_ratio) - 8192);
695 
696 	if ((w_ratio < 8192) || (h_ratio < 8192) ||
697 	    (match > LARGEST_ALLOWED_RATIO_MISMATCH))
698 		return -1;
699 
700 	return w_ratio + h_ratio;
701 }
702 
703 /* Return the nearest higher resolution index */
704 static int nearest_resolution_index(int w, int h)
705 {
706 	int i;
707 	int idx = -1;
708 	int dist;
709 	int min_dist = INT_MAX;
710 	struct gc2235_resolution *tmp_res = NULL;
711 
712 	for (i = 0; i < N_RES; i++) {
713 		tmp_res = &gc2235_res[i];
714 		dist = distance(tmp_res, w, h);
715 		if (dist == -1)
716 			continue;
717 		if (dist < min_dist) {
718 			min_dist = dist;
719 			idx = i;
720 		}
721 	}
722 
723 	return idx;
724 }
725 
726 static int get_resolution_index(int w, int h)
727 {
728 	int i;
729 
730 	for (i = 0; i < N_RES; i++) {
731 		if (w != gc2235_res[i].width)
732 			continue;
733 		if (h != gc2235_res[i].height)
734 			continue;
735 
736 		return i;
737 	}
738 
739 	return -1;
740 }
741 
742 static int startup(struct v4l2_subdev *sd)
743 {
744 	struct gc2235_device *dev = to_gc2235_sensor(sd);
745 	struct i2c_client *client = v4l2_get_subdevdata(sd);
746 	int ret = 0;
747 
748 	if (is_init == 0) {
749 		/* force gc2235 to do a reset in res change, otherwise it
750 		* can not output normal after switching res. and it is not
751 		* necessary for first time run up after power on, for the sack
752 		* of performance
753 		*/
754 		power_down(sd);
755 		power_up(sd);
756 		gc2235_write_reg_array(client, gc2235_init_settings);
757 	}
758 
759 	ret = gc2235_write_reg_array(client, gc2235_res[dev->fmt_idx].regs);
760 	if (ret) {
761 		dev_err(&client->dev, "gc2235 write register err.\n");
762 		return ret;
763 	}
764 	is_init = 0;
765 
766 	return ret;
767 }
768 
769 static int gc2235_set_fmt(struct v4l2_subdev *sd,
770 			  struct v4l2_subdev_pad_config *cfg,
771 			  struct v4l2_subdev_format *format)
772 {
773 	struct v4l2_mbus_framefmt *fmt = &format->format;
774 	struct gc2235_device *dev = to_gc2235_sensor(sd);
775 	struct i2c_client *client = v4l2_get_subdevdata(sd);
776 	struct camera_mipi_info *gc2235_info = NULL;
777 	int ret = 0;
778 	int idx;
779 
780 	gc2235_info = v4l2_get_subdev_hostdata(sd);
781 	if (!gc2235_info)
782 		return -EINVAL;
783 	if (format->pad)
784 		return -EINVAL;
785 	if (!fmt)
786 		return -EINVAL;
787 	mutex_lock(&dev->input_lock);
788 	idx = nearest_resolution_index(fmt->width, fmt->height);
789 	if (idx == -1) {
790 		/* return the largest resolution */
791 		fmt->width = gc2235_res[N_RES - 1].width;
792 		fmt->height = gc2235_res[N_RES - 1].height;
793 	} else {
794 		fmt->width = gc2235_res[idx].width;
795 		fmt->height = gc2235_res[idx].height;
796 	}
797 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
798 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
799 		cfg->try_fmt = *fmt;
800 		mutex_unlock(&dev->input_lock);
801 		return 0;
802 	}
803 
804 	dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
805 	if (dev->fmt_idx == -1) {
806 		dev_err(&client->dev, "get resolution fail\n");
807 		mutex_unlock(&dev->input_lock);
808 		return -EINVAL;
809 	}
810 
811 	ret = startup(sd);
812 	if (ret) {
813 		dev_err(&client->dev, "gc2235 startup err\n");
814 		goto err;
815 	}
816 
817 	ret = gc2235_get_intg_factor(client, gc2235_info,
818 				     &gc2235_res[dev->fmt_idx]);
819 	if (ret)
820 		dev_err(&client->dev, "failed to get integration_factor\n");
821 
822 err:
823 	mutex_unlock(&dev->input_lock);
824 	return ret;
825 }
826 
827 static int gc2235_get_fmt(struct v4l2_subdev *sd,
828 			  struct v4l2_subdev_pad_config *cfg,
829 			  struct v4l2_subdev_format *format)
830 {
831 	struct v4l2_mbus_framefmt *fmt = &format->format;
832 	struct gc2235_device *dev = to_gc2235_sensor(sd);
833 
834 	if (format->pad)
835 		return -EINVAL;
836 
837 	if (!fmt)
838 		return -EINVAL;
839 
840 	fmt->width = gc2235_res[dev->fmt_idx].width;
841 	fmt->height = gc2235_res[dev->fmt_idx].height;
842 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
843 
844 	return 0;
845 }
846 
847 static int gc2235_detect(struct i2c_client *client)
848 {
849 	struct i2c_adapter *adapter = client->adapter;
850 	u16 high, low;
851 	int ret;
852 	u16 id;
853 
854 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
855 		return -ENODEV;
856 
857 	ret = gc2235_read_reg(client, GC2235_8BIT,
858 			      GC2235_SENSOR_ID_H, &high);
859 	if (ret) {
860 		dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
861 		return -ENODEV;
862 	}
863 	ret = gc2235_read_reg(client, GC2235_8BIT,
864 			      GC2235_SENSOR_ID_L, &low);
865 	id = ((high << 8) | low);
866 
867 	if (id != GC2235_ID) {
868 		dev_err(&client->dev, "sensor ID error, 0x%x\n", id);
869 		return -ENODEV;
870 	}
871 
872 	dev_info(&client->dev, "detect gc2235 success\n");
873 	return 0;
874 }
875 
876 static int gc2235_s_stream(struct v4l2_subdev *sd, int enable)
877 {
878 	struct gc2235_device *dev = to_gc2235_sensor(sd);
879 	struct i2c_client *client = v4l2_get_subdevdata(sd);
880 	int ret;
881 
882 	mutex_lock(&dev->input_lock);
883 
884 	if (enable)
885 		ret = gc2235_write_reg_array(client, gc2235_stream_on);
886 	else
887 		ret = gc2235_write_reg_array(client, gc2235_stream_off);
888 
889 	mutex_unlock(&dev->input_lock);
890 	return ret;
891 }
892 
893 static int gc2235_s_config(struct v4l2_subdev *sd,
894 			   int irq, void *platform_data)
895 {
896 	struct gc2235_device *dev = to_gc2235_sensor(sd);
897 	struct i2c_client *client = v4l2_get_subdevdata(sd);
898 	int ret = 0;
899 
900 	if (!platform_data)
901 		return -ENODEV;
902 
903 	dev->platform_data =
904 	    (struct camera_sensor_platform_data *)platform_data;
905 
906 	mutex_lock(&dev->input_lock);
907 	/* power off the module, then power on it in future
908 	 * as first power on by board may not fulfill the
909 	 * power on sequqence needed by the module
910 	 */
911 	ret = power_down(sd);
912 	if (ret) {
913 		dev_err(&client->dev, "gc2235 power-off err.\n");
914 		goto fail_power_off;
915 	}
916 
917 	ret = power_up(sd);
918 	if (ret) {
919 		dev_err(&client->dev, "gc2235 power-up err.\n");
920 		goto fail_power_on;
921 	}
922 
923 	ret = dev->platform_data->csi_cfg(sd, 1);
924 	if (ret)
925 		goto fail_csi_cfg;
926 
927 	/* config & detect sensor */
928 	ret = gc2235_detect(client);
929 	if (ret) {
930 		dev_err(&client->dev, "gc2235_detect err s_config.\n");
931 		goto fail_csi_cfg;
932 	}
933 
934 	/* turn off sensor, after probed */
935 	ret = power_down(sd);
936 	if (ret) {
937 		dev_err(&client->dev, "gc2235 power-off err.\n");
938 		goto fail_csi_cfg;
939 	}
940 	mutex_unlock(&dev->input_lock);
941 
942 	return 0;
943 
944 fail_csi_cfg:
945 	dev->platform_data->csi_cfg(sd, 0);
946 fail_power_on:
947 	power_down(sd);
948 	dev_err(&client->dev, "sensor power-gating failed\n");
949 fail_power_off:
950 	mutex_unlock(&dev->input_lock);
951 	return ret;
952 }
953 
954 static int gc2235_g_frame_interval(struct v4l2_subdev *sd,
955 				   struct v4l2_subdev_frame_interval *interval)
956 {
957 	struct gc2235_device *dev = to_gc2235_sensor(sd);
958 
959 	interval->interval.numerator = 1;
960 	interval->interval.denominator = gc2235_res[dev->fmt_idx].fps;
961 
962 	return 0;
963 }
964 
965 static int gc2235_enum_mbus_code(struct v4l2_subdev *sd,
966 				 struct v4l2_subdev_pad_config *cfg,
967 				 struct v4l2_subdev_mbus_code_enum *code)
968 {
969 	if (code->index >= MAX_FMTS)
970 		return -EINVAL;
971 
972 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
973 	return 0;
974 }
975 
976 static int gc2235_enum_frame_size(struct v4l2_subdev *sd,
977 				  struct v4l2_subdev_pad_config *cfg,
978 				  struct v4l2_subdev_frame_size_enum *fse)
979 {
980 	int index = fse->index;
981 
982 	if (index >= N_RES)
983 		return -EINVAL;
984 
985 	fse->min_width = gc2235_res[index].width;
986 	fse->min_height = gc2235_res[index].height;
987 	fse->max_width = gc2235_res[index].width;
988 	fse->max_height = gc2235_res[index].height;
989 
990 	return 0;
991 }
992 
993 static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
994 {
995 	struct gc2235_device *dev = to_gc2235_sensor(sd);
996 
997 	mutex_lock(&dev->input_lock);
998 	*frames = gc2235_res[dev->fmt_idx].skip_frames;
999 	mutex_unlock(&dev->input_lock);
1000 
1001 	return 0;
1002 }
1003 
1004 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = {
1005 	.g_skip_frames	= gc2235_g_skip_frames,
1006 };
1007 
1008 static const struct v4l2_subdev_video_ops gc2235_video_ops = {
1009 	.s_stream = gc2235_s_stream,
1010 	.g_frame_interval = gc2235_g_frame_interval,
1011 };
1012 
1013 static const struct v4l2_subdev_core_ops gc2235_core_ops = {
1014 	.s_power = gc2235_s_power,
1015 	.ioctl = gc2235_ioctl,
1016 };
1017 
1018 static const struct v4l2_subdev_pad_ops gc2235_pad_ops = {
1019 	.enum_mbus_code = gc2235_enum_mbus_code,
1020 	.enum_frame_size = gc2235_enum_frame_size,
1021 	.get_fmt = gc2235_get_fmt,
1022 	.set_fmt = gc2235_set_fmt,
1023 };
1024 
1025 static const struct v4l2_subdev_ops gc2235_ops = {
1026 	.core = &gc2235_core_ops,
1027 	.video = &gc2235_video_ops,
1028 	.pad = &gc2235_pad_ops,
1029 	.sensor = &gc2235_sensor_ops,
1030 };
1031 
1032 static int gc2235_remove(struct i2c_client *client)
1033 {
1034 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1035 	struct gc2235_device *dev = to_gc2235_sensor(sd);
1036 
1037 	dev_dbg(&client->dev, "gc2235_remove...\n");
1038 
1039 	dev->platform_data->csi_cfg(sd, 0);
1040 
1041 	v4l2_device_unregister_subdev(sd);
1042 	media_entity_cleanup(&dev->sd.entity);
1043 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1044 	kfree(dev);
1045 
1046 	return 0;
1047 }
1048 
1049 static int gc2235_probe(struct i2c_client *client)
1050 {
1051 	struct gc2235_device *dev;
1052 	void *gcpdev;
1053 	int ret;
1054 	unsigned int i;
1055 
1056 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1057 	if (!dev)
1058 		return -ENOMEM;
1059 
1060 	mutex_init(&dev->input_lock);
1061 
1062 	dev->fmt_idx = 0;
1063 	v4l2_i2c_subdev_init(&dev->sd, client, &gc2235_ops);
1064 
1065 	gcpdev = gmin_camera_platform_data(&dev->sd,
1066 					   ATOMISP_INPUT_FORMAT_RAW_10,
1067 					   atomisp_bayer_order_grbg);
1068 
1069 	ret = gc2235_s_config(&dev->sd, client->irq, gcpdev);
1070 	if (ret)
1071 		goto out_free;
1072 
1073 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1074 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1075 	dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1076 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1077 	ret =
1078 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1079 				   ARRAY_SIZE(gc2235_controls));
1080 	if (ret) {
1081 		gc2235_remove(client);
1082 		return ret;
1083 	}
1084 
1085 	for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++)
1086 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i],
1087 				     NULL);
1088 
1089 	if (dev->ctrl_handler.error) {
1090 		gc2235_remove(client);
1091 		return dev->ctrl_handler.error;
1092 	}
1093 
1094 	/* Use same lock for controls as for everything else. */
1095 	dev->ctrl_handler.lock = &dev->input_lock;
1096 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1097 
1098 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1099 	if (ret)
1100 		gc2235_remove(client);
1101 
1102 	return atomisp_register_i2c_module(&dev->sd, gcpdev, RAW_CAMERA);
1103 
1104 out_free:
1105 	v4l2_device_unregister_subdev(&dev->sd);
1106 	kfree(dev);
1107 
1108 	return ret;
1109 }
1110 
1111 static const struct acpi_device_id gc2235_acpi_match[] = {
1112 	{ "INT33F8" },
1113 	{},
1114 };
1115 MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match);
1116 
1117 static struct i2c_driver gc2235_driver = {
1118 	.driver = {
1119 		.name = "gc2235",
1120 		.acpi_match_table = gc2235_acpi_match,
1121 	},
1122 	.probe_new = gc2235_probe,
1123 	.remove = gc2235_remove,
1124 };
1125 module_i2c_driver(gc2235_driver);
1126 
1127 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
1128 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
1129 MODULE_LICENSE("GPL");
1130