1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for OmniVision OV5693 1080p HD 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 
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/kmod.h>
27 #include <linux/device.h>
28 #include <linux/delay.h>
29 #include <linux/slab.h>
30 #include <linux/i2c.h>
31 #include <linux/moduleparam.h>
32 #include <media/v4l2-device.h>
33 #include <linux/io.h>
34 #include <linux/acpi.h>
35 #include "../../include/linux/atomisp_gmin_platform.h"
36 
37 #include "ov5693.h"
38 #include "ad5823.h"
39 
40 #define __cci_delay(t) \
41 	do { \
42 		if ((t) < 10) { \
43 			usleep_range((t) * 1000, ((t) + 1) * 1000); \
44 		} else { \
45 			msleep((t)); \
46 		} \
47 	} while (0)
48 
49 /* Value 30ms reached through experimentation on byt ecs.
50  * The DS specifies a much lower value but when using a smaller value
51  * the I2C bus sometimes locks up permanently when starting the camera.
52  * This issue could not be reproduced on cht, so we can reduce the
53  * delay value to a lower value when insmod.
54  */
55 static uint up_delay = 30;
56 module_param(up_delay, uint, 0644);
57 MODULE_PARM_DESC(up_delay,
58 		 "Delay prior to the first CCI transaction for ov5693");
59 
vcm_ad_i2c_wr8(struct i2c_client * client,u8 reg,u8 val)60 static int vcm_ad_i2c_wr8(struct i2c_client *client, u8 reg, u8 val)
61 {
62 	int err;
63 	struct i2c_msg msg;
64 	u8 buf[2];
65 
66 	buf[0] = reg;
67 	buf[1] = val;
68 
69 	msg.addr = VCM_ADDR;
70 	msg.flags = 0;
71 	msg.len = 2;
72 	msg.buf = &buf[0];
73 
74 	err = i2c_transfer(client->adapter, &msg, 1);
75 	if (err != 1) {
76 		dev_err(&client->dev, "%s: vcm i2c fail, err code = %d\n",
77 			__func__, err);
78 		return -EIO;
79 	}
80 	return 0;
81 }
82 
ad5823_i2c_write(struct i2c_client * client,u8 reg,u8 val)83 static int ad5823_i2c_write(struct i2c_client *client, u8 reg, u8 val)
84 {
85 	struct i2c_msg msg;
86 	u8 buf[2];
87 
88 	buf[0] = reg;
89 	buf[1] = val;
90 	msg.addr = AD5823_VCM_ADDR;
91 	msg.flags = 0;
92 	msg.len = 0x02;
93 	msg.buf = &buf[0];
94 
95 	if (i2c_transfer(client->adapter, &msg, 1) != 1)
96 		return -EIO;
97 	return 0;
98 }
99 
ad5823_i2c_read(struct i2c_client * client,u8 reg,u8 * val)100 static int ad5823_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
101 {
102 	struct i2c_msg msg[2];
103 	u8 buf[2];
104 
105 	buf[0] = reg;
106 	buf[1] = 0;
107 
108 	msg[0].addr = AD5823_VCM_ADDR;
109 	msg[0].flags = 0;
110 	msg[0].len = 0x01;
111 	msg[0].buf = &buf[0];
112 
113 	msg[1].addr = 0x0c;
114 	msg[1].flags = I2C_M_RD;
115 	msg[1].len = 0x01;
116 	msg[1].buf = &buf[1];
117 	*val = 0;
118 	if (i2c_transfer(client->adapter, msg, 2) != 2)
119 		return -EIO;
120 	*val = buf[1];
121 	return 0;
122 }
123 
124 static const u32 ov5693_embedded_effective_size = 28;
125 
126 /* i2c read/write stuff */
ov5693_read_reg(struct i2c_client * client,u16 data_length,u16 reg,u16 * val)127 static int ov5693_read_reg(struct i2c_client *client,
128 			   u16 data_length, u16 reg, u16 *val)
129 {
130 	int err;
131 	struct i2c_msg msg[2];
132 	unsigned char data[6];
133 
134 	if (!client->adapter) {
135 		dev_err(&client->dev, "%s error, no client->adapter\n",
136 			__func__);
137 		return -ENODEV;
138 	}
139 
140 	if (data_length != OV5693_8BIT && data_length != OV5693_16BIT
141 	    && data_length != OV5693_32BIT) {
142 		dev_err(&client->dev, "%s error, invalid data length\n",
143 			__func__);
144 		return -EINVAL;
145 	}
146 
147 	memset(msg, 0, sizeof(msg));
148 
149 	msg[0].addr = client->addr;
150 	msg[0].flags = 0;
151 	msg[0].len = I2C_MSG_LENGTH;
152 	msg[0].buf = data;
153 
154 	/* high byte goes out first */
155 	data[0] = (u8)(reg >> 8);
156 	data[1] = (u8)(reg & 0xff);
157 
158 	msg[1].addr = client->addr;
159 	msg[1].len = data_length;
160 	msg[1].flags = I2C_M_RD;
161 	msg[1].buf = data;
162 
163 	err = i2c_transfer(client->adapter, msg, 2);
164 	if (err != 2) {
165 		if (err >= 0)
166 			err = -EIO;
167 		dev_err(&client->dev,
168 			"read from offset 0x%x error %d", reg, err);
169 		return err;
170 	}
171 
172 	*val = 0;
173 	/* high byte comes first */
174 	if (data_length == OV5693_8BIT)
175 		*val = (u8)data[0];
176 	else if (data_length == OV5693_16BIT)
177 		*val = be16_to_cpu(*(__be16 *)&data[0]);
178 	else
179 		*val = be32_to_cpu(*(__be32 *)&data[0]);
180 
181 	return 0;
182 }
183 
ov5693_i2c_write(struct i2c_client * client,u16 len,u8 * data)184 static int ov5693_i2c_write(struct i2c_client *client, u16 len, u8 *data)
185 {
186 	struct i2c_msg msg;
187 	const int num_msg = 1;
188 	int ret;
189 
190 	msg.addr = client->addr;
191 	msg.flags = 0;
192 	msg.len = len;
193 	msg.buf = data;
194 	ret = i2c_transfer(client->adapter, &msg, 1);
195 
196 	return ret == num_msg ? 0 : -EIO;
197 }
198 
vcm_dw_i2c_write(struct i2c_client * client,u16 data)199 static int vcm_dw_i2c_write(struct i2c_client *client, u16 data)
200 {
201 	struct i2c_msg msg;
202 	const int num_msg = 1;
203 	int ret;
204 	__be16 val;
205 
206 	val = cpu_to_be16(data);
207 	msg.addr = VCM_ADDR;
208 	msg.flags = 0;
209 	msg.len = OV5693_16BIT;
210 	msg.buf = (void *)&val;
211 
212 	ret = i2c_transfer(client->adapter, &msg, 1);
213 
214 	return ret == num_msg ? 0 : -EIO;
215 }
216 
217 /*
218  * Theory: per datasheet, the two VCMs both allow for a 2-byte read.
219  * The DW9714 doesn't actually specify what this does (it has a
220  * two-byte write-only protocol, but specifies the read sequence as
221  * legal), but it returns the same data (zeroes) always, after an
222  * undocumented initial NAK.  The AD5823 has a one-byte address
223  * register to which all writes go, and subsequent reads will cycle
224  * through the 8 bytes of registers.  Notably, the default values (the
225  * device is always power-cycled affirmatively, so we can rely on
226  * these) in AD5823 are not pairwise repetitions of the same 16 bit
227  * word.  So all we have to do is sequentially read two bytes at a
228  * time and see if we detect a difference in any of the first four
229  * pairs.
230  */
vcm_detect(struct i2c_client * client)231 static int vcm_detect(struct i2c_client *client)
232 {
233 	int i, ret;
234 	struct i2c_msg msg;
235 	u16 data0 = 0, data;
236 
237 	for (i = 0; i < 4; i++) {
238 		msg.addr = VCM_ADDR;
239 		msg.flags = I2C_M_RD;
240 		msg.len = sizeof(data);
241 		msg.buf = (u8 *)&data;
242 		ret = i2c_transfer(client->adapter, &msg, 1);
243 
244 		/*
245 		 * DW9714 always fails the first read and returns
246 		 * zeroes for subsequent ones
247 		 */
248 		if (i == 0 && ret == -EREMOTEIO) {
249 			data0 = 0;
250 			continue;
251 		}
252 
253 		if (i == 0)
254 			data0 = data;
255 
256 		if (data != data0)
257 			return VCM_AD5823;
258 	}
259 	return ret == 1 ? VCM_DW9714 : ret;
260 }
261 
ov5693_write_reg(struct i2c_client * client,u16 data_length,u16 reg,u16 val)262 static int ov5693_write_reg(struct i2c_client *client, u16 data_length,
263 			    u16 reg, u16 val)
264 {
265 	int ret;
266 	unsigned char data[4] = {0};
267 	__be16 *wreg = (void *)data;
268 	const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
269 
270 	if (data_length != OV5693_8BIT && data_length != OV5693_16BIT) {
271 		dev_err(&client->dev,
272 			"%s error, invalid data_length\n", __func__);
273 		return -EINVAL;
274 	}
275 
276 	/* high byte goes out first */
277 	*wreg = cpu_to_be16(reg);
278 
279 	if (data_length == OV5693_8BIT) {
280 		data[2] = (u8)(val);
281 	} else {
282 		/* OV5693_16BIT */
283 		__be16 *wdata = (void *)&data[2];
284 
285 		*wdata = cpu_to_be16(val);
286 	}
287 
288 	ret = ov5693_i2c_write(client, len, data);
289 	if (ret)
290 		dev_err(&client->dev,
291 			"write error: wrote 0x%x to offset 0x%x error %d",
292 			val, reg, ret);
293 
294 	return ret;
295 }
296 
297 /*
298  * ov5693_write_reg_array - Initializes a list of OV5693 registers
299  * @client: i2c driver client structure
300  * @reglist: list of registers to be written
301  *
302  * This function initializes a list of registers. When consecutive addresses
303  * are found in a row on the list, this function creates a buffer and sends
304  * consecutive data in a single i2c_transfer().
305  *
306  * __ov5693_flush_reg_array, __ov5693_buf_reg_array() and
307  * __ov5693_write_reg_is_consecutive() are internal functions to
308  * ov5693_write_reg_array_fast() and should be not used anywhere else.
309  *
310  */
311 
__ov5693_flush_reg_array(struct i2c_client * client,struct ov5693_write_ctrl * ctrl)312 static int __ov5693_flush_reg_array(struct i2c_client *client,
313 				    struct ov5693_write_ctrl *ctrl)
314 {
315 	u16 size;
316 	__be16 *reg = (void *)&ctrl->buffer.addr;
317 
318 	if (ctrl->index == 0)
319 		return 0;
320 
321 	size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
322 
323 	*reg = cpu_to_be16(ctrl->buffer.addr);
324 	ctrl->index = 0;
325 
326 	return ov5693_i2c_write(client, size, (u8 *)reg);
327 }
328 
__ov5693_buf_reg_array(struct i2c_client * client,struct ov5693_write_ctrl * ctrl,const struct ov5693_reg * next)329 static int __ov5693_buf_reg_array(struct i2c_client *client,
330 				  struct ov5693_write_ctrl *ctrl,
331 				  const struct ov5693_reg *next)
332 {
333 	int size;
334 	__be16 *data16;
335 
336 	switch (next->type) {
337 	case OV5693_8BIT:
338 		size = 1;
339 		ctrl->buffer.data[ctrl->index] = (u8)next->val;
340 		break;
341 	case OV5693_16BIT:
342 		size = 2;
343 
344 		data16 = (void *)&ctrl->buffer.data[ctrl->index];
345 		*data16 = cpu_to_be16((u16)next->val);
346 		break;
347 	default:
348 		return -EINVAL;
349 	}
350 
351 	/* When first item is added, we need to store its starting address */
352 	if (ctrl->index == 0)
353 		ctrl->buffer.addr = next->reg;
354 
355 	ctrl->index += size;
356 
357 	/*
358 	 * Buffer cannot guarantee free space for u32? Better flush it to avoid
359 	 * possible lack of memory for next item.
360 	 */
361 	if (ctrl->index + sizeof(u16) >= OV5693_MAX_WRITE_BUF_SIZE)
362 		return __ov5693_flush_reg_array(client, ctrl);
363 
364 	return 0;
365 }
366 
__ov5693_write_reg_is_consecutive(struct i2c_client * client,struct ov5693_write_ctrl * ctrl,const struct ov5693_reg * next)367 static int __ov5693_write_reg_is_consecutive(struct i2c_client *client,
368 	struct ov5693_write_ctrl *ctrl,
369 	const struct ov5693_reg *next)
370 {
371 	if (ctrl->index == 0)
372 		return 1;
373 
374 	return ctrl->buffer.addr + ctrl->index == next->reg;
375 }
376 
ov5693_write_reg_array(struct i2c_client * client,const struct ov5693_reg * reglist)377 static int ov5693_write_reg_array(struct i2c_client *client,
378 				  const struct ov5693_reg *reglist)
379 {
380 	const struct ov5693_reg *next = reglist;
381 	struct ov5693_write_ctrl ctrl;
382 	int err;
383 
384 	ctrl.index = 0;
385 	for (; next->type != OV5693_TOK_TERM; next++) {
386 		switch (next->type & OV5693_TOK_MASK) {
387 		case OV5693_TOK_DELAY:
388 			err = __ov5693_flush_reg_array(client, &ctrl);
389 			if (err)
390 				return err;
391 			msleep(next->val);
392 			break;
393 		default:
394 			/*
395 			 * If next address is not consecutive, data needs to be
396 			 * flushed before proceed.
397 			 */
398 			if (!__ov5693_write_reg_is_consecutive(client, &ctrl,
399 							       next)) {
400 				err = __ov5693_flush_reg_array(client, &ctrl);
401 				if (err)
402 					return err;
403 			}
404 			err = __ov5693_buf_reg_array(client, &ctrl, next);
405 			if (err) {
406 				dev_err(&client->dev,
407 					"%s: write error, aborted\n",
408 					__func__);
409 				return err;
410 			}
411 			break;
412 		}
413 	}
414 
415 	return __ov5693_flush_reg_array(client, &ctrl);
416 }
417 
__ov5693_set_exposure(struct v4l2_subdev * sd,int coarse_itg,int gain,int digitgain)418 static long __ov5693_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
419 				  int gain, int digitgain)
420 
421 {
422 	struct i2c_client *client = v4l2_get_subdevdata(sd);
423 	struct ov5693_device *dev = to_ov5693_sensor(sd);
424 	u16 vts, hts;
425 	int ret, exp_val;
426 
427 	hts = ov5693_res[dev->fmt_idx].pixels_per_line;
428 	vts = ov5693_res[dev->fmt_idx].lines_per_frame;
429 	/*
430 	 * If coarse_itg is larger than 1<<15, can not write to reg directly.
431 	 * The way is to write coarse_itg/2 to the reg, meanwhile write 2*hts
432 	 * to the reg.
433 	 */
434 	if (coarse_itg > (1 << 15)) {
435 		hts = hts * 2;
436 		coarse_itg = (int)coarse_itg / 2;
437 	}
438 	/* group hold */
439 	ret = ov5693_write_reg(client, OV5693_8BIT,
440 			       OV5693_GROUP_ACCESS, 0x00);
441 	if (ret) {
442 		dev_err(&client->dev, "%s: write %x error, aborted\n",
443 			__func__, OV5693_GROUP_ACCESS);
444 		return ret;
445 	}
446 
447 	ret = ov5693_write_reg(client, OV5693_8BIT,
448 			       OV5693_TIMING_HTS_H, (hts >> 8) & 0xFF);
449 	if (ret) {
450 		dev_err(&client->dev, "%s: write %x error, aborted\n",
451 			__func__, OV5693_TIMING_HTS_H);
452 		return ret;
453 	}
454 
455 	ret = ov5693_write_reg(client, OV5693_8BIT,
456 			       OV5693_TIMING_HTS_L, hts & 0xFF);
457 	if (ret) {
458 		dev_err(&client->dev, "%s: write %x error, aborted\n",
459 			__func__, OV5693_TIMING_HTS_L);
460 		return ret;
461 	}
462 	/* Increase the VTS to match exposure + MARGIN */
463 	if (coarse_itg > vts - OV5693_INTEGRATION_TIME_MARGIN)
464 		vts = (u16)coarse_itg + OV5693_INTEGRATION_TIME_MARGIN;
465 
466 	ret = ov5693_write_reg(client, OV5693_8BIT,
467 			       OV5693_TIMING_VTS_H, (vts >> 8) & 0xFF);
468 	if (ret) {
469 		dev_err(&client->dev, "%s: write %x error, aborted\n",
470 			__func__, OV5693_TIMING_VTS_H);
471 		return ret;
472 	}
473 
474 	ret = ov5693_write_reg(client, OV5693_8BIT,
475 			       OV5693_TIMING_VTS_L, vts & 0xFF);
476 	if (ret) {
477 		dev_err(&client->dev, "%s: write %x error, aborted\n",
478 			__func__, OV5693_TIMING_VTS_L);
479 		return ret;
480 	}
481 
482 	/* set exposure */
483 
484 	/* Lower four bit should be 0*/
485 	exp_val = coarse_itg << 4;
486 	ret = ov5693_write_reg(client, OV5693_8BIT,
487 			       OV5693_EXPOSURE_L, exp_val & 0xFF);
488 	if (ret) {
489 		dev_err(&client->dev, "%s: write %x error, aborted\n",
490 			__func__, OV5693_EXPOSURE_L);
491 		return ret;
492 	}
493 
494 	ret = ov5693_write_reg(client, OV5693_8BIT,
495 			       OV5693_EXPOSURE_M, (exp_val >> 8) & 0xFF);
496 	if (ret) {
497 		dev_err(&client->dev, "%s: write %x error, aborted\n",
498 			__func__, OV5693_EXPOSURE_M);
499 		return ret;
500 	}
501 
502 	ret = ov5693_write_reg(client, OV5693_8BIT,
503 			       OV5693_EXPOSURE_H, (exp_val >> 16) & 0x0F);
504 	if (ret) {
505 		dev_err(&client->dev, "%s: write %x error, aborted\n",
506 			__func__, OV5693_EXPOSURE_H);
507 		return ret;
508 	}
509 
510 	/* Analog gain */
511 	ret = ov5693_write_reg(client, OV5693_8BIT,
512 			       OV5693_AGC_L, gain & 0xff);
513 	if (ret) {
514 		dev_err(&client->dev, "%s: write %x error, aborted\n",
515 			__func__, OV5693_AGC_L);
516 		return ret;
517 	}
518 
519 	ret = ov5693_write_reg(client, OV5693_8BIT,
520 			       OV5693_AGC_H, (gain >> 8) & 0xff);
521 	if (ret) {
522 		dev_err(&client->dev, "%s: write %x error, aborted\n",
523 			__func__, OV5693_AGC_H);
524 		return ret;
525 	}
526 
527 	/* Digital gain */
528 	if (digitgain) {
529 		ret = ov5693_write_reg(client, OV5693_16BIT,
530 				       OV5693_MWB_RED_GAIN_H, digitgain);
531 		if (ret) {
532 			dev_err(&client->dev, "%s: write %x error, aborted\n",
533 				__func__, OV5693_MWB_RED_GAIN_H);
534 			return ret;
535 		}
536 
537 		ret = ov5693_write_reg(client, OV5693_16BIT,
538 				       OV5693_MWB_GREEN_GAIN_H, digitgain);
539 		if (ret) {
540 			dev_err(&client->dev, "%s: write %x error, aborted\n",
541 				__func__, OV5693_MWB_RED_GAIN_H);
542 			return ret;
543 		}
544 
545 		ret = ov5693_write_reg(client, OV5693_16BIT,
546 				       OV5693_MWB_BLUE_GAIN_H, digitgain);
547 		if (ret) {
548 			dev_err(&client->dev, "%s: write %x error, aborted\n",
549 				__func__, OV5693_MWB_RED_GAIN_H);
550 			return ret;
551 		}
552 	}
553 
554 	/* End group */
555 	ret = ov5693_write_reg(client, OV5693_8BIT,
556 			       OV5693_GROUP_ACCESS, 0x10);
557 	if (ret)
558 		return ret;
559 
560 	/* Delay launch group */
561 	ret = ov5693_write_reg(client, OV5693_8BIT,
562 			       OV5693_GROUP_ACCESS, 0xa0);
563 	if (ret)
564 		return ret;
565 	return ret;
566 }
567 
ov5693_set_exposure(struct v4l2_subdev * sd,int exposure,int gain,int digitgain)568 static int ov5693_set_exposure(struct v4l2_subdev *sd, int exposure,
569 			       int gain, int digitgain)
570 {
571 	struct ov5693_device *dev = to_ov5693_sensor(sd);
572 	int ret;
573 
574 	mutex_lock(&dev->input_lock);
575 	ret = __ov5693_set_exposure(sd, exposure, gain, digitgain);
576 	mutex_unlock(&dev->input_lock);
577 
578 	return ret;
579 }
580 
ov5693_s_exposure(struct v4l2_subdev * sd,struct atomisp_exposure * exposure)581 static long ov5693_s_exposure(struct v4l2_subdev *sd,
582 			      struct atomisp_exposure *exposure)
583 {
584 	u16 coarse_itg = exposure->integration_time[0];
585 	u16 analog_gain = exposure->gain[0];
586 	u16 digital_gain = exposure->gain[1];
587 
588 	/* we should not accept the invalid value below */
589 	if (analog_gain == 0) {
590 		struct i2c_client *client = v4l2_get_subdevdata(sd);
591 
592 		v4l2_err(client, "%s: invalid value\n", __func__);
593 		return -EINVAL;
594 	}
595 	return ov5693_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
596 }
597 
ov5693_read_otp_reg_array(struct i2c_client * client,u16 size,u16 addr,u8 * buf)598 static int ov5693_read_otp_reg_array(struct i2c_client *client, u16 size,
599 				     u16 addr, u8 *buf)
600 {
601 	u16 index;
602 	int ret;
603 	u16 *pVal = NULL;
604 
605 	for (index = 0; index <= size; index++) {
606 		pVal = (u16 *)(buf + index);
607 		ret =
608 		    ov5693_read_reg(client, OV5693_8BIT, addr + index,
609 				    pVal);
610 		if (ret)
611 			return ret;
612 	}
613 
614 	return 0;
615 }
616 
__ov5693_otp_read(struct v4l2_subdev * sd,u8 * buf)617 static int __ov5693_otp_read(struct v4l2_subdev *sd, u8 *buf)
618 {
619 	struct i2c_client *client = v4l2_get_subdevdata(sd);
620 	struct ov5693_device *dev = to_ov5693_sensor(sd);
621 	int ret;
622 	int i;
623 	u8 *b = buf;
624 
625 	dev->otp_size = 0;
626 	for (i = 1; i < OV5693_OTP_BANK_MAX; i++) {
627 		/*set bank NO and OTP read mode. */
628 		ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_BANK_REG,
629 				       (i | 0xc0));	//[7:6] 2'b11 [5:0] bank no
630 		if (ret) {
631 			dev_err(&client->dev, "failed to prepare OTP page\n");
632 			return ret;
633 		}
634 		//pr_debug("write 0x%x->0x%x\n",OV5693_OTP_BANK_REG,(i|0xc0));
635 
636 		/*enable read */
637 		ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_READ_REG,
638 				       OV5693_OTP_MODE_READ);	// enable :1
639 		if (ret) {
640 			dev_err(&client->dev,
641 				"failed to set OTP reading mode page");
642 			return ret;
643 		}
644 		//pr_debug("write 0x%x->0x%x\n",OV5693_OTP_READ_REG,OV5693_OTP_MODE_READ);
645 
646 		/* Reading the OTP data array */
647 		ret = ov5693_read_otp_reg_array(client, OV5693_OTP_BANK_SIZE,
648 						OV5693_OTP_START_ADDR,
649 						b);
650 		if (ret) {
651 			dev_err(&client->dev, "failed to read OTP data\n");
652 			return ret;
653 		}
654 
655 		//pr_debug("BANK[%2d] %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", i, *b, *(b+1), *(b+2), *(b+3), *(b+4), *(b+5), *(b+6), *(b+7), *(b+8), *(b+9), *(b+10), *(b+11), *(b+12), *(b+13), *(b+14), *(b+15));
656 
657 		//Intel OTP map, try to read 320byts first.
658 		if (i == 21) {
659 			if ((*b) == 0) {
660 				dev->otp_size = 320;
661 				break;
662 			} else {
663 				b = buf;
664 				continue;
665 			}
666 		} else if (i ==
667 			   24) {		//if the first 320bytes data doesn't not exist, try to read the next 32bytes data.
668 			if ((*b) == 0) {
669 				dev->otp_size = 32;
670 				break;
671 			} else {
672 				b = buf;
673 				continue;
674 			}
675 		} else if (i ==
676 			   27) {		//if the prvious 32bytes data doesn't exist, try to read the next 32bytes data again.
677 			if ((*b) == 0) {
678 				dev->otp_size = 32;
679 				break;
680 			} else {
681 				dev->otp_size = 0;	// no OTP data.
682 				break;
683 			}
684 		}
685 
686 		b = b + OV5693_OTP_BANK_SIZE;
687 	}
688 	return 0;
689 }
690 
691 /*
692  * Read otp data and store it into a kmalloced buffer.
693  * The caller must kfree the buffer when no more needed.
694  * @size: set to the size of the returned otp data.
695  */
ov5693_otp_read(struct v4l2_subdev * sd)696 static void *ov5693_otp_read(struct v4l2_subdev *sd)
697 {
698 	struct i2c_client *client = v4l2_get_subdevdata(sd);
699 	u8 *buf;
700 	int ret;
701 
702 	buf = devm_kzalloc(&client->dev, (OV5693_OTP_DATA_SIZE + 16), GFP_KERNEL);
703 	if (!buf)
704 		return ERR_PTR(-ENOMEM);
705 
706 	//otp valid after mipi on and sw stream on
707 	ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x00);
708 
709 	ret = ov5693_write_reg(client, OV5693_8BIT,
710 			       OV5693_SW_STREAM, OV5693_START_STREAMING);
711 
712 	ret = __ov5693_otp_read(sd, buf);
713 
714 	//mipi off and sw stream off after otp read
715 	ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x0f);
716 
717 	ret = ov5693_write_reg(client, OV5693_8BIT,
718 			       OV5693_SW_STREAM, OV5693_STOP_STREAMING);
719 
720 	/* Driver has failed to find valid data */
721 	if (ret) {
722 		dev_err(&client->dev, "sensor found no valid OTP data\n");
723 		return ERR_PTR(ret);
724 	}
725 
726 	return buf;
727 }
728 
ov5693_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)729 static long ov5693_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
730 {
731 	switch (cmd) {
732 	case ATOMISP_IOC_S_EXPOSURE:
733 		return ov5693_s_exposure(sd, arg);
734 	default:
735 		return -EINVAL;
736 	}
737 	return 0;
738 }
739 
740 /*
741  * This returns the exposure time being used. This should only be used
742  * for filling in EXIF data, not for actual image processing.
743  */
ov5693_q_exposure(struct v4l2_subdev * sd,s32 * value)744 static int ov5693_q_exposure(struct v4l2_subdev *sd, s32 *value)
745 {
746 	struct i2c_client *client = v4l2_get_subdevdata(sd);
747 	u16 reg_v, reg_v2;
748 	int ret;
749 
750 	/* get exposure */
751 	ret = ov5693_read_reg(client, OV5693_8BIT,
752 			      OV5693_EXPOSURE_L,
753 			      &reg_v);
754 	if (ret)
755 		goto err;
756 
757 	ret = ov5693_read_reg(client, OV5693_8BIT,
758 			      OV5693_EXPOSURE_M,
759 			      &reg_v2);
760 	if (ret)
761 		goto err;
762 
763 	reg_v += reg_v2 << 8;
764 	ret = ov5693_read_reg(client, OV5693_8BIT,
765 			      OV5693_EXPOSURE_H,
766 			      &reg_v2);
767 	if (ret)
768 		goto err;
769 
770 	*value = reg_v + (((u32)reg_v2 << 16));
771 err:
772 	return ret;
773 }
774 
ad5823_t_focus_vcm(struct v4l2_subdev * sd,u16 val)775 static int ad5823_t_focus_vcm(struct v4l2_subdev *sd, u16 val)
776 {
777 	struct i2c_client *client = v4l2_get_subdevdata(sd);
778 	int ret;
779 	u8 vcm_code;
780 
781 	ret = ad5823_i2c_read(client, AD5823_REG_VCM_CODE_MSB, &vcm_code);
782 	if (ret)
783 		return ret;
784 
785 	/* set reg VCM_CODE_MSB Bit[1:0] */
786 	vcm_code = (vcm_code & VCM_CODE_MSB_MASK) |
787 		   ((val >> 8) & ~VCM_CODE_MSB_MASK);
788 	ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB, vcm_code);
789 	if (ret)
790 		return ret;
791 
792 	/* set reg VCM_CODE_LSB Bit[7:0] */
793 	ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_LSB, (val & 0xff));
794 	if (ret)
795 		return ret;
796 
797 	/* set required vcm move time */
798 	vcm_code = AD5823_RESONANCE_PERIOD / AD5823_RESONANCE_COEF
799 		   - AD5823_HIGH_FREQ_RANGE;
800 	ret = ad5823_i2c_write(client, AD5823_REG_VCM_MOVE_TIME, vcm_code);
801 
802 	return ret;
803 }
804 
ad5823_t_focus_abs(struct v4l2_subdev * sd,s32 value)805 static int ad5823_t_focus_abs(struct v4l2_subdev *sd, s32 value)
806 {
807 	value = min(value, AD5823_MAX_FOCUS_POS);
808 	return ad5823_t_focus_vcm(sd, value);
809 }
810 
ov5693_t_focus_abs(struct v4l2_subdev * sd,s32 value)811 static int ov5693_t_focus_abs(struct v4l2_subdev *sd, s32 value)
812 {
813 	struct ov5693_device *dev = to_ov5693_sensor(sd);
814 	struct i2c_client *client = v4l2_get_subdevdata(sd);
815 	int ret = 0;
816 
817 	dev_dbg(&client->dev, "%s: FOCUS_POS: 0x%x\n", __func__, value);
818 	value = clamp(value, 0, OV5693_VCM_MAX_FOCUS_POS);
819 	if (dev->vcm == VCM_DW9714) {
820 		if (dev->vcm_update) {
821 			ret = vcm_dw_i2c_write(client, VCM_PROTECTION_OFF);
822 			if (ret)
823 				return ret;
824 			ret = vcm_dw_i2c_write(client, DIRECT_VCM);
825 			if (ret)
826 				return ret;
827 			ret = vcm_dw_i2c_write(client, VCM_PROTECTION_ON);
828 			if (ret)
829 				return ret;
830 			dev->vcm_update = false;
831 		}
832 		ret = vcm_dw_i2c_write(client,
833 				       vcm_val(value, VCM_DEFAULT_S));
834 	} else if (dev->vcm == VCM_AD5823) {
835 		ad5823_t_focus_abs(sd, value);
836 	}
837 	if (ret == 0) {
838 		dev->number_of_steps = value - dev->focus;
839 		dev->focus = value;
840 		dev->timestamp_t_focus_abs = ktime_get();
841 	} else
842 		dev_err(&client->dev,
843 			"%s: i2c failed. ret %d\n", __func__, ret);
844 
845 	return ret;
846 }
847 
ov5693_t_focus_rel(struct v4l2_subdev * sd,s32 value)848 static int ov5693_t_focus_rel(struct v4l2_subdev *sd, s32 value)
849 {
850 	struct ov5693_device *dev = to_ov5693_sensor(sd);
851 
852 	return ov5693_t_focus_abs(sd, dev->focus + value);
853 }
854 
855 #define DELAY_PER_STEP_NS	1000000
856 #define DELAY_MAX_PER_STEP_NS	(1000000 * 1023)
ov5693_q_focus_status(struct v4l2_subdev * sd,s32 * value)857 static int ov5693_q_focus_status(struct v4l2_subdev *sd, s32 *value)
858 {
859 	u32 status = 0;
860 	struct ov5693_device *dev = to_ov5693_sensor(sd);
861 	ktime_t temptime;
862 	ktime_t timedelay = ns_to_ktime(min_t(u32,
863 					      abs(dev->number_of_steps) * DELAY_PER_STEP_NS,
864 					      DELAY_MAX_PER_STEP_NS));
865 
866 	temptime = ktime_sub(ktime_get(), (dev->timestamp_t_focus_abs));
867 	if (ktime_compare(temptime, timedelay) <= 0) {
868 		status |= ATOMISP_FOCUS_STATUS_MOVING;
869 		status |= ATOMISP_FOCUS_HP_IN_PROGRESS;
870 	} else {
871 		status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE;
872 		status |= ATOMISP_FOCUS_HP_COMPLETE;
873 	}
874 
875 	*value = status;
876 
877 	return 0;
878 }
879 
ov5693_q_focus_abs(struct v4l2_subdev * sd,s32 * value)880 static int ov5693_q_focus_abs(struct v4l2_subdev *sd, s32 *value)
881 {
882 	struct ov5693_device *dev = to_ov5693_sensor(sd);
883 	s32 val;
884 
885 	ov5693_q_focus_status(sd, &val);
886 
887 	if (val & ATOMISP_FOCUS_STATUS_MOVING)
888 		*value  = dev->focus - dev->number_of_steps;
889 	else
890 		*value  = dev->focus;
891 
892 	return 0;
893 }
894 
ov5693_t_vcm_slew(struct v4l2_subdev * sd,s32 value)895 static int ov5693_t_vcm_slew(struct v4l2_subdev *sd, s32 value)
896 {
897 	struct ov5693_device *dev = to_ov5693_sensor(sd);
898 
899 	dev->number_of_steps = value;
900 	dev->vcm_update = true;
901 	return 0;
902 }
903 
ov5693_t_vcm_timing(struct v4l2_subdev * sd,s32 value)904 static int ov5693_t_vcm_timing(struct v4l2_subdev *sd, s32 value)
905 {
906 	struct ov5693_device *dev = to_ov5693_sensor(sd);
907 
908 	dev->number_of_steps = value;
909 	dev->vcm_update = true;
910 	return 0;
911 }
912 
ov5693_s_ctrl(struct v4l2_ctrl * ctrl)913 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
914 {
915 	struct ov5693_device *dev =
916 	    container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
917 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
918 	int ret = 0;
919 
920 	switch (ctrl->id) {
921 	case V4L2_CID_FOCUS_ABSOLUTE:
922 		dev_dbg(&client->dev, "%s: CID_FOCUS_ABSOLUTE:%d.\n",
923 			__func__, ctrl->val);
924 		ret = ov5693_t_focus_abs(&dev->sd, ctrl->val);
925 		break;
926 	case V4L2_CID_FOCUS_RELATIVE:
927 		dev_dbg(&client->dev, "%s: CID_FOCUS_RELATIVE:%d.\n",
928 			__func__, ctrl->val);
929 		ret = ov5693_t_focus_rel(&dev->sd, ctrl->val);
930 		break;
931 	case V4L2_CID_VCM_SLEW:
932 		ret = ov5693_t_vcm_slew(&dev->sd, ctrl->val);
933 		break;
934 	case V4L2_CID_VCM_TIMING:
935 		ret = ov5693_t_vcm_timing(&dev->sd, ctrl->val);
936 		break;
937 	default:
938 		ret = -EINVAL;
939 	}
940 	return ret;
941 }
942 
ov5693_g_volatile_ctrl(struct v4l2_ctrl * ctrl)943 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
944 {
945 	struct ov5693_device *dev =
946 	    container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
947 	int ret = 0;
948 
949 	switch (ctrl->id) {
950 	case V4L2_CID_EXPOSURE_ABSOLUTE:
951 		ret = ov5693_q_exposure(&dev->sd, &ctrl->val);
952 		break;
953 	case V4L2_CID_FOCUS_ABSOLUTE:
954 		ret = ov5693_q_focus_abs(&dev->sd, &ctrl->val);
955 		break;
956 	case V4L2_CID_FOCUS_STATUS:
957 		ret = ov5693_q_focus_status(&dev->sd, &ctrl->val);
958 		break;
959 	default:
960 		ret = -EINVAL;
961 	}
962 
963 	return ret;
964 }
965 
966 static const struct v4l2_ctrl_ops ctrl_ops = {
967 	.s_ctrl = ov5693_s_ctrl,
968 	.g_volatile_ctrl = ov5693_g_volatile_ctrl
969 };
970 
971 static const struct v4l2_ctrl_config ov5693_controls[] = {
972 	{
973 		.ops = &ctrl_ops,
974 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
975 		.type = V4L2_CTRL_TYPE_INTEGER,
976 		.name = "exposure",
977 		.min = 0x0,
978 		.max = 0xffff,
979 		.step = 0x01,
980 		.def = 0x00,
981 		.flags = 0,
982 	},
983 	{
984 		.ops = &ctrl_ops,
985 		.id = V4L2_CID_FOCUS_ABSOLUTE,
986 		.type = V4L2_CTRL_TYPE_INTEGER,
987 		.name = "focus move absolute",
988 		.min = 0,
989 		.max = OV5693_VCM_MAX_FOCUS_POS,
990 		.step = 1,
991 		.def = 0,
992 		.flags = 0,
993 	},
994 	{
995 		.ops = &ctrl_ops,
996 		.id = V4L2_CID_FOCUS_RELATIVE,
997 		.type = V4L2_CTRL_TYPE_INTEGER,
998 		.name = "focus move relative",
999 		.min = OV5693_VCM_MAX_FOCUS_NEG,
1000 		.max = OV5693_VCM_MAX_FOCUS_POS,
1001 		.step = 1,
1002 		.def = 0,
1003 		.flags = 0,
1004 	},
1005 	{
1006 		.ops = &ctrl_ops,
1007 		.id = V4L2_CID_FOCUS_STATUS,
1008 		.type = V4L2_CTRL_TYPE_INTEGER,
1009 		.name = "focus status",
1010 		.min = 0,
1011 		.max = 100,		/* allow enum to grow in the future */
1012 		.step = 1,
1013 		.def = 0,
1014 		.flags = 0,
1015 	},
1016 	{
1017 		.ops = &ctrl_ops,
1018 		.id = V4L2_CID_VCM_SLEW,
1019 		.type = V4L2_CTRL_TYPE_INTEGER,
1020 		.name = "vcm slew",
1021 		.min = 0,
1022 		.max = OV5693_VCM_SLEW_STEP_MAX,
1023 		.step = 1,
1024 		.def = 0,
1025 		.flags = 0,
1026 	},
1027 	{
1028 		.ops = &ctrl_ops,
1029 		.id = V4L2_CID_VCM_TIMING,
1030 		.type = V4L2_CTRL_TYPE_INTEGER,
1031 		.name = "vcm step time",
1032 		.min = 0,
1033 		.max = OV5693_VCM_SLEW_TIME_MAX,
1034 		.step = 1,
1035 		.def = 0,
1036 		.flags = 0,
1037 	},
1038 };
1039 
ov5693_init(struct v4l2_subdev * sd)1040 static int ov5693_init(struct v4l2_subdev *sd)
1041 {
1042 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1043 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1044 	int ret;
1045 
1046 	pr_info("%s\n", __func__);
1047 	mutex_lock(&dev->input_lock);
1048 	dev->vcm_update = false;
1049 
1050 	if (dev->vcm == VCM_AD5823) {
1051 		ret = vcm_ad_i2c_wr8(client, 0x01, 0x01); /* vcm init test */
1052 		if (ret)
1053 			dev_err(&client->dev,
1054 				"vcm reset failed\n");
1055 		/*change the mode*/
1056 		ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB,
1057 				       AD5823_RING_CTRL_ENABLE);
1058 		if (ret)
1059 			dev_err(&client->dev,
1060 				"vcm enable ringing failed\n");
1061 		ret = ad5823_i2c_write(client, AD5823_REG_MODE,
1062 				       AD5823_ARC_RES1);
1063 		if (ret)
1064 			dev_err(&client->dev,
1065 				"vcm change mode failed\n");
1066 	}
1067 
1068 	/*change initial focus value for ad5823*/
1069 	if (dev->vcm == VCM_AD5823) {
1070 		dev->focus = AD5823_INIT_FOCUS_POS;
1071 		ov5693_t_focus_abs(sd, AD5823_INIT_FOCUS_POS);
1072 	} else {
1073 		dev->focus = 0;
1074 		ov5693_t_focus_abs(sd, 0);
1075 	}
1076 
1077 	mutex_unlock(&dev->input_lock);
1078 
1079 	return 0;
1080 }
1081 
power_ctrl(struct v4l2_subdev * sd,bool flag)1082 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
1083 {
1084 	int ret;
1085 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1086 
1087 	if (!dev || !dev->platform_data)
1088 		return -ENODEV;
1089 
1090 	/*
1091 	 * This driver assumes "internal DVDD, PWDNB tied to DOVDD".
1092 	 * In this set up only gpio0 (XSHUTDN) should be available
1093 	 * but in some products (for example ECS) gpio1 (PWDNB) is
1094 	 * also available. If gpio1 is available we emulate it being
1095 	 * tied to DOVDD here.
1096 	 */
1097 	if (flag) {
1098 		ret = dev->platform_data->v2p8_ctrl(sd, 1);
1099 		dev->platform_data->gpio1_ctrl(sd, 1);
1100 		if (ret == 0) {
1101 			ret = dev->platform_data->v1p8_ctrl(sd, 1);
1102 			if (ret) {
1103 				dev->platform_data->gpio1_ctrl(sd, 0);
1104 				ret = dev->platform_data->v2p8_ctrl(sd, 0);
1105 			}
1106 		}
1107 	} else {
1108 		dev->platform_data->gpio1_ctrl(sd, 0);
1109 		ret = dev->platform_data->v1p8_ctrl(sd, 0);
1110 		ret |= dev->platform_data->v2p8_ctrl(sd, 0);
1111 	}
1112 
1113 	return ret;
1114 }
1115 
gpio_ctrl(struct v4l2_subdev * sd,bool flag)1116 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
1117 {
1118 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1119 
1120 	if (!dev || !dev->platform_data)
1121 		return -ENODEV;
1122 
1123 	return dev->platform_data->gpio0_ctrl(sd, flag);
1124 }
1125 
__power_up(struct v4l2_subdev * sd)1126 static int __power_up(struct v4l2_subdev *sd)
1127 {
1128 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1129 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1130 	int ret;
1131 
1132 	if (!dev->platform_data) {
1133 		dev_err(&client->dev,
1134 			"no camera_sensor_platform_data");
1135 		return -ENODEV;
1136 	}
1137 
1138 	/* power control */
1139 	ret = power_ctrl(sd, 1);
1140 	if (ret)
1141 		goto fail_power;
1142 
1143 	/* according to DS, at least 5ms is needed between DOVDD and PWDN */
1144 	/* add this delay time to 10~11ms*/
1145 	usleep_range(10000, 11000);
1146 
1147 	/* gpio ctrl */
1148 	ret = gpio_ctrl(sd, 1);
1149 	if (ret) {
1150 		ret = gpio_ctrl(sd, 1);
1151 		if (ret)
1152 			goto fail_power;
1153 	}
1154 
1155 	/* flis clock control */
1156 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
1157 	if (ret)
1158 		goto fail_clk;
1159 
1160 	__cci_delay(up_delay);
1161 
1162 	return 0;
1163 
1164 fail_clk:
1165 	gpio_ctrl(sd, 0);
1166 fail_power:
1167 	power_ctrl(sd, 0);
1168 	dev_err(&client->dev, "sensor power-up failed\n");
1169 
1170 	return ret;
1171 }
1172 
power_down(struct v4l2_subdev * sd)1173 static int power_down(struct v4l2_subdev *sd)
1174 {
1175 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1176 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1177 	int ret = 0;
1178 
1179 	dev->focus = OV5693_INVALID_CONFIG;
1180 	if (!dev->platform_data) {
1181 		dev_err(&client->dev,
1182 			"no camera_sensor_platform_data");
1183 		return -ENODEV;
1184 	}
1185 
1186 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
1187 	if (ret)
1188 		dev_err(&client->dev, "flisclk failed\n");
1189 
1190 	/* gpio ctrl */
1191 	ret = gpio_ctrl(sd, 0);
1192 	if (ret) {
1193 		ret = gpio_ctrl(sd, 0);
1194 		if (ret)
1195 			dev_err(&client->dev, "gpio failed 2\n");
1196 	}
1197 
1198 	/* power control */
1199 	ret = power_ctrl(sd, 0);
1200 	if (ret)
1201 		dev_err(&client->dev, "vprog failed.\n");
1202 
1203 	return ret;
1204 }
1205 
power_up(struct v4l2_subdev * sd)1206 static int power_up(struct v4l2_subdev *sd)
1207 {
1208 	static const int retry_count = 4;
1209 	int i, ret;
1210 
1211 	for (i = 0; i < retry_count; i++) {
1212 		ret = __power_up(sd);
1213 		if (!ret)
1214 			return 0;
1215 
1216 		power_down(sd);
1217 	}
1218 	return ret;
1219 }
1220 
ov5693_s_power(struct v4l2_subdev * sd,int on)1221 static int ov5693_s_power(struct v4l2_subdev *sd, int on)
1222 {
1223 	int ret;
1224 
1225 	pr_info("%s: on %d\n", __func__, on);
1226 	if (on == 0)
1227 		return power_down(sd);
1228 	else {
1229 		ret = power_up(sd);
1230 		if (!ret) {
1231 			ret = ov5693_init(sd);
1232 			/* restore settings */
1233 			ov5693_res = ov5693_res_preview;
1234 			N_RES = N_RES_PREVIEW;
1235 		}
1236 	}
1237 	return ret;
1238 }
1239 
1240 /*
1241  * distance - calculate the distance
1242  * @res: resolution
1243  * @w: width
1244  * @h: height
1245  *
1246  * Get the gap between res_w/res_h and w/h.
1247  * distance = (res_w/res_h - w/h) / (w/h) * 8192
1248  * res->width/height smaller than w/h wouldn't be considered.
1249  * The gap of ratio larger than 1/8 wouldn't be considered.
1250  * Returns the value of gap or -1 if fail.
1251  */
1252 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
distance(struct ov5693_resolution * res,u32 w,u32 h)1253 static int distance(struct ov5693_resolution *res, u32 w, u32 h)
1254 {
1255 	int ratio;
1256 	int distance;
1257 
1258 	if (w == 0 || h == 0 ||
1259 	    res->width < w || res->height < h)
1260 		return -1;
1261 
1262 	ratio = res->width << 13;
1263 	ratio /= w;
1264 	ratio *= h;
1265 	ratio /= res->height;
1266 
1267 	distance = abs(ratio - 8192);
1268 
1269 	if (distance > LARGEST_ALLOWED_RATIO_MISMATCH)
1270 		return -1;
1271 
1272 	return distance;
1273 }
1274 
1275 /* Return the nearest higher resolution index
1276  * Firstly try to find the approximate aspect ratio resolution
1277  * If we find multiple same AR resolutions, choose the
1278  * minimal size.
1279  */
nearest_resolution_index(int w,int h)1280 static int nearest_resolution_index(int w, int h)
1281 {
1282 	int i;
1283 	int idx = -1;
1284 	int dist;
1285 	int min_dist = INT_MAX;
1286 	int min_res_w = INT_MAX;
1287 	struct ov5693_resolution *tmp_res = NULL;
1288 
1289 	for (i = 0; i < N_RES; i++) {
1290 		tmp_res = &ov5693_res[i];
1291 		dist = distance(tmp_res, w, h);
1292 		if (dist == -1)
1293 			continue;
1294 		if (dist < min_dist) {
1295 			min_dist = dist;
1296 			idx = i;
1297 			min_res_w = ov5693_res[i].width;
1298 			continue;
1299 		}
1300 		if (dist == min_dist && ov5693_res[i].width < min_res_w)
1301 			idx = i;
1302 	}
1303 
1304 	return idx;
1305 }
1306 
get_resolution_index(int w,int h)1307 static int get_resolution_index(int w, int h)
1308 {
1309 	int i;
1310 
1311 	for (i = 0; i < N_RES; i++) {
1312 		if (w != ov5693_res[i].width)
1313 			continue;
1314 		if (h != ov5693_res[i].height)
1315 			continue;
1316 
1317 		return i;
1318 	}
1319 
1320 	return -1;
1321 }
1322 
1323 /* TODO: remove it. */
startup(struct v4l2_subdev * sd)1324 static int startup(struct v4l2_subdev *sd)
1325 {
1326 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1327 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1328 	int ret = 0;
1329 
1330 	ret = ov5693_write_reg(client, OV5693_8BIT,
1331 			       OV5693_SW_RESET, 0x01);
1332 	if (ret) {
1333 		dev_err(&client->dev, "ov5693 reset err.\n");
1334 		return ret;
1335 	}
1336 
1337 	ret = ov5693_write_reg_array(client, ov5693_global_setting);
1338 	if (ret) {
1339 		dev_err(&client->dev, "ov5693 write register err.\n");
1340 		return ret;
1341 	}
1342 
1343 	ret = ov5693_write_reg_array(client, ov5693_res[dev->fmt_idx].regs);
1344 	if (ret) {
1345 		dev_err(&client->dev, "ov5693 write register err.\n");
1346 		return ret;
1347 	}
1348 
1349 	return ret;
1350 }
1351 
ov5693_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)1352 static int ov5693_set_fmt(struct v4l2_subdev *sd,
1353 			  struct v4l2_subdev_state *sd_state,
1354 			  struct v4l2_subdev_format *format)
1355 {
1356 	struct v4l2_mbus_framefmt *fmt = &format->format;
1357 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1358 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1359 	struct camera_mipi_info *ov5693_info = NULL;
1360 	int ret = 0;
1361 	int idx;
1362 
1363 	if (format->pad)
1364 		return -EINVAL;
1365 	if (!fmt)
1366 		return -EINVAL;
1367 	ov5693_info = v4l2_get_subdev_hostdata(sd);
1368 	if (!ov5693_info)
1369 		return -EINVAL;
1370 
1371 	mutex_lock(&dev->input_lock);
1372 	idx = nearest_resolution_index(fmt->width, fmt->height);
1373 	if (idx == -1) {
1374 		/* return the largest resolution */
1375 		fmt->width = ov5693_res[N_RES - 1].width;
1376 		fmt->height = ov5693_res[N_RES - 1].height;
1377 	} else {
1378 		fmt->width = ov5693_res[idx].width;
1379 		fmt->height = ov5693_res[idx].height;
1380 	}
1381 
1382 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1383 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1384 		sd_state->pads->try_fmt = *fmt;
1385 		mutex_unlock(&dev->input_lock);
1386 		return 0;
1387 	}
1388 
1389 	dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1390 	if (dev->fmt_idx == -1) {
1391 		dev_err(&client->dev, "get resolution fail\n");
1392 		mutex_unlock(&dev->input_lock);
1393 		return -EINVAL;
1394 	}
1395 
1396 	ret = startup(sd);
1397 	if (ret) {
1398 		int i = 0;
1399 
1400 		dev_err(&client->dev, "ov5693 startup err, retry to power up\n");
1401 		for (i = 0; i < OV5693_POWER_UP_RETRY_NUM; i++) {
1402 			dev_err(&client->dev,
1403 				"ov5693 retry to power up %d/%d times, result: ",
1404 				i + 1, OV5693_POWER_UP_RETRY_NUM);
1405 			power_down(sd);
1406 			ret = power_up(sd);
1407 			if (!ret) {
1408 				mutex_unlock(&dev->input_lock);
1409 				ov5693_init(sd);
1410 				mutex_lock(&dev->input_lock);
1411 			} else {
1412 				dev_err(&client->dev, "power up failed, continue\n");
1413 				continue;
1414 			}
1415 			ret = startup(sd);
1416 			if (ret) {
1417 				dev_err(&client->dev, " startup FAILED!\n");
1418 			} else {
1419 				dev_err(&client->dev, " startup SUCCESS!\n");
1420 				break;
1421 			}
1422 		}
1423 	}
1424 
1425 	/*
1426 	 * After sensor settings are set to HW, sometimes stream is started.
1427 	 * This would cause ISP timeout because ISP is not ready to receive
1428 	 * data yet. So add stop streaming here.
1429 	 */
1430 	ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1431 			       OV5693_STOP_STREAMING);
1432 	if (ret)
1433 		dev_warn(&client->dev, "ov5693 stream off err\n");
1434 
1435 	ov5693_info->metadata_width = fmt->width * 10 / 8;
1436 	ov5693_info->metadata_height = 1;
1437 	ov5693_info->metadata_effective_width = &ov5693_embedded_effective_size;
1438 
1439 	mutex_unlock(&dev->input_lock);
1440 	return ret;
1441 }
1442 
ov5693_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)1443 static int ov5693_get_fmt(struct v4l2_subdev *sd,
1444 			  struct v4l2_subdev_state *sd_state,
1445 			  struct v4l2_subdev_format *format)
1446 {
1447 	struct v4l2_mbus_framefmt *fmt = &format->format;
1448 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1449 
1450 	if (format->pad)
1451 		return -EINVAL;
1452 
1453 	if (!fmt)
1454 		return -EINVAL;
1455 
1456 	fmt->width = ov5693_res[dev->fmt_idx].width;
1457 	fmt->height = ov5693_res[dev->fmt_idx].height;
1458 	fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1459 
1460 	return 0;
1461 }
1462 
ov5693_detect(struct i2c_client * client)1463 static int ov5693_detect(struct i2c_client *client)
1464 {
1465 	struct i2c_adapter *adapter = client->adapter;
1466 	u16 high, low;
1467 	int ret;
1468 	u16 id;
1469 	u8 revision;
1470 
1471 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1472 		return -ENODEV;
1473 
1474 	ret = ov5693_read_reg(client, OV5693_8BIT,
1475 			      OV5693_SC_CMMN_CHIP_ID_H, &high);
1476 	if (ret) {
1477 		dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1478 		return -ENODEV;
1479 	}
1480 	ret = ov5693_read_reg(client, OV5693_8BIT,
1481 			      OV5693_SC_CMMN_CHIP_ID_L, &low);
1482 	if (ret)
1483 		return ret;
1484 	id = ((((u16)high) << 8) | (u16)low);
1485 
1486 	if (id != OV5693_ID) {
1487 		dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1488 		return -ENODEV;
1489 	}
1490 
1491 	ret = ov5693_read_reg(client, OV5693_8BIT,
1492 			      OV5693_SC_CMMN_SUB_ID, &high);
1493 	revision = (u8)high & 0x0f;
1494 
1495 	dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
1496 	dev_dbg(&client->dev, "detect ov5693 success\n");
1497 	return 0;
1498 }
1499 
ov5693_s_stream(struct v4l2_subdev * sd,int enable)1500 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
1501 {
1502 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1503 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1504 	int ret;
1505 
1506 	mutex_lock(&dev->input_lock);
1507 
1508 	ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1509 			       enable ? OV5693_START_STREAMING :
1510 			       OV5693_STOP_STREAMING);
1511 
1512 	mutex_unlock(&dev->input_lock);
1513 
1514 	return ret;
1515 }
1516 
ov5693_s_config(struct v4l2_subdev * sd,int irq,void * platform_data)1517 static int ov5693_s_config(struct v4l2_subdev *sd,
1518 			   int irq, void *platform_data)
1519 {
1520 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1521 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1522 	int ret = 0;
1523 
1524 	if (!platform_data)
1525 		return -ENODEV;
1526 
1527 	dev->platform_data =
1528 	    (struct camera_sensor_platform_data *)platform_data;
1529 
1530 	mutex_lock(&dev->input_lock);
1531 	/* power off the module, then power on it in future
1532 	 * as first power on by board may not fulfill the
1533 	 * power on sequqence needed by the module
1534 	 */
1535 	ret = power_down(sd);
1536 	if (ret) {
1537 		dev_err(&client->dev, "ov5693 power-off err.\n");
1538 		goto fail_power_off;
1539 	}
1540 
1541 	ret = power_up(sd);
1542 	if (ret) {
1543 		dev_err(&client->dev, "ov5693 power-up err.\n");
1544 		goto fail_power_on;
1545 	}
1546 
1547 	if (!dev->vcm)
1548 		dev->vcm = vcm_detect(client);
1549 
1550 	ret = dev->platform_data->csi_cfg(sd, 1);
1551 	if (ret)
1552 		goto fail_csi_cfg;
1553 
1554 	/* config & detect sensor */
1555 	ret = ov5693_detect(client);
1556 	if (ret) {
1557 		dev_err(&client->dev, "ov5693_detect err s_config.\n");
1558 		goto fail_csi_cfg;
1559 	}
1560 
1561 	dev->otp_data = ov5693_otp_read(sd);
1562 
1563 	/* turn off sensor, after probed */
1564 	ret = power_down(sd);
1565 	if (ret) {
1566 		dev_err(&client->dev, "ov5693 power-off err.\n");
1567 		goto fail_csi_cfg;
1568 	}
1569 	mutex_unlock(&dev->input_lock);
1570 
1571 	return ret;
1572 
1573 fail_csi_cfg:
1574 	dev->platform_data->csi_cfg(sd, 0);
1575 fail_power_on:
1576 	power_down(sd);
1577 	dev_err(&client->dev, "sensor power-gating failed\n");
1578 fail_power_off:
1579 	mutex_unlock(&dev->input_lock);
1580 	return ret;
1581 }
1582 
ov5693_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * interval)1583 static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
1584 				   struct v4l2_subdev_frame_interval *interval)
1585 {
1586 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1587 
1588 	interval->interval.numerator = 1;
1589 	interval->interval.denominator = ov5693_res[dev->fmt_idx].fps;
1590 
1591 	return 0;
1592 }
1593 
ov5693_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)1594 static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
1595 				 struct v4l2_subdev_state *sd_state,
1596 				 struct v4l2_subdev_mbus_code_enum *code)
1597 {
1598 	if (code->index >= MAX_FMTS)
1599 		return -EINVAL;
1600 
1601 	code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1602 	return 0;
1603 }
1604 
ov5693_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)1605 static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
1606 				  struct v4l2_subdev_state *sd_state,
1607 				  struct v4l2_subdev_frame_size_enum *fse)
1608 {
1609 	int index = fse->index;
1610 
1611 	if (index >= N_RES)
1612 		return -EINVAL;
1613 
1614 	fse->min_width = ov5693_res[index].width;
1615 	fse->min_height = ov5693_res[index].height;
1616 	fse->max_width = ov5693_res[index].width;
1617 	fse->max_height = ov5693_res[index].height;
1618 
1619 	return 0;
1620 }
1621 
1622 static const struct v4l2_subdev_video_ops ov5693_video_ops = {
1623 	.s_stream = ov5693_s_stream,
1624 	.g_frame_interval = ov5693_g_frame_interval,
1625 };
1626 
1627 static const struct v4l2_subdev_core_ops ov5693_core_ops = {
1628 	.s_power = ov5693_s_power,
1629 	.ioctl = ov5693_ioctl,
1630 };
1631 
1632 static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
1633 	.enum_mbus_code = ov5693_enum_mbus_code,
1634 	.enum_frame_size = ov5693_enum_frame_size,
1635 	.get_fmt = ov5693_get_fmt,
1636 	.set_fmt = ov5693_set_fmt,
1637 };
1638 
1639 static const struct v4l2_subdev_ops ov5693_ops = {
1640 	.core = &ov5693_core_ops,
1641 	.video = &ov5693_video_ops,
1642 	.pad = &ov5693_pad_ops,
1643 };
1644 
ov5693_remove(struct i2c_client * client)1645 static void ov5693_remove(struct i2c_client *client)
1646 {
1647 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1648 	struct ov5693_device *dev = to_ov5693_sensor(sd);
1649 
1650 	dev_dbg(&client->dev, "ov5693_remove...\n");
1651 
1652 	dev->platform_data->csi_cfg(sd, 0);
1653 
1654 	v4l2_device_unregister_subdev(sd);
1655 
1656 	atomisp_gmin_remove_subdev(sd);
1657 
1658 	media_entity_cleanup(&dev->sd.entity);
1659 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1660 	kfree(dev);
1661 }
1662 
ov5693_probe(struct i2c_client * client)1663 static int ov5693_probe(struct i2c_client *client)
1664 {
1665 	struct ov5693_device *dev;
1666 	int i2c;
1667 	int ret;
1668 	void *pdata;
1669 	unsigned int i;
1670 
1671 	/*
1672 	 * Firmware workaround: Some modules use a "secondary default"
1673 	 * address of 0x10 which doesn't appear on schematics, and
1674 	 * some BIOS versions haven't gotten the memo.  Work around
1675 	 * via config.
1676 	 */
1677 	i2c = gmin_get_var_int(&client->dev, false, "I2CAddr", -1);
1678 	if (i2c != -1) {
1679 		dev_info(&client->dev,
1680 			 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n",
1681 			 client->addr, i2c);
1682 		client->addr = i2c;
1683 	}
1684 
1685 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1686 	if (!dev)
1687 		return -ENOMEM;
1688 
1689 	mutex_init(&dev->input_lock);
1690 
1691 	dev->fmt_idx = 0;
1692 	v4l2_i2c_subdev_init(&dev->sd, client, &ov5693_ops);
1693 
1694 	pdata = gmin_camera_platform_data(&dev->sd,
1695 					  ATOMISP_INPUT_FORMAT_RAW_10,
1696 					  atomisp_bayer_order_bggr);
1697 	if (!pdata) {
1698 		ret = -EINVAL;
1699 		goto out_free;
1700 	}
1701 
1702 	ret = ov5693_s_config(&dev->sd, client->irq, pdata);
1703 	if (ret)
1704 		goto out_free;
1705 
1706 	ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1707 	if (ret)
1708 		goto out_free;
1709 
1710 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1711 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1712 	dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1713 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1714 	ret =
1715 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1716 				   ARRAY_SIZE(ov5693_controls));
1717 	if (ret) {
1718 		ov5693_remove(client);
1719 		return ret;
1720 	}
1721 
1722 	for (i = 0; i < ARRAY_SIZE(ov5693_controls); i++)
1723 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov5693_controls[i],
1724 				     NULL);
1725 
1726 	if (dev->ctrl_handler.error) {
1727 		ov5693_remove(client);
1728 		return dev->ctrl_handler.error;
1729 	}
1730 
1731 	/* Use same lock for controls as for everything else. */
1732 	dev->ctrl_handler.lock = &dev->input_lock;
1733 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1734 
1735 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1736 	if (ret)
1737 		ov5693_remove(client);
1738 
1739 	return ret;
1740 out_free:
1741 	v4l2_device_unregister_subdev(&dev->sd);
1742 	kfree(dev);
1743 	return ret;
1744 }
1745 
1746 static const struct acpi_device_id ov5693_acpi_match[] = {
1747 	{"INT33BE"},
1748 	{},
1749 };
1750 MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
1751 
1752 static struct i2c_driver ov5693_driver = {
1753 	.driver = {
1754 		.name = "ov5693",
1755 		.acpi_match_table = ov5693_acpi_match,
1756 	},
1757 	.probe = ov5693_probe,
1758 	.remove = ov5693_remove,
1759 };
1760 module_i2c_driver(ov5693_driver);
1761 
1762 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
1763 MODULE_LICENSE("GPL");
1764