blob: 45c0e96a4cbab2053791e010613553ac8513dccb [file] [log] [blame]
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001/*
2 * Copyright (c) 2017 Intel Corporation.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
15#include <linux/acpi.h>
16#include <linux/i2c.h>
17#include <linux/module.h>
18#include <linux/pm_runtime.h>
19#include <media/v4l2-ctrls.h>
20#include <media/v4l2-device.h>
21
22#define OV13858_REG_VALUE_08BIT 1
23#define OV13858_REG_VALUE_16BIT 2
24#define OV13858_REG_VALUE_24BIT 3
25
26#define OV13858_REG_MODE_SELECT 0x0100
27#define OV13858_MODE_STANDBY 0x00
28#define OV13858_MODE_STREAMING 0x01
29
30#define OV13858_REG_SOFTWARE_RST 0x0103
31#define OV13858_SOFTWARE_RST 0x01
32
33/* PLL1 generates PCLK and MIPI_PHY_CLK */
34#define OV13858_REG_PLL1_CTRL_0 0x0300
35#define OV13858_REG_PLL1_CTRL_1 0x0301
36#define OV13858_REG_PLL1_CTRL_2 0x0302
37#define OV13858_REG_PLL1_CTRL_3 0x0303
38#define OV13858_REG_PLL1_CTRL_4 0x0304
39#define OV13858_REG_PLL1_CTRL_5 0x0305
40
41/* PLL2 generates DAC_CLK, SCLK and SRAM_CLK */
42#define OV13858_REG_PLL2_CTRL_B 0x030b
43#define OV13858_REG_PLL2_CTRL_C 0x030c
44#define OV13858_REG_PLL2_CTRL_D 0x030d
45#define OV13858_REG_PLL2_CTRL_E 0x030e
46#define OV13858_REG_PLL2_CTRL_F 0x030f
47#define OV13858_REG_PLL2_CTRL_12 0x0312
48#define OV13858_REG_MIPI_SC_CTRL0 0x3016
49#define OV13858_REG_MIPI_SC_CTRL1 0x3022
50
51/* Chip ID */
52#define OV13858_REG_CHIP_ID 0x300a
53#define OV13858_CHIP_ID 0x00d855
54
55/* V_TIMING internal */
56#define OV13858_REG_VTS 0x380e
57#define OV13858_VTS_30FPS 0x0c8e /* 30 fps */
58#define OV13858_VTS_60FPS 0x0648 /* 60 fps */
59#define OV13858_VTS_MAX 0x7fff
60#define OV13858_VBLANK_MIN 56
61
62/* HBLANK control - read only */
Chiranjeevi Rapolu89d8b612017-07-29 03:00:39 -040063#define OV13858_PPL_270MHZ 2244
64#define OV13858_PPL_540MHZ 4488
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -030065
66/* Exposure control */
67#define OV13858_REG_EXPOSURE 0x3500
68#define OV13858_EXPOSURE_MIN 4
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -030069#define OV13858_EXPOSURE_STEP 1
70#define OV13858_EXPOSURE_DEFAULT 0x640
71
72/* Analog gain control */
73#define OV13858_REG_ANALOG_GAIN 0x3508
74#define OV13858_ANA_GAIN_MIN 0
75#define OV13858_ANA_GAIN_MAX 0x1fff
76#define OV13858_ANA_GAIN_STEP 1
77#define OV13858_ANA_GAIN_DEFAULT 0x80
78
79/* Digital gain control */
Chiranjeevi Rapolubfced6d2017-07-28 19:21:03 -040080#define OV13858_REG_B_MWB_GAIN 0x5100
81#define OV13858_REG_G_MWB_GAIN 0x5102
82#define OV13858_REG_R_MWB_GAIN 0x5104
83#define OV13858_DGTL_GAIN_MIN 0
84#define OV13858_DGTL_GAIN_MAX 16384 /* Max = 16 X */
85#define OV13858_DGTL_GAIN_DEFAULT 1024 /* Default gain = 1 X */
86#define OV13858_DGTL_GAIN_STEP 1 /* Each step = 1/1024 */
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -030087
88/* Test Pattern Control */
89#define OV13858_REG_TEST_PATTERN 0x4503
90#define OV13858_TEST_PATTERN_ENABLE BIT(7)
91#define OV13858_TEST_PATTERN_MASK 0xfc
92
93/* Number of frames to skip */
94#define OV13858_NUM_OF_SKIP_FRAMES 2
95
96struct ov13858_reg {
97 u16 address;
98 u8 val;
99};
100
101struct ov13858_reg_list {
102 u32 num_of_regs;
103 const struct ov13858_reg *regs;
104};
105
106/* Link frequency config */
107struct ov13858_link_freq_config {
108 u32 pixel_rate;
109 u32 pixels_per_line;
110
111 /* PLL registers for this link frequency */
112 struct ov13858_reg_list reg_list;
113};
114
115/* Mode : resolution and related config&values */
116struct ov13858_mode {
117 /* Frame width */
118 u32 width;
119 /* Frame height */
120 u32 height;
121
122 /* V-timing */
123 u32 vts;
124
125 /* Index of Link frequency config to be used */
126 u32 link_freq_index;
127 /* Default register values */
128 struct ov13858_reg_list reg_list;
129};
130
131/* 4224x3136 needs 1080Mbps/lane, 4 lanes */
132static const struct ov13858_reg mipi_data_rate_1080mbps[] = {
133 /* PLL1 registers */
134 {OV13858_REG_PLL1_CTRL_0, 0x07},
135 {OV13858_REG_PLL1_CTRL_1, 0x01},
136 {OV13858_REG_PLL1_CTRL_2, 0xc2},
137 {OV13858_REG_PLL1_CTRL_3, 0x00},
138 {OV13858_REG_PLL1_CTRL_4, 0x00},
139 {OV13858_REG_PLL1_CTRL_5, 0x01},
140
141 /* PLL2 registers */
142 {OV13858_REG_PLL2_CTRL_B, 0x05},
143 {OV13858_REG_PLL2_CTRL_C, 0x01},
144 {OV13858_REG_PLL2_CTRL_D, 0x0e},
145 {OV13858_REG_PLL2_CTRL_E, 0x05},
146 {OV13858_REG_PLL2_CTRL_F, 0x01},
147 {OV13858_REG_PLL2_CTRL_12, 0x01},
148 {OV13858_REG_MIPI_SC_CTRL0, 0x72},
149 {OV13858_REG_MIPI_SC_CTRL1, 0x01},
150};
151
152/*
153 * 2112x1568, 2112x1188, 1056x784 need 540Mbps/lane,
154 * 4 lanes
155 */
156static const struct ov13858_reg mipi_data_rate_540mbps[] = {
157 /* PLL1 registers */
158 {OV13858_REG_PLL1_CTRL_0, 0x07},
159 {OV13858_REG_PLL1_CTRL_1, 0x01},
160 {OV13858_REG_PLL1_CTRL_2, 0xc2},
161 {OV13858_REG_PLL1_CTRL_3, 0x01},
162 {OV13858_REG_PLL1_CTRL_4, 0x00},
163 {OV13858_REG_PLL1_CTRL_5, 0x01},
164
165 /* PLL2 registers */
166 {OV13858_REG_PLL2_CTRL_B, 0x05},
167 {OV13858_REG_PLL2_CTRL_C, 0x01},
168 {OV13858_REG_PLL2_CTRL_D, 0x0e},
169 {OV13858_REG_PLL2_CTRL_E, 0x05},
170 {OV13858_REG_PLL2_CTRL_F, 0x01},
171 {OV13858_REG_PLL2_CTRL_12, 0x01},
172 {OV13858_REG_MIPI_SC_CTRL0, 0x72},
173 {OV13858_REG_MIPI_SC_CTRL1, 0x01},
174};
175
176static const struct ov13858_reg mode_4224x3136_regs[] = {
177 {0x3013, 0x32},
178 {0x301b, 0xf0},
179 {0x301f, 0xd0},
180 {0x3106, 0x15},
181 {0x3107, 0x23},
182 {0x350a, 0x00},
183 {0x350e, 0x00},
184 {0x3510, 0x00},
185 {0x3511, 0x02},
186 {0x3512, 0x00},
187 {0x3600, 0x2b},
188 {0x3601, 0x52},
189 {0x3602, 0x60},
190 {0x3612, 0x05},
191 {0x3613, 0xa4},
192 {0x3620, 0x80},
193 {0x3621, 0x10},
194 {0x3622, 0x30},
195 {0x3624, 0x1c},
196 {0x3640, 0x10},
197 {0x3641, 0x70},
198 {0x3661, 0x80},
199 {0x3662, 0x12},
200 {0x3664, 0x73},
201 {0x3665, 0xa7},
202 {0x366e, 0xff},
203 {0x366f, 0xf4},
204 {0x3674, 0x00},
205 {0x3679, 0x0c},
206 {0x367f, 0x01},
207 {0x3680, 0x0c},
208 {0x3681, 0x50},
209 {0x3682, 0x50},
210 {0x3683, 0xa9},
211 {0x3684, 0xa9},
212 {0x3709, 0x5f},
213 {0x3714, 0x24},
214 {0x371a, 0x3e},
215 {0x3737, 0x04},
216 {0x3738, 0xcc},
217 {0x3739, 0x12},
218 {0x373d, 0x26},
219 {0x3764, 0x20},
220 {0x3765, 0x20},
221 {0x37a1, 0x36},
222 {0x37a8, 0x3b},
223 {0x37ab, 0x31},
224 {0x37c2, 0x04},
225 {0x37c3, 0xf1},
226 {0x37c5, 0x00},
227 {0x37d8, 0x03},
228 {0x37d9, 0x0c},
229 {0x37da, 0xc2},
230 {0x37dc, 0x02},
231 {0x37e0, 0x00},
232 {0x37e1, 0x0a},
233 {0x37e2, 0x14},
234 {0x37e3, 0x04},
235 {0x37e4, 0x2a},
236 {0x37e5, 0x03},
237 {0x37e6, 0x04},
238 {0x3800, 0x00},
239 {0x3801, 0x00},
240 {0x3802, 0x00},
241 {0x3803, 0x00},
242 {0x3804, 0x10},
243 {0x3805, 0x9f},
244 {0x3806, 0x0c},
245 {0x3807, 0x5f},
246 {0x3808, 0x10},
247 {0x3809, 0x80},
248 {0x380a, 0x0c},
249 {0x380b, 0x40},
250 {0x380c, 0x04},
251 {0x380d, 0x62},
252 {0x380e, 0x0c},
253 {0x380f, 0x8e},
254 {0x3811, 0x04},
255 {0x3813, 0x05},
256 {0x3814, 0x01},
257 {0x3815, 0x01},
258 {0x3816, 0x01},
259 {0x3817, 0x01},
260 {0x3820, 0xa8},
261 {0x3821, 0x00},
262 {0x3822, 0xc2},
263 {0x3823, 0x18},
264 {0x3826, 0x11},
265 {0x3827, 0x1c},
266 {0x3829, 0x03},
267 {0x3832, 0x00},
268 {0x3c80, 0x00},
269 {0x3c87, 0x01},
270 {0x3c8c, 0x19},
271 {0x3c8d, 0x1c},
272 {0x3c90, 0x00},
273 {0x3c91, 0x00},
274 {0x3c92, 0x00},
275 {0x3c93, 0x00},
276 {0x3c94, 0x40},
277 {0x3c95, 0x54},
278 {0x3c96, 0x34},
279 {0x3c97, 0x04},
280 {0x3c98, 0x00},
281 {0x3d8c, 0x73},
282 {0x3d8d, 0xc0},
283 {0x3f00, 0x0b},
284 {0x3f03, 0x00},
285 {0x4001, 0xe0},
286 {0x4008, 0x00},
287 {0x4009, 0x0f},
288 {0x4011, 0xf0},
289 {0x4017, 0x08},
290 {0x4050, 0x04},
291 {0x4051, 0x0b},
292 {0x4052, 0x00},
293 {0x4053, 0x80},
294 {0x4054, 0x00},
295 {0x4055, 0x80},
296 {0x4056, 0x00},
297 {0x4057, 0x80},
298 {0x4058, 0x00},
299 {0x4059, 0x80},
300 {0x405e, 0x20},
301 {0x4500, 0x07},
302 {0x4503, 0x00},
303 {0x450a, 0x04},
304 {0x4809, 0x04},
305 {0x480c, 0x12},
306 {0x481f, 0x30},
307 {0x4833, 0x10},
308 {0x4837, 0x0e},
309 {0x4902, 0x01},
310 {0x4d00, 0x03},
311 {0x4d01, 0xc9},
312 {0x4d02, 0xbc},
313 {0x4d03, 0xd7},
314 {0x4d04, 0xf0},
315 {0x4d05, 0xa2},
316 {0x5000, 0xfd},
317 {0x5001, 0x01},
318 {0x5040, 0x39},
319 {0x5041, 0x10},
320 {0x5042, 0x10},
321 {0x5043, 0x84},
322 {0x5044, 0x62},
323 {0x5180, 0x00},
324 {0x5181, 0x10},
325 {0x5182, 0x02},
326 {0x5183, 0x0f},
327 {0x5200, 0x1b},
328 {0x520b, 0x07},
329 {0x520c, 0x0f},
330 {0x5300, 0x04},
331 {0x5301, 0x0c},
332 {0x5302, 0x0c},
333 {0x5303, 0x0f},
334 {0x5304, 0x00},
335 {0x5305, 0x70},
336 {0x5306, 0x00},
337 {0x5307, 0x80},
338 {0x5308, 0x00},
339 {0x5309, 0xa5},
340 {0x530a, 0x00},
341 {0x530b, 0xd3},
342 {0x530c, 0x00},
343 {0x530d, 0xf0},
344 {0x530e, 0x01},
345 {0x530f, 0x10},
346 {0x5310, 0x01},
347 {0x5311, 0x20},
348 {0x5312, 0x01},
349 {0x5313, 0x20},
350 {0x5314, 0x01},
351 {0x5315, 0x20},
352 {0x5316, 0x08},
353 {0x5317, 0x08},
354 {0x5318, 0x10},
355 {0x5319, 0x88},
356 {0x531a, 0x88},
357 {0x531b, 0xa9},
358 {0x531c, 0xaa},
359 {0x531d, 0x0a},
360 {0x5405, 0x02},
361 {0x5406, 0x67},
362 {0x5407, 0x01},
363 {0x5408, 0x4a},
364};
365
366static const struct ov13858_reg mode_2112x1568_regs[] = {
367 {0x3013, 0x32},
368 {0x301b, 0xf0},
369 {0x301f, 0xd0},
370 {0x3106, 0x15},
371 {0x3107, 0x23},
372 {0x350a, 0x00},
373 {0x350e, 0x00},
374 {0x3510, 0x00},
375 {0x3511, 0x02},
376 {0x3512, 0x00},
377 {0x3600, 0x2b},
378 {0x3601, 0x52},
379 {0x3602, 0x60},
380 {0x3612, 0x05},
381 {0x3613, 0xa4},
382 {0x3620, 0x80},
383 {0x3621, 0x10},
384 {0x3622, 0x30},
385 {0x3624, 0x1c},
386 {0x3640, 0x10},
387 {0x3641, 0x70},
388 {0x3661, 0x80},
389 {0x3662, 0x10},
390 {0x3664, 0x73},
391 {0x3665, 0xa7},
392 {0x366e, 0xff},
393 {0x366f, 0xf4},
394 {0x3674, 0x00},
395 {0x3679, 0x0c},
396 {0x367f, 0x01},
397 {0x3680, 0x0c},
398 {0x3681, 0x50},
399 {0x3682, 0x50},
400 {0x3683, 0xa9},
401 {0x3684, 0xa9},
402 {0x3709, 0x5f},
403 {0x3714, 0x28},
404 {0x371a, 0x3e},
405 {0x3737, 0x08},
406 {0x3738, 0xcc},
407 {0x3739, 0x20},
408 {0x373d, 0x26},
409 {0x3764, 0x20},
410 {0x3765, 0x20},
411 {0x37a1, 0x36},
412 {0x37a8, 0x3b},
413 {0x37ab, 0x31},
414 {0x37c2, 0x14},
415 {0x37c3, 0xf1},
416 {0x37c5, 0x00},
417 {0x37d8, 0x03},
418 {0x37d9, 0x0c},
419 {0x37da, 0xc2},
420 {0x37dc, 0x02},
421 {0x37e0, 0x00},
422 {0x37e1, 0x0a},
423 {0x37e2, 0x14},
424 {0x37e3, 0x08},
425 {0x37e4, 0x38},
426 {0x37e5, 0x03},
427 {0x37e6, 0x08},
428 {0x3800, 0x00},
429 {0x3801, 0x00},
430 {0x3802, 0x00},
431 {0x3803, 0x00},
432 {0x3804, 0x10},
433 {0x3805, 0x9f},
434 {0x3806, 0x0c},
435 {0x3807, 0x5f},
436 {0x3808, 0x08},
437 {0x3809, 0x40},
438 {0x380a, 0x06},
439 {0x380b, 0x20},
440 {0x380c, 0x04},
441 {0x380d, 0x62},
442 {0x380e, 0x0c},
443 {0x380f, 0x8e},
444 {0x3811, 0x04},
445 {0x3813, 0x05},
446 {0x3814, 0x03},
447 {0x3815, 0x01},
448 {0x3816, 0x03},
449 {0x3817, 0x01},
450 {0x3820, 0xab},
451 {0x3821, 0x00},
452 {0x3822, 0xc2},
453 {0x3823, 0x18},
454 {0x3826, 0x04},
455 {0x3827, 0x90},
456 {0x3829, 0x07},
457 {0x3832, 0x00},
458 {0x3c80, 0x00},
459 {0x3c87, 0x01},
460 {0x3c8c, 0x19},
461 {0x3c8d, 0x1c},
462 {0x3c90, 0x00},
463 {0x3c91, 0x00},
464 {0x3c92, 0x00},
465 {0x3c93, 0x00},
466 {0x3c94, 0x40},
467 {0x3c95, 0x54},
468 {0x3c96, 0x34},
469 {0x3c97, 0x04},
470 {0x3c98, 0x00},
471 {0x3d8c, 0x73},
472 {0x3d8d, 0xc0},
473 {0x3f00, 0x0b},
474 {0x3f03, 0x00},
475 {0x4001, 0xe0},
476 {0x4008, 0x00},
477 {0x4009, 0x0d},
478 {0x4011, 0xf0},
479 {0x4017, 0x08},
480 {0x4050, 0x04},
481 {0x4051, 0x0b},
482 {0x4052, 0x00},
483 {0x4053, 0x80},
484 {0x4054, 0x00},
485 {0x4055, 0x80},
486 {0x4056, 0x00},
487 {0x4057, 0x80},
488 {0x4058, 0x00},
489 {0x4059, 0x80},
490 {0x405e, 0x20},
491 {0x4500, 0x07},
492 {0x4503, 0x00},
493 {0x450a, 0x04},
494 {0x4809, 0x04},
495 {0x480c, 0x12},
496 {0x481f, 0x30},
497 {0x4833, 0x10},
498 {0x4837, 0x1c},
499 {0x4902, 0x01},
500 {0x4d00, 0x03},
501 {0x4d01, 0xc9},
502 {0x4d02, 0xbc},
503 {0x4d03, 0xd7},
504 {0x4d04, 0xf0},
505 {0x4d05, 0xa2},
506 {0x5000, 0xfd},
507 {0x5001, 0x01},
508 {0x5040, 0x39},
509 {0x5041, 0x10},
510 {0x5042, 0x10},
511 {0x5043, 0x84},
512 {0x5044, 0x62},
513 {0x5180, 0x00},
514 {0x5181, 0x10},
515 {0x5182, 0x02},
516 {0x5183, 0x0f},
517 {0x5200, 0x1b},
518 {0x520b, 0x07},
519 {0x520c, 0x0f},
520 {0x5300, 0x04},
521 {0x5301, 0x0c},
522 {0x5302, 0x0c},
523 {0x5303, 0x0f},
524 {0x5304, 0x00},
525 {0x5305, 0x70},
526 {0x5306, 0x00},
527 {0x5307, 0x80},
528 {0x5308, 0x00},
529 {0x5309, 0xa5},
530 {0x530a, 0x00},
531 {0x530b, 0xd3},
532 {0x530c, 0x00},
533 {0x530d, 0xf0},
534 {0x530e, 0x01},
535 {0x530f, 0x10},
536 {0x5310, 0x01},
537 {0x5311, 0x20},
538 {0x5312, 0x01},
539 {0x5313, 0x20},
540 {0x5314, 0x01},
541 {0x5315, 0x20},
542 {0x5316, 0x08},
543 {0x5317, 0x08},
544 {0x5318, 0x10},
545 {0x5319, 0x88},
546 {0x531a, 0x88},
547 {0x531b, 0xa9},
548 {0x531c, 0xaa},
549 {0x531d, 0x0a},
550 {0x5405, 0x02},
551 {0x5406, 0x67},
552 {0x5407, 0x01},
553 {0x5408, 0x4a},
554};
555
556static const struct ov13858_reg mode_2112x1188_regs[] = {
557 {0x3013, 0x32},
558 {0x301b, 0xf0},
559 {0x301f, 0xd0},
560 {0x3106, 0x15},
561 {0x3107, 0x23},
562 {0x350a, 0x00},
563 {0x350e, 0x00},
564 {0x3510, 0x00},
565 {0x3511, 0x02},
566 {0x3512, 0x00},
567 {0x3600, 0x2b},
568 {0x3601, 0x52},
569 {0x3602, 0x60},
570 {0x3612, 0x05},
571 {0x3613, 0xa4},
572 {0x3620, 0x80},
573 {0x3621, 0x10},
574 {0x3622, 0x30},
575 {0x3624, 0x1c},
576 {0x3640, 0x10},
577 {0x3641, 0x70},
578 {0x3661, 0x80},
579 {0x3662, 0x10},
580 {0x3664, 0x73},
581 {0x3665, 0xa7},
582 {0x366e, 0xff},
583 {0x366f, 0xf4},
584 {0x3674, 0x00},
585 {0x3679, 0x0c},
586 {0x367f, 0x01},
587 {0x3680, 0x0c},
588 {0x3681, 0x50},
589 {0x3682, 0x50},
590 {0x3683, 0xa9},
591 {0x3684, 0xa9},
592 {0x3709, 0x5f},
593 {0x3714, 0x28},
594 {0x371a, 0x3e},
595 {0x3737, 0x08},
596 {0x3738, 0xcc},
597 {0x3739, 0x20},
598 {0x373d, 0x26},
599 {0x3764, 0x20},
600 {0x3765, 0x20},
601 {0x37a1, 0x36},
602 {0x37a8, 0x3b},
603 {0x37ab, 0x31},
604 {0x37c2, 0x14},
605 {0x37c3, 0xf1},
606 {0x37c5, 0x00},
607 {0x37d8, 0x03},
608 {0x37d9, 0x0c},
609 {0x37da, 0xc2},
610 {0x37dc, 0x02},
611 {0x37e0, 0x00},
612 {0x37e1, 0x0a},
613 {0x37e2, 0x14},
614 {0x37e3, 0x08},
615 {0x37e4, 0x38},
616 {0x37e5, 0x03},
617 {0x37e6, 0x08},
618 {0x3800, 0x00},
619 {0x3801, 0x00},
620 {0x3802, 0x01},
621 {0x3803, 0x84},
622 {0x3804, 0x10},
623 {0x3805, 0x9f},
624 {0x3806, 0x0a},
625 {0x3807, 0xd3},
626 {0x3808, 0x08},
627 {0x3809, 0x40},
628 {0x380a, 0x04},
629 {0x380b, 0xa4},
630 {0x380c, 0x04},
631 {0x380d, 0x62},
632 {0x380e, 0x0c},
633 {0x380f, 0x8e},
634 {0x3811, 0x08},
635 {0x3813, 0x03},
636 {0x3814, 0x03},
637 {0x3815, 0x01},
638 {0x3816, 0x03},
639 {0x3817, 0x01},
640 {0x3820, 0xab},
641 {0x3821, 0x00},
642 {0x3822, 0xc2},
643 {0x3823, 0x18},
644 {0x3826, 0x04},
645 {0x3827, 0x90},
646 {0x3829, 0x07},
647 {0x3832, 0x00},
648 {0x3c80, 0x00},
649 {0x3c87, 0x01},
650 {0x3c8c, 0x19},
651 {0x3c8d, 0x1c},
652 {0x3c90, 0x00},
653 {0x3c91, 0x00},
654 {0x3c92, 0x00},
655 {0x3c93, 0x00},
656 {0x3c94, 0x40},
657 {0x3c95, 0x54},
658 {0x3c96, 0x34},
659 {0x3c97, 0x04},
660 {0x3c98, 0x00},
661 {0x3d8c, 0x73},
662 {0x3d8d, 0xc0},
663 {0x3f00, 0x0b},
664 {0x3f03, 0x00},
665 {0x4001, 0xe0},
666 {0x4008, 0x00},
667 {0x4009, 0x0d},
668 {0x4011, 0xf0},
669 {0x4017, 0x08},
670 {0x4050, 0x04},
671 {0x4051, 0x0b},
672 {0x4052, 0x00},
673 {0x4053, 0x80},
674 {0x4054, 0x00},
675 {0x4055, 0x80},
676 {0x4056, 0x00},
677 {0x4057, 0x80},
678 {0x4058, 0x00},
679 {0x4059, 0x80},
680 {0x405e, 0x20},
681 {0x4500, 0x07},
682 {0x4503, 0x00},
683 {0x450a, 0x04},
684 {0x4809, 0x04},
685 {0x480c, 0x12},
686 {0x481f, 0x30},
687 {0x4833, 0x10},
688 {0x4837, 0x1c},
689 {0x4902, 0x01},
690 {0x4d00, 0x03},
691 {0x4d01, 0xc9},
692 {0x4d02, 0xbc},
693 {0x4d03, 0xd7},
694 {0x4d04, 0xf0},
695 {0x4d05, 0xa2},
696 {0x5000, 0xfd},
697 {0x5001, 0x01},
698 {0x5040, 0x39},
699 {0x5041, 0x10},
700 {0x5042, 0x10},
701 {0x5043, 0x84},
702 {0x5044, 0x62},
703 {0x5180, 0x00},
704 {0x5181, 0x10},
705 {0x5182, 0x02},
706 {0x5183, 0x0f},
707 {0x5200, 0x1b},
708 {0x520b, 0x07},
709 {0x520c, 0x0f},
710 {0x5300, 0x04},
711 {0x5301, 0x0c},
712 {0x5302, 0x0c},
713 {0x5303, 0x0f},
714 {0x5304, 0x00},
715 {0x5305, 0x70},
716 {0x5306, 0x00},
717 {0x5307, 0x80},
718 {0x5308, 0x00},
719 {0x5309, 0xa5},
720 {0x530a, 0x00},
721 {0x530b, 0xd3},
722 {0x530c, 0x00},
723 {0x530d, 0xf0},
724 {0x530e, 0x01},
725 {0x530f, 0x10},
726 {0x5310, 0x01},
727 {0x5311, 0x20},
728 {0x5312, 0x01},
729 {0x5313, 0x20},
730 {0x5314, 0x01},
731 {0x5315, 0x20},
732 {0x5316, 0x08},
733 {0x5317, 0x08},
734 {0x5318, 0x10},
735 {0x5319, 0x88},
736 {0x531a, 0x88},
737 {0x531b, 0xa9},
738 {0x531c, 0xaa},
739 {0x531d, 0x0a},
740 {0x5405, 0x02},
741 {0x5406, 0x67},
742 {0x5407, 0x01},
743 {0x5408, 0x4a},
744};
745
746static const struct ov13858_reg mode_1056x784_regs[] = {
747 {0x3013, 0x32},
748 {0x301b, 0xf0},
749 {0x301f, 0xd0},
750 {0x3106, 0x15},
751 {0x3107, 0x23},
752 {0x350a, 0x00},
753 {0x350e, 0x00},
754 {0x3510, 0x00},
755 {0x3511, 0x02},
756 {0x3512, 0x00},
757 {0x3600, 0x2b},
758 {0x3601, 0x52},
759 {0x3602, 0x60},
760 {0x3612, 0x05},
761 {0x3613, 0xa4},
762 {0x3620, 0x80},
763 {0x3621, 0x10},
764 {0x3622, 0x30},
765 {0x3624, 0x1c},
766 {0x3640, 0x10},
767 {0x3641, 0x70},
768 {0x3661, 0x80},
769 {0x3662, 0x08},
770 {0x3664, 0x73},
771 {0x3665, 0xa7},
772 {0x366e, 0xff},
773 {0x366f, 0xf4},
774 {0x3674, 0x00},
775 {0x3679, 0x0c},
776 {0x367f, 0x01},
777 {0x3680, 0x0c},
778 {0x3681, 0x50},
779 {0x3682, 0x50},
780 {0x3683, 0xa9},
781 {0x3684, 0xa9},
782 {0x3709, 0x5f},
783 {0x3714, 0x30},
784 {0x371a, 0x3e},
785 {0x3737, 0x08},
786 {0x3738, 0xcc},
787 {0x3739, 0x20},
788 {0x373d, 0x26},
789 {0x3764, 0x20},
790 {0x3765, 0x20},
791 {0x37a1, 0x36},
792 {0x37a8, 0x3b},
793 {0x37ab, 0x31},
794 {0x37c2, 0x2c},
795 {0x37c3, 0xf1},
796 {0x37c5, 0x00},
797 {0x37d8, 0x03},
798 {0x37d9, 0x06},
799 {0x37da, 0xc2},
800 {0x37dc, 0x02},
801 {0x37e0, 0x00},
802 {0x37e1, 0x0a},
803 {0x37e2, 0x14},
804 {0x37e3, 0x08},
805 {0x37e4, 0x36},
806 {0x37e5, 0x03},
807 {0x37e6, 0x08},
808 {0x3800, 0x00},
809 {0x3801, 0x00},
810 {0x3802, 0x00},
811 {0x3803, 0x00},
812 {0x3804, 0x10},
813 {0x3805, 0x9f},
814 {0x3806, 0x0c},
815 {0x3807, 0x5f},
816 {0x3808, 0x04},
817 {0x3809, 0x20},
818 {0x380a, 0x03},
819 {0x380b, 0x10},
820 {0x380c, 0x04},
821 {0x380d, 0x62},
822 {0x380e, 0x0c},
823 {0x380f, 0x8e},
824 {0x3811, 0x04},
825 {0x3813, 0x05},
826 {0x3814, 0x07},
827 {0x3815, 0x01},
828 {0x3816, 0x07},
829 {0x3817, 0x01},
830 {0x3820, 0xac},
831 {0x3821, 0x00},
832 {0x3822, 0xc2},
833 {0x3823, 0x18},
834 {0x3826, 0x04},
835 {0x3827, 0x48},
836 {0x3829, 0x03},
837 {0x3832, 0x00},
838 {0x3c80, 0x00},
839 {0x3c87, 0x01},
840 {0x3c8c, 0x19},
841 {0x3c8d, 0x1c},
842 {0x3c90, 0x00},
843 {0x3c91, 0x00},
844 {0x3c92, 0x00},
845 {0x3c93, 0x00},
846 {0x3c94, 0x40},
847 {0x3c95, 0x54},
848 {0x3c96, 0x34},
849 {0x3c97, 0x04},
850 {0x3c98, 0x00},
851 {0x3d8c, 0x73},
852 {0x3d8d, 0xc0},
853 {0x3f00, 0x0b},
854 {0x3f03, 0x00},
855 {0x4001, 0xe0},
856 {0x4008, 0x00},
857 {0x4009, 0x05},
858 {0x4011, 0xf0},
859 {0x4017, 0x08},
860 {0x4050, 0x02},
861 {0x4051, 0x05},
862 {0x4052, 0x00},
863 {0x4053, 0x80},
864 {0x4054, 0x00},
865 {0x4055, 0x80},
866 {0x4056, 0x00},
867 {0x4057, 0x80},
868 {0x4058, 0x00},
869 {0x4059, 0x80},
870 {0x405e, 0x20},
871 {0x4500, 0x07},
872 {0x4503, 0x00},
873 {0x450a, 0x04},
874 {0x4809, 0x04},
875 {0x480c, 0x12},
876 {0x481f, 0x30},
877 {0x4833, 0x10},
878 {0x4837, 0x1e},
879 {0x4902, 0x02},
880 {0x4d00, 0x03},
881 {0x4d01, 0xc9},
882 {0x4d02, 0xbc},
883 {0x4d03, 0xd7},
884 {0x4d04, 0xf0},
885 {0x4d05, 0xa2},
886 {0x5000, 0xfd},
887 {0x5001, 0x01},
888 {0x5040, 0x39},
889 {0x5041, 0x10},
890 {0x5042, 0x10},
891 {0x5043, 0x84},
892 {0x5044, 0x62},
893 {0x5180, 0x00},
894 {0x5181, 0x10},
895 {0x5182, 0x02},
896 {0x5183, 0x0f},
897 {0x5200, 0x1b},
898 {0x520b, 0x07},
899 {0x520c, 0x0f},
900 {0x5300, 0x04},
901 {0x5301, 0x0c},
902 {0x5302, 0x0c},
903 {0x5303, 0x0f},
904 {0x5304, 0x00},
905 {0x5305, 0x70},
906 {0x5306, 0x00},
907 {0x5307, 0x80},
908 {0x5308, 0x00},
909 {0x5309, 0xa5},
910 {0x530a, 0x00},
911 {0x530b, 0xd3},
912 {0x530c, 0x00},
913 {0x530d, 0xf0},
914 {0x530e, 0x01},
915 {0x530f, 0x10},
916 {0x5310, 0x01},
917 {0x5311, 0x20},
918 {0x5312, 0x01},
919 {0x5313, 0x20},
920 {0x5314, 0x01},
921 {0x5315, 0x20},
922 {0x5316, 0x08},
923 {0x5317, 0x08},
924 {0x5318, 0x10},
925 {0x5319, 0x88},
926 {0x531a, 0x88},
927 {0x531b, 0xa9},
928 {0x531c, 0xaa},
929 {0x531d, 0x0a},
930 {0x5405, 0x02},
931 {0x5406, 0x67},
932 {0x5407, 0x01},
933 {0x5408, 0x4a},
934};
935
936static const char * const ov13858_test_pattern_menu[] = {
937 "Disabled",
938 "Vertical Color Bar Type 1",
939 "Vertical Color Bar Type 2",
940 "Vertical Color Bar Type 3",
941 "Vertical Color Bar Type 4"
942};
943
944/* Configurations for supported link frequencies */
945#define OV13858_NUM_OF_LINK_FREQS 2
Chiranjeevi Rapolu89d8b612017-07-29 03:00:39 -0400946#define OV13858_LINK_FREQ_540MHZ 540000000ULL
947#define OV13858_LINK_FREQ_270MHZ 270000000ULL
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -0300948#define OV13858_LINK_FREQ_INDEX_0 0
949#define OV13858_LINK_FREQ_INDEX_1 1
950
951/* Menu items for LINK_FREQ V4L2 control */
Mauro Carvalho Chehab3bd30b22017-06-20 08:19:56 -0300952static const s64 link_freq_menu_items[OV13858_NUM_OF_LINK_FREQS] = {
Chiranjeevi Rapolu89d8b612017-07-29 03:00:39 -0400953 OV13858_LINK_FREQ_540MHZ,
954 OV13858_LINK_FREQ_270MHZ
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -0300955};
956
957/* Link frequency configs */
958static const struct ov13858_link_freq_config
959 link_freq_configs[OV13858_NUM_OF_LINK_FREQS] = {
960 {
Chiranjeevi Rapolu89d8b612017-07-29 03:00:39 -0400961 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
962 .pixel_rate = (OV13858_LINK_FREQ_540MHZ * 2 * 4) / 10,
963 .pixels_per_line = OV13858_PPL_540MHZ,
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -0300964 .reg_list = {
965 .num_of_regs = ARRAY_SIZE(mipi_data_rate_1080mbps),
966 .regs = mipi_data_rate_1080mbps,
967 }
968 },
969 {
Chiranjeevi Rapolu89d8b612017-07-29 03:00:39 -0400970 /* pixel_rate = link_freq * 2 * nr_of_lanes / bits_per_sample */
971 .pixel_rate = (OV13858_LINK_FREQ_270MHZ * 2 * 4) / 10,
972 .pixels_per_line = OV13858_PPL_270MHZ,
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -0300973 .reg_list = {
974 .num_of_regs = ARRAY_SIZE(mipi_data_rate_540mbps),
975 .regs = mipi_data_rate_540mbps,
976 }
977 }
978};
979
980/* Mode configs */
981static const struct ov13858_mode supported_modes[] = {
982 {
983 .width = 4224,
984 .height = 3136,
985 .vts = OV13858_VTS_30FPS,
986 .reg_list = {
987 .num_of_regs = ARRAY_SIZE(mode_4224x3136_regs),
988 .regs = mode_4224x3136_regs,
989 },
990 .link_freq_index = OV13858_LINK_FREQ_INDEX_0,
991 },
992 {
993 .width = 2112,
994 .height = 1568,
995 .vts = OV13858_VTS_30FPS,
996 .reg_list = {
997 .num_of_regs = ARRAY_SIZE(mode_2112x1568_regs),
998 .regs = mode_2112x1568_regs,
999 },
1000 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1001 },
1002 {
1003 .width = 2112,
1004 .height = 1188,
1005 .vts = OV13858_VTS_30FPS,
1006 .reg_list = {
1007 .num_of_regs = ARRAY_SIZE(mode_2112x1188_regs),
1008 .regs = mode_2112x1188_regs,
1009 },
1010 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1011 },
1012 {
1013 .width = 1056,
1014 .height = 784,
1015 .vts = OV13858_VTS_30FPS,
1016 .reg_list = {
1017 .num_of_regs = ARRAY_SIZE(mode_1056x784_regs),
1018 .regs = mode_1056x784_regs,
1019 },
1020 .link_freq_index = OV13858_LINK_FREQ_INDEX_1,
1021 }
1022};
1023
1024struct ov13858 {
1025 struct v4l2_subdev sd;
1026 struct media_pad pad;
1027
1028 struct v4l2_ctrl_handler ctrl_handler;
1029 /* V4L2 Controls */
1030 struct v4l2_ctrl *link_freq;
1031 struct v4l2_ctrl *pixel_rate;
1032 struct v4l2_ctrl *vblank;
1033 struct v4l2_ctrl *hblank;
1034 struct v4l2_ctrl *exposure;
1035
1036 /* Current mode */
1037 const struct ov13858_mode *cur_mode;
1038
1039 /* Mutex for serialized access */
1040 struct mutex mutex;
1041
1042 /* Streaming on/off */
1043 bool streaming;
1044};
1045
1046#define to_ov13858(_sd) container_of(_sd, struct ov13858, sd)
1047
1048/* Read registers up to 4 at a time */
1049static int ov13858_read_reg(struct ov13858 *ov13858, u16 reg, u32 len, u32 *val)
1050{
1051 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1052 struct i2c_msg msgs[2];
1053 u8 *data_be_p;
1054 int ret;
1055 u32 data_be = 0;
1056 u16 reg_addr_be = cpu_to_be16(reg);
1057
1058 if (len > 4)
1059 return -EINVAL;
1060
1061 data_be_p = (u8 *)&data_be;
1062 /* Write register address */
1063 msgs[0].addr = client->addr;
1064 msgs[0].flags = 0;
1065 msgs[0].len = 2;
1066 msgs[0].buf = (u8 *)&reg_addr_be;
1067
1068 /* Read data from register */
1069 msgs[1].addr = client->addr;
1070 msgs[1].flags = I2C_M_RD;
1071 msgs[1].len = len;
1072 msgs[1].buf = &data_be_p[4 - len];
1073
1074 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1075 if (ret != ARRAY_SIZE(msgs))
1076 return -EIO;
1077
1078 *val = be32_to_cpu(data_be);
1079
1080 return 0;
1081}
1082
1083/* Write registers up to 4 at a time */
1084static int ov13858_write_reg(struct ov13858 *ov13858, u16 reg, u32 len, u32 val)
1085{
1086 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1087 int buf_i, val_i;
1088 u8 buf[6], *val_p;
1089
1090 if (len > 4)
1091 return -EINVAL;
1092
1093 buf[0] = reg >> 8;
1094 buf[1] = reg & 0xff;
1095
1096 val = cpu_to_be32(val);
1097 val_p = (u8 *)&val;
1098 buf_i = 2;
1099 val_i = 4 - len;
1100
1101 while (val_i < 4)
1102 buf[buf_i++] = val_p[val_i++];
1103
1104 if (i2c_master_send(client, buf, len + 2) != len + 2)
1105 return -EIO;
1106
1107 return 0;
1108}
1109
1110/* Write a list of registers */
1111static int ov13858_write_regs(struct ov13858 *ov13858,
1112 const struct ov13858_reg *regs, u32 len)
1113{
1114 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1115 int ret;
1116 u32 i;
1117
1118 for (i = 0; i < len; i++) {
1119 ret = ov13858_write_reg(ov13858, regs[i].address, 1,
1120 regs[i].val);
1121 if (ret) {
1122 dev_err_ratelimited(
1123 &client->dev,
1124 "Failed to write reg 0x%4.4x. error = %d\n",
1125 regs[i].address, ret);
1126
1127 return ret;
1128 }
1129 }
1130
1131 return 0;
1132}
1133
1134static int ov13858_write_reg_list(struct ov13858 *ov13858,
1135 const struct ov13858_reg_list *r_list)
1136{
1137 return ov13858_write_regs(ov13858, r_list->regs, r_list->num_of_regs);
1138}
1139
1140/* Open sub-device */
1141static int ov13858_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1142{
1143 struct ov13858 *ov13858 = to_ov13858(sd);
1144 struct v4l2_mbus_framefmt *try_fmt = v4l2_subdev_get_try_format(sd,
1145 fh->pad,
1146 0);
1147
1148 mutex_lock(&ov13858->mutex);
1149
1150 /* Initialize try_fmt */
1151 try_fmt->width = ov13858->cur_mode->width;
1152 try_fmt->height = ov13858->cur_mode->height;
1153 try_fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1154 try_fmt->field = V4L2_FIELD_NONE;
1155
1156 /* No crop or compose */
1157 mutex_unlock(&ov13858->mutex);
1158
1159 return 0;
1160}
1161
1162static int ov13858_update_digital_gain(struct ov13858 *ov13858, u32 d_gain)
1163{
1164 int ret;
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001165
Chiranjeevi Rapolubfced6d2017-07-28 19:21:03 -04001166 ret = ov13858_write_reg(ov13858, OV13858_REG_B_MWB_GAIN,
1167 OV13858_REG_VALUE_16BIT, d_gain);
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001168 if (ret)
1169 return ret;
1170
Chiranjeevi Rapolubfced6d2017-07-28 19:21:03 -04001171 ret = ov13858_write_reg(ov13858, OV13858_REG_G_MWB_GAIN,
1172 OV13858_REG_VALUE_16BIT, d_gain);
1173 if (ret)
1174 return ret;
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001175
Chiranjeevi Rapolubfced6d2017-07-28 19:21:03 -04001176 ret = ov13858_write_reg(ov13858, OV13858_REG_R_MWB_GAIN,
1177 OV13858_REG_VALUE_16BIT, d_gain);
1178
1179 return ret;
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001180}
1181
1182static int ov13858_enable_test_pattern(struct ov13858 *ov13858, u32 pattern)
1183{
1184 int ret;
1185 u32 val;
1186
1187 ret = ov13858_read_reg(ov13858, OV13858_REG_TEST_PATTERN,
1188 OV13858_REG_VALUE_08BIT, &val);
1189 if (ret)
1190 return ret;
1191
1192 if (pattern) {
1193 val &= OV13858_TEST_PATTERN_MASK;
1194 val |= (pattern - 1) | OV13858_TEST_PATTERN_ENABLE;
1195 } else {
1196 val &= ~OV13858_TEST_PATTERN_ENABLE;
1197 }
1198
1199 return ov13858_write_reg(ov13858, OV13858_REG_TEST_PATTERN,
1200 OV13858_REG_VALUE_08BIT, val);
1201}
1202
1203static int ov13858_set_ctrl(struct v4l2_ctrl *ctrl)
1204{
1205 struct ov13858 *ov13858 = container_of(ctrl->handler,
1206 struct ov13858, ctrl_handler);
1207 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1208 s64 max;
1209 int ret;
1210
1211 /* Propagate change of current control to all related controls */
1212 switch (ctrl->id) {
1213 case V4L2_CID_VBLANK:
1214 /* Update max exposure while meeting expected vblanking */
1215 max = ov13858->cur_mode->height + ctrl->val - 8;
1216 __v4l2_ctrl_modify_range(ov13858->exposure,
1217 ov13858->exposure->minimum,
1218 max, ov13858->exposure->step, max);
1219 break;
1220 };
1221
1222 /*
1223 * Applying V4L2 control value only happens
1224 * when power is up for streaming
1225 */
1226 if (pm_runtime_get_if_in_use(&client->dev) <= 0)
1227 return 0;
1228
1229 ret = 0;
1230 switch (ctrl->id) {
1231 case V4L2_CID_ANALOGUE_GAIN:
1232 ret = ov13858_write_reg(ov13858, OV13858_REG_ANALOG_GAIN,
1233 OV13858_REG_VALUE_16BIT, ctrl->val);
1234 break;
1235 case V4L2_CID_DIGITAL_GAIN:
1236 ret = ov13858_update_digital_gain(ov13858, ctrl->val);
1237 break;
1238 case V4L2_CID_EXPOSURE:
1239 ret = ov13858_write_reg(ov13858, OV13858_REG_EXPOSURE,
1240 OV13858_REG_VALUE_24BIT,
1241 ctrl->val << 4);
1242 break;
1243 case V4L2_CID_VBLANK:
1244 /* Update VTS that meets expected vertical blanking */
1245 ret = ov13858_write_reg(ov13858, OV13858_REG_VTS,
1246 OV13858_REG_VALUE_16BIT,
1247 ov13858->cur_mode->height
1248 + ctrl->val);
1249 break;
1250 case V4L2_CID_TEST_PATTERN:
1251 ret = ov13858_enable_test_pattern(ov13858, ctrl->val);
1252 break;
1253 default:
1254 dev_info(&client->dev,
1255 "ctrl(id:0x%x,val:0x%x) is not handled\n",
1256 ctrl->id, ctrl->val);
1257 break;
1258 };
1259
1260 pm_runtime_put(&client->dev);
1261
1262 return ret;
1263}
1264
1265static const struct v4l2_ctrl_ops ov13858_ctrl_ops = {
1266 .s_ctrl = ov13858_set_ctrl,
1267};
1268
1269static int ov13858_enum_mbus_code(struct v4l2_subdev *sd,
1270 struct v4l2_subdev_pad_config *cfg,
1271 struct v4l2_subdev_mbus_code_enum *code)
1272{
1273 /* Only one bayer order(GRBG) is supported */
1274 if (code->index > 0)
1275 return -EINVAL;
1276
1277 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1278
1279 return 0;
1280}
1281
1282static int ov13858_enum_frame_size(struct v4l2_subdev *sd,
1283 struct v4l2_subdev_pad_config *cfg,
1284 struct v4l2_subdev_frame_size_enum *fse)
1285{
1286 if (fse->index >= ARRAY_SIZE(supported_modes))
1287 return -EINVAL;
1288
1289 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1290 return -EINVAL;
1291
1292 fse->min_width = supported_modes[fse->index].width;
1293 fse->max_width = fse->min_width;
1294 fse->min_height = supported_modes[fse->index].height;
1295 fse->max_height = fse->min_height;
1296
1297 return 0;
1298}
1299
1300static void ov13858_update_pad_format(const struct ov13858_mode *mode,
1301 struct v4l2_subdev_format *fmt)
1302{
1303 fmt->format.width = mode->width;
1304 fmt->format.height = mode->height;
1305 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1306 fmt->format.field = V4L2_FIELD_NONE;
1307}
1308
1309static int ov13858_do_get_pad_format(struct ov13858 *ov13858,
1310 struct v4l2_subdev_pad_config *cfg,
1311 struct v4l2_subdev_format *fmt)
1312{
1313 struct v4l2_mbus_framefmt *framefmt;
1314 struct v4l2_subdev *sd = &ov13858->sd;
1315
1316 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1317 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1318 fmt->format = *framefmt;
1319 } else {
1320 ov13858_update_pad_format(ov13858->cur_mode, fmt);
1321 }
1322
1323 return 0;
1324}
1325
1326static int ov13858_get_pad_format(struct v4l2_subdev *sd,
1327 struct v4l2_subdev_pad_config *cfg,
1328 struct v4l2_subdev_format *fmt)
1329{
1330 struct ov13858 *ov13858 = to_ov13858(sd);
1331 int ret;
1332
1333 mutex_lock(&ov13858->mutex);
1334 ret = ov13858_do_get_pad_format(ov13858, cfg, fmt);
1335 mutex_unlock(&ov13858->mutex);
1336
1337 return ret;
1338}
1339
1340/*
1341 * Calculate resolution distance
1342 */
1343static int
1344ov13858_get_resolution_dist(const struct ov13858_mode *mode,
1345 struct v4l2_mbus_framefmt *framefmt)
1346{
1347 return abs(mode->width - framefmt->width) +
1348 abs(mode->height - framefmt->height);
1349}
1350
1351/*
1352 * Find the closest supported resolution to the requested resolution
1353 */
1354static const struct ov13858_mode *
1355ov13858_find_best_fit(struct ov13858 *ov13858,
1356 struct v4l2_subdev_format *fmt)
1357{
1358 int i, dist, cur_best_fit = 0, cur_best_fit_dist = -1;
1359 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
1360
1361 for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
1362 dist = ov13858_get_resolution_dist(&supported_modes[i],
1363 framefmt);
1364 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
1365 cur_best_fit_dist = dist;
1366 cur_best_fit = i;
1367 }
1368 }
1369
1370 return &supported_modes[cur_best_fit];
1371}
1372
1373static int
1374ov13858_set_pad_format(struct v4l2_subdev *sd,
1375 struct v4l2_subdev_pad_config *cfg,
1376 struct v4l2_subdev_format *fmt)
1377{
1378 struct ov13858 *ov13858 = to_ov13858(sd);
1379 const struct ov13858_mode *mode;
1380 struct v4l2_mbus_framefmt *framefmt;
Chiranjeevi Rapolub3775ed2017-07-27 03:28:05 -04001381 s32 vblank_def;
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001382 s64 h_blank;
1383
1384 mutex_lock(&ov13858->mutex);
1385
1386 /* Only one raw bayer(GRBG) order is supported */
1387 if (fmt->format.code != MEDIA_BUS_FMT_SGRBG10_1X10)
1388 fmt->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1389
1390 mode = ov13858_find_best_fit(ov13858, fmt);
1391 ov13858_update_pad_format(mode, fmt);
1392 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1393 framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1394 *framefmt = fmt->format;
1395 } else {
1396 ov13858->cur_mode = mode;
1397 __v4l2_ctrl_s_ctrl(ov13858->link_freq, mode->link_freq_index);
1398 __v4l2_ctrl_s_ctrl_int64(
1399 ov13858->pixel_rate,
1400 link_freq_configs[mode->link_freq_index].pixel_rate);
1401 /* Update limits and set FPS to default */
Chiranjeevi Rapolub3775ed2017-07-27 03:28:05 -04001402 vblank_def = ov13858->cur_mode->vts - ov13858->cur_mode->height;
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001403 __v4l2_ctrl_modify_range(
1404 ov13858->vblank, OV13858_VBLANK_MIN,
1405 OV13858_VTS_MAX - ov13858->cur_mode->height, 1,
Chiranjeevi Rapolub3775ed2017-07-27 03:28:05 -04001406 vblank_def);
1407 __v4l2_ctrl_s_ctrl(ov13858->vblank, vblank_def);
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001408 h_blank =
1409 link_freq_configs[mode->link_freq_index].pixels_per_line
1410 - ov13858->cur_mode->width;
1411 __v4l2_ctrl_modify_range(ov13858->hblank, h_blank,
1412 h_blank, 1, h_blank);
1413 }
1414
1415 mutex_unlock(&ov13858->mutex);
1416
1417 return 0;
1418}
1419
1420static int ov13858_get_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1421{
1422 *frames = OV13858_NUM_OF_SKIP_FRAMES;
1423
1424 return 0;
1425}
1426
1427/* Start streaming */
1428static int ov13858_start_streaming(struct ov13858 *ov13858)
1429{
1430 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1431 const struct ov13858_reg_list *reg_list;
1432 int ret, link_freq_index;
1433
1434 /* Get out of from software reset */
1435 ret = ov13858_write_reg(ov13858, OV13858_REG_SOFTWARE_RST,
1436 OV13858_REG_VALUE_08BIT, OV13858_SOFTWARE_RST);
1437 if (ret) {
1438 dev_err(&client->dev, "%s failed to set powerup registers\n",
1439 __func__);
1440 return ret;
1441 }
1442
1443 /* Setup PLL */
1444 link_freq_index = ov13858->cur_mode->link_freq_index;
1445 reg_list = &link_freq_configs[link_freq_index].reg_list;
1446 ret = ov13858_write_reg_list(ov13858, reg_list);
1447 if (ret) {
1448 dev_err(&client->dev, "%s failed to set plls\n", __func__);
1449 return ret;
1450 }
1451
1452 /* Apply default values of current mode */
1453 reg_list = &ov13858->cur_mode->reg_list;
1454 ret = ov13858_write_reg_list(ov13858, reg_list);
1455 if (ret) {
1456 dev_err(&client->dev, "%s failed to set mode\n", __func__);
1457 return ret;
1458 }
1459
1460 /* Apply customized values from user */
1461 ret = __v4l2_ctrl_handler_setup(ov13858->sd.ctrl_handler);
1462 if (ret)
1463 return ret;
1464
1465 return ov13858_write_reg(ov13858, OV13858_REG_MODE_SELECT,
1466 OV13858_REG_VALUE_08BIT,
1467 OV13858_MODE_STREAMING);
1468}
1469
1470/* Stop streaming */
1471static int ov13858_stop_streaming(struct ov13858 *ov13858)
1472{
1473 return ov13858_write_reg(ov13858, OV13858_REG_MODE_SELECT,
1474 OV13858_REG_VALUE_08BIT, OV13858_MODE_STANDBY);
1475}
1476
1477static int ov13858_set_stream(struct v4l2_subdev *sd, int enable)
1478{
1479 struct ov13858 *ov13858 = to_ov13858(sd);
1480 struct i2c_client *client = v4l2_get_subdevdata(sd);
1481 int ret = 0;
1482
1483 mutex_lock(&ov13858->mutex);
1484 if (ov13858->streaming == enable) {
1485 mutex_unlock(&ov13858->mutex);
1486 return 0;
1487 }
1488
1489 if (enable) {
1490 ret = pm_runtime_get_sync(&client->dev);
1491 if (ret < 0) {
1492 pm_runtime_put_noidle(&client->dev);
1493 goto err_unlock;
1494 }
1495
1496 /*
1497 * Apply default & customized values
1498 * and then start streaming.
1499 */
1500 ret = ov13858_start_streaming(ov13858);
1501 if (ret)
1502 goto err_rpm_put;
1503 } else {
1504 ov13858_stop_streaming(ov13858);
1505 pm_runtime_put(&client->dev);
1506 }
1507
1508 ov13858->streaming = enable;
1509 mutex_unlock(&ov13858->mutex);
1510
1511 return ret;
1512
1513err_rpm_put:
1514 pm_runtime_put(&client->dev);
1515err_unlock:
1516 mutex_unlock(&ov13858->mutex);
1517
1518 return ret;
1519}
1520
1521static int __maybe_unused ov13858_suspend(struct device *dev)
1522{
1523 struct i2c_client *client = to_i2c_client(dev);
1524 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1525 struct ov13858 *ov13858 = to_ov13858(sd);
1526
1527 if (ov13858->streaming)
1528 ov13858_stop_streaming(ov13858);
1529
1530 return 0;
1531}
1532
1533static int __maybe_unused ov13858_resume(struct device *dev)
1534{
1535 struct i2c_client *client = to_i2c_client(dev);
1536 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1537 struct ov13858 *ov13858 = to_ov13858(sd);
1538 int ret;
1539
1540 if (ov13858->streaming) {
1541 ret = ov13858_start_streaming(ov13858);
1542 if (ret)
1543 goto error;
1544 }
1545
1546 return 0;
1547
1548error:
1549 ov13858_stop_streaming(ov13858);
1550 ov13858->streaming = 0;
1551 return ret;
1552}
1553
1554/* Verify chip ID */
1555static int ov13858_identify_module(struct ov13858 *ov13858)
1556{
1557 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1558 int ret;
1559 u32 val;
1560
1561 ret = ov13858_read_reg(ov13858, OV13858_REG_CHIP_ID,
1562 OV13858_REG_VALUE_24BIT, &val);
1563 if (ret)
1564 return ret;
1565
1566 if (val != OV13858_CHIP_ID) {
1567 dev_err(&client->dev, "chip id mismatch: %x!=%x\n",
1568 OV13858_CHIP_ID, val);
1569 return -EIO;
1570 }
1571
1572 return 0;
1573}
1574
1575static const struct v4l2_subdev_video_ops ov13858_video_ops = {
1576 .s_stream = ov13858_set_stream,
1577};
1578
1579static const struct v4l2_subdev_pad_ops ov13858_pad_ops = {
1580 .enum_mbus_code = ov13858_enum_mbus_code,
1581 .get_fmt = ov13858_get_pad_format,
1582 .set_fmt = ov13858_set_pad_format,
1583 .enum_frame_size = ov13858_enum_frame_size,
1584};
1585
1586static const struct v4l2_subdev_sensor_ops ov13858_sensor_ops = {
1587 .g_skip_frames = ov13858_get_skip_frames,
1588};
1589
1590static const struct v4l2_subdev_ops ov13858_subdev_ops = {
1591 .video = &ov13858_video_ops,
1592 .pad = &ov13858_pad_ops,
1593 .sensor = &ov13858_sensor_ops,
1594};
1595
1596static const struct media_entity_operations ov13858_subdev_entity_ops = {
1597 .link_validate = v4l2_subdev_link_validate,
1598};
1599
1600static const struct v4l2_subdev_internal_ops ov13858_internal_ops = {
1601 .open = ov13858_open,
1602};
1603
1604/* Initialize control handlers */
1605static int ov13858_init_controls(struct ov13858 *ov13858)
1606{
1607 struct i2c_client *client = v4l2_get_subdevdata(&ov13858->sd);
1608 struct v4l2_ctrl_handler *ctrl_hdlr;
Chiranjeevi Rapolu33eea132017-07-27 03:44:19 -04001609 s64 exposure_max;
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001610 int ret;
1611
1612 ctrl_hdlr = &ov13858->ctrl_handler;
1613 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
1614 if (ret)
1615 return ret;
1616
1617 mutex_init(&ov13858->mutex);
1618 ctrl_hdlr->lock = &ov13858->mutex;
1619 ov13858->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr,
1620 &ov13858_ctrl_ops,
1621 V4L2_CID_LINK_FREQ,
1622 OV13858_NUM_OF_LINK_FREQS - 1,
1623 0,
1624 link_freq_menu_items);
1625 ov13858->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1626
1627 /* By default, PIXEL_RATE is read only */
1628 ov13858->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops,
1629 V4L2_CID_PIXEL_RATE, 0,
1630 link_freq_configs[0].pixel_rate, 1,
1631 link_freq_configs[0].pixel_rate);
1632
1633 ov13858->vblank = v4l2_ctrl_new_std(
1634 ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_VBLANK,
1635 OV13858_VBLANK_MIN,
1636 OV13858_VTS_MAX - ov13858->cur_mode->height, 1,
1637 ov13858->cur_mode->vts
1638 - ov13858->cur_mode->height);
1639
1640 ov13858->hblank = v4l2_ctrl_new_std(
1641 ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_HBLANK,
Chiranjeevi Rapolu89d8b612017-07-29 03:00:39 -04001642 OV13858_PPL_540MHZ - ov13858->cur_mode->width,
1643 OV13858_PPL_540MHZ - ov13858->cur_mode->width,
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001644 1,
Chiranjeevi Rapolu89d8b612017-07-29 03:00:39 -04001645 OV13858_PPL_540MHZ - ov13858->cur_mode->width);
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001646 ov13858->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1647
Chiranjeevi Rapolu33eea132017-07-27 03:44:19 -04001648 exposure_max = ov13858->cur_mode->vts - 8;
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001649 ov13858->exposure = v4l2_ctrl_new_std(
1650 ctrl_hdlr, &ov13858_ctrl_ops,
1651 V4L2_CID_EXPOSURE, OV13858_EXPOSURE_MIN,
Chiranjeevi Rapolu33eea132017-07-27 03:44:19 -04001652 exposure_max, OV13858_EXPOSURE_STEP,
Hyungwoo Yang5fcf0922017-06-13 19:06:16 -03001653 OV13858_EXPOSURE_DEFAULT);
1654
1655 v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1656 OV13858_ANA_GAIN_MIN, OV13858_ANA_GAIN_MAX,
1657 OV13858_ANA_GAIN_STEP, OV13858_ANA_GAIN_DEFAULT);
1658
1659 /* Digital gain */
1660 v4l2_ctrl_new_std(ctrl_hdlr, &ov13858_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1661 OV13858_DGTL_GAIN_MIN, OV13858_DGTL_GAIN_MAX,
1662 OV13858_DGTL_GAIN_STEP, OV13858_DGTL_GAIN_DEFAULT);
1663
1664 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov13858_ctrl_ops,
1665 V4L2_CID_TEST_PATTERN,
1666 ARRAY_SIZE(ov13858_test_pattern_menu) - 1,
1667 0, 0, ov13858_test_pattern_menu);
1668 if (ctrl_hdlr->error) {
1669 ret = ctrl_hdlr->error;
1670 dev_err(&client->dev, "%s control init failed (%d)\n",
1671 __func__, ret);
1672 goto error;
1673 }
1674
1675 ov13858->sd.ctrl_handler = ctrl_hdlr;
1676
1677 return 0;
1678
1679error:
1680 v4l2_ctrl_handler_free(ctrl_hdlr);
1681 mutex_destroy(&ov13858->mutex);
1682
1683 return ret;
1684}
1685
1686static void ov13858_free_controls(struct ov13858 *ov13858)
1687{
1688 v4l2_ctrl_handler_free(ov13858->sd.ctrl_handler);
1689 mutex_destroy(&ov13858->mutex);
1690}
1691
1692static int ov13858_probe(struct i2c_client *client,
1693 const struct i2c_device_id *devid)
1694{
1695 struct ov13858 *ov13858;
1696 int ret;
1697 u32 val = 0;
1698
1699 device_property_read_u32(&client->dev, "clock-frequency", &val);
1700 if (val != 19200000)
1701 return -EINVAL;
1702
1703 ov13858 = devm_kzalloc(&client->dev, sizeof(*ov13858), GFP_KERNEL);
1704 if (!ov13858)
1705 return -ENOMEM;
1706
1707 /* Initialize subdev */
1708 v4l2_i2c_subdev_init(&ov13858->sd, client, &ov13858_subdev_ops);
1709
1710 /* Check module identity */
1711 ret = ov13858_identify_module(ov13858);
1712 if (ret) {
1713 dev_err(&client->dev, "failed to find sensor: %d\n", ret);
1714 return ret;
1715 }
1716
1717 /* Set default mode to max resolution */
1718 ov13858->cur_mode = &supported_modes[0];
1719
1720 ret = ov13858_init_controls(ov13858);
1721 if (ret)
1722 return ret;
1723
1724 /* Initialize subdev */
1725 ov13858->sd.internal_ops = &ov13858_internal_ops;
1726 ov13858->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1727 ov13858->sd.entity.ops = &ov13858_subdev_entity_ops;
1728 ov13858->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1729
1730 /* Initialize source pad */
1731 ov13858->pad.flags = MEDIA_PAD_FL_SOURCE;
1732 ret = media_entity_pads_init(&ov13858->sd.entity, 1, &ov13858->pad);
1733 if (ret) {
1734 dev_err(&client->dev, "%s failed:%d\n", __func__, ret);
1735 goto error_handler_free;
1736 }
1737
1738 ret = v4l2_async_register_subdev(&ov13858->sd);
1739 if (ret < 0)
1740 goto error_media_entity;
1741
1742 /*
1743 * Device is already turned on by i2c-core with ACPI domain PM.
1744 * Enable runtime PM and turn off the device.
1745 */
1746 pm_runtime_get_noresume(&client->dev);
1747 pm_runtime_set_active(&client->dev);
1748 pm_runtime_enable(&client->dev);
1749 pm_runtime_put(&client->dev);
1750
1751 return 0;
1752
1753error_media_entity:
1754 media_entity_cleanup(&ov13858->sd.entity);
1755
1756error_handler_free:
1757 ov13858_free_controls(ov13858);
1758 dev_err(&client->dev, "%s failed:%d\n", __func__, ret);
1759
1760 return ret;
1761}
1762
1763static int ov13858_remove(struct i2c_client *client)
1764{
1765 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1766 struct ov13858 *ov13858 = to_ov13858(sd);
1767
1768 v4l2_async_unregister_subdev(sd);
1769 media_entity_cleanup(&sd->entity);
1770 ov13858_free_controls(ov13858);
1771
1772 /*
1773 * Disable runtime PM but keep the device turned on.
1774 * i2c-core with ACPI domain PM will turn off the device.
1775 */
1776 pm_runtime_get_sync(&client->dev);
1777 pm_runtime_disable(&client->dev);
1778 pm_runtime_set_suspended(&client->dev);
1779 pm_runtime_put_noidle(&client->dev);
1780
1781 return 0;
1782}
1783
1784static const struct i2c_device_id ov13858_id_table[] = {
1785 {"ov13858", 0},
1786 {},
1787};
1788
1789MODULE_DEVICE_TABLE(i2c, ov13858_id_table);
1790
1791static const struct dev_pm_ops ov13858_pm_ops = {
1792 SET_SYSTEM_SLEEP_PM_OPS(ov13858_suspend, ov13858_resume)
1793};
1794
1795#ifdef CONFIG_ACPI
1796static const struct acpi_device_id ov13858_acpi_ids[] = {
1797 {"OVTID858"},
1798 { /* sentinel */ }
1799};
1800
1801MODULE_DEVICE_TABLE(acpi, ov13858_acpi_ids);
1802#endif
1803
1804static struct i2c_driver ov13858_i2c_driver = {
1805 .driver = {
1806 .name = "ov13858",
1807 .owner = THIS_MODULE,
1808 .pm = &ov13858_pm_ops,
1809 .acpi_match_table = ACPI_PTR(ov13858_acpi_ids),
1810 },
1811 .probe = ov13858_probe,
1812 .remove = ov13858_remove,
1813 .id_table = ov13858_id_table,
1814};
1815
1816module_i2c_driver(ov13858_i2c_driver);
1817
1818MODULE_AUTHOR("Kan, Chris <chris.kan@intel.com>");
1819MODULE_AUTHOR("Rapolu, Chiranjeevi <chiranjeevi.rapolu@intel.com>");
1820MODULE_AUTHOR("Yang, Hyungwoo <hyungwoo.yang@intel.com>");
1821MODULE_DESCRIPTION("Omnivision ov13858 sensor driver");
1822MODULE_LICENSE("GPL v2");