2 * Support for OmniVision OV2680 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.
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/gpio.h>
30 #include <linux/moduleparam.h>
31 #include <media/v4l2-device.h>
33 #include <linux/acpi.h>
34 #include "../include/linux/atomisp_gmin_platform.h"
38 static int h_flag
= 0;
39 static int v_flag
= 0;
40 static enum atomisp_bayer_order ov2680_bayer_order_mapping
[] = {
41 atomisp_bayer_order_bggr
,
42 atomisp_bayer_order_grbg
,
43 atomisp_bayer_order_gbrg
,
44 atomisp_bayer_order_rggb
,
47 /* i2c read/write stuff */
48 static int ov2680_read_reg(struct i2c_client
*client
,
49 u16 data_length
, u16 reg
, u16
*val
)
52 struct i2c_msg msg
[2];
53 unsigned char data
[6];
55 if (!client
->adapter
) {
56 dev_err(&client
->dev
, "%s error, no client->adapter\n",
61 if (data_length
!= OV2680_8BIT
&& data_length
!= OV2680_16BIT
62 && data_length
!= OV2680_32BIT
) {
63 dev_err(&client
->dev
, "%s error, invalid data length\n",
68 memset(msg
, 0 , sizeof(msg
));
70 msg
[0].addr
= client
->addr
;
72 msg
[0].len
= I2C_MSG_LENGTH
;
75 /* high byte goes out first */
76 data
[0] = (u8
)(reg
>> 8);
77 data
[1] = (u8
)(reg
& 0xff);
79 msg
[1].addr
= client
->addr
;
80 msg
[1].len
= data_length
;
81 msg
[1].flags
= I2C_M_RD
;
84 err
= i2c_transfer(client
->adapter
, msg
, 2);
89 "read from offset 0x%x error %d", reg
, err
);
94 /* high byte comes first */
95 if (data_length
== OV2680_8BIT
)
97 else if (data_length
== OV2680_16BIT
)
98 *val
= be16_to_cpu(*(u16
*)&data
[0]);
100 *val
= be32_to_cpu(*(u32
*)&data
[0]);
101 //dev_dbg(&client->dev, "++++i2c read adr%x = %x\n", reg,*val);
105 static int ov2680_i2c_write(struct i2c_client
*client
, u16 len
, u8
*data
)
108 const int num_msg
= 1;
111 msg
.addr
= client
->addr
;
115 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
116 //dev_dbg(&client->dev, "+++i2c write reg=%x->%x\n", data[0]*256 +data[1],data[2]);
117 return ret
== num_msg
? 0 : -EIO
;
120 static int ov2680_write_reg(struct i2c_client
*client
, u16 data_length
,
124 unsigned char data
[4] = {0};
125 u16
*wreg
= (u16
*)data
;
126 const u16 len
= data_length
+ sizeof(u16
); /* 16-bit address + data */
128 if (data_length
!= OV2680_8BIT
&& data_length
!= OV2680_16BIT
) {
129 dev_err(&client
->dev
,
130 "%s error, invalid data_length\n", __func__
);
134 /* high byte goes out first */
135 *wreg
= cpu_to_be16(reg
);
137 if (data_length
== OV2680_8BIT
) {
141 u16
*wdata
= (u16
*)&data
[2];
142 *wdata
= cpu_to_be16(val
);
145 ret
= ov2680_i2c_write(client
, len
, data
);
147 dev_err(&client
->dev
,
148 "write error: wrote 0x%x to offset 0x%x error %d",
155 * ov2680_write_reg_array - Initializes a list of OV2680 registers
156 * @client: i2c driver client structure
157 * @reglist: list of registers to be written
159 * This function initializes a list of registers. When consecutive addresses
160 * are found in a row on the list, this function creates a buffer and sends
161 * consecutive data in a single i2c_transfer().
163 * __ov2680_flush_reg_array, __ov2680_buf_reg_array() and
164 * __ov2680_write_reg_is_consecutive() are internal functions to
165 * ov2680_write_reg_array_fast() and should be not used anywhere else.
169 static int __ov2680_flush_reg_array(struct i2c_client
*client
,
170 struct ov2680_write_ctrl
*ctrl
)
174 if (ctrl
->index
== 0)
177 size
= sizeof(u16
) + ctrl
->index
; /* 16-bit address + data */
178 ctrl
->buffer
.addr
= cpu_to_be16(ctrl
->buffer
.addr
);
181 return ov2680_i2c_write(client
, size
, (u8
*)&ctrl
->buffer
);
184 static int __ov2680_buf_reg_array(struct i2c_client
*client
,
185 struct ov2680_write_ctrl
*ctrl
,
186 const struct ov2680_reg
*next
)
191 switch (next
->type
) {
194 ctrl
->buffer
.data
[ctrl
->index
] = (u8
)next
->val
;
198 data16
= (u16
*)&ctrl
->buffer
.data
[ctrl
->index
];
199 *data16
= cpu_to_be16((u16
)next
->val
);
205 /* When first item is added, we need to store its starting address */
206 if (ctrl
->index
== 0)
207 ctrl
->buffer
.addr
= next
->reg
;
212 * Buffer cannot guarantee free space for u32? Better flush it to avoid
213 * possible lack of memory for next item.
215 if (ctrl
->index
+ sizeof(u16
) >= OV2680_MAX_WRITE_BUF_SIZE
)
216 return __ov2680_flush_reg_array(client
, ctrl
);
221 static int __ov2680_write_reg_is_consecutive(struct i2c_client
*client
,
222 struct ov2680_write_ctrl
*ctrl
,
223 const struct ov2680_reg
*next
)
225 if (ctrl
->index
== 0)
228 return ctrl
->buffer
.addr
+ ctrl
->index
== next
->reg
;
231 static int ov2680_write_reg_array(struct i2c_client
*client
,
232 const struct ov2680_reg
*reglist
)
234 const struct ov2680_reg
*next
= reglist
;
235 struct ov2680_write_ctrl ctrl
;
237 dev_dbg(&client
->dev
, "++++write reg array\n");
239 for (; next
->type
!= OV2680_TOK_TERM
; next
++) {
240 switch (next
->type
& OV2680_TOK_MASK
) {
241 case OV2680_TOK_DELAY
:
242 err
= __ov2680_flush_reg_array(client
, &ctrl
);
249 * If next address is not consecutive, data needs to be
250 * flushed before proceed.
252 dev_dbg(&client
->dev
, "+++ov2680_write_reg_array reg=%x->%x\n", next
->reg
,next
->val
);
253 if (!__ov2680_write_reg_is_consecutive(client
, &ctrl
,
255 err
= __ov2680_flush_reg_array(client
, &ctrl
);
259 err
= __ov2680_buf_reg_array(client
, &ctrl
, next
);
261 dev_err(&client
->dev
, "%s: write error, aborted\n",
269 return __ov2680_flush_reg_array(client
, &ctrl
);
271 static int ov2680_g_focal(struct v4l2_subdev
*sd
, s32
*val
)
274 *val
= (OV2680_FOCAL_LENGTH_NUM
<< 16) | OV2680_FOCAL_LENGTH_DEM
;
278 static int ov2680_g_fnumber(struct v4l2_subdev
*sd
, s32
*val
)
280 /*const f number for ov2680*/
282 *val
= (OV2680_F_NUMBER_DEFAULT_NUM
<< 16) | OV2680_F_NUMBER_DEM
;
286 static int ov2680_g_fnumber_range(struct v4l2_subdev
*sd
, s32
*val
)
288 *val
= (OV2680_F_NUMBER_DEFAULT_NUM
<< 24) |
289 (OV2680_F_NUMBER_DEM
<< 16) |
290 (OV2680_F_NUMBER_DEFAULT_NUM
<< 8) | OV2680_F_NUMBER_DEM
;
294 static int ov2680_g_bin_factor_x(struct v4l2_subdev
*sd
, s32
*val
)
296 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
297 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
298 dev_dbg(&client
->dev
, "++++ov2680_g_bin_factor_x\n");
299 *val
= ov2680_res
[dev
->fmt_idx
].bin_factor_x
;
304 static int ov2680_g_bin_factor_y(struct v4l2_subdev
*sd
, s32
*val
)
306 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
307 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
309 *val
= ov2680_res
[dev
->fmt_idx
].bin_factor_y
;
310 dev_dbg(&client
->dev
, "++++ov2680_g_bin_factor_y\n");
315 static int ov2680_get_intg_factor(struct i2c_client
*client
,
316 struct camera_mipi_info
*info
,
317 const struct ov2680_resolution
*res
)
319 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
320 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
321 struct atomisp_sensor_mode_data
*buf
= &info
->data
;
322 unsigned int pix_clk_freq_hz
;
325 dev_dbg(&client
->dev
, "++++ov2680_get_intg_factor\n");
330 pix_clk_freq_hz
= res
->pix_clk_freq
* 1000000;
332 dev
->vt_pix_clk_freq_mhz
= pix_clk_freq_hz
;
333 buf
->vt_pix_clk_freq_mhz
= pix_clk_freq_hz
;
335 /* get integration time */
336 buf
->coarse_integration_time_min
= OV2680_COARSE_INTG_TIME_MIN
;
337 buf
->coarse_integration_time_max_margin
=
338 OV2680_COARSE_INTG_TIME_MAX_MARGIN
;
340 buf
->fine_integration_time_min
= OV2680_FINE_INTG_TIME_MIN
;
341 buf
->fine_integration_time_max_margin
=
342 OV2680_FINE_INTG_TIME_MAX_MARGIN
;
344 buf
->fine_integration_time_def
= OV2680_FINE_INTG_TIME_MIN
;
345 buf
->frame_length_lines
= res
->lines_per_frame
;
346 buf
->line_length_pck
= res
->pixels_per_line
;
347 buf
->read_mode
= res
->bin_mode
;
349 /* get the cropping and output resolution to ISP for this mode. */
350 ret
= ov2680_read_reg(client
, OV2680_16BIT
,
351 OV2680_HORIZONTAL_START_H
, ®_val
);
354 buf
->crop_horizontal_start
= reg_val
;
356 ret
= ov2680_read_reg(client
, OV2680_16BIT
,
357 OV2680_VERTICAL_START_H
, ®_val
);
360 buf
->crop_vertical_start
= reg_val
;
362 ret
= ov2680_read_reg(client
, OV2680_16BIT
,
363 OV2680_HORIZONTAL_END_H
, ®_val
);
366 buf
->crop_horizontal_end
= reg_val
;
368 ret
= ov2680_read_reg(client
, OV2680_16BIT
,
369 OV2680_VERTICAL_END_H
, ®_val
);
372 buf
->crop_vertical_end
= reg_val
;
374 ret
= ov2680_read_reg(client
, OV2680_16BIT
,
375 OV2680_HORIZONTAL_OUTPUT_SIZE_H
, ®_val
);
378 buf
->output_width
= reg_val
;
380 ret
= ov2680_read_reg(client
, OV2680_16BIT
,
381 OV2680_VERTICAL_OUTPUT_SIZE_H
, ®_val
);
384 buf
->output_height
= reg_val
;
386 buf
->binning_factor_x
= res
->bin_factor_x
?
387 (res
->bin_factor_x
* 2) : 1;
388 buf
->binning_factor_y
= res
->bin_factor_y
?
389 (res
->bin_factor_y
* 2) : 1;
393 static long __ov2680_set_exposure(struct v4l2_subdev
*sd
, int coarse_itg
,
394 int gain
, int digitgain
)
397 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
398 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
402 dev_dbg(&client
->dev
, "+++++++__ov2680_set_exposure coarse_itg %d, gain %d, digitgain %d++\n",coarse_itg
, gain
, digitgain
);
404 hts
= ov2680_res
[dev
->fmt_idx
].pixels_per_line
;
405 vts
= ov2680_res
[dev
->fmt_idx
].lines_per_frame
;
408 ret
= ov2680_write_reg(client
, OV2680_8BIT
,
409 OV2680_GROUP_ACCESS
, 0x00);
411 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
412 __func__
, OV2680_GROUP_ACCESS
);
416 /* Increase the VTS to match exposure + MARGIN */
417 if (coarse_itg
> vts
- OV2680_INTEGRATION_TIME_MARGIN
)
418 vts
= (u16
) coarse_itg
+ OV2680_INTEGRATION_TIME_MARGIN
;
420 ret
= ov2680_write_reg(client
, OV2680_16BIT
, OV2680_TIMING_VTS_H
, vts
);
422 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
423 __func__
, OV2680_TIMING_VTS_H
);
429 /* Lower four bit should be 0*/
430 exp_val
= coarse_itg
<< 4;
431 ret
= ov2680_write_reg(client
, OV2680_8BIT
,
432 OV2680_EXPOSURE_L
, exp_val
& 0xFF);
434 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
435 __func__
, OV2680_EXPOSURE_L
);
439 ret
= ov2680_write_reg(client
, OV2680_8BIT
,
440 OV2680_EXPOSURE_M
, (exp_val
>> 8) & 0xFF);
442 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
443 __func__
, OV2680_EXPOSURE_M
);
447 ret
= ov2680_write_reg(client
, OV2680_8BIT
,
448 OV2680_EXPOSURE_H
, (exp_val
>> 16) & 0x0F);
450 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
451 __func__
, OV2680_EXPOSURE_H
);
456 ret
= ov2680_write_reg(client
, OV2680_16BIT
, OV2680_AGC_H
, gain
);
458 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
459 __func__
, OV2680_AGC_H
);
464 ret
= ov2680_write_reg(client
, OV2680_16BIT
,
465 OV2680_MWB_RED_GAIN_H
, digitgain
);
467 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
468 __func__
, OV2680_MWB_RED_GAIN_H
);
472 ret
= ov2680_write_reg(client
, OV2680_16BIT
,
473 OV2680_MWB_GREEN_GAIN_H
, digitgain
);
475 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
476 __func__
, OV2680_MWB_RED_GAIN_H
);
480 ret
= ov2680_write_reg(client
, OV2680_16BIT
,
481 OV2680_MWB_BLUE_GAIN_H
, digitgain
);
483 dev_err(&client
->dev
, "%s: write %x error, aborted\n",
484 __func__
, OV2680_MWB_RED_GAIN_H
);
490 ret
= ov2680_write_reg(client
, OV2680_8BIT
,
491 OV2680_GROUP_ACCESS
, 0x10);
495 /* Delay launch group */
496 ret
= ov2680_write_reg(client
, OV2680_8BIT
,
497 OV2680_GROUP_ACCESS
, 0xa0);
503 static int ov2680_set_exposure(struct v4l2_subdev
*sd
, int exposure
,
504 int gain
, int digitgain
)
506 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
509 mutex_lock(&dev
->input_lock
);
510 ret
= __ov2680_set_exposure(sd
, exposure
, gain
, digitgain
);
511 mutex_unlock(&dev
->input_lock
);
516 static long ov2680_s_exposure(struct v4l2_subdev
*sd
,
517 struct atomisp_exposure
*exposure
)
519 u16 coarse_itg
= exposure
->integration_time
[0];
520 u16 analog_gain
= exposure
->gain
[0];
521 u16 digital_gain
= exposure
->gain
[1];
523 /* we should not accept the invalid value below */
524 if (analog_gain
== 0) {
525 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
526 v4l2_err(client
, "%s: invalid value\n", __func__
);
530 // EXPOSURE CONTROL DISABLED FOR INITIAL CHECKIN, TUNING DOESN'T WORK
531 return ov2680_set_exposure(sd
, coarse_itg
, analog_gain
, digital_gain
);
538 static long ov2680_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
542 case ATOMISP_IOC_S_EXPOSURE
:
543 return ov2680_s_exposure(sd
, arg
);
551 /* This returns the exposure time being used. This should only be used
552 * for filling in EXIF data, not for actual image processing.
554 static int ov2680_q_exposure(struct v4l2_subdev
*sd
, s32
*value
)
556 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
561 ret
= ov2680_read_reg(client
, OV2680_8BIT
,
567 ret
= ov2680_read_reg(client
, OV2680_8BIT
,
573 reg_v
+= reg_v2
<< 8;
574 ret
= ov2680_read_reg(client
, OV2680_8BIT
,
580 *value
= reg_v
+ (((u32
)reg_v2
<< 16));
585 static u32
ov2680_translate_bayer_order(enum atomisp_bayer_order code
)
588 case atomisp_bayer_order_rggb
:
589 return MEDIA_BUS_FMT_SRGGB10_1X10
;
590 case atomisp_bayer_order_grbg
:
591 return MEDIA_BUS_FMT_SGRBG10_1X10
;
592 case atomisp_bayer_order_bggr
:
593 return MEDIA_BUS_FMT_SBGGR10_1X10
;
594 case atomisp_bayer_order_gbrg
:
595 return MEDIA_BUS_FMT_SGBRG10_1X10
;
600 static int ov2680_v_flip(struct v4l2_subdev
*sd
, s32 value
)
602 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
603 struct camera_mipi_info
*ov2680_info
= NULL
;
604 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
608 dev_dbg(&client
->dev
, "@%s: value:%d\n", __func__
, value
);
609 ret
= ov2680_read_reg(client
, OV2680_8BIT
, OV2680_FLIP_REG
, &val
);
613 val
|= OV2680_FLIP_MIRROR_BIT_ENABLE
;
615 val
&= ~OV2680_FLIP_MIRROR_BIT_ENABLE
;
617 ret
= ov2680_write_reg(client
, OV2680_8BIT
,
618 OV2680_FLIP_REG
, val
);
621 index
= (v_flag
>0?OV2680_FLIP_BIT
:0) | (h_flag
>0?OV2680_MIRROR_BIT
:0);
622 ov2680_info
= v4l2_get_subdev_hostdata(sd
);
624 ov2680_info
->raw_bayer_order
= ov2680_bayer_order_mapping
[index
];
625 dev
->format
.code
= ov2680_translate_bayer_order(
626 ov2680_info
->raw_bayer_order
);
631 static int ov2680_h_flip(struct v4l2_subdev
*sd
, s32 value
)
633 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
634 struct camera_mipi_info
*ov2680_info
= NULL
;
635 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
639 dev_dbg(&client
->dev
, "@%s: value:%d\n", __func__
, value
);
641 ret
= ov2680_read_reg(client
, OV2680_8BIT
, OV2680_MIRROR_REG
, &val
);
645 val
|= OV2680_FLIP_MIRROR_BIT_ENABLE
;
647 val
&= ~OV2680_FLIP_MIRROR_BIT_ENABLE
;
649 ret
= ov2680_write_reg(client
, OV2680_8BIT
,
650 OV2680_MIRROR_REG
, val
);
653 index
= (v_flag
>0?OV2680_FLIP_BIT
:0) | (h_flag
>0?OV2680_MIRROR_BIT
:0);
654 ov2680_info
= v4l2_get_subdev_hostdata(sd
);
656 ov2680_info
->raw_bayer_order
= ov2680_bayer_order_mapping
[index
];
657 dev
->format
.code
= ov2680_translate_bayer_order(
658 ov2680_info
->raw_bayer_order
);
663 static int ov2680_s_ctrl(struct v4l2_ctrl
*ctrl
)
665 struct ov2680_device
*dev
=
666 container_of(ctrl
->handler
, struct ov2680_device
, ctrl_handler
);
667 struct i2c_client
*client
= v4l2_get_subdevdata(&dev
->sd
);
672 dev_dbg(&client
->dev
, "%s: CID_VFLIP:%d.\n",
673 __func__
, ctrl
->val
);
674 ret
= ov2680_v_flip(&dev
->sd
, ctrl
->val
);
677 dev_dbg(&client
->dev
, "%s: CID_HFLIP:%d.\n",
678 __func__
, ctrl
->val
);
679 ret
= ov2680_h_flip(&dev
->sd
, ctrl
->val
);
687 static int ov2680_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
689 struct ov2680_device
*dev
=
690 container_of(ctrl
->handler
, struct ov2680_device
, ctrl_handler
);
694 case V4L2_CID_EXPOSURE_ABSOLUTE
:
695 ret
= ov2680_q_exposure(&dev
->sd
, &ctrl
->val
);
697 case V4L2_CID_FOCAL_ABSOLUTE
:
698 ret
= ov2680_g_focal(&dev
->sd
, &ctrl
->val
);
700 case V4L2_CID_FNUMBER_ABSOLUTE
:
701 ret
= ov2680_g_fnumber(&dev
->sd
, &ctrl
->val
);
703 case V4L2_CID_FNUMBER_RANGE
:
704 ret
= ov2680_g_fnumber_range(&dev
->sd
, &ctrl
->val
);
706 case V4L2_CID_BIN_FACTOR_HORZ
:
707 ret
= ov2680_g_bin_factor_x(&dev
->sd
, &ctrl
->val
);
709 case V4L2_CID_BIN_FACTOR_VERT
:
710 ret
= ov2680_g_bin_factor_y(&dev
->sd
, &ctrl
->val
);
719 static const struct v4l2_ctrl_ops ctrl_ops
= {
720 .s_ctrl
= ov2680_s_ctrl
,
721 .g_volatile_ctrl
= ov2680_g_volatile_ctrl
724 struct v4l2_ctrl_config ov2680_controls
[] = {
727 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
728 .type
= V4L2_CTRL_TYPE_INTEGER
,
738 .id
= V4L2_CID_FOCAL_ABSOLUTE
,
739 .type
= V4L2_CTRL_TYPE_INTEGER
,
740 .name
= "focal length",
741 .min
= OV2680_FOCAL_LENGTH_DEFAULT
,
742 .max
= OV2680_FOCAL_LENGTH_DEFAULT
,
744 .def
= OV2680_FOCAL_LENGTH_DEFAULT
,
749 .id
= V4L2_CID_FNUMBER_ABSOLUTE
,
750 .type
= V4L2_CTRL_TYPE_INTEGER
,
752 .min
= OV2680_F_NUMBER_DEFAULT
,
753 .max
= OV2680_F_NUMBER_DEFAULT
,
755 .def
= OV2680_F_NUMBER_DEFAULT
,
760 .id
= V4L2_CID_FNUMBER_RANGE
,
761 .type
= V4L2_CTRL_TYPE_INTEGER
,
762 .name
= "f-number range",
763 .min
= OV2680_F_NUMBER_RANGE
,
764 .max
= OV2680_F_NUMBER_RANGE
,
766 .def
= OV2680_F_NUMBER_RANGE
,
771 .id
= V4L2_CID_BIN_FACTOR_HORZ
,
772 .type
= V4L2_CTRL_TYPE_INTEGER
,
773 .name
= "horizontal binning factor",
775 .max
= OV2680_BIN_FACTOR_MAX
,
782 .id
= V4L2_CID_BIN_FACTOR_VERT
,
783 .type
= V4L2_CTRL_TYPE_INTEGER
,
784 .name
= "vertical binning factor",
786 .max
= OV2680_BIN_FACTOR_MAX
,
793 .id
= V4L2_CID_VFLIP
,
794 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
803 .id
= V4L2_CID_HFLIP
,
804 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
813 static int ov2680_init_registers(struct v4l2_subdev
*sd
)
815 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
818 ret
= ov2680_write_reg(client
, OV2680_8BIT
, OV2680_SW_RESET
, 0x01);
819 ret
|= ov2680_write_reg_array(client
, ov2680_global_setting
);
824 static int ov2680_init(struct v4l2_subdev
*sd
)
826 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
830 mutex_lock(&dev
->input_lock
);
832 /* restore settings */
833 ov2680_res
= ov2680_res_preview
;
834 N_RES
= N_RES_PREVIEW
;
836 ret
= ov2680_init_registers(sd
);
838 mutex_unlock(&dev
->input_lock
);
843 static int power_ctrl(struct v4l2_subdev
*sd
, bool flag
)
846 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
847 if (!dev
|| !dev
->platform_data
)
850 /* Non-gmin platforms use the legacy callback */
851 if (dev
->platform_data
->power_ctrl
)
852 return dev
->platform_data
->power_ctrl(sd
, flag
);
855 ret
|= dev
->platform_data
->v1p8_ctrl(sd
, 1);
856 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 1);
857 usleep_range(10000, 15000);
861 ret
|= dev
->platform_data
->v1p8_ctrl(sd
, 0);
862 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 0);
867 static int gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
870 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
872 if (!dev
|| !dev
->platform_data
)
875 /* Non-gmin platforms use the legacy callback */
876 if (dev
->platform_data
->gpio_ctrl
)
877 return dev
->platform_data
->gpio_ctrl(sd
, flag
);
879 /* The OV2680 documents only one GPIO input (#XSHUTDN), but
880 * existing integrations often wire two (reset/power_down)
881 * because that is the way other sensors work. There is no
882 * way to tell how it is wired internally, so existing
883 * firmwares expose both and we drive them symmetrically. */
885 ret
= dev
->platform_data
->gpio0_ctrl(sd
, 1);
886 usleep_range(10000, 15000);
887 /* Ignore return from second gpio, it may not be there */
888 dev
->platform_data
->gpio1_ctrl(sd
, 1);
889 usleep_range(10000, 15000);
891 dev
->platform_data
->gpio1_ctrl(sd
, 0);
892 ret
= dev
->platform_data
->gpio0_ctrl(sd
, 0);
897 static int power_up(struct v4l2_subdev
*sd
)
899 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
900 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
903 if (!dev
->platform_data
) {
904 dev_err(&client
->dev
,
905 "no camera_sensor_platform_data");
910 ret
= power_ctrl(sd
, 1);
914 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
915 usleep_range(5000, 6000);
918 ret
= gpio_ctrl(sd
, 1);
920 ret
= gpio_ctrl(sd
, 1);
925 /* flis clock control */
926 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
930 /* according to DS, 20ms is needed between PWDN and i2c access */
939 dev_err(&client
->dev
, "sensor power-up failed\n");
944 static int power_down(struct v4l2_subdev
*sd
)
946 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
947 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
952 if (!dev
->platform_data
) {
953 dev_err(&client
->dev
,
954 "no camera_sensor_platform_data");
958 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 0);
960 dev_err(&client
->dev
, "flisclk failed\n");
963 ret
= gpio_ctrl(sd
, 0);
965 ret
= gpio_ctrl(sd
, 0);
967 dev_err(&client
->dev
, "gpio failed 2\n");
971 ret
= power_ctrl(sd
, 0);
973 dev_err(&client
->dev
, "vprog failed.\n");
978 static int ov2680_s_power(struct v4l2_subdev
*sd
, int on
)
983 ret
= power_down(sd
);
987 return ov2680_init(sd
);
993 * distance - calculate the distance
998 * Get the gap between resolution and w/h.
999 * res->width/height smaller than w/h wouldn't be considered.
1000 * Returns the value of gap or -1 if fail.
1002 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
1003 static int distance(struct ov2680_resolution
*res
, u32 w
, u32 h
)
1005 unsigned int w_ratio
= (res
->width
<< 13) / w
;
1006 unsigned int h_ratio
;
1011 h_ratio
= (res
->height
<< 13) / h
;
1014 match
= abs(((w_ratio
<< 13) / h_ratio
) - ((int)8192));
1017 if ((w_ratio
< (int)8192) || (h_ratio
< (int)8192) ||
1018 (match
> LARGEST_ALLOWED_RATIO_MISMATCH
))
1021 return w_ratio
+ h_ratio
;
1024 /* Return the nearest higher resolution index */
1025 static int nearest_resolution_index(int w
, int h
)
1030 int min_dist
= INT_MAX
;
1031 struct ov2680_resolution
*tmp_res
= NULL
;
1033 for (i
= 0; i
< N_RES
; i
++) {
1034 tmp_res
= &ov2680_res
[i
];
1035 dist
= distance(tmp_res
, w
, h
);
1038 if (dist
< min_dist
) {
1047 static int get_resolution_index(int w
, int h
)
1051 for (i
= 0; i
< N_RES
; i
++) {
1052 if (w
!= ov2680_res
[i
].width
)
1054 if (h
!= ov2680_res
[i
].height
)
1063 static int ov2680_set_fmt(struct v4l2_subdev
*sd
,
1064 struct v4l2_subdev_pad_config
*cfg
,
1065 struct v4l2_subdev_format
*format
)
1067 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1068 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1069 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1070 struct camera_mipi_info
*ov2680_info
= NULL
;
1073 dev_dbg(&client
->dev
, "+++++ov2680_s_mbus_fmt+++++l\n");
1080 ov2680_info
= v4l2_get_subdev_hostdata(sd
);
1084 mutex_lock(&dev
->input_lock
);
1085 idx
= nearest_resolution_index(fmt
->width
, fmt
->height
);
1087 /* return the largest resolution */
1088 fmt
->width
= ov2680_res
[N_RES
- 1].width
;
1089 fmt
->height
= ov2680_res
[N_RES
- 1].height
;
1091 fmt
->width
= ov2680_res
[idx
].width
;
1092 fmt
->height
= ov2680_res
[idx
].height
;
1094 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1095 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1096 cfg
->try_fmt
= *fmt
;
1097 mutex_unlock(&dev
->input_lock
);
1100 dev
->fmt_idx
= get_resolution_index(fmt
->width
, fmt
->height
);
1101 dev_dbg(&client
->dev
, "+++++get_resolution_index=%d+++++l\n",
1103 if (dev
->fmt_idx
== -1) {
1104 dev_err(&client
->dev
, "get resolution fail\n");
1105 mutex_unlock(&dev
->input_lock
);
1108 v4l2_info(client
, "__s_mbus_fmt i=%d, w=%d, h=%d\n", dev
->fmt_idx
,
1109 fmt
->width
, fmt
->height
);
1110 dev_dbg(&client
->dev
, "__s_mbus_fmt i=%d, w=%d, h=%d\n",
1111 dev
->fmt_idx
, fmt
->width
, fmt
->height
);
1113 ret
= ov2680_write_reg_array(client
, ov2680_res
[dev
->fmt_idx
].regs
);
1115 dev_err(&client
->dev
, "ov2680 write resolution register err\n");
1117 ret
= ov2680_get_intg_factor(client
, ov2680_info
,
1118 &ov2680_res
[dev
->fmt_idx
]);
1120 dev_err(&client
->dev
, "failed to get integration_factor\n");
1124 /*recall flip functions to avoid flip registers
1125 * were overridden by default setting
1128 ov2680_h_flip(sd
, h_flag
);
1130 ov2680_v_flip(sd
, v_flag
);
1132 v4l2_info(client
, "\n%s idx %d \n", __func__
, dev
->fmt_idx
);
1134 /*ret = startup(sd);
1136 * dev_err(&client->dev, "ov2680 startup err\n");
1139 mutex_unlock(&dev
->input_lock
);
1143 static int ov2680_get_fmt(struct v4l2_subdev
*sd
,
1144 struct v4l2_subdev_pad_config
*cfg
,
1145 struct v4l2_subdev_format
*format
)
1147 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1148 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1156 fmt
->width
= ov2680_res
[dev
->fmt_idx
].width
;
1157 fmt
->height
= ov2680_res
[dev
->fmt_idx
].height
;
1158 fmt
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1163 static int ov2680_detect(struct i2c_client
*client
)
1165 struct i2c_adapter
*adapter
= client
->adapter
;
1171 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
1174 ret
= ov2680_read_reg(client
, OV2680_8BIT
,
1175 OV2680_SC_CMMN_CHIP_ID_H
, &high
);
1177 dev_err(&client
->dev
, "sensor_id_high = 0x%x\n", high
);
1180 ret
= ov2680_read_reg(client
, OV2680_8BIT
,
1181 OV2680_SC_CMMN_CHIP_ID_L
, &low
);
1182 id
= ((((u16
) high
) << 8) | (u16
) low
);
1184 if (id
!= OV2680_ID
) {
1185 dev_err(&client
->dev
, "sensor ID error 0x%x\n", id
);
1189 ret
= ov2680_read_reg(client
, OV2680_8BIT
,
1190 OV2680_SC_CMMN_SUB_ID
, &high
);
1191 revision
= (u8
) high
& 0x0f;
1193 dev_info(&client
->dev
, "sensor_revision id = 0x%x\n", id
);
1198 static int ov2680_s_stream(struct v4l2_subdev
*sd
, int enable
)
1200 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1201 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1204 mutex_lock(&dev
->input_lock
);
1206 dev_dbg(&client
->dev
, "ov2680_s_stream one \n");
1208 dev_dbg(&client
->dev
, "ov2680_s_stream off \n");
1210 ret
= ov2680_write_reg(client
, OV2680_8BIT
, OV2680_SW_STREAM
,
1211 enable
? OV2680_START_STREAMING
:
1212 OV2680_STOP_STREAMING
);
1214 /* restore settings */
1215 ov2680_res
= ov2680_res_preview
;
1216 N_RES
= N_RES_PREVIEW
;
1219 //otp valid at stream on state
1220 //if(!dev->otp_data)
1221 // dev->otp_data = ov2680_otp_read(sd);
1223 mutex_unlock(&dev
->input_lock
);
1229 static int ov2680_s_config(struct v4l2_subdev
*sd
,
1230 int irq
, void *platform_data
)
1232 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1233 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1239 dev
->platform_data
=
1240 (struct camera_sensor_platform_data
*)platform_data
;
1242 mutex_lock(&dev
->input_lock
);
1243 /* power off the module, then power on it in future
1244 * as first power on by board may not fulfill the
1245 * power on sequqence needed by the module
1247 ret
= power_down(sd
);
1249 dev_err(&client
->dev
, "ov2680 power-off err.\n");
1250 goto fail_power_off
;
1255 dev_err(&client
->dev
, "ov2680 power-up err.\n");
1259 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
1263 /* config & detect sensor */
1264 ret
= ov2680_detect(client
);
1266 dev_err(&client
->dev
, "ov2680_detect err s_config.\n");
1270 /* turn off sensor, after probed */
1271 ret
= power_down(sd
);
1273 dev_err(&client
->dev
, "ov2680 power-off err.\n");
1276 mutex_unlock(&dev
->input_lock
);
1281 dev
->platform_data
->csi_cfg(sd
, 0);
1284 dev_err(&client
->dev
, "sensor power-gating failed\n");
1286 mutex_unlock(&dev
->input_lock
);
1290 static int ov2680_g_parm(struct v4l2_subdev
*sd
,
1291 struct v4l2_streamparm
*param
)
1293 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1294 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1299 if (param
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1300 dev_err(&client
->dev
, "unsupported buffer type.\n");
1304 memset(param
, 0, sizeof(*param
));
1305 param
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1307 if (dev
->fmt_idx
>= 0 && dev
->fmt_idx
< N_RES
) {
1308 param
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1309 param
->parm
.capture
.timeperframe
.numerator
= 1;
1310 param
->parm
.capture
.capturemode
= dev
->run_mode
;
1311 param
->parm
.capture
.timeperframe
.denominator
=
1312 ov2680_res
[dev
->fmt_idx
].fps
;
1317 static int ov2680_s_parm(struct v4l2_subdev
*sd
,
1318 struct v4l2_streamparm
*param
)
1320 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1321 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1322 dev
->run_mode
= param
->parm
.capture
.capturemode
;
1324 v4l2_info(client
, "\n%s:run_mode :%x\n", __func__
, dev
->run_mode
);
1326 mutex_lock(&dev
->input_lock
);
1327 switch (dev
->run_mode
) {
1329 ov2680_res
= ov2680_res_video
;
1330 N_RES
= N_RES_VIDEO
;
1332 case CI_MODE_STILL_CAPTURE
:
1333 ov2680_res
= ov2680_res_still
;
1334 N_RES
= N_RES_STILL
;
1337 ov2680_res
= ov2680_res_preview
;
1338 N_RES
= N_RES_PREVIEW
;
1340 mutex_unlock(&dev
->input_lock
);
1344 static int ov2680_g_frame_interval(struct v4l2_subdev
*sd
,
1345 struct v4l2_subdev_frame_interval
*interval
)
1347 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1349 interval
->interval
.numerator
= 1;
1350 interval
->interval
.denominator
= ov2680_res
[dev
->fmt_idx
].fps
;
1355 static int ov2680_enum_mbus_code(struct v4l2_subdev
*sd
,
1356 struct v4l2_subdev_pad_config
*cfg
,
1357 struct v4l2_subdev_mbus_code_enum
*code
)
1359 if (code
->index
>= MAX_FMTS
)
1362 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1366 static int ov2680_enum_frame_size(struct v4l2_subdev
*sd
,
1367 struct v4l2_subdev_pad_config
*cfg
,
1368 struct v4l2_subdev_frame_size_enum
*fse
)
1370 int index
= fse
->index
;
1375 fse
->min_width
= ov2680_res
[index
].width
;
1376 fse
->min_height
= ov2680_res
[index
].height
;
1377 fse
->max_width
= ov2680_res
[index
].width
;
1378 fse
->max_height
= ov2680_res
[index
].height
;
1384 static int ov2680_g_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
1386 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1388 mutex_lock(&dev
->input_lock
);
1389 *frames
= ov2680_res
[dev
->fmt_idx
].skip_frames
;
1390 mutex_unlock(&dev
->input_lock
);
1395 static const struct v4l2_subdev_video_ops ov2680_video_ops
= {
1396 .s_stream
= ov2680_s_stream
,
1397 .g_parm
= ov2680_g_parm
,
1398 .s_parm
= ov2680_s_parm
,
1399 .g_frame_interval
= ov2680_g_frame_interval
,
1402 static const struct v4l2_subdev_sensor_ops ov2680_sensor_ops
= {
1403 .g_skip_frames
= ov2680_g_skip_frames
,
1406 static const struct v4l2_subdev_core_ops ov2680_core_ops
= {
1407 .s_power
= ov2680_s_power
,
1408 .ioctl
= ov2680_ioctl
,
1411 static const struct v4l2_subdev_pad_ops ov2680_pad_ops
= {
1412 .enum_mbus_code
= ov2680_enum_mbus_code
,
1413 .enum_frame_size
= ov2680_enum_frame_size
,
1414 .get_fmt
= ov2680_get_fmt
,
1415 .set_fmt
= ov2680_set_fmt
,
1418 static const struct v4l2_subdev_ops ov2680_ops
= {
1419 .core
= &ov2680_core_ops
,
1420 .video
= &ov2680_video_ops
,
1421 .pad
= &ov2680_pad_ops
,
1422 .sensor
= &ov2680_sensor_ops
,
1425 static int ov2680_remove(struct i2c_client
*client
)
1427 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1428 struct ov2680_device
*dev
= to_ov2680_sensor(sd
);
1429 dev_dbg(&client
->dev
, "ov2680_remove...\n");
1431 dev
->platform_data
->csi_cfg(sd
, 0);
1433 v4l2_device_unregister_subdev(sd
);
1434 media_entity_cleanup(&dev
->sd
.entity
);
1435 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1441 static int ov2680_probe(struct i2c_client
*client
,
1442 const struct i2c_device_id
*id
)
1444 struct ov2680_device
*dev
;
1449 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1453 mutex_init(&dev
->input_lock
);
1456 v4l2_i2c_subdev_init(&(dev
->sd
), client
, &ov2680_ops
);
1458 if (ACPI_COMPANION(&client
->dev
))
1459 pdata
= gmin_camera_platform_data(&dev
->sd
,
1460 ATOMISP_INPUT_FORMAT_RAW_10
,
1461 atomisp_bayer_order_bggr
);
1463 pdata
= client
->dev
.platform_data
;
1470 ret
= ov2680_s_config(&dev
->sd
, client
->irq
, pdata
);
1474 ret
= atomisp_register_i2c_module(&dev
->sd
, pdata
, RAW_CAMERA
);
1478 dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1479 dev
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1480 dev
->format
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1481 dev
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1483 v4l2_ctrl_handler_init(&dev
->ctrl_handler
,
1484 ARRAY_SIZE(ov2680_controls
));
1486 ov2680_remove(client
);
1490 for (i
= 0; i
< ARRAY_SIZE(ov2680_controls
); i
++)
1491 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &ov2680_controls
[i
],
1494 if (dev
->ctrl_handler
.error
) {
1495 ov2680_remove(client
);
1496 return dev
->ctrl_handler
.error
;
1499 /* Use same lock for controls as for everything else. */
1500 dev
->ctrl_handler
.lock
= &dev
->input_lock
;
1501 dev
->sd
.ctrl_handler
= &dev
->ctrl_handler
;
1503 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
1506 ov2680_remove(client
);
1507 dev_dbg(&client
->dev
, "+++ remove ov2680 \n");
1511 dev_dbg(&client
->dev
, "+++ out free \n");
1512 v4l2_device_unregister_subdev(&dev
->sd
);
1517 static const struct acpi_device_id ov2680_acpi_match
[] = {
1522 MODULE_DEVICE_TABLE(acpi
, ov2680_acpi_match
);
1525 MODULE_DEVICE_TABLE(i2c
, ov2680_id
);
1526 static struct i2c_driver ov2680_driver
= {
1528 .owner
= THIS_MODULE
,
1529 .name
= OV2680_NAME
,
1530 .acpi_match_table
= ACPI_PTR(ov2680_acpi_match
),
1533 .probe
= ov2680_probe
,
1534 .remove
= ov2680_remove
,
1535 .id_table
= ov2680_id
,
1538 static int init_ov2680(void)
1540 return i2c_add_driver(&ov2680_driver
);
1543 static void exit_ov2680(void)
1546 i2c_del_driver(&ov2680_driver
);
1549 module_init(init_ov2680
);
1550 module_exit(exit_ov2680
);
1552 MODULE_AUTHOR("Jacky Wang <Jacky_wang@ovt.com>");
1553 MODULE_DESCRIPTION("A low-level driver for OmniVision 2680 sensors");
1554 MODULE_LICENSE("GPL");