blob: 349a4ad3ccc157488c91ce36626f9089e4fda758 [file] [log] [blame]
Bastian Hechtb52a8512011-06-24 07:57:36 -03001/*
2 * Driver for OV5642 CMOS Image Sensor from Omnivision
3 *
4 * Copyright (C) 2011, Bastian Hecht <hechtb@gmail.com>
5 *
6 * Based on Sony IMX074 Camera Driver
7 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
8 *
9 * Based on Omnivision OV7670 Camera Driver
10 * Copyright (C) 2006-7 Jonathan Corbet <corbet@lwn.net>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#include <linux/delay.h>
18#include <linux/i2c.h>
19#include <linux/slab.h>
20#include <linux/videodev2.h>
Stephen Rothwelld208c972011-07-29 15:30:00 +100021#include <linux/module.h>
Bastian Hechtb52a8512011-06-24 07:57:36 -030022
23#include <media/soc_camera.h>
24#include <media/soc_mediabus.h>
25#include <media/v4l2-chip-ident.h>
26#include <media/v4l2-subdev.h>
27
28/* OV5642 registers */
29#define REG_CHIP_ID_HIGH 0x300a
30#define REG_CHIP_ID_LOW 0x300b
31
32#define REG_WINDOW_START_X_HIGH 0x3800
33#define REG_WINDOW_START_X_LOW 0x3801
34#define REG_WINDOW_START_Y_HIGH 0x3802
35#define REG_WINDOW_START_Y_LOW 0x3803
36#define REG_WINDOW_WIDTH_HIGH 0x3804
37#define REG_WINDOW_WIDTH_LOW 0x3805
38#define REG_WINDOW_HEIGHT_HIGH 0x3806
39#define REG_WINDOW_HEIGHT_LOW 0x3807
40#define REG_OUT_WIDTH_HIGH 0x3808
41#define REG_OUT_WIDTH_LOW 0x3809
42#define REG_OUT_HEIGHT_HIGH 0x380a
43#define REG_OUT_HEIGHT_LOW 0x380b
44#define REG_OUT_TOTAL_WIDTH_HIGH 0x380c
45#define REG_OUT_TOTAL_WIDTH_LOW 0x380d
46#define REG_OUT_TOTAL_HEIGHT_HIGH 0x380e
47#define REG_OUT_TOTAL_HEIGHT_LOW 0x380f
48
49/*
50 * define standard resolution.
51 * Works currently only for up to 720 lines
52 * eg. 320x240, 640x480, 800x600, 1280x720, 2048x720
53 */
54
55#define OV5642_WIDTH 1280
56#define OV5642_HEIGHT 720
57#define OV5642_TOTAL_WIDTH 3200
58#define OV5642_TOTAL_HEIGHT 2000
59#define OV5642_SENSOR_SIZE_X 2592
60#define OV5642_SENSOR_SIZE_Y 1944
61
62struct regval_list {
63 u16 reg_num;
64 u8 value;
65};
66
67static struct regval_list ov5642_default_regs_init[] = {
68 { 0x3103, 0x93 },
69 { 0x3008, 0x82 },
70 { 0x3017, 0x7f },
71 { 0x3018, 0xfc },
72 { 0x3810, 0xc2 },
73 { 0x3615, 0xf0 },
74 { 0x3000, 0x0 },
75 { 0x3001, 0x0 },
76 { 0x3002, 0x0 },
77 { 0x3003, 0x0 },
78 { 0x3004, 0xff },
79 { 0x3030, 0x2b },
80 { 0x3011, 0x8 },
81 { 0x3010, 0x10 },
82 { 0x3604, 0x60 },
83 { 0x3622, 0x60 },
84 { 0x3621, 0x9 },
85 { 0x3709, 0x0 },
86 { 0x4000, 0x21 },
87 { 0x401d, 0x22 },
88 { 0x3600, 0x54 },
89 { 0x3605, 0x4 },
90 { 0x3606, 0x3f },
91 { 0x3c01, 0x80 },
92 { 0x300d, 0x22 },
93 { 0x3623, 0x22 },
94 { 0x5000, 0x4f },
95 { 0x5020, 0x4 },
96 { 0x5181, 0x79 },
97 { 0x5182, 0x0 },
98 { 0x5185, 0x22 },
99 { 0x5197, 0x1 },
100 { 0x5500, 0xa },
101 { 0x5504, 0x0 },
102 { 0x5505, 0x7f },
103 { 0x5080, 0x8 },
104 { 0x300e, 0x18 },
105 { 0x4610, 0x0 },
106 { 0x471d, 0x5 },
107 { 0x4708, 0x6 },
108 { 0x370c, 0xa0 },
109 { 0x5687, 0x94 },
110 { 0x501f, 0x0 },
111 { 0x5000, 0x4f },
112 { 0x5001, 0xcf },
113 { 0x4300, 0x30 },
114 { 0x4300, 0x30 },
115 { 0x460b, 0x35 },
116 { 0x471d, 0x0 },
117 { 0x3002, 0xc },
118 { 0x3002, 0x0 },
119 { 0x4713, 0x3 },
120 { 0x471c, 0x50 },
121 { 0x4721, 0x2 },
122 { 0x4402, 0x90 },
123 { 0x460c, 0x22 },
124 { 0x3815, 0x44 },
125 { 0x3503, 0x7 },
126 { 0x3501, 0x73 },
127 { 0x3502, 0x80 },
128 { 0x350b, 0x0 },
129 { 0x3818, 0xc8 },
130 { 0x3824, 0x11 },
131 { 0x3a00, 0x78 },
132 { 0x3a1a, 0x4 },
133 { 0x3a13, 0x30 },
134 { 0x3a18, 0x0 },
135 { 0x3a19, 0x7c },
136 { 0x3a08, 0x12 },
137 { 0x3a09, 0xc0 },
138 { 0x3a0a, 0xf },
139 { 0x3a0b, 0xa0 },
140 { 0x350c, 0x7 },
141 { 0x350d, 0xd0 },
142 { 0x3a0d, 0x8 },
143 { 0x3a0e, 0x6 },
144 { 0x3500, 0x0 },
145 { 0x3501, 0x0 },
146 { 0x3502, 0x0 },
147 { 0x350a, 0x0 },
148 { 0x350b, 0x0 },
149 { 0x3503, 0x0 },
150 { 0x3a0f, 0x3c },
151 { 0x3a10, 0x32 },
152 { 0x3a1b, 0x3c },
153 { 0x3a1e, 0x32 },
154 { 0x3a11, 0x80 },
155 { 0x3a1f, 0x20 },
156 { 0x3030, 0x2b },
157 { 0x3a02, 0x0 },
158 { 0x3a03, 0x7d },
159 { 0x3a04, 0x0 },
160 { 0x3a14, 0x0 },
161 { 0x3a15, 0x7d },
162 { 0x3a16, 0x0 },
163 { 0x3a00, 0x78 },
164 { 0x3a08, 0x9 },
165 { 0x3a09, 0x60 },
166 { 0x3a0a, 0x7 },
167 { 0x3a0b, 0xd0 },
168 { 0x3a0d, 0x10 },
169 { 0x3a0e, 0xd },
170 { 0x4407, 0x4 },
171 { 0x5193, 0x70 },
172 { 0x589b, 0x0 },
173 { 0x589a, 0xc0 },
174 { 0x401e, 0x20 },
175 { 0x4001, 0x42 },
176 { 0x401c, 0x6 },
177 { 0x3825, 0xac },
178 { 0x3827, 0xc },
179 { 0x528a, 0x1 },
180 { 0x528b, 0x4 },
181 { 0x528c, 0x8 },
182 { 0x528d, 0x10 },
183 { 0x528e, 0x20 },
184 { 0x528f, 0x28 },
185 { 0x5290, 0x30 },
186 { 0x5292, 0x0 },
187 { 0x5293, 0x1 },
188 { 0x5294, 0x0 },
189 { 0x5295, 0x4 },
190 { 0x5296, 0x0 },
191 { 0x5297, 0x8 },
192 { 0x5298, 0x0 },
193 { 0x5299, 0x10 },
194 { 0x529a, 0x0 },
195 { 0x529b, 0x20 },
196 { 0x529c, 0x0 },
197 { 0x529d, 0x28 },
198 { 0x529e, 0x0 },
199 { 0x529f, 0x30 },
200 { 0x5282, 0x0 },
201 { 0x5300, 0x0 },
202 { 0x5301, 0x20 },
203 { 0x5302, 0x0 },
204 { 0x5303, 0x7c },
205 { 0x530c, 0x0 },
206 { 0x530d, 0xc },
207 { 0x530e, 0x20 },
208 { 0x530f, 0x80 },
209 { 0x5310, 0x20 },
210 { 0x5311, 0x80 },
211 { 0x5308, 0x20 },
212 { 0x5309, 0x40 },
213 { 0x5304, 0x0 },
214 { 0x5305, 0x30 },
215 { 0x5306, 0x0 },
216 { 0x5307, 0x80 },
217 { 0x5314, 0x8 },
218 { 0x5315, 0x20 },
219 { 0x5319, 0x30 },
220 { 0x5316, 0x10 },
221 { 0x5317, 0x0 },
222 { 0x5318, 0x2 },
223 { 0x5380, 0x1 },
224 { 0x5381, 0x0 },
225 { 0x5382, 0x0 },
226 { 0x5383, 0x4e },
227 { 0x5384, 0x0 },
228 { 0x5385, 0xf },
229 { 0x5386, 0x0 },
230 { 0x5387, 0x0 },
231 { 0x5388, 0x1 },
232 { 0x5389, 0x15 },
233 { 0x538a, 0x0 },
234 { 0x538b, 0x31 },
235 { 0x538c, 0x0 },
236 { 0x538d, 0x0 },
237 { 0x538e, 0x0 },
238 { 0x538f, 0xf },
239 { 0x5390, 0x0 },
240 { 0x5391, 0xab },
241 { 0x5392, 0x0 },
242 { 0x5393, 0xa2 },
243 { 0x5394, 0x8 },
244 { 0x5480, 0x14 },
245 { 0x5481, 0x21 },
246 { 0x5482, 0x36 },
247 { 0x5483, 0x57 },
248 { 0x5484, 0x65 },
249 { 0x5485, 0x71 },
250 { 0x5486, 0x7d },
251 { 0x5487, 0x87 },
252 { 0x5488, 0x91 },
253 { 0x5489, 0x9a },
254 { 0x548a, 0xaa },
255 { 0x548b, 0xb8 },
256 { 0x548c, 0xcd },
257 { 0x548d, 0xdd },
258 { 0x548e, 0xea },
259 { 0x548f, 0x1d },
260 { 0x5490, 0x5 },
261 { 0x5491, 0x0 },
262 { 0x5492, 0x4 },
263 { 0x5493, 0x20 },
264 { 0x5494, 0x3 },
265 { 0x5495, 0x60 },
266 { 0x5496, 0x2 },
267 { 0x5497, 0xb8 },
268 { 0x5498, 0x2 },
269 { 0x5499, 0x86 },
270 { 0x549a, 0x2 },
271 { 0x549b, 0x5b },
272 { 0x549c, 0x2 },
273 { 0x549d, 0x3b },
274 { 0x549e, 0x2 },
275 { 0x549f, 0x1c },
276 { 0x54a0, 0x2 },
277 { 0x54a1, 0x4 },
278 { 0x54a2, 0x1 },
279 { 0x54a3, 0xed },
280 { 0x54a4, 0x1 },
281 { 0x54a5, 0xc5 },
282 { 0x54a6, 0x1 },
283 { 0x54a7, 0xa5 },
284 { 0x54a8, 0x1 },
285 { 0x54a9, 0x6c },
286 { 0x54aa, 0x1 },
287 { 0x54ab, 0x41 },
288 { 0x54ac, 0x1 },
289 { 0x54ad, 0x20 },
290 { 0x54ae, 0x0 },
291 { 0x54af, 0x16 },
292 { 0x54b0, 0x1 },
293 { 0x54b1, 0x20 },
294 { 0x54b2, 0x0 },
295 { 0x54b3, 0x10 },
296 { 0x54b4, 0x0 },
297 { 0x54b5, 0xf0 },
298 { 0x54b6, 0x0 },
299 { 0x54b7, 0xdf },
300 { 0x5402, 0x3f },
301 { 0x5403, 0x0 },
302 { 0x3406, 0x0 },
303 { 0x5180, 0xff },
304 { 0x5181, 0x52 },
305 { 0x5182, 0x11 },
306 { 0x5183, 0x14 },
307 { 0x5184, 0x25 },
308 { 0x5185, 0x24 },
309 { 0x5186, 0x6 },
310 { 0x5187, 0x8 },
311 { 0x5188, 0x8 },
312 { 0x5189, 0x7c },
313 { 0x518a, 0x60 },
314 { 0x518b, 0xb2 },
315 { 0x518c, 0xb2 },
316 { 0x518d, 0x44 },
317 { 0x518e, 0x3d },
318 { 0x518f, 0x58 },
319 { 0x5190, 0x46 },
320 { 0x5191, 0xf8 },
321 { 0x5192, 0x4 },
322 { 0x5193, 0x70 },
323 { 0x5194, 0xf0 },
324 { 0x5195, 0xf0 },
325 { 0x5196, 0x3 },
326 { 0x5197, 0x1 },
327 { 0x5198, 0x4 },
328 { 0x5199, 0x12 },
329 { 0x519a, 0x4 },
330 { 0x519b, 0x0 },
331 { 0x519c, 0x6 },
332 { 0x519d, 0x82 },
333 { 0x519e, 0x0 },
334 { 0x5025, 0x80 },
335 { 0x3a0f, 0x38 },
336 { 0x3a10, 0x30 },
337 { 0x3a1b, 0x3a },
338 { 0x3a1e, 0x2e },
339 { 0x3a11, 0x60 },
340 { 0x3a1f, 0x10 },
341 { 0x5688, 0xa6 },
342 { 0x5689, 0x6a },
343 { 0x568a, 0xea },
344 { 0x568b, 0xae },
345 { 0x568c, 0xa6 },
346 { 0x568d, 0x6a },
347 { 0x568e, 0x62 },
348 { 0x568f, 0x26 },
349 { 0x5583, 0x40 },
350 { 0x5584, 0x40 },
351 { 0x5580, 0x2 },
352 { 0x5000, 0xcf },
353 { 0x5800, 0x27 },
354 { 0x5801, 0x19 },
355 { 0x5802, 0x12 },
356 { 0x5803, 0xf },
357 { 0x5804, 0x10 },
358 { 0x5805, 0x15 },
359 { 0x5806, 0x1e },
360 { 0x5807, 0x2f },
361 { 0x5808, 0x15 },
362 { 0x5809, 0xd },
363 { 0x580a, 0xa },
364 { 0x580b, 0x9 },
365 { 0x580c, 0xa },
366 { 0x580d, 0xc },
367 { 0x580e, 0x12 },
368 { 0x580f, 0x19 },
369 { 0x5810, 0xb },
370 { 0x5811, 0x7 },
371 { 0x5812, 0x4 },
372 { 0x5813, 0x3 },
373 { 0x5814, 0x3 },
374 { 0x5815, 0x6 },
375 { 0x5816, 0xa },
376 { 0x5817, 0xf },
377 { 0x5818, 0xa },
378 { 0x5819, 0x5 },
379 { 0x581a, 0x1 },
380 { 0x581b, 0x0 },
381 { 0x581c, 0x0 },
382 { 0x581d, 0x3 },
383 { 0x581e, 0x8 },
384 { 0x581f, 0xc },
385 { 0x5820, 0xa },
386 { 0x5821, 0x5 },
387 { 0x5822, 0x1 },
388 { 0x5823, 0x0 },
389 { 0x5824, 0x0 },
390 { 0x5825, 0x3 },
391 { 0x5826, 0x8 },
392 { 0x5827, 0xc },
393 { 0x5828, 0xe },
394 { 0x5829, 0x8 },
395 { 0x582a, 0x6 },
396 { 0x582b, 0x4 },
397 { 0x582c, 0x5 },
398 { 0x582d, 0x7 },
399 { 0x582e, 0xb },
400 { 0x582f, 0x12 },
401 { 0x5830, 0x18 },
402 { 0x5831, 0x10 },
403 { 0x5832, 0xc },
404 { 0x5833, 0xa },
405 { 0x5834, 0xb },
406 { 0x5835, 0xe },
407 { 0x5836, 0x15 },
408 { 0x5837, 0x19 },
409 { 0x5838, 0x32 },
410 { 0x5839, 0x1f },
411 { 0x583a, 0x18 },
412 { 0x583b, 0x16 },
413 { 0x583c, 0x17 },
414 { 0x583d, 0x1e },
415 { 0x583e, 0x26 },
416 { 0x583f, 0x53 },
417 { 0x5840, 0x10 },
418 { 0x5841, 0xf },
419 { 0x5842, 0xd },
420 { 0x5843, 0xc },
421 { 0x5844, 0xe },
422 { 0x5845, 0x9 },
423 { 0x5846, 0x11 },
424 { 0x5847, 0x10 },
425 { 0x5848, 0x10 },
426 { 0x5849, 0x10 },
427 { 0x584a, 0x10 },
428 { 0x584b, 0xe },
429 { 0x584c, 0x10 },
430 { 0x584d, 0x10 },
431 { 0x584e, 0x11 },
432 { 0x584f, 0x10 },
433 { 0x5850, 0xf },
434 { 0x5851, 0xc },
435 { 0x5852, 0xf },
436 { 0x5853, 0x10 },
437 { 0x5854, 0x10 },
438 { 0x5855, 0xf },
439 { 0x5856, 0xe },
440 { 0x5857, 0xb },
441 { 0x5858, 0x10 },
442 { 0x5859, 0xd },
443 { 0x585a, 0xd },
444 { 0x585b, 0xc },
445 { 0x585c, 0xc },
446 { 0x585d, 0xc },
447 { 0x585e, 0xb },
448 { 0x585f, 0xc },
449 { 0x5860, 0xc },
450 { 0x5861, 0xc },
451 { 0x5862, 0xd },
452 { 0x5863, 0x8 },
453 { 0x5864, 0x11 },
454 { 0x5865, 0x18 },
455 { 0x5866, 0x18 },
456 { 0x5867, 0x19 },
457 { 0x5868, 0x17 },
458 { 0x5869, 0x19 },
459 { 0x586a, 0x16 },
460 { 0x586b, 0x13 },
461 { 0x586c, 0x13 },
462 { 0x586d, 0x12 },
463 { 0x586e, 0x13 },
464 { 0x586f, 0x16 },
465 { 0x5870, 0x14 },
466 { 0x5871, 0x12 },
467 { 0x5872, 0x10 },
468 { 0x5873, 0x11 },
469 { 0x5874, 0x11 },
470 { 0x5875, 0x16 },
471 { 0x5876, 0x14 },
472 { 0x5877, 0x11 },
473 { 0x5878, 0x10 },
474 { 0x5879, 0xf },
475 { 0x587a, 0x10 },
476 { 0x587b, 0x14 },
477 { 0x587c, 0x13 },
478 { 0x587d, 0x12 },
479 { 0x587e, 0x11 },
480 { 0x587f, 0x11 },
481 { 0x5880, 0x12 },
482 { 0x5881, 0x15 },
483 { 0x5882, 0x14 },
484 { 0x5883, 0x15 },
485 { 0x5884, 0x15 },
486 { 0x5885, 0x15 },
487 { 0x5886, 0x13 },
488 { 0x5887, 0x17 },
489 { 0x3710, 0x10 },
490 { 0x3632, 0x51 },
491 { 0x3702, 0x10 },
492 { 0x3703, 0xb2 },
493 { 0x3704, 0x18 },
494 { 0x370b, 0x40 },
495 { 0x370d, 0x3 },
496 { 0x3631, 0x1 },
497 { 0x3632, 0x52 },
498 { 0x3606, 0x24 },
499 { 0x3620, 0x96 },
500 { 0x5785, 0x7 },
501 { 0x3a13, 0x30 },
502 { 0x3600, 0x52 },
503 { 0x3604, 0x48 },
504 { 0x3606, 0x1b },
505 { 0x370d, 0xb },
506 { 0x370f, 0xc0 },
507 { 0x3709, 0x1 },
508 { 0x3823, 0x0 },
509 { 0x5007, 0x0 },
510 { 0x5009, 0x0 },
511 { 0x5011, 0x0 },
512 { 0x5013, 0x0 },
513 { 0x519e, 0x0 },
514 { 0x5086, 0x0 },
515 { 0x5087, 0x0 },
516 { 0x5088, 0x0 },
517 { 0x5089, 0x0 },
518 { 0x302b, 0x0 },
519 { 0x3503, 0x7 },
520 { 0x3011, 0x8 },
521 { 0x350c, 0x2 },
522 { 0x350d, 0xe4 },
523 { 0x3621, 0xc9 },
524 { 0x370a, 0x81 },
525 { 0xffff, 0xff },
526};
527
528static struct regval_list ov5642_default_regs_finalise[] = {
529 { 0x3810, 0xc2 },
530 { 0x3818, 0xc9 },
531 { 0x381c, 0x10 },
532 { 0x381d, 0xa0 },
533 { 0x381e, 0x5 },
534 { 0x381f, 0xb0 },
535 { 0x3820, 0x0 },
536 { 0x3821, 0x0 },
537 { 0x3824, 0x11 },
538 { 0x3a08, 0x1b },
539 { 0x3a09, 0xc0 },
540 { 0x3a0a, 0x17 },
541 { 0x3a0b, 0x20 },
542 { 0x3a0d, 0x2 },
543 { 0x3a0e, 0x1 },
544 { 0x401c, 0x4 },
545 { 0x5682, 0x5 },
546 { 0x5683, 0x0 },
547 { 0x5686, 0x2 },
548 { 0x5687, 0xcc },
549 { 0x5001, 0x4f },
550 { 0x589b, 0x6 },
551 { 0x589a, 0xc5 },
552 { 0x3503, 0x0 },
553 { 0x460c, 0x20 },
554 { 0x460b, 0x37 },
555 { 0x471c, 0xd0 },
556 { 0x471d, 0x5 },
557 { 0x3815, 0x1 },
558 { 0x3818, 0xc1 },
559 { 0x501f, 0x0 },
560 { 0x5002, 0xe0 },
561 { 0x4300, 0x32 }, /* UYVY */
562 { 0x3002, 0x1c },
563 { 0x4800, 0x14 },
564 { 0x4801, 0xf },
565 { 0x3007, 0x3b },
566 { 0x300e, 0x4 },
567 { 0x4803, 0x50 },
568 { 0x3815, 0x1 },
569 { 0x4713, 0x2 },
570 { 0x4842, 0x1 },
571 { 0x300f, 0xe },
572 { 0x3003, 0x3 },
573 { 0x3003, 0x1 },
574 { 0xffff, 0xff },
575};
576
577struct ov5642_datafmt {
578 enum v4l2_mbus_pixelcode code;
579 enum v4l2_colorspace colorspace;
580};
581
582struct ov5642 {
583 struct v4l2_subdev subdev;
584 const struct ov5642_datafmt *fmt;
585};
586
587static const struct ov5642_datafmt ov5642_colour_fmts[] = {
588 {V4L2_MBUS_FMT_UYVY8_2X8, V4L2_COLORSPACE_JPEG},
589};
590
591static struct ov5642 *to_ov5642(const struct i2c_client *client)
592{
593 return container_of(i2c_get_clientdata(client), struct ov5642, subdev);
594}
595
596/* Find a data format by a pixel code in an array */
597static const struct ov5642_datafmt
598 *ov5642_find_datafmt(enum v4l2_mbus_pixelcode code)
599{
600 int i;
601
602 for (i = 0; i < ARRAY_SIZE(ov5642_colour_fmts); i++)
603 if (ov5642_colour_fmts[i].code == code)
604 return ov5642_colour_fmts + i;
605
606 return NULL;
607}
608
609static int reg_read(struct i2c_client *client, u16 reg, u8 *val)
610{
611 int ret;
612 /* We have 16-bit i2c addresses - care for endianess */
613 unsigned char data[2] = { reg >> 8, reg & 0xff };
614
615 ret = i2c_master_send(client, data, 2);
616 if (ret < 2) {
617 dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
618 __func__, reg);
619 return ret < 0 ? ret : -EIO;
620 }
621
622 ret = i2c_master_recv(client, val, 1);
623 if (ret < 1) {
624 dev_err(&client->dev, "%s: i2c read error, reg: %x\n",
625 __func__, reg);
626 return ret < 0 ? ret : -EIO;
627 }
628 return 0;
629}
630
631static int reg_write(struct i2c_client *client, u16 reg, u8 val)
632{
633 int ret;
634 unsigned char data[3] = { reg >> 8, reg & 0xff, val };
635
636 ret = i2c_master_send(client, data, 3);
637 if (ret < 3) {
638 dev_err(&client->dev, "%s: i2c write error, reg: %x\n",
639 __func__, reg);
640 return ret < 0 ? ret : -EIO;
641 }
642
643 return 0;
644}
645#ifdef CONFIG_VIDEO_ADV_DEBUG
646static int ov5642_get_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
647{
648 struct i2c_client *client = v4l2_get_subdevdata(sd);
649 int ret;
650 u8 val;
651
652 if (reg->reg & ~0xffff)
653 return -EINVAL;
654
655 reg->size = 1;
656
657 ret = reg_read(client, reg->reg, &val);
658 if (!ret)
659 reg->val = (__u64)val;
660
661 return ret;
662}
663
664static int ov5642_set_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
665{
666 struct i2c_client *client = v4l2_get_subdevdata(sd);
667
668 if (reg->reg & ~0xffff || reg->val & ~0xff)
669 return -EINVAL;
670
671 return reg_write(client, reg->reg, reg->val);
672}
673#endif
674
675static int ov5642_write_array(struct i2c_client *client,
676 struct regval_list *vals)
677{
678 while (vals->reg_num != 0xffff || vals->value != 0xff) {
679 int ret = reg_write(client, vals->reg_num, vals->value);
680 if (ret < 0)
681 return ret;
682 vals++;
683 }
684 dev_dbg(&client->dev, "Register list loaded\n");
685 return 0;
686}
687
688static int ov5642_set_resolution(struct i2c_client *client)
689{
690 int ret;
691 u8 start_x_high = ((OV5642_SENSOR_SIZE_X - OV5642_WIDTH) / 2) >> 8;
692 u8 start_x_low = ((OV5642_SENSOR_SIZE_X - OV5642_WIDTH) / 2) & 0xff;
693 u8 start_y_high = ((OV5642_SENSOR_SIZE_Y - OV5642_HEIGHT) / 2) >> 8;
694 u8 start_y_low = ((OV5642_SENSOR_SIZE_Y - OV5642_HEIGHT) / 2) & 0xff;
695
696 u8 width_high = OV5642_WIDTH >> 8;
697 u8 width_low = OV5642_WIDTH & 0xff;
698 u8 height_high = OV5642_HEIGHT >> 8;
699 u8 height_low = OV5642_HEIGHT & 0xff;
700
701 u8 total_width_high = OV5642_TOTAL_WIDTH >> 8;
702 u8 total_width_low = OV5642_TOTAL_WIDTH & 0xff;
703 u8 total_height_high = OV5642_TOTAL_HEIGHT >> 8;
704 u8 total_height_low = OV5642_TOTAL_HEIGHT & 0xff;
705
706 ret = reg_write(client, REG_WINDOW_START_X_HIGH, start_x_high);
707 if (!ret)
708 ret = reg_write(client, REG_WINDOW_START_X_LOW, start_x_low);
709 if (!ret)
710 ret = reg_write(client, REG_WINDOW_START_Y_HIGH, start_y_high);
711 if (!ret)
712 ret = reg_write(client, REG_WINDOW_START_Y_LOW, start_y_low);
713
714 if (!ret)
715 ret = reg_write(client, REG_WINDOW_WIDTH_HIGH, width_high);
716 if (!ret)
717 ret = reg_write(client, REG_WINDOW_WIDTH_LOW , width_low);
718 if (!ret)
719 ret = reg_write(client, REG_WINDOW_HEIGHT_HIGH, height_high);
720 if (!ret)
721 ret = reg_write(client, REG_WINDOW_HEIGHT_LOW, height_low);
722
723 if (!ret)
724 ret = reg_write(client, REG_OUT_WIDTH_HIGH, width_high);
725 if (!ret)
726 ret = reg_write(client, REG_OUT_WIDTH_LOW , width_low);
727 if (!ret)
728 ret = reg_write(client, REG_OUT_HEIGHT_HIGH, height_high);
729 if (!ret)
730 ret = reg_write(client, REG_OUT_HEIGHT_LOW, height_low);
731
732 if (!ret)
733 ret = reg_write(client, REG_OUT_TOTAL_WIDTH_HIGH, total_width_high);
734 if (!ret)
735 ret = reg_write(client, REG_OUT_TOTAL_WIDTH_LOW, total_width_low);
736 if (!ret)
737 ret = reg_write(client, REG_OUT_TOTAL_HEIGHT_HIGH, total_height_high);
738 if (!ret)
739 ret = reg_write(client, REG_OUT_TOTAL_HEIGHT_LOW, total_height_low);
740
741 return ret;
742}
743
744static int ov5642_try_fmt(struct v4l2_subdev *sd,
745 struct v4l2_mbus_framefmt *mf)
746{
747 const struct ov5642_datafmt *fmt = ov5642_find_datafmt(mf->code);
748
749 dev_dbg(sd->v4l2_dev->dev, "%s(%u) width: %u heigth: %u\n",
750 __func__, mf->code, mf->width, mf->height);
751
752 if (!fmt) {
753 mf->code = ov5642_colour_fmts[0].code;
754 mf->colorspace = ov5642_colour_fmts[0].colorspace;
755 }
756
757 mf->width = OV5642_WIDTH;
758 mf->height = OV5642_HEIGHT;
759 mf->field = V4L2_FIELD_NONE;
760
761 return 0;
762}
763
764static int ov5642_s_fmt(struct v4l2_subdev *sd,
765 struct v4l2_mbus_framefmt *mf)
766{
767 struct i2c_client *client = v4l2_get_subdevdata(sd);
768 struct ov5642 *priv = to_ov5642(client);
769
770 dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
771
772 /* MIPI CSI could have changed the format, double-check */
773 if (!ov5642_find_datafmt(mf->code))
774 return -EINVAL;
775
776 ov5642_try_fmt(sd, mf);
777
778 priv->fmt = ov5642_find_datafmt(mf->code);
779
780 ov5642_write_array(client, ov5642_default_regs_init);
781 ov5642_set_resolution(client);
782 ov5642_write_array(client, ov5642_default_regs_finalise);
783
784 return 0;
785}
786
787static int ov5642_g_fmt(struct v4l2_subdev *sd,
788 struct v4l2_mbus_framefmt *mf)
789{
790 struct i2c_client *client = v4l2_get_subdevdata(sd);
791 struct ov5642 *priv = to_ov5642(client);
792
793 const struct ov5642_datafmt *fmt = priv->fmt;
794
795 mf->code = fmt->code;
796 mf->colorspace = fmt->colorspace;
797 mf->width = OV5642_WIDTH;
798 mf->height = OV5642_HEIGHT;
799 mf->field = V4L2_FIELD_NONE;
800
801 return 0;
802}
803
804static int ov5642_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
805 enum v4l2_mbus_pixelcode *code)
806{
807 if (index >= ARRAY_SIZE(ov5642_colour_fmts))
808 return -EINVAL;
809
810 *code = ov5642_colour_fmts[index].code;
811 return 0;
812}
813
814static int ov5642_g_chip_ident(struct v4l2_subdev *sd,
815 struct v4l2_dbg_chip_ident *id)
816{
817 struct i2c_client *client = v4l2_get_subdevdata(sd);
818
819 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
820 return -EINVAL;
821
822 if (id->match.addr != client->addr)
823 return -ENODEV;
824
825 id->ident = V4L2_IDENT_OV5642;
826 id->revision = 0;
827
828 return 0;
829}
830
831static int ov5642_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
832{
833 struct v4l2_rect *rect = &a->c;
834
835 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
836 rect->top = 0;
837 rect->left = 0;
838 rect->width = OV5642_WIDTH;
839 rect->height = OV5642_HEIGHT;
840
841 return 0;
842}
843
844static int ov5642_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
845{
846 a->bounds.left = 0;
847 a->bounds.top = 0;
848 a->bounds.width = OV5642_WIDTH;
849 a->bounds.height = OV5642_HEIGHT;
850 a->defrect = a->bounds;
851 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
852 a->pixelaspect.numerator = 1;
853 a->pixelaspect.denominator = 1;
854
855 return 0;
856}
857
858static struct v4l2_subdev_video_ops ov5642_subdev_video_ops = {
859 .s_mbus_fmt = ov5642_s_fmt,
860 .g_mbus_fmt = ov5642_g_fmt,
861 .try_mbus_fmt = ov5642_try_fmt,
862 .enum_mbus_fmt = ov5642_enum_fmt,
863 .g_crop = ov5642_g_crop,
864 .cropcap = ov5642_cropcap,
865};
866
867static struct v4l2_subdev_core_ops ov5642_subdev_core_ops = {
868 .g_chip_ident = ov5642_g_chip_ident,
869#ifdef CONFIG_VIDEO_ADV_DEBUG
870 .g_register = ov5642_get_register,
871 .s_register = ov5642_set_register,
872#endif
873};
874
875static struct v4l2_subdev_ops ov5642_subdev_ops = {
876 .core = &ov5642_subdev_core_ops,
877 .video = &ov5642_subdev_video_ops,
878};
879
880/*
881 * We have to provide soc-camera operations, but we don't have anything to say
882 * there. The MIPI CSI2 driver will provide .query_bus_param and .set_bus_param
883 */
884static unsigned long soc_ov5642_query_bus_param(struct soc_camera_device *icd)
885{
886 return 0;
887}
888
889static int soc_ov5642_set_bus_param(struct soc_camera_device *icd,
890 unsigned long flags)
891{
892 return -EINVAL;
893}
894
895static struct soc_camera_ops soc_ov5642_ops = {
896 .query_bus_param = soc_ov5642_query_bus_param,
897 .set_bus_param = soc_ov5642_set_bus_param,
898};
899
900static int ov5642_video_probe(struct soc_camera_device *icd,
901 struct i2c_client *client)
902{
903 int ret;
904 u8 id_high, id_low;
905 u16 id;
906
907 /* Read sensor Model ID */
908 ret = reg_read(client, REG_CHIP_ID_HIGH, &id_high);
909 if (ret < 0)
910 return ret;
911
912 id = id_high << 8;
913
914 ret = reg_read(client, REG_CHIP_ID_LOW, &id_low);
915 if (ret < 0)
916 return ret;
917
918 id |= id_low;
919
920 dev_info(&client->dev, "Chip ID 0x%04x detected\n", id);
921
922 if (id != 0x5642)
923 return -ENODEV;
924
925 return 0;
926}
927
928static int ov5642_probe(struct i2c_client *client,
929 const struct i2c_device_id *did)
930{
931 struct ov5642 *priv;
932 struct soc_camera_device *icd = client->dev.platform_data;
933 struct soc_camera_link *icl;
934 int ret;
935
936 if (!icd) {
937 dev_err(&client->dev, "OV5642: missing soc-camera data!\n");
938 return -EINVAL;
939 }
940
941 icl = to_soc_camera_link(icd);
942 if (!icl) {
943 dev_err(&client->dev, "OV5642: missing platform data!\n");
944 return -EINVAL;
945 }
946
947 priv = kzalloc(sizeof(struct ov5642), GFP_KERNEL);
948 if (!priv)
949 return -ENOMEM;
950
951 v4l2_i2c_subdev_init(&priv->subdev, client, &ov5642_subdev_ops);
952
953 icd->ops = &soc_ov5642_ops;
954 priv->fmt = &ov5642_colour_fmts[0];
955
956 ret = ov5642_video_probe(icd, client);
957 if (ret < 0)
958 goto error;
959
960 return 0;
961
962error:
963 icd->ops = NULL;
964 kfree(priv);
965 return ret;
966}
967
968static int ov5642_remove(struct i2c_client *client)
969{
970 struct ov5642 *priv = to_ov5642(client);
971 struct soc_camera_device *icd = client->dev.platform_data;
972 struct soc_camera_link *icl = to_soc_camera_link(icd);
973
974 icd->ops = NULL;
975 if (icl->free_bus)
976 icl->free_bus(icl);
977 kfree(priv);
978
979 return 0;
980}
981
982static const struct i2c_device_id ov5642_id[] = {
983 { "ov5642", 0 },
984 { }
985};
986MODULE_DEVICE_TABLE(i2c, ov5642_id);
987
988static struct i2c_driver ov5642_i2c_driver = {
989 .driver = {
990 .name = "ov5642",
991 },
992 .probe = ov5642_probe,
993 .remove = ov5642_remove,
994 .id_table = ov5642_id,
995};
996
997static int __init ov5642_mod_init(void)
998{
999 return i2c_add_driver(&ov5642_i2c_driver);
1000}
1001
1002static void __exit ov5642_mod_exit(void)
1003{
1004 i2c_del_driver(&ov5642_i2c_driver);
1005}
1006
1007module_init(ov5642_mod_init);
1008module_exit(ov5642_mod_exit);
1009
1010MODULE_DESCRIPTION("Omnivision OV5642 Camera driver");
1011MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
1012MODULE_LICENSE("GPL v2");