• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * A V4L2 driver for TP9930 YUV cameras.
3  *
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/videodev2.h>
16 #include <linux/clk.h>
17 #include <media/v4l2-device.h>
18 #include <media/v4l2-mediabus.h>
19 #include <linux/io.h>
20 
21 #include "camera.h"
22 #include "sensor_helper.h"
23 
24 //	Mode setting
25 /*
26  *	mode_set_720p_2ch
27  *	mode_set_720p_4ch
28  *	mode_set_1080p_2ch
29  *	mode_set_1080p_4ch
30  */
31 
32 #define mode_set_720p_4ch
33 
34 MODULE_AUTHOR("zw");
35 MODULE_DESCRIPTION("A low-level driver for TP9930 sensors");
36 MODULE_LICENSE("GPL");
37 
38 #define MCLK				(27 * 1000 * 1000)
39 #define CLK_POL				V4L2_MBUS_PCLK_SAMPLE_FALLING
40 #define DOUBLE_CLK_POL		(V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_PCLK_SAMPLE_RISING)
41 #define V4L2_IDENT_SENSOR	0x3228
42 
43 #define DBG_INFO(format, args...) (printk("[TP9930 INFO] LINE:%04d-->%s:"format, __LINE__, __func__, ##args))
44 #define DBG_ERR(format, args...)  (printk("[TP9930 ERR] LINE:%04d-->%s:"format, __LINE__, __func__, ##args))
45 
46 
47 /*
48  * Our nominal (default) frame rate.
49  */
50 #define SENSOR_FRAME_RATE 25
51 
52 /*
53  * The TP9920 sits on i2c with ID 0x88 or 0x8a
54  * SAD-low:0x88 SAD-high:0x8a
55  */
56 #define I2C_ADDR 0x88
57 #define SENSOR_NAME "tp9930"
58 static struct regval_list reg_1080p25_2ch[] = {
59 	{0x40, 0x00},
60 	{0x4d, 0x00},
61 	{0x4e, 0x00},
62 	{0x44, 0x47},
63 	{0xf4, 0x80},
64 	{0xffff, 0xa0},
65 	{0x44, 0x07},
66 	{0x3b, 0x20},
67 	{0x3d, 0xe0},
68 	{0x3d, 0x60},
69 	{0x3b, 0x25},
70 	{0x40, 0x40},
71 	{0x7a, 0x20},
72 	{0x3c, 0x20},
73 	{0x3c, 0x00},
74 	{0x7a, 0x25},
75 	{0x40, 0x00},
76 	{0x44, 0x57},
77 	{0x43, 0x12},
78 	{0x45, 0x09},
79 	{0xf4, 0x80},
80 	{0x44, 0x17},
81 //video setting
82 	{0x40, 0x04},
83 	{0x02, 0x44},//set 16bit 1120 output
84 	{0x07, 0x80},
85 	{0x0b, 0xc0},
86 	{0x0c, 0x03},
87 	{0x0d, 0x73},
88 	{0x10, 0x00},
89 	{0x11, 0x40},
90 	{0x12, 0x40},
91 	{0x13, 0x00},
92 	{0x14, 0x00},
93 	{0x15, 0x01},
94 	{0x16, 0xf0},
95 	{0x17, 0x80},
96 	{0x18, 0x29},
97 	{0x19, 0x38},
98 	{0x1a, 0x47},
99 	{0x1c, 0x0a},
100 	{0x1d, 0x50},
101 	{0x20, 0x3c},
102 	{0x21, 0x46},
103 	{0x22, 0x36},
104 	{0x23, 0x3c},
105 	{0x24, 0x04},
106 	{0x25, 0xfe},
107 	{0x26, 0x0d},
108 	{0x27, 0x2d},
109 	{0x28, 0x00},
110 	{0x29, 0x48},
111 	{0x2a, 0x30},
112 	{0x2b, 0x60},
113 	{0x2c, 0x3a},
114 	{0x2d, 0x54},
115 	{0x2e, 0x40},
116 	{0x30, 0xa5},
117 	{0x31, 0x86},
118 	{0x32, 0xfb},
119 	{0x33, 0x60},
120 	{0x35, 0x05},
121 	{0x36, 0xca},
122 	{0x38, 0x00},
123 	{0x39, 0x1c},
124 	{0x3a, 0x32},
125 	{0x3b, 0x26},
126 //output format
127 	{0x4f, 0x03},
128 	{0x50, 0x00},
129 	{0x52, 0x00},
130 	{0xf1, 0x04},
131 	{0xf2, 0x77},
132 	{0xf3, 0x77},
133 	{0xf4, 0x0c},//close the 3-4 channel power down
134 	{0xf5, 0x00},
135 	{0xf6, 0x01},
136 	{0xf8, 0x45},
137 	{0xfa, 0x88},
138 	{0xfb, 0x88},
139 //channel ID
140 	{0x40, 0x00},
141 	{0x34, 0x10},
142 	{0x40, 0x01},
143 	{0x34, 0x11},
144 	{0x40, 0x02},
145 	{0x34, 0x10},
146 	{0x3d, 0xff}, //close the 3 channel
147 	{0x40, 0x03},
148 	{0x34, 0x11},
149 	{0x3d, 0xff}, //close the 4 channel
150 //output enable
151 	{0x4d, 0x07},
152 	{0x4e, 0x55} //0x05
153 };
154 
155 static struct regval_list reg_1080p25_4ch[] = {
156 	{0x40, 0x00},
157 	{0x4d, 0x00},
158 	{0x4e, 0x00},
159 	{0x44, 0x07},
160 	{0xf4, 0xa0},
161 	{0x40, 0x04},
162 	{0x3b, 0x20},
163 	{0x3d, 0xe0},
164 	{0x3d, 0x60},
165 	{0x3b, 0x25},
166 	{0x40, 0x40},
167 	{0x7a, 0x20},
168 	{0x3c, 0x20},
169 	{0x3c, 0x00},
170 	{0x7a, 0x25},
171 	{0x40, 0x00},
172 	{0x44, 0x07},
173 	{0x43, 0x12},
174 	{0x45, 0x09},
175 	{0xf4, 0xa0},
176 //video setting
177 	{0x40, 0x04},
178 	{0x02, 0x44},
179 	{0x05, 0x00},
180 	{0x06, 0x32},
181 	{0x07, 0x80},
182 	{0x08, 0x00},
183 	{0x09, 0x24},
184 	{0x0a, 0x48},
185 	{0x0b, 0xc0},
186 	{0x0c, 0x03},
187 	{0x0d, 0x73},
188 	{0x10, 0x00},
189 	{0x11, 0x40},
190 	{0x12, 0x40},
191 	{0x13, 0x00},
192 	{0x14, 0x00},
193 	{0x15, 0x01},
194 	{0x16, 0xf0},
195 	{0x17, 0x80},
196 	{0x18, 0x29},
197 	{0x19, 0x38},
198 	{0x1a, 0x47},
199 	{0x1c, 0x0a},
200 	{0x1d, 0x50},
201 	{0x20, 0x3c},
202 	{0x21, 0x46},
203 	{0x22, 0x36},
204 	{0x23, 0x3c},
205 	{0x24, 0x04},
206 	{0x25, 0xfe},
207 	{0x26, 0x0d},
208 	{0x27, 0x2d},
209 	{0x28, 0x00},
210 	{0x29, 0x48},
211 	{0x2a, 0x30},
212   //{0x2a, 0x3c},
213 	{0x2b, 0x60},
214 	{0x2c, 0x3a},
215 	{0x2d, 0x54},
216 	{0x2e, 0x40},
217 	{0x30, 0xa5},
218 	{0x31, 0x86},
219 	{0x32, 0xfb},
220 	{0x33, 0x60},
221 	{0x35, 0x05},
222 	{0x36, 0xca},
223 	{0x38, 0x00},
224 	{0x39, 0x1c},
225 	{0x3a, 0x32},
226 	{0x3b, 0x26},
227 //channel ID
228 	{0x40, 0x00},
229 	{0x34, 0x10},
230 	{0x40, 0x01},
231 	{0x34, 0x11},
232 	{0x40, 0x02},
233 	{0x34, 0x12},
234 	{0x40, 0x03},
235 	{0x34, 0x13},
236 //output format
237 	{0x4f, 0x03},
238 	{0x50, 0xb2},
239 	{0x52, 0xf6},
240 	{0xf1, 0x04},
241 	{0xf2, 0x11},
242 	{0xf3, 0x11},
243 	{0xf5, 0x00},
244 	{0xf6, 0x10},
245 	{0xf8, 0x54},
246 	{0xfa, 0x88},
247 	{0xfb, 0x88},
248 //output enable
249 	{0x4d, 0x07},
250 	{0x4e, 0x55},
251 
252 };
253 static struct regval_list reg_720p25_2ch[] = {
254 	{0x40, 0x04},
255 	{0x3b, 0x20},
256 	{0x3d, 0xe0},
257 	{0x3d, 0x60},
258 	{0x3b, 0x25},
259 	{0x40, 0x40},
260 	{0x7a, 0x20},
261 	{0x3c, 0x20},
262 	{0x3c, 0x00},
263 	{0x7a, 0x25},
264 	{0x40, 0x00},
265 	{0x44, 0x17},
266 	{0x43, 0x12},
267 	{0x45, 0x09},
268 //video setting
269 	{0x40, 0x04},
270 	{0x02, 0xce},
271 	{0x05, 0x00},
272 	{0x06, 0x32},
273 	{0x07, 0xc0},
274 	{0x08, 0x00},
275 	{0x09, 0x24},
276 	{0x0a, 0x48},
277 	{0x0b, 0xc0},
278 	{0x0c, 0x13},
279 	{0x0d, 0x71},
280 	{0x0e, 0x00},
281 	{0x0f, 0x00},
282 	{0x10, 0x00},
283 	{0x11, 0x40},
284 	{0x12, 0x40},
285 	{0x13, 0x00},
286 	{0x14, 0x00},
287 	{0x15, 0x13},
288 	{0x16, 0x16},
289 	{0x17, 0x00},
290 	{0x18, 0x19},
291 	{0x19, 0xd0},
292 	{0x1a, 0x25},
293 	{0x1b, 0x00},
294 	{0x1c, 0x07},
295 	{0x1d, 0xbc},
296 	{0x1e, 0x60},
297 	{0x1f, 0x06},
298 	{0x20, 0x40},
299 	{0x21, 0x46},
300 	{0x22, 0x36},
301 	{0x23, 0x3c},
302 	{0x24, 0x04},
303 	{0x25, 0xfe},
304 	{0x26, 0x01},
305 	{0x27, 0x2d},
306 	{0x28, 0x00},
307 	{0x29, 0x48},
308 	{0x2a, 0x30},
309 	{0x2b, 0x60},
310 	{0x2c, 0x3a},
311 	{0x2d, 0x5a},
312 	{0x2e, 0x40},
313 	{0x2f, 0x06},
314 	{0x30, 0x9e},
315 	{0x31, 0x20},
316 	{0x32, 0x01},
317 	{0x33, 0x90},
318 	{0x35, 0x25},
319 	{0x36, 0xca},
320 	{0x37, 0x00},
321 	{0x38, 0x00},
322 	{0x39, 0x18},
323 	{0x3a, 0x32},
324 	{0x3b, 0x26},
325 	{0x3c, 0x00},
326 	{0x3d, 0x60},
327 	{0x3e, 0x00},
328 	{0x3f, 0x00},
329 //channel ID
330 	{0x40, 0x00},
331 	{0x34, 0x10},
332 	{0x40, 0x01},
333 	{0x34, 0x11},
334 	{0x40, 0x02},
335 	{0x34, 0x10},
336 	{0x40, 0x03},
337 	{0x34, 0x11},
338 //output format
339 	{0x4f, 0x03},
340 	{0x50, 0x00},
341 	{0x52, 0x00},
342 	{0xf1, 0x04},
343 	{0xf2, 0x77},
344 	{0xf3, 0x77},
345 	{0xf4, 0x00},
346 	{0xf5, 0x0f},
347 	{0xf6, 0x10},
348 	{0xf8, 0x23},
349 	{0xfa, 0x88},
350 	{0xfb, 0x88},
351 //output enable
352 	{0x4d, 0x07},
353 	{0x4e, 0x05},
354 
355 };
356 
357 static struct regval_list reg_720p25_4ch[] = {
358 	{0x40, 0x04},
359 	{0x3b, 0x20},
360 	{0x3d, 0xe0},
361 	{0x3d, 0x60},
362 	{0x3b, 0x25},
363 	{0x40, 0x40},
364 	{0x7a, 0x20},
365 	{0x3c, 0x20},
366 	{0x3c, 0x00},
367 	{0x7a, 0x25},
368 	{0x40, 0x00},
369 	{0x44, 0x17},
370 	{0x43, 0x12},
371 	{0x45, 0x09},
372 	{0xffff, 0x64},
373 //video setting
374 	{0x40, 0x04},
375 	{0x02, 0x4e},
376 	{0x05, 0x00},
377 	{0x06, 0x32},
378 	{0x07, 0xc0},
379 	{0x08, 0x00},
380 	{0x09, 0x24},
381 	{0x0a, 0x48},
382 	{0x0b, 0xc0},
383 	{0x0c, 0x13},
384 	{0x0d, 0x71},
385 	{0x0e, 0x00},
386 	{0x0f, 0x00},
387 	{0x10, 0x00},
388 	{0x11, 0x40},
389 	{0x12, 0x40},
390 	{0x13, 0x00},
391 	{0x14, 0x00},
392 	{0x15, 0x13},
393 	{0x16, 0x16},
394 	{0x17, 0x00},
395 	{0x18, 0x19},
396 	{0x19, 0xd0},
397 	{0x1a, 0x25},
398 	{0x1b, 0x00},
399 	{0x1c, 0x07},
400 	{0x1d, 0xbc},
401 	{0x1e, 0x60},
402 	{0x1f, 0x06},
403 	{0x20, 0x40},
404 	{0x21, 0x46},
405 	{0x22, 0x36},
406 	{0x23, 0x3c},
407 	{0x24, 0x04},
408 	{0x25, 0xfe},
409 	{0x26, 0x01},
410 	{0x27, 0x2d},
411 	{0x28, 0x00},
412 	{0x29, 0x48},
413 	{0x2a, 0x30},
414 	{0x2b, 0x60},
415 	{0x2c, 0x3a},
416 	{0x2d, 0x5A},
417 	{0x2e, 0x40},
418 	{0x2f, 0x06},
419 	{0x30, 0x9e},
420 	{0x31, 0x20},
421 	{0x32, 0x01},
422 	{0x33, 0x90},
423 	{0x35, 0x25},
424 	{0x36, 0xca},
425 	{0x37, 0x00},
426 	{0x38, 0x00},
427 	{0x39, 0x18},
428 	{0x3a, 0x32},
429 	{0x3b, 0x26},
430 	{0x3c, 0x00},
431 	{0x3d, 0x60},
432 	{0x3e, 0x00},
433 	{0x3f, 0x00},
434 //channel ID
435 	{0x40, 0x00},
436 	{0x34, 0x10},
437 	{0x40, 0x01},
438 	{0x34, 0x11},
439 	{0x40, 0x02},
440 	{0x34, 0x12},
441 	{0x40, 0x03},
442 	{0x34, 0x13},
443 //output format
444 	{0x4f, 0x03},
445 	{0x50, 0xA3},
446 	{0x52, 0xE7},
447 	{0xf1, 0x04},
448 	{0xf2, 0x33},
449 	{0xf3, 0x33},
450 	{0xf4, 0x00},
451 	{0xf5, 0x0f},
452 	{0xf6, 0x10},
453 	{0xf8, 0x54},
454 	{0xfa, 0x88},
455 	{0xfb, 0x88},
456 //output enable
457 	{0x4d, 0x07},
458 	{0x4e, 0x05},
459 };
460 
sensor_s_sw_stby(struct v4l2_subdev * sd,int on_off)461 static int sensor_s_sw_stby(struct v4l2_subdev *sd, int on_off)
462 {
463 	if (on_off)
464 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
465 	else
466 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
467 	return 0;
468 }
469 
sensor_power(struct v4l2_subdev * sd,int on)470 static int sensor_power(struct v4l2_subdev *sd, int on)
471 {
472 	switch (on) {
473 	case STBY_ON:
474 		sensor_dbg("CSI_SUBDEV_STBY_ON!\n");
475 		sensor_s_sw_stby(sd, ON);
476 		break;
477 	case STBY_OFF:
478 		sensor_dbg("CSI_SUBDEV_STBY_OFF!\n");
479 		sensor_s_sw_stby(sd, OFF);
480 		break;
481 	case PWR_ON:
482 		sensor_dbg("CSI_SUBDEV_PWR_ON!\n");
483 		cci_lock(sd);
484 		vin_gpio_set_status(sd, PWDN, 1);
485 		vin_gpio_set_status(sd, RESET, 1);
486 		vin_gpio_set_status(sd, SM_HS, 1);
487 		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
488 		vin_gpio_write(sd, SM_HS, CSI_GPIO_HIGH);
489 		vin_set_mclk(sd, ON);
490 		usleep_range(10000, 12000);
491 		vin_set_pmu_channel(sd, IOVDD, ON);
492 		vin_set_pmu_channel(sd, AVDD, ON);
493 		vin_set_pmu_channel(sd, DVDD, ON);
494 		usleep_range(10000, 12000);
495 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
496 		usleep_range(30000, 31000);
497 		cci_unlock(sd);
498 		break;
499 	case PWR_OFF:
500 		sensor_dbg("CSI_SUBDEV_PWR_OFF!\n");
501 		cci_lock(sd);
502 		vin_set_mclk(sd, OFF);
503 		vin_set_pmu_channel(sd, DVDD, OFF);
504 		vin_set_pmu_channel(sd, AVDD, OFF);
505 		vin_set_pmu_channel(sd, IOVDD, OFF);
506 		usleep_range(100, 120);
507 		vin_gpio_write(sd, SM_HS, CSI_GPIO_LOW);
508 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
509 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
510 		vin_gpio_set_status(sd, SM_HS, 0);
511 		vin_gpio_set_status(sd, RESET, 0);
512 		vin_gpio_set_status(sd, PWDN, 0);
513 		cci_unlock(sd);
514 		break;
515 	default:
516 		return -EINVAL;
517 	}
518 
519 	return 0;
520 }
521 
sensor_reset(struct v4l2_subdev * sd,u32 val)522 static int sensor_reset(struct v4l2_subdev *sd, u32 val)
523 {
524 	vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
525 	usleep_range(5000, 6000);
526 	vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
527 	usleep_range(5000, 6000);
528 	return 0;
529 }
530 
sensor_detect(struct v4l2_subdev * sd)531 static int sensor_detect(struct v4l2_subdev *sd)
532 {
533 	data_type rdval, rdval1, rdval2;
534 	int cnt = 0;
535 
536 	rdval = 0;
537 	rdval1 = 0;
538 	rdval2 = 0;
539 	DBG_INFO("\n");
540 	sensor_read(sd, 0xfe, &rdval1);
541 	sensor_read(sd, 0xff, &rdval2);
542 	rdval = ((rdval2 << 8) & 0xff00) | rdval1;
543 	pr_err("V4L2_IDENT_SENSOR = 0x%x\n", rdval);
544 
545 	while ((rdval != V4L2_IDENT_SENSOR) && (cnt < 5)) {
546 		sensor_read(sd, 0xfe, &rdval1);
547 		sensor_read(sd, 0xff, &rdval2);
548 		rdval = ((rdval2 << 8) & 0xff00) | rdval1;
549 		DBG_INFO("retry = %d, V4L2_IDENT_SENSOR = %x\n", cnt,
550 			     rdval);
551 		cnt++;
552 	}
553 
554 	if (rdval != V4L2_IDENT_SENSOR)
555 		return -ENODEV;
556 	DBG_INFO("tp9930 detect ok !!!");
557 	return 0;
558 }
559 
sensor_init(struct v4l2_subdev * sd,u32 val)560 static int sensor_init(struct v4l2_subdev *sd, u32 val)
561 {
562 	int ret;
563 	struct sensor_info *info = to_state(sd);
564 
565 	sensor_dbg("sensor_init\n");
566 
567 	/*Make sure it is a target sensor */
568 	ret = sensor_detect(sd);
569 	if (ret) {
570 		sensor_err("chip found is not an target chip.\n");
571 		return ret;
572 	}
573 
574 	info->focus_status = 0;
575 	info->low_speed = 0;
576 	info->width = HD720_WIDTH;
577 	info->height = HD720_HEIGHT;
578 	info->hflip = 0;
579 	info->vflip = 0;
580 
581 	info->tpf.numerator = 1;
582 	info->tpf.denominator = 25;	/* 25fps */
583 
584 	info->preview_first_flag = 1;
585 	return 0;
586 }
587 
sensor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)588 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
589 {
590 	int ret = 0;
591 	struct sensor_info *info = to_state(sd);
592 
593 	switch (cmd) {
594 	case GET_CURRENT_WIN_CFG:
595 		if (info->current_wins != NULL) {
596 			memcpy(arg,
597 			       info->current_wins,
598 			       sizeof(struct sensor_win_size));
599 			ret = 0;
600 		} else {
601 			sensor_err("empty wins!\n");
602 			ret = -1;
603 		}
604 		break;
605 	case SET_FPS:
606 		break;
607 	case VIDIOC_VIN_SENSOR_CFG_REQ:
608 		sensor_cfg_req(sd, (struct sensor_config *)arg);
609 		break;
610 	default:
611 		return -EINVAL;
612 	}
613 	return ret;
614 }
615 
616 /*
617  * Store information about the video data format.
618  */
619 static struct sensor_format_struct sensor_formats[] = {
620 	#ifdef mode_set_1080p_4ch
621 	{
622 		.desc = "BT1120 4CH 1080P",
623 		.mbus_code = MEDIA_BUS_FMT_YUYV8_1X16,
624 		.regs = NULL,
625 		.regs_size = 0,
626 		.bpp = 2,
627 	},
628 	#endif
629 
630 	#ifdef mode_set_1080p_2ch
631 	{
632 		.desc = "BT1120 2CH 1080P",
633 		.mbus_code = MEDIA_BUS_FMT_YUYV8_1X16,
634 		.regs = NULL,
635 		.regs_size = 0,
636 		.bpp = 2,
637 	},
638 	#endif
639 
640 	#ifdef mode_set_720p_4ch
641 	{
642 		.desc = "BT1120 4CH 720P",
643 		.mbus_code = MEDIA_BUS_FMT_YUYV8_1X16,
644 		.regs = NULL,
645 		.regs_size = 0,
646 		.bpp = 2,
647 	},
648 	#endif
649 
650 	#ifdef mode_set_720p_2ch
651 	{
652 		.desc = "BT1120 2CH 720P",
653 		.mbus_code = MEDIA_BUS_FMT_UYVY8_2X8,
654 		.regs = NULL,
655 		.regs_size = 0,
656 		.bpp = 2,
657 	},
658 	#endif
659 };
660 
661 #define N_FMTS ARRAY_SIZE(sensor_formats)
662 
663 /*
664  * Then there is the issue of window sizes.  Try to capture the info here.
665  */
666 
667 static struct sensor_win_size sensor_win_sizes[] = {
668 	#ifdef mode_set_1080p_4ch
669 	{
670 		.width = HD1080_WIDTH,
671 		.height = HD1080_HEIGHT,
672 		.hoffset = 0,
673 		.voffset = 0,
674 		.fps_fixed = 25,
675 		.regs = reg_1080p25_4ch,
676 		.regs_size = ARRAY_SIZE(reg_1080p25_4ch),
677 		.set_size = NULL,
678 	},
679 	#endif
680 
681 	#ifdef mode_set_1080p_2ch
682 	{
683 		.width = HD1080_WIDTH,
684 		.height = HD1080_HEIGHT,
685 		.hoffset = 0,
686 		.voffset = 0,
687 		.fps_fixed = 25,
688 		.regs = reg_1080p25_2ch,
689 		.regs_size = ARRAY_SIZE(reg_1080p25_2ch),
690 		.set_size = NULL,
691 	},
692 	#endif
693 
694 	#ifdef mode_set_720p_2ch
695 	{
696 		.width = HD720_WIDTH,
697 		.height = HD720_HEIGHT,
698 		.hoffset = 0,
699 		.voffset = 0,
700 		.fps_fixed = 25,
701 		.regs = reg_720p25_2ch,
702 		.regs_size = ARRAY_SIZE(reg_720p25_2ch),
703 		.set_size = NULL,
704 	},
705 	#endif
706 
707 	#ifdef mode_set_720p_4ch
708 	{
709 		.width = HD720_WIDTH,
710 		.height = HD720_HEIGHT,
711 		.hoffset = 0,
712 		.voffset = 0,
713 		.fps_fixed = 25,
714 		.regs = reg_720p25_4ch,
715 		.regs_size = ARRAY_SIZE(reg_720p25_4ch),
716 		.set_size = NULL,
717 	},
718 	#endif
719 };
720 
721 #define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))
722 
sensor_g_mbus_config(struct v4l2_subdev * sd,struct v4l2_mbus_config * cfg)723 static int sensor_g_mbus_config(struct v4l2_subdev *sd,
724 				struct v4l2_mbus_config *cfg)
725 {
726 	#ifdef	mode_set_1080p_2ch
727 	cfg->type = V4L2_MBUS_BT656;
728 	cfg->flags = CLK_POL | CSI_CH_0 | CSI_CH_1;
729 	return 0;
730 	#endif
731 
732 	#ifdef	mode_set_1080p_4ch
733 	cfg->type = V4L2_MBUS_BT656;
734 	cfg->flags = DOUBLE_CLK_POL | CSI_CH_0 | CSI_CH_1 | CSI_CH_2 | CSI_CH_3;
735 	return 0;
736 	#endif
737 
738 	#ifdef	mode_set_720p_4ch
739 	cfg->type = V4L2_MBUS_BT656;
740 	cfg->flags = CLK_POL | CSI_CH_0 | CSI_CH_1 | CSI_CH_2 | CSI_CH_3;
741 	return 0;
742 	#endif
743 
744 	#ifdef	mode_set_720p_2ch
745 	cfg->type = V4L2_MBUS_BT656;
746 	cfg->flags = CLK_POL | CSI_CH_0 | CSI_CH_1;
747 	return 0;
748 	#endif
749 }
750 
sensor_g_ctrl(struct v4l2_ctrl * ctrl)751 static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
752 {
753 	return -EINVAL;
754 }
755 
sensor_s_ctrl(struct v4l2_ctrl * ctrl)756 static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
757 {
758 	return -EINVAL;
759 }
760 
sensor_reg_init(struct sensor_info * info)761 static int sensor_reg_init(struct sensor_info *info)
762 {
763 	int i = 0;
764 	data_type val = 0;
765 	struct v4l2_subdev *sd = &info->sd;
766 	struct sensor_format_struct *sensor_fmt = info->fmt;
767 	struct sensor_win_size *wsize = info->current_wins;
768 
769 	sensor_print("sensor_reg_init sensor_fmt->regs_size = %d\n", sensor_fmt->regs_size);
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 	sensor_print("sensor_reg_init end %dx%d  wsize->regs_size=%d\n", wsize->width, wsize->height, wsize->regs_size);
776 
777 	if (wsize->set_size)
778 		wsize->set_size(sd);
779 
780 	info->fmt = sensor_fmt;
781 	info->width = wsize->width;
782 	info->height = wsize->height;
783 	return 0;
784 }
785 
sensor_s_stream(struct v4l2_subdev * sd,int enable)786 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
787 {
788 	struct sensor_info *info = to_state(sd);
789 
790 	DBG_INFO("%s on = %d, %d*%d %x\n", __func__, enable,
791 		  info->current_wins->width,
792 		  info->current_wins->height, info->fmt->mbus_code);
793 
794 	if (!enable)
795 		return 0;
796 
797 	return sensor_reg_init(info);
798 }
799 
800 /* ----------------------------------------------------------------------- */
801 static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
802 	.g_volatile_ctrl = sensor_g_ctrl,
803 	.s_ctrl = sensor_s_ctrl,
804 };
805 
806 static const struct v4l2_subdev_core_ops sensor_core_ops = {
807 	.reset = sensor_reset,
808 	.init = sensor_init,
809 	.s_power = sensor_power,
810 	.ioctl = sensor_ioctl,
811 };
812 
813 static const struct v4l2_subdev_video_ops sensor_video_ops = {
814 	.s_stream = sensor_s_stream,
815 	.g_mbus_config = sensor_g_mbus_config,
816 };
817 
818 static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
819 	.enum_mbus_code = sensor_enum_mbus_code,
820 	.enum_frame_size = sensor_enum_frame_size,
821 	.get_fmt = sensor_get_fmt,
822 	.set_fmt = sensor_set_fmt,
823 };
824 
825 static const struct v4l2_subdev_ops sensor_ops = {
826 	.core = &sensor_core_ops,
827 	.video = &sensor_video_ops,
828 	.pad = &sensor_pad_ops,
829 };
830 
831 static struct cci_driver cci_drv = {
832 	.name = SENSOR_NAME,
833 	.addr_width = CCI_BITS_8,
834 	.data_width = CCI_BITS_8,
835 };
836 
sensor_init_controls(struct v4l2_subdev * sd,const struct v4l2_ctrl_ops * ops)837 static int sensor_init_controls(struct v4l2_subdev *sd,
838 				const struct v4l2_ctrl_ops *ops)
839 {
840 	struct sensor_info *info = to_state(sd);
841 	struct v4l2_ctrl_handler *handler = &info->handler;
842 	struct v4l2_ctrl *ctrl;
843 	int ret = 0;
844 
845 	v4l2_ctrl_handler_init(handler, 2);
846 
847 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 1 * 1600,
848 			      256 * 1600, 1, 1 * 1600);
849 	ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 0,
850 			      65536 * 16, 1, 0);
851 	if (ctrl != NULL)
852 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
853 
854 	if (handler->error) {
855 		ret = handler->error;
856 		v4l2_ctrl_handler_free(handler);
857 	}
858 	sd->ctrl_handler = handler;
859 	return ret;
860 }
861 
862 
863 
sensor_probe(struct i2c_client * client,const struct i2c_device_id * id)864 static int sensor_probe(struct i2c_client *client,
865 			const struct i2c_device_id *id)
866 {
867 	struct v4l2_subdev *sd;
868 	struct sensor_info *info;
869 	int ret;
870 	int i = 0;
871 
872 	info = kzalloc(sizeof(struct sensor_info), GFP_KERNEL);
873 	if (info == NULL)
874 		return -ENOMEM;
875 	sd = &info->sd;
876 
877 	cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv);
878 	sensor_init_controls(sd, &sensor_ctrl_ops);
879 	mutex_init(&info->lock);
880 
881 	info->fmt = &sensor_formats[0];
882 	info->fmt_pt = &sensor_formats[0];
883 	info->win_pt = &sensor_win_sizes[0];
884 	info->fmt_num = N_FMTS;
885 	info->win_size_num = N_WIN_SIZES;
886 	info->sensor_field = V4L2_FIELD_NONE;
887 
888 	return 0;
889 }
890 
sensor_remove(struct i2c_client * client)891 static int sensor_remove(struct i2c_client *client)
892 {
893 	struct v4l2_subdev *sd;
894 
895 	sd = cci_dev_remove_helper(client, &cci_drv);
896 
897 	kfree(to_state(sd));
898 	return 0;
899 }
900 
sensor_shutdown(struct i2c_client * client)901 static void sensor_shutdown(struct i2c_client *client)
902 {
903 	struct v4l2_subdev *sd;
904 
905 	sd = cci_dev_remove_helper(client, &cci_drv);
906 
907 	kfree(to_state(sd));
908 }
909 
910 static const struct i2c_device_id sensor_id[] = {
911 	{SENSOR_NAME, 0},
912 	{}
913 };
914 
915 MODULE_DEVICE_TABLE(i2c, sensor_id);
916 
917 static struct i2c_driver sensor_driver = {
918 	.driver = {
919 		   .owner = THIS_MODULE,
920 		   .name = SENSOR_NAME,
921 		   },
922 	.probe = sensor_probe,
923 	.remove = sensor_remove,
924 	.shutdown = sensor_shutdown,
925 	.id_table = sensor_id,
926 };
init_sensor(void)927 static __init int init_sensor(void)
928 {
929 	return cci_dev_init_helper(&sensor_driver);
930 }
931 
exit_sensor(void)932 static __exit void exit_sensor(void)
933 {
934 	cci_dev_exit_helper(&sensor_driver);
935 }
936 
937 #ifdef CONFIG_VIDEO_SUNXI_VIN_SPECIAL
938 subsys_initcall_sync(init_sensor);
939 #else
940 module_init(init_sensor);
941 #endif
942 module_exit(exit_sensor);
943