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