1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Support for OmniVision OV2680 5M camera sensor.
5 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 #include <linux/kernel.h>
22 #include <linux/types.h>
23 #include <linux/i2c.h>
24 #include <linux/delay.h>
25 #include <linux/videodev2.h>
26 #include <linux/spinlock.h>
27 #include <media/v4l2-subdev.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ctrls.h>
30 #include <linux/v4l2-mediabus.h>
31 #include <media/media-entity.h>
33 #include "../include/linux/atomisp_platform.h"
35 /* Defines for register writes and register array processing */
36 #define I2C_MSG_LENGTH 0x2
37 #define I2C_RETRY_COUNT 5
39 #define OV2680_FOCAL_LENGTH_NUM 334 /*3.34mm*/
40 #define OV2680_FOCAL_LENGTH_DEM 100
41 #define OV2680_F_NUMBER_DEFAULT_NUM 24
42 #define OV2680_F_NUMBER_DEM 10
44 #define OV2680_BIN_FACTOR_MAX 4
48 /* sensor_mode_data read_mode adaptation */
49 #define OV2680_READ_MODE_BINNING_ON 0x0400
50 #define OV2680_READ_MODE_BINNING_OFF 0x00
51 #define OV2680_INTEGRATION_TIME_MARGIN 8
53 #define OV2680_MAX_EXPOSURE_VALUE 0xFFF1
54 #define OV2680_MAX_GAIN_VALUE 0xFF
57 * focal length bits definition:
58 * bits 31-16: numerator, bits 15-0: denominator
60 #define OV2680_FOCAL_LENGTH_DEFAULT 0x1B70064
63 * current f-number bits definition:
64 * bits 31-16: numerator, bits 15-0: denominator
66 #define OV2680_F_NUMBER_DEFAULT 0x18000a
69 * f-number range bits definition:
70 * bits 31-24: max f-number numerator
71 * bits 23-16: max f-number denominator
72 * bits 15-8: min f-number numerator
73 * bits 7-0: min f-number denominator
75 #define OV2680_F_NUMBER_RANGE 0x180a180a
76 #define OV2680_ID 0x2680
78 #define OV2680_FINE_INTG_TIME_MIN 0
79 #define OV2680_FINE_INTG_TIME_MAX_MARGIN 0
80 #define OV2680_COARSE_INTG_TIME_MIN 1
81 #define OV2680_COARSE_INTG_TIME_MAX_MARGIN 6
84 * OV2680 System control registers
86 #define OV2680_SW_SLEEP 0x0100
87 #define OV2680_SW_RESET 0x0103
88 #define OV2680_SW_STREAM 0x0100
90 #define OV2680_SC_CMMN_CHIP_ID_H 0x300A
91 #define OV2680_SC_CMMN_CHIP_ID_L 0x300B
92 #define OV2680_SC_CMMN_SCCB_ID 0x302B /* 0x300C*/
93 #define OV2680_SC_CMMN_SUB_ID 0x302A /* process, version*/
95 #define OV2680_GROUP_ACCESS 0x3208 /*Bit[7:4] Group control, Bit[3:0] Group ID*/
97 #define OV2680_EXPOSURE_H 0x3500 /*Bit[3:0] Bit[19:16] of exposure, remaining 16 bits lies in Reg0x3501&Reg0x3502*/
98 #define OV2680_EXPOSURE_M 0x3501
99 #define OV2680_EXPOSURE_L 0x3502
100 #define OV2680_AGC_H 0x350A /*Bit[1:0] means Bit[9:8] of gain*/
101 #define OV2680_AGC_L 0x350B /*Bit[7:0] of gain*/
103 #define OV2680_HORIZONTAL_START_H 0x3800 /*Bit[11:8]*/
104 #define OV2680_HORIZONTAL_START_L 0x3801 /*Bit[7:0]*/
105 #define OV2680_VERTICAL_START_H 0x3802 /*Bit[11:8]*/
106 #define OV2680_VERTICAL_START_L 0x3803 /*Bit[7:0]*/
107 #define OV2680_HORIZONTAL_END_H 0x3804 /*Bit[11:8]*/
108 #define OV2680_HORIZONTAL_END_L 0x3805 /*Bit[7:0]*/
109 #define OV2680_VERTICAL_END_H 0x3806 /*Bit[11:8]*/
110 #define OV2680_VERTICAL_END_L 0x3807 /*Bit[7:0]*/
111 #define OV2680_HORIZONTAL_OUTPUT_SIZE_H 0x3808 /*Bit[3:0]*/
112 #define OV2680_HORIZONTAL_OUTPUT_SIZE_L 0x3809 /*Bit[7:0]*/
113 #define OV2680_VERTICAL_OUTPUT_SIZE_H 0x380a /*Bit[3:0]*/
114 #define OV2680_VERTICAL_OUTPUT_SIZE_L 0x380b /*Bit[7:0]*/
115 #define OV2680_TIMING_HTS_H 0x380C /*High 8-bit, and low 8-bit HTS address is 0x380d*/
116 #define OV2680_TIMING_HTS_L 0x380D /*High 8-bit, and low 8-bit HTS address is 0x380d*/
117 #define OV2680_TIMING_VTS_H 0x380e /*High 8-bit, and low 8-bit HTS address is 0x380f*/
118 #define OV2680_TIMING_VTS_L 0x380f /*High 8-bit, and low 8-bit HTS address is 0x380f*/
119 #define OV2680_FRAME_OFF_NUM 0x4202
122 #define OV2680_FLIP_REG 0x3820
123 #define OV2680_MIRROR_REG 0x3821
124 #define OV2680_FLIP_BIT 1
125 #define OV2680_MIRROR_BIT 2
126 #define OV2680_FLIP_MIRROR_BIT_ENABLE 4
128 #define OV2680_MWB_RED_GAIN_H 0x5004/*0x3400*/
129 #define OV2680_MWB_GREEN_GAIN_H 0x5006/*0x3402*/
130 #define OV2680_MWB_BLUE_GAIN_H 0x5008/*0x3404*/
131 #define OV2680_MWB_GAIN_MAX 0x0fff
133 #define OV2680_START_STREAMING 0x01
134 #define OV2680_STOP_STREAMING 0x00
136 #define OV2680_INVALID_CONFIG 0xffffffff
143 struct ov2680_resolution {
144 const struct ov2680_reg *regs;
158 struct ov2680_format {
161 struct ov2680_reg *regs;
165 * ov2680 device structure.
167 struct ov2680_device {
168 struct v4l2_subdev sd;
169 struct media_pad pad;
170 struct mutex input_lock;
171 struct v4l2_ctrl_handler ctrl_handler;
172 struct ov2680_resolution *res;
173 struct camera_sensor_platform_data *platform_data;
181 * struct ov2680_reg - MI sensor register format
182 * @type: type of the register
183 * @reg: 16-bit offset to register
184 * @val: 8/16/32-bit register value
186 * Define a structure for sensor register initialization values
190 u32 val; /* @set value for read/mod/write, @mask */
193 #define to_ov2680_sensor(x) container_of(x, struct ov2680_device, sd)
195 #define OV2680_MAX_WRITE_BUF_SIZE 30
197 struct ov2680_write_buffer {
199 u8 data[OV2680_MAX_WRITE_BUF_SIZE];
202 struct ov2680_write_ctrl {
204 struct ov2680_write_buffer buffer;
207 static struct ov2680_reg const ov2680_global_setting[] = {
248 {0x5004, 0x04},//manual awb 1x
255 {0x3701, 0x64}, //add on 14/05/13
256 {0x3784, 0x0c}, //based OV2680_R1A_AM10.ovt add on 14/06/13
257 {0x5780, 0x3e}, //based OV2680_R1A_AM10.ovt,Adjust DPC setting (57xx) on 14/06/13
277 {0x5794, 0x03}, //based OV2680_R1A_AM10.ovt,Adjust DPC setting (57xx) on 14/06/13
278 {0x0100, 0x00}, //stream off
284 * 176x144 30fps VBlanking 1lane 10Bit (binning)
286 static struct ov2680_reg const ov2680_QCIF_30fps[] = {
313 {0x5708, 0x00}, //add for full size flip off and mirror off 2014/09/11
325 * 352x288 30fps VBlanking 1lane 10Bit (binning)
327 static struct ov2680_reg const ov2680_CIF_30fps[] = {
352 {0x5708, 0x00}, //add for full size flip off and mirror off 2014/09/11
364 * 336x256 30fps VBlanking 1lane 10Bit (binning)
366 static struct ov2680_reg const ov2680_QVGA_30fps[] = {
391 {0x5708, 0x00}, //add for full size flip off and mirror off 2014/09/11
403 * 656x496 30fps VBlanking 1lane 10Bit (binning)
405 static struct ov2680_reg const ov2680_656x496_30fps[] = {
430 {0x5708, 0x00}, //add for full size flip off and mirror off 2014/09/11
442 * 720x592 30fps VBlanking 1lane 10Bit (binning)
444 static struct ov2680_reg const ov2680_720x592_30fps[] = {
447 {0x3801, 0x00}, // X_ADDR_START;
449 {0x3803, 0x00}, // Y_ADDR_START;
451 {0x3805, 0xaf}, // X_ADDR_END;
453 {0x3807, 0xaf}, // Y_ADDR_END;
455 {0x3809, 0xd0}, // X_OUTPUT_SIZE;
457 {0x380b, 0x50}, // Y_OUTPUT_SIZE;
459 {0x380d, 0xac}, // HTS;
470 {0x5705, 0xd0}, // X_WIN;
472 {0x5707, 0x50}, // Y_WIN;
473 {0x3820, 0xc2}, // FLIP_FORMAT;
474 {0x3821, 0x01}, // MIRROR_FORMAT;
475 {0x5090, 0x00}, // PRE ISP CTRL16, default value is 0x0C;
476 // BIT[3]: Mirror order, BG or GB;
477 // BIT[2]: Flip order, BR or RB;
483 * 800x600 30fps VBlanking 1lane 10Bit (binning)
485 static struct ov2680_reg const ov2680_800x600_30fps[] = {
522 * 720p=1280*720 30fps VBlanking 1lane 10Bit (no-Scaling)
524 static struct ov2680_reg const ov2680_720p_30fps[] = {
549 {0x5708, 0x00}, //add for full size flip off and mirror off 2014/09/11
561 * 1296x976 30fps VBlanking 1lane 10Bit(no-scaling)
563 static struct ov2680_reg const ov2680_1296x976_30fps[] = {
588 {0x5708, 0x00}, //add for full size flip off and mirror off 2014/09/11
594 {0x3821, 0x00}, //mirror/flip
600 * 1456*1096 30fps VBlanking 1lane 10bit(no-scaling)
602 static struct ov2680_reg const ov2680_1456x1096_30fps[] = {
627 {0x5708, 0x00}, //add for full size flip off and mirror off 2014/09/11
639 *1616x916 30fps VBlanking 1lane 10bit
642 static struct ov2680_reg const ov2680_1616x916_30fps[] = {
667 {0x5708, 0x01}, //add for full size flip off and mirror off 2014/09/11
679 * 1616x1082 30fps VBlanking 1lane 10Bit
681 static struct ov2680_reg const ov2680_1616x1082_30fps[] = {
703 {0x5708, 0x01}, //add for full size flip off and mirror off 2014/09/11
718 * 1616x1216 30fps VBlanking 1lane 10Bit
720 static struct ov2680_reg const ov2680_1616x1216_30fps[] = {
731 {0x3809, 0x50},//50},//4line for mirror and flip
733 {0x380b, 0xc0},//c0},
745 {0x5708, 0x01}, //add for full size flip off and mirror off 2014/09/11
756 static struct ov2680_resolution ov2680_res_preview[] = {
762 .pixels_per_line = 1698,//1704,
763 .lines_per_frame = 1294,
768 .regs = ov2680_1616x1216_30fps,
775 .pixels_per_line = 1698,//1704,
776 .lines_per_frame = 1294,
781 .regs = ov2680_1616x1082_30fps,
788 .pixels_per_line = 1698,//1704,
789 .lines_per_frame = 1294,
794 .regs = ov2680_1616x916_30fps,
801 .pixels_per_line = 1698,//1704,
802 .lines_per_frame = 1294,
807 .regs = ov2680_1456x1096_30fps,
814 .pixels_per_line = 1698,//1704,
815 .lines_per_frame = 1294,
820 .regs = ov2680_1296x976_30fps,
827 .pixels_per_line = 1698,//1704,
828 .lines_per_frame = 1294,
833 .regs = ov2680_720p_30fps,
840 .pixels_per_line = 1698,//1704,
841 .lines_per_frame = 1294,
846 .regs = ov2680_800x600_30fps,
853 .pixels_per_line = 1698,//1704,
854 .lines_per_frame = 1294,
859 .regs = ov2680_720x592_30fps,
866 .pixels_per_line = 1698,//1704,
867 .lines_per_frame = 1294,
872 .regs = ov2680_656x496_30fps,
879 .pixels_per_line = 1698,//1704,
880 .lines_per_frame = 1294,
885 .regs = ov2680_QVGA_30fps,
892 .pixels_per_line = 1698,//1704,
893 .lines_per_frame = 1294,
898 .regs = ov2680_CIF_30fps,
905 .pixels_per_line = 1698,//1704,
906 .lines_per_frame = 1294,
911 .regs = ov2680_QCIF_30fps,
915 #define N_RES_PREVIEW (ARRAY_SIZE(ov2680_res_preview))