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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/kmod.h>
30 #include <linux/device.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/i2c.h>
34 #include <linux/gpio.h>
35 #include <linux/moduleparam.h>
36 #include <media/v4l2-device.h>
38 #include <linux/acpi.h>
39 #include "../../include/linux/atomisp_gmin_platform.h"
44 #define __cci_delay(t) \
47 usleep_range((t) * 1000, ((t) + 1) * 1000); \
53 /* Value 30ms reached through experimentation on byt ecs.
54 * The DS specifies a much lower value but when using a smaller value
55 * the I2C bus sometimes locks up permanently when starting the camera.
56 * This issue could not be reproduced on cht, so we can reduce the
57 * delay value to a lower value when insmod.
59 static uint up_delay = 30;
60 module_param(up_delay, uint, 0644);
61 MODULE_PARM_DESC(up_delay, "Delay prior to the first CCI transaction for ov5693");
63 static int vcm_ad_i2c_wr8(struct i2c_client *client, u8 reg, u8 val)
77 err = i2c_transfer(client->adapter, &msg, 1);
79 dev_err(&client->dev, "%s: vcm i2c fail, err code = %d\n",
86 static int ad5823_i2c_write(struct i2c_client *client, u8 reg, u8 val)
92 msg.addr = AD5823_VCM_ADDR;
97 if (i2c_transfer(client->adapter, &msg, 1) != 1)
102 static int ad5823_i2c_read(struct i2c_client *client, u8 reg, u8 *val)
104 struct i2c_msg msg[2];
109 msg[0].addr = AD5823_VCM_ADDR;
112 msg[0].buf = &buf[0];
115 msg[1].flags = I2C_M_RD;
117 msg[1].buf = &buf[1];
119 if (i2c_transfer(client->adapter, msg, 2) != 2)
126 static const uint32_t ov5693_embedded_effective_size = 28;
128 /* i2c read/write stuff */
129 static int ov5693_read_reg(struct i2c_client *client,
130 u16 data_length, u16 reg, u16 *val)
133 struct i2c_msg msg[2];
134 unsigned char data[6];
136 if (!client->adapter) {
137 dev_err(&client->dev, "%s error, no client->adapter\n",
142 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT
143 && data_length != OV5693_32BIT) {
144 dev_err(&client->dev, "%s error, invalid data length\n",
149 memset(msg, 0, sizeof(msg));
151 msg[0].addr = client->addr;
153 msg[0].len = I2C_MSG_LENGTH;
156 /* high byte goes out first */
157 data[0] = (u8)(reg >> 8);
158 data[1] = (u8)(reg & 0xff);
160 msg[1].addr = client->addr;
161 msg[1].len = data_length;
162 msg[1].flags = I2C_M_RD;
165 err = i2c_transfer(client->adapter, msg, 2);
169 dev_err(&client->dev,
170 "read from offset 0x%x error %d", reg, err);
175 /* high byte comes first */
176 if (data_length == OV5693_8BIT)
178 else if (data_length == OV5693_16BIT)
179 *val = be16_to_cpu(*(u16 *)&data[0]);
181 *val = be32_to_cpu(*(u32 *)&data[0]);
186 static int ov5693_i2c_write(struct i2c_client *client, u16 len, u8 *data)
189 const int num_msg = 1;
192 msg.addr = client->addr;
196 ret = i2c_transfer(client->adapter, &msg, 1);
198 return ret == num_msg ? 0 : -EIO;
201 static int vcm_dw_i2c_write(struct i2c_client *client, u16 data)
204 const int num_msg = 1;
208 val = cpu_to_be16(data);
211 msg.len = OV5693_16BIT;
212 msg.buf = (u8 *)&val;
214 ret = i2c_transfer(client->adapter, &msg, 1);
216 return ret == num_msg ? 0 : -EIO;
219 /* Theory: per datasheet, the two VCMs both allow for a 2-byte read.
220 * The DW9714 doesn't actually specify what this does (it has a
221 * two-byte write-only protocol, but specifies the read sequence as
222 * legal), but it returns the same data (zeroes) always, after an
223 * undocumented initial NAK. The AD5823 has a one-byte address
224 * register to which all writes go, and subsequent reads will cycle
225 * through the 8 bytes of registers. Notably, the default values (the
226 * device is always power-cycled affirmatively, so we can rely on
227 * these) in AD5823 are not pairwise repetitions of the same 16 bit
228 * word. So all we have to do is sequentially read two bytes at a
229 * time and see if we detect a difference in any of the first four
231 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);
243 /* DW9714 always fails the first read and returns
244 * zeroes for subsequent ones */
245 if (i == 0 && ret == -EREMOTEIO) {
256 return ret == 1 ? VCM_DW9714 : ret;
259 static int ov5693_write_reg(struct i2c_client *client, u16 data_length,
263 unsigned char data[4] = {0};
264 u16 *wreg = (u16 *)data;
265 const u16 len = data_length + sizeof(u16); /* 16-bit address + data */
267 if (data_length != OV5693_8BIT && data_length != OV5693_16BIT) {
268 dev_err(&client->dev,
269 "%s error, invalid data_length\n", __func__);
273 /* high byte goes out first */
274 *wreg = cpu_to_be16(reg);
276 if (data_length == OV5693_8BIT) {
280 u16 *wdata = (u16 *)&data[2];
281 *wdata = cpu_to_be16(val);
284 ret = ov5693_i2c_write(client, len, data);
286 dev_err(&client->dev,
287 "write error: wrote 0x%x to offset 0x%x error %d",
294 * ov5693_write_reg_array - Initializes a list of OV5693 registers
295 * @client: i2c driver client structure
296 * @reglist: list of registers to be written
298 * This function initializes a list of registers. When consecutive addresses
299 * are found in a row on the list, this function creates a buffer and sends
300 * consecutive data in a single i2c_transfer().
302 * __ov5693_flush_reg_array, __ov5693_buf_reg_array() and
303 * __ov5693_write_reg_is_consecutive() are internal functions to
304 * ov5693_write_reg_array_fast() and should be not used anywhere else.
308 static int __ov5693_flush_reg_array(struct i2c_client *client,
309 struct ov5693_write_ctrl *ctrl)
313 if (ctrl->index == 0)
316 size = sizeof(u16) + ctrl->index; /* 16-bit address + data */
317 ctrl->buffer.addr = cpu_to_be16(ctrl->buffer.addr);
320 return ov5693_i2c_write(client, size, (u8 *)&ctrl->buffer);
323 static int __ov5693_buf_reg_array(struct i2c_client *client,
324 struct ov5693_write_ctrl *ctrl,
325 const struct ov5693_reg *next)
330 switch (next->type) {
333 ctrl->buffer.data[ctrl->index] = (u8)next->val;
337 data16 = (u16 *)&ctrl->buffer.data[ctrl->index];
338 *data16 = cpu_to_be16((u16)next->val);
344 /* When first item is added, we need to store its starting address */
345 if (ctrl->index == 0)
346 ctrl->buffer.addr = next->reg;
351 * Buffer cannot guarantee free space for u32? Better flush it to avoid
352 * possible lack of memory for next item.
354 if (ctrl->index + sizeof(u16) >= OV5693_MAX_WRITE_BUF_SIZE)
355 return __ov5693_flush_reg_array(client, ctrl);
360 static int __ov5693_write_reg_is_consecutive(struct i2c_client *client,
361 struct ov5693_write_ctrl *ctrl,
362 const struct ov5693_reg *next)
364 if (ctrl->index == 0)
367 return ctrl->buffer.addr + ctrl->index == next->reg;
370 static int ov5693_write_reg_array(struct i2c_client *client,
371 const struct ov5693_reg *reglist)
373 const struct ov5693_reg *next = reglist;
374 struct ov5693_write_ctrl ctrl;
378 for (; next->type != OV5693_TOK_TERM; next++) {
379 switch (next->type & OV5693_TOK_MASK) {
380 case OV5693_TOK_DELAY:
381 err = __ov5693_flush_reg_array(client, &ctrl);
388 * If next address is not consecutive, data needs to be
389 * flushed before proceed.
391 if (!__ov5693_write_reg_is_consecutive(client, &ctrl,
393 err = __ov5693_flush_reg_array(client, &ctrl);
397 err = __ov5693_buf_reg_array(client, &ctrl, next);
399 dev_err(&client->dev,
400 "%s: write error, aborted\n",
408 return __ov5693_flush_reg_array(client, &ctrl);
410 static int ov5693_g_focal(struct v4l2_subdev *sd, s32 *val)
412 *val = (OV5693_FOCAL_LENGTH_NUM << 16) | OV5693_FOCAL_LENGTH_DEM;
416 static int ov5693_g_fnumber(struct v4l2_subdev *sd, s32 *val)
418 /*const f number for imx*/
419 *val = (OV5693_F_NUMBER_DEFAULT_NUM << 16) | OV5693_F_NUMBER_DEM;
423 static int ov5693_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
425 *val = (OV5693_F_NUMBER_DEFAULT_NUM << 24) |
426 (OV5693_F_NUMBER_DEM << 16) |
427 (OV5693_F_NUMBER_DEFAULT_NUM << 8) | OV5693_F_NUMBER_DEM;
431 static int ov5693_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
433 struct ov5693_device *dev = to_ov5693_sensor(sd);
435 *val = ov5693_res[dev->fmt_idx].bin_factor_x;
440 static int ov5693_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
442 struct ov5693_device *dev = to_ov5693_sensor(sd);
444 *val = ov5693_res[dev->fmt_idx].bin_factor_y;
449 static int ov5693_get_intg_factor(struct i2c_client *client,
450 struct camera_mipi_info *info,
451 const struct ov5693_resolution *res)
453 struct v4l2_subdev *sd = i2c_get_clientdata(client);
454 struct ov5693_device *dev = to_ov5693_sensor(sd);
455 struct atomisp_sensor_mode_data *buf = &info->data;
456 unsigned int pix_clk_freq_hz;
464 pix_clk_freq_hz = res->pix_clk_freq * 1000000;
466 dev->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
467 buf->vt_pix_clk_freq_mhz = pix_clk_freq_hz;
469 /* get integration time */
470 buf->coarse_integration_time_min = OV5693_COARSE_INTG_TIME_MIN;
471 buf->coarse_integration_time_max_margin =
472 OV5693_COARSE_INTG_TIME_MAX_MARGIN;
474 buf->fine_integration_time_min = OV5693_FINE_INTG_TIME_MIN;
475 buf->fine_integration_time_max_margin =
476 OV5693_FINE_INTG_TIME_MAX_MARGIN;
478 buf->fine_integration_time_def = OV5693_FINE_INTG_TIME_MIN;
479 buf->frame_length_lines = res->lines_per_frame;
480 buf->line_length_pck = res->pixels_per_line;
481 buf->read_mode = res->bin_mode;
483 /* get the cropping and output resolution to ISP for this mode. */
484 ret = ov5693_read_reg(client, OV5693_16BIT,
485 OV5693_HORIZONTAL_START_H, ®_val);
488 buf->crop_horizontal_start = reg_val;
490 ret = ov5693_read_reg(client, OV5693_16BIT,
491 OV5693_VERTICAL_START_H, ®_val);
494 buf->crop_vertical_start = reg_val;
496 ret = ov5693_read_reg(client, OV5693_16BIT,
497 OV5693_HORIZONTAL_END_H, ®_val);
500 buf->crop_horizontal_end = reg_val;
502 ret = ov5693_read_reg(client, OV5693_16BIT,
503 OV5693_VERTICAL_END_H, ®_val);
506 buf->crop_vertical_end = reg_val;
508 ret = ov5693_read_reg(client, OV5693_16BIT,
509 OV5693_HORIZONTAL_OUTPUT_SIZE_H, ®_val);
512 buf->output_width = reg_val;
514 ret = ov5693_read_reg(client, OV5693_16BIT,
515 OV5693_VERTICAL_OUTPUT_SIZE_H, ®_val);
518 buf->output_height = reg_val;
520 buf->binning_factor_x = res->bin_factor_x ?
521 res->bin_factor_x : 1;
522 buf->binning_factor_y = res->bin_factor_y ?
523 res->bin_factor_y : 1;
527 static long __ov5693_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
528 int gain, int digitgain)
531 struct i2c_client *client = v4l2_get_subdevdata(sd);
532 struct ov5693_device *dev = to_ov5693_sensor(sd);
536 hts = ov5693_res[dev->fmt_idx].pixels_per_line;
537 vts = ov5693_res[dev->fmt_idx].lines_per_frame;
538 /*If coarse_itg is larger than 1<<15, can not write to reg directly.
539 The way is to write coarse_itg/2 to the reg, meanwhile write 2*hts
541 if (coarse_itg > (1 << 15)) {
543 coarse_itg = (int)coarse_itg / 2;
546 ret = ov5693_write_reg(client, OV5693_8BIT,
547 OV5693_GROUP_ACCESS, 0x00);
549 dev_err(&client->dev, "%s: write %x error, aborted\n",
550 __func__, OV5693_GROUP_ACCESS);
554 ret = ov5693_write_reg(client, OV5693_8BIT,
555 OV5693_TIMING_HTS_H, (hts >> 8) & 0xFF);
557 dev_err(&client->dev, "%s: write %x error, aborted\n",
558 __func__, OV5693_TIMING_HTS_H);
562 ret = ov5693_write_reg(client, OV5693_8BIT,
563 OV5693_TIMING_HTS_L, hts & 0xFF);
565 dev_err(&client->dev, "%s: write %x error, aborted\n",
566 __func__, OV5693_TIMING_HTS_L);
569 /* Increase the VTS to match exposure + MARGIN */
570 if (coarse_itg > vts - OV5693_INTEGRATION_TIME_MARGIN)
571 vts = (u16) coarse_itg + OV5693_INTEGRATION_TIME_MARGIN;
573 ret = ov5693_write_reg(client, OV5693_8BIT,
574 OV5693_TIMING_VTS_H, (vts >> 8) & 0xFF);
576 dev_err(&client->dev, "%s: write %x error, aborted\n",
577 __func__, OV5693_TIMING_VTS_H);
581 ret = ov5693_write_reg(client, OV5693_8BIT,
582 OV5693_TIMING_VTS_L, vts & 0xFF);
584 dev_err(&client->dev, "%s: write %x error, aborted\n",
585 __func__, OV5693_TIMING_VTS_L);
591 /* Lower four bit should be 0*/
592 exp_val = coarse_itg << 4;
593 ret = ov5693_write_reg(client, OV5693_8BIT,
594 OV5693_EXPOSURE_L, exp_val & 0xFF);
596 dev_err(&client->dev, "%s: write %x error, aborted\n",
597 __func__, OV5693_EXPOSURE_L);
601 ret = ov5693_write_reg(client, OV5693_8BIT,
602 OV5693_EXPOSURE_M, (exp_val >> 8) & 0xFF);
604 dev_err(&client->dev, "%s: write %x error, aborted\n",
605 __func__, OV5693_EXPOSURE_M);
609 ret = ov5693_write_reg(client, OV5693_8BIT,
610 OV5693_EXPOSURE_H, (exp_val >> 16) & 0x0F);
612 dev_err(&client->dev, "%s: write %x error, aborted\n",
613 __func__, OV5693_EXPOSURE_H);
618 ret = ov5693_write_reg(client, OV5693_8BIT,
619 OV5693_AGC_L, gain & 0xff);
621 dev_err(&client->dev, "%s: write %x error, aborted\n",
622 __func__, OV5693_AGC_L);
626 ret = ov5693_write_reg(client, OV5693_8BIT,
627 OV5693_AGC_H, (gain >> 8) & 0xff);
629 dev_err(&client->dev, "%s: write %x error, aborted\n",
630 __func__, OV5693_AGC_H);
636 ret = ov5693_write_reg(client, OV5693_16BIT,
637 OV5693_MWB_RED_GAIN_H, digitgain);
639 dev_err(&client->dev, "%s: write %x error, aborted\n",
640 __func__, OV5693_MWB_RED_GAIN_H);
644 ret = ov5693_write_reg(client, OV5693_16BIT,
645 OV5693_MWB_GREEN_GAIN_H, digitgain);
647 dev_err(&client->dev, "%s: write %x error, aborted\n",
648 __func__, OV5693_MWB_RED_GAIN_H);
652 ret = ov5693_write_reg(client, OV5693_16BIT,
653 OV5693_MWB_BLUE_GAIN_H, digitgain);
655 dev_err(&client->dev, "%s: write %x error, aborted\n",
656 __func__, OV5693_MWB_RED_GAIN_H);
662 ret = ov5693_write_reg(client, OV5693_8BIT,
663 OV5693_GROUP_ACCESS, 0x10);
667 /* Delay launch group */
668 ret = ov5693_write_reg(client, OV5693_8BIT,
669 OV5693_GROUP_ACCESS, 0xa0);
675 static int ov5693_set_exposure(struct v4l2_subdev *sd, int exposure,
676 int gain, int digitgain)
678 struct ov5693_device *dev = to_ov5693_sensor(sd);
681 mutex_lock(&dev->input_lock);
682 ret = __ov5693_set_exposure(sd, exposure, gain, digitgain);
683 mutex_unlock(&dev->input_lock);
688 static long ov5693_s_exposure(struct v4l2_subdev *sd,
689 struct atomisp_exposure *exposure)
691 u16 coarse_itg = exposure->integration_time[0];
692 u16 analog_gain = exposure->gain[0];
693 u16 digital_gain = exposure->gain[1];
695 /* we should not accept the invalid value below */
696 if (analog_gain == 0) {
697 struct i2c_client *client = v4l2_get_subdevdata(sd);
698 v4l2_err(client, "%s: invalid value\n", __func__);
701 return ov5693_set_exposure(sd, coarse_itg, analog_gain, digital_gain);
704 static int ov5693_read_otp_reg_array(struct i2c_client *client, u16 size,
711 for (index = 0; index <= size; index++) {
712 pVal = (u16 *) (buf + index);
714 ov5693_read_reg(client, OV5693_8BIT, addr + index,
723 static int __ov5693_otp_read(struct v4l2_subdev *sd, u8 *buf)
725 struct i2c_client *client = v4l2_get_subdevdata(sd);
726 struct ov5693_device *dev = to_ov5693_sensor(sd);
731 for (i = 1; i < OV5693_OTP_BANK_MAX; i++) {
732 /*set bank NO and OTP read mode. */
733 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_BANK_REG, (i | 0xc0)); //[7:6] 2'b11 [5:0] bank no
735 dev_err(&client->dev, "failed to prepare OTP page\n");
738 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_BANK_REG,(i|0xc0));
741 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_OTP_READ_REG, OV5693_OTP_MODE_READ); // enable :1
743 dev_err(&client->dev,
744 "failed to set OTP reading mode page");
747 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_READ_REG,OV5693_OTP_MODE_READ);
749 /* Reading the OTP data array */
750 ret = ov5693_read_otp_reg_array(client, OV5693_OTP_BANK_SIZE,
751 OV5693_OTP_START_ADDR,
754 dev_err(&client->dev, "failed to read OTP data\n");
758 //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));
760 //Intel OTP map, try to read 320byts first.
769 } else if (24 == i) { //if the first 320bytes data doesn't not exist, try to read the next 32bytes data.
777 } else if (27 == i) { //if the prvious 32bytes data doesn't exist, try to read the next 32bytes data again.
782 dev->otp_size = 0; // no OTP data.
787 b = b + OV5693_OTP_BANK_SIZE;
793 * Read otp data and store it into a kmalloced buffer.
794 * The caller must kfree the buffer when no more needed.
795 * @size: set to the size of the returned otp data.
797 static void *ov5693_otp_read(struct v4l2_subdev *sd)
799 struct i2c_client *client = v4l2_get_subdevdata(sd);
803 buf = devm_kzalloc(&client->dev, (OV5693_OTP_DATA_SIZE + 16), GFP_KERNEL);
805 return ERR_PTR(-ENOMEM);
807 //otp valid after mipi on and sw stream on
808 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x00);
810 ret = ov5693_write_reg(client, OV5693_8BIT,
811 OV5693_SW_STREAM, OV5693_START_STREAMING);
813 ret = __ov5693_otp_read(sd, buf);
815 //mipi off and sw stream off after otp read
816 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_FRAME_OFF_NUM, 0x0f);
818 ret = ov5693_write_reg(client, OV5693_8BIT,
819 OV5693_SW_STREAM, OV5693_STOP_STREAMING);
821 /* Driver has failed to find valid data */
823 dev_err(&client->dev, "sensor found no valid OTP data\n");
830 static int ov5693_g_priv_int_data(struct v4l2_subdev *sd,
831 struct v4l2_private_int_data *priv)
833 struct i2c_client *client = v4l2_get_subdevdata(sd);
834 struct ov5693_device *dev = to_ov5693_sensor(sd);
835 u8 __user *to = priv->data;
836 u32 read_size = priv->size;
839 /* No need to copy data if size is 0 */
843 if (IS_ERR(dev->otp_data)) {
844 dev_err(&client->dev, "OTP data not available");
845 return PTR_ERR(dev->otp_data);
848 /* Correct read_size value only if bigger than maximum */
849 if (read_size > OV5693_OTP_DATA_SIZE)
850 read_size = OV5693_OTP_DATA_SIZE;
852 ret = copy_to_user(to, dev->otp_data, read_size);
854 dev_err(&client->dev, "%s: failed to copy OTP data to user\n",
859 pr_debug("%s read_size:%d\n", __func__, read_size);
862 /* Return correct size */
863 priv->size = dev->otp_size;
869 static long ov5693_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
873 case ATOMISP_IOC_S_EXPOSURE:
874 return ov5693_s_exposure(sd, arg);
875 case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA:
876 return ov5693_g_priv_int_data(sd, arg);
883 /* This returns the exposure time being used. This should only be used
884 for filling in EXIF data, not for actual image processing. */
885 static int ov5693_q_exposure(struct v4l2_subdev *sd, s32 *value)
887 struct i2c_client *client = v4l2_get_subdevdata(sd);
892 ret = ov5693_read_reg(client, OV5693_8BIT,
898 ret = ov5693_read_reg(client, OV5693_8BIT,
904 reg_v += reg_v2 << 8;
905 ret = ov5693_read_reg(client, OV5693_8BIT,
911 *value = reg_v + (((u32)reg_v2 << 16));
916 static int ad5823_t_focus_vcm(struct v4l2_subdev *sd, u16 val)
918 struct i2c_client *client = v4l2_get_subdevdata(sd);
922 ret = ad5823_i2c_read(client, AD5823_REG_VCM_CODE_MSB, &vcm_code);
926 /* set reg VCM_CODE_MSB Bit[1:0] */
927 vcm_code = (vcm_code & VCM_CODE_MSB_MASK) |
928 ((val >> 8) & ~VCM_CODE_MSB_MASK);
929 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB, vcm_code);
933 /* set reg VCM_CODE_LSB Bit[7:0] */
934 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_LSB, (val & 0xff));
938 /* set required vcm move time */
939 vcm_code = AD5823_RESONANCE_PERIOD / AD5823_RESONANCE_COEF
940 - AD5823_HIGH_FREQ_RANGE;
941 ret = ad5823_i2c_write(client, AD5823_REG_VCM_MOVE_TIME, vcm_code);
946 int ad5823_t_focus_abs(struct v4l2_subdev *sd, s32 value)
950 value = min(value, AD5823_MAX_FOCUS_POS);
951 ret = ad5823_t_focus_vcm(sd, value);
956 static int ov5693_t_focus_abs(struct v4l2_subdev *sd, s32 value)
958 struct ov5693_device *dev = to_ov5693_sensor(sd);
959 struct i2c_client *client = v4l2_get_subdevdata(sd);
962 dev_dbg(&client->dev, "%s: FOCUS_POS: 0x%x\n", __func__, value);
963 value = clamp(value, 0, OV5693_VCM_MAX_FOCUS_POS);
964 if (dev->vcm == VCM_DW9714) {
965 if (dev->vcm_update) {
966 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_OFF);
969 ret = vcm_dw_i2c_write(client, DIRECT_VCM);
972 ret = vcm_dw_i2c_write(client, VCM_PROTECTION_ON);
975 dev->vcm_update = false;
977 ret = vcm_dw_i2c_write(client,
978 vcm_val(value, VCM_DEFAULT_S));
979 } else if (dev->vcm == VCM_AD5823) {
980 ad5823_t_focus_abs(sd, value);
983 dev->number_of_steps = value - dev->focus;
985 getnstimeofday(&(dev->timestamp_t_focus_abs));
987 dev_err(&client->dev,
988 "%s: i2c failed. ret %d\n", __func__, ret);
993 static int ov5693_t_focus_rel(struct v4l2_subdev *sd, s32 value)
995 struct ov5693_device *dev = to_ov5693_sensor(sd);
996 return ov5693_t_focus_abs(sd, dev->focus + value);
999 #define DELAY_PER_STEP_NS 1000000
1000 #define DELAY_MAX_PER_STEP_NS (1000000 * 1023)
1001 static int ov5693_q_focus_status(struct v4l2_subdev *sd, s32 *value)
1004 struct ov5693_device *dev = to_ov5693_sensor(sd);
1005 struct timespec temptime;
1006 const struct timespec timedelay = {
1008 min((u32)abs(dev->number_of_steps) * DELAY_PER_STEP_NS,
1009 (u32)DELAY_MAX_PER_STEP_NS),
1012 getnstimeofday(&temptime);
1013 temptime = timespec_sub(temptime, (dev->timestamp_t_focus_abs));
1014 if (timespec_compare(&temptime, &timedelay) <= 0) {
1015 status |= ATOMISP_FOCUS_STATUS_MOVING;
1016 status |= ATOMISP_FOCUS_HP_IN_PROGRESS;
1018 status |= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE;
1019 status |= ATOMISP_FOCUS_HP_COMPLETE;
1027 static int ov5693_q_focus_abs(struct v4l2_subdev *sd, s32 *value)
1029 struct ov5693_device *dev = to_ov5693_sensor(sd);
1032 ov5693_q_focus_status(sd, &val);
1034 if (val & ATOMISP_FOCUS_STATUS_MOVING)
1035 *value = dev->focus - dev->number_of_steps;
1037 *value = dev->focus;
1042 static int ov5693_t_vcm_slew(struct v4l2_subdev *sd, s32 value)
1044 struct ov5693_device *dev = to_ov5693_sensor(sd);
1045 dev->number_of_steps = value;
1046 dev->vcm_update = true;
1050 static int ov5693_t_vcm_timing(struct v4l2_subdev *sd, s32 value)
1052 struct ov5693_device *dev = to_ov5693_sensor(sd);
1053 dev->number_of_steps = value;
1054 dev->vcm_update = true;
1058 static int ov5693_s_ctrl(struct v4l2_ctrl *ctrl)
1060 struct ov5693_device *dev =
1061 container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
1062 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1066 case V4L2_CID_FOCUS_ABSOLUTE:
1067 dev_dbg(&client->dev, "%s: CID_FOCUS_ABSOLUTE:%d.\n",
1068 __func__, ctrl->val);
1069 ret = ov5693_t_focus_abs(&dev->sd, ctrl->val);
1071 case V4L2_CID_FOCUS_RELATIVE:
1072 dev_dbg(&client->dev, "%s: CID_FOCUS_RELATIVE:%d.\n",
1073 __func__, ctrl->val);
1074 ret = ov5693_t_focus_rel(&dev->sd, ctrl->val);
1076 case V4L2_CID_VCM_SLEW:
1077 ret = ov5693_t_vcm_slew(&dev->sd, ctrl->val);
1079 case V4L2_CID_VCM_TIMEING:
1080 ret = ov5693_t_vcm_timing(&dev->sd, ctrl->val);
1088 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1090 struct ov5693_device *dev =
1091 container_of(ctrl->handler, struct ov5693_device, ctrl_handler);
1095 case V4L2_CID_EXPOSURE_ABSOLUTE:
1096 ret = ov5693_q_exposure(&dev->sd, &ctrl->val);
1098 case V4L2_CID_FOCAL_ABSOLUTE:
1099 ret = ov5693_g_focal(&dev->sd, &ctrl->val);
1101 case V4L2_CID_FNUMBER_ABSOLUTE:
1102 ret = ov5693_g_fnumber(&dev->sd, &ctrl->val);
1104 case V4L2_CID_FNUMBER_RANGE:
1105 ret = ov5693_g_fnumber_range(&dev->sd, &ctrl->val);
1107 case V4L2_CID_FOCUS_ABSOLUTE:
1108 ret = ov5693_q_focus_abs(&dev->sd, &ctrl->val);
1110 case V4L2_CID_FOCUS_STATUS:
1111 ret = ov5693_q_focus_status(&dev->sd, &ctrl->val);
1113 case V4L2_CID_BIN_FACTOR_HORZ:
1114 ret = ov5693_g_bin_factor_x(&dev->sd, &ctrl->val);
1116 case V4L2_CID_BIN_FACTOR_VERT:
1117 ret = ov5693_g_bin_factor_y(&dev->sd, &ctrl->val);
1126 static const struct v4l2_ctrl_ops ctrl_ops = {
1127 .s_ctrl = ov5693_s_ctrl,
1128 .g_volatile_ctrl = ov5693_g_volatile_ctrl
1131 struct v4l2_ctrl_config ov5693_controls[] = {
1134 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
1135 .type = V4L2_CTRL_TYPE_INTEGER,
1145 .id = V4L2_CID_FOCAL_ABSOLUTE,
1146 .type = V4L2_CTRL_TYPE_INTEGER,
1147 .name = "focal length",
1148 .min = OV5693_FOCAL_LENGTH_DEFAULT,
1149 .max = OV5693_FOCAL_LENGTH_DEFAULT,
1151 .def = OV5693_FOCAL_LENGTH_DEFAULT,
1156 .id = V4L2_CID_FNUMBER_ABSOLUTE,
1157 .type = V4L2_CTRL_TYPE_INTEGER,
1159 .min = OV5693_F_NUMBER_DEFAULT,
1160 .max = OV5693_F_NUMBER_DEFAULT,
1162 .def = OV5693_F_NUMBER_DEFAULT,
1167 .id = V4L2_CID_FNUMBER_RANGE,
1168 .type = V4L2_CTRL_TYPE_INTEGER,
1169 .name = "f-number range",
1170 .min = OV5693_F_NUMBER_RANGE,
1171 .max = OV5693_F_NUMBER_RANGE,
1173 .def = OV5693_F_NUMBER_RANGE,
1178 .id = V4L2_CID_FOCUS_ABSOLUTE,
1179 .type = V4L2_CTRL_TYPE_INTEGER,
1180 .name = "focus move absolute",
1182 .max = OV5693_VCM_MAX_FOCUS_POS,
1189 .id = V4L2_CID_FOCUS_RELATIVE,
1190 .type = V4L2_CTRL_TYPE_INTEGER,
1191 .name = "focus move relative",
1192 .min = OV5693_VCM_MAX_FOCUS_NEG,
1193 .max = OV5693_VCM_MAX_FOCUS_POS,
1200 .id = V4L2_CID_FOCUS_STATUS,
1201 .type = V4L2_CTRL_TYPE_INTEGER,
1202 .name = "focus status",
1204 .max = 100, /* allow enum to grow in the future */
1211 .id = V4L2_CID_VCM_SLEW,
1212 .type = V4L2_CTRL_TYPE_INTEGER,
1215 .max = OV5693_VCM_SLEW_STEP_MAX,
1222 .id = V4L2_CID_VCM_TIMEING,
1223 .type = V4L2_CTRL_TYPE_INTEGER,
1224 .name = "vcm step time",
1226 .max = OV5693_VCM_SLEW_TIME_MAX,
1233 .id = V4L2_CID_BIN_FACTOR_HORZ,
1234 .type = V4L2_CTRL_TYPE_INTEGER,
1235 .name = "horizontal binning factor",
1237 .max = OV5693_BIN_FACTOR_MAX,
1244 .id = V4L2_CID_BIN_FACTOR_VERT,
1245 .type = V4L2_CTRL_TYPE_INTEGER,
1246 .name = "vertical binning factor",
1248 .max = OV5693_BIN_FACTOR_MAX,
1255 static int ov5693_init(struct v4l2_subdev *sd)
1257 struct ov5693_device *dev = to_ov5693_sensor(sd);
1258 struct i2c_client *client = v4l2_get_subdevdata(sd);
1261 pr_info("%s\n", __func__);
1262 mutex_lock(&dev->input_lock);
1263 dev->vcm_update = false;
1265 if (dev->vcm == VCM_AD5823) {
1266 ret = vcm_ad_i2c_wr8(client, 0x01, 0x01); /* vcm init test */
1268 dev_err(&client->dev,
1269 "vcm reset failed\n");
1271 ret = ad5823_i2c_write(client, AD5823_REG_VCM_CODE_MSB,
1272 AD5823_RING_CTRL_ENABLE);
1274 dev_err(&client->dev,
1275 "vcm enable ringing failed\n");
1276 ret = ad5823_i2c_write(client, AD5823_REG_MODE,
1279 dev_err(&client->dev,
1280 "vcm change mode failed\n");
1283 /*change initial focus value for ad5823*/
1284 if (dev->vcm == VCM_AD5823) {
1285 dev->focus = AD5823_INIT_FOCUS_POS;
1286 ov5693_t_focus_abs(sd, AD5823_INIT_FOCUS_POS);
1289 ov5693_t_focus_abs(sd, 0);
1292 mutex_unlock(&dev->input_lock);
1297 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
1300 struct ov5693_device *dev = to_ov5693_sensor(sd);
1302 if (!dev || !dev->platform_data)
1305 /* Non-gmin platforms use the legacy callback */
1306 if (dev->platform_data->power_ctrl)
1307 return dev->platform_data->power_ctrl(sd, flag);
1309 /* This driver assumes "internal DVDD, PWDNB tied to DOVDD".
1310 * In this set up only gpio0 (XSHUTDN) should be available
1311 * but in some products (for example ECS) gpio1 (PWDNB) is
1312 * also available. If gpio1 is available we emulate it being
1313 * tied to DOVDD here. */
1315 ret = dev->platform_data->v2p8_ctrl(sd, 1);
1316 dev->platform_data->gpio1_ctrl(sd, 1);
1318 ret = dev->platform_data->v1p8_ctrl(sd, 1);
1320 dev->platform_data->gpio1_ctrl(sd, 0);
1321 ret = dev->platform_data->v2p8_ctrl(sd, 0);
1325 dev->platform_data->gpio1_ctrl(sd, 0);
1326 ret = dev->platform_data->v1p8_ctrl(sd, 0);
1327 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
1333 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
1336 struct ov5693_device *dev = to_ov5693_sensor(sd);
1338 if (!dev || !dev->platform_data)
1341 /* Non-gmin platforms use the legacy callback */
1342 if (dev->platform_data->gpio_ctrl)
1343 return dev->platform_data->gpio_ctrl(sd, flag);
1345 ret = dev->platform_data->gpio0_ctrl(sd, flag);
1350 static int __power_up(struct v4l2_subdev *sd)
1352 struct ov5693_device *dev = to_ov5693_sensor(sd);
1353 struct i2c_client *client = v4l2_get_subdevdata(sd);
1356 if (NULL == dev->platform_data) {
1357 dev_err(&client->dev,
1358 "no camera_sensor_platform_data");
1363 ret = power_ctrl(sd, 1);
1367 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
1368 /* add this delay time to 10~11ms*/
1369 usleep_range(10000, 11000);
1372 ret = gpio_ctrl(sd, 1);
1374 ret = gpio_ctrl(sd, 1);
1379 /* flis clock control */
1380 ret = dev->platform_data->flisclk_ctrl(sd, 1);
1384 __cci_delay(up_delay);
1392 dev_err(&client->dev, "sensor power-up failed\n");
1397 static int power_down(struct v4l2_subdev *sd)
1399 struct ov5693_device *dev = to_ov5693_sensor(sd);
1400 struct i2c_client *client = v4l2_get_subdevdata(sd);
1403 dev->focus = OV5693_INVALID_CONFIG;
1404 if (NULL == dev->platform_data) {
1405 dev_err(&client->dev,
1406 "no camera_sensor_platform_data");
1410 ret = dev->platform_data->flisclk_ctrl(sd, 0);
1412 dev_err(&client->dev, "flisclk failed\n");
1415 ret = gpio_ctrl(sd, 0);
1417 ret = gpio_ctrl(sd, 0);
1419 dev_err(&client->dev, "gpio failed 2\n");
1423 ret = power_ctrl(sd, 0);
1425 dev_err(&client->dev, "vprog failed.\n");
1430 static int power_up(struct v4l2_subdev *sd)
1432 static const int retry_count = 4;
1435 for (i = 0; i < retry_count; i++) {
1436 ret = __power_up(sd);
1445 static int ov5693_s_power(struct v4l2_subdev *sd, int on)
1449 pr_info("%s: on %d\n", __func__, on);
1451 return power_down(sd);
1455 ret = ov5693_init(sd);
1456 /* restore settings */
1457 ov5693_res = ov5693_res_preview;
1458 N_RES = N_RES_PREVIEW;
1465 * distance - calculate the distance
1470 * Get the gap between res_w/res_h and w/h.
1471 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1472 * res->width/height smaller than w/h wouldn't be considered.
1473 * The gap of ratio larger than 1/8 wouldn't be considered.
1474 * Returns the value of gap or -1 if fail.
1476 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1477 static int distance(struct ov5693_resolution *res, u32 w, u32 h)
1482 if (w == 0 || h == 0 ||
1483 res->width < w || res->height < h)
1486 ratio = res->width << 13;
1489 ratio /= res->height;
1491 distance = abs(ratio - 8192);
1493 if (distance > LARGEST_ALLOWED_RATIO_MISMATCH)
1499 /* Return the nearest higher resolution index
1500 * Firstly try to find the approximate aspect ratio resolution
1501 * If we find multiple same AR resolutions, choose the
1504 static int nearest_resolution_index(int w, int h)
1509 int min_dist = INT_MAX;
1510 int min_res_w = INT_MAX;
1511 struct ov5693_resolution *tmp_res = NULL;
1513 for (i = 0; i < N_RES; i++) {
1514 tmp_res = &ov5693_res[i];
1515 dist = distance(tmp_res, w, h);
1518 if (dist < min_dist) {
1521 min_res_w = ov5693_res[i].width;
1524 if (dist == min_dist && ov5693_res[i].width < min_res_w)
1531 static int get_resolution_index(int w, int h)
1535 for (i = 0; i < N_RES; i++) {
1536 if (w != ov5693_res[i].width)
1538 if (h != ov5693_res[i].height)
1547 /* TODO: remove it. */
1548 static int startup(struct v4l2_subdev *sd)
1550 struct ov5693_device *dev = to_ov5693_sensor(sd);
1551 struct i2c_client *client = v4l2_get_subdevdata(sd);
1554 ret = ov5693_write_reg(client, OV5693_8BIT,
1555 OV5693_SW_RESET, 0x01);
1557 dev_err(&client->dev, "ov5693 reset err.\n");
1561 ret = ov5693_write_reg_array(client, ov5693_global_setting);
1563 dev_err(&client->dev, "ov5693 write register err.\n");
1567 ret = ov5693_write_reg_array(client, ov5693_res[dev->fmt_idx].regs);
1569 dev_err(&client->dev, "ov5693 write register err.\n");
1576 static int ov5693_set_fmt(struct v4l2_subdev *sd,
1577 struct v4l2_subdev_pad_config *cfg,
1578 struct v4l2_subdev_format *format)
1580 struct v4l2_mbus_framefmt *fmt = &format->format;
1581 struct ov5693_device *dev = to_ov5693_sensor(sd);
1582 struct i2c_client *client = v4l2_get_subdevdata(sd);
1583 struct camera_mipi_info *ov5693_info = NULL;
1590 ov5693_info = v4l2_get_subdev_hostdata(sd);
1591 if (ov5693_info == NULL)
1594 mutex_lock(&dev->input_lock);
1595 idx = nearest_resolution_index(fmt->width, fmt->height);
1597 /* return the largest resolution */
1598 fmt->width = ov5693_res[N_RES - 1].width;
1599 fmt->height = ov5693_res[N_RES - 1].height;
1601 fmt->width = ov5693_res[idx].width;
1602 fmt->height = ov5693_res[idx].height;
1605 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1606 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1607 cfg->try_fmt = *fmt;
1608 mutex_unlock(&dev->input_lock);
1612 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1613 if (dev->fmt_idx == -1) {
1614 dev_err(&client->dev, "get resolution fail\n");
1615 mutex_unlock(&dev->input_lock);
1622 dev_err(&client->dev, "ov5693 startup err, retry to power up\n");
1623 for (i = 0; i < OV5693_POWER_UP_RETRY_NUM; i++) {
1624 dev_err(&client->dev,
1625 "ov5693 retry to power up %d/%d times, result: ",
1626 i+1, OV5693_POWER_UP_RETRY_NUM);
1630 mutex_unlock(&dev->input_lock);
1632 mutex_lock(&dev->input_lock);
1634 dev_err(&client->dev, "power up failed, continue\n");
1639 dev_err(&client->dev, " startup FAILED!\n");
1641 dev_err(&client->dev, " startup SUCCESS!\n");
1648 * After sensor settings are set to HW, sometimes stream is started.
1649 * This would cause ISP timeout because ISP is not ready to receive
1650 * data yet. So add stop streaming here.
1652 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1653 OV5693_STOP_STREAMING);
1655 dev_warn(&client->dev, "ov5693 stream off err\n");
1657 ret = ov5693_get_intg_factor(client, ov5693_info,
1658 &ov5693_res[dev->fmt_idx]);
1660 dev_err(&client->dev, "failed to get integration_factor\n");
1664 ov5693_info->metadata_width = fmt->width * 10 / 8;
1665 ov5693_info->metadata_height = 1;
1666 ov5693_info->metadata_effective_width = &ov5693_embedded_effective_size;
1669 mutex_unlock(&dev->input_lock);
1672 static int ov5693_get_fmt(struct v4l2_subdev *sd,
1673 struct v4l2_subdev_pad_config *cfg,
1674 struct v4l2_subdev_format *format)
1676 struct v4l2_mbus_framefmt *fmt = &format->format;
1677 struct ov5693_device *dev = to_ov5693_sensor(sd);
1684 fmt->width = ov5693_res[dev->fmt_idx].width;
1685 fmt->height = ov5693_res[dev->fmt_idx].height;
1686 fmt->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1691 static int ov5693_detect(struct i2c_client *client)
1693 struct i2c_adapter *adapter = client->adapter;
1699 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1702 ret = ov5693_read_reg(client, OV5693_8BIT,
1703 OV5693_SC_CMMN_CHIP_ID_H, &high);
1705 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
1708 ret = ov5693_read_reg(client, OV5693_8BIT,
1709 OV5693_SC_CMMN_CHIP_ID_L, &low);
1710 id = ((((u16) high) << 8) | (u16) low);
1712 if (id != OV5693_ID) {
1713 dev_err(&client->dev, "sensor ID error 0x%x\n", id);
1717 ret = ov5693_read_reg(client, OV5693_8BIT,
1718 OV5693_SC_CMMN_SUB_ID, &high);
1719 revision = (u8) high & 0x0f;
1721 dev_dbg(&client->dev, "sensor_revision = 0x%x\n", revision);
1722 dev_dbg(&client->dev, "detect ov5693 success\n");
1726 static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
1728 struct ov5693_device *dev = to_ov5693_sensor(sd);
1729 struct i2c_client *client = v4l2_get_subdevdata(sd);
1732 mutex_lock(&dev->input_lock);
1734 ret = ov5693_write_reg(client, OV5693_8BIT, OV5693_SW_STREAM,
1735 enable ? OV5693_START_STREAMING :
1736 OV5693_STOP_STREAMING);
1738 mutex_unlock(&dev->input_lock);
1744 static int ov5693_s_config(struct v4l2_subdev *sd,
1745 int irq, void *platform_data)
1747 struct ov5693_device *dev = to_ov5693_sensor(sd);
1748 struct i2c_client *client = v4l2_get_subdevdata(sd);
1751 if (platform_data == NULL)
1754 dev->platform_data =
1755 (struct camera_sensor_platform_data *)platform_data;
1757 mutex_lock(&dev->input_lock);
1758 /* power off the module, then power on it in future
1759 * as first power on by board may not fulfill the
1760 * power on sequqence needed by the module
1762 ret = power_down(sd);
1764 dev_err(&client->dev, "ov5693 power-off err.\n");
1765 goto fail_power_off;
1770 dev_err(&client->dev, "ov5693 power-up err.\n");
1775 dev->vcm = vcm_detect(client);
1777 ret = dev->platform_data->csi_cfg(sd, 1);
1781 /* config & detect sensor */
1782 ret = ov5693_detect(client);
1784 dev_err(&client->dev, "ov5693_detect err s_config.\n");
1788 dev->otp_data = ov5693_otp_read(sd);
1790 /* turn off sensor, after probed */
1791 ret = power_down(sd);
1793 dev_err(&client->dev, "ov5693 power-off err.\n");
1796 mutex_unlock(&dev->input_lock);
1801 dev->platform_data->csi_cfg(sd, 0);
1804 dev_err(&client->dev, "sensor power-gating failed\n");
1806 mutex_unlock(&dev->input_lock);
1810 static int ov5693_g_parm(struct v4l2_subdev *sd,
1811 struct v4l2_streamparm *param)
1813 struct ov5693_device *dev = to_ov5693_sensor(sd);
1814 struct i2c_client *client = v4l2_get_subdevdata(sd);
1819 if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1820 dev_err(&client->dev, "unsupported buffer type.\n");
1824 memset(param, 0, sizeof(*param));
1825 param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1827 if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
1828 param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1829 param->parm.capture.timeperframe.numerator = 1;
1830 param->parm.capture.capturemode = dev->run_mode;
1831 param->parm.capture.timeperframe.denominator =
1832 ov5693_res[dev->fmt_idx].fps;
1837 static int ov5693_s_parm(struct v4l2_subdev *sd,
1838 struct v4l2_streamparm *param)
1840 struct ov5693_device *dev = to_ov5693_sensor(sd);
1841 dev->run_mode = param->parm.capture.capturemode;
1843 mutex_lock(&dev->input_lock);
1844 switch (dev->run_mode) {
1846 ov5693_res = ov5693_res_video;
1847 N_RES = N_RES_VIDEO;
1849 case CI_MODE_STILL_CAPTURE:
1850 ov5693_res = ov5693_res_still;
1851 N_RES = N_RES_STILL;
1854 ov5693_res = ov5693_res_preview;
1855 N_RES = N_RES_PREVIEW;
1857 mutex_unlock(&dev->input_lock);
1861 static int ov5693_g_frame_interval(struct v4l2_subdev *sd,
1862 struct v4l2_subdev_frame_interval *interval)
1864 struct ov5693_device *dev = to_ov5693_sensor(sd);
1866 interval->interval.numerator = 1;
1867 interval->interval.denominator = ov5693_res[dev->fmt_idx].fps;
1872 static int ov5693_enum_mbus_code(struct v4l2_subdev *sd,
1873 struct v4l2_subdev_pad_config *cfg,
1874 struct v4l2_subdev_mbus_code_enum *code)
1876 if (code->index >= MAX_FMTS)
1879 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1883 static int ov5693_enum_frame_size(struct v4l2_subdev *sd,
1884 struct v4l2_subdev_pad_config *cfg,
1885 struct v4l2_subdev_frame_size_enum *fse)
1887 int index = fse->index;
1892 fse->min_width = ov5693_res[index].width;
1893 fse->min_height = ov5693_res[index].height;
1894 fse->max_width = ov5693_res[index].width;
1895 fse->max_height = ov5693_res[index].height;
1901 static const struct v4l2_subdev_video_ops ov5693_video_ops = {
1902 .s_stream = ov5693_s_stream,
1903 .g_parm = ov5693_g_parm,
1904 .s_parm = ov5693_s_parm,
1905 .g_frame_interval = ov5693_g_frame_interval,
1908 static const struct v4l2_subdev_core_ops ov5693_core_ops = {
1909 .s_power = ov5693_s_power,
1910 .ioctl = ov5693_ioctl,
1913 static const struct v4l2_subdev_pad_ops ov5693_pad_ops = {
1914 .enum_mbus_code = ov5693_enum_mbus_code,
1915 .enum_frame_size = ov5693_enum_frame_size,
1916 .get_fmt = ov5693_get_fmt,
1917 .set_fmt = ov5693_set_fmt,
1920 static const struct v4l2_subdev_ops ov5693_ops = {
1921 .core = &ov5693_core_ops,
1922 .video = &ov5693_video_ops,
1923 .pad = &ov5693_pad_ops,
1926 static int ov5693_remove(struct i2c_client *client)
1928 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1929 struct ov5693_device *dev = to_ov5693_sensor(sd);
1930 dev_dbg(&client->dev, "ov5693_remove...\n");
1932 dev->platform_data->csi_cfg(sd, 0);
1934 v4l2_device_unregister_subdev(sd);
1936 atomisp_gmin_remove_subdev(sd);
1938 media_entity_cleanup(&dev->sd.entity);
1939 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1945 static int ov5693_probe(struct i2c_client *client,
1946 const struct i2c_device_id *id)
1948 struct ov5693_device *dev;
1951 void *pdata = client->dev.platform_data;
1952 struct acpi_device *adev;
1955 /* Firmware workaround: Some modules use a "secondary default"
1956 * address of 0x10 which doesn't appear on schematics, and
1957 * some BIOS versions haven't gotten the memo. Work around
1959 i2c = gmin_get_var_int(&client->dev, "I2CAddr", -1);
1961 dev_info(&client->dev,
1962 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n",
1967 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1969 dev_err(&client->dev, "out of memory\n");
1973 mutex_init(&dev->input_lock);
1976 v4l2_i2c_subdev_init(&(dev->sd), client, &ov5693_ops);
1978 adev = ACPI_COMPANION(&client->dev);
1980 adev->power.flags.power_resources = 0;
1981 pdata = gmin_camera_platform_data(&dev->sd,
1982 ATOMISP_INPUT_FORMAT_RAW_10,
1983 atomisp_bayer_order_bggr);
1989 ret = ov5693_s_config(&dev->sd, client->irq, pdata);
1993 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1997 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1998 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1999 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
2000 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2002 v4l2_ctrl_handler_init(&dev->ctrl_handler,
2003 ARRAY_SIZE(ov5693_controls));
2005 ov5693_remove(client);
2009 for (i = 0; i < ARRAY_SIZE(ov5693_controls); i++)
2010 v4l2_ctrl_new_custom(&dev->ctrl_handler, &ov5693_controls[i],
2013 if (dev->ctrl_handler.error) {
2014 ov5693_remove(client);
2015 return dev->ctrl_handler.error;
2018 /* Use same lock for controls as for everything else. */
2019 dev->ctrl_handler.lock = &dev->input_lock;
2020 dev->sd.ctrl_handler = &dev->ctrl_handler;
2022 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
2024 ov5693_remove(client);
2028 v4l2_device_unregister_subdev(&dev->sd);
2033 MODULE_DEVICE_TABLE(i2c, ov5693_id);
2035 static const struct acpi_device_id ov5693_acpi_match[] = {
2039 MODULE_DEVICE_TABLE(acpi, ov5693_acpi_match);
2041 static struct i2c_driver ov5693_driver = {
2043 .name = OV5693_NAME,
2044 .acpi_match_table = ACPI_PTR(ov5693_acpi_match),
2046 .probe = ov5693_probe,
2047 .remove = ov5693_remove,
2048 .id_table = ov5693_id,
2051 static int init_ov5693(void)
2053 return i2c_add_driver(&ov5693_driver);
2056 static void exit_ov5693(void)
2059 i2c_del_driver(&ov5693_driver);
2062 module_init(init_ov5693);
2063 module_exit(exit_ov5693);
2065 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
2066 MODULE_LICENSE("GPL");