1 // SPDX-License-Identifier: GPL-2.0
3 * Support for OmniVision OV5693 1080p HD camera sensor.
5 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
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.
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.
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.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>
34 #include <linux/acpi.h>
35 #include "../../include/linux/atomisp_gmin_platform.h"
40 #define __cci_delay(t) \
43 usleep_range((t) * 1000, ((t) + 1) * 1000); \
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.
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");
60 static int vcm_ad_i2c_wr8(struct i2c_client *client, u8 reg, u8 val)
74 err = i2c_transfer(client->adapter, &msg, 1);
76 dev_err(&client->dev, "%s: vcm i2c fail, err code = %d\n",
83 static int ad5823_i2c_write(struct i2c_client *client, u8 reg, u8 val)
90 msg.addr = AD5823_VCM_ADDR;
95 if (i2c_transfer(client->adapter, &msg, 1) != 1)
100 static int ad5823_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
102 struct i2c_msg msg[2];
108 msg[0].addr = AD5823_VCM_ADDR;
111 msg[0].buf = &buf[0];
114 msg[1].flags = I2C_M_RD;
116 msg[1].buf = &buf[1];
118 if (i2c_transfer(client->adapter, msg, 2) != 2)
124 static const u32 ov5693_embedded_effective_size = 28;
126 /* i2c read/write stuff */
127 static int ov5693_read_reg(struct i2c_client *client,
128 u16 data_length, u16 reg, u16 *val)
131 struct i2c_msg msg[2];
132 unsigned char data[6];
134 if (!client->adapter) {
135 dev_err(&client->dev, "%s error, no client->adapter\n",
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",
147 memset(msg, 0, sizeof(msg));
149 msg[0].addr = client->addr;
151 msg[0].len = I2C_MSG_LENGTH;
154 /* high byte goes out first */
155 data[0] = (u8)(reg >> 8);
156 data[1] = (u8)(reg & 0xff);
158 msg[1].addr = client->addr;
159 msg[1].len = data_length;
160 msg[1].flags = I2C_M_RD;
163 err = i2c_transfer(client->adapter, msg, 2);
167 dev_err(&client->dev,
168 "read from offset 0x%x error %d", reg, err);
173 /* high byte comes first */
174 if (data_length == OV5693_8BIT)
176 else if (data_length == OV5693_16BIT)
177 *val = be16_to_cpu(*(__be16 *)&data[0]);
179 *val = be32_to_cpu(*(__be32 *)&data[0]);
184 static int ov5693_i2c_write(struct i2c_client *client, u16 len, u8 *data)
187 const int num_msg = 1;
190 msg.addr = client->addr;
194 ret = i2c_transfer(client->adapter, &msg, 1);
196 return ret == num_msg ? 0 : -EIO;
199 static int vcm_dw_i2c_write(struct i2c_client *client, u16 data)
202 const int num_msg = 1;
206 val = cpu_to_be16(data);
209 msg.len = OV5693_16BIT;
210 msg.buf = (void *)&val;
212 ret = i2c_transfer(client->adapter, &msg, 1);
214 return ret == num_msg ? 0 : -EIO;
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
231 static int vcm_detect(struct i2c_client *client)
237 for (i = 0; i < 4; i++) {
239 msg.flags = I2C_M_RD;
240 msg.len = sizeof(data);
241 msg.buf = (u8 *)&data;
242 ret = i2c_transfer(client->adapter, &msg, 1);
245 * DW9714 always fails the first read and returns
246 * zeroes for subsequent ones
248 if (i == 0 && ret == -EREMOTEIO) {
259 return ret == 1 ? VCM_DW9714 : ret;
262 static int ov5693_write_reg(struct i2c_client *client, u16 data_length,
266 unsigned char data[4] = {0};
267 __be16 *wreg = (void *)data;
268 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
270 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT) {
271 dev_err(&client->dev,
272 "%s error, invalid data_length\n", __func__);
276 /* high byte goes out first */
277 *wreg = cpu_to_be16(reg);
279 if (data_length == OV5693_8BIT) {
283 __be16 *wdata = (void *)&data[2];
285 *wdata = cpu_to_be16(val);
288 ret = ov5693_i2c_write(client, len, data);
290 dev_err(&client->dev,
291 "write error: wrote 0x%x to offset 0x%x error %d",
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
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().
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.
312 static int __ov5693_flush_reg_array(struct i2c_client *client,
313 struct ov5693_write_ctrl *ctrl)
316 __be16 *reg = (void *)&ctrl->buffer.addr;
318 if (ctrl->index == 0)
321 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
323 *reg = cpu_to_be16(ctrl->buffer.addr);
326 return ov5693_i2c_write(client, size, (u8 *)reg);
329 static int __ov5693_buf_reg_array(struct i2c_client *client,
330 struct ov5693_write_ctrl *ctrl,
331 const struct ov5693_reg *next)
336 switch (next->type) {
339 ctrl->buffer.data[ctrl->index] = (u8)next->val;
344 data16 = (void *)&ctrl->buffer.data[ctrl->index];
345 *data16 = cpu_to_be16((u16)next->val);
351 /* When first item is added, we need to store its starting address */
352 if (ctrl->index == 0)
353 ctrl->buffer.addr = next->reg;
358 * Buffer cannot guarantee free space for u32? Better flush it to avoid
359 * possible lack of memory for next item.
361 if (ctrl->index + sizeof(u16) >= OV5693_MAX_WRITE_BUF_SIZE)
362 return __ov5693_flush_reg_array(client, ctrl);
367 static int __ov5693_write_reg_is_consecutive(struct i2c_client *client,
368 struct ov5693_write_ctrl *ctrl,
369 const struct ov5693_reg *next)
371 if (ctrl->index == 0)
374 return ctrl->buffer.addr + ctrl->index == next->reg;
377 static int ov5693_write_reg_array(struct i2c_client *client,
378 const struct ov5693_reg *reglist)
380 const struct ov5693_reg *next = reglist;
381 struct ov5693_write_ctrl ctrl;
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);
395 * If next address is not consecutive, data needs to be
396 * flushed before proceed.
398 if (!__ov5693_write_reg_is_consecutive(client, &ctrl,
400 err = __ov5693_flush_reg_array(client, &ctrl);
404 err = __ov5693_buf_reg_array(client, &ctrl, next);
406 dev_err(&client->dev,
407 "%s: write error, aborted\n",
415 return __ov5693_flush_reg_array(client, &ctrl);
418 static long __ov5693_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
419 int gain, int digitgain)
422 struct i2c_client *client = v4l2_get_subdevdata(sd);
423 struct ov5693_device *dev = to_ov5693_sensor(sd);
427 hts = ov5693_res[dev->fmt_idx].pixels_per_line;
428 vts = ov5693_res[dev->fmt_idx].lines_per_frame;
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
434 if (coarse_itg > (1 << 15)) {
436 coarse_itg = (int)coarse_itg / 2;
439 ret = ov5693_write_reg(client, OV5693_8BIT,
440 OV5693_GROUP_ACCESS, 0x00);
442 dev_err(&client->dev, "%s: write %x error, aborted\n",
443 __func__, OV5693_GROUP_ACCESS);
447 ret = ov5693_write_reg(client, OV5693_8BIT,
448 OV5693_TIMING_HTS_H, (hts >> 8) & 0xFF);
450 dev_err(&client->dev, "%s: write %x error, aborted\n",
451 __func__, OV5693_TIMING_HTS_H);
455 ret = ov5693_write_reg(client, OV5693_8BIT,
456 OV5693_TIMING_HTS_L, hts & 0xFF);
458 dev_err(&client->dev, "%s: write %x error, aborted\n",
459 __func__, OV5693_TIMING_HTS_L);
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;
466 ret = ov5693_write_reg(client, OV5693_8BIT,
467 OV5693_TIMING_VTS_H, (vts >> 8) & 0xFF);
469 dev_err(&client->dev, "%s: write %x error, aborted\n",
470 __func__, OV5693_TIMING_VTS_H);
474 ret = ov5693_write_reg(client, OV5693_8BIT,
475 OV5693_TIMING_VTS_L, vts & 0xFF);
477 dev_err(&client->dev, "%s: write %x error, aborted\n",
478 __func__, OV5693_TIMING_VTS_L);
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);
489 dev_err(&client->dev, "%s: write %x error, aborted\n",
490 __func__, OV5693_EXPOSURE_L);
494 ret = ov5693_write_reg(client, OV5693_8BIT,
495 OV5693_EXPOSURE_M, (exp_val >> 8) & 0xFF);
497 dev_err(&client->dev, "%s: write %x error, aborted\n",
498 __func__, OV5693_EXPOSURE_M);
502 ret = ov5693_write_reg(client, OV5693_8BIT,
503 OV5693_EXPOSURE_H, (exp_val >> 16) & 0x0F);
505 dev_err(&client->dev, "%s: write %x error, aborted\n",
506 __func__, OV5693_EXPOSURE_H);
511 ret = ov5693_write_reg(client, OV5693_8BIT,
512 OV5693_AGC_L, gain & 0xff);
514 dev_err(&client->dev, "%s: write %x error, aborted\n",
515 __func__, OV5693_AGC_L);
519 ret = ov5693_write_reg(client, OV5693_8BIT,
520 OV5693_AGC_H, (gain >> 8) & 0xff);
522 dev_err(&client->dev, "%s: write %x error, aborted\n",
523 __func__, OV5693_AGC_H);
529 ret = ov5693_write_reg(client, OV5693_16BIT,
530 OV5693_MWB_RED_GAIN_H, digitgain);
532 dev_err(&client->dev, "%s: write %x error, aborted\n",
533 __func__, OV5693_MWB_RED_GAIN_H);
537 ret = ov5693_write_reg(client, OV5693_16BIT,
538 OV5693_MWB_GREEN_GAIN_H, digitgain);
540 dev_err(&client->dev, "%s: write %x error, aborted\n",
541 __func__, OV5693_MWB_RED_GAIN_H);
545 ret = ov5693_write_reg(client, OV5693_16BIT,
546 OV5693_MWB_BLUE_GAIN_H, digitgain);
548 dev_err(&client->dev, "%s: write %x error, aborted\n",
549 __func__, OV5693_MWB_RED_GAIN_H);
555 ret = ov5693_write_reg(client, OV5693_8BIT,
556 OV5693_GROUP_ACCESS, 0x10);
560 /* Delay launch group */
561 ret = ov5693_write_reg(client, OV5693_8BIT,
562 OV5693_GROUP_ACCESS, 0xa0);
568 static int ov5693_set_exposure(struct v4l2_subdev *sd, int exposure,
569 int gain, int digitgain)
571 struct ov5693_device *dev = to_ov5693_sensor(sd);
574 mutex_lock(&dev->input_lock);
575 ret = __ov5693_set_exposure(sd, exposure, gain, digitgain);
576 mutex_unlock(&dev->input_lock);
581 static long ov5693_s_exposure(struct v4l2_subdev *sd,
582 struct atomisp_exposure *exposure)
584 u16 coarse_itg = exposure->integration_time[0];
585 u16 analog_gain = exposure->gain[0];
586 u16 digital_gain = exposure->gain[1];
588 /* we should not accept the invalid value below */
589 if (analog_gain == 0) {
590 struct i2c_client *client = v4l2_get_subdevdata(sd);
592 v4l2_err(client, "%s: invalid value\n", __func__);
595 return ov5693_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
598 static int ov5693_read_otp_reg_array(struct i2c_client *client, u16 size,
605 for (index = 0; index <= size; index++) {
606 pVal = (u16 *)(buf + index);
608 ov5693_read_reg(client, OV5693_8BIT, addr + index,
617 static int __ov5693_otp_read(struct v4l2_subdev *sd, u8 *buf)
619 struct i2c_client *client = v4l2_get_subdevdata(sd);
620 struct ov5693_device *dev = to_ov5693_sensor(sd);
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
631 dev_err(&client->dev, "failed to prepare OTP page\n");
634 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_BANK_REG,(i|0xc0));
637 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_READ_REG,
638 OV5693_OTP_MODE_READ); // enable :1
640 dev_err(&client->dev,
641 "failed to set OTP reading mode page");
644 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_READ_REG,OV5693_OTP_MODE_READ);
646 /* Reading the OTP data array */
647 ret = ov5693_read_otp_reg_array(client, OV5693_OTP_BANK_SIZE,
648 OV5693_OTP_START_ADDR,
651 dev_err(&client->dev, "failed to read OTP data\n");
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));
657 //Intel OTP map, try to read 320byts first.
667 24) { //if the first 320bytes data doesn't not exist, try to read the next 32bytes data.
676 27) { //if the prvious 32bytes data doesn't exist, try to read the next 32bytes data again.
681 dev->otp_size = 0; // no OTP data.
686 b = b + OV5693_OTP_BANK_SIZE;
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.
696 static void *ov5693_otp_read(struct v4l2_subdev *sd)
698 struct i2c_client *client = v4l2_get_subdevdata(sd);
702 buf = devm_kzalloc(&client->dev, (OV5693_OTP_DATA_SIZE + 16), GFP_KERNEL);
704 return ERR_PTR(-ENOMEM);
706 //otp valid after mipi on and sw stream on
707 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x00);
709 ret = ov5693_write_reg(client, OV5693_8BIT,
710 OV5693_SW_STREAM, OV5693_START_STREAMING);
712 ret = __ov5693_otp_read(sd, buf);
714 //mipi off and sw stream off after otp read
715 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x0f);
717 ret = ov5693_write_reg(client, OV5693_8BIT,
718 OV5693_SW_STREAM, OV5693_STOP_STREAMING);
720 /* Driver has failed to find valid data */
722 dev_err(&client->dev, "sensor found no valid OTP data\n");
729 static long ov5693_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
732 case ATOMISP_IOC_S_EXPOSURE:
733 return ov5693_s_exposure(sd, arg);
741 * This returns the exposure time being used. This should only be used
742 * for filling in EXIF data, not for actual image processing.
744 static int ov5693_q_exposure(struct v4l2_subdev *sd, s32 *value)
746 struct i2c_client *client = v4l2_get_subdevdata(sd);
751 ret = ov5693_read_reg(client, OV5693_8BIT,
757 ret = ov5693_read_reg(client, OV5693_8BIT,
763 reg_v += reg_v2 << 8;
764 ret = ov5693_read_reg(client, OV5693_8BIT,
770 *value = reg_v + (((u32)reg_v2 << 16));
775 static int ad5823_t_focus_vcm(struct v4l2_subdev *sd, u16 val)
777 struct i2c_client *client = v4l2_get_subdevdata(sd);
781 ret = ad5823_i2c_read(client, AD5823_REG_VCM_CODE_MSB, &vcm_code);
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);
792 /* set reg VCM_CODE_LSB Bit[7:0] */
793 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_LSB, (val & 0xff));
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);
805 static int ad5823_t_focus_abs(struct v4l2_subdev *sd, s32 value)
807 value = min(value, AD5823_MAX_FOCUS_POS);
808 return ad5823_t_focus_vcm(sd, value);
811 static int ov5693_t_focus_abs(struct v4l2_subdev *sd, s32 value)
813 struct ov5693_device *dev = to_ov5693_sensor(sd);
814 struct i2c_client *client = v4l2_get_subdevdata(sd);
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);
824 ret = vcm_dw_i2c_write(client, DIRECT_VCM);
827 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_ON);
830 dev->vcm_update = false;
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);
838 dev->number_of_steps = value - dev->focus;
840 dev->timestamp_t_focus_abs = ktime_get();
842 dev_err(&client->dev,
843 "%s: i2c failed. ret %d\n", __func__, ret);
848 static int ov5693_t_focus_rel(struct v4l2_subdev *sd, s32 value)
850 struct ov5693_device *dev = to_ov5693_sensor(sd);
852 return ov5693_t_focus_abs(sd, dev->focus + value);
855 #define DELAY_PER_STEP_NS 1000000
856 #define DELAY_MAX_PER_STEP_NS (1000000 * 1023)
857 static int ov5693_q_focus_status(struct v4l2_subdev *sd, s32 *value)
860 struct ov5693_device *dev = to_ov5693_sensor(sd);
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));
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;
871 status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE;
872 status |= ATOMISP_FOCUS_HP_COMPLETE;
880 static int ov5693_q_focus_abs(struct v4l2_subdev *sd, s32 *value)
882 struct ov5693_device *dev = to_ov5693_sensor(sd);
885 ov5693_q_focus_status(sd, &val);
887 if (val & ATOMISP_FOCUS_STATUS_MOVING)
888 *value = dev->focus - dev->number_of_steps;
895 static int ov5693_t_vcm_slew(struct v4l2_subdev *sd, s32 value)
897 struct ov5693_device *dev = to_ov5693_sensor(sd);
899 dev->number_of_steps = value;
900 dev->vcm_update = true;
904 static int ov5693_t_vcm_timing(struct v4l2_subdev *sd, s32 value)
906 struct ov5693_device *dev = to_ov5693_sensor(sd);
908 dev->number_of_steps = value;
909 dev->vcm_update = true;
913 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
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);
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);
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);
931 case V4L2_CID_VCM_SLEW:
932 ret = ov5693_t_vcm_slew(&dev->sd, ctrl->val);
934 case V4L2_CID_VCM_TIMING:
935 ret = ov5693_t_vcm_timing(&dev->sd, ctrl->val);
943 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
945 struct ov5693_device *dev =
946 container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
950 case V4L2_CID_EXPOSURE_ABSOLUTE:
951 ret = ov5693_q_exposure(&dev->sd, &ctrl->val);
953 case V4L2_CID_FOCUS_ABSOLUTE:
954 ret = ov5693_q_focus_abs(&dev->sd, &ctrl->val);
956 case V4L2_CID_FOCUS_STATUS:
957 ret = ov5693_q_focus_status(&dev->sd, &ctrl->val);
966 static const struct v4l2_ctrl_ops ctrl_ops = {
967 .s_ctrl = ov5693_s_ctrl,
968 .g_volatile_ctrl = ov5693_g_volatile_ctrl
971 static const struct v4l2_ctrl_config ov5693_controls[] = {
974 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
975 .type = V4L2_CTRL_TYPE_INTEGER,
985 .id = V4L2_CID_FOCUS_ABSOLUTE,
986 .type = V4L2_CTRL_TYPE_INTEGER,
987 .name = "focus move absolute",
989 .max = OV5693_VCM_MAX_FOCUS_POS,
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,
1007 .id = V4L2_CID_FOCUS_STATUS,
1008 .type = V4L2_CTRL_TYPE_INTEGER,
1009 .name = "focus status",
1011 .max = 100, /* allow enum to grow in the future */
1018 .id = V4L2_CID_VCM_SLEW,
1019 .type = V4L2_CTRL_TYPE_INTEGER,
1022 .max = OV5693_VCM_SLEW_STEP_MAX,
1029 .id = V4L2_CID_VCM_TIMING,
1030 .type = V4L2_CTRL_TYPE_INTEGER,
1031 .name = "vcm step time",
1033 .max = OV5693_VCM_SLEW_TIME_MAX,
1040 static int ov5693_init(struct v4l2_subdev *sd)
1042 struct ov5693_device *dev = to_ov5693_sensor(sd);
1043 struct i2c_client *client = v4l2_get_subdevdata(sd);
1046 pr_info("%s\n", __func__);
1047 mutex_lock(&dev->input_lock);
1048 dev->vcm_update = false;
1050 if (dev->vcm == VCM_AD5823) {
1051 ret = vcm_ad_i2c_wr8(client, 0x01, 0x01); /* vcm init test */
1053 dev_err(&client->dev,
1054 "vcm reset failed\n");
1056 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB,
1057 AD5823_RING_CTRL_ENABLE);
1059 dev_err(&client->dev,
1060 "vcm enable ringing failed\n");
1061 ret = ad5823_i2c_write(client, AD5823_REG_MODE,
1064 dev_err(&client->dev,
1065 "vcm change mode failed\n");
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);
1074 ov5693_t_focus_abs(sd, 0);
1077 mutex_unlock(&dev->input_lock);
1082 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
1085 struct ov5693_device *dev = to_ov5693_sensor(sd);
1087 if (!dev || !dev->platform_data)
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.
1098 ret = dev->platform_data->v2p8_ctrl(sd, 1);
1099 dev->platform_data->gpio1_ctrl(sd, 1);
1101 ret = dev->platform_data->v1p8_ctrl(sd, 1);
1103 dev->platform_data->gpio1_ctrl(sd, 0);
1104 ret = dev->platform_data->v2p8_ctrl(sd, 0);
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);
1116 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
1118 struct ov5693_device *dev = to_ov5693_sensor(sd);
1120 if (!dev || !dev->platform_data)
1123 return dev->platform_data->gpio0_ctrl(sd, flag);
1126 static int __power_up(struct v4l2_subdev *sd)
1128 struct ov5693_device *dev = to_ov5693_sensor(sd);
1129 struct i2c_client *client = v4l2_get_subdevdata(sd);
1132 if (!dev->platform_data) {
1133 dev_err(&client->dev,
1134 "no camera_sensor_platform_data");
1139 ret = power_ctrl(sd, 1);
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);
1148 ret = gpio_ctrl(sd, 1);
1150 ret = gpio_ctrl(sd, 1);
1155 /* flis clock control */
1156 ret = dev->platform_data->flisclk_ctrl(sd, 1);
1160 __cci_delay(up_delay);
1168 dev_err(&client->dev, "sensor power-up failed\n");
1173 static int power_down(struct v4l2_subdev *sd)
1175 struct ov5693_device *dev = to_ov5693_sensor(sd);
1176 struct i2c_client *client = v4l2_get_subdevdata(sd);
1179 dev->focus = OV5693_INVALID_CONFIG;
1180 if (!dev->platform_data) {
1181 dev_err(&client->dev,
1182 "no camera_sensor_platform_data");
1186 ret = dev->platform_data->flisclk_ctrl(sd, 0);
1188 dev_err(&client->dev, "flisclk failed\n");
1191 ret = gpio_ctrl(sd, 0);
1193 ret = gpio_ctrl(sd, 0);
1195 dev_err(&client->dev, "gpio failed 2\n");
1199 ret = power_ctrl(sd, 0);
1201 dev_err(&client->dev, "vprog failed.\n");
1206 static int power_up(struct v4l2_subdev *sd)
1208 static const int retry_count = 4;
1211 for (i = 0; i < retry_count; i++) {
1212 ret = __power_up(sd);
1221 static int ov5693_s_power(struct v4l2_subdev *sd, int on)
1225 pr_info("%s: on %d\n", __func__, on);
1227 return power_down(sd);
1231 ret = ov5693_init(sd);
1232 /* restore settings */
1233 ov5693_res = ov5693_res_preview;
1234 N_RES = N_RES_PREVIEW;
1241 * distance - calculate the distance
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.
1252 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1253 static int distance(struct ov5693_resolution *res, u32 w, u32 h)
1258 if (w == 0 || h == 0 ||
1259 res->width < w || res->height < h)
1262 ratio = res->width << 13;
1265 ratio /= res->height;
1267 distance = abs(ratio - 8192);
1269 if (distance > LARGEST_ALLOWED_RATIO_MISMATCH)
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
1280 static int nearest_resolution_index(int w, int h)
1285 int min_dist = INT_MAX;
1286 int min_res_w = INT_MAX;
1287 struct ov5693_resolution *tmp_res = NULL;
1289 for (i = 0; i < N_RES; i++) {
1290 tmp_res = &ov5693_res[i];
1291 dist = distance(tmp_res, w, h);
1294 if (dist < min_dist) {
1297 min_res_w = ov5693_res[i].width;
1300 if (dist == min_dist && ov5693_res[i].width < min_res_w)
1307 static int get_resolution_index(int w, int h)
1311 for (i = 0; i < N_RES; i++) {
1312 if (w != ov5693_res[i].width)
1314 if (h != ov5693_res[i].height)
1323 /* TODO: remove it. */
1324 static int startup(struct v4l2_subdev *sd)
1326 struct ov5693_device *dev = to_ov5693_sensor(sd);
1327 struct i2c_client *client = v4l2_get_subdevdata(sd);
1330 ret = ov5693_write_reg(client, OV5693_8BIT,
1331 OV5693_SW_RESET, 0x01);
1333 dev_err(&client->dev, "ov5693 reset err.\n");
1337 ret = ov5693_write_reg_array(client, ov5693_global_setting);
1339 dev_err(&client->dev, "ov5693 write register err.\n");
1343 ret = ov5693_write_reg_array(client, ov5693_res[dev->fmt_idx].regs);
1345 dev_err(&client->dev, "ov5693 write register err.\n");
1352 static int ov5693_set_fmt(struct v4l2_subdev *sd,
1353 struct v4l2_subdev_state *sd_state,
1354 struct v4l2_subdev_format *format)
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;
1367 ov5693_info = v4l2_get_subdev_hostdata(sd);
1371 mutex_lock(&dev->input_lock);
1372 idx = nearest_resolution_index(fmt->width, fmt->height);
1374 /* return the largest resolution */
1375 fmt->width = ov5693_res[N_RES - 1].width;
1376 fmt->height = ov5693_res[N_RES - 1].height;
1378 fmt->width = ov5693_res[idx].width;
1379 fmt->height = ov5693_res[idx].height;
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);
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);
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);
1408 mutex_unlock(&dev->input_lock);
1410 mutex_lock(&dev->input_lock);
1412 dev_err(&client->dev, "power up failed, continue\n");
1417 dev_err(&client->dev, " startup FAILED!\n");
1419 dev_err(&client->dev, " startup SUCCESS!\n");
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.
1430 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1431 OV5693_STOP_STREAMING);
1433 dev_warn(&client->dev, "ov5693 stream off err\n");
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;
1439 mutex_unlock(&dev->input_lock);
1443 static int ov5693_get_fmt(struct v4l2_subdev *sd,
1444 struct v4l2_subdev_state *sd_state,
1445 struct v4l2_subdev_format *format)
1447 struct v4l2_mbus_framefmt *fmt = &format->format;
1448 struct ov5693_device *dev = to_ov5693_sensor(sd);
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;
1463 static int ov5693_detect(struct i2c_client *client)
1465 struct i2c_adapter *adapter = client->adapter;
1471 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1474 ret = ov5693_read_reg(client, OV5693_8BIT,
1475 OV5693_SC_CMMN_CHIP_ID_H, &high);
1477 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1480 ret = ov5693_read_reg(client, OV5693_8BIT,
1481 OV5693_SC_CMMN_CHIP_ID_L, &low);
1484 id = ((((u16)high) << 8) | (u16)low);
1486 if (id != OV5693_ID) {
1487 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1491 ret = ov5693_read_reg(client, OV5693_8BIT,
1492 OV5693_SC_CMMN_SUB_ID, &high);
1493 revision = (u8)high & 0x0f;
1495 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
1496 dev_dbg(&client->dev, "detect ov5693 success\n");
1500 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
1502 struct ov5693_device *dev = to_ov5693_sensor(sd);
1503 struct i2c_client *client = v4l2_get_subdevdata(sd);
1506 mutex_lock(&dev->input_lock);
1508 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1509 enable ? OV5693_START_STREAMING :
1510 OV5693_STOP_STREAMING);
1512 mutex_unlock(&dev->input_lock);
1517 static int ov5693_s_config(struct v4l2_subdev *sd,
1518 int irq, void *platform_data)
1520 struct ov5693_device *dev = to_ov5693_sensor(sd);
1521 struct i2c_client *client = v4l2_get_subdevdata(sd);
1527 dev->platform_data =
1528 (struct camera_sensor_platform_data *)platform_data;
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
1535 ret = power_down(sd);
1537 dev_err(&client->dev, "ov5693 power-off err.\n");
1538 goto fail_power_off;
1543 dev_err(&client->dev, "ov5693 power-up err.\n");
1548 dev->vcm = vcm_detect(client);
1550 ret = dev->platform_data->csi_cfg(sd, 1);
1554 /* config & detect sensor */
1555 ret = ov5693_detect(client);
1557 dev_err(&client->dev, "ov5693_detect err s_config.\n");
1561 dev->otp_data = ov5693_otp_read(sd);
1563 /* turn off sensor, after probed */
1564 ret = power_down(sd);
1566 dev_err(&client->dev, "ov5693 power-off err.\n");
1569 mutex_unlock(&dev->input_lock);
1574 dev->platform_data->csi_cfg(sd, 0);
1577 dev_err(&client->dev, "sensor power-gating failed\n");
1579 mutex_unlock(&dev->input_lock);
1583 static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
1584 struct v4l2_subdev_frame_interval *interval)
1586 struct ov5693_device *dev = to_ov5693_sensor(sd);
1588 interval->interval.numerator = 1;
1589 interval->interval.denominator = ov5693_res[dev->fmt_idx].fps;
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)
1598 if (code->index >= MAX_FMTS)
1601 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
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)
1609 int index = fse->index;
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;
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,
1627 static const struct v4l2_subdev_core_ops ov5693_core_ops = {
1628 .s_power = ov5693_s_power,
1629 .ioctl = ov5693_ioctl,
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,
1639 static const struct v4l2_subdev_ops ov5693_ops = {
1640 .core = &ov5693_core_ops,
1641 .video = &ov5693_video_ops,
1642 .pad = &ov5693_pad_ops,
1645 static void ov5693_remove(struct i2c_client *client)
1647 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1648 struct ov5693_device *dev = to_ov5693_sensor(sd);
1650 dev_dbg(&client->dev, "ov5693_remove...\n");
1652 dev->platform_data->csi_cfg(sd, 0);
1654 v4l2_device_unregister_subdev(sd);
1656 atomisp_gmin_remove_subdev(sd);
1658 media_entity_cleanup(&dev->sd.entity);
1659 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1663 static int ov5693_probe(struct i2c_client *client)
1665 struct ov5693_device *dev;
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
1677 i2c = gmin_get_var_int(&client->dev, false, "I2CAddr", -1);
1679 dev_info(&client->dev,
1680 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n",
1685 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1689 mutex_init(&dev->input_lock);
1692 v4l2_i2c_subdev_init(&dev->sd, client, &ov5693_ops);
1694 pdata = gmin_camera_platform_data(&dev->sd,
1695 ATOMISP_INPUT_FORMAT_RAW_10,
1696 atomisp_bayer_order_bggr);
1702 ret = ov5693_s_config(&dev->sd, client->irq, pdata);
1706 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
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;
1715 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1716 ARRAY_SIZE(ov5693_controls));
1718 ov5693_remove(client);
1722 for (i = 0; i < ARRAY_SIZE(ov5693_controls); i++)
1723 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov5693_controls[i],
1726 if (dev->ctrl_handler.error) {
1727 ov5693_remove(client);
1728 return dev->ctrl_handler.error;
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;
1735 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1737 ov5693_remove(client);
1741 v4l2_device_unregister_subdev(&dev->sd);
1746 static const struct acpi_device_id ov5693_acpi_match[] = {
1750 MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
1752 static struct i2c_driver ov5693_driver = {
1755 .acpi_match_table = ov5693_acpi_match,
1757 .probe = ov5693_probe,
1758 .remove = ov5693_remove,
1760 module_i2c_driver(ov5693_driver);
1762 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
1763 MODULE_LICENSE("GPL");