• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * A V4L2 driver for ov5658 cameras.
3  *
4  * Copyright (c) 2019 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
5  *
6  * Authors:  Zheng ZeQun <zequnzheng@allwinnertech.com>
7  *    Liang WeiJie <liangweijie@allwinnertech.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
19 #include <linux/videodev2.h>
20 #include <linux/clk.h>
21 #include <media/v4l2-device.h>
22 #include <media/v4l2-mediabus.h>
23 #include <linux/io.h>
24 
25 #include "camera.h"
26 #include "sensor_helper.h"
27 
28 MODULE_AUTHOR("lwj");
29 MODULE_DESCRIPTION("A low-level driver for ov5658 sensors");
30 MODULE_LICENSE("GPL");
31 
32 #define MCLK              (24 * 1000 * 1000)
33 #define V4L2_IDENT_SENSOR 0x5656
34 
35 /*
36  * Our nominal (default) frame rate.
37  */
38 
39 #define SENSOR_FRAME_RATE 30
40 
41 /*
42  * The ov5658 sits on i2c with ID 0x6c
43  */
44 #define I2C_ADDR 0x6c
45 
46 #define SENSOR_NAME "ov5658"
47 
48 struct cfg_array { /* coming later */
49 	struct regval_list *regs;
50 	int size;
51 };
52 
53 /*
54  * The default register settings
55  *
56  */
57 static struct regval_list sensor_default_regs[] = {
58 	{0x0103, 0x01},
59 	{0x3210, 0x43},
60 	{0x3001, 0x0e},
61 	{0x3002, 0xc0},
62 	{0x3011, 0x21},	/* 2 lanes */
63 	{0x3012, 0x0a},	/* 10-bits */
64 	{0x3013, 0x50},
65 	{0x3015, 0x09},
66 	{0x3018, 0xf0},
67 	{0x3021, 0x40},
68 	{0x3500, 0x00},	/* log exp */
69 	{0x3501, 0x9b},
70 	{0x3502, 0x00},
71 	{0x3503, 0x07},
72 	{0x3505, 0x00},	/* manual sensor gain */
73 	{0x3506, 0x70},	/* short exp */
74 	{0x3507, 0x02},
75 	{0x3508, 0x00},
76 	{0x3509, 0x10},
77 	{0x350a, 0x00},
78 	{0x350b, 0x80},
79 
80 	/* clk */
81 	{0x3600, 0x69},	/* multiplier */
82 	{0x3602, 0x3c},	/* 24/3=8M, 8M x 105 = 840M, 840M/4/2=105M(pclk) */
83 	{0x3605, 0x14},
84 	{0x3606, 0x09},
85 
86 	{0x3612, 0x04},
87 	{0x3613, 0x66},
88 	{0x3614, 0x39},
89 	{0x3615, 0x33},
90 	{0x3616, 0x46},
91 	{0x361a, 0x0a},
92 	{0x361c, 0x76},
93 	{0x3620, 0x40},
94 	{0x3640, 0x03},
95 	{0x3641, 0x60},
96 	{0x3642, 0x00},
97 	{0x3643, 0x90},
98 	{0x3660, 0x04},
99 	{0x3665, 0x00},
100 	{0x3666, 0x20},
101 	{0x3667, 0x00},
102 	{0x366a, 0x80},
103 	{0x3680, 0xe0},
104 	{0x3692, 0x80},
105 	{0x3700, 0x42},
106 	{0x3701, 0x14},
107 	{0x3702, 0xe8},
108 	{0x3703, 0x20},
109 	{0x3704, 0x5e},
110 	{0x3705, 0x02},
111 	{0x3708, 0xe3},
112 	{0x3709, 0xc3},
113 	{0x370a, 0x00},
114 	{0x370b, 0x20},
115 	{0x370c, 0x0c},
116 	{0x370d, 0x11},
117 	{0x370e, 0x00},
118 	{0x370f, 0x40},
119 	{0x3710, 0x00},
120 	{0x3715, 0x09},
121 	{0x371a, 0x04},
122 	{0x371b, 0x05},
123 	{0x371c, 0x01},
124 	{0x371e, 0xa1},
125 	{0x371f, 0x18},
126 	{0x3721, 0x00},
127 	{0x3726, 0x00},
128 	{0x372a, 0x01},
129 	{0x3730, 0x10},
130 	{0x3738, 0x22},
131 	{0x3739, 0xe5},
132 	{0x373a, 0x50},
133 	{0x373b, 0x02},
134 	{0x373c, 0x2c},
135 	{0x373f, 0x02},
136 	{0x3740, 0x42},
137 	{0x3741, 0x02},
138 	{0x3743, 0x01},
139 	{0x3744, 0x02},
140 	{0x3747, 0x00},
141 	{0x3754, 0xc0},
142 	{0x3755, 0x07},
143 	{0x3756, 0x1a},
144 	{0x3759, 0x0f},
145 	{0x375c, 0x04},
146 	{0x3767, 0x00},
147 	{0x376b, 0x44},
148 	{0x377b, 0x44},
149 	{0x377c, 0x30},
150 	{0x377e, 0x30},
151 	{0x377f, 0x08},
152 	{0x3781, 0x0c},
153 	{0x3785, 0x1e},
154 	{0x378f, 0xf5},
155 	{0x3791, 0xb0},
156 	{0x379c, 0x0c},
157 	{0x379d, 0x20},
158 	{0x379e, 0x00},
159 	{0x3796, 0x72},
160 	{0x379a, 0x07},
161 	{0x379b, 0xb0},
162 	{0x37c5, 0x00},
163 	{0x37c6, 0x00},
164 	{0x37c7, 0x00},
165 	{0x37c9, 0x00},
166 	{0x37ca, 0x00},
167 	{0x37cb, 0x00},
168 	{0x37cc, 0x00},
169 	{0x37cd, 0x00},
170 	{0x37ce, 0x01},
171 	{0x37cf, 0x00},
172 	{0x37d1, 0x00},
173 	{0x37de, 0x00},
174 	{0x37df, 0x00},
175 
176 	{0x3823, 0x00},
177 	{0x3824, 0x00},
178 	{0x3825, 0x00},
179 	{0x3826, 0x00},
180 	{0x3827, 0x00},
181 	{0x3829, 0x0b},
182 	{0x382a, 0x04},
183 	{0x382c, 0x34},
184 	{0x382d, 0x00},
185 	{0x3a04, 0x06},
186 	{0x3a05, 0x14},
187 	{0x3a06, 0x00},
188 	{0x3a07, 0xfe},
189 	{0x3b00, 0x00},
190 	{0x3b02, 0x00},
191 	{0x3b03, 0x00},
192 	{0x3b04, 0x00},
193 	{0x3b05, 0x00},
194 	{0x4000, 0x18},
195 	{0x4001, 0x04},
196 	{0x4002, 0x45},
197 	{0x4004, 0x04},
198 	{0x4005, 0x18},
199 	{0x4006, 0x20},
200 	{0x4007, 0x98},
201 	{0x4008, 0x24},
202 	{0x4009, 0x12},	/* blc level, 0~255 */
203 	{0x400c, 0x00},
204 	{0x400d, 0x00},
205 	{0x404e, 0x37},
206 	{0x404f, 0x8f},
207 	{0x4058, 0x00},
208 	{0x4100, 0x50},
209 	{0x4101, 0x34},
210 	{0x4102, 0x34},
211 	{0x4104, 0xde},
212 	{0x4300, 0xff},
213 	{0x4307, 0x30},
214 	{0x4311, 0x04},	/* vsync width, 4311~4316 */
215 	{0x4315, 0x01},
216 	{0x4501, 0x08},
217 	{0x4502, 0x08},
218 	{0x4800, 0x24},	/* lp */
219 	{0x4816, 0x52},
220 	{0x481f, 0x30},
221 	{0x4826, 0x28},
222 	{0x4837, 0x0d},
223 	{0x4a00, 0xaa},
224 	{0x4a02, 0x00},
225 	{0x4a03, 0x01},
226 	{0x4a05, 0x40},
227 	{0x4a0a, 0x88},
228 	{0x5000, 0x06},
229 	{0x5001, 0x01},
230 	{0x5002, 0x00},
231 	{0x5003, 0x20},
232 	{0x5013, 0x00},
233 	{0x501f, 0x00},
234 	{0x5043, 0x48},
235 	{0x5780, 0x1c},
236 	{0x5786, 0x20},
237 	{0x5788, 0x18},
238 	{0x578a, 0x04},
239 	{0x578b, 0x02},
240 	{0x578c, 0x02},
241 	{0x578e, 0x06},
242 	{0x578f, 0x02},
243 	{0x5790, 0x02},
244 	{0x5791, 0xff},
245 	{0x5e00, 0x00},
246 	{0x5e10, 0x0c},
247 
248 	{0x0100, 0x01},
249 
250 	{REG_DLY, 0x08},
251 
252 	{0x5800, 0x22},
253 	{0x5801, 0x11},
254 	{0x5802, 0x0d},
255 	{0x5803, 0x0d},
256 	{0x5804, 0x12},
257 	{0x5805, 0x26},
258 	{0x5806, 0x09},
259 	{0x5807, 0x07},
260 	{0x5808, 0x05},
261 	{0x5809, 0x05},
262 	{0x580a, 0x07},
263 	{0x580b, 0x0a},
264 	{0x580c, 0x07},
265 	{0x580d, 0x02},
266 	{0x580e, 0x00},
267 	{0x580f, 0x00},
268 	{0x5810, 0x03},
269 	{0x5811, 0x07},
270 	{0x5812, 0x06},
271 	{0x5813, 0x02},
272 	{0x5814, 0x00},
273 	{0x5815, 0x00},
274 	{0x5816, 0x03},
275 	{0x5817, 0x07},
276 	{0x5818, 0x09},
277 	{0x5819, 0x05},
278 	{0x581a, 0x04},
279 	{0x581b, 0x04},
280 	{0x581c, 0x07},
281 	{0x581d, 0x09},
282 	{0x581e, 0x1d},
283 	{0x581f, 0x0e},
284 	{0x5820, 0x0b},
285 	{0x5821, 0x0b},
286 	{0x5822, 0x0f},
287 	{0x5823, 0x1e},
288 	{0x5824, 0x59},
289 	{0x5825, 0x46},
290 	{0x5826, 0x37},
291 	{0x5827, 0x36},
292 	{0x5828, 0x45},
293 	{0x5829, 0x39},
294 	{0x582a, 0x46},
295 	{0x582b, 0x44},
296 	{0x582c, 0x45},
297 	{0x582d, 0x28},
298 	{0x582e, 0x38},
299 	{0x582f, 0x52},
300 	{0x5830, 0x60},
301 	{0x5831, 0x51},
302 	{0x5832, 0x26},
303 	{0x5833, 0x38},
304 	{0x5834, 0x43},
305 	{0x5835, 0x42},
306 	{0x5836, 0x34},
307 	{0x5837, 0x18},
308 	{0x5838, 0x05},
309 	{0x5839, 0x27},
310 	{0x583a, 0x27},
311 	{0x583b, 0x27},
312 	{0x583c, 0x0a},
313 	{0x583d, 0xbf},
314 	{0x5842, 0x01},
315 	{0x5843, 0x2b},
316 	{0x5844, 0x01},
317 	{0x5845, 0x92},
318 	{0x5846, 0x01},
319 	{0x5847, 0x8f},
320 	{0x5848, 0x01},
321 	{0x5849, 0x0c},
322 };
323 
324 /*
325  * Xclk 24Mhz
326  * Pclk 84Mhz
327  * linelength 2816(0xb00)
328  * framelength 1984(0x7c0)
329  * grabwindow_width 2592
330  * grabwindow_height 1944
331  * max_framerate 15fps
332  * mipi_datarate per lane 420Mbps
333  */
334 static struct regval_list sensor_2592x1944_regs[] = {
335 	{0x0100, 0x00},
336 	{0x3800, 0x00}, /* xstart = 0 */
337 	{0x3801, 0x00}, /* xstart */
338 	{0x3802, 0x00}, /* ystart = 0 */
339 	{0x3803, 0x00}, /* ystart */
340 	{0x3804, 0x0a}, /* xend = 2623 */
341 	{0x3805, 0x3f}, /* xend */
342 	{0x3806, 0x07}, /* yend = 1955 */
343 	{0x3807, 0xa3}, /* yend */
344 
345 	{0x3808, 0x07}, /* x output size = 1920 */
346 	{0x3809, 0x80}, /* x output size */
347 	{0x380a, 0x06}, /* y output size = 1600 */
348 	{0x380b, 0x40}, /* y output size */
349 
350 	{0x380c, 0x0c}, /* hts = 3224 */
351 	{0x380d, 0x98}, /* hts */
352 	{0x380e, 0x07}, /* vts = 1984 */
353 	{0x380f, 0xc0}, /* vts */
354 	{0x3810, 0x01}, /* isp x win = 436 */
355 	{0x3811, 0x50}, /* isp x win */
356 	{0x3812, 0x00}, /* isp y win = 180 */
357 	{0x3813, 0xb4}, /* isp y win */
358 	{0x3814, 0x11}, /* x inc */
359 	{0x3815, 0x11}, /* y inc */
360 	{0x3820, 0x10}, /* flip off, v bin off */
361 	{0x3821, 0x1e}, /* mirror on, v bin off */
362 	{0x4009, 0x12},
363 	{0x4826, 0x38}, /* gain = 4x */
364 	{0x4837, 0x0a}, /* MIPI global timing */
365 
366 	{0x3500, 0x00},
367 	{0x3501, 0x6f},
368 	{0x3502, 0xb0},
369 	{0x350b, 0xF6},
370 
371 	{0x0100, 0x01},
372 };
373 
374 /*
375  * Here we'll try to encapsulate the changes for just the output
376  * video format.
377  *
378  */
379 static struct regval_list sensor_fmt_raw[] = {
380 };
381 
382 /*
383  * Code for dealing with controls.
384  * fill with different sensor module
385  * different sensor module has different settings here
386  * if not support the follow function ,retrun -EINVAL
387  */
388 
sensor_g_exp(struct v4l2_subdev * sd,__s32 * value)389 static int sensor_g_exp(struct v4l2_subdev *sd, __s32 *value)
390 {
391 	struct sensor_info *info = to_state(sd);
392 
393 	*value = info->exp;
394 	sensor_dbg("sensor_get_exposure = %d\n", info->exp);
395 
396 	return 0;
397 }
398 
399 static unsigned int last_exp;
400 static int ov5658_sensor_vts;
sensor_s_exp(struct v4l2_subdev * sd,unsigned int exp_val)401 static int sensor_s_exp(struct v4l2_subdev *sd, unsigned int exp_val)
402 {
403 	data_type explow, expmid, exphigh;
404 	struct sensor_info *info = to_state(sd);
405 
406 	if (exp_val > ((ov5658_sensor_vts - 16) << 4))
407 		exp_val = (ov5658_sensor_vts - 16) << 4;
408 	if (exp_val < 16)
409 		exp_val = 16;
410 
411 	if (last_exp == exp_val)
412 		return 0;
413 
414 	exphigh = (unsigned char)((exp_val >> 16) & 0x0F);
415 	expmid = (unsigned char)((exp_val >> 8) & 0xFF);
416 	explow = (unsigned char)(exp_val & 0xFF);
417 
418 	sensor_write(sd, 0x3500, exphigh);
419 	sensor_write(sd, 0x3501, expmid);
420 	sensor_write(sd, 0x3502, explow);
421 
422 	sensor_dbg("sensor_s_exp info->exp 0x%x\n", exp_val);
423 	info->exp = exp_val;
424 
425 	last_exp = info->exp;
426 
427 	return 0;
428 }
429 
sensor_g_gain(struct v4l2_subdev * sd,__s32 * value)430 static int sensor_g_gain(struct v4l2_subdev *sd, __s32 *value)
431 {
432 	struct sensor_info *info = to_state(sd);
433 
434 	*value = info->gain;
435 	sensor_dbg("sensor_get_gain = %d\n", info->gain);
436 
437 	return 0;
438 }
439 
440 static unsigned int last_gain;
sensor_s_gain(struct v4l2_subdev * sd,unsigned int gain_val)441 static int sensor_s_gain(struct v4l2_subdev *sd, unsigned int gain_val)
442 {
443 	data_type gainhigh, gainlow;
444 	struct sensor_info *info = to_state(sd);
445 
446 	if (gain_val < 1 * 16)
447 		gain_val = 16;
448 	if (gain_val > 16 * 16 - 1)
449 		gain_val = 16 * 16 - 1;
450 
451 	if (last_gain == gain_val)
452 		return 0;
453 
454 	gainhigh = (unsigned char)((gain_val >> 8) & 0xFF);
455 	gainlow = (unsigned char)(gain_val & 0xFF);
456 
457 	/* sensor_write(sd, 0x350a, gainhigh); */
458 	sensor_write(sd, 0x350b, gainlow);
459 
460 	sensor_print("sensor_s_gain info->gain %d\n", gain_val);
461 	info->gain = gain_val;
462 
463 	last_gain = info->gain;
464 
465 	return 0;
466 }
467 
sensor_s_exp_gain(struct v4l2_subdev * sd,struct sensor_exp_gain * exp_gain)468 static int sensor_s_exp_gain(struct v4l2_subdev *sd,
469 			     struct sensor_exp_gain *exp_gain)
470 {
471 	sensor_s_exp(sd, exp_gain->exp_val);
472 	sensor_s_gain(sd, exp_gain->gain_val);
473 
474 	return 0;
475 }
476 
sensor_s_sw_stby(struct v4l2_subdev * sd,int on_off)477 static int sensor_s_sw_stby(struct v4l2_subdev *sd, int on_off)
478 {
479 	int ret;
480 	data_type rdval;
481 
482 	ret = sensor_read(sd, 0x0100, &rdval);
483 
484 	if (ret != 0)
485 		return ret;
486 
487 	if (on_off == STBY_ON)
488 		ret = sensor_write(sd, 0x0100, rdval & 0xfe);
489 	else
490 		ret = sensor_write(sd, 0x0100, rdval | 0x01);
491 
492 	return ret;
493 }
494 
495 /*
496  * Stuff that knows about the sensor.
497  */
sensor_power(struct v4l2_subdev * sd,int on)498 static int sensor_power(struct v4l2_subdev *sd, int on)
499 {
500 	int ret;
501 
502 	ret = 0;
503 	switch (on) {
504 	case STBY_ON:
505 		ret = sensor_s_sw_stby(sd, 1);
506 		if (ret < 0)
507 			sensor_err("soft stby falied!\n");
508 		usleep_range(10000, 12000);
509 
510 		cci_lock(sd);
511 		/* standby on io */
512 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
513 		cci_unlock(sd);
514 		/* inactive mclk after stadby in */
515 		vin_set_mclk(sd, OFF);
516 		break;
517 	case STBY_OFF:
518 		cci_lock(sd);
519 
520 		vin_set_mclk_freq(sd, MCLK);
521 		vin_set_mclk(sd, ON);
522 		usleep_range(10000, 12000);
523 
524 		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
525 		usleep_range(10000, 12000);
526 
527 		cci_unlock(sd);
528 		ret = sensor_s_sw_stby(sd, 0);
529 		if (ret < 0)
530 			sensor_err("soft stby off falied!\n");
531 		usleep_range(10000, 12000);
532 
533 		break;
534 	case PWR_ON:
535 		cci_lock(sd);
536 
537 		vin_gpio_set_status(sd, PWDN, 1);
538 		vin_gpio_set_status(sd, RESET, 1);
539 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
540 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
541 
542 		vin_set_mclk(sd, ON);
543 		vin_set_mclk_freq(sd, MCLK);
544 
545 		vin_set_pmu_channel(sd, IOVDD, ON);
546 		usleep_range(1000, 1100);	/* T0 */
547 		vin_set_pmu_channel(sd, AVDD, ON);
548 
549 		usleep_range(4000, 5100);	/* T2 */
550 
551 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
552 		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
553 		usleep_range(8000, 10000);
554 
555 		cci_unlock(sd);
556 		break;
557 	case PWR_OFF:
558 		cci_lock(sd);
559 
560 		vin_set_mclk(sd, OFF);
561 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
562 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
563 
564 		vin_set_pmu_channel(sd, AVDD, OFF);
565 		vin_set_pmu_channel(sd, IOVDD, OFF);
566 
567 		vin_gpio_set_status(sd, RESET, 0);
568 		vin_gpio_set_status(sd, PWDN, 0);
569 
570 		cci_unlock(sd);
571 		break;
572 	default:
573 		return -EINVAL;
574 	}
575 
576 	return 0;
577 }
578 
sensor_reset(struct v4l2_subdev * sd,u32 val)579 static int sensor_reset(struct v4l2_subdev *sd, u32 val)
580 {
581 	sensor_print("%s val %d\n", __func__, val);
582 
583 	switch (val) {
584 	case 0:
585 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
586 		usleep_range(10000, 12000);
587 		break;
588 	case 1:
589 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
590 		usleep_range(10000, 12000);
591 		break;
592 	default:
593 		return -EINVAL;
594 	}
595 	return 0;
596 }
597 
sensor_detect(struct v4l2_subdev * sd)598 static int sensor_detect(struct v4l2_subdev *sd)
599 {
600 	data_type rdval = 0;
601 
602 	sensor_read(sd, 0x300A, &rdval);
603 	if (rdval != (V4L2_IDENT_SENSOR >> 8))
604 		return -ENODEV;
605 
606 	sensor_read(sd, 0x300B, &rdval);
607 	if (rdval != (V4L2_IDENT_SENSOR & 0xff))
608 		return -ENODEV;
609 
610 	return 0;
611 }
612 
sensor_init(struct v4l2_subdev * sd,u32 val)613 static int sensor_init(struct v4l2_subdev *sd, u32 val)
614 {
615 	int ret;
616 	struct sensor_info *info = to_state(sd);
617 
618 	sensor_dbg("sensor_init\n");
619 
620 	/*Make sure it is a target sensor */
621 	ret = sensor_detect(sd);
622 	if (ret) {
623 		sensor_err("chip found is not an target chip.\n");
624 		return ret;
625 	}
626 
627 	info->focus_status = 0;
628 	info->low_speed = 0;
629 	info->width = 1920;
630 	info->height = 1600;
631 	info->hflip = 0;
632 	info->vflip = 0;
633 	info->exp = 0;
634 	info->gain = 0;
635 
636 	info->tpf.numerator = 1;
637 	info->tpf.denominator = SENSOR_FRAME_RATE; /* 30 fps */
638 	info->preview_first_flag = 1;
639 
640 	return 0;
641 }
642 
sensor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)643 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
644 {
645 	int ret = 0;
646 	struct sensor_info *info = to_state(sd);
647 
648 	switch (cmd) {
649 	case GET_CURRENT_WIN_CFG:
650 		if (info->current_wins) {
651 			memcpy(arg, info->current_wins,
652 			       sizeof(struct sensor_win_size));
653 			ret = 0;
654 		} else {
655 			sensor_err("empty wins!\n");
656 			ret = -1;
657 		}
658 		break;
659 	case SET_FPS:
660 		ret = 0;
661 		break;
662 	case VIDIOC_VIN_SENSOR_EXP_GAIN:
663 		ret = sensor_s_exp_gain(sd, (struct sensor_exp_gain *)arg);
664 		break;
665 	case VIDIOC_VIN_SENSOR_CFG_REQ:
666 		sensor_cfg_req(sd, (struct sensor_config *)arg);
667 		break;
668 	default:
669 		return -EINVAL;
670 	}
671 	return ret;
672 }
673 
674 /*
675  * Store information about the video data format.
676  */
677 static struct sensor_format_struct sensor_formats[] = {
678 	{
679 		.desc = "Raw RGB Bayer",
680 		.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
681 		.regs = sensor_fmt_raw,
682 		.regs_size = ARRAY_SIZE(sensor_fmt_raw),
683 		.bpp = 1
684 	},
685 };
686 
687 #define N_FMTS ARRAY_SIZE(sensor_formats)
688 
689 /*
690  * Then there is the issue of window sizes.  Try to capture the info here.
691  */
692 static struct sensor_win_size sensor_win_sizes[] = {
693 	{
694 		.width      = 1920,
695 		.height     = 1600,
696 		.hoffset    = 0,
697 		.voffset    = 0,
698 		.hts        = 3224,
699 		.vts        = 1984,
700 		.pclk       = 192 * 1000 * 1000,
701 		.mipi_bps   = 840 * 1000 * 1000,
702 		.fps_fixed  = 30,
703 		.bin_factor = 1,
704 		.intg_min   = 1 << 4,
705 		.intg_max   = (1984) << 4,
706 		.gain_min   = 1 << 4,
707 		.gain_max   = 16 << 4,
708 		.regs       = sensor_2592x1944_regs,
709 		.regs_size  = ARRAY_SIZE(sensor_2592x1944_regs),
710 		.set_size   = NULL,
711 	},
712 };
713 
714 #define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))
715 
sensor_g_mbus_config(struct v4l2_subdev * sd,struct v4l2_mbus_config * cfg)716 static int sensor_g_mbus_config(struct v4l2_subdev *sd,
717 				struct v4l2_mbus_config *cfg)
718 {
719 	cfg->type = V4L2_MBUS_CSI2_DPHY;
720 	cfg->flags = 0 | V4L2_MBUS_CSI2_2_LANE | V4L2_MBUS_CSI2_CHANNEL_0;
721 
722 	return 0;
723 }
724 
sensor_g_ctrl(struct v4l2_ctrl * ctrl)725 static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
726 {
727 	struct sensor_info *info =
728 		container_of(ctrl->handler, struct sensor_info, handler);
729 	struct v4l2_subdev *sd = &info->sd;
730 
731 	switch (ctrl->id) {
732 	case V4L2_CID_GAIN:
733 		return sensor_g_gain(sd, &ctrl->val);
734 	case V4L2_CID_EXPOSURE:
735 		return sensor_g_exp(sd, &ctrl->val);
736 	}
737 	return -EINVAL;
738 }
739 
sensor_s_ctrl(struct v4l2_ctrl * ctrl)740 static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
741 {
742 	struct sensor_info *info =
743 		container_of(ctrl->handler, struct sensor_info, handler);
744 	struct v4l2_subdev *sd = &info->sd;
745 
746 	switch (ctrl->id) {
747 	case V4L2_CID_GAIN:
748 		return sensor_s_gain(sd, ctrl->val);
749 	case V4L2_CID_EXPOSURE:
750 		return sensor_s_exp(sd, ctrl->val);
751 	}
752 	return -EINVAL;
753 }
754 
sensor_reg_init(struct sensor_info * info)755 static int sensor_reg_init(struct sensor_info *info)
756 {
757 	int ret;
758 	struct v4l2_subdev *sd = &info->sd;
759 	struct sensor_format_struct *sensor_fmt = info->fmt;
760 	struct sensor_win_size *wsize = info->current_wins;
761 
762 	ret = sensor_write_array(sd, sensor_default_regs,
763 				 ARRAY_SIZE(sensor_default_regs));
764 	if (ret < 0) {
765 		sensor_err("write sensor_default_regs error\n");
766 		return ret;
767 	}
768 
769 	sensor_dbg("sensor_reg_init\n");
770 
771 	sensor_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size);
772 
773 	if (wsize->regs)
774 		sensor_write_array(sd, wsize->regs, wsize->regs_size);
775 
776 	if (wsize->set_size)
777 		wsize->set_size(sd);
778 
779 	info->width = wsize->width;
780 	info->height = wsize->height;
781 	ov5658_sensor_vts = wsize->vts;
782 	info->exp = 0;
783 	info->gain = 0;
784 
785 	sensor_print("s_fmt set width = %d, height = %d\n", wsize->width,
786 		     wsize->height);
787 
788 	return 0;
789 }
790 
sensor_s_stream(struct v4l2_subdev * sd,int enable)791 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
792 {
793 	struct sensor_info *info = to_state(sd);
794 
795 	sensor_print("%s on = %d, %d*%d fps: %d code: %x\n", __func__, enable,
796 		     info->current_wins->width, info->current_wins->height,
797 		     info->current_wins->fps_fixed, info->fmt->mbus_code);
798 
799 	last_exp = 0;
800 	last_gain = 0;
801 
802 	if (!enable) {
803 		/* stream off */
804 		sensor_write(sd, 0x0100, 0x00);
805 		return 0;
806 	}
807 
808 	return sensor_reg_init(info);
809 }
810 
811 /* ----------------------------------------------------------------------- */
812 
813 static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
814 	.g_volatile_ctrl = sensor_g_ctrl,
815 	.s_ctrl = sensor_s_ctrl,
816 };
817 
818 static const struct v4l2_subdev_core_ops sensor_core_ops = {
819 	.reset = sensor_reset,
820 	.init = sensor_init,
821 	.s_power = sensor_power,
822 	.ioctl = sensor_ioctl,
823 #ifdef CONFIG_COMPAT
824 	.compat_ioctl32 = sensor_compat_ioctl32,
825 #endif
826 };
827 
828 static const struct v4l2_subdev_video_ops sensor_video_ops = {
829 	.s_stream = sensor_s_stream,
830 	.g_mbus_config = sensor_g_mbus_config,
831 };
832 
833 static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
834 	.enum_mbus_code = sensor_enum_mbus_code,
835 	.enum_frame_size = sensor_enum_frame_size,
836 	.get_fmt = sensor_get_fmt,
837 	.set_fmt = sensor_set_fmt,
838 };
839 
840 static const struct v4l2_subdev_ops sensor_ops = {
841 	.core = &sensor_core_ops,
842 	.video = &sensor_video_ops,
843 	.pad = &sensor_pad_ops,
844 };
845 
846 /* ----------------------------------------------------------------------- */
847 static struct cci_driver cci_drv = {
848 	.name = SENSOR_NAME,
849 	.addr_width = CCI_BITS_16,
850 	.data_width = CCI_BITS_8,
851 };
852 
853 static const struct v4l2_ctrl_config sensor_custom_ctrls[] = {
854 	{
855 		.ops = &sensor_ctrl_ops,
856 		.id = V4L2_CID_FRAME_RATE,
857 		.name = "frame rate",
858 		.type = V4L2_CTRL_TYPE_INTEGER,
859 		.min = 15,
860 		.max = 120,
861 		.step = 1,
862 		.def = 120,
863 	},
864 };
865 
sensor_init_controls(struct v4l2_subdev * sd,const struct v4l2_ctrl_ops * ops)866 static int sensor_init_controls(struct v4l2_subdev *sd,
867 				const struct v4l2_ctrl_ops *ops)
868 {
869 	struct sensor_info *info = to_state(sd);
870 	struct v4l2_ctrl_handler *handler = &info->handler;
871 	struct v4l2_ctrl *ctrl;
872 	int ret = 0;
873 
874 	v4l2_ctrl_handler_init(handler, 2);
875 
876 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 1 * 16, 16 * 16, 1, 16);
877 	ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE,
878 				 3 * 16, 65536 * 16, 1, 3 * 16);
879 	if (ctrl)
880 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
881 
882 	if (handler->error) {
883 		ret = handler->error;
884 		v4l2_ctrl_handler_free(handler);
885 	}
886 
887 	sd->ctrl_handler = handler;
888 
889 	return ret;
890 }
891 
sensor_probe(struct i2c_client * client,const struct i2c_device_id * id)892 static int sensor_probe(struct i2c_client *client,
893 			const struct i2c_device_id *id)
894 {
895 	struct v4l2_subdev *sd;
896 	struct sensor_info *info;
897 
898 	info = kzalloc(sizeof(*info), GFP_KERNEL);
899 	if (!info)
900 		return -ENOMEM;
901 
902 	sd = &info->sd;
903 
904 	cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv);
905 	sensor_init_controls(sd, &sensor_ctrl_ops);
906 
907 	mutex_init(&info->lock);
908 
909 	info->fmt = &sensor_formats[0];
910 	info->fmt_pt = &sensor_formats[0];
911 	info->win_pt = &sensor_win_sizes[0];
912 	info->fmt_num = N_FMTS;
913 	info->win_size_num = N_WIN_SIZES;
914 	info->sensor_field = V4L2_FIELD_NONE;
915 	info->stream_seq = MIPI_BEFORE_SENSOR;
916 	info->af_first_flag = 1;
917 	info->exp = 0;
918 	info->gain = 0;
919 
920 	return 0;
921 }
922 
sensor_remove(struct i2c_client * client)923 static int sensor_remove(struct i2c_client *client)
924 {
925 	struct v4l2_subdev *sd;
926 
927 	sd = cci_dev_remove_helper(client, &cci_drv);
928 
929 	kfree(to_state(sd));
930 	return 0;
931 }
932 
933 static const struct i2c_device_id sensor_id[] = {
934 	{SENSOR_NAME, 0},
935 	{}
936 };
937 
938 MODULE_DEVICE_TABLE(i2c, sensor_id);
939 
940 static struct i2c_driver sensor_driver = {
941 	.driver = {
942 		.owner = THIS_MODULE,
943 		.name = SENSOR_NAME,
944 	},
945 	.probe = sensor_probe,
946 	.remove = sensor_remove,
947 	.id_table = sensor_id,
948 };
949 
init_sensor(void)950 static __init int init_sensor(void)
951 {
952 	return cci_dev_init_helper(&sensor_driver);
953 }
954 
exit_sensor(void)955 static __exit void exit_sensor(void)
956 {
957 	cci_dev_exit_helper(&sensor_driver);
958 }
959 
960 module_init(init_sensor);
961 module_exit(exit_sensor);
962