1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * GC032A CMOS Image Sensor driver
4 *
5 * Copyright (C) 2020 Rockchip Electronics Co., Ltd.
6 * V0.0X01.0X01 init driver.
7 * V0.0X01.0X02 add quick stream on/off
8 * V0.0X01.0X03 set sensor in stream off state by default
9 * to avoid sending abnormal data in the early stage.
10 */
11
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/i2c.h>
20 #include <linux/kernel.h>
21 #include <linux/media.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_graph.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28 #include <linux/videodev2.h>
29 #include <linux/version.h>
30 #include <linux/rk-camera-module.h>
31 #include <media/media-entity.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ctrls.h>
34 #include <media/v4l2-device.h>
35 #include <media/v4l2-event.h>
36 #include <media/v4l2-fwnode.h>
37 #include <media/v4l2-image-sizes.h>
38 #include <media/v4l2-mediabus.h>
39 #include <media/v4l2-subdev.h>
40
41 #define DRIVER_VERSION KERNEL_VERSION(0, 0x01, 0x2)
42 #define DRIVER_NAME "gc032a"
43 #define GC032A_PIXEL_RATE (96 * 1000 * 1000)
44 //#define GC032A_AUTO_FPS
45
46 /*
47 * GC032A register definitions
48 */
49 #define REG_SOFTWARE_STANDBY 0xf3
50
51 #define REG_SC_CHIP_ID_H 0xf0
52 #define REG_SC_CHIP_ID_L 0xf1
53
54 #define REG_NULL 0xFFFF /* Array end token */
55
56 #define SENSOR_ID(_msb, _lsb) ((_msb) << 8 | (_lsb))
57 #define GC032A_ID 0x232a
58
59 struct sensor_register {
60 u16 addr;
61 u8 value;
62 };
63
64 struct gc032a_framesize {
65 u16 width;
66 u16 height;
67 u16 max_exp_lines;
68 struct v4l2_fract max_fps;
69 const struct sensor_register *regs;
70 };
71
72 struct gc032a_pll_ctrl {
73 u8 ctrl1;
74 u8 ctrl2;
75 u8 ctrl3;
76 };
77
78 struct gc032a_pixfmt {
79 u32 code;
80 /* Output format Register Value (REG_FORMAT_CTRL00) */
81 struct sensor_register *format_ctrl_regs;
82 };
83
84 struct pll_ctrl_reg {
85 unsigned int div;
86 unsigned char reg;
87 };
88
89 static const char * const gc032a_supply_names[] = {
90 "dovdd", /* Digital I/O power */
91 "avdd", /* Analog power */
92 "dvdd", /* Digital core power */
93 };
94
95 #define GC032A_NUM_SUPPLIES ARRAY_SIZE(gc032a_supply_names)
96
97 struct gc032a {
98 struct v4l2_subdev sd;
99 struct media_pad pad;
100 struct v4l2_mbus_framefmt format;
101 unsigned int xvclk_frequency;
102 struct clk *xvclk;
103 struct gpio_desc *pwdn_gpio;
104 struct regulator_bulk_data supplies[GC032A_NUM_SUPPLIES];
105 struct mutex lock;
106 struct i2c_client *client;
107 struct v4l2_ctrl_handler ctrls;
108 struct v4l2_ctrl *link_frequency;
109 const struct gc032a_framesize *frame_size;
110 int streaming;
111 bool power_on;
112 u32 module_index;
113 const char *module_facing;
114 const char *module_name;
115 const char *len_name;
116 };
117
118 static const struct sensor_register gc032a_vga_regs[] = {
119 /*System*/
120 {0xf3, 0x00},
121 {0xf5, 0x06},
122 {0xf7, 0x01},
123 {0xf8, 0x03},
124 {0xf9, 0xce},
125 {0xfa, 0x00},
126 {0xfc, 0x02},
127 {0xfe, 0x02},
128 {0x81, 0x03},
129
130 {0xfe, 0x00},
131 {0x77, 0x64},
132 {0x78, 0x40},
133 {0x79, 0x60},
134
135 /*ANALOG & CISCTL*/
136 {0xfe, 0x00},
137 {0x03, 0x01},
138 {0x04, 0xce},
139 {0x05, 0x01},
140 {0x06, 0xad},
141 {0x07, 0x00},
142 {0x08, 0x10},
143 {0x0a, 0x00},
144 {0x0c, 0x00},
145 {0x0d, 0x01},
146 {0x0e, 0xe8},
147 {0x0f, 0x02},
148 {0x10, 0x88},
149 {0x17, 0x54},
150 {0x19, 0x08},
151 {0x1a, 0x0a},
152 {0x1f, 0x40},
153 {0x20, 0x30},
154 {0x2e, 0x80},
155 {0x2f, 0x2b},
156 {0x30, 0x1a},
157 {0xfe, 0x02},
158 {0x03, 0x02},
159 {0x05, 0xd7},
160 {0x06, 0x60},
161 {0x08, 0x80},
162 {0x12, 0x89},
163
164 /*blk*/
165 {0xfe, 0x00},
166 {0x18, 0x02},
167 {0xfe, 0x02},
168 {0x40, 0x22},
169 {0x45, 0x00},
170 {0x46, 0x00},
171 {0x49, 0x20},
172 {0x4b, 0x3c},
173 {0x50, 0x20},
174 {0x42, 0x10},
175
176 /*isp*/
177 {0xfe, 0x01},
178 {0x0a, 0xc5},
179 {0x45, 0x00},
180 {0xfe, 0x00},
181 {0x40, 0xff},
182 {0x41, 0x25},
183 {0x42, 0xcf},
184 {0x43, 0x10},
185 {0x44, 0x83},//Output_format //80
186 {0x46, 0x22},//sync
187 {0x49, 0x03},
188 {0xfe, 0x02},
189 {0x22, 0xf6},
190
191 /*Shading*/
192 {0xfe, 0x00},
193 {0xfe, 0x01},
194 {0xc1, 0x38},
195 {0xc2, 0x4c},
196 {0xc3, 0x00},
197 {0xc4, 0x32},
198 {0xc5, 0x24},
199 {0xc6, 0x16},
200 {0xc7, 0x08},
201 {0xc8, 0x08},
202 {0xc9, 0x00},
203 {0xca, 0x20},
204 {0xdc, 0x8a},
205 {0xdd, 0xa0},
206 {0xde, 0xa6},
207 {0xdf, 0x75},
208
209 /*AWB*//*20170110*/
210 {0xfe, 0x01},
211 {0x90, 0x00},
212 {0x91, 0x00},
213 {0x92, 0xe3},
214 {0x93, 0xbe},
215 {0x95, 0x0b},
216 {0x96, 0xe3},
217 {0x97, 0x2e},
218 {0x98, 0x0b},
219 {0x9a, 0x2d},
220 {0x9b, 0x0b},
221 {0x9c, 0x59},
222 {0x9d, 0x2e},
223 {0x9f, 0x67},
224 {0xa0, 0x59},
225 {0xa1, 0x00},
226 {0xa2, 0x00},
227 {0x86, 0x00},
228 {0x87, 0x00},
229 {0x88, 0x00},
230 {0x89, 0x00},
231 {0xa4, 0x00},
232 {0xa5, 0x00},
233 {0xa6, 0xda},
234 {0xa7, 0x97},
235 {0xa9, 0xda},
236 {0xaa, 0x9a},
237 {0xab, 0xac},
238 {0xac, 0x86},
239 {0xae, 0xda},
240 {0xaf, 0xac},
241 {0xb0, 0xda},
242 {0xb1, 0xac},
243 {0xb3, 0xda},
244 {0xb4, 0xac},
245 {0xb5, 0x00},
246 {0xb6, 0x00},
247 {0x8b, 0x00},
248 {0x8c, 0x00},
249 {0x8d, 0x00},
250 {0x8e, 0x00},
251 {0x94, 0x50},
252 {0x99, 0xa6},
253 {0x9e, 0xaa},
254 {0xa3, 0x0a},
255 {0x8a, 0x00},
256 {0xa8, 0x50},
257 {0xad, 0x55},
258 {0xb2, 0x55},
259 {0xb7, 0x05},
260 {0x8f, 0x00},
261 {0xb8, 0xae},
262 {0xb9, 0xbb},
263
264 /*CC*/
265 {0xfe, 0x01},
266 {0xd0, 0x40},
267 {0xd1, 0xf8},
268 {0xd2, 0x00},
269 {0xd3, 0xfa},
270 {0xd4, 0x45},
271 {0xd5, 0x02},
272 {0xd6, 0x30},
273 {0xd7, 0xfa},
274 {0xd8, 0x08},
275 {0xd9, 0x08},
276 {0xda, 0x58},
277 {0xdb, 0x02},
278 {0xfe, 0x00},
279
280 /*Gamma*/
281 {0xfe, 0x00},
282 {0xba, 0x00},
283 {0xbb, 0x04},
284 {0xbc, 0x0a},
285 {0xbd, 0x0e},
286 {0xbe, 0x22},
287 {0xbf, 0x30},
288 {0xc0, 0x3d},
289 {0xc1, 0x4a},
290 {0xc2, 0x5d},
291 {0xc3, 0x6b},
292 {0xc4, 0x7a},
293 {0xc5, 0x85},
294 {0xc6, 0x90},
295 {0xc7, 0xa5},
296 {0xc8, 0xb5},
297 {0xc9, 0xc2},
298 {0xca, 0xcc},
299 {0xcb, 0xd5},
300 {0xcc, 0xde},
301 {0xcd, 0xea},
302 {0xce, 0xf5},
303 {0xcf, 0xff},
304
305 /*Auto Gamma*/
306 {0xfe, 0x00},
307 {0x5a, 0x08},
308 {0x5b, 0x0f},
309 {0x5c, 0x15},
310 {0x5d, 0x1c},
311 {0x5e, 0x28},
312 {0x5f, 0x36},
313 {0x60, 0x45},
314 {0x61, 0x51},
315 {0x62, 0x6a},
316 {0x63, 0x7d},
317 {0x64, 0x8d},
318 {0x65, 0x98},
319 {0x66, 0xa2},
320 {0x67, 0xb5},
321 {0x68, 0xc3},
322 {0x69, 0xcd},
323 {0x6a, 0xd4},
324 {0x6b, 0xdc},
325 {0x6c, 0xe3},
326 {0x6d, 0xf0},
327 {0x6e, 0xf9},
328 {0x6f, 0xff},
329
330 /*Gain*/
331 {0xfe, 0x00},
332 {0x70, 0x50},
333
334 /*AEC*/
335 {0xfe, 0x00},
336 {0x4f, 0x01},
337 {0xfe, 0x01},
338 {0x0d, 0x00},//08 add 20170110
339 {0x12, 0xa0},
340 {0x13, 0x3a},
341 {0x44, 0x04},
342 {0x1f, 0x30},
343 {0x20, 0x40},
344 {0x26, 0x9a},
345 {0x3e, 0x20},
346 {0x3f, 0x2d},
347 {0x40, 0x40},
348 {0x41, 0x5b},
349 {0x42, 0x82},
350 {0x43, 0xb7},
351 {0x04, 0x0a},
352 {0x02, 0x79},
353 {0x03, 0xc0},
354
355 /*measure window*/
356 {0xfe, 0x01},
357 {0xcc, 0x08},
358 {0xcd, 0x08},
359 {0xce, 0xa4},
360 {0xcf, 0xec},
361
362 /*DNDD*/
363 {0xfe, 0x00},
364 {0x81, 0xb8},
365 {0x82, 0x15},//de_noise
366 {0x83, 0x1a},//de_noise dark
367 {0x84, 0x01},
368 {0x86, 0x50},
369 {0x87, 0x18},
370 {0x88, 0x10},
371 {0x89, 0x70},
372 {0x8a, 0x20},
373 {0x8b, 0x10},
374 {0x8c, 0x08},
375 {0x8d, 0x0a},
376
377 /*Intpee*/
378 {0xfe, 0x00},
379 {0x8f, 0xaa},
380 {0x90, 0x9c},
381 {0x91, 0x52},
382 {0x92, 0x03},
383 {0x93, 0x03},
384 {0x94, 0x08},
385 {0x95, 0x65},//sharpness
386 {0x97, 0x00},
387 {0x98, 0x00},
388
389 /*ASDE*/
390 {0xfe, 0x00},
391 {0xa1, 0x30},
392 {0xa2, 0x41},
393 {0xa4, 0x30},
394 {0xa5, 0x20},
395 {0xaa, 0x30},
396 {0xac, 0x32},
397
398 /*YCP*/
399 {0xfe, 0x00},
400 {0xd1, 0x37},//3a
401 {0xd2, 0x37},//3a
402 {0xd3, 0x40},//38
403 {0xd6, 0xf4},
404 {0xd7, 0x1d},
405 {0xdd, 0x73},
406 {0xde, 0x84},
407
408 #ifdef GC032A_AUTO_FPS
409 /*Banding*/
410 /* 7fps */
411 {0xfe, 0x00},
412 {0x05, 0x01},
413 {0x06, 0xad},
414 {0x07, 0x00},
415 {0x08, 0x10},
416
417 {0xfe, 0x01},
418 {0x25, 0x00},
419 {0x26, 0x9a},
420 {0x27, 0x01},
421 {0x28, 0xce},
422 {0x29, 0x03},
423 {0x2a, 0x02},
424 {0x2b, 0x04},
425 {0x2c, 0x36},
426 {0x2d, 0x07},
427 {0x2e, 0xd2},
428 {0x2f, 0x0b},
429 {0x30, 0x6e},
430 {0x31, 0x0e},
431 {0x32, 0x70},
432 {0x33, 0x12},
433 {0x34, 0x0c},
434 {0x3c, 0x20},
435 #else
436 /*Banding*/
437 /* 30 fps */
438 {0xfe, 0x00},
439 {0x05, 0x01},
440 {0x06, 0xad},
441 {0x07, 0x00},
442 {0x08, 0x10},
443
444 {0xfe, 0x01},
445 {0x25, 0x00},
446 {0x26, 0x9a},
447 {0x27, 0x01},
448 {0x28, 0xce},
449 {0x29, 0x01},
450 {0x2a, 0xce},
451 {0x2b, 0x01},
452 {0x2c, 0xce},
453 {0x2d, 0x01},
454 {0x2e, 0xce},
455 {0x2f, 0x01},
456 {0x30, 0xce},
457 {0x31, 0x01},
458 {0x32, 0xce},
459 {0x33, 0x01},
460 {0x34, 0xce},
461 {0x3c, 0x00},
462 #endif
463 {0xfe, 0x00},
464 {REG_NULL, 0x00},
465 };
466
467 static const struct gc032a_framesize gc032a_framesizes[] = {
468 { /* VGA */
469 .width = 640,
470 .height = 480,
471 .max_fps = {
472 .numerator = 10000,
473 .denominator = 300000,
474 },
475 .regs = gc032a_vga_regs,
476 .max_exp_lines = 488,
477 }
478 };
479
480 static const struct gc032a_pixfmt gc032a_formats[] = {
481 {
482 .code = MEDIA_BUS_FMT_YUYV8_2X8,
483 }
484 };
485
to_gc032a(struct v4l2_subdev * sd)486 static inline struct gc032a *to_gc032a(struct v4l2_subdev *sd)
487 {
488 return container_of(sd, struct gc032a, sd);
489 }
490
491 /* sensor register write */
gc032a_write(struct i2c_client * client,u8 reg,u8 val)492 static int gc032a_write(struct i2c_client *client, u8 reg, u8 val)
493 {
494 struct i2c_msg msg;
495 u8 buf[2];
496 int ret;
497
498 buf[0] = reg & 0xFF;
499 buf[1] = val;
500
501 msg.addr = client->addr;
502 msg.flags = client->flags;
503 msg.buf = buf;
504 msg.len = sizeof(buf);
505
506 ret = i2c_transfer(client->adapter, &msg, 1);
507 if (ret >= 0)
508 return 0;
509
510 dev_err(&client->dev,
511 "gc032a write reg(0x%x val:0x%x) failed !\n", reg, val);
512
513 return ret;
514 }
515
516 /* sensor register read */
gc032a_read(struct i2c_client * client,u8 reg,u8 * val)517 static int gc032a_read(struct i2c_client *client, u8 reg, u8 *val)
518 {
519 struct i2c_msg msg[2];
520 u8 buf[1];
521 int ret;
522
523 buf[0] = reg & 0xFF;
524
525 msg[0].addr = client->addr;
526 msg[0].flags = client->flags;
527 msg[0].buf = buf;
528 msg[0].len = sizeof(buf);
529
530 msg[1].addr = client->addr;
531 msg[1].flags = client->flags | I2C_M_RD;
532 msg[1].buf = buf;
533 msg[1].len = 1;
534
535 ret = i2c_transfer(client->adapter, msg, 2);
536 if (ret >= 0) {
537 *val = buf[0];
538 return 0;
539 }
540
541 dev_err(&client->dev,
542 "gc032a read reg:0x%x failed !\n", reg);
543
544 return ret;
545 }
546
gc032a_write_array(struct i2c_client * client,const struct sensor_register * regs)547 static int gc032a_write_array(struct i2c_client *client,
548 const struct sensor_register *regs)
549 {
550 int i, ret = 0;
551
552 i = 0;
553 while (regs[i].addr != REG_NULL) {
554 ret = gc032a_write(client, regs[i].addr, regs[i].value);
555 if (ret) {
556 dev_err(&client->dev, "%s failed !\n", __func__);
557 break;
558 }
559
560 i++;
561 }
562
563 return ret;
564 }
565
gc032a_get_default_format(struct v4l2_mbus_framefmt * format)566 static void gc032a_get_default_format(struct v4l2_mbus_framefmt *format)
567 {
568 format->width = gc032a_framesizes[0].width;
569 format->height = gc032a_framesizes[0].height;
570 format->colorspace = V4L2_COLORSPACE_SRGB;
571 format->code = gc032a_formats[0].code;
572 format->field = V4L2_FIELD_NONE;
573 }
574
gc032a_set_streaming(struct gc032a * gc032a,int on)575 static void gc032a_set_streaming(struct gc032a *gc032a, int on)
576 {
577 struct i2c_client *client = gc032a->client;
578 int ret;
579
580 dev_dbg(&client->dev, "%s: on: %d\n", __func__, on);
581
582 ret = gc032a_write(client, REG_SOFTWARE_STANDBY, on);
583 if (ret)
584 dev_err(&client->dev, "gc032a soft standby failed\n");
585 }
586
587 /*
588 * V4L2 subdev video and pad level operations
589 */
590
gc032a_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)591 static int gc032a_enum_mbus_code(struct v4l2_subdev *sd,
592 struct v4l2_subdev_pad_config *cfg,
593 struct v4l2_subdev_mbus_code_enum *code)
594 {
595 struct i2c_client *client = v4l2_get_subdevdata(sd);
596
597 dev_dbg(&client->dev, "%s:\n", __func__);
598
599 if (code->index >= ARRAY_SIZE(gc032a_formats))
600 return -EINVAL;
601
602 code->code = gc032a_formats[code->index].code;
603
604 return 0;
605 }
606
gc032a_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)607 static int gc032a_enum_frame_sizes(struct v4l2_subdev *sd,
608 struct v4l2_subdev_pad_config *cfg,
609 struct v4l2_subdev_frame_size_enum *fse)
610 {
611 struct i2c_client *client = v4l2_get_subdevdata(sd);
612 int i = ARRAY_SIZE(gc032a_formats);
613
614 dev_dbg(&client->dev, "%s:\n", __func__);
615
616 if (fse->index >= ARRAY_SIZE(gc032a_framesizes))
617 return -EINVAL;
618
619 while (--i)
620 if (fse->code == gc032a_formats[i].code)
621 break;
622
623 fse->code = gc032a_formats[i].code;
624
625 fse->min_width = gc032a_framesizes[fse->index].width;
626 fse->max_width = fse->min_width;
627 fse->max_height = gc032a_framesizes[fse->index].height;
628 fse->min_height = fse->max_height;
629
630 return 0;
631 }
632
gc032a_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)633 static int gc032a_get_fmt(struct v4l2_subdev *sd,
634 struct v4l2_subdev_pad_config *cfg,
635 struct v4l2_subdev_format *fmt)
636 {
637 struct i2c_client *client = v4l2_get_subdevdata(sd);
638 struct gc032a *gc032a = to_gc032a(sd);
639
640 dev_dbg(&client->dev, "%s enter\n", __func__);
641
642 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
643 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
644 struct v4l2_mbus_framefmt *mf;
645
646 mf = v4l2_subdev_get_try_format(sd, cfg, 0);
647 mutex_lock(&gc032a->lock);
648 fmt->format = *mf;
649 mutex_unlock(&gc032a->lock);
650 return 0;
651 #else
652 return -ENOTTY;
653 #endif
654 }
655
656 mutex_lock(&gc032a->lock);
657 fmt->format = gc032a->format;
658 mutex_unlock(&gc032a->lock);
659
660 dev_dbg(&client->dev, "%s: %x %dx%d\n", __func__,
661 gc032a->format.code, gc032a->format.width,
662 gc032a->format.height);
663
664 return 0;
665 }
666
__gc032a_try_frame_size(struct v4l2_mbus_framefmt * mf,const struct gc032a_framesize ** size)667 static void __gc032a_try_frame_size(struct v4l2_mbus_framefmt *mf,
668 const struct gc032a_framesize **size)
669 {
670 const struct gc032a_framesize *fsize = &gc032a_framesizes[0];
671 const struct gc032a_framesize *match = NULL;
672 int i = ARRAY_SIZE(gc032a_framesizes);
673 unsigned int min_err = UINT_MAX;
674
675 while (i--) {
676 unsigned int err = abs(fsize->width - mf->width)
677 + abs(fsize->height - mf->height);
678 if (err < min_err && fsize->regs[0].addr) {
679 min_err = err;
680 match = fsize;
681 }
682 fsize++;
683 }
684
685 if (!match)
686 match = &gc032a_framesizes[0];
687
688 mf->width = match->width;
689 mf->height = match->height;
690
691 if (size)
692 *size = match;
693 }
694
gc032a_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)695 static int gc032a_set_fmt(struct v4l2_subdev *sd,
696 struct v4l2_subdev_pad_config *cfg,
697 struct v4l2_subdev_format *fmt)
698 {
699 struct i2c_client *client = v4l2_get_subdevdata(sd);
700 int index = ARRAY_SIZE(gc032a_formats);
701 struct v4l2_mbus_framefmt *mf = &fmt->format;
702 const struct gc032a_framesize *size = NULL;
703 struct gc032a *gc032a = to_gc032a(sd);
704 int ret = 0;
705
706 dev_dbg(&client->dev, "%s enter\n", __func__);
707
708 __gc032a_try_frame_size(mf, &size);
709
710 while (--index >= 0)
711 if (gc032a_formats[index].code == mf->code)
712 break;
713
714 if (index < 0)
715 return -EINVAL;
716
717 mf->colorspace = V4L2_COLORSPACE_SRGB;
718 mf->code = gc032a_formats[index].code;
719 mf->field = V4L2_FIELD_NONE;
720
721 mutex_lock(&gc032a->lock);
722
723 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
724 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
725 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
726 *mf = fmt->format;
727 #else
728 return -ENOTTY;
729 #endif
730 } else {
731 if (gc032a->streaming) {
732 mutex_unlock(&gc032a->lock);
733 return -EBUSY;
734 }
735
736 gc032a->frame_size = size;
737 gc032a->format = fmt->format;
738 }
739
740 mutex_unlock(&gc032a->lock);
741 return ret;
742 }
743
gc032a_get_module_inf(struct gc032a * gc032a,struct rkmodule_inf * inf)744 static void gc032a_get_module_inf(struct gc032a *gc032a,
745 struct rkmodule_inf *inf)
746 {
747 memset(inf, 0, sizeof(*inf));
748 strlcpy(inf->base.sensor, DRIVER_NAME, sizeof(inf->base.sensor));
749 strlcpy(inf->base.module, gc032a->module_name,
750 sizeof(inf->base.module));
751 strlcpy(inf->base.lens, gc032a->len_name, sizeof(inf->base.lens));
752 }
753
gc032a_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)754 static long gc032a_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
755 {
756 struct gc032a *gc032a = to_gc032a(sd);
757 long ret = 0;
758 u32 stream = 0;
759
760 switch (cmd) {
761 case RKMODULE_GET_MODULE_INFO:
762 gc032a_get_module_inf(gc032a, (struct rkmodule_inf *)arg);
763 break;
764 case RKMODULE_SET_QUICK_STREAM:
765
766 stream = *((u32 *)arg);
767
768 if (stream)
769 gc032a_set_streaming(gc032a, 0xff);
770 else
771 gc032a_set_streaming(gc032a, 0x00);
772 break;
773 default:
774 ret = -ENOIOCTLCMD;
775 break;
776 }
777
778 return ret;
779 }
780
781 #ifdef CONFIG_COMPAT
gc032a_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)782 static long gc032a_compat_ioctl32(struct v4l2_subdev *sd,
783 unsigned int cmd, unsigned long arg)
784 {
785 void __user *up = compat_ptr(arg);
786 struct rkmodule_inf *inf;
787 struct rkmodule_awb_cfg *cfg;
788 long ret;
789 u32 stream = 0;
790
791 switch (cmd) {
792 case RKMODULE_GET_MODULE_INFO:
793 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
794 if (!inf) {
795 ret = -ENOMEM;
796 return ret;
797 }
798
799 ret = gc032a_ioctl(sd, cmd, inf);
800 if (!ret)
801 ret = copy_to_user(up, inf, sizeof(*inf));
802 kfree(inf);
803 break;
804 case RKMODULE_AWB_CFG:
805 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
806 if (!cfg) {
807 ret = -ENOMEM;
808 return ret;
809 }
810
811 ret = copy_from_user(cfg, up, sizeof(*cfg));
812 if (!ret)
813 ret = gc032a_ioctl(sd, cmd, cfg);
814 kfree(cfg);
815 break;
816 case RKMODULE_SET_QUICK_STREAM:
817 ret = copy_from_user(&stream, up, sizeof(u32));
818 if (!ret)
819 ret = gc032a_ioctl(sd, cmd, &stream);
820 break;
821 default:
822 ret = -ENOIOCTLCMD;
823 break;
824 }
825
826 return ret;
827 }
828 #endif
829
gc032a_s_stream(struct v4l2_subdev * sd,int on)830 static int gc032a_s_stream(struct v4l2_subdev *sd, int on)
831 {
832 struct i2c_client *client = v4l2_get_subdevdata(sd);
833 struct gc032a *gc032a = to_gc032a(sd);
834
835 dev_info(&client->dev, "%s: on: %d\n", __func__, on);
836
837 mutex_lock(&gc032a->lock);
838 on = !!on;
839 if (gc032a->streaming == on)
840 goto unlock;
841
842 if (!on) {
843 /* Stop Streaming Sequence */
844 gc032a_set_streaming(gc032a, 0x00);
845 gc032a->streaming = on;
846 goto unlock;
847 }
848
849 gc032a_set_streaming(gc032a, 0xFF);
850 gc032a->streaming = on;
851
852 unlock:
853 mutex_unlock(&gc032a->lock);
854 return 0;
855 }
856
gc032a_set_test_pattern(struct gc032a * gc032a,int value)857 static int gc032a_set_test_pattern(struct gc032a *gc032a, int value)
858 {
859 return 0;
860 }
861
gc032a_s_ctrl(struct v4l2_ctrl * ctrl)862 static int gc032a_s_ctrl(struct v4l2_ctrl *ctrl)
863 {
864 struct gc032a *gc032a =
865 container_of(ctrl->handler, struct gc032a, ctrls);
866
867 switch (ctrl->id) {
868 case V4L2_CID_TEST_PATTERN:
869 return gc032a_set_test_pattern(gc032a, ctrl->val);
870 }
871
872 return 0;
873 }
874
875 static const struct v4l2_ctrl_ops gc032a_ctrl_ops = {
876 .s_ctrl = gc032a_s_ctrl,
877 };
878
879 static const char * const gc032a_test_pattern_menu[] = {
880 "Disabled",
881 "Vertical Color Bars",
882 };
883
884 /* -----------------------------------------------------------------------------
885 * V4L2 subdev internal operations
886 */
887
888 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
gc032a_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)889 static int gc032a_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
890 {
891 struct i2c_client *client = v4l2_get_subdevdata(sd);
892 struct v4l2_mbus_framefmt *format =
893 v4l2_subdev_get_try_format(sd, fh->pad, 0);
894
895 dev_dbg(&client->dev, "%s:\n", __func__);
896
897 gc032a_get_default_format(format);
898
899 return 0;
900 }
901 #endif
902
gc032a_g_mbus_config(struct v4l2_subdev * sd,struct v4l2_mbus_config * config)903 static int gc032a_g_mbus_config(struct v4l2_subdev *sd,
904 struct v4l2_mbus_config *config)
905 {
906 config->type = V4L2_MBUS_PARALLEL;
907 config->flags = V4L2_MBUS_HSYNC_ACTIVE_HIGH |
908 V4L2_MBUS_VSYNC_ACTIVE_LOW |
909 V4L2_MBUS_PCLK_SAMPLE_RISING;
910
911 return 0;
912 }
913
gc032a_power(struct v4l2_subdev * sd,int on)914 static int gc032a_power(struct v4l2_subdev *sd, int on)
915 {
916 int ret;
917 struct gc032a *gc032a = to_gc032a(sd);
918 struct i2c_client *client = gc032a->client;
919
920 dev_info(&client->dev, "%s(%d) on(%d)\n", __func__, __LINE__, on);
921 if (on) {
922 if (!IS_ERR(gc032a->pwdn_gpio)) {
923 gpiod_set_value_cansleep(gc032a->pwdn_gpio, 0);
924 usleep_range(2000, 5000);
925 }
926 ret = gc032a_write_array(client, gc032a->frame_size->regs);
927 if (ret)
928 dev_err(&client->dev, "init error\n");
929 gc032a->power_on = true;
930 } else {
931 if (!IS_ERR(gc032a->pwdn_gpio)) {
932 gpiod_set_value_cansleep(gc032a->pwdn_gpio, 1);
933 usleep_range(2000, 5000);
934 }
935 gc032a->power_on = false;
936 }
937 return 0;
938 }
939
gc032a_enum_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_interval_enum * fie)940 static int gc032a_enum_frame_interval(struct v4l2_subdev *sd,
941 struct v4l2_subdev_pad_config *cfg,
942 struct v4l2_subdev_frame_interval_enum *fie)
943 {
944 if (fie->index >= ARRAY_SIZE(gc032a_framesizes))
945 return -EINVAL;
946
947 fie->code = MEDIA_BUS_FMT_YUYV8_2X8;
948 fie->width = gc032a_framesizes[fie->index].width;
949 fie->height = gc032a_framesizes[fie->index].height;
950 fie->interval = gc032a_framesizes[fie->index].max_fps;
951 return 0;
952 }
953
954 static const struct v4l2_subdev_core_ops gc032a_subdev_core_ops = {
955 .log_status = v4l2_ctrl_subdev_log_status,
956 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
957 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
958 .ioctl = gc032a_ioctl,
959 #ifdef CONFIG_COMPAT
960 .compat_ioctl32 = gc032a_compat_ioctl32,
961 #endif
962 .s_power = gc032a_power,
963 };
964
965 static const struct v4l2_subdev_video_ops gc032a_subdev_video_ops = {
966 .s_stream = gc032a_s_stream,
967 .g_mbus_config = gc032a_g_mbus_config,
968 };
969
970 static const struct v4l2_subdev_pad_ops gc032a_subdev_pad_ops = {
971 .enum_mbus_code = gc032a_enum_mbus_code,
972 .enum_frame_size = gc032a_enum_frame_sizes,
973 .enum_frame_interval = gc032a_enum_frame_interval,
974 .get_fmt = gc032a_get_fmt,
975 .set_fmt = gc032a_set_fmt,
976 };
977
978 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
979 static const struct v4l2_subdev_ops gc032a_subdev_ops = {
980 .core = &gc032a_subdev_core_ops,
981 .video = &gc032a_subdev_video_ops,
982 .pad = &gc032a_subdev_pad_ops,
983 };
984
985 static const struct v4l2_subdev_internal_ops gc032a_subdev_internal_ops = {
986 .open = gc032a_open,
987 };
988 #endif
989
gc032a_detect(struct gc032a * gc032a)990 static int gc032a_detect(struct gc032a *gc032a)
991 {
992 struct i2c_client *client = gc032a->client;
993 u8 pid, ver;
994 int ret;
995
996 dev_dbg(&client->dev, "%s:\n", __func__);
997
998 /* Check sensor revision */
999 ret = gc032a_read(client, REG_SC_CHIP_ID_H, &pid);
1000 if (!ret)
1001 ret = gc032a_read(client, REG_SC_CHIP_ID_L, &ver);
1002
1003 if (!ret) {
1004 unsigned short id;
1005
1006 id = SENSOR_ID(pid, ver);
1007 if (id != GC032A_ID) {
1008 ret = -1;
1009 dev_err(&client->dev,
1010 "Sensor detection failed (%04X, %d)\n",
1011 id, ret);
1012 } else {
1013 dev_info(&client->dev, "Found GC%04X sensor\n", id);
1014 if (!IS_ERR(gc032a->pwdn_gpio))
1015 gpiod_set_value_cansleep(gc032a->pwdn_gpio, 1);
1016 }
1017 }
1018
1019 return ret;
1020 }
1021
__gc032a_power_on(struct gc032a * gc032a)1022 static int __gc032a_power_on(struct gc032a *gc032a)
1023 {
1024 int ret;
1025 struct device *dev = &gc032a->client->dev;
1026
1027 if (!IS_ERR(gc032a->xvclk)) {
1028 ret = clk_set_rate(gc032a->xvclk, 24000000);
1029 if (ret < 0)
1030 dev_info(dev, "Failed to set xvclk rate (24MHz)\n");
1031 }
1032
1033 if (!IS_ERR(gc032a->pwdn_gpio)) {
1034 gpiod_set_value_cansleep(gc032a->pwdn_gpio, 1);
1035 usleep_range(2000, 5000);
1036 }
1037
1038 if (!IS_ERR(gc032a->supplies)) {
1039 ret = regulator_bulk_enable(GC032A_NUM_SUPPLIES,
1040 gc032a->supplies);
1041 if (ret < 0)
1042 dev_info(dev, "Failed to enable regulators\n");
1043
1044 usleep_range(2000, 5000);
1045 }
1046
1047 if (!IS_ERR(gc032a->pwdn_gpio)) {
1048 gpiod_set_value_cansleep(gc032a->pwdn_gpio, 0);
1049 usleep_range(2000, 5000);
1050 }
1051
1052 if (!IS_ERR(gc032a->xvclk)) {
1053 ret = clk_prepare_enable(gc032a->xvclk);
1054 if (ret < 0)
1055 dev_info(dev, "Failed to enable xvclk\n");
1056 }
1057
1058 usleep_range(7000, 10000);
1059 gc032a->power_on = true;
1060 return 0;
1061 }
1062
__gc032a_power_off(struct gc032a * gc032a)1063 static void __gc032a_power_off(struct gc032a *gc032a)
1064 {
1065 if (!IS_ERR(gc032a->xvclk))
1066 clk_disable_unprepare(gc032a->xvclk);
1067 if (!IS_ERR(gc032a->supplies))
1068 regulator_bulk_disable(GC032A_NUM_SUPPLIES, gc032a->supplies);
1069 if (!IS_ERR(gc032a->pwdn_gpio))
1070 gpiod_set_value_cansleep(gc032a->pwdn_gpio, 1);
1071 gc032a->power_on = false;
1072 }
1073
gc032a_configure_regulators(struct gc032a * gc032a)1074 static int gc032a_configure_regulators(struct gc032a *gc032a)
1075 {
1076 unsigned int i;
1077
1078 for (i = 0; i < GC032A_NUM_SUPPLIES; i++)
1079 gc032a->supplies[i].supply = gc032a_supply_names[i];
1080
1081 return devm_regulator_bulk_get(&gc032a->client->dev,
1082 GC032A_NUM_SUPPLIES,
1083 gc032a->supplies);
1084 }
1085
gc032a_parse_of(struct gc032a * gc032a)1086 static int gc032a_parse_of(struct gc032a *gc032a)
1087 {
1088 struct device *dev = &gc032a->client->dev;
1089 int ret;
1090
1091 gc032a->pwdn_gpio = devm_gpiod_get(dev, "pwdn", GPIOD_OUT_LOW);
1092 if (IS_ERR(gc032a->pwdn_gpio))
1093 dev_info(dev, "Failed to get pwdn-gpios, maybe no used\n");
1094
1095 ret = gc032a_configure_regulators(gc032a);
1096 if (ret)
1097 dev_info(dev, "Failed to get power regulators\n");
1098
1099 return __gc032a_power_on(gc032a);
1100 }
1101
gc032a_probe(struct i2c_client * client,const struct i2c_device_id * id)1102 static int gc032a_probe(struct i2c_client *client,
1103 const struct i2c_device_id *id)
1104 {
1105 struct device *dev = &client->dev;
1106 struct device_node *node = dev->of_node;
1107 struct v4l2_subdev *sd;
1108 struct gc032a *gc032a;
1109 char facing[2];
1110 int ret;
1111
1112 dev_info(dev, "driver version: %02x.%02x.%02x",
1113 DRIVER_VERSION >> 16,
1114 (DRIVER_VERSION & 0xff00) >> 8,
1115 DRIVER_VERSION & 0x00ff);
1116
1117 gc032a = devm_kzalloc(&client->dev, sizeof(*gc032a), GFP_KERNEL);
1118 if (!gc032a)
1119 return -ENOMEM;
1120
1121 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
1122 &gc032a->module_index);
1123 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
1124 &gc032a->module_facing);
1125 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
1126 &gc032a->module_name);
1127 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
1128 &gc032a->len_name);
1129 if (ret) {
1130 dev_err(dev, "could not get module information!\n");
1131 return -EINVAL;
1132 }
1133
1134 gc032a->client = client;
1135 gc032a->xvclk = devm_clk_get(&client->dev, "xvclk");
1136 if (IS_ERR(gc032a->xvclk)) {
1137 dev_err(&client->dev, "Failed to get xvclk\n");
1138 return -EINVAL;
1139 }
1140
1141 gc032a_parse_of(gc032a);
1142
1143 gc032a->xvclk_frequency = clk_get_rate(gc032a->xvclk);
1144 if (gc032a->xvclk_frequency < 6000000 ||
1145 gc032a->xvclk_frequency > 27000000)
1146 return -EINVAL;
1147
1148 v4l2_ctrl_handler_init(&gc032a->ctrls, 2);
1149 gc032a->link_frequency =
1150 v4l2_ctrl_new_std(&gc032a->ctrls, &gc032a_ctrl_ops,
1151 V4L2_CID_PIXEL_RATE, 0,
1152 GC032A_PIXEL_RATE, 1,
1153 GC032A_PIXEL_RATE);
1154
1155 v4l2_ctrl_new_std_menu_items(&gc032a->ctrls, &gc032a_ctrl_ops,
1156 V4L2_CID_TEST_PATTERN,
1157 ARRAY_SIZE(gc032a_test_pattern_menu) - 1,
1158 0, 0, gc032a_test_pattern_menu);
1159 gc032a->sd.ctrl_handler = &gc032a->ctrls;
1160
1161 if (gc032a->ctrls.error) {
1162 dev_err(&client->dev, "%s: control initialization error %d\n",
1163 __func__, gc032a->ctrls.error);
1164 return gc032a->ctrls.error;
1165 }
1166
1167 sd = &gc032a->sd;
1168 client->flags |= I2C_CLIENT_SCCB;
1169 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1170 v4l2_i2c_subdev_init(sd, client, &gc032a_subdev_ops);
1171
1172 sd->internal_ops = &gc032a_subdev_internal_ops;
1173 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE |
1174 V4L2_SUBDEV_FL_HAS_EVENTS;
1175 #endif
1176
1177 #if defined(CONFIG_MEDIA_CONTROLLER)
1178 gc032a->pad.flags = MEDIA_PAD_FL_SOURCE;
1179 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1180 ret = media_entity_pads_init(&sd->entity, 1, &gc032a->pad);
1181 if (ret < 0) {
1182 v4l2_ctrl_handler_free(&gc032a->ctrls);
1183 return ret;
1184 }
1185 #endif
1186
1187 mutex_init(&gc032a->lock);
1188
1189 gc032a_get_default_format(&gc032a->format);
1190 gc032a->frame_size = &gc032a_framesizes[0];
1191
1192 ret = gc032a_detect(gc032a);
1193 if (ret < 0)
1194 goto error;
1195
1196 memset(facing, 0, sizeof(facing));
1197 if (strcmp(gc032a->module_facing, "back") == 0)
1198 facing[0] = 'b';
1199 else
1200 facing[0] = 'f';
1201
1202 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1203 gc032a->module_index, facing,
1204 DRIVER_NAME, dev_name(sd->dev));
1205 ret = v4l2_async_register_subdev_sensor_common(sd);
1206 if (ret)
1207 goto error;
1208
1209 dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
1210 gc032a->power_on = false;
1211 return 0;
1212
1213 error:
1214 v4l2_ctrl_handler_free(&gc032a->ctrls);
1215 #if defined(CONFIG_MEDIA_CONTROLLER)
1216 media_entity_cleanup(&sd->entity);
1217 #endif
1218 mutex_destroy(&gc032a->lock);
1219 __gc032a_power_off(gc032a);
1220 return ret;
1221 }
1222
gc032a_remove(struct i2c_client * client)1223 static int gc032a_remove(struct i2c_client *client)
1224 {
1225 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1226 struct gc032a *gc032a = to_gc032a(sd);
1227
1228 v4l2_ctrl_handler_free(&gc032a->ctrls);
1229 v4l2_async_unregister_subdev(sd);
1230 #if defined(CONFIG_MEDIA_CONTROLLER)
1231 media_entity_cleanup(&sd->entity);
1232 #endif
1233 mutex_destroy(&gc032a->lock);
1234
1235 __gc032a_power_off(gc032a);
1236
1237 return 0;
1238 }
1239
1240 static const struct i2c_device_id gc032a_id[] = {
1241 { "gc032a", 0 },
1242 { /* sentinel */ },
1243 };
1244 MODULE_DEVICE_TABLE(i2c, gc032a_id);
1245
1246 #if IS_ENABLED(CONFIG_OF)
1247 static const struct of_device_id gc032a_of_match[] = {
1248 { .compatible = "galaxycore,gc032a", },
1249 { /* sentinel */ },
1250 };
1251 MODULE_DEVICE_TABLE(of, gc032a_of_match);
1252 #endif
1253
1254 static struct i2c_driver gc032a_i2c_driver = {
1255 .driver = {
1256 .name = DRIVER_NAME,
1257 .of_match_table = of_match_ptr(gc032a_of_match),
1258 },
1259 .probe = gc032a_probe,
1260 .remove = gc032a_remove,
1261 .id_table = gc032a_id,
1262 };
1263
sensor_mod_init(void)1264 static int __init sensor_mod_init(void)
1265 {
1266 return i2c_add_driver(&gc032a_i2c_driver);
1267 }
1268
sensor_mod_exit(void)1269 static void __exit sensor_mod_exit(void)
1270 {
1271 i2c_del_driver(&gc032a_i2c_driver);
1272 }
1273
1274 device_initcall_sync(sensor_mod_init);
1275 module_exit(sensor_mod_exit);
1276
1277 MODULE_AUTHOR("randy.wang <randy.wang@rock-chips.com>");
1278 MODULE_DESCRIPTION("GC032A CMOS Image Sensor driver");
1279 MODULE_LICENSE("GPL v2");
1280