• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * A V4L2 driver for s5k3h5xa Raw cameras.
3  *
4  * Copyright (c) 2017 by Allwinnertech Co., Ltd.  http://www.allwinnertech.com
5  *
6  * Authors:  Zhao Wei <zhaowei@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("myf");
29 MODULE_DESCRIPTION("A low-level driver for s5k3h5xa sensors");
30 MODULE_LICENSE("GPL");
31 
32 #define MCLK              (24*1000*1000)
33 #define V4L2_IDENT_SENSOR 0x0535
34 
35 /*
36  * Our nominal (default) frame rate.
37  */
38 
39 #define SENSOR_FRAME_RATE 30
40 
41 /*
42  * The s5k3h5xa i2c address
43  */
44 #define I2C_ADDR 0x20
45 
46 #define SENSOR_NUM 0x2
47 #define SENSOR_NAME "s5k3h5xa"
48 #define SENSOR_NAME_2 "s5k3h5xa_2"
49 
50 /*
51  * The default register settings
52  */
53 
54 static struct regval_list sensor_default_regs[] = {
55 	{0xFCFC, 0xD000},
56 	{0x6010, 0x0001},
57 
58 	{0x6028, 0x7000},
59 	{0x602a, 0x1870},
60 	{0x6F12, 0x2DE9},
61 	{0x6F12, 0xF84F},
62 	{0x6F12, 0x9FE5},
63 	{0x6F12, 0x6081},
64 	{0x6F12, 0xA0E1},
65 	{0x6F12, 0x0090},
66 	{0x6F12, 0x98E5},
67 	{0x6F12, 0x00A0},
68 	{0x6F12, 0x9FE5},
69 	{0x6F12, 0x5071},
70 	{0x6F12, 0x9FE5},
71 	{0x6F12, 0x5401},
72 	{0x6F12, 0xD7E1},
73 	{0x6F12, 0xBE5E},
74 	{0x6F12, 0xD0E1},
75 	{0x6F12, 0xBC03},
76 	{0x6F12, 0xDAE5},
77 	{0x6F12, 0x9D10},
78 	{0x6F12, 0xDAE5},
79 	{0x6F12, 0x9FB0},
80 	{0x6F12, 0x40E0},
81 	{0x6F12, 0x0100},
82 	{0x6F12, 0x00E0},
83 	{0x6F12, 0x9500},
84 	{0x6F12, 0x55E1},
85 	{0x6F12, 0x0B00},
86 	{0x6F12, 0xA0E1},
87 	{0x6F12, 0x2005},
88 	{0x6F12, 0x002A},
89 	{0x6F12, 0x1B00},
90 	{0x6F12, 0x5BE1},
91 	{0x6F12, 0x0000},
92 	{0x6F12, 0x008A},
93 	{0x6F12, 0x0B00},
94 	{0x6F12, 0xA0E3},
95 	{0x6F12, 0x0040},
96 	{0x6F12, 0xC7E1},
97 	{0x6F12, 0xBEBE},
98 	{0x6F12, 0x87E0},
99 	{0x6F12, 0x0461},
100 	{0x6F12, 0x96E5},
101 	{0x6F12, 0xF800},
102 	{0x6F12, 0xA0E1},
103 	{0x6F12, 0x0B10},
104 	{0x6F12, 0x00E0},
105 	{0x6F12, 0x9500},
106 	{0x6F12, 0x00EB},
107 	{0x6F12, 0x4800},
108 	{0x6F12, 0x84E2},
109 	{0x6F12, 0x0140},
110 	{0x6F12, 0x54E3},
111 	{0x6F12, 0x0400},
112 	{0x6F12, 0x86E5},
113 	{0x6F12, 0xF800},
114 	{0x6F12, 0xFF3A},
115 	{0x6F12, 0xF6FF},
116 	{0x6F12, 0x00EA},
117 	{0x6F12, 0x0D00},
118 	{0x6F12, 0xDAE5},
119 	{0x6F12, 0x9E00},
120 	{0x6F12, 0x50E1},
121 	{0x6F12, 0x0500},
122 	{0x6F12, 0x002A},
123 	{0x6F12, 0x0A00},
124 	{0x6F12, 0xA0E3},
125 	{0x6F12, 0x0040},
126 	{0x6F12, 0xC7E1},
127 	{0x6F12, 0xBE0E},
128 	{0x6F12, 0x87E0},
129 	{0x6F12, 0x0461},
130 	{0x6F12, 0x96E5},
131 	{0x6F12, 0xF800},
132 	{0x6F12, 0xDAE5},
133 	{0x6F12, 0x9E10},
134 	{0x6F12, 0x00E0},
135 	{0x6F12, 0x9500},
136 	{0x6F12, 0x00EB},
137 	{0x6F12, 0x3900},
138 	{0x6F12, 0x84E2},
139 	{0x6F12, 0x0140},
140 	{0x6F12, 0x54E3},
141 	{0x6F12, 0x0400},
142 	{0x6F12, 0x86E5},
143 	{0x6F12, 0xF800},
144 	{0x6F12, 0xFF3A},
145 	{0x6F12, 0xF6FF},
146 	{0x6F12, 0x9FE5},
147 	{0x6F12, 0xC000},
148 	{0x6F12, 0x87E2},
149 	{0x6F12, 0xC830},
150 	{0x6F12, 0x8DE5},
151 	{0x6F12, 0x0030},
152 	{0x6F12, 0xD0E5},
153 	{0x6F12, 0xB920},
154 	{0x6F12, 0xD0E1},
155 	{0x6F12, 0xB618},
156 	{0x6F12, 0x9FE5},
157 	{0x6F12, 0xB000},
158 	{0x6F12, 0xA0E1},
159 	{0x6F12, 0x0930},
160 	{0x6F12, 0x90E5},
161 	{0x6F12, 0x28C0},
162 	{0x6F12, 0x87E2},
163 	{0x6F12, 0xE800},
164 	{0x6F12, 0xA0E1},
165 	{0x6F12, 0x0FE0},
166 	{0x6F12, 0x2FE1},
167 	{0x6F12, 0x1CFF},
168 	{0x6F12, 0x50E3},
169 	{0x6F12, 0x0000},
170 	{0x6F12, 0x001B},
171 	{0x6F12, 0x2A00},
172 	{0x6F12, 0x9FE5},
173 	{0x6F12, 0x9400},
174 	{0x6F12, 0x98E5},
175 	{0x6F12, 0x0010},
176 	{0x6F12, 0xD0E1},
177 	{0x6F12, 0xBA05},
178 	{0x6F12, 0xD1E5},
179 	{0x6F12, 0x9C10},
180 	{0x6F12, 0x9FE5},
181 	{0x6F12, 0x8830},
182 	{0x6F12, 0x50E1},
183 	{0x6F12, 0x0100},
184 	{0x6F12, 0xA0E3},
185 	{0x6F12, 0x0000},
186 	{0x6F12, 0x002A},
187 	{0x6F12, 0x1300},
188 	{0x6F12, 0x00EA},
189 	{0x6F12, 0x0500},
190 	{0x6F12, 0xD1E1},
191 	{0x6F12, 0xB210},
192 	{0x6F12, 0x82E2},
193 	{0x6F12, 0x0D22},
194 	{0x6F12, 0xC2E1},
195 	{0x6F12, 0xB010},
196 	{0x6F12, 0x80E2},
197 	{0x6F12, 0x0100},
198 	{0x6F12, 0x50E3},
199 	{0x6F12, 0x1400},
200 	{0x6F12, 0x00AA},
201 	{0x6F12, 0x0400},
202 	{0x6F12, 0x80E0},
203 	{0x6F12, 0x8010},
204 	{0x6F12, 0x83E0},
205 	{0x6F12, 0x8110},
206 	{0x6F12, 0xD1E1},
207 	{0x6F12, 0xB020},
208 	{0x6F12, 0x52E3},
209 	{0x6F12, 0x0000},
210 	{0x6F12, 0xFF1A},
211 	{0x6F12, 0xF4FF},
212 	{0x6F12, 0xBDE8},
213 	{0x6F12, 0xF84F},
214 	{0x6F12, 0x2FE1},
215 	{0x6F12, 0x1EFF},
216 	{0x6F12, 0xD1E1},
217 	{0x6F12, 0xB410},
218 	{0x6F12, 0x82E2},
219 	{0x6F12, 0x0D22},
220 	{0x6F12, 0xC2E1},
221 	{0x6F12, 0xB010},
222 	{0x6F12, 0x80E2},
223 	{0x6F12, 0x0100},
224 	{0x6F12, 0x50E3},
225 	{0x6F12, 0x1400},
226 	{0x6F12, 0xFFAA},
227 	{0x6F12, 0xF7FF},
228 	{0x6F12, 0x80E0},
229 	{0x6F12, 0x8010},
230 	{0x6F12, 0x83E0},
231 	{0x6F12, 0x8110},
232 	{0x6F12, 0xD1E1},
233 	{0x6F12, 0xB020},
234 	{0x6F12, 0x52E3},
235 	{0x6F12, 0x0000},
236 	{0x6F12, 0xFF1A},
237 	{0x6F12, 0xF4FF},
238 	{0x6F12, 0xFFEA},
239 	{0x6F12, 0xF1FF},
240 	{0x6F12, 0x0070},
241 	{0x6F12, 0x7014},
242 	{0x6F12, 0x0070},
243 	{0x6F12, 0x0000},
244 	{0x6F12, 0x0070},
245 	{0x6F12, 0x800C},
246 	{0x6F12, 0x0070},
247 	{0x6F12, 0x3013},
248 	{0x6F12, 0x0070},
249 	{0x6F12, 0xF804},
250 	{0x6F12, 0x0070},
251 	{0x6F12, 0x7015},
252 	{0x6F12, 0x0070},
253 	{0x6F12, 0x002D},
254 	{0x6F12, 0x1FE5},
255 	{0x6F12, 0x04F0},
256 	{0x6F12, 0x0000},
257 	{0x6F12, 0x20E1},
258 	{0x6F12, 0x1FE5},
259 	{0x6F12, 0x04F0},
260 	{0x6F12, 0x0000},
261 	{0x6F12, 0x641C},
262 	{0x6028, 0xD000},
263 
264 
265 	{0x3902, 0x0002},
266 	{0x3158, 0x0215},
267 	{0x32B4, 0xF4B6},
268 	{0x32B6, 0xF466},
269 	{0x32B8, 0xF456},
270 	{0x32BA, 0xF45E},
271 	{0x32BC, 0x10  },
272 	{0x32BD, 0x00  },
273 	{0x32BE, 0x00  },
274 	{0x3338, 0x0214},
275 	{0x6218, 0xF1D0},
276 	{0x6214, 0xF9F0},
277 	{0x6226, 0x0001},
278 	{0xF446, 0x0029},
279 	{0xF448, 0x001D},
280 	{0xF440, 0x0071},
281 	{0xF42E, 0x00C1},
282 	{0xF42A, 0x0802},
283 	{0xB0C8, 0x0044},
284 	{0x6226, 0x0000},
285 	{0x6218, 0xF9F0},
286 	{0x34A2, 0x00D6},
287 	{0x34B2, 0x01FA},
288 	{0x34CA, 0x00D6},
289 	{0x34DA, 0x01FA},
290 	{0x3522, 0x00D6},
291 	{0x3532, 0x01FA},
292 	{0x3254, 0x79D3},
293 	{0x3256, 0x79D3},
294 	{0x3258, 0x79D3},
295 	{0x325A, 0x79D3},
296 	{0x325C, 0x79D3},
297 	{0x325E, 0x79D3},
298 	{0x357A, 0x00BD},
299 	{0x32F6, 0x1110},
300 	{0x012C, 0x60  },
301 	{0x012D, 0x4F  },
302 	{0x012E, 0x2F  },
303 	{0x012F, 0x40  },
304 	{0x6028, 0x7000},
305 	{0x602A, 0x2D00},
306 	{0x6F12, 0x30F4},
307 	{0x6F12, 0xD370},
308 	{0x6F12, 0xD379},
309 	{0x6F12, 0x12F4},
310 	{0x6F12, 0x0500},
311 	{0x6F12, 0x0100},
312 	{0x6F12, 0x4638},
313 	{0x6F12, 0x0007},
314 	{0x6F12, 0xF004},
315 	{0x6F12, 0x5038},
316 	{0x6F12, 0x0000},
317 	{0x6F12, 0x1002},
318 	{0x6F12, 0xF838},
319 	{0x6F12, 0xFAFF},
320 	{0x6F12, 0x0000},
321 	{0x6F12, 0x4C38},
322 	{0x6F12, 0x7805},
323 	{0x6F12, 0x9C04},
324 	{0x6F12, 0x78F4},
325 	{0x6F12, 0x0700},
326 	{0x6F12, 0x0700},
327 	{0x6F12, 0x9AF4},
328 	{0x6F12, 0x3100},
329 	{0x6F12, 0x3100},
330 	{0x6F12, 0x36F4},
331 	{0x6F12, 0x0600},
332 	{0x6F12, 0x0600},
333 	{0x6F12, 0x0000},
334 	{0x6028, 0xD000},
335 	{0x6226, 0x0001},
336 	{0x6100, 0x0003},
337 	{0x6110, 0x1CA0},
338 	{0x6112, 0x1CA4},
339 	{0x6150, 0x172C},
340 	{0x6152, 0x1730},
341 	{0x6028, 0x7000},
342 	{0x602A, 0x172C},
343 	{0x6F12, 0x1FE5},
344 	{0x6F12, 0x04F0},
345 	{0x6F12, 0x0070},
346 	{0x6F12, 0x7018},
347 	{0x6028, 0xD000},
348 	{0x6226, 0x0000},
349 };
350 
351 #if 0
352 static struct regval_list sensor_8M_30fps_regs[] = {
353 	/* 3264 * 2448 _ 30fps */
354 	{0x0100, 0x00  },// software stanby
355 	{0x0112, 0x0A0A},// 0a0a: Top 10bit of pixel data,RAW10
356 	{0x0114, 0x03  },
357 	{0x0120, 0x00  },//0:Global Analogue Gain
358 	{0x0200, 0x0BEF},//Fine integration time(pixels)  = 3055
359 	{0x0202, 0x09D9},//Coarse integration time(lines) = 2521
360 	{0x0204, 0x0020},//Global Analogue Gain Code = 32
361 	{0x0300, 0x0002},//Value Timing Pixel Clock Divider = 2
362 	{0x0302, 0x0001},//Video Timing system Clock Divider Value = 1
363 	{0x0304, 0x0006},//Pre PLL clock Divider Value = 6
364 	{0x0306, 0x008C},//PLL multiplier Value = 140
365 	{0x0308, 0x0008},//output Pixel Clock Divider = 8
366 	{0x030A, 0x0001},//output system clock Divider Value = 1
367 	{0x030C, 0x0006},//2nd PLL Pre clock Divider value = 6
368 	{0x030E, 0x00A5},//2nd PLL multiplier value = 165
369 
370 	{0x0340, 0x09E2},//frame_length_lines = 2530 Frame_Length --- VTS
371 	{0x0342, 0x0E68},//line_length_pck = 3688 Line Length --- HTS
372 
373 	{0x32CE, 0x0094},
374 	{0x32D0, 0x0024},
375 	{0x0344, 0x0008},//x_addr_start = 8
376 	{0x0346, 0x0008},//y_addr_start = 8
377 	{0x0348, 0x0CC7},//x_addr_end = 3271
378 	{0x034A, 0x0997},//y_addr_end = 2445
379 	{0x034C, 0x0CC0},//x_output_size = 3264
380 	{0x034E, 0x0990},//y_output_size = 2448
381 	{0x0380, 0x0001},//x_even_inc = 1
382 	{0x0382, 0x0001},//x_odd_inc = 1
383 	{0x0384, 0x0001},//y_even_inc = 1 Increment for even pixels = 0,2,4 etc
384 	{0x0386, 0x0001},//y_odd_inc = 1 Increment for odd pixels = 1,3,5etc
385 	{0x0900, 0x01  },
386 	{0x0901, 0x11  },
387 	{0x0902, 0x01  },
388 	//scale
389 	{0x0400, 0x0000},//horizontal scaling
390 	{0x3011, 0x01  },//No binning
391 	{0x3293, 0x00  },
392 	{0x0100, 0x01  },//streaming
393 };
394 #endif
395 
396 static struct regval_list sensor_6M_25fps_regs[] = {
397 	/* 3264 * 1836 _ 25fps */
398 	{0x0100, 0x00  },// software stanby
399 	{0x0112, 0x0A0A},// 0a0a: Top 10bit of pixel data,RAW10
400 	{0x0114, 0x03  },
401 	{0x0120, 0x00  },//0:Global Analogue Gain
402 	{0x0200, 0x0BEF},//Fine integration time(pixels)  = 3055
403 	{0x0202, 0x09D9},//Coarse integration time(lines) = 2521
404 	{0x0204, 0x0020},//Global Analogue Gain Code = 32
405 	{0x0300, 0x0002},//Value Timing Pixel Clock Divider = 2
406 	{0x0302, 0x0001},//Video Timing system Clock Divider Value = 1
407 	{0x0304, 0x0006},//Pre PLL clock Divider Value = 6
408 	{0x0306, 0x008C},//PLL multiplier Value = 140
409 	{0x0308, 0x0008},//output Pixel Clock Divider = 8
410 	{0x030A, 0x0001},//output system clock Divider Value = 1
411 	{0x030C, 0x0006},//2nd PLL Pre clock Divider value = 6
412 	{0x030E, 0x00A5},//2nd PLL multiplier value = 165
413 
414 	{0x0340, 0x09e2},//frame_length_lines = 2530 Frame_Length --- VTS
415 	{0x0342, 0x1144},//line_length_pck = 4420 Line Length --- HTS
416 
417 	{0x32CE, 0x0094},
418 	{0x32D0, 0x0024},
419 
420 	{0x0344, 0x0008},//x_addr_start = 8
421 	{0x0346, 0x0008},//y_addr_start = 314
422 	{0x0348, 0x0CC7},//x_addr_end = 3271
423 	{0x034A, 0x0997},//y_addr_end = 2149
424 	{0x034C, 0x0CC0},//x_output_size = 3264
425 	{0x034E, 0x072c},//y_output_size = 1836
426 
427 	{0x0380, 0x0001},//x_even_inc = 1
428 	{0x0382, 0x0001},//x_odd_inc = 1
429 	{0x0384, 0x0001},//y_even_inc = 1 Increment for even pixels = 0,2,4 etc
430 	{0x0386, 0x0001}, //y_odd_inc = 1 Increment for odd pixels = 1,3,5etc
431 
432 	{0x0900, 0x01  },//binning_mode  1:ENABLE
433 	{0x0901, 0x11  },//binning type
434 	{0x0902, 0x01  },// binning_weighting
435 	//scale
436 	{0x0400, 0x0000},//horizontal scaling
437 	{0x3011, 0x01  },//No binning
438 	{0x3293, 0x00  },
439 	{0x0100, 0x01  },//streaming
440 
441 };
442 
sensor_read_byte(struct v4l2_subdev * sd,unsigned short reg,unsigned char * value)443 static int sensor_read_byte(struct v4l2_subdev *sd, unsigned short reg,
444 	unsigned char *value)
445 {
446 	int ret = 0, cnt = 0;
447 
448 	if (!sd || !sd->entity.use_count) {
449 		sensor_print("%s error! sensor is not used!\n", __func__);
450 		return -1;
451 	}
452 
453 	ret = cci_read_a16_d8(sd, reg, value);
454 	while ((ret != 0) && (cnt < 2)) {
455 		ret = cci_read_a16_d8(sd, reg, value);
456 		cnt++;
457 	}
458 	if (cnt > 0)
459 		pr_info("%s sensor read retry = %d\n", sd->name, cnt);
460 
461 	return ret;
462 }
463 
sensor_write_byte(struct v4l2_subdev * sd,unsigned short reg,unsigned char value)464 static int sensor_write_byte(struct v4l2_subdev *sd, unsigned short reg,
465 	unsigned char value)
466 {
467 	int ret = 0, cnt = 0;
468 
469 	if (!sd || !sd->entity.use_count) {
470 		sensor_print("%s error! sensor is not used!\n", __func__);
471 		return -1;
472 	}
473 
474 	ret = cci_write_a16_d8(sd, reg, value);
475 	while ((ret != 0) && (cnt < 2)) {
476 		ret = cci_write_a16_d8(sd, reg, value);
477 		cnt++;
478 	}
479 	if (cnt > 0)
480 		pr_info("%s sensor write retry = %d\n", sd->name, cnt);
481 
482 	return ret;
483 }
484 
s5k3h5xa_write_array(struct v4l2_subdev * sd,struct regval_list * regs,int array_size)485 static int s5k3h5xa_write_array(struct v4l2_subdev *sd, struct regval_list *regs, int array_size)
486 {
487 	int i = 0, ret = 0;
488 
489 	if (!regs)
490 		return -EINVAL;
491 
492 	while (i < array_size) {
493 		if (regs->addr == REG_DLY) {
494 			usleep_range(regs->data * 1000, regs->data * 1000 + 100);
495 		} else {
496 			//if(regs->width==16)
497 			//	ret = sensor_write(sd, regs->addr, regs->data);
498 			//else if(regs->width==8)
499 			//	ret = sensor_write_byte(sd, regs->addr, regs->data);
500 			if (regs->addr == 0x32bc || regs->addr == 0x32bd || regs->addr == 0x32be ||
501 				regs->addr == 0x012c || regs->addr == 0x012d || regs->addr == 0x012e ||
502 				regs->addr == 0x012f || regs->addr == 0x0100 || regs->addr == 0x0114 ||
503 				regs->addr == 0x0120 || regs->addr == 0x0900 || regs->addr == 0x0901 ||
504 				regs->addr == 0x0902 || regs->addr == 0x3011 || regs->addr == 0x3293)
505 				ret = sensor_write_byte(sd, regs->addr, regs->data);
506 			else
507 				ret = sensor_write(sd, regs->addr, regs->data);
508 
509 			if (ret < 0) {
510 				sensor_print("%s sensor write array error, array_size %d!\n", sd->name, array_size);
511 				return -1;
512 			}
513 		}
514 		i++;
515 		regs++;
516 	}
517 	return 0;
518 }
519 
520 /*
521  * Here we'll try to encapsulate the changes for just the output
522  * video format.
523  *
524  */
525 
526 static struct regval_list sensor_fmt_raw[] = {
527 
528 };
529 
530 /*
531  * Code for dealing with controls.
532  * fill with different sensor module
533  * different sensor module has different settings here
534  * if not support the follow function ,retrun -EINVAL
535  */
sensor_g_exp(struct v4l2_subdev * sd,__s32 * value)536 static int sensor_g_exp(struct v4l2_subdev *sd, __s32 *value)
537 {
538 	struct sensor_info *info = to_state(sd);
539 	*value = info->exp;
540 	sensor_dbg("sensor_get_exposure = %d\n", info->exp);
541 	return 0;
542 }
543 
544 static int s5k3h5xa_sensor_vts;
545 static int s5k3h5xa_sensor_hts;
546 
sensor_s_exp(struct v4l2_subdev * sd,unsigned int exp_val)547 static int sensor_s_exp(struct v4l2_subdev *sd, unsigned int exp_val)
548 {
549 	unsigned int exp_coarse;
550 	unsigned short exp_fine;
551 	struct sensor_info *info = to_state(sd);
552 
553 	if (exp_val > 0xffffff)
554 		exp_val = 0xfffff0;
555 	if (exp_val < 16)
556 		exp_val = 16;
557 
558 	if (info->exp == exp_val)
559 		return 0;
560 
561 	exp_coarse = exp_val >> 4;//rounding to 1
562 	//exp_fine = (exp_val - exp_coarse * 16) * info->current_wins->hts / 16;
563 	exp_fine = (unsigned short) (((exp_val - exp_coarse * 16) * s5k3h5xa_sensor_hts) / 16);
564 
565 	sensor_write(sd, 0x0200, exp_fine);
566 	sensor_write(sd, 0x0202, (unsigned short)exp_coarse);
567 
568 	info->exp = exp_val;
569 	return 0;
570 }
571 
sensor_g_gain(struct v4l2_subdev * sd,__s32 * value)572 static int sensor_g_gain(struct v4l2_subdev *sd, __s32 *value)
573 {
574 	struct sensor_info *info = to_state(sd);
575 	*value = info->gain;
576 	sensor_dbg("sensor_get_gain = %d\n", info->gain);
577 	return 0;
578 }
579 
sensor_s_gain(struct v4l2_subdev * sd,int gain_val)580 static int sensor_s_gain(struct v4l2_subdev *sd, int gain_val)
581 {
582 	struct sensor_info *info = to_state(sd);
583 	//	unsigned short gain;
584 	int ana_gain = 0, digi_gain = 0;
585 	unsigned short GR_GAIN, R_GAIN, B_GAIN, GB_GAIN;
586 
587 	if (info->gain == gain_val)
588 		return 0;
589 
590 	if (gain_val <= 1 * 16) {
591 		ana_gain = 16;
592 		digi_gain = 256;
593 	} else if (gain_val > 16 && gain_val <= (16 * 16 - 1)) {
594 		ana_gain = gain_val;
595 		digi_gain = 256;
596 	} else if (gain_val > (16 * 16 - 1) && gain_val < (32 * 16 - 1)) {
597 		ana_gain = 16 * 16 - 1;
598 		digi_gain = (gain_val - 255 + 16) * 1 + 256;
599 	} else {
600 		ana_gain = 16 * 16 - 1;
601 		digi_gain = 512;
602 	}
603 	ana_gain *= 2;//shift to 1/32 step
604 	GR_GAIN = (unsigned short)digi_gain;
605 	R_GAIN  = (unsigned short)digi_gain;
606 	B_GAIN  = (unsigned short)digi_gain;
607 	GB_GAIN = (unsigned short)digi_gain;
608 //	gain = gain_val * 2;//shift to 1/32 step
609 
610 //	sensor_write(sd, 0x0204, gain);
611 	sensor_write(sd, 0x0204, (unsigned short)ana_gain);
612 	sensor_write(sd, 0x020e, GR_GAIN);
613 	sensor_write(sd, 0x0210, R_GAIN);
614 	sensor_write(sd, 0x0212, B_GAIN);
615 	sensor_write(sd, 0x0214, GB_GAIN);
616 
617 	info->gain = gain_val;
618 
619 	return 0;
620 }
621 
622 
sensor_s_exp_gain(struct v4l2_subdev * sd,struct sensor_exp_gain * exp_gain)623 static int sensor_s_exp_gain(struct v4l2_subdev *sd,
624 			     struct sensor_exp_gain *exp_gain)
625 {
626 	int exp_val, gain_val, shutter, frame_length;
627 	struct sensor_info *info = to_state(sd);
628 
629 	exp_val = exp_gain->exp_val;
630 	gain_val = exp_gain->gain_val;
631 
632 	shutter = exp_val >> 4;
633 
634 	if (shutter  > s5k3h5xa_sensor_vts - 4)
635 		frame_length = shutter + 4;
636 	else
637 		frame_length = s5k3h5xa_sensor_vts;
638 
639 	sensor_write_byte(sd, 0x0104, 0x01);
640     sensor_write(sd, 0x0340, frame_length);
641     sensor_s_gain(sd, gain_val);
642     sensor_s_exp(sd, exp_val);
643     sensor_write_byte(sd, 0x0104, 0x00);
644 
645 	info->exp = exp_val;
646 	info->gain = gain_val;
647 	return 0;
648 }
649 
sensor_s_sw_stby(struct v4l2_subdev * sd,int on_off)650 static int sensor_s_sw_stby(struct v4l2_subdev *sd, int on_off)
651 {
652 	int ret;
653 	unsigned char rdval;
654 
655 	ret = sensor_read_byte(sd, 0x0100, &rdval);
656 	if (ret != 0)
657 		return ret;
658 
659 	if (on_off == STBY_ON)
660 		ret = sensor_write_byte(sd, 0x0100, rdval & 0xfe);
661 	else
662 		ret = sensor_write_byte(sd, 0x0100, rdval | 0x01);
663 	return ret;
664 }
665 
666 /*
667  * Stuff that knows about the sensor.
668  */
sensor_power(struct v4l2_subdev * sd,int on)669 static int sensor_power(struct v4l2_subdev *sd, int on)
670 {
671 	int ret = 0;
672 
673 	switch (on) {
674 	case STBY_ON:
675 		sensor_dbg("STBY_ON!\n");
676 		cci_lock(sd);
677 		ret = sensor_s_sw_stby(sd, STBY_ON);
678 		if (ret < 0)
679 			sensor_err("soft stby falied!\n");
680 		usleep_range(10000, 12000);
681 		cci_unlock(sd);
682 		break;
683 	case STBY_OFF:
684 		sensor_dbg("STBY_OFF!\n");
685 		cci_lock(sd);
686 		usleep_range(10000, 12000);
687 		ret = sensor_s_sw_stby(sd, STBY_OFF);
688 		if (ret < 0)
689 			sensor_err("soft stby off falied!\n");
690 		cci_unlock(sd);
691 		break;
692 	case PWR_ON:
693 		sensor_dbg("PWR_ON!\n");
694 		cci_lock(sd);
695 		vin_gpio_set_status(sd, PWDN, 1);
696 		vin_gpio_set_status(sd, RESET, 1); /* RESET port set to output mode and pull-up */
697 		vin_gpio_set_status(sd, POWER_EN, 1);
698 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
699 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
700 		vin_gpio_write(sd, POWER_EN, CSI_GPIO_HIGH);
701 		usleep_range(1000, 1200);
702 		vin_set_pmu_channel(sd, IOVDD, ON);
703 		usleep_range(1000, 1200);
704 		vin_set_pmu_channel(sd, AVDD, ON);
705 		vin_set_pmu_channel(sd, DVDD, ON);
706 		usleep_range(10000, 12000);
707 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
708 		vin_gpio_write(sd, PWDN, CSI_GPIO_HIGH);
709 		usleep_range(10000, 12000);
710 		vin_set_mclk(sd, ON);
711 		usleep_range(10000, 12000);
712 		vin_set_mclk_freq(sd, MCLK);
713 		usleep_range(30000, 32000);
714 		cci_unlock(sd);
715 		break;
716 	case PWR_OFF:
717 		sensor_dbg("PWR_OFF!\n");
718 		cci_lock(sd);
719 		vin_gpio_set_status(sd, PWDN, 1);
720 		vin_gpio_set_status(sd, RESET, 1);
721 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
722 		vin_gpio_write(sd, PWDN, CSI_GPIO_LOW);
723 		vin_set_mclk(sd, OFF);
724 		vin_set_pmu_channel(sd, AFVDD, OFF);
725 		vin_set_pmu_channel(sd, AVDD, OFF);
726 		vin_set_pmu_channel(sd, IOVDD, OFF);
727 		vin_set_pmu_channel(sd, DVDD, OFF);
728 		vin_gpio_write(sd, POWER_EN, CSI_GPIO_LOW);
729 		vin_gpio_set_status(sd, RESET, 0);
730 		vin_gpio_set_status(sd, PWDN, 0);
731 		vin_gpio_set_status(sd, POWER_EN, 0);
732 		cci_unlock(sd);
733 		break;
734 	default:
735 		return -EINVAL;
736 	}
737 
738 	return 0;
739 }
740 
sensor_reset(struct v4l2_subdev * sd,u32 val)741 static int sensor_reset(struct v4l2_subdev *sd, u32 val)
742 {
743 	switch (val) {
744 	case 0:
745 		vin_gpio_write(sd, RESET, CSI_GPIO_HIGH);
746 		usleep_range(100, 120);
747 		break;
748 	case 1:
749 		vin_gpio_write(sd, RESET, CSI_GPIO_LOW);
750 		usleep_range(100, 120);
751 		break;
752 	default:
753 		return -EINVAL;
754 	}
755 	return 0;
756 }
757 
sensor_detect(struct v4l2_subdev * sd)758 static int sensor_detect(struct v4l2_subdev *sd)
759 {
760 	unsigned short rdval = 0;
761 
762 	sensor_read(sd, 0x0000, &rdval);
763 	if (rdval != 0x3085) {
764 		sensor_dbg("read 0x0000: 0x%04x != 0x3085\n", rdval);
765 		return -ENODEV;
766 	}
767 	sensor_dbg("sensor detect success ID = 0x%04x \n", rdval);
768 	return 0;
769 }
770 
sensor_init(struct v4l2_subdev * sd,u32 val)771 static int sensor_init(struct v4l2_subdev *sd, u32 val)
772 {
773 	int ret;
774 	struct sensor_info *info = to_state(sd);
775 
776 	sensor_dbg("sensor_init\n");
777 
778 	/*Make sure it is a target sensor */
779 	ret = sensor_detect(sd);
780 	if (ret) {
781 		sensor_err("chip found is not an target chip.\n");
782 		return ret;
783 	}
784 
785 	info->focus_status = 0;
786 	info->low_speed = 0;
787 	info->width = 3264;
788 	info->height = 2448;
789 	info->hflip = 0;
790 	info->vflip = 0;
791 	info->gain = 0;
792 	info->exp = 0;
793 
794 	info->tpf.numerator = 1;
795 	info->tpf.denominator = 30;	/* 30fps */
796 
797 	return 0;
798 }
799 
sensor_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)800 static long sensor_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
801 {
802 	int ret = 0;
803 	struct sensor_info *info = to_state(sd);
804 
805 	switch (cmd) {
806 	case GET_CURRENT_WIN_CFG:
807 		if (info->current_wins != NULL) {
808 			memcpy(arg, info->current_wins,
809 				sizeof(struct sensor_win_size));
810 			ret = 0;
811 		} else {
812 			sensor_err("empty wins!\n");
813 			ret = -1;
814 		}
815 		break;
816 	case SET_FPS:
817 		ret = 0;
818 		break;
819 	case VIDIOC_VIN_SENSOR_EXP_GAIN:
820 		ret = sensor_s_exp_gain(sd, (struct sensor_exp_gain *)arg);
821 		break;
822 	case VIDIOC_VIN_SENSOR_SET_FPS:
823 		ret = 0;
824 		break;
825 	case VIDIOC_VIN_SENSOR_CFG_REQ:
826 		sensor_cfg_req(sd, (struct sensor_config *)arg);
827 		break;
828 	default:
829 		return -EINVAL;
830 	}
831 	return ret;
832 }
833 
834 /*
835  * Store information about the video data format.
836  */
837 static struct sensor_format_struct sensor_formats[] = {
838 	{
839 		.desc = "Raw RGB Bayer",
840 		.mbus_code = MEDIA_BUS_FMT_SGRBG10_1X10,
841 		.regs = sensor_fmt_raw,
842 		.regs_size = ARRAY_SIZE(sensor_fmt_raw),
843 		.bpp = 1
844 	},
845 };
846 #define N_FMTS ARRAY_SIZE(sensor_formats)
847 
848 /*
849  * Then there is the issue of window sizes.  Try to capture the info here.
850  */
851 
852 static struct sensor_win_size sensor_win_sizes[] = {
853 #if 0
854 	{
855 	.width         = 3264,
856 	.height        = 2448,
857 	.hoffset       = 0,
858 	.voffset       = 0,
859 	.hts           = 3688,
860 	.vts           = 2530,
861 	.pclk          = 280*1000*1000,
862 	.mipi_bps      = 660*1000*1000,
863 	.fps_fixed     = 30,
864 	.bin_factor    = 1,
865 	.intg_min      = 16,
866 	.intg_max      = (2530-4)<<4,
867 	.gain_min      = 16,
868 	.gain_max      = (32<<4),
869 	.regs          = sensor_8M_30fps_regs,
870 	.regs_size     = ARRAY_SIZE(sensor_8M_30fps_regs),
871 	.set_size      = NULL,
872 	},
873 #endif
874 	{
875 	.width         = 3200,
876 	.height        = 1800,
877 	.hoffset       = 32,
878 	.voffset       = 18,
879 	.hts           = 4420,
880 	.vts           = 2530,
881 	.pclk          = 280*1000*1000,
882 	.mipi_bps      = 660*1000*1000,
883 	.fps_fixed     = 25,
884 	.bin_factor    = 1,
885 	.intg_min      = 16,
886 	.intg_max      = (2530-4)<<4,
887 	.gain_min      = 16,
888 	.gain_max      = (32<<4),
889 	.regs          = sensor_6M_25fps_regs,
890 	.regs_size     = ARRAY_SIZE(sensor_6M_25fps_regs),
891 	.set_size      = NULL,
892 	.top_clk       = 310*1000*1000,
893 	.isp_clk       = 286*1000*1000,
894 	},
895 };
896 
897 #define N_WIN_SIZES (ARRAY_SIZE(sensor_win_sizes))
898 
sensor_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)899 static int sensor_g_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
900 				struct v4l2_mbus_config *cfg)
901 {
902 	cfg->type = V4L2_MBUS_CSI2_DPHY;
903 
904 	cfg->flags = 0 | V4L2_MBUS_CSI2_4_LANE | V4L2_MBUS_CSI2_CHANNEL_0;
905 
906 	return 0;
907 }
908 
sensor_g_ctrl(struct v4l2_ctrl * ctrl)909 static int sensor_g_ctrl(struct v4l2_ctrl *ctrl)
910 {
911 
912 	struct sensor_info *info =
913 			container_of(ctrl->handler, struct sensor_info, handler);
914 	struct v4l2_subdev *sd = &info->sd;
915 
916 	switch (ctrl->id) {
917 	case V4L2_CID_GAIN:
918 		return sensor_g_gain(sd, &ctrl->val);
919 	case V4L2_CID_EXPOSURE:
920 		return sensor_g_exp(sd, &ctrl->val);
921 	}
922 	return -EINVAL;
923 }
924 
sensor_s_ctrl(struct v4l2_ctrl * ctrl)925 static int sensor_s_ctrl(struct v4l2_ctrl *ctrl)
926 {
927 	struct sensor_info *info =
928 			container_of(ctrl->handler, struct sensor_info, handler);
929 	struct v4l2_subdev *sd = &info->sd;
930 
931 	switch (ctrl->id) {
932 	case V4L2_CID_GAIN:
933 		return sensor_s_gain(sd, ctrl->val);
934 	case V4L2_CID_EXPOSURE:
935 		return sensor_s_exp(sd, ctrl->val);
936 	}
937 	return -EINVAL;
938 }
939 
sensor_reg_init(struct sensor_info * info)940 static int sensor_reg_init(struct sensor_info *info)
941 {
942 	int ret;
943 	struct v4l2_subdev *sd = &info->sd;
944 	struct sensor_format_struct *sensor_fmt = info->fmt;
945 	struct sensor_win_size *wsize = info->current_wins;
946 
947 	ret = s5k3h5xa_write_array(sd, sensor_default_regs,
948 				 ARRAY_SIZE(sensor_default_regs));
949 	if (ret < 0) {
950 		sensor_err("write sensor_default_regs error\n");
951 		return ret;
952 	}
953 
954 	sensor_dbg("sensor_reg_init\n");
955 
956 	s5k3h5xa_write_array(sd, sensor_fmt->regs, sensor_fmt->regs_size);
957 
958 	if (wsize->regs)
959 		s5k3h5xa_write_array(sd, wsize->regs, wsize->regs_size);
960 
961 	if (wsize->set_size)
962 		wsize->set_size(sd);
963 
964 	info->width = wsize->width;
965 	info->height = wsize->height;
966 	s5k3h5xa_sensor_vts = wsize->vts;
967 	s5k3h5xa_sensor_hts = wsize->hts;
968 
969 	return 0;
970 }
971 
sensor_s_stream(struct v4l2_subdev * sd,int enable)972 static int sensor_s_stream(struct v4l2_subdev *sd, int enable)
973 {
974 	struct sensor_info *info = to_state(sd);
975 
976 	sensor_dbg("%s on = %d, %d*%d fps: %d code: %x\n", __func__, enable,
977 		     info->current_wins->width, info->current_wins->height,
978 		     info->current_wins->fps_fixed, info->fmt->mbus_code);
979 
980 	if (!enable)
981 		return 0;
982 
983 	return sensor_reg_init(info);
984 }
985 
986 /* ----------------------------------------------------------------------- */
987 
988 static const struct v4l2_ctrl_ops sensor_ctrl_ops = {
989 	.g_volatile_ctrl = sensor_g_ctrl,
990 	.s_ctrl = sensor_s_ctrl,
991 };
992 
993 static const struct v4l2_subdev_core_ops sensor_core_ops = {
994 	.reset = sensor_reset,
995 	.init = sensor_init,
996 	.s_power = sensor_power,
997 	.ioctl = sensor_ioctl,
998 #ifdef CONFIG_COMPAT
999 	.compat_ioctl32 = sensor_compat_ioctl32,
1000 #endif
1001 };
1002 
1003 static const struct v4l2_subdev_video_ops sensor_video_ops = {
1004 	.s_stream = sensor_s_stream,
1005 };
1006 
1007 static const struct v4l2_subdev_pad_ops sensor_pad_ops = {
1008 	.enum_mbus_code = sensor_enum_mbus_code,
1009 	.enum_frame_size = sensor_enum_frame_size,
1010 	.get_fmt = sensor_get_fmt,
1011 	.set_fmt = sensor_set_fmt,
1012 	.get_mbus_config = sensor_g_mbus_config,
1013 };
1014 
1015 static const struct v4l2_subdev_ops sensor_ops = {
1016 	.core = &sensor_core_ops,
1017 	.video = &sensor_video_ops,
1018 	.pad = &sensor_pad_ops,
1019 };
1020 
1021 /* ----------------------------------------------------------------------- */
1022 static struct cci_driver cci_drv[] = {
1023 	{
1024 		.name = SENSOR_NAME,
1025 		.addr_width = CCI_BITS_16,
1026 		.data_width = CCI_BITS_16,
1027 	}, {
1028 		.name = SENSOR_NAME_2,
1029 		.addr_width = CCI_BITS_16,
1030 		.data_width = CCI_BITS_16,
1031 	}
1032 };
1033 
1034 
sensor_init_controls(struct v4l2_subdev * sd,const struct v4l2_ctrl_ops * ops)1035 static int sensor_init_controls(struct v4l2_subdev *sd, const struct v4l2_ctrl_ops *ops)
1036 {
1037 	struct sensor_info *info = to_state(sd);
1038 	struct v4l2_ctrl_handler *handler = &info->handler;
1039 	struct v4l2_ctrl *ctrl;
1040 	int ret = 0;
1041 
1042 	v4l2_ctrl_handler_init(handler, 2);
1043 
1044 	v4l2_ctrl_new_std(handler, ops, V4L2_CID_GAIN, 1 * 1600,
1045 			      256 * 1600, 1, 1 * 1600);
1046 	ctrl = v4l2_ctrl_new_std(handler, ops, V4L2_CID_EXPOSURE, 0,
1047 			      65536 * 16, 1, 0);
1048 	if (ctrl != NULL)
1049 		ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
1050 
1051 	if (handler->error) {
1052 		ret = handler->error;
1053 		v4l2_ctrl_handler_free(handler);
1054 	}
1055 
1056 	sd->ctrl_handler = handler;
1057 
1058 	return ret;
1059 }
1060 
1061 static int sensor_dev_id;
1062 
sensor_probe(struct i2c_client * client,const struct i2c_device_id * id)1063 static int sensor_probe(struct i2c_client *client,
1064 			const struct i2c_device_id *id)
1065 {
1066 	struct v4l2_subdev *sd;
1067 	struct sensor_info *info;
1068 	int i;
1069 
1070 	info = kzalloc(sizeof(struct sensor_info), GFP_KERNEL);
1071 	if (info == NULL)
1072 		return -ENOMEM;
1073 	sd = &info->sd;
1074 
1075 	if (client) {
1076 		for (i = 0; i < SENSOR_NUM; i++) {
1077 			if (!strcmp(cci_drv[i].name, client->name))
1078 				break;
1079 		}
1080 		cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv[i]);
1081 	} else {
1082 		cci_dev_probe_helper(sd, client, &sensor_ops, &cci_drv[sensor_dev_id++]);
1083 	}
1084 
1085 	sensor_init_controls(sd, &sensor_ctrl_ops);
1086 
1087 	mutex_init(&info->lock);
1088 
1089 #ifdef CONFIG_SAME_I2C
1090 	info->sensor_i2c_addr = I2C_ADDR >> 1;
1091 #endif
1092 	info->fmt = &sensor_formats[0];
1093 	info->fmt_pt = &sensor_formats[0];
1094 	info->win_pt = &sensor_win_sizes[0];
1095 	info->fmt_num = N_FMTS;
1096 	info->win_size_num = N_WIN_SIZES;
1097 	info->sensor_field = V4L2_FIELD_NONE;
1098 	info->stream_seq = MIPI_BEFORE_SENSOR;
1099 	info->combo_mode = CMB_PHYA_OFFSET1 | MIPI_NORMAL_MODE;
1100 	info->af_first_flag = 1;
1101 	info->exp = 0;
1102 	info->gain = 0;
1103 
1104 	return 0;
1105 }
1106 
sensor_remove(struct i2c_client * client)1107 static int sensor_remove(struct i2c_client *client)
1108 {
1109 	struct v4l2_subdev *sd;
1110 	int i;
1111 
1112 	if (client) {
1113 		for (i = 0; i < SENSOR_NUM; i++) {
1114 			if (!strcmp(cci_drv[i].name, client->name))
1115 				break;
1116 		}
1117 		sd = cci_dev_remove_helper(client, &cci_drv[i]);
1118 	} else {
1119 		sd = cci_dev_remove_helper(client, &cci_drv[sensor_dev_id++]);
1120 	}
1121 
1122 	kfree(to_state(sd));
1123 	return 0;
1124 }
1125 
1126 static const struct i2c_device_id sensor_id[] = {
1127 	{SENSOR_NAME, 0},
1128 	{}
1129 };
1130 
1131 static const struct i2c_device_id sensor_id_2[] = {
1132 	{SENSOR_NAME_2, 0},
1133 	{}
1134 };
1135 
1136 MODULE_DEVICE_TABLE(i2c, sensor_id);
1137 MODULE_DEVICE_TABLE(i2c, sensor_id_2);
1138 
1139 static struct i2c_driver sensor_driver[] = {
1140 	{
1141 		.driver = {
1142 			   .owner = THIS_MODULE,
1143 			   .name = SENSOR_NAME,
1144 			   },
1145 		.probe = sensor_probe,
1146 		.remove = sensor_remove,
1147 		.id_table = sensor_id,
1148 	}, {
1149 		.driver = {
1150 			   .owner = THIS_MODULE,
1151 			   .name = SENSOR_NAME_2,
1152 			   },
1153 		.probe = sensor_probe,
1154 		.remove = sensor_remove,
1155 		.id_table = sensor_id_2,
1156 	},
1157 };
init_sensor(void)1158 static __init int init_sensor(void)
1159 {
1160 	int i, ret = 0;
1161 
1162 	sensor_dev_id = 0;
1163 
1164 	for (i = 0; i < SENSOR_NUM; i++)
1165 		ret = cci_dev_init_helper(&sensor_driver[i]);
1166 
1167 	return ret;
1168 }
1169 
exit_sensor(void)1170 static __exit void exit_sensor(void)
1171 {
1172 	int i;
1173 
1174 	sensor_dev_id = 0;
1175 
1176 	for (i = 0; i < SENSOR_NUM; i++)
1177 		cci_dev_exit_helper(&sensor_driver[i]);
1178 }
1179 
1180 module_init(init_sensor);
1181 module_exit(exit_sensor);
1182