blob: a1fd69a21df1f08ad5fcd18b9d162797939aefb4 [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
98enum ov5640_mode_id {
99 OV5640_MODE_QCIF_176_144 = 0,
100 OV5640_MODE_QVGA_320_240,
101 OV5640_MODE_VGA_640_480,
102 OV5640_MODE_NTSC_720_480,
103 OV5640_MODE_PAL_720_576,
104 OV5640_MODE_XGA_1024_768,
105 OV5640_MODE_720P_1280_720,
106 OV5640_MODE_1080P_1920_1080,
107 OV5640_MODE_QSXGA_2592_1944,
108 OV5640_NUM_MODES,
109};
110
111enum ov5640_frame_rate {
112 OV5640_15_FPS = 0,
113 OV5640_30_FPS,
Maxime Riparde823fb12018-12-03 03:44:26 -0500114 OV5640_60_FPS,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300115 OV5640_NUM_FRAMERATES,
116};
117
Loic Poulainb7ed3ab2018-11-02 12:38:43 -0400118enum ov5640_format_mux {
119 OV5640_FMT_MUX_YUV422 = 0,
120 OV5640_FMT_MUX_RGB,
121 OV5640_FMT_MUX_DITHER,
122 OV5640_FMT_MUX_RAW_DPC,
123 OV5640_FMT_MUX_SNR_RAW,
124 OV5640_FMT_MUX_RAW_CIP,
125};
126
Hugues Fruchete3ee6912018-01-03 04:57:32 -0500127struct ov5640_pixfmt {
128 u32 code;
129 u32 colorspace;
130};
131
132static const struct ov5640_pixfmt ov5640_formats[] = {
Hugues Fruchetd47c4122018-01-31 04:08:10 -0500133 { MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
Hugues Fruchete3ee6912018-01-03 04:57:32 -0500134 { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
135 { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
136 { MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
137 { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
Loic Poulainb7ed3ab2018-11-02 12:38:43 -0400138 { MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB, },
139 { MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_COLORSPACE_SRGB, },
140 { MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_COLORSPACE_SRGB, },
141 { MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_COLORSPACE_SRGB, },
Hugues Fruchete3ee6912018-01-03 04:57:32 -0500142};
143
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300144/*
145 * FIXME: remove this when a subdev API becomes available
146 * to set the MIPI CSI-2 virtual channel.
147 */
148static unsigned int virtual_channel;
Hugues Fruchet8670d702018-02-06 08:24:09 -0500149module_param(virtual_channel, uint, 0444);
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300150MODULE_PARM_DESC(virtual_channel,
151 "MIPI CSI-2 virtual channel (0..3), default 0");
152
153static const int ov5640_framerates[] = {
154 [OV5640_15_FPS] = 15,
155 [OV5640_30_FPS] = 30,
Maxime Riparde823fb12018-12-03 03:44:26 -0500156 [OV5640_60_FPS] = 60,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300157};
158
159/* regulator supplies */
160static const char * const ov5640_supply_name[] = {
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -0500161 "DOVDD", /* Digital I/O (1.8V) supply */
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300162 "DVDD", /* Digital Core (1.5V) supply */
163 "AVDD", /* Analog (2.8V) supply */
164};
165
166#define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
167
168/*
169 * Image size under 1280 * 960 are SUBSAMPLING
170 * Image size upper 1280 * 960 are SCALING
171 */
172enum ov5640_downsize_mode {
173 SUBSAMPLING,
174 SCALING,
175};
176
177struct reg_value {
178 u16 reg_addr;
179 u8 val;
180 u8 mask;
181 u32 delay_ms;
182};
183
184struct ov5640_mode_info {
185 enum ov5640_mode_id id;
186 enum ov5640_downsize_mode dn_mode;
Maxime Riparddba13a02018-04-16 08:36:54 -0400187 u32 hact;
Maxime Ripard476dec02018-04-16 08:36:55 -0400188 u32 htot;
Maxime Riparddba13a02018-04-16 08:36:54 -0400189 u32 vact;
Maxime Ripard476dec02018-04-16 08:36:55 -0400190 u32 vtot;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300191 const struct reg_value *reg_data;
192 u32 reg_data_size;
193};
194
195struct ov5640_ctrls {
196 struct v4l2_ctrl_handler handler;
197 struct {
198 struct v4l2_ctrl *auto_exp;
199 struct v4l2_ctrl *exposure;
200 };
201 struct {
202 struct v4l2_ctrl *auto_wb;
203 struct v4l2_ctrl *blue_balance;
204 struct v4l2_ctrl *red_balance;
205 };
206 struct {
207 struct v4l2_ctrl *auto_gain;
208 struct v4l2_ctrl *gain;
209 };
210 struct v4l2_ctrl *brightness;
Mylène Josserand1068fec2018-04-16 08:36:51 -0400211 struct v4l2_ctrl *light_freq;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300212 struct v4l2_ctrl *saturation;
213 struct v4l2_ctrl *contrast;
214 struct v4l2_ctrl *hue;
215 struct v4l2_ctrl *test_pattern;
Hugues Fruchetce857052018-06-18 06:29:17 -0400216 struct v4l2_ctrl *hflip;
217 struct v4l2_ctrl *vflip;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300218};
219
220struct ov5640_dev {
221 struct i2c_client *i2c_client;
222 struct v4l2_subdev sd;
223 struct media_pad pad;
224 struct v4l2_fwnode_endpoint ep; /* the parsed DT endpoint info */
225 struct clk *xclk; /* system clock to OV5640 */
226 u32 xclk_freq;
227
228 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
229 struct gpio_desc *reset_gpio;
230 struct gpio_desc *pwdn_gpio;
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -0400231 bool upside_down;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300232
233 /* lock to protect all members below */
234 struct mutex lock;
235
236 int power_count;
237
238 struct v4l2_mbus_framefmt fmt;
Hugues Fruchetfb98e292018-08-16 05:46:53 -0400239 bool pending_fmt_change;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300240
241 const struct ov5640_mode_info *current_mode;
Hugues Fruchet985cdcb2018-09-11 09:48:21 -0400242 const struct ov5640_mode_info *last_mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300243 enum ov5640_frame_rate current_fr;
244 struct v4l2_fract frame_interval;
245
246 struct ov5640_ctrls ctrls;
247
248 u32 prev_sysclk, prev_hts;
249 u32 ae_low, ae_high, ae_target;
250
251 bool pending_mode_change;
252 bool streaming;
253};
254
255static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
256{
257 return container_of(sd, struct ov5640_dev, sd);
258}
259
260static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
261{
262 return &container_of(ctrl->handler, struct ov5640_dev,
263 ctrls.handler)->sd;
264}
265
266/*
267 * FIXME: all of these register tables are likely filled with
268 * entries that set the register to their power-on default values,
269 * and which are otherwise not touched by this driver. Those entries
270 * should be identified and removed to speed register load time
271 * over i2c.
272 */
Hugues Fruchetfb98e292018-08-16 05:46:53 -0400273/* YUV422 UYVY VGA@30fps */
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300274static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300275 {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
276 {0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500277 {0x3630, 0x36, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300278 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
279 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
280 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
281 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
282 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
283 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
284 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
285 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
286 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
287 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
288 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
289 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
290 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
291 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
292 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
293 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400294 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300295 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
296 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
297 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
298 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
299 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
300 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
301 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
302 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -0400303 {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300304 {0x501f, 0x00, 0, 0}, {0x4713, 0x03, 0, 0}, {0x4407, 0x04, 0, 0},
305 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -0400306 {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300307 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
308 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
309 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
310 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
311 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
312 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
313 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
314 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
315 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
316 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
317 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
318 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
319 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
320 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
321 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
322 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
323 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
324 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
325 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
326 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
327 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
328 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
329 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
330 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
331 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
332 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
333 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
334 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
335 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
336 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
337 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
338 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
339 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
340 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
341 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
342 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
343 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
344 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
345 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
346 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
347 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
348 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
349 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
350 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
351 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
352 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
353 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
354 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
355 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
356 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
357};
358
Maxime Ripard086c25f2018-12-03 03:44:23 -0500359static const struct reg_value ov5640_setting_VGA_640_480[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500360 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300361 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400362 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300363 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
364 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
365 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400366 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300367 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
368 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
369 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
370 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
371 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
372 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
373 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
374 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
375 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
376};
377
Maxime Ripard086c25f2018-12-03 03:44:23 -0500378static const struct reg_value ov5640_setting_XGA_1024_768[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500379 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300380 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400381 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300382 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
383 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
384 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400385 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300386 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
387 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
388 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
389 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
390 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
391 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
392 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
393 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
Maxime Ripard86633412018-04-16 08:36:56 -0400394 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300395};
396
Maxime Ripard086c25f2018-12-03 03:44:23 -0500397static const struct reg_value ov5640_setting_QVGA_320_240[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500398 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300399 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400400 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300401 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
402 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
403 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400404 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300405 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
406 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
407 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
408 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
409 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
410 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
411 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
412 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
413 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
414};
415
Maxime Ripard086c25f2018-12-03 03:44:23 -0500416static const struct reg_value ov5640_setting_QCIF_176_144[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500417 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300418 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400419 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300420 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
421 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
422 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400423 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300424 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
425 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
426 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
427 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
428 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
429 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
430 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
431 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
432 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
433};
434
Maxime Ripard086c25f2018-12-03 03:44:23 -0500435static const struct reg_value ov5640_setting_NTSC_720_480[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500436 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300437 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400438 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300439 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
440 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
441 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400442 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300443 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
444 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
445 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
446 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
447 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
448 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
449 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
450 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
451 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
452};
453
Maxime Ripard086c25f2018-12-03 03:44:23 -0500454static const struct reg_value ov5640_setting_PAL_720_576[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500455 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300456 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400457 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300458 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
459 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
460 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400461 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300462 {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
463 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
464 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
465 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
466 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
467 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
468 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
469 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
470 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
471};
472
Maxime Ripard086c25f2018-12-03 03:44:23 -0500473static const struct reg_value ov5640_setting_720P_1280_720[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500474 {0x3c07, 0x07, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300475 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400476 {0x3814, 0x31, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300477 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
478 {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
479 {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400480 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300481 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
482 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
483 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
484 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
485 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
486 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
Hugues Fruchetb7911872018-09-24 09:33:27 -0400487 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x4713, 0x03, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300488 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
489 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
490};
491
Maxime Ripard086c25f2018-12-03 03:44:23 -0500492static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300493 {0x3008, 0x42, 0, 0},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500494 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300495 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400496 {0x3814, 0x11, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300497 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
498 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
499 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400500 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300501 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
502 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
503 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
504 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
505 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
506 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
507 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
508 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
Maxime Ripardc14d1072018-12-03 03:44:18 -0500509 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
510 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300511 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
512 {0x3800, 0x01, 0, 0}, {0x3801, 0x50, 0, 0}, {0x3802, 0x01, 0, 0},
513 {0x3803, 0xb2, 0, 0}, {0x3804, 0x08, 0, 0}, {0x3805, 0xef, 0, 0},
Maxime Ripard86633412018-04-16 08:36:56 -0400514 {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400515 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300516 {0x3a02, 0x04, 0, 0}, {0x3a03, 0x60, 0, 0}, {0x3a08, 0x01, 0, 0},
517 {0x3a09, 0x50, 0, 0}, {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x18, 0, 0},
518 {0x3a0e, 0x03, 0, 0}, {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x04, 0, 0},
519 {0x3a15, 0x60, 0, 0}, {0x4713, 0x02, 0, 0}, {0x4407, 0x04, 0, 0},
520 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
Maxime Ripard1da3afb2018-12-03 03:44:27 -0500521 {0x4005, 0x1a, 0, 0}, {0x3008, 0x02, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300522};
523
Maxime Ripard086c25f2018-12-03 03:44:23 -0500524static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
Maxime Ripardc14d1072018-12-03 03:44:18 -0500525 {0x3c07, 0x08, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300526 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
Hugues Fruchetce857052018-06-18 06:29:17 -0400527 {0x3814, 0x11, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300528 {0x3815, 0x11, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
529 {0x3802, 0x00, 0, 0}, {0x3803, 0x00, 0, 0}, {0x3804, 0x0a, 0, 0},
530 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9f, 0, 0},
Maxime Ripard476dec02018-04-16 08:36:55 -0400531 {0x3810, 0x00, 0, 0},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300532 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
533 {0x3618, 0x04, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x21, 0, 0},
534 {0x3709, 0x12, 0, 0}, {0x370c, 0x00, 0, 0}, {0x3a02, 0x03, 0, 0},
535 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
536 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
537 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
538 {0x4001, 0x02, 0, 0}, {0x4004, 0x06, 0, 0}, {0x4713, 0x03, 0, 0},
539 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
540 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 70},
541};
542
543/* power-on sensor init reg table */
544static const struct ov5640_mode_info ov5640_mode_init_data = {
Maxime Ripard476dec02018-04-16 08:36:55 -0400545 0, SUBSAMPLING, 640, 1896, 480, 984,
546 ov5640_init_setting_30fps_VGA,
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300547 ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
548};
549
550static const struct ov5640_mode_info
Maxime Ripard086c25f2018-12-03 03:44:23 -0500551ov5640_mode_data[OV5640_NUM_MODES] = {
552 {OV5640_MODE_QCIF_176_144, SUBSAMPLING,
553 176, 1896, 144, 984,
554 ov5640_setting_QCIF_176_144,
555 ARRAY_SIZE(ov5640_setting_QCIF_176_144)},
556 {OV5640_MODE_QVGA_320_240, SUBSAMPLING,
557 320, 1896, 240, 984,
558 ov5640_setting_QVGA_320_240,
559 ARRAY_SIZE(ov5640_setting_QVGA_320_240)},
560 {OV5640_MODE_VGA_640_480, SUBSAMPLING,
561 640, 1896, 480, 1080,
562 ov5640_setting_VGA_640_480,
563 ARRAY_SIZE(ov5640_setting_VGA_640_480)},
564 {OV5640_MODE_NTSC_720_480, SUBSAMPLING,
565 720, 1896, 480, 984,
566 ov5640_setting_NTSC_720_480,
567 ARRAY_SIZE(ov5640_setting_NTSC_720_480)},
568 {OV5640_MODE_PAL_720_576, SUBSAMPLING,
569 720, 1896, 576, 984,
570 ov5640_setting_PAL_720_576,
571 ARRAY_SIZE(ov5640_setting_PAL_720_576)},
572 {OV5640_MODE_XGA_1024_768, SUBSAMPLING,
573 1024, 1896, 768, 1080,
574 ov5640_setting_XGA_1024_768,
575 ARRAY_SIZE(ov5640_setting_XGA_1024_768)},
576 {OV5640_MODE_720P_1280_720, SUBSAMPLING,
577 1280, 1892, 720, 740,
578 ov5640_setting_720P_1280_720,
579 ARRAY_SIZE(ov5640_setting_720P_1280_720)},
580 {OV5640_MODE_1080P_1920_1080, SCALING,
581 1920, 2500, 1080, 1120,
582 ov5640_setting_1080P_1920_1080,
583 ARRAY_SIZE(ov5640_setting_1080P_1920_1080)},
584 {OV5640_MODE_QSXGA_2592_1944, SCALING,
585 2592, 2844, 1944, 1968,
586 ov5640_setting_QSXGA_2592_1944,
587 ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944)},
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300588};
589
590static int ov5640_init_slave_id(struct ov5640_dev *sensor)
591{
592 struct i2c_client *client = sensor->i2c_client;
593 struct i2c_msg msg;
594 u8 buf[3];
595 int ret;
596
597 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
598 return 0;
599
600 buf[0] = OV5640_REG_SLAVE_ID >> 8;
601 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
602 buf[2] = client->addr << 1;
603
604 msg.addr = OV5640_DEFAULT_SLAVE_ID;
605 msg.flags = 0;
606 msg.buf = buf;
607 msg.len = sizeof(buf);
608
609 ret = i2c_transfer(client->adapter, &msg, 1);
610 if (ret < 0) {
611 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
612 return ret;
613 }
614
615 return 0;
616}
617
618static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
619{
620 struct i2c_client *client = sensor->i2c_client;
621 struct i2c_msg msg;
622 u8 buf[3];
623 int ret;
624
625 buf[0] = reg >> 8;
626 buf[1] = reg & 0xff;
627 buf[2] = val;
628
629 msg.addr = client->addr;
630 msg.flags = client->flags;
631 msg.buf = buf;
632 msg.len = sizeof(buf);
633
634 ret = i2c_transfer(client->adapter, &msg, 1);
635 if (ret < 0) {
Hugues Fruchet3924c622018-01-31 07:46:17 -0500636 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300637 __func__, reg, val);
638 return ret;
639 }
640
641 return 0;
642}
643
644static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
645{
646 struct i2c_client *client = sensor->i2c_client;
647 struct i2c_msg msg[2];
648 u8 buf[2];
649 int ret;
650
651 buf[0] = reg >> 8;
652 buf[1] = reg & 0xff;
653
654 msg[0].addr = client->addr;
655 msg[0].flags = client->flags;
656 msg[0].buf = buf;
657 msg[0].len = sizeof(buf);
658
659 msg[1].addr = client->addr;
660 msg[1].flags = client->flags | I2C_M_RD;
661 msg[1].buf = buf;
662 msg[1].len = 1;
663
664 ret = i2c_transfer(client->adapter, msg, 2);
Hugues Fruchet3924c622018-01-31 07:46:17 -0500665 if (ret < 0) {
666 dev_err(&client->dev, "%s: error: reg=%x\n",
667 __func__, reg);
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300668 return ret;
Hugues Fruchet3924c622018-01-31 07:46:17 -0500669 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300670
671 *val = buf[0];
672 return 0;
673}
674
675static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
676{
677 u8 hi, lo;
678 int ret;
679
680 ret = ov5640_read_reg(sensor, reg, &hi);
681 if (ret)
682 return ret;
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -0500683 ret = ov5640_read_reg(sensor, reg + 1, &lo);
Steve Longerbeam19a81c12017-06-07 15:33:56 -0300684 if (ret)
685 return ret;
686
687 *val = ((u16)hi << 8) | (u16)lo;
688 return 0;
689}
690
691static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
692{
693 int ret;
694
695 ret = ov5640_write_reg(sensor, reg, val >> 8);
696 if (ret)
697 return ret;
698
699 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
700}
701
702static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
703 u8 mask, u8 val)
704{
705 u8 readval;
706 int ret;
707
708 ret = ov5640_read_reg(sensor, reg, &readval);
709 if (ret)
710 return ret;
711
712 readval &= ~mask;
713 val &= mask;
714 val |= readval;
715
716 return ov5640_write_reg(sensor, reg, val);
717}
718
Maxime Ripardaa288242018-12-03 03:44:17 -0500719/*
720 * After trying the various combinations, reading various
721 * documentations spreaded around the net, and from the various
722 * feedback, the clock tree is probably as follows:
723 *
724 * +--------------+
725 * | Ext. Clock |
726 * +-+------------+
727 * | +----------+
728 * +->| PLL1 | - reg 0x3036, for the multiplier
729 * +-+--------+ - reg 0x3037, bits 0-3 for the pre-divider
730 * | +--------------+
731 * +->| System Clock | - reg 0x3035, bits 4-7
732 * +-+------------+
733 * | +--------------+
734 * +->| MIPI Divider | - reg 0x3035, bits 0-3
735 * | +-+------------+
736 * | +----------------> MIPI SCLK
737 * | + +-----+
738 * | +->| / 2 |-------> MIPI BIT CLK
739 * | +-----+
740 * | +--------------+
741 * +->| PLL Root Div | - reg 0x3037, bit 4
742 * +-+------------+
743 * | +---------+
744 * +->| Bit Div | - reg 0x3035, bits 0-3
745 * +-+-------+
746 * | +-------------+
747 * +->| SCLK Div | - reg 0x3108, bits 0-1
748 * | +-+-----------+
749 * | +---------------> SCLK
750 * | +-------------+
751 * +->| SCLK 2X Div | - reg 0x3108, bits 2-3
752 * | +-+-----------+
753 * | +---------------> SCLK 2X
754 * | +-------------+
755 * +->| PCLK Div | - reg 0x3108, bits 4-5
756 * ++------------+
757 * + +-----------+
758 * +->| P_DIV | - reg 0x3035, bits 0-3
759 * +-----+-----+
760 * +------------> PCLK
761 *
762 * This is deviating from the datasheet at least for the register
763 * 0x3108, since it's said here that the PCLK would be clocked from
764 * the PLL.
765 *
766 * There seems to be also (unverified) constraints:
767 * - the PLL pre-divider output rate should be in the 4-27MHz range
768 * - the PLL multiplier output rate should be in the 500-1000MHz range
769 * - PCLK >= SCLK * 2 in YUV, >= SCLK in Raw or JPEG
770 *
771 * In the two latter cases, these constraints are met since our
772 * factors are hardcoded. If we were to change that, we would need to
773 * take this into account. The only varying parts are the PLL
774 * multiplier and the system clock divider, which are shared between
775 * all these clocks so won't cause any issue.
776 */
777
778/*
779 * This is supposed to be ranging from 1 to 8, but the value is always
780 * set to 3 in the vendor kernels.
781 */
782#define OV5640_PLL_PREDIV 3
783
784#define OV5640_PLL_MULT_MIN 4
785#define OV5640_PLL_MULT_MAX 252
786
787/*
788 * This is supposed to be ranging from 1 to 16, but the value is
789 * always set to either 1 or 2 in the vendor kernels.
790 */
791#define OV5640_SYSDIV_MIN 1
792#define OV5640_SYSDIV_MAX 16
793
794/*
795 * Hardcode these values for scaler and non-scaler modes.
796 * FIXME: to be re-calcualted for 1 data lanes setups
797 */
798#define OV5640_MIPI_DIV_PCLK 2
799#define OV5640_MIPI_DIV_SCLK 1
800
801/*
802 * This is supposed to be ranging from 1 to 2, but the value is always
803 * set to 2 in the vendor kernels.
804 */
805#define OV5640_PLL_ROOT_DIV 2
806#define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
807
808/*
809 * We only supports 8-bit formats at the moment
810 */
811#define OV5640_BIT_DIV 2
812#define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
813
814/*
815 * This is supposed to be ranging from 1 to 8, but the value is always
816 * set to 2 in the vendor kernels.
817 */
818#define OV5640_SCLK_ROOT_DIV 2
819
820/*
821 * This is hardcoded so that the consistency is maintained between SCLK and
822 * SCLK 2x.
823 */
824#define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
825
826/*
827 * This is supposed to be ranging from 1 to 8, but the value is always
828 * set to 1 in the vendor kernels.
829 */
830#define OV5640_PCLK_ROOT_DIV 1
831#define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
832
833static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
834 u8 pll_prediv, u8 pll_mult,
835 u8 sysdiv)
836{
837 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
838
839 /* PLL1 output cannot exceed 1GHz. */
840 if (sysclk / 1000000 > 1000)
841 return 0;
842
843 return sysclk / sysdiv;
844}
845
846static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
847 unsigned long rate,
848 u8 *pll_prediv, u8 *pll_mult,
849 u8 *sysdiv)
850{
851 unsigned long best = ~0;
852 u8 best_sysdiv = 1, best_mult = 1;
853 u8 _sysdiv, _pll_mult;
854
855 for (_sysdiv = OV5640_SYSDIV_MIN;
856 _sysdiv <= OV5640_SYSDIV_MAX;
857 _sysdiv++) {
858 for (_pll_mult = OV5640_PLL_MULT_MIN;
859 _pll_mult <= OV5640_PLL_MULT_MAX;
860 _pll_mult++) {
861 unsigned long _rate;
862
863 /*
864 * The PLL multiplier cannot be odd if above
865 * 127.
866 */
867 if (_pll_mult > 127 && (_pll_mult % 2))
868 continue;
869
870 _rate = ov5640_compute_sys_clk(sensor,
871 OV5640_PLL_PREDIV,
872 _pll_mult, _sysdiv);
873
874 /*
875 * We have reached the maximum allowed PLL1 output,
876 * increase sysdiv.
877 */
878 if (!rate)
879 break;
880
881 /*
882 * Prefer rates above the expected clock rate than
883 * below, even if that means being less precise.
884 */
885 if (_rate < rate)
886 continue;
887
888 if (abs(rate - _rate) < abs(rate - best)) {
889 best = _rate;
890 best_sysdiv = _sysdiv;
891 best_mult = _pll_mult;
892 }
893
894 if (_rate == rate)
895 goto out;
896 }
897 }
898
899out:
900 *sysdiv = best_sysdiv;
901 *pll_prediv = OV5640_PLL_PREDIV;
902 *pll_mult = best_mult;
903
904 return best;
905}
906
907/*
908 * ov5640_set_mipi_pclk() - Calculate the clock tree configuration values
909 * for the MIPI CSI-2 output.
910 *
911 * @rate: The requested bandwidth per lane in bytes per second.
912 * 'Bandwidth Per Lane' is calculated as:
913 * bpl = HTOT * VTOT * FPS * bpp / num_lanes;
914 *
915 * This function use the requested bandwidth to calculate:
916 * - sample_rate = bpl / (bpp / num_lanes);
917 * = bpl / (PLL_RDIV * BIT_DIV * PCLK_DIV * MIPI_DIV / num_lanes);
918 *
919 * - mipi_sclk = bpl / MIPI_DIV / 2; ( / 2 is for CSI-2 DDR)
920 *
921 * with these fixed parameters:
922 * PLL_RDIV = 2;
923 * BIT_DIVIDER = 2; (MIPI_BIT_MODE == 8 ? 2 : 2,5);
924 * PCLK_DIV = 1;
925 *
926 * The MIPI clock generation differs for modes that use the scaler and modes
927 * that do not. In case the scaler is in use, the MIPI_SCLK generates the MIPI
928 * BIT CLk, and thus:
929 *
930 * - mipi_sclk = bpl / MIPI_DIV / 2;
931 * MIPI_DIV = 1;
932 *
933 * For modes that do not go through the scaler, the MIPI BIT CLOCK is generated
934 * from the pixel clock, and thus:
935 *
936 * - sample_rate = bpl / (bpp / num_lanes);
937 * = bpl / (2 * 2 * 1 * MIPI_DIV / num_lanes);
938 * = bpl / (4 * MIPI_DIV / num_lanes);
939 * - MIPI_DIV = bpp / (4 * num_lanes);
940 *
941 * FIXME: this have been tested with 16bpp and 2 lanes setup only.
942 * MIPI_DIV is fixed to value 2, but it -might- be changed according to the
943 * above formula for setups with 1 lane or image formats with different bpp.
944 *
945 * FIXME: this deviates from the sensor manual documentation which is quite
946 * thin on the MIPI clock tree generation part.
947 */
948static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor,
949 unsigned long rate)
950{
951 const struct ov5640_mode_info *mode = sensor->current_mode;
952 u8 prediv, mult, sysdiv;
953 u8 mipi_div;
954 int ret;
955
956 /*
957 * 1280x720 is reported to use 'SUBSAMPLING' only,
958 * but according to the sensor manual it goes through the
959 * scaler before subsampling.
960 */
961 if (mode->dn_mode == SCALING ||
962 (mode->id == OV5640_MODE_720P_1280_720))
963 mipi_div = OV5640_MIPI_DIV_SCLK;
964 else
965 mipi_div = OV5640_MIPI_DIV_PCLK;
966
967 ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv);
968
969 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
970 0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT);
971
972 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
973 0xff, sysdiv << 4 | mipi_div);
974 if (ret)
975 return ret;
976
977 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
978 if (ret)
979 return ret;
980
981 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
982 0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv);
983 if (ret)
984 return ret;
985
986 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER,
987 0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS);
988}
989
990static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
991 unsigned long rate,
992 u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
993 u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
994{
995 unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
996 OV5640_PCLK_ROOT_DIV;
997
998 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
999 sysdiv);
1000 *pll_rdiv = OV5640_PLL_ROOT_DIV;
1001 *bit_div = OV5640_BIT_DIV;
1002 *pclk_div = OV5640_PCLK_ROOT_DIV;
1003
1004 return _rate / *pll_rdiv / *bit_div / *pclk_div;
1005}
1006
1007static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate)
1008{
1009 u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1010 int ret;
1011
1012 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1013 &bit_div, &pclk_div);
1014
1015 if (bit_div == 2)
1016 bit_div = 8;
1017
1018 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1019 0x0f, bit_div);
1020 if (ret)
1021 return ret;
1022
1023 /*
1024 * We need to set sysdiv according to the clock, and to clear
1025 * the MIPI divider.
1026 */
1027 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1028 0xff, sysdiv << 4);
1029 if (ret)
1030 return ret;
1031
1032 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1033 0xff, mult);
1034 if (ret)
1035 return ret;
1036
1037 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1038 0x1f, prediv | ((pll_rdiv - 1) << 4));
1039 if (ret)
1040 return ret;
1041
1042 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1043 (ilog2(pclk_div) << 4));
1044}
1045
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001046/* download ov5640 settings to sensor through i2c */
Jacopo Mondibad17742018-07-18 06:06:23 -04001047static int ov5640_set_timings(struct ov5640_dev *sensor,
1048 const struct ov5640_mode_info *mode)
1049{
1050 int ret;
1051
1052 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
1053 if (ret < 0)
1054 return ret;
1055
1056 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
1057 if (ret < 0)
1058 return ret;
1059
1060 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
1061 if (ret < 0)
1062 return ret;
1063
1064 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
1065}
1066
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001067static int ov5640_load_regs(struct ov5640_dev *sensor,
1068 const struct ov5640_mode_info *mode)
1069{
1070 const struct reg_value *regs = mode->reg_data;
1071 unsigned int i;
1072 u32 delay_ms;
1073 u16 reg_addr;
1074 u8 mask, val;
1075 int ret = 0;
1076
1077 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
1078 delay_ms = regs->delay_ms;
1079 reg_addr = regs->reg_addr;
1080 val = regs->val;
1081 mask = regs->mask;
1082
1083 if (mask)
1084 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1085 else
1086 ret = ov5640_write_reg(sensor, reg_addr, val);
1087 if (ret)
1088 break;
1089
1090 if (delay_ms)
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05001091 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001092 }
1093
Jacopo Mondibad17742018-07-18 06:06:23 -04001094 return ov5640_set_timings(sensor, mode);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001095}
1096
Hugues Fruchetdc29a1c2018-09-11 09:48:17 -04001097static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1098{
1099 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1100 BIT(0), on ? 0 : BIT(0));
1101}
1102
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001103/* read exposure, in number of line periods */
1104static int ov5640_get_exposure(struct ov5640_dev *sensor)
1105{
1106 int exp, ret;
1107 u8 temp;
1108
1109 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1110 if (ret)
1111 return ret;
1112 exp = ((int)temp & 0x0f) << 16;
1113 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1114 if (ret)
1115 return ret;
1116 exp |= ((int)temp << 8);
1117 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1118 if (ret)
1119 return ret;
1120 exp |= (int)temp;
1121
1122 return exp >> 4;
1123}
1124
1125/* write exposure, given number of line periods */
1126static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1127{
1128 int ret;
1129
1130 exposure <<= 4;
1131
1132 ret = ov5640_write_reg(sensor,
1133 OV5640_REG_AEC_PK_EXPOSURE_LO,
1134 exposure & 0xff);
1135 if (ret)
1136 return ret;
1137 ret = ov5640_write_reg(sensor,
1138 OV5640_REG_AEC_PK_EXPOSURE_MED,
1139 (exposure >> 8) & 0xff);
1140 if (ret)
1141 return ret;
1142 return ov5640_write_reg(sensor,
1143 OV5640_REG_AEC_PK_EXPOSURE_HI,
1144 (exposure >> 16) & 0x0f);
1145}
1146
1147static int ov5640_get_gain(struct ov5640_dev *sensor)
1148{
1149 u16 gain;
1150 int ret;
1151
1152 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1153 if (ret)
1154 return ret;
1155
1156 return gain & 0x3ff;
1157}
1158
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001159static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1160{
1161 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1162 (u16)gain & 0x3ff);
1163}
1164
1165static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1166{
1167 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1168 BIT(1), on ? 0 : BIT(1));
1169}
1170
Hugues Fruchetf22996d2018-01-03 04:57:31 -05001171static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1172{
1173 int ret;
1174 unsigned int flags = sensor->ep.bus.parallel.flags;
1175 u8 pclk_pol = 0;
1176 u8 hsync_pol = 0;
1177 u8 vsync_pol = 0;
1178
1179 /*
1180 * Note about parallel port configuration.
1181 *
1182 * When configured in parallel mode, the OV5640 will
1183 * output 10 bits data on DVP data lines [9:0].
1184 * If only 8 bits data are wanted, the 8 bits data lines
1185 * of the camera interface must be physically connected
1186 * on the DVP data lines [9:2].
1187 *
1188 * Control lines polarity can be configured through
1189 * devicetree endpoint control lines properties.
1190 * If no endpoint control lines properties are set,
1191 * polarity will be as below:
1192 * - VSYNC: active high
1193 * - HREF: active low
1194 * - PCLK: active low
1195 */
1196
1197 if (on) {
1198 /*
Hugues Fruchetf22996d2018-01-03 04:57:31 -05001199 * configure parallel port control lines polarity
1200 *
1201 * POLARITY CTRL0
1202 * - [5]: PCLK polarity (0: active low, 1: active high)
1203 * - [1]: HREF polarity (0: active low, 1: active high)
1204 * - [0]: VSYNC polarity (mismatch here between
1205 * datasheet and hardware, 0 is active high
1206 * and 1 is active low...)
1207 */
1208 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1209 pclk_pol = 1;
1210 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1211 hsync_pol = 1;
1212 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1213 vsync_pol = 1;
1214
1215 ret = ov5640_write_reg(sensor,
1216 OV5640_REG_POLARITY_CTRL00,
1217 (pclk_pol << 5) |
1218 (hsync_pol << 1) |
1219 vsync_pol);
1220
1221 if (ret)
1222 return ret;
1223 }
1224
1225 /*
1226 * powerdown MIPI TX/RX PHY & disable MIPI
1227 *
1228 * MIPI CONTROL 00
1229 * 4: PWDN PHY TX
1230 * 3: PWDN PHY RX
1231 * 2: MIPI enable
1232 */
1233 ret = ov5640_write_reg(sensor,
1234 OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1235 if (ret)
1236 return ret;
1237
1238 /*
1239 * enable VSYNC/HREF/PCLK DVP control lines
1240 * & D[9:6] DVP data lines
1241 *
1242 * PAD OUTPUT ENABLE 01
1243 * - 6: VSYNC output enable
1244 * - 5: HREF output enable
1245 * - 4: PCLK output enable
1246 * - [3:0]: D[9:6] output enable
1247 */
1248 ret = ov5640_write_reg(sensor,
1249 OV5640_REG_PAD_OUTPUT_ENABLE01,
1250 on ? 0x7f : 0);
1251 if (ret)
1252 return ret;
1253
1254 /*
1255 * enable D[5:0] DVP data lines
1256 *
1257 * PAD OUTPUT ENABLE 02
1258 * - [7:2]: D[5:0] output enable
1259 */
1260 return ov5640_write_reg(sensor,
1261 OV5640_REG_PAD_OUTPUT_ENABLE02,
1262 on ? 0xfc : 0);
1263}
1264
1265static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001266{
1267 int ret;
1268
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04001269 /*
1270 * Enable/disable the MIPI interface
1271 *
1272 * 0x300e = on ? 0x45 : 0x40
1273 *
1274 * FIXME: the sensor manual (version 2.03) reports
1275 * [7:5] = 000 : 1 data lane mode
1276 * [7:5] = 001 : 2 data lanes mode
1277 * But this settings do not work, while the following ones
1278 * have been validated for 2 data lanes mode.
1279 *
1280 * [7:5] = 010 : 2 data lanes mode
1281 * [4] = 0 : Power up MIPI HS Tx
1282 * [3] = 0 : Power up MIPI LS Rx
1283 * [2] = 1/0 : MIPI interface enable/disable
1284 * [1:0] = 01/00: FIXME: 'debug'
1285 */
1286 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1287 on ? 0x45 : 0x40);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001288 if (ret)
1289 return ret;
1290
1291 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1292 on ? 0x00 : 0x0f);
1293}
1294
1295static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1296{
1297 /* calculate sysclk */
1298 u32 xvclk = sensor->xclk_freq / 10000;
1299 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1300 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1301 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1302 u8 temp1, temp2;
1303 int ret;
1304
1305 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1306 if (ret)
1307 return ret;
1308 temp2 = temp1 & 0x0f;
1309 if (temp2 == 8 || temp2 == 10)
1310 bit_div2x = temp2 / 2;
1311
1312 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1313 if (ret)
1314 return ret;
1315 sysdiv = temp1 >> 4;
1316 if (sysdiv == 0)
1317 sysdiv = 16;
1318
1319 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1320 if (ret)
1321 return ret;
1322 multiplier = temp1;
1323
1324 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1325 if (ret)
1326 return ret;
1327 prediv = temp1 & 0x0f;
1328 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1329
1330 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1331 if (ret)
1332 return ret;
1333 temp2 = temp1 & 0x03;
1334 sclk_rdiv = sclk_rdiv_map[temp2];
1335
1336 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1337 return -EINVAL;
1338
1339 VCO = xvclk * multiplier / prediv;
1340
1341 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1342
1343 return sysclk;
1344}
1345
1346static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1347{
1348 /* read HTS from register settings */
1349 u8 mode;
1350 int ret;
1351
1352 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1353 if (ret)
1354 return ret;
1355 mode &= 0xfb;
1356 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1357}
1358
1359static int ov5640_get_hts(struct ov5640_dev *sensor)
1360{
1361 /* read HTS from register settings */
1362 u16 hts;
1363 int ret;
1364
1365 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1366 if (ret)
1367 return ret;
1368 return hts;
1369}
1370
1371static int ov5640_get_vts(struct ov5640_dev *sensor)
1372{
1373 u16 vts;
1374 int ret;
1375
1376 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1377 if (ret)
1378 return ret;
1379 return vts;
1380}
1381
1382static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1383{
1384 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1385}
1386
1387static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1388{
1389 /* get banding filter value */
1390 int ret, light_freq = 0;
1391 u8 temp, temp1;
1392
1393 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1394 if (ret)
1395 return ret;
1396
1397 if (temp & 0x80) {
1398 /* manual */
1399 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1400 &temp1);
1401 if (ret)
1402 return ret;
1403 if (temp1 & 0x04) {
1404 /* 50Hz */
1405 light_freq = 50;
1406 } else {
1407 /* 60Hz */
1408 light_freq = 60;
1409 }
1410 } else {
1411 /* auto */
1412 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1413 &temp1);
1414 if (ret)
1415 return ret;
1416
1417 if (temp1 & 0x01) {
1418 /* 50Hz */
1419 light_freq = 50;
1420 } else {
1421 /* 60Hz */
1422 }
1423 }
1424
1425 return light_freq;
1426}
1427
1428static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1429{
1430 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1431 int ret;
1432
1433 /* read preview PCLK */
1434 ret = ov5640_get_sysclk(sensor);
1435 if (ret < 0)
1436 return ret;
1437 if (ret == 0)
1438 return -EINVAL;
1439 sensor->prev_sysclk = ret;
1440 /* read preview HTS */
1441 ret = ov5640_get_hts(sensor);
1442 if (ret < 0)
1443 return ret;
1444 if (ret == 0)
1445 return -EINVAL;
1446 sensor->prev_hts = ret;
1447
1448 /* read preview VTS */
1449 ret = ov5640_get_vts(sensor);
1450 if (ret < 0)
1451 return ret;
1452 prev_vts = ret;
1453
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001454 /* calculate banding filter */
1455 /* 60Hz */
1456 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1457 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1458 if (ret)
1459 return ret;
1460 if (!band_step60)
1461 return -EINVAL;
1462 max_band60 = (int)((prev_vts - 4) / band_step60);
1463 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1464 if (ret)
1465 return ret;
1466
1467 /* 50Hz */
1468 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1469 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1470 if (ret)
1471 return ret;
1472 if (!band_step50)
1473 return -EINVAL;
1474 max_band50 = (int)((prev_vts - 4) / band_step50);
1475 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1476}
1477
1478static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1479{
1480 /* stable in high */
1481 u32 fast_high, fast_low;
1482 int ret;
1483
1484 sensor->ae_low = target * 23 / 25; /* 0.92 */
1485 sensor->ae_high = target * 27 / 25; /* 1.08 */
1486
1487 fast_high = sensor->ae_high << 1;
1488 if (fast_high > 255)
1489 fast_high = 255;
1490
1491 fast_low = sensor->ae_low >> 1;
1492
1493 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1494 if (ret)
1495 return ret;
1496 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1497 if (ret)
1498 return ret;
1499 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1500 if (ret)
1501 return ret;
1502 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1503 if (ret)
1504 return ret;
1505 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1506 if (ret)
1507 return ret;
1508 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1509}
1510
Hugues Fruchetc2c3f422018-09-11 09:48:19 -04001511static int ov5640_get_binning(struct ov5640_dev *sensor)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001512{
1513 u8 temp;
1514 int ret;
1515
1516 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1517 if (ret)
1518 return ret;
Hugues Fruchetc2c3f422018-09-11 09:48:19 -04001519
1520 return temp & BIT(0);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001521}
1522
Hugues Fruchetce857052018-06-18 06:29:17 -04001523static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1524{
1525 int ret;
1526
1527 /*
1528 * TIMING TC REG21:
1529 * - [0]: Horizontal binning enable
1530 */
1531 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1532 BIT(0), enable ? BIT(0) : 0);
1533 if (ret)
1534 return ret;
1535 /*
1536 * TIMING TC REG20:
1537 * - [0]: Undocumented, but hardcoded init sequences
1538 * are always setting REG21/REG20 bit 0 to same value...
1539 */
1540 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1541 BIT(0), enable ? BIT(0) : 0);
1542}
1543
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001544static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1545{
Hugues Fruchet8670d702018-02-06 08:24:09 -05001546 struct i2c_client *client = sensor->i2c_client;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001547 u8 temp, channel = virtual_channel;
1548 int ret;
1549
Hugues Fruchet8670d702018-02-06 08:24:09 -05001550 if (channel > 3) {
1551 dev_err(&client->dev,
1552 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
1553 __func__, channel);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001554 return -EINVAL;
Hugues Fruchet8670d702018-02-06 08:24:09 -05001555 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001556
1557 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1558 if (ret)
1559 return ret;
1560 temp &= ~(3 << 6);
1561 temp |= (channel << 6);
1562 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1563}
1564
1565static const struct ov5640_mode_info *
1566ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1567 int width, int height, bool nearest)
1568{
Hugues Fruchet3c4a7372018-06-20 04:40:57 -04001569 const struct ov5640_mode_info *mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001570
Maxime Ripard086c25f2018-12-03 03:44:23 -05001571 mode = v4l2_find_nearest_size(ov5640_mode_data,
1572 ARRAY_SIZE(ov5640_mode_data),
Hugues Fruchet3c4a7372018-06-20 04:40:57 -04001573 hact, vact,
1574 width, height);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001575
Hugues Fruchet3c4a7372018-06-20 04:40:57 -04001576 if (!mode ||
1577 (!nearest && (mode->hact != width || mode->vact != height)))
1578 return NULL;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001579
Maxime Riparde823fb12018-12-03 03:44:26 -05001580 /* Only 640x480 can operate at 60fps (for now) */
1581 if (fr == OV5640_60_FPS &&
1582 !(mode->hact == 640 && mode->vact == 480))
1583 return NULL;
1584
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001585 return mode;
1586}
1587
1588/*
1589 * sensor changes between scaling and subsampling, go through
1590 * exposure calculation
1591 */
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05001592static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
1593 const struct ov5640_mode_info *mode)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001594{
1595 u32 prev_shutter, prev_gain16;
1596 u32 cap_shutter, cap_gain16;
1597 u32 cap_sysclk, cap_hts, cap_vts;
1598 u32 light_freq, cap_bandfilt, cap_maxband;
1599 u32 cap_gain16_shutter;
1600 u8 average;
1601 int ret;
1602
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05001603 if (!mode->reg_data)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001604 return -EINVAL;
1605
1606 /* read preview shutter */
1607 ret = ov5640_get_exposure(sensor);
1608 if (ret < 0)
1609 return ret;
1610 prev_shutter = ret;
Hugues Fruchetc2c3f422018-09-11 09:48:19 -04001611 ret = ov5640_get_binning(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001612 if (ret < 0)
1613 return ret;
1614 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1615 mode->id != OV5640_MODE_1080P_1920_1080)
1616 prev_shutter *= 2;
1617
1618 /* read preview gain */
1619 ret = ov5640_get_gain(sensor);
1620 if (ret < 0)
1621 return ret;
1622 prev_gain16 = ret;
1623
1624 /* get average */
1625 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1626 if (ret)
1627 return ret;
1628
1629 /* turn off night mode for capture */
1630 ret = ov5640_set_night_mode(sensor);
1631 if (ret < 0)
1632 return ret;
1633
1634 /* Write capture setting */
1635 ret = ov5640_load_regs(sensor, mode);
1636 if (ret < 0)
1637 return ret;
1638
1639 /* read capture VTS */
1640 ret = ov5640_get_vts(sensor);
1641 if (ret < 0)
1642 return ret;
1643 cap_vts = ret;
1644 ret = ov5640_get_hts(sensor);
1645 if (ret < 0)
1646 return ret;
1647 if (ret == 0)
1648 return -EINVAL;
1649 cap_hts = ret;
1650
1651 ret = ov5640_get_sysclk(sensor);
1652 if (ret < 0)
1653 return ret;
1654 if (ret == 0)
1655 return -EINVAL;
1656 cap_sysclk = ret;
1657
1658 /* calculate capture banding filter */
1659 ret = ov5640_get_light_freq(sensor);
1660 if (ret < 0)
1661 return ret;
1662 light_freq = ret;
1663
1664 if (light_freq == 60) {
1665 /* 60Hz */
1666 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1667 } else {
1668 /* 50Hz */
1669 cap_bandfilt = cap_sysclk * 100 / cap_hts;
1670 }
1671
1672 if (!sensor->prev_sysclk) {
1673 ret = ov5640_get_sysclk(sensor);
1674 if (ret < 0)
1675 return ret;
1676 if (ret == 0)
1677 return -EINVAL;
1678 sensor->prev_sysclk = ret;
1679 }
1680
1681 if (!cap_bandfilt)
1682 return -EINVAL;
1683
1684 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1685
1686 /* calculate capture shutter/gain16 */
1687 if (average > sensor->ae_low && average < sensor->ae_high) {
1688 /* in stable range */
1689 cap_gain16_shutter =
1690 prev_gain16 * prev_shutter *
1691 cap_sysclk / sensor->prev_sysclk *
1692 sensor->prev_hts / cap_hts *
1693 sensor->ae_target / average;
1694 } else {
1695 cap_gain16_shutter =
1696 prev_gain16 * prev_shutter *
1697 cap_sysclk / sensor->prev_sysclk *
1698 sensor->prev_hts / cap_hts;
1699 }
1700
1701 /* gain to shutter */
1702 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
1703 /* shutter < 1/100 */
1704 cap_shutter = cap_gain16_shutter / 16;
1705 if (cap_shutter < 1)
1706 cap_shutter = 1;
1707
1708 cap_gain16 = cap_gain16_shutter / cap_shutter;
1709 if (cap_gain16 < 16)
1710 cap_gain16 = 16;
1711 } else {
1712 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
1713 /* exposure reach max */
1714 cap_shutter = cap_bandfilt * cap_maxband;
1715 if (!cap_shutter)
1716 return -EINVAL;
1717
1718 cap_gain16 = cap_gain16_shutter / cap_shutter;
1719 } else {
1720 /* 1/100 < (cap_shutter = n/100) =< max */
1721 cap_shutter =
1722 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
1723 * cap_bandfilt;
1724 if (!cap_shutter)
1725 return -EINVAL;
1726
1727 cap_gain16 = cap_gain16_shutter / cap_shutter;
1728 }
1729 }
1730
1731 /* set capture gain */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001732 ret = ov5640_set_gain(sensor, cap_gain16);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001733 if (ret)
1734 return ret;
1735
1736 /* write capture shutter */
1737 if (cap_shutter > (cap_vts - 4)) {
1738 cap_vts = cap_shutter + 4;
1739 ret = ov5640_set_vts(sensor, cap_vts);
1740 if (ret < 0)
1741 return ret;
1742 }
1743
1744 /* set exposure */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001745 return ov5640_set_exposure(sensor, cap_shutter);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001746}
1747
1748/*
1749 * if sensor changes inside scaling or subsampling
1750 * change mode directly
1751 */
1752static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001753 const struct ov5640_mode_info *mode)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001754{
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05001755 if (!mode->reg_data)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001756 return -EINVAL;
1757
1758 /* Write capture setting */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001759 return ov5640_load_regs(sensor, mode);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001760}
1761
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04001762static int ov5640_set_mode(struct ov5640_dev *sensor)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001763{
1764 const struct ov5640_mode_info *mode = sensor->current_mode;
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04001765 const struct ov5640_mode_info *orig_mode = sensor->last_mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001766 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001767 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
Hugues Fruchetdc29a1c2018-09-11 09:48:17 -04001768 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
Maxime Ripardaa288242018-12-03 03:44:17 -05001769 unsigned long rate;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001770 int ret;
1771
1772 dn_mode = mode->dn_mode;
1773 orig_dn_mode = orig_mode->dn_mode;
1774
1775 /* auto gain and exposure must be turned off when changing modes */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001776 if (auto_gain) {
1777 ret = ov5640_set_autogain(sensor, false);
1778 if (ret)
1779 return ret;
1780 }
Maxime Ripardbf4a4b52018-04-16 08:36:52 -04001781
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001782 if (auto_exp) {
1783 ret = ov5640_set_autoexposure(sensor, false);
1784 if (ret)
1785 goto restore_auto_gain;
1786 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001787
Maxime Ripardaa288242018-12-03 03:44:17 -05001788 /*
1789 * All the formats we support have 16 bits per pixel, seems to require
1790 * the same rate than YUV, so we can just use 16 bpp all the time.
1791 */
Maxime Riparddfbfb7a2018-12-03 03:44:21 -05001792 rate = mode->vtot * mode->htot * 16;
1793 rate *= ov5640_framerates[sensor->current_fr];
Maxime Ripardaa288242018-12-03 03:44:17 -05001794 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
1795 rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes;
1796 ret = ov5640_set_mipi_pclk(sensor, rate);
1797 } else {
1798 rate = rate / sensor->ep.bus.parallel.bus_width;
1799 ret = ov5640_set_dvp_pclk(sensor, rate);
1800 }
1801
1802 if (ret < 0)
1803 return 0;
1804
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001805 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
1806 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
1807 /*
1808 * change between subsampling and scaling
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001809 * go through exposure calculation
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001810 */
1811 ret = ov5640_set_mode_exposure_calc(sensor, mode);
1812 } else {
1813 /*
1814 * change inside subsampling or scaling
1815 * download firmware directly
1816 */
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001817 ret = ov5640_set_mode_direct(sensor, mode);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001818 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001819 if (ret < 0)
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001820 goto restore_auto_exp_gain;
1821
1822 /* restore auto gain and exposure */
1823 if (auto_gain)
1824 ov5640_set_autogain(sensor, true);
1825 if (auto_exp)
1826 ov5640_set_autoexposure(sensor, true);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001827
Hugues Fruchetce857052018-06-18 06:29:17 -04001828 ret = ov5640_set_binning(sensor, dn_mode != SCALING);
1829 if (ret < 0)
1830 return ret;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001831 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
1832 if (ret < 0)
1833 return ret;
1834 ret = ov5640_get_light_freq(sensor);
1835 if (ret < 0)
1836 return ret;
1837 ret = ov5640_set_bandingfilter(sensor);
1838 if (ret < 0)
1839 return ret;
1840 ret = ov5640_set_virtual_channel(sensor);
1841 if (ret < 0)
1842 return ret;
1843
1844 sensor->pending_mode_change = false;
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04001845 sensor->last_mode = mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001846
1847 return 0;
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04001848
1849restore_auto_exp_gain:
1850 if (auto_exp)
1851 ov5640_set_autoexposure(sensor, true);
1852restore_auto_gain:
1853 if (auto_gain)
1854 ov5640_set_autogain(sensor, true);
1855
1856 return ret;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001857}
1858
Akinobu Mita19ad26f2018-03-11 11:34:41 -04001859static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1860 struct v4l2_mbus_framefmt *format);
1861
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001862/* restore the last set video mode after chip power-on */
1863static int ov5640_restore_mode(struct ov5640_dev *sensor)
1864{
1865 int ret;
1866
1867 /* first load the initial register values */
1868 ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
1869 if (ret < 0)
1870 return ret;
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04001871 sensor->last_mode = &ov5640_mode_init_data;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001872
Maxime Ripard8f57c2f2018-04-16 08:36:53 -04001873 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
Maxime Ripard7851fe72018-12-03 03:44:19 -05001874 (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
1875 ilog2(OV5640_SCLK_ROOT_DIV));
Maxime Ripard8f57c2f2018-04-16 08:36:53 -04001876 if (ret)
1877 return ret;
1878
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001879 /* now restore the last capture mode */
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04001880 ret = ov5640_set_mode(sensor);
Akinobu Mita19ad26f2018-03-11 11:34:41 -04001881 if (ret < 0)
1882 return ret;
1883
1884 return ov5640_set_framefmt(sensor, &sensor->fmt);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001885}
1886
1887static void ov5640_power(struct ov5640_dev *sensor, bool enable)
1888{
Hugues Fruchet1fddc5d2018-01-03 04:57:28 -05001889 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001890}
1891
1892static void ov5640_reset(struct ov5640_dev *sensor)
1893{
1894 if (!sensor->reset_gpio)
1895 return;
1896
Hugues Fruchet1fddc5d2018-01-03 04:57:28 -05001897 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001898
1899 /* camera power cycle */
1900 ov5640_power(sensor, false);
1901 usleep_range(5000, 10000);
1902 ov5640_power(sensor, true);
1903 usleep_range(5000, 10000);
1904
Hugues Fruchet1fddc5d2018-01-03 04:57:28 -05001905 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001906 usleep_range(1000, 2000);
1907
Hugues Fruchet1fddc5d2018-01-03 04:57:28 -05001908 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001909 usleep_range(5000, 10000);
1910}
1911
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05001912static int ov5640_set_power_on(struct ov5640_dev *sensor)
1913{
1914 struct i2c_client *client = sensor->i2c_client;
1915 int ret;
1916
1917 ret = clk_prepare_enable(sensor->xclk);
1918 if (ret) {
1919 dev_err(&client->dev, "%s: failed to enable clock\n",
1920 __func__);
1921 return ret;
1922 }
1923
1924 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
1925 sensor->supplies);
1926 if (ret) {
1927 dev_err(&client->dev, "%s: failed to enable regulators\n",
1928 __func__);
1929 goto xclk_off;
1930 }
1931
1932 ov5640_reset(sensor);
1933 ov5640_power(sensor, true);
1934
1935 ret = ov5640_init_slave_id(sensor);
1936 if (ret)
1937 goto power_off;
1938
1939 return 0;
1940
1941power_off:
1942 ov5640_power(sensor, false);
1943 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1944xclk_off:
1945 clk_disable_unprepare(sensor->xclk);
1946 return ret;
1947}
1948
1949static void ov5640_set_power_off(struct ov5640_dev *sensor)
1950{
1951 ov5640_power(sensor, false);
1952 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1953 clk_disable_unprepare(sensor->xclk);
1954}
1955
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001956static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
1957{
1958 int ret = 0;
1959
1960 if (on) {
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05001961 ret = ov5640_set_power_on(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001962 if (ret)
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05001963 return ret;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03001964
1965 ret = ov5640_restore_mode(sensor);
1966 if (ret)
1967 goto power_off;
1968
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04001969 /* We're done here for DVP bus, while CSI-2 needs setup. */
Sakari Ailus2d95e7e2018-07-03 17:19:27 -04001970 if (sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY)
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04001971 return 0;
1972
1973 /*
1974 * Power up MIPI HS Tx and LS Rx; 2 data lanes mode
1975 *
1976 * 0x300e = 0x40
1977 * [7:5] = 010 : 2 data lanes mode (see FIXME note in
1978 * "ov5640_set_stream_mipi()")
1979 * [4] = 0 : Power up MIPI HS Tx
1980 * [3] = 0 : Power up MIPI LS Rx
1981 * [2] = 0 : MIPI interface disabled
1982 */
1983 ret = ov5640_write_reg(sensor,
1984 OV5640_REG_IO_MIPI_CTRL00, 0x40);
1985 if (ret)
1986 goto power_off;
1987
1988 /*
1989 * Gate clock and set LP11 in 'no packets mode' (idle)
1990 *
1991 * 0x4800 = 0x24
1992 * [5] = 1 : Gate clock when 'no packets'
1993 * [2] = 1 : MIPI bus in LP11 when 'no packets'
1994 */
1995 ret = ov5640_write_reg(sensor,
1996 OV5640_REG_MIPI_CTRL00, 0x24);
1997 if (ret)
1998 goto power_off;
1999
2000 /*
2001 * Set data lanes and clock in LP11 when 'sleeping'
2002 *
2003 * 0x3019 = 0x70
2004 * [6] = 1 : MIPI data lane 2 in LP11 when 'sleeping'
2005 * [5] = 1 : MIPI data lane 1 in LP11 when 'sleeping'
2006 * [4] = 1 : MIPI clock lane in LP11 when 'sleeping'
2007 */
2008 ret = ov5640_write_reg(sensor,
2009 OV5640_REG_PAD_OUTPUT00, 0x70);
2010 if (ret)
2011 goto power_off;
2012
2013 /* Give lanes some time to coax into LP11 state. */
2014 usleep_range(500, 1000);
2015
2016 } else {
Sakari Ailus2d95e7e2018-07-03 17:19:27 -04002017 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04002018 /* Reset MIPI bus settings to their default values. */
2019 ov5640_write_reg(sensor,
2020 OV5640_REG_IO_MIPI_CTRL00, 0x58);
2021 ov5640_write_reg(sensor,
2022 OV5640_REG_MIPI_CTRL00, 0x04);
2023 ov5640_write_reg(sensor,
2024 OV5640_REG_PAD_OUTPUT00, 0x00);
Hugues Fruchetf22996d2018-01-03 04:57:31 -05002025 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002026
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04002027 ov5640_set_power_off(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002028 }
2029
Jacopo Mondiaa4bb8b2018-07-06 05:51:52 -04002030 return 0;
2031
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002032power_off:
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05002033 ov5640_set_power_off(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002034 return ret;
2035}
2036
2037/* --------------- Subdev Operations --------------- */
2038
2039static int ov5640_s_power(struct v4l2_subdev *sd, int on)
2040{
2041 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2042 int ret = 0;
2043
2044 mutex_lock(&sensor->lock);
2045
2046 /*
2047 * If the power count is modified from 0 to != 0 or from != 0 to 0,
2048 * update the power state.
2049 */
2050 if (sensor->power_count == !on) {
2051 ret = ov5640_set_power(sensor, !!on);
2052 if (ret)
2053 goto out;
2054 }
2055
2056 /* Update the power count. */
2057 sensor->power_count += on ? 1 : -1;
2058 WARN_ON(sensor->power_count < 0);
2059out:
2060 mutex_unlock(&sensor->lock);
2061
2062 if (on && !ret && sensor->power_count == 1) {
2063 /* restore controls */
2064 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2065 }
2066
2067 return ret;
2068}
2069
2070static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2071 struct v4l2_fract *fi,
2072 u32 width, u32 height)
2073{
2074 const struct ov5640_mode_info *mode;
Maxime Ripard5a3ad932018-12-03 03:44:24 -05002075 enum ov5640_frame_rate rate = OV5640_30_FPS;
Maxime Ripardf6cc1922018-12-03 03:44:25 -05002076 int minfps, maxfps, best_fps, fps;
2077 int i;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002078
2079 minfps = ov5640_framerates[OV5640_15_FPS];
Maxime Riparde823fb12018-12-03 03:44:26 -05002080 maxfps = ov5640_framerates[OV5640_60_FPS];
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002081
2082 if (fi->numerator == 0) {
2083 fi->denominator = maxfps;
2084 fi->numerator = 1;
Maxime Riparde823fb12018-12-03 03:44:26 -05002085 rate = OV5640_60_FPS;
2086 goto find_mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002087 }
2088
Maxime Ripardf6cc1922018-12-03 03:44:25 -05002089 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2090 minfps, maxfps);
2091
2092 best_fps = minfps;
2093 for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2094 int curr_fps = ov5640_framerates[i];
2095
2096 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2097 best_fps = curr_fps;
2098 rate = i;
2099 }
2100 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002101
2102 fi->numerator = 1;
Maxime Ripardf6cc1922018-12-03 03:44:25 -05002103 fi->denominator = best_fps;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002104
Maxime Riparde823fb12018-12-03 03:44:26 -05002105find_mode:
Maxime Ripard5a3ad932018-12-03 03:44:24 -05002106 mode = ov5640_find_mode(sensor, rate, width, height, false);
2107 return mode ? rate : -EINVAL;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002108}
2109
2110static int ov5640_get_fmt(struct v4l2_subdev *sd,
2111 struct v4l2_subdev_pad_config *cfg,
2112 struct v4l2_subdev_format *format)
2113{
2114 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2115 struct v4l2_mbus_framefmt *fmt;
2116
2117 if (format->pad != 0)
2118 return -EINVAL;
2119
2120 mutex_lock(&sensor->lock);
2121
2122 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2123 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
2124 format->pad);
2125 else
2126 fmt = &sensor->fmt;
2127
2128 format->format = *fmt;
2129
2130 mutex_unlock(&sensor->lock);
2131
2132 return 0;
2133}
2134
2135static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2136 struct v4l2_mbus_framefmt *fmt,
2137 enum ov5640_frame_rate fr,
2138 const struct ov5640_mode_info **new_mode)
2139{
2140 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2141 const struct ov5640_mode_info *mode;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002142 int i;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002143
2144 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
2145 if (!mode)
2146 return -EINVAL;
Maxime Riparddba13a02018-04-16 08:36:54 -04002147 fmt->width = mode->hact;
2148 fmt->height = mode->vact;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002149
2150 if (new_mode)
2151 *new_mode = mode;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002152
2153 for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2154 if (ov5640_formats[i].code == fmt->code)
2155 break;
2156 if (i >= ARRAY_SIZE(ov5640_formats))
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002157 i = 0;
2158
2159 fmt->code = ov5640_formats[i].code;
2160 fmt->colorspace = ov5640_formats[i].colorspace;
2161 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2162 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2163 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002164
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002165 return 0;
2166}
2167
2168static int ov5640_set_fmt(struct v4l2_subdev *sd,
2169 struct v4l2_subdev_pad_config *cfg,
2170 struct v4l2_subdev_format *format)
2171{
2172 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2173 const struct ov5640_mode_info *new_mode;
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002174 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
Jacopo Mondi07115442018-12-03 03:44:16 -05002175 struct v4l2_mbus_framefmt *fmt;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002176 int ret;
2177
2178 if (format->pad != 0)
2179 return -EINVAL;
2180
2181 mutex_lock(&sensor->lock);
2182
2183 if (sensor->streaming) {
2184 ret = -EBUSY;
2185 goto out;
2186 }
2187
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002188 ret = ov5640_try_fmt_internal(sd, mbus_fmt,
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002189 sensor->current_fr, &new_mode);
2190 if (ret)
2191 goto out;
2192
Jacopo Mondi07115442018-12-03 03:44:16 -05002193 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2194 fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
2195 else
2196 fmt = &sensor->fmt;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002197
Jacopo Mondi07115442018-12-03 03:44:16 -05002198 *fmt = *mbus_fmt;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002199
Hugues Fruchet6949d862018-07-04 09:04:38 -04002200 if (new_mode != sensor->current_mode) {
2201 sensor->current_mode = new_mode;
Hugues Fruchet6949d862018-07-04 09:04:38 -04002202 sensor->pending_mode_change = true;
2203 }
Jacopo Mondi07115442018-12-03 03:44:16 -05002204 if (mbus_fmt->code != sensor->fmt.code)
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002205 sensor->pending_fmt_change = true;
Jacopo Mondi07115442018-12-03 03:44:16 -05002206
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002207out:
2208 mutex_unlock(&sensor->lock);
2209 return ret;
2210}
2211
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002212static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2213 struct v4l2_mbus_framefmt *format)
2214{
2215 int ret = 0;
Hugues Fruchetd47c4122018-01-31 04:08:10 -05002216 bool is_jpeg = false;
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002217 u8 fmt, mux;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002218
2219 switch (format->code) {
2220 case MEDIA_BUS_FMT_UYVY8_2X8:
2221 /* YUV422, UYVY */
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002222 fmt = 0x3f;
2223 mux = OV5640_FMT_MUX_YUV422;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002224 break;
2225 case MEDIA_BUS_FMT_YUYV8_2X8:
2226 /* YUV422, YUYV */
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002227 fmt = 0x30;
2228 mux = OV5640_FMT_MUX_YUV422;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002229 break;
2230 case MEDIA_BUS_FMT_RGB565_2X8_LE:
2231 /* RGB565 {g[2:0],b[4:0]},{r[4:0],g[5:3]} */
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002232 fmt = 0x6F;
2233 mux = OV5640_FMT_MUX_RGB;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002234 break;
2235 case MEDIA_BUS_FMT_RGB565_2X8_BE:
2236 /* RGB565 {r[4:0],g[5:3]},{g[2:0],b[4:0]} */
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002237 fmt = 0x61;
2238 mux = OV5640_FMT_MUX_RGB;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002239 break;
Hugues Fruchetd47c4122018-01-31 04:08:10 -05002240 case MEDIA_BUS_FMT_JPEG_1X8:
2241 /* YUV422, YUYV */
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002242 fmt = 0x30;
2243 mux = OV5640_FMT_MUX_YUV422;
Hugues Fruchetd47c4122018-01-31 04:08:10 -05002244 is_jpeg = true;
2245 break;
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002246 case MEDIA_BUS_FMT_SBGGR8_1X8:
2247 /* Raw, BGBG... / GRGR... */
2248 fmt = 0x00;
2249 mux = OV5640_FMT_MUX_RAW_DPC;
2250 break;
2251 case MEDIA_BUS_FMT_SGBRG8_1X8:
2252 /* Raw bayer, GBGB... / RGRG... */
2253 fmt = 0x01;
2254 mux = OV5640_FMT_MUX_RAW_DPC;
2255 break;
2256 case MEDIA_BUS_FMT_SGRBG8_1X8:
2257 /* Raw bayer, GRGR... / BGBG... */
2258 fmt = 0x02;
2259 mux = OV5640_FMT_MUX_RAW_DPC;
2260 break;
2261 case MEDIA_BUS_FMT_SRGGB8_1X8:
2262 /* Raw bayer, RGRG... / GBGB... */
2263 fmt = 0x03;
2264 mux = OV5640_FMT_MUX_RAW_DPC;
2265 break;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002266 default:
2267 return -EINVAL;
2268 }
2269
2270 /* FORMAT CONTROL00: YUV and RGB formatting */
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002271 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt);
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002272 if (ret)
2273 return ret;
2274
2275 /* FORMAT MUX CONTROL: ISP YUV or RGB */
Loic Poulainb7ed3ab2018-11-02 12:38:43 -04002276 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux);
Hugues Fruchetd47c4122018-01-31 04:08:10 -05002277 if (ret)
2278 return ret;
2279
2280 /*
2281 * TIMING TC REG21:
2282 * - [5]: JPEG enable
2283 */
2284 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2285 BIT(5), is_jpeg ? BIT(5) : 0);
2286 if (ret)
2287 return ret;
2288
2289 /*
2290 * SYSTEM RESET02:
2291 * - [4]: Reset JFIFO
2292 * - [3]: Reset SFIFO
2293 * - [2]: Reset JPEG
2294 */
2295 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2296 BIT(4) | BIT(3) | BIT(2),
2297 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2298 if (ret)
2299 return ret;
2300
2301 /*
2302 * CLOCK ENABLE02:
2303 * - [5]: Enable JPEG 2x clock
2304 * - [3]: Enable JPEG clock
2305 */
2306 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2307 BIT(5) | BIT(3),
2308 is_jpeg ? (BIT(5) | BIT(3)) : 0);
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002309}
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002310
2311/*
2312 * Sensor Controls.
2313 */
2314
2315static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
2316{
2317 int ret;
2318
2319 if (value) {
2320 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2321 BIT(0), BIT(0));
2322 if (ret)
2323 return ret;
2324 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
2325 } else {
2326 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
2327 }
2328
2329 return ret;
2330}
2331
2332static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
2333{
2334 int ret;
2335
2336 if (value) {
2337 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2338 BIT(2), BIT(2));
2339 if (ret)
2340 return ret;
2341 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
2342 value & 0xff);
2343 } else {
2344 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
2345 }
2346
2347 return ret;
2348}
2349
2350static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
2351{
2352 int ret;
2353
2354 if (value) {
2355 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2356 BIT(1), BIT(1));
2357 if (ret)
2358 return ret;
2359 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
2360 value & 0xff);
2361 if (ret)
2362 return ret;
2363 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
2364 value & 0xff);
2365 } else {
2366 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
2367 }
2368
2369 return ret;
2370}
2371
2372static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
2373{
2374 int ret;
2375
2376 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
2377 BIT(0), awb ? 0 : 1);
2378 if (ret)
2379 return ret;
2380
2381 if (!awb) {
2382 u16 red = (u16)sensor->ctrls.red_balance->val;
2383 u16 blue = (u16)sensor->ctrls.blue_balance->val;
2384
2385 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
2386 if (ret)
2387 return ret;
2388 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
2389 }
2390
2391 return ret;
2392}
2393
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002394static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
2395 enum v4l2_exposure_auto_type auto_exposure)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002396{
2397 struct ov5640_ctrls *ctrls = &sensor->ctrls;
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002398 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002399 int ret = 0;
2400
2401 if (ctrls->auto_exp->is_new) {
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002402 ret = ov5640_set_autoexposure(sensor, auto_exp);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002403 if (ret)
2404 return ret;
2405 }
2406
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002407 if (!auto_exp && ctrls->exposure->is_new) {
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002408 u16 max_exp;
2409
2410 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
2411 &max_exp);
2412 if (ret)
2413 return ret;
2414 ret = ov5640_get_vts(sensor);
2415 if (ret < 0)
2416 return ret;
2417 max_exp += ret;
Hugues Fruchet6146fde2018-01-22 05:06:40 -05002418 ret = 0;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002419
2420 if (ctrls->exposure->val < max_exp)
2421 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
2422 }
2423
2424 return ret;
2425}
2426
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002427static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002428{
2429 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2430 int ret = 0;
2431
2432 if (ctrls->auto_gain->is_new) {
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002433 ret = ov5640_set_autogain(sensor, auto_gain);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002434 if (ret)
2435 return ret;
2436 }
2437
Hugues Fruchet3cca8ef2018-09-11 09:48:18 -04002438 if (!auto_gain && ctrls->gain->is_new)
2439 ret = ov5640_set_gain(sensor, ctrls->gain->val);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002440
2441 return ret;
2442}
2443
Chen-Yu Tsai9f6d7ba2019-01-18 03:52:01 -05002444static const char * const test_pattern_menu[] = {
2445 "Disabled",
2446 "Color bars",
2447};
2448
Chen-Yu Tsaia0c29af2019-01-18 03:52:02 -05002449#define OV5640_TEST_ENABLE BIT(7)
2450#define OV5640_TEST_ROLLING BIT(6) /* rolling horizontal bar */
2451#define OV5640_TEST_TRANSPARENT BIT(5)
2452#define OV5640_TEST_SQUARE_BW BIT(4) /* black & white squares */
2453#define OV5640_TEST_BAR_STANDARD (0 << 2)
2454#define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2)
2455#define OV5640_TEST_BAR_HOR_CHANGE (2 << 2)
2456#define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2)
2457#define OV5640_TEST_BAR (0 << 0)
2458#define OV5640_TEST_RANDOM (1 << 0)
2459#define OV5640_TEST_SQUARE (2 << 0)
2460#define OV5640_TEST_BLACK (3 << 0)
2461
2462static const u8 test_pattern_val[] = {
2463 0,
Chen-Yu Tsai2aff1fc2019-01-18 03:52:03 -05002464 OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
Chen-Yu Tsaia0c29af2019-01-18 03:52:02 -05002465 OV5640_TEST_BAR,
2466};
2467
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002468static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
2469{
Chen-Yu Tsaia0c29af2019-01-18 03:52:02 -05002470 return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
2471 test_pattern_val[value]);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002472}
2473
Mylène Josserand1068fec2018-04-16 08:36:51 -04002474static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
2475{
2476 int ret;
2477
2478 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
2479 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
2480 0 : BIT(7));
2481 if (ret)
2482 return ret;
2483
2484 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
2485 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
2486 BIT(2) : 0);
2487}
2488
Hugues Fruchetce857052018-06-18 06:29:17 -04002489static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2490{
2491 /*
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002492 * If sensor is mounted upside down, mirror logic is inversed.
2493 *
Hugues Fruchetce857052018-06-18 06:29:17 -04002494 * Sensor is a BSI (Back Side Illuminated) one,
2495 * so image captured is physically mirrored.
2496 * This is why mirror logic is inversed in
2497 * order to cancel this mirror effect.
2498 */
2499
2500 /*
2501 * TIMING TC REG21:
2502 * - [2]: ISP mirror
2503 * - [1]: Sensor mirror
2504 */
2505 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2506 BIT(2) | BIT(1),
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002507 (!(value ^ sensor->upside_down)) ?
2508 (BIT(2) | BIT(1)) : 0);
Hugues Fruchetce857052018-06-18 06:29:17 -04002509}
2510
2511static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2512{
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002513 /* If sensor is mounted upside down, flip logic is inversed */
2514
Hugues Fruchetce857052018-06-18 06:29:17 -04002515 /*
2516 * TIMING TC REG20:
2517 * - [2]: ISP vflip
2518 * - [1]: Sensor vflip
2519 */
2520 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2521 BIT(2) | BIT(1),
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002522 (value ^ sensor->upside_down) ?
2523 (BIT(2) | BIT(1)) : 0);
Hugues Fruchetce857052018-06-18 06:29:17 -04002524}
2525
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002526static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
2527{
2528 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2529 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2530 int val;
2531
2532 /* v4l2_ctrl_lock() locks our own mutex */
2533
2534 switch (ctrl->id) {
2535 case V4L2_CID_AUTOGAIN:
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002536 val = ov5640_get_gain(sensor);
2537 if (val < 0)
2538 return val;
2539 sensor->ctrls.gain->val = val;
2540 break;
2541 case V4L2_CID_EXPOSURE_AUTO:
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002542 val = ov5640_get_exposure(sensor);
2543 if (val < 0)
2544 return val;
2545 sensor->ctrls.exposure->val = val;
2546 break;
2547 }
2548
2549 return 0;
2550}
2551
2552static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
2553{
2554 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2555 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2556 int ret;
2557
2558 /* v4l2_ctrl_lock() locks our own mutex */
2559
2560 /*
2561 * If the device is not powered up by the host driver do
2562 * not apply any controls to H/W at this time. Instead
2563 * the controls will be restored right after power-up.
2564 */
2565 if (sensor->power_count == 0)
2566 return 0;
2567
2568 switch (ctrl->id) {
2569 case V4L2_CID_AUTOGAIN:
2570 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
2571 break;
2572 case V4L2_CID_EXPOSURE_AUTO:
2573 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
2574 break;
2575 case V4L2_CID_AUTO_WHITE_BALANCE:
2576 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
2577 break;
2578 case V4L2_CID_HUE:
2579 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
2580 break;
2581 case V4L2_CID_CONTRAST:
2582 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
2583 break;
2584 case V4L2_CID_SATURATION:
2585 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
2586 break;
2587 case V4L2_CID_TEST_PATTERN:
2588 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
2589 break;
Mylène Josserand1068fec2018-04-16 08:36:51 -04002590 case V4L2_CID_POWER_LINE_FREQUENCY:
2591 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
2592 break;
Hugues Fruchetce857052018-06-18 06:29:17 -04002593 case V4L2_CID_HFLIP:
2594 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
2595 break;
2596 case V4L2_CID_VFLIP:
2597 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
2598 break;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002599 default:
2600 ret = -EINVAL;
2601 break;
2602 }
2603
2604 return ret;
2605}
2606
2607static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
2608 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
2609 .s_ctrl = ov5640_s_ctrl,
2610};
2611
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002612static int ov5640_init_controls(struct ov5640_dev *sensor)
2613{
2614 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
2615 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2616 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2617 int ret;
2618
2619 v4l2_ctrl_handler_init(hdl, 32);
2620
2621 /* we can use our own mutex for the ctrl lock */
2622 hdl->lock = &sensor->lock;
2623
2624 /* Auto/manual white balance */
2625 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
2626 V4L2_CID_AUTO_WHITE_BALANCE,
2627 0, 1, 1, 1);
2628 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
2629 0, 4095, 1, 0);
2630 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
2631 0, 4095, 1, 0);
2632 /* Auto/manual exposure */
2633 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
2634 V4L2_CID_EXPOSURE_AUTO,
2635 V4L2_EXPOSURE_MANUAL, 0,
2636 V4L2_EXPOSURE_AUTO);
2637 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2638 0, 65535, 1, 0);
2639 /* Auto/manual gain */
2640 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
2641 0, 1, 1, 1);
2642 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
2643 0, 1023, 1, 0);
2644
2645 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
2646 0, 255, 1, 64);
2647 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
2648 0, 359, 1, 0);
2649 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
2650 0, 255, 1, 0);
2651 ctrls->test_pattern =
2652 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2653 ARRAY_SIZE(test_pattern_menu) - 1,
2654 0, 0, test_pattern_menu);
Hugues Fruchetce857052018-06-18 06:29:17 -04002655 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2656 0, 1, 1, 0);
2657 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2658 0, 1, 1, 0);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002659
Mylène Josserand1068fec2018-04-16 08:36:51 -04002660 ctrls->light_freq =
2661 v4l2_ctrl_new_std_menu(hdl, ops,
2662 V4L2_CID_POWER_LINE_FREQUENCY,
2663 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
2664 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
2665
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002666 if (hdl->error) {
2667 ret = hdl->error;
2668 goto free_ctrls;
2669 }
2670
2671 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2672 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2673
2674 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2675 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
2676 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
2677
2678 sensor->sd.ctrl_handler = hdl;
2679 return 0;
2680
2681free_ctrls:
2682 v4l2_ctrl_handler_free(hdl);
2683 return ret;
2684}
2685
2686static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
2687 struct v4l2_subdev_pad_config *cfg,
2688 struct v4l2_subdev_frame_size_enum *fse)
2689{
2690 if (fse->pad != 0)
2691 return -EINVAL;
2692 if (fse->index >= OV5640_NUM_MODES)
2693 return -EINVAL;
2694
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05002695 fse->min_width =
Maxime Ripard086c25f2018-12-03 03:44:23 -05002696 ov5640_mode_data[fse->index].hact;
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05002697 fse->max_width = fse->min_width;
2698 fse->min_height =
Maxime Ripard086c25f2018-12-03 03:44:23 -05002699 ov5640_mode_data[fse->index].vact;
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05002700 fse->max_height = fse->min_height;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002701
2702 return 0;
2703}
2704
2705static int ov5640_enum_frame_interval(
2706 struct v4l2_subdev *sd,
2707 struct v4l2_subdev_pad_config *cfg,
2708 struct v4l2_subdev_frame_interval_enum *fie)
2709{
2710 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2711 struct v4l2_fract tpf;
2712 int ret;
2713
2714 if (fie->pad != 0)
2715 return -EINVAL;
2716 if (fie->index >= OV5640_NUM_FRAMERATES)
2717 return -EINVAL;
2718
2719 tpf.numerator = 1;
2720 tpf.denominator = ov5640_framerates[fie->index];
2721
2722 ret = ov5640_try_frame_interval(sensor, &tpf,
2723 fie->width, fie->height);
2724 if (ret < 0)
2725 return -EINVAL;
2726
2727 fie->interval = tpf;
2728 return 0;
2729}
2730
2731static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
2732 struct v4l2_subdev_frame_interval *fi)
2733{
2734 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2735
2736 mutex_lock(&sensor->lock);
2737 fi->interval = sensor->frame_interval;
2738 mutex_unlock(&sensor->lock);
2739
2740 return 0;
2741}
2742
2743static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
2744 struct v4l2_subdev_frame_interval *fi)
2745{
2746 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2747 const struct ov5640_mode_info *mode;
2748 int frame_rate, ret = 0;
2749
2750 if (fi->pad != 0)
2751 return -EINVAL;
2752
2753 mutex_lock(&sensor->lock);
2754
2755 if (sensor->streaming) {
2756 ret = -EBUSY;
2757 goto out;
2758 }
2759
2760 mode = sensor->current_mode;
2761
2762 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
Maxime Riparddba13a02018-04-16 08:36:54 -04002763 mode->hact, mode->vact);
Maxime Riparde823fb12018-12-03 03:44:26 -05002764 if (frame_rate < 0) {
2765 /* Always return a valid frame interval value */
2766 fi->interval = sensor->frame_interval;
2767 goto out;
2768 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002769
Hugues Fruchet3c4a7372018-06-20 04:40:57 -04002770 mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
2771 mode->vact, true);
2772 if (!mode) {
2773 ret = -EINVAL;
2774 goto out;
2775 }
2776
Hugues Fruchet09299832018-10-04 07:21:57 -04002777 if (mode != sensor->current_mode ||
2778 frame_rate != sensor->current_fr) {
2779 sensor->current_fr = frame_rate;
2780 sensor->frame_interval = fi->interval;
Hugues Fruchet6949d862018-07-04 09:04:38 -04002781 sensor->current_mode = mode;
2782 sensor->pending_mode_change = true;
2783 }
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002784out:
2785 mutex_unlock(&sensor->lock);
2786 return ret;
2787}
2788
2789static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
Hugues Fruchet41d8d7f2018-02-01 03:44:06 -05002790 struct v4l2_subdev_pad_config *cfg,
2791 struct v4l2_subdev_mbus_code_enum *code)
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002792{
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002793 if (code->pad != 0)
2794 return -EINVAL;
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002795 if (code->index >= ARRAY_SIZE(ov5640_formats))
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002796 return -EINVAL;
2797
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002798 code->code = ov5640_formats[code->index].code;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002799 return 0;
2800}
2801
2802static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
2803{
2804 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2805 int ret = 0;
2806
2807 mutex_lock(&sensor->lock);
2808
2809 if (sensor->streaming == !enable) {
2810 if (enable && sensor->pending_mode_change) {
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04002811 ret = ov5640_set_mode(sensor);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002812 if (ret)
2813 goto out;
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002814 }
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002815
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002816 if (enable && sensor->pending_fmt_change) {
Hugues Fruchete3ee6912018-01-03 04:57:32 -05002817 ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2818 if (ret)
2819 goto out;
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002820 sensor->pending_fmt_change = false;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002821 }
2822
Sakari Ailus2d95e7e2018-07-03 17:19:27 -04002823 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
Hugues Fruchetf22996d2018-01-03 04:57:31 -05002824 ret = ov5640_set_stream_mipi(sensor, enable);
2825 else
2826 ret = ov5640_set_stream_dvp(sensor, enable);
2827
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002828 if (!ret)
2829 sensor->streaming = enable;
2830 }
2831out:
2832 mutex_unlock(&sensor->lock);
2833 return ret;
2834}
2835
2836static const struct v4l2_subdev_core_ops ov5640_core_ops = {
2837 .s_power = ov5640_s_power,
Akinobu Mita2d18fbc2018-11-12 11:00:52 -05002838 .log_status = v4l2_ctrl_subdev_log_status,
2839 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2840 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002841};
2842
2843static const struct v4l2_subdev_video_ops ov5640_video_ops = {
2844 .g_frame_interval = ov5640_g_frame_interval,
2845 .s_frame_interval = ov5640_s_frame_interval,
2846 .s_stream = ov5640_s_stream,
2847};
2848
2849static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
2850 .enum_mbus_code = ov5640_enum_mbus_code,
2851 .get_fmt = ov5640_get_fmt,
2852 .set_fmt = ov5640_set_fmt,
2853 .enum_frame_size = ov5640_enum_frame_size,
2854 .enum_frame_interval = ov5640_enum_frame_interval,
2855};
2856
2857static const struct v4l2_subdev_ops ov5640_subdev_ops = {
2858 .core = &ov5640_core_ops,
2859 .video = &ov5640_video_ops,
2860 .pad = &ov5640_pad_ops,
2861};
2862
2863static int ov5640_get_regulators(struct ov5640_dev *sensor)
2864{
2865 int i;
2866
2867 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
2868 sensor->supplies[i].supply = ov5640_supply_name[i];
2869
2870 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
2871 OV5640_NUM_SUPPLIES,
2872 sensor->supplies);
2873}
2874
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05002875static int ov5640_check_chip_id(struct ov5640_dev *sensor)
2876{
2877 struct i2c_client *client = sensor->i2c_client;
2878 int ret = 0;
2879 u16 chip_id;
2880
2881 ret = ov5640_set_power_on(sensor);
2882 if (ret)
2883 return ret;
2884
2885 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
2886 if (ret) {
2887 dev_err(&client->dev, "%s: failed to read chip identifier\n",
2888 __func__);
2889 goto power_off;
2890 }
2891
2892 if (chip_id != 0x5640) {
2893 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
2894 __func__, chip_id);
2895 ret = -ENXIO;
2896 }
2897
2898power_off:
2899 ov5640_set_power_off(sensor);
2900 return ret;
2901}
2902
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002903static int ov5640_probe(struct i2c_client *client,
2904 const struct i2c_device_id *id)
2905{
2906 struct device *dev = &client->dev;
2907 struct fwnode_handle *endpoint;
2908 struct ov5640_dev *sensor;
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002909 struct v4l2_mbus_framefmt *fmt;
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002910 u32 rotation;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002911 int ret;
2912
2913 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2914 if (!sensor)
2915 return -ENOMEM;
2916
2917 sensor->i2c_client = client;
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002918
2919 /*
2920 * default init sequence initialize sensor to
2921 * YUV422 UYVY VGA@30fps
2922 */
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002923 fmt = &sensor->fmt;
Hugues Fruchetfb98e292018-08-16 05:46:53 -04002924 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
2925 fmt->colorspace = V4L2_COLORSPACE_SRGB;
Hugues Fruchete6441fd2018-03-06 12:04:39 -05002926 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2927 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2928 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2929 fmt->width = 640;
2930 fmt->height = 480;
2931 fmt->field = V4L2_FIELD_NONE;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002932 sensor->frame_interval.numerator = 1;
2933 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
2934 sensor->current_fr = OV5640_30_FPS;
2935 sensor->current_mode =
Maxime Ripard086c25f2018-12-03 03:44:23 -05002936 &ov5640_mode_data[OV5640_MODE_VGA_640_480];
Hugues Fruchet985cdcb2018-09-11 09:48:21 -04002937 sensor->last_mode = sensor->current_mode;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002938
2939 sensor->ae_target = 52;
2940
Hugues Fruchetc3f3ba32018-06-18 06:29:19 -04002941 /* optional indication of physical rotation of sensor */
2942 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
2943 &rotation);
2944 if (!ret) {
2945 switch (rotation) {
2946 case 180:
2947 sensor->upside_down = true;
2948 /* fall through */
2949 case 0:
2950 break;
2951 default:
2952 dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
2953 rotation);
2954 }
2955 }
2956
Sakari Ailusce96bcf2018-04-24 06:25:47 -04002957 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
2958 NULL);
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002959 if (!endpoint) {
2960 dev_err(dev, "endpoint node not found\n");
2961 return -EINVAL;
2962 }
2963
2964 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
2965 fwnode_handle_put(endpoint);
2966 if (ret) {
2967 dev_err(dev, "Could not parse endpoint\n");
2968 return ret;
2969 }
2970
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002971 /* get system clock (xclk) */
2972 sensor->xclk = devm_clk_get(dev, "xclk");
2973 if (IS_ERR(sensor->xclk)) {
2974 dev_err(dev, "failed to get xclk\n");
2975 return PTR_ERR(sensor->xclk);
2976 }
2977
2978 sensor->xclk_freq = clk_get_rate(sensor->xclk);
2979 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
2980 sensor->xclk_freq > OV5640_XCLK_MAX) {
2981 dev_err(dev, "xclk frequency out of range: %d Hz\n",
2982 sensor->xclk_freq);
2983 return -EINVAL;
2984 }
2985
2986 /* request optional power down pin */
2987 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
2988 GPIOD_OUT_HIGH);
2989 /* request optional reset pin */
2990 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2991 GPIOD_OUT_HIGH);
2992
2993 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
2994
Akinobu Mita2d18fbc2018-11-12 11:00:52 -05002995 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
2996 V4L2_SUBDEV_FL_HAS_EVENTS;
Steve Longerbeam19a81c12017-06-07 15:33:56 -03002997 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
2998 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
2999 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3000 if (ret)
3001 return ret;
3002
3003 ret = ov5640_get_regulators(sensor);
3004 if (ret)
3005 return ret;
3006
3007 mutex_init(&sensor->lock);
3008
Hugues Fruchet0f7acb52018-01-03 04:57:29 -05003009 ret = ov5640_check_chip_id(sensor);
3010 if (ret)
3011 goto entity_cleanup;
3012
Steve Longerbeam19a81c12017-06-07 15:33:56 -03003013 ret = ov5640_init_controls(sensor);
3014 if (ret)
3015 goto entity_cleanup;
3016
3017 ret = v4l2_async_register_subdev(&sensor->sd);
3018 if (ret)
3019 goto free_ctrls;
3020
3021 return 0;
3022
3023free_ctrls:
3024 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3025entity_cleanup:
3026 mutex_destroy(&sensor->lock);
3027 media_entity_cleanup(&sensor->sd.entity);
3028 return ret;
3029}
3030
3031static int ov5640_remove(struct i2c_client *client)
3032{
3033 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3034 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3035
3036 v4l2_async_unregister_subdev(&sensor->sd);
3037 mutex_destroy(&sensor->lock);
3038 media_entity_cleanup(&sensor->sd.entity);
3039 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3040
3041 return 0;
3042}
3043
3044static const struct i2c_device_id ov5640_id[] = {
3045 {"ov5640", 0},
3046 {},
3047};
3048MODULE_DEVICE_TABLE(i2c, ov5640_id);
3049
3050static const struct of_device_id ov5640_dt_ids[] = {
3051 { .compatible = "ovti,ov5640" },
3052 { /* sentinel */ }
3053};
3054MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3055
3056static struct i2c_driver ov5640_i2c_driver = {
3057 .driver = {
3058 .name = "ov5640",
3059 .of_match_table = ov5640_dt_ids,
3060 },
3061 .id_table = ov5640_id,
3062 .probe = ov5640_probe,
3063 .remove = ov5640_remove,
3064};
3065
3066module_i2c_driver(ov5640_i2c_driver);
3067
3068MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
3069MODULE_LICENSE("GPL");