1
2
3
4#include <asm/unaligned.h>
5#include <linux/acpi.h>
6#include <linux/delay.h>
7#include <linux/i2c.h>
8#include <linux/module.h>
9#include <linux/pm_runtime.h>
10#include <media/v4l2-ctrls.h>
11#include <media/v4l2-device.h>
12#include <media/v4l2-fwnode.h>
13
14#define OV8856_REG_VALUE_08BIT 1
15#define OV8856_REG_VALUE_16BIT 2
16#define OV8856_REG_VALUE_24BIT 3
17
18#define OV8856_LINK_FREQ_360MHZ 360000000ULL
19#define OV8856_LINK_FREQ_180MHZ 180000000ULL
20#define OV8856_SCLK 144000000ULL
21#define OV8856_MCLK 19200000
22#define OV8856_DATA_LANES 4
23#define OV8856_RGB_DEPTH 10
24
25#define OV8856_REG_CHIP_ID 0x300a
26#define OV8856_CHIP_ID 0x00885a
27
28#define OV8856_REG_MODE_SELECT 0x0100
29#define OV8856_MODE_STANDBY 0x00
30#define OV8856_MODE_STREAMING 0x01
31
32
33#define OV8856_REG_VTS 0x380e
34#define OV8856_VTS_MAX 0x7fff
35
36
37#define OV8856_REG_HTS 0x380c
38
39
40#define OV8856_REG_EXPOSURE 0x3500
41#define OV8856_EXPOSURE_MIN 6
42#define OV8856_EXPOSURE_MAX_MARGIN 6
43#define OV8856_EXPOSURE_STEP 1
44
45
46#define OV8856_REG_ANALOG_GAIN 0x3508
47#define OV8856_ANAL_GAIN_MIN 128
48#define OV8856_ANAL_GAIN_MAX 2047
49#define OV8856_ANAL_GAIN_STEP 1
50
51
52#define OV8856_REG_MWB_R_GAIN 0x5019
53#define OV8856_REG_MWB_G_GAIN 0x501b
54#define OV8856_REG_MWB_B_GAIN 0x501d
55#define OV8856_DGTL_GAIN_MIN 0
56#define OV8856_DGTL_GAIN_MAX 4095
57#define OV8856_DGTL_GAIN_STEP 1
58#define OV8856_DGTL_GAIN_DEFAULT 1024
59
60
61#define OV8856_REG_TEST_PATTERN 0x5e00
62#define OV8856_TEST_PATTERN_ENABLE BIT(7)
63#define OV8856_TEST_PATTERN_BAR_SHIFT 2
64
65#define to_ov8856(_sd) container_of(_sd, struct ov8856, sd)
66
67enum {
68 OV8856_LINK_FREQ_720MBPS,
69 OV8856_LINK_FREQ_360MBPS,
70};
71
72struct ov8856_reg {
73 u16 address;
74 u8 val;
75};
76
77struct ov8856_reg_list {
78 u32 num_of_regs;
79 const struct ov8856_reg *regs;
80};
81
82struct ov8856_link_freq_config {
83 const struct ov8856_reg_list reg_list;
84};
85
86struct ov8856_mode {
87
88 u32 width;
89
90
91 u32 height;
92
93
94 u32 hts;
95
96
97 u32 vts_def;
98
99
100 u32 vts_min;
101
102
103 u32 link_freq_index;
104
105
106 const struct ov8856_reg_list reg_list;
107};
108
109static const struct ov8856_reg mipi_data_rate_720mbps[] = {
110 {0x0103, 0x01},
111 {0x0100, 0x00},
112 {0x0302, 0x4b},
113 {0x0303, 0x01},
114 {0x030b, 0x02},
115 {0x030d, 0x4b},
116 {0x031e, 0x0c},
117};
118
119static const struct ov8856_reg mipi_data_rate_360mbps[] = {
120 {0x0103, 0x01},
121 {0x0100, 0x00},
122 {0x0302, 0x4b},
123 {0x0303, 0x03},
124 {0x030b, 0x02},
125 {0x030d, 0x4b},
126 {0x031e, 0x0c},
127};
128
129static const struct ov8856_reg mode_3280x2464_regs[] = {
130 {0x3000, 0x20},
131 {0x3003, 0x08},
132 {0x300e, 0x20},
133 {0x3010, 0x00},
134 {0x3015, 0x84},
135 {0x3018, 0x72},
136 {0x3021, 0x23},
137 {0x3033, 0x24},
138 {0x3500, 0x00},
139 {0x3501, 0x9a},
140 {0x3502, 0x20},
141 {0x3503, 0x08},
142 {0x3505, 0x83},
143 {0x3508, 0x01},
144 {0x3509, 0x80},
145 {0x350c, 0x00},
146 {0x350d, 0x80},
147 {0x350e, 0x04},
148 {0x350f, 0x00},
149 {0x3510, 0x00},
150 {0x3511, 0x02},
151 {0x3512, 0x00},
152 {0x3600, 0x72},
153 {0x3601, 0x40},
154 {0x3602, 0x30},
155 {0x3610, 0xc5},
156 {0x3611, 0x58},
157 {0x3612, 0x5c},
158 {0x3613, 0xca},
159 {0x3614, 0x20},
160 {0x3628, 0xff},
161 {0x3629, 0xff},
162 {0x362a, 0xff},
163 {0x3633, 0x10},
164 {0x3634, 0x10},
165 {0x3635, 0x10},
166 {0x3636, 0x10},
167 {0x3663, 0x08},
168 {0x3669, 0x34},
169 {0x366e, 0x10},
170 {0x3706, 0x86},
171 {0x370b, 0x7e},
172 {0x3714, 0x23},
173 {0x3730, 0x12},
174 {0x3733, 0x10},
175 {0x3764, 0x00},
176 {0x3765, 0x00},
177 {0x3769, 0x62},
178 {0x376a, 0x2a},
179 {0x376b, 0x30},
180 {0x3780, 0x00},
181 {0x3781, 0x24},
182 {0x3782, 0x00},
183 {0x3783, 0x23},
184 {0x3798, 0x2f},
185 {0x37a1, 0x60},
186 {0x37a8, 0x6a},
187 {0x37ab, 0x3f},
188 {0x37c2, 0x04},
189 {0x37c3, 0xf1},
190 {0x37c9, 0x80},
191 {0x37cb, 0x16},
192 {0x37cc, 0x16},
193 {0x37cd, 0x16},
194 {0x37ce, 0x16},
195 {0x3800, 0x00},
196 {0x3801, 0x00},
197 {0x3802, 0x00},
198 {0x3803, 0x07},
199 {0x3804, 0x0c},
200 {0x3805, 0xdf},
201 {0x3806, 0x09},
202 {0x3807, 0xa6},
203 {0x3808, 0x0c},
204 {0x3809, 0xd0},
205 {0x380a, 0x09},
206 {0x380b, 0xa0},
207 {0x380c, 0x07},
208 {0x380d, 0x88},
209 {0x380e, 0x09},
210 {0x380f, 0xb8},
211 {0x3810, 0x00},
212 {0x3811, 0x00},
213 {0x3812, 0x00},
214 {0x3813, 0x00},
215 {0x3814, 0x01},
216 {0x3815, 0x01},
217 {0x3816, 0x00},
218 {0x3817, 0x00},
219 {0x3818, 0x00},
220 {0x3819, 0x10},
221 {0x3820, 0x80},
222 {0x3821, 0x46},
223 {0x382a, 0x01},
224 {0x382b, 0x01},
225 {0x3830, 0x06},
226 {0x3836, 0x02},
227 {0x3862, 0x04},
228 {0x3863, 0x08},
229 {0x3cc0, 0x33},
230 {0x3d85, 0x17},
231 {0x3d8c, 0x73},
232 {0x3d8d, 0xde},
233 {0x4001, 0xe0},
234 {0x4003, 0x40},
235 {0x4008, 0x00},
236 {0x4009, 0x0b},
237 {0x400a, 0x00},
238 {0x400b, 0x84},
239 {0x400f, 0x80},
240 {0x4010, 0xf0},
241 {0x4011, 0xff},
242 {0x4012, 0x02},
243 {0x4013, 0x01},
244 {0x4014, 0x01},
245 {0x4015, 0x01},
246 {0x4042, 0x00},
247 {0x4043, 0x80},
248 {0x4044, 0x00},
249 {0x4045, 0x80},
250 {0x4046, 0x00},
251 {0x4047, 0x80},
252 {0x4048, 0x00},
253 {0x4049, 0x80},
254 {0x4041, 0x03},
255 {0x404c, 0x20},
256 {0x404d, 0x00},
257 {0x404e, 0x20},
258 {0x4203, 0x80},
259 {0x4307, 0x30},
260 {0x4317, 0x00},
261 {0x4503, 0x08},
262 {0x4601, 0x80},
263 {0x4800, 0x44},
264 {0x4816, 0x53},
265 {0x481b, 0x58},
266 {0x481f, 0x27},
267 {0x4837, 0x16},
268 {0x483c, 0x0f},
269 {0x484b, 0x05},
270 {0x5000, 0x57},
271 {0x5001, 0x0a},
272 {0x5004, 0x04},
273 {0x502e, 0x03},
274 {0x5030, 0x41},
275 {0x5780, 0x14},
276 {0x5781, 0x0f},
277 {0x5782, 0x44},
278 {0x5783, 0x02},
279 {0x5784, 0x01},
280 {0x5785, 0x01},
281 {0x5786, 0x00},
282 {0x5787, 0x04},
283 {0x5788, 0x02},
284 {0x5789, 0x0f},
285 {0x578a, 0xfd},
286 {0x578b, 0xf5},
287 {0x578c, 0xf5},
288 {0x578d, 0x03},
289 {0x578e, 0x08},
290 {0x578f, 0x0c},
291 {0x5790, 0x08},
292 {0x5791, 0x04},
293 {0x5792, 0x00},
294 {0x5793, 0x52},
295 {0x5794, 0xa3},
296 {0x5795, 0x02},
297 {0x5796, 0x20},
298 {0x5797, 0x20},
299 {0x5798, 0xd5},
300 {0x5799, 0xd5},
301 {0x579a, 0x00},
302 {0x579b, 0x50},
303 {0x579c, 0x00},
304 {0x579d, 0x2c},
305 {0x579e, 0x0c},
306 {0x579f, 0x40},
307 {0x57a0, 0x09},
308 {0x57a1, 0x40},
309 {0x59f8, 0x3d},
310 {0x5a08, 0x02},
311 {0x5b00, 0x02},
312 {0x5b01, 0x10},
313 {0x5b02, 0x03},
314 {0x5b03, 0xcf},
315 {0x5b05, 0x6c},
316 {0x5e00, 0x00}
317};
318
319static const struct ov8856_reg mode_1640x1232_regs[] = {
320 {0x3000, 0x20},
321 {0x3003, 0x08},
322 {0x300e, 0x20},
323 {0x3010, 0x00},
324 {0x3015, 0x84},
325 {0x3018, 0x72},
326 {0x3021, 0x23},
327 {0x3033, 0x24},
328 {0x3500, 0x00},
329 {0x3501, 0x4c},
330 {0x3502, 0xe0},
331 {0x3503, 0x08},
332 {0x3505, 0x83},
333 {0x3508, 0x01},
334 {0x3509, 0x80},
335 {0x350c, 0x00},
336 {0x350d, 0x80},
337 {0x350e, 0x04},
338 {0x350f, 0x00},
339 {0x3510, 0x00},
340 {0x3511, 0x02},
341 {0x3512, 0x00},
342 {0x3600, 0x72},
343 {0x3601, 0x40},
344 {0x3602, 0x30},
345 {0x3610, 0xc5},
346 {0x3611, 0x58},
347 {0x3612, 0x5c},
348 {0x3613, 0xca},
349 {0x3614, 0x20},
350 {0x3628, 0xff},
351 {0x3629, 0xff},
352 {0x362a, 0xff},
353 {0x3633, 0x10},
354 {0x3634, 0x10},
355 {0x3635, 0x10},
356 {0x3636, 0x10},
357 {0x3663, 0x08},
358 {0x3669, 0x34},
359 {0x366e, 0x08},
360 {0x3706, 0x86},
361 {0x370b, 0x7e},
362 {0x3714, 0x27},
363 {0x3730, 0x12},
364 {0x3733, 0x10},
365 {0x3764, 0x00},
366 {0x3765, 0x00},
367 {0x3769, 0x62},
368 {0x376a, 0x2a},
369 {0x376b, 0x30},
370 {0x3780, 0x00},
371 {0x3781, 0x24},
372 {0x3782, 0x00},
373 {0x3783, 0x23},
374 {0x3798, 0x2f},
375 {0x37a1, 0x60},
376 {0x37a8, 0x6a},
377 {0x37ab, 0x3f},
378 {0x37c2, 0x14},
379 {0x37c3, 0xf1},
380 {0x37c9, 0x80},
381 {0x37cb, 0x16},
382 {0x37cc, 0x16},
383 {0x37cd, 0x16},
384 {0x37ce, 0x16},
385 {0x3800, 0x00},
386 {0x3801, 0x00},
387 {0x3802, 0x00},
388 {0x3803, 0x07},
389 {0x3804, 0x0c},
390 {0x3805, 0xdf},
391 {0x3806, 0x09},
392 {0x3807, 0xa6},
393 {0x3808, 0x06},
394 {0x3809, 0x68},
395 {0x380a, 0x04},
396 {0x380b, 0xd0},
397 {0x380c, 0x0e},
398 {0x380d, 0xec},
399 {0x380e, 0x04},
400 {0x380f, 0xe8},
401 {0x3810, 0x00},
402 {0x3811, 0x00},
403 {0x3812, 0x00},
404 {0x3813, 0x00},
405 {0x3814, 0x03},
406 {0x3815, 0x01},
407 {0x3816, 0x00},
408 {0x3817, 0x00},
409 {0x3818, 0x00},
410 {0x3819, 0x10},
411 {0x3820, 0x90},
412 {0x3821, 0x67},
413 {0x382a, 0x03},
414 {0x382b, 0x01},
415 {0x3830, 0x06},
416 {0x3836, 0x02},
417 {0x3862, 0x04},
418 {0x3863, 0x08},
419 {0x3cc0, 0x33},
420 {0x3d85, 0x17},
421 {0x3d8c, 0x73},
422 {0x3d8d, 0xde},
423 {0x4001, 0xe0},
424 {0x4003, 0x40},
425 {0x4008, 0x00},
426 {0x4009, 0x05},
427 {0x400a, 0x00},
428 {0x400b, 0x84},
429 {0x400f, 0x80},
430 {0x4010, 0xf0},
431 {0x4011, 0xff},
432 {0x4012, 0x02},
433 {0x4013, 0x01},
434 {0x4014, 0x01},
435 {0x4015, 0x01},
436 {0x4042, 0x00},
437 {0x4043, 0x80},
438 {0x4044, 0x00},
439 {0x4045, 0x80},
440 {0x4046, 0x00},
441 {0x4047, 0x80},
442 {0x4048, 0x00},
443 {0x4049, 0x80},
444 {0x4041, 0x03},
445 {0x404c, 0x20},
446 {0x404d, 0x00},
447 {0x404e, 0x20},
448 {0x4203, 0x80},
449 {0x4307, 0x30},
450 {0x4317, 0x00},
451 {0x4503, 0x08},
452 {0x4601, 0x80},
453 {0x4800, 0x44},
454 {0x4816, 0x53},
455 {0x481b, 0x58},
456 {0x481f, 0x27},
457 {0x4837, 0x16},
458 {0x483c, 0x0f},
459 {0x484b, 0x05},
460 {0x5000, 0x57},
461 {0x5001, 0x0a},
462 {0x5004, 0x04},
463 {0x502e, 0x03},
464 {0x5030, 0x41},
465 {0x5780, 0x14},
466 {0x5781, 0x0f},
467 {0x5782, 0x44},
468 {0x5783, 0x02},
469 {0x5784, 0x01},
470 {0x5785, 0x01},
471 {0x5786, 0x00},
472 {0x5787, 0x04},
473 {0x5788, 0x02},
474 {0x5789, 0x0f},
475 {0x578a, 0xfd},
476 {0x578b, 0xf5},
477 {0x578c, 0xf5},
478 {0x578d, 0x03},
479 {0x578e, 0x08},
480 {0x578f, 0x0c},
481 {0x5790, 0x08},
482 {0x5791, 0x04},
483 {0x5792, 0x00},
484 {0x5793, 0x52},
485 {0x5794, 0xa3},
486 {0x5795, 0x00},
487 {0x5796, 0x10},
488 {0x5797, 0x10},
489 {0x5798, 0x73},
490 {0x5799, 0x73},
491 {0x579a, 0x00},
492 {0x579b, 0x28},
493 {0x579c, 0x00},
494 {0x579d, 0x16},
495 {0x579e, 0x06},
496 {0x579f, 0x20},
497 {0x57a0, 0x04},
498 {0x57a1, 0xa0},
499 {0x59f8, 0x3d},
500 {0x5a08, 0x02},
501 {0x5b00, 0x02},
502 {0x5b01, 0x10},
503 {0x5b02, 0x03},
504 {0x5b03, 0xcf},
505 {0x5b05, 0x6c},
506 {0x5e00, 0x00}
507};
508
509static const char * const ov8856_test_pattern_menu[] = {
510 "Disabled",
511 "Standard Color Bar",
512 "Top-Bottom Darker Color Bar",
513 "Right-Left Darker Color Bar",
514 "Bottom-Top Darker Color Bar"
515};
516
517static const s64 link_freq_menu_items[] = {
518 OV8856_LINK_FREQ_360MHZ,
519 OV8856_LINK_FREQ_180MHZ
520};
521
522static const struct ov8856_link_freq_config link_freq_configs[] = {
523 [OV8856_LINK_FREQ_720MBPS] = {
524 .reg_list = {
525 .num_of_regs = ARRAY_SIZE(mipi_data_rate_720mbps),
526 .regs = mipi_data_rate_720mbps,
527 }
528 },
529 [OV8856_LINK_FREQ_360MBPS] = {
530 .reg_list = {
531 .num_of_regs = ARRAY_SIZE(mipi_data_rate_360mbps),
532 .regs = mipi_data_rate_360mbps,
533 }
534 }
535};
536
537static const struct ov8856_mode supported_modes[] = {
538 {
539 .width = 3280,
540 .height = 2464,
541 .hts = 1928,
542 .vts_def = 2488,
543 .vts_min = 2488,
544 .reg_list = {
545 .num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
546 .regs = mode_3280x2464_regs,
547 },
548 .link_freq_index = OV8856_LINK_FREQ_720MBPS,
549 },
550 {
551 .width = 1640,
552 .height = 1232,
553 .hts = 3820,
554 .vts_def = 1256,
555 .vts_min = 1256,
556 .reg_list = {
557 .num_of_regs = ARRAY_SIZE(mode_1640x1232_regs),
558 .regs = mode_1640x1232_regs,
559 },
560 .link_freq_index = OV8856_LINK_FREQ_360MBPS,
561 }
562};
563
564struct ov8856 {
565 struct v4l2_subdev sd;
566 struct media_pad pad;
567 struct v4l2_ctrl_handler ctrl_handler;
568
569
570 struct v4l2_ctrl *link_freq;
571 struct v4l2_ctrl *pixel_rate;
572 struct v4l2_ctrl *vblank;
573 struct v4l2_ctrl *hblank;
574 struct v4l2_ctrl *exposure;
575
576
577 const struct ov8856_mode *cur_mode;
578
579
580 struct mutex mutex;
581
582
583 bool streaming;
584};
585
586static u64 to_pixel_rate(u32 f_index)
587{
588 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV8856_DATA_LANES;
589
590 do_div(pixel_rate, OV8856_RGB_DEPTH);
591
592 return pixel_rate;
593}
594
595static u64 to_pixels_per_line(u32 hts, u32 f_index)
596{
597 u64 ppl = hts * to_pixel_rate(f_index);
598
599 do_div(ppl, OV8856_SCLK);
600
601 return ppl;
602}
603
604static int ov8856_read_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 *val)
605{
606 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
607 struct i2c_msg msgs[2];
608 u8 addr_buf[2];
609 u8 data_buf[4] = {0};
610 int ret;
611
612 if (len > 4)
613 return -EINVAL;
614
615 put_unaligned_be16(reg, addr_buf);
616 msgs[0].addr = client->addr;
617 msgs[0].flags = 0;
618 msgs[0].len = sizeof(addr_buf);
619 msgs[0].buf = addr_buf;
620 msgs[1].addr = client->addr;
621 msgs[1].flags = I2C_M_RD;
622 msgs[1].len = len;
623 msgs[1].buf = &data_buf[4 - len];
624
625 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
626 if (ret != ARRAY_SIZE(msgs))
627 return -EIO;
628
629 *val = get_unaligned_be32(data_buf);
630
631 return 0;
632}
633
634static int ov8856_write_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 val)
635{
636 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
637 u8 buf[6];
638
639 if (len > 4)
640 return -EINVAL;
641
642 put_unaligned_be16(reg, buf);
643 put_unaligned_be32(val << 8 * (4 - len), buf + 2);
644 if (i2c_master_send(client, buf, len + 2) != len + 2)
645 return -EIO;
646
647 return 0;
648}
649
650static int ov8856_write_reg_list(struct ov8856 *ov8856,
651 const struct ov8856_reg_list *r_list)
652{
653 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
654 unsigned int i;
655 int ret;
656
657 for (i = 0; i < r_list->num_of_regs; i++) {
658 ret = ov8856_write_reg(ov8856, r_list->regs[i].address, 1,
659 r_list->regs[i].val);
660 if (ret) {
661 dev_err_ratelimited(&client->dev,
662 "failed to write reg 0x%4.4x. error = %d",
663 r_list->regs[i].address, ret);
664 return ret;
665 }
666 }
667
668 return 0;
669}
670
671static int ov8856_update_digital_gain(struct ov8856 *ov8856, u32 d_gain)
672{
673 int ret;
674
675 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_R_GAIN,
676 OV8856_REG_VALUE_16BIT, d_gain);
677 if (ret)
678 return ret;
679
680 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_G_GAIN,
681 OV8856_REG_VALUE_16BIT, d_gain);
682 if (ret)
683 return ret;
684
685 return ov8856_write_reg(ov8856, OV8856_REG_MWB_B_GAIN,
686 OV8856_REG_VALUE_16BIT, d_gain);
687}
688
689static int ov8856_test_pattern(struct ov8856 *ov8856, u32 pattern)
690{
691 if (pattern)
692 pattern = (pattern - 1) << OV8856_TEST_PATTERN_BAR_SHIFT |
693 OV8856_TEST_PATTERN_ENABLE;
694
695 return ov8856_write_reg(ov8856, OV8856_REG_TEST_PATTERN,
696 OV8856_REG_VALUE_08BIT, pattern);
697}
698
699static int ov8856_set_ctrl(struct v4l2_ctrl *ctrl)
700{
701 struct ov8856 *ov8856 = container_of(ctrl->handler,
702 struct ov8856, ctrl_handler);
703 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
704 s64 exposure_max;
705 int ret = 0;
706
707
708 if (ctrl->id == V4L2_CID_VBLANK) {
709
710 exposure_max = ov8856->cur_mode->height + ctrl->val -
711 OV8856_EXPOSURE_MAX_MARGIN;
712 __v4l2_ctrl_modify_range(ov8856->exposure,
713 ov8856->exposure->minimum,
714 exposure_max, ov8856->exposure->step,
715 exposure_max);
716 }
717
718
719 if (!pm_runtime_get_if_in_use(&client->dev))
720 return 0;
721
722 switch (ctrl->id) {
723 case V4L2_CID_ANALOGUE_GAIN:
724 ret = ov8856_write_reg(ov8856, OV8856_REG_ANALOG_GAIN,
725 OV8856_REG_VALUE_16BIT, ctrl->val);
726 break;
727
728 case V4L2_CID_DIGITAL_GAIN:
729 ret = ov8856_update_digital_gain(ov8856, ctrl->val);
730 break;
731
732 case V4L2_CID_EXPOSURE:
733
734 ret = ov8856_write_reg(ov8856, OV8856_REG_EXPOSURE,
735 OV8856_REG_VALUE_24BIT, ctrl->val << 4);
736 break;
737
738 case V4L2_CID_VBLANK:
739 ret = ov8856_write_reg(ov8856, OV8856_REG_VTS,
740 OV8856_REG_VALUE_16BIT,
741 ov8856->cur_mode->height + ctrl->val);
742 break;
743
744 case V4L2_CID_TEST_PATTERN:
745 ret = ov8856_test_pattern(ov8856, ctrl->val);
746 break;
747
748 default:
749 ret = -EINVAL;
750 break;
751 }
752
753 pm_runtime_put(&client->dev);
754
755 return ret;
756}
757
758static const struct v4l2_ctrl_ops ov8856_ctrl_ops = {
759 .s_ctrl = ov8856_set_ctrl,
760};
761
762static int ov8856_init_controls(struct ov8856 *ov8856)
763{
764 struct v4l2_ctrl_handler *ctrl_hdlr;
765 s64 exposure_max, h_blank;
766 int ret;
767
768 ctrl_hdlr = &ov8856->ctrl_handler;
769 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
770 if (ret)
771 return ret;
772
773 ctrl_hdlr->lock = &ov8856->mutex;
774 ov8856->link_freq = v4l2_ctrl_new_int_menu(ctrl_hdlr, &ov8856_ctrl_ops,
775 V4L2_CID_LINK_FREQ,
776 ARRAY_SIZE(link_freq_menu_items) - 1,
777 0, link_freq_menu_items);
778 if (ov8856->link_freq)
779 ov8856->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
780
781 ov8856->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
782 V4L2_CID_PIXEL_RATE, 0,
783 to_pixel_rate(OV8856_LINK_FREQ_720MBPS),
784 1,
785 to_pixel_rate(OV8856_LINK_FREQ_720MBPS));
786 ov8856->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
787 V4L2_CID_VBLANK,
788 ov8856->cur_mode->vts_min - ov8856->cur_mode->height,
789 OV8856_VTS_MAX - ov8856->cur_mode->height, 1,
790 ov8856->cur_mode->vts_def - ov8856->cur_mode->height);
791 h_blank = to_pixels_per_line(ov8856->cur_mode->hts,
792 ov8856->cur_mode->link_freq_index) - ov8856->cur_mode->width;
793 ov8856->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
794 V4L2_CID_HBLANK, h_blank, h_blank, 1,
795 h_blank);
796 if (ov8856->hblank)
797 ov8856->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
798
799 v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops, V4L2_CID_ANALOGUE_GAIN,
800 OV8856_ANAL_GAIN_MIN, OV8856_ANAL_GAIN_MAX,
801 OV8856_ANAL_GAIN_STEP, OV8856_ANAL_GAIN_MIN);
802 v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops, V4L2_CID_DIGITAL_GAIN,
803 OV8856_DGTL_GAIN_MIN, OV8856_DGTL_GAIN_MAX,
804 OV8856_DGTL_GAIN_STEP, OV8856_DGTL_GAIN_DEFAULT);
805 exposure_max = ov8856->cur_mode->vts_def - OV8856_EXPOSURE_MAX_MARGIN;
806 ov8856->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &ov8856_ctrl_ops,
807 V4L2_CID_EXPOSURE,
808 OV8856_EXPOSURE_MIN, exposure_max,
809 OV8856_EXPOSURE_STEP,
810 exposure_max);
811 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &ov8856_ctrl_ops,
812 V4L2_CID_TEST_PATTERN,
813 ARRAY_SIZE(ov8856_test_pattern_menu) - 1,
814 0, 0, ov8856_test_pattern_menu);
815 if (ctrl_hdlr->error)
816 return ctrl_hdlr->error;
817
818 ov8856->sd.ctrl_handler = ctrl_hdlr;
819
820 return 0;
821}
822
823static void ov8856_update_pad_format(const struct ov8856_mode *mode,
824 struct v4l2_mbus_framefmt *fmt)
825{
826 fmt->width = mode->width;
827 fmt->height = mode->height;
828 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
829 fmt->field = V4L2_FIELD_NONE;
830}
831
832static int ov8856_start_streaming(struct ov8856 *ov8856)
833{
834 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
835 const struct ov8856_reg_list *reg_list;
836 int link_freq_index, ret;
837
838 link_freq_index = ov8856->cur_mode->link_freq_index;
839 reg_list = &link_freq_configs[link_freq_index].reg_list;
840 ret = ov8856_write_reg_list(ov8856, reg_list);
841 if (ret) {
842 dev_err(&client->dev, "failed to set plls");
843 return ret;
844 }
845
846 reg_list = &ov8856->cur_mode->reg_list;
847 ret = ov8856_write_reg_list(ov8856, reg_list);
848 if (ret) {
849 dev_err(&client->dev, "failed to set mode");
850 return ret;
851 }
852
853 ret = __v4l2_ctrl_handler_setup(ov8856->sd.ctrl_handler);
854 if (ret)
855 return ret;
856
857 ret = ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
858 OV8856_REG_VALUE_08BIT, OV8856_MODE_STREAMING);
859 if (ret) {
860 dev_err(&client->dev, "failed to set stream");
861 return ret;
862 }
863
864 return 0;
865}
866
867static void ov8856_stop_streaming(struct ov8856 *ov8856)
868{
869 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
870
871 if (ov8856_write_reg(ov8856, OV8856_REG_MODE_SELECT,
872 OV8856_REG_VALUE_08BIT, OV8856_MODE_STANDBY))
873 dev_err(&client->dev, "failed to set stream");
874}
875
876static int ov8856_set_stream(struct v4l2_subdev *sd, int enable)
877{
878 struct ov8856 *ov8856 = to_ov8856(sd);
879 struct i2c_client *client = v4l2_get_subdevdata(sd);
880 int ret = 0;
881
882 if (ov8856->streaming == enable)
883 return 0;
884
885 mutex_lock(&ov8856->mutex);
886 if (enable) {
887 ret = pm_runtime_get_sync(&client->dev);
888 if (ret < 0) {
889 pm_runtime_put_noidle(&client->dev);
890 mutex_unlock(&ov8856->mutex);
891 return ret;
892 }
893
894 ret = ov8856_start_streaming(ov8856);
895 if (ret) {
896 enable = 0;
897 ov8856_stop_streaming(ov8856);
898 pm_runtime_put(&client->dev);
899 }
900 } else {
901 ov8856_stop_streaming(ov8856);
902 pm_runtime_put(&client->dev);
903 }
904
905 ov8856->streaming = enable;
906 mutex_unlock(&ov8856->mutex);
907
908 return ret;
909}
910
911static int __maybe_unused ov8856_suspend(struct device *dev)
912{
913 struct i2c_client *client = to_i2c_client(dev);
914 struct v4l2_subdev *sd = i2c_get_clientdata(client);
915 struct ov8856 *ov8856 = to_ov8856(sd);
916
917 mutex_lock(&ov8856->mutex);
918 if (ov8856->streaming)
919 ov8856_stop_streaming(ov8856);
920
921 mutex_unlock(&ov8856->mutex);
922
923 return 0;
924}
925
926static int __maybe_unused ov8856_resume(struct device *dev)
927{
928 struct i2c_client *client = to_i2c_client(dev);
929 struct v4l2_subdev *sd = i2c_get_clientdata(client);
930 struct ov8856 *ov8856 = to_ov8856(sd);
931 int ret;
932
933 mutex_lock(&ov8856->mutex);
934 if (ov8856->streaming) {
935 ret = ov8856_start_streaming(ov8856);
936 if (ret) {
937 ov8856->streaming = false;
938 ov8856_stop_streaming(ov8856);
939 mutex_unlock(&ov8856->mutex);
940 return ret;
941 }
942 }
943
944 mutex_unlock(&ov8856->mutex);
945
946 return 0;
947}
948
949static int ov8856_set_format(struct v4l2_subdev *sd,
950 struct v4l2_subdev_pad_config *cfg,
951 struct v4l2_subdev_format *fmt)
952{
953 struct ov8856 *ov8856 = to_ov8856(sd);
954 const struct ov8856_mode *mode;
955 s32 vblank_def, h_blank;
956
957 mode = v4l2_find_nearest_size(supported_modes,
958 ARRAY_SIZE(supported_modes), width,
959 height, fmt->format.width,
960 fmt->format.height);
961
962 mutex_lock(&ov8856->mutex);
963 ov8856_update_pad_format(mode, &fmt->format);
964 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
965 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
966 } else {
967 ov8856->cur_mode = mode;
968 __v4l2_ctrl_s_ctrl(ov8856->link_freq, mode->link_freq_index);
969 __v4l2_ctrl_s_ctrl_int64(ov8856->pixel_rate,
970 to_pixel_rate(mode->link_freq_index));
971
972
973 vblank_def = mode->vts_def - mode->height;
974 __v4l2_ctrl_modify_range(ov8856->vblank,
975 mode->vts_min - mode->height,
976 OV8856_VTS_MAX - mode->height, 1,
977 vblank_def);
978 __v4l2_ctrl_s_ctrl(ov8856->vblank, vblank_def);
979 h_blank = to_pixels_per_line(mode->hts, mode->link_freq_index) -
980 mode->width;
981 __v4l2_ctrl_modify_range(ov8856->hblank, h_blank, h_blank, 1,
982 h_blank);
983 }
984
985 mutex_unlock(&ov8856->mutex);
986
987 return 0;
988}
989
990static int ov8856_get_format(struct v4l2_subdev *sd,
991 struct v4l2_subdev_pad_config *cfg,
992 struct v4l2_subdev_format *fmt)
993{
994 struct ov8856 *ov8856 = to_ov8856(sd);
995
996 mutex_lock(&ov8856->mutex);
997 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
998 fmt->format = *v4l2_subdev_get_try_format(&ov8856->sd, cfg,
999 fmt->pad);
1000 else
1001 ov8856_update_pad_format(ov8856->cur_mode, &fmt->format);
1002
1003 mutex_unlock(&ov8856->mutex);
1004
1005 return 0;
1006}
1007
1008static int ov8856_enum_mbus_code(struct v4l2_subdev *sd,
1009 struct v4l2_subdev_pad_config *cfg,
1010 struct v4l2_subdev_mbus_code_enum *code)
1011{
1012
1013 if (code->index > 0)
1014 return -EINVAL;
1015
1016 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1017
1018 return 0;
1019}
1020
1021static int ov8856_enum_frame_size(struct v4l2_subdev *sd,
1022 struct v4l2_subdev_pad_config *cfg,
1023 struct v4l2_subdev_frame_size_enum *fse)
1024{
1025 if (fse->index >= ARRAY_SIZE(supported_modes))
1026 return -EINVAL;
1027
1028 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1029 return -EINVAL;
1030
1031 fse->min_width = supported_modes[fse->index].width;
1032 fse->max_width = fse->min_width;
1033 fse->min_height = supported_modes[fse->index].height;
1034 fse->max_height = fse->min_height;
1035
1036 return 0;
1037}
1038
1039static int ov8856_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1040{
1041 struct ov8856 *ov8856 = to_ov8856(sd);
1042
1043 mutex_lock(&ov8856->mutex);
1044 ov8856_update_pad_format(&supported_modes[0],
1045 v4l2_subdev_get_try_format(sd, fh->pad, 0));
1046 mutex_unlock(&ov8856->mutex);
1047
1048 return 0;
1049}
1050
1051static const struct v4l2_subdev_video_ops ov8856_video_ops = {
1052 .s_stream = ov8856_set_stream,
1053};
1054
1055static const struct v4l2_subdev_pad_ops ov8856_pad_ops = {
1056 .set_fmt = ov8856_set_format,
1057 .get_fmt = ov8856_get_format,
1058 .enum_mbus_code = ov8856_enum_mbus_code,
1059 .enum_frame_size = ov8856_enum_frame_size,
1060};
1061
1062static const struct v4l2_subdev_ops ov8856_subdev_ops = {
1063 .video = &ov8856_video_ops,
1064 .pad = &ov8856_pad_ops,
1065};
1066
1067static const struct media_entity_operations ov8856_subdev_entity_ops = {
1068 .link_validate = v4l2_subdev_link_validate,
1069};
1070
1071static const struct v4l2_subdev_internal_ops ov8856_internal_ops = {
1072 .open = ov8856_open,
1073};
1074
1075static int ov8856_identify_module(struct ov8856 *ov8856)
1076{
1077 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1078 int ret;
1079 u32 val;
1080
1081 ret = ov8856_read_reg(ov8856, OV8856_REG_CHIP_ID,
1082 OV8856_REG_VALUE_24BIT, &val);
1083 if (ret)
1084 return ret;
1085
1086 if (val != OV8856_CHIP_ID) {
1087 dev_err(&client->dev, "chip id mismatch: %x!=%x",
1088 OV8856_CHIP_ID, val);
1089 return -ENXIO;
1090 }
1091
1092 return 0;
1093}
1094
1095static int ov8856_check_hwcfg(struct device *dev)
1096{
1097 struct fwnode_handle *ep;
1098 struct fwnode_handle *fwnode = dev_fwnode(dev);
1099 struct v4l2_fwnode_endpoint bus_cfg = {
1100 .bus_type = V4L2_MBUS_CSI2_DPHY
1101 };
1102 u32 mclk;
1103 int ret;
1104 unsigned int i, j;
1105
1106 if (!fwnode)
1107 return -ENXIO;
1108
1109 fwnode_property_read_u32(fwnode, "clock-frequency", &mclk);
1110 if (mclk != OV8856_MCLK) {
1111 dev_err(dev, "external clock %d is not supported", mclk);
1112 return -EINVAL;
1113 }
1114
1115 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1116 if (!ep)
1117 return -ENXIO;
1118
1119 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1120 fwnode_handle_put(ep);
1121 if (ret)
1122 return ret;
1123
1124 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV8856_DATA_LANES) {
1125 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1126 bus_cfg.bus.mipi_csi2.num_data_lanes);
1127 ret = -EINVAL;
1128 goto check_hwcfg_error;
1129 }
1130
1131 if (!bus_cfg.nr_of_link_frequencies) {
1132 dev_err(dev, "no link frequencies defined");
1133 ret = -EINVAL;
1134 goto check_hwcfg_error;
1135 }
1136
1137 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1138 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1139 if (link_freq_menu_items[i] ==
1140 bus_cfg.link_frequencies[j])
1141 break;
1142 }
1143
1144 if (j == bus_cfg.nr_of_link_frequencies) {
1145 dev_err(dev, "no link frequency %lld supported",
1146 link_freq_menu_items[i]);
1147 ret = -EINVAL;
1148 goto check_hwcfg_error;
1149 }
1150 }
1151
1152check_hwcfg_error:
1153 v4l2_fwnode_endpoint_free(&bus_cfg);
1154
1155 return ret;
1156}
1157
1158static int ov8856_remove(struct i2c_client *client)
1159{
1160 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1161 struct ov8856 *ov8856 = to_ov8856(sd);
1162
1163 v4l2_async_unregister_subdev(sd);
1164 media_entity_cleanup(&sd->entity);
1165 v4l2_ctrl_handler_free(sd->ctrl_handler);
1166 pm_runtime_disable(&client->dev);
1167 mutex_destroy(&ov8856->mutex);
1168
1169 return 0;
1170}
1171
1172static int ov8856_probe(struct i2c_client *client)
1173{
1174 struct ov8856 *ov8856;
1175 int ret;
1176
1177 ret = ov8856_check_hwcfg(&client->dev);
1178 if (ret) {
1179 dev_err(&client->dev, "failed to check HW configuration: %d",
1180 ret);
1181 return ret;
1182 }
1183
1184 ov8856 = devm_kzalloc(&client->dev, sizeof(*ov8856), GFP_KERNEL);
1185 if (!ov8856)
1186 return -ENOMEM;
1187
1188 v4l2_i2c_subdev_init(&ov8856->sd, client, &ov8856_subdev_ops);
1189 ret = ov8856_identify_module(ov8856);
1190 if (ret) {
1191 dev_err(&client->dev, "failed to find sensor: %d", ret);
1192 return ret;
1193 }
1194
1195 mutex_init(&ov8856->mutex);
1196 ov8856->cur_mode = &supported_modes[0];
1197 ret = ov8856_init_controls(ov8856);
1198 if (ret) {
1199 dev_err(&client->dev, "failed to init controls: %d", ret);
1200 goto probe_error_v4l2_ctrl_handler_free;
1201 }
1202
1203 ov8856->sd.internal_ops = &ov8856_internal_ops;
1204 ov8856->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1205 ov8856->sd.entity.ops = &ov8856_subdev_entity_ops;
1206 ov8856->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1207 ov8856->pad.flags = MEDIA_PAD_FL_SOURCE;
1208 ret = media_entity_pads_init(&ov8856->sd.entity, 1, &ov8856->pad);
1209 if (ret) {
1210 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1211 goto probe_error_v4l2_ctrl_handler_free;
1212 }
1213
1214 ret = v4l2_async_register_subdev_sensor_common(&ov8856->sd);
1215 if (ret < 0) {
1216 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1217 ret);
1218 goto probe_error_media_entity_cleanup;
1219 }
1220
1221
1222
1223
1224
1225 pm_runtime_set_active(&client->dev);
1226 pm_runtime_enable(&client->dev);
1227 pm_runtime_idle(&client->dev);
1228
1229 return 0;
1230
1231probe_error_media_entity_cleanup:
1232 media_entity_cleanup(&ov8856->sd.entity);
1233
1234probe_error_v4l2_ctrl_handler_free:
1235 v4l2_ctrl_handler_free(ov8856->sd.ctrl_handler);
1236 mutex_destroy(&ov8856->mutex);
1237
1238 return ret;
1239}
1240
1241static const struct dev_pm_ops ov8856_pm_ops = {
1242 SET_SYSTEM_SLEEP_PM_OPS(ov8856_suspend, ov8856_resume)
1243};
1244
1245#ifdef CONFIG_ACPI
1246static const struct acpi_device_id ov8856_acpi_ids[] = {
1247 {"OVTI8856"},
1248 {}
1249};
1250
1251MODULE_DEVICE_TABLE(acpi, ov8856_acpi_ids);
1252#endif
1253
1254static struct i2c_driver ov8856_i2c_driver = {
1255 .driver = {
1256 .name = "ov8856",
1257 .pm = &ov8856_pm_ops,
1258 .acpi_match_table = ACPI_PTR(ov8856_acpi_ids),
1259 },
1260 .probe_new = ov8856_probe,
1261 .remove = ov8856_remove,
1262};
1263
1264module_i2c_driver(ov8856_i2c_driver);
1265
1266MODULE_AUTHOR("Ben Kao <ben.kao@intel.com>");
1267MODULE_DESCRIPTION("OmniVision OV8856 sensor driver");
1268MODULE_LICENSE("GPL v2");
1269