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 /* Non-gmin platforms use the legacy callback */
1301 if (dev
->platform_data
->power_ctrl
)
1302 return dev
->platform_data
->power_ctrl(sd
, flag
);
1304 /* This driver assumes "internal DVDD, PWDNB tied to DOVDD".
1305 * In this set up only gpio0 (XSHUTDN) should be available
1306 * but in some products (for example ECS) gpio1 (PWDNB) is
1307 * also available. If gpio1 is available we emulate it being
1308 * tied to DOVDD here. */
1310 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 1);
1311 dev
->platform_data
->gpio1_ctrl(sd
, 1);
1313 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 1);
1315 dev
->platform_data
->gpio1_ctrl(sd
, 0);
1316 ret
= dev
->platform_data
->v2p8_ctrl(sd
, 0);
1320 dev
->platform_data
->gpio1_ctrl(sd
, 0);
1321 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 0);
1322 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 0);
1328 static int gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
1330 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1332 if (!dev
|| !dev
->platform_data
)
1335 /* Non-gmin platforms use the legacy callback */
1336 if (dev
->platform_data
->gpio_ctrl
)
1337 return dev
->platform_data
->gpio_ctrl(sd
, flag
);
1339 return dev
->platform_data
->gpio0_ctrl(sd
, flag
);
1342 static int __power_up(struct v4l2_subdev
*sd
)
1344 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1345 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1348 if (NULL
== dev
->platform_data
) {
1349 dev_err(&client
->dev
,
1350 "no camera_sensor_platform_data");
1355 ret
= power_ctrl(sd
, 1);
1359 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
1360 /* add this delay time to 10~11ms*/
1361 usleep_range(10000, 11000);
1364 ret
= gpio_ctrl(sd
, 1);
1366 ret
= gpio_ctrl(sd
, 1);
1371 /* flis clock control */
1372 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
1376 __cci_delay(up_delay
);
1384 dev_err(&client
->dev
, "sensor power-up failed\n");
1389 static int power_down(struct v4l2_subdev
*sd
)
1391 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1392 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1395 dev
->focus
= OV5693_INVALID_CONFIG
;
1396 if (NULL
== dev
->platform_data
) {
1397 dev_err(&client
->dev
,
1398 "no camera_sensor_platform_data");
1402 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 0);
1404 dev_err(&client
->dev
, "flisclk failed\n");
1407 ret
= gpio_ctrl(sd
, 0);
1409 ret
= gpio_ctrl(sd
, 0);
1411 dev_err(&client
->dev
, "gpio failed 2\n");
1415 ret
= power_ctrl(sd
, 0);
1417 dev_err(&client
->dev
, "vprog failed.\n");
1422 static int power_up(struct v4l2_subdev
*sd
)
1424 static const int retry_count
= 4;
1427 for (i
= 0; i
< retry_count
; i
++) {
1428 ret
= __power_up(sd
);
1437 static int ov5693_s_power(struct v4l2_subdev
*sd
, int on
)
1441 pr_info("%s: on %d\n", __func__
, on
);
1443 return power_down(sd
);
1447 ret
= ov5693_init(sd
);
1448 /* restore settings */
1449 ov5693_res
= ov5693_res_preview
;
1450 N_RES
= N_RES_PREVIEW
;
1457 * distance - calculate the distance
1462 * Get the gap between res_w/res_h and w/h.
1463 * distance = (res_w/res_h - w/h) / (w/h) * 8192
1464 * res->width/height smaller than w/h wouldn't be considered.
1465 * The gap of ratio larger than 1/8 wouldn't be considered.
1466 * Returns the value of gap or -1 if fail.
1468 #define LARGEST_ALLOWED_RATIO_MISMATCH 1024
1469 static int distance(struct ov5693_resolution
*res
, u32 w
, u32 h
)
1474 if (w
== 0 || h
== 0 ||
1475 res
->width
< w
|| res
->height
< h
)
1478 ratio
= res
->width
<< 13;
1481 ratio
/= res
->height
;
1483 distance
= abs(ratio
- 8192);
1485 if (distance
> LARGEST_ALLOWED_RATIO_MISMATCH
)
1491 /* Return the nearest higher resolution index
1492 * Firstly try to find the approximate aspect ratio resolution
1493 * If we find multiple same AR resolutions, choose the
1496 static int nearest_resolution_index(int w
, int h
)
1501 int min_dist
= INT_MAX
;
1502 int min_res_w
= INT_MAX
;
1503 struct ov5693_resolution
*tmp_res
= NULL
;
1505 for (i
= 0; i
< N_RES
; i
++) {
1506 tmp_res
= &ov5693_res
[i
];
1507 dist
= distance(tmp_res
, w
, h
);
1510 if (dist
< min_dist
) {
1513 min_res_w
= ov5693_res
[i
].width
;
1516 if (dist
== min_dist
&& ov5693_res
[i
].width
< min_res_w
)
1523 static int get_resolution_index(int w
, int h
)
1527 for (i
= 0; i
< N_RES
; i
++) {
1528 if (w
!= ov5693_res
[i
].width
)
1530 if (h
!= ov5693_res
[i
].height
)
1539 /* TODO: remove it. */
1540 static int startup(struct v4l2_subdev
*sd
)
1542 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1543 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1546 ret
= ov5693_write_reg(client
, OV5693_8BIT
,
1547 OV5693_SW_RESET
, 0x01);
1549 dev_err(&client
->dev
, "ov5693 reset err.\n");
1553 ret
= ov5693_write_reg_array(client
, ov5693_global_setting
);
1555 dev_err(&client
->dev
, "ov5693 write register err.\n");
1559 ret
= ov5693_write_reg_array(client
, ov5693_res
[dev
->fmt_idx
].regs
);
1561 dev_err(&client
->dev
, "ov5693 write register err.\n");
1568 static int ov5693_set_fmt(struct v4l2_subdev
*sd
,
1569 struct v4l2_subdev_pad_config
*cfg
,
1570 struct v4l2_subdev_format
*format
)
1572 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1573 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1574 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1575 struct camera_mipi_info
*ov5693_info
= NULL
;
1582 ov5693_info
= v4l2_get_subdev_hostdata(sd
);
1583 if (ov5693_info
== NULL
)
1586 mutex_lock(&dev
->input_lock
);
1587 idx
= nearest_resolution_index(fmt
->width
, fmt
->height
);
1589 /* return the largest resolution */
1590 fmt
->width
= ov5693_res
[N_RES
- 1].width
;
1591 fmt
->height
= ov5693_res
[N_RES
- 1].height
;
1593 fmt
->width
= ov5693_res
[idx
].width
;
1594 fmt
->height
= ov5693_res
[idx
].height
;
1597 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1598 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1599 cfg
->try_fmt
= *fmt
;
1600 mutex_unlock(&dev
->input_lock
);
1604 dev
->fmt_idx
= get_resolution_index(fmt
->width
, fmt
->height
);
1605 if (dev
->fmt_idx
== -1) {
1606 dev_err(&client
->dev
, "get resolution fail\n");
1607 mutex_unlock(&dev
->input_lock
);
1614 dev_err(&client
->dev
, "ov5693 startup err, retry to power up\n");
1615 for (i
= 0; i
< OV5693_POWER_UP_RETRY_NUM
; i
++) {
1616 dev_err(&client
->dev
,
1617 "ov5693 retry to power up %d/%d times, result: ",
1618 i
+1, OV5693_POWER_UP_RETRY_NUM
);
1622 mutex_unlock(&dev
->input_lock
);
1624 mutex_lock(&dev
->input_lock
);
1626 dev_err(&client
->dev
, "power up failed, continue\n");
1631 dev_err(&client
->dev
, " startup FAILED!\n");
1633 dev_err(&client
->dev
, " startup SUCCESS!\n");
1640 * After sensor settings are set to HW, sometimes stream is started.
1641 * This would cause ISP timeout because ISP is not ready to receive
1642 * data yet. So add stop streaming here.
1644 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_SW_STREAM
,
1645 OV5693_STOP_STREAMING
);
1647 dev_warn(&client
->dev
, "ov5693 stream off err\n");
1649 ret
= ov5693_get_intg_factor(client
, ov5693_info
,
1650 &ov5693_res
[dev
->fmt_idx
]);
1652 dev_err(&client
->dev
, "failed to get integration_factor\n");
1656 ov5693_info
->metadata_width
= fmt
->width
* 10 / 8;
1657 ov5693_info
->metadata_height
= 1;
1658 ov5693_info
->metadata_effective_width
= &ov5693_embedded_effective_size
;
1661 mutex_unlock(&dev
->input_lock
);
1664 static int ov5693_get_fmt(struct v4l2_subdev
*sd
,
1665 struct v4l2_subdev_pad_config
*cfg
,
1666 struct v4l2_subdev_format
*format
)
1668 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1669 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1676 fmt
->width
= ov5693_res
[dev
->fmt_idx
].width
;
1677 fmt
->height
= ov5693_res
[dev
->fmt_idx
].height
;
1678 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1683 static int ov5693_detect(struct i2c_client
*client
)
1685 struct i2c_adapter
*adapter
= client
->adapter
;
1691 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
1694 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1695 OV5693_SC_CMMN_CHIP_ID_H
, &high
);
1697 dev_err(&client
->dev
, "sensor_id_high = 0x%x\n", high
);
1700 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1701 OV5693_SC_CMMN_CHIP_ID_L
, &low
);
1702 id
= ((((u16
) high
) << 8) | (u16
) low
);
1704 if (id
!= OV5693_ID
) {
1705 dev_err(&client
->dev
, "sensor ID error 0x%x\n", id
);
1709 ret
= ov5693_read_reg(client
, OV5693_8BIT
,
1710 OV5693_SC_CMMN_SUB_ID
, &high
);
1711 revision
= (u8
) high
& 0x0f;
1713 dev_dbg(&client
->dev
, "sensor_revision = 0x%x\n", revision
);
1714 dev_dbg(&client
->dev
, "detect ov5693 success\n");
1718 static int ov5693_s_stream(struct v4l2_subdev
*sd
, int enable
)
1720 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1721 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1724 mutex_lock(&dev
->input_lock
);
1726 ret
= ov5693_write_reg(client
, OV5693_8BIT
, OV5693_SW_STREAM
,
1727 enable
? OV5693_START_STREAMING
:
1728 OV5693_STOP_STREAMING
);
1730 mutex_unlock(&dev
->input_lock
);
1736 static int ov5693_s_config(struct v4l2_subdev
*sd
,
1737 int irq
, void *platform_data
)
1739 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1740 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1743 if (platform_data
== NULL
)
1746 dev
->platform_data
=
1747 (struct camera_sensor_platform_data
*)platform_data
;
1749 mutex_lock(&dev
->input_lock
);
1750 /* power off the module, then power on it in future
1751 * as first power on by board may not fulfill the
1752 * power on sequqence needed by the module
1754 ret
= power_down(sd
);
1756 dev_err(&client
->dev
, "ov5693 power-off err.\n");
1757 goto fail_power_off
;
1762 dev_err(&client
->dev
, "ov5693 power-up err.\n");
1767 dev
->vcm
= vcm_detect(client
);
1769 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
1773 /* config & detect sensor */
1774 ret
= ov5693_detect(client
);
1776 dev_err(&client
->dev
, "ov5693_detect err s_config.\n");
1780 dev
->otp_data
= ov5693_otp_read(sd
);
1782 /* turn off sensor, after probed */
1783 ret
= power_down(sd
);
1785 dev_err(&client
->dev
, "ov5693 power-off err.\n");
1788 mutex_unlock(&dev
->input_lock
);
1793 dev
->platform_data
->csi_cfg(sd
, 0);
1796 dev_err(&client
->dev
, "sensor power-gating failed\n");
1798 mutex_unlock(&dev
->input_lock
);
1802 static int ov5693_g_parm(struct v4l2_subdev
*sd
,
1803 struct v4l2_streamparm
*param
)
1805 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1806 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1811 if (param
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1812 dev_err(&client
->dev
, "unsupported buffer type.\n");
1816 memset(param
, 0, sizeof(*param
));
1817 param
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1819 if (dev
->fmt_idx
>= 0 && dev
->fmt_idx
< N_RES
) {
1820 param
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1821 param
->parm
.capture
.timeperframe
.numerator
= 1;
1822 param
->parm
.capture
.capturemode
= dev
->run_mode
;
1823 param
->parm
.capture
.timeperframe
.denominator
=
1824 ov5693_res
[dev
->fmt_idx
].fps
;
1829 static int ov5693_s_parm(struct v4l2_subdev
*sd
,
1830 struct v4l2_streamparm
*param
)
1832 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1833 dev
->run_mode
= param
->parm
.capture
.capturemode
;
1835 mutex_lock(&dev
->input_lock
);
1836 switch (dev
->run_mode
) {
1838 ov5693_res
= ov5693_res_video
;
1839 N_RES
= N_RES_VIDEO
;
1841 case CI_MODE_STILL_CAPTURE
:
1842 ov5693_res
= ov5693_res_still
;
1843 N_RES
= N_RES_STILL
;
1846 ov5693_res
= ov5693_res_preview
;
1847 N_RES
= N_RES_PREVIEW
;
1849 mutex_unlock(&dev
->input_lock
);
1853 static int ov5693_g_frame_interval(struct v4l2_subdev
*sd
,
1854 struct v4l2_subdev_frame_interval
*interval
)
1856 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1858 interval
->interval
.numerator
= 1;
1859 interval
->interval
.denominator
= ov5693_res
[dev
->fmt_idx
].fps
;
1864 static int ov5693_enum_mbus_code(struct v4l2_subdev
*sd
,
1865 struct v4l2_subdev_pad_config
*cfg
,
1866 struct v4l2_subdev_mbus_code_enum
*code
)
1868 if (code
->index
>= MAX_FMTS
)
1871 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1875 static int ov5693_enum_frame_size(struct v4l2_subdev
*sd
,
1876 struct v4l2_subdev_pad_config
*cfg
,
1877 struct v4l2_subdev_frame_size_enum
*fse
)
1879 int index
= fse
->index
;
1884 fse
->min_width
= ov5693_res
[index
].width
;
1885 fse
->min_height
= ov5693_res
[index
].height
;
1886 fse
->max_width
= ov5693_res
[index
].width
;
1887 fse
->max_height
= ov5693_res
[index
].height
;
1893 static const struct v4l2_subdev_video_ops ov5693_video_ops
= {
1894 .s_stream
= ov5693_s_stream
,
1895 .g_parm
= ov5693_g_parm
,
1896 .s_parm
= ov5693_s_parm
,
1897 .g_frame_interval
= ov5693_g_frame_interval
,
1900 static const struct v4l2_subdev_core_ops ov5693_core_ops
= {
1901 .s_power
= ov5693_s_power
,
1902 .ioctl
= ov5693_ioctl
,
1905 static const struct v4l2_subdev_pad_ops ov5693_pad_ops
= {
1906 .enum_mbus_code
= ov5693_enum_mbus_code
,
1907 .enum_frame_size
= ov5693_enum_frame_size
,
1908 .get_fmt
= ov5693_get_fmt
,
1909 .set_fmt
= ov5693_set_fmt
,
1912 static const struct v4l2_subdev_ops ov5693_ops
= {
1913 .core
= &ov5693_core_ops
,
1914 .video
= &ov5693_video_ops
,
1915 .pad
= &ov5693_pad_ops
,
1918 static int ov5693_remove(struct i2c_client
*client
)
1920 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1921 struct ov5693_device
*dev
= to_ov5693_sensor(sd
);
1922 dev_dbg(&client
->dev
, "ov5693_remove...\n");
1924 dev
->platform_data
->csi_cfg(sd
, 0);
1926 v4l2_device_unregister_subdev(sd
);
1928 atomisp_gmin_remove_subdev(sd
);
1930 media_entity_cleanup(&dev
->sd
.entity
);
1931 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1937 static int ov5693_probe(struct i2c_client
*client
)
1939 struct ov5693_device
*dev
;
1942 void *pdata
= client
->dev
.platform_data
;
1943 struct acpi_device
*adev
;
1946 /* Firmware workaround: Some modules use a "secondary default"
1947 * address of 0x10 which doesn't appear on schematics, and
1948 * some BIOS versions haven't gotten the memo. Work around
1950 i2c
= gmin_get_var_int(&client
->dev
, "I2CAddr", -1);
1952 dev_info(&client
->dev
,
1953 "Overriding firmware-provided I2C address (0x%x) with 0x%x\n",
1958 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1962 mutex_init(&dev
->input_lock
);
1965 v4l2_i2c_subdev_init(&(dev
->sd
), client
, &ov5693_ops
);
1967 adev
= ACPI_COMPANION(&client
->dev
);
1969 adev
->power
.flags
.power_resources
= 0;
1970 pdata
= gmin_camera_platform_data(&dev
->sd
,
1971 ATOMISP_INPUT_FORMAT_RAW_10
,
1972 atomisp_bayer_order_bggr
);
1978 ret
= ov5693_s_config(&dev
->sd
, client
->irq
, pdata
);
1982 ret
= atomisp_register_i2c_module(&dev
->sd
, pdata
, RAW_CAMERA
);
1986 dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1987 dev
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1988 dev
->format
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1989 dev
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1991 v4l2_ctrl_handler_init(&dev
->ctrl_handler
,
1992 ARRAY_SIZE(ov5693_controls
));
1994 ov5693_remove(client
);
1998 for (i
= 0; i
< ARRAY_SIZE(ov5693_controls
); i
++)
1999 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &ov5693_controls
[i
],
2002 if (dev
->ctrl_handler
.error
) {
2003 ov5693_remove(client
);
2004 return dev
->ctrl_handler
.error
;
2007 /* Use same lock for controls as for everything else. */
2008 dev
->ctrl_handler
.lock
= &dev
->input_lock
;
2009 dev
->sd
.ctrl_handler
= &dev
->ctrl_handler
;
2011 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
2013 ov5693_remove(client
);
2017 v4l2_device_unregister_subdev(&dev
->sd
);
2022 static const struct acpi_device_id ov5693_acpi_match
[] = {
2026 MODULE_DEVICE_TABLE(acpi
, ov5693_acpi_match
);
2028 static struct i2c_driver ov5693_driver
= {
2031 .acpi_match_table
= ov5693_acpi_match
,
2033 .probe_new
= ov5693_probe
,
2034 .remove
= ov5693_remove
,
2036 module_i2c_driver(ov5693_driver
);
2038 MODULE_DESCRIPTION("A low-level driver for OmniVision 5693 sensors");
2039 MODULE_LICENSE("GPL");