1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
3
4 #include <asm/unaligned.h>
5 #include <linux/acpi.h>
6 #include <linux/clk.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/i2c.h>
10 #include <linux/module.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regulator/consumer.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-fwnode.h>
16
17 #define OV8856_REG_VALUE_08BIT 1
18 #define OV8856_REG_VALUE_16BIT 2
19 #define OV8856_REG_VALUE_24BIT 3
20
21 #define OV8856_LINK_FREQ_360MHZ 360000000ULL
22 #define OV8856_LINK_FREQ_180MHZ 180000000ULL
23 #define OV8856_SCLK 144000000ULL
24 #define OV8856_XVCLK_19_2 19200000
25 #define OV8856_DATA_LANES 4
26 #define OV8856_RGB_DEPTH 10
27
28 #define OV8856_REG_CHIP_ID 0x300a
29 #define OV8856_CHIP_ID 0x00885a
30
31 #define OV8856_REG_MODE_SELECT 0x0100
32 #define OV8856_MODE_STANDBY 0x00
33 #define OV8856_MODE_STREAMING 0x01
34
35 /* module revisions */
36 #define OV8856_2A_MODULE 0x01
37 #define OV8856_1B_MODULE 0x02
38
39 /* the OTP read-out buffer is at 0x7000 and 0xf is the offset
40 * of the byte in the OTP that means the module revision
41 */
42 #define OV8856_MODULE_REVISION 0x700f
43 #define OV8856_OTP_MODE_CTRL 0x3d84
44 #define OV8856_OTP_LOAD_CTRL 0x3d81
45 #define OV8856_OTP_MODE_AUTO 0x00
46 #define OV8856_OTP_LOAD_CTRL_ENABLE BIT(0)
47
48 /* vertical-timings from sensor */
49 #define OV8856_REG_VTS 0x380e
50 #define OV8856_VTS_MAX 0x7fff
51
52 /* horizontal-timings from sensor */
53 #define OV8856_REG_HTS 0x380c
54
55 /* Exposure controls from sensor */
56 #define OV8856_REG_EXPOSURE 0x3500
57 #define OV8856_EXPOSURE_MIN 6
58 #define OV8856_EXPOSURE_MAX_MARGIN 6
59 #define OV8856_EXPOSURE_STEP 1
60
61 /* Analog gain controls from sensor */
62 #define OV8856_REG_ANALOG_GAIN 0x3508
63 #define OV8856_ANAL_GAIN_MIN 128
64 #define OV8856_ANAL_GAIN_MAX 2047
65 #define OV8856_ANAL_GAIN_STEP 1
66
67 /* Digital gain controls from sensor */
68 #define OV8856_REG_MWB_R_GAIN 0x5019
69 #define OV8856_REG_MWB_G_GAIN 0x501b
70 #define OV8856_REG_MWB_B_GAIN 0x501d
71 #define OV8856_DGTL_GAIN_MIN 0
72 #define OV8856_DGTL_GAIN_MAX 4095
73 #define OV8856_DGTL_GAIN_STEP 1
74 #define OV8856_DGTL_GAIN_DEFAULT 1024
75
76 /* Test Pattern Control */
77 #define OV8856_REG_TEST_PATTERN 0x5e00
78 #define OV8856_TEST_PATTERN_ENABLE BIT(7)
79 #define OV8856_TEST_PATTERN_BAR_SHIFT 2
80
81 #define to_ov8856(_sd) container_of(_sd, struct ov8856, sd)
82
83 static const char * const ov8856_supply_names[] = {
84 "dovdd", /* Digital I/O power */
85 "avdd", /* Analog power */
86 "dvdd", /* Digital core power */
87 };
88
89 enum {
90 OV8856_LINK_FREQ_720MBPS,
91 OV8856_LINK_FREQ_360MBPS,
92 };
93
94 struct ov8856_reg {
95 u16 address;
96 u8 val;
97 };
98
99 struct ov8856_reg_list {
100 u32 num_of_regs;
101 const struct ov8856_reg *regs;
102 };
103
104 struct ov8856_link_freq_config {
105 const struct ov8856_reg_list reg_list;
106 };
107
108 struct ov8856_mode {
109 /* Frame width in pixels */
110 u32 width;
111
112 /* Frame height in pixels */
113 u32 height;
114
115 /* Horizontal timining size */
116 u32 hts;
117
118 /* Default vertical timining size */
119 u32 vts_def;
120
121 /* Min vertical timining size */
122 u32 vts_min;
123
124 /* Link frequency needed for this resolution */
125 u32 link_freq_index;
126
127 /* Sensor register settings for this resolution */
128 const struct ov8856_reg_list reg_list;
129 };
130
131 static const struct ov8856_reg mipi_data_rate_720mbps[] = {
132 {0x0103, 0x01},
133 {0x0100, 0x00},
134 {0x0302, 0x4b},
135 {0x0303, 0x01},
136 {0x030b, 0x02},
137 {0x030d, 0x4b},
138 {0x031e, 0x0c},
139 };
140
141 static const struct ov8856_reg mipi_data_rate_360mbps[] = {
142 {0x0103, 0x01},
143 {0x0100, 0x00},
144 {0x0302, 0x4b},
145 {0x0303, 0x03},
146 {0x030b, 0x02},
147 {0x030d, 0x4b},
148 {0x031e, 0x0c},
149 };
150
151 static const struct ov8856_reg mode_3280x2464_regs[] = {
152 {0x3000, 0x20},
153 {0x3003, 0x08},
154 {0x300e, 0x20},
155 {0x3010, 0x00},
156 {0x3015, 0x84},
157 {0x3018, 0x72},
158 {0x3021, 0x23},
159 {0x3033, 0x24},
160 {0x3500, 0x00},
161 {0x3501, 0x9a},
162 {0x3502, 0x20},
163 {0x3503, 0x08},
164 {0x3505, 0x83},
165 {0x3508, 0x01},
166 {0x3509, 0x80},
167 {0x350c, 0x00},
168 {0x350d, 0x80},
169 {0x350e, 0x04},
170 {0x350f, 0x00},
171 {0x3510, 0x00},
172 {0x3511, 0x02},
173 {0x3512, 0x00},
174 {0x3600, 0x72},
175 {0x3601, 0x40},
176 {0x3602, 0x30},
177 {0x3610, 0xc5},
178 {0x3611, 0x58},
179 {0x3612, 0x5c},
180 {0x3613, 0xca},
181 {0x3614, 0x20},
182 {0x3628, 0xff},
183 {0x3629, 0xff},
184 {0x362a, 0xff},
185 {0x3633, 0x10},
186 {0x3634, 0x10},
187 {0x3635, 0x10},
188 {0x3636, 0x10},
189 {0x3663, 0x08},
190 {0x3669, 0x34},
191 {0x366e, 0x10},
192 {0x3706, 0x86},
193 {0x370b, 0x7e},
194 {0x3714, 0x23},
195 {0x3730, 0x12},
196 {0x3733, 0x10},
197 {0x3764, 0x00},
198 {0x3765, 0x00},
199 {0x3769, 0x62},
200 {0x376a, 0x2a},
201 {0x376b, 0x30},
202 {0x3780, 0x00},
203 {0x3781, 0x24},
204 {0x3782, 0x00},
205 {0x3783, 0x23},
206 {0x3798, 0x2f},
207 {0x37a1, 0x60},
208 {0x37a8, 0x6a},
209 {0x37ab, 0x3f},
210 {0x37c2, 0x04},
211 {0x37c3, 0xf1},
212 {0x37c9, 0x80},
213 {0x37cb, 0x16},
214 {0x37cc, 0x16},
215 {0x37cd, 0x16},
216 {0x37ce, 0x16},
217 {0x3800, 0x00},
218 {0x3801, 0x00},
219 {0x3802, 0x00},
220 {0x3803, 0x06},
221 {0x3804, 0x0c},
222 {0x3805, 0xdf},
223 {0x3806, 0x09},
224 {0x3807, 0xa7},
225 {0x3808, 0x0c},
226 {0x3809, 0xd0},
227 {0x380a, 0x09},
228 {0x380b, 0xa0},
229 {0x380c, 0x07},
230 {0x380d, 0x88},
231 {0x380e, 0x09},
232 {0x380f, 0xb8},
233 {0x3810, 0x00},
234 {0x3811, 0x00},
235 {0x3812, 0x00},
236 {0x3813, 0x01},
237 {0x3814, 0x01},
238 {0x3815, 0x01},
239 {0x3816, 0x00},
240 {0x3817, 0x00},
241 {0x3818, 0x00},
242 {0x3819, 0x10},
243 {0x3820, 0x80},
244 {0x3821, 0x46},
245 {0x382a, 0x01},
246 {0x382b, 0x01},
247 {0x3830, 0x06},
248 {0x3836, 0x02},
249 {0x3862, 0x04},
250 {0x3863, 0x08},
251 {0x3cc0, 0x33},
252 {0x3d85, 0x17},
253 {0x3d8c, 0x73},
254 {0x3d8d, 0xde},
255 {0x4001, 0xe0},
256 {0x4003, 0x40},
257 {0x4008, 0x00},
258 {0x4009, 0x0b},
259 {0x400a, 0x00},
260 {0x400b, 0x84},
261 {0x400f, 0x80},
262 {0x4010, 0xf0},
263 {0x4011, 0xff},
264 {0x4012, 0x02},
265 {0x4013, 0x01},
266 {0x4014, 0x01},
267 {0x4015, 0x01},
268 {0x4042, 0x00},
269 {0x4043, 0x80},
270 {0x4044, 0x00},
271 {0x4045, 0x80},
272 {0x4046, 0x00},
273 {0x4047, 0x80},
274 {0x4048, 0x00},
275 {0x4049, 0x80},
276 {0x4041, 0x03},
277 {0x404c, 0x20},
278 {0x404d, 0x00},
279 {0x404e, 0x20},
280 {0x4203, 0x80},
281 {0x4307, 0x30},
282 {0x4317, 0x00},
283 {0x4503, 0x08},
284 {0x4601, 0x80},
285 {0x4800, 0x44},
286 {0x4816, 0x53},
287 {0x481b, 0x58},
288 {0x481f, 0x27},
289 {0x4837, 0x16},
290 {0x483c, 0x0f},
291 {0x484b, 0x05},
292 {0x5000, 0x57},
293 {0x5001, 0x0a},
294 {0x5004, 0x04},
295 {0x502e, 0x03},
296 {0x5030, 0x41},
297 {0x5780, 0x14},
298 {0x5781, 0x0f},
299 {0x5782, 0x44},
300 {0x5783, 0x02},
301 {0x5784, 0x01},
302 {0x5785, 0x01},
303 {0x5786, 0x00},
304 {0x5787, 0x04},
305 {0x5788, 0x02},
306 {0x5789, 0x0f},
307 {0x578a, 0xfd},
308 {0x578b, 0xf5},
309 {0x578c, 0xf5},
310 {0x578d, 0x03},
311 {0x578e, 0x08},
312 {0x578f, 0x0c},
313 {0x5790, 0x08},
314 {0x5791, 0x04},
315 {0x5792, 0x00},
316 {0x5793, 0x52},
317 {0x5794, 0xa3},
318 {0x5795, 0x02},
319 {0x5796, 0x20},
320 {0x5797, 0x20},
321 {0x5798, 0xd5},
322 {0x5799, 0xd5},
323 {0x579a, 0x00},
324 {0x579b, 0x50},
325 {0x579c, 0x00},
326 {0x579d, 0x2c},
327 {0x579e, 0x0c},
328 {0x579f, 0x40},
329 {0x57a0, 0x09},
330 {0x57a1, 0x40},
331 {0x59f8, 0x3d},
332 {0x5a08, 0x02},
333 {0x5b00, 0x02},
334 {0x5b01, 0x10},
335 {0x5b02, 0x03},
336 {0x5b03, 0xcf},
337 {0x5b05, 0x6c},
338 {0x5e00, 0x00}
339 };
340
341 static const struct ov8856_reg mode_3264x2448_regs[] = {
342 {0x0103, 0x01},
343 {0x0302, 0x3c},
344 {0x0303, 0x01},
345 {0x031e, 0x0c},
346 {0x3000, 0x20},
347 {0x3003, 0x08},
348 {0x300e, 0x20},
349 {0x3010, 0x00},
350 {0x3015, 0x84},
351 {0x3018, 0x72},
352 {0x3021, 0x23},
353 {0x3033, 0x24},
354 {0x3500, 0x00},
355 {0x3501, 0x9a},
356 {0x3502, 0x20},
357 {0x3503, 0x08},
358 {0x3505, 0x83},
359 {0x3508, 0x01},
360 {0x3509, 0x80},
361 {0x350c, 0x00},
362 {0x350d, 0x80},
363 {0x350e, 0x04},
364 {0x350f, 0x00},
365 {0x3510, 0x00},
366 {0x3511, 0x02},
367 {0x3512, 0x00},
368 {0x3600, 0x72},
369 {0x3601, 0x40},
370 {0x3602, 0x30},
371 {0x3610, 0xc5},
372 {0x3611, 0x58},
373 {0x3612, 0x5c},
374 {0x3613, 0xca},
375 {0x3614, 0x60},
376 {0x3628, 0xff},
377 {0x3629, 0xff},
378 {0x362a, 0xff},
379 {0x3633, 0x10},
380 {0x3634, 0x10},
381 {0x3635, 0x10},
382 {0x3636, 0x10},
383 {0x3663, 0x08},
384 {0x3669, 0x34},
385 {0x366d, 0x00},
386 {0x366e, 0x10},
387 {0x3706, 0x86},
388 {0x370b, 0x7e},
389 {0x3714, 0x23},
390 {0x3730, 0x12},
391 {0x3733, 0x10},
392 {0x3764, 0x00},
393 {0x3765, 0x00},
394 {0x3769, 0x62},
395 {0x376a, 0x2a},
396 {0x376b, 0x30},
397 {0x3780, 0x00},
398 {0x3781, 0x24},
399 {0x3782, 0x00},
400 {0x3783, 0x23},
401 {0x3798, 0x2f},
402 {0x37a1, 0x60},
403 {0x37a8, 0x6a},
404 {0x37ab, 0x3f},
405 {0x37c2, 0x04},
406 {0x37c3, 0xf1},
407 {0x37c9, 0x80},
408 {0x37cb, 0x16},
409 {0x37cc, 0x16},
410 {0x37cd, 0x16},
411 {0x37ce, 0x16},
412 {0x3800, 0x00},
413 {0x3801, 0x00},
414 {0x3802, 0x00},
415 {0x3803, 0x0c},
416 {0x3804, 0x0c},
417 {0x3805, 0xdf},
418 {0x3806, 0x09},
419 {0x3807, 0xa3},
420 {0x3808, 0x0c},
421 {0x3809, 0xc0},
422 {0x380a, 0x09},
423 {0x380b, 0x90},
424 {0x380c, 0x07},
425 {0x380d, 0x8c},
426 {0x380e, 0x09},
427 {0x380f, 0xb2},
428 {0x3810, 0x00},
429 {0x3811, 0x04},
430 {0x3812, 0x00},
431 {0x3813, 0x02},
432 {0x3814, 0x01},
433 {0x3815, 0x01},
434 {0x3816, 0x00},
435 {0x3817, 0x00},
436 {0x3818, 0x00},
437 {0x3819, 0x10},
438 {0x3820, 0x80},
439 {0x3821, 0x46},
440 {0x382a, 0x01},
441 {0x382b, 0x01},
442 {0x3830, 0x06},
443 {0x3836, 0x02},
444 {0x3862, 0x04},
445 {0x3863, 0x08},
446 {0x3cc0, 0x33},
447 {0x3d85, 0x17},
448 {0x3d8c, 0x73},
449 {0x3d8d, 0xde},
450 {0x4001, 0xe0},
451 {0x4003, 0x40},
452 {0x4008, 0x00},
453 {0x4009, 0x0b},
454 {0x400a, 0x00},
455 {0x400b, 0x84},
456 {0x400f, 0x80},
457 {0x4010, 0xf0},
458 {0x4011, 0xff},
459 {0x4012, 0x02},
460 {0x4013, 0x01},
461 {0x4014, 0x01},
462 {0x4015, 0x01},
463 {0x4042, 0x00},
464 {0x4043, 0x80},
465 {0x4044, 0x00},
466 {0x4045, 0x80},
467 {0x4046, 0x00},
468 {0x4047, 0x80},
469 {0x4048, 0x00},
470 {0x4049, 0x80},
471 {0x4041, 0x03},
472 {0x404c, 0x20},
473 {0x404d, 0x00},
474 {0x404e, 0x20},
475 {0x4203, 0x80},
476 {0x4307, 0x30},
477 {0x4317, 0x00},
478 {0x4502, 0x50},
479 {0x4503, 0x08},
480 {0x4601, 0x80},
481 {0x4800, 0x44},
482 {0x4816, 0x53},
483 {0x481b, 0x50},
484 {0x481f, 0x27},
485 {0x4823, 0x3c},
486 {0x482b, 0x00},
487 {0x4831, 0x66},
488 {0x4837, 0x16},
489 {0x483c, 0x0f},
490 {0x484b, 0x05},
491 {0x5000, 0x77},
492 {0x5001, 0x0a},
493 {0x5003, 0xc8},
494 {0x5004, 0x04},
495 {0x5006, 0x00},
496 {0x5007, 0x00},
497 {0x502e, 0x03},
498 {0x5030, 0x41},
499 {0x5780, 0x14},
500 {0x5781, 0x0f},
501 {0x5782, 0x44},
502 {0x5783, 0x02},
503 {0x5784, 0x01},
504 {0x5785, 0x01},
505 {0x5786, 0x00},
506 {0x5787, 0x04},
507 {0x5788, 0x02},
508 {0x5789, 0x0f},
509 {0x578a, 0xfd},
510 {0x578b, 0xf5},
511 {0x578c, 0xf5},
512 {0x578d, 0x03},
513 {0x578e, 0x08},
514 {0x578f, 0x0c},
515 {0x5790, 0x08},
516 {0x5791, 0x04},
517 {0x5792, 0x00},
518 {0x5793, 0x52},
519 {0x5794, 0xa3},
520 {0x5795, 0x02},
521 {0x5796, 0x20},
522 {0x5797, 0x20},
523 {0x5798, 0xd5},
524 {0x5799, 0xd5},
525 {0x579a, 0x00},
526 {0x579b, 0x50},
527 {0x579c, 0x00},
528 {0x579d, 0x2c},
529 {0x579e, 0x0c},
530 {0x579f, 0x40},
531 {0x57a0, 0x09},
532 {0x57a1, 0x40},
533 {0x59f8, 0x3d},
534 {0x5a08, 0x02},
535 {0x5b00, 0x02},
536 {0x5b01, 0x10},
537 {0x5b02, 0x03},
538 {0x5b03, 0xcf},
539 {0x5b05, 0x6c},
540 {0x5e00, 0x00},
541 {0x5e10, 0xfc}
542 };
543
544 static const struct ov8856_reg mode_1640x1232_regs[] = {
545 {0x3000, 0x20},
546 {0x3003, 0x08},
547 {0x300e, 0x20},
548 {0x3010, 0x00},
549 {0x3015, 0x84},
550 {0x3018, 0x72},
551 {0x3021, 0x23},
552 {0x3033, 0x24},
553 {0x3500, 0x00},
554 {0x3501, 0x4c},
555 {0x3502, 0xe0},
556 {0x3503, 0x08},
557 {0x3505, 0x83},
558 {0x3508, 0x01},
559 {0x3509, 0x80},
560 {0x350c, 0x00},
561 {0x350d, 0x80},
562 {0x350e, 0x04},
563 {0x350f, 0x00},
564 {0x3510, 0x00},
565 {0x3511, 0x02},
566 {0x3512, 0x00},
567 {0x3600, 0x72},
568 {0x3601, 0x40},
569 {0x3602, 0x30},
570 {0x3610, 0xc5},
571 {0x3611, 0x58},
572 {0x3612, 0x5c},
573 {0x3613, 0xca},
574 {0x3614, 0x20},
575 {0x3628, 0xff},
576 {0x3629, 0xff},
577 {0x362a, 0xff},
578 {0x3633, 0x10},
579 {0x3634, 0x10},
580 {0x3635, 0x10},
581 {0x3636, 0x10},
582 {0x3663, 0x08},
583 {0x3669, 0x34},
584 {0x366e, 0x08},
585 {0x3706, 0x86},
586 {0x370b, 0x7e},
587 {0x3714, 0x27},
588 {0x3730, 0x12},
589 {0x3733, 0x10},
590 {0x3764, 0x00},
591 {0x3765, 0x00},
592 {0x3769, 0x62},
593 {0x376a, 0x2a},
594 {0x376b, 0x30},
595 {0x3780, 0x00},
596 {0x3781, 0x24},
597 {0x3782, 0x00},
598 {0x3783, 0x23},
599 {0x3798, 0x2f},
600 {0x37a1, 0x60},
601 {0x37a8, 0x6a},
602 {0x37ab, 0x3f},
603 {0x37c2, 0x14},
604 {0x37c3, 0xf1},
605 {0x37c9, 0x80},
606 {0x37cb, 0x16},
607 {0x37cc, 0x16},
608 {0x37cd, 0x16},
609 {0x37ce, 0x16},
610 {0x3800, 0x00},
611 {0x3801, 0x00},
612 {0x3802, 0x00},
613 {0x3803, 0x06},
614 {0x3804, 0x0c},
615 {0x3805, 0xdf},
616 {0x3806, 0x09},
617 {0x3807, 0xa7},
618 {0x3808, 0x06},
619 {0x3809, 0x68},
620 {0x380a, 0x04},
621 {0x380b, 0xd0},
622 {0x380c, 0x0e},
623 {0x380d, 0xec},
624 {0x380e, 0x04},
625 {0x380f, 0xe8},
626 {0x3810, 0x00},
627 {0x3811, 0x00},
628 {0x3812, 0x00},
629 {0x3813, 0x01},
630 {0x3814, 0x03},
631 {0x3815, 0x01},
632 {0x3816, 0x00},
633 {0x3817, 0x00},
634 {0x3818, 0x00},
635 {0x3819, 0x10},
636 {0x3820, 0x90},
637 {0x3821, 0x67},
638 {0x382a, 0x03},
639 {0x382b, 0x01},
640 {0x3830, 0x06},
641 {0x3836, 0x02},
642 {0x3862, 0x04},
643 {0x3863, 0x08},
644 {0x3cc0, 0x33},
645 {0x3d85, 0x17},
646 {0x3d8c, 0x73},
647 {0x3d8d, 0xde},
648 {0x4001, 0xe0},
649 {0x4003, 0x40},
650 {0x4008, 0x00},
651 {0x4009, 0x05},
652 {0x400a, 0x00},
653 {0x400b, 0x84},
654 {0x400f, 0x80},
655 {0x4010, 0xf0},
656 {0x4011, 0xff},
657 {0x4012, 0x02},
658 {0x4013, 0x01},
659 {0x4014, 0x01},
660 {0x4015, 0x01},
661 {0x4042, 0x00},
662 {0x4043, 0x80},
663 {0x4044, 0x00},
664 {0x4045, 0x80},
665 {0x4046, 0x00},
666 {0x4047, 0x80},
667 {0x4048, 0x00},
668 {0x4049, 0x80},
669 {0x4041, 0x03},
670 {0x404c, 0x20},
671 {0x404d, 0x00},
672 {0x404e, 0x20},
673 {0x4203, 0x80},
674 {0x4307, 0x30},
675 {0x4317, 0x00},
676 {0x4503, 0x08},
677 {0x4601, 0x80},
678 {0x4800, 0x44},
679 {0x4816, 0x53},
680 {0x481b, 0x58},
681 {0x481f, 0x27},
682 {0x4837, 0x16},
683 {0x483c, 0x0f},
684 {0x484b, 0x05},
685 {0x5000, 0x57},
686 {0x5001, 0x0a},
687 {0x5004, 0x04},
688 {0x502e, 0x03},
689 {0x5030, 0x41},
690 {0x5780, 0x14},
691 {0x5781, 0x0f},
692 {0x5782, 0x44},
693 {0x5783, 0x02},
694 {0x5784, 0x01},
695 {0x5785, 0x01},
696 {0x5786, 0x00},
697 {0x5787, 0x04},
698 {0x5788, 0x02},
699 {0x5789, 0x0f},
700 {0x578a, 0xfd},
701 {0x578b, 0xf5},
702 {0x578c, 0xf5},
703 {0x578d, 0x03},
704 {0x578e, 0x08},
705 {0x578f, 0x0c},
706 {0x5790, 0x08},
707 {0x5791, 0x04},
708 {0x5792, 0x00},
709 {0x5793, 0x52},
710 {0x5794, 0xa3},
711 {0x5795, 0x00},
712 {0x5796, 0x10},
713 {0x5797, 0x10},
714 {0x5798, 0x73},
715 {0x5799, 0x73},
716 {0x579a, 0x00},
717 {0x579b, 0x28},
718 {0x579c, 0x00},
719 {0x579d, 0x16},
720 {0x579e, 0x06},
721 {0x579f, 0x20},
722 {0x57a0, 0x04},
723 {0x57a1, 0xa0},
724 {0x59f8, 0x3d},
725 {0x5a08, 0x02},
726 {0x5b00, 0x02},
727 {0x5b01, 0x10},
728 {0x5b02, 0x03},
729 {0x5b03, 0xcf},
730 {0x5b05, 0x6c},
731 {0x5e00, 0x00}
732 };
733
734 static const struct ov8856_reg mode_1632x1224_regs[] = {
735 {0x0103, 0x01},
736 {0x0302, 0x3c},
737 {0x0303, 0x01},
738 {0x031e, 0x0c},
739 {0x3000, 0x20},
740 {0x3003, 0x08},
741 {0x300e, 0x20},
742 {0x3010, 0x00},
743 {0x3015, 0x84},
744 {0x3018, 0x72},
745 {0x3021, 0x23},
746 {0x3033, 0x24},
747 {0x3500, 0x00},
748 {0x3501, 0x4c},
749 {0x3502, 0xe0},
750 {0x3503, 0x08},
751 {0x3505, 0x83},
752 {0x3508, 0x01},
753 {0x3509, 0x80},
754 {0x350c, 0x00},
755 {0x350d, 0x80},
756 {0x350e, 0x04},
757 {0x350f, 0x00},
758 {0x3510, 0x00},
759 {0x3511, 0x02},
760 {0x3512, 0x00},
761 {0x3600, 0x72},
762 {0x3601, 0x40},
763 {0x3602, 0x30},
764 {0x3610, 0xc5},
765 {0x3611, 0x58},
766 {0x3612, 0x5c},
767 {0x3613, 0xca},
768 {0x3614, 0x60},
769 {0x3628, 0xff},
770 {0x3629, 0xff},
771 {0x362a, 0xff},
772 {0x3633, 0x10},
773 {0x3634, 0x10},
774 {0x3635, 0x10},
775 {0x3636, 0x10},
776 {0x3663, 0x08},
777 {0x3669, 0x34},
778 {0x366d, 0x00},
779 {0x366e, 0x08},
780 {0x3706, 0x86},
781 {0x370b, 0x7e},
782 {0x3714, 0x27},
783 {0x3730, 0x12},
784 {0x3733, 0x10},
785 {0x3764, 0x00},
786 {0x3765, 0x00},
787 {0x3769, 0x62},
788 {0x376a, 0x2a},
789 {0x376b, 0x30},
790 {0x3780, 0x00},
791 {0x3781, 0x24},
792 {0x3782, 0x00},
793 {0x3783, 0x23},
794 {0x3798, 0x2f},
795 {0x37a1, 0x60},
796 {0x37a8, 0x6a},
797 {0x37ab, 0x3f},
798 {0x37c2, 0x14},
799 {0x37c3, 0xf1},
800 {0x37c9, 0x80},
801 {0x37cb, 0x16},
802 {0x37cc, 0x16},
803 {0x37cd, 0x16},
804 {0x37ce, 0x16},
805 {0x3800, 0x00},
806 {0x3801, 0x00},
807 {0x3802, 0x00},
808 {0x3803, 0x0c},
809 {0x3804, 0x0c},
810 {0x3805, 0xdf},
811 {0x3806, 0x09},
812 {0x3807, 0xa3},
813 {0x3808, 0x06},
814 {0x3809, 0x60},
815 {0x380a, 0x04},
816 {0x380b, 0xc8},
817 {0x380c, 0x07},
818 {0x380d, 0x8c},
819 {0x380e, 0x09},
820 {0x380f, 0xb2},
821 {0x3810, 0x00},
822 {0x3811, 0x02},
823 {0x3812, 0x00},
824 {0x3813, 0x02},
825 {0x3814, 0x03},
826 {0x3815, 0x01},
827 {0x3816, 0x00},
828 {0x3817, 0x00},
829 {0x3818, 0x00},
830 {0x3819, 0x10},
831 {0x3820, 0x80},
832 {0x3821, 0x47},
833 {0x382a, 0x03},
834 {0x382b, 0x01},
835 {0x3830, 0x06},
836 {0x3836, 0x02},
837 {0x3862, 0x04},
838 {0x3863, 0x08},
839 {0x3cc0, 0x33},
840 {0x3d85, 0x17},
841 {0x3d8c, 0x73},
842 {0x3d8d, 0xde},
843 {0x4001, 0xe0},
844 {0x4003, 0x40},
845 {0x4008, 0x00},
846 {0x4009, 0x05},
847 {0x400a, 0x00},
848 {0x400b, 0x84},
849 {0x400f, 0x80},
850 {0x4010, 0xf0},
851 {0x4011, 0xff},
852 {0x4012, 0x02},
853 {0x4013, 0x01},
854 {0x4014, 0x01},
855 {0x4015, 0x01},
856 {0x4042, 0x00},
857 {0x4043, 0x80},
858 {0x4044, 0x00},
859 {0x4045, 0x80},
860 {0x4046, 0x00},
861 {0x4047, 0x80},
862 {0x4048, 0x00},
863 {0x4049, 0x80},
864 {0x4041, 0x03},
865 {0x404c, 0x20},
866 {0x404d, 0x00},
867 {0x404e, 0x20},
868 {0x4203, 0x80},
869 {0x4307, 0x30},
870 {0x4317, 0x00},
871 {0x4502, 0x50},
872 {0x4503, 0x08},
873 {0x4601, 0x80},
874 {0x4800, 0x44},
875 {0x4816, 0x53},
876 {0x481b, 0x50},
877 {0x481f, 0x27},
878 {0x4823, 0x3c},
879 {0x482b, 0x00},
880 {0x4831, 0x66},
881 {0x4837, 0x16},
882 {0x483c, 0x0f},
883 {0x484b, 0x05},
884 {0x5000, 0x77},
885 {0x5001, 0x0a},
886 {0x5003, 0xc8},
887 {0x5004, 0x04},
888 {0x5006, 0x00},
889 {0x5007, 0x00},
890 {0x502e, 0x03},
891 {0x5030, 0x41},
892 {0x5795, 0x00},
893 {0x5796, 0x10},
894 {0x5797, 0x10},
895 {0x5798, 0x73},
896 {0x5799, 0x73},
897 {0x579a, 0x00},
898 {0x579b, 0x28},
899 {0x579c, 0x00},
900 {0x579d, 0x16},
901 {0x579e, 0x06},
902 {0x579f, 0x20},
903 {0x57a0, 0x04},
904 {0x57a1, 0xa0},
905 {0x5780, 0x14},
906 {0x5781, 0x0f},
907 {0x5782, 0x44},
908 {0x5783, 0x02},
909 {0x5784, 0x01},
910 {0x5785, 0x01},
911 {0x5786, 0x00},
912 {0x5787, 0x04},
913 {0x5788, 0x02},
914 {0x5789, 0x0f},
915 {0x578a, 0xfd},
916 {0x578b, 0xf5},
917 {0x578c, 0xf5},
918 {0x578d, 0x03},
919 {0x578e, 0x08},
920 {0x578f, 0x0c},
921 {0x5790, 0x08},
922 {0x5791, 0x04},
923 {0x5792, 0x00},
924 {0x5793, 0x52},
925 {0x5794, 0xa3},
926 {0x59f8, 0x3d},
927 {0x5a08, 0x02},
928 {0x5b00, 0x02},
929 {0x5b01, 0x10},
930 {0x5b02, 0x03},
931 {0x5b03, 0xcf},
932 {0x5b05, 0x6c},
933 {0x5e00, 0x00},
934 {0x5e10, 0xfc}
935 };
936
937 static const char * const ov8856_test_pattern_menu[] = {
938 "Disabled",
939 "Standard Color Bar",
940 "Top-Bottom Darker Color Bar",
941 "Right-Left Darker Color Bar",
942 "Bottom-Top Darker Color Bar"
943 };
944
945 static const s64 link_freq_menu_items[] = {
946 OV8856_LINK_FREQ_360MHZ,
947 OV8856_LINK_FREQ_180MHZ
948 };
949
950 static const struct ov8856_link_freq_config link_freq_configs[] = {
951 [OV8856_LINK_FREQ_720MBPS] = {
952 .reg_list = {
953 .num_of_regs = ARRAY_SIZE(mipi_data_rate_720mbps),
954 .regs = mipi_data_rate_720mbps,
955 }
956 },
957 [OV8856_LINK_FREQ_360MBPS] = {
958 .reg_list = {
959 .num_of_regs = ARRAY_SIZE(mipi_data_rate_360mbps),
960 .regs = mipi_data_rate_360mbps,
961 }
962 }
963 };
964
965 static const struct ov8856_mode supported_modes[] = {
966 {
967 .width = 3280,
968 .height = 2464,
969 .hts = 1928,
970 .vts_def = 2488,
971 .vts_min = 2488,
972 .reg_list = {
973 .num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
974 .regs = mode_3280x2464_regs,
975 },
976 .link_freq_index = OV8856_LINK_FREQ_720MBPS,
977 },
978 {
979 .width = 3264,
980 .height = 2448,
981 .hts = 1932,
982 .vts_def = 2482,
983 .vts_min = 2482,
984 .reg_list = {
985 .num_of_regs = ARRAY_SIZE(mode_3264x2448_regs),
986 .regs = mode_3264x2448_regs,
987 },
988 .link_freq_index = OV8856_LINK_FREQ_720MBPS,
989 },
990 {
991 .width = 1640,
992 .height = 1232,
993 .hts = 3820,
994 .vts_def = 1256,
995 .vts_min = 1256,
996 .reg_list = {
997 .num_of_regs = ARRAY_SIZE(mode_1640x1232_regs),
998 .regs = mode_1640x1232_regs,
999 },
1000 .link_freq_index = OV8856_LINK_FREQ_360MBPS,
1001 },
1002 {
1003 .width = 1632,
1004 .height = 1224,
1005 .hts = 1932,
1006 .vts_def = 2482,
1007 .vts_min = 2482,
1008 .reg_list = {
1009 .num_of_regs = ARRAY_SIZE(mode_1632x1224_regs),
1010 .regs = mode_1632x1224_regs,
1011 },
1012 .link_freq_index = OV8856_LINK_FREQ_360MBPS,
1013 }
1014 };
1015
1016 struct ov8856 {
1017 struct v4l2_subdev sd;
1018 struct media_pad pad;
1019 struct v4l2_ctrl_handler ctrl_handler;
1020
1021 struct clk *xvclk;
1022 struct gpio_desc *reset_gpio;
1023 struct regulator_bulk_data supplies[ARRAY_SIZE(ov8856_supply_names)];
1024
1025 /* V4L2 Controls */
1026 struct v4l2_ctrl *link_freq;
1027 struct v4l2_ctrl *pixel_rate;
1028 struct v4l2_ctrl *vblank;
1029 struct v4l2_ctrl *hblank;
1030 struct v4l2_ctrl *exposure;
1031
1032 /* Current mode */
1033 const struct ov8856_mode *cur_mode;
1034
1035 /* To serialize asynchronus callbacks */
1036 struct mutex mutex;
1037
1038 /* Streaming on/off */
1039 bool streaming;
1040 };
1041
to_pixel_rate(u32 f_index)1042 static u64 to_pixel_rate(u32 f_index)
1043 {
1044 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV8856_DATA_LANES;
1045
1046 do_div(pixel_rate, OV8856_RGB_DEPTH);
1047
1048 return pixel_rate;
1049 }
1050
to_pixels_per_line(u32 hts,u32 f_index)1051 static u64 to_pixels_per_line(u32 hts, u32 f_index)
1052 {
1053 u64 ppl = hts * to_pixel_rate(f_index);
1054
1055 do_div(ppl, OV8856_SCLK);
1056
1057 return ppl;
1058 }
1059
ov8856_read_reg(struct ov8856 * ov8856,u16 reg,u16 len,u32 * val)1060 static int ov8856_read_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 *val)
1061 {
1062 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1063 struct i2c_msg msgs[2];
1064 u8 addr_buf[2];
1065 u8 data_buf[4] = {0};
1066 int ret;
1067
1068 if (len > 4)
1069 return -EINVAL;
1070
1071 put_unaligned_be16(reg, addr_buf);
1072 msgs[0].addr = client->addr;
1073 msgs[0].flags = 0;
1074 msgs[0].len = sizeof(addr_buf);
1075 msgs[0].buf = addr_buf;
1076 msgs[1].addr = client->addr;
1077 msgs[1].flags = I2C_M_RD;
1078 msgs[1].len = len;
1079 msgs[1].buf = &data_buf[4 - len];
1080
1081 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1082 if (ret != ARRAY_SIZE(msgs))
1083 return -EIO;
1084
1085 *val = get_unaligned_be32(data_buf);
1086
1087 return 0;
1088 }
1089
ov8856_write_reg(struct ov8856 * ov8856,u16 reg,u16 len,u32 val)1090 static int ov8856_write_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 val)
1091 {
1092 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1093 u8 buf[6];
1094
1095 if (len > 4)
1096 return -EINVAL;
1097
1098 put_unaligned_be16(reg, buf);
1099 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
1100 if (i2c_master_send(client, buf, len + 2) != len + 2)
1101 return -EIO;
1102
1103 return 0;
1104 }
1105
ov8856_write_reg_list(struct ov8856 * ov8856,const struct ov8856_reg_list * r_list)1106 static int ov8856_write_reg_list(struct ov8856 *ov8856,
1107 const struct ov8856_reg_list *r_list)
1108 {
1109 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1110 unsigned int i;
1111 int ret;
1112
1113 for (i = 0; i < r_list->num_of_regs; i++) {
1114 ret = ov8856_write_reg(ov8856, r_list->regs[i].address, 1,
1115 r_list->regs[i].val);
1116 if (ret) {
1117 dev_err_ratelimited(&client->dev,
1118 "failed to write reg 0x%4.4x. error = %d",
1119 r_list->regs[i].address, ret);
1120 return ret;
1121 }
1122 }
1123
1124 return 0;
1125 }
1126
ov8856_update_digital_gain(struct ov8856 * ov8856,u32 d_gain)1127 static int ov8856_update_digital_gain(struct ov8856 *ov8856, u32 d_gain)
1128 {
1129 int ret;
1130
1131 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_R_GAIN,
1132 OV8856_REG_VALUE_16BIT, d_gain);
1133 if (ret)
1134 return ret;
1135
1136 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_G_GAIN,
1137 OV8856_REG_VALUE_16BIT, d_gain);
1138 if (ret)
1139 return ret;
1140
1141 return ov8856_write_reg(ov8856, OV8856_REG_MWB_B_GAIN,
1142 OV8856_REG_VALUE_16BIT, d_gain);
1143 }
1144
ov8856_test_pattern(struct ov8856 * ov8856,u32 pattern)1145 static int ov8856_test_pattern(struct ov8856 *ov8856, u32 pattern)
1146 {
1147 if (pattern)
1148 pattern = (pattern - 1) << OV8856_TEST_PATTERN_BAR_SHIFT |
1149 OV8856_TEST_PATTERN_ENABLE;
1150
1151 return ov8856_write_reg(ov8856, OV8856_REG_TEST_PATTERN,
1152 OV8856_REG_VALUE_08BIT, pattern);
1153 }
1154
ov8856_set_ctrl(struct v4l2_ctrl * ctrl)1155 static int ov8856_set_ctrl(struct v4l2_ctrl *ctrl)
1156 {
1157 struct ov8856 *ov8856 = container_of(ctrl->handler,
1158 struct ov8856, ctrl_handler);
1159 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1160 s64 exposure_max;
1161 int ret = 0;
1162
1163 /* Propagate change of current control to all related controls */
1164 if (ctrl->id == V4L2_CID_VBLANK) {
1165 /* Update max exposure while meeting expected vblanking */
1166 exposure_max = ov8856->cur_mode->height + ctrl->val -
1167 OV8856_EXPOSURE_MAX_MARGIN;
1168 __v4l2_ctrl_modify_range(ov8856->exposure,
1169 ov8856->exposure->minimum,
1170 exposure_max, ov8856->exposure->step,
1171 exposure_max);
1172 }
1173
1174 /* V4L2 controls values will be applied only when power is already up */
1175 if (!pm_runtime_get_if_in_use(&client->dev))
1176 return 0;
1177
1178 switch (ctrl->id) {
1179 case V4L2_CID_ANALOGUE_GAIN:
1180 ret = ov8856_write_reg(ov8856, OV8856_REG_ANALOG_GAIN,
1181 OV8856_REG_VALUE_16BIT, ctrl->val);
1182 break;
1183
1184 case V4L2_CID_DIGITAL_GAIN:
1185 ret = ov8856_update_digital_gain(ov8856, ctrl->val);
1186 break;
1187
1188 case V4L2_CID_EXPOSURE:
1189 /* 4 least significant bits of expsoure are fractional part */
1190 ret = ov8856_write_reg(ov8856, OV8856_REG_EXPOSURE,
1191 OV8856_REG_VALUE_24BIT, ctrl->val << 4);
1192 break;
1193
1194 case V4L2_CID_VBLANK:
1195 ret = ov8856_write_reg(ov8856, OV8856_REG_VTS,
1196 OV8856_REG_VALUE_16BIT,
1197 ov8856->cur_mode->height + ctrl->val);
1198 break;
1199
1200 case V4L2_CID_TEST_PATTERN:
1201 ret = ov8856_test_pattern(ov8856, ctrl->val);
1202 break;
1203
1204 default:
1205 ret = -EINVAL;
1206 break;
1207 }
1208
1209 pm_runtime_put(&client->dev);
1210
1211 return ret;
1212 }
1213
1214 static const struct v4l2_ctrl_ops ov8856_ctrl_ops = {
1215 .s_ctrl = ov8856_set_ctrl,
1216 };
1217
ov8856_init_controls(struct ov8856 * ov8856)1218 static int ov8856_init_controls(struct ov8856 *ov8856)
1219 {
1220 struct v4l2_ctrl_handler *ctrl_hdlr;
1221 s64 exposure_max, h_blank;
1222 int ret;
1223
1224 ctrl_hdlr = &ov8856->ctrl_handler;
1225 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
1226 if (ret)
1227 return ret;
1228
1229 ctrl_hdlr->lock = &ov8856->mutex;
1230 ov8856->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov8856_ctrl_ops,
1231 V4L2_CID_LINK_FREQ,
1232 ARRAY_SIZE(link_freq_menu_items) - 1,
1233 0, link_freq_menu_items);
1234 if (ov8856->link_freq)
1235 ov8856->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1236
1237 ov8856->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
1238 V4L2_CID_PIXEL_RATE, 0,
1239 to_pixel_rate(OV8856_LINK_FREQ_720MBPS),
1240 1,
1241 to_pixel_rate(OV8856_LINK_FREQ_720MBPS));
1242 ov8856->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
1243 V4L2_CID_VBLANK,
1244 ov8856->cur_mode->vts_min - ov8856->cur_mode->height,
1245 OV8856_VTS_MAX - ov8856->cur_mode->height, 1,
1246 ov8856->cur_mode->vts_def - ov8856->cur_mode->height);
1247 h_blank = to_pixels_per_line(ov8856->cur_mode->hts,
1248 ov8856->cur_mode->link_freq_index) - ov8856->cur_mode->width;
1249 ov8856->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
1250 V4L2_CID_HBLANK, h_blank, h_blank, 1,
1251 h_blank);
1252 if (ov8856->hblank)
1253 ov8856->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1254
1255 v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
1256 OV8856_ANAL_GAIN_MIN, OV8856_ANAL_GAIN_MAX,
1257 OV8856_ANAL_GAIN_STEP, OV8856_ANAL_GAIN_MIN);
1258 v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
1259 OV8856_DGTL_GAIN_MIN, OV8856_DGTL_GAIN_MAX,
1260 OV8856_DGTL_GAIN_STEP, OV8856_DGTL_GAIN_DEFAULT);
1261 exposure_max = ov8856->cur_mode->vts_def - OV8856_EXPOSURE_MAX_MARGIN;
1262 ov8856->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
1263 V4L2_CID_EXPOSURE,
1264 OV8856_EXPOSURE_MIN, exposure_max,
1265 OV8856_EXPOSURE_STEP,
1266 exposure_max);
1267 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov8856_ctrl_ops,
1268 V4L2_CID_TEST_PATTERN,
1269 ARRAY_SIZE(ov8856_test_pattern_menu) - 1,
1270 0, 0, ov8856_test_pattern_menu);
1271 if (ctrl_hdlr->error)
1272 return ctrl_hdlr->error;
1273
1274 ov8856->sd.ctrl_handler = ctrl_hdlr;
1275
1276 return 0;
1277 }
1278
ov8856_update_pad_format(const struct ov8856_mode * mode,struct v4l2_mbus_framefmt * fmt)1279 static void ov8856_update_pad_format(const struct ov8856_mode *mode,
1280 struct v4l2_mbus_framefmt *fmt)
1281 {
1282 fmt->width = mode->width;
1283 fmt->height = mode->height;
1284 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1285 fmt->field = V4L2_FIELD_NONE;
1286 }
1287
ov8856_start_streaming(struct ov8856 * ov8856)1288 static int ov8856_start_streaming(struct ov8856 *ov8856)
1289 {
1290 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1291 const struct ov8856_reg_list *reg_list;
1292 int link_freq_index, ret;
1293
1294 link_freq_index = ov8856->cur_mode->link_freq_index;
1295 reg_list = &link_freq_configs[link_freq_index].reg_list;
1296 ret = ov8856_write_reg_list(ov8856, reg_list);
1297 if (ret) {
1298 dev_err(&client->dev, "failed to set plls");
1299 return ret;
1300 }
1301
1302 reg_list = &ov8856->cur_mode->reg_list;
1303 ret = ov8856_write_reg_list(ov8856, reg_list);
1304 if (ret) {
1305 dev_err(&client->dev, "failed to set mode");
1306 return ret;
1307 }
1308
1309 ret = __v4l2_ctrl_handler_setup(ov8856->sd.ctrl_handler);
1310 if (ret)
1311 return ret;
1312
1313 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1314 OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING);
1315 if (ret) {
1316 dev_err(&client->dev, "failed to set stream");
1317 return ret;
1318 }
1319
1320 return 0;
1321 }
1322
ov8856_stop_streaming(struct ov8856 * ov8856)1323 static void ov8856_stop_streaming(struct ov8856 *ov8856)
1324 {
1325 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1326
1327 if (ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1328 OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY))
1329 dev_err(&client->dev, "failed to set stream");
1330 }
1331
ov8856_set_stream(struct v4l2_subdev * sd,int enable)1332 static int ov8856_set_stream(struct v4l2_subdev *sd, int enable)
1333 {
1334 struct ov8856 *ov8856 = to_ov8856(sd);
1335 struct i2c_client *client = v4l2_get_subdevdata(sd);
1336 int ret = 0;
1337
1338 if (ov8856->streaming == enable)
1339 return 0;
1340
1341 mutex_lock(&ov8856->mutex);
1342 if (enable) {
1343 ret = pm_runtime_get_sync(&client->dev);
1344 if (ret < 0) {
1345 pm_runtime_put_noidle(&client->dev);
1346 mutex_unlock(&ov8856->mutex);
1347 return ret;
1348 }
1349
1350 ret = ov8856_start_streaming(ov8856);
1351 if (ret) {
1352 enable = 0;
1353 ov8856_stop_streaming(ov8856);
1354 pm_runtime_put(&client->dev);
1355 }
1356 } else {
1357 ov8856_stop_streaming(ov8856);
1358 pm_runtime_put(&client->dev);
1359 }
1360
1361 ov8856->streaming = enable;
1362 mutex_unlock(&ov8856->mutex);
1363
1364 return ret;
1365 }
1366
__ov8856_power_on(struct ov8856 * ov8856)1367 static int __ov8856_power_on(struct ov8856 *ov8856)
1368 {
1369 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1370 int ret;
1371
1372 if (is_acpi_node(dev_fwnode(&client->dev)))
1373 return 0;
1374
1375 ret = clk_prepare_enable(ov8856->xvclk);
1376 if (ret < 0) {
1377 dev_err(&client->dev, "failed to enable xvclk\n");
1378 return ret;
1379 }
1380
1381 if (ov8856->reset_gpio) {
1382 gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
1383 usleep_range(1000, 2000);
1384 }
1385
1386 ret = regulator_bulk_enable(ARRAY_SIZE(ov8856_supply_names),
1387 ov8856->supplies);
1388 if (ret < 0) {
1389 dev_err(&client->dev, "failed to enable regulators\n");
1390 goto disable_clk;
1391 }
1392
1393 gpiod_set_value_cansleep(ov8856->reset_gpio, 0);
1394 usleep_range(1500, 1800);
1395
1396 return 0;
1397
1398 disable_clk:
1399 gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
1400 clk_disable_unprepare(ov8856->xvclk);
1401
1402 return ret;
1403 }
1404
__ov8856_power_off(struct ov8856 * ov8856)1405 static void __ov8856_power_off(struct ov8856 *ov8856)
1406 {
1407 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1408
1409 if (is_acpi_node(dev_fwnode(&client->dev)))
1410 return;
1411
1412 gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
1413 regulator_bulk_disable(ARRAY_SIZE(ov8856_supply_names),
1414 ov8856->supplies);
1415 clk_disable_unprepare(ov8856->xvclk);
1416 }
1417
ov8856_suspend(struct device * dev)1418 static int __maybe_unused ov8856_suspend(struct device *dev)
1419 {
1420 struct i2c_client *client = to_i2c_client(dev);
1421 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1422 struct ov8856 *ov8856 = to_ov8856(sd);
1423
1424 mutex_lock(&ov8856->mutex);
1425 if (ov8856->streaming)
1426 ov8856_stop_streaming(ov8856);
1427
1428 __ov8856_power_off(ov8856);
1429 mutex_unlock(&ov8856->mutex);
1430
1431 return 0;
1432 }
1433
ov8856_resume(struct device * dev)1434 static int __maybe_unused ov8856_resume(struct device *dev)
1435 {
1436 struct i2c_client *client = to_i2c_client(dev);
1437 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1438 struct ov8856 *ov8856 = to_ov8856(sd);
1439 int ret;
1440
1441 mutex_lock(&ov8856->mutex);
1442
1443 __ov8856_power_on(ov8856);
1444 if (ov8856->streaming) {
1445 ret = ov8856_start_streaming(ov8856);
1446 if (ret) {
1447 ov8856->streaming = false;
1448 ov8856_stop_streaming(ov8856);
1449 mutex_unlock(&ov8856->mutex);
1450 return ret;
1451 }
1452 }
1453
1454 mutex_unlock(&ov8856->mutex);
1455
1456 return 0;
1457 }
1458
ov8856_set_format(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1459 static int ov8856_set_format(struct v4l2_subdev *sd,
1460 struct v4l2_subdev_pad_config *cfg,
1461 struct v4l2_subdev_format *fmt)
1462 {
1463 struct ov8856 *ov8856 = to_ov8856(sd);
1464 const struct ov8856_mode *mode;
1465 s32 vblank_def, h_blank;
1466
1467 mode = v4l2_find_nearest_size(supported_modes,
1468 ARRAY_SIZE(supported_modes), width,
1469 height, fmt->format.width,
1470 fmt->format.height);
1471
1472 mutex_lock(&ov8856->mutex);
1473 ov8856_update_pad_format(mode, &fmt->format);
1474 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1475 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
1476 } else {
1477 ov8856->cur_mode = mode;
1478 __v4l2_ctrl_s_ctrl(ov8856->link_freq, mode->link_freq_index);
1479 __v4l2_ctrl_s_ctrl_int64(ov8856->pixel_rate,
1480 to_pixel_rate(mode->link_freq_index));
1481
1482 /* Update limits and set FPS to default */
1483 vblank_def = mode->vts_def - mode->height;
1484 __v4l2_ctrl_modify_range(ov8856->vblank,
1485 mode->vts_min - mode->height,
1486 OV8856_VTS_MAX - mode->height, 1,
1487 vblank_def);
1488 __v4l2_ctrl_s_ctrl(ov8856->vblank, vblank_def);
1489 h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
1490 mode->width;
1491 __v4l2_ctrl_modify_range(ov8856->hblank, h_blank, h_blank, 1,
1492 h_blank);
1493 }
1494
1495 mutex_unlock(&ov8856->mutex);
1496
1497 return 0;
1498 }
1499
ov8856_get_format(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1500 static int ov8856_get_format(struct v4l2_subdev *sd,
1501 struct v4l2_subdev_pad_config *cfg,
1502 struct v4l2_subdev_format *fmt)
1503 {
1504 struct ov8856 *ov8856 = to_ov8856(sd);
1505
1506 mutex_lock(&ov8856->mutex);
1507 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1508 fmt->format = *v4l2_subdev_get_try_format(&ov8856->sd, cfg,
1509 fmt->pad);
1510 else
1511 ov8856_update_pad_format(ov8856->cur_mode, &fmt->format);
1512
1513 mutex_unlock(&ov8856->mutex);
1514
1515 return 0;
1516 }
1517
ov8856_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1518 static int ov8856_enum_mbus_code(struct v4l2_subdev *sd,
1519 struct v4l2_subdev_pad_config *cfg,
1520 struct v4l2_subdev_mbus_code_enum *code)
1521 {
1522 /* Only one bayer order GRBG is supported */
1523 if (code->index > 0)
1524 return -EINVAL;
1525
1526 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1527
1528 return 0;
1529 }
1530
ov8856_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1531 static int ov8856_enum_frame_size(struct v4l2_subdev *sd,
1532 struct v4l2_subdev_pad_config *cfg,
1533 struct v4l2_subdev_frame_size_enum *fse)
1534 {
1535 if (fse->index >= ARRAY_SIZE(supported_modes))
1536 return -EINVAL;
1537
1538 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1539 return -EINVAL;
1540
1541 fse->min_width = supported_modes[fse->index].width;
1542 fse->max_width = fse->min_width;
1543 fse->min_height = supported_modes[fse->index].height;
1544 fse->max_height = fse->min_height;
1545
1546 return 0;
1547 }
1548
ov8856_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1549 static int ov8856_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1550 {
1551 struct ov8856 *ov8856 = to_ov8856(sd);
1552
1553 mutex_lock(&ov8856->mutex);
1554 ov8856_update_pad_format(&supported_modes[0],
1555 v4l2_subdev_get_try_format(sd, fh->pad, 0));
1556 mutex_unlock(&ov8856->mutex);
1557
1558 return 0;
1559 }
1560
1561 static const struct v4l2_subdev_video_ops ov8856_video_ops = {
1562 .s_stream = ov8856_set_stream,
1563 };
1564
1565 static const struct v4l2_subdev_pad_ops ov8856_pad_ops = {
1566 .set_fmt = ov8856_set_format,
1567 .get_fmt = ov8856_get_format,
1568 .enum_mbus_code = ov8856_enum_mbus_code,
1569 .enum_frame_size = ov8856_enum_frame_size,
1570 };
1571
1572 static const struct v4l2_subdev_ops ov8856_subdev_ops = {
1573 .video = &ov8856_video_ops,
1574 .pad = &ov8856_pad_ops,
1575 };
1576
1577 static const struct media_entity_operations ov8856_subdev_entity_ops = {
1578 .link_validate = v4l2_subdev_link_validate,
1579 };
1580
1581 static const struct v4l2_subdev_internal_ops ov8856_internal_ops = {
1582 .open = ov8856_open,
1583 };
1584
ov8856_identify_module(struct ov8856 * ov8856)1585 static int ov8856_identify_module(struct ov8856 *ov8856)
1586 {
1587 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1588 int ret;
1589 u32 val;
1590
1591 ret = ov8856_read_reg(ov8856, OV8856_REG_CHIP_ID,
1592 OV8856_REG_VALUE_24BIT, &val);
1593 if (ret)
1594 return ret;
1595
1596 if (val != OV8856_CHIP_ID) {
1597 dev_err(&client->dev, "chip id mismatch: %x!=%x",
1598 OV8856_CHIP_ID, val);
1599 return -ENXIO;
1600 }
1601
1602 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1603 OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING);
1604 if (ret)
1605 return ret;
1606
1607 ret = ov8856_write_reg(ov8856, OV8856_OTP_MODE_CTRL,
1608 OV8856_REG_VALUE_08BIT, OV8856_OTP_MODE_AUTO);
1609 if (ret) {
1610 dev_err(&client->dev, "failed to set otp mode");
1611 return ret;
1612 }
1613
1614 ret = ov8856_write_reg(ov8856, OV8856_OTP_LOAD_CTRL,
1615 OV8856_REG_VALUE_08BIT,
1616 OV8856_OTP_LOAD_CTRL_ENABLE);
1617 if (ret) {
1618 dev_err(&client->dev, "failed to enable load control");
1619 return ret;
1620 }
1621
1622 ret = ov8856_read_reg(ov8856, OV8856_MODULE_REVISION,
1623 OV8856_REG_VALUE_08BIT, &val);
1624 if (ret) {
1625 dev_err(&client->dev, "failed to read module revision");
1626 return ret;
1627 }
1628
1629 dev_info(&client->dev, "OV8856 revision %x (%s) at address 0x%02x\n",
1630 val,
1631 val == OV8856_2A_MODULE ? "2A" :
1632 val == OV8856_1B_MODULE ? "1B" : "unknown revision",
1633 client->addr);
1634
1635 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1636 OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY);
1637 if (ret) {
1638 dev_err(&client->dev, "failed to exit streaming mode");
1639 return ret;
1640 }
1641
1642 return 0;
1643 }
1644
ov8856_get_hwcfg(struct ov8856 * ov8856,struct device * dev)1645 static int ov8856_get_hwcfg(struct ov8856 *ov8856, struct device *dev)
1646 {
1647 struct fwnode_handle *ep;
1648 struct fwnode_handle *fwnode = dev_fwnode(dev);
1649 struct v4l2_fwnode_endpoint bus_cfg = {
1650 .bus_type = V4L2_MBUS_CSI2_DPHY
1651 };
1652 u32 xvclk_rate;
1653 int ret;
1654 unsigned int i, j;
1655
1656 if (!fwnode)
1657 return -ENXIO;
1658
1659 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &xvclk_rate);
1660 if (ret)
1661 return ret;
1662
1663 if (!is_acpi_node(fwnode)) {
1664 ov8856->xvclk = devm_clk_get(dev, "xvclk");
1665 if (IS_ERR(ov8856->xvclk)) {
1666 dev_err(dev, "could not get xvclk clock (%pe)\n",
1667 ov8856->xvclk);
1668 return PTR_ERR(ov8856->xvclk);
1669 }
1670
1671 clk_set_rate(ov8856->xvclk, xvclk_rate);
1672 xvclk_rate = clk_get_rate(ov8856->xvclk);
1673 }
1674
1675 if (xvclk_rate != OV8856_XVCLK_19_2)
1676 dev_warn(dev, "external clock rate %u is unsupported",
1677 xvclk_rate);
1678
1679 ov8856->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1680 GPIOD_OUT_LOW);
1681 if (IS_ERR(ov8856->reset_gpio))
1682 return PTR_ERR(ov8856->reset_gpio);
1683
1684 for (i = 0; i < ARRAY_SIZE(ov8856_supply_names); i++)
1685 ov8856->supplies[i].supply = ov8856_supply_names[i];
1686
1687 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov8856_supply_names),
1688 ov8856->supplies);
1689 if (ret)
1690 return ret;
1691
1692 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1693 if (!ep)
1694 return -ENXIO;
1695
1696 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1697 fwnode_handle_put(ep);
1698 if (ret)
1699 return ret;
1700
1701 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV8856_DATA_LANES) {
1702 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1703 bus_cfg.bus.mipi_csi2.num_data_lanes);
1704 ret = -EINVAL;
1705 goto check_hwcfg_error;
1706 }
1707
1708 if (!bus_cfg.nr_of_link_frequencies) {
1709 dev_err(dev, "no link frequencies defined");
1710 ret = -EINVAL;
1711 goto check_hwcfg_error;
1712 }
1713
1714 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1715 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1716 if (link_freq_menu_items[i] ==
1717 bus_cfg.link_frequencies[j])
1718 break;
1719 }
1720
1721 if (j == bus_cfg.nr_of_link_frequencies) {
1722 dev_err(dev, "no link frequency %lld supported",
1723 link_freq_menu_items[i]);
1724 ret = -EINVAL;
1725 goto check_hwcfg_error;
1726 }
1727 }
1728
1729 check_hwcfg_error:
1730 v4l2_fwnode_endpoint_free(&bus_cfg);
1731
1732 return ret;
1733 }
1734
ov8856_remove(struct i2c_client * client)1735 static int ov8856_remove(struct i2c_client *client)
1736 {
1737 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1738 struct ov8856 *ov8856 = to_ov8856(sd);
1739
1740 v4l2_async_unregister_subdev(sd);
1741 media_entity_cleanup(&sd->entity);
1742 v4l2_ctrl_handler_free(sd->ctrl_handler);
1743 pm_runtime_disable(&client->dev);
1744 mutex_destroy(&ov8856->mutex);
1745
1746 __ov8856_power_off(ov8856);
1747
1748 return 0;
1749 }
1750
ov8856_probe(struct i2c_client * client)1751 static int ov8856_probe(struct i2c_client *client)
1752 {
1753 struct ov8856 *ov8856;
1754 int ret;
1755
1756 ov8856 = devm_kzalloc(&client->dev, sizeof(*ov8856), GFP_KERNEL);
1757 if (!ov8856)
1758 return -ENOMEM;
1759
1760 ret = ov8856_get_hwcfg(ov8856, &client->dev);
1761 if (ret) {
1762 dev_err(&client->dev, "failed to get HW configuration: %d",
1763 ret);
1764 return ret;
1765 }
1766
1767 v4l2_i2c_subdev_init(&ov8856->sd, client, &ov8856_subdev_ops);
1768
1769 ret = __ov8856_power_on(ov8856);
1770 if (ret) {
1771 dev_err(&client->dev, "failed to power on\n");
1772 return ret;
1773 }
1774
1775 ret = ov8856_identify_module(ov8856);
1776 if (ret) {
1777 dev_err(&client->dev, "failed to find sensor: %d", ret);
1778 goto probe_power_off;
1779 }
1780
1781 mutex_init(&ov8856->mutex);
1782 ov8856->cur_mode = &supported_modes[0];
1783 ret = ov8856_init_controls(ov8856);
1784 if (ret) {
1785 dev_err(&client->dev, "failed to init controls: %d", ret);
1786 goto probe_error_v4l2_ctrl_handler_free;
1787 }
1788
1789 ov8856->sd.internal_ops = &ov8856_internal_ops;
1790 ov8856->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1791 ov8856->sd.entity.ops = &ov8856_subdev_entity_ops;
1792 ov8856->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1793 ov8856->pad.flags = MEDIA_PAD_FL_SOURCE;
1794 ret = media_entity_pads_init(&ov8856->sd.entity, 1, &ov8856->pad);
1795 if (ret) {
1796 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1797 goto probe_error_v4l2_ctrl_handler_free;
1798 }
1799
1800 ret = v4l2_async_register_subdev_sensor_common(&ov8856->sd);
1801 if (ret < 0) {
1802 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1803 ret);
1804 goto probe_error_media_entity_cleanup;
1805 }
1806
1807 /*
1808 * Device is already turned on by i2c-core with ACPI domain PM.
1809 * Enable runtime PM and turn off the device.
1810 */
1811 pm_runtime_set_active(&client->dev);
1812 pm_runtime_enable(&client->dev);
1813 pm_runtime_idle(&client->dev);
1814
1815 return 0;
1816
1817 probe_error_media_entity_cleanup:
1818 media_entity_cleanup(&ov8856->sd.entity);
1819
1820 probe_error_v4l2_ctrl_handler_free:
1821 v4l2_ctrl_handler_free(ov8856->sd.ctrl_handler);
1822 mutex_destroy(&ov8856->mutex);
1823
1824 probe_power_off:
1825 __ov8856_power_off(ov8856);
1826
1827 return ret;
1828 }
1829
1830 static const struct dev_pm_ops ov8856_pm_ops = {
1831 SET_SYSTEM_SLEEP_PM_OPS(ov8856_suspend, ov8856_resume)
1832 };
1833
1834 #ifdef CONFIG_ACPI
1835 static const struct acpi_device_id ov8856_acpi_ids[] = {
1836 {"OVTI8856"},
1837 {}
1838 };
1839
1840 MODULE_DEVICE_TABLE(acpi, ov8856_acpi_ids);
1841 #endif
1842
1843 static const struct of_device_id ov8856_of_match[] = {
1844 { .compatible = "ovti,ov8856" },
1845 { /* sentinel */ }
1846 };
1847 MODULE_DEVICE_TABLE(of, ov8856_of_match);
1848
1849 static struct i2c_driver ov8856_i2c_driver = {
1850 .driver = {
1851 .name = "ov8856",
1852 .pm = &ov8856_pm_ops,
1853 .acpi_match_table = ACPI_PTR(ov8856_acpi_ids),
1854 .of_match_table = ov8856_of_match,
1855 },
1856 .probe_new = ov8856_probe,
1857 .remove = ov8856_remove,
1858 };
1859
1860 module_i2c_driver(ov8856_i2c_driver);
1861
1862 MODULE_AUTHOR("Ben Kao <ben.kao@intel.com>");
1863 MODULE_DESCRIPTION("OmniVision OV8856 sensor driver");
1864 MODULE_LICENSE("GPL v2");
1865