• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * A V4L2 driver for IMX214  Raw cameras.
3  *
4  * Copyright (c) 2017 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
5  *
6  * Authors:  Zhao Wei <zhaowei@allwinnertech.com>
7  *    Yang Feng <yangfeng@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 #include "camera.h"
25 #include "sensor_helper.h"
26 
27 MODULE_AUTHOR("lwj");
28 MODULE_DESCRIPTION("A low-level driver for IMX214 sensors");
29 MODULE_LICENSE("GPL");
30 
31 #define MCLK              (24*1000*1000)
32 #define V4L2_IDENT_SENSOR 0x0214
33 
34 #define DGAIN_R  0x100
35 #define DGAIN_G  0x100
36 #define DGAIN_B  0x100
37 
38 /*
39  * Our nominal (default) frame rate.
40  */
41 
42 #define SENSOR_FRAME_RATE 30
43 
44 /*
45  * The IMX214 sits on i2c with ID 0x6c
46  */
47 #define I2C_ADDR 0x20
48 #define SENSOR_NAME "imx214"
49 int imx214_sensor_vts;
50 
51 struct cfg_array {		/* coming later */
52 	struct regval_list *regs;
53 	int size;
54 };
55 
56 
57 
58 /*
59  * The default register settings
60  *
61  */
62 
63 static struct regval_list sensor_default_regs[] = {
64 	{0x0101, 0x03},
65 	{0x0105, 0x01},
66 	{0x0106, 0x01},
67 	{0x4550, 0x02},
68 	{0x4601, 0x04},
69 	{0x4642, 0x01},
70 	{0x6227, 0x11},
71 	{0x6276, 0x00},
72 	{0x900E, 0x06},
73 	{0xA802, 0x90},
74 	{0xA803, 0x11},
75 	{0xA804, 0x62},
76 	{0xA805, 0x77},
77 	{0xA806, 0xAE},
78 	{0xA807, 0x34},
79 	{0xA808, 0xAE},
80 	{0xA809, 0x35},
81 	{0xA80A, 0x62},
82 	{0xA80B, 0x83},
83 	{0xAE33, 0x00},
84 	{0x4174, 0x00},
85 	{0x4175, 0x11},
86 	{0x4612, 0x29},
87 	{0x461B, 0x2C},
88 	{0x461F, 0x06},
89 	{0x4635, 0x07},
90 	{0x4637, 0x30},
91 	{0x463F, 0x18},
92 	{0x4641, 0x0D},
93 	{0x465B, 0x2C},
94 	{0x465F, 0x2B},
95 	{0x4663, 0x2B},
96 	{0x4667, 0x24},
97 	{0x466F, 0x24},
98 	{0x470E, 0x09},
99 	{0x4909, 0xAB},
100 	{0x490B, 0x95},
101 	{0x4915, 0x5D},
102 	{0x4A5F, 0xFF},
103 	{0x4A61, 0xFF},
104 	{0x4A73, 0x62},
105 	{0x4A85, 0x00},
106 	{0x4A87, 0xFF},
107 	{0x583C, 0x04},
108 	{0x620E, 0x04},
109 	{0x6EB2, 0x01},
110 	{0x6EB3, 0x00},
111 	{0x9300, 0x02},
112 	{0x080b, 119},
113 	{0x080d, 55},
114 	{0x080f, 103},
115 	{0x0811, 55},
116 	{0x0813, 55},
117 	{0x0815, 55},
118 	{0x0817, 223},
119 	{0x0819, 47},
120 	{0x4601, 0x04},
121 	{0x4642, 0x01},
122 	{0x461B, 0x2C},
123 	{0x465B, 0x2C},
124 	{0x465F, 0x2B},
125 	{0x4663, 0x2B},
126 	{0x4667, 0x24},
127 	{0x466F, 0x24},
128 };
129 
130 
131 static struct regval_list sensor_13mega_regs[] = {
132 	{0x0114, 0x03},
133 	{0x0220, 0x00},
134 	{0x0221, 0x11},
135 	{0x0222, 0x01},
136 	{0x0340, 0x0C},
137 	{0x0341, 0x7A},
138 	{0x0342, 0x13},
139 	{0x0343, 0x90},
140 	{0x0344, 0x00},
141 	{0x0345, 0x00},
142 	{0x0346, 0x00},
143 	{0x0347, 0x00},
144 	{0x0348, 0x10},
145 	{0x0349, 0x6F},
146 	{0x034A, 0x0C},
147 	{0x034B, 0x2F},
148 	{0x0381, 0x01},
149 	{0x0383, 0x01},
150 	{0x0385, 0x01},
151 	{0x0387, 0x01},
152 	{0x0900, 0x00},
153 	{0x0901, 0x00},
154 	{0x0902, 0x00},
155 	{0x3000, 0x35},
156 	{0x3054, 0x01},
157 	{0x305C, 0x11},
158 	{0x0112, 0x0A},
159 	{0x0113, 0x0A},
160 	{0x034C, 0x10},
161 	{0x034D, 0x70},
162 	{0x034E, 0x0C},
163 	{0x034F, 0x30},
164 	{0x0401, 0x00},
165 	{0x0404, 0x00},
166 	{0x0405, 0x10},
167 	{0x0408, 0x00},
168 	{0x0409, 0x00},
169 	{0x040A, 0x00},
170 	{0x040B, 0x00},
171 	{0x040C, 0x10},
172 	{0x040D, 0x70},
173 	{0x040E, 0x0C},
174 	{0x040F, 0x30},
175 	{0x0301, 0x05},
176 	{0x0303, 0x02},
177 	{0x0305, 0x03},
178 	{0x0306, 0x00},
179 	{0x0307, 0x64},
180 	{0x0309, 0x0A},
181 	{0x030B, 0x01},
182 	{0x0310, 0x00},
183 	{0x0820, 0x0C},
184 	{0x0821, 0x80},
185 	{0x0822, 0x00},
186 	{0x0823, 0x00},
187 	{0x3A03, 0x08},
188 	{0x3A04, 0xD0},
189 	{0x3A05, 0x02},
190 	{0x0B06, 0x01},
191 	{0x30A2, 0x00},
192 	{0x30B4, 0x00},
193 	{0x3A02, 0xFF},
194 	{0x3011, 0x00},
195 	{0x3013, 0x01},
196 	{0x0202, 0x0C},
197 	{0x0203, 0x70},
198 	{0x0224, 0x01},
199 	{0x0225, 0xF4},
200 	{0x0204, 0x00},
201 	{0x0205, 0x00},
202 	{0x020E, 0x01},
203 	{0x020F, 0x00},
204 	{0x0210, 0x01},
205 	{0x0211, 0x00},
206 	{0x0212, 0x01},
207 	{0x0213, 0x00},
208 	{0x0214, 0x01},
209 	{0x0215, 0x00},
210 	{0x0216, 0x00},
211 	{0x0217, 0x00},
212 	{0x4170, 0x00},
213 	{0x4171, 0x10},
214 	{0x4176, 0x00},
215 	{0x4177, 0x3C},
216 	{0xAE20, 0x04},
217 	{0xAE21, 0x5C},
218 	{0x0138, 0x01},
219 	{0x0100, 0x01},
220 };
221 
222 
223 static struct regval_list sensor_4k_videos[] = {
224 	{0x0114, 0x03},
225 	{0x0220, 0x00},
226 	{0x0221, 0x11},
227 	{0x0222, 0x01},
228 	{0x0340, 0x09},
229 	{0x0341, 0x3C},
230 	{0x0342, 0x13},
231 	{0x0343, 0x90},
232 	{0x0344, 0x00},
233 	{0x0345, 0xB8},
234 	{0x0346, 0x01},
235 	{0x0347, 0xE0},
236 	{0x0348, 0x0F},
237 	{0x0349, 0xB7},
238 	{0x034A, 0x0A},
239 	{0x034B, 0x4F},
240 	{0x0381, 0x01},
241 	{0x0383, 0x01},
242 	{0x0385, 0x01},
243 	{0x0387, 0x01},
244 	{0x0900, 0x00},
245 	{0x0901, 0x00},
246 	{0x0902, 0x00},
247 	{0x3000, 0x35},
248 	{0x3054, 0x01},
249 	{0x305C, 0x11},
250 	{0x0112, 0x0A},
251 	{0x0113, 0x0A},
252 	{0x034C, 0x0F},
253 	{0x034D, 0x00},
254 	{0x034E, 0x08},
255 	{0x034F, 0x70},
256 	{0x0401, 0x00},
257 	{0x0404, 0x00},
258 	{0x0405, 0x10},
259 	{0x0408, 0x00},
260 	{0x0409, 0x00},
261 	{0x040A, 0x00},
262 	{0x040B, 0x00},
263 	{0x040C, 0x0F},
264 	{0x040D, 0x00},
265 	{0x040E, 0x08},
266 	{0x040F, 0x70},
267 	{0x0301, 0x05},
268 	{0x0303, 0x02},
269 	{0x0305, 0x01},
270 	{0x0306, 0x00},
271 	{0x0307, 0x25},
272 	{0x0309, 0x0A},
273 	{0x030B, 0x01},
274 	{0x0310, 0x00},
275 	{0x0820, 0x0D},
276 	{0x0821, 0xE0},
277 	{0x0822, 0x00},
278 	{0x0823, 0x00},
279 	{0x3A03, 0x09},
280 	{0x3A04, 0xA0},
281 	{0x3A05, 0x04},
282 	{0x0B06, 0x01},
283 	{0x30A2, 0x00},
284 	{0x30B4, 0x00},
285 	{0x3A02, 0xFF},
286 	{0x3011, 0x00},
287 	{0x3013, 0x01},
288 	{0x0202, 0x09},
289 	{0x0203, 0x32},
290 	{0x0224, 0x01},
291 	{0x0225, 0xF4},
292 	{0x0204, 0x00},
293 	{0x0205, 0x00},
294 	{0x020E, 0x01},
295 	{0x020F, 0x00},
296 	{0x0210, 0x01},
297 	{0x0211, 0x00},
298 	{0x0212, 0x01},
299 	{0x0213, 0x00},
300 	{0x0214, 0x01},
301 	{0x0215, 0x00},
302 	{0x0216, 0x00},
303 	{0x0217, 0x00},
304 	{0x4170, 0x00},
305 	{0x4171, 0x10},
306 	{0x4176, 0x00},
307 	{0x4177, 0x3C},
308 	{0xAE20, 0x04},
309 	{0xAE21, 0x5C},
310 };
311 
312 static struct regval_list sensor_1080p_regs[] = {
313 	{0x0114, 0x03},
314 	{0x0220, 0x00},
315 	{0x0221, 0x11},
316 	{0x0222, 0x01},
317 	{0x0340, 0x08},
318 	{0x0341, 0x50},
319 	{0x0342, 0x13},
320 	{0x0343, 0x90},
321 	{0x0344, 0x00},
322 	{0x0345, 0xB8},
323 	{0x0346, 0x01},
324 	{0x0347, 0xE0},
325 	{0x0348, 0x0F},
326 	{0x0349, 0xB7},
327 	{0x034A, 0x0A},
328 	{0x034B, 0x4F},
329 	{0x0381, 0x01},
330 	{0x0383, 0x01},
331 	{0x0385, 0x01},
332 	{0x0387, 0x01},
333 	{0x0900, 0x01},
334 	{0x0901, 0x22},
335 	{0x0902, 0x02},
336 	{0x3000, 0x35},
337 	{0x3054, 0x01},
338 	{0x305C, 0x11},
339 	{0x0112, 0x0A},
340 	{0x0113, 0x0A},
341 	{0x034C, 0x07},
342 	{0x034D, 0x80},
343 	{0x034E, 0x04},
344 	{0x034F, 0x38},
345 	{0x0401, 0x00},
346 	{0x0404, 0x00},
347 	{0x0405, 0x10},
348 	{0x0408, 0x00},
349 	{0x0409, 0x00},
350 	{0x040A, 0x00},
351 	{0x040B, 0x00},
352 	{0x040C, 0x07},
353 	{0x040D, 0x80},
354 	{0x040E, 0x04},
355 	{0x040F, 0x38},
356 	{0x0301, 0x05},
357 	{0x0303, 0x02},
358 	{0x0305, 0x03},
359 	{0x0306, 0x00},
360 	{0x0307, 0x64},
361 	{0x0309, 0x0A},
362 	{0x030B, 0x01},
363 	{0x0310, 0x00},
364 	{0x0820, 0x0C},
365 	{0x0821, 0x80},
366 	{0x0822, 0x00},
367 	{0x0823, 0x00},
368 	{0x3A03, 0x06},
369 	{0x3A04, 0x28},
370 	{0x3A05, 0x04},
371 	{0x0B06, 0x01},
372 	{0x30A2, 0x00},
373 	{0x30B4, 0x00},
374 	{0x3A02, 0xFF},
375 	{0x3011, 0x00},
376 	{0x3013, 0x01},
377 	{0x0202, 0x08},
378 	{0x0203, 0x46},
379 	{0x0224, 0x01},
380 	{0x0225, 0xF4},
381 	{0x0204, 0x00},
382 	{0x0205, 0x00},
383 	{0x020E, 0x01},
384 	{0x020F, 0x00},
385 	{0x0210, 0x01},
386 	{0x0211, 0x00},
387 	{0x0212, 0x01},
388 	{0x0213, 0x00},
389 	{0x0214, 0x01},
390 	{0x0215, 0x00},
391 	{0x0216, 0x00},
392 	{0x0217, 0x00},
393 	{0x4170, 0x00},
394 	{0x4171, 0x10},
395 	{0x4176, 0x00},
396 	{0x4177, 0x3C},
397 	{0xAE20, 0x04},
398 	{0xAE21, 0x5C},
399 	{0x0138, 0x01},
400 	{0x0100, 0x01},
401 };
402 
403 /*
404  * Here we'll try to encapsulate the changes for just the output
405  * video format.
406  *
407  */
408 
409 static struct regval_list sensor_fmt_raw[] = {
410 };
411 
sensor_g_exp(struct v4l2_subdev * sd,__s32 * value)412 static int sensor_g_exp(struct v4l2_subdev *sd, __s32 *value)
413 {
414 	struct sensor_info *info = to_state(sd);
415 
416 	*value = info->exp;
417 	sensor_dbg("sensor_get_exposure = %d\n", info->exp);
418 	return 0;
419 }
420 
sensor_s_exp(struct v4l2_subdev * sd,unsigned int exp_val)421 static int sensor_s_exp(struct v4l2_subdev *sd, unsigned int exp_val)
422 {
423 	unsigned char explow, exphigh;
424 	struct sensor_info *info = to_state(sd);
425 
426 	if (exp_val > 0xffffff)
427 		exp_val = 0xfffff0;
428 	if (exp_val < 16)
429 		exp_val = 16;
430 
431 	exp_val = (exp_val + 8) >> 4;
432 
433 	exphigh = (unsigned char)((0xff00 & exp_val) >> 8);
434 	explow = (unsigned char)((0x00ff & exp_val));
435 
436 	sensor_write(sd, 0x0203, explow);
437 	sensor_write(sd, 0x0202, exphigh);
438 
439 	info->exp = exp_val;
440 	return 0;
441 }
442 
sensor_g_gain(struct v4l2_subdev * sd,__s32 * value)443 static int sensor_g_gain(struct v4l2_subdev *sd, __s32 *value)
444 {
445 	struct sensor_info *info = to_state(sd);
446 
447 	*value = info->gain;
448 	sensor_dbg("sensor_get_gain = %d\n", info->gain);
449 	return 0;
450 }
451 
sensor_s_gain(struct v4l2_subdev * sd,int gain_val)452 static int sensor_s_gain(struct v4l2_subdev *sd, int gain_val)
453 {
454 	struct sensor_info *info = to_state(sd);
455 	unsigned char gainlow = 0;
456 	unsigned char gainhigh = 0;
457 	int gainana = 512 - 8192 / gain_val;
458 
459 	gainlow = (unsigned char)(gainana & 0xff);
460 	gainhigh = (unsigned char)((gainana >> 8) & 0xff);
461 
462 	sensor_write(sd, 0x0205, gainlow);
463 	sensor_write(sd, 0x0204, gainhigh);
464 
465 	info->gain = gain_val;
466 
467 	return 0;
468 }
469 
sensor_s_exp_gain(struct v4l2_subdev * sd,struct sensor_exp_gain * exp_gain)470 static int sensor_s_exp_gain(struct v4l2_subdev *sd,
471 			     struct sensor_exp_gain *exp_gain)
472 {
473 	int exp_val, gain_val, shutter, frame_length;
474 	struct sensor_info *info = to_state(sd);
475 
476 	exp_val = exp_gain->exp_val;
477 	gain_val = exp_gain->gain_val;
478 
479 	if (gain_val < 1 * 16)
480 		gain_val = 16;
481 	if (gain_val > 64 * 16 - 1)
482 		gain_val = 64 * 16 - 1;
483 
484 	if (exp_val > 0xfffff)
485 		exp_val = 0xfffff;
486 
487 	shutter = exp_val / 16;
488 	if (shutter > imx214_sensor_vts)
489 		frame_length = shutter;
490 	else
491 		frame_length = imx214_sensor_vts;
492 
493 
494 
495 
496 	sensor_write(sd, 0x0104, 0x01);
497 	sensor_s_exp(sd, exp_val);
498 	sensor_s_gain(sd, gain_val);
499 	sensor_write(sd, 0x0104, 0x00);
500 
501 	if (gain_val > 64) {
502 		sensor_write(sd, 0x30a2, 0x03);
503 		sensor_write(sd, 0x9706, (gain_val - 64) / 12);
504 		sensor_write(sd, 0x9e25, (gain_val - 64));
505 	} else {
506 		sensor_write(sd, 0x30a2, 0x00);
507 	}
508 
509 	info->exp = exp_val;
510 	info->gain = gain_val;
511 	return 0;
512 }
513 
514 /*
515  * Stuff that knows about the sensor.
516  */
517 
sensor_power(struct v4l2_subdev * sd,int on)518 static int sensor_power(struct v4l2_subdev *sd, int on)
519 {
520 	int ret;
521 
522 	ret = 0;
523 	switch (on) {
524 	case STBY_ON:
525 		sensor_dbg("STBY_ON!\n");
526 		cci_lock(sd);
527 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
528 		cci_unlock(sd);
529 		vin_set_mclk(sd, OFF);
530 		break;
531 	case STBY_OFF:
532 		sensor_dbg("STBY_OFF!\n");
533 		cci_lock(sd);
534 		vin_set_mclk_freq(sd, MCLK);
535 		vin_set_mclk(sd, ON);
536 		msleep(20);
537 		cci_unlock(sd);
538 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
539 		break;
540 	case PWR_ON:
541 		sensor_dbg("PWR_ON!\n");
542 		cci_lock(sd);
543 		vin_gpio_set_status(sd, PWDN, 1);
544 		vin_gpio_set_status(sd, RESET, 1);
545 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
546 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
547 		usleep_range(1000, 1200);
548 		vin_set_mclk_freq(sd, MCLK);
549 		vin_set_mclk(sd, ON);
550 		usleep_range(10000, 12000);
551 		vin_gpio_write(sd, POWER_EN, CSI_GPIO_HIGH);
552 		vin_set_pmu_channel(sd, IOVDD, ON);
553 		vin_set_pmu_channel(sd, AVDD, ON);
554 		vin_set_pmu_channel(sd, DVDD, ON);
555 		vin_set_pmu_channel(sd, AFVDD, ON);
556 		usleep_range(10000, 12000);
557 		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
558 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
559 
560 		usleep_range(30000, 31000);
561 		cci_unlock(sd);
562 		break;
563 	case PWR_OFF:
564 		sensor_dbg("PWR_OFF!\n");
565 		cci_lock(sd);
566 		vin_set_mclk(sd, OFF);
567 		vin_gpio_write(sd, POWER_EN, CSI_GPIO_LOW);
568 		vin_set_pmu_channel(sd, AFVDD, OFF);
569 		vin_set_pmu_channel(sd, DVDD, OFF);
570 		vin_set_pmu_channel(sd, AVDD, OFF);
571 		vin_set_pmu_channel(sd, IOVDD, OFF);
572 		usleep_range(10000, 12000);
573 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
574 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
575 
576 		vin_gpio_set_status(sd, RESET, 0);
577 		vin_gpio_set_status(sd, PWDN, 0);
578 		cci_unlock(sd);
579 		break;
580 	default:
581 		return -EINVAL;
582 	}
583 	return 0;
584 }
585 
sensor_reset(struct v4l2_subdev * sd,u32 val)586 static int sensor_reset(struct v4l2_subdev *sd, u32 val)
587 {
588 	switch (val) {
589 	case 0:
590 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
591 		usleep_range(10000, 12000);
592 		break;
593 	case 1:
594 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
595 		usleep_range(10000, 12000);
596 		break;
597 	default:
598 		return -EINVAL;
599 	}
600 
601 	return 0;
602 }
603 
sensor_detect(struct v4l2_subdev * sd)604 static int sensor_detect(struct v4l2_subdev *sd)
605 {
606 	data_type rdval;
607 
608 	sensor_read(sd, 0x0000, &rdval);
609 	sensor_read(sd, 0x0001, &rdval);
610 	sensor_print("find the sony IMX214 ***********\n");
611 	return 0;
612 }
613 
sensor_init(struct v4l2_subdev * sd,u32 val)614 static int sensor_init(struct v4l2_subdev *sd, u32 val)
615 {
616 	int ret;
617 	struct sensor_info *info = to_state(sd);
618 
619 	sensor_dbg("sensor_init\n");
620 
621 	/*Make sure it is a target sensor */
622 	ret = sensor_detect(sd);
623 	if (ret) {
624 		sensor_err("chip found is not an target chip.\n");
625 		return ret;
626 	}
627 
628 	info->focus_status = 0;
629 	info->low_speed = 0;
630 	info->width = 4208;
631 	info->height = 3120;
632 	info->hflip = 0;
633 	info->vflip = 0;
634 	info->gain = 0;
635 
636 	info->tpf.numerator = 1;
637 	info->tpf.denominator = 30;	/* 30fps */
638 
639 	info->preview_first_flag = 1;
640 
641 	return 0;
642 }
643 
sensor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)644 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
645 {
646 	int ret = 0;
647 	struct sensor_info *info = to_state(sd);
648 
649 	switch (cmd) {
650 	case GET_CURRENT_WIN_CFG:
651 		if (info->current_wins != NULL) {
652 			memcpy(arg,
653 			       info->current_wins,
654 			       sizeof(struct sensor_win_size));
655 			ret = 0;
656 		} else {
657 			sensor_err("empty wins!\n");
658 			ret = -1;
659 		}
660 		break;
661 	case SET_FPS:
662 		break;
663 	case VIDIOC_VIN_SENSOR_EXP_GAIN:
664 		ret = sensor_s_exp_gain(sd, (struct sensor_exp_gain *)arg);
665 		break;
666 	case VIDIOC_VIN_SENSOR_CFG_REQ:
667 		sensor_cfg_req(sd, (struct sensor_config *)arg);
668 		break;
669 	default:
670 		return -EINVAL;
671 	}
672 	return ret;
673 }
674 
675 /*
676  * Store information about the video data format.
677  */
678 static struct sensor_format_struct sensor_formats[] = {
679 	{
680 		.desc = "Raw RGB Bayer",
681 		.mbus_code = MEDIA_BUS_FMT_SBGGR10_1X10,
682 		.regs = sensor_fmt_raw,
683 		.regs_size = ARRAY_SIZE(sensor_fmt_raw),
684 		.bpp = 1
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 
693 static struct sensor_win_size sensor_win_sizes[] = {
694 #if 1
695 	/* Fullsize: 4208*3120 */
696 	{
697 	 .width = 4208,
698 	 .height = 3120,
699 	 .hoffset = 0,
700 	 .voffset = 0,
701 	 .hts = 5008,
702 	 .vts = 3194,
703 	 .pclk = 320 * 1000 * 1000,
704 	 .mipi_bps = 800 * 1000 * 1000,
705 	 .fps_fixed = 2,
706 	 .bin_factor = 1,
707 	 .intg_min = 16,
708 	 .intg_max = (3194 - 10) << 4,
709 	 .gain_min = 16,
710 	 .gain_max = (16 << 4),
711 	 .regs = sensor_13mega_regs,
712 	 .regs_size = ARRAY_SIZE(sensor_13mega_regs),
713 	 .set_size = NULL,
714 	 },
715 #endif
716 #if 1
717 	/*4k video */
718 	{
719 	 .width = 3840,
720 	 .height = 2160,
721 	 .hoffset = 0,
722 	 .voffset = 0,
723 	 .hts = 5008,
724 	 .vts = 2362,
725 	 .pclk = 355 * 1000 * 1000,
726 	 .mipi_bps = 888 * 1000 * 1000,
727 	 .fps_fixed = 2,
728 	 .bin_factor = 1,
729 	 .intg_min = 16,
730 	 .intg_max = (2362 - 10) << 4,
731 	 .gain_min = 16,
732 	 .gain_max = (16 << 4),
733 	 .regs = sensor_4k_videos,
734 	 .regs_size = ARRAY_SIZE(sensor_4k_videos),
735 	 .set_size = NULL,
736 	 },
737 
738 	/* 1080p */
739 	{
740 	 .width = 1920,
741 	 .height = 1080,
742 	 .hoffset = 0,
743 	 .voffset = 0,
744 	 .hts = 5008,
745 	 .vts = 2128,
746 	 .pclk = 320 * 1000 * 1000,
747 	 .mipi_bps = 800 * 1000 * 1000,
748 	 .fps_fixed = 1,
749 	 .bin_factor = 1,
750 	 .intg_min = 16,
751 	 .intg_max = (2128 - 10) << 4,
752 	 .gain_min = 16,
753 	 .gain_max = (16 << 4),
754 	 .regs = sensor_1080p_regs,
755 	 .regs_size = ARRAY_SIZE(sensor_1080p_regs),
756 	 .set_size = NULL,
757 	 },
758 #endif
759 
760 };
761 
762 #define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))
763 
sensor_reg_init(struct sensor_info * info)764 static int sensor_reg_init(struct sensor_info *info)
765 {
766 
767 	int ret = 0;
768 	struct v4l2_subdev *sd = &info->sd;
769 	struct sensor_format_struct *sensor_fmt = info->fmt;
770 	struct sensor_win_size *wsize = info->current_wins;
771 
772 	ret = sensor_write_array(sd, sensor_default_regs,
773 			       ARRAY_SIZE(sensor_default_regs));
774 	if (ret < 0) {
775 		sensor_err("write sensor_default_regs error\n");
776 		return ret;
777 	}
778 
779 	sensor_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size);
780 
781 	if (wsize->regs)
782 		sensor_write_array(sd, wsize->regs, wsize->regs_size);
783 	if (wsize->set_size)
784 		wsize->set_size(sd);
785 
786 	info->width = wsize->width;
787 	info->height = wsize->height;
788 	imx214_sensor_vts = wsize->vts;
789 
790 	sensor_print("s_fmt set width = %d, height = %d\n", wsize->width,
791 		      wsize->height);
792 
793 	return 0;
794 }
795 
sensor_s_stream(struct v4l2_subdev * sd,int enable)796 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
797 {
798 	struct sensor_info *info = to_state(sd);
799 
800 	sensor_print("%s on = %d, %d*%d %x\n", __func__, enable,
801 		  info->current_wins->width,
802 		  info->current_wins->height, info->fmt->mbus_code);
803 
804 	if (!enable)
805 		return 0;
806 	return sensor_reg_init(info);
807 }
808 
sensor_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)809 static int sensor_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
810 				struct v4l2_mbus_config *cfg)
811 {
812 	cfg->type = V4L2_MBUS_CSI2_DPHY;
813 	cfg->flags = 0 | V4L2_MBUS_CSI2_4_LANE | V4L2_MBUS_CSI2_CHANNEL_0;
814 
815 	return 0;
816 }
817 
sensor_g_ctrl(struct v4l2_ctrl * ctrl)818 static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
819 {
820 	struct sensor_info *info =
821 			container_of(ctrl->handler, struct sensor_info, handler);
822 	struct v4l2_subdev *sd = &info->sd;
823 
824 	switch (ctrl->id) {
825 	case V4L2_CID_GAIN:
826 		return sensor_g_gain(sd, &ctrl->val);
827 	case V4L2_CID_EXPOSURE:
828 		return sensor_g_exp(sd, &ctrl->val);
829 	}
830 	return -EINVAL;
831 }
832 
sensor_s_ctrl(struct v4l2_ctrl * ctrl)833 static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
834 {
835 	struct sensor_info *info =
836 			container_of(ctrl->handler, struct sensor_info, handler);
837 	struct v4l2_subdev *sd = &info->sd;
838 
839 	switch (ctrl->id) {
840 	case V4L2_CID_GAIN:
841 		return sensor_s_gain(sd, ctrl->val);
842 	case V4L2_CID_EXPOSURE:
843 		return sensor_s_exp(sd, ctrl->val);
844 	}
845 	return -EINVAL;
846 }
847 
848 /* ----------------------------------------------------------------------- */
849 
850 static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
851 	.g_volatile_ctrl = sensor_g_ctrl,
852 	.s_ctrl = sensor_s_ctrl,
853 };
854 
855 static const struct v4l2_subdev_core_ops sensor_core_ops = {
856 	.reset = sensor_reset,
857 	.init = sensor_init,
858 	.s_power = sensor_power,
859 	.ioctl = sensor_ioctl,
860 #ifdef CONFIG_COMPAT
861 	.compat_ioctl32 = sensor_compat_ioctl32,
862 #endif
863 };
864 
865 static const struct v4l2_subdev_video_ops sensor_video_ops = {
866 	.s_stream = sensor_s_stream,
867 };
868 
869 static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
870 	.enum_mbus_code = sensor_enum_mbus_code,
871 	.enum_frame_size = sensor_enum_frame_size,
872 	.get_fmt = sensor_get_fmt,
873 	.set_fmt = sensor_set_fmt,
874 	.get_mbus_config = sensor_g_mbus_config,
875 };
876 
877 static const struct v4l2_subdev_ops sensor_ops = {
878 	.core = &sensor_core_ops,
879 	.video = &sensor_video_ops,
880 	.pad = &sensor_pad_ops,
881 };
882 
883 /* ----------------------------------------------------------------------- */
884 static struct cci_driver cci_drv = {
885 	.name = SENSOR_NAME,
886 	.addr_width = CCI_BITS_16,
887 	.data_width = CCI_BITS_8,
888 };
889 
890 static const struct v4l2_ctrl_config sensor_custom_ctrls[] = {
891 	{
892 		.ops = &sensor_ctrl_ops,
893 		.id = V4L2_CID_FRAME_RATE,
894 		.name = "frame rate",
895 		.type = V4L2_CTRL_TYPE_INTEGER,
896 		.min = 15,
897 		.max = 120,
898 		.step = 1,
899 		.def = 120,
900 	},
901 };
902 
sensor_init_controls(struct v4l2_subdev * sd,const struct v4l2_ctrl_ops * ops)903 static int sensor_init_controls(struct v4l2_subdev *sd, const struct v4l2_ctrl_ops *ops)
904 {
905 	struct sensor_info *info = to_state(sd);
906 	struct v4l2_ctrl_handler *handler = &info->handler;
907 	struct v4l2_ctrl *ctrl;
908 	int i;
909 	int ret = 0;
910 
911 	v4l2_ctrl_handler_init(handler, 2 + ARRAY_SIZE(sensor_custom_ctrls));
912 
913 	ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 0,
914 			      65536 * 16, 1, 0);
915 	if (ctrl != NULL)
916 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
917 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 1 * 1600,
918 			      256 * 1600, 1, 1 * 1600);
919 	for (i = 0; i < ARRAY_SIZE(sensor_custom_ctrls); i++)
920 		v4l2_ctrl_new_custom(handler, &sensor_custom_ctrls[i], NULL);
921 
922 	if (handler->error) {
923 		ret = handler->error;
924 		v4l2_ctrl_handler_free(handler);
925 	}
926 
927 	sd->ctrl_handler = handler;
928 
929 	return ret;
930 }
931 
sensor_probe(struct i2c_client * client,const struct i2c_device_id * id)932 static int sensor_probe(struct i2c_client *client,
933 			const struct i2c_device_id *id)
934 {
935 	struct v4l2_subdev *sd;
936 	struct sensor_info *info;
937 
938 	info = kzalloc(sizeof(struct sensor_info), GFP_KERNEL);
939 	if (info == NULL)
940 		return -ENOMEM;
941 	sd = &info->sd;
942 	cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv);
943 	sensor_init_controls(sd, &sensor_ctrl_ops);
944 	mutex_init(&info->lock);
945 
946 #ifdef CONFIG_SAME_I2C
947 	info->sensor_i2c_addr = I2C_ADDR >> 1;
948 #endif
949 
950 	info->fmt = &sensor_formats[0];
951 	info->fmt_pt = &sensor_formats[0];
952 	info->win_pt = &sensor_win_sizes[0];
953 	info->fmt_num = N_FMTS;
954 	info->win_size_num = N_WIN_SIZES;
955 	info->sensor_field = V4L2_FIELD_NONE;
956 	info->af_first_flag = 1;
957 
958 	return 0;
959 }
sensor_remove(struct i2c_client * client)960 static int sensor_remove(struct i2c_client *client)
961 {
962 	struct v4l2_subdev *sd;
963 
964 	sd = cci_dev_remove_helper(client, &cci_drv);
965 	kfree(to_state(sd));
966 	return 0;
967 }
968 
969 static const struct i2c_device_id sensor_id[] = {
970 	{SENSOR_NAME, 0},
971 	{}
972 };
973 
974 MODULE_DEVICE_TABLE(i2c, sensor_id);
975 
976 static struct i2c_driver sensor_driver = {
977 	.driver = {
978 		   .owner = THIS_MODULE,
979 		   .name = SENSOR_NAME,
980 		   },
981 	.probe = sensor_probe,
982 	.remove = sensor_remove,
983 	.id_table = sensor_id,
984 };
init_sensor(void)985 static __init int init_sensor(void)
986 {
987 	return cci_dev_init_helper(&sensor_driver);
988 }
989 
exit_sensor(void)990 static __exit void exit_sensor(void)
991 {
992 	cci_dev_exit_helper(&sensor_driver);
993 }
994 
995 module_init(init_sensor);
996 module_exit(exit_sensor);
997