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