gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / media / i2c / ov8856.c
blob8655842af2755886620480d1120f8d251b6d06f6
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/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>
14 #define OV8856_REG_VALUE_08BIT 1
15 #define OV8856_REG_VALUE_16BIT 2
16 #define OV8856_REG_VALUE_24BIT 3
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
25 #define OV8856_REG_CHIP_ID 0x300a
26 #define OV8856_CHIP_ID 0x00885a
28 #define OV8856_REG_MODE_SELECT 0x0100
29 #define OV8856_MODE_STANDBY 0x00
30 #define OV8856_MODE_STREAMING 0x01
32 /* vertical-timings from sensor */
33 #define OV8856_REG_VTS 0x380e
34 #define OV8856_VTS_MAX 0x7fff
36 /* horizontal-timings from sensor */
37 #define OV8856_REG_HTS 0x380c
39 /* Exposure controls from sensor */
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
45 /* Analog gain controls from sensor */
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
51 /* Digital gain controls from sensor */
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
60 /* Test Pattern Control */
61 #define OV8856_REG_TEST_PATTERN 0x5e00
62 #define OV8856_TEST_PATTERN_ENABLE BIT(7)
63 #define OV8856_TEST_PATTERN_BAR_SHIFT 2
65 #define to_ov8856(_sd) container_of(_sd, struct ov8856, sd)
67 enum {
68 OV8856_LINK_FREQ_720MBPS,
69 OV8856_LINK_FREQ_360MBPS,
72 struct ov8856_reg {
73 u16 address;
74 u8 val;
77 struct ov8856_reg_list {
78 u32 num_of_regs;
79 const struct ov8856_reg *regs;
82 struct ov8856_link_freq_config {
83 const struct ov8856_reg_list reg_list;
86 struct ov8856_mode {
87 /* Frame width in pixels */
88 u32 width;
90 /* Frame height in pixels */
91 u32 height;
93 /* Horizontal timining size */
94 u32 hts;
96 /* Default vertical timining size */
97 u32 vts_def;
99 /* Min vertical timining size */
100 u32 vts_min;
102 /* Link frequency needed for this resolution */
103 u32 link_freq_index;
105 /* Sensor register settings for this resolution */
106 const struct ov8856_reg_list reg_list;
109 static 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},
119 static 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},
129 static 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, 0x06},
199 {0x3804, 0x0c},
200 {0x3805, 0xdf},
201 {0x3806, 0x09},
202 {0x3807, 0xa7},
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, 0x01},
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}
319 static 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, 0x06},
389 {0x3804, 0x0c},
390 {0x3805, 0xdf},
391 {0x3806, 0x09},
392 {0x3807, 0xa7},
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, 0x01},
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}
509 static 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"
517 static const s64 link_freq_menu_items[] = {
518 OV8856_LINK_FREQ_360MHZ,
519 OV8856_LINK_FREQ_180MHZ
522 static 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,
529 [OV8856_LINK_FREQ_360MBPS] = {
530 .reg_list = {
531 .num_of_regs = ARRAY_SIZE(mipi_data_rate_360mbps),
532 .regs = mipi_data_rate_360mbps,
537 static const struct ov8856_mode supported_modes[] = {
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,
548 .link_freq_index = OV8856_LINK_FREQ_720MBPS,
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,
560 .link_freq_index = OV8856_LINK_FREQ_360MBPS,
564 struct ov8856 {
565 struct v4l2_subdev sd;
566 struct media_pad pad;
567 struct v4l2_ctrl_handler ctrl_handler;
569 /* V4L2 Controls */
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;
576 /* Current mode */
577 const struct ov8856_mode *cur_mode;
579 /* To serialize asynchronus callbacks */
580 struct mutex mutex;
582 /* Streaming on/off */
583 bool streaming;
586 static u64 to_pixel_rate(u32 f_index)
588 u64 pixel_rate = link_freq_menu_items[f_index] * 2 * OV8856_DATA_LANES;
590 do_div(pixel_rate, OV8856_RGB_DEPTH);
592 return pixel_rate;
595 static u64 to_pixels_per_line(u32 hts, u32 f_index)
597 u64 ppl = hts * to_pixel_rate(f_index);
599 do_div(ppl, OV8856_SCLK);
601 return ppl;
604 static int ov8856_read_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 *val)
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;
612 if (len > 4)
613 return -EINVAL;
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];
625 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
626 if (ret != ARRAY_SIZE(msgs))
627 return -EIO;
629 *val = get_unaligned_be32(data_buf);
631 return 0;
634 static int ov8856_write_reg(struct ov8856 *ov8856, u16 reg, u16 len, u32 val)
636 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
637 u8 buf[6];
639 if (len > 4)
640 return -EINVAL;
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;
647 return 0;
650 static int ov8856_write_reg_list(struct ov8856 *ov8856,
651 const struct ov8856_reg_list *r_list)
653 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
654 unsigned int i;
655 int ret;
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;
668 return 0;
671 static int ov8856_update_digital_gain(struct ov8856 *ov8856, u32 d_gain)
673 int ret;
675 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_R_GAIN,
676 OV8856_REG_VALUE_16BIT, d_gain);
677 if (ret)
678 return ret;
680 ret = ov8856_write_reg(ov8856, OV8856_REG_MWB_G_GAIN,
681 OV8856_REG_VALUE_16BIT, d_gain);
682 if (ret)
683 return ret;
685 return ov8856_write_reg(ov8856, OV8856_REG_MWB_B_GAIN,
686 OV8856_REG_VALUE_16BIT, d_gain);
689 static int ov8856_test_pattern(struct ov8856 *ov8856, u32 pattern)
691 if (pattern)
692 pattern = (pattern - 1) << OV8856_TEST_PATTERN_BAR_SHIFT |
693 OV8856_TEST_PATTERN_ENABLE;
695 return ov8856_write_reg(ov8856, OV8856_REG_TEST_PATTERN,
696 OV8856_REG_VALUE_08BIT, pattern);
699 static int ov8856_set_ctrl(struct v4l2_ctrl *ctrl)
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;
707 /* Propagate change of current control to all related controls */
708 if (ctrl->id == V4L2_CID_VBLANK) {
709 /* Update max exposure while meeting expected vblanking */
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);
718 /* V4L2 controls values will be applied only when power is already up */
719 if (!pm_runtime_get_if_in_use(&client->dev))
720 return 0;
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;
728 case V4L2_CID_DIGITAL_GAIN:
729 ret = ov8856_update_digital_gain(ov8856, ctrl->val);
730 break;
732 case V4L2_CID_EXPOSURE:
733 /* 4 least significant bits of expsoure are fractional part */
734 ret = ov8856_write_reg(ov8856, OV8856_REG_EXPOSURE,
735 OV8856_REG_VALUE_24BIT, ctrl->val << 4);
736 break;
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;
744 case V4L2_CID_TEST_PATTERN:
745 ret = ov8856_test_pattern(ov8856, ctrl->val);
746 break;
748 default:
749 ret = -EINVAL;
750 break;
753 pm_runtime_put(&client->dev);
755 return ret;
758 static const struct v4l2_ctrl_ops ov8856_ctrl_ops = {
759 .s_ctrl = ov8856_set_ctrl,
762 static int ov8856_init_controls(struct ov8856 *ov8856)
764 struct v4l2_ctrl_handler *ctrl_hdlr;
765 s64 exposure_max, h_blank;
766 int ret;
768 ctrl_hdlr = &ov8856->ctrl_handler;
769 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 8);
770 if (ret)
771 return ret;
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;
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),
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;
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;
818 ov8856->sd.ctrl_handler = ctrl_hdlr;
820 return 0;
823 static void ov8856_update_pad_format(const struct ov8856_mode *mode,
824 struct v4l2_mbus_framefmt *fmt)
826 fmt->width = mode->width;
827 fmt->height = mode->height;
828 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
829 fmt->field = V4L2_FIELD_NONE;
832 static int ov8856_start_streaming(struct ov8856 *ov8856)
834 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
835 const struct ov8856_reg_list *reg_list;
836 int link_freq_index, ret;
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;
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;
853 ret = __v4l2_ctrl_handler_setup(ov8856->sd.ctrl_handler);
854 if (ret)
855 return ret;
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;
864 return 0;
867 static void ov8856_stop_streaming(struct ov8856 *ov8856)
869 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
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");
876 static int ov8856_set_stream(struct v4l2_subdev *sd, int enable)
878 struct ov8856 *ov8856 = to_ov8856(sd);
879 struct i2c_client *client = v4l2_get_subdevdata(sd);
880 int ret = 0;
882 if (ov8856->streaming == enable)
883 return 0;
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;
894 ret = ov8856_start_streaming(ov8856);
895 if (ret) {
896 enable = 0;
897 ov8856_stop_streaming(ov8856);
898 pm_runtime_put(&client->dev);
900 } else {
901 ov8856_stop_streaming(ov8856);
902 pm_runtime_put(&client->dev);
905 ov8856->streaming = enable;
906 mutex_unlock(&ov8856->mutex);
908 return ret;
911 static int __maybe_unused ov8856_suspend(struct device *dev)
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);
917 mutex_lock(&ov8856->mutex);
918 if (ov8856->streaming)
919 ov8856_stop_streaming(ov8856);
921 mutex_unlock(&ov8856->mutex);
923 return 0;
926 static int __maybe_unused ov8856_resume(struct device *dev)
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;
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;
944 mutex_unlock(&ov8856->mutex);
946 return 0;
949 static int ov8856_set_format(struct v4l2_subdev *sd,
950 struct v4l2_subdev_pad_config *cfg,
951 struct v4l2_subdev_format *fmt)
953 struct ov8856 *ov8856 = to_ov8856(sd);
954 const struct ov8856_mode *mode;
955 s32 vblank_def, h_blank;
957 mode = v4l2_find_nearest_size(supported_modes,
958 ARRAY_SIZE(supported_modes), width,
959 height, fmt->format.width,
960 fmt->format.height);
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));
972 /* Update limits and set FPS to default */
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);
985 mutex_unlock(&ov8856->mutex);
987 return 0;
990 static int ov8856_get_format(struct v4l2_subdev *sd,
991 struct v4l2_subdev_pad_config *cfg,
992 struct v4l2_subdev_format *fmt)
994 struct ov8856 *ov8856 = to_ov8856(sd);
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);
1003 mutex_unlock(&ov8856->mutex);
1005 return 0;
1008 static int ov8856_enum_mbus_code(struct v4l2_subdev *sd,
1009 struct v4l2_subdev_pad_config *cfg,
1010 struct v4l2_subdev_mbus_code_enum *code)
1012 /* Only one bayer order GRBG is supported */
1013 if (code->index > 0)
1014 return -EINVAL;
1016 code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1018 return 0;
1021 static int ov8856_enum_frame_size(struct v4l2_subdev *sd,
1022 struct v4l2_subdev_pad_config *cfg,
1023 struct v4l2_subdev_frame_size_enum *fse)
1025 if (fse->index >= ARRAY_SIZE(supported_modes))
1026 return -EINVAL;
1028 if (fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
1029 return -EINVAL;
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;
1036 return 0;
1039 static int ov8856_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1041 struct ov8856 *ov8856 = to_ov8856(sd);
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);
1048 return 0;
1051 static const struct v4l2_subdev_video_ops ov8856_video_ops = {
1052 .s_stream = ov8856_set_stream,
1055 static 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,
1062 static const struct v4l2_subdev_ops ov8856_subdev_ops = {
1063 .video = &ov8856_video_ops,
1064 .pad = &ov8856_pad_ops,
1067 static const struct media_entity_operations ov8856_subdev_entity_ops = {
1068 .link_validate = v4l2_subdev_link_validate,
1071 static const struct v4l2_subdev_internal_ops ov8856_internal_ops = {
1072 .open = ov8856_open,
1075 static int ov8856_identify_module(struct ov8856 *ov8856)
1077 struct i2c_client *client = v4l2_get_subdevdata(&ov8856->sd);
1078 int ret;
1079 u32 val;
1081 ret = ov8856_read_reg(ov8856, OV8856_REG_CHIP_ID,
1082 OV8856_REG_VALUE_24BIT, &val);
1083 if (ret)
1084 return ret;
1086 if (val != OV8856_CHIP_ID) {
1087 dev_err(&client->dev, "chip id mismatch: %x!=%x",
1088 OV8856_CHIP_ID, val);
1089 return -ENXIO;
1092 return 0;
1095 static int ov8856_check_hwcfg(struct device *dev)
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
1102 u32 mclk;
1103 int ret;
1104 unsigned int i, j;
1106 if (!fwnode)
1107 return -ENXIO;
1109 ret = fwnode_property_read_u32(fwnode, "clock-frequency", &mclk);
1110 if (ret)
1111 return ret;
1113 if (mclk != OV8856_MCLK) {
1114 dev_err(dev, "external clock %d is not supported", mclk);
1115 return -EINVAL;
1118 ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
1119 if (!ep)
1120 return -ENXIO;
1122 ret = v4l2_fwnode_endpoint_alloc_parse(ep, &bus_cfg);
1123 fwnode_handle_put(ep);
1124 if (ret)
1125 return ret;
1127 if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV8856_DATA_LANES) {
1128 dev_err(dev, "number of CSI2 data lanes %d is not supported",
1129 bus_cfg.bus.mipi_csi2.num_data_lanes);
1130 ret = -EINVAL;
1131 goto check_hwcfg_error;
1134 if (!bus_cfg.nr_of_link_frequencies) {
1135 dev_err(dev, "no link frequencies defined");
1136 ret = -EINVAL;
1137 goto check_hwcfg_error;
1140 for (i = 0; i < ARRAY_SIZE(link_freq_menu_items); i++) {
1141 for (j = 0; j < bus_cfg.nr_of_link_frequencies; j++) {
1142 if (link_freq_menu_items[i] ==
1143 bus_cfg.link_frequencies[j])
1144 break;
1147 if (j == bus_cfg.nr_of_link_frequencies) {
1148 dev_err(dev, "no link frequency %lld supported",
1149 link_freq_menu_items[i]);
1150 ret = -EINVAL;
1151 goto check_hwcfg_error;
1155 check_hwcfg_error:
1156 v4l2_fwnode_endpoint_free(&bus_cfg);
1158 return ret;
1161 static int ov8856_remove(struct i2c_client *client)
1163 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1164 struct ov8856 *ov8856 = to_ov8856(sd);
1166 v4l2_async_unregister_subdev(sd);
1167 media_entity_cleanup(&sd->entity);
1168 v4l2_ctrl_handler_free(sd->ctrl_handler);
1169 pm_runtime_disable(&client->dev);
1170 mutex_destroy(&ov8856->mutex);
1172 return 0;
1175 static int ov8856_probe(struct i2c_client *client)
1177 struct ov8856 *ov8856;
1178 int ret;
1180 ret = ov8856_check_hwcfg(&client->dev);
1181 if (ret) {
1182 dev_err(&client->dev, "failed to check HW configuration: %d",
1183 ret);
1184 return ret;
1187 ov8856 = devm_kzalloc(&client->dev, sizeof(*ov8856), GFP_KERNEL);
1188 if (!ov8856)
1189 return -ENOMEM;
1191 v4l2_i2c_subdev_init(&ov8856->sd, client, &ov8856_subdev_ops);
1192 ret = ov8856_identify_module(ov8856);
1193 if (ret) {
1194 dev_err(&client->dev, "failed to find sensor: %d", ret);
1195 return ret;
1198 mutex_init(&ov8856->mutex);
1199 ov8856->cur_mode = &supported_modes[0];
1200 ret = ov8856_init_controls(ov8856);
1201 if (ret) {
1202 dev_err(&client->dev, "failed to init controls: %d", ret);
1203 goto probe_error_v4l2_ctrl_handler_free;
1206 ov8856->sd.internal_ops = &ov8856_internal_ops;
1207 ov8856->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1208 ov8856->sd.entity.ops = &ov8856_subdev_entity_ops;
1209 ov8856->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1210 ov8856->pad.flags = MEDIA_PAD_FL_SOURCE;
1211 ret = media_entity_pads_init(&ov8856->sd.entity, 1, &ov8856->pad);
1212 if (ret) {
1213 dev_err(&client->dev, "failed to init entity pads: %d", ret);
1214 goto probe_error_v4l2_ctrl_handler_free;
1217 ret = v4l2_async_register_subdev_sensor_common(&ov8856->sd);
1218 if (ret < 0) {
1219 dev_err(&client->dev, "failed to register V4L2 subdev: %d",
1220 ret);
1221 goto probe_error_media_entity_cleanup;
1225 * Device is already turned on by i2c-core with ACPI domain PM.
1226 * Enable runtime PM and turn off the device.
1228 pm_runtime_set_active(&client->dev);
1229 pm_runtime_enable(&client->dev);
1230 pm_runtime_idle(&client->dev);
1232 return 0;
1234 probe_error_media_entity_cleanup:
1235 media_entity_cleanup(&ov8856->sd.entity);
1237 probe_error_v4l2_ctrl_handler_free:
1238 v4l2_ctrl_handler_free(ov8856->sd.ctrl_handler);
1239 mutex_destroy(&ov8856->mutex);
1241 return ret;
1244 static const struct dev_pm_ops ov8856_pm_ops = {
1245 SET_SYSTEM_SLEEP_PM_OPS(ov8856_suspend, ov8856_resume)
1248 #ifdef CONFIG_ACPI
1249 static const struct acpi_device_id ov8856_acpi_ids[] = {
1250 {"OVTI8856"},
1254 MODULE_DEVICE_TABLE(acpi, ov8856_acpi_ids);
1255 #endif
1257 static struct i2c_driver ov8856_i2c_driver = {
1258 .driver = {
1259 .name = "ov8856",
1260 .pm = &ov8856_pm_ops,
1261 .acpi_match_table = ACPI_PTR(ov8856_acpi_ids),
1263 .probe_new = ov8856_probe,
1264 .remove = ov8856_remove,
1267 module_i2c_driver(ov8856_i2c_driver);
1269 MODULE_AUTHOR("Ben Kao <ben.kao@intel.com>");
1270 MODULE_DESCRIPTION("OmniVision OV8856 sensor driver");
1271 MODULE_LICENSE("GPL v2");