1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * gc2155 driver
4 *
5 * Copyright (C) 2018 Fuzhou Rockchip Electronics Co., Ltd.
6 *
7 * V0.0X01.0X01 add poweron function.
8 * V0.0X01.0X02 fix mclk issue when probe multiple camera.
9 * V0.0X01.0X03 add enum_frame_interval function.
10 * V0.0X01.0X04 add exposure control and fix v4l2_ctrl init issues.
11 */
12
13 #include <linux/clk.h>
14 #include <linux/device.h>
15 #include <linux/delay.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/i2c.h>
18 #include <linux/module.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/of.h>
21 #include <linux/of_graph.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/sysfs.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/version.h>
26 #include <media/v4l2-async.h>
27 #include <media/media-entity.h>
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-event.h>
32 #include <media/v4l2-fwnode.h>
33 #include <media/v4l2-image-sizes.h>
34 #include <media/v4l2-mediabus.h>
35 #include <media/v4l2-subdev.h>
36 #include <linux/rk-camera-module.h>
37
38 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x04)
39 #define GC2155_PIXEL_RATE (120 * 1000 * 1000)
40 #define GC2155_EXPOSURE_CONTROL
41
42 #define REG_CHIP_ID_H 0xf0
43 #define REG_CHIP_ID_L 0xf1
44 #define CHIP_ID_H 0x21
45 #define CHIP_ID_L 0x55
46
47 #define REG_NULL 0xFF
48
49 #define GC2155_XVCLK_FREQ 24000000
50
51 #define GC2155_NAME "gc2155"
52
53 static const char * const gc2155_supply_names[] = {
54 "avdd",
55 "dovdd",
56 "dvdd",
57 };
58
59 #define GC2155_NUM_SUPPLIES ARRAY_SIZE(gc2155_supply_names)
60
61 struct regval {
62 u8 addr;
63 u8 val;
64 };
65
66 struct gc2155_mode {
67 u32 width;
68 u32 height;
69 unsigned int fps;
70 struct v4l2_fract max_fps;
71 const struct regval *reg_list;
72 };
73
74 struct gc2155 {
75 struct i2c_client *client;
76 struct clk *xvclk;
77 struct gpio_desc *reset_gpio;
78 struct gpio_desc *pwdn_gpio;
79 struct gpio_desc *power_gpio;
80 struct regulator_bulk_data supplies[GC2155_NUM_SUPPLIES];
81
82 bool streaming;
83 bool power_on;
84 struct mutex mutex; /* lock to serialize v4l2 callback */
85 struct v4l2_subdev subdev;
86 struct media_pad pad;
87 struct v4l2_mbus_framefmt format;
88 unsigned int fps;
89 struct v4l2_ctrl_handler ctrls;
90 struct v4l2_ctrl *pixel_rate;
91
92 const struct gc2155_mode *cur_mode;
93 const struct gc2155_mode *framesize_cfg;
94 unsigned int cfg_num;
95 u32 module_index;
96 const char *module_facing;
97 const char *module_name;
98 const char *len_name;
99 };
100
101 #define to_gc2155(sd) container_of(sd, struct gc2155, subdev)
102
103 static struct regval gc2155_global_regs[] = {
104 {0xfe, 0xf0},
105 {0xfe, 0xf0},
106 {0xfe, 0xf0},
107 {0xfc, 0x06},
108 {0xf6, 0x00},
109 {0xf7, 0x1d},
110 {0xf8, 0x84},
111 {0xfa, 0x00},
112 {0xf9, 0xfe},
113 {0xf2, 0x00},
114 /* ISP reg */
115 {0xfe, 0x00},
116 {0x03, 0x04},
117 {0x04, 0xe2},
118 {0x09, 0x00},
119 {0x0a, 0x00},
120 {0x0b, 0x00},
121 {0x0c, 0x00},
122 {0x0d, 0x04},
123 {0x0e, 0xc0},
124 {0x0f, 0x06},
125 {0x10, 0x50},
126 {0x12, 0x2e},
127 {0x17, 0x14}, // mirror
128 {0x18, 0x02},
129 {0x19, 0x0e},
130 {0x1a, 0x01},
131 {0x1b, 0x4b},
132 {0x1c, 0x07},
133 {0x1d, 0x10},
134 {0x1e, 0x98},
135 {0x1f, 0x78},
136 {0x20, 0x05},
137 {0x21, 0x40},
138 {0x22, 0xf0},
139 {0x24, 0x16},
140 {0x25, 0x01},
141 {0x26, 0x10},
142 {0x2d, 0x40},
143 {0x30, 0x01},
144 {0x31, 0x90},
145 {0x33, 0x04},
146 {0x34, 0x01},
147 /* ISP reg */
148 {0xfe, 0x00},
149 {0x80, 0xff},
150 {0x81, 0x2c},
151 {0x82, 0xfa},
152 {0x83, 0x00},
153 {0x84, 0x00}, //yuv 01
154 {0x85, 0x08},
155 {0x86, 0x02},
156 {0x89, 0x03},
157 {0x8a, 0x00},
158 {0x8b, 0x00},
159 {0xb0, 0x55},
160 {0xc3, 0x11}, //00
161 {0xc4, 0x20},
162 {0xc5, 0x30},
163 {0xc6, 0x38},
164 {0xc7, 0x40},
165 {0xec, 0x02},
166 {0xed, 0x04},
167 {0xee, 0x60},
168 {0xef, 0x90},
169 {0xb6, 0x01},
170 {0x90, 0x01},
171 {0x91, 0x00},
172 {0x92, 0x00},
173 {0x93, 0x00},
174 {0x94, 0x00},
175 {0x95, 0x04},
176 {0x96, 0xb0},
177 {0x97, 0x06},
178 {0x98, 0x40},
179 /* BLK */
180 {0xfe, 0x00},
181 {0x18, 0x02},
182 {0x40, 0x42},
183 {0x41, 0x00},
184 {0x43, 0x5b}, //0x54
185 {0x5e, 0x00},
186 {0x5f, 0x00},
187 {0x60, 0x00},
188 {0x61, 0x00},
189 {0x62, 0x00},
190 {0x63, 0x00},
191 {0x64, 0x00},
192 {0x65, 0x00},
193 {0x66, 0x20},
194 {0x67, 0x20},
195 {0x68, 0x20},
196 {0x69, 0x20},
197 {0x6a, 0x08},
198 {0x6b, 0x08},
199 {0x6c, 0x08},
200 {0x6d, 0x08},
201 {0x6e, 0x08},
202 {0x6f, 0x08},
203 {0x70, 0x08},
204 {0x71, 0x08},
205 {0x72, 0xf0},
206 {0x7e, 0x3c},
207 {0x7f, 0x00},
208 {0xfe, 0x00},
209 /* AEC */
210 {0xfe, 0x01},
211 {0x01, 0x08},
212 {0x02, 0xc0},
213 {0x03, 0x04},
214 {0x04, 0x90},
215 {0x05, 0x30},
216 {0x06, 0x98},
217 {0x07, 0x28},
218 {0x08, 0x6c},
219 {0x09, 0x00},
220 {0x0a, 0xc2},
221 {0x0b, 0x11},
222 {0x0c, 0x10},
223 {0x13, 0x2d},
224 {0x17, 0x00},
225 {0x1c, 0x11},
226 {0x1e, 0x61},
227 {0x1f, 0x30},
228 {0x20, 0x40},
229 {0x22, 0x80},
230 {0x23, 0x20},
231
232 {0x12, 0x35},
233 {0x15, 0x50},
234 {0x10, 0x31},
235 {0x3e, 0x28},
236 {0x3f, 0xe0},
237 {0x40, 0xe0},
238 {0x41, 0x08},
239
240 {0xfe, 0x02},
241 {0x0f, 0x05},
242 /* INTPEE */
243 {0xfe, 0x02},
244 {0x90, 0x6c},
245 {0x91, 0x03},
246 {0x92, 0xc4},
247 {0x97, 0x64},
248 {0x98, 0x88},
249 {0x9d, 0x08},
250 {0xa2, 0x11},
251 {0xfe, 0x00},
252 /* DNDD */
253 {0xfe, 0x02},
254 {0x80, 0xc1},
255 {0x81, 0x08},
256 {0x82, 0x05},
257 {0x83, 0x04},
258 {0x84, 0x0a},
259 {0x86, 0x80},
260 {0x87, 0x30},
261 {0x88, 0x15},
262 {0x89, 0x80},
263 {0x8a, 0x60},
264 {0x8b, 0x30},
265 /* ASDE */
266 {0xfe, 0x01},
267 {0x21, 0x14},
268 {0xfe, 0x02},
269 {0x3c, 0x06},
270 {0x3d, 0x40},
271 {0x48, 0x30},
272 {0x49, 0x06},
273 {0x4b, 0x08},
274 {0x4c, 0x20},
275 {0xa3, 0x50},
276 {0xa4, 0x30},
277 {0xa5, 0x40},
278 {0xa6, 0x80},
279 {0xab, 0x40},
280 {0xae, 0x0c},
281 {0xb3, 0x42},
282 {0xb4, 0x24},
283 {0xb6, 0x50},
284 {0xb7, 0x01},
285 {0xb9, 0x28},
286 {0xfe, 0x00},
287 /* gamma1 */
288 {0xfe, 0x02},
289 {0x10, 0x0d},
290 {0x11, 0x12},
291 {0x12, 0x17},
292 {0x13, 0x1c},
293 {0x14, 0x27},
294 {0x15, 0x34},
295 {0x16, 0x44},
296 {0x17, 0x55},
297 {0x18, 0x6e},
298 {0x19, 0x81},
299 {0x1a, 0x91},
300 {0x1b, 0x9c},
301 {0x1c, 0xaa},
302 {0x1d, 0xbb},
303 {0x1e, 0xca},
304 {0x1f, 0xd5},
305 {0x20, 0xe0},
306 {0x21, 0xe7},
307 {0x22, 0xed},
308 {0x23, 0xf6},
309 {0x24, 0xfb},
310 {0x25, 0xff},
311 /* gamma2 */
312 {0xfe, 0x02},
313 {0x26, 0x0d},
314 {0x27, 0x12},
315 {0x28, 0x17},
316 {0x29, 0x1c},
317 {0x2a, 0x27},
318 {0x2b, 0x34},
319 {0x2c, 0x44},
320 {0x2d, 0x55},
321 {0x2e, 0x6e},
322 {0x2f, 0x81},
323 {0x30, 0x91},
324 {0x31, 0x9c},
325 {0x32, 0xaa},
326 {0x33, 0xbb},
327 {0x34, 0xca},
328 {0x35, 0xd5},
329 {0x36, 0xe0},
330 {0x37, 0xe7},
331 {0x38, 0xed},
332 {0x39, 0xf6},
333 {0x3a, 0xfb},
334 {0x3b, 0xff},
335 /* YCP */
336 {0xfe, 0x02},
337 {0xd1, 0x28},
338 {0xd2, 0x28},
339 {0xdd, 0x14},
340 {0xde, 0x88},
341 {0xed, 0x80},
342 /* LSC */
343 {0xfe, 0x01},
344 {0xc2, 0x1f},
345 {0xc3, 0x13},
346 {0xc4, 0x0e},
347 {0xc8, 0x16},
348 {0xc9, 0x0f},
349 {0xca, 0x0c},
350 {0xbc, 0x52},
351 {0xbd, 0x2c},
352 {0xbe, 0x27},
353 {0xb6, 0x47},
354 {0xb7, 0x32},
355 {0xb8, 0x30},
356 {0xc5, 0x00},
357 {0xc6, 0x00},
358 {0xc7, 0x00},
359 {0xcb, 0x00},
360 {0xcc, 0x00},
361 {0xcd, 0x00},
362 {0xbf, 0x0e},
363 {0xc0, 0x00},
364 {0xc1, 0x00},
365 {0xb9, 0x08},
366 {0xba, 0x00},
367 {0xbb, 0x00},
368 {0xaa, 0x0a},
369 {0xab, 0x0c},
370 {0xac, 0x0d},
371 {0xad, 0x02},
372 {0xae, 0x06},
373 {0xaf, 0x05},
374 {0xb0, 0x00},
375 {0xb1, 0x05},
376 {0xb2, 0x02},
377 {0xb3, 0x04},
378 {0xb4, 0x04},
379 {0xb5, 0x05},
380 {0xd0, 0x00},
381 {0xd1, 0x00},
382 {0xd2, 0x00},
383 {0xd6, 0x02},
384 {0xd7, 0x00},
385 {0xd8, 0x00},
386 {0xd9, 0x00},
387 {0xda, 0x00},
388 {0xdb, 0x00},
389 {0xd3, 0x00},
390 {0xd4, 0x00},
391 {0xd5, 0x00},
392 {0xa4, 0x04},
393 {0xa5, 0x00},
394 {0xa6, 0x77},
395 {0xa7, 0x77},
396 {0xa8, 0x77},
397 {0xa9, 0x77},
398 {0xa1, 0x80},
399 {0xa2, 0x80},
400
401 {0xfe, 0x01},
402 {0xdc, 0x35},
403 {0xdd, 0x28},
404 {0xdf, 0x0d},
405 {0xe0, 0x70},
406 {0xe1, 0x78},
407 {0xe2, 0x70},
408 {0xe3, 0x78},
409 {0xe6, 0x90},
410 {0xe7, 0x70},
411 {0xe8, 0x90},
412 {0xe9, 0x70},
413 {0xfe, 0x00},
414 /* AWB */
415 {0xfe, 0x01},
416 {0x4f, 0x00},
417 {0x4f, 0x00},
418 {0x4b, 0x01},
419 {0x4f, 0x00},
420 {0x4c, 0x01},
421 {0x4d, 0x71},
422 {0x4e, 0x02},
423 {0x4c, 0x01},
424 {0x4d, 0x91},
425 {0x4e, 0x02},
426 {0x4c, 0x01},
427 {0x4d, 0x50},
428 {0x4e, 0x02},
429 {0x4c, 0x01},
430 {0x4d, 0x70},
431 {0x4e, 0x02},
432 {0x4c, 0x01},
433 {0x4d, 0x90},
434 {0x4e, 0x02},
435 {0x4c, 0x01},
436 {0x4d, 0xb0},
437 {0x4e, 0x02},
438 {0x4c, 0x01},
439 {0x4d, 0xd0},
440 {0x4e, 0x02},
441 {0x4c, 0x01},
442 {0x4d, 0x4f},
443 {0x4e, 0x02},
444 {0x4c, 0x01},
445 {0x4d, 0x6f},
446 {0x4e, 0x02},
447 {0x4c, 0x01},
448 {0x4d, 0x8f},
449 {0x4e, 0x02},
450 {0x4c, 0x01},
451 {0x4d, 0xaf},
452 {0x4e, 0x02},
453 {0x4c, 0x01},
454 {0x4d, 0xcf},
455 {0x4e, 0x02},
456 {0x4c, 0x01},
457 {0x4d, 0x6e},
458 {0x4e, 0x03},
459 {0x4c, 0x01},
460 {0x4d, 0x8e},
461 {0x4e, 0x03},
462 {0x4c, 0x01},
463 {0x4d, 0xae},
464 {0x4e, 0x03},
465 {0x4c, 0x01},
466 {0x4d, 0xce},
467 {0x4e, 0x03},
468 {0x4c, 0x01},
469 {0x4d, 0x4d},
470 {0x4e, 0x03},
471 {0x4c, 0x01},
472 {0x4d, 0x6d},
473 {0x4e, 0x03},
474 {0x4c, 0x01},
475 {0x4d, 0x8d},
476 {0x4e, 0x03},
477 {0x4c, 0x01},
478 {0x4d, 0xad},
479 {0x4e, 0x03},
480 {0x4c, 0x01},
481 {0x4d, 0xcd},
482 {0x4e, 0x03},
483 {0x4c, 0x01},
484 {0x4d, 0x4c},
485 {0x4e, 0x03},
486 {0x4c, 0x01},
487 {0x4d, 0x6c},
488 {0x4e, 0x03},
489 {0x4c, 0x01},
490 {0x4d, 0x8c},
491 {0x4e, 0x03},
492 {0x4c, 0x01},
493 {0x4d, 0xac},
494 {0x4e, 0x03},
495 {0x4c, 0x01},
496 {0x4d, 0xcc},
497 {0x4e, 0x03},
498 {0x4c, 0x01},
499 {0x4d, 0xec},
500 {0x4e, 0x03},
501 {0x4c, 0x01},
502 {0x4d, 0x4b},
503 {0x4e, 0x03},
504 {0x4c, 0x01},
505 {0x4d, 0x6b},
506 {0x4e, 0x03},
507 {0x4c, 0x01},
508 {0x4d, 0x8b},
509 {0x4e, 0x03},
510 {0x4c, 0x01},
511 {0x4d, 0xab},
512 {0x4e, 0x03},
513 {0x4c, 0x01},
514 {0x4d, 0x8a},
515 {0x4e, 0x04},
516 {0x4c, 0x01},
517 {0x4d, 0xaa},
518 {0x4e, 0x04},
519 {0x4c, 0x01},
520 {0x4d, 0xca},
521 {0x4e, 0x04},
522 {0x4c, 0x01},
523 {0x4d, 0xa9},
524 {0x4e, 0x04},
525 {0x4c, 0x01},
526 {0x4d, 0xc9},
527 {0x4e, 0x04},
528 {0x4c, 0x01},
529 {0x4d, 0xcb},
530 {0x4e, 0x05},
531 {0x4c, 0x01},
532 {0x4d, 0xeb},
533 {0x4e, 0x05},
534 {0x4c, 0x02},
535 {0x4d, 0x0b},
536 {0x4e, 0x05},
537 {0x4c, 0x02},
538 {0x4d, 0x2b},
539 {0x4e, 0x05},
540 {0x4c, 0x02},
541 {0x4d, 0x4b},
542 {0x4e, 0x05},
543 {0x4c, 0x01},
544 {0x4d, 0xea},
545 {0x4e, 0x05},
546 {0x4c, 0x02},
547 {0x4d, 0x0a},
548 {0x4e, 0x05},
549 {0x4c, 0x02},
550 {0x4d, 0x2a},
551 {0x4e, 0x05},
552 {0x4c, 0x02},
553 {0x4d, 0x6a},
554 {0x4e, 0x06},
555 {0x4c, 0x02},
556 {0x4d, 0x29},
557 {0x4e, 0x06},
558 {0x4c, 0x02},
559 {0x4d, 0x49},
560 {0x4e, 0x06},
561 {0x4c, 0x02},
562 {0x4d, 0x69},
563 {0x4e, 0x06},
564 {0x4c, 0x02},
565 {0x4d, 0x89},
566 {0x4e, 0x06},
567 {0x4c, 0x02},
568 {0x4d, 0xa9},
569 {0x4e, 0x06},
570 {0x4c, 0x02},
571 {0x4d, 0xc9},
572 {0x4e, 0x06},
573 {0x4c, 0x02},
574 {0x4d, 0x48},
575 {0x4e, 0x06},
576 {0x4c, 0x02},
577 {0x4d, 0x68},
578 {0x4e, 0x06},
579 {0x4c, 0x03},
580 {0x4d, 0x09},
581 {0x4e, 0x07},
582 {0x4c, 0x02},
583 {0x4d, 0xa8},
584 {0x4e, 0x07},
585 {0x4c, 0x02},
586 {0x4d, 0xc8},
587 {0x4e, 0x07},
588 {0x4c, 0x02},
589 {0x4d, 0xe8},
590 {0x4e, 0x07},
591 {0x4c, 0x03},
592 {0x4d, 0x08},
593 {0x4e, 0x07},
594 {0x4c, 0x03},
595 {0x4d, 0x28},
596 {0x4e, 0x07},
597 {0x4c, 0x02},
598 {0x4d, 0x87},
599 {0x4e, 0x07},
600 {0x4c, 0x02},
601 {0x4d, 0xa7},
602 {0x4e, 0x07},
603 {0x4c, 0x02},
604 {0x4d, 0xc7},
605 {0x4e, 0x07},
606 {0x4c, 0x02},
607 {0x4d, 0xe7},
608 {0x4e, 0x07},
609 {0x4c, 0x03},
610 {0x4d, 0x07},
611 {0x4e, 0x07},
612 {0x4f, 0x01},
613 {0xfe, 0x01},
614
615 {0x50, 0x80},
616 {0x51, 0xa8},
617 {0x52, 0x57},
618 {0x53, 0x38},
619 {0x54, 0xc7},
620 {0x56, 0x0e},
621 {0x58, 0x08},
622 {0x5b, 0x00},
623 {0x5c, 0x74},
624 {0x5d, 0x8b},
625 {0x61, 0xd3},
626 {0x62, 0x90},
627 {0x63, 0xaa},
628 {0x65, 0x04},
629 {0x67, 0xb2},
630 {0x68, 0xac},
631 {0x69, 0x00},
632 {0x6a, 0xb2},
633 {0x6b, 0xac},
634 {0x6c, 0xdc},
635 {0x6d, 0xb0},
636 {0x6e, 0x30},
637 {0x6f, 0x40},
638 {0x70, 0x05},
639 {0x71, 0x80},
640 {0x72, 0x80},
641 {0x73, 0x30},
642 {0x74, 0x01},
643 {0x75, 0x01},
644 {0x7f, 0x08},
645 {0x76, 0x70},
646 {0x77, 0x48},
647 {0x78, 0xa0},
648 {0xfe, 0x00},
649 /* CC */
650 {0xfe, 0x02},
651 {0xc0, 0x01},
652 {0xc1, 0x4a},
653 {0xc2, 0xf3},
654 {0xc3, 0xfc},
655 {0xc4, 0xe4},
656 {0xc5, 0x48},
657 {0xc6, 0xec},
658 {0xc7, 0x45},
659 {0xc8, 0xf8},
660 {0xc9, 0x02},
661 {0xca, 0xfe},
662 {0xcb, 0x42},
663 {0xcc, 0x00},
664 {0xcd, 0x45},
665 {0xce, 0xf0},
666 {0xcf, 0x00},
667 {0xe3, 0xf0},
668 {0xe4, 0x45},
669 {0xe5, 0xe8},
670 /* ABS */
671 {0xfe, 0x01},
672 {0x9f, 0x42},
673 {0xfe, 0x00},
674 /* frame rate 50Hz */
675 {0xfe, 0x00},
676 {0x05, 0x02},
677 {0x06, 0x20},
678 {0x07, 0x00},
679 {0x08, 0x50},
680 {0xfe, 0x01},
681 {0x25, 0x00},
682 {0x26, 0xfa},
683
684 {0x27, 0x04},
685 {0x28, 0xe2},
686 {0x29, 0x04},
687 {0x2a, 0xe2},
688 {0x2b, 0x04},
689 {0x2c, 0xe2},
690 {0x2d, 0x04},
691 {0x2e, 0xe2},
692
693 /* SVGA */
694 {0xfe, 0x00},
695 {0xfa, 0x00},
696 {0xfd, 0x01},
697 /* crop window */
698 {0xfe, 0x00},
699 {0x90, 0x01},
700 {0x91, 0x00},
701 {0x92, 0x00},
702 {0x93, 0x00},
703 {0x94, 0x00},
704 {0x95, 0x02},
705 {0x96, 0x58},
706 {0x97, 0x03},
707 {0x98, 0x20},
708 {0x99, 0x11},
709 {0x9a, 0x06},
710 /* AWB */
711 {0xfe, 0x00},
712 {0xec, 0x01},
713 {0xed, 0x02},
714 {0xee, 0x30},
715 {0xef, 0x48},
716 {0xfe, 0x01},
717 {0x74, 0x00},
718 /* AEC */
719 {0xfe, 0x01},
720 {0x01, 0x04},
721 {0x02, 0x60},
722 {0x03, 0x02},
723 {0x04, 0x48},
724 {0x05, 0x18},
725 {0x06, 0x4c},
726 {0x07, 0x14},
727 {0x08, 0x36},
728 {0x0a, 0xc0},
729 {0x21, 0x14},
730 {0xfe, 0x00},
731 /* gamma */
732 {0xfe, 0x00},
733 {0xc3, 0x11},
734 {0xc4, 0x20},
735 {0xc5, 0x30},
736 {0xfe, 0x00},
737 /* OUTPUT */
738 {0xfe, 0x00},
739 {0xf2, 0x0f},
740 {REG_NULL, 0x0},
741 };
742
743 static struct regval gc2155_800x600_15fps[] = {
744 //SENSORDB("GC2155_Sensor_SVGA"},
745 {0xfe, 0x00},
746 {0xb6, 0x01},
747 {0xfd, 0x01},
748 {0xfa, 0x00},
749
750 /*crop window*/
751 {0xfe, 0x00},
752 {0x90, 0x01},
753 {0x91, 0x00},
754 {0x92, 0x00},
755 {0x93, 0x00},
756 {0x94, 0x00},
757 {0x95, 0x02},
758 {0x96, 0x58},
759 {0x97, 0x03},
760 {0x98, 0x20},
761 {0x99, 0x11},
762 {0x9a, 0x06},
763 /*AWB*/
764 {0xfe, 0x00},
765 {0xec, 0x01},
766 {0xed, 0x02},
767 {0xee, 0x30},
768 {0xef, 0x48},
769 {0xfe, 0x01},
770 {0x74, 0x00},
771 /* AEC */
772 {0xfe, 0x01},
773 {0x01, 0x04},
774 {0x02, 0x60},
775 {0x03, 0x02},
776 {0x04, 0x48},
777 {0x05, 0x18},
778 {0x06, 0x4c},
779 {0x07, 0x14},
780 {0x08, 0x36},
781 {0x0a, 0xc0},
782 {0x21, 0x14},
783 {0xfe, 0x00},
784 /* gamma */
785 {0xfe, 0x00},
786 {0xc3, 0x11},
787 {0xc4, 0x20},
788 {0xc5, 0x30},
789 {0xfe, 0x00},
790
791 {REG_NULL, 0x0},
792 };
793
794 static struct regval gc2155_1600x1200_7fps[] = {
795 //SENSORDB("GC2155_Sensor_2M"},
796 {0xfe, 0x00},
797 {0xb6, 0x00},
798 {0xfa, 0x11},
799 {0xfd, 0x00},
800 /* crop window */
801 {0xfe, 0x00},
802 {0x90, 0x01},
803 {0x91, 0x00},
804 {0x92, 0x00},
805 {0x93, 0x00},
806 {0x94, 0x00},
807 {0x95, 0x04},
808 {0x96, 0xb0},
809 {0x97, 0x06},
810 {0x98, 0x40},
811 {0x99, 0x11},
812 {0x9a, 0x06},
813 /*AWB*/
814 {0xfe, 0x00},
815 {0xec, 0x02},
816 {0xed, 0x04},
817 {0xee, 0x60},
818 {0xef, 0x90},
819 {0xfe, 0x01},
820 {0x74, 0x01},
821 /* AEC */
822 {0xfe, 0x01},
823 {0x01, 0x08},
824 {0x02, 0xc0},
825 {0x03, 0x04},
826 {0x04, 0x90},
827 {0x05, 0x30},
828 {0x06, 0x98},
829 {0x07, 0x28},
830 {0x08, 0x6c},
831 {0x0a, 0xc2},
832 {0x21, 0x15}, //if 0xfa=11,then 0x21=15;else if 0xfa=00,then 0x21=14
833 {0xfe, 0x00},
834 /* gamma */
835 {0xfe, 0x00},
836 {0xc3, 0x00}, //if shutter/2 when capture,then exp_gamma_th/2
837 {0xc4, 0x90},
838 {0xc5, 0x98},
839 {0xfe, 0x00},
840
841 {REG_NULL, 0x0},
842 };
843
844 static const struct gc2155_mode supported_modes[] = {
845 {
846 .width = 800,
847 .height = 600,
848 .fps = 15,
849 .max_fps = {
850 .numerator = 10000,
851 .denominator = 150000,
852 },
853 .reg_list = gc2155_800x600_15fps,
854 },
855 {
856 .width = 1600,
857 .height = 1200,
858 .fps = 7,
859 .max_fps = {
860 .numerator = 10000,
861 .denominator = 70000,
862 },
863 .reg_list = gc2155_1600x1200_7fps,
864 },
865 };
866
gc2155_write_reg(struct i2c_client * client,u8 reg,u8 val)867 static int gc2155_write_reg(struct i2c_client *client, u8 reg, u8 val)
868 {
869 int ret;
870
871 ret = i2c_smbus_write_byte_data(client, reg, val);
872 if (ret < 0)
873 dev_err(&client->dev, "write reg error: %d\n", ret);
874
875 return ret;
876 }
877
gc2155_write_array(struct i2c_client * client,const struct regval * regs)878 static int gc2155_write_array(struct i2c_client *client,
879 const struct regval *regs)
880 {
881 int i, ret = 0;
882
883 for (i = 0; ret == 0 && regs[i].addr != REG_NULL; i++)
884 ret = gc2155_write_reg(client, regs[i].addr, regs[i].val);
885
886 return ret;
887 }
888
gc2155_read_reg(struct i2c_client * client,u8 reg)889 static inline u8 gc2155_read_reg(struct i2c_client *client, u8 reg)
890 {
891 return i2c_smbus_read_byte_data(client, reg);
892 }
893
gc2155_get_reso_dist(const struct gc2155_mode * mode,struct v4l2_mbus_framefmt * framefmt)894 static int gc2155_get_reso_dist(const struct gc2155_mode *mode,
895 struct v4l2_mbus_framefmt *framefmt)
896 {
897 return abs(mode->width - framefmt->width) +
898 abs(mode->height - framefmt->height);
899 }
900
901 static const struct gc2155_mode *
gc2155_find_best_fit(struct v4l2_subdev_format * fmt)902 gc2155_find_best_fit(struct v4l2_subdev_format *fmt)
903 {
904 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
905 int dist;
906 int cur_best_fit = 0;
907 int cur_best_fit_dist = -1;
908 int i;
909
910 for (i = 0; i < ARRAY_SIZE(supported_modes); i++) {
911 dist = gc2155_get_reso_dist(&supported_modes[i], framefmt);
912 if (cur_best_fit_dist == -1 || dist < cur_best_fit_dist) {
913 cur_best_fit_dist = dist;
914 cur_best_fit = i;
915 }
916 }
917
918 return &supported_modes[cur_best_fit];
919 }
920
921 #ifdef GC2155_EXPOSURE_CONTROL
922 /*
923 * the function is called before sensor register setting in VIDIOC_S_FMT
924 */
925 /* Row times = Hb + Sh_delay + win_width + 4*/
926
gc2155_aec_ctrl(struct v4l2_subdev * sd,struct v4l2_mbus_framefmt * mf)927 static int gc2155_aec_ctrl(struct v4l2_subdev *sd,
928 struct v4l2_mbus_framefmt *mf)
929 {
930 struct i2c_client *client = v4l2_get_subdevdata(sd);
931 int ret = 0;
932 u8 value;
933 static unsigned int capture_fps = 75, capture_lines = 1266;
934 static unsigned int preview_fps = 150, preview_lines = 1266;
935 static unsigned int lines_10ms = 1;
936 static unsigned int shutter_h = 0x04, shutter_l = 0xe2;
937 static unsigned int cap = 0, shutter = 0x04e2;
938
939 dev_info(&client->dev, "%s enter\n", __func__);
940 if ((mf->width == 800 && mf->height == 600) && cap == 1) {
941 cap = 0;
942 ret = gc2155_write_reg(client, 0xfe, 0x00);
943 ret |= gc2155_write_reg(client, 0xb6, 0x00);
944 ret |= gc2155_write_reg(client, 0x03, shutter_h);
945 ret |= gc2155_write_reg(client, 0x04, shutter_l);
946 ret |= gc2155_write_reg(client, 0x82, 0xfa);
947 ret |= gc2155_write_reg(client, 0xb6, 0x01);
948 if (ret)
949 dev_err(&client->dev, "gc2155 reconfig failed!\n");
950 }
951 if (mf->width == 1600 && mf->height == 1200) {
952 cap = 1;
953 ret = gc2155_write_reg(client, 0xfe, 0x00);
954 ret |= gc2155_write_reg(client, 0xb6, 0x00);
955 ret |= gc2155_write_reg(client, 0x82, 0xf8);
956
957 /*shutter calculate*/
958 value = gc2155_read_reg(client, 0x03);
959 shutter_h = value;
960 shutter = (value << 8);
961 value = gc2155_read_reg(client, 0x04);
962 shutter_l = value;
963 shutter |= (value & 0xff);
964 dev_info(&client->dev, "%s(%d) 800x600 shutter read(0x%04x)!\n",
965 __func__, __LINE__, shutter);
966 shutter = shutter * capture_lines / preview_lines;
967 shutter = shutter * capture_fps / preview_fps;
968 lines_10ms = capture_fps * capture_lines / 100 / 10;
969 if (shutter > lines_10ms) {
970 shutter = shutter + lines_10ms / 2;
971 shutter /= lines_10ms;
972 shutter *= lines_10ms;
973 }
974 if (shutter < 1)
975 shutter = 0x276;
976 dev_info(&client->dev, "%s(%d)lines_10ms(%d),cal(0x%08x)!\n",
977 __func__, __LINE__, lines_10ms, shutter);
978
979 ret |= gc2155_write_reg(client, 0x03, ((shutter >> 8) & 0x1f));
980 ret |= gc2155_write_reg(client, 0x04, (shutter & 0xff));
981 if (ret)
982 dev_err(&client->dev, "full exp reconfig failed!\n");
983 }
984 return ret;
985 }
986 #endif
987
gc2155_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)988 static int gc2155_set_fmt(struct v4l2_subdev *sd,
989 struct v4l2_subdev_pad_config *cfg,
990 struct v4l2_subdev_format *fmt)
991 {
992 struct gc2155 *gc2155 = to_gc2155(sd);
993 const struct gc2155_mode *mode;
994
995 mutex_lock(&gc2155->mutex);
996
997 mode = gc2155_find_best_fit(fmt);
998 fmt->format.code = MEDIA_BUS_FMT_UYVY8_2X8;
999 fmt->format.width = mode->width;
1000 fmt->format.height = mode->height;
1001 fmt->format.field = V4L2_FIELD_NONE;
1002 fmt->format.colorspace = V4L2_COLORSPACE_SRGB;
1003 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1004 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1005 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
1006 #else
1007 mutex_unlock(&gc2155->mutex);
1008 return -ENOTTY;
1009 #endif
1010 } else {
1011 gc2155->cur_mode = mode;
1012 gc2155->format = fmt->format;
1013 }
1014
1015 #ifdef GC2155_EXPOSURE_CONTROL
1016 if (gc2155->power_on)
1017 gc2155_aec_ctrl(sd, &fmt->format);
1018 #endif
1019 mutex_unlock(&gc2155->mutex);
1020
1021 return 0;
1022 }
1023
gc2155_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)1024 static int gc2155_get_fmt(struct v4l2_subdev *sd,
1025 struct v4l2_subdev_pad_config *cfg,
1026 struct v4l2_subdev_format *fmt)
1027 {
1028 struct gc2155 *gc2155 = to_gc2155(sd);
1029 const struct gc2155_mode *mode = gc2155->cur_mode;
1030
1031 mutex_lock(&gc2155->mutex);
1032 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1033 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1034 fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
1035 #else
1036 mutex_unlock(&gc2155->mutex);
1037 return -ENOTTY;
1038 #endif
1039 } else {
1040 fmt->format.width = mode->width;
1041 fmt->format.height = mode->height;
1042 fmt->format.code = MEDIA_BUS_FMT_UYVY8_2X8;
1043 fmt->format.field = V4L2_FIELD_NONE;
1044 fmt->format.colorspace = V4L2_COLORSPACE_SRGB;
1045 }
1046 mutex_unlock(&gc2155->mutex);
1047
1048 return 0;
1049 }
1050
gc2155_get_default_format(struct gc2155 * gc2155,struct v4l2_mbus_framefmt * format)1051 static void gc2155_get_default_format(struct gc2155 *gc2155,
1052 struct v4l2_mbus_framefmt *format)
1053 {
1054 format->width = gc2155->cur_mode->width;
1055 format->height = gc2155->cur_mode->height;
1056 format->colorspace = V4L2_COLORSPACE_SRGB;
1057 format->code = MEDIA_BUS_FMT_UYVY8_2X8;
1058 format->field = V4L2_FIELD_NONE;
1059 }
1060
gc2155_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)1061 static int gc2155_enum_mbus_code(struct v4l2_subdev *sd,
1062 struct v4l2_subdev_pad_config *cfg,
1063 struct v4l2_subdev_mbus_code_enum *code)
1064 {
1065 if (code->index >= ARRAY_SIZE(supported_modes))
1066 return -EINVAL;
1067
1068 code->code = MEDIA_BUS_FMT_UYVY8_2X8;
1069
1070 return 0;
1071 }
1072
gc2155_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)1073 static int gc2155_enum_frame_sizes(struct v4l2_subdev *sd,
1074 struct v4l2_subdev_pad_config *cfg,
1075 struct v4l2_subdev_frame_size_enum *fse)
1076 {
1077 u32 index = fse->index;
1078
1079 if (index >= ARRAY_SIZE(supported_modes))
1080 return -EINVAL;
1081
1082 fse->code = MEDIA_BUS_FMT_UYVY8_2X8;
1083
1084 fse->min_width = supported_modes[index].width;
1085 fse->max_width = supported_modes[index].width;
1086 fse->max_height = supported_modes[index].height;
1087 fse->min_height = supported_modes[index].height;
1088
1089 return 0;
1090 }
1091
__gc2155_power_on(struct gc2155 * gc2155)1092 static int __gc2155_power_on(struct gc2155 *gc2155)
1093 {
1094 int ret;
1095 struct device *dev = &gc2155->client->dev;
1096
1097 dev_info(dev, "%s(%d)\n", __func__, __LINE__);
1098 if (!IS_ERR(gc2155->power_gpio)) {
1099 gpiod_set_value_cansleep(gc2155->power_gpio, 1);
1100 usleep_range(2000, 5000);
1101 }
1102
1103 if (!IS_ERR(gc2155->reset_gpio)) {
1104 gpiod_set_value_cansleep(gc2155->reset_gpio, 0);
1105 usleep_range(2000, 5000);
1106 }
1107 ret = regulator_bulk_enable(GC2155_NUM_SUPPLIES, gc2155->supplies);
1108 if (ret < 0) {
1109 dev_err(dev, "Failed to enable regulators\n");
1110 return ret;
1111 }
1112
1113 ret = clk_set_rate(gc2155->xvclk, GC2155_XVCLK_FREQ);
1114 if (ret < 0)
1115 dev_warn(dev, "Failed to set xvclk rate (24MHz)\n");
1116 if (clk_get_rate(gc2155->xvclk) != GC2155_XVCLK_FREQ)
1117 dev_warn(dev, "xvclk mismatched, modes are based on 24MHz\n");
1118 ret = clk_prepare_enable(gc2155->xvclk);
1119 if (ret < 0) {
1120 dev_err(dev, "Failed to enable xvclk\n");
1121 return ret;
1122 }
1123
1124 if (!IS_ERR(gc2155->pwdn_gpio)) {
1125 gpiod_set_value_cansleep(gc2155->pwdn_gpio, 0);
1126 usleep_range(2000, 5000);
1127 }
1128
1129 if (!IS_ERR(gc2155->reset_gpio))
1130 gpiod_set_value_cansleep(gc2155->reset_gpio, 1);
1131 usleep_range(7000, 10000);
1132 gc2155->power_on = true;
1133 return 0;
1134 }
1135
__gc2155_power_off(struct gc2155 * gc2155)1136 static void __gc2155_power_off(struct gc2155 *gc2155)
1137 {
1138 if (!IS_ERR(gc2155->reset_gpio))
1139 gpiod_set_value_cansleep(gc2155->reset_gpio, 0);
1140 if (!IS_ERR(gc2155->pwdn_gpio))
1141 gpiod_set_value_cansleep(gc2155->pwdn_gpio, 1);
1142
1143 if (!IS_ERR(gc2155->xvclk))
1144 clk_disable_unprepare(gc2155->xvclk);
1145 if (!IS_ERR(gc2155->power_gpio))
1146 gpiod_set_value_cansleep(gc2155->power_gpio, 0);
1147 regulator_bulk_disable(GC2155_NUM_SUPPLIES, gc2155->supplies);
1148 gc2155->power_on = false;
1149 }
1150
gc2155_get_module_inf(struct gc2155 * gc2155,struct rkmodule_inf * inf)1151 static void gc2155_get_module_inf(struct gc2155 *gc2155,
1152 struct rkmodule_inf *inf)
1153 {
1154 memset(inf, 0, sizeof(*inf));
1155 strlcpy(inf->base.sensor, GC2155_NAME, sizeof(inf->base.sensor));
1156 strlcpy(inf->base.module, gc2155->module_name,
1157 sizeof(inf->base.module));
1158 strlcpy(inf->base.lens, gc2155->len_name, sizeof(inf->base.lens));
1159 }
1160
gc2155_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)1161 static long gc2155_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1162 {
1163 struct gc2155 *gc2155 = to_gc2155(sd);
1164 long ret = 0;
1165
1166 switch (cmd) {
1167 case RKMODULE_GET_MODULE_INFO:
1168 gc2155_get_module_inf(gc2155, (struct rkmodule_inf *)arg);
1169 break;
1170 default:
1171 ret = -ENOIOCTLCMD;
1172 break;
1173 }
1174
1175 return ret;
1176 }
1177
1178 #ifdef CONFIG_COMPAT
gc2155_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)1179 static long gc2155_compat_ioctl32(struct v4l2_subdev *sd,
1180 unsigned int cmd, unsigned long arg)
1181 {
1182 void __user *up = compat_ptr(arg);
1183 struct rkmodule_inf *inf;
1184 struct rkmodule_awb_cfg *cfg;
1185 long ret;
1186
1187 switch (cmd) {
1188 case RKMODULE_GET_MODULE_INFO:
1189 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
1190 if (!inf) {
1191 ret = -ENOMEM;
1192 return ret;
1193 }
1194
1195 ret = gc2155_ioctl(sd, cmd, inf);
1196 if (!ret)
1197 ret = copy_to_user(up, inf, sizeof(*inf));
1198 kfree(inf);
1199 break;
1200 case RKMODULE_AWB_CFG:
1201 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
1202 if (!cfg) {
1203 ret = -ENOMEM;
1204 return ret;
1205 }
1206
1207 ret = copy_from_user(cfg, up, sizeof(*cfg));
1208 if (!ret)
1209 ret = gc2155_ioctl(sd, cmd, cfg);
1210 kfree(cfg);
1211 break;
1212 default:
1213 ret = -ENOIOCTLCMD;
1214 break;
1215 }
1216
1217 return ret;
1218 }
1219 #endif
1220
gc2155_s_stream(struct v4l2_subdev * sd,int on)1221 static int gc2155_s_stream(struct v4l2_subdev *sd, int on)
1222 {
1223 struct gc2155 *gc2155 = to_gc2155(sd);
1224 struct i2c_client *client = gc2155->client;
1225 int ret = 0;
1226 u8 val;
1227 unsigned int fps;
1228 int delay_us;
1229
1230 fps = DIV_ROUND_CLOSEST(gc2155->cur_mode->max_fps.denominator,
1231 gc2155->cur_mode->max_fps.numerator);
1232
1233 dev_info(&client->dev, "%s: on: %d, %dx%d@%d\n", __func__, on,
1234 gc2155->cur_mode->width,
1235 gc2155->cur_mode->height,
1236 fps);
1237
1238 mutex_lock(&gc2155->mutex);
1239
1240 on = !!on;
1241 if (on == gc2155->streaming)
1242 goto unlock_and_return;
1243
1244 if (on) {
1245 ret = pm_runtime_get_sync(&gc2155->client->dev);
1246 if (ret < 0) {
1247 pm_runtime_put_noidle(&client->dev);
1248 goto unlock_and_return;
1249 }
1250
1251 ret = gc2155_write_array(gc2155->client,
1252 gc2155->cur_mode->reg_list);
1253 if (ret) {
1254 pm_runtime_put(&client->dev);
1255 goto unlock_and_return;
1256 }
1257
1258 } else {
1259 pm_runtime_put(&client->dev);
1260 }
1261 val = on ? 0x0f : 0;
1262 ret = gc2155_write_reg(client, 0xf2, val);
1263 gc2155->streaming = on;
1264
1265 /* delay to enable oneframe complete */
1266 if (!on) {
1267 delay_us = 1000 * 1000 / fps;
1268 usleep_range(delay_us, delay_us + 10);
1269 dev_info(&client->dev, "%s: on: %d, sleep(%dus)\n",
1270 __func__, on, delay_us);
1271 }
1272
1273 unlock_and_return:
1274 mutex_unlock(&gc2155->mutex);
1275
1276 return ret;
1277 }
1278
gc2155_s_power(struct v4l2_subdev * sd,int on)1279 static int gc2155_s_power(struct v4l2_subdev *sd, int on)
1280 {
1281 struct gc2155 *gc2155 = to_gc2155(sd);
1282 struct i2c_client *client = gc2155->client;
1283 int ret = 0;
1284
1285 mutex_lock(&gc2155->mutex);
1286
1287 /* If the power state is not modified - no work to do. */
1288 if (gc2155->power_on == !!on)
1289 goto unlock_and_return;
1290
1291 if (on) {
1292 ret = pm_runtime_get_sync(&client->dev);
1293 if (ret < 0) {
1294 pm_runtime_put_noidle(&client->dev);
1295 goto unlock_and_return;
1296 }
1297
1298 ret = gc2155_write_array(gc2155->client, gc2155_global_regs);
1299 if (ret) {
1300 v4l2_err(sd, "could not set init registers\n");
1301 pm_runtime_put_noidle(&client->dev);
1302 goto unlock_and_return;
1303 }
1304
1305 gc2155->power_on = true;
1306 } else {
1307 pm_runtime_put(&client->dev);
1308 gc2155->power_on = false;
1309 }
1310
1311 unlock_and_return:
1312 mutex_unlock(&gc2155->mutex);
1313
1314 return ret;
1315 }
1316
gc2155_set_test_pattern(struct gc2155 * gc2155,int value)1317 static int gc2155_set_test_pattern(struct gc2155 *gc2155, int value)
1318 {
1319 return 0;
1320 }
1321
gc2155_s_ctrl(struct v4l2_ctrl * ctrl)1322 static int gc2155_s_ctrl(struct v4l2_ctrl *ctrl)
1323 {
1324 struct gc2155 *gc2155 =
1325 container_of(ctrl->handler, struct gc2155, ctrls);
1326
1327 switch (ctrl->id) {
1328 case V4L2_CID_TEST_PATTERN:
1329 return gc2155_set_test_pattern(gc2155, ctrl->val);
1330 }
1331
1332 return 0;
1333 }
1334
1335 static const struct v4l2_ctrl_ops gc2155_ctrl_ops = {
1336 .s_ctrl = gc2155_s_ctrl,
1337 };
1338
1339 static const char * const gc2155_test_pattern_menu[] = {
1340 "Disabled",
1341 "Vertical Color Bars",
1342 };
1343
1344 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
gc2155_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)1345 static int gc2155_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1346 {
1347 struct gc2155 *gc2155 = to_gc2155(sd);
1348 struct v4l2_mbus_framefmt *try_fmt =
1349 v4l2_subdev_get_try_format(sd, fh->pad, 0);
1350 const struct gc2155_mode *def_mode = &supported_modes[0];
1351
1352 mutex_lock(&gc2155->mutex);
1353
1354 try_fmt->width = def_mode->width;
1355 try_fmt->height = def_mode->height;
1356 try_fmt->code = MEDIA_BUS_FMT_UYVY8_2X8;
1357 try_fmt->field = V4L2_FIELD_NONE;
1358 try_fmt->colorspace = V4L2_COLORSPACE_SRGB;
1359
1360 mutex_unlock(&gc2155->mutex);
1361
1362 return 0;
1363 }
1364 #endif
1365
gc2155_runtime_resume(struct device * dev)1366 static int gc2155_runtime_resume(struct device *dev)
1367 {
1368 struct i2c_client *client = to_i2c_client(dev);
1369 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1370 struct gc2155 *gc2155 = to_gc2155(sd);
1371
1372 return __gc2155_power_on(gc2155);
1373 }
1374
gc2155_runtime_suspend(struct device * dev)1375 static int gc2155_runtime_suspend(struct device *dev)
1376 {
1377 struct i2c_client *client = to_i2c_client(dev);
1378 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1379 struct gc2155 *gc2155 = to_gc2155(sd);
1380
1381 __gc2155_power_off(gc2155);
1382
1383 return 0;
1384 }
1385
gc2155_g_mbus_config(struct v4l2_subdev * sd,struct v4l2_mbus_config * config)1386 static int gc2155_g_mbus_config(struct v4l2_subdev *sd,
1387 struct v4l2_mbus_config *config)
1388 {
1389 config->type = V4L2_MBUS_PARALLEL;
1390 config->flags = V4L2_MBUS_HSYNC_ACTIVE_HIGH |
1391 V4L2_MBUS_VSYNC_ACTIVE_LOW |
1392 V4L2_MBUS_PCLK_SAMPLE_RISING;
1393
1394 return 0;
1395 }
1396
gc2155_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)1397 static int gc2155_g_frame_interval(struct v4l2_subdev *sd,
1398 struct v4l2_subdev_frame_interval *fi)
1399 {
1400 struct gc2155 *gc2155 = to_gc2155(sd);
1401
1402 fi->interval = gc2155->cur_mode->max_fps;
1403
1404 return 0;
1405 }
1406
__gc2155_try_frame_size_fps(struct gc2155 * gc2155,struct v4l2_mbus_framefmt * mf,const struct gc2155_mode ** size,unsigned int fps)1407 static void __gc2155_try_frame_size_fps(struct gc2155 *gc2155,
1408 struct v4l2_mbus_framefmt *mf,
1409 const struct gc2155_mode **size,
1410 unsigned int fps)
1411 {
1412 const struct gc2155_mode *fsize = &gc2155->framesize_cfg[0];
1413 const struct gc2155_mode *match = NULL;
1414 unsigned int i = gc2155->cfg_num;
1415 unsigned int min_err = UINT_MAX;
1416
1417 while (i--) {
1418 unsigned int err = abs(fsize->width - mf->width)
1419 + abs(fsize->height - mf->height);
1420 if (err < min_err && fsize->reg_list[0].addr) {
1421 min_err = err;
1422 match = fsize;
1423 }
1424 fsize++;
1425 }
1426
1427 if (!match) {
1428 match = &gc2155->framesize_cfg[0];
1429 } else {
1430 fsize = &gc2155->framesize_cfg[0];
1431 for (i = 0; i < gc2155->cfg_num; i++) {
1432 if (fsize->width == match->width &&
1433 fsize->height == match->height &&
1434 fps >= fsize->fps)
1435 match = fsize;
1436
1437 fsize++;
1438 }
1439 }
1440
1441 mf->width = match->width;
1442 mf->height = match->height;
1443
1444 if (size)
1445 *size = match;
1446 }
1447
gc2155_s_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)1448 static int gc2155_s_frame_interval(struct v4l2_subdev *sd,
1449 struct v4l2_subdev_frame_interval *fi)
1450 {
1451 struct i2c_client *client = v4l2_get_subdevdata(sd);
1452 struct gc2155 *gc2155 = to_gc2155(sd);
1453 const struct gc2155_mode *mode = NULL;
1454 struct v4l2_mbus_framefmt mf;
1455 unsigned int fps;
1456 int ret = 0;
1457
1458 dev_dbg(&client->dev, "Setting %d/%d frame interval\n",
1459 fi->interval.numerator, fi->interval.denominator);
1460
1461 mutex_lock(&gc2155->mutex);
1462 if (gc2155->cur_mode->width == 1600)
1463 goto unlock;
1464 fps = DIV_ROUND_CLOSEST(fi->interval.denominator,
1465 fi->interval.numerator);
1466 mf = gc2155->format;
1467 __gc2155_try_frame_size_fps(gc2155, &mf, &mode, fps);
1468 if (gc2155->cur_mode != mode) {
1469 ret = gc2155_write_array(client, mode->reg_list);
1470 if (ret)
1471 goto unlock;
1472 gc2155->cur_mode = mode;
1473 gc2155->fps = fps;
1474 }
1475 unlock:
1476 mutex_unlock(&gc2155->mutex);
1477
1478 return ret;
1479 }
1480
gc2155_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)1481 static int gc2155_enum_frame_interval(struct v4l2_subdev *sd,
1482 struct v4l2_subdev_pad_config *cfg,
1483 struct v4l2_subdev_frame_interval_enum *fie)
1484 {
1485 if (fie->index >= ARRAY_SIZE(supported_modes))
1486 return -EINVAL;
1487
1488 fie->code = MEDIA_BUS_FMT_UYVY8_2X8;
1489 fie->width = supported_modes[fie->index].width;
1490 fie->height = supported_modes[fie->index].height;
1491 fie->interval = supported_modes[fie->index].max_fps;
1492 return 0;
1493 }
1494
1495 static const struct dev_pm_ops gc2155_pm_ops = {
1496 SET_RUNTIME_PM_OPS(gc2155_runtime_suspend,
1497 gc2155_runtime_resume, NULL)
1498 };
1499
1500 static const struct v4l2_subdev_core_ops gc2155_core_ops = {
1501 .log_status = v4l2_ctrl_subdev_log_status,
1502 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
1503 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
1504 .ioctl = gc2155_ioctl,
1505 #ifdef CONFIG_COMPAT
1506 .compat_ioctl32 = gc2155_compat_ioctl32,
1507 #endif
1508 .s_power = gc2155_s_power,
1509 };
1510
1511 static const struct v4l2_subdev_video_ops gc2155_video_ops = {
1512 .s_stream = gc2155_s_stream,
1513 .g_mbus_config = gc2155_g_mbus_config,
1514 .g_frame_interval = gc2155_g_frame_interval,
1515 .s_frame_interval = gc2155_s_frame_interval,
1516 };
1517
1518 static const struct v4l2_subdev_pad_ops gc2155_pad_ops = {
1519 .enum_mbus_code = gc2155_enum_mbus_code,
1520 .enum_frame_size = gc2155_enum_frame_sizes,
1521 .enum_frame_interval = gc2155_enum_frame_interval,
1522 .get_fmt = gc2155_get_fmt,
1523 .set_fmt = gc2155_set_fmt,
1524 };
1525
1526 static const struct v4l2_subdev_ops gc2155_subdev_ops = {
1527 .core = &gc2155_core_ops,
1528 .video = &gc2155_video_ops,
1529 .pad = &gc2155_pad_ops,
1530 };
1531
1532 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1533 static const struct v4l2_subdev_internal_ops gc2155_internal_ops = {
1534 .open = gc2155_open,
1535 };
1536 #endif
1537
gc2155_check_sensor_id(struct gc2155 * gc2155,struct i2c_client * client)1538 static int gc2155_check_sensor_id(struct gc2155 *gc2155,
1539 struct i2c_client *client)
1540 {
1541 struct device *dev = &gc2155->client->dev;
1542 u8 id_h, id_l;
1543
1544 id_h = gc2155_read_reg(client, REG_CHIP_ID_H);
1545 id_l = gc2155_read_reg(client, REG_CHIP_ID_L);
1546 if (id_h != CHIP_ID_H && id_l != CHIP_ID_L) {
1547 dev_err(dev, "Wrong camera sensor id(0x%02x%02x)\n",
1548 id_h, id_l);
1549 return -EINVAL;
1550 }
1551
1552 dev_info(dev, "Detected GC2155 (0x%02x%02x) sensor\n",
1553 CHIP_ID_H, CHIP_ID_L);
1554
1555 return 0;
1556 }
1557
gc2155_configure_regulators(struct gc2155 * gc2155)1558 static int gc2155_configure_regulators(struct gc2155 *gc2155)
1559 {
1560 u32 i;
1561
1562 for (i = 0; i < GC2155_NUM_SUPPLIES; i++)
1563 gc2155->supplies[i].supply = gc2155_supply_names[i];
1564
1565 return devm_regulator_bulk_get(&gc2155->client->dev,
1566 GC2155_NUM_SUPPLIES,
1567 gc2155->supplies);
1568 }
1569
gc2155_probe(struct i2c_client * client,const struct i2c_device_id * id)1570 static int gc2155_probe(struct i2c_client *client,
1571 const struct i2c_device_id *id)
1572 {
1573 struct device *dev = &client->dev;
1574 struct device_node *node = dev->of_node;
1575 struct gc2155 *gc2155;
1576 struct v4l2_subdev *sd;
1577 char facing[2];
1578 int ret;
1579
1580 dev_info(dev, "driver version: %02x.%02x.%02x",
1581 DRIVER_VERSION >> 16,
1582 (DRIVER_VERSION & 0xff00) >> 8,
1583 DRIVER_VERSION & 0x00ff);
1584
1585 gc2155 = devm_kzalloc(dev, sizeof(*gc2155), GFP_KERNEL);
1586 if (!gc2155)
1587 return -ENOMEM;
1588
1589 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1590 &gc2155->module_index);
1591 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1592 &gc2155->module_facing);
1593 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1594 &gc2155->module_name);
1595 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1596 &gc2155->len_name);
1597 if (ret) {
1598 dev_err(dev, "could not get module information!\n");
1599 return -EINVAL;
1600 }
1601
1602 gc2155->client = client;
1603 gc2155->cur_mode = &supported_modes[0];
1604 gc2155_get_default_format(gc2155, &gc2155->format);
1605 gc2155->format.width = gc2155->cur_mode->width;
1606 gc2155->format.height = gc2155->cur_mode->height;
1607 gc2155->fps = DIV_ROUND_CLOSEST(gc2155->cur_mode->max_fps.denominator,
1608 gc2155->cur_mode->max_fps.numerator);
1609 gc2155->framesize_cfg = supported_modes;
1610 gc2155->cfg_num = ARRAY_SIZE(supported_modes);
1611
1612 gc2155->xvclk = devm_clk_get(dev, "xvclk");
1613 if (IS_ERR(gc2155->xvclk)) {
1614 dev_err(dev, "Failed to get xvclk\n");
1615 return -EINVAL;
1616 }
1617 gc2155->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_LOW);
1618 if (IS_ERR(gc2155->power_gpio))
1619 dev_info(dev, "Failed to get power-gpios, maybe no use\n");
1620
1621 gc2155->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1622 if (IS_ERR(gc2155->reset_gpio))
1623 dev_warn(dev, "Failed to get reset-gpios\n");
1624
1625 gc2155->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
1626 if (IS_ERR(gc2155->pwdn_gpio))
1627 dev_warn(dev, "Failed to get gc2155-gpios\n");
1628
1629 ret = gc2155_configure_regulators(gc2155);
1630 if (ret) {
1631 dev_warn(dev, "Failed to get power regulators\n");
1632 return ret;
1633 }
1634 v4l2_ctrl_handler_init(&gc2155->ctrls, 2);
1635 gc2155->pixel_rate =
1636 v4l2_ctrl_new_std(&gc2155->ctrls, &gc2155_ctrl_ops,
1637 V4L2_CID_PIXEL_RATE, 0,
1638 GC2155_PIXEL_RATE, 1,
1639 GC2155_PIXEL_RATE);
1640
1641 v4l2_ctrl_new_std_menu_items(&gc2155->ctrls, &gc2155_ctrl_ops,
1642 V4L2_CID_TEST_PATTERN,
1643 ARRAY_SIZE(gc2155_test_pattern_menu) - 1,
1644 0, 0, gc2155_test_pattern_menu);
1645 gc2155->subdev.ctrl_handler = &gc2155->ctrls;
1646
1647 if (gc2155->ctrls.error) {
1648 dev_err(&client->dev, "%s: control initialization error %d\n",
1649 __func__, gc2155->ctrls.error);
1650 return gc2155->ctrls.error;
1651 }
1652
1653 mutex_init(&gc2155->mutex);
1654 v4l2_i2c_subdev_init(&gc2155->subdev, client, &gc2155_subdev_ops);
1655
1656 ret = __gc2155_power_on(gc2155);
1657 if (ret)
1658 goto err_destroy_mutex;
1659
1660 ret = gc2155_check_sensor_id(gc2155, client);
1661 if (ret)
1662 goto err_power_off;
1663
1664 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1665 gc2155->subdev.internal_ops = &gc2155_internal_ops;
1666 gc2155->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1667 #endif
1668 #if defined(CONFIG_MEDIA_CONTROLLER)
1669 gc2155->pad.flags = MEDIA_PAD_FL_SOURCE;
1670 gc2155->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1671 ret = media_entity_pads_init(&gc2155->subdev.entity, 1, &gc2155->pad);
1672 if (ret < 0)
1673 goto err_power_off;
1674 #endif
1675
1676 sd = &gc2155->subdev;
1677 memset(facing, 0, sizeof(facing));
1678 if (strcmp(gc2155->module_facing, "back") == 0)
1679 facing[0] = 'b';
1680 else
1681 facing[0] = 'f';
1682
1683 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1684 gc2155->module_index, facing,
1685 GC2155_NAME, dev_name(sd->dev));
1686 ret = v4l2_async_register_subdev_sensor_common(sd);
1687 if (ret) {
1688 dev_err(dev, "v4l2 async register subdev failed\n");
1689 goto err_clean_entity;
1690 }
1691
1692 pm_runtime_set_active(dev);
1693 pm_runtime_enable(dev);
1694 pm_runtime_idle(dev);
1695
1696 return 0;
1697
1698 err_clean_entity:
1699 #if defined(CONFIG_MEDIA_CONTROLLER)
1700 media_entity_cleanup(&gc2155->subdev.entity);
1701 #endif
1702 err_power_off:
1703 __gc2155_power_off(gc2155);
1704 err_destroy_mutex:
1705 mutex_destroy(&gc2155->mutex);
1706 v4l2_ctrl_handler_free(&gc2155->ctrls);
1707 return ret;
1708 }
1709
gc2155_remove(struct i2c_client * client)1710 static int gc2155_remove(struct i2c_client *client)
1711 {
1712 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1713 struct gc2155 *gc2155 = to_gc2155(sd);
1714
1715 v4l2_async_unregister_subdev(sd);
1716 #if defined(CONFIG_MEDIA_CONTROLLER)
1717 media_entity_cleanup(&sd->entity);
1718 #endif
1719 mutex_destroy(&gc2155->mutex);
1720
1721 pm_runtime_disable(&client->dev);
1722 if (!pm_runtime_status_suspended(&client->dev))
1723 __gc2155_power_off(gc2155);
1724 pm_runtime_set_suspended(&client->dev);
1725
1726 return 0;
1727 }
1728
1729 #if IS_ENABLED(CONFIG_OF)
1730 static const struct of_device_id gc2155_of_match[] = {
1731 { .compatible = "galaxycore,gc2155" },
1732 {},
1733 };
1734 MODULE_DEVICE_TABLE(of, gc2155_of_match);
1735 #endif
1736
1737 static const struct i2c_device_id gc2155_match_id[] = {
1738 {"gc2155", 0},
1739 {},
1740 };
1741
1742 static struct i2c_driver gc2155_i2c_driver = {
1743 .driver = {
1744 .name = GC2155_NAME,
1745 .pm = &gc2155_pm_ops,
1746 .of_match_table = of_match_ptr(gc2155_of_match),
1747 },
1748 .probe = gc2155_probe,
1749 .remove = gc2155_remove,
1750 .id_table = gc2155_match_id,
1751 };
1752
sensor_mod_init(void)1753 static int __init sensor_mod_init(void)
1754 {
1755 return i2c_add_driver(&gc2155_i2c_driver);
1756 }
1757
sensor_mod_exit(void)1758 static void __exit sensor_mod_exit(void)
1759 {
1760 i2c_del_driver(&gc2155_i2c_driver);
1761 }
1762
1763 device_initcall_sync(sensor_mod_init);
1764 module_exit(sensor_mod_exit);
1765
1766 MODULE_DESCRIPTION("GalaxyCore gc2155 sensor driver");
1767 MODULE_LICENSE("GPL v2");
1768