1 /* 2 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved. 3 * Copyright (C) 2014-2017 Mentor Graphics Inc. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 */ 10 11 #include <linux/clk.h> 12 #include <linux/clk-provider.h> 13 #include <linux/clkdev.h> 14 #include <linux/ctype.h> 15 #include <linux/delay.h> 16 #include <linux/device.h> 17 #include <linux/gpio/consumer.h> 18 #include <linux/i2c.h> 19 #include <linux/init.h> 20 #include <linux/module.h> 21 #include <linux/of_device.h> 22 #include <linux/regulator/consumer.h> 23 #include <linux/slab.h> 24 #include <linux/types.h> 25 #include <media/v4l2-async.h> 26 #include <media/v4l2-ctrls.h> 27 #include <media/v4l2-device.h> 28 #include <media/v4l2-fwnode.h> 29 #include <media/v4l2-subdev.h> 30 31 /* min/typical/max system clock (xclk) frequencies */ 32 #define OV5640_XCLK_MIN 6000000 33 #define OV5640_XCLK_MAX 54000000 34 35 #define OV5640_DEFAULT_SLAVE_ID 0x3c 36 37 #define OV5640_REG_SYS_RESET02 0x3002 38 #define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006 39 #define OV5640_REG_SYS_CTRL0 0x3008 40 #define OV5640_REG_CHIP_ID 0x300a 41 #define OV5640_REG_IO_MIPI_CTRL00 0x300e 42 #define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017 43 #define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018 44 #define OV5640_REG_PAD_OUTPUT00 0x3019 45 #define OV5640_REG_SYSTEM_CONTROL1 0x302e 46 #define OV5640_REG_SC_PLL_CTRL0 0x3034 47 #define OV5640_REG_SC_PLL_CTRL1 0x3035 48 #define OV5640_REG_SC_PLL_CTRL2 0x3036 49 #define OV5640_REG_SC_PLL_CTRL3 0x3037 50 #define OV5640_REG_SLAVE_ID 0x3100 51 #define OV5640_REG_SCCB_SYS_CTRL1 0x3103 52 #define OV5640_REG_SYS_ROOT_DIVIDER 0x3108 53 #define OV5640_REG_AWB_R_GAIN 0x3400 54 #define OV5640_REG_AWB_G_GAIN 0x3402 55 #define OV5640_REG_AWB_B_GAIN 0x3404 56 #define OV5640_REG_AWB_MANUAL_CTRL 0x3406 57 #define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500 58 #define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501 59 #define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502 60 #define OV5640_REG_AEC_PK_MANUAL 0x3503 61 #define OV5640_REG_AEC_PK_REAL_GAIN 0x350a 62 #define OV5640_REG_AEC_PK_VTS 0x350c 63 #define OV5640_REG_TIMING_DVPHO 0x3808 64 #define OV5640_REG_TIMING_DVPVO 0x380a 65 #define OV5640_REG_TIMING_HTS 0x380c 66 #define OV5640_REG_TIMING_VTS 0x380e 67 #define OV5640_REG_TIMING_TC_REG20 0x3820 68 #define OV5640_REG_TIMING_TC_REG21 0x3821 69 #define OV5640_REG_AEC_CTRL00 0x3a00 70 #define OV5640_REG_AEC_B50_STEP 0x3a08 71 #define OV5640_REG_AEC_B60_STEP 0x3a0a 72 #define OV5640_REG_AEC_CTRL0D 0x3a0d 73 #define OV5640_REG_AEC_CTRL0E 0x3a0e 74 #define OV5640_REG_AEC_CTRL0F 0x3a0f 75 #define OV5640_REG_AEC_CTRL10 0x3a10 76 #define OV5640_REG_AEC_CTRL11 0x3a11 77 #define OV5640_REG_AEC_CTRL1B 0x3a1b 78 #define OV5640_REG_AEC_CTRL1E 0x3a1e 79 #define OV5640_REG_AEC_CTRL1F 0x3a1f 80 #define OV5640_REG_HZ5060_CTRL00 0x3c00 81 #define OV5640_REG_HZ5060_CTRL01 0x3c01 82 #define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c 83 #define OV5640_REG_FRAME_CTRL01 0x4202 84 #define OV5640_REG_FORMAT_CONTROL00 0x4300 85 #define OV5640_REG_POLARITY_CTRL00 0x4740 86 #define OV5640_REG_MIPI_CTRL00 0x4800 87 #define OV5640_REG_DEBUG_MODE 0x4814 88 #define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f 89 #define OV5640_REG_PRE_ISP_TEST_SET1 0x503d 90 #define OV5640_REG_SDE_CTRL0 0x5580 91 #define OV5640_REG_SDE_CTRL1 0x5581 92 #define OV5640_REG_SDE_CTRL3 0x5583 93 #define OV5640_REG_SDE_CTRL4 0x5584 94 #define OV5640_REG_SDE_CTRL5 0x5585 95 #define OV5640_REG_AVG_READOUT 0x56a1 96 97 #define OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT 1 98 #define OV5640_SCLK_ROOT_DIVIDER_DEFAULT 2 99 100 enum ov5640_mode_id { 101 OV5640_MODE_QCIF_176_144 = 0, 102 OV5640_MODE_QVGA_320_240, 103 OV5640_MODE_VGA_640_480, 104 OV5640_MODE_NTSC_720_480, 105 OV5640_MODE_PAL_720_576, 106 OV5640_MODE_XGA_1024_768, 107 OV5640_MODE_720P_1280_720, 108 OV5640_MODE_1080P_1920_1080, 109 OV5640_MODE_QSXGA_2592_1944, 110 OV5640_NUM_MODES, 111 }; 112 113 enum ov5640_frame_rate { 114 OV5640_15_FPS = 0, 115 OV5640_30_FPS, 116 OV5640_NUM_FRAMERATES, 117 }; 118 119 struct ov5640_pixfmt { 120 u32 code; 121 u32 colorspace; 122 }; 123 124 static const struct ov5640_pixfmt ov5640_formats[] = { 125 { MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, }, 126 { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, }, 127 { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, }, 128 { MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, }, 129 { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, }, 130 }; 131 132 /* 133 * FIXME: remove this when a subdev API becomes available 134 * to set the MIPI CSI-2 virtual channel. 135 */ 136 static unsigned int virtual_channel; 137 module_param(virtual_channel, uint, 0444); 138 MODULE_PARM_DESC(virtual_channel, 139 "MIPI CSI-2 virtual channel (0..3), default 0"); 140 141 static const int ov5640_framerates[] = { 142 [OV5640_15_FPS] = 15, 143 [OV5640_30_FPS] = 30, 144 }; 145 146 /* regulator supplies */ 147 static const char * const ov5640_supply_name[] = { 148 "DOVDD", /* Digital I/O (1.8V) supply */ 149 "DVDD", /* Digital Core (1.5V) supply */ 150 "AVDD", /* Analog (2.8V) supply */ 151 }; 152 153 #define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name) 154 155 /* 156 * Image size under 1280 * 960 are SUBSAMPLING 157 * Image size upper 1280 * 960 are SCALING 158 */ 159 enum ov5640_downsize_mode { 160 SUBSAMPLING, 161 SCALING, 162 }; 163 164 struct reg_value { 165 u16 reg_addr; 166 u8 val; 167 u8 mask; 168 u32 delay_ms; 169 }; 170 171 struct ov5640_mode_info { 172 enum ov5640_mode_id id; 173 enum ov5640_downsize_mode dn_mode; 174 u32 hact; 175 u32 htot; 176 u32 vact; 177 u32 vtot; 178 const struct reg_value *reg_data; 179 u32 reg_data_size; 180 }; 181 182 struct ov5640_ctrls { 183 struct v4l2_ctrl_handler handler; 184 struct { 185 struct v4l2_ctrl *auto_exp; 186 struct v4l2_ctrl *exposure; 187 }; 188 struct { 189 struct v4l2_ctrl *auto_wb; 190 struct v4l2_ctrl *blue_balance; 191 struct v4l2_ctrl *red_balance; 192 }; 193 struct { 194 struct v4l2_ctrl *auto_gain; 195 struct v4l2_ctrl *gain; 196 }; 197 struct v4l2_ctrl *brightness; 198 struct v4l2_ctrl *light_freq; 199 struct v4l2_ctrl *saturation; 200 struct v4l2_ctrl *contrast; 201 struct v4l2_ctrl *hue; 202 struct v4l2_ctrl *test_pattern; 203 struct v4l2_ctrl *hflip; 204 struct v4l2_ctrl *vflip; 205 }; 206 207 struct ov5640_dev { 208 struct i2c_client *i2c_client; 209 struct v4l2_subdev sd; 210 struct media_pad pad; 211 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */ 212 struct clk *xclk; /* system clock to OV5640 */ 213 u32 xclk_freq; 214 215 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES]; 216 struct gpio_desc *reset_gpio; 217 struct gpio_desc *pwdn_gpio; 218 bool upside_down; 219 220 /* lock to protect all members below */ 221 struct mutex lock; 222 223 int power_count; 224 225 struct v4l2_mbus_framefmt fmt; 226 bool pending_fmt_change; 227 228 const struct ov5640_mode_info *current_mode; 229 enum ov5640_frame_rate current_fr; 230 struct v4l2_fract frame_interval; 231 232 struct ov5640_ctrls ctrls; 233 234 u32 prev_sysclk, prev_hts; 235 u32 ae_low, ae_high, ae_target; 236 237 bool pending_mode_change; 238 bool streaming; 239 }; 240 241 static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd) 242 { 243 return container_of(sd, struct ov5640_dev, sd); 244 } 245 246 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) 247 { 248 return &container_of(ctrl->handler, struct ov5640_dev, 249 ctrls.handler)->sd; 250 } 251 252 /* 253 * FIXME: all of these register tables are likely filled with 254 * entries that set the register to their power-on default values, 255 * and which are otherwise not touched by this driver. Those entries 256 * should be identified and removed to speed register load time 257 * over i2c. 258 */ 259 /* YUV422 UYVY VGA@30fps */ 260 static const struct reg_value ov5640_init_setting_30fps_VGA[] = { 261 {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0}, 262 {0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0}, 263 {0x3034, 0x18, 0, 0}, {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, 264 {0x3037, 0x13, 0, 0}, {0x3630, 0x36, 0, 0}, 265 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0}, 266 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0}, 267 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0}, 268 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0}, 269 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0}, 270 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0}, 271 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0}, 272 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0}, 273 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0}, 274 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0}, 275 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0}, 276 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 277 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0}, 278 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 279 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 280 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 281 {0x3810, 0x00, 0, 0}, 282 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 283 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 284 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 285 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 286 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 287 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 288 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0}, 289 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0}, 290 {0x300e, 0x45, 0, 0}, {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0}, 291 {0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0}, 292 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 293 {0x4837, 0x0a, 0, 0}, {0x4800, 0x04, 0, 0}, {0x3824, 0x02, 0, 0}, 294 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0}, 295 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0}, 296 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0}, 297 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0}, 298 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0}, 299 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0}, 300 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0}, 301 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0}, 302 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0}, 303 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0}, 304 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0}, 305 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0}, 306 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0}, 307 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0}, 308 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0}, 309 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0}, 310 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0}, 311 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0}, 312 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0}, 313 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0}, 314 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0}, 315 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0}, 316 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0}, 317 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0}, 318 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0}, 319 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0}, 320 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0}, 321 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0}, 322 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0}, 323 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0}, 324 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0}, 325 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0}, 326 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0}, 327 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0}, 328 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0}, 329 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0}, 330 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0}, 331 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0}, 332 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0}, 333 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0}, 334 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0}, 335 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0}, 336 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0}, 337 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0}, 338 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0}, 339 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0}, 340 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0}, 341 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0}, 342 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0}, 343 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300}, 344 }; 345 346 static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = { 347 {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 348 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 349 {0x3814, 0x31, 0, 0}, 350 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 351 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 352 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 353 {0x3810, 0x00, 0, 0}, 354 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 355 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 356 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 357 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0}, 358 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 359 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 360 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 361 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 362 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0}, 363 }; 364 365 static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = { 366 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 367 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 368 {0x3814, 0x31, 0, 0}, 369 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 370 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 371 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 372 {0x3810, 0x00, 0, 0}, 373 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 374 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 375 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 376 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 377 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 378 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 379 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 380 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 381 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 382 }; 383 384 static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = { 385 {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 386 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 387 {0x3814, 0x31, 0, 0}, 388 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 389 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 390 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 391 {0x3810, 0x00, 0, 0}, 392 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 393 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 394 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 395 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0}, 396 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 397 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 398 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 399 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 400 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0}, 401 {0x3035, 0x12, 0, 0}, 402 }; 403 404 static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = { 405 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 406 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 407 {0x3814, 0x31, 0, 0}, 408 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 409 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 410 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 411 {0x3810, 0x00, 0, 0}, 412 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 413 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 414 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 415 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 416 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 417 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 418 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 419 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 420 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 421 }; 422 423 static const struct reg_value ov5640_setting_30fps_QVGA_320_240[] = { 424 {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 425 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 426 {0x3814, 0x31, 0, 0}, 427 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 428 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 429 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 430 {0x3810, 0x00, 0, 0}, 431 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 432 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 433 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 434 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 435 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 436 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 437 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 438 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 439 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 440 }; 441 442 static const struct reg_value ov5640_setting_15fps_QVGA_320_240[] = { 443 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 444 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 445 {0x3814, 0x31, 0, 0}, 446 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 447 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 448 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 449 {0x3810, 0x00, 0, 0}, 450 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 451 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 452 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 453 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 454 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 455 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 456 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 457 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 458 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 459 }; 460 461 static const struct reg_value ov5640_setting_30fps_QCIF_176_144[] = { 462 {0x3035, 0x14, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 463 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 464 {0x3814, 0x31, 0, 0}, 465 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 466 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 467 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 468 {0x3810, 0x00, 0, 0}, 469 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 470 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 471 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 472 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 473 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 474 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 475 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 476 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 477 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 478 }; 479 480 static const struct reg_value ov5640_setting_15fps_QCIF_176_144[] = { 481 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 482 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 483 {0x3814, 0x31, 0, 0}, 484 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 485 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 486 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 487 {0x3810, 0x00, 0, 0}, 488 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 489 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 490 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 491 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 492 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 493 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 494 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 495 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 496 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 497 }; 498 499 static const struct reg_value ov5640_setting_30fps_NTSC_720_480[] = { 500 {0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 501 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 502 {0x3814, 0x31, 0, 0}, 503 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 504 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 505 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 506 {0x3810, 0x00, 0, 0}, 507 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0}, 508 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 509 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 510 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 511 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 512 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 513 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 514 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 515 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 516 }; 517 518 static const struct reg_value ov5640_setting_15fps_NTSC_720_480[] = { 519 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 520 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 521 {0x3814, 0x31, 0, 0}, 522 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 523 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 524 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 525 {0x3810, 0x00, 0, 0}, 526 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0}, 527 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 528 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 529 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 530 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 531 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 532 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 533 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 534 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 535 }; 536 537 static const struct reg_value ov5640_setting_30fps_PAL_720_576[] = { 538 {0x3035, 0x12, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 539 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 540 {0x3814, 0x31, 0, 0}, 541 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 542 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 543 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 544 {0x3810, 0x00, 0, 0}, 545 {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 546 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 547 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 548 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 549 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 550 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 551 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 552 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 553 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 554 }; 555 556 static const struct reg_value ov5640_setting_15fps_PAL_720_576[] = { 557 {0x3035, 0x22, 0, 0}, {0x3036, 0x38, 0, 0}, {0x3c07, 0x08, 0, 0}, 558 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 559 {0x3814, 0x31, 0, 0}, 560 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 561 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0}, 562 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0}, 563 {0x3810, 0x00, 0, 0}, 564 {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0}, 565 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 566 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0}, 567 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 568 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 569 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 570 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0}, 571 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 572 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, 573 }; 574 575 static const struct reg_value ov5640_setting_30fps_720P_1280_720[] = { 576 {0x3008, 0x42, 0, 0}, 577 {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, 578 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 579 {0x3814, 0x31, 0, 0}, 580 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 581 {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0}, 582 {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0}, 583 {0x3810, 0x00, 0, 0}, 584 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 585 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 586 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0}, 587 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0}, 588 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0}, 589 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0}, 590 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0}, 591 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, 592 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, {0x4005, 0x1a, 0, 0}, 593 {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0}, 594 }; 595 596 static const struct reg_value ov5640_setting_15fps_720P_1280_720[] = { 597 {0x3035, 0x41, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, 598 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 599 {0x3814, 0x31, 0, 0}, 600 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 601 {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0}, 602 {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0}, 603 {0x3810, 0x00, 0, 0}, 604 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 605 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0}, 606 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0}, 607 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0}, 608 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0}, 609 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0}, 610 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x02, 0, 0}, 611 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, 612 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0}, 613 }; 614 615 static const struct reg_value ov5640_setting_30fps_1080P_1920_1080[] = { 616 {0x3008, 0x42, 0, 0}, 617 {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0}, 618 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 619 {0x3814, 0x11, 0, 0}, 620 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 621 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 622 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 623 {0x3810, 0x00, 0, 0}, 624 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 625 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 626 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 627 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 628 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 629 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 630 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0}, 631 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 632 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x11, 0, 0}, 633 {0x3036, 0x54, 0, 0}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0}, 634 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 635 {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0}, 636 {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0}, 637 {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, 638 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0}, 639 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0}, 640 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0}, 641 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0}, 642 {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0}, 643 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0}, 644 {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, 645 {0x3503, 0, 0, 0}, 646 }; 647 648 static const struct reg_value ov5640_setting_15fps_1080P_1920_1080[] = { 649 {0x3008, 0x42, 0, 0}, 650 {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0}, 651 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 652 {0x3814, 0x11, 0, 0}, 653 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 654 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 655 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 656 {0x3810, 0x00, 0, 0}, 657 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 658 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 659 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 660 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 661 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 662 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 663 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0}, 664 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 665 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0}, {0x3035, 0x21, 0, 0}, 666 {0x3036, 0x54, 0, 1}, {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0}, 667 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 668 {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0}, 669 {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0}, 670 {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0}, 671 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0}, 672 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0}, 673 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0}, 674 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0}, 675 {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0}, 676 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0}, 677 {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3503, 0, 0, 0}, 678 }; 679 680 static const struct reg_value ov5640_setting_15fps_QSXGA_2592_1944[] = { 681 {0x3035, 0x21, 0, 0}, {0x3036, 0x54, 0, 0}, {0x3c07, 0x08, 0, 0}, 682 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0}, 683 {0x3814, 0x11, 0, 0}, 684 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0}, 685 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0}, 686 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0}, 687 {0x3810, 0x00, 0, 0}, 688 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0}, 689 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0}, 690 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0}, 691 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0}, 692 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0}, 693 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0}, 694 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0}, 695 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0}, 696 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70}, 697 }; 698 699 /* power-on sensor init reg table */ 700 static const struct ov5640_mode_info ov5640_mode_init_data = { 701 0, SUBSAMPLING, 640, 1896, 480, 984, 702 ov5640_init_setting_30fps_VGA, 703 ARRAY_SIZE(ov5640_init_setting_30fps_VGA), 704 }; 705 706 static const struct ov5640_mode_info 707 ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = { 708 { 709 {OV5640_MODE_QCIF_176_144, SUBSAMPLING, 710 176, 1896, 144, 984, 711 ov5640_setting_15fps_QCIF_176_144, 712 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)}, 713 {OV5640_MODE_QVGA_320_240, SUBSAMPLING, 714 320, 1896, 240, 984, 715 ov5640_setting_15fps_QVGA_320_240, 716 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)}, 717 {OV5640_MODE_VGA_640_480, SUBSAMPLING, 718 640, 1896, 480, 1080, 719 ov5640_setting_15fps_VGA_640_480, 720 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)}, 721 {OV5640_MODE_NTSC_720_480, SUBSAMPLING, 722 720, 1896, 480, 984, 723 ov5640_setting_15fps_NTSC_720_480, 724 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)}, 725 {OV5640_MODE_PAL_720_576, SUBSAMPLING, 726 720, 1896, 576, 984, 727 ov5640_setting_15fps_PAL_720_576, 728 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)}, 729 {OV5640_MODE_XGA_1024_768, SUBSAMPLING, 730 1024, 1896, 768, 1080, 731 ov5640_setting_15fps_XGA_1024_768, 732 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)}, 733 {OV5640_MODE_720P_1280_720, SUBSAMPLING, 734 1280, 1892, 720, 740, 735 ov5640_setting_15fps_720P_1280_720, 736 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)}, 737 {OV5640_MODE_1080P_1920_1080, SCALING, 738 1920, 2500, 1080, 1120, 739 ov5640_setting_15fps_1080P_1920_1080, 740 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)}, 741 {OV5640_MODE_QSXGA_2592_1944, SCALING, 742 2592, 2844, 1944, 1968, 743 ov5640_setting_15fps_QSXGA_2592_1944, 744 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)}, 745 }, { 746 {OV5640_MODE_QCIF_176_144, SUBSAMPLING, 747 176, 1896, 144, 984, 748 ov5640_setting_30fps_QCIF_176_144, 749 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)}, 750 {OV5640_MODE_QVGA_320_240, SUBSAMPLING, 751 320, 1896, 240, 984, 752 ov5640_setting_30fps_QVGA_320_240, 753 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)}, 754 {OV5640_MODE_VGA_640_480, SUBSAMPLING, 755 640, 1896, 480, 1080, 756 ov5640_setting_30fps_VGA_640_480, 757 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)}, 758 {OV5640_MODE_NTSC_720_480, SUBSAMPLING, 759 720, 1896, 480, 984, 760 ov5640_setting_30fps_NTSC_720_480, 761 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)}, 762 {OV5640_MODE_PAL_720_576, SUBSAMPLING, 763 720, 1896, 576, 984, 764 ov5640_setting_30fps_PAL_720_576, 765 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)}, 766 {OV5640_MODE_XGA_1024_768, SUBSAMPLING, 767 1024, 1896, 768, 1080, 768 ov5640_setting_30fps_XGA_1024_768, 769 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)}, 770 {OV5640_MODE_720P_1280_720, SUBSAMPLING, 771 1280, 1892, 720, 740, 772 ov5640_setting_30fps_720P_1280_720, 773 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)}, 774 {OV5640_MODE_1080P_1920_1080, SCALING, 775 1920, 2500, 1080, 1120, 776 ov5640_setting_30fps_1080P_1920_1080, 777 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)}, 778 {OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, 0, 0, NULL, 0}, 779 }, 780 }; 781 782 static int ov5640_init_slave_id(struct ov5640_dev *sensor) 783 { 784 struct i2c_client *client = sensor->i2c_client; 785 struct i2c_msg msg; 786 u8 buf[3]; 787 int ret; 788 789 if (client->addr == OV5640_DEFAULT_SLAVE_ID) 790 return 0; 791 792 buf[0] = OV5640_REG_SLAVE_ID >> 8; 793 buf[1] = OV5640_REG_SLAVE_ID & 0xff; 794 buf[2] = client->addr << 1; 795 796 msg.addr = OV5640_DEFAULT_SLAVE_ID; 797 msg.flags = 0; 798 msg.buf = buf; 799 msg.len = sizeof(buf); 800 801 ret = i2c_transfer(client->adapter, &msg, 1); 802 if (ret < 0) { 803 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret); 804 return ret; 805 } 806 807 return 0; 808 } 809 810 static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val) 811 { 812 struct i2c_client *client = sensor->i2c_client; 813 struct i2c_msg msg; 814 u8 buf[3]; 815 int ret; 816 817 buf[0] = reg >> 8; 818 buf[1] = reg & 0xff; 819 buf[2] = val; 820 821 msg.addr = client->addr; 822 msg.flags = client->flags; 823 msg.buf = buf; 824 msg.len = sizeof(buf); 825 826 ret = i2c_transfer(client->adapter, &msg, 1); 827 if (ret < 0) { 828 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n", 829 __func__, reg, val); 830 return ret; 831 } 832 833 return 0; 834 } 835 836 static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val) 837 { 838 struct i2c_client *client = sensor->i2c_client; 839 struct i2c_msg msg[2]; 840 u8 buf[2]; 841 int ret; 842 843 buf[0] = reg >> 8; 844 buf[1] = reg & 0xff; 845 846 msg[0].addr = client->addr; 847 msg[0].flags = client->flags; 848 msg[0].buf = buf; 849 msg[0].len = sizeof(buf); 850 851 msg[1].addr = client->addr; 852 msg[1].flags = client->flags | I2C_M_RD; 853 msg[1].buf = buf; 854 msg[1].len = 1; 855 856 ret = i2c_transfer(client->adapter, msg, 2); 857 if (ret < 0) { 858 dev_err(&client->dev, "%s: error: reg=%x\n", 859 __func__, reg); 860 return ret; 861 } 862 863 *val = buf[0]; 864 return 0; 865 } 866 867 static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val) 868 { 869 u8 hi, lo; 870 int ret; 871 872 ret = ov5640_read_reg(sensor, reg, &hi); 873 if (ret) 874 return ret; 875 ret = ov5640_read_reg(sensor, reg + 1, &lo); 876 if (ret) 877 return ret; 878 879 *val = ((u16)hi << 8) | (u16)lo; 880 return 0; 881 } 882 883 static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val) 884 { 885 int ret; 886 887 ret = ov5640_write_reg(sensor, reg, val >> 8); 888 if (ret) 889 return ret; 890 891 return ov5640_write_reg(sensor, reg + 1, val & 0xff); 892 } 893 894 static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg, 895 u8 mask, u8 val) 896 { 897 u8 readval; 898 int ret; 899 900 ret = ov5640_read_reg(sensor, reg, &readval); 901 if (ret) 902 return ret; 903 904 readval &= ~mask; 905 val &= mask; 906 val |= readval; 907 908 return ov5640_write_reg(sensor, reg, val); 909 } 910 911 /* download ov5640 settings to sensor through i2c */ 912 static int ov5640_load_regs(struct ov5640_dev *sensor, 913 const struct ov5640_mode_info *mode) 914 { 915 const struct reg_value *regs = mode->reg_data; 916 unsigned int i; 917 u32 delay_ms; 918 u16 reg_addr; 919 u8 mask, val; 920 int ret = 0; 921 922 for (i = 0; i < mode->reg_data_size; ++i, ++regs) { 923 delay_ms = regs->delay_ms; 924 reg_addr = regs->reg_addr; 925 val = regs->val; 926 mask = regs->mask; 927 928 if (mask) 929 ret = ov5640_mod_reg(sensor, reg_addr, mask, val); 930 else 931 ret = ov5640_write_reg(sensor, reg_addr, val); 932 if (ret) 933 break; 934 935 if (delay_ms) 936 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100); 937 } 938 939 return ret; 940 } 941 942 /* read exposure, in number of line periods */ 943 static int ov5640_get_exposure(struct ov5640_dev *sensor) 944 { 945 int exp, ret; 946 u8 temp; 947 948 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp); 949 if (ret) 950 return ret; 951 exp = ((int)temp & 0x0f) << 16; 952 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp); 953 if (ret) 954 return ret; 955 exp |= ((int)temp << 8); 956 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp); 957 if (ret) 958 return ret; 959 exp |= (int)temp; 960 961 return exp >> 4; 962 } 963 964 /* write exposure, given number of line periods */ 965 static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure) 966 { 967 int ret; 968 969 exposure <<= 4; 970 971 ret = ov5640_write_reg(sensor, 972 OV5640_REG_AEC_PK_EXPOSURE_LO, 973 exposure & 0xff); 974 if (ret) 975 return ret; 976 ret = ov5640_write_reg(sensor, 977 OV5640_REG_AEC_PK_EXPOSURE_MED, 978 (exposure >> 8) & 0xff); 979 if (ret) 980 return ret; 981 return ov5640_write_reg(sensor, 982 OV5640_REG_AEC_PK_EXPOSURE_HI, 983 (exposure >> 16) & 0x0f); 984 } 985 986 static int ov5640_get_gain(struct ov5640_dev *sensor) 987 { 988 u16 gain; 989 int ret; 990 991 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain); 992 if (ret) 993 return ret; 994 995 return gain & 0x3ff; 996 } 997 998 static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on) 999 { 1000 int ret; 1001 unsigned int flags = sensor->ep.bus.parallel.flags; 1002 u8 pclk_pol = 0; 1003 u8 hsync_pol = 0; 1004 u8 vsync_pol = 0; 1005 1006 /* 1007 * Note about parallel port configuration. 1008 * 1009 * When configured in parallel mode, the OV5640 will 1010 * output 10 bits data on DVP data lines [9:0]. 1011 * If only 8 bits data are wanted, the 8 bits data lines 1012 * of the camera interface must be physically connected 1013 * on the DVP data lines [9:2]. 1014 * 1015 * Control lines polarity can be configured through 1016 * devicetree endpoint control lines properties. 1017 * If no endpoint control lines properties are set, 1018 * polarity will be as below: 1019 * - VSYNC: active high 1020 * - HREF: active low 1021 * - PCLK: active low 1022 */ 1023 1024 if (on) { 1025 /* 1026 * reset MIPI PCLK/SERCLK divider 1027 * 1028 * SC PLL CONTRL1 0 1029 * - [3..0]: MIPI PCLK/SERCLK divider 1030 */ 1031 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0); 1032 if (ret) 1033 return ret; 1034 1035 /* 1036 * configure parallel port control lines polarity 1037 * 1038 * POLARITY CTRL0 1039 * - [5]: PCLK polarity (0: active low, 1: active high) 1040 * - [1]: HREF polarity (0: active low, 1: active high) 1041 * - [0]: VSYNC polarity (mismatch here between 1042 * datasheet and hardware, 0 is active high 1043 * and 1 is active low...) 1044 */ 1045 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING) 1046 pclk_pol = 1; 1047 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH) 1048 hsync_pol = 1; 1049 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW) 1050 vsync_pol = 1; 1051 1052 ret = ov5640_write_reg(sensor, 1053 OV5640_REG_POLARITY_CTRL00, 1054 (pclk_pol << 5) | 1055 (hsync_pol << 1) | 1056 vsync_pol); 1057 1058 if (ret) 1059 return ret; 1060 } 1061 1062 /* 1063 * powerdown MIPI TX/RX PHY & disable MIPI 1064 * 1065 * MIPI CONTROL 00 1066 * 4: PWDN PHY TX 1067 * 3: PWDN PHY RX 1068 * 2: MIPI enable 1069 */ 1070 ret = ov5640_write_reg(sensor, 1071 OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0); 1072 if (ret) 1073 return ret; 1074 1075 /* 1076 * enable VSYNC/HREF/PCLK DVP control lines 1077 * & D[9:6] DVP data lines 1078 * 1079 * PAD OUTPUT ENABLE 01 1080 * - 6: VSYNC output enable 1081 * - 5: HREF output enable 1082 * - 4: PCLK output enable 1083 * - [3:0]: D[9:6] output enable 1084 */ 1085 ret = ov5640_write_reg(sensor, 1086 OV5640_REG_PAD_OUTPUT_ENABLE01, 1087 on ? 0x7f : 0); 1088 if (ret) 1089 return ret; 1090 1091 /* 1092 * enable D[5:0] DVP data lines 1093 * 1094 * PAD OUTPUT ENABLE 02 1095 * - [7:2]: D[5:0] output enable 1096 */ 1097 return ov5640_write_reg(sensor, 1098 OV5640_REG_PAD_OUTPUT_ENABLE02, 1099 on ? 0xfc : 0); 1100 } 1101 1102 static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on) 1103 { 1104 int ret; 1105 1106 ret = ov5640_mod_reg(sensor, OV5640_REG_MIPI_CTRL00, BIT(5), 1107 on ? 0 : BIT(5)); 1108 if (ret) 1109 return ret; 1110 ret = ov5640_write_reg(sensor, OV5640_REG_PAD_OUTPUT00, 1111 on ? 0x00 : 0x70); 1112 if (ret) 1113 return ret; 1114 1115 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01, 1116 on ? 0x00 : 0x0f); 1117 } 1118 1119 static int ov5640_get_sysclk(struct ov5640_dev *sensor) 1120 { 1121 /* calculate sysclk */ 1122 u32 xvclk = sensor->xclk_freq / 10000; 1123 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv; 1124 u32 sclk_rdiv_map[] = {1, 2, 4, 8}; 1125 u32 bit_div2x = 1, sclk_rdiv, sysclk; 1126 u8 temp1, temp2; 1127 int ret; 1128 1129 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1); 1130 if (ret) 1131 return ret; 1132 temp2 = temp1 & 0x0f; 1133 if (temp2 == 8 || temp2 == 10) 1134 bit_div2x = temp2 / 2; 1135 1136 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1); 1137 if (ret) 1138 return ret; 1139 sysdiv = temp1 >> 4; 1140 if (sysdiv == 0) 1141 sysdiv = 16; 1142 1143 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1); 1144 if (ret) 1145 return ret; 1146 multiplier = temp1; 1147 1148 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1); 1149 if (ret) 1150 return ret; 1151 prediv = temp1 & 0x0f; 1152 pll_rdiv = ((temp1 >> 4) & 0x01) + 1; 1153 1154 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1); 1155 if (ret) 1156 return ret; 1157 temp2 = temp1 & 0x03; 1158 sclk_rdiv = sclk_rdiv_map[temp2]; 1159 1160 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x) 1161 return -EINVAL; 1162 1163 VCO = xvclk * multiplier / prediv; 1164 1165 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv; 1166 1167 return sysclk; 1168 } 1169 1170 static int ov5640_set_night_mode(struct ov5640_dev *sensor) 1171 { 1172 /* read HTS from register settings */ 1173 u8 mode; 1174 int ret; 1175 1176 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode); 1177 if (ret) 1178 return ret; 1179 mode &= 0xfb; 1180 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode); 1181 } 1182 1183 static int ov5640_get_hts(struct ov5640_dev *sensor) 1184 { 1185 /* read HTS from register settings */ 1186 u16 hts; 1187 int ret; 1188 1189 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts); 1190 if (ret) 1191 return ret; 1192 return hts; 1193 } 1194 1195 static int ov5640_get_vts(struct ov5640_dev *sensor) 1196 { 1197 u16 vts; 1198 int ret; 1199 1200 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts); 1201 if (ret) 1202 return ret; 1203 return vts; 1204 } 1205 1206 static int ov5640_set_vts(struct ov5640_dev *sensor, int vts) 1207 { 1208 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts); 1209 } 1210 1211 static int ov5640_get_light_freq(struct ov5640_dev *sensor) 1212 { 1213 /* get banding filter value */ 1214 int ret, light_freq = 0; 1215 u8 temp, temp1; 1216 1217 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp); 1218 if (ret) 1219 return ret; 1220 1221 if (temp & 0x80) { 1222 /* manual */ 1223 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00, 1224 &temp1); 1225 if (ret) 1226 return ret; 1227 if (temp1 & 0x04) { 1228 /* 50Hz */ 1229 light_freq = 50; 1230 } else { 1231 /* 60Hz */ 1232 light_freq = 60; 1233 } 1234 } else { 1235 /* auto */ 1236 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C, 1237 &temp1); 1238 if (ret) 1239 return ret; 1240 1241 if (temp1 & 0x01) { 1242 /* 50Hz */ 1243 light_freq = 50; 1244 } else { 1245 /* 60Hz */ 1246 } 1247 } 1248 1249 return light_freq; 1250 } 1251 1252 static int ov5640_set_bandingfilter(struct ov5640_dev *sensor) 1253 { 1254 u32 band_step60, max_band60, band_step50, max_band50, prev_vts; 1255 int ret; 1256 1257 /* read preview PCLK */ 1258 ret = ov5640_get_sysclk(sensor); 1259 if (ret < 0) 1260 return ret; 1261 if (ret == 0) 1262 return -EINVAL; 1263 sensor->prev_sysclk = ret; 1264 /* read preview HTS */ 1265 ret = ov5640_get_hts(sensor); 1266 if (ret < 0) 1267 return ret; 1268 if (ret == 0) 1269 return -EINVAL; 1270 sensor->prev_hts = ret; 1271 1272 /* read preview VTS */ 1273 ret = ov5640_get_vts(sensor); 1274 if (ret < 0) 1275 return ret; 1276 prev_vts = ret; 1277 1278 /* calculate banding filter */ 1279 /* 60Hz */ 1280 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120; 1281 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60); 1282 if (ret) 1283 return ret; 1284 if (!band_step60) 1285 return -EINVAL; 1286 max_band60 = (int)((prev_vts - 4) / band_step60); 1287 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60); 1288 if (ret) 1289 return ret; 1290 1291 /* 50Hz */ 1292 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts; 1293 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50); 1294 if (ret) 1295 return ret; 1296 if (!band_step50) 1297 return -EINVAL; 1298 max_band50 = (int)((prev_vts - 4) / band_step50); 1299 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50); 1300 } 1301 1302 static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target) 1303 { 1304 /* stable in high */ 1305 u32 fast_high, fast_low; 1306 int ret; 1307 1308 sensor->ae_low = target * 23 / 25; /* 0.92 */ 1309 sensor->ae_high = target * 27 / 25; /* 1.08 */ 1310 1311 fast_high = sensor->ae_high << 1; 1312 if (fast_high > 255) 1313 fast_high = 255; 1314 1315 fast_low = sensor->ae_low >> 1; 1316 1317 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high); 1318 if (ret) 1319 return ret; 1320 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low); 1321 if (ret) 1322 return ret; 1323 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high); 1324 if (ret) 1325 return ret; 1326 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low); 1327 if (ret) 1328 return ret; 1329 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high); 1330 if (ret) 1331 return ret; 1332 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low); 1333 } 1334 1335 static int ov5640_binning_on(struct ov5640_dev *sensor) 1336 { 1337 u8 temp; 1338 int ret; 1339 1340 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp); 1341 if (ret) 1342 return ret; 1343 temp &= 0xfe; 1344 return temp ? 1 : 0; 1345 } 1346 1347 static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable) 1348 { 1349 int ret; 1350 1351 /* 1352 * TIMING TC REG21: 1353 * - [0]: Horizontal binning enable 1354 */ 1355 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 1356 BIT(0), enable ? BIT(0) : 0); 1357 if (ret) 1358 return ret; 1359 /* 1360 * TIMING TC REG20: 1361 * - [0]: Undocumented, but hardcoded init sequences 1362 * are always setting REG21/REG20 bit 0 to same value... 1363 */ 1364 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, 1365 BIT(0), enable ? BIT(0) : 0); 1366 } 1367 1368 static int ov5640_set_virtual_channel(struct ov5640_dev *sensor) 1369 { 1370 struct i2c_client *client = sensor->i2c_client; 1371 u8 temp, channel = virtual_channel; 1372 int ret; 1373 1374 if (channel > 3) { 1375 dev_err(&client->dev, 1376 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n", 1377 __func__, channel); 1378 return -EINVAL; 1379 } 1380 1381 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp); 1382 if (ret) 1383 return ret; 1384 temp &= ~(3 << 6); 1385 temp |= (channel << 6); 1386 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp); 1387 } 1388 1389 static int ov5640_set_timings(struct ov5640_dev *sensor, 1390 const struct ov5640_mode_info *mode) 1391 { 1392 int ret; 1393 1394 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact); 1395 if (ret < 0) 1396 return ret; 1397 1398 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact); 1399 if (ret < 0) 1400 return ret; 1401 1402 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot); 1403 if (ret < 0) 1404 return ret; 1405 1406 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot); 1407 if (ret < 0) 1408 return ret; 1409 1410 return 0; 1411 } 1412 1413 static const struct ov5640_mode_info * 1414 ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr, 1415 int width, int height, bool nearest) 1416 { 1417 const struct ov5640_mode_info *mode; 1418 1419 mode = v4l2_find_nearest_size(ov5640_mode_data[fr], 1420 ARRAY_SIZE(ov5640_mode_data[fr]), 1421 hact, vact, 1422 width, height); 1423 1424 if (!mode || 1425 (!nearest && (mode->hact != width || mode->vact != height))) 1426 return NULL; 1427 1428 return mode; 1429 } 1430 1431 /* 1432 * sensor changes between scaling and subsampling, go through 1433 * exposure calculation 1434 */ 1435 static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor, 1436 const struct ov5640_mode_info *mode) 1437 { 1438 u32 prev_shutter, prev_gain16; 1439 u32 cap_shutter, cap_gain16; 1440 u32 cap_sysclk, cap_hts, cap_vts; 1441 u32 light_freq, cap_bandfilt, cap_maxband; 1442 u32 cap_gain16_shutter; 1443 u8 average; 1444 int ret; 1445 1446 if (!mode->reg_data) 1447 return -EINVAL; 1448 1449 /* read preview shutter */ 1450 ret = ov5640_get_exposure(sensor); 1451 if (ret < 0) 1452 return ret; 1453 prev_shutter = ret; 1454 ret = ov5640_binning_on(sensor); 1455 if (ret < 0) 1456 return ret; 1457 if (ret && mode->id != OV5640_MODE_720P_1280_720 && 1458 mode->id != OV5640_MODE_1080P_1920_1080) 1459 prev_shutter *= 2; 1460 1461 /* read preview gain */ 1462 ret = ov5640_get_gain(sensor); 1463 if (ret < 0) 1464 return ret; 1465 prev_gain16 = ret; 1466 1467 /* get average */ 1468 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average); 1469 if (ret) 1470 return ret; 1471 1472 /* turn off night mode for capture */ 1473 ret = ov5640_set_night_mode(sensor); 1474 if (ret < 0) 1475 return ret; 1476 1477 /* Write capture setting */ 1478 ret = ov5640_load_regs(sensor, mode); 1479 if (ret < 0) 1480 return ret; 1481 1482 /* read capture VTS */ 1483 ret = ov5640_get_vts(sensor); 1484 if (ret < 0) 1485 return ret; 1486 cap_vts = ret; 1487 ret = ov5640_get_hts(sensor); 1488 if (ret < 0) 1489 return ret; 1490 if (ret == 0) 1491 return -EINVAL; 1492 cap_hts = ret; 1493 1494 ret = ov5640_get_sysclk(sensor); 1495 if (ret < 0) 1496 return ret; 1497 if (ret == 0) 1498 return -EINVAL; 1499 cap_sysclk = ret; 1500 1501 /* calculate capture banding filter */ 1502 ret = ov5640_get_light_freq(sensor); 1503 if (ret < 0) 1504 return ret; 1505 light_freq = ret; 1506 1507 if (light_freq == 60) { 1508 /* 60Hz */ 1509 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120; 1510 } else { 1511 /* 50Hz */ 1512 cap_bandfilt = cap_sysclk * 100 / cap_hts; 1513 } 1514 1515 if (!sensor->prev_sysclk) { 1516 ret = ov5640_get_sysclk(sensor); 1517 if (ret < 0) 1518 return ret; 1519 if (ret == 0) 1520 return -EINVAL; 1521 sensor->prev_sysclk = ret; 1522 } 1523 1524 if (!cap_bandfilt) 1525 return -EINVAL; 1526 1527 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt); 1528 1529 /* calculate capture shutter/gain16 */ 1530 if (average > sensor->ae_low && average < sensor->ae_high) { 1531 /* in stable range */ 1532 cap_gain16_shutter = 1533 prev_gain16 * prev_shutter * 1534 cap_sysclk / sensor->prev_sysclk * 1535 sensor->prev_hts / cap_hts * 1536 sensor->ae_target / average; 1537 } else { 1538 cap_gain16_shutter = 1539 prev_gain16 * prev_shutter * 1540 cap_sysclk / sensor->prev_sysclk * 1541 sensor->prev_hts / cap_hts; 1542 } 1543 1544 /* gain to shutter */ 1545 if (cap_gain16_shutter < (cap_bandfilt * 16)) { 1546 /* shutter < 1/100 */ 1547 cap_shutter = cap_gain16_shutter / 16; 1548 if (cap_shutter < 1) 1549 cap_shutter = 1; 1550 1551 cap_gain16 = cap_gain16_shutter / cap_shutter; 1552 if (cap_gain16 < 16) 1553 cap_gain16 = 16; 1554 } else { 1555 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) { 1556 /* exposure reach max */ 1557 cap_shutter = cap_bandfilt * cap_maxband; 1558 if (!cap_shutter) 1559 return -EINVAL; 1560 1561 cap_gain16 = cap_gain16_shutter / cap_shutter; 1562 } else { 1563 /* 1/100 < (cap_shutter = n/100) =< max */ 1564 cap_shutter = 1565 ((int)(cap_gain16_shutter / 16 / cap_bandfilt)) 1566 * cap_bandfilt; 1567 if (!cap_shutter) 1568 return -EINVAL; 1569 1570 cap_gain16 = cap_gain16_shutter / cap_shutter; 1571 } 1572 } 1573 1574 /* set capture gain */ 1575 ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.gain, cap_gain16); 1576 if (ret) 1577 return ret; 1578 1579 /* write capture shutter */ 1580 if (cap_shutter > (cap_vts - 4)) { 1581 cap_vts = cap_shutter + 4; 1582 ret = ov5640_set_vts(sensor, cap_vts); 1583 if (ret < 0) 1584 return ret; 1585 } 1586 1587 /* set exposure */ 1588 return __v4l2_ctrl_s_ctrl(sensor->ctrls.exposure, cap_shutter); 1589 } 1590 1591 /* 1592 * if sensor changes inside scaling or subsampling 1593 * change mode directly 1594 */ 1595 static int ov5640_set_mode_direct(struct ov5640_dev *sensor, 1596 const struct ov5640_mode_info *mode, 1597 s32 exposure) 1598 { 1599 int ret; 1600 1601 if (!mode->reg_data) 1602 return -EINVAL; 1603 1604 /* Write capture setting */ 1605 ret = ov5640_load_regs(sensor, mode); 1606 if (ret < 0) 1607 return ret; 1608 1609 /* turn auto gain/exposure back on for direct mode */ 1610 ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 1); 1611 if (ret) 1612 return ret; 1613 1614 return __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_exp, exposure); 1615 } 1616 1617 static int ov5640_set_mode(struct ov5640_dev *sensor, 1618 const struct ov5640_mode_info *orig_mode) 1619 { 1620 const struct ov5640_mode_info *mode = sensor->current_mode; 1621 enum ov5640_downsize_mode dn_mode, orig_dn_mode; 1622 s32 exposure; 1623 int ret; 1624 1625 dn_mode = mode->dn_mode; 1626 orig_dn_mode = orig_mode->dn_mode; 1627 1628 /* auto gain and exposure must be turned off when changing modes */ 1629 ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.auto_gain, 0); 1630 if (ret) 1631 return ret; 1632 1633 exposure = sensor->ctrls.auto_exp->val; 1634 ret = ov5640_set_exposure(sensor, V4L2_EXPOSURE_MANUAL); 1635 if (ret) 1636 return ret; 1637 1638 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) || 1639 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) { 1640 /* 1641 * change between subsampling and scaling 1642 * go through exposure calucation 1643 */ 1644 ret = ov5640_set_mode_exposure_calc(sensor, mode); 1645 } else { 1646 /* 1647 * change inside subsampling or scaling 1648 * download firmware directly 1649 */ 1650 ret = ov5640_set_mode_direct(sensor, mode, exposure); 1651 } 1652 1653 if (ret < 0) 1654 return ret; 1655 1656 ret = ov5640_set_timings(sensor, mode); 1657 if (ret < 0) 1658 return ret; 1659 1660 ret = ov5640_set_binning(sensor, dn_mode != SCALING); 1661 if (ret < 0) 1662 return ret; 1663 ret = ov5640_set_ae_target(sensor, sensor->ae_target); 1664 if (ret < 0) 1665 return ret; 1666 ret = ov5640_get_light_freq(sensor); 1667 if (ret < 0) 1668 return ret; 1669 ret = ov5640_set_bandingfilter(sensor); 1670 if (ret < 0) 1671 return ret; 1672 ret = ov5640_set_virtual_channel(sensor); 1673 if (ret < 0) 1674 return ret; 1675 1676 sensor->pending_mode_change = false; 1677 1678 return 0; 1679 } 1680 1681 static int ov5640_set_framefmt(struct ov5640_dev *sensor, 1682 struct v4l2_mbus_framefmt *format); 1683 1684 /* restore the last set video mode after chip power-on */ 1685 static int ov5640_restore_mode(struct ov5640_dev *sensor) 1686 { 1687 int ret; 1688 1689 /* first load the initial register values */ 1690 ret = ov5640_load_regs(sensor, &ov5640_mode_init_data); 1691 if (ret < 0) 1692 return ret; 1693 1694 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f, 1695 (ilog2(OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT) << 2) | 1696 ilog2(OV5640_SCLK_ROOT_DIVIDER_DEFAULT)); 1697 if (ret) 1698 return ret; 1699 1700 /* now restore the last capture mode */ 1701 ret = ov5640_set_mode(sensor, &ov5640_mode_init_data); 1702 if (ret < 0) 1703 return ret; 1704 1705 return ov5640_set_framefmt(sensor, &sensor->fmt); 1706 } 1707 1708 static void ov5640_power(struct ov5640_dev *sensor, bool enable) 1709 { 1710 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1); 1711 } 1712 1713 static void ov5640_reset(struct ov5640_dev *sensor) 1714 { 1715 if (!sensor->reset_gpio) 1716 return; 1717 1718 gpiod_set_value_cansleep(sensor->reset_gpio, 0); 1719 1720 /* camera power cycle */ 1721 ov5640_power(sensor, false); 1722 usleep_range(5000, 10000); 1723 ov5640_power(sensor, true); 1724 usleep_range(5000, 10000); 1725 1726 gpiod_set_value_cansleep(sensor->reset_gpio, 1); 1727 usleep_range(1000, 2000); 1728 1729 gpiod_set_value_cansleep(sensor->reset_gpio, 0); 1730 usleep_range(5000, 10000); 1731 } 1732 1733 static int ov5640_set_power_on(struct ov5640_dev *sensor) 1734 { 1735 struct i2c_client *client = sensor->i2c_client; 1736 int ret; 1737 1738 ret = clk_prepare_enable(sensor->xclk); 1739 if (ret) { 1740 dev_err(&client->dev, "%s: failed to enable clock\n", 1741 __func__); 1742 return ret; 1743 } 1744 1745 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES, 1746 sensor->supplies); 1747 if (ret) { 1748 dev_err(&client->dev, "%s: failed to enable regulators\n", 1749 __func__); 1750 goto xclk_off; 1751 } 1752 1753 ov5640_reset(sensor); 1754 ov5640_power(sensor, true); 1755 1756 ret = ov5640_init_slave_id(sensor); 1757 if (ret) 1758 goto power_off; 1759 1760 return 0; 1761 1762 power_off: 1763 ov5640_power(sensor, false); 1764 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); 1765 xclk_off: 1766 clk_disable_unprepare(sensor->xclk); 1767 return ret; 1768 } 1769 1770 static void ov5640_set_power_off(struct ov5640_dev *sensor) 1771 { 1772 ov5640_power(sensor, false); 1773 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies); 1774 clk_disable_unprepare(sensor->xclk); 1775 } 1776 1777 static int ov5640_set_power(struct ov5640_dev *sensor, bool on) 1778 { 1779 int ret = 0; 1780 1781 if (on) { 1782 ret = ov5640_set_power_on(sensor); 1783 if (ret) 1784 return ret; 1785 1786 ret = ov5640_restore_mode(sensor); 1787 if (ret) 1788 goto power_off; 1789 1790 if (sensor->ep.bus_type == V4L2_MBUS_CSI2) { 1791 /* 1792 * start streaming briefly followed by stream off in 1793 * order to coax the clock lane into LP-11 state. 1794 */ 1795 ret = ov5640_set_stream_mipi(sensor, true); 1796 if (ret) 1797 goto power_off; 1798 usleep_range(1000, 2000); 1799 ret = ov5640_set_stream_mipi(sensor, false); 1800 if (ret) 1801 goto power_off; 1802 } 1803 1804 return 0; 1805 } 1806 1807 power_off: 1808 ov5640_set_power_off(sensor); 1809 return ret; 1810 } 1811 1812 /* --------------- Subdev Operations --------------- */ 1813 1814 static int ov5640_s_power(struct v4l2_subdev *sd, int on) 1815 { 1816 struct ov5640_dev *sensor = to_ov5640_dev(sd); 1817 int ret = 0; 1818 1819 mutex_lock(&sensor->lock); 1820 1821 /* 1822 * If the power count is modified from 0 to != 0 or from != 0 to 0, 1823 * update the power state. 1824 */ 1825 if (sensor->power_count == !on) { 1826 ret = ov5640_set_power(sensor, !!on); 1827 if (ret) 1828 goto out; 1829 } 1830 1831 /* Update the power count. */ 1832 sensor->power_count += on ? 1 : -1; 1833 WARN_ON(sensor->power_count < 0); 1834 out: 1835 mutex_unlock(&sensor->lock); 1836 1837 if (on && !ret && sensor->power_count == 1) { 1838 /* restore controls */ 1839 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler); 1840 } 1841 1842 return ret; 1843 } 1844 1845 static int ov5640_try_frame_interval(struct ov5640_dev *sensor, 1846 struct v4l2_fract *fi, 1847 u32 width, u32 height) 1848 { 1849 const struct ov5640_mode_info *mode; 1850 u32 minfps, maxfps, fps; 1851 int ret; 1852 1853 minfps = ov5640_framerates[OV5640_15_FPS]; 1854 maxfps = ov5640_framerates[OV5640_30_FPS]; 1855 1856 if (fi->numerator == 0) { 1857 fi->denominator = maxfps; 1858 fi->numerator = 1; 1859 return OV5640_30_FPS; 1860 } 1861 1862 fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator); 1863 1864 fi->numerator = 1; 1865 if (fps > maxfps) 1866 fi->denominator = maxfps; 1867 else if (fps < minfps) 1868 fi->denominator = minfps; 1869 else if (2 * fps >= 2 * minfps + (maxfps - minfps)) 1870 fi->denominator = maxfps; 1871 else 1872 fi->denominator = minfps; 1873 1874 ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS; 1875 1876 mode = ov5640_find_mode(sensor, ret, width, height, false); 1877 return mode ? ret : -EINVAL; 1878 } 1879 1880 static int ov5640_get_fmt(struct v4l2_subdev *sd, 1881 struct v4l2_subdev_pad_config *cfg, 1882 struct v4l2_subdev_format *format) 1883 { 1884 struct ov5640_dev *sensor = to_ov5640_dev(sd); 1885 struct v4l2_mbus_framefmt *fmt; 1886 1887 if (format->pad != 0) 1888 return -EINVAL; 1889 1890 mutex_lock(&sensor->lock); 1891 1892 if (format->which == V4L2_SUBDEV_FORMAT_TRY) 1893 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg, 1894 format->pad); 1895 else 1896 fmt = &sensor->fmt; 1897 1898 format->format = *fmt; 1899 1900 mutex_unlock(&sensor->lock); 1901 1902 return 0; 1903 } 1904 1905 static int ov5640_try_fmt_internal(struct v4l2_subdev *sd, 1906 struct v4l2_mbus_framefmt *fmt, 1907 enum ov5640_frame_rate fr, 1908 const struct ov5640_mode_info **new_mode) 1909 { 1910 struct ov5640_dev *sensor = to_ov5640_dev(sd); 1911 const struct ov5640_mode_info *mode; 1912 int i; 1913 1914 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true); 1915 if (!mode) 1916 return -EINVAL; 1917 fmt->width = mode->hact; 1918 fmt->height = mode->vact; 1919 1920 if (new_mode) 1921 *new_mode = mode; 1922 1923 for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++) 1924 if (ov5640_formats[i].code == fmt->code) 1925 break; 1926 if (i >= ARRAY_SIZE(ov5640_formats)) 1927 i = 0; 1928 1929 fmt->code = ov5640_formats[i].code; 1930 fmt->colorspace = ov5640_formats[i].colorspace; 1931 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 1932 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 1933 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 1934 1935 return 0; 1936 } 1937 1938 static int ov5640_set_fmt(struct v4l2_subdev *sd, 1939 struct v4l2_subdev_pad_config *cfg, 1940 struct v4l2_subdev_format *format) 1941 { 1942 struct ov5640_dev *sensor = to_ov5640_dev(sd); 1943 const struct ov5640_mode_info *new_mode; 1944 struct v4l2_mbus_framefmt *mbus_fmt = &format->format; 1945 int ret; 1946 1947 if (format->pad != 0) 1948 return -EINVAL; 1949 1950 mutex_lock(&sensor->lock); 1951 1952 if (sensor->streaming) { 1953 ret = -EBUSY; 1954 goto out; 1955 } 1956 1957 ret = ov5640_try_fmt_internal(sd, mbus_fmt, 1958 sensor->current_fr, &new_mode); 1959 if (ret) 1960 goto out; 1961 1962 if (format->which == V4L2_SUBDEV_FORMAT_TRY) { 1963 struct v4l2_mbus_framefmt *fmt = 1964 v4l2_subdev_get_try_format(sd, cfg, 0); 1965 1966 *fmt = *mbus_fmt; 1967 goto out; 1968 } 1969 1970 if (new_mode != sensor->current_mode) { 1971 sensor->current_mode = new_mode; 1972 sensor->pending_mode_change = true; 1973 } 1974 if (mbus_fmt->code != sensor->fmt.code) { 1975 sensor->fmt = *mbus_fmt; 1976 sensor->pending_fmt_change = true; 1977 } 1978 out: 1979 mutex_unlock(&sensor->lock); 1980 return ret; 1981 } 1982 1983 static int ov5640_set_framefmt(struct ov5640_dev *sensor, 1984 struct v4l2_mbus_framefmt *format) 1985 { 1986 int ret = 0; 1987 bool is_rgb = false; 1988 bool is_jpeg = false; 1989 u8 val; 1990 1991 switch (format->code) { 1992 case MEDIA_BUS_FMT_UYVY8_2X8: 1993 /* YUV422, UYVY */ 1994 val = 0x3f; 1995 break; 1996 case MEDIA_BUS_FMT_YUYV8_2X8: 1997 /* YUV422, YUYV */ 1998 val = 0x30; 1999 break; 2000 case MEDIA_BUS_FMT_RGB565_2X8_LE: 2001 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */ 2002 val = 0x6F; 2003 is_rgb = true; 2004 break; 2005 case MEDIA_BUS_FMT_RGB565_2X8_BE: 2006 /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */ 2007 val = 0x61; 2008 is_rgb = true; 2009 break; 2010 case MEDIA_BUS_FMT_JPEG_1X8: 2011 /* YUV422, YUYV */ 2012 val = 0x30; 2013 is_jpeg = true; 2014 break; 2015 default: 2016 return -EINVAL; 2017 } 2018 2019 /* FORMAT CONTROL00: YUV and RGB formatting */ 2020 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val); 2021 if (ret) 2022 return ret; 2023 2024 /* FORMAT MUX CONTROL: ISP YUV or RGB */ 2025 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, 2026 is_rgb ? 0x01 : 0x00); 2027 if (ret) 2028 return ret; 2029 2030 /* 2031 * TIMING TC REG21: 2032 * - [5]: JPEG enable 2033 */ 2034 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 2035 BIT(5), is_jpeg ? BIT(5) : 0); 2036 if (ret) 2037 return ret; 2038 2039 /* 2040 * SYSTEM RESET02: 2041 * - [4]: Reset JFIFO 2042 * - [3]: Reset SFIFO 2043 * - [2]: Reset JPEG 2044 */ 2045 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02, 2046 BIT(4) | BIT(3) | BIT(2), 2047 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2))); 2048 if (ret) 2049 return ret; 2050 2051 /* 2052 * CLOCK ENABLE02: 2053 * - [5]: Enable JPEG 2x clock 2054 * - [3]: Enable JPEG clock 2055 */ 2056 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02, 2057 BIT(5) | BIT(3), 2058 is_jpeg ? (BIT(5) | BIT(3)) : 0); 2059 } 2060 2061 /* 2062 * Sensor Controls. 2063 */ 2064 2065 static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value) 2066 { 2067 int ret; 2068 2069 if (value) { 2070 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 2071 BIT(0), BIT(0)); 2072 if (ret) 2073 return ret; 2074 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value); 2075 } else { 2076 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0); 2077 } 2078 2079 return ret; 2080 } 2081 2082 static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value) 2083 { 2084 int ret; 2085 2086 if (value) { 2087 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 2088 BIT(2), BIT(2)); 2089 if (ret) 2090 return ret; 2091 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5, 2092 value & 0xff); 2093 } else { 2094 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0); 2095 } 2096 2097 return ret; 2098 } 2099 2100 static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value) 2101 { 2102 int ret; 2103 2104 if (value) { 2105 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, 2106 BIT(1), BIT(1)); 2107 if (ret) 2108 return ret; 2109 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3, 2110 value & 0xff); 2111 if (ret) 2112 return ret; 2113 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4, 2114 value & 0xff); 2115 } else { 2116 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0); 2117 } 2118 2119 return ret; 2120 } 2121 2122 static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb) 2123 { 2124 int ret; 2125 2126 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL, 2127 BIT(0), awb ? 0 : 1); 2128 if (ret) 2129 return ret; 2130 2131 if (!awb) { 2132 u16 red = (u16)sensor->ctrls.red_balance->val; 2133 u16 blue = (u16)sensor->ctrls.blue_balance->val; 2134 2135 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red); 2136 if (ret) 2137 return ret; 2138 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue); 2139 } 2140 2141 return ret; 2142 } 2143 2144 static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor, int exp) 2145 { 2146 struct ov5640_ctrls *ctrls = &sensor->ctrls; 2147 bool auto_exposure = (exp == V4L2_EXPOSURE_AUTO); 2148 int ret = 0; 2149 2150 if (ctrls->auto_exp->is_new) { 2151 ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, 2152 BIT(0), auto_exposure ? 0 : BIT(0)); 2153 if (ret) 2154 return ret; 2155 } 2156 2157 if (!auto_exposure && ctrls->exposure->is_new) { 2158 u16 max_exp; 2159 2160 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS, 2161 &max_exp); 2162 if (ret) 2163 return ret; 2164 ret = ov5640_get_vts(sensor); 2165 if (ret < 0) 2166 return ret; 2167 max_exp += ret; 2168 ret = 0; 2169 2170 if (ctrls->exposure->val < max_exp) 2171 ret = ov5640_set_exposure(sensor, ctrls->exposure->val); 2172 } 2173 2174 return ret; 2175 } 2176 2177 static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, int auto_gain) 2178 { 2179 struct ov5640_ctrls *ctrls = &sensor->ctrls; 2180 int ret = 0; 2181 2182 if (ctrls->auto_gain->is_new) { 2183 ret = ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL, 2184 BIT(1), 2185 ctrls->auto_gain->val ? 0 : BIT(1)); 2186 if (ret) 2187 return ret; 2188 } 2189 2190 if (!auto_gain && ctrls->gain->is_new) { 2191 u16 gain = (u16)ctrls->gain->val; 2192 2193 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, 2194 gain & 0x3ff); 2195 } 2196 2197 return ret; 2198 } 2199 2200 static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value) 2201 { 2202 return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1, 2203 0xa4, value ? 0xa4 : 0); 2204 } 2205 2206 static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value) 2207 { 2208 int ret; 2209 2210 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7), 2211 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ? 2212 0 : BIT(7)); 2213 if (ret) 2214 return ret; 2215 2216 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2), 2217 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ? 2218 BIT(2) : 0); 2219 } 2220 2221 static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value) 2222 { 2223 /* 2224 * If sensor is mounted upside down, mirror logic is inversed. 2225 * 2226 * Sensor is a BSI (Back Side Illuminated) one, 2227 * so image captured is physically mirrored. 2228 * This is why mirror logic is inversed in 2229 * order to cancel this mirror effect. 2230 */ 2231 2232 /* 2233 * TIMING TC REG21: 2234 * - [2]: ISP mirror 2235 * - [1]: Sensor mirror 2236 */ 2237 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21, 2238 BIT(2) | BIT(1), 2239 (!(value ^ sensor->upside_down)) ? 2240 (BIT(2) | BIT(1)) : 0); 2241 } 2242 2243 static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value) 2244 { 2245 /* If sensor is mounted upside down, flip logic is inversed */ 2246 2247 /* 2248 * TIMING TC REG20: 2249 * - [2]: ISP vflip 2250 * - [1]: Sensor vflip 2251 */ 2252 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20, 2253 BIT(2) | BIT(1), 2254 (value ^ sensor->upside_down) ? 2255 (BIT(2) | BIT(1)) : 0); 2256 } 2257 2258 static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl) 2259 { 2260 struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 2261 struct ov5640_dev *sensor = to_ov5640_dev(sd); 2262 int val; 2263 2264 /* v4l2_ctrl_lock() locks our own mutex */ 2265 2266 switch (ctrl->id) { 2267 case V4L2_CID_AUTOGAIN: 2268 if (!ctrl->val) 2269 return 0; 2270 val = ov5640_get_gain(sensor); 2271 if (val < 0) 2272 return val; 2273 sensor->ctrls.gain->val = val; 2274 break; 2275 case V4L2_CID_EXPOSURE_AUTO: 2276 if (ctrl->val == V4L2_EXPOSURE_MANUAL) 2277 return 0; 2278 val = ov5640_get_exposure(sensor); 2279 if (val < 0) 2280 return val; 2281 sensor->ctrls.exposure->val = val; 2282 break; 2283 } 2284 2285 return 0; 2286 } 2287 2288 static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl) 2289 { 2290 struct v4l2_subdev *sd = ctrl_to_sd(ctrl); 2291 struct ov5640_dev *sensor = to_ov5640_dev(sd); 2292 int ret; 2293 2294 /* v4l2_ctrl_lock() locks our own mutex */ 2295 2296 /* 2297 * If the device is not powered up by the host driver do 2298 * not apply any controls to H/W at this time. Instead 2299 * the controls will be restored right after power-up. 2300 */ 2301 if (sensor->power_count == 0) 2302 return 0; 2303 2304 switch (ctrl->id) { 2305 case V4L2_CID_AUTOGAIN: 2306 ret = ov5640_set_ctrl_gain(sensor, ctrl->val); 2307 break; 2308 case V4L2_CID_EXPOSURE_AUTO: 2309 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val); 2310 break; 2311 case V4L2_CID_AUTO_WHITE_BALANCE: 2312 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val); 2313 break; 2314 case V4L2_CID_HUE: 2315 ret = ov5640_set_ctrl_hue(sensor, ctrl->val); 2316 break; 2317 case V4L2_CID_CONTRAST: 2318 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val); 2319 break; 2320 case V4L2_CID_SATURATION: 2321 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val); 2322 break; 2323 case V4L2_CID_TEST_PATTERN: 2324 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val); 2325 break; 2326 case V4L2_CID_POWER_LINE_FREQUENCY: 2327 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val); 2328 break; 2329 case V4L2_CID_HFLIP: 2330 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val); 2331 break; 2332 case V4L2_CID_VFLIP: 2333 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val); 2334 break; 2335 default: 2336 ret = -EINVAL; 2337 break; 2338 } 2339 2340 return ret; 2341 } 2342 2343 static const struct v4l2_ctrl_ops ov5640_ctrl_ops = { 2344 .g_volatile_ctrl = ov5640_g_volatile_ctrl, 2345 .s_ctrl = ov5640_s_ctrl, 2346 }; 2347 2348 static const char * const test_pattern_menu[] = { 2349 "Disabled", 2350 "Color bars", 2351 }; 2352 2353 static int ov5640_init_controls(struct ov5640_dev *sensor) 2354 { 2355 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops; 2356 struct ov5640_ctrls *ctrls = &sensor->ctrls; 2357 struct v4l2_ctrl_handler *hdl = &ctrls->handler; 2358 int ret; 2359 2360 v4l2_ctrl_handler_init(hdl, 32); 2361 2362 /* we can use our own mutex for the ctrl lock */ 2363 hdl->lock = &sensor->lock; 2364 2365 /* Auto/manual white balance */ 2366 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops, 2367 V4L2_CID_AUTO_WHITE_BALANCE, 2368 0, 1, 1, 1); 2369 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE, 2370 0, 4095, 1, 0); 2371 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE, 2372 0, 4095, 1, 0); 2373 /* Auto/manual exposure */ 2374 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops, 2375 V4L2_CID_EXPOSURE_AUTO, 2376 V4L2_EXPOSURE_MANUAL, 0, 2377 V4L2_EXPOSURE_AUTO); 2378 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE, 2379 0, 65535, 1, 0); 2380 /* Auto/manual gain */ 2381 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN, 2382 0, 1, 1, 1); 2383 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN, 2384 0, 1023, 1, 0); 2385 2386 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION, 2387 0, 255, 1, 64); 2388 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE, 2389 0, 359, 1, 0); 2390 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST, 2391 0, 255, 1, 0); 2392 ctrls->test_pattern = 2393 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN, 2394 ARRAY_SIZE(test_pattern_menu) - 1, 2395 0, 0, test_pattern_menu); 2396 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 2397 0, 1, 1, 0); 2398 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 2399 0, 1, 1, 0); 2400 2401 ctrls->light_freq = 2402 v4l2_ctrl_new_std_menu(hdl, ops, 2403 V4L2_CID_POWER_LINE_FREQUENCY, 2404 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0, 2405 V4L2_CID_POWER_LINE_FREQUENCY_50HZ); 2406 2407 if (hdl->error) { 2408 ret = hdl->error; 2409 goto free_ctrls; 2410 } 2411 2412 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE; 2413 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE; 2414 2415 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false); 2416 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true); 2417 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true); 2418 2419 sensor->sd.ctrl_handler = hdl; 2420 return 0; 2421 2422 free_ctrls: 2423 v4l2_ctrl_handler_free(hdl); 2424 return ret; 2425 } 2426 2427 static int ov5640_enum_frame_size(struct v4l2_subdev *sd, 2428 struct v4l2_subdev_pad_config *cfg, 2429 struct v4l2_subdev_frame_size_enum *fse) 2430 { 2431 if (fse->pad != 0) 2432 return -EINVAL; 2433 if (fse->index >= OV5640_NUM_MODES) 2434 return -EINVAL; 2435 2436 fse->min_width = 2437 ov5640_mode_data[0][fse->index].hact; 2438 fse->max_width = fse->min_width; 2439 fse->min_height = 2440 ov5640_mode_data[0][fse->index].vact; 2441 fse->max_height = fse->min_height; 2442 2443 return 0; 2444 } 2445 2446 static int ov5640_enum_frame_interval( 2447 struct v4l2_subdev *sd, 2448 struct v4l2_subdev_pad_config *cfg, 2449 struct v4l2_subdev_frame_interval_enum *fie) 2450 { 2451 struct ov5640_dev *sensor = to_ov5640_dev(sd); 2452 struct v4l2_fract tpf; 2453 int ret; 2454 2455 if (fie->pad != 0) 2456 return -EINVAL; 2457 if (fie->index >= OV5640_NUM_FRAMERATES) 2458 return -EINVAL; 2459 2460 tpf.numerator = 1; 2461 tpf.denominator = ov5640_framerates[fie->index]; 2462 2463 ret = ov5640_try_frame_interval(sensor, &tpf, 2464 fie->width, fie->height); 2465 if (ret < 0) 2466 return -EINVAL; 2467 2468 fie->interval = tpf; 2469 return 0; 2470 } 2471 2472 static int ov5640_g_frame_interval(struct v4l2_subdev *sd, 2473 struct v4l2_subdev_frame_interval *fi) 2474 { 2475 struct ov5640_dev *sensor = to_ov5640_dev(sd); 2476 2477 mutex_lock(&sensor->lock); 2478 fi->interval = sensor->frame_interval; 2479 mutex_unlock(&sensor->lock); 2480 2481 return 0; 2482 } 2483 2484 static int ov5640_s_frame_interval(struct v4l2_subdev *sd, 2485 struct v4l2_subdev_frame_interval *fi) 2486 { 2487 struct ov5640_dev *sensor = to_ov5640_dev(sd); 2488 const struct ov5640_mode_info *mode; 2489 int frame_rate, ret = 0; 2490 2491 if (fi->pad != 0) 2492 return -EINVAL; 2493 2494 mutex_lock(&sensor->lock); 2495 2496 if (sensor->streaming) { 2497 ret = -EBUSY; 2498 goto out; 2499 } 2500 2501 mode = sensor->current_mode; 2502 2503 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval, 2504 mode->hact, mode->vact); 2505 if (frame_rate < 0) 2506 frame_rate = OV5640_15_FPS; 2507 2508 sensor->current_fr = frame_rate; 2509 sensor->frame_interval = fi->interval; 2510 mode = ov5640_find_mode(sensor, frame_rate, mode->hact, 2511 mode->vact, true); 2512 if (!mode) { 2513 ret = -EINVAL; 2514 goto out; 2515 } 2516 2517 if (mode != sensor->current_mode) { 2518 sensor->current_mode = mode; 2519 sensor->pending_mode_change = true; 2520 } 2521 out: 2522 mutex_unlock(&sensor->lock); 2523 return ret; 2524 } 2525 2526 static int ov5640_enum_mbus_code(struct v4l2_subdev *sd, 2527 struct v4l2_subdev_pad_config *cfg, 2528 struct v4l2_subdev_mbus_code_enum *code) 2529 { 2530 if (code->pad != 0) 2531 return -EINVAL; 2532 if (code->index >= ARRAY_SIZE(ov5640_formats)) 2533 return -EINVAL; 2534 2535 code->code = ov5640_formats[code->index].code; 2536 return 0; 2537 } 2538 2539 static int ov5640_s_stream(struct v4l2_subdev *sd, int enable) 2540 { 2541 struct ov5640_dev *sensor = to_ov5640_dev(sd); 2542 int ret = 0; 2543 2544 mutex_lock(&sensor->lock); 2545 2546 if (sensor->streaming == !enable) { 2547 if (enable && sensor->pending_mode_change) { 2548 ret = ov5640_set_mode(sensor, sensor->current_mode); 2549 if (ret) 2550 goto out; 2551 } 2552 2553 if (enable && sensor->pending_fmt_change) { 2554 ret = ov5640_set_framefmt(sensor, &sensor->fmt); 2555 if (ret) 2556 goto out; 2557 sensor->pending_fmt_change = false; 2558 } 2559 2560 if (sensor->ep.bus_type == V4L2_MBUS_CSI2) 2561 ret = ov5640_set_stream_mipi(sensor, enable); 2562 else 2563 ret = ov5640_set_stream_dvp(sensor, enable); 2564 2565 if (!ret) 2566 sensor->streaming = enable; 2567 } 2568 out: 2569 mutex_unlock(&sensor->lock); 2570 return ret; 2571 } 2572 2573 static const struct v4l2_subdev_core_ops ov5640_core_ops = { 2574 .s_power = ov5640_s_power, 2575 }; 2576 2577 static const struct v4l2_subdev_video_ops ov5640_video_ops = { 2578 .g_frame_interval = ov5640_g_frame_interval, 2579 .s_frame_interval = ov5640_s_frame_interval, 2580 .s_stream = ov5640_s_stream, 2581 }; 2582 2583 static const struct v4l2_subdev_pad_ops ov5640_pad_ops = { 2584 .enum_mbus_code = ov5640_enum_mbus_code, 2585 .get_fmt = ov5640_get_fmt, 2586 .set_fmt = ov5640_set_fmt, 2587 .enum_frame_size = ov5640_enum_frame_size, 2588 .enum_frame_interval = ov5640_enum_frame_interval, 2589 }; 2590 2591 static const struct v4l2_subdev_ops ov5640_subdev_ops = { 2592 .core = &ov5640_core_ops, 2593 .video = &ov5640_video_ops, 2594 .pad = &ov5640_pad_ops, 2595 }; 2596 2597 static int ov5640_get_regulators(struct ov5640_dev *sensor) 2598 { 2599 int i; 2600 2601 for (i = 0; i < OV5640_NUM_SUPPLIES; i++) 2602 sensor->supplies[i].supply = ov5640_supply_name[i]; 2603 2604 return devm_regulator_bulk_get(&sensor->i2c_client->dev, 2605 OV5640_NUM_SUPPLIES, 2606 sensor->supplies); 2607 } 2608 2609 static int ov5640_check_chip_id(struct ov5640_dev *sensor) 2610 { 2611 struct i2c_client *client = sensor->i2c_client; 2612 int ret = 0; 2613 u16 chip_id; 2614 2615 ret = ov5640_set_power_on(sensor); 2616 if (ret) 2617 return ret; 2618 2619 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id); 2620 if (ret) { 2621 dev_err(&client->dev, "%s: failed to read chip identifier\n", 2622 __func__); 2623 goto power_off; 2624 } 2625 2626 if (chip_id != 0x5640) { 2627 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n", 2628 __func__, chip_id); 2629 ret = -ENXIO; 2630 } 2631 2632 power_off: 2633 ov5640_set_power_off(sensor); 2634 return ret; 2635 } 2636 2637 static int ov5640_probe(struct i2c_client *client, 2638 const struct i2c_device_id *id) 2639 { 2640 struct device *dev = &client->dev; 2641 struct fwnode_handle *endpoint; 2642 struct ov5640_dev *sensor; 2643 struct v4l2_mbus_framefmt *fmt; 2644 u32 rotation; 2645 int ret; 2646 2647 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL); 2648 if (!sensor) 2649 return -ENOMEM; 2650 2651 sensor->i2c_client = client; 2652 2653 /* 2654 * default init sequence initialize sensor to 2655 * YUV422 UYVY VGA@30fps 2656 */ 2657 fmt = &sensor->fmt; 2658 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8; 2659 fmt->colorspace = V4L2_COLORSPACE_SRGB; 2660 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace); 2661 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE; 2662 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace); 2663 fmt->width = 640; 2664 fmt->height = 480; 2665 fmt->field = V4L2_FIELD_NONE; 2666 sensor->frame_interval.numerator = 1; 2667 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS]; 2668 sensor->current_fr = OV5640_30_FPS; 2669 sensor->current_mode = 2670 &ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480]; 2671 2672 sensor->ae_target = 52; 2673 2674 /* optional indication of physical rotation of sensor */ 2675 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation", 2676 &rotation); 2677 if (!ret) { 2678 switch (rotation) { 2679 case 180: 2680 sensor->upside_down = true; 2681 /* fall through */ 2682 case 0: 2683 break; 2684 default: 2685 dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n", 2686 rotation); 2687 } 2688 } 2689 2690 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev), 2691 NULL); 2692 if (!endpoint) { 2693 dev_err(dev, "endpoint node not found\n"); 2694 return -EINVAL; 2695 } 2696 2697 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep); 2698 fwnode_handle_put(endpoint); 2699 if (ret) { 2700 dev_err(dev, "Could not parse endpoint\n"); 2701 return ret; 2702 } 2703 2704 /* get system clock (xclk) */ 2705 sensor->xclk = devm_clk_get(dev, "xclk"); 2706 if (IS_ERR(sensor->xclk)) { 2707 dev_err(dev, "failed to get xclk\n"); 2708 return PTR_ERR(sensor->xclk); 2709 } 2710 2711 sensor->xclk_freq = clk_get_rate(sensor->xclk); 2712 if (sensor->xclk_freq < OV5640_XCLK_MIN || 2713 sensor->xclk_freq > OV5640_XCLK_MAX) { 2714 dev_err(dev, "xclk frequency out of range: %d Hz\n", 2715 sensor->xclk_freq); 2716 return -EINVAL; 2717 } 2718 2719 /* request optional power down pin */ 2720 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown", 2721 GPIOD_OUT_HIGH); 2722 /* request optional reset pin */ 2723 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset", 2724 GPIOD_OUT_HIGH); 2725 2726 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops); 2727 2728 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 2729 sensor->pad.flags = MEDIA_PAD_FL_SOURCE; 2730 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 2731 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad); 2732 if (ret) 2733 return ret; 2734 2735 ret = ov5640_get_regulators(sensor); 2736 if (ret) 2737 return ret; 2738 2739 mutex_init(&sensor->lock); 2740 2741 ret = ov5640_check_chip_id(sensor); 2742 if (ret) 2743 goto entity_cleanup; 2744 2745 ret = ov5640_init_controls(sensor); 2746 if (ret) 2747 goto entity_cleanup; 2748 2749 ret = v4l2_async_register_subdev(&sensor->sd); 2750 if (ret) 2751 goto free_ctrls; 2752 2753 return 0; 2754 2755 free_ctrls: 2756 v4l2_ctrl_handler_free(&sensor->ctrls.handler); 2757 entity_cleanup: 2758 mutex_destroy(&sensor->lock); 2759 media_entity_cleanup(&sensor->sd.entity); 2760 return ret; 2761 } 2762 2763 static int ov5640_remove(struct i2c_client *client) 2764 { 2765 struct v4l2_subdev *sd = i2c_get_clientdata(client); 2766 struct ov5640_dev *sensor = to_ov5640_dev(sd); 2767 2768 v4l2_async_unregister_subdev(&sensor->sd); 2769 mutex_destroy(&sensor->lock); 2770 media_entity_cleanup(&sensor->sd.entity); 2771 v4l2_ctrl_handler_free(&sensor->ctrls.handler); 2772 2773 return 0; 2774 } 2775 2776 static const struct i2c_device_id ov5640_id[] = { 2777 {"ov5640", 0}, 2778 {}, 2779 }; 2780 MODULE_DEVICE_TABLE(i2c, ov5640_id); 2781 2782 static const struct of_device_id ov5640_dt_ids[] = { 2783 { .compatible = "ovti,ov5640" }, 2784 { /* sentinel */ } 2785 }; 2786 MODULE_DEVICE_TABLE(of, ov5640_dt_ids); 2787 2788 static struct i2c_driver ov5640_i2c_driver = { 2789 .driver = { 2790 .name = "ov5640", 2791 .of_match_table = ov5640_dt_ids, 2792 }, 2793 .id_table = ov5640_id, 2794 .probe = ov5640_probe, 2795 .remove = ov5640_remove, 2796 }; 2797 2798 module_i2c_driver(ov5640_i2c_driver); 2799 2800 MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver"); 2801 MODULE_LICENSE("GPL"); 2802