1
2
3
4
5
6
7#include <linux/clk.h>
8#include <linux/clk-provider.h>
9#include <linux/clkdev.h>
10#include <linux/ctype.h>
11#include <linux/delay.h>
12#include <linux/device.h>
13#include <linux/gpio/consumer.h>
14#include <linux/i2c.h>
15#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/of_device.h>
18#include <linux/regulator/consumer.h>
19#include <linux/slab.h>
20#include <linux/types.h>
21#include <media/v4l2-async.h>
22#include <media/v4l2-ctrls.h>
23#include <media/v4l2-device.h>
24#include <media/v4l2-event.h>
25#include <media/v4l2-fwnode.h>
26#include <media/v4l2-subdev.h>
27
28
29#define OV5640_XCLK_MIN 6000000
30#define OV5640_XCLK_MAX 54000000
31
32#define OV5640_DEFAULT_SLAVE_ID 0x3c
33
34#define OV5640_REG_SYS_RESET02 0x3002
35#define OV5640_REG_SYS_CLOCK_ENABLE02 0x3006
36#define OV5640_REG_SYS_CTRL0 0x3008
37#define OV5640_REG_CHIP_ID 0x300a
38#define OV5640_REG_IO_MIPI_CTRL00 0x300e
39#define OV5640_REG_PAD_OUTPUT_ENABLE01 0x3017
40#define OV5640_REG_PAD_OUTPUT_ENABLE02 0x3018
41#define OV5640_REG_PAD_OUTPUT00 0x3019
42#define OV5640_REG_SYSTEM_CONTROL1 0x302e
43#define OV5640_REG_SC_PLL_CTRL0 0x3034
44#define OV5640_REG_SC_PLL_CTRL1 0x3035
45#define OV5640_REG_SC_PLL_CTRL2 0x3036
46#define OV5640_REG_SC_PLL_CTRL3 0x3037
47#define OV5640_REG_SLAVE_ID 0x3100
48#define OV5640_REG_SCCB_SYS_CTRL1 0x3103
49#define OV5640_REG_SYS_ROOT_DIVIDER 0x3108
50#define OV5640_REG_AWB_R_GAIN 0x3400
51#define OV5640_REG_AWB_G_GAIN 0x3402
52#define OV5640_REG_AWB_B_GAIN 0x3404
53#define OV5640_REG_AWB_MANUAL_CTRL 0x3406
54#define OV5640_REG_AEC_PK_EXPOSURE_HI 0x3500
55#define OV5640_REG_AEC_PK_EXPOSURE_MED 0x3501
56#define OV5640_REG_AEC_PK_EXPOSURE_LO 0x3502
57#define OV5640_REG_AEC_PK_MANUAL 0x3503
58#define OV5640_REG_AEC_PK_REAL_GAIN 0x350a
59#define OV5640_REG_AEC_PK_VTS 0x350c
60#define OV5640_REG_TIMING_DVPHO 0x3808
61#define OV5640_REG_TIMING_DVPVO 0x380a
62#define OV5640_REG_TIMING_HTS 0x380c
63#define OV5640_REG_TIMING_VTS 0x380e
64#define OV5640_REG_TIMING_TC_REG20 0x3820
65#define OV5640_REG_TIMING_TC_REG21 0x3821
66#define OV5640_REG_AEC_CTRL00 0x3a00
67#define OV5640_REG_AEC_B50_STEP 0x3a08
68#define OV5640_REG_AEC_B60_STEP 0x3a0a
69#define OV5640_REG_AEC_CTRL0D 0x3a0d
70#define OV5640_REG_AEC_CTRL0E 0x3a0e
71#define OV5640_REG_AEC_CTRL0F 0x3a0f
72#define OV5640_REG_AEC_CTRL10 0x3a10
73#define OV5640_REG_AEC_CTRL11 0x3a11
74#define OV5640_REG_AEC_CTRL1B 0x3a1b
75#define OV5640_REG_AEC_CTRL1E 0x3a1e
76#define OV5640_REG_AEC_CTRL1F 0x3a1f
77#define OV5640_REG_HZ5060_CTRL00 0x3c00
78#define OV5640_REG_HZ5060_CTRL01 0x3c01
79#define OV5640_REG_SIGMADELTA_CTRL0C 0x3c0c
80#define OV5640_REG_FRAME_CTRL01 0x4202
81#define OV5640_REG_FORMAT_CONTROL00 0x4300
82#define OV5640_REG_VFIFO_HSIZE 0x4602
83#define OV5640_REG_VFIFO_VSIZE 0x4604
84#define OV5640_REG_JPG_MODE_SELECT 0x4713
85#define OV5640_REG_POLARITY_CTRL00 0x4740
86#define OV5640_REG_MIPI_CTRL00 0x4800
87#define OV5640_REG_DEBUG_MODE 0x4814
88#define OV5640_REG_ISP_FORMAT_MUX_CTRL 0x501f
89#define OV5640_REG_PRE_ISP_TEST_SET1 0x503d
90#define OV5640_REG_SDE_CTRL0 0x5580
91#define OV5640_REG_SDE_CTRL1 0x5581
92#define OV5640_REG_SDE_CTRL3 0x5583
93#define OV5640_REG_SDE_CTRL4 0x5584
94#define OV5640_REG_SDE_CTRL5 0x5585
95#define OV5640_REG_AVG_READOUT 0x56a1
96
97enum ov5640_mode_id {
98 OV5640_MODE_QCIF_176_144 = 0,
99 OV5640_MODE_QVGA_320_240,
100 OV5640_MODE_VGA_640_480,
101 OV5640_MODE_NTSC_720_480,
102 OV5640_MODE_PAL_720_576,
103 OV5640_MODE_XGA_1024_768,
104 OV5640_MODE_720P_1280_720,
105 OV5640_MODE_1080P_1920_1080,
106 OV5640_MODE_QSXGA_2592_1944,
107 OV5640_NUM_MODES,
108};
109
110enum ov5640_frame_rate {
111 OV5640_15_FPS = 0,
112 OV5640_30_FPS,
113 OV5640_60_FPS,
114 OV5640_NUM_FRAMERATES,
115};
116
117enum ov5640_format_mux {
118 OV5640_FMT_MUX_YUV422 = 0,
119 OV5640_FMT_MUX_RGB,
120 OV5640_FMT_MUX_DITHER,
121 OV5640_FMT_MUX_RAW_DPC,
122 OV5640_FMT_MUX_SNR_RAW,
123 OV5640_FMT_MUX_RAW_CIP,
124};
125
126struct ov5640_pixfmt {
127 u32 code;
128 u32 colorspace;
129};
130
131static const struct ov5640_pixfmt ov5640_formats[] = {
132 { MEDIA_BUS_FMT_JPEG_1X8, V4L2_COLORSPACE_JPEG, },
133 { MEDIA_BUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_SRGB, },
134 { MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_SRGB, },
135 { MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB, },
136 { MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB, },
137 { MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB, },
138 { MEDIA_BUS_FMT_SGBRG8_1X8, V4L2_COLORSPACE_SRGB, },
139 { MEDIA_BUS_FMT_SGRBG8_1X8, V4L2_COLORSPACE_SRGB, },
140 { MEDIA_BUS_FMT_SRGGB8_1X8, V4L2_COLORSPACE_SRGB, },
141};
142
143
144
145
146
147static unsigned int virtual_channel;
148module_param(virtual_channel, uint, 0444);
149MODULE_PARM_DESC(virtual_channel,
150 "MIPI CSI-2 virtual channel (0..3), default 0");
151
152static const int ov5640_framerates[] = {
153 [OV5640_15_FPS] = 15,
154 [OV5640_30_FPS] = 30,
155 [OV5640_60_FPS] = 60,
156};
157
158
159static const char * const ov5640_supply_name[] = {
160 "DOVDD",
161 "AVDD",
162 "DVDD",
163};
164
165#define OV5640_NUM_SUPPLIES ARRAY_SIZE(ov5640_supply_name)
166
167
168
169
170
171enum ov5640_downsize_mode {
172 SUBSAMPLING,
173 SCALING,
174};
175
176struct reg_value {
177 u16 reg_addr;
178 u8 val;
179 u8 mask;
180 u32 delay_ms;
181};
182
183struct ov5640_mode_info {
184 enum ov5640_mode_id id;
185 enum ov5640_downsize_mode dn_mode;
186 u32 hact;
187 u32 htot;
188 u32 vact;
189 u32 vtot;
190 const struct reg_value *reg_data;
191 u32 reg_data_size;
192 u32 max_fps;
193};
194
195struct ov5640_ctrls {
196 struct v4l2_ctrl_handler handler;
197 struct v4l2_ctrl *pixel_rate;
198 struct {
199 struct v4l2_ctrl *auto_exp;
200 struct v4l2_ctrl *exposure;
201 };
202 struct {
203 struct v4l2_ctrl *auto_wb;
204 struct v4l2_ctrl *blue_balance;
205 struct v4l2_ctrl *red_balance;
206 };
207 struct {
208 struct v4l2_ctrl *auto_gain;
209 struct v4l2_ctrl *gain;
210 };
211 struct v4l2_ctrl *brightness;
212 struct v4l2_ctrl *light_freq;
213 struct v4l2_ctrl *saturation;
214 struct v4l2_ctrl *contrast;
215 struct v4l2_ctrl *hue;
216 struct v4l2_ctrl *test_pattern;
217 struct v4l2_ctrl *hflip;
218 struct v4l2_ctrl *vflip;
219};
220
221struct ov5640_dev {
222 struct i2c_client *i2c_client;
223 struct v4l2_subdev sd;
224 struct media_pad pad;
225 struct v4l2_fwnode_endpoint ep;
226 struct clk *xclk;
227 u32 xclk_freq;
228
229 struct regulator_bulk_data supplies[OV5640_NUM_SUPPLIES];
230 struct gpio_desc *reset_gpio;
231 struct gpio_desc *pwdn_gpio;
232 bool upside_down;
233
234
235 struct mutex lock;
236
237 int power_count;
238
239 struct v4l2_mbus_framefmt fmt;
240 bool pending_fmt_change;
241
242 const struct ov5640_mode_info *current_mode;
243 const struct ov5640_mode_info *last_mode;
244 enum ov5640_frame_rate current_fr;
245 struct v4l2_fract frame_interval;
246
247 struct ov5640_ctrls ctrls;
248
249 u32 prev_sysclk, prev_hts;
250 u32 ae_low, ae_high, ae_target;
251
252 bool pending_mode_change;
253 bool streaming;
254};
255
256static inline struct ov5640_dev *to_ov5640_dev(struct v4l2_subdev *sd)
257{
258 return container_of(sd, struct ov5640_dev, sd);
259}
260
261static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
262{
263 return &container_of(ctrl->handler, struct ov5640_dev,
264 ctrls.handler)->sd;
265}
266
267
268
269
270
271
272
273
274
275static const struct reg_value ov5640_init_setting_30fps_VGA[] = {
276 {0x3103, 0x11, 0, 0}, {0x3008, 0x82, 0, 5}, {0x3008, 0x42, 0, 0},
277 {0x3103, 0x03, 0, 0}, {0x3017, 0x00, 0, 0}, {0x3018, 0x00, 0, 0},
278 {0x3630, 0x36, 0, 0},
279 {0x3631, 0x0e, 0, 0}, {0x3632, 0xe2, 0, 0}, {0x3633, 0x12, 0, 0},
280 {0x3621, 0xe0, 0, 0}, {0x3704, 0xa0, 0, 0}, {0x3703, 0x5a, 0, 0},
281 {0x3715, 0x78, 0, 0}, {0x3717, 0x01, 0, 0}, {0x370b, 0x60, 0, 0},
282 {0x3705, 0x1a, 0, 0}, {0x3905, 0x02, 0, 0}, {0x3906, 0x10, 0, 0},
283 {0x3901, 0x0a, 0, 0}, {0x3731, 0x12, 0, 0}, {0x3600, 0x08, 0, 0},
284 {0x3601, 0x33, 0, 0}, {0x302d, 0x60, 0, 0}, {0x3620, 0x52, 0, 0},
285 {0x371b, 0x20, 0, 0}, {0x471c, 0x50, 0, 0}, {0x3a13, 0x43, 0, 0},
286 {0x3a18, 0x00, 0, 0}, {0x3a19, 0xf8, 0, 0}, {0x3635, 0x13, 0, 0},
287 {0x3636, 0x03, 0, 0}, {0x3634, 0x40, 0, 0}, {0x3622, 0x01, 0, 0},
288 {0x3c01, 0xa4, 0, 0}, {0x3c04, 0x28, 0, 0}, {0x3c05, 0x98, 0, 0},
289 {0x3c06, 0x00, 0, 0}, {0x3c07, 0x08, 0, 0}, {0x3c08, 0x00, 0, 0},
290 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
291 {0x3820, 0x41, 0, 0}, {0x3821, 0x07, 0, 0}, {0x3814, 0x31, 0, 0},
292 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
293 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
294 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
295 {0x3810, 0x00, 0, 0},
296 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
297 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
298 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
299 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
300 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
301 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
302 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0}, {0x3000, 0x00, 0, 0},
303 {0x3002, 0x1c, 0, 0}, {0x3004, 0xff, 0, 0}, {0x3006, 0xc3, 0, 0},
304 {0x302e, 0x08, 0, 0}, {0x4300, 0x3f, 0, 0},
305 {0x501f, 0x00, 0, 0}, {0x4407, 0x04, 0, 0},
306 {0x440e, 0x00, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
307 {0x4837, 0x0a, 0, 0}, {0x3824, 0x02, 0, 0},
308 {0x5000, 0xa7, 0, 0}, {0x5001, 0xa3, 0, 0}, {0x5180, 0xff, 0, 0},
309 {0x5181, 0xf2, 0, 0}, {0x5182, 0x00, 0, 0}, {0x5183, 0x14, 0, 0},
310 {0x5184, 0x25, 0, 0}, {0x5185, 0x24, 0, 0}, {0x5186, 0x09, 0, 0},
311 {0x5187, 0x09, 0, 0}, {0x5188, 0x09, 0, 0}, {0x5189, 0x88, 0, 0},
312 {0x518a, 0x54, 0, 0}, {0x518b, 0xee, 0, 0}, {0x518c, 0xb2, 0, 0},
313 {0x518d, 0x50, 0, 0}, {0x518e, 0x34, 0, 0}, {0x518f, 0x6b, 0, 0},
314 {0x5190, 0x46, 0, 0}, {0x5191, 0xf8, 0, 0}, {0x5192, 0x04, 0, 0},
315 {0x5193, 0x70, 0, 0}, {0x5194, 0xf0, 0, 0}, {0x5195, 0xf0, 0, 0},
316 {0x5196, 0x03, 0, 0}, {0x5197, 0x01, 0, 0}, {0x5198, 0x04, 0, 0},
317 {0x5199, 0x6c, 0, 0}, {0x519a, 0x04, 0, 0}, {0x519b, 0x00, 0, 0},
318 {0x519c, 0x09, 0, 0}, {0x519d, 0x2b, 0, 0}, {0x519e, 0x38, 0, 0},
319 {0x5381, 0x1e, 0, 0}, {0x5382, 0x5b, 0, 0}, {0x5383, 0x08, 0, 0},
320 {0x5384, 0x0a, 0, 0}, {0x5385, 0x7e, 0, 0}, {0x5386, 0x88, 0, 0},
321 {0x5387, 0x7c, 0, 0}, {0x5388, 0x6c, 0, 0}, {0x5389, 0x10, 0, 0},
322 {0x538a, 0x01, 0, 0}, {0x538b, 0x98, 0, 0}, {0x5300, 0x08, 0, 0},
323 {0x5301, 0x30, 0, 0}, {0x5302, 0x10, 0, 0}, {0x5303, 0x00, 0, 0},
324 {0x5304, 0x08, 0, 0}, {0x5305, 0x30, 0, 0}, {0x5306, 0x08, 0, 0},
325 {0x5307, 0x16, 0, 0}, {0x5309, 0x08, 0, 0}, {0x530a, 0x30, 0, 0},
326 {0x530b, 0x04, 0, 0}, {0x530c, 0x06, 0, 0}, {0x5480, 0x01, 0, 0},
327 {0x5481, 0x08, 0, 0}, {0x5482, 0x14, 0, 0}, {0x5483, 0x28, 0, 0},
328 {0x5484, 0x51, 0, 0}, {0x5485, 0x65, 0, 0}, {0x5486, 0x71, 0, 0},
329 {0x5487, 0x7d, 0, 0}, {0x5488, 0x87, 0, 0}, {0x5489, 0x91, 0, 0},
330 {0x548a, 0x9a, 0, 0}, {0x548b, 0xaa, 0, 0}, {0x548c, 0xb8, 0, 0},
331 {0x548d, 0xcd, 0, 0}, {0x548e, 0xdd, 0, 0}, {0x548f, 0xea, 0, 0},
332 {0x5490, 0x1d, 0, 0}, {0x5580, 0x02, 0, 0}, {0x5583, 0x40, 0, 0},
333 {0x5584, 0x10, 0, 0}, {0x5589, 0x10, 0, 0}, {0x558a, 0x00, 0, 0},
334 {0x558b, 0xf8, 0, 0}, {0x5800, 0x23, 0, 0}, {0x5801, 0x14, 0, 0},
335 {0x5802, 0x0f, 0, 0}, {0x5803, 0x0f, 0, 0}, {0x5804, 0x12, 0, 0},
336 {0x5805, 0x26, 0, 0}, {0x5806, 0x0c, 0, 0}, {0x5807, 0x08, 0, 0},
337 {0x5808, 0x05, 0, 0}, {0x5809, 0x05, 0, 0}, {0x580a, 0x08, 0, 0},
338 {0x580b, 0x0d, 0, 0}, {0x580c, 0x08, 0, 0}, {0x580d, 0x03, 0, 0},
339 {0x580e, 0x00, 0, 0}, {0x580f, 0x00, 0, 0}, {0x5810, 0x03, 0, 0},
340 {0x5811, 0x09, 0, 0}, {0x5812, 0x07, 0, 0}, {0x5813, 0x03, 0, 0},
341 {0x5814, 0x00, 0, 0}, {0x5815, 0x01, 0, 0}, {0x5816, 0x03, 0, 0},
342 {0x5817, 0x08, 0, 0}, {0x5818, 0x0d, 0, 0}, {0x5819, 0x08, 0, 0},
343 {0x581a, 0x05, 0, 0}, {0x581b, 0x06, 0, 0}, {0x581c, 0x08, 0, 0},
344 {0x581d, 0x0e, 0, 0}, {0x581e, 0x29, 0, 0}, {0x581f, 0x17, 0, 0},
345 {0x5820, 0x11, 0, 0}, {0x5821, 0x11, 0, 0}, {0x5822, 0x15, 0, 0},
346 {0x5823, 0x28, 0, 0}, {0x5824, 0x46, 0, 0}, {0x5825, 0x26, 0, 0},
347 {0x5826, 0x08, 0, 0}, {0x5827, 0x26, 0, 0}, {0x5828, 0x64, 0, 0},
348 {0x5829, 0x26, 0, 0}, {0x582a, 0x24, 0, 0}, {0x582b, 0x22, 0, 0},
349 {0x582c, 0x24, 0, 0}, {0x582d, 0x24, 0, 0}, {0x582e, 0x06, 0, 0},
350 {0x582f, 0x22, 0, 0}, {0x5830, 0x40, 0, 0}, {0x5831, 0x42, 0, 0},
351 {0x5832, 0x24, 0, 0}, {0x5833, 0x26, 0, 0}, {0x5834, 0x24, 0, 0},
352 {0x5835, 0x22, 0, 0}, {0x5836, 0x22, 0, 0}, {0x5837, 0x26, 0, 0},
353 {0x5838, 0x44, 0, 0}, {0x5839, 0x24, 0, 0}, {0x583a, 0x26, 0, 0},
354 {0x583b, 0x28, 0, 0}, {0x583c, 0x42, 0, 0}, {0x583d, 0xce, 0, 0},
355 {0x5025, 0x00, 0, 0}, {0x3a0f, 0x30, 0, 0}, {0x3a10, 0x28, 0, 0},
356 {0x3a1b, 0x30, 0, 0}, {0x3a1e, 0x26, 0, 0}, {0x3a11, 0x60, 0, 0},
357 {0x3a1f, 0x14, 0, 0}, {0x3008, 0x02, 0, 0}, {0x3c00, 0x04, 0, 300},
358};
359
360static const struct reg_value ov5640_setting_VGA_640_480[] = {
361 {0x3c07, 0x08, 0, 0},
362 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
363 {0x3814, 0x31, 0, 0},
364 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
365 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
366 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
367 {0x3810, 0x00, 0, 0},
368 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
369 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
370 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
371 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
372 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
373 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
374 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
375 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
376 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
377};
378
379static const struct reg_value ov5640_setting_XGA_1024_768[] = {
380 {0x3c07, 0x08, 0, 0},
381 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
382 {0x3814, 0x31, 0, 0},
383 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
384 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
385 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
386 {0x3810, 0x00, 0, 0},
387 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
388 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
389 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
390 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
391 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
392 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
393 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
394 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
395 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
396};
397
398static const struct reg_value ov5640_setting_QVGA_320_240[] = {
399 {0x3c07, 0x08, 0, 0},
400 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
401 {0x3814, 0x31, 0, 0},
402 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
403 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
404 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
405 {0x3810, 0x00, 0, 0},
406 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
407 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
408 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
409 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
410 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
411 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
412 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
413 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
414 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
415};
416
417static const struct reg_value ov5640_setting_QCIF_176_144[] = {
418 {0x3c07, 0x08, 0, 0},
419 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
420 {0x3814, 0x31, 0, 0},
421 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
422 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
423 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
424 {0x3810, 0x00, 0, 0},
425 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
426 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
427 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
428 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
429 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
430 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
431 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
432 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
433 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
434};
435
436static const struct reg_value ov5640_setting_NTSC_720_480[] = {
437 {0x3c07, 0x08, 0, 0},
438 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
439 {0x3814, 0x31, 0, 0},
440 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
441 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
442 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
443 {0x3810, 0x00, 0, 0},
444 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x3c, 0, 0},
445 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
446 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
447 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
448 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
449 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
450 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
451 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
452 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
453};
454
455static const struct reg_value ov5640_setting_PAL_720_576[] = {
456 {0x3c07, 0x08, 0, 0},
457 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
458 {0x3814, 0x31, 0, 0},
459 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
460 {0x3802, 0x00, 0, 0}, {0x3803, 0x04, 0, 0}, {0x3804, 0x0a, 0, 0},
461 {0x3805, 0x3f, 0, 0}, {0x3806, 0x07, 0, 0}, {0x3807, 0x9b, 0, 0},
462 {0x3810, 0x00, 0, 0},
463 {0x3811, 0x38, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x06, 0, 0},
464 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
465 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x03, 0, 0},
466 {0x3a03, 0xd8, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0x27, 0, 0},
467 {0x3a0a, 0x00, 0, 0}, {0x3a0b, 0xf6, 0, 0}, {0x3a0e, 0x03, 0, 0},
468 {0x3a0d, 0x04, 0, 0}, {0x3a14, 0x03, 0, 0}, {0x3a15, 0xd8, 0, 0},
469 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
470 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
471 {0x3824, 0x02, 0, 0}, {0x5001, 0xa3, 0, 0},
472};
473
474static const struct reg_value ov5640_setting_720P_1280_720[] = {
475 {0x3c07, 0x07, 0, 0},
476 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
477 {0x3814, 0x31, 0, 0},
478 {0x3815, 0x31, 0, 0}, {0x3800, 0x00, 0, 0}, {0x3801, 0x00, 0, 0},
479 {0x3802, 0x00, 0, 0}, {0x3803, 0xfa, 0, 0}, {0x3804, 0x0a, 0, 0},
480 {0x3805, 0x3f, 0, 0}, {0x3806, 0x06, 0, 0}, {0x3807, 0xa9, 0, 0},
481 {0x3810, 0x00, 0, 0},
482 {0x3811, 0x10, 0, 0}, {0x3812, 0x00, 0, 0}, {0x3813, 0x04, 0, 0},
483 {0x3618, 0x00, 0, 0}, {0x3612, 0x29, 0, 0}, {0x3708, 0x64, 0, 0},
484 {0x3709, 0x52, 0, 0}, {0x370c, 0x03, 0, 0}, {0x3a02, 0x02, 0, 0},
485 {0x3a03, 0xe4, 0, 0}, {0x3a08, 0x01, 0, 0}, {0x3a09, 0xbc, 0, 0},
486 {0x3a0a, 0x01, 0, 0}, {0x3a0b, 0x72, 0, 0}, {0x3a0e, 0x01, 0, 0},
487 {0x3a0d, 0x02, 0, 0}, {0x3a14, 0x02, 0, 0}, {0x3a15, 0xe4, 0, 0},
488 {0x4001, 0x02, 0, 0}, {0x4004, 0x02, 0, 0},
489 {0x4407, 0x04, 0, 0}, {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0},
490 {0x3824, 0x04, 0, 0}, {0x5001, 0x83, 0, 0},
491};
492
493static const struct reg_value ov5640_setting_1080P_1920_1080[] = {
494 {0x3c07, 0x08, 0, 0},
495 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
496 {0x3814, 0x11, 0, 0},
497 {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},
500 {0x3810, 0x00, 0, 0},
501 {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},
508 {0x4407, 0x04, 0, 0}, {0x460b, 0x35, 0, 0}, {0x460c, 0x22, 0, 0},
509 {0x3824, 0x02, 0, 0}, {0x5001, 0x83, 0, 0},
510 {0x3c07, 0x07, 0, 0}, {0x3c08, 0x00, 0, 0},
511 {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},
514 {0x3806, 0x05, 0, 0}, {0x3807, 0xf1, 0, 0},
515 {0x3612, 0x2b, 0, 0}, {0x3708, 0x64, 0, 0},
516 {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}, {0x4407, 0x04, 0, 0},
520 {0x460b, 0x37, 0, 0}, {0x460c, 0x20, 0, 0}, {0x3824, 0x04, 0, 0},
521 {0x4005, 0x1a, 0, 0},
522};
523
524static const struct reg_value ov5640_setting_QSXGA_2592_1944[] = {
525 {0x3c07, 0x08, 0, 0},
526 {0x3c09, 0x1c, 0, 0}, {0x3c0a, 0x9c, 0, 0}, {0x3c0b, 0x40, 0, 0},
527 {0x3814, 0x11, 0, 0},
528 {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},
531 {0x3810, 0x00, 0, 0},
532 {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},
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
544static const struct ov5640_mode_info ov5640_mode_init_data = {
545 0, SUBSAMPLING, 640, 1896, 480, 984,
546 ov5640_init_setting_30fps_VGA,
547 ARRAY_SIZE(ov5640_init_setting_30fps_VGA),
548 OV5640_30_FPS,
549};
550
551static const struct ov5640_mode_info
552ov5640_mode_data[OV5640_NUM_MODES] = {
553 {OV5640_MODE_QCIF_176_144, SUBSAMPLING,
554 176, 1896, 144, 984,
555 ov5640_setting_QCIF_176_144,
556 ARRAY_SIZE(ov5640_setting_QCIF_176_144),
557 OV5640_30_FPS},
558 {OV5640_MODE_QVGA_320_240, SUBSAMPLING,
559 320, 1896, 240, 984,
560 ov5640_setting_QVGA_320_240,
561 ARRAY_SIZE(ov5640_setting_QVGA_320_240),
562 OV5640_30_FPS},
563 {OV5640_MODE_VGA_640_480, SUBSAMPLING,
564 640, 1896, 480, 1080,
565 ov5640_setting_VGA_640_480,
566 ARRAY_SIZE(ov5640_setting_VGA_640_480),
567 OV5640_60_FPS},
568 {OV5640_MODE_NTSC_720_480, SUBSAMPLING,
569 720, 1896, 480, 984,
570 ov5640_setting_NTSC_720_480,
571 ARRAY_SIZE(ov5640_setting_NTSC_720_480),
572 OV5640_30_FPS},
573 {OV5640_MODE_PAL_720_576, SUBSAMPLING,
574 720, 1896, 576, 984,
575 ov5640_setting_PAL_720_576,
576 ARRAY_SIZE(ov5640_setting_PAL_720_576),
577 OV5640_30_FPS},
578 {OV5640_MODE_XGA_1024_768, SUBSAMPLING,
579 1024, 1896, 768, 1080,
580 ov5640_setting_XGA_1024_768,
581 ARRAY_SIZE(ov5640_setting_XGA_1024_768),
582 OV5640_30_FPS},
583 {OV5640_MODE_720P_1280_720, SUBSAMPLING,
584 1280, 1892, 720, 740,
585 ov5640_setting_720P_1280_720,
586 ARRAY_SIZE(ov5640_setting_720P_1280_720),
587 OV5640_30_FPS},
588 {OV5640_MODE_1080P_1920_1080, SCALING,
589 1920, 2500, 1080, 1120,
590 ov5640_setting_1080P_1920_1080,
591 ARRAY_SIZE(ov5640_setting_1080P_1920_1080),
592 OV5640_30_FPS},
593 {OV5640_MODE_QSXGA_2592_1944, SCALING,
594 2592, 2844, 1944, 1968,
595 ov5640_setting_QSXGA_2592_1944,
596 ARRAY_SIZE(ov5640_setting_QSXGA_2592_1944),
597 OV5640_15_FPS},
598};
599
600static int ov5640_init_slave_id(struct ov5640_dev *sensor)
601{
602 struct i2c_client *client = sensor->i2c_client;
603 struct i2c_msg msg;
604 u8 buf[3];
605 int ret;
606
607 if (client->addr == OV5640_DEFAULT_SLAVE_ID)
608 return 0;
609
610 buf[0] = OV5640_REG_SLAVE_ID >> 8;
611 buf[1] = OV5640_REG_SLAVE_ID & 0xff;
612 buf[2] = client->addr << 1;
613
614 msg.addr = OV5640_DEFAULT_SLAVE_ID;
615 msg.flags = 0;
616 msg.buf = buf;
617 msg.len = sizeof(buf);
618
619 ret = i2c_transfer(client->adapter, &msg, 1);
620 if (ret < 0) {
621 dev_err(&client->dev, "%s: failed with %d\n", __func__, ret);
622 return ret;
623 }
624
625 return 0;
626}
627
628static int ov5640_write_reg(struct ov5640_dev *sensor, u16 reg, u8 val)
629{
630 struct i2c_client *client = sensor->i2c_client;
631 struct i2c_msg msg;
632 u8 buf[3];
633 int ret;
634
635 buf[0] = reg >> 8;
636 buf[1] = reg & 0xff;
637 buf[2] = val;
638
639 msg.addr = client->addr;
640 msg.flags = client->flags;
641 msg.buf = buf;
642 msg.len = sizeof(buf);
643
644 ret = i2c_transfer(client->adapter, &msg, 1);
645 if (ret < 0) {
646 dev_err(&client->dev, "%s: error: reg=%x, val=%x\n",
647 __func__, reg, val);
648 return ret;
649 }
650
651 return 0;
652}
653
654static int ov5640_read_reg(struct ov5640_dev *sensor, u16 reg, u8 *val)
655{
656 struct i2c_client *client = sensor->i2c_client;
657 struct i2c_msg msg[2];
658 u8 buf[2];
659 int ret;
660
661 buf[0] = reg >> 8;
662 buf[1] = reg & 0xff;
663
664 msg[0].addr = client->addr;
665 msg[0].flags = client->flags;
666 msg[0].buf = buf;
667 msg[0].len = sizeof(buf);
668
669 msg[1].addr = client->addr;
670 msg[1].flags = client->flags | I2C_M_RD;
671 msg[1].buf = buf;
672 msg[1].len = 1;
673
674 ret = i2c_transfer(client->adapter, msg, 2);
675 if (ret < 0) {
676 dev_err(&client->dev, "%s: error: reg=%x\n",
677 __func__, reg);
678 return ret;
679 }
680
681 *val = buf[0];
682 return 0;
683}
684
685static int ov5640_read_reg16(struct ov5640_dev *sensor, u16 reg, u16 *val)
686{
687 u8 hi, lo;
688 int ret;
689
690 ret = ov5640_read_reg(sensor, reg, &hi);
691 if (ret)
692 return ret;
693 ret = ov5640_read_reg(sensor, reg + 1, &lo);
694 if (ret)
695 return ret;
696
697 *val = ((u16)hi << 8) | (u16)lo;
698 return 0;
699}
700
701static int ov5640_write_reg16(struct ov5640_dev *sensor, u16 reg, u16 val)
702{
703 int ret;
704
705 ret = ov5640_write_reg(sensor, reg, val >> 8);
706 if (ret)
707 return ret;
708
709 return ov5640_write_reg(sensor, reg + 1, val & 0xff);
710}
711
712static int ov5640_mod_reg(struct ov5640_dev *sensor, u16 reg,
713 u8 mask, u8 val)
714{
715 u8 readval;
716 int ret;
717
718 ret = ov5640_read_reg(sensor, reg, &readval);
719 if (ret)
720 return ret;
721
722 readval &= ~mask;
723 val &= mask;
724 val |= readval;
725
726 return ov5640_write_reg(sensor, reg, val);
727}
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792#define OV5640_PLL_PREDIV 3
793
794#define OV5640_PLL_MULT_MIN 4
795#define OV5640_PLL_MULT_MAX 252
796
797
798
799
800
801#define OV5640_SYSDIV_MIN 1
802#define OV5640_SYSDIV_MAX 16
803
804
805
806
807
808#define OV5640_MIPI_DIV_PCLK 2
809#define OV5640_MIPI_DIV_SCLK 1
810
811
812
813
814
815#define OV5640_PLL_ROOT_DIV 2
816#define OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 BIT(4)
817
818
819
820
821#define OV5640_BIT_DIV 2
822#define OV5640_PLL_CTRL0_MIPI_MODE_8BIT 0x08
823
824
825
826
827
828#define OV5640_SCLK_ROOT_DIV 2
829
830
831
832
833
834#define OV5640_SCLK2X_ROOT_DIV (OV5640_SCLK_ROOT_DIV / 2)
835
836
837
838
839
840#define OV5640_PCLK_ROOT_DIV 1
841#define OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS 0x00
842
843static unsigned long ov5640_compute_sys_clk(struct ov5640_dev *sensor,
844 u8 pll_prediv, u8 pll_mult,
845 u8 sysdiv)
846{
847 unsigned long sysclk = sensor->xclk_freq / pll_prediv * pll_mult;
848
849
850 if (sysclk / 1000000 > 1000)
851 return 0;
852
853 return sysclk / sysdiv;
854}
855
856static unsigned long ov5640_calc_sys_clk(struct ov5640_dev *sensor,
857 unsigned long rate,
858 u8 *pll_prediv, u8 *pll_mult,
859 u8 *sysdiv)
860{
861 unsigned long best = ~0;
862 u8 best_sysdiv = 1, best_mult = 1;
863 u8 _sysdiv, _pll_mult;
864
865 for (_sysdiv = OV5640_SYSDIV_MIN;
866 _sysdiv <= OV5640_SYSDIV_MAX;
867 _sysdiv++) {
868 for (_pll_mult = OV5640_PLL_MULT_MIN;
869 _pll_mult <= OV5640_PLL_MULT_MAX;
870 _pll_mult++) {
871 unsigned long _rate;
872
873
874
875
876
877 if (_pll_mult > 127 && (_pll_mult % 2))
878 continue;
879
880 _rate = ov5640_compute_sys_clk(sensor,
881 OV5640_PLL_PREDIV,
882 _pll_mult, _sysdiv);
883
884
885
886
887
888 if (!_rate)
889 break;
890
891
892
893
894
895 if (_rate < rate)
896 continue;
897
898 if (abs(rate - _rate) < abs(rate - best)) {
899 best = _rate;
900 best_sysdiv = _sysdiv;
901 best_mult = _pll_mult;
902 }
903
904 if (_rate == rate)
905 goto out;
906 }
907 }
908
909out:
910 *sysdiv = best_sysdiv;
911 *pll_prediv = OV5640_PLL_PREDIV;
912 *pll_mult = best_mult;
913
914 return best;
915}
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958static int ov5640_set_mipi_pclk(struct ov5640_dev *sensor,
959 unsigned long rate)
960{
961 const struct ov5640_mode_info *mode = sensor->current_mode;
962 u8 prediv, mult, sysdiv;
963 u8 mipi_div;
964 int ret;
965
966
967
968
969
970
971 if (mode->dn_mode == SCALING ||
972 (mode->id == OV5640_MODE_720P_1280_720))
973 mipi_div = OV5640_MIPI_DIV_SCLK;
974 else
975 mipi_div = OV5640_MIPI_DIV_PCLK;
976
977 ov5640_calc_sys_clk(sensor, rate, &prediv, &mult, &sysdiv);
978
979 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
980 0x0f, OV5640_PLL_CTRL0_MIPI_MODE_8BIT);
981
982 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
983 0xff, sysdiv << 4 | mipi_div);
984 if (ret)
985 return ret;
986
987 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2, 0xff, mult);
988 if (ret)
989 return ret;
990
991 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
992 0x1f, OV5640_PLL_CTRL3_PLL_ROOT_DIV_2 | prediv);
993 if (ret)
994 return ret;
995
996 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER,
997 0x30, OV5640_PLL_SYS_ROOT_DIVIDER_BYPASS);
998}
999
1000static unsigned long ov5640_calc_pclk(struct ov5640_dev *sensor,
1001 unsigned long rate,
1002 u8 *pll_prediv, u8 *pll_mult, u8 *sysdiv,
1003 u8 *pll_rdiv, u8 *bit_div, u8 *pclk_div)
1004{
1005 unsigned long _rate = rate * OV5640_PLL_ROOT_DIV * OV5640_BIT_DIV *
1006 OV5640_PCLK_ROOT_DIV;
1007
1008 _rate = ov5640_calc_sys_clk(sensor, _rate, pll_prediv, pll_mult,
1009 sysdiv);
1010 *pll_rdiv = OV5640_PLL_ROOT_DIV;
1011 *bit_div = OV5640_BIT_DIV;
1012 *pclk_div = OV5640_PCLK_ROOT_DIV;
1013
1014 return _rate / *pll_rdiv / *bit_div / *pclk_div;
1015}
1016
1017static int ov5640_set_dvp_pclk(struct ov5640_dev *sensor, unsigned long rate)
1018{
1019 u8 prediv, mult, sysdiv, pll_rdiv, bit_div, pclk_div;
1020 int ret;
1021
1022 ov5640_calc_pclk(sensor, rate, &prediv, &mult, &sysdiv, &pll_rdiv,
1023 &bit_div, &pclk_div);
1024
1025 if (bit_div == 2)
1026 bit_div = 8;
1027
1028 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL0,
1029 0x0f, bit_div);
1030 if (ret)
1031 return ret;
1032
1033
1034
1035
1036
1037 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL1,
1038 0xff, sysdiv << 4);
1039 if (ret)
1040 return ret;
1041
1042 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL2,
1043 0xff, mult);
1044 if (ret)
1045 return ret;
1046
1047 ret = ov5640_mod_reg(sensor, OV5640_REG_SC_PLL_CTRL3,
1048 0x1f, prediv | ((pll_rdiv - 1) << 4));
1049 if (ret)
1050 return ret;
1051
1052 return ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x30,
1053 (ilog2(pclk_div) << 4));
1054}
1055
1056
1057static int ov5640_set_jpeg_timings(struct ov5640_dev *sensor,
1058 const struct ov5640_mode_info *mode)
1059{
1060 int ret;
1061
1062
1063
1064
1065
1066
1067
1068
1069 ret = ov5640_mod_reg(sensor, OV5640_REG_JPG_MODE_SELECT, 0x7, 0x3);
1070 if (ret < 0)
1071 return ret;
1072
1073 ret = ov5640_write_reg16(sensor, OV5640_REG_VFIFO_HSIZE, mode->hact);
1074 if (ret < 0)
1075 return ret;
1076
1077 return ov5640_write_reg16(sensor, OV5640_REG_VFIFO_VSIZE, mode->vact);
1078}
1079
1080
1081static int ov5640_set_timings(struct ov5640_dev *sensor,
1082 const struct ov5640_mode_info *mode)
1083{
1084 int ret;
1085
1086 if (sensor->fmt.code == MEDIA_BUS_FMT_JPEG_1X8) {
1087 ret = ov5640_set_jpeg_timings(sensor, mode);
1088 if (ret < 0)
1089 return ret;
1090 }
1091
1092 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPHO, mode->hact);
1093 if (ret < 0)
1094 return ret;
1095
1096 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_DVPVO, mode->vact);
1097 if (ret < 0)
1098 return ret;
1099
1100 ret = ov5640_write_reg16(sensor, OV5640_REG_TIMING_HTS, mode->htot);
1101 if (ret < 0)
1102 return ret;
1103
1104 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, mode->vtot);
1105}
1106
1107static int ov5640_load_regs(struct ov5640_dev *sensor,
1108 const struct ov5640_mode_info *mode)
1109{
1110 const struct reg_value *regs = mode->reg_data;
1111 unsigned int i;
1112 u32 delay_ms;
1113 u16 reg_addr;
1114 u8 mask, val;
1115 int ret = 0;
1116
1117 for (i = 0; i < mode->reg_data_size; ++i, ++regs) {
1118 delay_ms = regs->delay_ms;
1119 reg_addr = regs->reg_addr;
1120 val = regs->val;
1121 mask = regs->mask;
1122
1123 if (mask)
1124 ret = ov5640_mod_reg(sensor, reg_addr, mask, val);
1125 else
1126 ret = ov5640_write_reg(sensor, reg_addr, val);
1127 if (ret)
1128 break;
1129
1130 if (delay_ms)
1131 usleep_range(1000 * delay_ms, 1000 * delay_ms + 100);
1132 }
1133
1134 return ov5640_set_timings(sensor, mode);
1135}
1136
1137static int ov5640_set_autoexposure(struct ov5640_dev *sensor, bool on)
1138{
1139 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1140 BIT(0), on ? 0 : BIT(0));
1141}
1142
1143
1144static int ov5640_get_exposure(struct ov5640_dev *sensor)
1145{
1146 int exp, ret;
1147 u8 temp;
1148
1149 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_HI, &temp);
1150 if (ret)
1151 return ret;
1152 exp = ((int)temp & 0x0f) << 16;
1153 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_MED, &temp);
1154 if (ret)
1155 return ret;
1156 exp |= ((int)temp << 8);
1157 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_PK_EXPOSURE_LO, &temp);
1158 if (ret)
1159 return ret;
1160 exp |= (int)temp;
1161
1162 return exp >> 4;
1163}
1164
1165
1166static int ov5640_set_exposure(struct ov5640_dev *sensor, u32 exposure)
1167{
1168 int ret;
1169
1170 exposure <<= 4;
1171
1172 ret = ov5640_write_reg(sensor,
1173 OV5640_REG_AEC_PK_EXPOSURE_LO,
1174 exposure & 0xff);
1175 if (ret)
1176 return ret;
1177 ret = ov5640_write_reg(sensor,
1178 OV5640_REG_AEC_PK_EXPOSURE_MED,
1179 (exposure >> 8) & 0xff);
1180 if (ret)
1181 return ret;
1182 return ov5640_write_reg(sensor,
1183 OV5640_REG_AEC_PK_EXPOSURE_HI,
1184 (exposure >> 16) & 0x0f);
1185}
1186
1187static int ov5640_get_gain(struct ov5640_dev *sensor)
1188{
1189 u16 gain;
1190 int ret;
1191
1192 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN, &gain);
1193 if (ret)
1194 return ret;
1195
1196 return gain & 0x3ff;
1197}
1198
1199static int ov5640_set_gain(struct ov5640_dev *sensor, int gain)
1200{
1201 return ov5640_write_reg16(sensor, OV5640_REG_AEC_PK_REAL_GAIN,
1202 (u16)gain & 0x3ff);
1203}
1204
1205static int ov5640_set_autogain(struct ov5640_dev *sensor, bool on)
1206{
1207 return ov5640_mod_reg(sensor, OV5640_REG_AEC_PK_MANUAL,
1208 BIT(1), on ? 0 : BIT(1));
1209}
1210
1211static int ov5640_set_stream_dvp(struct ov5640_dev *sensor, bool on)
1212{
1213 int ret;
1214 unsigned int flags = sensor->ep.bus.parallel.flags;
1215 u8 pclk_pol = 0;
1216 u8 hsync_pol = 0;
1217 u8 vsync_pol = 0;
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237 if (on) {
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248 if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
1249 pclk_pol = 1;
1250 if (flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH)
1251 hsync_pol = 1;
1252 if (flags & V4L2_MBUS_VSYNC_ACTIVE_LOW)
1253 vsync_pol = 1;
1254
1255 ret = ov5640_write_reg(sensor,
1256 OV5640_REG_POLARITY_CTRL00,
1257 (pclk_pol << 5) |
1258 (hsync_pol << 1) |
1259 vsync_pol);
1260
1261 if (ret)
1262 return ret;
1263 }
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273 ret = ov5640_write_reg(sensor,
1274 OV5640_REG_IO_MIPI_CTRL00, on ? 0x18 : 0);
1275 if (ret)
1276 return ret;
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288 ret = ov5640_write_reg(sensor,
1289 OV5640_REG_PAD_OUTPUT_ENABLE01,
1290 on ? 0x7f : 0);
1291 if (ret)
1292 return ret;
1293
1294
1295
1296
1297
1298
1299
1300 return ov5640_write_reg(sensor,
1301 OV5640_REG_PAD_OUTPUT_ENABLE02,
1302 on ? 0xfc : 0);
1303}
1304
1305static int ov5640_set_stream_mipi(struct ov5640_dev *sensor, bool on)
1306{
1307 int ret;
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326 ret = ov5640_write_reg(sensor, OV5640_REG_IO_MIPI_CTRL00,
1327 on ? 0x45 : 0x40);
1328 if (ret)
1329 return ret;
1330
1331 return ov5640_write_reg(sensor, OV5640_REG_FRAME_CTRL01,
1332 on ? 0x00 : 0x0f);
1333}
1334
1335static int ov5640_get_sysclk(struct ov5640_dev *sensor)
1336{
1337
1338 u32 xvclk = sensor->xclk_freq / 10000;
1339 u32 multiplier, prediv, VCO, sysdiv, pll_rdiv;
1340 u32 sclk_rdiv_map[] = {1, 2, 4, 8};
1341 u32 bit_div2x = 1, sclk_rdiv, sysclk;
1342 u8 temp1, temp2;
1343 int ret;
1344
1345 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL0, &temp1);
1346 if (ret)
1347 return ret;
1348 temp2 = temp1 & 0x0f;
1349 if (temp2 == 8 || temp2 == 10)
1350 bit_div2x = temp2 / 2;
1351
1352 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL1, &temp1);
1353 if (ret)
1354 return ret;
1355 sysdiv = temp1 >> 4;
1356 if (sysdiv == 0)
1357 sysdiv = 16;
1358
1359 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL2, &temp1);
1360 if (ret)
1361 return ret;
1362 multiplier = temp1;
1363
1364 ret = ov5640_read_reg(sensor, OV5640_REG_SC_PLL_CTRL3, &temp1);
1365 if (ret)
1366 return ret;
1367 prediv = temp1 & 0x0f;
1368 pll_rdiv = ((temp1 >> 4) & 0x01) + 1;
1369
1370 ret = ov5640_read_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, &temp1);
1371 if (ret)
1372 return ret;
1373 temp2 = temp1 & 0x03;
1374 sclk_rdiv = sclk_rdiv_map[temp2];
1375
1376 if (!prediv || !sysdiv || !pll_rdiv || !bit_div2x)
1377 return -EINVAL;
1378
1379 VCO = xvclk * multiplier / prediv;
1380
1381 sysclk = VCO / sysdiv / pll_rdiv * 2 / bit_div2x / sclk_rdiv;
1382
1383 return sysclk;
1384}
1385
1386static int ov5640_set_night_mode(struct ov5640_dev *sensor)
1387{
1388
1389 u8 mode;
1390 int ret;
1391
1392 ret = ov5640_read_reg(sensor, OV5640_REG_AEC_CTRL00, &mode);
1393 if (ret)
1394 return ret;
1395 mode &= 0xfb;
1396 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL00, mode);
1397}
1398
1399static int ov5640_get_hts(struct ov5640_dev *sensor)
1400{
1401
1402 u16 hts;
1403 int ret;
1404
1405 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_HTS, &hts);
1406 if (ret)
1407 return ret;
1408 return hts;
1409}
1410
1411static int ov5640_get_vts(struct ov5640_dev *sensor)
1412{
1413 u16 vts;
1414 int ret;
1415
1416 ret = ov5640_read_reg16(sensor, OV5640_REG_TIMING_VTS, &vts);
1417 if (ret)
1418 return ret;
1419 return vts;
1420}
1421
1422static int ov5640_set_vts(struct ov5640_dev *sensor, int vts)
1423{
1424 return ov5640_write_reg16(sensor, OV5640_REG_TIMING_VTS, vts);
1425}
1426
1427static int ov5640_get_light_freq(struct ov5640_dev *sensor)
1428{
1429
1430 int ret, light_freq = 0;
1431 u8 temp, temp1;
1432
1433 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL01, &temp);
1434 if (ret)
1435 return ret;
1436
1437 if (temp & 0x80) {
1438
1439 ret = ov5640_read_reg(sensor, OV5640_REG_HZ5060_CTRL00,
1440 &temp1);
1441 if (ret)
1442 return ret;
1443 if (temp1 & 0x04) {
1444
1445 light_freq = 50;
1446 } else {
1447
1448 light_freq = 60;
1449 }
1450 } else {
1451
1452 ret = ov5640_read_reg(sensor, OV5640_REG_SIGMADELTA_CTRL0C,
1453 &temp1);
1454 if (ret)
1455 return ret;
1456
1457 if (temp1 & 0x01) {
1458
1459 light_freq = 50;
1460 } else {
1461
1462 }
1463 }
1464
1465 return light_freq;
1466}
1467
1468static int ov5640_set_bandingfilter(struct ov5640_dev *sensor)
1469{
1470 u32 band_step60, max_band60, band_step50, max_band50, prev_vts;
1471 int ret;
1472
1473
1474 ret = ov5640_get_sysclk(sensor);
1475 if (ret < 0)
1476 return ret;
1477 if (ret == 0)
1478 return -EINVAL;
1479 sensor->prev_sysclk = ret;
1480
1481 ret = ov5640_get_hts(sensor);
1482 if (ret < 0)
1483 return ret;
1484 if (ret == 0)
1485 return -EINVAL;
1486 sensor->prev_hts = ret;
1487
1488
1489 ret = ov5640_get_vts(sensor);
1490 if (ret < 0)
1491 return ret;
1492 prev_vts = ret;
1493
1494
1495
1496 band_step60 = sensor->prev_sysclk * 100 / sensor->prev_hts * 100 / 120;
1497 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B60_STEP, band_step60);
1498 if (ret)
1499 return ret;
1500 if (!band_step60)
1501 return -EINVAL;
1502 max_band60 = (int)((prev_vts - 4) / band_step60);
1503 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0D, max_band60);
1504 if (ret)
1505 return ret;
1506
1507
1508 band_step50 = sensor->prev_sysclk * 100 / sensor->prev_hts;
1509 ret = ov5640_write_reg16(sensor, OV5640_REG_AEC_B50_STEP, band_step50);
1510 if (ret)
1511 return ret;
1512 if (!band_step50)
1513 return -EINVAL;
1514 max_band50 = (int)((prev_vts - 4) / band_step50);
1515 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0E, max_band50);
1516}
1517
1518static int ov5640_set_ae_target(struct ov5640_dev *sensor, int target)
1519{
1520
1521 u32 fast_high, fast_low;
1522 int ret;
1523
1524 sensor->ae_low = target * 23 / 25;
1525 sensor->ae_high = target * 27 / 25;
1526
1527 fast_high = sensor->ae_high << 1;
1528 if (fast_high > 255)
1529 fast_high = 255;
1530
1531 fast_low = sensor->ae_low >> 1;
1532
1533 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL0F, sensor->ae_high);
1534 if (ret)
1535 return ret;
1536 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL10, sensor->ae_low);
1537 if (ret)
1538 return ret;
1539 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1B, sensor->ae_high);
1540 if (ret)
1541 return ret;
1542 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1E, sensor->ae_low);
1543 if (ret)
1544 return ret;
1545 ret = ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL11, fast_high);
1546 if (ret)
1547 return ret;
1548 return ov5640_write_reg(sensor, OV5640_REG_AEC_CTRL1F, fast_low);
1549}
1550
1551static int ov5640_get_binning(struct ov5640_dev *sensor)
1552{
1553 u8 temp;
1554 int ret;
1555
1556 ret = ov5640_read_reg(sensor, OV5640_REG_TIMING_TC_REG21, &temp);
1557 if (ret)
1558 return ret;
1559
1560 return temp & BIT(0);
1561}
1562
1563static int ov5640_set_binning(struct ov5640_dev *sensor, bool enable)
1564{
1565 int ret;
1566
1567
1568
1569
1570
1571 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
1572 BIT(0), enable ? BIT(0) : 0);
1573 if (ret)
1574 return ret;
1575
1576
1577
1578
1579
1580 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
1581 BIT(0), enable ? BIT(0) : 0);
1582}
1583
1584static int ov5640_set_virtual_channel(struct ov5640_dev *sensor)
1585{
1586 struct i2c_client *client = sensor->i2c_client;
1587 u8 temp, channel = virtual_channel;
1588 int ret;
1589
1590 if (channel > 3) {
1591 dev_err(&client->dev,
1592 "%s: wrong virtual_channel parameter, expected (0..3), got %d\n",
1593 __func__, channel);
1594 return -EINVAL;
1595 }
1596
1597 ret = ov5640_read_reg(sensor, OV5640_REG_DEBUG_MODE, &temp);
1598 if (ret)
1599 return ret;
1600 temp &= ~(3 << 6);
1601 temp |= (channel << 6);
1602 return ov5640_write_reg(sensor, OV5640_REG_DEBUG_MODE, temp);
1603}
1604
1605static const struct ov5640_mode_info *
1606ov5640_find_mode(struct ov5640_dev *sensor, enum ov5640_frame_rate fr,
1607 int width, int height, bool nearest)
1608{
1609 const struct ov5640_mode_info *mode;
1610
1611 mode = v4l2_find_nearest_size(ov5640_mode_data,
1612 ARRAY_SIZE(ov5640_mode_data),
1613 hact, vact,
1614 width, height);
1615
1616 if (!mode ||
1617 (!nearest && (mode->hact != width || mode->vact != height)))
1618 return NULL;
1619
1620
1621 if (ov5640_framerates[fr] > ov5640_framerates[mode->max_fps])
1622 return NULL;
1623
1624 return mode;
1625}
1626
1627static u64 ov5640_calc_pixel_rate(struct ov5640_dev *sensor)
1628{
1629 u64 rate;
1630
1631 rate = sensor->current_mode->vtot * sensor->current_mode->htot;
1632 rate *= ov5640_framerates[sensor->current_fr];
1633
1634 return rate;
1635}
1636
1637
1638
1639
1640
1641static int ov5640_set_mode_exposure_calc(struct ov5640_dev *sensor,
1642 const struct ov5640_mode_info *mode)
1643{
1644 u32 prev_shutter, prev_gain16;
1645 u32 cap_shutter, cap_gain16;
1646 u32 cap_sysclk, cap_hts, cap_vts;
1647 u32 light_freq, cap_bandfilt, cap_maxband;
1648 u32 cap_gain16_shutter;
1649 u8 average;
1650 int ret;
1651
1652 if (!mode->reg_data)
1653 return -EINVAL;
1654
1655
1656 ret = ov5640_get_exposure(sensor);
1657 if (ret < 0)
1658 return ret;
1659 prev_shutter = ret;
1660 ret = ov5640_get_binning(sensor);
1661 if (ret < 0)
1662 return ret;
1663 if (ret && mode->id != OV5640_MODE_720P_1280_720 &&
1664 mode->id != OV5640_MODE_1080P_1920_1080)
1665 prev_shutter *= 2;
1666
1667
1668 ret = ov5640_get_gain(sensor);
1669 if (ret < 0)
1670 return ret;
1671 prev_gain16 = ret;
1672
1673
1674 ret = ov5640_read_reg(sensor, OV5640_REG_AVG_READOUT, &average);
1675 if (ret)
1676 return ret;
1677
1678
1679 ret = ov5640_set_night_mode(sensor);
1680 if (ret < 0)
1681 return ret;
1682
1683
1684 ret = ov5640_load_regs(sensor, mode);
1685 if (ret < 0)
1686 return ret;
1687
1688
1689 ret = ov5640_get_vts(sensor);
1690 if (ret < 0)
1691 return ret;
1692 cap_vts = ret;
1693 ret = ov5640_get_hts(sensor);
1694 if (ret < 0)
1695 return ret;
1696 if (ret == 0)
1697 return -EINVAL;
1698 cap_hts = ret;
1699
1700 ret = ov5640_get_sysclk(sensor);
1701 if (ret < 0)
1702 return ret;
1703 if (ret == 0)
1704 return -EINVAL;
1705 cap_sysclk = ret;
1706
1707
1708 ret = ov5640_get_light_freq(sensor);
1709 if (ret < 0)
1710 return ret;
1711 light_freq = ret;
1712
1713 if (light_freq == 60) {
1714
1715 cap_bandfilt = cap_sysclk * 100 / cap_hts * 100 / 120;
1716 } else {
1717
1718 cap_bandfilt = cap_sysclk * 100 / cap_hts;
1719 }
1720
1721 if (!sensor->prev_sysclk) {
1722 ret = ov5640_get_sysclk(sensor);
1723 if (ret < 0)
1724 return ret;
1725 if (ret == 0)
1726 return -EINVAL;
1727 sensor->prev_sysclk = ret;
1728 }
1729
1730 if (!cap_bandfilt)
1731 return -EINVAL;
1732
1733 cap_maxband = (int)((cap_vts - 4) / cap_bandfilt);
1734
1735
1736 if (average > sensor->ae_low && average < sensor->ae_high) {
1737
1738 cap_gain16_shutter =
1739 prev_gain16 * prev_shutter *
1740 cap_sysclk / sensor->prev_sysclk *
1741 sensor->prev_hts / cap_hts *
1742 sensor->ae_target / average;
1743 } else {
1744 cap_gain16_shutter =
1745 prev_gain16 * prev_shutter *
1746 cap_sysclk / sensor->prev_sysclk *
1747 sensor->prev_hts / cap_hts;
1748 }
1749
1750
1751 if (cap_gain16_shutter < (cap_bandfilt * 16)) {
1752
1753 cap_shutter = cap_gain16_shutter / 16;
1754 if (cap_shutter < 1)
1755 cap_shutter = 1;
1756
1757 cap_gain16 = cap_gain16_shutter / cap_shutter;
1758 if (cap_gain16 < 16)
1759 cap_gain16 = 16;
1760 } else {
1761 if (cap_gain16_shutter > (cap_bandfilt * cap_maxband * 16)) {
1762
1763 cap_shutter = cap_bandfilt * cap_maxband;
1764 if (!cap_shutter)
1765 return -EINVAL;
1766
1767 cap_gain16 = cap_gain16_shutter / cap_shutter;
1768 } else {
1769
1770 cap_shutter =
1771 ((int)(cap_gain16_shutter / 16 / cap_bandfilt))
1772 * cap_bandfilt;
1773 if (!cap_shutter)
1774 return -EINVAL;
1775
1776 cap_gain16 = cap_gain16_shutter / cap_shutter;
1777 }
1778 }
1779
1780
1781 ret = ov5640_set_gain(sensor, cap_gain16);
1782 if (ret)
1783 return ret;
1784
1785
1786 if (cap_shutter > (cap_vts - 4)) {
1787 cap_vts = cap_shutter + 4;
1788 ret = ov5640_set_vts(sensor, cap_vts);
1789 if (ret < 0)
1790 return ret;
1791 }
1792
1793
1794 return ov5640_set_exposure(sensor, cap_shutter);
1795}
1796
1797
1798
1799
1800
1801static int ov5640_set_mode_direct(struct ov5640_dev *sensor,
1802 const struct ov5640_mode_info *mode)
1803{
1804 if (!mode->reg_data)
1805 return -EINVAL;
1806
1807
1808 return ov5640_load_regs(sensor, mode);
1809}
1810
1811static int ov5640_set_mode(struct ov5640_dev *sensor)
1812{
1813 const struct ov5640_mode_info *mode = sensor->current_mode;
1814 const struct ov5640_mode_info *orig_mode = sensor->last_mode;
1815 enum ov5640_downsize_mode dn_mode, orig_dn_mode;
1816 bool auto_gain = sensor->ctrls.auto_gain->val == 1;
1817 bool auto_exp = sensor->ctrls.auto_exp->val == V4L2_EXPOSURE_AUTO;
1818 unsigned long rate;
1819 int ret;
1820
1821 dn_mode = mode->dn_mode;
1822 orig_dn_mode = orig_mode->dn_mode;
1823
1824
1825 if (auto_gain) {
1826 ret = ov5640_set_autogain(sensor, false);
1827 if (ret)
1828 return ret;
1829 }
1830
1831 if (auto_exp) {
1832 ret = ov5640_set_autoexposure(sensor, false);
1833 if (ret)
1834 goto restore_auto_gain;
1835 }
1836
1837
1838
1839
1840
1841 rate = ov5640_calc_pixel_rate(sensor) * 16;
1842 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
1843 rate = rate / sensor->ep.bus.mipi_csi2.num_data_lanes;
1844 ret = ov5640_set_mipi_pclk(sensor, rate);
1845 } else {
1846 rate = rate / sensor->ep.bus.parallel.bus_width;
1847 ret = ov5640_set_dvp_pclk(sensor, rate);
1848 }
1849
1850 if (ret < 0)
1851 return 0;
1852
1853 if ((dn_mode == SUBSAMPLING && orig_dn_mode == SCALING) ||
1854 (dn_mode == SCALING && orig_dn_mode == SUBSAMPLING)) {
1855
1856
1857
1858
1859 ret = ov5640_set_mode_exposure_calc(sensor, mode);
1860 } else {
1861
1862
1863
1864
1865 ret = ov5640_set_mode_direct(sensor, mode);
1866 }
1867 if (ret < 0)
1868 goto restore_auto_exp_gain;
1869
1870
1871 if (auto_gain)
1872 ov5640_set_autogain(sensor, true);
1873 if (auto_exp)
1874 ov5640_set_autoexposure(sensor, true);
1875
1876 ret = ov5640_set_binning(sensor, dn_mode != SCALING);
1877 if (ret < 0)
1878 return ret;
1879 ret = ov5640_set_ae_target(sensor, sensor->ae_target);
1880 if (ret < 0)
1881 return ret;
1882 ret = ov5640_get_light_freq(sensor);
1883 if (ret < 0)
1884 return ret;
1885 ret = ov5640_set_bandingfilter(sensor);
1886 if (ret < 0)
1887 return ret;
1888 ret = ov5640_set_virtual_channel(sensor);
1889 if (ret < 0)
1890 return ret;
1891
1892 sensor->pending_mode_change = false;
1893 sensor->last_mode = mode;
1894
1895 return 0;
1896
1897restore_auto_exp_gain:
1898 if (auto_exp)
1899 ov5640_set_autoexposure(sensor, true);
1900restore_auto_gain:
1901 if (auto_gain)
1902 ov5640_set_autogain(sensor, true);
1903
1904 return ret;
1905}
1906
1907static int ov5640_set_framefmt(struct ov5640_dev *sensor,
1908 struct v4l2_mbus_framefmt *format);
1909
1910
1911static int ov5640_restore_mode(struct ov5640_dev *sensor)
1912{
1913 int ret;
1914
1915
1916 ret = ov5640_load_regs(sensor, &ov5640_mode_init_data);
1917 if (ret < 0)
1918 return ret;
1919 sensor->last_mode = &ov5640_mode_init_data;
1920
1921 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_ROOT_DIVIDER, 0x3f,
1922 (ilog2(OV5640_SCLK2X_ROOT_DIV) << 2) |
1923 ilog2(OV5640_SCLK_ROOT_DIV));
1924 if (ret)
1925 return ret;
1926
1927
1928 ret = ov5640_set_mode(sensor);
1929 if (ret < 0)
1930 return ret;
1931
1932 return ov5640_set_framefmt(sensor, &sensor->fmt);
1933}
1934
1935static void ov5640_power(struct ov5640_dev *sensor, bool enable)
1936{
1937 gpiod_set_value_cansleep(sensor->pwdn_gpio, enable ? 0 : 1);
1938}
1939
1940static void ov5640_reset(struct ov5640_dev *sensor)
1941{
1942 if (!sensor->reset_gpio)
1943 return;
1944
1945 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1946
1947
1948 ov5640_power(sensor, false);
1949 usleep_range(5000, 10000);
1950 ov5640_power(sensor, true);
1951 usleep_range(5000, 10000);
1952
1953 gpiod_set_value_cansleep(sensor->reset_gpio, 1);
1954 usleep_range(1000, 2000);
1955
1956 gpiod_set_value_cansleep(sensor->reset_gpio, 0);
1957 usleep_range(20000, 25000);
1958}
1959
1960static int ov5640_set_power_on(struct ov5640_dev *sensor)
1961{
1962 struct i2c_client *client = sensor->i2c_client;
1963 int ret;
1964
1965 ret = clk_prepare_enable(sensor->xclk);
1966 if (ret) {
1967 dev_err(&client->dev, "%s: failed to enable clock\n",
1968 __func__);
1969 return ret;
1970 }
1971
1972 ret = regulator_bulk_enable(OV5640_NUM_SUPPLIES,
1973 sensor->supplies);
1974 if (ret) {
1975 dev_err(&client->dev, "%s: failed to enable regulators\n",
1976 __func__);
1977 goto xclk_off;
1978 }
1979
1980 ov5640_reset(sensor);
1981 ov5640_power(sensor, true);
1982
1983 ret = ov5640_init_slave_id(sensor);
1984 if (ret)
1985 goto power_off;
1986
1987 return 0;
1988
1989power_off:
1990 ov5640_power(sensor, false);
1991 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
1992xclk_off:
1993 clk_disable_unprepare(sensor->xclk);
1994 return ret;
1995}
1996
1997static void ov5640_set_power_off(struct ov5640_dev *sensor)
1998{
1999 ov5640_power(sensor, false);
2000 regulator_bulk_disable(OV5640_NUM_SUPPLIES, sensor->supplies);
2001 clk_disable_unprepare(sensor->xclk);
2002}
2003
2004static int ov5640_set_power(struct ov5640_dev *sensor, bool on)
2005{
2006 int ret = 0;
2007
2008 if (on) {
2009 ret = ov5640_set_power_on(sensor);
2010 if (ret)
2011 return ret;
2012
2013 ret = ov5640_restore_mode(sensor);
2014 if (ret)
2015 goto power_off;
2016
2017
2018 if (sensor->ep.bus_type != V4L2_MBUS_CSI2_DPHY)
2019 return 0;
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031 ret = ov5640_write_reg(sensor,
2032 OV5640_REG_IO_MIPI_CTRL00, 0x40);
2033 if (ret)
2034 goto power_off;
2035
2036
2037
2038
2039
2040
2041
2042
2043 ret = ov5640_write_reg(sensor,
2044 OV5640_REG_MIPI_CTRL00, 0x24);
2045 if (ret)
2046 goto power_off;
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056 ret = ov5640_write_reg(sensor,
2057 OV5640_REG_PAD_OUTPUT00, 0x70);
2058 if (ret)
2059 goto power_off;
2060
2061
2062 usleep_range(500, 1000);
2063
2064 } else {
2065 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY) {
2066
2067 ov5640_write_reg(sensor,
2068 OV5640_REG_IO_MIPI_CTRL00, 0x58);
2069 ov5640_write_reg(sensor,
2070 OV5640_REG_MIPI_CTRL00, 0x04);
2071 ov5640_write_reg(sensor,
2072 OV5640_REG_PAD_OUTPUT00, 0x00);
2073 }
2074
2075 ov5640_set_power_off(sensor);
2076 }
2077
2078 return 0;
2079
2080power_off:
2081 ov5640_set_power_off(sensor);
2082 return ret;
2083}
2084
2085
2086
2087static int ov5640_s_power(struct v4l2_subdev *sd, int on)
2088{
2089 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2090 int ret = 0;
2091
2092 mutex_lock(&sensor->lock);
2093
2094
2095
2096
2097
2098 if (sensor->power_count == !on) {
2099 ret = ov5640_set_power(sensor, !!on);
2100 if (ret)
2101 goto out;
2102 }
2103
2104
2105 sensor->power_count += on ? 1 : -1;
2106 WARN_ON(sensor->power_count < 0);
2107out:
2108 mutex_unlock(&sensor->lock);
2109
2110 if (on && !ret && sensor->power_count == 1) {
2111
2112 ret = v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
2113 }
2114
2115 return ret;
2116}
2117
2118static int ov5640_try_frame_interval(struct ov5640_dev *sensor,
2119 struct v4l2_fract *fi,
2120 u32 width, u32 height)
2121{
2122 const struct ov5640_mode_info *mode;
2123 enum ov5640_frame_rate rate = OV5640_15_FPS;
2124 int minfps, maxfps, best_fps, fps;
2125 int i;
2126
2127 minfps = ov5640_framerates[OV5640_15_FPS];
2128 maxfps = ov5640_framerates[OV5640_60_FPS];
2129
2130 if (fi->numerator == 0) {
2131 fi->denominator = maxfps;
2132 fi->numerator = 1;
2133 rate = OV5640_60_FPS;
2134 goto find_mode;
2135 }
2136
2137 fps = clamp_val(DIV_ROUND_CLOSEST(fi->denominator, fi->numerator),
2138 minfps, maxfps);
2139
2140 best_fps = minfps;
2141 for (i = 0; i < ARRAY_SIZE(ov5640_framerates); i++) {
2142 int curr_fps = ov5640_framerates[i];
2143
2144 if (abs(curr_fps - fps) < abs(best_fps - fps)) {
2145 best_fps = curr_fps;
2146 rate = i;
2147 }
2148 }
2149
2150 fi->numerator = 1;
2151 fi->denominator = best_fps;
2152
2153find_mode:
2154 mode = ov5640_find_mode(sensor, rate, width, height, false);
2155 return mode ? rate : -EINVAL;
2156}
2157
2158static int ov5640_get_fmt(struct v4l2_subdev *sd,
2159 struct v4l2_subdev_pad_config *cfg,
2160 struct v4l2_subdev_format *format)
2161{
2162 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2163 struct v4l2_mbus_framefmt *fmt;
2164
2165 if (format->pad != 0)
2166 return -EINVAL;
2167
2168 mutex_lock(&sensor->lock);
2169
2170 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2171 fmt = v4l2_subdev_get_try_format(&sensor->sd, cfg,
2172 format->pad);
2173 else
2174 fmt = &sensor->fmt;
2175
2176 format->format = *fmt;
2177
2178 mutex_unlock(&sensor->lock);
2179
2180 return 0;
2181}
2182
2183static int ov5640_try_fmt_internal(struct v4l2_subdev *sd,
2184 struct v4l2_mbus_framefmt *fmt,
2185 enum ov5640_frame_rate fr,
2186 const struct ov5640_mode_info **new_mode)
2187{
2188 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2189 const struct ov5640_mode_info *mode;
2190 int i;
2191
2192 mode = ov5640_find_mode(sensor, fr, fmt->width, fmt->height, true);
2193 if (!mode)
2194 return -EINVAL;
2195 fmt->width = mode->hact;
2196 fmt->height = mode->vact;
2197
2198 if (new_mode)
2199 *new_mode = mode;
2200
2201 for (i = 0; i < ARRAY_SIZE(ov5640_formats); i++)
2202 if (ov5640_formats[i].code == fmt->code)
2203 break;
2204 if (i >= ARRAY_SIZE(ov5640_formats))
2205 i = 0;
2206
2207 fmt->code = ov5640_formats[i].code;
2208 fmt->colorspace = ov5640_formats[i].colorspace;
2209 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2210 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2211 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2212
2213 return 0;
2214}
2215
2216static int ov5640_set_fmt(struct v4l2_subdev *sd,
2217 struct v4l2_subdev_pad_config *cfg,
2218 struct v4l2_subdev_format *format)
2219{
2220 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2221 const struct ov5640_mode_info *new_mode;
2222 struct v4l2_mbus_framefmt *mbus_fmt = &format->format;
2223 struct v4l2_mbus_framefmt *fmt;
2224 int ret;
2225
2226 if (format->pad != 0)
2227 return -EINVAL;
2228
2229 mutex_lock(&sensor->lock);
2230
2231 if (sensor->streaming) {
2232 ret = -EBUSY;
2233 goto out;
2234 }
2235
2236 ret = ov5640_try_fmt_internal(sd, mbus_fmt,
2237 sensor->current_fr, &new_mode);
2238 if (ret)
2239 goto out;
2240
2241 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
2242 fmt = v4l2_subdev_get_try_format(sd, cfg, 0);
2243 else
2244 fmt = &sensor->fmt;
2245
2246 *fmt = *mbus_fmt;
2247
2248 if (new_mode != sensor->current_mode) {
2249 sensor->current_mode = new_mode;
2250 sensor->pending_mode_change = true;
2251 }
2252 if (mbus_fmt->code != sensor->fmt.code)
2253 sensor->pending_fmt_change = true;
2254
2255 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2256 ov5640_calc_pixel_rate(sensor));
2257out:
2258 mutex_unlock(&sensor->lock);
2259 return ret;
2260}
2261
2262static int ov5640_set_framefmt(struct ov5640_dev *sensor,
2263 struct v4l2_mbus_framefmt *format)
2264{
2265 int ret = 0;
2266 bool is_jpeg = false;
2267 u8 fmt, mux;
2268
2269 switch (format->code) {
2270 case MEDIA_BUS_FMT_UYVY8_2X8:
2271
2272 fmt = 0x3f;
2273 mux = OV5640_FMT_MUX_YUV422;
2274 break;
2275 case MEDIA_BUS_FMT_YUYV8_2X8:
2276
2277 fmt = 0x30;
2278 mux = OV5640_FMT_MUX_YUV422;
2279 break;
2280 case MEDIA_BUS_FMT_RGB565_2X8_LE:
2281
2282 fmt = 0x6F;
2283 mux = OV5640_FMT_MUX_RGB;
2284 break;
2285 case MEDIA_BUS_FMT_RGB565_2X8_BE:
2286
2287 fmt = 0x61;
2288 mux = OV5640_FMT_MUX_RGB;
2289 break;
2290 case MEDIA_BUS_FMT_JPEG_1X8:
2291
2292 fmt = 0x30;
2293 mux = OV5640_FMT_MUX_YUV422;
2294 is_jpeg = true;
2295 break;
2296 case MEDIA_BUS_FMT_SBGGR8_1X8:
2297
2298 fmt = 0x00;
2299 mux = OV5640_FMT_MUX_RAW_DPC;
2300 break;
2301 case MEDIA_BUS_FMT_SGBRG8_1X8:
2302
2303 fmt = 0x01;
2304 mux = OV5640_FMT_MUX_RAW_DPC;
2305 break;
2306 case MEDIA_BUS_FMT_SGRBG8_1X8:
2307
2308 fmt = 0x02;
2309 mux = OV5640_FMT_MUX_RAW_DPC;
2310 break;
2311 case MEDIA_BUS_FMT_SRGGB8_1X8:
2312
2313 fmt = 0x03;
2314 mux = OV5640_FMT_MUX_RAW_DPC;
2315 break;
2316 default:
2317 return -EINVAL;
2318 }
2319
2320
2321 ret = ov5640_write_reg(sensor, OV5640_REG_FORMAT_CONTROL00, fmt);
2322 if (ret)
2323 return ret;
2324
2325
2326 ret = ov5640_write_reg(sensor, OV5640_REG_ISP_FORMAT_MUX_CTRL, mux);
2327 if (ret)
2328 return ret;
2329
2330
2331
2332
2333
2334 ret = ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2335 BIT(5), is_jpeg ? BIT(5) : 0);
2336 if (ret)
2337 return ret;
2338
2339
2340
2341
2342
2343
2344
2345 ret = ov5640_mod_reg(sensor, OV5640_REG_SYS_RESET02,
2346 BIT(4) | BIT(3) | BIT(2),
2347 is_jpeg ? 0 : (BIT(4) | BIT(3) | BIT(2)));
2348 if (ret)
2349 return ret;
2350
2351
2352
2353
2354
2355
2356 return ov5640_mod_reg(sensor, OV5640_REG_SYS_CLOCK_ENABLE02,
2357 BIT(5) | BIT(3),
2358 is_jpeg ? (BIT(5) | BIT(3)) : 0);
2359}
2360
2361
2362
2363
2364
2365static int ov5640_set_ctrl_hue(struct ov5640_dev *sensor, int value)
2366{
2367 int ret;
2368
2369 if (value) {
2370 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2371 BIT(0), BIT(0));
2372 if (ret)
2373 return ret;
2374 ret = ov5640_write_reg16(sensor, OV5640_REG_SDE_CTRL1, value);
2375 } else {
2376 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(0), 0);
2377 }
2378
2379 return ret;
2380}
2381
2382static int ov5640_set_ctrl_contrast(struct ov5640_dev *sensor, int value)
2383{
2384 int ret;
2385
2386 if (value) {
2387 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2388 BIT(2), BIT(2));
2389 if (ret)
2390 return ret;
2391 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL5,
2392 value & 0xff);
2393 } else {
2394 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(2), 0);
2395 }
2396
2397 return ret;
2398}
2399
2400static int ov5640_set_ctrl_saturation(struct ov5640_dev *sensor, int value)
2401{
2402 int ret;
2403
2404 if (value) {
2405 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0,
2406 BIT(1), BIT(1));
2407 if (ret)
2408 return ret;
2409 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL3,
2410 value & 0xff);
2411 if (ret)
2412 return ret;
2413 ret = ov5640_write_reg(sensor, OV5640_REG_SDE_CTRL4,
2414 value & 0xff);
2415 } else {
2416 ret = ov5640_mod_reg(sensor, OV5640_REG_SDE_CTRL0, BIT(1), 0);
2417 }
2418
2419 return ret;
2420}
2421
2422static int ov5640_set_ctrl_white_balance(struct ov5640_dev *sensor, int awb)
2423{
2424 int ret;
2425
2426 ret = ov5640_mod_reg(sensor, OV5640_REG_AWB_MANUAL_CTRL,
2427 BIT(0), awb ? 0 : 1);
2428 if (ret)
2429 return ret;
2430
2431 if (!awb) {
2432 u16 red = (u16)sensor->ctrls.red_balance->val;
2433 u16 blue = (u16)sensor->ctrls.blue_balance->val;
2434
2435 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_R_GAIN, red);
2436 if (ret)
2437 return ret;
2438 ret = ov5640_write_reg16(sensor, OV5640_REG_AWB_B_GAIN, blue);
2439 }
2440
2441 return ret;
2442}
2443
2444static int ov5640_set_ctrl_exposure(struct ov5640_dev *sensor,
2445 enum v4l2_exposure_auto_type auto_exposure)
2446{
2447 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2448 bool auto_exp = (auto_exposure == V4L2_EXPOSURE_AUTO);
2449 int ret = 0;
2450
2451 if (ctrls->auto_exp->is_new) {
2452 ret = ov5640_set_autoexposure(sensor, auto_exp);
2453 if (ret)
2454 return ret;
2455 }
2456
2457 if (!auto_exp && ctrls->exposure->is_new) {
2458 u16 max_exp;
2459
2460 ret = ov5640_read_reg16(sensor, OV5640_REG_AEC_PK_VTS,
2461 &max_exp);
2462 if (ret)
2463 return ret;
2464 ret = ov5640_get_vts(sensor);
2465 if (ret < 0)
2466 return ret;
2467 max_exp += ret;
2468 ret = 0;
2469
2470 if (ctrls->exposure->val < max_exp)
2471 ret = ov5640_set_exposure(sensor, ctrls->exposure->val);
2472 }
2473
2474 return ret;
2475}
2476
2477static int ov5640_set_ctrl_gain(struct ov5640_dev *sensor, bool auto_gain)
2478{
2479 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2480 int ret = 0;
2481
2482 if (ctrls->auto_gain->is_new) {
2483 ret = ov5640_set_autogain(sensor, auto_gain);
2484 if (ret)
2485 return ret;
2486 }
2487
2488 if (!auto_gain && ctrls->gain->is_new)
2489 ret = ov5640_set_gain(sensor, ctrls->gain->val);
2490
2491 return ret;
2492}
2493
2494static const char * const test_pattern_menu[] = {
2495 "Disabled",
2496 "Color bars",
2497 "Color bars w/ rolling bar",
2498 "Color squares",
2499 "Color squares w/ rolling bar",
2500};
2501
2502#define OV5640_TEST_ENABLE BIT(7)
2503#define OV5640_TEST_ROLLING BIT(6)
2504#define OV5640_TEST_TRANSPARENT BIT(5)
2505#define OV5640_TEST_SQUARE_BW BIT(4)
2506#define OV5640_TEST_BAR_STANDARD (0 << 2)
2507#define OV5640_TEST_BAR_VERT_CHANGE_1 (1 << 2)
2508#define OV5640_TEST_BAR_HOR_CHANGE (2 << 2)
2509#define OV5640_TEST_BAR_VERT_CHANGE_2 (3 << 2)
2510#define OV5640_TEST_BAR (0 << 0)
2511#define OV5640_TEST_RANDOM (1 << 0)
2512#define OV5640_TEST_SQUARE (2 << 0)
2513#define OV5640_TEST_BLACK (3 << 0)
2514
2515static const u8 test_pattern_val[] = {
2516 0,
2517 OV5640_TEST_ENABLE | OV5640_TEST_BAR_VERT_CHANGE_1 |
2518 OV5640_TEST_BAR,
2519 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING |
2520 OV5640_TEST_BAR_VERT_CHANGE_1 | OV5640_TEST_BAR,
2521 OV5640_TEST_ENABLE | OV5640_TEST_SQUARE,
2522 OV5640_TEST_ENABLE | OV5640_TEST_ROLLING | OV5640_TEST_SQUARE,
2523};
2524
2525static int ov5640_set_ctrl_test_pattern(struct ov5640_dev *sensor, int value)
2526{
2527 return ov5640_write_reg(sensor, OV5640_REG_PRE_ISP_TEST_SET1,
2528 test_pattern_val[value]);
2529}
2530
2531static int ov5640_set_ctrl_light_freq(struct ov5640_dev *sensor, int value)
2532{
2533 int ret;
2534
2535 ret = ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL01, BIT(7),
2536 (value == V4L2_CID_POWER_LINE_FREQUENCY_AUTO) ?
2537 0 : BIT(7));
2538 if (ret)
2539 return ret;
2540
2541 return ov5640_mod_reg(sensor, OV5640_REG_HZ5060_CTRL00, BIT(2),
2542 (value == V4L2_CID_POWER_LINE_FREQUENCY_50HZ) ?
2543 BIT(2) : 0);
2544}
2545
2546static int ov5640_set_ctrl_hflip(struct ov5640_dev *sensor, int value)
2547{
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG21,
2563 BIT(2) | BIT(1),
2564 (!(value ^ sensor->upside_down)) ?
2565 (BIT(2) | BIT(1)) : 0);
2566}
2567
2568static int ov5640_set_ctrl_vflip(struct ov5640_dev *sensor, int value)
2569{
2570
2571
2572
2573
2574
2575
2576
2577 return ov5640_mod_reg(sensor, OV5640_REG_TIMING_TC_REG20,
2578 BIT(2) | BIT(1),
2579 (value ^ sensor->upside_down) ?
2580 (BIT(2) | BIT(1)) : 0);
2581}
2582
2583static int ov5640_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
2584{
2585 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2586 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2587 int val;
2588
2589
2590
2591 switch (ctrl->id) {
2592 case V4L2_CID_AUTOGAIN:
2593 val = ov5640_get_gain(sensor);
2594 if (val < 0)
2595 return val;
2596 sensor->ctrls.gain->val = val;
2597 break;
2598 case V4L2_CID_EXPOSURE_AUTO:
2599 val = ov5640_get_exposure(sensor);
2600 if (val < 0)
2601 return val;
2602 sensor->ctrls.exposure->val = val;
2603 break;
2604 }
2605
2606 return 0;
2607}
2608
2609static int ov5640_s_ctrl(struct v4l2_ctrl *ctrl)
2610{
2611 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
2612 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2613 int ret;
2614
2615
2616
2617
2618
2619
2620
2621
2622 if (sensor->power_count == 0)
2623 return 0;
2624
2625 switch (ctrl->id) {
2626 case V4L2_CID_AUTOGAIN:
2627 ret = ov5640_set_ctrl_gain(sensor, ctrl->val);
2628 break;
2629 case V4L2_CID_EXPOSURE_AUTO:
2630 ret = ov5640_set_ctrl_exposure(sensor, ctrl->val);
2631 break;
2632 case V4L2_CID_AUTO_WHITE_BALANCE:
2633 ret = ov5640_set_ctrl_white_balance(sensor, ctrl->val);
2634 break;
2635 case V4L2_CID_HUE:
2636 ret = ov5640_set_ctrl_hue(sensor, ctrl->val);
2637 break;
2638 case V4L2_CID_CONTRAST:
2639 ret = ov5640_set_ctrl_contrast(sensor, ctrl->val);
2640 break;
2641 case V4L2_CID_SATURATION:
2642 ret = ov5640_set_ctrl_saturation(sensor, ctrl->val);
2643 break;
2644 case V4L2_CID_TEST_PATTERN:
2645 ret = ov5640_set_ctrl_test_pattern(sensor, ctrl->val);
2646 break;
2647 case V4L2_CID_POWER_LINE_FREQUENCY:
2648 ret = ov5640_set_ctrl_light_freq(sensor, ctrl->val);
2649 break;
2650 case V4L2_CID_HFLIP:
2651 ret = ov5640_set_ctrl_hflip(sensor, ctrl->val);
2652 break;
2653 case V4L2_CID_VFLIP:
2654 ret = ov5640_set_ctrl_vflip(sensor, ctrl->val);
2655 break;
2656 default:
2657 ret = -EINVAL;
2658 break;
2659 }
2660
2661 return ret;
2662}
2663
2664static const struct v4l2_ctrl_ops ov5640_ctrl_ops = {
2665 .g_volatile_ctrl = ov5640_g_volatile_ctrl,
2666 .s_ctrl = ov5640_s_ctrl,
2667};
2668
2669static int ov5640_init_controls(struct ov5640_dev *sensor)
2670{
2671 const struct v4l2_ctrl_ops *ops = &ov5640_ctrl_ops;
2672 struct ov5640_ctrls *ctrls = &sensor->ctrls;
2673 struct v4l2_ctrl_handler *hdl = &ctrls->handler;
2674 int ret;
2675
2676 v4l2_ctrl_handler_init(hdl, 32);
2677
2678
2679 hdl->lock = &sensor->lock;
2680
2681
2682 ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_PIXEL_RATE,
2683 0, INT_MAX, 1,
2684 ov5640_calc_pixel_rate(sensor));
2685
2686
2687 ctrls->auto_wb = v4l2_ctrl_new_std(hdl, ops,
2688 V4L2_CID_AUTO_WHITE_BALANCE,
2689 0, 1, 1, 1);
2690 ctrls->blue_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_BLUE_BALANCE,
2691 0, 4095, 1, 0);
2692 ctrls->red_balance = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_RED_BALANCE,
2693 0, 4095, 1, 0);
2694
2695 ctrls->auto_exp = v4l2_ctrl_new_std_menu(hdl, ops,
2696 V4L2_CID_EXPOSURE_AUTO,
2697 V4L2_EXPOSURE_MANUAL, 0,
2698 V4L2_EXPOSURE_AUTO);
2699 ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
2700 0, 65535, 1, 0);
2701
2702 ctrls->auto_gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_AUTOGAIN,
2703 0, 1, 1, 1);
2704 ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_GAIN,
2705 0, 1023, 1, 0);
2706
2707 ctrls->saturation = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_SATURATION,
2708 0, 255, 1, 64);
2709 ctrls->hue = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HUE,
2710 0, 359, 1, 0);
2711 ctrls->contrast = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_CONTRAST,
2712 0, 255, 1, 0);
2713 ctrls->test_pattern =
2714 v4l2_ctrl_new_std_menu_items(hdl, ops, V4L2_CID_TEST_PATTERN,
2715 ARRAY_SIZE(test_pattern_menu) - 1,
2716 0, 0, test_pattern_menu);
2717 ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP,
2718 0, 1, 1, 0);
2719 ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP,
2720 0, 1, 1, 0);
2721
2722 ctrls->light_freq =
2723 v4l2_ctrl_new_std_menu(hdl, ops,
2724 V4L2_CID_POWER_LINE_FREQUENCY,
2725 V4L2_CID_POWER_LINE_FREQUENCY_AUTO, 0,
2726 V4L2_CID_POWER_LINE_FREQUENCY_50HZ);
2727
2728 if (hdl->error) {
2729 ret = hdl->error;
2730 goto free_ctrls;
2731 }
2732
2733 ctrls->pixel_rate->flags |= V4L2_CTRL_FLAG_READ_ONLY;
2734 ctrls->gain->flags |= V4L2_CTRL_FLAG_VOLATILE;
2735 ctrls->exposure->flags |= V4L2_CTRL_FLAG_VOLATILE;
2736
2737 v4l2_ctrl_auto_cluster(3, &ctrls->auto_wb, 0, false);
2738 v4l2_ctrl_auto_cluster(2, &ctrls->auto_gain, 0, true);
2739 v4l2_ctrl_auto_cluster(2, &ctrls->auto_exp, 1, true);
2740
2741 sensor->sd.ctrl_handler = hdl;
2742 return 0;
2743
2744free_ctrls:
2745 v4l2_ctrl_handler_free(hdl);
2746 return ret;
2747}
2748
2749static int ov5640_enum_frame_size(struct v4l2_subdev *sd,
2750 struct v4l2_subdev_pad_config *cfg,
2751 struct v4l2_subdev_frame_size_enum *fse)
2752{
2753 if (fse->pad != 0)
2754 return -EINVAL;
2755 if (fse->index >= OV5640_NUM_MODES)
2756 return -EINVAL;
2757
2758 fse->min_width =
2759 ov5640_mode_data[fse->index].hact;
2760 fse->max_width = fse->min_width;
2761 fse->min_height =
2762 ov5640_mode_data[fse->index].vact;
2763 fse->max_height = fse->min_height;
2764
2765 return 0;
2766}
2767
2768static int ov5640_enum_frame_interval(
2769 struct v4l2_subdev *sd,
2770 struct v4l2_subdev_pad_config *cfg,
2771 struct v4l2_subdev_frame_interval_enum *fie)
2772{
2773 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2774 struct v4l2_fract tpf;
2775 int ret;
2776
2777 if (fie->pad != 0)
2778 return -EINVAL;
2779 if (fie->index >= OV5640_NUM_FRAMERATES)
2780 return -EINVAL;
2781
2782 tpf.numerator = 1;
2783 tpf.denominator = ov5640_framerates[fie->index];
2784
2785 ret = ov5640_try_frame_interval(sensor, &tpf,
2786 fie->width, fie->height);
2787 if (ret < 0)
2788 return -EINVAL;
2789
2790 fie->interval = tpf;
2791 return 0;
2792}
2793
2794static int ov5640_g_frame_interval(struct v4l2_subdev *sd,
2795 struct v4l2_subdev_frame_interval *fi)
2796{
2797 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2798
2799 mutex_lock(&sensor->lock);
2800 fi->interval = sensor->frame_interval;
2801 mutex_unlock(&sensor->lock);
2802
2803 return 0;
2804}
2805
2806static int ov5640_s_frame_interval(struct v4l2_subdev *sd,
2807 struct v4l2_subdev_frame_interval *fi)
2808{
2809 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2810 const struct ov5640_mode_info *mode;
2811 int frame_rate, ret = 0;
2812
2813 if (fi->pad != 0)
2814 return -EINVAL;
2815
2816 mutex_lock(&sensor->lock);
2817
2818 if (sensor->streaming) {
2819 ret = -EBUSY;
2820 goto out;
2821 }
2822
2823 mode = sensor->current_mode;
2824
2825 frame_rate = ov5640_try_frame_interval(sensor, &fi->interval,
2826 mode->hact, mode->vact);
2827 if (frame_rate < 0) {
2828
2829 fi->interval = sensor->frame_interval;
2830 goto out;
2831 }
2832
2833 mode = ov5640_find_mode(sensor, frame_rate, mode->hact,
2834 mode->vact, true);
2835 if (!mode) {
2836 ret = -EINVAL;
2837 goto out;
2838 }
2839
2840 if (mode != sensor->current_mode ||
2841 frame_rate != sensor->current_fr) {
2842 sensor->current_fr = frame_rate;
2843 sensor->frame_interval = fi->interval;
2844 sensor->current_mode = mode;
2845 sensor->pending_mode_change = true;
2846
2847 __v4l2_ctrl_s_ctrl_int64(sensor->ctrls.pixel_rate,
2848 ov5640_calc_pixel_rate(sensor));
2849 }
2850out:
2851 mutex_unlock(&sensor->lock);
2852 return ret;
2853}
2854
2855static int ov5640_enum_mbus_code(struct v4l2_subdev *sd,
2856 struct v4l2_subdev_pad_config *cfg,
2857 struct v4l2_subdev_mbus_code_enum *code)
2858{
2859 if (code->pad != 0)
2860 return -EINVAL;
2861 if (code->index >= ARRAY_SIZE(ov5640_formats))
2862 return -EINVAL;
2863
2864 code->code = ov5640_formats[code->index].code;
2865 return 0;
2866}
2867
2868static int ov5640_s_stream(struct v4l2_subdev *sd, int enable)
2869{
2870 struct ov5640_dev *sensor = to_ov5640_dev(sd);
2871 int ret = 0;
2872
2873 mutex_lock(&sensor->lock);
2874
2875 if (sensor->streaming == !enable) {
2876 if (enable && sensor->pending_mode_change) {
2877 ret = ov5640_set_mode(sensor);
2878 if (ret)
2879 goto out;
2880 }
2881
2882 if (enable && sensor->pending_fmt_change) {
2883 ret = ov5640_set_framefmt(sensor, &sensor->fmt);
2884 if (ret)
2885 goto out;
2886 sensor->pending_fmt_change = false;
2887 }
2888
2889 if (sensor->ep.bus_type == V4L2_MBUS_CSI2_DPHY)
2890 ret = ov5640_set_stream_mipi(sensor, enable);
2891 else
2892 ret = ov5640_set_stream_dvp(sensor, enable);
2893
2894 if (!ret)
2895 sensor->streaming = enable;
2896 }
2897out:
2898 mutex_unlock(&sensor->lock);
2899 return ret;
2900}
2901
2902static const struct v4l2_subdev_core_ops ov5640_core_ops = {
2903 .s_power = ov5640_s_power,
2904 .log_status = v4l2_ctrl_subdev_log_status,
2905 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
2906 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
2907};
2908
2909static const struct v4l2_subdev_video_ops ov5640_video_ops = {
2910 .g_frame_interval = ov5640_g_frame_interval,
2911 .s_frame_interval = ov5640_s_frame_interval,
2912 .s_stream = ov5640_s_stream,
2913};
2914
2915static const struct v4l2_subdev_pad_ops ov5640_pad_ops = {
2916 .enum_mbus_code = ov5640_enum_mbus_code,
2917 .get_fmt = ov5640_get_fmt,
2918 .set_fmt = ov5640_set_fmt,
2919 .enum_frame_size = ov5640_enum_frame_size,
2920 .enum_frame_interval = ov5640_enum_frame_interval,
2921};
2922
2923static const struct v4l2_subdev_ops ov5640_subdev_ops = {
2924 .core = &ov5640_core_ops,
2925 .video = &ov5640_video_ops,
2926 .pad = &ov5640_pad_ops,
2927};
2928
2929static int ov5640_get_regulators(struct ov5640_dev *sensor)
2930{
2931 int i;
2932
2933 for (i = 0; i < OV5640_NUM_SUPPLIES; i++)
2934 sensor->supplies[i].supply = ov5640_supply_name[i];
2935
2936 return devm_regulator_bulk_get(&sensor->i2c_client->dev,
2937 OV5640_NUM_SUPPLIES,
2938 sensor->supplies);
2939}
2940
2941static int ov5640_check_chip_id(struct ov5640_dev *sensor)
2942{
2943 struct i2c_client *client = sensor->i2c_client;
2944 int ret = 0;
2945 u16 chip_id;
2946
2947 ret = ov5640_set_power_on(sensor);
2948 if (ret)
2949 return ret;
2950
2951 ret = ov5640_read_reg16(sensor, OV5640_REG_CHIP_ID, &chip_id);
2952 if (ret) {
2953 dev_err(&client->dev, "%s: failed to read chip identifier\n",
2954 __func__);
2955 goto power_off;
2956 }
2957
2958 if (chip_id != 0x5640) {
2959 dev_err(&client->dev, "%s: wrong chip identifier, expected 0x5640, got 0x%x\n",
2960 __func__, chip_id);
2961 ret = -ENXIO;
2962 }
2963
2964power_off:
2965 ov5640_set_power_off(sensor);
2966 return ret;
2967}
2968
2969static int ov5640_probe(struct i2c_client *client)
2970{
2971 struct device *dev = &client->dev;
2972 struct fwnode_handle *endpoint;
2973 struct ov5640_dev *sensor;
2974 struct v4l2_mbus_framefmt *fmt;
2975 u32 rotation;
2976 int ret;
2977
2978 sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
2979 if (!sensor)
2980 return -ENOMEM;
2981
2982 sensor->i2c_client = client;
2983
2984
2985
2986
2987
2988 fmt = &sensor->fmt;
2989 fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
2990 fmt->colorspace = V4L2_COLORSPACE_SRGB;
2991 fmt->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(fmt->colorspace);
2992 fmt->quantization = V4L2_QUANTIZATION_FULL_RANGE;
2993 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
2994 fmt->width = 640;
2995 fmt->height = 480;
2996 fmt->field = V4L2_FIELD_NONE;
2997 sensor->frame_interval.numerator = 1;
2998 sensor->frame_interval.denominator = ov5640_framerates[OV5640_30_FPS];
2999 sensor->current_fr = OV5640_30_FPS;
3000 sensor->current_mode =
3001 &ov5640_mode_data[OV5640_MODE_VGA_640_480];
3002 sensor->last_mode = sensor->current_mode;
3003
3004 sensor->ae_target = 52;
3005
3006
3007 ret = fwnode_property_read_u32(dev_fwnode(&client->dev), "rotation",
3008 &rotation);
3009 if (!ret) {
3010 switch (rotation) {
3011 case 180:
3012 sensor->upside_down = true;
3013
3014 case 0:
3015 break;
3016 default:
3017 dev_warn(dev, "%u degrees rotation is not supported, ignoring...\n",
3018 rotation);
3019 }
3020 }
3021
3022 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(&client->dev),
3023 NULL);
3024 if (!endpoint) {
3025 dev_err(dev, "endpoint node not found\n");
3026 return -EINVAL;
3027 }
3028
3029 ret = v4l2_fwnode_endpoint_parse(endpoint, &sensor->ep);
3030 fwnode_handle_put(endpoint);
3031 if (ret) {
3032 dev_err(dev, "Could not parse endpoint\n");
3033 return ret;
3034 }
3035
3036
3037 sensor->xclk = devm_clk_get(dev, "xclk");
3038 if (IS_ERR(sensor->xclk)) {
3039 dev_err(dev, "failed to get xclk\n");
3040 return PTR_ERR(sensor->xclk);
3041 }
3042
3043 sensor->xclk_freq = clk_get_rate(sensor->xclk);
3044 if (sensor->xclk_freq < OV5640_XCLK_MIN ||
3045 sensor->xclk_freq > OV5640_XCLK_MAX) {
3046 dev_err(dev, "xclk frequency out of range: %d Hz\n",
3047 sensor->xclk_freq);
3048 return -EINVAL;
3049 }
3050
3051
3052 sensor->pwdn_gpio = devm_gpiod_get_optional(dev, "powerdown",
3053 GPIOD_OUT_HIGH);
3054 if (IS_ERR(sensor->pwdn_gpio))
3055 return PTR_ERR(sensor->pwdn_gpio);
3056
3057
3058 sensor->reset_gpio = devm_gpiod_get_optional(dev, "reset",
3059 GPIOD_OUT_HIGH);
3060 if (IS_ERR(sensor->reset_gpio))
3061 return PTR_ERR(sensor->reset_gpio);
3062
3063 v4l2_i2c_subdev_init(&sensor->sd, client, &ov5640_subdev_ops);
3064
3065 sensor->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
3066 V4L2_SUBDEV_FL_HAS_EVENTS;
3067 sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
3068 sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
3069 ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
3070 if (ret)
3071 return ret;
3072
3073 ret = ov5640_get_regulators(sensor);
3074 if (ret)
3075 return ret;
3076
3077 mutex_init(&sensor->lock);
3078
3079 ret = ov5640_check_chip_id(sensor);
3080 if (ret)
3081 goto entity_cleanup;
3082
3083 ret = ov5640_init_controls(sensor);
3084 if (ret)
3085 goto entity_cleanup;
3086
3087 ret = v4l2_async_register_subdev_sensor_common(&sensor->sd);
3088 if (ret)
3089 goto free_ctrls;
3090
3091 return 0;
3092
3093free_ctrls:
3094 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3095entity_cleanup:
3096 mutex_destroy(&sensor->lock);
3097 media_entity_cleanup(&sensor->sd.entity);
3098 return ret;
3099}
3100
3101static int ov5640_remove(struct i2c_client *client)
3102{
3103 struct v4l2_subdev *sd = i2c_get_clientdata(client);
3104 struct ov5640_dev *sensor = to_ov5640_dev(sd);
3105
3106 v4l2_async_unregister_subdev(&sensor->sd);
3107 mutex_destroy(&sensor->lock);
3108 media_entity_cleanup(&sensor->sd.entity);
3109 v4l2_ctrl_handler_free(&sensor->ctrls.handler);
3110
3111 return 0;
3112}
3113
3114static const struct i2c_device_id ov5640_id[] = {
3115 {"ov5640", 0},
3116 {},
3117};
3118MODULE_DEVICE_TABLE(i2c, ov5640_id);
3119
3120static const struct of_device_id ov5640_dt_ids[] = {
3121 { .compatible = "ovti,ov5640" },
3122 { }
3123};
3124MODULE_DEVICE_TABLE(of, ov5640_dt_ids);
3125
3126static struct i2c_driver ov5640_i2c_driver = {
3127 .driver = {
3128 .name = "ov5640",
3129 .of_match_table = ov5640_dt_ids,
3130 },
3131 .id_table = ov5640_id,
3132 .probe_new = ov5640_probe,
3133 .remove = ov5640_remove,
3134};
3135
3136module_i2c_driver(ov5640_i2c_driver);
3137
3138MODULE_DESCRIPTION("OV5640 MIPI Camera Subdev Driver");
3139MODULE_LICENSE("GPL");
3140