xref: /openbmc/linux/drivers/media/i2c/ov5645.c (revision 8cb5d748)
1 /*
2  * Driver for the OV5645 camera sensor.
3  *
4  * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
5  * Copyright (C) 2015 By Tech Design S.L. All Rights Reserved.
6  * Copyright (C) 2012-2013 Freescale Semiconductor, Inc. All Rights Reserved.
7  *
8  * Based on:
9  * - the OV5645 driver from QC msm-3.10 kernel on codeaurora.org:
10  *   https://us.codeaurora.org/cgit/quic/la/kernel/msm-3.10/tree/drivers/
11  *       media/platform/msm/camera_v2/sensor/ov5645.c?h=LA.BR.1.2.4_rb1.41
12  * - the OV5640 driver posted on linux-media:
13  *   https://www.mail-archive.com/linux-media%40vger.kernel.org/msg92671.html
14  */
15 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21 
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  */
27 
28 #include <linux/bitops.h>
29 #include <linux/clk.h>
30 #include <linux/delay.h>
31 #include <linux/device.h>
32 #include <linux/gpio/consumer.h>
33 #include <linux/i2c.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/of.h>
37 #include <linux/of_graph.h>
38 #include <linux/regulator/consumer.h>
39 #include <linux/slab.h>
40 #include <linux/types.h>
41 #include <media/v4l2-ctrls.h>
42 #include <media/v4l2-fwnode.h>
43 #include <media/v4l2-subdev.h>
44 
45 #define OV5645_VOLTAGE_ANALOG               2800000
46 #define OV5645_VOLTAGE_DIGITAL_CORE         1500000
47 #define OV5645_VOLTAGE_DIGITAL_IO           1800000
48 
49 #define OV5645_SYSTEM_CTRL0		0x3008
50 #define		OV5645_SYSTEM_CTRL0_START	0x02
51 #define		OV5645_SYSTEM_CTRL0_STOP	0x42
52 #define OV5645_CHIP_ID_HIGH		0x300a
53 #define		OV5645_CHIP_ID_HIGH_BYTE	0x56
54 #define OV5645_CHIP_ID_LOW		0x300b
55 #define		OV5645_CHIP_ID_LOW_BYTE		0x45
56 #define OV5645_AWB_MANUAL_CONTROL	0x3406
57 #define		OV5645_AWB_MANUAL_ENABLE	BIT(0)
58 #define OV5645_AEC_PK_MANUAL		0x3503
59 #define		OV5645_AEC_MANUAL_ENABLE	BIT(0)
60 #define		OV5645_AGC_MANUAL_ENABLE	BIT(1)
61 #define OV5645_TIMING_TC_REG20		0x3820
62 #define		OV5645_SENSOR_VFLIP		BIT(1)
63 #define		OV5645_ISP_VFLIP		BIT(2)
64 #define OV5645_TIMING_TC_REG21		0x3821
65 #define		OV5645_SENSOR_MIRROR		BIT(1)
66 #define OV5645_PRE_ISP_TEST_SETTING_1	0x503d
67 #define		OV5645_TEST_PATTERN_MASK	0x3
68 #define		OV5645_SET_TEST_PATTERN(x)	((x) & OV5645_TEST_PATTERN_MASK)
69 #define		OV5645_TEST_PATTERN_ENABLE	BIT(7)
70 #define OV5645_SDE_SAT_U		0x5583
71 #define OV5645_SDE_SAT_V		0x5584
72 
73 struct reg_value {
74 	u16 reg;
75 	u8 val;
76 };
77 
78 struct ov5645_mode_info {
79 	u32 width;
80 	u32 height;
81 	const struct reg_value *data;
82 	u32 data_size;
83 	u32 pixel_clock;
84 	u32 link_freq;
85 };
86 
87 struct ov5645 {
88 	struct i2c_client *i2c_client;
89 	struct device *dev;
90 	struct v4l2_subdev sd;
91 	struct media_pad pad;
92 	struct v4l2_fwnode_endpoint ep;
93 	struct v4l2_mbus_framefmt fmt;
94 	struct v4l2_rect crop;
95 	struct clk *xclk;
96 
97 	struct regulator *io_regulator;
98 	struct regulator *core_regulator;
99 	struct regulator *analog_regulator;
100 
101 	const struct ov5645_mode_info *current_mode;
102 
103 	struct v4l2_ctrl_handler ctrls;
104 	struct v4l2_ctrl *pixel_clock;
105 	struct v4l2_ctrl *link_freq;
106 
107 	/* Cached register values */
108 	u8 aec_pk_manual;
109 	u8 timing_tc_reg20;
110 	u8 timing_tc_reg21;
111 
112 	struct mutex power_lock; /* lock to protect power state */
113 	int power_count;
114 
115 	struct gpio_desc *enable_gpio;
116 	struct gpio_desc *rst_gpio;
117 };
118 
119 static inline struct ov5645 *to_ov5645(struct v4l2_subdev *sd)
120 {
121 	return container_of(sd, struct ov5645, sd);
122 }
123 
124 static const struct reg_value ov5645_global_init_setting[] = {
125 	{ 0x3103, 0x11 },
126 	{ 0x3008, 0x82 },
127 	{ 0x3008, 0x42 },
128 	{ 0x3103, 0x03 },
129 	{ 0x3503, 0x07 },
130 	{ 0x3002, 0x1c },
131 	{ 0x3006, 0xc3 },
132 	{ 0x300e, 0x45 },
133 	{ 0x3017, 0x00 },
134 	{ 0x3018, 0x00 },
135 	{ 0x302e, 0x0b },
136 	{ 0x3037, 0x13 },
137 	{ 0x3108, 0x01 },
138 	{ 0x3611, 0x06 },
139 	{ 0x3500, 0x00 },
140 	{ 0x3501, 0x01 },
141 	{ 0x3502, 0x00 },
142 	{ 0x350a, 0x00 },
143 	{ 0x350b, 0x3f },
144 	{ 0x3620, 0x33 },
145 	{ 0x3621, 0xe0 },
146 	{ 0x3622, 0x01 },
147 	{ 0x3630, 0x2e },
148 	{ 0x3631, 0x00 },
149 	{ 0x3632, 0x32 },
150 	{ 0x3633, 0x52 },
151 	{ 0x3634, 0x70 },
152 	{ 0x3635, 0x13 },
153 	{ 0x3636, 0x03 },
154 	{ 0x3703, 0x5a },
155 	{ 0x3704, 0xa0 },
156 	{ 0x3705, 0x1a },
157 	{ 0x3709, 0x12 },
158 	{ 0x370b, 0x61 },
159 	{ 0x370f, 0x10 },
160 	{ 0x3715, 0x78 },
161 	{ 0x3717, 0x01 },
162 	{ 0x371b, 0x20 },
163 	{ 0x3731, 0x12 },
164 	{ 0x3901, 0x0a },
165 	{ 0x3905, 0x02 },
166 	{ 0x3906, 0x10 },
167 	{ 0x3719, 0x86 },
168 	{ 0x3810, 0x00 },
169 	{ 0x3811, 0x10 },
170 	{ 0x3812, 0x00 },
171 	{ 0x3821, 0x01 },
172 	{ 0x3824, 0x01 },
173 	{ 0x3826, 0x03 },
174 	{ 0x3828, 0x08 },
175 	{ 0x3a19, 0xf8 },
176 	{ 0x3c01, 0x34 },
177 	{ 0x3c04, 0x28 },
178 	{ 0x3c05, 0x98 },
179 	{ 0x3c07, 0x07 },
180 	{ 0x3c09, 0xc2 },
181 	{ 0x3c0a, 0x9c },
182 	{ 0x3c0b, 0x40 },
183 	{ 0x3c01, 0x34 },
184 	{ 0x4001, 0x02 },
185 	{ 0x4514, 0x00 },
186 	{ 0x4520, 0xb0 },
187 	{ 0x460b, 0x37 },
188 	{ 0x460c, 0x20 },
189 	{ 0x4818, 0x01 },
190 	{ 0x481d, 0xf0 },
191 	{ 0x481f, 0x50 },
192 	{ 0x4823, 0x70 },
193 	{ 0x4831, 0x14 },
194 	{ 0x5000, 0xa7 },
195 	{ 0x5001, 0x83 },
196 	{ 0x501d, 0x00 },
197 	{ 0x501f, 0x00 },
198 	{ 0x503d, 0x00 },
199 	{ 0x505c, 0x30 },
200 	{ 0x5181, 0x59 },
201 	{ 0x5183, 0x00 },
202 	{ 0x5191, 0xf0 },
203 	{ 0x5192, 0x03 },
204 	{ 0x5684, 0x10 },
205 	{ 0x5685, 0xa0 },
206 	{ 0x5686, 0x0c },
207 	{ 0x5687, 0x78 },
208 	{ 0x5a00, 0x08 },
209 	{ 0x5a21, 0x00 },
210 	{ 0x5a24, 0x00 },
211 	{ 0x3008, 0x02 },
212 	{ 0x3503, 0x00 },
213 	{ 0x5180, 0xff },
214 	{ 0x5181, 0xf2 },
215 	{ 0x5182, 0x00 },
216 	{ 0x5183, 0x14 },
217 	{ 0x5184, 0x25 },
218 	{ 0x5185, 0x24 },
219 	{ 0x5186, 0x09 },
220 	{ 0x5187, 0x09 },
221 	{ 0x5188, 0x0a },
222 	{ 0x5189, 0x75 },
223 	{ 0x518a, 0x52 },
224 	{ 0x518b, 0xea },
225 	{ 0x518c, 0xa8 },
226 	{ 0x518d, 0x42 },
227 	{ 0x518e, 0x38 },
228 	{ 0x518f, 0x56 },
229 	{ 0x5190, 0x42 },
230 	{ 0x5191, 0xf8 },
231 	{ 0x5192, 0x04 },
232 	{ 0x5193, 0x70 },
233 	{ 0x5194, 0xf0 },
234 	{ 0x5195, 0xf0 },
235 	{ 0x5196, 0x03 },
236 	{ 0x5197, 0x01 },
237 	{ 0x5198, 0x04 },
238 	{ 0x5199, 0x12 },
239 	{ 0x519a, 0x04 },
240 	{ 0x519b, 0x00 },
241 	{ 0x519c, 0x06 },
242 	{ 0x519d, 0x82 },
243 	{ 0x519e, 0x38 },
244 	{ 0x5381, 0x1e },
245 	{ 0x5382, 0x5b },
246 	{ 0x5383, 0x08 },
247 	{ 0x5384, 0x0a },
248 	{ 0x5385, 0x7e },
249 	{ 0x5386, 0x88 },
250 	{ 0x5387, 0x7c },
251 	{ 0x5388, 0x6c },
252 	{ 0x5389, 0x10 },
253 	{ 0x538a, 0x01 },
254 	{ 0x538b, 0x98 },
255 	{ 0x5300, 0x08 },
256 	{ 0x5301, 0x30 },
257 	{ 0x5302, 0x10 },
258 	{ 0x5303, 0x00 },
259 	{ 0x5304, 0x08 },
260 	{ 0x5305, 0x30 },
261 	{ 0x5306, 0x08 },
262 	{ 0x5307, 0x16 },
263 	{ 0x5309, 0x08 },
264 	{ 0x530a, 0x30 },
265 	{ 0x530b, 0x04 },
266 	{ 0x530c, 0x06 },
267 	{ 0x5480, 0x01 },
268 	{ 0x5481, 0x08 },
269 	{ 0x5482, 0x14 },
270 	{ 0x5483, 0x28 },
271 	{ 0x5484, 0x51 },
272 	{ 0x5485, 0x65 },
273 	{ 0x5486, 0x71 },
274 	{ 0x5487, 0x7d },
275 	{ 0x5488, 0x87 },
276 	{ 0x5489, 0x91 },
277 	{ 0x548a, 0x9a },
278 	{ 0x548b, 0xaa },
279 	{ 0x548c, 0xb8 },
280 	{ 0x548d, 0xcd },
281 	{ 0x548e, 0xdd },
282 	{ 0x548f, 0xea },
283 	{ 0x5490, 0x1d },
284 	{ 0x5580, 0x02 },
285 	{ 0x5583, 0x40 },
286 	{ 0x5584, 0x10 },
287 	{ 0x5589, 0x10 },
288 	{ 0x558a, 0x00 },
289 	{ 0x558b, 0xf8 },
290 	{ 0x5800, 0x3f },
291 	{ 0x5801, 0x16 },
292 	{ 0x5802, 0x0e },
293 	{ 0x5803, 0x0d },
294 	{ 0x5804, 0x17 },
295 	{ 0x5805, 0x3f },
296 	{ 0x5806, 0x0b },
297 	{ 0x5807, 0x06 },
298 	{ 0x5808, 0x04 },
299 	{ 0x5809, 0x04 },
300 	{ 0x580a, 0x06 },
301 	{ 0x580b, 0x0b },
302 	{ 0x580c, 0x09 },
303 	{ 0x580d, 0x03 },
304 	{ 0x580e, 0x00 },
305 	{ 0x580f, 0x00 },
306 	{ 0x5810, 0x03 },
307 	{ 0x5811, 0x08 },
308 	{ 0x5812, 0x0a },
309 	{ 0x5813, 0x03 },
310 	{ 0x5814, 0x00 },
311 	{ 0x5815, 0x00 },
312 	{ 0x5816, 0x04 },
313 	{ 0x5817, 0x09 },
314 	{ 0x5818, 0x0f },
315 	{ 0x5819, 0x08 },
316 	{ 0x581a, 0x06 },
317 	{ 0x581b, 0x06 },
318 	{ 0x581c, 0x08 },
319 	{ 0x581d, 0x0c },
320 	{ 0x581e, 0x3f },
321 	{ 0x581f, 0x1e },
322 	{ 0x5820, 0x12 },
323 	{ 0x5821, 0x13 },
324 	{ 0x5822, 0x21 },
325 	{ 0x5823, 0x3f },
326 	{ 0x5824, 0x68 },
327 	{ 0x5825, 0x28 },
328 	{ 0x5826, 0x2c },
329 	{ 0x5827, 0x28 },
330 	{ 0x5828, 0x08 },
331 	{ 0x5829, 0x48 },
332 	{ 0x582a, 0x64 },
333 	{ 0x582b, 0x62 },
334 	{ 0x582c, 0x64 },
335 	{ 0x582d, 0x28 },
336 	{ 0x582e, 0x46 },
337 	{ 0x582f, 0x62 },
338 	{ 0x5830, 0x60 },
339 	{ 0x5831, 0x62 },
340 	{ 0x5832, 0x26 },
341 	{ 0x5833, 0x48 },
342 	{ 0x5834, 0x66 },
343 	{ 0x5835, 0x44 },
344 	{ 0x5836, 0x64 },
345 	{ 0x5837, 0x28 },
346 	{ 0x5838, 0x66 },
347 	{ 0x5839, 0x48 },
348 	{ 0x583a, 0x2c },
349 	{ 0x583b, 0x28 },
350 	{ 0x583c, 0x26 },
351 	{ 0x583d, 0xae },
352 	{ 0x5025, 0x00 },
353 	{ 0x3a0f, 0x30 },
354 	{ 0x3a10, 0x28 },
355 	{ 0x3a1b, 0x30 },
356 	{ 0x3a1e, 0x26 },
357 	{ 0x3a11, 0x60 },
358 	{ 0x3a1f, 0x14 },
359 	{ 0x0601, 0x02 },
360 	{ 0x3008, 0x42 },
361 	{ 0x3008, 0x02 }
362 };
363 
364 static const struct reg_value ov5645_setting_sxga[] = {
365 	{ 0x3612, 0xa9 },
366 	{ 0x3614, 0x50 },
367 	{ 0x3618, 0x00 },
368 	{ 0x3034, 0x18 },
369 	{ 0x3035, 0x21 },
370 	{ 0x3036, 0x70 },
371 	{ 0x3600, 0x09 },
372 	{ 0x3601, 0x43 },
373 	{ 0x3708, 0x66 },
374 	{ 0x370c, 0xc3 },
375 	{ 0x3800, 0x00 },
376 	{ 0x3801, 0x00 },
377 	{ 0x3802, 0x00 },
378 	{ 0x3803, 0x06 },
379 	{ 0x3804, 0x0a },
380 	{ 0x3805, 0x3f },
381 	{ 0x3806, 0x07 },
382 	{ 0x3807, 0x9d },
383 	{ 0x3808, 0x05 },
384 	{ 0x3809, 0x00 },
385 	{ 0x380a, 0x03 },
386 	{ 0x380b, 0xc0 },
387 	{ 0x380c, 0x07 },
388 	{ 0x380d, 0x68 },
389 	{ 0x380e, 0x03 },
390 	{ 0x380f, 0xd8 },
391 	{ 0x3813, 0x06 },
392 	{ 0x3814, 0x31 },
393 	{ 0x3815, 0x31 },
394 	{ 0x3820, 0x47 },
395 	{ 0x3a02, 0x03 },
396 	{ 0x3a03, 0xd8 },
397 	{ 0x3a08, 0x01 },
398 	{ 0x3a09, 0xf8 },
399 	{ 0x3a0a, 0x01 },
400 	{ 0x3a0b, 0xa4 },
401 	{ 0x3a0e, 0x02 },
402 	{ 0x3a0d, 0x02 },
403 	{ 0x3a14, 0x03 },
404 	{ 0x3a15, 0xd8 },
405 	{ 0x3a18, 0x00 },
406 	{ 0x4004, 0x02 },
407 	{ 0x4005, 0x18 },
408 	{ 0x4300, 0x32 },
409 	{ 0x4202, 0x00 }
410 };
411 
412 static const struct reg_value ov5645_setting_1080p[] = {
413 	{ 0x3612, 0xab },
414 	{ 0x3614, 0x50 },
415 	{ 0x3618, 0x04 },
416 	{ 0x3034, 0x18 },
417 	{ 0x3035, 0x11 },
418 	{ 0x3036, 0x54 },
419 	{ 0x3600, 0x08 },
420 	{ 0x3601, 0x33 },
421 	{ 0x3708, 0x63 },
422 	{ 0x370c, 0xc0 },
423 	{ 0x3800, 0x01 },
424 	{ 0x3801, 0x50 },
425 	{ 0x3802, 0x01 },
426 	{ 0x3803, 0xb2 },
427 	{ 0x3804, 0x08 },
428 	{ 0x3805, 0xef },
429 	{ 0x3806, 0x05 },
430 	{ 0x3807, 0xf1 },
431 	{ 0x3808, 0x07 },
432 	{ 0x3809, 0x80 },
433 	{ 0x380a, 0x04 },
434 	{ 0x380b, 0x38 },
435 	{ 0x380c, 0x09 },
436 	{ 0x380d, 0xc4 },
437 	{ 0x380e, 0x04 },
438 	{ 0x380f, 0x60 },
439 	{ 0x3813, 0x04 },
440 	{ 0x3814, 0x11 },
441 	{ 0x3815, 0x11 },
442 	{ 0x3820, 0x47 },
443 	{ 0x4514, 0x88 },
444 	{ 0x3a02, 0x04 },
445 	{ 0x3a03, 0x60 },
446 	{ 0x3a08, 0x01 },
447 	{ 0x3a09, 0x50 },
448 	{ 0x3a0a, 0x01 },
449 	{ 0x3a0b, 0x18 },
450 	{ 0x3a0e, 0x03 },
451 	{ 0x3a0d, 0x04 },
452 	{ 0x3a14, 0x04 },
453 	{ 0x3a15, 0x60 },
454 	{ 0x3a18, 0x00 },
455 	{ 0x4004, 0x06 },
456 	{ 0x4005, 0x18 },
457 	{ 0x4300, 0x32 },
458 	{ 0x4202, 0x00 },
459 	{ 0x4837, 0x0b }
460 };
461 
462 static const struct reg_value ov5645_setting_full[] = {
463 	{ 0x3612, 0xab },
464 	{ 0x3614, 0x50 },
465 	{ 0x3618, 0x04 },
466 	{ 0x3034, 0x18 },
467 	{ 0x3035, 0x11 },
468 	{ 0x3036, 0x54 },
469 	{ 0x3600, 0x08 },
470 	{ 0x3601, 0x33 },
471 	{ 0x3708, 0x63 },
472 	{ 0x370c, 0xc0 },
473 	{ 0x3800, 0x00 },
474 	{ 0x3801, 0x00 },
475 	{ 0x3802, 0x00 },
476 	{ 0x3803, 0x00 },
477 	{ 0x3804, 0x0a },
478 	{ 0x3805, 0x3f },
479 	{ 0x3806, 0x07 },
480 	{ 0x3807, 0x9f },
481 	{ 0x3808, 0x0a },
482 	{ 0x3809, 0x20 },
483 	{ 0x380a, 0x07 },
484 	{ 0x380b, 0x98 },
485 	{ 0x380c, 0x0b },
486 	{ 0x380d, 0x1c },
487 	{ 0x380e, 0x07 },
488 	{ 0x380f, 0xb0 },
489 	{ 0x3813, 0x06 },
490 	{ 0x3814, 0x11 },
491 	{ 0x3815, 0x11 },
492 	{ 0x3820, 0x47 },
493 	{ 0x4514, 0x88 },
494 	{ 0x3a02, 0x07 },
495 	{ 0x3a03, 0xb0 },
496 	{ 0x3a08, 0x01 },
497 	{ 0x3a09, 0x27 },
498 	{ 0x3a0a, 0x00 },
499 	{ 0x3a0b, 0xf6 },
500 	{ 0x3a0e, 0x06 },
501 	{ 0x3a0d, 0x08 },
502 	{ 0x3a14, 0x07 },
503 	{ 0x3a15, 0xb0 },
504 	{ 0x3a18, 0x01 },
505 	{ 0x4004, 0x06 },
506 	{ 0x4005, 0x18 },
507 	{ 0x4300, 0x32 },
508 	{ 0x4837, 0x0b },
509 	{ 0x4202, 0x00 }
510 };
511 
512 static const s64 link_freq[] = {
513 	222880000,
514 	334320000
515 };
516 
517 static const struct ov5645_mode_info ov5645_mode_info_data[] = {
518 	{
519 		.width = 1280,
520 		.height = 960,
521 		.data = ov5645_setting_sxga,
522 		.data_size = ARRAY_SIZE(ov5645_setting_sxga),
523 		.pixel_clock = 111440000,
524 		.link_freq = 0 /* an index in link_freq[] */
525 	},
526 	{
527 		.width = 1920,
528 		.height = 1080,
529 		.data = ov5645_setting_1080p,
530 		.data_size = ARRAY_SIZE(ov5645_setting_1080p),
531 		.pixel_clock = 167160000,
532 		.link_freq = 1 /* an index in link_freq[] */
533 	},
534 	{
535 		.width = 2592,
536 		.height = 1944,
537 		.data = ov5645_setting_full,
538 		.data_size = ARRAY_SIZE(ov5645_setting_full),
539 		.pixel_clock = 167160000,
540 		.link_freq = 1 /* an index in link_freq[] */
541 	},
542 };
543 
544 static int ov5645_regulators_enable(struct ov5645 *ov5645)
545 {
546 	int ret;
547 
548 	ret = regulator_enable(ov5645->io_regulator);
549 	if (ret < 0) {
550 		dev_err(ov5645->dev, "set io voltage failed\n");
551 		return ret;
552 	}
553 
554 	ret = regulator_enable(ov5645->analog_regulator);
555 	if (ret) {
556 		dev_err(ov5645->dev, "set analog voltage failed\n");
557 		goto err_disable_io;
558 	}
559 
560 	ret = regulator_enable(ov5645->core_regulator);
561 	if (ret) {
562 		dev_err(ov5645->dev, "set core voltage failed\n");
563 		goto err_disable_analog;
564 	}
565 
566 	return 0;
567 
568 err_disable_analog:
569 	regulator_disable(ov5645->analog_regulator);
570 err_disable_io:
571 	regulator_disable(ov5645->io_regulator);
572 
573 	return ret;
574 }
575 
576 static void ov5645_regulators_disable(struct ov5645 *ov5645)
577 {
578 	int ret;
579 
580 	ret = regulator_disable(ov5645->core_regulator);
581 	if (ret < 0)
582 		dev_err(ov5645->dev, "core regulator disable failed\n");
583 
584 	ret = regulator_disable(ov5645->analog_regulator);
585 	if (ret < 0)
586 		dev_err(ov5645->dev, "analog regulator disable failed\n");
587 
588 	ret = regulator_disable(ov5645->io_regulator);
589 	if (ret < 0)
590 		dev_err(ov5645->dev, "io regulator disable failed\n");
591 }
592 
593 static int ov5645_write_reg(struct ov5645 *ov5645, u16 reg, u8 val)
594 {
595 	u8 regbuf[3];
596 	int ret;
597 
598 	regbuf[0] = reg >> 8;
599 	regbuf[1] = reg & 0xff;
600 	regbuf[2] = val;
601 
602 	ret = i2c_master_send(ov5645->i2c_client, regbuf, 3);
603 	if (ret < 0)
604 		dev_err(ov5645->dev, "%s: write reg error %d: reg=%x, val=%x\n",
605 			__func__, ret, reg, val);
606 
607 	return ret;
608 }
609 
610 static int ov5645_read_reg(struct ov5645 *ov5645, u16 reg, u8 *val)
611 {
612 	u8 regbuf[2];
613 	int ret;
614 
615 	regbuf[0] = reg >> 8;
616 	regbuf[1] = reg & 0xff;
617 
618 	ret = i2c_master_send(ov5645->i2c_client, regbuf, 2);
619 	if (ret < 0) {
620 		dev_err(ov5645->dev, "%s: write reg error %d: reg=%x\n",
621 			__func__, ret, reg);
622 		return ret;
623 	}
624 
625 	ret = i2c_master_recv(ov5645->i2c_client, val, 1);
626 	if (ret < 0) {
627 		dev_err(ov5645->dev, "%s: read reg error %d: reg=%x\n",
628 			__func__, ret, reg);
629 		return ret;
630 	}
631 
632 	return 0;
633 }
634 
635 static int ov5645_set_aec_mode(struct ov5645 *ov5645, u32 mode)
636 {
637 	u8 val = ov5645->aec_pk_manual;
638 	int ret;
639 
640 	if (mode == V4L2_EXPOSURE_AUTO)
641 		val &= ~OV5645_AEC_MANUAL_ENABLE;
642 	else /* V4L2_EXPOSURE_MANUAL */
643 		val |= OV5645_AEC_MANUAL_ENABLE;
644 
645 	ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
646 	if (!ret)
647 		ov5645->aec_pk_manual = val;
648 
649 	return ret;
650 }
651 
652 static int ov5645_set_agc_mode(struct ov5645 *ov5645, u32 enable)
653 {
654 	u8 val = ov5645->aec_pk_manual;
655 	int ret;
656 
657 	if (enable)
658 		val &= ~OV5645_AGC_MANUAL_ENABLE;
659 	else
660 		val |= OV5645_AGC_MANUAL_ENABLE;
661 
662 	ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
663 	if (!ret)
664 		ov5645->aec_pk_manual = val;
665 
666 	return ret;
667 }
668 
669 static int ov5645_set_register_array(struct ov5645 *ov5645,
670 				     const struct reg_value *settings,
671 				     unsigned int num_settings)
672 {
673 	unsigned int i;
674 	int ret;
675 
676 	for (i = 0; i < num_settings; ++i, ++settings) {
677 		ret = ov5645_write_reg(ov5645, settings->reg, settings->val);
678 		if (ret < 0)
679 			return ret;
680 	}
681 
682 	return 0;
683 }
684 
685 static int ov5645_set_power_on(struct ov5645 *ov5645)
686 {
687 	int ret;
688 
689 	ret = ov5645_regulators_enable(ov5645);
690 	if (ret < 0) {
691 		return ret;
692 	}
693 
694 	ret = clk_prepare_enable(ov5645->xclk);
695 	if (ret < 0) {
696 		dev_err(ov5645->dev, "clk prepare enable failed\n");
697 		ov5645_regulators_disable(ov5645);
698 		return ret;
699 	}
700 
701 	usleep_range(5000, 15000);
702 	gpiod_set_value_cansleep(ov5645->enable_gpio, 1);
703 
704 	usleep_range(1000, 2000);
705 	gpiod_set_value_cansleep(ov5645->rst_gpio, 0);
706 
707 	msleep(20);
708 
709 	return 0;
710 }
711 
712 static void ov5645_set_power_off(struct ov5645 *ov5645)
713 {
714 	gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
715 	gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
716 	clk_disable_unprepare(ov5645->xclk);
717 	ov5645_regulators_disable(ov5645);
718 }
719 
720 static int ov5645_s_power(struct v4l2_subdev *sd, int on)
721 {
722 	struct ov5645 *ov5645 = to_ov5645(sd);
723 	int ret = 0;
724 
725 	mutex_lock(&ov5645->power_lock);
726 
727 	/* If the power count is modified from 0 to != 0 or from != 0 to 0,
728 	 * update the power state.
729 	 */
730 	if (ov5645->power_count == !on) {
731 		if (on) {
732 			ret = ov5645_set_power_on(ov5645);
733 			if (ret < 0)
734 				goto exit;
735 
736 			ret = ov5645_set_register_array(ov5645,
737 					ov5645_global_init_setting,
738 					ARRAY_SIZE(ov5645_global_init_setting));
739 			if (ret < 0) {
740 				dev_err(ov5645->dev,
741 					"could not set init registers\n");
742 				ov5645_set_power_off(ov5645);
743 				goto exit;
744 			}
745 
746 			ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
747 					       OV5645_SYSTEM_CTRL0_STOP);
748 			if (ret < 0) {
749 				ov5645_set_power_off(ov5645);
750 				goto exit;
751 			}
752 		} else {
753 			ov5645_set_power_off(ov5645);
754 		}
755 	}
756 
757 	/* Update the power count. */
758 	ov5645->power_count += on ? 1 : -1;
759 	WARN_ON(ov5645->power_count < 0);
760 
761 exit:
762 	mutex_unlock(&ov5645->power_lock);
763 
764 	return ret;
765 }
766 
767 static int ov5645_set_saturation(struct ov5645 *ov5645, s32 value)
768 {
769 	u32 reg_value = (value * 0x10) + 0x40;
770 	int ret;
771 
772 	ret = ov5645_write_reg(ov5645, OV5645_SDE_SAT_U, reg_value);
773 	if (ret < 0)
774 		return ret;
775 
776 	return ov5645_write_reg(ov5645, OV5645_SDE_SAT_V, reg_value);
777 }
778 
779 static int ov5645_set_hflip(struct ov5645 *ov5645, s32 value)
780 {
781 	u8 val = ov5645->timing_tc_reg21;
782 	int ret;
783 
784 	if (value == 0)
785 		val &= ~(OV5645_SENSOR_MIRROR);
786 	else
787 		val |= (OV5645_SENSOR_MIRROR);
788 
789 	ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG21, val);
790 	if (!ret)
791 		ov5645->timing_tc_reg21 = val;
792 
793 	return ret;
794 }
795 
796 static int ov5645_set_vflip(struct ov5645 *ov5645, s32 value)
797 {
798 	u8 val = ov5645->timing_tc_reg20;
799 	int ret;
800 
801 	if (value == 0)
802 		val |= (OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
803 	else
804 		val &= ~(OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
805 
806 	ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG20, val);
807 	if (!ret)
808 		ov5645->timing_tc_reg20 = val;
809 
810 	return ret;
811 }
812 
813 static int ov5645_set_test_pattern(struct ov5645 *ov5645, s32 value)
814 {
815 	u8 val = 0;
816 
817 	if (value) {
818 		val = OV5645_SET_TEST_PATTERN(value - 1);
819 		val |= OV5645_TEST_PATTERN_ENABLE;
820 	}
821 
822 	return ov5645_write_reg(ov5645, OV5645_PRE_ISP_TEST_SETTING_1, val);
823 }
824 
825 static const char * const ov5645_test_pattern_menu[] = {
826 	"Disabled",
827 	"Vertical Color Bars",
828 	"Pseudo-Random Data",
829 	"Color Square",
830 	"Black Image",
831 };
832 
833 static int ov5645_set_awb(struct ov5645 *ov5645, s32 enable_auto)
834 {
835 	u8 val = 0;
836 
837 	if (!enable_auto)
838 		val = OV5645_AWB_MANUAL_ENABLE;
839 
840 	return ov5645_write_reg(ov5645, OV5645_AWB_MANUAL_CONTROL, val);
841 }
842 
843 static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
844 {
845 	struct ov5645 *ov5645 = container_of(ctrl->handler,
846 					     struct ov5645, ctrls);
847 	int ret;
848 
849 	mutex_lock(&ov5645->power_lock);
850 	if (!ov5645->power_count) {
851 		mutex_unlock(&ov5645->power_lock);
852 		return 0;
853 	}
854 
855 	switch (ctrl->id) {
856 	case V4L2_CID_SATURATION:
857 		ret = ov5645_set_saturation(ov5645, ctrl->val);
858 		break;
859 	case V4L2_CID_AUTO_WHITE_BALANCE:
860 		ret = ov5645_set_awb(ov5645, ctrl->val);
861 		break;
862 	case V4L2_CID_AUTOGAIN:
863 		ret = ov5645_set_agc_mode(ov5645, ctrl->val);
864 		break;
865 	case V4L2_CID_EXPOSURE_AUTO:
866 		ret = ov5645_set_aec_mode(ov5645, ctrl->val);
867 		break;
868 	case V4L2_CID_TEST_PATTERN:
869 		ret = ov5645_set_test_pattern(ov5645, ctrl->val);
870 		break;
871 	case V4L2_CID_HFLIP:
872 		ret = ov5645_set_hflip(ov5645, ctrl->val);
873 		break;
874 	case V4L2_CID_VFLIP:
875 		ret = ov5645_set_vflip(ov5645, ctrl->val);
876 		break;
877 	default:
878 		ret = -EINVAL;
879 		break;
880 	}
881 
882 	mutex_unlock(&ov5645->power_lock);
883 
884 	return ret;
885 }
886 
887 static struct v4l2_ctrl_ops ov5645_ctrl_ops = {
888 	.s_ctrl = ov5645_s_ctrl,
889 };
890 
891 static int ov5645_enum_mbus_code(struct v4l2_subdev *sd,
892 				 struct v4l2_subdev_pad_config *cfg,
893 				 struct v4l2_subdev_mbus_code_enum *code)
894 {
895 	if (code->index > 0)
896 		return -EINVAL;
897 
898 	code->code = MEDIA_BUS_FMT_UYVY8_2X8;
899 
900 	return 0;
901 }
902 
903 static int ov5645_enum_frame_size(struct v4l2_subdev *subdev,
904 				  struct v4l2_subdev_pad_config *cfg,
905 				  struct v4l2_subdev_frame_size_enum *fse)
906 {
907 	if (fse->code != MEDIA_BUS_FMT_UYVY8_2X8)
908 		return -EINVAL;
909 
910 	if (fse->index >= ARRAY_SIZE(ov5645_mode_info_data))
911 		return -EINVAL;
912 
913 	fse->min_width = ov5645_mode_info_data[fse->index].width;
914 	fse->max_width = ov5645_mode_info_data[fse->index].width;
915 	fse->min_height = ov5645_mode_info_data[fse->index].height;
916 	fse->max_height = ov5645_mode_info_data[fse->index].height;
917 
918 	return 0;
919 }
920 
921 static struct v4l2_mbus_framefmt *
922 __ov5645_get_pad_format(struct ov5645 *ov5645,
923 			struct v4l2_subdev_pad_config *cfg,
924 			unsigned int pad,
925 			enum v4l2_subdev_format_whence which)
926 {
927 	switch (which) {
928 	case V4L2_SUBDEV_FORMAT_TRY:
929 		return v4l2_subdev_get_try_format(&ov5645->sd, cfg, pad);
930 	case V4L2_SUBDEV_FORMAT_ACTIVE:
931 		return &ov5645->fmt;
932 	default:
933 		return NULL;
934 	}
935 }
936 
937 static int ov5645_get_format(struct v4l2_subdev *sd,
938 			     struct v4l2_subdev_pad_config *cfg,
939 			     struct v4l2_subdev_format *format)
940 {
941 	struct ov5645 *ov5645 = to_ov5645(sd);
942 
943 	format->format = *__ov5645_get_pad_format(ov5645, cfg, format->pad,
944 						  format->which);
945 	return 0;
946 }
947 
948 static struct v4l2_rect *
949 __ov5645_get_pad_crop(struct ov5645 *ov5645, struct v4l2_subdev_pad_config *cfg,
950 		      unsigned int pad, enum v4l2_subdev_format_whence which)
951 {
952 	switch (which) {
953 	case V4L2_SUBDEV_FORMAT_TRY:
954 		return v4l2_subdev_get_try_crop(&ov5645->sd, cfg, pad);
955 	case V4L2_SUBDEV_FORMAT_ACTIVE:
956 		return &ov5645->crop;
957 	default:
958 		return NULL;
959 	}
960 }
961 
962 static const struct ov5645_mode_info *
963 ov5645_find_nearest_mode(unsigned int width, unsigned int height)
964 {
965 	int i;
966 
967 	for (i = ARRAY_SIZE(ov5645_mode_info_data) - 1; i >= 0; i--) {
968 		if (ov5645_mode_info_data[i].width <= width &&
969 		    ov5645_mode_info_data[i].height <= height)
970 			break;
971 	}
972 
973 	if (i < 0)
974 		i = 0;
975 
976 	return &ov5645_mode_info_data[i];
977 }
978 
979 static int ov5645_set_format(struct v4l2_subdev *sd,
980 			     struct v4l2_subdev_pad_config *cfg,
981 			     struct v4l2_subdev_format *format)
982 {
983 	struct ov5645 *ov5645 = to_ov5645(sd);
984 	struct v4l2_mbus_framefmt *__format;
985 	struct v4l2_rect *__crop;
986 	const struct ov5645_mode_info *new_mode;
987 	int ret;
988 
989 	__crop = __ov5645_get_pad_crop(ov5645, cfg, format->pad,
990 			format->which);
991 
992 	new_mode = ov5645_find_nearest_mode(format->format.width,
993 					    format->format.height);
994 	__crop->width = new_mode->width;
995 	__crop->height = new_mode->height;
996 
997 	if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
998 		ret = v4l2_ctrl_s_ctrl_int64(ov5645->pixel_clock,
999 					     new_mode->pixel_clock);
1000 		if (ret < 0)
1001 			return ret;
1002 
1003 		ret = v4l2_ctrl_s_ctrl(ov5645->link_freq,
1004 				       new_mode->link_freq);
1005 		if (ret < 0)
1006 			return ret;
1007 
1008 		ov5645->current_mode = new_mode;
1009 	}
1010 
1011 	__format = __ov5645_get_pad_format(ov5645, cfg, format->pad,
1012 			format->which);
1013 	__format->width = __crop->width;
1014 	__format->height = __crop->height;
1015 	__format->code = MEDIA_BUS_FMT_UYVY8_2X8;
1016 	__format->field = V4L2_FIELD_NONE;
1017 	__format->colorspace = V4L2_COLORSPACE_SRGB;
1018 
1019 	format->format = *__format;
1020 
1021 	return 0;
1022 }
1023 
1024 static int ov5645_entity_init_cfg(struct v4l2_subdev *subdev,
1025 				  struct v4l2_subdev_pad_config *cfg)
1026 {
1027 	struct v4l2_subdev_format fmt = { 0 };
1028 
1029 	fmt.which = cfg ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
1030 	fmt.format.width = 1920;
1031 	fmt.format.height = 1080;
1032 
1033 	ov5645_set_format(subdev, cfg, &fmt);
1034 
1035 	return 0;
1036 }
1037 
1038 static int ov5645_get_selection(struct v4l2_subdev *sd,
1039 			   struct v4l2_subdev_pad_config *cfg,
1040 			   struct v4l2_subdev_selection *sel)
1041 {
1042 	struct ov5645 *ov5645 = to_ov5645(sd);
1043 
1044 	if (sel->target != V4L2_SEL_TGT_CROP)
1045 		return -EINVAL;
1046 
1047 	sel->r = *__ov5645_get_pad_crop(ov5645, cfg, sel->pad,
1048 					sel->which);
1049 	return 0;
1050 }
1051 
1052 static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
1053 {
1054 	struct ov5645 *ov5645 = to_ov5645(subdev);
1055 	int ret;
1056 
1057 	if (enable) {
1058 		ret = ov5645_set_register_array(ov5645,
1059 					ov5645->current_mode->data,
1060 					ov5645->current_mode->data_size);
1061 		if (ret < 0) {
1062 			dev_err(ov5645->dev, "could not set mode %dx%d\n",
1063 				ov5645->current_mode->width,
1064 				ov5645->current_mode->height);
1065 			return ret;
1066 		}
1067 		ret = v4l2_ctrl_handler_setup(&ov5645->ctrls);
1068 		if (ret < 0) {
1069 			dev_err(ov5645->dev, "could not sync v4l2 controls\n");
1070 			return ret;
1071 		}
1072 		ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
1073 				       OV5645_SYSTEM_CTRL0_START);
1074 		if (ret < 0)
1075 			return ret;
1076 	} else {
1077 		ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
1078 				       OV5645_SYSTEM_CTRL0_STOP);
1079 		if (ret < 0)
1080 			return ret;
1081 	}
1082 
1083 	return 0;
1084 }
1085 
1086 static const struct v4l2_subdev_core_ops ov5645_core_ops = {
1087 	.s_power = ov5645_s_power,
1088 };
1089 
1090 static const struct v4l2_subdev_video_ops ov5645_video_ops = {
1091 	.s_stream = ov5645_s_stream,
1092 };
1093 
1094 static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
1095 	.init_cfg = ov5645_entity_init_cfg,
1096 	.enum_mbus_code = ov5645_enum_mbus_code,
1097 	.enum_frame_size = ov5645_enum_frame_size,
1098 	.get_fmt = ov5645_get_format,
1099 	.set_fmt = ov5645_set_format,
1100 	.get_selection = ov5645_get_selection,
1101 };
1102 
1103 static const struct v4l2_subdev_ops ov5645_subdev_ops = {
1104 	.core = &ov5645_core_ops,
1105 	.video = &ov5645_video_ops,
1106 	.pad = &ov5645_subdev_pad_ops,
1107 };
1108 
1109 static int ov5645_probe(struct i2c_client *client,
1110 			const struct i2c_device_id *id)
1111 {
1112 	struct device *dev = &client->dev;
1113 	struct device_node *endpoint;
1114 	struct ov5645 *ov5645;
1115 	u8 chip_id_high, chip_id_low;
1116 	u32 xclk_freq;
1117 	int ret;
1118 
1119 	ov5645 = devm_kzalloc(dev, sizeof(struct ov5645), GFP_KERNEL);
1120 	if (!ov5645)
1121 		return -ENOMEM;
1122 
1123 	ov5645->i2c_client = client;
1124 	ov5645->dev = dev;
1125 
1126 	endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
1127 	if (!endpoint) {
1128 		dev_err(dev, "endpoint node not found\n");
1129 		return -EINVAL;
1130 	}
1131 
1132 	ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
1133 					 &ov5645->ep);
1134 	if (ret < 0) {
1135 		dev_err(dev, "parsing endpoint node failed\n");
1136 		return ret;
1137 	}
1138 
1139 	of_node_put(endpoint);
1140 
1141 	if (ov5645->ep.bus_type != V4L2_MBUS_CSI2) {
1142 		dev_err(dev, "invalid bus type, must be CSI2\n");
1143 		return -EINVAL;
1144 	}
1145 
1146 	/* get system clock (xclk) */
1147 	ov5645->xclk = devm_clk_get(dev, "xclk");
1148 	if (IS_ERR(ov5645->xclk)) {
1149 		dev_err(dev, "could not get xclk");
1150 		return PTR_ERR(ov5645->xclk);
1151 	}
1152 
1153 	ret = of_property_read_u32(dev->of_node, "clock-frequency", &xclk_freq);
1154 	if (ret) {
1155 		dev_err(dev, "could not get xclk frequency\n");
1156 		return ret;
1157 	}
1158 
1159 	if (xclk_freq != 23880000) {
1160 		dev_err(dev, "external clock frequency %u is not supported\n",
1161 			xclk_freq);
1162 		return -EINVAL;
1163 	}
1164 
1165 	ret = clk_set_rate(ov5645->xclk, xclk_freq);
1166 	if (ret) {
1167 		dev_err(dev, "could not set xclk frequency\n");
1168 		return ret;
1169 	}
1170 
1171 	ov5645->io_regulator = devm_regulator_get(dev, "vdddo");
1172 	if (IS_ERR(ov5645->io_regulator)) {
1173 		dev_err(dev, "cannot get io regulator\n");
1174 		return PTR_ERR(ov5645->io_regulator);
1175 	}
1176 
1177 	ret = regulator_set_voltage(ov5645->io_regulator,
1178 				    OV5645_VOLTAGE_DIGITAL_IO,
1179 				    OV5645_VOLTAGE_DIGITAL_IO);
1180 	if (ret < 0) {
1181 		dev_err(dev, "cannot set io voltage\n");
1182 		return ret;
1183 	}
1184 
1185 	ov5645->core_regulator = devm_regulator_get(dev, "vddd");
1186 	if (IS_ERR(ov5645->core_regulator)) {
1187 		dev_err(dev, "cannot get core regulator\n");
1188 		return PTR_ERR(ov5645->core_regulator);
1189 	}
1190 
1191 	ret = regulator_set_voltage(ov5645->core_regulator,
1192 				    OV5645_VOLTAGE_DIGITAL_CORE,
1193 				    OV5645_VOLTAGE_DIGITAL_CORE);
1194 	if (ret < 0) {
1195 		dev_err(dev, "cannot set core voltage\n");
1196 		return ret;
1197 	}
1198 
1199 	ov5645->analog_regulator = devm_regulator_get(dev, "vdda");
1200 	if (IS_ERR(ov5645->analog_regulator)) {
1201 		dev_err(dev, "cannot get analog regulator\n");
1202 		return PTR_ERR(ov5645->analog_regulator);
1203 	}
1204 
1205 	ret = regulator_set_voltage(ov5645->analog_regulator,
1206 				    OV5645_VOLTAGE_ANALOG,
1207 				    OV5645_VOLTAGE_ANALOG);
1208 	if (ret < 0) {
1209 		dev_err(dev, "cannot set analog voltage\n");
1210 		return ret;
1211 	}
1212 
1213 	ov5645->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
1214 	if (IS_ERR(ov5645->enable_gpio)) {
1215 		dev_err(dev, "cannot get enable gpio\n");
1216 		return PTR_ERR(ov5645->enable_gpio);
1217 	}
1218 
1219 	ov5645->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1220 	if (IS_ERR(ov5645->rst_gpio)) {
1221 		dev_err(dev, "cannot get reset gpio\n");
1222 		return PTR_ERR(ov5645->rst_gpio);
1223 	}
1224 
1225 	mutex_init(&ov5645->power_lock);
1226 
1227 	v4l2_ctrl_handler_init(&ov5645->ctrls, 9);
1228 	v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1229 			  V4L2_CID_SATURATION, -4, 4, 1, 0);
1230 	v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1231 			  V4L2_CID_HFLIP, 0, 1, 1, 0);
1232 	v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1233 			  V4L2_CID_VFLIP, 0, 1, 1, 0);
1234 	v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1235 			  V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1236 	v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
1237 			  V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
1238 	v4l2_ctrl_new_std_menu(&ov5645->ctrls, &ov5645_ctrl_ops,
1239 			       V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
1240 			       0, V4L2_EXPOSURE_AUTO);
1241 	v4l2_ctrl_new_std_menu_items(&ov5645->ctrls, &ov5645_ctrl_ops,
1242 				     V4L2_CID_TEST_PATTERN,
1243 				     ARRAY_SIZE(ov5645_test_pattern_menu) - 1,
1244 				     0, 0, ov5645_test_pattern_menu);
1245 	ov5645->pixel_clock = v4l2_ctrl_new_std(&ov5645->ctrls,
1246 						&ov5645_ctrl_ops,
1247 						V4L2_CID_PIXEL_RATE,
1248 						1, INT_MAX, 1, 1);
1249 	ov5645->link_freq = v4l2_ctrl_new_int_menu(&ov5645->ctrls,
1250 						   &ov5645_ctrl_ops,
1251 						   V4L2_CID_LINK_FREQ,
1252 						   ARRAY_SIZE(link_freq) - 1,
1253 						   0, link_freq);
1254 	if (ov5645->link_freq)
1255 		ov5645->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1256 
1257 	ov5645->sd.ctrl_handler = &ov5645->ctrls;
1258 
1259 	if (ov5645->ctrls.error) {
1260 		dev_err(dev, "%s: control initialization error %d\n",
1261 		       __func__, ov5645->ctrls.error);
1262 		ret = ov5645->ctrls.error;
1263 		goto free_ctrl;
1264 	}
1265 
1266 	v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
1267 	ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1268 	ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
1269 	ov5645->sd.dev = &client->dev;
1270 	ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1271 
1272 	ret = media_entity_pads_init(&ov5645->sd.entity, 1, &ov5645->pad);
1273 	if (ret < 0) {
1274 		dev_err(dev, "could not register media entity\n");
1275 		goto free_ctrl;
1276 	}
1277 
1278 	ret = ov5645_s_power(&ov5645->sd, true);
1279 	if (ret < 0) {
1280 		dev_err(dev, "could not power up OV5645\n");
1281 		goto free_entity;
1282 	}
1283 
1284 	ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high);
1285 	if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) {
1286 		dev_err(dev, "could not read ID high\n");
1287 		ret = -ENODEV;
1288 		goto power_down;
1289 	}
1290 	ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_LOW, &chip_id_low);
1291 	if (ret < 0 || chip_id_low != OV5645_CHIP_ID_LOW_BYTE) {
1292 		dev_err(dev, "could not read ID low\n");
1293 		ret = -ENODEV;
1294 		goto power_down;
1295 	}
1296 
1297 	dev_info(dev, "OV5645 detected at address 0x%02x\n", client->addr);
1298 
1299 	ret = ov5645_read_reg(ov5645, OV5645_AEC_PK_MANUAL,
1300 			      &ov5645->aec_pk_manual);
1301 	if (ret < 0) {
1302 		dev_err(dev, "could not read AEC/AGC mode\n");
1303 		ret = -ENODEV;
1304 		goto power_down;
1305 	}
1306 
1307 	ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG20,
1308 			      &ov5645->timing_tc_reg20);
1309 	if (ret < 0) {
1310 		dev_err(dev, "could not read vflip value\n");
1311 		ret = -ENODEV;
1312 		goto power_down;
1313 	}
1314 
1315 	ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG21,
1316 			      &ov5645->timing_tc_reg21);
1317 	if (ret < 0) {
1318 		dev_err(dev, "could not read hflip value\n");
1319 		ret = -ENODEV;
1320 		goto power_down;
1321 	}
1322 
1323 	ov5645_s_power(&ov5645->sd, false);
1324 
1325 	ret = v4l2_async_register_subdev(&ov5645->sd);
1326 	if (ret < 0) {
1327 		dev_err(dev, "could not register v4l2 device\n");
1328 		goto free_entity;
1329 	}
1330 
1331 	ov5645_entity_init_cfg(&ov5645->sd, NULL);
1332 
1333 	return 0;
1334 
1335 power_down:
1336 	ov5645_s_power(&ov5645->sd, false);
1337 free_entity:
1338 	media_entity_cleanup(&ov5645->sd.entity);
1339 free_ctrl:
1340 	v4l2_ctrl_handler_free(&ov5645->ctrls);
1341 	mutex_destroy(&ov5645->power_lock);
1342 
1343 	return ret;
1344 }
1345 
1346 static int ov5645_remove(struct i2c_client *client)
1347 {
1348 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1349 	struct ov5645 *ov5645 = to_ov5645(sd);
1350 
1351 	v4l2_async_unregister_subdev(&ov5645->sd);
1352 	media_entity_cleanup(&ov5645->sd.entity);
1353 	v4l2_ctrl_handler_free(&ov5645->ctrls);
1354 	mutex_destroy(&ov5645->power_lock);
1355 
1356 	return 0;
1357 }
1358 
1359 static const struct i2c_device_id ov5645_id[] = {
1360 	{ "ov5645", 0 },
1361 	{}
1362 };
1363 MODULE_DEVICE_TABLE(i2c, ov5645_id);
1364 
1365 static const struct of_device_id ov5645_of_match[] = {
1366 	{ .compatible = "ovti,ov5645" },
1367 	{ /* sentinel */ }
1368 };
1369 MODULE_DEVICE_TABLE(of, ov5645_of_match);
1370 
1371 static struct i2c_driver ov5645_i2c_driver = {
1372 	.driver = {
1373 		.of_match_table = of_match_ptr(ov5645_of_match),
1374 		.name  = "ov5645",
1375 	},
1376 	.probe  = ov5645_probe,
1377 	.remove = ov5645_remove,
1378 	.id_table = ov5645_id,
1379 };
1380 
1381 module_i2c_driver(ov5645_i2c_driver);
1382 
1383 MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
1384 MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
1385 MODULE_LICENSE("GPL v2");
1386