WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / i2c / ov8856.c
blobd8cefd3d40452412f3bf9bb1195c31a8d9e9a6bc
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2019 Intel Corporation.
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>
17 #define OV8856_REG_VALUE_08BIT 1
18 #define OV8856_REG_VALUE_16BIT 2
19 #define OV8856_REG_VALUE_24BIT 3
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
28 #define OV8856_REG_CHIP_ID 0x300a
29 #define OV8856_CHIP_ID 0x00885a
31 #define OV8856_REG_MODE_SELECT 0x0100
32 #define OV8856_MODE_STANDBY 0x00
33 #define OV8856_MODE_STREAMING 0x01
35 /* module revisions */
36 #define OV8856_2A_MODULE 0x01
37 #define OV8856_1B_MODULE 0x02
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
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)
48 /* vertical-timings from sensor */
49 #define OV8856_REG_VTS 0x380e
50 #define OV8856_VTS_MAX 0x7fff
52 /* horizontal-timings from sensor */
53 #define OV8856_REG_HTS 0x380c
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
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
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
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
81 #define to_ov8856(_sd) container_of(_sd, struct ov8856, sd)
83 static const char * const ov8856_supply_names[] = {
84 "dovdd", /* Digital I/O power */
85 "avdd", /* Analog power */
86 "dvdd", /* Digital core power */
89 enum {
90 OV8856_LINK_FREQ_720MBPS,
91 OV8856_LINK_FREQ_360MBPS,
94 struct ov8856_reg {
95 u16 address;
96 u8 val;
99 struct ov8856_reg_list {
100 u32 num_of_regs;
101 const struct ov8856_reg *regs;
104 struct ov8856_link_freq_config {
105 const struct ov8856_reg_list reg_list;
108 struct ov8856_mode {
109 /* Frame width in pixels */
110 u32 width;
112 /* Frame height in pixels */
113 u32 height;
115 /* Horizontal timining size */
116 u32 hts;
118 /* Default vertical timining size */
119 u32 vts_def;
121 /* Min vertical timining size */
122 u32 vts_min;
124 /* Link frequency needed for this resolution */
125 u32 link_freq_index;
127 /* Sensor register settings for this resolution */
128 const struct ov8856_reg_list reg_list;
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},
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},
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}
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}
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}
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}
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"
945 static const s64 link_freq_menu_items[] = {
946 OV8856_LINK_FREQ_360MHZ,
947 OV8856_LINK_FREQ_180MHZ
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,
957 [OV8856_LINK_FREQ_360MBPS] = {
958 .reg_list = {
959 .num_of_regs = ARRAY_SIZE(mipi_data_rate_360mbps),
960 .regs = mipi_data_rate_360mbps,
965 static const struct ov8856_mode supported_modes[] = {
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,
976 .link_freq_index = OV8856_LINK_FREQ_720MBPS,
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,
988 .link_freq_index = OV8856_LINK_FREQ_720MBPS,
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,
1000 .link_freq_index = OV8856_LINK_FREQ_360MBPS,
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,
1012 .link_freq_index = OV8856_LINK_FREQ_360MBPS,
1016 struct ov8856 {
1017 struct v4l2_subdev sd;
1018 struct media_pad pad;
1019 struct v4l2_ctrl_handler ctrl_handler;
1021 struct clk *xvclk;
1022 struct gpio_desc *reset_gpio;
1023 struct regulator_bulk_data supplies[ARRAY_SIZE(ov8856_supply_names)];
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;
1032 /* Current mode */
1033 const struct ov8856_mode *cur_mode;
1035 /* To serialize asynchronus callbacks */
1036 struct mutex mutex;
1038 /* Streaming on/off */
1039 bool streaming;
1042 static u64 to_pixel_rate(u32 f_index)
1044 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV8856_DATA_LANES;
1046 do_div(pixel_rate, OV8856_RGB_DEPTH);
1048 return pixel_rate;
1051 static u64 to_pixels_per_line(u32 hts, u32 f_index)
1053 u64 ppl = hts * to_pixel_rate(f_index);
1055 do_div(ppl, OV8856_SCLK);
1057 return ppl;
1060 static int ov8856_read_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 *val)
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;
1068 if (len > 4)
1069 return -EINVAL;
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];
1081 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
1082 if (ret != ARRAY_SIZE(msgs))
1083 return -EIO;
1085 *val = get_unaligned_be32(data_buf);
1087 return 0;
1090 static int ov8856_write_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 val)
1092 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1093 u8 buf[6];
1095 if (len > 4)
1096 return -EINVAL;
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;
1103 return 0;
1106 static int ov8856_write_reg_list(struct ov8856 *ov8856,
1107 const struct ov8856_reg_list *r_list)
1109 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1110 unsigned int i;
1111 int ret;
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;
1124 return 0;
1127 static int ov8856_update_digital_gain(struct ov8856 *ov8856, u32 d_gain)
1129 int ret;
1131 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_R_GAIN,
1132 OV8856_REG_VALUE_16BIT, d_gain);
1133 if (ret)
1134 return ret;
1136 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_G_GAIN,
1137 OV8856_REG_VALUE_16BIT, d_gain);
1138 if (ret)
1139 return ret;
1141 return ov8856_write_reg(ov8856, OV8856_REG_MWB_B_GAIN,
1142 OV8856_REG_VALUE_16BIT, d_gain);
1145 static int ov8856_test_pattern(struct ov8856 *ov8856, u32 pattern)
1147 if (pattern)
1148 pattern = (pattern - 1) << OV8856_TEST_PATTERN_BAR_SHIFT |
1149 OV8856_TEST_PATTERN_ENABLE;
1151 return ov8856_write_reg(ov8856, OV8856_REG_TEST_PATTERN,
1152 OV8856_REG_VALUE_08BIT, pattern);
1155 static int ov8856_set_ctrl(struct v4l2_ctrl *ctrl)
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;
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);
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;
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;
1184 case V4L2_CID_DIGITAL_GAIN:
1185 ret = ov8856_update_digital_gain(ov8856, ctrl->val);
1186 break;
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;
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;
1200 case V4L2_CID_TEST_PATTERN:
1201 ret = ov8856_test_pattern(ov8856, ctrl->val);
1202 break;
1204 default:
1205 ret = -EINVAL;
1206 break;
1209 pm_runtime_put(&client->dev);
1211 return ret;
1214 static const struct v4l2_ctrl_ops ov8856_ctrl_ops = {
1215 .s_ctrl = ov8856_set_ctrl,
1218 static int ov8856_init_controls(struct ov8856 *ov8856)
1220 struct v4l2_ctrl_handler *ctrl_hdlr;
1221 s64 exposure_max, h_blank;
1222 int ret;
1224 ctrl_hdlr = &ov8856->ctrl_handler;
1225 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
1226 if (ret)
1227 return ret;
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;
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),
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;
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;
1274 ov8856->sd.ctrl_handler = ctrl_hdlr;
1276 return 0;
1279 static void ov8856_update_pad_format(const struct ov8856_mode *mode,
1280 struct v4l2_mbus_framefmt *fmt)
1282 fmt->width = mode->width;
1283 fmt->height = mode->height;
1284 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1285 fmt->field = V4L2_FIELD_NONE;
1288 static int ov8856_start_streaming(struct ov8856 *ov8856)
1290 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1291 const struct ov8856_reg_list *reg_list;
1292 int link_freq_index, ret;
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;
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;
1309 ret = __v4l2_ctrl_handler_setup(ov8856->sd.ctrl_handler);
1310 if (ret)
1311 return ret;
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;
1320 return 0;
1323 static void ov8856_stop_streaming(struct ov8856 *ov8856)
1325 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
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");
1332 static int ov8856_set_stream(struct v4l2_subdev *sd, int enable)
1334 struct ov8856 *ov8856 = to_ov8856(sd);
1335 struct i2c_client *client = v4l2_get_subdevdata(sd);
1336 int ret = 0;
1338 if (ov8856->streaming == enable)
1339 return 0;
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;
1350 ret = ov8856_start_streaming(ov8856);
1351 if (ret) {
1352 enable = 0;
1353 ov8856_stop_streaming(ov8856);
1354 pm_runtime_put(&client->dev);
1356 } else {
1357 ov8856_stop_streaming(ov8856);
1358 pm_runtime_put(&client->dev);
1361 ov8856->streaming = enable;
1362 mutex_unlock(&ov8856->mutex);
1364 return ret;
1367 static int __ov8856_power_on(struct ov8856 *ov8856)
1369 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1370 int ret;
1372 if (is_acpi_node(dev_fwnode(&client->dev)))
1373 return 0;
1375 ret = clk_prepare_enable(ov8856->xvclk);
1376 if (ret < 0) {
1377 dev_err(&client->dev, "failed to enable xvclk\n");
1378 return ret;
1381 if (ov8856->reset_gpio) {
1382 gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
1383 usleep_range(1000, 2000);
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;
1393 gpiod_set_value_cansleep(ov8856->reset_gpio, 0);
1394 usleep_range(1500, 1800);
1396 return 0;
1398 disable_clk:
1399 gpiod_set_value_cansleep(ov8856->reset_gpio, 1);
1400 clk_disable_unprepare(ov8856->xvclk);
1402 return ret;
1405 static void __ov8856_power_off(struct ov8856 *ov8856)
1407 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1409 if (is_acpi_node(dev_fwnode(&client->dev)))
1410 return;
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);
1418 static int __maybe_unused ov8856_suspend(struct device *dev)
1420 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1421 struct ov8856 *ov8856 = to_ov8856(sd);
1423 mutex_lock(&ov8856->mutex);
1424 if (ov8856->streaming)
1425 ov8856_stop_streaming(ov8856);
1427 __ov8856_power_off(ov8856);
1428 mutex_unlock(&ov8856->mutex);
1430 return 0;
1433 static int __maybe_unused ov8856_resume(struct device *dev)
1435 struct v4l2_subdev *sd = dev_get_drvdata(dev);
1436 struct ov8856 *ov8856 = to_ov8856(sd);
1437 int ret;
1439 mutex_lock(&ov8856->mutex);
1441 __ov8856_power_on(ov8856);
1442 if (ov8856->streaming) {
1443 ret = ov8856_start_streaming(ov8856);
1444 if (ret) {
1445 ov8856->streaming = false;
1446 ov8856_stop_streaming(ov8856);
1447 mutex_unlock(&ov8856->mutex);
1448 return ret;
1452 mutex_unlock(&ov8856->mutex);
1454 return 0;
1457 static int ov8856_set_format(struct v4l2_subdev *sd,
1458 struct v4l2_subdev_pad_config *cfg,
1459 struct v4l2_subdev_format *fmt)
1461 struct ov8856 *ov8856 = to_ov8856(sd);
1462 const struct ov8856_mode *mode;
1463 s32 vblank_def, h_blank;
1465 mode = v4l2_find_nearest_size(supported_modes,
1466 ARRAY_SIZE(supported_modes), width,
1467 height, fmt->format.width,
1468 fmt->format.height);
1470 mutex_lock(&ov8856->mutex);
1471 ov8856_update_pad_format(mode, &fmt->format);
1472 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1473 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
1474 } else {
1475 ov8856->cur_mode = mode;
1476 __v4l2_ctrl_s_ctrl(ov8856->link_freq, mode->link_freq_index);
1477 __v4l2_ctrl_s_ctrl_int64(ov8856->pixel_rate,
1478 to_pixel_rate(mode->link_freq_index));
1480 /* Update limits and set FPS to default */
1481 vblank_def = mode->vts_def - mode->height;
1482 __v4l2_ctrl_modify_range(ov8856->vblank,
1483 mode->vts_min - mode->height,
1484 OV8856_VTS_MAX - mode->height, 1,
1485 vblank_def);
1486 __v4l2_ctrl_s_ctrl(ov8856->vblank, vblank_def);
1487 h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
1488 mode->width;
1489 __v4l2_ctrl_modify_range(ov8856->hblank, h_blank, h_blank, 1,
1490 h_blank);
1493 mutex_unlock(&ov8856->mutex);
1495 return 0;
1498 static int ov8856_get_format(struct v4l2_subdev *sd,
1499 struct v4l2_subdev_pad_config *cfg,
1500 struct v4l2_subdev_format *fmt)
1502 struct ov8856 *ov8856 = to_ov8856(sd);
1504 mutex_lock(&ov8856->mutex);
1505 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
1506 fmt->format = *v4l2_subdev_get_try_format(&ov8856->sd, cfg,
1507 fmt->pad);
1508 else
1509 ov8856_update_pad_format(ov8856->cur_mode, &fmt->format);
1511 mutex_unlock(&ov8856->mutex);
1513 return 0;
1516 static int ov8856_enum_mbus_code(struct v4l2_subdev *sd,
1517 struct v4l2_subdev_pad_config *cfg,
1518 struct v4l2_subdev_mbus_code_enum *code)
1520 /* Only one bayer order GRBG is supported */
1521 if (code->index > 0)
1522 return -EINVAL;
1524 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1526 return 0;
1529 static int ov8856_enum_frame_size(struct v4l2_subdev *sd,
1530 struct v4l2_subdev_pad_config *cfg,
1531 struct v4l2_subdev_frame_size_enum *fse)
1533 if (fse->index >= ARRAY_SIZE(supported_modes))
1534 return -EINVAL;
1536 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1537 return -EINVAL;
1539 fse->min_width = supported_modes[fse->index].width;
1540 fse->max_width = fse->min_width;
1541 fse->min_height = supported_modes[fse->index].height;
1542 fse->max_height = fse->min_height;
1544 return 0;
1547 static int ov8856_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1549 struct ov8856 *ov8856 = to_ov8856(sd);
1551 mutex_lock(&ov8856->mutex);
1552 ov8856_update_pad_format(&supported_modes[0],
1553 v4l2_subdev_get_try_format(sd, fh->pad, 0));
1554 mutex_unlock(&ov8856->mutex);
1556 return 0;
1559 static const struct v4l2_subdev_video_ops ov8856_video_ops = {
1560 .s_stream = ov8856_set_stream,
1563 static const struct v4l2_subdev_pad_ops ov8856_pad_ops = {
1564 .set_fmt = ov8856_set_format,
1565 .get_fmt = ov8856_get_format,
1566 .enum_mbus_code = ov8856_enum_mbus_code,
1567 .enum_frame_size = ov8856_enum_frame_size,
1570 static const struct v4l2_subdev_ops ov8856_subdev_ops = {
1571 .video = &ov8856_video_ops,
1572 .pad = &ov8856_pad_ops,
1575 static const struct media_entity_operations ov8856_subdev_entity_ops = {
1576 .link_validate = v4l2_subdev_link_validate,
1579 static const struct v4l2_subdev_internal_ops ov8856_internal_ops = {
1580 .open = ov8856_open,
1583 static int ov8856_identify_module(struct ov8856 *ov8856)
1585 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1586 int ret;
1587 u32 val;
1589 ret = ov8856_read_reg(ov8856, OV8856_REG_CHIP_ID,
1590 OV8856_REG_VALUE_24BIT, &val);
1591 if (ret)
1592 return ret;
1594 if (val != OV8856_CHIP_ID) {
1595 dev_err(&client->dev, "chip id mismatch: %x!=%x",
1596 OV8856_CHIP_ID, val);
1597 return -ENXIO;
1600 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1601 OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING);
1602 if (ret)
1603 return ret;
1605 ret = ov8856_write_reg(ov8856, OV8856_OTP_MODE_CTRL,
1606 OV8856_REG_VALUE_08BIT, OV8856_OTP_MODE_AUTO);
1607 if (ret) {
1608 dev_err(&client->dev, "failed to set otp mode");
1609 return ret;
1612 ret = ov8856_write_reg(ov8856, OV8856_OTP_LOAD_CTRL,
1613 OV8856_REG_VALUE_08BIT,
1614 OV8856_OTP_LOAD_CTRL_ENABLE);
1615 if (ret) {
1616 dev_err(&client->dev, "failed to enable load control");
1617 return ret;
1620 ret = ov8856_read_reg(ov8856, OV8856_MODULE_REVISION,
1621 OV8856_REG_VALUE_08BIT, &val);
1622 if (ret) {
1623 dev_err(&client->dev, "failed to read module revision");
1624 return ret;
1627 dev_info(&client->dev, "OV8856 revision %x (%s) at address 0x%02x\n",
1628 val,
1629 val == OV8856_2A_MODULE ? "2A" :
1630 val == OV8856_1B_MODULE ? "1B" : "unknown revision",
1631 client->addr);
1633 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
1634 OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY);
1635 if (ret) {
1636 dev_err(&client->dev, "failed to exit streaming mode");
1637 return ret;
1640 return 0;
1643 static int ov8856_get_hwcfg(struct ov8856 *ov8856, struct device *dev)
1645 struct fwnode_handle *ep;
1646 struct fwnode_handle *fwnode = dev_fwnode(dev);
1647 struct v4l2_fwnode_endpoint bus_cfg = {
1648 .bus_type = V4L2_MBUS_CSI2_DPHY
1650 u32 xvclk_rate;
1651 int ret;
1652 unsigned int i, j;
1654 if (!fwnode)
1655 return -ENXIO;
1657 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &xvclk_rate);
1658 if (ret)
1659 return ret;
1661 if (!is_acpi_node(fwnode)) {
1662 ov8856->xvclk = devm_clk_get(dev, "xvclk");
1663 if (IS_ERR(ov8856->xvclk)) {
1664 dev_err(dev, "could not get xvclk clock (%pe)\n",
1665 ov8856->xvclk);
1666 return PTR_ERR(ov8856->xvclk);
1669 clk_set_rate(ov8856->xvclk, xvclk_rate);
1670 xvclk_rate = clk_get_rate(ov8856->xvclk);
1673 if (xvclk_rate != OV8856_XVCLK_19_2)
1674 dev_warn(dev, "external clock rate %u is unsupported",
1675 xvclk_rate);
1677 ov8856->reset_gpio = devm_gpiod_get_optional(dev, "reset",
1678 GPIOD_OUT_LOW);
1679 if (IS_ERR(ov8856->reset_gpio))
1680 return PTR_ERR(ov8856->reset_gpio);
1682 for (i = 0; i < ARRAY_SIZE(ov8856_supply_names); i++)
1683 ov8856->supplies[i].supply = ov8856_supply_names[i];
1685 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ov8856_supply_names),
1686 ov8856->supplies);
1687 if (ret)
1688 return ret;
1690 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1691 if (!ep)
1692 return -ENXIO;
1694 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1695 fwnode_handle_put(ep);
1696 if (ret)
1697 return ret;
1699 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV8856_DATA_LANES) {
1700 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1701 bus_cfg.bus.mipi_csi2.num_data_lanes);
1702 ret = -EINVAL;
1703 goto check_hwcfg_error;
1706 if (!bus_cfg.nr_of_link_frequencies) {
1707 dev_err(dev, "no link frequencies defined");
1708 ret = -EINVAL;
1709 goto check_hwcfg_error;
1712 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1713 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1714 if (link_freq_menu_items[i] ==
1715 bus_cfg.link_frequencies[j])
1716 break;
1719 if (j == bus_cfg.nr_of_link_frequencies) {
1720 dev_err(dev, "no link frequency %lld supported",
1721 link_freq_menu_items[i]);
1722 ret = -EINVAL;
1723 goto check_hwcfg_error;
1727 check_hwcfg_error:
1728 v4l2_fwnode_endpoint_free(&bus_cfg);
1730 return ret;
1733 static int ov8856_remove(struct i2c_client *client)
1735 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1736 struct ov8856 *ov8856 = to_ov8856(sd);
1738 v4l2_async_unregister_subdev(sd);
1739 media_entity_cleanup(&sd->entity);
1740 v4l2_ctrl_handler_free(sd->ctrl_handler);
1741 pm_runtime_disable(&client->dev);
1742 mutex_destroy(&ov8856->mutex);
1744 __ov8856_power_off(ov8856);
1746 return 0;
1749 static int ov8856_probe(struct i2c_client *client)
1751 struct ov8856 *ov8856;
1752 int ret;
1754 ov8856 = devm_kzalloc(&client->dev, sizeof(*ov8856), GFP_KERNEL);
1755 if (!ov8856)
1756 return -ENOMEM;
1758 ret = ov8856_get_hwcfg(ov8856, &client->dev);
1759 if (ret) {
1760 dev_err(&client->dev, "failed to get HW configuration: %d",
1761 ret);
1762 return ret;
1765 v4l2_i2c_subdev_init(&ov8856->sd, client, &ov8856_subdev_ops);
1767 ret = __ov8856_power_on(ov8856);
1768 if (ret) {
1769 dev_err(&client->dev, "failed to power on\n");
1770 return ret;
1773 ret = ov8856_identify_module(ov8856);
1774 if (ret) {
1775 dev_err(&client->dev, "failed to find sensor: %d", ret);
1776 goto probe_power_off;
1779 mutex_init(&ov8856->mutex);
1780 ov8856->cur_mode = &supported_modes[0];
1781 ret = ov8856_init_controls(ov8856);
1782 if (ret) {
1783 dev_err(&client->dev, "failed to init controls: %d", ret);
1784 goto probe_error_v4l2_ctrl_handler_free;
1787 ov8856->sd.internal_ops = &ov8856_internal_ops;
1788 ov8856->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1789 ov8856->sd.entity.ops = &ov8856_subdev_entity_ops;
1790 ov8856->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1791 ov8856->pad.flags = MEDIA_PAD_FL_SOURCE;
1792 ret = media_entity_pads_init(&ov8856->sd.entity, 1, &ov8856->pad);
1793 if (ret) {
1794 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1795 goto probe_error_v4l2_ctrl_handler_free;
1798 ret = v4l2_async_register_subdev_sensor_common(&ov8856->sd);
1799 if (ret < 0) {
1800 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1801 ret);
1802 goto probe_error_media_entity_cleanup;
1806 * Device is already turned on by i2c-core with ACPI domain PM.
1807 * Enable runtime PM and turn off the device.
1809 pm_runtime_set_active(&client->dev);
1810 pm_runtime_enable(&client->dev);
1811 pm_runtime_idle(&client->dev);
1813 return 0;
1815 probe_error_media_entity_cleanup:
1816 media_entity_cleanup(&ov8856->sd.entity);
1818 probe_error_v4l2_ctrl_handler_free:
1819 v4l2_ctrl_handler_free(ov8856->sd.ctrl_handler);
1820 mutex_destroy(&ov8856->mutex);
1822 probe_power_off:
1823 __ov8856_power_off(ov8856);
1825 return ret;
1828 static const struct dev_pm_ops ov8856_pm_ops = {
1829 SET_SYSTEM_SLEEP_PM_OPS(ov8856_suspend, ov8856_resume)
1832 #ifdef CONFIG_ACPI
1833 static const struct acpi_device_id ov8856_acpi_ids[] = {
1834 {"OVTI8856"},
1838 MODULE_DEVICE_TABLE(acpi, ov8856_acpi_ids);
1839 #endif
1841 static const struct of_device_id ov8856_of_match[] = {
1842 { .compatible = "ovti,ov8856" },
1843 { /* sentinel */ }
1845 MODULE_DEVICE_TABLE(of, ov8856_of_match);
1847 static struct i2c_driver ov8856_i2c_driver = {
1848 .driver = {
1849 .name = "ov8856",
1850 .pm = &ov8856_pm_ops,
1851 .acpi_match_table = ACPI_PTR(ov8856_acpi_ids),
1852 .of_match_table = ov8856_of_match,
1854 .probe_new = ov8856_probe,
1855 .remove = ov8856_remove,
1858 module_i2c_driver(ov8856_i2c_driver);
1860 MODULE_AUTHOR("Ben Kao <ben.kao@intel.com>");
1861 MODULE_DESCRIPTION("OmniVision OV8856 sensor driver");
1862 MODULE_LICENSE("GPL v2");