1 /*
2 * A V4L2 driver for IMX219 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("Chomoly");
28 MODULE_DESCRIPTION("A low-level driver for IMX219 sensors");
29 MODULE_LICENSE("GPL");
30
31 #define MCLK (24*1000*1000)
32 #define V4L2_IDENT_SENSOR 0x0219
33
34 /*
35 * Our nominal (default) frame rate.
36 */
37 #ifdef FPGA
38 #define SENSOR_FRAME_RATE 15
39 #else
40 #define SENSOR_FRAME_RATE 30
41 #endif
42
43 /*
44 * The IMX219 sits on i2c with ID 0x6c
45 */
46 #define I2C_ADDR 0x20
47 #define SENSOR_NAME "imx219"
48 int imx219_sensor_vts;
49 #define ES_GAIN(a, b, c) ((unsigned short)(a * 160) < (c * 10) && (c*10) <= (unsigned short)(b * 160))
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[] = { /*3280 x 2464_20fps 4lanes 720Mbps/lane */
64
65 };
66
67
68 static struct regval_list sensor_hxga_regs[] = {
69
70 {0x30EB, 0x05},
71 {0x30EB, 0x0C},
72 {0x300A, 0xFF},
73 {0x300B, 0xFF},
74 {0x30EB, 0x05},
75 {0x30EB, 0x09},
76 {0x0114, 0x03},
77 {0x0128, 0x00},
78 {0x012A, 0x18},
79 {0x012B, 0x00},
80
81 {0x0160, 0x0F},
82 {0x0161, 0xC5},
83 {0x0162, 0x0D},
84 {0x0163, 0x78},
85
86 {0x0164, 0x00},
87 {0x0165, 0x00},
88 {0x0166, 0x0C},
89 {0x0167, 0xCF},
90 {0x0168, 0x00},
91 {0x0169, 0x00},
92 {0x016A, 0x09},
93 {0x016B, 0x9F},
94 {0x016C, 0x0C},
95 {0x016D, 0xD0},
96 {0x016E, 0x09},
97 {0x016F, 0xA0},
98 {0x0170, 0x01},
99 {0x0171, 0x01},
100 {0x0174, 0x00},
101 {0x0175, 0x00},
102 {0x018C, 0x0A},
103 {0x018D, 0x0A},
104 {0x0301, 0x05},
105 {0x0303, 0x01},
106 {0x0304, 0x03},
107 {0x0305, 0x03},
108 {0x0306, 0x00},
109 {0x0307, 0x57},
110 {0x0309, 0x0A},
111 {0x030B, 0x01},
112 {0x030C, 0x00},
113 {0x030D, 0x5A},
114 {0x4767, 0x0F},
115 {0x4750, 0x14},
116 {0x47B4, 0x14},
117
118 {0x0100, 0x01},
119
120 };
121
122 static struct regval_list sensor_sxga_regs[] = {
123
124 {0x30EB, 0x05},
125 {0x30EB, 0x0C},
126 {0x300A, 0xFF},
127 {0x300B, 0xFF},
128 {0x30EB, 0x05},
129 {0x30EB, 0x09},
130 {0x0114, 0x03},
131 {0x0128, 0x00},
132 {0x012A, 0x18},
133 {0x012B, 0x00},
134 {0x0160, 0x0a},
135 {0x0161, 0x2f},
136 {0x0162, 0x0d},
137 {0x0163, 0xe8},
138 {0x0164, 0x03},
139 {0x0165, 0xe8},
140 {0x0166, 0x08},
141 {0x0167, 0xe7},
142 {0x0168, 0x02},
143 {0x0169, 0xf0},
144 {0x016A, 0x06},
145 {0x016B, 0xaF},
146 {0x016C, 0x05},
147 {0x016D, 0x00},
148 {0x016E, 0x03},
149 {0x016F, 0xc0},
150
151
152
153
154
155 {0x0170, 0x01},
156 {0x0171, 0x01},
157 {0x0174, 0x00},
158 {0x0175, 0x00},
159 {0x018C, 0x0A},
160 {0x018D, 0x0A},
161 {0x0301, 0x05},
162 {0x0303, 0x01},
163 {0x0304, 0x03},
164 {0x0305, 0x03},
165 {0x0306, 0x00},
166 {0x0307, 0x57},
167 {0x0309, 0x0A},
168 {0x030B, 0x01},
169 {0x030C, 0x00},
170 {0x030D, 0x5A},
171 {0x4767, 0x0F},
172 {0x4750, 0x14},
173 {0x47B4, 0x14},
174 {0x0100, 0x01},
175 };
176
177
178 static struct regval_list sensor_1080p_regs[] = {
179
180
181 {0x30EB, 0x05},
182 {0x30EB, 0x0C},
183 {0x300A, 0xFF},
184 {0x300B, 0xFF},
185 {0x30EB, 0x05},
186 {0x30EB, 0x09},
187
188 {0x0114, 0x03},
189 {0x0128, 0x00},
190 {0x012A, 0x18},
191 {0x012B, 0x00},
192
193
194
195 {0x0160, 0x0A},
196 {0x0161, 0x2F},
197 {0x0162, 0x0D},
198 {0x0163, 0xE8},
199 {0x0164, 0x02},
200 {0x0165, 0xA8},
201 {0x0166, 0x0A},
202 {0x0167, 0x27},
203 {0x0168, 0x02},
204 {0x0169, 0xB4},
205 {0x016A, 0x06},
206 {0x016B, 0xEB},
207 {0x016C, 0x07},
208 {0x016D, 0x80},
209 {0x016E, 0x04},
210 {0x016F, 0x38},
211 {0x0170, 0x01},
212 {0x0171, 0x01},
213 {0x0174, 0x00},
214 {0x0175, 0x00},
215 {0x018C, 0x0A},
216 {0x018D, 0x0A},
217 {0x0301, 0x05},
218 {0x0303, 0x01},
219 {0x0304, 0x03},
220 {0x0305, 0x03},
221 {0x0306, 0x00},
222 {0x0307, 0x57},
223 {0x0309, 0x0A},
224 {0x030B, 0x01},
225 {0x030C, 0x00},
226 {0x030D, 0x5A},
227 {0x4767, 0x0F},
228 {0x4750, 0x14},
229 {0x47B4, 0x14},
230 {0x0100, 0x01},
231 };
232
233 static struct regval_list sensor_720p_regs[] = {
234
235
236 {0x30EB, 0x05},
237 {0x30EB, 0x0C},
238 {0x300A, 0xFF},
239 {0x300B, 0xFF},
240 {0x30EB, 0x05},
241 {0x30EB, 0x09},
242 {0x0114, 0x03},
243 {0x0128, 0x00},
244 {0x012A, 0x18},
245 {0x012B, 0x00},
246 {0x0160, 0x02},
247 {0x0161, 0x00},
248 {0x0162, 0x0d /*0D */ },
249 {0x0163, 0xE8},
250 {0x0164, 0x03},
251 {0x0165, 0xE8},
252 {0x0166, 0x08},
253 {0x0167, 0xE7},
254 {0x0168, 0x03},
255 {0x0169, 0x68},
256 {0x016A, 0x06},
257 {0x016B, 0x37},
258 {0x016C, 0x05},
259 {0x016D, 0x00},
260 {0x016E, 0x02},
261 {0x016F, 0xD0},
262 {0x0170, 0x01},
263 {0x0171, 0x01},
264 {0x0174, 0x00},
265 {0x0175, 0x00},
266 {0x018C, 0x0A},
267 {0x018D, 0x0A},
268 {0x0301, 0x05},
269 {0x0303, 0x01},
270 {0x0304, 0x03},
271 {0x0305, 0x03},
272 {0x0306, 0x00},
273 {0x0307, 0x57},
274 {0x0309, 0x05 /*0A */ },
275 {0x030B, 0x01},
276 {0x030C, 0x00},
277 {0x030D, 0x5A},
278 {0x4767, 0x0F},
279 {0x4750, 0x14},
280 {0x47B4, 0x14},
281 {0x0100, 0x01},
282 };
283
284
285 /*
286 * Here we'll try to encapsulate the changes for just the output
287 * video format.
288 *
289 */
290
291 static struct regval_list sensor_fmt_raw[] = {
292
293 };
294
sensor_s_exp_gain(struct v4l2_subdev * sd,struct sensor_exp_gain * exp_gain)295 static int sensor_s_exp_gain(struct v4l2_subdev *sd,
296 struct sensor_exp_gain *exp_gain)
297 {
298 int exp_val, gain_val, frame_length, shutter;
299 unsigned char explow = 0, exphigh = 0;
300 struct sensor_info *info = to_state(sd);
301
302 exp_val = exp_gain->exp_val;
303 gain_val = exp_gain->gain_val;
304
305 if (gain_val < 1 * 16)
306 gain_val = 16;
307 if (gain_val > 10 * 16 - 1)
308 gain_val = 10 * 16 - 1;
309
310 if (exp_val > 0xfffff)
311 exp_val = 0xfffff;
312
313 exp_val >>= 4;
314 exphigh = (unsigned char)((0x00ff00 & exp_val) >> 8);
315 explow = (unsigned char)((0x0000ff & exp_val));
316
317 sensor_write(sd, 0x015b, explow);
318 sensor_write(sd, 0x015a, exphigh);
319
320 shutter = exp_val;
321
322 if (shutter > imx219_sensor_vts - 4)
323 frame_length = shutter + 4;
324 else
325 frame_length = imx219_sensor_vts;
326 sensor_write(sd, 0x0161, frame_length & 0xff);
327
328 sensor_write(sd, 0x0160, frame_length >> 8);
329 if (gain_val == 16)
330 sensor_write(sd, 0x0157, 0x01);
331 if (ES_GAIN(1.0, 1.1, gain_val))
332 sensor_write(sd, 0x0157, 24);
333 else if (ES_GAIN(1.1, 1.2, gain_val))
334 sensor_write(sd, 0x0157, 42);
335 else if (ES_GAIN(1.2, 1.3, gain_val))
336 sensor_write(sd, 0x0157, 60);
337 else if (ES_GAIN(1.3, 1.4, gain_val))
338 sensor_write(sd, 0x0157, 73);
339 else if (ES_GAIN(1.4, 1.5, gain_val))
340 sensor_write(sd, 0x0157, 85);
341 else if (ES_GAIN(1.5, 1.6, gain_val))
342 sensor_write(sd, 0x0157, 96);
343 else if (ES_GAIN(1.6, 1.7, gain_val))
344 sensor_write(sd, 0x0157, 105);
345 else if (ES_GAIN(1.7, 1.8, gain_val))
346 sensor_write(sd, 0x0157, 114);
347 else if (ES_GAIN(1.8, 1.9, gain_val))
348 sensor_write(sd, 0x0157, 122);
349 else if (ES_GAIN(1.9, 2.0, gain_val))
350 sensor_write(sd, 0x0157, 0x80);
351 else if (ES_GAIN(2.0, 2.1, gain_val))
352 sensor_write(sd, 0x0157, 134);
353 else if (ES_GAIN(2.1, 2.2, gain_val))
354 sensor_write(sd, 0x0157, 140);
355 else if (ES_GAIN(2.2, 2.3, gain_val))
356 sensor_write(sd, 0x0157, 145);
357 else if (ES_GAIN(2.3, 2.4, gain_val))
358 sensor_write(sd, 0x0157, 150);
359 else if (ES_GAIN(2.4, 2.5, gain_val))
360 sensor_write(sd, 0x0157, 154);
361 else if (ES_GAIN(2.5, 2.6, gain_val))
362 sensor_write(sd, 0x0157, 158);
363 else if (ES_GAIN(2.6, 2.7, gain_val))
364 sensor_write(sd, 0x0157, 162);
365 else if (ES_GAIN(2.7, 2.8, gain_val))
366 sensor_write(sd, 0x0157, 165);
367
368 else if (ES_GAIN(2.8, 2.9, gain_val))
369 sensor_write(sd, 0x0157, 168);
370 else if (ES_GAIN(2.9, 3.0, gain_val))
371 sensor_write(sd, 0x0157, 0xab);
372 else if (ES_GAIN(3.0, 3.1, gain_val))
373 sensor_write(sd, 0x0157, 174);
374 else if (ES_GAIN(3.1, 3.2, gain_val))
375 sensor_write(sd, 0x0157, 176);
376 else if (ES_GAIN(3.2, 3.3, gain_val))
377 sensor_write(sd, 0x0157, 179);
378 else if (ES_GAIN(3.3, 3.4, gain_val))
379 sensor_write(sd, 0x0157, 181);
380 else if (ES_GAIN(3.4, 3.5, gain_val))
381 sensor_write(sd, 0x0157, 183);
382 else if (ES_GAIN(3.5, 3.6, gain_val))
383 sensor_write(sd, 0x0157, 185);
384 else if (ES_GAIN(3.6, 3.7, gain_val))
385 sensor_write(sd, 0x0157, 187);
386 else if (ES_GAIN(3.7, 3.8, gain_val))
387 sensor_write(sd, 0x0157, 189);
388 else if (ES_GAIN(3.8, 3.9, gain_val))
389 sensor_write(sd, 0x0157, 191);
390 else if (ES_GAIN(3.9, 4.0, gain_val))
391 sensor_write(sd, 0x0157, 192);
392 else if (ES_GAIN(4.0, 4.1, gain_val))
393 sensor_write(sd, 0x0157, 194);
394 else if (ES_GAIN(4.1, 4.2, gain_val))
395 sensor_write(sd, 0x0157, 195);
396 else if (ES_GAIN(4.2, 4.3, gain_val))
397 sensor_write(sd, 0x0157, 197);
398 else if (ES_GAIN(4.3, 4.4, gain_val))
399 sensor_write(sd, 0x0157, 198);
400 else if (ES_GAIN(4.4, 4.5, gain_val))
401 sensor_write(sd, 0x0157, 200);
402 else if (ES_GAIN(4.5, 4.6, gain_val))
403 sensor_write(sd, 0x0157, 201);
404 else if (ES_GAIN(4.6, 4.7, gain_val))
405 sensor_write(sd, 0x0157, 202);
406 else if (ES_GAIN(4.7, 4.8, gain_val))
407 sensor_write(sd, 0x0157, 203);
408 else if (ES_GAIN(4.8, 4.9, gain_val))
409 sensor_write(sd, 0x0157, 204);
410 else if (ES_GAIN(4.9, 5.0, gain_val))
411 sensor_write(sd, 0x0157, 205);
412 else if (ES_GAIN(5.0, 5.1, gain_val))
413 sensor_write(sd, 0x0157, 206);
414 else if (ES_GAIN(5.1, 5.2, gain_val))
415 sensor_write(sd, 0x0157, 207);
416
417 else if (ES_GAIN(5.2, 5.3, gain_val))
418 sensor_write(sd, 0x0157, 208);
419 else if (ES_GAIN(5.3, 5.4, gain_val))
420 sensor_write(sd, 0x0157, 209);
421 else if (ES_GAIN(5.4, 5.5, gain_val))
422 sensor_write(sd, 0x0157, 210);
423 else if (ES_GAIN(5.5, 5.7, gain_val))
424 sensor_write(sd, 0x0157, 211);
425 else if (ES_GAIN(5.7, 5.8, gain_val))
426 sensor_write(sd, 0x0157, 212);
427 else if (ES_GAIN(5.8, 5.9, gain_val))
428 sensor_write(sd, 0x0157, 213);
429 else if (ES_GAIN(5.9, 6.2, gain_val))
430 sensor_write(sd, 0x0157, 215);
431 else if (ES_GAIN(6.2, 6.4, gain_val))
432 sensor_write(sd, 0x0157, 216);
433 else if (ES_GAIN(6.4, 6.5, gain_val))
434 sensor_write(sd, 0x0157, 217);
435 else if (ES_GAIN(6.5, 6.7, gain_val))
436 sensor_write(sd, 0x0157, 218);
437 else if (ES_GAIN(6.7, 6.9, gain_val))
438 sensor_write(sd, 0x0157, 219);
439 else if (ES_GAIN(6.9, 7.1, gain_val))
440 sensor_write(sd, 0x0157, 220);
441 else if (ES_GAIN(7.1, 7.3, gain_val))
442 sensor_write(sd, 0x0157, 221);
443 else if (ES_GAIN(7.3, 7.5, gain_val))
444 sensor_write(sd, 0x0157, 222);
445 else if (ES_GAIN(7.5, 7.7, gain_val))
446 sensor_write(sd, 0x0157, 223);
447 else if (ES_GAIN(7.7, 8.0, gain_val))
448 sensor_write(sd, 0x0157, 224);
449 else if (ES_GAIN(8.0, 8.3, gain_val))
450 sensor_write(sd, 0x0157, 225);
451 else if (ES_GAIN(8.3, 8.5, gain_val))
452 sensor_write(sd, 0x0157, 226);
453 else if (ES_GAIN(8.5, 8.8, gain_val))
454 sensor_write(sd, 0x0157, 227);
455 else if (ES_GAIN(8.8, 9.1, gain_val))
456 sensor_write(sd, 0x0157, 228);
457
458 else if (ES_GAIN(9.1, 9.4, gain_val))
459 sensor_write(sd, 0x0157, 228);
460 else if (ES_GAIN(9.4, 9.8, gain_val))
461 sensor_write(sd, 0x0157, 230);
462 else if (ES_GAIN(9.8, 10.2, gain_val))
463 sensor_write(sd, 0x0157, 231);
464 else if (ES_GAIN(10.0, 10.6, gain_val))
465 sensor_write(sd, 0x0157, 232);
466
467 info->exp = exp_val;
468 info->gain = gain_val;
469 return 0;
470 }
471
sensor_g_exp(struct v4l2_subdev * sd,__s32 * value)472 static int sensor_g_exp(struct v4l2_subdev *sd, __s32 *value)
473 {
474 struct sensor_info *info = to_state(sd);
475
476 *value = info->exp;
477 sensor_dbg("sensor_get_exposure = %d\n", info->exp);
478 return 0;
479 }
480
sensor_s_exp(struct v4l2_subdev * sd,unsigned int exp_val)481 static int sensor_s_exp(struct v4l2_subdev *sd, unsigned int exp_val)
482 {
483 unsigned char explow, exphigh;
484 struct sensor_info *info = to_state(sd);
485
486 if (exp_val > 0xfffff)
487 exp_val = 0xfffff;
488 exp_val >>= 4;
489 exphigh = (unsigned char)((0x00ff00 & exp_val) >> 8);
490 explow = (unsigned char)((0x0000ff & exp_val));
491 sensor_write(sd, 0x015b, explow);
492 sensor_write(sd, 0x015a, exphigh);
493
494 info->exp = exp_val;
495 return 0;
496 }
497
sensor_g_gain(struct v4l2_subdev * sd,__s32 * value)498 static int sensor_g_gain(struct v4l2_subdev *sd, __s32 *value)
499 {
500 struct sensor_info *info = to_state(sd);
501
502 *value = info->gain;
503 sensor_dbg("sensor_get_gain = %d\n", info->gain);
504 return 0;
505 }
506
sensor_s_gain(struct v4l2_subdev * sd,int gain_val)507 static int sensor_s_gain(struct v4l2_subdev *sd, int gain_val)
508 {
509 struct sensor_info *info = to_state(sd);
510
511 if (gain_val < 1 * 16)
512 gain_val = 16;
513 if (gain_val > 0x1ff)
514 gain_val = 0x1ff;
515 if (gain_val == 16)
516 sensor_write(sd, 0x0157, 0x01);
517 if (ES_GAIN(1.0, 1.1, gain_val))
518 sensor_write(sd, 0x0157, 24);
519 else if (ES_GAIN(1.1, 1.2, gain_val))
520 sensor_write(sd, 0x0157, 42);
521 else if (ES_GAIN(1.2, 1.3, gain_val))
522 sensor_write(sd, 0x0157, 60);
523 else if (ES_GAIN(1.3, 1.4, gain_val))
524 sensor_write(sd, 0x0157, 73);
525 else if (ES_GAIN(1.4, 1.5, gain_val))
526 sensor_write(sd, 0x0157, 85);
527 else if (ES_GAIN(1.5, 1.6, gain_val))
528 sensor_write(sd, 0x0157, 96);
529 else if (ES_GAIN(1.6, 1.7, gain_val))
530 sensor_write(sd, 0x0157, 105);
531 else if (ES_GAIN(1.7, 1.8, gain_val))
532 sensor_write(sd, 0x0157, 114);
533 else if (ES_GAIN(1.8, 1.9, gain_val))
534 sensor_write(sd, 0x0157, 122);
535 else if (ES_GAIN(1.9, 2.0, gain_val))
536 sensor_write(sd, 0x0157, 0x80);
537 else if (ES_GAIN(2.0, 2.1, gain_val))
538 sensor_write(sd, 0x0157, 134);
539 else if (ES_GAIN(2.1, 2.2, gain_val))
540 sensor_write(sd, 0x0157, 140);
541 else if (ES_GAIN(2.2, 2.3, gain_val))
542 sensor_write(sd, 0x0157, 145);
543 else if (ES_GAIN(2.3, 2.4, gain_val))
544 sensor_write(sd, 0x0157, 150);
545 else if (ES_GAIN(2.4, 2.5, gain_val))
546 sensor_write(sd, 0x0157, 154);
547 else if (ES_GAIN(2.5, 2.6, gain_val))
548 sensor_write(sd, 0x0157, 158);
549 else if (ES_GAIN(2.6, 2.7, gain_val))
550 sensor_write(sd, 0x0157, 162);
551 else if (ES_GAIN(2.7, 2.8, gain_val))
552 sensor_write(sd, 0x0157, 165);
553
554 else if (ES_GAIN(2.8, 2.9, gain_val))
555 sensor_write(sd, 0x0157, 168);
556 else if (ES_GAIN(2.9, 3.0, gain_val))
557 sensor_write(sd, 0x0157, 0xab);
558 else if (ES_GAIN(3.0, 3.1, gain_val))
559 sensor_write(sd, 0x0157, 174);
560 else if (ES_GAIN(3.1, 3.2, gain_val))
561 sensor_write(sd, 0x0157, 176);
562 else if (ES_GAIN(3.2, 3.3, gain_val))
563 sensor_write(sd, 0x0157, 179);
564 else if (ES_GAIN(3.3, 3.4, gain_val))
565 sensor_write(sd, 0x0157, 181);
566 else if (ES_GAIN(3.4, 3.5, gain_val))
567 sensor_write(sd, 0x0157, 183);
568 else if (ES_GAIN(3.5, 3.6, gain_val))
569 sensor_write(sd, 0x0157, 185);
570 else if (ES_GAIN(3.6, 3.7, gain_val))
571 sensor_write(sd, 0x0157, 187);
572 else if (ES_GAIN(3.7, 3.8, gain_val))
573 sensor_write(sd, 0x0157, 189);
574 else if (ES_GAIN(3.8, 3.9, gain_val))
575 sensor_write(sd, 0x0157, 191);
576 else if (ES_GAIN(3.9, 4.0, gain_val))
577 sensor_write(sd, 0x0157, 192);
578 else if (ES_GAIN(4.0, 4.1, gain_val))
579 sensor_write(sd, 0x0157, 194);
580 else if (ES_GAIN(4.1, 4.2, gain_val))
581 sensor_write(sd, 0x0157, 195);
582 else if (ES_GAIN(4.2, 4.3, gain_val))
583 sensor_write(sd, 0x0157, 197);
584 else if (ES_GAIN(4.3, 4.4, gain_val))
585 sensor_write(sd, 0x0157, 198);
586 else if (ES_GAIN(4.4, 4.5, gain_val))
587 sensor_write(sd, 0x0157, 200);
588 else if (ES_GAIN(4.5, 4.6, gain_val))
589 sensor_write(sd, 0x0157, 201);
590 else if (ES_GAIN(4.6, 4.7, gain_val))
591 sensor_write(sd, 0x0157, 202);
592 else if (ES_GAIN(4.7, 4.8, gain_val))
593 sensor_write(sd, 0x0157, 203);
594 else if (ES_GAIN(4.8, 4.9, gain_val))
595 sensor_write(sd, 0x0157, 204);
596 else if (ES_GAIN(4.9, 5.0, gain_val))
597 sensor_write(sd, 0x0157, 205);
598 else if (ES_GAIN(5.0, 5.1, gain_val))
599 sensor_write(sd, 0x0157, 206);
600 else if (ES_GAIN(5.1, 5.2, gain_val))
601 sensor_write(sd, 0x0157, 207);
602
603 else if (ES_GAIN(5.2, 5.3, gain_val))
604 sensor_write(sd, 0x0157, 208);
605 else if (ES_GAIN(5.3, 5.4, gain_val))
606 sensor_write(sd, 0x0157, 209);
607 else if (ES_GAIN(5.4, 5.5, gain_val))
608 sensor_write(sd, 0x0157, 210);
609 else if (ES_GAIN(5.5, 5.7, gain_val))
610 sensor_write(sd, 0x0157, 211);
611 else if (ES_GAIN(5.7, 5.8, gain_val))
612 sensor_write(sd, 0x0157, 212);
613 else if (ES_GAIN(5.8, 5.9, gain_val))
614 sensor_write(sd, 0x0157, 213);
615 else if (ES_GAIN(5.9, 6.2, gain_val))
616 sensor_write(sd, 0x0157, 215);
617 else if (ES_GAIN(6.2, 6.4, gain_val))
618 sensor_write(sd, 0x0157, 216);
619 else if (ES_GAIN(6.4, 6.5, gain_val))
620 sensor_write(sd, 0x0157, 217);
621 else if (ES_GAIN(6.5, 6.7, gain_val))
622 sensor_write(sd, 0x0157, 218);
623 else if (ES_GAIN(6.7, 6.9, gain_val))
624 sensor_write(sd, 0x0157, 219);
625 else if (ES_GAIN(6.9, 7.1, gain_val))
626 sensor_write(sd, 0x0157, 220);
627 else if (ES_GAIN(7.1, 7.3, gain_val))
628 sensor_write(sd, 0x0157, 221);
629 else if (ES_GAIN(7.3, 7.5, gain_val))
630 sensor_write(sd, 0x0157, 222);
631 else if (ES_GAIN(7.5, 7.7, gain_val))
632 sensor_write(sd, 0x0157, 223);
633 else if (ES_GAIN(7.7, 8.0, gain_val))
634 sensor_write(sd, 0x0157, 224);
635 else if (ES_GAIN(8.0, 8.3, gain_val))
636 sensor_write(sd, 0x0157, 225);
637 else if (ES_GAIN(8.3, 8.5, gain_val))
638 sensor_write(sd, 0x0157, 226);
639 else if (ES_GAIN(8.5, 8.8, gain_val))
640 sensor_write(sd, 0x0157, 227);
641 else if (ES_GAIN(8.8, 9.1, gain_val))
642 sensor_write(sd, 0x0157, 228);
643
644 else if (ES_GAIN(9.1, 9.4, gain_val))
645 sensor_write(sd, 0x0157, 228);
646 else if (ES_GAIN(9.4, 9.8, gain_val))
647 sensor_write(sd, 0x0157, 230);
648 else if (ES_GAIN(9.8, 10.2, gain_val))
649 sensor_write(sd, 0x0157, 231);
650 else if (ES_GAIN(10.0, 10.6, gain_val))
651 sensor_write(sd, 0x0157, 232);
652 info->gain = gain_val;
653
654 return 0;
655 }
656 #if 0
657 static int sensor_s_sw_stby(struct v4l2_subdev *sd, int on_off)
658 {
659 int ret;
660 data_type rdval;
661
662 ret = sensor_read(sd, 0x0100, &rdval);
663 if (ret != 0)
664 return ret;
665
666 if (on_off == CSI_GPIO_LOW)
667 ret = sensor_write(sd, 0x0100, rdval & 0xfe);
668 else
669 ret = sensor_write(sd, 0x0100, rdval | 0x01);
670 return ret;
671 }
672 #endif
673 /*
674 * Stuff that knows about the sensor.
675 */
676
sensor_power(struct v4l2_subdev * sd,int on)677 static int sensor_power(struct v4l2_subdev *sd, int on)
678 {
679 int ret;
680
681 ret = 0;
682 switch (on) {
683 case STBY_ON:
684 sensor_dbg("STBY_ON!\n");
685 cci_lock(sd);
686 vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
687 cci_unlock(sd);
688 vin_set_mclk(sd, OFF);
689 break;
690 case STBY_OFF:
691 sensor_dbg("STBY_OFF!\n");
692 cci_lock(sd);
693 vin_set_mclk_freq(sd, MCLK);
694 vin_set_mclk(sd, ON);
695 msleep(20);
696 cci_unlock(sd);
697 vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
698 break;
699 case PWR_ON:
700 sensor_dbg("PWR_ON!\n");
701 cci_lock(sd);
702 vin_gpio_set_status(sd, PWDN, 1);
703 vin_gpio_set_status(sd, RESET, 1);
704 vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
705 vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
706 usleep_range(1000, 1200);
707 vin_set_mclk_freq(sd, MCLK);
708 vin_set_mclk(sd, ON);
709 usleep_range(10000, 12000);
710 vin_gpio_write(sd, POWER_EN, CSI_GPIO_HIGH);
711 vin_set_pmu_channel(sd, IOVDD, ON);
712 vin_set_pmu_channel(sd, AVDD, ON);
713 vin_set_pmu_channel(sd, DVDD, ON);
714 vin_set_pmu_channel(sd, AFVDD, ON);
715 usleep_range(10000, 12000);
716 vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
717 vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
718 usleep_range(30000, 31000);
719 cci_unlock(sd);
720 break;
721 case PWR_OFF:
722 sensor_dbg("PWR_OFF!\n");
723 cci_lock(sd);
724 vin_set_mclk(sd, OFF);
725 vin_gpio_write(sd, POWER_EN, CSI_GPIO_LOW);
726 vin_set_pmu_channel(sd, AFVDD, OFF);
727 vin_set_pmu_channel(sd, DVDD, OFF);
728 vin_set_pmu_channel(sd, AVDD, OFF);
729 vin_set_pmu_channel(sd, IOVDD, OFF);
730 usleep_range(10000, 12000);
731 vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
732 vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
733
734 vin_gpio_set_status(sd, RESET, 0);
735 vin_gpio_set_status(sd, PWDN, 0);
736 cci_unlock(sd);
737 break;
738 default:
739 return -EINVAL;
740 }
741 return 0;
742 }
743
sensor_reset(struct v4l2_subdev * sd,u32 val)744 static int sensor_reset(struct v4l2_subdev *sd, u32 val)
745 {
746 switch (val) {
747 case 0:
748 vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
749 usleep_range(10000, 12000);
750 break;
751 case 1:
752 vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
753 usleep_range(10000, 12000);
754 break;
755 default:
756 return -EINVAL;
757 }
758
759 return 0;
760 }
761
sensor_detect(struct v4l2_subdev * sd)762 static int sensor_detect(struct v4l2_subdev *sd)
763 {
764 data_type rdval;
765
766 sensor_read(sd, 0x0000, &rdval);
767 if ((rdval & 0x0f) != 0x02)
768 return -ENODEV;
769
770 sensor_read(sd, 0x0001, &rdval);
771 if (rdval != 0x19)
772 return -ENODEV;
773 sensor_print("find the sony IMX219 ***********\n");
774 return 0;
775 }
776
sensor_init(struct v4l2_subdev * sd,u32 val)777 static int sensor_init(struct v4l2_subdev *sd, u32 val)
778 {
779 int ret;
780 struct sensor_info *info = to_state(sd);
781
782 sensor_dbg("sensor_init\n");
783
784 /*Make sure it is a target sensor */
785 ret = sensor_detect(sd);
786 if (ret) {
787 sensor_err("chip found is not an target chip.\n");
788 return ret;
789 }
790
791 info->focus_status = 0;
792 info->low_speed = 0;
793 info->width = HXGA_WIDTH;
794 info->height = HXGA_HEIGHT;
795 info->hflip = 0;
796 info->vflip = 0;
797 info->gain = 0;
798
799 info->tpf.numerator = 1;
800 info->tpf.denominator = 30; /* 30fps */
801
802 info->preview_first_flag = 1;
803
804 return 0;
805 }
806
sensor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)807 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
808 {
809 int ret = 0;
810 struct sensor_info *info = to_state(sd);
811
812 switch (cmd) {
813 case GET_CURRENT_WIN_CFG:
814 if (info->current_wins != NULL) {
815 memcpy(arg,
816 info->current_wins,
817 sizeof(struct sensor_win_size));
818 ret = 0;
819 } else {
820 sensor_err("empty wins!\n");
821 ret = -1;
822 }
823 break;
824 case SET_FPS:
825 break;
826 case VIDIOC_VIN_SENSOR_EXP_GAIN:
827 sensor_s_exp_gain(sd, (struct sensor_exp_gain *)arg);
828 break;
829 case VIDIOC_VIN_SENSOR_CFG_REQ:
830 sensor_cfg_req(sd, (struct sensor_config *)arg);
831 break;
832 default:
833 return -EINVAL;
834 }
835 return ret;
836 }
837
838 /*
839 * Store information about the video data format.
840 */
841 static struct sensor_format_struct sensor_formats[] = {
842 {
843 .desc = "Raw RGB Bayer",
844 .mbus_code = MEDIA_BUS_FMT_SRGGB10_1X10,
845 .regs = sensor_fmt_raw,
846 .regs_size = ARRAY_SIZE(sensor_fmt_raw),
847 .bpp = 1
848 },
849 };
850 #define N_FMTS ARRAY_SIZE(sensor_formats)
851
852 /*
853 * Then there is the issue of window sizes. Try to capture the info here.
854 */
855
856 static struct sensor_win_size sensor_win_sizes[] = {
857 /* 3280*2464 */
858 {
859 .width = 3264,
860 .height = 2448,
861 .hoffset = (3280 - 3264) / 2,
862 .voffset = (2464 - 2448) / 2,
863 .hts = 3448,
864 .vts = 4037,
865 .pclk = (278 * 1000 * 1000),
866 .mipi_bps = 720 * 1000 * 1000,
867 .fps_fixed = 1,
868 .bin_factor = 1,
869 .intg_min = 1 << 4,
870 .intg_max = (4037 - 4) << 4,
871 .gain_min = 1 << 4,
872 .gain_max = 10 << 4,
873 .regs = sensor_hxga_regs,
874 .regs_size = ARRAY_SIZE(sensor_hxga_regs),
875 .set_size = NULL,
876 },
877
878 /* 1080P */
879 {
880 .width = HD1080_WIDTH,
881 .height = HD1080_HEIGHT,
882 .hoffset = 0,
883 .voffset = 0,
884 .hts = 3560,
885 .vts = 2607,
886 .pclk = (278 * 1000 * 1000),
887 .mipi_bps = 720 * 1000 * 1000,
888 .fps_fixed = 1,
889 .bin_factor = 2,
890 .intg_min = 1 << 4,
891 .intg_max = (2607 - 4) << 4,
892 .gain_min = 1 << 4,
893 .gain_max = 10 << 4,
894 .regs = sensor_1080p_regs,
895 .regs_size = ARRAY_SIZE(sensor_1080p_regs),
896 .set_size = NULL,
897 },
898 /* SXGA */
899 {
900 .width = SXGA_WIDTH,
901 .height = SXGA_HEIGHT,
902 .hoffset = 0,
903 .voffset = 0,
904 .hts = 3560,
905 .vts = 2607,
906 .pclk = (278 * 1000 * 1000),
907 .mipi_bps = 720 * 1000 * 1000,
908 .fps_fixed = 1,
909 .bin_factor = 2,
910 .intg_min = 1 << 4,
911 .intg_max = 2607 << 4,
912 .gain_min = 1 << 4,
913 .gain_max = 10 << 4,
914 .regs = sensor_sxga_regs,
915 .regs_size = ARRAY_SIZE(sensor_sxga_regs),
916 .set_size = NULL,
917 },
918 /* 720p */
919 {
920 .width = HD720_WIDTH,
921 .height = HD720_HEIGHT,
922 .hoffset = 0,
923 .voffset = 0,
924 .hts = 2560,
925 .vts = 1303,
926 .pclk = (200 * 1000 * 1000),
927 .mipi_bps = 720 * 1000 * 1000,
928 .fps_fixed = 1,
929 .bin_factor = 2,
930 .intg_min = 1 << 4,
931 .intg_max = (1303 - 4) << 4,
932 .gain_min = 1 << 4,
933 .gain_max = 10 << 4,
934 .regs = sensor_720p_regs,
935 .regs_size = ARRAY_SIZE(sensor_720p_regs),
936 .set_size = NULL,
937 },
938
939 };
940
941 #define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))
942
sensor_reg_init(struct sensor_info * info)943 static int sensor_reg_init(struct sensor_info *info)
944 {
945
946 int ret = 0;
947 struct v4l2_subdev *sd = &info->sd;
948 struct sensor_format_struct *sensor_fmt = info->fmt;
949 struct sensor_win_size *wsize = info->current_wins;
950
951 ret = sensor_write_array(sd, sensor_default_regs,
952 ARRAY_SIZE(sensor_default_regs));
953 if (ret < 0) {
954 sensor_err("write sensor_default_regs error\n");
955 return ret;
956 }
957
958 sensor_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size);
959
960 if (wsize->regs)
961 sensor_write_array(sd, wsize->regs, wsize->regs_size);
962
963 if (wsize->set_size)
964 wsize->set_size(sd);
965
966 info->width = wsize->width;
967 info->height = wsize->height;
968 imx219_sensor_vts = wsize->vts;
969
970 sensor_print("s_fmt set width = %d, height = %d\n", wsize->width,
971 wsize->height);
972
973 return 0;
974 }
975
sensor_s_stream(struct v4l2_subdev * sd,int enable)976 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
977 {
978 struct sensor_info *info = to_state(sd);
979
980 sensor_print("%s on = %d, %d*%d %x\n", __func__, enable,
981 info->current_wins->width,
982 info->current_wins->height, info->fmt->mbus_code);
983
984 if (!enable)
985 return 0;
986 return sensor_reg_init(info);
987 }
988
sensor_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)989 static int sensor_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
990 struct v4l2_mbus_config *cfg)
991 {
992 cfg->type = V4L2_MBUS_CSI2_DPHY;
993 cfg->flags = 0 | V4L2_MBUS_CSI2_4_LANE | V4L2_MBUS_CSI2_CHANNEL_0;
994
995 return 0;
996 }
997
sensor_g_ctrl(struct v4l2_ctrl * ctrl)998 static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
999 {
1000 struct sensor_info *info =
1001 container_of(ctrl->handler, struct sensor_info, handler);
1002 struct v4l2_subdev *sd = &info->sd;
1003
1004 switch (ctrl->id) {
1005 case V4L2_CID_GAIN:
1006 return sensor_g_gain(sd, &ctrl->val);
1007 case V4L2_CID_EXPOSURE:
1008 return sensor_g_exp(sd, &ctrl->val);
1009 }
1010 return -EINVAL;
1011 }
1012
sensor_s_ctrl(struct v4l2_ctrl * ctrl)1013 static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
1014 {
1015 struct sensor_info *info =
1016 container_of(ctrl->handler, struct sensor_info, handler);
1017 struct v4l2_subdev *sd = &info->sd;
1018
1019 switch (ctrl->id) {
1020 case V4L2_CID_GAIN:
1021 return sensor_s_gain(sd, ctrl->val);
1022 case V4L2_CID_EXPOSURE:
1023 return sensor_s_exp(sd, ctrl->val);
1024 }
1025 return -EINVAL;
1026 }
1027
1028 /* ----------------------------------------------------------------------- */
1029
1030 static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
1031 .g_volatile_ctrl = sensor_g_ctrl,
1032 .s_ctrl = sensor_s_ctrl,
1033 };
1034
1035 static const struct v4l2_subdev_core_ops sensor_core_ops = {
1036 .reset = sensor_reset,
1037 .init = sensor_init,
1038 .s_power = sensor_power,
1039 .ioctl = sensor_ioctl,
1040 #ifdef CONFIG_COMPAT
1041 .compat_ioctl32 = sensor_compat_ioctl32,
1042 #endif
1043 };
1044
1045 static const struct v4l2_subdev_video_ops sensor_video_ops = {
1046 .s_stream = sensor_s_stream,
1047 };
1048
1049 static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
1050 .enum_mbus_code = sensor_enum_mbus_code,
1051 .enum_frame_size = sensor_enum_frame_size,
1052 .get_fmt = sensor_get_fmt,
1053 .set_fmt = sensor_set_fmt,
1054 .get_mbus_config = sensor_g_mbus_config,
1055 };
1056
1057 static const struct v4l2_subdev_ops sensor_ops = {
1058 .core = &sensor_core_ops,
1059 .video = &sensor_video_ops,
1060 .pad = &sensor_pad_ops,
1061 };
1062
1063 /* ----------------------------------------------------------------------- */
1064 static struct cci_driver cci_drv = {
1065 .name = SENSOR_NAME,
1066 .addr_width = CCI_BITS_16,
1067 .data_width = CCI_BITS_8,
1068 };
1069
1070 static const struct v4l2_ctrl_config sensor_custom_ctrls[] = {
1071 {
1072 .ops = &sensor_ctrl_ops,
1073 .id = V4L2_CID_FRAME_RATE,
1074 .name = "frame rate",
1075 .type = V4L2_CTRL_TYPE_INTEGER,
1076 .min = 15,
1077 .max = 120,
1078 .step = 1,
1079 .def = 120,
1080 },
1081 };
1082
sensor_init_controls(struct v4l2_subdev * sd,const struct v4l2_ctrl_ops * ops)1083 static int sensor_init_controls(struct v4l2_subdev *sd, const struct v4l2_ctrl_ops *ops)
1084 {
1085 struct sensor_info *info = to_state(sd);
1086 struct v4l2_ctrl_handler *handler = &info->handler;
1087 struct v4l2_ctrl *ctrl;
1088 int i;
1089 int ret = 0;
1090
1091 v4l2_ctrl_handler_init(handler, 2 + ARRAY_SIZE(sensor_custom_ctrls));
1092
1093 ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 0,
1094 65536 * 16, 1, 0);
1095 if (ctrl != NULL)
1096 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1097 v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 1 * 1600,
1098 256 * 1600, 1, 1 * 1600);
1099 for (i = 0; i < ARRAY_SIZE(sensor_custom_ctrls); i++)
1100 v4l2_ctrl_new_custom(handler, &sensor_custom_ctrls[i], NULL);
1101
1102 if (handler->error) {
1103 ret = handler->error;
1104 v4l2_ctrl_handler_free(handler);
1105 }
1106
1107 sd->ctrl_handler = handler;
1108
1109 return ret;
1110 }
1111
sensor_probe(struct i2c_client * client,const struct i2c_device_id * id)1112 static int sensor_probe(struct i2c_client *client,
1113 const struct i2c_device_id *id)
1114 {
1115 struct v4l2_subdev *sd;
1116 struct sensor_info *info;
1117
1118 info = kzalloc(sizeof(struct sensor_info), GFP_KERNEL);
1119 if (info == NULL)
1120 return -ENOMEM;
1121 sd = &info->sd;
1122 cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv);
1123 sensor_init_controls(sd, &sensor_ctrl_ops);
1124 mutex_init(&info->lock);
1125
1126 #ifdef CONFIG_SAME_I2C
1127 info->sensor_i2c_addr = I2C_ADDR >> 1;
1128 #endif
1129 info->fmt = &sensor_formats[0];
1130 info->fmt_pt = &sensor_formats[0];
1131 info->win_pt = &sensor_win_sizes[0];
1132 info->fmt_num = N_FMTS;
1133 info->win_size_num = N_WIN_SIZES;
1134 info->sensor_field = V4L2_FIELD_NONE;
1135 info->af_first_flag = 1;
1136
1137 return 0;
1138 }
sensor_remove(struct i2c_client * client)1139 static int sensor_remove(struct i2c_client *client)
1140 {
1141 struct v4l2_subdev *sd;
1142
1143 sd = cci_dev_remove_helper(client, &cci_drv);
1144 kfree(to_state(sd));
1145 return 0;
1146 }
1147
1148 static const struct i2c_device_id sensor_id[] = {
1149 {SENSOR_NAME, 0},
1150 {}
1151 };
1152
1153 MODULE_DEVICE_TABLE(i2c, sensor_id);
1154
1155 static struct i2c_driver sensor_driver = {
1156 .driver = {
1157 .owner = THIS_MODULE,
1158 .name = SENSOR_NAME,
1159 },
1160 .probe = sensor_probe,
1161 .remove = sensor_remove,
1162 .id_table = sensor_id,
1163 };
init_sensor(void)1164 static __init int init_sensor(void)
1165 {
1166 return cci_dev_init_helper(&sensor_driver);
1167 }
1168
exit_sensor(void)1169 static __exit void exit_sensor(void)
1170 {
1171 cci_dev_exit_helper(&sensor_driver);
1172 }
1173
1174 module_init(init_sensor);
1175 module_exit(exit_sensor);
1176