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