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