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/moduleparam.h>
35 #include <media/v4l2-device.h>
37 #include <linux/acpi.h>
38 #include "../../include/linux/atomisp_gmin_platform.h"
43 #define __cci_delay(t) \
46 usleep_range((t) * 1000, ((t) + 1) * 1000); \
52 /* Value 30ms reached through experimentation on byt ecs.
53 * The DS specifies a much lower value but when using a smaller value
54 * the I2C bus sometimes locks up permanently when starting the camera.
55 * This issue could not be reproduced on cht, so we can reduce the
56 * delay value to a lower value when insmod.
58 static uint up_delay
= 30;
59 module_param(up_delay
, uint
, 0644);
60 MODULE_PARM_DESC(up_delay
, "Delay prior to the first CCI transaction for ov5693");
62 static int vcm_ad_i2c_wr8(struct i2c_client
*client
, u8 reg
, u8 val
)
76 err
= i2c_transfer(client
->adapter
, &msg
, 1);
78 dev_err(&client
->dev
, "%s: vcm i2c fail, err code = %d\n",
85 static int ad5823_i2c_write(struct i2c_client
*client
, u8 reg
, u8 val
)
91 msg
.addr
= AD5823_VCM_ADDR
;
96 if (i2c_transfer(client
->adapter
, &msg
, 1) != 1)
101 static int ad5823_i2c_read(struct i2c_client
*client
, u8 reg
, u8
*val
)
103 struct i2c_msg msg
[2];
108 msg
[0].addr
= AD5823_VCM_ADDR
;
111 msg
[0].buf
= &buf
[0];
114 msg
[1].flags
= I2C_M_RD
;
116 msg
[1].buf
= &buf
[1];
118 if (i2c_transfer(client
->adapter
, msg
, 2) != 2)
125 static const uint32_t ov5693_embedded_effective_size
= 28;
127 /* i2c read/write stuff */
128 static int ov5693_read_reg(struct i2c_client
*client
,
129 u16 data_length
, u16 reg
, u16
*val
)
132 struct i2c_msg msg
[2];
133 unsigned char data
[6];
135 if (!client
->adapter
) {
136 dev_err(&client
->dev
, "%s error, no client->adapter\n",
141 if (data_length
!= OV5693_8BIT
&& data_length
!= OV5693_16BIT
142 && data_length
!= OV5693_32BIT
) {
143 dev_err(&client
->dev
, "%s error, invalid data length\n",
148 memset(msg
, 0, sizeof(msg
));
150 msg
[0].addr
= client
->addr
;
152 msg
[0].len
= I2C_MSG_LENGTH
;
155 /* high byte goes out first */
156 data
[0] = (u8
)(reg
>> 8);
157 data
[1] = (u8
)(reg
& 0xff);
159 msg
[1].addr
= client
->addr
;
160 msg
[1].len
= data_length
;
161 msg
[1].flags
= I2C_M_RD
;
164 err
= i2c_transfer(client
->adapter
, msg
, 2);
168 dev_err(&client
->dev
,
169 "read from offset 0x%x error %d", reg
, err
);
174 /* high byte comes first */
175 if (data_length
== OV5693_8BIT
)
177 else if (data_length
== OV5693_16BIT
)
178 *val
= be16_to_cpu(*(u16
*)&data
[0]);
180 *val
= be32_to_cpu(*(u32
*)&data
[0]);
185 static int ov5693_i2c_write(struct i2c_client
*client
, u16 len
, u8
*data
)
188 const int num_msg
= 1;
191 msg
.addr
= client
->addr
;
195 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
197 return ret
== num_msg
? 0 : -EIO
;
200 static int vcm_dw_i2c_write(struct i2c_client
*client
, u16 data
)
203 const int num_msg
= 1;
207 val
= cpu_to_be16(data
);
210 msg
.len
= OV5693_16BIT
;
211 msg
.buf
= (u8
*)&val
;
213 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
215 return ret
== num_msg
? 0 : -EIO
;
218 /* Theory: per datasheet, the two VCMs both allow for a 2-byte read.
219 * The DW9714 doesn't actually specify what this does (it has a
220 * two-byte write-only protocol, but specifies the read sequence as
221 * legal), but it returns the same data (zeroes) always, after an
222 * undocumented initial NAK. The AD5823 has a one-byte address
223 * register to which all writes go, and subsequent reads will cycle
224 * through the 8 bytes of registers. Notably, the default values (the
225 * device is always power-cycled affirmatively, so we can rely on
226 * these) in AD5823 are not pairwise repetitions of the same 16 bit
227 * word. So all we have to do is sequentially read two bytes at a
228 * time and see if we detect a difference in any of the first four
230 static int vcm_detect(struct i2c_client
*client
)
235 for (i
= 0; i
< 4; i
++) {
237 msg
.flags
= I2C_M_RD
;
238 msg
.len
= sizeof(data
);
239 msg
.buf
= (u8
*)&data
;
240 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
242 /* DW9714 always fails the first read and returns
243 * zeroes for subsequent ones */
244 if (i
== 0 && ret
== -EREMOTEIO
) {
255 return ret
== 1 ? VCM_DW9714
: ret
;
258 static int ov5693_write_reg(struct i2c_client
*client
, u16 data_length
,
262 unsigned char data
[4] = {0};
263 u16
*wreg
= (u16
*)data
;
264 const u16 len
= data_length
+ sizeof(u16
); /* 16-bit address + data */
266 if (data_length
!= OV5693_8BIT
&& data_length
!= OV5693_16BIT
) {
267 dev_err(&client
->dev
,
268 "%s error, invalid data_length\n", __func__
);
272 /* high byte goes out first */
273 *wreg
= cpu_to_be16(reg
);
275 if (data_length
== OV5693_8BIT
) {
279 u16
*wdata
= (u16
*)&data
[2];
280 *wdata
= cpu_to_be16(val
);
283 ret
= ov5693_i2c_write(client
, len
, data
);
285 dev_err(&client
->dev
,
286 "write error: wrote 0x%x to offset 0x%x error %d",
293 * ov5693_write_reg_array - Initializes a list of OV5693 registers
294 * @client: i2c driver client structure
295 * @reglist: list of registers to be written
297 * This function initializes a list of registers. When consecutive addresses
298 * are found in a row on the list, this function creates a buffer and sends
299 * consecutive data in a single i2c_transfer().
301 * __ov5693_flush_reg_array, __ov5693_buf_reg_array() and
302 * __ov5693_write_reg_is_consecutive() are internal functions to
303 * ov5693_write_reg_array_fast() and should be not used anywhere else.
307 static int __ov5693_flush_reg_array(struct i2c_client
*client
,
308 struct ov5693_write_ctrl
*ctrl
)
312 if (ctrl
->index
== 0)
315 size
= sizeof(u16
) + ctrl
->index
; /* 16-bit address + data */
316 ctrl
->buffer
.addr
= cpu_to_be16(ctrl
->buffer
.addr
);
319 return ov5693_i2c_write(client
, size
, (u8
*)&ctrl
->buffer
);
322 static int __ov5693_buf_reg_array(struct i2c_client
*client
,
323 struct ov5693_write_ctrl
*ctrl
,
324 const struct ov5693_reg
*next
)
329 switch (next
->type
) {
332 ctrl
->buffer
.data
[ctrl
->index
] = (u8
)next
->val
;
336 data16
= (u16
*)&ctrl
->buffer
.data
[ctrl
->index
];
337 *data16
= cpu_to_be16((u16
)next
->val
);
343 /* When first item is added, we need to store its starting address */
344 if (ctrl
->index
== 0)
345 ctrl
->buffer
.addr
= next
->reg
;
350 * Buffer cannot guarantee free space for u32? Better flush it to avoid
351 * possible lack of memory for next item.
353 if (ctrl
->index
+ sizeof(u16
) >= OV5693_MAX_WRITE_BUF_SIZE
)
354 return __ov5693_flush_reg_array(client
, ctrl
);
359 static int __ov5693_write_reg_is_consecutive(struct i2c_client
*client
,
360 struct ov5693_write_ctrl
*ctrl
,
361 const struct ov5693_reg
*next
)
363 if (ctrl
->index
== 0)
366 return ctrl
->buffer
.addr
+ ctrl
->index
== next
->reg
;
369 static int ov5693_write_reg_array(struct i2c_client
*client
,
370 const struct ov5693_reg
*reglist
)
372 const struct ov5693_reg
*next
= reglist
;
373 struct ov5693_write_ctrl ctrl
;
377 for (; next
->type
!= OV5693_TOK_TERM
; next
++) {
378 switch (next
->type
& OV5693_TOK_MASK
) {
379 case OV5693_TOK_DELAY
:
380 err
= __ov5693_flush_reg_array(client
, &ctrl
);
387 * If next address is not consecutive, data needs to be
388 * flushed before proceed.
390 if (!__ov5693_write_reg_is_consecutive(client
, &ctrl
,
392 err
= __ov5693_flush_reg_array(client
, &ctrl
);
396 err
= __ov5693_buf_reg_array(client
, &ctrl
, next
);
398 dev_err(&client
->dev
,
399 "%s: write error, aborted\n",
407 return __ov5693_flush_reg_array(client
, &ctrl
);
409 static int ov5693_g_focal(struct v4l2_subdev
*sd
, s32
*val
)
411 *val
= (OV5693_FOCAL_LENGTH_NUM
<< 16) | OV5693_FOCAL_LENGTH_DEM
;
415 static int ov5693_g_fnumber(struct v4l2_subdev
*sd
, s32
*val
)
417 /*const f number for imx*/
418 *val
= (OV5693_F_NUMBER_DEFAULT_NUM
<< 16) | OV5693_F_NUMBER_DEM
;
422 static int ov5693_g_fnumber_range(struct v4l2_subdev
*sd
, s32
*val
)
424 *val
= (OV5693_F_NUMBER_DEFAULT_NUM
<< 24) |
425 (OV5693_F_NUMBER_DEM
<< 16) |
426 (OV5693_F_NUMBER_DEFAULT_NUM
<< 8) | OV5693_F_NUMBER_DEM
;
430 static int ov5693_g_bin_factor_x(struct v4l2_subdev
*sd
, s32
*val
)
432 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
434 *val
= ov5693_res
[dev
->fmt_idx
].bin_factor_x
;
439 static int ov5693_g_bin_factor_y(struct v4l2_subdev
*sd
, s32
*val
)
441 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
443 *val
= ov5693_res
[dev
->fmt_idx
].bin_factor_y
;
448 static int ov5693_get_intg_factor(struct i2c_client
*client
,
449 struct camera_mipi_info
*info
,
450 const struct ov5693_resolution
*res
)
452 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
453 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
454 struct atomisp_sensor_mode_data
*buf
= &info
->data
;
455 unsigned int pix_clk_freq_hz
;
463 pix_clk_freq_hz
= res
->pix_clk_freq
* 1000000;
465 dev
->vt_pix_clk_freq_mhz
= pix_clk_freq_hz
;
466 buf
->vt_pix_clk_freq_mhz
= pix_clk_freq_hz
;
468 /* get integration time */
469 buf
->coarse_integration_time_min
= OV5693_COARSE_INTG_TIME_MIN
;
470 buf
->coarse_integration_time_max_margin
=
471 OV5693_COARSE_INTG_TIME_MAX_MARGIN
;
473 buf
->fine_integration_time_min
= OV5693_FINE_INTG_TIME_MIN
;
474 buf
->fine_integration_time_max_margin
=
475 OV5693_FINE_INTG_TIME_MAX_MARGIN
;
477 buf
->fine_integration_time_def
= OV5693_FINE_INTG_TIME_MIN
;
478 buf
->frame_length_lines
= res
->lines_per_frame
;
479 buf
->line_length_pck
= res
->pixels_per_line
;
480 buf
->read_mode
= res
->bin_mode
;
482 /* get the cropping and output resolution to ISP for this mode. */
483 ret
= ov5693_read_reg(client
, OV5693_16BIT
,
484 OV5693_HORIZONTAL_START_H
, ®_val
);
487 buf
->crop_horizontal_start
= reg_val
;
489 ret
= ov5693_read_reg(client
, OV5693_16BIT
,
490 OV5693_VERTICAL_START_H
, ®_val
);
493 buf
->crop_vertical_start
= reg_val
;
495 ret
= ov5693_read_reg(client
, OV5693_16BIT
,
496 OV5693_HORIZONTAL_END_H
, ®_val
);
499 buf
->crop_horizontal_end
= reg_val
;
501 ret
= ov5693_read_reg(client
, OV5693_16BIT
,
502 OV5693_VERTICAL_END_H
, ®_val
);
505 buf
->crop_vertical_end
= reg_val
;
507 ret
= ov5693_read_reg(client
, OV5693_16BIT
,
508 OV5693_HORIZONTAL_OUTPUT_SIZE_H
, ®_val
);
511 buf
->output_width
= reg_val
;
513 ret
= ov5693_read_reg(client
, OV5693_16BIT
,
514 OV5693_VERTICAL_OUTPUT_SIZE_H
, ®_val
);
517 buf
->output_height
= reg_val
;
519 buf
->binning_factor_x
= res
->bin_factor_x
?
520 res
->bin_factor_x
: 1;
521 buf
->binning_factor_y
= res
->bin_factor_y
?
522 res
->bin_factor_y
: 1;
526 static long __ov5693_set_exposure(struct v4l2_subdev
*sd
, int coarse_itg
,
527 int gain
, int digitgain
)
530 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
531 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
535 hts
= ov5693_res
[dev
->fmt_idx
].pixels_per_line
;
536 vts
= ov5693_res
[dev
->fmt_idx
].lines_per_frame
;
537 /*If coarse_itg is larger than 1<<15, can not write to reg directly.
538 The way is to write coarse_itg/2 to the reg, meanwhile write 2*hts
540 if (coarse_itg
> (1 << 15)) {
542 coarse_itg
= (int)coarse_itg
/ 2;
545 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
546 OV5693_GROUP_ACCESS
, 0x00);
548 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
549 __func__
, OV5693_GROUP_ACCESS
);
553 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
554 OV5693_TIMING_HTS_H
, (hts
>> 8) & 0xFF);
556 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
557 __func__
, OV5693_TIMING_HTS_H
);
561 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
562 OV5693_TIMING_HTS_L
, hts
& 0xFF);
564 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
565 __func__
, OV5693_TIMING_HTS_L
);
568 /* Increase the VTS to match exposure + MARGIN */
569 if (coarse_itg
> vts
- OV5693_INTEGRATION_TIME_MARGIN
)
570 vts
= (u16
) coarse_itg
+ OV5693_INTEGRATION_TIME_MARGIN
;
572 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
573 OV5693_TIMING_VTS_H
, (vts
>> 8) & 0xFF);
575 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
576 __func__
, OV5693_TIMING_VTS_H
);
580 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
581 OV5693_TIMING_VTS_L
, vts
& 0xFF);
583 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
584 __func__
, OV5693_TIMING_VTS_L
);
590 /* Lower four bit should be 0*/
591 exp_val
= coarse_itg
<< 4;
592 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
593 OV5693_EXPOSURE_L
, exp_val
& 0xFF);
595 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
596 __func__
, OV5693_EXPOSURE_L
);
600 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
601 OV5693_EXPOSURE_M
, (exp_val
>> 8) & 0xFF);
603 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
604 __func__
, OV5693_EXPOSURE_M
);
608 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
609 OV5693_EXPOSURE_H
, (exp_val
>> 16) & 0x0F);
611 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
612 __func__
, OV5693_EXPOSURE_H
);
617 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
618 OV5693_AGC_L
, gain
& 0xff);
620 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
621 __func__
, OV5693_AGC_L
);
625 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
626 OV5693_AGC_H
, (gain
>> 8) & 0xff);
628 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
629 __func__
, OV5693_AGC_H
);
635 ret
= ov5693_write_reg(client
, OV5693_16BIT
,
636 OV5693_MWB_RED_GAIN_H
, digitgain
);
638 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
639 __func__
, OV5693_MWB_RED_GAIN_H
);
643 ret
= ov5693_write_reg(client
, OV5693_16BIT
,
644 OV5693_MWB_GREEN_GAIN_H
, digitgain
);
646 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
647 __func__
, OV5693_MWB_RED_GAIN_H
);
651 ret
= ov5693_write_reg(client
, OV5693_16BIT
,
652 OV5693_MWB_BLUE_GAIN_H
, digitgain
);
654 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
655 __func__
, OV5693_MWB_RED_GAIN_H
);
661 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
662 OV5693_GROUP_ACCESS
, 0x10);
666 /* Delay launch group */
667 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
668 OV5693_GROUP_ACCESS
, 0xa0);
674 static int ov5693_set_exposure(struct v4l2_subdev
*sd
, int exposure
,
675 int gain
, int digitgain
)
677 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
680 mutex_lock(&dev
->input_lock
);
681 ret
= __ov5693_set_exposure(sd
, exposure
, gain
, digitgain
);
682 mutex_unlock(&dev
->input_lock
);
687 static long ov5693_s_exposure(struct v4l2_subdev
*sd
,
688 struct atomisp_exposure
*exposure
)
690 u16 coarse_itg
= exposure
->integration_time
[0];
691 u16 analog_gain
= exposure
->gain
[0];
692 u16 digital_gain
= exposure
->gain
[1];
694 /* we should not accept the invalid value below */
695 if (analog_gain
== 0) {
696 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
697 v4l2_err(client
, "%s: invalid value\n", __func__
);
700 return ov5693_set_exposure(sd
, coarse_itg
, analog_gain
, digital_gain
);
703 static int ov5693_read_otp_reg_array(struct i2c_client
*client
, u16 size
,
710 for (index
= 0; index
<= size
; index
++) {
711 pVal
= (u16
*) (buf
+ index
);
713 ov5693_read_reg(client
, OV5693_8BIT
, addr
+ index
,
722 static int __ov5693_otp_read(struct v4l2_subdev
*sd
, u8
*buf
)
724 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
725 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
730 for (i
= 1; i
< OV5693_OTP_BANK_MAX
; i
++) {
731 /*set bank NO and OTP read mode. */
732 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_OTP_BANK_REG
, (i
| 0xc0)); //[7:6] 2'b11 [5:0] bank no
734 dev_err(&client
->dev
, "failed to prepare OTP page\n");
737 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_BANK_REG,(i|0xc0));
740 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_OTP_READ_REG
, OV5693_OTP_MODE_READ
); // enable :1
742 dev_err(&client
->dev
,
743 "failed to set OTP reading mode page");
746 //pr_debug("write 0x%x->0x%x\n",OV5693_OTP_READ_REG,OV5693_OTP_MODE_READ);
748 /* Reading the OTP data array */
749 ret
= ov5693_read_otp_reg_array(client
, OV5693_OTP_BANK_SIZE
,
750 OV5693_OTP_START_ADDR
,
753 dev_err(&client
->dev
, "failed to read OTP data\n");
757 //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));
759 //Intel OTP map, try to read 320byts first.
768 } else if (24 == i
) { //if the first 320bytes data doesn't not exist, try to read the next 32bytes data.
776 } else if (27 == i
) { //if the prvious 32bytes data doesn't exist, try to read the next 32bytes data again.
781 dev
->otp_size
= 0; // no OTP data.
786 b
= b
+ OV5693_OTP_BANK_SIZE
;
792 * Read otp data and store it into a kmalloced buffer.
793 * The caller must kfree the buffer when no more needed.
794 * @size: set to the size of the returned otp data.
796 static void *ov5693_otp_read(struct v4l2_subdev
*sd
)
798 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
802 buf
= devm_kzalloc(&client
->dev
, (OV5693_OTP_DATA_SIZE
+ 16), GFP_KERNEL
);
804 return ERR_PTR(-ENOMEM
);
806 //otp valid after mipi on and sw stream on
807 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_FRAME_OFF_NUM
, 0x00);
809 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
810 OV5693_SW_STREAM
, OV5693_START_STREAMING
);
812 ret
= __ov5693_otp_read(sd
, buf
);
814 //mipi off and sw stream off after otp read
815 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_FRAME_OFF_NUM
, 0x0f);
817 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
818 OV5693_SW_STREAM
, OV5693_STOP_STREAMING
);
820 /* Driver has failed to find valid data */
822 dev_err(&client
->dev
, "sensor found no valid OTP data\n");
829 static int ov5693_g_priv_int_data(struct v4l2_subdev
*sd
,
830 struct v4l2_private_int_data
*priv
)
832 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
833 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
834 u8 __user
*to
= priv
->data
;
835 u32 read_size
= priv
->size
;
838 /* No need to copy data if size is 0 */
842 if (IS_ERR(dev
->otp_data
)) {
843 dev_err(&client
->dev
, "OTP data not available");
844 return PTR_ERR(dev
->otp_data
);
847 /* Correct read_size value only if bigger than maximum */
848 if (read_size
> OV5693_OTP_DATA_SIZE
)
849 read_size
= OV5693_OTP_DATA_SIZE
;
851 ret
= copy_to_user(to
, dev
->otp_data
, read_size
);
853 dev_err(&client
->dev
, "%s: failed to copy OTP data to user\n",
858 pr_debug("%s read_size:%d\n", __func__
, read_size
);
861 /* Return correct size */
862 priv
->size
= dev
->otp_size
;
868 static long ov5693_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
872 case ATOMISP_IOC_S_EXPOSURE
:
873 return ov5693_s_exposure(sd
, arg
);
874 case ATOMISP_IOC_G_SENSOR_PRIV_INT_DATA
:
875 return ov5693_g_priv_int_data(sd
, arg
);
882 /* This returns the exposure time being used. This should only be used
883 for filling in EXIF data, not for actual image processing. */
884 static int ov5693_q_exposure(struct v4l2_subdev
*sd
, s32
*value
)
886 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
891 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
897 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
903 reg_v
+= reg_v2
<< 8;
904 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
910 *value
= reg_v
+ (((u32
)reg_v2
<< 16));
915 static int ad5823_t_focus_vcm(struct v4l2_subdev
*sd
, u16 val
)
917 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
921 ret
= ad5823_i2c_read(client
, AD5823_REG_VCM_CODE_MSB
, &vcm_code
);
925 /* set reg VCM_CODE_MSB Bit[1:0] */
926 vcm_code
= (vcm_code
& VCM_CODE_MSB_MASK
) |
927 ((val
>> 8) & ~VCM_CODE_MSB_MASK
);
928 ret
= ad5823_i2c_write(client
, AD5823_REG_VCM_CODE_MSB
, vcm_code
);
932 /* set reg VCM_CODE_LSB Bit[7:0] */
933 ret
= ad5823_i2c_write(client
, AD5823_REG_VCM_CODE_LSB
, (val
& 0xff));
937 /* set required vcm move time */
938 vcm_code
= AD5823_RESONANCE_PERIOD
/ AD5823_RESONANCE_COEF
939 - AD5823_HIGH_FREQ_RANGE
;
940 ret
= ad5823_i2c_write(client
, AD5823_REG_VCM_MOVE_TIME
, vcm_code
);
945 int ad5823_t_focus_abs(struct v4l2_subdev
*sd
, s32 value
)
947 value
= min(value
, AD5823_MAX_FOCUS_POS
);
948 return ad5823_t_focus_vcm(sd
, value
);
951 static int ov5693_t_focus_abs(struct v4l2_subdev
*sd
, s32 value
)
953 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
954 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
957 dev_dbg(&client
->dev
, "%s: FOCUS_POS: 0x%x\n", __func__
, value
);
958 value
= clamp(value
, 0, OV5693_VCM_MAX_FOCUS_POS
);
959 if (dev
->vcm
== VCM_DW9714
) {
960 if (dev
->vcm_update
) {
961 ret
= vcm_dw_i2c_write(client
, VCM_PROTECTION_OFF
);
964 ret
= vcm_dw_i2c_write(client
, DIRECT_VCM
);
967 ret
= vcm_dw_i2c_write(client
, VCM_PROTECTION_ON
);
970 dev
->vcm_update
= false;
972 ret
= vcm_dw_i2c_write(client
,
973 vcm_val(value
, VCM_DEFAULT_S
));
974 } else if (dev
->vcm
== VCM_AD5823
) {
975 ad5823_t_focus_abs(sd
, value
);
978 dev
->number_of_steps
= value
- dev
->focus
;
980 getnstimeofday(&(dev
->timestamp_t_focus_abs
));
982 dev_err(&client
->dev
,
983 "%s: i2c failed. ret %d\n", __func__
, ret
);
988 static int ov5693_t_focus_rel(struct v4l2_subdev
*sd
, s32 value
)
990 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
991 return ov5693_t_focus_abs(sd
, dev
->focus
+ value
);
994 #define DELAY_PER_STEP_NS 1000000
995 #define DELAY_MAX_PER_STEP_NS (1000000 * 1023)
996 static int ov5693_q_focus_status(struct v4l2_subdev
*sd
, s32
*value
)
999 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1000 struct timespec temptime
;
1001 const struct timespec timedelay
= {
1003 min((u32
)abs(dev
->number_of_steps
) * DELAY_PER_STEP_NS
,
1004 (u32
)DELAY_MAX_PER_STEP_NS
),
1007 getnstimeofday(&temptime
);
1008 temptime
= timespec_sub(temptime
, (dev
->timestamp_t_focus_abs
));
1009 if (timespec_compare(&temptime
, &timedelay
) <= 0) {
1010 status
|= ATOMISP_FOCUS_STATUS_MOVING
;
1011 status
|= ATOMISP_FOCUS_HP_IN_PROGRESS
;
1013 status
|= ATOMISP_FOCUS_STATUS_ACCEPTS_NEW_MOVE
;
1014 status
|= ATOMISP_FOCUS_HP_COMPLETE
;
1022 static int ov5693_q_focus_abs(struct v4l2_subdev
*sd
, s32
*value
)
1024 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1027 ov5693_q_focus_status(sd
, &val
);
1029 if (val
& ATOMISP_FOCUS_STATUS_MOVING
)
1030 *value
= dev
->focus
- dev
->number_of_steps
;
1032 *value
= dev
->focus
;
1037 static int ov5693_t_vcm_slew(struct v4l2_subdev
*sd
, s32 value
)
1039 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1040 dev
->number_of_steps
= value
;
1041 dev
->vcm_update
= true;
1045 static int ov5693_t_vcm_timing(struct v4l2_subdev
*sd
, s32 value
)
1047 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1048 dev
->number_of_steps
= value
;
1049 dev
->vcm_update
= true;
1053 static int ov5693_s_ctrl(struct v4l2_ctrl
*ctrl
)
1055 struct ov5693_device
*dev
=
1056 container_of(ctrl
->handler
, struct ov5693_device
, ctrl_handler
);
1057 struct i2c_client
*client
= v4l2_get_subdevdata(&dev
->sd
);
1061 case V4L2_CID_FOCUS_ABSOLUTE
:
1062 dev_dbg(&client
->dev
, "%s: CID_FOCUS_ABSOLUTE:%d.\n",
1063 __func__
, ctrl
->val
);
1064 ret
= ov5693_t_focus_abs(&dev
->sd
, ctrl
->val
);
1066 case V4L2_CID_FOCUS_RELATIVE
:
1067 dev_dbg(&client
->dev
, "%s: CID_FOCUS_RELATIVE:%d.\n",
1068 __func__
, ctrl
->val
);
1069 ret
= ov5693_t_focus_rel(&dev
->sd
, ctrl
->val
);
1071 case V4L2_CID_VCM_SLEW
:
1072 ret
= ov5693_t_vcm_slew(&dev
->sd
, ctrl
->val
);
1074 case V4L2_CID_VCM_TIMEING
:
1075 ret
= ov5693_t_vcm_timing(&dev
->sd
, ctrl
->val
);
1083 static int ov5693_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
1085 struct ov5693_device
*dev
=
1086 container_of(ctrl
->handler
, struct ov5693_device
, ctrl_handler
);
1090 case V4L2_CID_EXPOSURE_ABSOLUTE
:
1091 ret
= ov5693_q_exposure(&dev
->sd
, &ctrl
->val
);
1093 case V4L2_CID_FOCAL_ABSOLUTE
:
1094 ret
= ov5693_g_focal(&dev
->sd
, &ctrl
->val
);
1096 case V4L2_CID_FNUMBER_ABSOLUTE
:
1097 ret
= ov5693_g_fnumber(&dev
->sd
, &ctrl
->val
);
1099 case V4L2_CID_FNUMBER_RANGE
:
1100 ret
= ov5693_g_fnumber_range(&dev
->sd
, &ctrl
->val
);
1102 case V4L2_CID_FOCUS_ABSOLUTE
:
1103 ret
= ov5693_q_focus_abs(&dev
->sd
, &ctrl
->val
);
1105 case V4L2_CID_FOCUS_STATUS
:
1106 ret
= ov5693_q_focus_status(&dev
->sd
, &ctrl
->val
);
1108 case V4L2_CID_BIN_FACTOR_HORZ
:
1109 ret
= ov5693_g_bin_factor_x(&dev
->sd
, &ctrl
->val
);
1111 case V4L2_CID_BIN_FACTOR_VERT
:
1112 ret
= ov5693_g_bin_factor_y(&dev
->sd
, &ctrl
->val
);
1121 static const struct v4l2_ctrl_ops ctrl_ops
= {
1122 .s_ctrl
= ov5693_s_ctrl
,
1123 .g_volatile_ctrl
= ov5693_g_volatile_ctrl
1126 struct v4l2_ctrl_config ov5693_controls
[] = {
1129 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
1130 .type
= V4L2_CTRL_TYPE_INTEGER
,
1140 .id
= V4L2_CID_FOCAL_ABSOLUTE
,
1141 .type
= V4L2_CTRL_TYPE_INTEGER
,
1142 .name
= "focal length",
1143 .min
= OV5693_FOCAL_LENGTH_DEFAULT
,
1144 .max
= OV5693_FOCAL_LENGTH_DEFAULT
,
1146 .def
= OV5693_FOCAL_LENGTH_DEFAULT
,
1151 .id
= V4L2_CID_FNUMBER_ABSOLUTE
,
1152 .type
= V4L2_CTRL_TYPE_INTEGER
,
1154 .min
= OV5693_F_NUMBER_DEFAULT
,
1155 .max
= OV5693_F_NUMBER_DEFAULT
,
1157 .def
= OV5693_F_NUMBER_DEFAULT
,
1162 .id
= V4L2_CID_FNUMBER_RANGE
,
1163 .type
= V4L2_CTRL_TYPE_INTEGER
,
1164 .name
= "f-number range",
1165 .min
= OV5693_F_NUMBER_RANGE
,
1166 .max
= OV5693_F_NUMBER_RANGE
,
1168 .def
= OV5693_F_NUMBER_RANGE
,
1173 .id
= V4L2_CID_FOCUS_ABSOLUTE
,
1174 .type
= V4L2_CTRL_TYPE_INTEGER
,
1175 .name
= "focus move absolute",
1177 .max
= OV5693_VCM_MAX_FOCUS_POS
,
1184 .id
= V4L2_CID_FOCUS_RELATIVE
,
1185 .type
= V4L2_CTRL_TYPE_INTEGER
,
1186 .name
= "focus move relative",
1187 .min
= OV5693_VCM_MAX_FOCUS_NEG
,
1188 .max
= OV5693_VCM_MAX_FOCUS_POS
,
1195 .id
= V4L2_CID_FOCUS_STATUS
,
1196 .type
= V4L2_CTRL_TYPE_INTEGER
,
1197 .name
= "focus status",
1199 .max
= 100, /* allow enum to grow in the future */
1206 .id
= V4L2_CID_VCM_SLEW
,
1207 .type
= V4L2_CTRL_TYPE_INTEGER
,
1210 .max
= OV5693_VCM_SLEW_STEP_MAX
,
1217 .id
= V4L2_CID_VCM_TIMEING
,
1218 .type
= V4L2_CTRL_TYPE_INTEGER
,
1219 .name
= "vcm step time",
1221 .max
= OV5693_VCM_SLEW_TIME_MAX
,
1228 .id
= V4L2_CID_BIN_FACTOR_HORZ
,
1229 .type
= V4L2_CTRL_TYPE_INTEGER
,
1230 .name
= "horizontal binning factor",
1232 .max
= OV5693_BIN_FACTOR_MAX
,
1239 .id
= V4L2_CID_BIN_FACTOR_VERT
,
1240 .type
= V4L2_CTRL_TYPE_INTEGER
,
1241 .name
= "vertical binning factor",
1243 .max
= OV5693_BIN_FACTOR_MAX
,
1250 static int ov5693_init(struct v4l2_subdev
*sd
)
1252 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1253 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1256 pr_info("%s\n", __func__
);
1257 mutex_lock(&dev
->input_lock
);
1258 dev
->vcm_update
= false;
1260 if (dev
->vcm
== VCM_AD5823
) {
1261 ret
= vcm_ad_i2c_wr8(client
, 0x01, 0x01); /* vcm init test */
1263 dev_err(&client
->dev
,
1264 "vcm reset failed\n");
1266 ret
= ad5823_i2c_write(client
, AD5823_REG_VCM_CODE_MSB
,
1267 AD5823_RING_CTRL_ENABLE
);
1269 dev_err(&client
->dev
,
1270 "vcm enable ringing failed\n");
1271 ret
= ad5823_i2c_write(client
, AD5823_REG_MODE
,
1274 dev_err(&client
->dev
,
1275 "vcm change mode failed\n");
1278 /*change initial focus value for ad5823*/
1279 if (dev
->vcm
== VCM_AD5823
) {
1280 dev
->focus
= AD5823_INIT_FOCUS_POS
;
1281 ov5693_t_focus_abs(sd
, AD5823_INIT_FOCUS_POS
);
1284 ov5693_t_focus_abs(sd
, 0);
1287 mutex_unlock(&dev
->input_lock
);
1292 static int power_ctrl(struct v4l2_subdev
*sd
, bool flag
)
1295 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1297 if (!dev
|| !dev
->platform_data
)
1300 /* This driver assumes "internal DVDD, PWDNB tied to DOVDD".
1301 * In this set up only gpio0 (XSHUTDN) should be available
1302 * but in some products (for example ECS) gpio1 (PWDNB) is
1303 * also available. If gpio1 is available we emulate it being
1304 * tied to DOVDD here. */
1306 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 1);
1307 dev
->platform_data
->gpio1_ctrl(sd
, 1);
1309 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 1);
1311 dev
->platform_data
->gpio1_ctrl(sd
, 0);
1312 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 0);
1316 dev
->platform_data
->gpio1_ctrl(sd
, 0);
1317 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 0);
1318 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 0);
1324 static int gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
1326 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1328 if (!dev
|| !dev
->platform_data
)
1331 return dev
->platform_data
->gpio0_ctrl(sd
, flag
);
1334 static int __power_up(struct v4l2_subdev
*sd
)
1336 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1337 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1340 if (NULL
== dev
->platform_data
) {
1341 dev_err(&client
->dev
,
1342 "no camera_sensor_platform_data");
1347 ret
= power_ctrl(sd
, 1);
1351 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
1352 /* add this delay time to 10~11ms*/
1353 usleep_range(10000, 11000);
1356 ret
= gpio_ctrl(sd
, 1);
1358 ret
= gpio_ctrl(sd
, 1);
1363 /* flis clock control */
1364 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
1368 __cci_delay(up_delay
);
1376 dev_err(&client
->dev
, "sensor power-up failed\n");
1381 static int power_down(struct v4l2_subdev
*sd
)
1383 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1384 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1387 dev
->focus
= OV5693_INVALID_CONFIG
;
1388 if (NULL
== dev
->platform_data
) {
1389 dev_err(&client
->dev
,
1390 "no camera_sensor_platform_data");
1394 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 0);
1396 dev_err(&client
->dev
, "flisclk failed\n");
1399 ret
= gpio_ctrl(sd
, 0);
1401 ret
= gpio_ctrl(sd
, 0);
1403 dev_err(&client
->dev
, "gpio failed 2\n");
1407 ret
= power_ctrl(sd
, 0);
1409 dev_err(&client
->dev
, "vprog failed.\n");
1414 static int power_up(struct v4l2_subdev
*sd
)
1416 static const int retry_count
= 4;
1419 for (i
= 0; i
< retry_count
; i
++) {
1420 ret
= __power_up(sd
);
1429 static int ov5693_s_power(struct v4l2_subdev
*sd
, int on
)
1433 pr_info("%s: on %d\n", __func__
, on
);
1435 return power_down(sd
);
1439 ret
= ov5693_init(sd
);
1440 /* restore settings */
1441 ov5693_res
= ov5693_res_preview
;
1442 N_RES
= N_RES_PREVIEW
;
1449 * distance - calculate the distance
1454 * Get the gap between res_w/res_h and w/h.
1455 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1456 * res->width/height smaller than w/h wouldn't be considered.
1457 * The gap of ratio larger than 1/8 wouldn't be considered.
1458 * Returns the value of gap or -1 if fail.
1460 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1461 static int distance(struct ov5693_resolution
*res
, u32 w
, u32 h
)
1466 if (w
== 0 || h
== 0 ||
1467 res
->width
< w
|| res
->height
< h
)
1470 ratio
= res
->width
<< 13;
1473 ratio
/= res
->height
;
1475 distance
= abs(ratio
- 8192);
1477 if (distance
> LARGEST_ALLOWED_RATIO_MISMATCH
)
1483 /* Return the nearest higher resolution index
1484 * Firstly try to find the approximate aspect ratio resolution
1485 * If we find multiple same AR resolutions, choose the
1488 static int nearest_resolution_index(int w
, int h
)
1493 int min_dist
= INT_MAX
;
1494 int min_res_w
= INT_MAX
;
1495 struct ov5693_resolution
*tmp_res
= NULL
;
1497 for (i
= 0; i
< N_RES
; i
++) {
1498 tmp_res
= &ov5693_res
[i
];
1499 dist
= distance(tmp_res
, w
, h
);
1502 if (dist
< min_dist
) {
1505 min_res_w
= ov5693_res
[i
].width
;
1508 if (dist
== min_dist
&& ov5693_res
[i
].width
< min_res_w
)
1515 static int get_resolution_index(int w
, int h
)
1519 for (i
= 0; i
< N_RES
; i
++) {
1520 if (w
!= ov5693_res
[i
].width
)
1522 if (h
!= ov5693_res
[i
].height
)
1531 /* TODO: remove it. */
1532 static int startup(struct v4l2_subdev
*sd
)
1534 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1535 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1538 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
1539 OV5693_SW_RESET
, 0x01);
1541 dev_err(&client
->dev
, "ov5693 reset err.\n");
1545 ret
= ov5693_write_reg_array(client
, ov5693_global_setting
);
1547 dev_err(&client
->dev
, "ov5693 write register err.\n");
1551 ret
= ov5693_write_reg_array(client
, ov5693_res
[dev
->fmt_idx
].regs
);
1553 dev_err(&client
->dev
, "ov5693 write register err.\n");
1560 static int ov5693_set_fmt(struct v4l2_subdev
*sd
,
1561 struct v4l2_subdev_pad_config
*cfg
,
1562 struct v4l2_subdev_format
*format
)
1564 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1565 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1566 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1567 struct camera_mipi_info
*ov5693_info
= NULL
;
1574 ov5693_info
= v4l2_get_subdev_hostdata(sd
);
1575 if (ov5693_info
== NULL
)
1578 mutex_lock(&dev
->input_lock
);
1579 idx
= nearest_resolution_index(fmt
->width
, fmt
->height
);
1581 /* return the largest resolution */
1582 fmt
->width
= ov5693_res
[N_RES
- 1].width
;
1583 fmt
->height
= ov5693_res
[N_RES
- 1].height
;
1585 fmt
->width
= ov5693_res
[idx
].width
;
1586 fmt
->height
= ov5693_res
[idx
].height
;
1589 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1590 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1591 cfg
->try_fmt
= *fmt
;
1592 mutex_unlock(&dev
->input_lock
);
1596 dev
->fmt_idx
= get_resolution_index(fmt
->width
, fmt
->height
);
1597 if (dev
->fmt_idx
== -1) {
1598 dev_err(&client
->dev
, "get resolution fail\n");
1599 mutex_unlock(&dev
->input_lock
);
1606 dev_err(&client
->dev
, "ov5693 startup err, retry to power up\n");
1607 for (i
= 0; i
< OV5693_POWER_UP_RETRY_NUM
; i
++) {
1608 dev_err(&client
->dev
,
1609 "ov5693 retry to power up %d/%d times, result: ",
1610 i
+1, OV5693_POWER_UP_RETRY_NUM
);
1614 mutex_unlock(&dev
->input_lock
);
1616 mutex_lock(&dev
->input_lock
);
1618 dev_err(&client
->dev
, "power up failed, continue\n");
1623 dev_err(&client
->dev
, " startup FAILED!\n");
1625 dev_err(&client
->dev
, " startup SUCCESS!\n");
1632 * After sensor settings are set to HW, sometimes stream is started.
1633 * This would cause ISP timeout because ISP is not ready to receive
1634 * data yet. So add stop streaming here.
1636 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_SW_STREAM
,
1637 OV5693_STOP_STREAMING
);
1639 dev_warn(&client
->dev
, "ov5693 stream off err\n");
1641 ret
= ov5693_get_intg_factor(client
, ov5693_info
,
1642 &ov5693_res
[dev
->fmt_idx
]);
1644 dev_err(&client
->dev
, "failed to get integration_factor\n");
1648 ov5693_info
->metadata_width
= fmt
->width
* 10 / 8;
1649 ov5693_info
->metadata_height
= 1;
1650 ov5693_info
->metadata_effective_width
= &ov5693_embedded_effective_size
;
1653 mutex_unlock(&dev
->input_lock
);
1656 static int ov5693_get_fmt(struct v4l2_subdev
*sd
,
1657 struct v4l2_subdev_pad_config
*cfg
,
1658 struct v4l2_subdev_format
*format
)
1660 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1661 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1668 fmt
->width
= ov5693_res
[dev
->fmt_idx
].width
;
1669 fmt
->height
= ov5693_res
[dev
->fmt_idx
].height
;
1670 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1675 static int ov5693_detect(struct i2c_client
*client
)
1677 struct i2c_adapter
*adapter
= client
->adapter
;
1683 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
1686 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1687 OV5693_SC_CMMN_CHIP_ID_H
, &high
);
1689 dev_err(&client
->dev
, "sensor_id_high = 0x%x\n", high
);
1692 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1693 OV5693_SC_CMMN_CHIP_ID_L
, &low
);
1694 id
= ((((u16
) high
) << 8) | (u16
) low
);
1696 if (id
!= OV5693_ID
) {
1697 dev_err(&client
->dev
, "sensor ID error 0x%x\n", id
);
1701 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1702 OV5693_SC_CMMN_SUB_ID
, &high
);
1703 revision
= (u8
) high
& 0x0f;
1705 dev_dbg(&client
->dev
, "sensor_revision = 0x%x\n", revision
);
1706 dev_dbg(&client
->dev
, "detect ov5693 success\n");
1710 static int ov5693_s_stream(struct v4l2_subdev
*sd
, int enable
)
1712 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1713 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1716 mutex_lock(&dev
->input_lock
);
1718 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_SW_STREAM
,
1719 enable
? OV5693_START_STREAMING
:
1720 OV5693_STOP_STREAMING
);
1722 mutex_unlock(&dev
->input_lock
);
1728 static int ov5693_s_config(struct v4l2_subdev
*sd
,
1729 int irq
, void *platform_data
)
1731 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1732 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1735 if (platform_data
== NULL
)
1738 dev
->platform_data
=
1739 (struct camera_sensor_platform_data
*)platform_data
;
1741 mutex_lock(&dev
->input_lock
);
1742 /* power off the module, then power on it in future
1743 * as first power on by board may not fulfill the
1744 * power on sequqence needed by the module
1746 ret
= power_down(sd
);
1748 dev_err(&client
->dev
, "ov5693 power-off err.\n");
1749 goto fail_power_off
;
1754 dev_err(&client
->dev
, "ov5693 power-up err.\n");
1759 dev
->vcm
= vcm_detect(client
);
1761 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
1765 /* config & detect sensor */
1766 ret
= ov5693_detect(client
);
1768 dev_err(&client
->dev
, "ov5693_detect err s_config.\n");
1772 dev
->otp_data
= ov5693_otp_read(sd
);
1774 /* turn off sensor, after probed */
1775 ret
= power_down(sd
);
1777 dev_err(&client
->dev
, "ov5693 power-off err.\n");
1780 mutex_unlock(&dev
->input_lock
);
1785 dev
->platform_data
->csi_cfg(sd
, 0);
1788 dev_err(&client
->dev
, "sensor power-gating failed\n");
1790 mutex_unlock(&dev
->input_lock
);
1794 static int ov5693_g_parm(struct v4l2_subdev
*sd
,
1795 struct v4l2_streamparm
*param
)
1797 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1798 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1803 if (param
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1804 dev_err(&client
->dev
, "unsupported buffer type.\n");
1808 memset(param
, 0, sizeof(*param
));
1809 param
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1811 if (dev
->fmt_idx
>= 0 && dev
->fmt_idx
< N_RES
) {
1812 param
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1813 param
->parm
.capture
.timeperframe
.numerator
= 1;
1814 param
->parm
.capture
.capturemode
= dev
->run_mode
;
1815 param
->parm
.capture
.timeperframe
.denominator
=
1816 ov5693_res
[dev
->fmt_idx
].fps
;
1821 static int ov5693_s_parm(struct v4l2_subdev
*sd
,
1822 struct v4l2_streamparm
*param
)
1824 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1825 dev
->run_mode
= param
->parm
.capture
.capturemode
;
1827 mutex_lock(&dev
->input_lock
);
1828 switch (dev
->run_mode
) {
1830 ov5693_res
= ov5693_res_video
;
1831 N_RES
= N_RES_VIDEO
;
1833 case CI_MODE_STILL_CAPTURE
:
1834 ov5693_res
= ov5693_res_still
;
1835 N_RES
= N_RES_STILL
;
1838 ov5693_res
= ov5693_res_preview
;
1839 N_RES
= N_RES_PREVIEW
;
1841 mutex_unlock(&dev
->input_lock
);
1845 static int ov5693_g_frame_interval(struct v4l2_subdev
*sd
,
1846 struct v4l2_subdev_frame_interval
*interval
)
1848 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1850 interval
->interval
.numerator
= 1;
1851 interval
->interval
.denominator
= ov5693_res
[dev
->fmt_idx
].fps
;
1856 static int ov5693_enum_mbus_code(struct v4l2_subdev
*sd
,
1857 struct v4l2_subdev_pad_config
*cfg
,
1858 struct v4l2_subdev_mbus_code_enum
*code
)
1860 if (code
->index
>= MAX_FMTS
)
1863 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1867 static int ov5693_enum_frame_size(struct v4l2_subdev
*sd
,
1868 struct v4l2_subdev_pad_config
*cfg
,
1869 struct v4l2_subdev_frame_size_enum
*fse
)
1871 int index
= fse
->index
;
1876 fse
->min_width
= ov5693_res
[index
].width
;
1877 fse
->min_height
= ov5693_res
[index
].height
;
1878 fse
->max_width
= ov5693_res
[index
].width
;
1879 fse
->max_height
= ov5693_res
[index
].height
;
1885 static const struct v4l2_subdev_video_ops ov5693_video_ops
= {
1886 .s_stream
= ov5693_s_stream
,
1887 .g_parm
= ov5693_g_parm
,
1888 .s_parm
= ov5693_s_parm
,
1889 .g_frame_interval
= ov5693_g_frame_interval
,
1892 static const struct v4l2_subdev_core_ops ov5693_core_ops
= {
1893 .s_power
= ov5693_s_power
,
1894 .ioctl
= ov5693_ioctl
,
1897 static const struct v4l2_subdev_pad_ops ov5693_pad_ops
= {
1898 .enum_mbus_code
= ov5693_enum_mbus_code
,
1899 .enum_frame_size
= ov5693_enum_frame_size
,
1900 .get_fmt
= ov5693_get_fmt
,
1901 .set_fmt
= ov5693_set_fmt
,
1904 static const struct v4l2_subdev_ops ov5693_ops
= {
1905 .core
= &ov5693_core_ops
,
1906 .video
= &ov5693_video_ops
,
1907 .pad
= &ov5693_pad_ops
,
1910 static int ov5693_remove(struct i2c_client
*client
)
1912 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1913 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1914 dev_dbg(&client
->dev
, "ov5693_remove...\n");
1916 dev
->platform_data
->csi_cfg(sd
, 0);
1918 v4l2_device_unregister_subdev(sd
);
1920 atomisp_gmin_remove_subdev(sd
);
1922 media_entity_cleanup(&dev
->sd
.entity
);
1923 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1929 static int ov5693_probe(struct i2c_client
*client
)
1931 struct ov5693_device
*dev
;
1934 void *pdata
= client
->dev
.platform_data
;
1935 struct acpi_device
*adev
;
1938 /* Firmware workaround: Some modules use a "secondary default"
1939 * address of 0x10 which doesn't appear on schematics, and
1940 * some BIOS versions haven't gotten the memo. Work around
1942 i2c
= gmin_get_var_int(&client
->dev
, "I2CAddr", -1);
1944 dev_info(&client
->dev
,
1945 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n",
1950 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1954 mutex_init(&dev
->input_lock
);
1957 v4l2_i2c_subdev_init(&(dev
->sd
), client
, &ov5693_ops
);
1959 adev
= ACPI_COMPANION(&client
->dev
);
1961 adev
->power
.flags
.power_resources
= 0;
1962 pdata
= gmin_camera_platform_data(&dev
->sd
,
1963 ATOMISP_INPUT_FORMAT_RAW_10
,
1964 atomisp_bayer_order_bggr
);
1970 ret
= ov5693_s_config(&dev
->sd
, client
->irq
, pdata
);
1974 ret
= atomisp_register_i2c_module(&dev
->sd
, pdata
, RAW_CAMERA
);
1978 dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1979 dev
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1980 dev
->format
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1981 dev
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1983 v4l2_ctrl_handler_init(&dev
->ctrl_handler
,
1984 ARRAY_SIZE(ov5693_controls
));
1986 ov5693_remove(client
);
1990 for (i
= 0; i
< ARRAY_SIZE(ov5693_controls
); i
++)
1991 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &ov5693_controls
[i
],
1994 if (dev
->ctrl_handler
.error
) {
1995 ov5693_remove(client
);
1996 return dev
->ctrl_handler
.error
;
1999 /* Use same lock for controls as for everything else. */
2000 dev
->ctrl_handler
.lock
= &dev
->input_lock
;
2001 dev
->sd
.ctrl_handler
= &dev
->ctrl_handler
;
2003 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
2005 ov5693_remove(client
);
2009 v4l2_device_unregister_subdev(&dev
->sd
);
2014 static const struct acpi_device_id ov5693_acpi_match
[] = {
2018 MODULE_DEVICE_TABLE(acpi
, ov5693_acpi_match
);
2020 static struct i2c_driver ov5693_driver
= {
2023 .acpi_match_table
= ov5693_acpi_match
,
2025 .probe_new
= ov5693_probe
,
2026 .remove
= ov5693_remove
,
2028 module_i2c_driver(ov5693_driver
);
2030 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
2031 MODULE_LICENSE("GPL");