2 * Support for OmniVision OV5693 1080p HD camera sensor.
4 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.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>
33 #include <linux/acpi.h>
34 #include "../../include/linux/atomisp_gmin_platform.h"
39 #define __cci_delay(t) \
42 usleep_range((t) * 1000, ((t) + 1) * 1000); \
48 /* Value 30ms reached through experimentation on byt ecs.
49 * The DS specifies a much lower value but when using a smaller value
50 * the I2C bus sometimes locks up permanently when starting the camera.
51 * This issue could not be reproduced on cht, so we can reduce the
52 * delay value to a lower value when insmod.
54 static uint up_delay = 30;
55 module_param(up_delay, uint, 0644);
56 MODULE_PARM_DESC(up_delay,
57 "Delay prior to the first CCI transaction for ov5693");
59 static int vcm_ad_i2c_wr8(struct i2c_client *client, u8 reg, u8 val)
73 err = i2c_transfer(client->adapter, &msg, 1);
75 dev_err(&client->dev, "%s: vcm i2c fail, err code = %d\n",
82 static int ad5823_i2c_write(struct i2c_client *client, u8 reg, u8 val)
89 msg.addr = AD5823_VCM_ADDR;
94 if (i2c_transfer(client->adapter, &msg, 1) != 1)
99 static int ad5823_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
101 struct i2c_msg msg[2];
107 msg[0].addr = AD5823_VCM_ADDR;
110 msg[0].buf = &buf[0];
113 msg[1].flags = I2C_M_RD;
115 msg[1].buf = &buf[1];
117 if (i2c_transfer(client->adapter, msg, 2) != 2)
123 static const u32 ov5693_embedded_effective_size = 28;
125 /* i2c read/write stuff */
126 static int ov5693_read_reg(struct i2c_client *client,
127 u16 data_length, u16 reg, u16 *val)
130 struct i2c_msg msg[2];
131 unsigned char data[6];
133 if (!client->adapter) {
134 dev_err(&client->dev, "%s error, no client->adapter\n",
139 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT
140 && data_length != OV5693_32BIT) {
141 dev_err(&client->dev, "%s error, invalid data length\n",
146 memset(msg, 0, sizeof(msg));
148 msg[0].addr = client->addr;
150 msg[0].len = I2C_MSG_LENGTH;
153 /* high byte goes out first */
154 data[0] = (u8)(reg >> 8);
155 data[1] = (u8)(reg & 0xff);
157 msg[1].addr = client->addr;
158 msg[1].len = data_length;
159 msg[1].flags = I2C_M_RD;
162 err = i2c_transfer(client->adapter, msg, 2);
166 dev_err(&client->dev,
167 "read from offset 0x%x error %d", reg, err);
172 /* high byte comes first */
173 if (data_length == OV5693_8BIT)
175 else if (data_length == OV5693_16BIT)
176 *val = be16_to_cpu(*(__be16 *)&data[0]);
178 *val = be32_to_cpu(*(__be32 *)&data[0]);
183 static int ov5693_i2c_write(struct i2c_client *client, u16 len, u8 *data)
186 const int num_msg = 1;
189 msg.addr = client->addr;
193 ret = i2c_transfer(client->adapter, &msg, 1);
195 return ret == num_msg ? 0 : -EIO;
198 static int vcm_dw_i2c_write(struct i2c_client *client, u16 data)
201 const int num_msg = 1;
205 val = cpu_to_be16(data);
208 msg.len = OV5693_16BIT;
209 msg.buf = (void *)&val;
211 ret = i2c_transfer(client->adapter, &msg, 1);
213 return ret == num_msg ? 0 : -EIO;
217 * Theory: per datasheet, the two VCMs both allow for a 2-byte read.
218 * The DW9714 doesn't actually specify what this does (it has a
219 * two-byte write-only protocol, but specifies the read sequence as
220 * legal), but it returns the same data (zeroes) always, after an
221 * undocumented initial NAK. The AD5823 has a one-byte address
222 * register to which all writes go, and subsequent reads will cycle
223 * through the 8 bytes of registers. Notably, the default values (the
224 * device is always power-cycled affirmatively, so we can rely on
225 * these) in AD5823 are not pairwise repetitions of the same 16 bit
226 * word. So all we have to do is sequentially read two bytes at a
227 * time and see if we detect a difference in any of the first four
230 static int vcm_detect(struct i2c_client *client)
236 for (i = 0; i < 4; i++) {
238 msg.flags = I2C_M_RD;
239 msg.len = sizeof(data);
240 msg.buf = (u8 *)&data;
241 ret = i2c_transfer(client->adapter, &msg, 1);
244 * DW9714 always fails the first read and returns
245 * zeroes for subsequent ones
247 if (i == 0 && ret == -EREMOTEIO) {
258 return ret == 1 ? VCM_DW9714 : ret;
261 static int ov5693_write_reg(struct i2c_client *client, u16 data_length,
265 unsigned char data[4] = {0};
266 __be16 *wreg = (void *)data;
267 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
269 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT) {
270 dev_err(&client->dev,
271 "%s error, invalid data_length\n", __func__);
275 /* high byte goes out first */
276 *wreg = cpu_to_be16(reg);
278 if (data_length == OV5693_8BIT) {
282 __be16 *wdata = (void *)&data[2];
284 *wdata = cpu_to_be16(val);
287 ret = ov5693_i2c_write(client, len, data);
289 dev_err(&client->dev,
290 "write error: wrote 0x%x to offset 0x%x error %d",
297 * ov5693_write_reg_array - Initializes a list of OV5693 registers
298 * @client: i2c driver client structure
299 * @reglist: list of registers to be written
301 * This function initializes a list of registers. When consecutive addresses
302 * are found in a row on the list, this function creates a buffer and sends
303 * consecutive data in a single i2c_transfer().
305 * __ov5693_flush_reg_array, __ov5693_buf_reg_array() and
306 * __ov5693_write_reg_is_consecutive() are internal functions to
307 * ov5693_write_reg_array_fast() and should be not used anywhere else.
311 static int __ov5693_flush_reg_array(struct i2c_client *client,
312 struct ov5693_write_ctrl *ctrl)
315 __be16 *reg = (void *)&ctrl->buffer.addr;
317 if (ctrl->index == 0)
320 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
322 *reg = cpu_to_be16(ctrl->buffer.addr);
325 return ov5693_i2c_write(client, size, (u8 *)reg);
328 static int __ov5693_buf_reg_array(struct i2c_client *client,
329 struct ov5693_write_ctrl *ctrl,
330 const struct ov5693_reg *next)
335 switch (next->type) {
338 ctrl->buffer.data[ctrl->index] = (u8)next->val;
343 data16 = (void *)&ctrl->buffer.data[ctrl->index];
344 *data16 = cpu_to_be16((u16)next->val);
350 /* When first item is added, we need to store its starting address */
351 if (ctrl->index == 0)
352 ctrl->buffer.addr = next->reg;
357 * Buffer cannot guarantee free space for u32? Better flush it to avoid
358 * possible lack of memory for next item.
360 if (ctrl->index + sizeof(u16) >= OV5693_MAX_WRITE_BUF_SIZE)
361 return __ov5693_flush_reg_array(client, ctrl);
366 static int __ov5693_write_reg_is_consecutive(struct i2c_client *client,
367 struct ov5693_write_ctrl *ctrl,
368 const struct ov5693_reg *next)
370 if (ctrl->index == 0)
373 return ctrl->buffer.addr + ctrl->index == next->reg;
376 static int ov5693_write_reg_array(struct i2c_client *client,
377 const struct ov5693_reg *reglist)
379 const struct ov5693_reg *next = reglist;
380 struct ov5693_write_ctrl ctrl;
384 for (; next->type != OV5693_TOK_TERM; next++) {
385 switch (next->type & OV5693_TOK_MASK) {
386 case OV5693_TOK_DELAY:
387 err = __ov5693_flush_reg_array(client, &ctrl);
394 * If next address is not consecutive, data needs to be
395 * flushed before proceed.
397 if (!__ov5693_write_reg_is_consecutive(client, &ctrl,
399 err = __ov5693_flush_reg_array(client, &ctrl);
403 err = __ov5693_buf_reg_array(client, &ctrl, next);
405 dev_err(&client->dev,
406 "%s: write error, aborted\n",
414 return __ov5693_flush_reg_array(client, &ctrl);
417 static int ov5693_g_focal(struct v4l2_subdev *sd, s32 *val)
419 *val = (OV5693_FOCAL_LENGTH_NUM << 16) | OV5693_FOCAL_LENGTH_DEM;
423 static int ov5693_g_fnumber(struct v4l2_subdev *sd, s32 *val)
425 /*const f number for imx*/
426 *val = (OV5693_F_NUMBER_DEFAULT_NUM << 16) | OV5693_F_NUMBER_DEM;
430 static int ov5693_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
432 *val = (OV5693_F_NUMBER_DEFAULT_NUM << 24) |
433 (OV5693_F_NUMBER_DEM << 16) |
434 (OV5693_F_NUMBER_DEFAULT_NUM << 8) | OV5693_F_NUMBER_DEM;
438 static int ov5693_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
440 struct ov5693_device *dev = to_ov5693_sensor(sd);
442 *val = ov5693_res[dev->fmt_idx].bin_factor_x;
447 static int ov5693_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
449 struct ov5693_device *dev = to_ov5693_sensor(sd);
451 *val = ov5693_res[dev->fmt_idx].bin_factor_y;
456 static int ov5693_get_intg_factor(struct i2c_client *client,
457 struct camera_mipi_info *info,
458 const struct ov5693_resolution *res)
460 struct v4l2_subdev *sd = i2c_get_clientdata(client);
461 struct ov5693_device *dev = to_ov5693_sensor(sd);
462 struct atomisp_sensor_mode_data *buf = &info->data;
463 unsigned int pix_clk_freq_hz;
471 pix_clk_freq_hz = res->pix_clk_freq * 1000000;
473 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
474 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
476 /* get integration time */
477 buf->coarse_integration_time_min = OV5693_COARSE_INTG_TIME_MIN;
478 buf->coarse_integration_time_max_margin =
479 OV5693_COARSE_INTG_TIME_MAX_MARGIN;
481 buf->fine_integration_time_min = OV5693_FINE_INTG_TIME_MIN;
482 buf->fine_integration_time_max_margin =
483 OV5693_FINE_INTG_TIME_MAX_MARGIN;
485 buf->fine_integration_time_def = OV5693_FINE_INTG_TIME_MIN;
486 buf->frame_length_lines = res->lines_per_frame;
487 buf->line_length_pck = res->pixels_per_line;
488 buf->read_mode = res->bin_mode;
490 /* get the cropping and output resolution to ISP for this mode. */
491 ret = ov5693_read_reg(client, OV5693_16BIT,
492 OV5693_HORIZONTAL_START_H, ®_val);
495 buf->crop_horizontal_start = reg_val;
497 ret = ov5693_read_reg(client, OV5693_16BIT,
498 OV5693_VERTICAL_START_H, ®_val);
501 buf->crop_vertical_start = reg_val;
503 ret = ov5693_read_reg(client, OV5693_16BIT,
504 OV5693_HORIZONTAL_END_H, ®_val);
507 buf->crop_horizontal_end = reg_val;
509 ret = ov5693_read_reg(client, OV5693_16BIT,
510 OV5693_VERTICAL_END_H, ®_val);
513 buf->crop_vertical_end = reg_val;
515 ret = ov5693_read_reg(client, OV5693_16BIT,
516 OV5693_HORIZONTAL_OUTPUT_SIZE_H, ®_val);
519 buf->output_width = reg_val;
521 ret = ov5693_read_reg(client, OV5693_16BIT,
522 OV5693_VERTICAL_OUTPUT_SIZE_H, ®_val);
525 buf->output_height = reg_val;
527 buf->binning_factor_x = res->bin_factor_x ?
528 res->bin_factor_x : 1;
529 buf->binning_factor_y = res->bin_factor_y ?
530 res->bin_factor_y : 1;
534 static long __ov5693_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
535 int gain, int digitgain)
538 struct i2c_client *client = v4l2_get_subdevdata(sd);
539 struct ov5693_device *dev = to_ov5693_sensor(sd);
543 hts = ov5693_res[dev->fmt_idx].pixels_per_line;
544 vts = ov5693_res[dev->fmt_idx].lines_per_frame;
546 * If coarse_itg is larger than 1<<15, can not write to reg directly.
547 * The way is to write coarse_itg/2 to the reg, meanwhile write 2*hts
550 if (coarse_itg > (1 << 15)) {
552 coarse_itg = (int)coarse_itg / 2;
555 ret = ov5693_write_reg(client, OV5693_8BIT,
556 OV5693_GROUP_ACCESS, 0x00);
558 dev_err(&client->dev, "%s: write %x error, aborted\n",
559 __func__, OV5693_GROUP_ACCESS);
563 ret = ov5693_write_reg(client, OV5693_8BIT,
564 OV5693_TIMING_HTS_H, (hts >> 8) & 0xFF);
566 dev_err(&client->dev, "%s: write %x error, aborted\n",
567 __func__, OV5693_TIMING_HTS_H);
571 ret = ov5693_write_reg(client, OV5693_8BIT,
572 OV5693_TIMING_HTS_L, hts & 0xFF);
574 dev_err(&client->dev, "%s: write %x error, aborted\n",
575 __func__, OV5693_TIMING_HTS_L);
578 /* Increase the VTS to match exposure + MARGIN */
579 if (coarse_itg > vts - OV5693_INTEGRATION_TIME_MARGIN)
580 vts = (u16)coarse_itg + OV5693_INTEGRATION_TIME_MARGIN;
582 ret = ov5693_write_reg(client, OV5693_8BIT,
583 OV5693_TIMING_VTS_H, (vts >> 8) & 0xFF);
585 dev_err(&client->dev, "%s: write %x error, aborted\n",
586 __func__, OV5693_TIMING_VTS_H);
590 ret = ov5693_write_reg(client, OV5693_8BIT,
591 OV5693_TIMING_VTS_L, vts & 0xFF);
593 dev_err(&client->dev, "%s: write %x error, aborted\n",
594 __func__, OV5693_TIMING_VTS_L);
600 /* Lower four bit should be 0*/
601 exp_val = coarse_itg << 4;
602 ret = ov5693_write_reg(client, OV5693_8BIT,
603 OV5693_EXPOSURE_L, exp_val & 0xFF);
605 dev_err(&client->dev, "%s: write %x error, aborted\n",
606 __func__, OV5693_EXPOSURE_L);
610 ret = ov5693_write_reg(client, OV5693_8BIT,
611 OV5693_EXPOSURE_M, (exp_val >> 8) & 0xFF);
613 dev_err(&client->dev, "%s: write %x error, aborted\n",
614 __func__, OV5693_EXPOSURE_M);
618 ret = ov5693_write_reg(client, OV5693_8BIT,
619 OV5693_EXPOSURE_H, (exp_val >> 16) & 0x0F);
621 dev_err(&client->dev, "%s: write %x error, aborted\n",
622 __func__, OV5693_EXPOSURE_H);
627 ret = ov5693_write_reg(client, OV5693_8BIT,
628 OV5693_AGC_L, gain & 0xff);
630 dev_err(&client->dev, "%s: write %x error, aborted\n",
631 __func__, OV5693_AGC_L);
635 ret = ov5693_write_reg(client, OV5693_8BIT,
636 OV5693_AGC_H, (gain >> 8) & 0xff);
638 dev_err(&client->dev, "%s: write %x error, aborted\n",
639 __func__, OV5693_AGC_H);
645 ret = ov5693_write_reg(client, OV5693_16BIT,
646 OV5693_MWB_RED_GAIN_H, digitgain);
648 dev_err(&client->dev, "%s: write %x error, aborted\n",
649 __func__, OV5693_MWB_RED_GAIN_H);
653 ret = ov5693_write_reg(client, OV5693_16BIT,
654 OV5693_MWB_GREEN_GAIN_H, digitgain);
656 dev_err(&client->dev, "%s: write %x error, aborted\n",
657 __func__, OV5693_MWB_RED_GAIN_H);
661 ret = ov5693_write_reg(client, OV5693_16BIT,
662 OV5693_MWB_BLUE_GAIN_H, digitgain);
664 dev_err(&client->dev, "%s: write %x error, aborted\n",
665 __func__, OV5693_MWB_RED_GAIN_H);
671 ret = ov5693_write_reg(client, OV5693_8BIT,
672 OV5693_GROUP_ACCESS, 0x10);
676 /* Delay launch group */
677 ret = ov5693_write_reg(client, OV5693_8BIT,
678 OV5693_GROUP_ACCESS, 0xa0);
684 static int ov5693_set_exposure(struct v4l2_subdev *sd, int exposure,
685 int gain, int digitgain)
687 struct ov5693_device *dev = to_ov5693_sensor(sd);
690 mutex_lock(&dev->input_lock);
691 ret = __ov5693_set_exposure(sd, exposure, gain, digitgain);
692 mutex_unlock(&dev->input_lock);
697 static long ov5693_s_exposure(struct v4l2_subdev *sd,
698 struct atomisp_exposure *exposure)
700 u16 coarse_itg = exposure->integration_time[0];
701 u16 analog_gain = exposure->gain[0];
702 u16 digital_gain = exposure->gain[1];
704 /* we should not accept the invalid value below */
705 if (analog_gain == 0) {
706 struct i2c_client *client = v4l2_get_subdevdata(sd);
708 v4l2_err(client, "%s: invalid value\n", __func__);
711 return ov5693_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
714 static int ov5693_read_otp_reg_array(struct i2c_client *client, u16 size,
721 for (index = 0; index <= size; index++) {
722 pVal = (u16 *)(buf + index);
724 ov5693_read_reg(client, OV5693_8BIT, addr + index,
733 static int __ov5693_otp_read(struct v4l2_subdev *sd, u8 *buf)
735 struct i2c_client *client = v4l2_get_subdevdata(sd);
736 struct ov5693_device *dev = to_ov5693_sensor(sd);
742 for (i = 1; i < OV5693_OTP_BANK_MAX; i++) {
743 /*set bank NO and OTP read mode. */
744 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_BANK_REG,
745 (i | 0xc0)); //[7:6] 2'b11 [5:0] bank no
747 dev_err(&client->dev, "failed to prepare OTP page\n");
750 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_BANK_REG,(i|0xc0));
753 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_READ_REG,
754 OV5693_OTP_MODE_READ); // enable :1
756 dev_err(&client->dev,
757 "failed to set OTP reading mode page");
760 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_READ_REG,OV5693_OTP_MODE_READ);
762 /* Reading the OTP data array */
763 ret = ov5693_read_otp_reg_array(client, OV5693_OTP_BANK_SIZE,
764 OV5693_OTP_START_ADDR,
767 dev_err(&client->dev, "failed to read OTP data\n");
771 //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));
773 //Intel OTP map, try to read 320byts first.
783 24) { //if the first 320bytes data doesn't not exist, try to read the next 32bytes data.
792 27) { //if the prvious 32bytes data doesn't exist, try to read the next 32bytes data again.
797 dev->otp_size = 0; // no OTP data.
802 b = b + OV5693_OTP_BANK_SIZE;
808 * Read otp data and store it into a kmalloced buffer.
809 * The caller must kfree the buffer when no more needed.
810 * @size: set to the size of the returned otp data.
812 static void *ov5693_otp_read(struct v4l2_subdev *sd)
814 struct i2c_client *client = v4l2_get_subdevdata(sd);
818 buf = devm_kzalloc(&client->dev, (OV5693_OTP_DATA_SIZE + 16), GFP_KERNEL);
820 return ERR_PTR(-ENOMEM);
822 //otp valid after mipi on and sw stream on
823 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x00);
825 ret = ov5693_write_reg(client, OV5693_8BIT,
826 OV5693_SW_STREAM, OV5693_START_STREAMING);
828 ret = __ov5693_otp_read(sd, buf);
830 //mipi off and sw stream off after otp read
831 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x0f);
833 ret = ov5693_write_reg(client, OV5693_8BIT,
834 OV5693_SW_STREAM, OV5693_STOP_STREAMING);
836 /* Driver has failed to find valid data */
838 dev_err(&client->dev, "sensor found no valid OTP data\n");
845 static int ov5693_g_priv_int_data(struct v4l2_subdev *sd,
846 struct v4l2_private_int_data *priv)
848 struct i2c_client *client = v4l2_get_subdevdata(sd);
849 struct ov5693_device *dev = to_ov5693_sensor(sd);
850 u8 __user *to = priv->data;
851 u32 read_size = priv->size;
854 /* No need to copy data if size is 0 */
858 if (IS_ERR(dev->otp_data)) {
859 dev_err(&client->dev, "OTP data not available");
860 return PTR_ERR(dev->otp_data);
863 /* Correct read_size value only if bigger than maximum */
864 if (read_size > OV5693_OTP_DATA_SIZE)
865 read_size = OV5693_OTP_DATA_SIZE;
867 ret = copy_to_user(to, dev->otp_data, read_size);
869 dev_err(&client->dev, "%s: failed to copy OTP data to user\n",
874 pr_debug("%s read_size:%d\n", __func__, read_size);
877 /* Return correct size */
878 priv->size = dev->otp_size;
883 static long ov5693_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
886 case ATOMISP_IOC_S_EXPOSURE:
887 return ov5693_s_exposure(sd, arg);
888 case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
889 return ov5693_g_priv_int_data(sd, arg);
897 * This returns the exposure time being used. This should only be used
898 * for filling in EXIF data, not for actual image processing.
900 static int ov5693_q_exposure(struct v4l2_subdev *sd, s32 *value)
902 struct i2c_client *client = v4l2_get_subdevdata(sd);
907 ret = ov5693_read_reg(client, OV5693_8BIT,
913 ret = ov5693_read_reg(client, OV5693_8BIT,
919 reg_v += reg_v2 << 8;
920 ret = ov5693_read_reg(client, OV5693_8BIT,
926 *value = reg_v + (((u32)reg_v2 << 16));
931 static int ad5823_t_focus_vcm(struct v4l2_subdev *sd, u16 val)
933 struct i2c_client *client = v4l2_get_subdevdata(sd);
937 ret = ad5823_i2c_read(client, AD5823_REG_VCM_CODE_MSB, &vcm_code);
941 /* set reg VCM_CODE_MSB Bit[1:0] */
942 vcm_code = (vcm_code & VCM_CODE_MSB_MASK) |
943 ((val >> 8) & ~VCM_CODE_MSB_MASK);
944 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB, vcm_code);
948 /* set reg VCM_CODE_LSB Bit[7:0] */
949 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_LSB, (val & 0xff));
953 /* set required vcm move time */
954 vcm_code = AD5823_RESONANCE_PERIOD / AD5823_RESONANCE_COEF
955 - AD5823_HIGH_FREQ_RANGE;
956 ret = ad5823_i2c_write(client, AD5823_REG_VCM_MOVE_TIME, vcm_code);
961 static int ad5823_t_focus_abs(struct v4l2_subdev *sd, s32 value)
963 value = min(value, AD5823_MAX_FOCUS_POS);
964 return ad5823_t_focus_vcm(sd, value);
967 static int ov5693_t_focus_abs(struct v4l2_subdev *sd, s32 value)
969 struct ov5693_device *dev = to_ov5693_sensor(sd);
970 struct i2c_client *client = v4l2_get_subdevdata(sd);
973 dev_dbg(&client->dev, "%s: FOCUS_POS: 0x%x\n", __func__, value);
974 value = clamp(value, 0, OV5693_VCM_MAX_FOCUS_POS);
975 if (dev->vcm == VCM_DW9714) {
976 if (dev->vcm_update) {
977 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_OFF);
980 ret = vcm_dw_i2c_write(client, DIRECT_VCM);
983 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_ON);
986 dev->vcm_update = false;
988 ret = vcm_dw_i2c_write(client,
989 vcm_val(value, VCM_DEFAULT_S));
990 } else if (dev->vcm == VCM_AD5823) {
991 ad5823_t_focus_abs(sd, value);
994 dev->number_of_steps = value - dev->focus;
996 dev->timestamp_t_focus_abs = ktime_get();
998 dev_err(&client->dev,
999 "%s: i2c failed. ret %d\n", __func__, ret);
1004 static int ov5693_t_focus_rel(struct v4l2_subdev *sd, s32 value)
1006 struct ov5693_device *dev = to_ov5693_sensor(sd);
1008 return ov5693_t_focus_abs(sd, dev->focus + value);
1011 #define DELAY_PER_STEP_NS 1000000
1012 #define DELAY_MAX_PER_STEP_NS (1000000 * 1023)
1013 static int ov5693_q_focus_status(struct v4l2_subdev *sd, s32 *value)
1016 struct ov5693_device *dev = to_ov5693_sensor(sd);
1018 ktime_t timedelay = ns_to_ktime(min_t(u32,
1019 abs(dev->number_of_steps) * DELAY_PER_STEP_NS,
1020 DELAY_MAX_PER_STEP_NS));
1022 temptime = ktime_sub(ktime_get(), (dev->timestamp_t_focus_abs));
1023 if (ktime_compare(temptime, timedelay) <= 0) {
1024 status |= ATOMISP_FOCUS_STATUS_MOVING;
1025 status |= ATOMISP_FOCUS_HP_IN_PROGRESS;
1027 status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE;
1028 status |= ATOMISP_FOCUS_HP_COMPLETE;
1036 static int ov5693_q_focus_abs(struct v4l2_subdev *sd, s32 *value)
1038 struct ov5693_device *dev = to_ov5693_sensor(sd);
1041 ov5693_q_focus_status(sd, &val);
1043 if (val & ATOMISP_FOCUS_STATUS_MOVING)
1044 *value = dev->focus - dev->number_of_steps;
1046 *value = dev->focus;
1051 static int ov5693_t_vcm_slew(struct v4l2_subdev *sd, s32 value)
1053 struct ov5693_device *dev = to_ov5693_sensor(sd);
1055 dev->number_of_steps = value;
1056 dev->vcm_update = true;
1060 static int ov5693_t_vcm_timing(struct v4l2_subdev *sd, s32 value)
1062 struct ov5693_device *dev = to_ov5693_sensor(sd);
1064 dev->number_of_steps = value;
1065 dev->vcm_update = true;
1069 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
1071 struct ov5693_device *dev =
1072 container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
1073 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1077 case V4L2_CID_FOCUS_ABSOLUTE:
1078 dev_dbg(&client->dev, "%s: CID_FOCUS_ABSOLUTE:%d.\n",
1079 __func__, ctrl->val);
1080 ret = ov5693_t_focus_abs(&dev->sd, ctrl->val);
1082 case V4L2_CID_FOCUS_RELATIVE:
1083 dev_dbg(&client->dev, "%s: CID_FOCUS_RELATIVE:%d.\n",
1084 __func__, ctrl->val);
1085 ret = ov5693_t_focus_rel(&dev->sd, ctrl->val);
1087 case V4L2_CID_VCM_SLEW:
1088 ret = ov5693_t_vcm_slew(&dev->sd, ctrl->val);
1090 case V4L2_CID_VCM_TIMEING:
1091 ret = ov5693_t_vcm_timing(&dev->sd, ctrl->val);
1099 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1101 struct ov5693_device *dev =
1102 container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
1106 case V4L2_CID_EXPOSURE_ABSOLUTE:
1107 ret = ov5693_q_exposure(&dev->sd, &ctrl->val);
1109 case V4L2_CID_FOCAL_ABSOLUTE:
1110 ret = ov5693_g_focal(&dev->sd, &ctrl->val);
1112 case V4L2_CID_FNUMBER_ABSOLUTE:
1113 ret = ov5693_g_fnumber(&dev->sd, &ctrl->val);
1115 case V4L2_CID_FNUMBER_RANGE:
1116 ret = ov5693_g_fnumber_range(&dev->sd, &ctrl->val);
1118 case V4L2_CID_FOCUS_ABSOLUTE:
1119 ret = ov5693_q_focus_abs(&dev->sd, &ctrl->val);
1121 case V4L2_CID_FOCUS_STATUS:
1122 ret = ov5693_q_focus_status(&dev->sd, &ctrl->val);
1124 case V4L2_CID_BIN_FACTOR_HORZ:
1125 ret = ov5693_g_bin_factor_x(&dev->sd, &ctrl->val);
1127 case V4L2_CID_BIN_FACTOR_VERT:
1128 ret = ov5693_g_bin_factor_y(&dev->sd, &ctrl->val);
1137 static const struct v4l2_ctrl_ops ctrl_ops = {
1138 .s_ctrl = ov5693_s_ctrl,
1139 .g_volatile_ctrl = ov5693_g_volatile_ctrl
1142 static const struct v4l2_ctrl_config ov5693_controls[] = {
1145 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1146 .type = V4L2_CTRL_TYPE_INTEGER,
1156 .id = V4L2_CID_FOCAL_ABSOLUTE,
1157 .type = V4L2_CTRL_TYPE_INTEGER,
1158 .name = "focal length",
1159 .min = OV5693_FOCAL_LENGTH_DEFAULT,
1160 .max = OV5693_FOCAL_LENGTH_DEFAULT,
1162 .def = OV5693_FOCAL_LENGTH_DEFAULT,
1167 .id = V4L2_CID_FNUMBER_ABSOLUTE,
1168 .type = V4L2_CTRL_TYPE_INTEGER,
1170 .min = OV5693_F_NUMBER_DEFAULT,
1171 .max = OV5693_F_NUMBER_DEFAULT,
1173 .def = OV5693_F_NUMBER_DEFAULT,
1178 .id = V4L2_CID_FNUMBER_RANGE,
1179 .type = V4L2_CTRL_TYPE_INTEGER,
1180 .name = "f-number range",
1181 .min = OV5693_F_NUMBER_RANGE,
1182 .max = OV5693_F_NUMBER_RANGE,
1184 .def = OV5693_F_NUMBER_RANGE,
1189 .id = V4L2_CID_FOCUS_ABSOLUTE,
1190 .type = V4L2_CTRL_TYPE_INTEGER,
1191 .name = "focus move absolute",
1193 .max = OV5693_VCM_MAX_FOCUS_POS,
1200 .id = V4L2_CID_FOCUS_RELATIVE,
1201 .type = V4L2_CTRL_TYPE_INTEGER,
1202 .name = "focus move relative",
1203 .min = OV5693_VCM_MAX_FOCUS_NEG,
1204 .max = OV5693_VCM_MAX_FOCUS_POS,
1211 .id = V4L2_CID_FOCUS_STATUS,
1212 .type = V4L2_CTRL_TYPE_INTEGER,
1213 .name = "focus status",
1215 .max = 100, /* allow enum to grow in the future */
1222 .id = V4L2_CID_VCM_SLEW,
1223 .type = V4L2_CTRL_TYPE_INTEGER,
1226 .max = OV5693_VCM_SLEW_STEP_MAX,
1233 .id = V4L2_CID_VCM_TIMEING,
1234 .type = V4L2_CTRL_TYPE_INTEGER,
1235 .name = "vcm step time",
1237 .max = OV5693_VCM_SLEW_TIME_MAX,
1244 .id = V4L2_CID_BIN_FACTOR_HORZ,
1245 .type = V4L2_CTRL_TYPE_INTEGER,
1246 .name = "horizontal binning factor",
1248 .max = OV5693_BIN_FACTOR_MAX,
1255 .id = V4L2_CID_BIN_FACTOR_VERT,
1256 .type = V4L2_CTRL_TYPE_INTEGER,
1257 .name = "vertical binning factor",
1259 .max = OV5693_BIN_FACTOR_MAX,
1266 static int ov5693_init(struct v4l2_subdev *sd)
1268 struct ov5693_device *dev = to_ov5693_sensor(sd);
1269 struct i2c_client *client = v4l2_get_subdevdata(sd);
1272 pr_info("%s\n", __func__);
1273 mutex_lock(&dev->input_lock);
1274 dev->vcm_update = false;
1276 if (dev->vcm == VCM_AD5823) {
1277 ret = vcm_ad_i2c_wr8(client, 0x01, 0x01); /* vcm init test */
1279 dev_err(&client->dev,
1280 "vcm reset failed\n");
1282 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB,
1283 AD5823_RING_CTRL_ENABLE);
1285 dev_err(&client->dev,
1286 "vcm enable ringing failed\n");
1287 ret = ad5823_i2c_write(client, AD5823_REG_MODE,
1290 dev_err(&client->dev,
1291 "vcm change mode failed\n");
1294 /*change initial focus value for ad5823*/
1295 if (dev->vcm == VCM_AD5823) {
1296 dev->focus = AD5823_INIT_FOCUS_POS;
1297 ov5693_t_focus_abs(sd, AD5823_INIT_FOCUS_POS);
1300 ov5693_t_focus_abs(sd, 0);
1303 mutex_unlock(&dev->input_lock);
1308 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
1311 struct ov5693_device *dev = to_ov5693_sensor(sd);
1313 if (!dev || !dev->platform_data)
1317 * This driver assumes "internal DVDD, PWDNB tied to DOVDD".
1318 * In this set up only gpio0 (XSHUTDN) should be available
1319 * but in some products (for example ECS) gpio1 (PWDNB) is
1320 * also available. If gpio1 is available we emulate it being
1321 * tied to DOVDD here.
1324 ret = dev->platform_data->v2p8_ctrl(sd, 1);
1325 dev->platform_data->gpio1_ctrl(sd, 1);
1327 ret = dev->platform_data->v1p8_ctrl(sd, 1);
1329 dev->platform_data->gpio1_ctrl(sd, 0);
1330 ret = dev->platform_data->v2p8_ctrl(sd, 0);
1334 dev->platform_data->gpio1_ctrl(sd, 0);
1335 ret = dev->platform_data->v1p8_ctrl(sd, 0);
1336 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
1342 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
1344 struct ov5693_device *dev = to_ov5693_sensor(sd);
1346 if (!dev || !dev->platform_data)
1349 return dev->platform_data->gpio0_ctrl(sd, flag);
1352 static int __power_up(struct v4l2_subdev *sd)
1354 struct ov5693_device *dev = to_ov5693_sensor(sd);
1355 struct i2c_client *client = v4l2_get_subdevdata(sd);
1358 if (!dev->platform_data) {
1359 dev_err(&client->dev,
1360 "no camera_sensor_platform_data");
1365 ret = power_ctrl(sd, 1);
1369 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
1370 /* add this delay time to 10~11ms*/
1371 usleep_range(10000, 11000);
1374 ret = gpio_ctrl(sd, 1);
1376 ret = gpio_ctrl(sd, 1);
1381 /* flis clock control */
1382 ret = dev->platform_data->flisclk_ctrl(sd, 1);
1386 __cci_delay(up_delay);
1394 dev_err(&client->dev, "sensor power-up failed\n");
1399 static int power_down(struct v4l2_subdev *sd)
1401 struct ov5693_device *dev = to_ov5693_sensor(sd);
1402 struct i2c_client *client = v4l2_get_subdevdata(sd);
1405 dev->focus = OV5693_INVALID_CONFIG;
1406 if (!dev->platform_data) {
1407 dev_err(&client->dev,
1408 "no camera_sensor_platform_data");
1412 ret = dev->platform_data->flisclk_ctrl(sd, 0);
1414 dev_err(&client->dev, "flisclk failed\n");
1417 ret = gpio_ctrl(sd, 0);
1419 ret = gpio_ctrl(sd, 0);
1421 dev_err(&client->dev, "gpio failed 2\n");
1425 ret = power_ctrl(sd, 0);
1427 dev_err(&client->dev, "vprog failed.\n");
1432 static int power_up(struct v4l2_subdev *sd)
1434 static const int retry_count = 4;
1437 for (i = 0; i < retry_count; i++) {
1438 ret = __power_up(sd);
1447 static int ov5693_s_power(struct v4l2_subdev *sd, int on)
1451 pr_info("%s: on %d\n", __func__, on);
1453 return power_down(sd);
1457 ret = ov5693_init(sd);
1458 /* restore settings */
1459 ov5693_res = ov5693_res_preview;
1460 N_RES = N_RES_PREVIEW;
1467 * distance - calculate the distance
1472 * Get the gap between res_w/res_h and w/h.
1473 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1474 * res->width/height smaller than w/h wouldn't be considered.
1475 * The gap of ratio larger than 1/8 wouldn't be considered.
1476 * Returns the value of gap or -1 if fail.
1478 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1479 static int distance(struct ov5693_resolution *res, u32 w, u32 h)
1484 if (w == 0 || h == 0 ||
1485 res->width < w || res->height < h)
1488 ratio = res->width << 13;
1491 ratio /= res->height;
1493 distance = abs(ratio - 8192);
1495 if (distance > LARGEST_ALLOWED_RATIO_MISMATCH)
1501 /* Return the nearest higher resolution index
1502 * Firstly try to find the approximate aspect ratio resolution
1503 * If we find multiple same AR resolutions, choose the
1506 static int nearest_resolution_index(int w, int h)
1511 int min_dist = INT_MAX;
1512 int min_res_w = INT_MAX;
1513 struct ov5693_resolution *tmp_res = NULL;
1515 for (i = 0; i < N_RES; i++) {
1516 tmp_res = &ov5693_res[i];
1517 dist = distance(tmp_res, w, h);
1520 if (dist < min_dist) {
1523 min_res_w = ov5693_res[i].width;
1526 if (dist == min_dist && ov5693_res[i].width < min_res_w)
1533 static int get_resolution_index(int w, int h)
1537 for (i = 0; i < N_RES; i++) {
1538 if (w != ov5693_res[i].width)
1540 if (h != ov5693_res[i].height)
1549 /* TODO: remove it. */
1550 static int startup(struct v4l2_subdev *sd)
1552 struct ov5693_device *dev = to_ov5693_sensor(sd);
1553 struct i2c_client *client = v4l2_get_subdevdata(sd);
1556 ret = ov5693_write_reg(client, OV5693_8BIT,
1557 OV5693_SW_RESET, 0x01);
1559 dev_err(&client->dev, "ov5693 reset err.\n");
1563 ret = ov5693_write_reg_array(client, ov5693_global_setting);
1565 dev_err(&client->dev, "ov5693 write register err.\n");
1569 ret = ov5693_write_reg_array(client, ov5693_res[dev->fmt_idx].regs);
1571 dev_err(&client->dev, "ov5693 write register err.\n");
1578 static int ov5693_set_fmt(struct v4l2_subdev *sd,
1579 struct v4l2_subdev_pad_config *cfg,
1580 struct v4l2_subdev_format *format)
1582 struct v4l2_mbus_framefmt *fmt = &format->format;
1583 struct ov5693_device *dev = to_ov5693_sensor(sd);
1584 struct i2c_client *client = v4l2_get_subdevdata(sd);
1585 struct camera_mipi_info *ov5693_info = NULL;
1593 ov5693_info = v4l2_get_subdev_hostdata(sd);
1597 mutex_lock(&dev->input_lock);
1598 idx = nearest_resolution_index(fmt->width, fmt->height);
1600 /* return the largest resolution */
1601 fmt->width = ov5693_res[N_RES - 1].width;
1602 fmt->height = ov5693_res[N_RES - 1].height;
1604 fmt->width = ov5693_res[idx].width;
1605 fmt->height = ov5693_res[idx].height;
1608 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1609 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1610 cfg->try_fmt = *fmt;
1611 mutex_unlock(&dev->input_lock);
1615 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1616 if (dev->fmt_idx == -1) {
1617 dev_err(&client->dev, "get resolution fail\n");
1618 mutex_unlock(&dev->input_lock);
1626 dev_err(&client->dev, "ov5693 startup err, retry to power up\n");
1627 for (i = 0; i < OV5693_POWER_UP_RETRY_NUM; i++) {
1628 dev_err(&client->dev,
1629 "ov5693 retry to power up %d/%d times, result: ",
1630 i + 1, OV5693_POWER_UP_RETRY_NUM);
1634 mutex_unlock(&dev->input_lock);
1636 mutex_lock(&dev->input_lock);
1638 dev_err(&client->dev, "power up failed, continue\n");
1643 dev_err(&client->dev, " startup FAILED!\n");
1645 dev_err(&client->dev, " startup SUCCESS!\n");
1652 * After sensor settings are set to HW, sometimes stream is started.
1653 * This would cause ISP timeout because ISP is not ready to receive
1654 * data yet. So add stop streaming here.
1656 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1657 OV5693_STOP_STREAMING);
1659 dev_warn(&client->dev, "ov5693 stream off err\n");
1661 ret = ov5693_get_intg_factor(client, ov5693_info,
1662 &ov5693_res[dev->fmt_idx]);
1664 dev_err(&client->dev, "failed to get integration_factor\n");
1668 ov5693_info->metadata_width = fmt->width * 10 / 8;
1669 ov5693_info->metadata_height = 1;
1670 ov5693_info->metadata_effective_width = &ov5693_embedded_effective_size;
1673 mutex_unlock(&dev->input_lock);
1677 static int ov5693_get_fmt(struct v4l2_subdev *sd,
1678 struct v4l2_subdev_pad_config *cfg,
1679 struct v4l2_subdev_format *format)
1681 struct v4l2_mbus_framefmt *fmt = &format->format;
1682 struct ov5693_device *dev = to_ov5693_sensor(sd);
1690 fmt->width = ov5693_res[dev->fmt_idx].width;
1691 fmt->height = ov5693_res[dev->fmt_idx].height;
1692 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1697 static int ov5693_detect(struct i2c_client *client)
1699 struct i2c_adapter *adapter = client->adapter;
1705 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1708 ret = ov5693_read_reg(client, OV5693_8BIT,
1709 OV5693_SC_CMMN_CHIP_ID_H, &high);
1711 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1714 ret = ov5693_read_reg(client, OV5693_8BIT,
1715 OV5693_SC_CMMN_CHIP_ID_L, &low);
1716 id = ((((u16)high) << 8) | (u16)low);
1718 if (id != OV5693_ID) {
1719 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1723 ret = ov5693_read_reg(client, OV5693_8BIT,
1724 OV5693_SC_CMMN_SUB_ID, &high);
1725 revision = (u8)high & 0x0f;
1727 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
1728 dev_dbg(&client->dev, "detect ov5693 success\n");
1732 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
1734 struct ov5693_device *dev = to_ov5693_sensor(sd);
1735 struct i2c_client *client = v4l2_get_subdevdata(sd);
1738 mutex_lock(&dev->input_lock);
1740 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1741 enable ? OV5693_START_STREAMING :
1742 OV5693_STOP_STREAMING);
1744 mutex_unlock(&dev->input_lock);
1749 static int ov5693_s_config(struct v4l2_subdev *sd,
1750 int irq, void *platform_data)
1752 struct ov5693_device *dev = to_ov5693_sensor(sd);
1753 struct i2c_client *client = v4l2_get_subdevdata(sd);
1759 dev->platform_data =
1760 (struct camera_sensor_platform_data *)platform_data;
1762 mutex_lock(&dev->input_lock);
1763 /* power off the module, then power on it in future
1764 * as first power on by board may not fulfill the
1765 * power on sequqence needed by the module
1767 ret = power_down(sd);
1769 dev_err(&client->dev, "ov5693 power-off err.\n");
1770 goto fail_power_off;
1775 dev_err(&client->dev, "ov5693 power-up err.\n");
1780 dev->vcm = vcm_detect(client);
1782 ret = dev->platform_data->csi_cfg(sd, 1);
1786 /* config & detect sensor */
1787 ret = ov5693_detect(client);
1789 dev_err(&client->dev, "ov5693_detect err s_config.\n");
1793 dev->otp_data = ov5693_otp_read(sd);
1795 /* turn off sensor, after probed */
1796 ret = power_down(sd);
1798 dev_err(&client->dev, "ov5693 power-off err.\n");
1801 mutex_unlock(&dev->input_lock);
1806 dev->platform_data->csi_cfg(sd, 0);
1809 dev_err(&client->dev, "sensor power-gating failed\n");
1811 mutex_unlock(&dev->input_lock);
1815 static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
1816 struct v4l2_subdev_frame_interval *interval)
1818 struct ov5693_device *dev = to_ov5693_sensor(sd);
1820 interval->interval.numerator = 1;
1821 interval->interval.denominator = ov5693_res[dev->fmt_idx].fps;
1826 static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
1827 struct v4l2_subdev_pad_config *cfg,
1828 struct v4l2_subdev_mbus_code_enum *code)
1830 if (code->index >= MAX_FMTS)
1833 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1837 static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
1838 struct v4l2_subdev_pad_config *cfg,
1839 struct v4l2_subdev_frame_size_enum *fse)
1841 int index = fse->index;
1846 fse->min_width = ov5693_res[index].width;
1847 fse->min_height = ov5693_res[index].height;
1848 fse->max_width = ov5693_res[index].width;
1849 fse->max_height = ov5693_res[index].height;
1854 static const struct v4l2_subdev_video_ops ov5693_video_ops = {
1855 .s_stream = ov5693_s_stream,
1856 .g_frame_interval = ov5693_g_frame_interval,
1859 static const struct v4l2_subdev_core_ops ov5693_core_ops = {
1860 .s_power = ov5693_s_power,
1861 .ioctl = ov5693_ioctl,
1864 static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
1865 .enum_mbus_code = ov5693_enum_mbus_code,
1866 .enum_frame_size = ov5693_enum_frame_size,
1867 .get_fmt = ov5693_get_fmt,
1868 .set_fmt = ov5693_set_fmt,
1871 static const struct v4l2_subdev_ops ov5693_ops = {
1872 .core = &ov5693_core_ops,
1873 .video = &ov5693_video_ops,
1874 .pad = &ov5693_pad_ops,
1877 static int ov5693_remove(struct i2c_client *client)
1879 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1880 struct ov5693_device *dev = to_ov5693_sensor(sd);
1882 dev_dbg(&client->dev, "ov5693_remove...\n");
1884 dev->platform_data->csi_cfg(sd, 0);
1886 v4l2_device_unregister_subdev(sd);
1888 atomisp_gmin_remove_subdev(sd);
1890 media_entity_cleanup(&dev->sd.entity);
1891 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1897 static int ov5693_probe(struct i2c_client *client)
1899 struct ov5693_device *dev;
1905 struct acpi_device *adev;
1907 handle = ACPI_HANDLE(&client->dev);
1908 if (!handle || acpi_bus_get_device(handle, &adev)) {
1909 dev_err(&client->dev, "Error could not get ACPI device\n");
1913 pr_info("%s: ACPI detected it on bus ID=%s, HID=%s\n",
1914 __func__, acpi_device_bid(adev), acpi_device_hid(adev));
1917 * Firmware workaround: Some modules use a "secondary default"
1918 * address of 0x10 which doesn't appear on schematics, and
1919 * some BIOS versions haven't gotten the memo. Work around
1922 i2c = gmin_get_var_int(&client->dev, false, "I2CAddr", -1);
1924 dev_info(&client->dev,
1925 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n",
1930 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1934 mutex_init(&dev->input_lock);
1937 v4l2_i2c_subdev_init(&dev->sd, client, &ov5693_ops);
1939 pdata = gmin_camera_platform_data(&dev->sd,
1940 ATOMISP_INPUT_FORMAT_RAW_10,
1941 atomisp_bayer_order_bggr);
1945 ret = ov5693_s_config(&dev->sd, client->irq, pdata);
1949 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1953 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1954 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1955 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1956 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1958 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1959 ARRAY_SIZE(ov5693_controls));
1961 ov5693_remove(client);
1965 for (i = 0; i < ARRAY_SIZE(ov5693_controls); i++)
1966 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov5693_controls[i],
1969 if (dev->ctrl_handler.error) {
1970 ov5693_remove(client);
1971 return dev->ctrl_handler.error;
1974 /* Use same lock for controls as for everything else. */
1975 dev->ctrl_handler.lock = &dev->input_lock;
1976 dev->sd.ctrl_handler = &dev->ctrl_handler;
1978 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1980 ov5693_remove(client);
1984 v4l2_device_unregister_subdev(&dev->sd);
1989 static const struct acpi_device_id ov5693_acpi_match[] = {
1993 MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
1995 static struct i2c_driver ov5693_driver = {
1998 .acpi_match_table = ov5693_acpi_match,
2000 .probe_new = ov5693_probe,
2001 .remove = ov5693_remove,
2003 module_i2c_driver(ov5693_driver);
2005 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
2006 MODULE_LICENSE("GPL");