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