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