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