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