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
)
948 value
= min(value
, AD5823_MAX_FOCUS_POS
);
949 return ad5823_t_focus_vcm(sd
, value
);
952 static int ov5693_t_focus_abs(struct v4l2_subdev
*sd
, s32 value
)
954 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
955 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
958 dev_dbg(&client
->dev
, "%s: FOCUS_POS: 0x%x\n", __func__
, value
);
959 value
= clamp(value
, 0, OV5693_VCM_MAX_FOCUS_POS
);
960 if (dev
->vcm
== VCM_DW9714
) {
961 if (dev
->vcm_update
) {
962 ret
= vcm_dw_i2c_write(client
, VCM_PROTECTION_OFF
);
965 ret
= vcm_dw_i2c_write(client
, DIRECT_VCM
);
968 ret
= vcm_dw_i2c_write(client
, VCM_PROTECTION_ON
);
971 dev
->vcm_update
= false;
973 ret
= vcm_dw_i2c_write(client
,
974 vcm_val(value
, VCM_DEFAULT_S
));
975 } else if (dev
->vcm
== VCM_AD5823
) {
976 ad5823_t_focus_abs(sd
, value
);
979 dev
->number_of_steps
= value
- dev
->focus
;
981 getnstimeofday(&(dev
->timestamp_t_focus_abs
));
983 dev_err(&client
->dev
,
984 "%s: i2c failed. ret %d\n", __func__
, ret
);
989 static int ov5693_t_focus_rel(struct v4l2_subdev
*sd
, s32 value
)
991 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
992 return ov5693_t_focus_abs(sd
, dev
->focus
+ value
);
995 #define DELAY_PER_STEP_NS 1000000
996 #define DELAY_MAX_PER_STEP_NS (1000000 * 1023)
997 static int ov5693_q_focus_status(struct v4l2_subdev
*sd
, s32
*value
)
1000 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1001 struct timespec temptime
;
1002 const struct timespec timedelay
= {
1004 min((u32
)abs(dev
->number_of_steps
) * DELAY_PER_STEP_NS
,
1005 (u32
)DELAY_MAX_PER_STEP_NS
),
1008 getnstimeofday(&temptime
);
1009 temptime
= timespec_sub(temptime
, (dev
->timestamp_t_focus_abs
));
1010 if (timespec_compare(&temptime
, &timedelay
) <= 0) {
1011 status
|= ATOMISP_FOCUS_STATUS_MOVING
;
1012 status
|= ATOMISP_FOCUS_HP_IN_PROGRESS
;
1014 status
|= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE
;
1015 status
|= ATOMISP_FOCUS_HP_COMPLETE
;
1023 static int ov5693_q_focus_abs(struct v4l2_subdev
*sd
, s32
*value
)
1025 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1028 ov5693_q_focus_status(sd
, &val
);
1030 if (val
& ATOMISP_FOCUS_STATUS_MOVING
)
1031 *value
= dev
->focus
- dev
->number_of_steps
;
1033 *value
= dev
->focus
;
1038 static int ov5693_t_vcm_slew(struct v4l2_subdev
*sd
, s32 value
)
1040 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1041 dev
->number_of_steps
= value
;
1042 dev
->vcm_update
= true;
1046 static int ov5693_t_vcm_timing(struct v4l2_subdev
*sd
, s32 value
)
1048 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1049 dev
->number_of_steps
= value
;
1050 dev
->vcm_update
= true;
1054 static int ov5693_s_ctrl(struct v4l2_ctrl
*ctrl
)
1056 struct ov5693_device
*dev
=
1057 container_of(ctrl
->handler
, struct ov5693_device
, ctrl_handler
);
1058 struct i2c_client
*client
= v4l2_get_subdevdata(&dev
->sd
);
1062 case V4L2_CID_FOCUS_ABSOLUTE
:
1063 dev_dbg(&client
->dev
, "%s: CID_FOCUS_ABSOLUTE:%d.\n",
1064 __func__
, ctrl
->val
);
1065 ret
= ov5693_t_focus_abs(&dev
->sd
, ctrl
->val
);
1067 case V4L2_CID_FOCUS_RELATIVE
:
1068 dev_dbg(&client
->dev
, "%s: CID_FOCUS_RELATIVE:%d.\n",
1069 __func__
, ctrl
->val
);
1070 ret
= ov5693_t_focus_rel(&dev
->sd
, ctrl
->val
);
1072 case V4L2_CID_VCM_SLEW
:
1073 ret
= ov5693_t_vcm_slew(&dev
->sd
, ctrl
->val
);
1075 case V4L2_CID_VCM_TIMEING
:
1076 ret
= ov5693_t_vcm_timing(&dev
->sd
, ctrl
->val
);
1084 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
1086 struct ov5693_device
*dev
=
1087 container_of(ctrl
->handler
, struct ov5693_device
, ctrl_handler
);
1091 case V4L2_CID_EXPOSURE_ABSOLUTE
:
1092 ret
= ov5693_q_exposure(&dev
->sd
, &ctrl
->val
);
1094 case V4L2_CID_FOCAL_ABSOLUTE
:
1095 ret
= ov5693_g_focal(&dev
->sd
, &ctrl
->val
);
1097 case V4L2_CID_FNUMBER_ABSOLUTE
:
1098 ret
= ov5693_g_fnumber(&dev
->sd
, &ctrl
->val
);
1100 case V4L2_CID_FNUMBER_RANGE
:
1101 ret
= ov5693_g_fnumber_range(&dev
->sd
, &ctrl
->val
);
1103 case V4L2_CID_FOCUS_ABSOLUTE
:
1104 ret
= ov5693_q_focus_abs(&dev
->sd
, &ctrl
->val
);
1106 case V4L2_CID_FOCUS_STATUS
:
1107 ret
= ov5693_q_focus_status(&dev
->sd
, &ctrl
->val
);
1109 case V4L2_CID_BIN_FACTOR_HORZ
:
1110 ret
= ov5693_g_bin_factor_x(&dev
->sd
, &ctrl
->val
);
1112 case V4L2_CID_BIN_FACTOR_VERT
:
1113 ret
= ov5693_g_bin_factor_y(&dev
->sd
, &ctrl
->val
);
1122 static const struct v4l2_ctrl_ops ctrl_ops
= {
1123 .s_ctrl
= ov5693_s_ctrl
,
1124 .g_volatile_ctrl
= ov5693_g_volatile_ctrl
1127 struct v4l2_ctrl_config ov5693_controls
[] = {
1130 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
1131 .type
= V4L2_CTRL_TYPE_INTEGER
,
1141 .id
= V4L2_CID_FOCAL_ABSOLUTE
,
1142 .type
= V4L2_CTRL_TYPE_INTEGER
,
1143 .name
= "focal length",
1144 .min
= OV5693_FOCAL_LENGTH_DEFAULT
,
1145 .max
= OV5693_FOCAL_LENGTH_DEFAULT
,
1147 .def
= OV5693_FOCAL_LENGTH_DEFAULT
,
1152 .id
= V4L2_CID_FNUMBER_ABSOLUTE
,
1153 .type
= V4L2_CTRL_TYPE_INTEGER
,
1155 .min
= OV5693_F_NUMBER_DEFAULT
,
1156 .max
= OV5693_F_NUMBER_DEFAULT
,
1158 .def
= OV5693_F_NUMBER_DEFAULT
,
1163 .id
= V4L2_CID_FNUMBER_RANGE
,
1164 .type
= V4L2_CTRL_TYPE_INTEGER
,
1165 .name
= "f-number range",
1166 .min
= OV5693_F_NUMBER_RANGE
,
1167 .max
= OV5693_F_NUMBER_RANGE
,
1169 .def
= OV5693_F_NUMBER_RANGE
,
1174 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
1175 .type
= V4L2_CTRL_TYPE_INTEGER
,
1176 .name
= "focus move absolute",
1178 .max
= OV5693_VCM_MAX_FOCUS_POS
,
1185 .id
= V4L2_CID_FOCUS_RELATIVE
,
1186 .type
= V4L2_CTRL_TYPE_INTEGER
,
1187 .name
= "focus move relative",
1188 .min
= OV5693_VCM_MAX_FOCUS_NEG
,
1189 .max
= OV5693_VCM_MAX_FOCUS_POS
,
1196 .id
= V4L2_CID_FOCUS_STATUS
,
1197 .type
= V4L2_CTRL_TYPE_INTEGER
,
1198 .name
= "focus status",
1200 .max
= 100, /* allow enum to grow in the future */
1207 .id
= V4L2_CID_VCM_SLEW
,
1208 .type
= V4L2_CTRL_TYPE_INTEGER
,
1211 .max
= OV5693_VCM_SLEW_STEP_MAX
,
1218 .id
= V4L2_CID_VCM_TIMEING
,
1219 .type
= V4L2_CTRL_TYPE_INTEGER
,
1220 .name
= "vcm step time",
1222 .max
= OV5693_VCM_SLEW_TIME_MAX
,
1229 .id
= V4L2_CID_BIN_FACTOR_HORZ
,
1230 .type
= V4L2_CTRL_TYPE_INTEGER
,
1231 .name
= "horizontal binning factor",
1233 .max
= OV5693_BIN_FACTOR_MAX
,
1240 .id
= V4L2_CID_BIN_FACTOR_VERT
,
1241 .type
= V4L2_CTRL_TYPE_INTEGER
,
1242 .name
= "vertical binning factor",
1244 .max
= OV5693_BIN_FACTOR_MAX
,
1251 static int ov5693_init(struct v4l2_subdev
*sd
)
1253 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1254 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1257 pr_info("%s\n", __func__
);
1258 mutex_lock(&dev
->input_lock
);
1259 dev
->vcm_update
= false;
1261 if (dev
->vcm
== VCM_AD5823
) {
1262 ret
= vcm_ad_i2c_wr8(client
, 0x01, 0x01); /* vcm init test */
1264 dev_err(&client
->dev
,
1265 "vcm reset failed\n");
1267 ret
= ad5823_i2c_write(client
, AD5823_REG_VCM_CODE_MSB
,
1268 AD5823_RING_CTRL_ENABLE
);
1270 dev_err(&client
->dev
,
1271 "vcm enable ringing failed\n");
1272 ret
= ad5823_i2c_write(client
, AD5823_REG_MODE
,
1275 dev_err(&client
->dev
,
1276 "vcm change mode failed\n");
1279 /*change initial focus value for ad5823*/
1280 if (dev
->vcm
== VCM_AD5823
) {
1281 dev
->focus
= AD5823_INIT_FOCUS_POS
;
1282 ov5693_t_focus_abs(sd
, AD5823_INIT_FOCUS_POS
);
1285 ov5693_t_focus_abs(sd
, 0);
1288 mutex_unlock(&dev
->input_lock
);
1293 static int power_ctrl(struct v4l2_subdev
*sd
, bool flag
)
1296 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1298 if (!dev
|| !dev
->platform_data
)
1301 /* Non-gmin platforms use the legacy callback */
1302 if (dev
->platform_data
->power_ctrl
)
1303 return dev
->platform_data
->power_ctrl(sd
, flag
);
1305 /* This driver assumes "internal DVDD, PWDNB tied to DOVDD".
1306 * In this set up only gpio0 (XSHUTDN) should be available
1307 * but in some products (for example ECS) gpio1 (PWDNB) is
1308 * also available. If gpio1 is available we emulate it being
1309 * tied to DOVDD here. */
1311 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 1);
1312 dev
->platform_data
->gpio1_ctrl(sd
, 1);
1314 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 1);
1316 dev
->platform_data
->gpio1_ctrl(sd
, 0);
1317 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 0);
1321 dev
->platform_data
->gpio1_ctrl(sd
, 0);
1322 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 0);
1323 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 0);
1329 static int gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
1331 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1333 if (!dev
|| !dev
->platform_data
)
1336 /* Non-gmin platforms use the legacy callback */
1337 if (dev
->platform_data
->gpio_ctrl
)
1338 return dev
->platform_data
->gpio_ctrl(sd
, flag
);
1340 return dev
->platform_data
->gpio0_ctrl(sd
, flag
);
1343 static int __power_up(struct v4l2_subdev
*sd
)
1345 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1346 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1349 if (NULL
== dev
->platform_data
) {
1350 dev_err(&client
->dev
,
1351 "no camera_sensor_platform_data");
1356 ret
= power_ctrl(sd
, 1);
1360 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
1361 /* add this delay time to 10~11ms*/
1362 usleep_range(10000, 11000);
1365 ret
= gpio_ctrl(sd
, 1);
1367 ret
= gpio_ctrl(sd
, 1);
1372 /* flis clock control */
1373 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
1377 __cci_delay(up_delay
);
1385 dev_err(&client
->dev
, "sensor power-up failed\n");
1390 static int power_down(struct v4l2_subdev
*sd
)
1392 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1393 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1396 dev
->focus
= OV5693_INVALID_CONFIG
;
1397 if (NULL
== dev
->platform_data
) {
1398 dev_err(&client
->dev
,
1399 "no camera_sensor_platform_data");
1403 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 0);
1405 dev_err(&client
->dev
, "flisclk failed\n");
1408 ret
= gpio_ctrl(sd
, 0);
1410 ret
= gpio_ctrl(sd
, 0);
1412 dev_err(&client
->dev
, "gpio failed 2\n");
1416 ret
= power_ctrl(sd
, 0);
1418 dev_err(&client
->dev
, "vprog failed.\n");
1423 static int power_up(struct v4l2_subdev
*sd
)
1425 static const int retry_count
= 4;
1428 for (i
= 0; i
< retry_count
; i
++) {
1429 ret
= __power_up(sd
);
1438 static int ov5693_s_power(struct v4l2_subdev
*sd
, int on
)
1442 pr_info("%s: on %d\n", __func__
, on
);
1444 return power_down(sd
);
1448 ret
= ov5693_init(sd
);
1449 /* restore settings */
1450 ov5693_res
= ov5693_res_preview
;
1451 N_RES
= N_RES_PREVIEW
;
1458 * distance - calculate the distance
1463 * Get the gap between res_w/res_h and w/h.
1464 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1465 * res->width/height smaller than w/h wouldn't be considered.
1466 * The gap of ratio larger than 1/8 wouldn't be considered.
1467 * Returns the value of gap or -1 if fail.
1469 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1470 static int distance(struct ov5693_resolution
*res
, u32 w
, u32 h
)
1475 if (w
== 0 || h
== 0 ||
1476 res
->width
< w
|| res
->height
< h
)
1479 ratio
= res
->width
<< 13;
1482 ratio
/= res
->height
;
1484 distance
= abs(ratio
- 8192);
1486 if (distance
> LARGEST_ALLOWED_RATIO_MISMATCH
)
1492 /* Return the nearest higher resolution index
1493 * Firstly try to find the approximate aspect ratio resolution
1494 * If we find multiple same AR resolutions, choose the
1497 static int nearest_resolution_index(int w
, int h
)
1502 int min_dist
= INT_MAX
;
1503 int min_res_w
= INT_MAX
;
1504 struct ov5693_resolution
*tmp_res
= NULL
;
1506 for (i
= 0; i
< N_RES
; i
++) {
1507 tmp_res
= &ov5693_res
[i
];
1508 dist
= distance(tmp_res
, w
, h
);
1511 if (dist
< min_dist
) {
1514 min_res_w
= ov5693_res
[i
].width
;
1517 if (dist
== min_dist
&& ov5693_res
[i
].width
< min_res_w
)
1524 static int get_resolution_index(int w
, int h
)
1528 for (i
= 0; i
< N_RES
; i
++) {
1529 if (w
!= ov5693_res
[i
].width
)
1531 if (h
!= ov5693_res
[i
].height
)
1540 /* TODO: remove it. */
1541 static int startup(struct v4l2_subdev
*sd
)
1543 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1544 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1547 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
1548 OV5693_SW_RESET
, 0x01);
1550 dev_err(&client
->dev
, "ov5693 reset err.\n");
1554 ret
= ov5693_write_reg_array(client
, ov5693_global_setting
);
1556 dev_err(&client
->dev
, "ov5693 write register err.\n");
1560 ret
= ov5693_write_reg_array(client
, ov5693_res
[dev
->fmt_idx
].regs
);
1562 dev_err(&client
->dev
, "ov5693 write register err.\n");
1569 static int ov5693_set_fmt(struct v4l2_subdev
*sd
,
1570 struct v4l2_subdev_pad_config
*cfg
,
1571 struct v4l2_subdev_format
*format
)
1573 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1574 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1575 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1576 struct camera_mipi_info
*ov5693_info
= NULL
;
1583 ov5693_info
= v4l2_get_subdev_hostdata(sd
);
1584 if (ov5693_info
== NULL
)
1587 mutex_lock(&dev
->input_lock
);
1588 idx
= nearest_resolution_index(fmt
->width
, fmt
->height
);
1590 /* return the largest resolution */
1591 fmt
->width
= ov5693_res
[N_RES
- 1].width
;
1592 fmt
->height
= ov5693_res
[N_RES
- 1].height
;
1594 fmt
->width
= ov5693_res
[idx
].width
;
1595 fmt
->height
= ov5693_res
[idx
].height
;
1598 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1599 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1600 cfg
->try_fmt
= *fmt
;
1601 mutex_unlock(&dev
->input_lock
);
1605 dev
->fmt_idx
= get_resolution_index(fmt
->width
, fmt
->height
);
1606 if (dev
->fmt_idx
== -1) {
1607 dev_err(&client
->dev
, "get resolution fail\n");
1608 mutex_unlock(&dev
->input_lock
);
1615 dev_err(&client
->dev
, "ov5693 startup err, retry to power up\n");
1616 for (i
= 0; i
< OV5693_POWER_UP_RETRY_NUM
; i
++) {
1617 dev_err(&client
->dev
,
1618 "ov5693 retry to power up %d/%d times, result: ",
1619 i
+1, OV5693_POWER_UP_RETRY_NUM
);
1623 mutex_unlock(&dev
->input_lock
);
1625 mutex_lock(&dev
->input_lock
);
1627 dev_err(&client
->dev
, "power up failed, continue\n");
1632 dev_err(&client
->dev
, " startup FAILED!\n");
1634 dev_err(&client
->dev
, " startup SUCCESS!\n");
1641 * After sensor settings are set to HW, sometimes stream is started.
1642 * This would cause ISP timeout because ISP is not ready to receive
1643 * data yet. So add stop streaming here.
1645 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_SW_STREAM
,
1646 OV5693_STOP_STREAMING
);
1648 dev_warn(&client
->dev
, "ov5693 stream off err\n");
1650 ret
= ov5693_get_intg_factor(client
, ov5693_info
,
1651 &ov5693_res
[dev
->fmt_idx
]);
1653 dev_err(&client
->dev
, "failed to get integration_factor\n");
1657 ov5693_info
->metadata_width
= fmt
->width
* 10 / 8;
1658 ov5693_info
->metadata_height
= 1;
1659 ov5693_info
->metadata_effective_width
= &ov5693_embedded_effective_size
;
1662 mutex_unlock(&dev
->input_lock
);
1665 static int ov5693_get_fmt(struct v4l2_subdev
*sd
,
1666 struct v4l2_subdev_pad_config
*cfg
,
1667 struct v4l2_subdev_format
*format
)
1669 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1670 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1677 fmt
->width
= ov5693_res
[dev
->fmt_idx
].width
;
1678 fmt
->height
= ov5693_res
[dev
->fmt_idx
].height
;
1679 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1684 static int ov5693_detect(struct i2c_client
*client
)
1686 struct i2c_adapter
*adapter
= client
->adapter
;
1692 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
1695 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1696 OV5693_SC_CMMN_CHIP_ID_H
, &high
);
1698 dev_err(&client
->dev
, "sensor_id_high = 0x%x\n", high
);
1701 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1702 OV5693_SC_CMMN_CHIP_ID_L
, &low
);
1703 id
= ((((u16
) high
) << 8) | (u16
) low
);
1705 if (id
!= OV5693_ID
) {
1706 dev_err(&client
->dev
, "sensor ID error 0x%x\n", id
);
1710 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1711 OV5693_SC_CMMN_SUB_ID
, &high
);
1712 revision
= (u8
) high
& 0x0f;
1714 dev_dbg(&client
->dev
, "sensor_revision = 0x%x\n", revision
);
1715 dev_dbg(&client
->dev
, "detect ov5693 success\n");
1719 static int ov5693_s_stream(struct v4l2_subdev
*sd
, int enable
)
1721 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1722 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1725 mutex_lock(&dev
->input_lock
);
1727 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_SW_STREAM
,
1728 enable
? OV5693_START_STREAMING
:
1729 OV5693_STOP_STREAMING
);
1731 mutex_unlock(&dev
->input_lock
);
1737 static int ov5693_s_config(struct v4l2_subdev
*sd
,
1738 int irq
, void *platform_data
)
1740 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1741 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1744 if (platform_data
== NULL
)
1747 dev
->platform_data
=
1748 (struct camera_sensor_platform_data
*)platform_data
;
1750 mutex_lock(&dev
->input_lock
);
1751 /* power off the module, then power on it in future
1752 * as first power on by board may not fulfill the
1753 * power on sequqence needed by the module
1755 ret
= power_down(sd
);
1757 dev_err(&client
->dev
, "ov5693 power-off err.\n");
1758 goto fail_power_off
;
1763 dev_err(&client
->dev
, "ov5693 power-up err.\n");
1768 dev
->vcm
= vcm_detect(client
);
1770 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
1774 /* config & detect sensor */
1775 ret
= ov5693_detect(client
);
1777 dev_err(&client
->dev
, "ov5693_detect err s_config.\n");
1781 dev
->otp_data
= ov5693_otp_read(sd
);
1783 /* turn off sensor, after probed */
1784 ret
= power_down(sd
);
1786 dev_err(&client
->dev
, "ov5693 power-off err.\n");
1789 mutex_unlock(&dev
->input_lock
);
1794 dev
->platform_data
->csi_cfg(sd
, 0);
1797 dev_err(&client
->dev
, "sensor power-gating failed\n");
1799 mutex_unlock(&dev
->input_lock
);
1803 static int ov5693_g_parm(struct v4l2_subdev
*sd
,
1804 struct v4l2_streamparm
*param
)
1806 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1807 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1812 if (param
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1813 dev_err(&client
->dev
, "unsupported buffer type.\n");
1817 memset(param
, 0, sizeof(*param
));
1818 param
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1820 if (dev
->fmt_idx
>= 0 && dev
->fmt_idx
< N_RES
) {
1821 param
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1822 param
->parm
.capture
.timeperframe
.numerator
= 1;
1823 param
->parm
.capture
.capturemode
= dev
->run_mode
;
1824 param
->parm
.capture
.timeperframe
.denominator
=
1825 ov5693_res
[dev
->fmt_idx
].fps
;
1830 static int ov5693_s_parm(struct v4l2_subdev
*sd
,
1831 struct v4l2_streamparm
*param
)
1833 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1834 dev
->run_mode
= param
->parm
.capture
.capturemode
;
1836 mutex_lock(&dev
->input_lock
);
1837 switch (dev
->run_mode
) {
1839 ov5693_res
= ov5693_res_video
;
1840 N_RES
= N_RES_VIDEO
;
1842 case CI_MODE_STILL_CAPTURE
:
1843 ov5693_res
= ov5693_res_still
;
1844 N_RES
= N_RES_STILL
;
1847 ov5693_res
= ov5693_res_preview
;
1848 N_RES
= N_RES_PREVIEW
;
1850 mutex_unlock(&dev
->input_lock
);
1854 static int ov5693_g_frame_interval(struct v4l2_subdev
*sd
,
1855 struct v4l2_subdev_frame_interval
*interval
)
1857 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1859 interval
->interval
.numerator
= 1;
1860 interval
->interval
.denominator
= ov5693_res
[dev
->fmt_idx
].fps
;
1865 static int ov5693_enum_mbus_code(struct v4l2_subdev
*sd
,
1866 struct v4l2_subdev_pad_config
*cfg
,
1867 struct v4l2_subdev_mbus_code_enum
*code
)
1869 if (code
->index
>= MAX_FMTS
)
1872 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1876 static int ov5693_enum_frame_size(struct v4l2_subdev
*sd
,
1877 struct v4l2_subdev_pad_config
*cfg
,
1878 struct v4l2_subdev_frame_size_enum
*fse
)
1880 int index
= fse
->index
;
1885 fse
->min_width
= ov5693_res
[index
].width
;
1886 fse
->min_height
= ov5693_res
[index
].height
;
1887 fse
->max_width
= ov5693_res
[index
].width
;
1888 fse
->max_height
= ov5693_res
[index
].height
;
1894 static const struct v4l2_subdev_video_ops ov5693_video_ops
= {
1895 .s_stream
= ov5693_s_stream
,
1896 .g_parm
= ov5693_g_parm
,
1897 .s_parm
= ov5693_s_parm
,
1898 .g_frame_interval
= ov5693_g_frame_interval
,
1901 static const struct v4l2_subdev_core_ops ov5693_core_ops
= {
1902 .s_power
= ov5693_s_power
,
1903 .ioctl
= ov5693_ioctl
,
1906 static const struct v4l2_subdev_pad_ops ov5693_pad_ops
= {
1907 .enum_mbus_code
= ov5693_enum_mbus_code
,
1908 .enum_frame_size
= ov5693_enum_frame_size
,
1909 .get_fmt
= ov5693_get_fmt
,
1910 .set_fmt
= ov5693_set_fmt
,
1913 static const struct v4l2_subdev_ops ov5693_ops
= {
1914 .core
= &ov5693_core_ops
,
1915 .video
= &ov5693_video_ops
,
1916 .pad
= &ov5693_pad_ops
,
1919 static int ov5693_remove(struct i2c_client
*client
)
1921 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1922 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1923 dev_dbg(&client
->dev
, "ov5693_remove...\n");
1925 dev
->platform_data
->csi_cfg(sd
, 0);
1927 v4l2_device_unregister_subdev(sd
);
1929 atomisp_gmin_remove_subdev(sd
);
1931 media_entity_cleanup(&dev
->sd
.entity
);
1932 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1938 static int ov5693_probe(struct i2c_client
*client
,
1939 const struct i2c_device_id
*id
)
1941 struct ov5693_device
*dev
;
1944 void *pdata
= client
->dev
.platform_data
;
1945 struct acpi_device
*adev
;
1948 /* Firmware workaround: Some modules use a "secondary default"
1949 * address of 0x10 which doesn't appear on schematics, and
1950 * some BIOS versions haven't gotten the memo. Work around
1952 i2c
= gmin_get_var_int(&client
->dev
, "I2CAddr", -1);
1954 dev_info(&client
->dev
,
1955 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n",
1960 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1964 mutex_init(&dev
->input_lock
);
1967 v4l2_i2c_subdev_init(&(dev
->sd
), client
, &ov5693_ops
);
1969 adev
= ACPI_COMPANION(&client
->dev
);
1971 adev
->power
.flags
.power_resources
= 0;
1972 pdata
= gmin_camera_platform_data(&dev
->sd
,
1973 ATOMISP_INPUT_FORMAT_RAW_10
,
1974 atomisp_bayer_order_bggr
);
1980 ret
= ov5693_s_config(&dev
->sd
, client
->irq
, pdata
);
1984 ret
= atomisp_register_i2c_module(&dev
->sd
, pdata
, RAW_CAMERA
);
1988 dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1989 dev
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1990 dev
->format
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1991 dev
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1993 v4l2_ctrl_handler_init(&dev
->ctrl_handler
,
1994 ARRAY_SIZE(ov5693_controls
));
1996 ov5693_remove(client
);
2000 for (i
= 0; i
< ARRAY_SIZE(ov5693_controls
); i
++)
2001 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &ov5693_controls
[i
],
2004 if (dev
->ctrl_handler
.error
) {
2005 ov5693_remove(client
);
2006 return dev
->ctrl_handler
.error
;
2009 /* Use same lock for controls as for everything else. */
2010 dev
->ctrl_handler
.lock
= &dev
->input_lock
;
2011 dev
->sd
.ctrl_handler
= &dev
->ctrl_handler
;
2013 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
2015 ov5693_remove(client
);
2019 v4l2_device_unregister_subdev(&dev
->sd
);
2024 MODULE_DEVICE_TABLE(i2c
, ov5693_id
);
2026 static const struct acpi_device_id ov5693_acpi_match
[] = {
2030 MODULE_DEVICE_TABLE(acpi
, ov5693_acpi_match
);
2032 static struct i2c_driver ov5693_driver
= {
2034 .name
= OV5693_NAME
,
2035 .acpi_match_table
= ACPI_PTR(ov5693_acpi_match
),
2037 .probe
= ov5693_probe
,
2038 .remove
= ov5693_remove
,
2039 .id_table
= ov5693_id
,
2041 module_i2c_driver(ov5693_driver
);
2043 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
2044 MODULE_LICENSE("GPL");