blob: 588b98e2e99fc9f9e99f10b9e6b1fee42fb6fe9c [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>
Akinobu Mita2d18fbc2018-11-12 11:00:52 -050028#include <media/v4l2-event.h>
Steve Longerbeam19a81c12017-06-07 15:33:56 -030029#include <media/v4l2-fwnode.h>
30#include <media/v4l2-subdev.h>
31
32/* min/typical/max system clock (xclk) frequencies */
33#define OV5640_XCLK_MIN 6000000
Philipp Puschmann41cb1c72018-06-06 05:11:38 -040034#define OV5640_XCLK_MAX 54000000
Steve Longerbeam19a81c12017-06-07 15:33:56 -030035
36#define OV5640_DEFAULT_SLAVE_ID 0x3c
37
Hugues Fruchetd47c4122018-01-31 04:08:10 -050038#define OV5640_REG_SYS_RESET02 0x3002
39#define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006
Hugues Fruchetf22996d2018-01-03 04:57:31 -050040#define OV5640_REG_SYS_CTRL0 0x3008
Steve Longerbeam19a81c12017-06-07 15:33:56 -030041#define OV5640_REG_CHIP_ID 0x300a
Hugues Fruchetf22996d2018-01-03 04:57:31 -050042#define OV5640_REG_IO_MIPI_CTRL00 0x300e
43#define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
44#define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
Steve Longerbeam19a81c12017-06-07 15:33:56 -030045#define OV5640_REG_PAD_OUTPUT00 0x3019
Hugues Fruchetf22996d2018-01-03 04:57:31 -050046#define OV5640_REG_SYSTEM_CONTROL1 0x302e
Steve Longerbeam19a81c12017-06-07 15:33:56 -030047#define OV5640_REG_SC_PLL_CTRL0 0x3034
48#define OV5640_REG_SC_PLL_CTRL1 0x3035
49#define OV5640_REG_SC_PLL_CTRL2 0x3036
50#define OV5640_REG_SC_PLL_CTRL3 0x3037
51#define OV5640_REG_SLAVE_ID 0x3100
Hugues Fruchetf22996d2018-01-03 04:57:31 -050052#define OV5640_REG_SCCB_SYS_CTRL1 0x3103
Steve Longerbeam19a81c12017-06-07 15:33:56 -030053#define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
54#define OV5640_REG_AWB_R_GAIN 0x3400
55#define OV5640_REG_AWB_G_GAIN 0x3402
56#define OV5640_REG_AWB_B_GAIN 0x3404
57#define OV5640_REG_AWB_MANUAL_CTRL 0x3406
58#define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
59#define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
60#define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
61#define OV5640_REG_AEC_PK_MANUAL 0x3503
62#define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
63#define OV5640_REG_AEC_PK_VTS 0x350c
Maxime Ripard86633412018-04-16 08:36:56 -040064#define OV5640_REG_TIMING_DVPHO 0x3808
65#define OV5640_REG_TIMING_DVPVO 0x380a
Steve Longerbeam19a81c12017-06-07 15:33:56 -030066#define OV5640_REG_TIMING_HTS 0x380c
67#define OV5640_REG_TIMING_VTS 0x380e
Hugues Fruchetce857052018-06-18 06:29:17 -040068#define OV5640_REG_TIMING_TC_REG20 0x3820
Steve Longerbeam19a81c12017-06-07 15:33:56 -030069#define OV5640_REG_TIMING_TC_REG21 0x3821
70#define OV5640_REG_AEC_CTRL00 0x3a00
71#define OV5640_REG_AEC_B50_STEP 0x3a08
72#define OV5640_REG_AEC_B60_STEP 0x3a0a
73#define OV5640_REG_AEC_CTRL0D 0x3a0d
74#define OV5640_REG_AEC_CTRL0E 0x3a0e
75#define OV5640_REG_AEC_CTRL0F 0x3a0f
76#define OV5640_REG_AEC_CTRL10 0x3a10
77#define OV5640_REG_AEC_CTRL11 0x3a11
78#define OV5640_REG_AEC_CTRL1B 0x3a1b
79#define OV5640_REG_AEC_CTRL1E 0x3a1e
80#define OV5640_REG_AEC_CTRL1F 0x3a1f
81#define OV5640_REG_HZ5060_CTRL00 0x3c00
82#define OV5640_REG_HZ5060_CTRL01 0x3c01
83#define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
84#define OV5640_REG_FRAME_CTRL01 0x4202
Hugues Fruchete3ee6912018-01-03 04:57:32 -050085#define OV5640_REG_FORMAT_CONTROL00 0x4300
Hugues Fruchetf22996d2018-01-03 04:57:31 -050086#define OV5640_REG_POLARITY_CTRL00 0x4740
Steve Longerbeam19a81c12017-06-07 15:33:56 -030087#define OV5640_REG_MIPI_CTRL00 0x4800
88#define OV5640_REG_DEBUG_MODE 0x4814
Hugues Fruchete3ee6912018-01-03 04:57:32 -050089#define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
Steve Longerbeam19a81c12017-06-07 15:33:56 -030090#define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
91#define OV5640_REG_SDE_CTRL0 0x5580
92#define OV5640_REG_SDE_CTRL1 0x5581
93#define OV5640_REG_SDE_CTRL3 0x5583
94#define OV5640_REG_SDE_CTRL4 0x5584
95#define OV5640_REG_SDE_CTRL5 0x5585
96#define OV5640_REG_AVG_READOUT 0x56a1
97
Maxime Ripard8f57c2f2018-04-16 08:36:53 -040098#define OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT 1
99#define OV5640_SCLK_ROOT_DIVIDER_DEFAULT 2
100
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300101enum ov5640_mode_id {
102 OV5640_MODE_QCIF_176_144 = 0,
103 OV5640_MODE_QVGA_320_240,
104 OV5640_MODE_VGA_640_480,
105 OV5640_MODE_NTSC_720_480,
106 OV5640_MODE_PAL_720_576,
107 OV5640_MODE_XGA_1024_768,
108 OV5640_MODE_720P_1280_720,
109 OV5640_MODE_1080P_1920_1080,
110 OV5640_MODE_QSXGA_2592_1944,
111 OV5640_NUM_MODES,
112};
113
114enum ov5640_frame_rate {
115 OV5640_15_FPS = 0,
116 OV5640_30_FPS,
117 OV5640_NUM_FRAMERATES,
118};
119
Hugues Fruchete3ee6912018-01-03 04:57:32 -0500120struct ov5640_pixfmt {
121 u32 code;
122 u32 colorspace;
123};
124
125static const struct ov5640_pixfmt ov5640_formats[] = {
Hugues Fruchetd47c4122018-01-31 04:08:10 -0500126 { MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
Hugues Fruchete3ee6912018-01-03 04:57:32 -0500127 { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
128 { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
129 { MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
130 { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
131};
132
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300133/*
134 * FIXME: remove this when a subdev API becomes available
135 * to set the MIPI CSI-2 virtual channel.
136 */
137static unsigned int virtual_channel;
Hugues Fruchet8670d702018-02-06 08:24:09 -0500138module_param(virtual_channel, uint, 0444);
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300139MODULE_PARM_DESC(virtual_channel,
140 "MIPI CSI-2 virtual channel (0..3), default 0");
141
142static const int ov5640_framerates[] = {
143 [OV5640_15_FPS] = 15,
144 [OV5640_30_FPS] = 30,
145};
146
147/* regulator supplies */
148static const char * const ov5640_supply_name[] = {
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -0500149 "DOVDD", /* Digital I/O (1.8V) supply */
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300150 "DVDD", /* Digital Core (1.5V) supply */
151 "AVDD", /* Analog (2.8V) supply */
152};
153
154#define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
155
156/*
157 * Image size under 1280 * 960 are SUBSAMPLING
158 * Image size upper 1280 * 960 are SCALING
159 */
160enum ov5640_downsize_mode {
161 SUBSAMPLING,
162 SCALING,
163};
164
165struct reg_value {
166 u16 reg_addr;
167 u8 val;
168 u8 mask;
169 u32 delay_ms;
170};
171
172struct ov5640_mode_info {
173 enum ov5640_mode_id id;
174 enum ov5640_downsize_mode dn_mode;
Maxime Riparddba13a02018-04-16 08:36:54 -0400175 u32 hact;
Maxime Ripard476dec02018-04-16 08:36:55 -0400176 u32 htot;
Maxime Riparddba13a02018-04-16 08:36:54 -0400177 u32 vact;
Maxime Ripard476dec02018-04-16 08:36:55 -0400178 u32 vtot;
Maxime Ripardaa288242018-12-03 03:44:17 -0500179 u32 pixel_clock;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300180 const struct reg_value *reg_data;
181 u32 reg_data_size;
182};
183
184struct ov5640_ctrls {
185 struct v4l2_ctrl_handler handler;
186 struct {
187 struct v4l2_ctrl *auto_exp;
188 struct v4l2_ctrl *exposure;
189 };
190 struct {
191 struct v4l2_ctrl *auto_wb;
192 struct v4l2_ctrl *blue_balance;
193 struct v4l2_ctrl *red_balance;
194 };
195 struct {
196 struct v4l2_ctrl *auto_gain;
197 struct v4l2_ctrl *gain;
198 };
199 struct v4l2_ctrl *brightness;
Mylène Josserand1068fec2018-04-16 08:36:51 -0400200 struct v4l2_ctrl *light_freq;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300201 struct v4l2_ctrl *saturation;
202 struct v4l2_ctrl *contrast;
203 struct v4l2_ctrl *hue;
204 struct v4l2_ctrl *test_pattern;
Hugues Fruchetce857052018-06-18 06:29:17 -0400205 struct v4l2_ctrl *hflip;
206 struct v4l2_ctrl *vflip;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300207};
208
209struct ov5640_dev {
210 struct i2c_client *i2c_client;
211 struct v4l2_subdev sd;
212 struct media_pad pad;
213 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
214 struct clk *xclk; /* system clock to OV5640 */
215 u32 xclk_freq;
216
217 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
218 struct gpio_desc *reset_gpio;
219 struct gpio_desc *pwdn_gpio;
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -0400220 bool upside_down;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300221
222 /* lock to protect all members below */
223 struct mutex lock;
224
225 int power_count;
226
227 struct v4l2_mbus_framefmt fmt;
Hugues Fruchetfb98e292018-08-16 05:46:53 -0400228 bool pending_fmt_change;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300229
230 const struct ov5640_mode_info *current_mode;
Hugues Fruchet985cdcb2018-09-11 09:48:21 -0400231 const struct ov5640_mode_info *last_mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300232 enum ov5640_frame_rate current_fr;
233 struct v4l2_fract frame_interval;
234
235 struct ov5640_ctrls ctrls;
236
237 u32 prev_sysclk, prev_hts;
238 u32 ae_low, ae_high, ae_target;
239
240 bool pending_mode_change;
241 bool streaming;
242};
243
244static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
245{
246 return container_of(sd, struct ov5640_dev, sd);
247}
248
249static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
250{
251 return &container_of(ctrl->handler, struct ov5640_dev,
252 ctrls.handler)->sd;
253}
254
255/*
256 * FIXME: all of these register tables are likely filled with
257 * entries that set the register to their power-on default values,
258 * and which are otherwise not touched by this driver. Those entries
259 * should be identified and removed to speed register load time
260 * over i2c.
261 */
Hugues Fruchetfb98e292018-08-16 05:46:53 -0400262/* YUV422 UYVY VGA@30fps */
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300263static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300264 {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
265 {0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500266 {0x3630, 0x36, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300267 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
268 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
269 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
270 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
271 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
272 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
273 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
274 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
275 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
276 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
277 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
278 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
279 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
280 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
281 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
282 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400283 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300284 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
285 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
286 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
287 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
288 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
289 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
290 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
291 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -0400292 {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300293 {0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
294 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -0400295 {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300296 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
297 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
298 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
299 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
300 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
301 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
302 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
303 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
304 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
305 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
306 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
307 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
308 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
309 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
310 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
311 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
312 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
313 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
314 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
315 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
316 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
317 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
318 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
319 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
320 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
321 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
322 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
323 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
324 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
325 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
326 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
327 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
328 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
329 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
330 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
331 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
332 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
333 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
334 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
335 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
336 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
337 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
338 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
339 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
340 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
341 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
342 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
343 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
344 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
345 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
346};
347
348static const struct reg_value ov5640_setting_30fps_VGA_640_480[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500349 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300350 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400351 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300352 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
353 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
354 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400355 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300356 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
357 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
358 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
359 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
360 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
361 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
362 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
363 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
364 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
365};
366
367static const struct reg_value ov5640_setting_15fps_VGA_640_480[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500368 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300369 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400370 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300371 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
372 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
373 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400374 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300375 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
376 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
377 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
378 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
379 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
380 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
381 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
382 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
383 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
384};
385
386static const struct reg_value ov5640_setting_30fps_XGA_1024_768[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500387 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300388 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400389 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300390 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
391 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
392 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400393 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300394 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
395 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
396 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
397 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x0e, 0, 0},
398 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
399 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
400 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
401 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
402 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x3503, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300403};
404
405static const struct reg_value ov5640_setting_15fps_XGA_1024_768[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500406 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300407 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500425 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300426 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500444 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300445 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500463 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300464 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500482 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300483 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500501 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300502 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500520 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300521 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500539 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300540 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500558 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300559 {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},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500578 {0x3c07, 0x07, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300579 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500598 {0x3c07, 0x07, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300599 {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},
Hugues Fruchetb7911872018-09-24 09:33:27 -0400611 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300612 {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},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500618 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300619 {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},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500633 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
634 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300635 {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},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500651 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300652 {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},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500666 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
667 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300668 {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[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500682 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300683 {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,
Maxime Ripardaa288242018-12-03 03:44:17 -0500703 56000000,
Maxime Ripard476dec02018-04-16 08:36:55 -0400704 ov5640_init_setting_30fps_VGA,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300705 ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
706};
707
708static const struct ov5640_mode_info
709ov5640_mode_data[OV5640_NUM_FRAMERATES][OV5640_NUM_MODES] = {
710 {
Maxime Ripard476dec02018-04-16 08:36:55 -0400711 {OV5640_MODE_QCIF_176_144, SUBSAMPLING,
712 176, 1896, 144, 984,
Maxime Ripardaa288242018-12-03 03:44:17 -0500713 28000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300714 ov5640_setting_15fps_QCIF_176_144,
715 ARRAY_SIZE(ov5640_setting_15fps_QCIF_176_144)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400716 {OV5640_MODE_QVGA_320_240, SUBSAMPLING,
717 320, 1896, 240, 984,
Maxime Ripardaa288242018-12-03 03:44:17 -0500718 28000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300719 ov5640_setting_15fps_QVGA_320_240,
720 ARRAY_SIZE(ov5640_setting_15fps_QVGA_320_240)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400721 {OV5640_MODE_VGA_640_480, SUBSAMPLING,
722 640, 1896, 480, 1080,
Maxime Ripardaa288242018-12-03 03:44:17 -0500723 28000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300724 ov5640_setting_15fps_VGA_640_480,
725 ARRAY_SIZE(ov5640_setting_15fps_VGA_640_480)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400726 {OV5640_MODE_NTSC_720_480, SUBSAMPLING,
727 720, 1896, 480, 984,
Maxime Ripardaa288242018-12-03 03:44:17 -0500728 28000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300729 ov5640_setting_15fps_NTSC_720_480,
730 ARRAY_SIZE(ov5640_setting_15fps_NTSC_720_480)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400731 {OV5640_MODE_PAL_720_576, SUBSAMPLING,
732 720, 1896, 576, 984,
Maxime Ripardaa288242018-12-03 03:44:17 -0500733 28000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300734 ov5640_setting_15fps_PAL_720_576,
735 ARRAY_SIZE(ov5640_setting_15fps_PAL_720_576)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400736 {OV5640_MODE_XGA_1024_768, SUBSAMPLING,
737 1024, 1896, 768, 1080,
Maxime Ripardaa288242018-12-03 03:44:17 -0500738 28000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300739 ov5640_setting_15fps_XGA_1024_768,
740 ARRAY_SIZE(ov5640_setting_15fps_XGA_1024_768)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400741 {OV5640_MODE_720P_1280_720, SUBSAMPLING,
742 1280, 1892, 720, 740,
Maxime Ripardaa288242018-12-03 03:44:17 -0500743 21000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300744 ov5640_setting_15fps_720P_1280_720,
745 ARRAY_SIZE(ov5640_setting_15fps_720P_1280_720)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400746 {OV5640_MODE_1080P_1920_1080, SCALING,
747 1920, 2500, 1080, 1120,
Maxime Ripardaa288242018-12-03 03:44:17 -0500748 42000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300749 ov5640_setting_15fps_1080P_1920_1080,
750 ARRAY_SIZE(ov5640_setting_15fps_1080P_1920_1080)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400751 {OV5640_MODE_QSXGA_2592_1944, SCALING,
752 2592, 2844, 1944, 1968,
Maxime Ripardaa288242018-12-03 03:44:17 -0500753 84000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300754 ov5640_setting_15fps_QSXGA_2592_1944,
755 ARRAY_SIZE(ov5640_setting_15fps_QSXGA_2592_1944)},
756 }, {
Maxime Ripard476dec02018-04-16 08:36:55 -0400757 {OV5640_MODE_QCIF_176_144, SUBSAMPLING,
758 176, 1896, 144, 984,
Maxime Ripardaa288242018-12-03 03:44:17 -0500759 56000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300760 ov5640_setting_30fps_QCIF_176_144,
761 ARRAY_SIZE(ov5640_setting_30fps_QCIF_176_144)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400762 {OV5640_MODE_QVGA_320_240, SUBSAMPLING,
763 320, 1896, 240, 984,
Maxime Ripardaa288242018-12-03 03:44:17 -0500764 56000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300765 ov5640_setting_30fps_QVGA_320_240,
766 ARRAY_SIZE(ov5640_setting_30fps_QVGA_320_240)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400767 {OV5640_MODE_VGA_640_480, SUBSAMPLING,
768 640, 1896, 480, 1080,
Maxime Ripardaa288242018-12-03 03:44:17 -0500769 56000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300770 ov5640_setting_30fps_VGA_640_480,
771 ARRAY_SIZE(ov5640_setting_30fps_VGA_640_480)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400772 {OV5640_MODE_NTSC_720_480, SUBSAMPLING,
773 720, 1896, 480, 984,
Maxime Ripardaa288242018-12-03 03:44:17 -0500774 56000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300775 ov5640_setting_30fps_NTSC_720_480,
776 ARRAY_SIZE(ov5640_setting_30fps_NTSC_720_480)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400777 {OV5640_MODE_PAL_720_576, SUBSAMPLING,
778 720, 1896, 576, 984,
Maxime Ripardaa288242018-12-03 03:44:17 -0500779 56000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300780 ov5640_setting_30fps_PAL_720_576,
781 ARRAY_SIZE(ov5640_setting_30fps_PAL_720_576)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400782 {OV5640_MODE_XGA_1024_768, SUBSAMPLING,
783 1024, 1896, 768, 1080,
Maxime Ripardaa288242018-12-03 03:44:17 -0500784 56000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300785 ov5640_setting_30fps_XGA_1024_768,
786 ARRAY_SIZE(ov5640_setting_30fps_XGA_1024_768)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400787 {OV5640_MODE_720P_1280_720, SUBSAMPLING,
788 1280, 1892, 720, 740,
Maxime Ripardaa288242018-12-03 03:44:17 -0500789 42000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300790 ov5640_setting_30fps_720P_1280_720,
791 ARRAY_SIZE(ov5640_setting_30fps_720P_1280_720)},
Maxime Ripard476dec02018-04-16 08:36:55 -0400792 {OV5640_MODE_1080P_1920_1080, SCALING,
793 1920, 2500, 1080, 1120,
Maxime Ripardaa288242018-12-03 03:44:17 -0500794 84000000,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300795 ov5640_setting_30fps_1080P_1920_1080,
796 ARRAY_SIZE(ov5640_setting_30fps_1080P_1920_1080)},
Maxime Ripardaa288242018-12-03 03:44:17 -0500797 {OV5640_MODE_QSXGA_2592_1944, -1, 0, 0, 0, 0, 0, NULL, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300798 },
799};
800
801static int ov5640_init_slave_id(struct ov5640_dev *sensor)
802{
803 struct i2c_client *client = sensor->i2c_client;
804 struct i2c_msg msg;
805 u8 buf[3];
806 int ret;
807
808 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
809 return 0;
810
811 buf[0] = OV5640_REG_SLAVE_ID >> 8;
812 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
813 buf[2] = client->addr << 1;
814
815 msg.addr = OV5640_DEFAULT_SLAVE_ID;
816 msg.flags = 0;
817 msg.buf = buf;
818 msg.len = sizeof(buf);
819
820 ret = i2c_transfer(client->adapter, &msg, 1);
821 if (ret < 0) {
822 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
823 return ret;
824 }
825
826 return 0;
827}
828
829static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
830{
831 struct i2c_client *client = sensor->i2c_client;
832 struct i2c_msg msg;
833 u8 buf[3];
834 int ret;
835
836 buf[0] = reg >> 8;
837 buf[1] = reg & 0xff;
838 buf[2] = val;
839
840 msg.addr = client->addr;
841 msg.flags = client->flags;
842 msg.buf = buf;
843 msg.len = sizeof(buf);
844
845 ret = i2c_transfer(client->adapter, &msg, 1);
846 if (ret < 0) {
Hugues Fruchet3924c622018-01-31 07:46:17 -0500847 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300848 __func__, reg, val);
849 return ret;
850 }
851
852 return 0;
853}
854
855static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
856{
857 struct i2c_client *client = sensor->i2c_client;
858 struct i2c_msg msg[2];
859 u8 buf[2];
860 int ret;
861
862 buf[0] = reg >> 8;
863 buf[1] = reg & 0xff;
864
865 msg[0].addr = client->addr;
866 msg[0].flags = client->flags;
867 msg[0].buf = buf;
868 msg[0].len = sizeof(buf);
869
870 msg[1].addr = client->addr;
871 msg[1].flags = client->flags | I2C_M_RD;
872 msg[1].buf = buf;
873 msg[1].len = 1;
874
875 ret = i2c_transfer(client->adapter, msg, 2);
Hugues Fruchet3924c622018-01-31 07:46:17 -0500876 if (ret < 0) {
877 dev_err(&client->dev, "%s: error: reg=%x\n",
878 __func__, reg);
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300879 return ret;
Hugues Fruchet3924c622018-01-31 07:46:17 -0500880 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300881
882 *val = buf[0];
883 return 0;
884}
885
886static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
887{
888 u8 hi, lo;
889 int ret;
890
891 ret = ov5640_read_reg(sensor, reg, &hi);
892 if (ret)
893 return ret;
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -0500894 ret = ov5640_read_reg(sensor, reg + 1, &lo);
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300895 if (ret)
896 return ret;
897
898 *val = ((u16)hi << 8) | (u16)lo;
899 return 0;
900}
901
902static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
903{
904 int ret;
905
906 ret = ov5640_write_reg(sensor, reg, val >> 8);
907 if (ret)
908 return ret;
909
910 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
911}
912
913static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
914 u8 mask, u8 val)
915{
916 u8 readval;
917 int ret;
918
919 ret = ov5640_read_reg(sensor, reg, &readval);
920 if (ret)
921 return ret;
922
923 readval &= ~mask;
924 val &= mask;
925 val |= readval;
926
927 return ov5640_write_reg(sensor, reg, val);
928}
929
Maxime Ripardaa288242018-12-03 03:44:17 -0500930/*
931 * After trying the various combinations, reading various
932 * documentations spreaded around the net, and from the various
933 * feedback, the clock tree is probably as follows:
934 *
935 * +--------------+
936 * | Ext. Clock |
937 * +-+------------+
938 * | +----------+
939 * +->| PLL1 | - reg 0x3036, for the multiplier
940 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
941 * | +--------------+
942 * +->| System Clock | - reg 0x3035, bits 4-7
943 * +-+------------+
944 * | +--------------+
945 * +->| MIPI Divider | - reg 0x3035, bits 0-3
946 * | +-+------------+
947 * | +----------------> MIPI SCLK
948 * | + +-----+
949 * | +->| / 2 |-------> MIPI BIT CLK
950 * | +-----+
951 * | +--------------+
952 * +->| PLL Root Div | - reg 0x3037, bit 4
953 * +-+------------+
954 * | +---------+
955 * +->| Bit Div | - reg 0x3035, bits 0-3
956 * +-+-------+
957 * | +-------------+
958 * +->| SCLK Div | - reg 0x3108, bits 0-1
959 * | +-+-----------+
960 * | +---------------> SCLK
961 * | +-------------+
962 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
963 * | +-+-----------+
964 * | +---------------> SCLK 2X
965 * | +-------------+
966 * +->| PCLK Div | - reg 0x3108, bits 4-5
967 * ++------------+
968 * + +-----------+
969 * +->| P_DIV | - reg 0x3035, bits 0-3
970 * +-----+-----+
971 * +------------> PCLK
972 *
973 * This is deviating from the datasheet at least for the register
974 * 0x3108, since it's said here that the PCLK would be clocked from
975 * the PLL.
976 *
977 * There seems to be also (unverified) constraints:
978 * - the PLL pre-divider output rate should be in the 4-27MHz range
979 * - the PLL multiplier output rate should be in the 500-1000MHz range
980 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
981 *
982 * In the two latter cases, these constraints are met since our
983 * factors are hardcoded. If we were to change that, we would need to
984 * take this into account. The only varying parts are the PLL
985 * multiplier and the system clock divider, which are shared between
986 * all these clocks so won't cause any issue.
987 */
988
989/*
990 * This is supposed to be ranging from 1 to 8, but the value is always
991 * set to 3 in the vendor kernels.
992 */
993#define OV5640_PLL_PREDIV 3
994
995#define OV5640_PLL_MULT_MIN 4
996#define OV5640_PLL_MULT_MAX 252
997
998/*
999 * This is supposed to be ranging from 1 to 16, but the value is
1000 * always set to either 1 or 2 in the vendor kernels.
1001 */
1002#define OV5640_SYSDIV_MIN 1
1003#define OV5640_SYSDIV_MAX 16
1004
1005/*
1006 * Hardcode these values for scaler and non-scaler modes.
1007 * FIXME: to be re-calcualted for 1 data lanes setups
1008 */
1009#define OV5640_MIPI_DIV_PCLK 2
1010#define OV5640_MIPI_DIV_SCLK 1
1011
1012/*
1013 * This is supposed to be ranging from 1 to 2, but the value is always
1014 * set to 2 in the vendor kernels.
1015 */
1016#define OV5640_PLL_ROOT_DIV 2
1017#define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
1018
1019/*
1020 * We only supports 8-bit formats at the moment
1021 */
1022#define OV5640_BIT_DIV 2
1023#define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
1024
1025/*
1026 * This is supposed to be ranging from 1 to 8, but the value is always
1027 * set to 2 in the vendor kernels.
1028 */
1029#define OV5640_SCLK_ROOT_DIV 2
1030
1031/*
1032 * This is hardcoded so that the consistency is maintained between SCLK and
1033 * SCLK 2x.
1034 */
1035#define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
1036
1037/*
1038 * This is supposed to be ranging from 1 to 8, but the value is always
1039 * set to 1 in the vendor kernels.
1040 */
1041#define OV5640_PCLK_ROOT_DIV 1
1042#define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
1043
1044static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
1045 u8 pll_prediv, u8 pll_mult,
1046 u8 sysdiv)
1047{
1048 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
1049
1050 /* PLL1 output cannot exceed 1GHz. */
1051 if (sysclk / 1000000 > 1000)
1052 return 0;
1053
1054 return sysclk / sysdiv;
1055}
1056
1057static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
1058 unsigned long rate,
1059 u8 *pll_prediv, u8 *pll_mult,
1060 u8 *sysdiv)
1061{
1062 unsigned long best = ~0;
1063 u8 best_sysdiv = 1, best_mult = 1;
1064 u8 _sysdiv, _pll_mult;
1065
1066 for (_sysdiv = OV5640_SYSDIV_MIN;
1067 _sysdiv <= OV5640_SYSDIV_MAX;
1068 _sysdiv++) {
1069 for (_pll_mult = OV5640_PLL_MULT_MIN;
1070 _pll_mult <= OV5640_PLL_MULT_MAX;
1071 _pll_mult++) {
1072 unsigned long _rate;
1073
1074 /*
1075 * The PLL multiplier cannot be odd if above
1076 * 127.
1077 */
1078 if (_pll_mult > 127 && (_pll_mult % 2))
1079 continue;
1080
1081 _rate = ov5640_compute_sys_clk(sensor,
1082 OV5640_PLL_PREDIV,
1083 _pll_mult, _sysdiv);
1084
1085 /*
1086 * We have reached the maximum allowed PLL1 output,
1087 * increase sysdiv.
1088 */
1089 if (!rate)
1090 break;
1091
1092 /*
1093 * Prefer rates above the expected clock rate than
1094 * below, even if that means being less precise.
1095 */
1096 if (_rate < rate)
1097 continue;
1098
1099 if (abs(rate - _rate) < abs(rate - best)) {
1100 best = _rate;
1101 best_sysdiv = _sysdiv;
1102 best_mult = _pll_mult;
1103 }
1104
1105 if (_rate == rate)
1106 goto out;
1107 }
1108 }
1109
1110out:
1111 *sysdiv = best_sysdiv;
1112 *pll_prediv = OV5640_PLL_PREDIV;
1113 *pll_mult = best_mult;
1114
1115 return best;
1116}
1117
1118/*
1119 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
1120 * for the MIPI CSI-2 output.
1121 *
1122 * @rate: The requested bandwidth per lane in bytes per second.
1123 * 'Bandwidth Per Lane' is calculated as:
1124 * bpl = HTOT * VTOT * FPS * bpp / num_lanes;
1125 *
1126 * This function use the requested bandwidth to calculate:
1127 * - sample_rate = bpl / (bpp / num_lanes);
1128 * = bpl / (PLL_RDIV * BIT_DIV * PCLK_DIV * MIPI_DIV / num_lanes);
1129 *
1130 * - mipi_sclk = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR)
1131 *
1132 * with these fixed parameters:
1133 * PLL_RDIV = 2;
1134 * BIT_DIVIDER = 2; (MIPI_BIT_MODE == 8 ? 2 : 2,5);
1135 * PCLK_DIV = 1;
1136 *
1137 * The MIPI clock generation differs for modes that use the scaler and modes
1138 * that do not. In case the scaler is in use, the MIPI_SCLK generates the MIPI
1139 * BIT CLk, and thus:
1140 *
1141 * - mipi_sclk = bpl / MIPI_DIV / 2;
1142 * MIPI_DIV = 1;
1143 *
1144 * For modes that do not go through the scaler, the MIPI BIT CLOCK is generated
1145 * from the pixel clock, and thus:
1146 *
1147 * - sample_rate = bpl / (bpp / num_lanes);
1148 * = bpl / (2 * 2 * 1 * MIPI_DIV / num_lanes);
1149 * = bpl / (4 * MIPI_DIV / num_lanes);
1150 * - MIPI_DIV = bpp / (4 * num_lanes);
1151 *
1152 * FIXME: this have been tested with 16bpp and 2 lanes setup only.
1153 * MIPI_DIV is fixed to value 2, but it -might- be changed according to the
1154 * above formula for setups with 1 lane or image formats with different bpp.
1155 *
1156 * FIXME: this deviates from the sensor manual documentation which is quite
1157 * thin on the MIPI clock tree generation part.
1158 */
1159static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor,
1160 unsigned long rate)
1161{
1162 const struct ov5640_mode_info *mode = sensor->current_mode;
1163 u8 prediv, mult, sysdiv;
1164 u8 mipi_div;
1165 int ret;
1166
1167 /*
1168 * 1280x720 is reported to use 'SUBSAMPLING' only,
1169 * but according to the sensor manual it goes through the
1170 * scaler before subsampling.
1171 */
1172 if (mode->dn_mode == SCALING ||
1173 (mode->id == OV5640_MODE_720P_1280_720))
1174 mipi_div = OV5640_MIPI_DIV_SCLK;
1175 else
1176 mipi_div = OV5640_MIPI_DIV_PCLK;
1177
1178 ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv);
1179
1180 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1181 0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT);
1182
1183 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1184 0xff, sysdiv << 4 | mipi_div);
1185 if (ret)
1186 return ret;
1187
1188 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
1189 if (ret)
1190 return ret;
1191
1192 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1193 0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv);
1194 if (ret)
1195 return ret;
1196
1197 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER,
1198 0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS);
1199}
1200
1201static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1202 unsigned long rate,
1203 u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1204 u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1205{
1206 unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1207 OV5640_PCLK_ROOT_DIV;
1208
1209 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1210 sysdiv);
1211 *pll_rdiv = OV5640_PLL_ROOT_DIV;
1212 *bit_div = OV5640_BIT_DIV;
1213 *pclk_div = OV5640_PCLK_ROOT_DIV;
1214
1215 return _rate / *pll_rdiv / *bit_div / *pclk_div;
1216}
1217
1218static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate)
1219{
1220 u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1221 int ret;
1222
1223 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1224 &bit_div, &pclk_div);
1225
1226 if (bit_div == 2)
1227 bit_div = 8;
1228
1229 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1230 0x0f, bit_div);
1231 if (ret)
1232 return ret;
1233
1234 /*
1235 * We need to set sysdiv according to the clock, and to clear
1236 * the MIPI divider.
1237 */
1238 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1239 0xff, sysdiv << 4);
1240 if (ret)
1241 return ret;
1242
1243 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1244 0xff, mult);
1245 if (ret)
1246 return ret;
1247
1248 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1249 0x1f, prediv | ((pll_rdiv - 1) << 4));
1250 if (ret)
1251 return ret;
1252
1253 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1254 (ilog2(pclk_div) << 4));
1255}
1256
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001257/* download ov5640 settings to sensor through i2c */
Jacopo Mondibad17742018-07-18 06:06:23 -04001258static int ov5640_set_timings(struct ov5640_dev *sensor,
1259 const struct ov5640_mode_info *mode)
1260{
1261 int ret;
1262
1263 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
1264 if (ret < 0)
1265 return ret;
1266
1267 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
1268 if (ret < 0)
1269 return ret;
1270
1271 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
1272 if (ret < 0)
1273 return ret;
1274
1275 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
1276}
1277
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001278static int ov5640_load_regs(struct ov5640_dev *sensor,
1279 const struct ov5640_mode_info *mode)
1280{
1281 const struct reg_value *regs = mode->reg_data;
1282 unsigned int i;
1283 u32 delay_ms;
1284 u16 reg_addr;
1285 u8 mask, val;
1286 int ret = 0;
1287
1288 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
1289 delay_ms = regs->delay_ms;
1290 reg_addr = regs->reg_addr;
1291 val = regs->val;
1292 mask = regs->mask;
1293
1294 if (mask)
1295 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1296 else
1297 ret = ov5640_write_reg(sensor, reg_addr, val);
1298 if (ret)
1299 break;
1300
1301 if (delay_ms)
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05001302 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001303 }
1304
Jacopo Mondibad17742018-07-18 06:06:23 -04001305 return ov5640_set_timings(sensor, mode);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001306}
1307
Hugues Fruchetdc29a1c2018-09-11 09:48:17 -04001308static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1309{
1310 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1311 BIT(0), on ? 0 : BIT(0));
1312}
1313
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001314/* read exposure, in number of line periods */
1315static int ov5640_get_exposure(struct ov5640_dev *sensor)
1316{
1317 int exp, ret;
1318 u8 temp;
1319
1320 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1321 if (ret)
1322 return ret;
1323 exp = ((int)temp & 0x0f) << 16;
1324 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1325 if (ret)
1326 return ret;
1327 exp |= ((int)temp << 8);
1328 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1329 if (ret)
1330 return ret;
1331 exp |= (int)temp;
1332
1333 return exp >> 4;
1334}
1335
1336/* write exposure, given number of line periods */
1337static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1338{
1339 int ret;
1340
1341 exposure <<= 4;
1342
1343 ret = ov5640_write_reg(sensor,
1344 OV5640_REG_AEC_PK_EXPOSURE_LO,
1345 exposure & 0xff);
1346 if (ret)
1347 return ret;
1348 ret = ov5640_write_reg(sensor,
1349 OV5640_REG_AEC_PK_EXPOSURE_MED,
1350 (exposure >> 8) & 0xff);
1351 if (ret)
1352 return ret;
1353 return ov5640_write_reg(sensor,
1354 OV5640_REG_AEC_PK_EXPOSURE_HI,
1355 (exposure >> 16) & 0x0f);
1356}
1357
1358static int ov5640_get_gain(struct ov5640_dev *sensor)
1359{
1360 u16 gain;
1361 int ret;
1362
1363 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1364 if (ret)
1365 return ret;
1366
1367 return gain & 0x3ff;
1368}
1369
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001370static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1371{
1372 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1373 (u16)gain & 0x3ff);
1374}
1375
1376static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1377{
1378 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1379 BIT(1), on ? 0 : BIT(1));
1380}
1381
Hugues Fruchetf22996d2018-01-03 04:57:31 -05001382static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1383{
1384 int ret;
1385 unsigned int flags = sensor->ep.bus.parallel.flags;
1386 u8 pclk_pol = 0;
1387 u8 hsync_pol = 0;
1388 u8 vsync_pol = 0;
1389
1390 /*
1391 * Note about parallel port configuration.
1392 *
1393 * When configured in parallel mode, the OV5640 will
1394 * output 10 bits data on DVP data lines [9:0].
1395 * If only 8 bits data are wanted, the 8 bits data lines
1396 * of the camera interface must be physically connected
1397 * on the DVP data lines [9:2].
1398 *
1399 * Control lines polarity can be configured through
1400 * devicetree endpoint control lines properties.
1401 * If no endpoint control lines properties are set,
1402 * polarity will be as below:
1403 * - VSYNC: active high
1404 * - HREF: active low
1405 * - PCLK: active low
1406 */
1407
1408 if (on) {
1409 /*
1410 * reset MIPI PCLK/SERCLK divider
1411 *
1412 * SC PLL CONTRL1 0
1413 * - [3..0]: MIPI PCLK/SERCLK divider
1414 */
1415 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1, 0x0f, 0);
1416 if (ret)
1417 return ret;
1418
1419 /*
1420 * configure parallel port control lines polarity
1421 *
1422 * POLARITY CTRL0
1423 * - [5]: PCLK polarity (0: active low, 1: active high)
1424 * - [1]: HREF polarity (0: active low, 1: active high)
1425 * - [0]: VSYNC polarity (mismatch here between
1426 * datasheet and hardware, 0 is active high
1427 * and 1 is active low...)
1428 */
1429 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1430 pclk_pol = 1;
1431 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1432 hsync_pol = 1;
1433 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1434 vsync_pol = 1;
1435
1436 ret = ov5640_write_reg(sensor,
1437 OV5640_REG_POLARITY_CTRL00,
1438 (pclk_pol << 5) |
1439 (hsync_pol << 1) |
1440 vsync_pol);
1441
1442 if (ret)
1443 return ret;
1444 }
1445
1446 /*
1447 * powerdown MIPI TX/RX PHY & disable MIPI
1448 *
1449 * MIPI CONTROL 00
1450 * 4: PWDN PHY TX
1451 * 3: PWDN PHY RX
1452 * 2: MIPI enable
1453 */
1454 ret = ov5640_write_reg(sensor,
1455 OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1456 if (ret)
1457 return ret;
1458
1459 /*
1460 * enable VSYNC/HREF/PCLK DVP control lines
1461 * & D[9:6] DVP data lines
1462 *
1463 * PAD OUTPUT ENABLE 01
1464 * - 6: VSYNC output enable
1465 * - 5: HREF output enable
1466 * - 4: PCLK output enable
1467 * - [3:0]: D[9:6] output enable
1468 */
1469 ret = ov5640_write_reg(sensor,
1470 OV5640_REG_PAD_OUTPUT_ENABLE01,
1471 on ? 0x7f : 0);
1472 if (ret)
1473 return ret;
1474
1475 /*
1476 * enable D[5:0] DVP data lines
1477 *
1478 * PAD OUTPUT ENABLE 02
1479 * - [7:2]: D[5:0] output enable
1480 */
1481 return ov5640_write_reg(sensor,
1482 OV5640_REG_PAD_OUTPUT_ENABLE02,
1483 on ? 0xfc : 0);
1484}
1485
1486static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001487{
1488 int ret;
1489
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04001490 /*
1491 * Enable/disable the MIPI interface
1492 *
1493 * 0x300e = on ? 0x45 : 0x40
1494 *
1495 * FIXME: the sensor manual (version 2.03) reports
1496 * [7:5] = 000 : 1 data lane mode
1497 * [7:5] = 001 : 2 data lanes mode
1498 * But this settings do not work, while the following ones
1499 * have been validated for 2 data lanes mode.
1500 *
1501 * [7:5] = 010 : 2 data lanes mode
1502 * [4] = 0 : Power up MIPI HS Tx
1503 * [3] = 0 : Power up MIPI LS Rx
1504 * [2] = 1/0 : MIPI interface enable/disable
1505 * [1:0] = 01/00: FIXME: 'debug'
1506 */
1507 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1508 on ? 0x45 : 0x40);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001509 if (ret)
1510 return ret;
1511
1512 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1513 on ? 0x00 : 0x0f);
1514}
1515
1516static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1517{
1518 /* calculate sysclk */
1519 u32 xvclk = sensor->xclk_freq / 10000;
1520 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1521 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1522 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1523 u8 temp1, temp2;
1524 int ret;
1525
1526 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1527 if (ret)
1528 return ret;
1529 temp2 = temp1 & 0x0f;
1530 if (temp2 == 8 || temp2 == 10)
1531 bit_div2x = temp2 / 2;
1532
1533 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1534 if (ret)
1535 return ret;
1536 sysdiv = temp1 >> 4;
1537 if (sysdiv == 0)
1538 sysdiv = 16;
1539
1540 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1541 if (ret)
1542 return ret;
1543 multiplier = temp1;
1544
1545 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1546 if (ret)
1547 return ret;
1548 prediv = temp1 & 0x0f;
1549 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1550
1551 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1552 if (ret)
1553 return ret;
1554 temp2 = temp1 & 0x03;
1555 sclk_rdiv = sclk_rdiv_map[temp2];
1556
1557 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1558 return -EINVAL;
1559
1560 VCO = xvclk * multiplier / prediv;
1561
1562 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1563
1564 return sysclk;
1565}
1566
1567static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1568{
1569 /* read HTS from register settings */
1570 u8 mode;
1571 int ret;
1572
1573 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1574 if (ret)
1575 return ret;
1576 mode &= 0xfb;
1577 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1578}
1579
1580static int ov5640_get_hts(struct ov5640_dev *sensor)
1581{
1582 /* read HTS from register settings */
1583 u16 hts;
1584 int ret;
1585
1586 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1587 if (ret)
1588 return ret;
1589 return hts;
1590}
1591
1592static int ov5640_get_vts(struct ov5640_dev *sensor)
1593{
1594 u16 vts;
1595 int ret;
1596
1597 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1598 if (ret)
1599 return ret;
1600 return vts;
1601}
1602
1603static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1604{
1605 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1606}
1607
1608static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1609{
1610 /* get banding filter value */
1611 int ret, light_freq = 0;
1612 u8 temp, temp1;
1613
1614 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1615 if (ret)
1616 return ret;
1617
1618 if (temp & 0x80) {
1619 /* manual */
1620 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1621 &temp1);
1622 if (ret)
1623 return ret;
1624 if (temp1 & 0x04) {
1625 /* 50Hz */
1626 light_freq = 50;
1627 } else {
1628 /* 60Hz */
1629 light_freq = 60;
1630 }
1631 } else {
1632 /* auto */
1633 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1634 &temp1);
1635 if (ret)
1636 return ret;
1637
1638 if (temp1 & 0x01) {
1639 /* 50Hz */
1640 light_freq = 50;
1641 } else {
1642 /* 60Hz */
1643 }
1644 }
1645
1646 return light_freq;
1647}
1648
1649static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1650{
1651 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1652 int ret;
1653
1654 /* read preview PCLK */
1655 ret = ov5640_get_sysclk(sensor);
1656 if (ret < 0)
1657 return ret;
1658 if (ret == 0)
1659 return -EINVAL;
1660 sensor->prev_sysclk = ret;
1661 /* read preview HTS */
1662 ret = ov5640_get_hts(sensor);
1663 if (ret < 0)
1664 return ret;
1665 if (ret == 0)
1666 return -EINVAL;
1667 sensor->prev_hts = ret;
1668
1669 /* read preview VTS */
1670 ret = ov5640_get_vts(sensor);
1671 if (ret < 0)
1672 return ret;
1673 prev_vts = ret;
1674
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001675 /* calculate banding filter */
1676 /* 60Hz */
1677 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1678 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1679 if (ret)
1680 return ret;
1681 if (!band_step60)
1682 return -EINVAL;
1683 max_band60 = (int)((prev_vts - 4) / band_step60);
1684 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1685 if (ret)
1686 return ret;
1687
1688 /* 50Hz */
1689 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1690 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1691 if (ret)
1692 return ret;
1693 if (!band_step50)
1694 return -EINVAL;
1695 max_band50 = (int)((prev_vts - 4) / band_step50);
1696 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1697}
1698
1699static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1700{
1701 /* stable in high */
1702 u32 fast_high, fast_low;
1703 int ret;
1704
1705 sensor->ae_low = target * 23 / 25; /* 0.92 */
1706 sensor->ae_high = target * 27 / 25; /* 1.08 */
1707
1708 fast_high = sensor->ae_high << 1;
1709 if (fast_high > 255)
1710 fast_high = 255;
1711
1712 fast_low = sensor->ae_low >> 1;
1713
1714 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1715 if (ret)
1716 return ret;
1717 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1718 if (ret)
1719 return ret;
1720 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1721 if (ret)
1722 return ret;
1723 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1724 if (ret)
1725 return ret;
1726 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1727 if (ret)
1728 return ret;
1729 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1730}
1731
Hugues Fruchetc2c3f422018-09-11 09:48:19 -04001732static int ov5640_get_binning(struct ov5640_dev *sensor)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001733{
1734 u8 temp;
1735 int ret;
1736
1737 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1738 if (ret)
1739 return ret;
Hugues Fruchetc2c3f422018-09-11 09:48:19 -04001740
1741 return temp & BIT(0);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001742}
1743
Hugues Fruchetce857052018-06-18 06:29:17 -04001744static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1745{
1746 int ret;
1747
1748 /*
1749 * TIMING TC REG21:
1750 * - [0]: Horizontal binning enable
1751 */
1752 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1753 BIT(0), enable ? BIT(0) : 0);
1754 if (ret)
1755 return ret;
1756 /*
1757 * TIMING TC REG20:
1758 * - [0]: Undocumented, but hardcoded init sequences
1759 * are always setting REG21/REG20 bit 0 to same value...
1760 */
1761 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1762 BIT(0), enable ? BIT(0) : 0);
1763}
1764
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001765static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1766{
Hugues Fruchet8670d702018-02-06 08:24:09 -05001767 struct i2c_client *client = sensor->i2c_client;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001768 u8 temp, channel = virtual_channel;
1769 int ret;
1770
Hugues Fruchet8670d702018-02-06 08:24:09 -05001771 if (channel > 3) {
1772 dev_err(&client->dev,
1773 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
1774 __func__, channel);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001775 return -EINVAL;
Hugues Fruchet8670d702018-02-06 08:24:09 -05001776 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001777
1778 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1779 if (ret)
1780 return ret;
1781 temp &= ~(3 << 6);
1782 temp |= (channel << 6);
1783 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1784}
1785
1786static const struct ov5640_mode_info *
1787ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1788 int width, int height, bool nearest)
1789{
Hugues Fruchet3c4a7372018-06-20 04:40:57 -04001790 const struct ov5640_mode_info *mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001791
Hugues Fruchet3c4a7372018-06-20 04:40:57 -04001792 mode = v4l2_find_nearest_size(ov5640_mode_data[fr],
1793 ARRAY_SIZE(ov5640_mode_data[fr]),
1794 hact, vact,
1795 width, height);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001796
Hugues Fruchet3c4a7372018-06-20 04:40:57 -04001797 if (!mode ||
1798 (!nearest && (mode->hact != width || mode->vact != height)))
1799 return NULL;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001800
1801 return mode;
1802}
1803
1804/*
1805 * sensor changes between scaling and subsampling, go through
1806 * exposure calculation
1807 */
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05001808static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
1809 const struct ov5640_mode_info *mode)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001810{
1811 u32 prev_shutter, prev_gain16;
1812 u32 cap_shutter, cap_gain16;
1813 u32 cap_sysclk, cap_hts, cap_vts;
1814 u32 light_freq, cap_bandfilt, cap_maxband;
1815 u32 cap_gain16_shutter;
1816 u8 average;
1817 int ret;
1818
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05001819 if (!mode->reg_data)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001820 return -EINVAL;
1821
1822 /* read preview shutter */
1823 ret = ov5640_get_exposure(sensor);
1824 if (ret < 0)
1825 return ret;
1826 prev_shutter = ret;
Hugues Fruchetc2c3f422018-09-11 09:48:19 -04001827 ret = ov5640_get_binning(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001828 if (ret < 0)
1829 return ret;
1830 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1831 mode->id != OV5640_MODE_1080P_1920_1080)
1832 prev_shutter *= 2;
1833
1834 /* read preview gain */
1835 ret = ov5640_get_gain(sensor);
1836 if (ret < 0)
1837 return ret;
1838 prev_gain16 = ret;
1839
1840 /* get average */
1841 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1842 if (ret)
1843 return ret;
1844
1845 /* turn off night mode for capture */
1846 ret = ov5640_set_night_mode(sensor);
1847 if (ret < 0)
1848 return ret;
1849
1850 /* Write capture setting */
1851 ret = ov5640_load_regs(sensor, mode);
1852 if (ret < 0)
1853 return ret;
1854
1855 /* read capture VTS */
1856 ret = ov5640_get_vts(sensor);
1857 if (ret < 0)
1858 return ret;
1859 cap_vts = ret;
1860 ret = ov5640_get_hts(sensor);
1861 if (ret < 0)
1862 return ret;
1863 if (ret == 0)
1864 return -EINVAL;
1865 cap_hts = ret;
1866
1867 ret = ov5640_get_sysclk(sensor);
1868 if (ret < 0)
1869 return ret;
1870 if (ret == 0)
1871 return -EINVAL;
1872 cap_sysclk = ret;
1873
1874 /* calculate capture banding filter */
1875 ret = ov5640_get_light_freq(sensor);
1876 if (ret < 0)
1877 return ret;
1878 light_freq = ret;
1879
1880 if (light_freq == 60) {
1881 /* 60Hz */
1882 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1883 } else {
1884 /* 50Hz */
1885 cap_bandfilt = cap_sysclk * 100 / cap_hts;
1886 }
1887
1888 if (!sensor->prev_sysclk) {
1889 ret = ov5640_get_sysclk(sensor);
1890 if (ret < 0)
1891 return ret;
1892 if (ret == 0)
1893 return -EINVAL;
1894 sensor->prev_sysclk = ret;
1895 }
1896
1897 if (!cap_bandfilt)
1898 return -EINVAL;
1899
1900 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1901
1902 /* calculate capture shutter/gain16 */
1903 if (average > sensor->ae_low && average < sensor->ae_high) {
1904 /* in stable range */
1905 cap_gain16_shutter =
1906 prev_gain16 * prev_shutter *
1907 cap_sysclk / sensor->prev_sysclk *
1908 sensor->prev_hts / cap_hts *
1909 sensor->ae_target / average;
1910 } else {
1911 cap_gain16_shutter =
1912 prev_gain16 * prev_shutter *
1913 cap_sysclk / sensor->prev_sysclk *
1914 sensor->prev_hts / cap_hts;
1915 }
1916
1917 /* gain to shutter */
1918 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
1919 /* shutter < 1/100 */
1920 cap_shutter = cap_gain16_shutter / 16;
1921 if (cap_shutter < 1)
1922 cap_shutter = 1;
1923
1924 cap_gain16 = cap_gain16_shutter / cap_shutter;
1925 if (cap_gain16 < 16)
1926 cap_gain16 = 16;
1927 } else {
1928 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
1929 /* exposure reach max */
1930 cap_shutter = cap_bandfilt * cap_maxband;
1931 if (!cap_shutter)
1932 return -EINVAL;
1933
1934 cap_gain16 = cap_gain16_shutter / cap_shutter;
1935 } else {
1936 /* 1/100 < (cap_shutter = n/100) =< max */
1937 cap_shutter =
1938 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
1939 * cap_bandfilt;
1940 if (!cap_shutter)
1941 return -EINVAL;
1942
1943 cap_gain16 = cap_gain16_shutter / cap_shutter;
1944 }
1945 }
1946
1947 /* set capture gain */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001948 ret = ov5640_set_gain(sensor, cap_gain16);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001949 if (ret)
1950 return ret;
1951
1952 /* write capture shutter */
1953 if (cap_shutter > (cap_vts - 4)) {
1954 cap_vts = cap_shutter + 4;
1955 ret = ov5640_set_vts(sensor, cap_vts);
1956 if (ret < 0)
1957 return ret;
1958 }
1959
1960 /* set exposure */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001961 return ov5640_set_exposure(sensor, cap_shutter);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001962}
1963
1964/*
1965 * if sensor changes inside scaling or subsampling
1966 * change mode directly
1967 */
1968static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001969 const struct ov5640_mode_info *mode)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001970{
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05001971 if (!mode->reg_data)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001972 return -EINVAL;
1973
1974 /* Write capture setting */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001975 return ov5640_load_regs(sensor, mode);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001976}
1977
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04001978static int ov5640_set_mode(struct ov5640_dev *sensor)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001979{
1980 const struct ov5640_mode_info *mode = sensor->current_mode;
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04001981 const struct ov5640_mode_info *orig_mode = sensor->last_mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001982 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001983 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
Hugues Fruchetdc29a1c2018-09-11 09:48:17 -04001984 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
Maxime Ripardaa288242018-12-03 03:44:17 -05001985 unsigned long rate;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001986 int ret;
1987
1988 dn_mode = mode->dn_mode;
1989 orig_dn_mode = orig_mode->dn_mode;
1990
1991 /* auto gain and exposure must be turned off when changing modes */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001992 if (auto_gain) {
1993 ret = ov5640_set_autogain(sensor, false);
1994 if (ret)
1995 return ret;
1996 }
Maxime Ripardbf4a4b52018-04-16 08:36:52 -04001997
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001998 if (auto_exp) {
1999 ret = ov5640_set_autoexposure(sensor, false);
2000 if (ret)
2001 goto restore_auto_gain;
2002 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002003
Maxime Ripardaa288242018-12-03 03:44:17 -05002004 /*
2005 * All the formats we support have 16 bits per pixel, seems to require
2006 * the same rate than YUV, so we can just use 16 bpp all the time.
2007 */
2008 rate = mode->pixel_clock * 16;
2009 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
2010 rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes;
2011 ret = ov5640_set_mipi_pclk(sensor, rate);
2012 } else {
2013 rate = rate / sensor->ep.bus.parallel.bus_width;
2014 ret = ov5640_set_dvp_pclk(sensor, rate);
2015 }
2016
2017 if (ret < 0)
2018 return 0;
2019
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002020 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
2021 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
2022 /*
2023 * change between subsampling and scaling
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002024 * go through exposure calculation
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002025 */
2026 ret = ov5640_set_mode_exposure_calc(sensor, mode);
2027 } else {
2028 /*
2029 * change inside subsampling or scaling
2030 * download firmware directly
2031 */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002032 ret = ov5640_set_mode_direct(sensor, mode);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002033 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002034 if (ret < 0)
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002035 goto restore_auto_exp_gain;
2036
2037 /* restore auto gain and exposure */
2038 if (auto_gain)
2039 ov5640_set_autogain(sensor, true);
2040 if (auto_exp)
2041 ov5640_set_autoexposure(sensor, true);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002042
Hugues Fruchetce857052018-06-18 06:29:17 -04002043 ret = ov5640_set_binning(sensor, dn_mode != SCALING);
2044 if (ret < 0)
2045 return ret;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002046 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
2047 if (ret < 0)
2048 return ret;
2049 ret = ov5640_get_light_freq(sensor);
2050 if (ret < 0)
2051 return ret;
2052 ret = ov5640_set_bandingfilter(sensor);
2053 if (ret < 0)
2054 return ret;
2055 ret = ov5640_set_virtual_channel(sensor);
2056 if (ret < 0)
2057 return ret;
2058
2059 sensor->pending_mode_change = false;
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04002060 sensor->last_mode = mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002061
2062 return 0;
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002063
2064restore_auto_exp_gain:
2065 if (auto_exp)
2066 ov5640_set_autoexposure(sensor, true);
2067restore_auto_gain:
2068 if (auto_gain)
2069 ov5640_set_autogain(sensor, true);
2070
2071 return ret;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002072}
2073
Akinobu Mita19ad26f2018-03-11 11:34:41 -04002074static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2075 struct v4l2_mbus_framefmt *format);
2076
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002077/* restore the last set video mode after chip power-on */
2078static int ov5640_restore_mode(struct ov5640_dev *sensor)
2079{
2080 int ret;
2081
2082 /* first load the initial register values */
2083 ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
2084 if (ret < 0)
2085 return ret;
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04002086 sensor->last_mode = &ov5640_mode_init_data;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002087
Maxime Ripard8f57c2f2018-04-16 08:36:53 -04002088 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
2089 (ilog2(OV5640_SCLK2X_ROOT_DIVIDER_DEFAULT) << 2) |
2090 ilog2(OV5640_SCLK_ROOT_DIVIDER_DEFAULT));
2091 if (ret)
2092 return ret;
2093
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002094 /* now restore the last capture mode */
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04002095 ret = ov5640_set_mode(sensor);
Akinobu Mita19ad26f2018-03-11 11:34:41 -04002096 if (ret < 0)
2097 return ret;
2098
2099 return ov5640_set_framefmt(sensor, &sensor->fmt);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002100}
2101
2102static void ov5640_power(struct ov5640_dev *sensor, bool enable)
2103{
Hugues Fruchet1fddc5d2018-01-03 04:57:28 -05002104 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002105}
2106
2107static void ov5640_reset(struct ov5640_dev *sensor)
2108{
2109 if (!sensor->reset_gpio)
2110 return;
2111
Hugues Fruchet1fddc5d2018-01-03 04:57:28 -05002112 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002113
2114 /* camera power cycle */
2115 ov5640_power(sensor, false);
2116 usleep_range(5000, 10000);
2117 ov5640_power(sensor, true);
2118 usleep_range(5000, 10000);
2119
Hugues Fruchet1fddc5d2018-01-03 04:57:28 -05002120 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002121 usleep_range(1000, 2000);
2122
Hugues Fruchet1fddc5d2018-01-03 04:57:28 -05002123 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002124 usleep_range(5000, 10000);
2125}
2126
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05002127static int ov5640_set_power_on(struct ov5640_dev *sensor)
2128{
2129 struct i2c_client *client = sensor->i2c_client;
2130 int ret;
2131
2132 ret = clk_prepare_enable(sensor->xclk);
2133 if (ret) {
2134 dev_err(&client->dev, "%s: failed to enable clock\n",
2135 __func__);
2136 return ret;
2137 }
2138
2139 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
2140 sensor->supplies);
2141 if (ret) {
2142 dev_err(&client->dev, "%s: failed to enable regulators\n",
2143 __func__);
2144 goto xclk_off;
2145 }
2146
2147 ov5640_reset(sensor);
2148 ov5640_power(sensor, true);
2149
2150 ret = ov5640_init_slave_id(sensor);
2151 if (ret)
2152 goto power_off;
2153
2154 return 0;
2155
2156power_off:
2157 ov5640_power(sensor, false);
2158 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2159xclk_off:
2160 clk_disable_unprepare(sensor->xclk);
2161 return ret;
2162}
2163
2164static void ov5640_set_power_off(struct ov5640_dev *sensor)
2165{
2166 ov5640_power(sensor, false);
2167 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2168 clk_disable_unprepare(sensor->xclk);
2169}
2170
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002171static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
2172{
2173 int ret = 0;
2174
2175 if (on) {
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05002176 ret = ov5640_set_power_on(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002177 if (ret)
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05002178 return ret;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002179
2180 ret = ov5640_restore_mode(sensor);
2181 if (ret)
2182 goto power_off;
2183
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04002184 /* We're done here for DVP bus, while CSI-2 needs setup. */
Sakari Ailus2d95e7e2018-07-03 17:19:27 -04002185 if (sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY)
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04002186 return 0;
2187
2188 /*
2189 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
2190 *
2191 * 0x300e = 0x40
2192 * [7:5] = 010 : 2 data lanes mode (see FIXME note in
2193 * "ov5640_set_stream_mipi()")
2194 * [4] = 0 : Power up MIPI HS Tx
2195 * [3] = 0 : Power up MIPI LS Rx
2196 * [2] = 0 : MIPI interface disabled
2197 */
2198 ret = ov5640_write_reg(sensor,
2199 OV5640_REG_IO_MIPI_CTRL00, 0x40);
2200 if (ret)
2201 goto power_off;
2202
2203 /*
2204 * Gate clock and set LP11 in 'no packets mode' (idle)
2205 *
2206 * 0x4800 = 0x24
2207 * [5] = 1 : Gate clock when 'no packets'
2208 * [2] = 1 : MIPI bus in LP11 when 'no packets'
2209 */
2210 ret = ov5640_write_reg(sensor,
2211 OV5640_REG_MIPI_CTRL00, 0x24);
2212 if (ret)
2213 goto power_off;
2214
2215 /*
2216 * Set data lanes and clock in LP11 when 'sleeping'
2217 *
2218 * 0x3019 = 0x70
2219 * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping'
2220 * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping'
2221 * [4] = 1 : MIPI clock lane in LP11 when 'sleeping'
2222 */
2223 ret = ov5640_write_reg(sensor,
2224 OV5640_REG_PAD_OUTPUT00, 0x70);
2225 if (ret)
2226 goto power_off;
2227
2228 /* Give lanes some time to coax into LP11 state. */
2229 usleep_range(500, 1000);
2230
2231 } else {
Sakari Ailus2d95e7e2018-07-03 17:19:27 -04002232 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04002233 /* Reset MIPI bus settings to their default values. */
2234 ov5640_write_reg(sensor,
2235 OV5640_REG_IO_MIPI_CTRL00, 0x58);
2236 ov5640_write_reg(sensor,
2237 OV5640_REG_MIPI_CTRL00, 0x04);
2238 ov5640_write_reg(sensor,
2239 OV5640_REG_PAD_OUTPUT00, 0x00);
Hugues Fruchetf22996d2018-01-03 04:57:31 -05002240 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002241
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04002242 ov5640_set_power_off(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002243 }
2244
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04002245 return 0;
2246
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002247power_off:
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05002248 ov5640_set_power_off(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002249 return ret;
2250}
2251
2252/* --------------- Subdev Operations --------------- */
2253
2254static int ov5640_s_power(struct v4l2_subdev *sd, int on)
2255{
2256 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2257 int ret = 0;
2258
2259 mutex_lock(&sensor->lock);
2260
2261 /*
2262 * If the power count is modified from 0 to != 0 or from != 0 to 0,
2263 * update the power state.
2264 */
2265 if (sensor->power_count == !on) {
2266 ret = ov5640_set_power(sensor, !!on);
2267 if (ret)
2268 goto out;
2269 }
2270
2271 /* Update the power count. */
2272 sensor->power_count += on ? 1 : -1;
2273 WARN_ON(sensor->power_count < 0);
2274out:
2275 mutex_unlock(&sensor->lock);
2276
2277 if (on && !ret && sensor->power_count == 1) {
2278 /* restore controls */
2279 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2280 }
2281
2282 return ret;
2283}
2284
2285static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2286 struct v4l2_fract *fi,
2287 u32 width, u32 height)
2288{
2289 const struct ov5640_mode_info *mode;
2290 u32 minfps, maxfps, fps;
2291 int ret;
2292
2293 minfps = ov5640_framerates[OV5640_15_FPS];
2294 maxfps = ov5640_framerates[OV5640_30_FPS];
2295
2296 if (fi->numerator == 0) {
2297 fi->denominator = maxfps;
2298 fi->numerator = 1;
2299 return OV5640_30_FPS;
2300 }
2301
2302 fps = DIV_ROUND_CLOSEST(fi->denominator, fi->numerator);
2303
2304 fi->numerator = 1;
2305 if (fps > maxfps)
2306 fi->denominator = maxfps;
2307 else if (fps < minfps)
2308 fi->denominator = minfps;
2309 else if (2 * fps >= 2 * minfps + (maxfps - minfps))
2310 fi->denominator = maxfps;
2311 else
2312 fi->denominator = minfps;
2313
2314 ret = (fi->denominator == minfps) ? OV5640_15_FPS : OV5640_30_FPS;
2315
2316 mode = ov5640_find_mode(sensor, ret, width, height, false);
2317 return mode ? ret : -EINVAL;
2318}
2319
2320static int ov5640_get_fmt(struct v4l2_subdev *sd,
2321 struct v4l2_subdev_pad_config *cfg,
2322 struct v4l2_subdev_format *format)
2323{
2324 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2325 struct v4l2_mbus_framefmt *fmt;
2326
2327 if (format->pad != 0)
2328 return -EINVAL;
2329
2330 mutex_lock(&sensor->lock);
2331
2332 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2333 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
2334 format->pad);
2335 else
2336 fmt = &sensor->fmt;
2337
2338 format->format = *fmt;
2339
2340 mutex_unlock(&sensor->lock);
2341
2342 return 0;
2343}
2344
2345static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2346 struct v4l2_mbus_framefmt *fmt,
2347 enum ov5640_frame_rate fr,
2348 const struct ov5640_mode_info **new_mode)
2349{
2350 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2351 const struct ov5640_mode_info *mode;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002352 int i;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002353
2354 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
2355 if (!mode)
2356 return -EINVAL;
Maxime Riparddba13a02018-04-16 08:36:54 -04002357 fmt->width = mode->hact;
2358 fmt->height = mode->vact;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002359
2360 if (new_mode)
2361 *new_mode = mode;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002362
2363 for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2364 if (ov5640_formats[i].code == fmt->code)
2365 break;
2366 if (i >= ARRAY_SIZE(ov5640_formats))
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002367 i = 0;
2368
2369 fmt->code = ov5640_formats[i].code;
2370 fmt->colorspace = ov5640_formats[i].colorspace;
2371 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2372 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2373 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002374
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002375 return 0;
2376}
2377
2378static int ov5640_set_fmt(struct v4l2_subdev *sd,
2379 struct v4l2_subdev_pad_config *cfg,
2380 struct v4l2_subdev_format *format)
2381{
2382 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2383 const struct ov5640_mode_info *new_mode;
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002384 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
Jacopo Mondi07115442018-12-03 03:44:16 -05002385 struct v4l2_mbus_framefmt *fmt;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002386 int ret;
2387
2388 if (format->pad != 0)
2389 return -EINVAL;
2390
2391 mutex_lock(&sensor->lock);
2392
2393 if (sensor->streaming) {
2394 ret = -EBUSY;
2395 goto out;
2396 }
2397
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002398 ret = ov5640_try_fmt_internal(sd, mbus_fmt,
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002399 sensor->current_fr, &new_mode);
2400 if (ret)
2401 goto out;
2402
Jacopo Mondi07115442018-12-03 03:44:16 -05002403 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2404 fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
2405 else
2406 fmt = &sensor->fmt;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002407
Jacopo Mondi07115442018-12-03 03:44:16 -05002408 *fmt = *mbus_fmt;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002409
Hugues Fruchet6949d862018-07-04 09:04:38 -04002410 if (new_mode != sensor->current_mode) {
2411 sensor->current_mode = new_mode;
Hugues Fruchet6949d862018-07-04 09:04:38 -04002412 sensor->pending_mode_change = true;
2413 }
Jacopo Mondi07115442018-12-03 03:44:16 -05002414 if (mbus_fmt->code != sensor->fmt.code)
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002415 sensor->pending_fmt_change = true;
Jacopo Mondi07115442018-12-03 03:44:16 -05002416
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002417out:
2418 mutex_unlock(&sensor->lock);
2419 return ret;
2420}
2421
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002422static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2423 struct v4l2_mbus_framefmt *format)
2424{
2425 int ret = 0;
2426 bool is_rgb = false;
Hugues Fruchetd47c4122018-01-31 04:08:10 -05002427 bool is_jpeg = false;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002428 u8 val;
2429
2430 switch (format->code) {
2431 case MEDIA_BUS_FMT_UYVY8_2X8:
2432 /* YUV422, UYVY */
2433 val = 0x3f;
2434 break;
2435 case MEDIA_BUS_FMT_YUYV8_2X8:
2436 /* YUV422, YUYV */
2437 val = 0x30;
2438 break;
2439 case MEDIA_BUS_FMT_RGB565_2X8_LE:
2440 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
2441 val = 0x6F;
2442 is_rgb = true;
2443 break;
2444 case MEDIA_BUS_FMT_RGB565_2X8_BE:
2445 /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
2446 val = 0x61;
2447 is_rgb = true;
2448 break;
Hugues Fruchetd47c4122018-01-31 04:08:10 -05002449 case MEDIA_BUS_FMT_JPEG_1X8:
2450 /* YUV422, YUYV */
2451 val = 0x30;
2452 is_jpeg = true;
2453 break;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002454 default:
2455 return -EINVAL;
2456 }
2457
2458 /* FORMAT CONTROL00: YUV and RGB formatting */
2459 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, val);
2460 if (ret)
2461 return ret;
2462
2463 /* FORMAT MUX CONTROL: ISP YUV or RGB */
Hugues Fruchetd47c4122018-01-31 04:08:10 -05002464 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL,
2465 is_rgb ? 0x01 : 0x00);
2466 if (ret)
2467 return ret;
2468
2469 /*
2470 * TIMING TC REG21:
2471 * - [5]: JPEG enable
2472 */
2473 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2474 BIT(5), is_jpeg ? BIT(5) : 0);
2475 if (ret)
2476 return ret;
2477
2478 /*
2479 * SYSTEM RESET02:
2480 * - [4]: Reset JFIFO
2481 * - [3]: Reset SFIFO
2482 * - [2]: Reset JPEG
2483 */
2484 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2485 BIT(4) | BIT(3) | BIT(2),
2486 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2487 if (ret)
2488 return ret;
2489
2490 /*
2491 * CLOCK ENABLE02:
2492 * - [5]: Enable JPEG 2x clock
2493 * - [3]: Enable JPEG clock
2494 */
2495 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2496 BIT(5) | BIT(3),
2497 is_jpeg ? (BIT(5) | BIT(3)) : 0);
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002498}
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002499
2500/*
2501 * Sensor Controls.
2502 */
2503
2504static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
2505{
2506 int ret;
2507
2508 if (value) {
2509 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2510 BIT(0), BIT(0));
2511 if (ret)
2512 return ret;
2513 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
2514 } else {
2515 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
2516 }
2517
2518 return ret;
2519}
2520
2521static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
2522{
2523 int ret;
2524
2525 if (value) {
2526 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2527 BIT(2), BIT(2));
2528 if (ret)
2529 return ret;
2530 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
2531 value & 0xff);
2532 } else {
2533 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
2534 }
2535
2536 return ret;
2537}
2538
2539static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
2540{
2541 int ret;
2542
2543 if (value) {
2544 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2545 BIT(1), BIT(1));
2546 if (ret)
2547 return ret;
2548 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
2549 value & 0xff);
2550 if (ret)
2551 return ret;
2552 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
2553 value & 0xff);
2554 } else {
2555 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
2556 }
2557
2558 return ret;
2559}
2560
2561static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
2562{
2563 int ret;
2564
2565 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
2566 BIT(0), awb ? 0 : 1);
2567 if (ret)
2568 return ret;
2569
2570 if (!awb) {
2571 u16 red = (u16)sensor->ctrls.red_balance->val;
2572 u16 blue = (u16)sensor->ctrls.blue_balance->val;
2573
2574 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
2575 if (ret)
2576 return ret;
2577 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
2578 }
2579
2580 return ret;
2581}
2582
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002583static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
2584 enum v4l2_exposure_auto_type auto_exposure)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002585{
2586 struct ov5640_ctrls *ctrls = &sensor->ctrls;
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002587 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002588 int ret = 0;
2589
2590 if (ctrls->auto_exp->is_new) {
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002591 ret = ov5640_set_autoexposure(sensor, auto_exp);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002592 if (ret)
2593 return ret;
2594 }
2595
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002596 if (!auto_exp && ctrls->exposure->is_new) {
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002597 u16 max_exp;
2598
2599 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
2600 &max_exp);
2601 if (ret)
2602 return ret;
2603 ret = ov5640_get_vts(sensor);
2604 if (ret < 0)
2605 return ret;
2606 max_exp += ret;
Hugues Fruchet6146fde2018-01-22 05:06:40 -05002607 ret = 0;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002608
2609 if (ctrls->exposure->val < max_exp)
2610 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
2611 }
2612
2613 return ret;
2614}
2615
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002616static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002617{
2618 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2619 int ret = 0;
2620
2621 if (ctrls->auto_gain->is_new) {
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002622 ret = ov5640_set_autogain(sensor, auto_gain);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002623 if (ret)
2624 return ret;
2625 }
2626
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002627 if (!auto_gain && ctrls->gain->is_new)
2628 ret = ov5640_set_gain(sensor, ctrls->gain->val);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002629
2630 return ret;
2631}
2632
2633static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
2634{
2635 return ov5640_mod_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
2636 0xa4, value ? 0xa4 : 0);
2637}
2638
Mylène Josserand1068fec2018-04-16 08:36:51 -04002639static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
2640{
2641 int ret;
2642
2643 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
2644 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
2645 0 : BIT(7));
2646 if (ret)
2647 return ret;
2648
2649 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
2650 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
2651 BIT(2) : 0);
2652}
2653
Hugues Fruchetce857052018-06-18 06:29:17 -04002654static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2655{
2656 /*
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002657 * If sensor is mounted upside down, mirror logic is inversed.
2658 *
Hugues Fruchetce857052018-06-18 06:29:17 -04002659 * Sensor is a BSI (Back Side Illuminated) one,
2660 * so image captured is physically mirrored.
2661 * This is why mirror logic is inversed in
2662 * order to cancel this mirror effect.
2663 */
2664
2665 /*
2666 * TIMING TC REG21:
2667 * - [2]: ISP mirror
2668 * - [1]: Sensor mirror
2669 */
2670 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2671 BIT(2) | BIT(1),
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002672 (!(value ^ sensor->upside_down)) ?
2673 (BIT(2) | BIT(1)) : 0);
Hugues Fruchetce857052018-06-18 06:29:17 -04002674}
2675
2676static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2677{
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002678 /* If sensor is mounted upside down, flip logic is inversed */
2679
Hugues Fruchetce857052018-06-18 06:29:17 -04002680 /*
2681 * TIMING TC REG20:
2682 * - [2]: ISP vflip
2683 * - [1]: Sensor vflip
2684 */
2685 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2686 BIT(2) | BIT(1),
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002687 (value ^ sensor->upside_down) ?
2688 (BIT(2) | BIT(1)) : 0);
Hugues Fruchetce857052018-06-18 06:29:17 -04002689}
2690
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002691static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
2692{
2693 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2694 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2695 int val;
2696
2697 /* v4l2_ctrl_lock() locks our own mutex */
2698
2699 switch (ctrl->id) {
2700 case V4L2_CID_AUTOGAIN:
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002701 val = ov5640_get_gain(sensor);
2702 if (val < 0)
2703 return val;
2704 sensor->ctrls.gain->val = val;
2705 break;
2706 case V4L2_CID_EXPOSURE_AUTO:
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002707 val = ov5640_get_exposure(sensor);
2708 if (val < 0)
2709 return val;
2710 sensor->ctrls.exposure->val = val;
2711 break;
2712 }
2713
2714 return 0;
2715}
2716
2717static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
2718{
2719 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2720 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2721 int ret;
2722
2723 /* v4l2_ctrl_lock() locks our own mutex */
2724
2725 /*
2726 * If the device is not powered up by the host driver do
2727 * not apply any controls to H/W at this time. Instead
2728 * the controls will be restored right after power-up.
2729 */
2730 if (sensor->power_count == 0)
2731 return 0;
2732
2733 switch (ctrl->id) {
2734 case V4L2_CID_AUTOGAIN:
2735 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
2736 break;
2737 case V4L2_CID_EXPOSURE_AUTO:
2738 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
2739 break;
2740 case V4L2_CID_AUTO_WHITE_BALANCE:
2741 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
2742 break;
2743 case V4L2_CID_HUE:
2744 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
2745 break;
2746 case V4L2_CID_CONTRAST:
2747 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
2748 break;
2749 case V4L2_CID_SATURATION:
2750 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
2751 break;
2752 case V4L2_CID_TEST_PATTERN:
2753 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
2754 break;
Mylène Josserand1068fec2018-04-16 08:36:51 -04002755 case V4L2_CID_POWER_LINE_FREQUENCY:
2756 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
2757 break;
Hugues Fruchetce857052018-06-18 06:29:17 -04002758 case V4L2_CID_HFLIP:
2759 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
2760 break;
2761 case V4L2_CID_VFLIP:
2762 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
2763 break;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002764 default:
2765 ret = -EINVAL;
2766 break;
2767 }
2768
2769 return ret;
2770}
2771
2772static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
2773 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
2774 .s_ctrl = ov5640_s_ctrl,
2775};
2776
2777static const char * const test_pattern_menu[] = {
2778 "Disabled",
2779 "Color bars",
2780};
2781
2782static int ov5640_init_controls(struct ov5640_dev *sensor)
2783{
2784 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
2785 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2786 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2787 int ret;
2788
2789 v4l2_ctrl_handler_init(hdl, 32);
2790
2791 /* we can use our own mutex for the ctrl lock */
2792 hdl->lock = &sensor->lock;
2793
2794 /* Auto/manual white balance */
2795 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
2796 V4L2_CID_AUTO_WHITE_BALANCE,
2797 0, 1, 1, 1);
2798 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
2799 0, 4095, 1, 0);
2800 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
2801 0, 4095, 1, 0);
2802 /* Auto/manual exposure */
2803 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
2804 V4L2_CID_EXPOSURE_AUTO,
2805 V4L2_EXPOSURE_MANUAL, 0,
2806 V4L2_EXPOSURE_AUTO);
2807 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2808 0, 65535, 1, 0);
2809 /* Auto/manual gain */
2810 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
2811 0, 1, 1, 1);
2812 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
2813 0, 1023, 1, 0);
2814
2815 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
2816 0, 255, 1, 64);
2817 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
2818 0, 359, 1, 0);
2819 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
2820 0, 255, 1, 0);
2821 ctrls->test_pattern =
2822 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2823 ARRAY_SIZE(test_pattern_menu) - 1,
2824 0, 0, test_pattern_menu);
Hugues Fruchetce857052018-06-18 06:29:17 -04002825 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2826 0, 1, 1, 0);
2827 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2828 0, 1, 1, 0);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002829
Mylène Josserand1068fec2018-04-16 08:36:51 -04002830 ctrls->light_freq =
2831 v4l2_ctrl_new_std_menu(hdl, ops,
2832 V4L2_CID_POWER_LINE_FREQUENCY,
2833 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
2834 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
2835
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002836 if (hdl->error) {
2837 ret = hdl->error;
2838 goto free_ctrls;
2839 }
2840
2841 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2842 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2843
2844 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2845 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
2846 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
2847
2848 sensor->sd.ctrl_handler = hdl;
2849 return 0;
2850
2851free_ctrls:
2852 v4l2_ctrl_handler_free(hdl);
2853 return ret;
2854}
2855
2856static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
2857 struct v4l2_subdev_pad_config *cfg,
2858 struct v4l2_subdev_frame_size_enum *fse)
2859{
2860 if (fse->pad != 0)
2861 return -EINVAL;
2862 if (fse->index >= OV5640_NUM_MODES)
2863 return -EINVAL;
2864
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05002865 fse->min_width =
Maxime Riparddba13a02018-04-16 08:36:54 -04002866 ov5640_mode_data[0][fse->index].hact;
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05002867 fse->max_width = fse->min_width;
2868 fse->min_height =
Maxime Riparddba13a02018-04-16 08:36:54 -04002869 ov5640_mode_data[0][fse->index].vact;
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05002870 fse->max_height = fse->min_height;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002871
2872 return 0;
2873}
2874
2875static int ov5640_enum_frame_interval(
2876 struct v4l2_subdev *sd,
2877 struct v4l2_subdev_pad_config *cfg,
2878 struct v4l2_subdev_frame_interval_enum *fie)
2879{
2880 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2881 struct v4l2_fract tpf;
2882 int ret;
2883
2884 if (fie->pad != 0)
2885 return -EINVAL;
2886 if (fie->index >= OV5640_NUM_FRAMERATES)
2887 return -EINVAL;
2888
2889 tpf.numerator = 1;
2890 tpf.denominator = ov5640_framerates[fie->index];
2891
2892 ret = ov5640_try_frame_interval(sensor, &tpf,
2893 fie->width, fie->height);
2894 if (ret < 0)
2895 return -EINVAL;
2896
2897 fie->interval = tpf;
2898 return 0;
2899}
2900
2901static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
2902 struct v4l2_subdev_frame_interval *fi)
2903{
2904 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2905
2906 mutex_lock(&sensor->lock);
2907 fi->interval = sensor->frame_interval;
2908 mutex_unlock(&sensor->lock);
2909
2910 return 0;
2911}
2912
2913static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
2914 struct v4l2_subdev_frame_interval *fi)
2915{
2916 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2917 const struct ov5640_mode_info *mode;
2918 int frame_rate, ret = 0;
2919
2920 if (fi->pad != 0)
2921 return -EINVAL;
2922
2923 mutex_lock(&sensor->lock);
2924
2925 if (sensor->streaming) {
2926 ret = -EBUSY;
2927 goto out;
2928 }
2929
2930 mode = sensor->current_mode;
2931
2932 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
Maxime Riparddba13a02018-04-16 08:36:54 -04002933 mode->hact, mode->vact);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002934 if (frame_rate < 0)
2935 frame_rate = OV5640_15_FPS;
2936
Hugues Fruchet3c4a7372018-06-20 04:40:57 -04002937 mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
2938 mode->vact, true);
2939 if (!mode) {
2940 ret = -EINVAL;
2941 goto out;
2942 }
2943
Hugues Fruchet09299832018-10-04 07:21:57 -04002944 if (mode != sensor->current_mode ||
2945 frame_rate != sensor->current_fr) {
2946 sensor->current_fr = frame_rate;
2947 sensor->frame_interval = fi->interval;
Hugues Fruchet6949d862018-07-04 09:04:38 -04002948 sensor->current_mode = mode;
2949 sensor->pending_mode_change = true;
2950 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002951out:
2952 mutex_unlock(&sensor->lock);
2953 return ret;
2954}
2955
2956static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05002957 struct v4l2_subdev_pad_config *cfg,
2958 struct v4l2_subdev_mbus_code_enum *code)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002959{
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002960 if (code->pad != 0)
2961 return -EINVAL;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002962 if (code->index >= ARRAY_SIZE(ov5640_formats))
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002963 return -EINVAL;
2964
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002965 code->code = ov5640_formats[code->index].code;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002966 return 0;
2967}
2968
2969static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
2970{
2971 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2972 int ret = 0;
2973
2974 mutex_lock(&sensor->lock);
2975
2976 if (sensor->streaming == !enable) {
2977 if (enable && sensor->pending_mode_change) {
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04002978 ret = ov5640_set_mode(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002979 if (ret)
2980 goto out;
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002981 }
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002982
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002983 if (enable && sensor->pending_fmt_change) {
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002984 ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2985 if (ret)
2986 goto out;
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002987 sensor->pending_fmt_change = false;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002988 }
2989
Sakari Ailus2d95e7e2018-07-03 17:19:27 -04002990 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
Hugues Fruchetf22996d2018-01-03 04:57:31 -05002991 ret = ov5640_set_stream_mipi(sensor, enable);
2992 else
2993 ret = ov5640_set_stream_dvp(sensor, enable);
2994
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002995 if (!ret)
2996 sensor->streaming = enable;
2997 }
2998out:
2999 mutex_unlock(&sensor->lock);
3000 return ret;
3001}
3002
3003static const struct v4l2_subdev_core_ops ov5640_core_ops = {
3004 .s_power = ov5640_s_power,
Akinobu Mita2d18fbc2018-11-12 11:00:52 -05003005 .log_status = v4l2_ctrl_subdev_log_status,
3006 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
3007 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003008};
3009
3010static const struct v4l2_subdev_video_ops ov5640_video_ops = {
3011 .g_frame_interval = ov5640_g_frame_interval,
3012 .s_frame_interval = ov5640_s_frame_interval,
3013 .s_stream = ov5640_s_stream,
3014};
3015
3016static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
3017 .enum_mbus_code = ov5640_enum_mbus_code,
3018 .get_fmt = ov5640_get_fmt,
3019 .set_fmt = ov5640_set_fmt,
3020 .enum_frame_size = ov5640_enum_frame_size,
3021 .enum_frame_interval = ov5640_enum_frame_interval,
3022};
3023
3024static const struct v4l2_subdev_ops ov5640_subdev_ops = {
3025 .core = &ov5640_core_ops,
3026 .video = &ov5640_video_ops,
3027 .pad = &ov5640_pad_ops,
3028};
3029
3030static int ov5640_get_regulators(struct ov5640_dev *sensor)
3031{
3032 int i;
3033
3034 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
3035 sensor->supplies[i].supply = ov5640_supply_name[i];
3036
3037 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
3038 OV5640_NUM_SUPPLIES,
3039 sensor->supplies);
3040}
3041
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05003042static int ov5640_check_chip_id(struct ov5640_dev *sensor)
3043{
3044 struct i2c_client *client = sensor->i2c_client;
3045 int ret = 0;
3046 u16 chip_id;
3047
3048 ret = ov5640_set_power_on(sensor);
3049 if (ret)
3050 return ret;
3051
3052 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
3053 if (ret) {
3054 dev_err(&client->dev, "%s: failed to read chip identifier\n",
3055 __func__);
3056 goto power_off;
3057 }
3058
3059 if (chip_id != 0x5640) {
3060 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
3061 __func__, chip_id);
3062 ret = -ENXIO;
3063 }
3064
3065power_off:
3066 ov5640_set_power_off(sensor);
3067 return ret;
3068}
3069
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003070static int ov5640_probe(struct i2c_client *client,
3071 const struct i2c_device_id *id)
3072{
3073 struct device *dev = &client->dev;
3074 struct fwnode_handle *endpoint;
3075 struct ov5640_dev *sensor;
Hugues Fruchete6441fd2018-03-06 12:04:39 -05003076 struct v4l2_mbus_framefmt *fmt;
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04003077 u32 rotation;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003078 int ret;
3079
3080 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
3081 if (!sensor)
3082 return -ENOMEM;
3083
3084 sensor->i2c_client = client;
Hugues Fruchetfb98e292018-08-16 05:46:53 -04003085
3086 /*
3087 * default init sequence initialize sensor to
3088 * YUV422 UYVY VGA@30fps
3089 */
Hugues Fruchete6441fd2018-03-06 12:04:39 -05003090 fmt = &sensor->fmt;
Hugues Fruchetfb98e292018-08-16 05:46:53 -04003091 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
3092 fmt->colorspace = V4L2_COLORSPACE_SRGB;
Hugues Fruchete6441fd2018-03-06 12:04:39 -05003093 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
3094 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
3095 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
3096 fmt->width = 640;
3097 fmt->height = 480;
3098 fmt->field = V4L2_FIELD_NONE;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003099 sensor->frame_interval.numerator = 1;
3100 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
3101 sensor->current_fr = OV5640_30_FPS;
3102 sensor->current_mode =
3103 &ov5640_mode_data[OV5640_30_FPS][OV5640_MODE_VGA_640_480];
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04003104 sensor->last_mode = sensor->current_mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003105
3106 sensor->ae_target = 52;
3107
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04003108 /* optional indication of physical rotation of sensor */
3109 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
3110 &rotation);
3111 if (!ret) {
3112 switch (rotation) {
3113 case 180:
3114 sensor->upside_down = true;
3115 /* fall through */
3116 case 0:
3117 break;
3118 default:
3119 dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
3120 rotation);
3121 }
3122 }
3123
Sakari Ailusce96bcf2018-04-24 06:25:47 -04003124 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3125 NULL);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003126 if (!endpoint) {
3127 dev_err(dev, "endpoint node not found\n");
3128 return -EINVAL;
3129 }
3130
3131 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3132 fwnode_handle_put(endpoint);
3133 if (ret) {
3134 dev_err(dev, "Could not parse endpoint\n");
3135 return ret;
3136 }
3137
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003138 /* get system clock (xclk) */
3139 sensor->xclk = devm_clk_get(dev, "xclk");
3140 if (IS_ERR(sensor->xclk)) {
3141 dev_err(dev, "failed to get xclk\n");
3142 return PTR_ERR(sensor->xclk);
3143 }
3144
3145 sensor->xclk_freq = clk_get_rate(sensor->xclk);
3146 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3147 sensor->xclk_freq > OV5640_XCLK_MAX) {
3148 dev_err(dev, "xclk frequency out of range: %d Hz\n",
3149 sensor->xclk_freq);
3150 return -EINVAL;
3151 }
3152
3153 /* request optional power down pin */
3154 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3155 GPIOD_OUT_HIGH);
3156 /* request optional reset pin */
3157 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3158 GPIOD_OUT_HIGH);
3159
3160 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3161
Akinobu Mita2d18fbc2018-11-12 11:00:52 -05003162 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3163 V4L2_SUBDEV_FL_HAS_EVENTS;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003164 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3165 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3166 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3167 if (ret)
3168 return ret;
3169
3170 ret = ov5640_get_regulators(sensor);
3171 if (ret)
3172 return ret;
3173
3174 mutex_init(&sensor->lock);
3175
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05003176 ret = ov5640_check_chip_id(sensor);
3177 if (ret)
3178 goto entity_cleanup;
3179
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003180 ret = ov5640_init_controls(sensor);
3181 if (ret)
3182 goto entity_cleanup;
3183
3184 ret = v4l2_async_register_subdev(&sensor->sd);
3185 if (ret)
3186 goto free_ctrls;
3187
3188 return 0;
3189
3190free_ctrls:
3191 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3192entity_cleanup:
3193 mutex_destroy(&sensor->lock);
3194 media_entity_cleanup(&sensor->sd.entity);
3195 return ret;
3196}
3197
3198static int ov5640_remove(struct i2c_client *client)
3199{
3200 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3201 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3202
3203 v4l2_async_unregister_subdev(&sensor->sd);
3204 mutex_destroy(&sensor->lock);
3205 media_entity_cleanup(&sensor->sd.entity);
3206 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3207
3208 return 0;
3209}
3210
3211static const struct i2c_device_id ov5640_id[] = {
3212 {"ov5640", 0},
3213 {},
3214};
3215MODULE_DEVICE_TABLE(i2c, ov5640_id);
3216
3217static const struct of_device_id ov5640_dt_ids[] = {
3218 { .compatible = "ovti,ov5640" },
3219 { /* sentinel */ }
3220};
3221MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3222
3223static struct i2c_driver ov5640_i2c_driver = {
3224 .driver = {
3225 .name = "ov5640",
3226 .of_match_table = ov5640_dt_ids,
3227 },
3228 .id_table = ov5640_id,
3229 .probe = ov5640_probe,
3230 .remove = ov5640_remove,
3231};
3232
3233module_i2c_driver(ov5640_i2c_driver);
3234
3235MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
3236MODULE_LICENSE("GPL");