]>
Commit | Line | Data |
---|---|---|
0985dd30 LL |
1 | /* |
2 | * imx274.c - IMX274 CMOS Image Sensor driver | |
3 | * | |
4 | * Copyright (C) 2017, Leopard Imaging, Inc. | |
5 | * | |
6 | * Leon Luo <leonl@leopardimaging.com> | |
7 | * Edwin Zou <edwinz@leopardimaging.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms and conditions of the GNU General Public License, | |
11 | * version 2, as published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope it will be useful, but WITHOUT | |
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 | * more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include <linux/clk.h> | |
23 | #include <linux/delay.h> | |
24 | #include <linux/gpio.h> | |
25 | #include <linux/gpio/consumer.h> | |
26 | #include <linux/i2c.h> | |
27 | #include <linux/init.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/of_gpio.h> | |
30 | #include <linux/regmap.h> | |
31 | #include <linux/slab.h> | |
32 | #include <linux/v4l2-mediabus.h> | |
33 | #include <linux/videodev2.h> | |
34 | ||
35 | #include <media/v4l2-ctrls.h> | |
36 | #include <media/v4l2-device.h> | |
37 | #include <media/v4l2-subdev.h> | |
38 | ||
39 | /* | |
40 | * See "SHR, SVR Setting" in datasheet | |
41 | */ | |
42 | #define IMX274_DEFAULT_FRAME_LENGTH (4550) | |
43 | #define IMX274_MAX_FRAME_LENGTH (0x000fffff) | |
44 | ||
45 | /* | |
46 | * See "Frame Rate Adjustment" in datasheet | |
47 | */ | |
48 | #define IMX274_PIXCLK_CONST1 (72000000) | |
49 | #define IMX274_PIXCLK_CONST2 (1000000) | |
50 | ||
51 | /* | |
52 | * The input gain is shifted by IMX274_GAIN_SHIFT to get | |
53 | * decimal number. The real gain is | |
54 | * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT) | |
55 | */ | |
56 | #define IMX274_GAIN_SHIFT (8) | |
57 | #define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1) | |
58 | ||
59 | /* | |
60 | * See "Analog Gain" and "Digital Gain" in datasheet | |
61 | * min gain is 1X | |
62 | * max gain is calculated based on IMX274_GAIN_REG_MAX | |
63 | */ | |
64 | #define IMX274_GAIN_REG_MAX (1957) | |
65 | #define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT) | |
66 | #define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\ | |
67 | / (2048 - IMX274_GAIN_REG_MAX)) | |
68 | #define IMX274_MAX_DIGITAL_GAIN (8) | |
69 | #define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT) | |
70 | #define IMX274_GAIN_CONST (2048) /* for gain formula */ | |
71 | ||
72 | /* | |
73 | * 1 line time in us = (HMAX / 72), minimal is 4 lines | |
74 | */ | |
75 | #define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72) | |
76 | ||
77 | #define IMX274_DEFAULT_MODE IMX274_MODE_3840X2160 | |
78 | #define IMX274_MAX_WIDTH (3840) | |
79 | #define IMX274_MAX_HEIGHT (2160) | |
80 | #define IMX274_MAX_FRAME_RATE (120) | |
81 | #define IMX274_MIN_FRAME_RATE (5) | |
82 | #define IMX274_DEF_FRAME_RATE (60) | |
83 | ||
84 | /* | |
85 | * register SHR is limited to (SVR value + 1) x VMAX value - 4 | |
86 | */ | |
87 | #define IMX274_SHR_LIMIT_CONST (4) | |
88 | ||
89 | /* | |
90 | * Constants for sensor reset delay | |
91 | */ | |
92 | #define IMX274_RESET_DELAY1 (2000) | |
93 | #define IMX274_RESET_DELAY2 (2200) | |
94 | ||
95 | /* | |
96 | * shift and mask constants | |
97 | */ | |
98 | #define IMX274_SHIFT_8_BITS (8) | |
99 | #define IMX274_SHIFT_16_BITS (16) | |
100 | #define IMX274_MASK_LSB_2_BITS (0x03) | |
101 | #define IMX274_MASK_LSB_3_BITS (0x07) | |
102 | #define IMX274_MASK_LSB_4_BITS (0x0f) | |
103 | #define IMX274_MASK_LSB_8_BITS (0x00ff) | |
104 | ||
105 | #define DRIVER_NAME "IMX274" | |
106 | ||
107 | /* | |
108 | * IMX274 register definitions | |
109 | */ | |
110 | #define IMX274_FRAME_LENGTH_ADDR_1 0x30FA /* VMAX, MSB */ | |
111 | #define IMX274_FRAME_LENGTH_ADDR_2 0x30F9 /* VMAX */ | |
112 | #define IMX274_FRAME_LENGTH_ADDR_3 0x30F8 /* VMAX, LSB */ | |
113 | #define IMX274_SVR_REG_MSB 0x300F /* SVR */ | |
114 | #define IMX274_SVR_REG_LSB 0x300E /* SVR */ | |
115 | #define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */ | |
116 | #define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */ | |
117 | #define IMX274_COARSE_TIME_ADDR_MSB 0x300D /* SHR */ | |
118 | #define IMX274_COARSE_TIME_ADDR_LSB 0x300C /* SHR */ | |
119 | #define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */ | |
120 | #define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */ | |
121 | #define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */ | |
122 | #define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */ | |
123 | #define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */ | |
124 | #define IMX274_STANDBY_REG 0x3000 /* STANDBY */ | |
125 | ||
126 | #define IMX274_TABLE_WAIT_MS 0 | |
127 | #define IMX274_TABLE_END 1 | |
128 | ||
129 | /* | |
130 | * imx274 I2C operation related structure | |
131 | */ | |
132 | struct reg_8 { | |
133 | u16 addr; | |
134 | u8 val; | |
135 | }; | |
136 | ||
137 | static const struct regmap_config imx274_regmap_config = { | |
138 | .reg_bits = 16, | |
139 | .val_bits = 8, | |
140 | .cache_type = REGCACHE_RBTREE, | |
141 | }; | |
142 | ||
143 | enum imx274_mode { | |
144 | IMX274_MODE_3840X2160, | |
145 | IMX274_MODE_1920X1080, | |
146 | IMX274_MODE_1280X720, | |
147 | ||
148 | IMX274_MODE_START_STREAM_1, | |
149 | IMX274_MODE_START_STREAM_2, | |
150 | IMX274_MODE_START_STREAM_3, | |
151 | IMX274_MODE_START_STREAM_4, | |
152 | IMX274_MODE_STOP_STREAM | |
153 | }; | |
154 | ||
155 | /* | |
156 | * imx274 format related structure | |
157 | */ | |
158 | struct imx274_frmfmt { | |
159 | u32 mbus_code; | |
160 | enum v4l2_colorspace colorspace; | |
161 | struct v4l2_frmsize_discrete size; | |
162 | enum imx274_mode mode; | |
163 | }; | |
164 | ||
165 | /* | |
166 | * imx274 test pattern related structure | |
167 | */ | |
168 | enum { | |
169 | TEST_PATTERN_DISABLED = 0, | |
170 | TEST_PATTERN_ALL_000H, | |
171 | TEST_PATTERN_ALL_FFFH, | |
172 | TEST_PATTERN_ALL_555H, | |
173 | TEST_PATTERN_ALL_AAAH, | |
174 | TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */ | |
175 | TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */ | |
176 | TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */ | |
177 | TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */ | |
178 | TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */ | |
179 | TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */ | |
180 | TEST_PATTERN_H_COLOR_BARS, | |
181 | TEST_PATTERN_V_COLOR_BARS, | |
182 | }; | |
183 | ||
184 | static const char * const tp_qmenu[] = { | |
185 | "Disabled", | |
186 | "All 000h Pattern", | |
187 | "All FFFh Pattern", | |
188 | "All 555h Pattern", | |
189 | "All AAAh Pattern", | |
190 | "Vertical Stripe (555h / AAAh)", | |
191 | "Vertical Stripe (AAAh / 555h)", | |
192 | "Vertical Stripe (000h / 555h)", | |
193 | "Vertical Stripe (555h / 000h)", | |
194 | "Vertical Stripe (000h / FFFh)", | |
195 | "Vertical Stripe (FFFh / 000h)", | |
196 | "Horizontal Color Bars", | |
197 | "Vertical Color Bars", | |
198 | }; | |
199 | ||
200 | /* | |
201 | * All-pixel scan mode (10-bit) | |
202 | * imx274 mode1(refer to datasheet) register configuration with | |
203 | * 3840x2160 resolution, raw10 data and mipi four lane output | |
204 | */ | |
205 | static const struct reg_8 imx274_mode1_3840x2160_raw10[] = { | |
206 | {0x3004, 0x01}, | |
207 | {0x3005, 0x01}, | |
208 | {0x3006, 0x00}, | |
209 | {0x3007, 0x02}, | |
210 | ||
211 | {0x3018, 0xA2}, /* output XVS, HVS */ | |
212 | ||
213 | {0x306B, 0x05}, | |
214 | {0x30E2, 0x01}, | |
215 | {0x30F6, 0x07}, /* HMAX, 263 */ | |
216 | {0x30F7, 0x01}, /* HMAX */ | |
217 | ||
218 | {0x30dd, 0x01}, /* crop to 2160 */ | |
219 | {0x30de, 0x06}, | |
220 | {0x30df, 0x00}, | |
221 | {0x30e0, 0x12}, | |
222 | {0x30e1, 0x00}, | |
223 | {0x3037, 0x01}, /* to crop to 3840 */ | |
224 | {0x3038, 0x0c}, | |
225 | {0x3039, 0x00}, | |
226 | {0x303a, 0x0c}, | |
227 | {0x303b, 0x0f}, | |
228 | ||
229 | {0x30EE, 0x01}, | |
230 | {0x3130, 0x86}, | |
231 | {0x3131, 0x08}, | |
232 | {0x3132, 0x7E}, | |
233 | {0x3133, 0x08}, | |
234 | {0x3342, 0x0A}, | |
235 | {0x3343, 0x00}, | |
236 | {0x3344, 0x16}, | |
237 | {0x3345, 0x00}, | |
238 | {0x33A6, 0x01}, | |
239 | {0x3528, 0x0E}, | |
240 | {0x3554, 0x1F}, | |
241 | {0x3555, 0x01}, | |
242 | {0x3556, 0x01}, | |
243 | {0x3557, 0x01}, | |
244 | {0x3558, 0x01}, | |
245 | {0x3559, 0x00}, | |
246 | {0x355A, 0x00}, | |
247 | {0x35BA, 0x0E}, | |
248 | {0x366A, 0x1B}, | |
249 | {0x366B, 0x1A}, | |
250 | {0x366C, 0x19}, | |
251 | {0x366D, 0x17}, | |
252 | {0x3A41, 0x08}, | |
253 | ||
254 | {IMX274_TABLE_END, 0x00} | |
255 | }; | |
256 | ||
257 | /* | |
258 | * Horizontal/vertical 2/2-line binning | |
259 | * (Horizontal and vertical weightedbinning, 10-bit) | |
260 | * imx274 mode3(refer to datasheet) register configuration with | |
261 | * 1920x1080 resolution, raw10 data and mipi four lane output | |
262 | */ | |
263 | static const struct reg_8 imx274_mode3_1920x1080_raw10[] = { | |
264 | {0x3004, 0x02}, | |
265 | {0x3005, 0x21}, | |
266 | {0x3006, 0x00}, | |
267 | {0x3007, 0x11}, | |
268 | ||
269 | {0x3018, 0xA2}, /* output XVS, HVS */ | |
270 | ||
271 | {0x306B, 0x05}, | |
272 | {0x30E2, 0x02}, | |
273 | ||
274 | {0x30F6, 0x04}, /* HMAX, 260 */ | |
275 | {0x30F7, 0x01}, /* HMAX */ | |
276 | ||
277 | {0x30dd, 0x01}, /* to crop to 1920x1080 */ | |
278 | {0x30de, 0x05}, | |
279 | {0x30df, 0x00}, | |
280 | {0x30e0, 0x04}, | |
281 | {0x30e1, 0x00}, | |
282 | {0x3037, 0x01}, | |
283 | {0x3038, 0x0c}, | |
284 | {0x3039, 0x00}, | |
285 | {0x303a, 0x0c}, | |
286 | {0x303b, 0x0f}, | |
287 | ||
288 | {0x30EE, 0x01}, | |
289 | {0x3130, 0x4E}, | |
290 | {0x3131, 0x04}, | |
291 | {0x3132, 0x46}, | |
292 | {0x3133, 0x04}, | |
293 | {0x3342, 0x0A}, | |
294 | {0x3343, 0x00}, | |
295 | {0x3344, 0x1A}, | |
296 | {0x3345, 0x00}, | |
297 | {0x33A6, 0x01}, | |
298 | {0x3528, 0x0E}, | |
299 | {0x3554, 0x00}, | |
300 | {0x3555, 0x01}, | |
301 | {0x3556, 0x01}, | |
302 | {0x3557, 0x01}, | |
303 | {0x3558, 0x01}, | |
304 | {0x3559, 0x00}, | |
305 | {0x355A, 0x00}, | |
306 | {0x35BA, 0x0E}, | |
307 | {0x366A, 0x1B}, | |
308 | {0x366B, 0x1A}, | |
309 | {0x366C, 0x19}, | |
310 | {0x366D, 0x17}, | |
311 | {0x3A41, 0x08}, | |
312 | ||
313 | {IMX274_TABLE_END, 0x00} | |
314 | }; | |
315 | ||
316 | /* | |
317 | * Vertical 2/3 subsampling binning horizontal 3 binning | |
318 | * imx274 mode5(refer to datasheet) register configuration with | |
319 | * 1280x720 resolution, raw10 data and mipi four lane output | |
320 | */ | |
321 | static const struct reg_8 imx274_mode5_1280x720_raw10[] = { | |
322 | {0x3004, 0x03}, | |
323 | {0x3005, 0x31}, | |
324 | {0x3006, 0x00}, | |
325 | {0x3007, 0x09}, | |
326 | ||
327 | {0x3018, 0xA2}, /* output XVS, HVS */ | |
328 | ||
329 | {0x306B, 0x05}, | |
330 | {0x30E2, 0x03}, | |
331 | ||
332 | {0x30F6, 0x04}, /* HMAX, 260 */ | |
333 | {0x30F7, 0x01}, /* HMAX */ | |
334 | ||
335 | {0x30DD, 0x01}, | |
336 | {0x30DE, 0x07}, | |
337 | {0x30DF, 0x00}, | |
338 | {0x40E0, 0x04}, | |
339 | {0x30E1, 0x00}, | |
340 | {0x3030, 0xD4}, | |
341 | {0x3031, 0x02}, | |
342 | {0x3032, 0xD0}, | |
343 | {0x3033, 0x02}, | |
344 | ||
345 | {0x30EE, 0x01}, | |
346 | {0x3130, 0xE2}, | |
347 | {0x3131, 0x02}, | |
348 | {0x3132, 0xDE}, | |
349 | {0x3133, 0x02}, | |
350 | {0x3342, 0x0A}, | |
351 | {0x3343, 0x00}, | |
352 | {0x3344, 0x1B}, | |
353 | {0x3345, 0x00}, | |
354 | {0x33A6, 0x01}, | |
355 | {0x3528, 0x0E}, | |
356 | {0x3554, 0x00}, | |
357 | {0x3555, 0x01}, | |
358 | {0x3556, 0x01}, | |
359 | {0x3557, 0x01}, | |
360 | {0x3558, 0x01}, | |
361 | {0x3559, 0x00}, | |
362 | {0x355A, 0x00}, | |
363 | {0x35BA, 0x0E}, | |
364 | {0x366A, 0x1B}, | |
365 | {0x366B, 0x19}, | |
366 | {0x366C, 0x17}, | |
367 | {0x366D, 0x17}, | |
368 | {0x3A41, 0x04}, | |
369 | ||
370 | {IMX274_TABLE_END, 0x00} | |
371 | }; | |
372 | ||
373 | /* | |
374 | * imx274 first step register configuration for | |
375 | * starting stream | |
376 | */ | |
377 | static const struct reg_8 imx274_start_1[] = { | |
378 | {IMX274_STANDBY_REG, 0x12}, | |
379 | {IMX274_TABLE_END, 0x00} | |
380 | }; | |
381 | ||
382 | /* | |
383 | * imx274 second step register configuration for | |
384 | * starting stream | |
385 | */ | |
386 | static const struct reg_8 imx274_start_2[] = { | |
387 | {0x3120, 0xF0}, /* clock settings */ | |
388 | {0x3121, 0x00}, /* clock settings */ | |
389 | {0x3122, 0x02}, /* clock settings */ | |
390 | {0x3129, 0x9C}, /* clock settings */ | |
391 | {0x312A, 0x02}, /* clock settings */ | |
392 | {0x312D, 0x02}, /* clock settings */ | |
393 | ||
394 | {0x310B, 0x00}, | |
395 | ||
396 | /* PLSTMG */ | |
397 | {0x304C, 0x00}, /* PLSTMG01 */ | |
398 | {0x304D, 0x03}, | |
399 | {0x331C, 0x1A}, | |
400 | {0x331D, 0x00}, | |
401 | {0x3502, 0x02}, | |
402 | {0x3529, 0x0E}, | |
403 | {0x352A, 0x0E}, | |
404 | {0x352B, 0x0E}, | |
405 | {0x3538, 0x0E}, | |
406 | {0x3539, 0x0E}, | |
407 | {0x3553, 0x00}, | |
408 | {0x357D, 0x05}, | |
409 | {0x357F, 0x05}, | |
410 | {0x3581, 0x04}, | |
411 | {0x3583, 0x76}, | |
412 | {0x3587, 0x01}, | |
413 | {0x35BB, 0x0E}, | |
414 | {0x35BC, 0x0E}, | |
415 | {0x35BD, 0x0E}, | |
416 | {0x35BE, 0x0E}, | |
417 | {0x35BF, 0x0E}, | |
418 | {0x366E, 0x00}, | |
419 | {0x366F, 0x00}, | |
420 | {0x3670, 0x00}, | |
421 | {0x3671, 0x00}, | |
422 | ||
423 | /* PSMIPI */ | |
424 | {0x3304, 0x32}, /* PSMIPI1 */ | |
425 | {0x3305, 0x00}, | |
426 | {0x3306, 0x32}, | |
427 | {0x3307, 0x00}, | |
428 | {0x3590, 0x32}, | |
429 | {0x3591, 0x00}, | |
430 | {0x3686, 0x32}, | |
431 | {0x3687, 0x00}, | |
432 | ||
433 | {IMX274_TABLE_END, 0x00} | |
434 | }; | |
435 | ||
436 | /* | |
437 | * imx274 third step register configuration for | |
438 | * starting stream | |
439 | */ | |
440 | static const struct reg_8 imx274_start_3[] = { | |
441 | {IMX274_STANDBY_REG, 0x00}, | |
442 | {0x303E, 0x02}, /* SYS_MODE = 2 */ | |
443 | {IMX274_TABLE_END, 0x00} | |
444 | }; | |
445 | ||
446 | /* | |
447 | * imx274 forth step register configuration for | |
448 | * starting stream | |
449 | */ | |
450 | static const struct reg_8 imx274_start_4[] = { | |
451 | {0x30F4, 0x00}, | |
452 | {0x3018, 0xA2}, /* XHS VHS OUTUPT */ | |
453 | {IMX274_TABLE_END, 0x00} | |
454 | }; | |
455 | ||
456 | /* | |
457 | * imx274 register configuration for stoping stream | |
458 | */ | |
459 | static const struct reg_8 imx274_stop[] = { | |
460 | {IMX274_STANDBY_REG, 0x01}, | |
461 | {IMX274_TABLE_END, 0x00} | |
462 | }; | |
463 | ||
464 | /* | |
465 | * imx274 disable test pattern register configuration | |
466 | */ | |
467 | static const struct reg_8 imx274_tp_disabled[] = { | |
468 | {0x303C, 0x00}, | |
469 | {0x377F, 0x00}, | |
470 | {0x3781, 0x00}, | |
471 | {0x370B, 0x00}, | |
472 | {IMX274_TABLE_END, 0x00} | |
473 | }; | |
474 | ||
475 | /* | |
476 | * imx274 test pattern register configuration | |
477 | * reg 0x303D defines the test pattern modes | |
478 | */ | |
479 | static const struct reg_8 imx274_tp_regs[] = { | |
480 | {0x303C, 0x11}, | |
481 | {0x370E, 0x01}, | |
482 | {0x377F, 0x01}, | |
483 | {0x3781, 0x01}, | |
484 | {0x370B, 0x11}, | |
485 | {IMX274_TABLE_END, 0x00} | |
486 | }; | |
487 | ||
488 | static const struct reg_8 *mode_table[] = { | |
489 | [IMX274_MODE_3840X2160] = imx274_mode1_3840x2160_raw10, | |
490 | [IMX274_MODE_1920X1080] = imx274_mode3_1920x1080_raw10, | |
491 | [IMX274_MODE_1280X720] = imx274_mode5_1280x720_raw10, | |
492 | ||
493 | [IMX274_MODE_START_STREAM_1] = imx274_start_1, | |
494 | [IMX274_MODE_START_STREAM_2] = imx274_start_2, | |
495 | [IMX274_MODE_START_STREAM_3] = imx274_start_3, | |
496 | [IMX274_MODE_START_STREAM_4] = imx274_start_4, | |
497 | [IMX274_MODE_STOP_STREAM] = imx274_stop, | |
498 | }; | |
499 | ||
500 | /* | |
501 | * imx274 format related structure | |
502 | */ | |
503 | static const struct imx274_frmfmt imx274_formats[] = { | |
504 | {MEDIA_BUS_FMT_SRGGB10_1X10, V4L2_COLORSPACE_SRGB, {3840, 2160}, | |
505 | IMX274_MODE_3840X2160}, | |
506 | {MEDIA_BUS_FMT_SRGGB10_1X10, V4L2_COLORSPACE_SRGB, {1920, 1080}, | |
507 | IMX274_MODE_1920X1080}, | |
508 | {MEDIA_BUS_FMT_SRGGB10_1X10, V4L2_COLORSPACE_SRGB, {1280, 720}, | |
509 | IMX274_MODE_1280X720}, | |
510 | }; | |
511 | ||
512 | /* | |
513 | * minimal frame length for each mode | |
514 | * refer to datasheet section "Frame Rate Adjustment (CSI-2)" | |
515 | */ | |
516 | static const int min_frame_len[] = { | |
517 | 4550, /* mode 1, 4K */ | |
518 | 2310, /* mode 3, 1080p */ | |
519 | 2310 /* mode 5, 720p */ | |
520 | }; | |
521 | ||
522 | /* | |
523 | * minimal numbers of SHR register | |
524 | * refer to datasheet table "Shutter Setting (CSI-2)" | |
525 | */ | |
526 | static const int min_SHR[] = { | |
527 | 12, /* mode 1, 4K */ | |
528 | 8, /* mode 3, 1080p */ | |
529 | 8 /* mode 5, 720p */ | |
530 | }; | |
531 | ||
532 | static const int max_frame_rate[] = { | |
533 | 60, /* mode 1 , 4K */ | |
534 | 120, /* mode 3, 1080p */ | |
535 | 120 /* mode 5, 720p */ | |
536 | }; | |
537 | ||
538 | /* | |
539 | * Number of clocks per internal offset period | |
540 | * a constant based on mode | |
541 | * refer to section "Integration Time in Each Readout Drive Mode (CSI-2)" | |
542 | * in the datasheet | |
543 | * for the implemented 3 modes, it happens to be the same number | |
544 | */ | |
545 | static const int nocpiop[] = { | |
546 | 112, /* mode 1 , 4K */ | |
547 | 112, /* mode 3, 1080p */ | |
548 | 112 /* mode 5, 720p */ | |
549 | }; | |
550 | ||
551 | /* | |
552 | * struct imx274_ctrls - imx274 ctrl structure | |
553 | * @handler: V4L2 ctrl handler structure | |
554 | * @exposure: Pointer to expsure ctrl structure | |
555 | * @gain: Pointer to gain ctrl structure | |
556 | * @vflip: Pointer to vflip ctrl structure | |
557 | * @test_pattern: Pointer to test pattern ctrl structure | |
558 | */ | |
559 | struct imx274_ctrls { | |
560 | struct v4l2_ctrl_handler handler; | |
561 | struct v4l2_ctrl *exposure; | |
562 | struct v4l2_ctrl *gain; | |
563 | struct v4l2_ctrl *vflip; | |
564 | struct v4l2_ctrl *test_pattern; | |
565 | }; | |
566 | ||
567 | /* | |
568 | * struct stim274 - imx274 device structure | |
569 | * @sd: V4L2 subdevice structure | |
570 | * @pd: Media pad structure | |
571 | * @client: Pointer to I2C client | |
572 | * @ctrls: imx274 control structure | |
573 | * @format: V4L2 media bus frame format structure | |
574 | * @frame_rate: V4L2 frame rate structure | |
575 | * @regmap: Pointer to regmap structure | |
576 | * @reset_gpio: Pointer to reset gpio | |
577 | * @lock: Mutex structure | |
578 | * @mode_index: Resolution mode index | |
579 | */ | |
580 | struct stimx274 { | |
581 | struct v4l2_subdev sd; | |
582 | struct media_pad pad; | |
583 | struct i2c_client *client; | |
584 | struct imx274_ctrls ctrls; | |
585 | struct v4l2_mbus_framefmt format; | |
586 | struct v4l2_fract frame_interval; | |
587 | struct regmap *regmap; | |
588 | struct gpio_desc *reset_gpio; | |
589 | struct mutex lock; /* mutex lock for operations */ | |
590 | u32 mode_index; | |
591 | }; | |
592 | ||
593 | /* | |
594 | * Function declaration | |
595 | */ | |
596 | static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl); | |
597 | static int imx274_set_exposure(struct stimx274 *priv, int val); | |
598 | static int imx274_set_vflip(struct stimx274 *priv, int val); | |
599 | static int imx274_set_test_pattern(struct stimx274 *priv, int val); | |
600 | static int imx274_set_frame_interval(struct stimx274 *priv, | |
601 | struct v4l2_fract frame_interval); | |
602 | ||
603 | static inline void msleep_range(unsigned int delay_base) | |
604 | { | |
605 | usleep_range(delay_base * 1000, delay_base * 1000 + 500); | |
606 | } | |
607 | ||
608 | /* | |
609 | * v4l2_ctrl and v4l2_subdev related operations | |
610 | */ | |
611 | static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) | |
612 | { | |
613 | return &container_of(ctrl->handler, | |
614 | struct stimx274, ctrls.handler)->sd; | |
615 | } | |
616 | ||
617 | static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd) | |
618 | { | |
619 | return container_of(sd, struct stimx274, sd); | |
620 | } | |
621 | ||
622 | /* | |
623 | * imx274_regmap_util_write_table_8 - Function for writing register table | |
624 | * @regmap: Pointer to device reg map structure | |
625 | * @table: Table containing register values | |
626 | * @wait_ms_addr: Flag for performing delay | |
627 | * @end_addr: Flag for incating end of table | |
628 | * | |
629 | * This is used to write register table into sensor's reg map. | |
630 | * | |
631 | * Return: 0 on success, errors otherwise | |
632 | */ | |
633 | static int imx274_regmap_util_write_table_8(struct regmap *regmap, | |
634 | const struct reg_8 table[], | |
635 | u16 wait_ms_addr, u16 end_addr) | |
636 | { | |
637 | int err; | |
638 | const struct reg_8 *next; | |
639 | u8 val; | |
640 | ||
641 | int range_start = -1; | |
642 | int range_count = 0; | |
643 | u8 range_vals[16]; | |
644 | int max_range_vals = ARRAY_SIZE(range_vals); | |
645 | ||
646 | for (next = table;; next++) { | |
647 | if ((next->addr != range_start + range_count) || | |
648 | (next->addr == end_addr) || | |
649 | (next->addr == wait_ms_addr) || | |
650 | (range_count == max_range_vals)) { | |
651 | if (range_count == 1) | |
652 | err = regmap_write(regmap, | |
653 | range_start, range_vals[0]); | |
654 | else if (range_count > 1) | |
655 | err = regmap_bulk_write(regmap, range_start, | |
656 | &range_vals[0], | |
657 | range_count); | |
658 | ||
659 | if (err) | |
660 | return err; | |
661 | ||
662 | range_start = -1; | |
663 | range_count = 0; | |
664 | ||
665 | /* Handle special address values */ | |
666 | if (next->addr == end_addr) | |
667 | break; | |
668 | ||
669 | if (next->addr == wait_ms_addr) { | |
670 | msleep_range(next->val); | |
671 | continue; | |
672 | } | |
673 | } | |
674 | ||
675 | val = next->val; | |
676 | ||
677 | if (range_start == -1) | |
678 | range_start = next->addr; | |
679 | ||
680 | range_vals[range_count++] = val; | |
681 | } | |
682 | return 0; | |
683 | } | |
684 | ||
685 | static inline int imx274_read_reg(struct stimx274 *priv, u16 addr, u8 *val) | |
686 | { | |
687 | int err; | |
688 | ||
689 | err = regmap_read(priv->regmap, addr, (unsigned int *)val); | |
690 | if (err) | |
691 | dev_err(&priv->client->dev, | |
692 | "%s : i2c read failed, addr = %x\n", __func__, addr); | |
693 | else | |
694 | dev_dbg(&priv->client->dev, | |
695 | "%s : addr 0x%x, val=0x%x\n", __func__, | |
696 | addr, *val); | |
697 | return err; | |
698 | } | |
699 | ||
700 | static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val) | |
701 | { | |
702 | int err; | |
703 | ||
704 | err = regmap_write(priv->regmap, addr, val); | |
705 | if (err) | |
706 | dev_err(&priv->client->dev, | |
707 | "%s : i2c write failed, %x = %x\n", __func__, | |
708 | addr, val); | |
709 | else | |
710 | dev_dbg(&priv->client->dev, | |
711 | "%s : addr 0x%x, val=0x%x\n", __func__, | |
712 | addr, val); | |
713 | return err; | |
714 | } | |
715 | ||
716 | static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[]) | |
717 | { | |
718 | return imx274_regmap_util_write_table_8(priv->regmap, | |
719 | table, IMX274_TABLE_WAIT_MS, IMX274_TABLE_END); | |
720 | } | |
721 | ||
722 | /* | |
723 | * imx274_mode_regs - Function for set mode registers per mode index | |
724 | * @priv: Pointer to device structure | |
725 | * @mode: Mode index value | |
726 | * | |
727 | * This is used to start steam per mode index. | |
728 | * mode = 0, start stream for sensor Mode 1: 4K/raw10 | |
729 | * mode = 1, start stream for sensor Mode 3: 1080p/raw10 | |
730 | * mode = 2, start stream for sensor Mode 5: 720p/raw10 | |
731 | * | |
732 | * Return: 0 on success, errors otherwise | |
733 | */ | |
734 | static int imx274_mode_regs(struct stimx274 *priv, int mode) | |
735 | { | |
736 | int err = 0; | |
737 | ||
738 | err = imx274_write_table(priv, mode_table[IMX274_MODE_START_STREAM_1]); | |
739 | if (err) | |
740 | return err; | |
741 | ||
742 | err = imx274_write_table(priv, mode_table[IMX274_MODE_START_STREAM_2]); | |
743 | if (err) | |
744 | return err; | |
745 | ||
746 | err = imx274_write_table(priv, mode_table[mode]); | |
747 | ||
748 | return err; | |
749 | } | |
750 | ||
751 | /* | |
752 | * imx274_start_stream - Function for starting stream per mode index | |
753 | * @priv: Pointer to device structure | |
754 | * | |
755 | * Return: 0 on success, errors otherwise | |
756 | */ | |
757 | static int imx274_start_stream(struct stimx274 *priv) | |
758 | { | |
759 | int err = 0; | |
760 | ||
761 | /* | |
762 | * Refer to "Standby Cancel Sequence when using CSI-2" in | |
763 | * imx274 datasheet, it should wait 10ms or more here. | |
764 | * give it 1 extra ms for margin | |
765 | */ | |
766 | msleep_range(11); | |
767 | err = imx274_write_table(priv, mode_table[IMX274_MODE_START_STREAM_3]); | |
768 | if (err) | |
769 | return err; | |
770 | ||
771 | /* | |
772 | * Refer to "Standby Cancel Sequence when using CSI-2" in | |
773 | * imx274 datasheet, it should wait 7ms or more here. | |
774 | * give it 1 extra ms for margin | |
775 | */ | |
776 | msleep_range(8); | |
777 | err = imx274_write_table(priv, mode_table[IMX274_MODE_START_STREAM_4]); | |
778 | if (err) | |
779 | return err; | |
780 | ||
781 | return 0; | |
782 | } | |
783 | ||
784 | /* | |
785 | * imx274_reset - Function called to reset the sensor | |
786 | * @priv: Pointer to device structure | |
787 | * @rst: Input value for determining the sensor's end state after reset | |
788 | * | |
789 | * Set the senor in reset and then | |
790 | * if rst = 0, keep it in reset; | |
791 | * if rst = 1, bring it out of reset. | |
792 | * | |
793 | */ | |
794 | static void imx274_reset(struct stimx274 *priv, int rst) | |
795 | { | |
796 | gpiod_set_value_cansleep(priv->reset_gpio, 0); | |
797 | usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2); | |
798 | gpiod_set_value_cansleep(priv->reset_gpio, !!rst); | |
799 | usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2); | |
800 | } | |
801 | ||
802 | /** | |
803 | * imx274_s_ctrl - This is used to set the imx274 V4L2 controls | |
804 | * @ctrl: V4L2 control to be set | |
805 | * | |
806 | * This function is used to set the V4L2 controls for the imx274 sensor. | |
807 | * | |
808 | * Return: 0 on success, errors otherwise | |
809 | */ | |
810 | static int imx274_s_ctrl(struct v4l2_ctrl *ctrl) | |
811 | { | |
812 | struct v4l2_subdev *sd = ctrl_to_sd(ctrl); | |
813 | struct stimx274 *imx274 = to_imx274(sd); | |
814 | int ret = -EINVAL; | |
815 | ||
816 | dev_dbg(&imx274->client->dev, | |
817 | "%s : s_ctrl: %s, value: %d\n", __func__, | |
818 | ctrl->name, ctrl->val); | |
819 | ||
820 | switch (ctrl->id) { | |
821 | case V4L2_CID_EXPOSURE: | |
822 | dev_dbg(&imx274->client->dev, | |
823 | "%s : set V4L2_CID_EXPOSURE\n", __func__); | |
824 | ret = imx274_set_exposure(imx274, ctrl->val); | |
825 | break; | |
826 | ||
827 | case V4L2_CID_GAIN: | |
828 | dev_dbg(&imx274->client->dev, | |
829 | "%s : set V4L2_CID_GAIN\n", __func__); | |
830 | ret = imx274_set_gain(imx274, ctrl); | |
831 | break; | |
832 | ||
833 | case V4L2_CID_VFLIP: | |
834 | dev_dbg(&imx274->client->dev, | |
835 | "%s : set V4L2_CID_VFLIP\n", __func__); | |
836 | ret = imx274_set_vflip(imx274, ctrl->val); | |
837 | break; | |
838 | ||
839 | case V4L2_CID_TEST_PATTERN: | |
840 | dev_dbg(&imx274->client->dev, | |
841 | "%s : set V4L2_CID_TEST_PATTERN\n", __func__); | |
842 | ret = imx274_set_test_pattern(imx274, ctrl->val); | |
843 | break; | |
844 | } | |
845 | ||
846 | return ret; | |
847 | } | |
848 | ||
849 | /** | |
850 | * imx274_get_fmt - Get the pad format | |
851 | * @sd: Pointer to V4L2 Sub device structure | |
852 | * @cfg: Pointer to sub device pad information structure | |
853 | * @fmt: Pointer to pad level media bus format | |
854 | * | |
855 | * This function is used to get the pad format information. | |
856 | * | |
857 | * Return: 0 on success | |
858 | */ | |
859 | static int imx274_get_fmt(struct v4l2_subdev *sd, | |
860 | struct v4l2_subdev_pad_config *cfg, | |
861 | struct v4l2_subdev_format *fmt) | |
862 | { | |
863 | struct stimx274 *imx274 = to_imx274(sd); | |
864 | ||
865 | mutex_lock(&imx274->lock); | |
866 | fmt->format = imx274->format; | |
867 | mutex_unlock(&imx274->lock); | |
868 | return 0; | |
869 | } | |
870 | ||
871 | /** | |
872 | * imx274_set_fmt - This is used to set the pad format | |
873 | * @sd: Pointer to V4L2 Sub device structure | |
874 | * @cfg: Pointer to sub device pad information structure | |
875 | * @format: Pointer to pad level media bus format | |
876 | * | |
877 | * This function is used to set the pad format. | |
878 | * | |
879 | * Return: 0 on success | |
880 | */ | |
881 | static int imx274_set_fmt(struct v4l2_subdev *sd, | |
882 | struct v4l2_subdev_pad_config *cfg, | |
883 | struct v4l2_subdev_format *format) | |
884 | { | |
885 | struct v4l2_mbus_framefmt *fmt = &format->format; | |
886 | struct stimx274 *imx274 = to_imx274(sd); | |
887 | struct i2c_client *client = imx274->client; | |
888 | int index; | |
889 | ||
890 | dev_dbg(&client->dev, | |
891 | "%s: width = %d height = %d code = %d mbus_code = %d\n", | |
892 | __func__, fmt->width, fmt->height, fmt->code, | |
893 | imx274_formats[imx274->mode_index].mbus_code); | |
894 | ||
895 | mutex_lock(&imx274->lock); | |
896 | ||
897 | for (index = 0; index < ARRAY_SIZE(imx274_formats); index++) { | |
898 | if (imx274_formats[index].size.width == fmt->width && | |
899 | imx274_formats[index].size.height == fmt->height) | |
900 | break; | |
901 | } | |
902 | ||
903 | if (index >= ARRAY_SIZE(imx274_formats)) { | |
904 | /* default to first format */ | |
905 | index = 0; | |
906 | } | |
907 | ||
908 | imx274->mode_index = index; | |
909 | ||
910 | if (fmt->width > IMX274_MAX_WIDTH) | |
911 | fmt->width = IMX274_MAX_WIDTH; | |
912 | if (fmt->height > IMX274_MAX_HEIGHT) | |
913 | fmt->height = IMX274_MAX_HEIGHT; | |
914 | fmt->width = fmt->width & (~IMX274_MASK_LSB_2_BITS); | |
915 | fmt->height = fmt->height & (~IMX274_MASK_LSB_2_BITS); | |
916 | fmt->field = V4L2_FIELD_NONE; | |
917 | ||
918 | if (format->which == V4L2_SUBDEV_FORMAT_TRY) | |
919 | cfg->try_fmt = *fmt; | |
920 | else | |
921 | imx274->format = *fmt; | |
922 | ||
923 | mutex_unlock(&imx274->lock); | |
924 | return 0; | |
925 | } | |
926 | ||
927 | /** | |
928 | * imx274_g_frame_interval - Get the frame interval | |
929 | * @sd: Pointer to V4L2 Sub device structure | |
930 | * @fi: Pointer to V4l2 Sub device frame interval structure | |
931 | * | |
932 | * This function is used to get the frame interval. | |
933 | * | |
934 | * Return: 0 on success | |
935 | */ | |
936 | static int imx274_g_frame_interval(struct v4l2_subdev *sd, | |
937 | struct v4l2_subdev_frame_interval *fi) | |
938 | { | |
939 | struct stimx274 *imx274 = to_imx274(sd); | |
940 | ||
941 | fi->interval = imx274->frame_interval; | |
942 | dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n", | |
943 | __func__, imx274->frame_interval.numerator, | |
944 | imx274->frame_interval.denominator); | |
945 | ||
946 | return 0; | |
947 | } | |
948 | ||
949 | /** | |
950 | * imx274_s_frame_interval - Set the frame interval | |
951 | * @sd: Pointer to V4L2 Sub device structure | |
952 | * @fi: Pointer to V4l2 Sub device frame interval structure | |
953 | * | |
954 | * This function is used to set the frame intervavl. | |
955 | * | |
956 | * Return: 0 on success | |
957 | */ | |
958 | static int imx274_s_frame_interval(struct v4l2_subdev *sd, | |
959 | struct v4l2_subdev_frame_interval *fi) | |
960 | { | |
961 | struct stimx274 *imx274 = to_imx274(sd); | |
962 | struct v4l2_ctrl *ctrl = imx274->ctrls.exposure; | |
963 | int min, max, def; | |
964 | int ret; | |
965 | ||
966 | mutex_lock(&imx274->lock); | |
967 | ret = imx274_set_frame_interval(imx274, fi->interval); | |
968 | ||
969 | if (!ret) { | |
970 | /* | |
971 | * exposure time range is decided by frame interval | |
972 | * need to update it after frame interal changes | |
973 | */ | |
974 | min = IMX274_MIN_EXPOSURE_TIME; | |
975 | max = fi->interval.numerator * 1000000 | |
976 | / fi->interval.denominator; | |
977 | def = max; | |
978 | if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def)) { | |
979 | dev_err(&imx274->client->dev, | |
980 | "Exposure ctrl range update failed\n"); | |
981 | goto unlock; | |
982 | } | |
983 | ||
984 | /* update exposure time accordingly */ | |
985 | imx274_set_exposure(imx274, imx274->ctrls.exposure->val); | |
986 | ||
987 | dev_dbg(&imx274->client->dev, "set frame interval to %uus\n", | |
988 | fi->interval.numerator * 1000000 | |
989 | / fi->interval.denominator); | |
990 | } | |
991 | ||
992 | unlock: | |
993 | mutex_unlock(&imx274->lock); | |
994 | ||
995 | return ret; | |
996 | } | |
997 | ||
998 | /** | |
999 | * imx274_load_default - load default control values | |
1000 | * @priv: Pointer to device structure | |
1001 | * | |
1002 | * Return: 0 on success, errors otherwise | |
1003 | */ | |
1004 | static int imx274_load_default(struct stimx274 *priv) | |
1005 | { | |
1006 | int ret; | |
1007 | ||
1008 | /* load default control values */ | |
1009 | priv->frame_interval.numerator = 1; | |
1010 | priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE; | |
1011 | priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE; | |
1012 | priv->ctrls.gain->val = IMX274_DEF_GAIN; | |
1013 | priv->ctrls.vflip->val = 0; | |
1014 | priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED; | |
1015 | ||
1016 | /* update frame rate */ | |
1017 | ret = imx274_set_frame_interval(priv, | |
1018 | priv->frame_interval); | |
1019 | if (ret) | |
1020 | return ret; | |
1021 | ||
1022 | /* update exposure time */ | |
1023 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.exposure, priv->ctrls.exposure->val); | |
1024 | if (ret) | |
1025 | return ret; | |
1026 | ||
1027 | /* update gain */ | |
1028 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.gain, priv->ctrls.gain->val); | |
1029 | if (ret) | |
1030 | return ret; | |
1031 | ||
1032 | /* update vflip */ | |
1033 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.vflip, priv->ctrls.vflip->val); | |
1034 | if (ret) | |
1035 | return ret; | |
1036 | ||
1037 | return 0; | |
1038 | } | |
1039 | ||
1040 | /** | |
1041 | * imx274_s_stream - It is used to start/stop the streaming. | |
1042 | * @sd: V4L2 Sub device | |
1043 | * @on: Flag (True / False) | |
1044 | * | |
1045 | * This function controls the start or stop of streaming for the | |
1046 | * imx274 sensor. | |
1047 | * | |
1048 | * Return: 0 on success, errors otherwise | |
1049 | */ | |
1050 | static int imx274_s_stream(struct v4l2_subdev *sd, int on) | |
1051 | { | |
1052 | struct stimx274 *imx274 = to_imx274(sd); | |
1053 | int ret = 0; | |
1054 | ||
1055 | dev_dbg(&imx274->client->dev, "%s : %s, mode index = %d\n", __func__, | |
1056 | on ? "Stream Start" : "Stream Stop", imx274->mode_index); | |
1057 | ||
1058 | mutex_lock(&imx274->lock); | |
1059 | ||
1060 | if (on) { | |
1061 | /* load mode registers */ | |
2b00e30f | 1062 | ret = imx274_mode_regs(imx274, imx274->mode_index); |
0985dd30 LL |
1063 | if (ret) |
1064 | goto fail; | |
1065 | ||
1066 | /* | |
1067 | * update frame rate & expsoure. if the last mode is different, | |
1068 | * HMAX could be changed. As the result, frame rate & exposure | |
1069 | * are changed. | |
1070 | * gain is not affected. | |
1071 | */ | |
1072 | ret = imx274_set_frame_interval(imx274, | |
1073 | imx274->frame_interval); | |
1074 | if (ret) | |
1075 | goto fail; | |
1076 | ||
1077 | /* update exposure time */ | |
1078 | ret = __v4l2_ctrl_s_ctrl(imx274->ctrls.exposure, | |
1079 | imx274->ctrls.exposure->val); | |
1080 | if (ret) | |
1081 | goto fail; | |
1082 | ||
1083 | /* start stream */ | |
1084 | ret = imx274_start_stream(imx274); | |
1085 | if (ret) | |
1086 | goto fail; | |
1087 | } else { | |
1088 | /* stop stream */ | |
1089 | ret = imx274_write_table(imx274, | |
1090 | mode_table[IMX274_MODE_STOP_STREAM]); | |
1091 | if (ret) | |
1092 | goto fail; | |
1093 | } | |
1094 | ||
1095 | mutex_unlock(&imx274->lock); | |
1096 | dev_dbg(&imx274->client->dev, | |
1097 | "%s : Done: mode = %d\n", __func__, imx274->mode_index); | |
1098 | return 0; | |
1099 | ||
1100 | fail: | |
1101 | mutex_unlock(&imx274->lock); | |
1102 | dev_err(&imx274->client->dev, "s_stream failed\n"); | |
1103 | return ret; | |
1104 | } | |
1105 | ||
1106 | /* | |
1107 | * imx274_get_frame_length - Function for obtaining current frame length | |
1108 | * @priv: Pointer to device structure | |
1109 | * @val: Pointer to obainted value | |
1110 | * | |
1111 | * frame_length = vmax x (svr + 1), in unit of hmax. | |
1112 | * | |
1113 | * Return: 0 on success | |
1114 | */ | |
1115 | static int imx274_get_frame_length(struct stimx274 *priv, u32 *val) | |
1116 | { | |
1117 | int err; | |
1118 | u16 svr; | |
1119 | u32 vmax; | |
1120 | u8 reg_val[3]; | |
1121 | ||
1122 | /* svr */ | |
1123 | err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, ®_val[0]); | |
1124 | if (err) | |
1125 | goto fail; | |
1126 | ||
1127 | err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, ®_val[1]); | |
1128 | if (err) | |
1129 | goto fail; | |
1130 | ||
1131 | svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; | |
1132 | ||
1133 | /* vmax */ | |
1134 | err = imx274_read_reg(priv, IMX274_FRAME_LENGTH_ADDR_3, ®_val[0]); | |
1135 | if (err) | |
1136 | goto fail; | |
1137 | ||
1138 | err = imx274_read_reg(priv, IMX274_FRAME_LENGTH_ADDR_2, ®_val[1]); | |
1139 | if (err) | |
1140 | goto fail; | |
1141 | ||
1142 | err = imx274_read_reg(priv, IMX274_FRAME_LENGTH_ADDR_1, ®_val[2]); | |
1143 | if (err) | |
1144 | goto fail; | |
1145 | ||
1146 | vmax = ((reg_val[2] & IMX274_MASK_LSB_3_BITS) << IMX274_SHIFT_16_BITS) | |
1147 | + (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; | |
1148 | ||
1149 | *val = vmax * (svr + 1); | |
1150 | ||
1151 | return 0; | |
1152 | ||
1153 | fail: | |
1154 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); | |
1155 | return err; | |
1156 | } | |
1157 | ||
1158 | static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val, | |
1159 | u32 *frame_length) | |
1160 | { | |
1161 | int err; | |
1162 | ||
1163 | err = imx274_get_frame_length(priv, frame_length); | |
1164 | if (err) | |
1165 | return err; | |
1166 | ||
1167 | if (*frame_length < min_frame_len[priv->mode_index]) | |
1168 | *frame_length = min_frame_len[priv->mode_index]; | |
1169 | ||
1170 | *val = *frame_length - *val; /* convert to raw shr */ | |
1171 | if (*val > *frame_length - IMX274_SHR_LIMIT_CONST) | |
1172 | *val = *frame_length - IMX274_SHR_LIMIT_CONST; | |
1173 | else if (*val < min_SHR[priv->mode_index]) | |
1174 | *val = min_SHR[priv->mode_index]; | |
1175 | ||
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | /* | |
1180 | * imx274_set_digital gain - Function called when setting digital gain | |
1181 | * @priv: Pointer to device structure | |
1182 | * @dgain: Value of digital gain. | |
1183 | * | |
1184 | * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x | |
1185 | * | |
1186 | * Return: 0 on success | |
1187 | */ | |
1188 | static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain) | |
1189 | { | |
1190 | u8 reg_val; | |
1191 | ||
1192 | reg_val = ffs(dgain); | |
1193 | ||
1194 | if (reg_val) | |
1195 | reg_val--; | |
1196 | ||
1197 | reg_val = clamp(reg_val, (u8)0, (u8)3); | |
1198 | ||
1199 | return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG, | |
1200 | reg_val & IMX274_MASK_LSB_4_BITS); | |
1201 | } | |
1202 | ||
1203 | static inline void imx274_calculate_gain_regs(struct reg_8 regs[2], u16 gain) | |
1204 | { | |
1205 | regs->addr = IMX274_ANALOG_GAIN_ADDR_MSB; | |
1206 | regs->val = (gain >> IMX274_SHIFT_8_BITS) & IMX274_MASK_LSB_3_BITS; | |
1207 | ||
1208 | (regs + 1)->addr = IMX274_ANALOG_GAIN_ADDR_LSB; | |
1209 | (regs + 1)->val = (gain) & IMX274_MASK_LSB_8_BITS; | |
1210 | } | |
1211 | ||
1212 | /* | |
1213 | * imx274_set_gain - Function called when setting gain | |
1214 | * @priv: Pointer to device structure | |
1215 | * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT; | |
1216 | * @ctrl: v4l2 control pointer | |
1217 | * | |
1218 | * Set the gain based on input value. | |
1219 | * The caller should hold the mutex lock imx274->lock if necessary | |
1220 | * | |
1221 | * Return: 0 on success | |
1222 | */ | |
1223 | static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl) | |
1224 | { | |
1225 | struct reg_8 reg_list[2]; | |
1226 | int err; | |
1227 | u32 gain, analog_gain, digital_gain, gain_reg; | |
1228 | int i; | |
1229 | ||
1230 | gain = (u32)(ctrl->val); | |
1231 | ||
1232 | dev_dbg(&priv->client->dev, | |
1233 | "%s : input gain = %d.%d\n", __func__, | |
1234 | gain >> IMX274_GAIN_SHIFT, | |
1235 | ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT); | |
1236 | ||
1237 | if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN) | |
1238 | gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN; | |
1239 | else if (gain < IMX274_MIN_GAIN) | |
1240 | gain = IMX274_MIN_GAIN; | |
1241 | ||
1242 | if (gain <= IMX274_MAX_ANALOG_GAIN) | |
1243 | digital_gain = 1; | |
1244 | else if (gain <= IMX274_MAX_ANALOG_GAIN * 2) | |
1245 | digital_gain = 2; | |
1246 | else if (gain <= IMX274_MAX_ANALOG_GAIN * 4) | |
1247 | digital_gain = 4; | |
1248 | else | |
1249 | digital_gain = IMX274_MAX_DIGITAL_GAIN; | |
1250 | ||
1251 | analog_gain = gain / digital_gain; | |
1252 | ||
1253 | dev_dbg(&priv->client->dev, | |
1254 | "%s : digital gain = %d, analog gain = %d.%d\n", | |
1255 | __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT, | |
1256 | ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100) | |
1257 | >> IMX274_GAIN_SHIFT); | |
1258 | ||
1259 | err = imx274_set_digital_gain(priv, digital_gain); | |
1260 | if (err) | |
1261 | goto fail; | |
1262 | ||
1263 | /* convert to register value, refer to imx274 datasheet */ | |
1264 | gain_reg = (u32)IMX274_GAIN_CONST - | |
1265 | (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain; | |
1266 | if (gain_reg > IMX274_GAIN_REG_MAX) | |
1267 | gain_reg = IMX274_GAIN_REG_MAX; | |
1268 | ||
1269 | imx274_calculate_gain_regs(reg_list, (u16)gain_reg); | |
1270 | ||
1271 | for (i = 0; i < ARRAY_SIZE(reg_list); i++) { | |
1272 | err = imx274_write_reg(priv, reg_list[i].addr, | |
1273 | reg_list[i].val); | |
1274 | if (err) | |
1275 | goto fail; | |
1276 | } | |
1277 | ||
1278 | if (IMX274_GAIN_CONST - gain_reg == 0) { | |
1279 | err = -EINVAL; | |
1280 | goto fail; | |
1281 | } | |
1282 | ||
1283 | /* convert register value back to gain value */ | |
1284 | ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) | |
1285 | / (IMX274_GAIN_CONST - gain_reg) * digital_gain; | |
1286 | ||
1287 | dev_dbg(&priv->client->dev, | |
1288 | "%s : GAIN control success, gain_reg = %d, new gain = %d\n", | |
1289 | __func__, gain_reg, ctrl->val); | |
1290 | ||
1291 | return 0; | |
1292 | ||
1293 | fail: | |
1294 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); | |
1295 | return err; | |
1296 | } | |
1297 | ||
1298 | static inline void imx274_calculate_coarse_time_regs(struct reg_8 regs[2], | |
1299 | u32 coarse_time) | |
1300 | { | |
1301 | regs->addr = IMX274_COARSE_TIME_ADDR_MSB; | |
1302 | regs->val = (coarse_time >> IMX274_SHIFT_8_BITS) | |
1303 | & IMX274_MASK_LSB_8_BITS; | |
1304 | (regs + 1)->addr = IMX274_COARSE_TIME_ADDR_LSB; | |
1305 | (regs + 1)->val = (coarse_time) & IMX274_MASK_LSB_8_BITS; | |
1306 | } | |
1307 | ||
1308 | /* | |
1309 | * imx274_set_coarse_time - Function called when setting SHR value | |
1310 | * @priv: Pointer to device structure | |
1311 | * @val: Value for exposure time in number of line_length, or [HMAX] | |
1312 | * | |
1313 | * Set SHR value based on input value. | |
1314 | * | |
1315 | * Return: 0 on success | |
1316 | */ | |
1317 | static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val) | |
1318 | { | |
1319 | struct reg_8 reg_list[2]; | |
1320 | int err; | |
1321 | u32 coarse_time, frame_length; | |
1322 | int i; | |
1323 | ||
1324 | coarse_time = *val; | |
1325 | ||
1326 | /* convert exposure_time to appropriate SHR value */ | |
1327 | err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length); | |
1328 | if (err) | |
1329 | goto fail; | |
1330 | ||
1331 | /* prepare SHR registers */ | |
1332 | imx274_calculate_coarse_time_regs(reg_list, coarse_time); | |
1333 | ||
1334 | /* write to SHR registers */ | |
1335 | for (i = 0; i < ARRAY_SIZE(reg_list); i++) { | |
1336 | err = imx274_write_reg(priv, reg_list[i].addr, | |
1337 | reg_list[i].val); | |
1338 | if (err) | |
1339 | goto fail; | |
1340 | } | |
1341 | ||
1342 | *val = frame_length - coarse_time; | |
1343 | return 0; | |
1344 | ||
1345 | fail: | |
1346 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); | |
1347 | return err; | |
1348 | } | |
1349 | ||
1350 | /* | |
1351 | * imx274_set_exposure - Function called when setting exposure time | |
1352 | * @priv: Pointer to device structure | |
1353 | * @val: Variable for exposure time, in the unit of micro-second | |
1354 | * | |
1355 | * Set exposure time based on input value. | |
1356 | * The caller should hold the mutex lock imx274->lock if necessary | |
1357 | * | |
1358 | * Return: 0 on success | |
1359 | */ | |
1360 | static int imx274_set_exposure(struct stimx274 *priv, int val) | |
1361 | { | |
1362 | int err; | |
1363 | u16 hmax; | |
1364 | u8 reg_val[2]; | |
1365 | u32 coarse_time; /* exposure time in unit of line (HMAX)*/ | |
1366 | ||
1367 | dev_dbg(&priv->client->dev, | |
1368 | "%s : EXPOSURE control input = %d\n", __func__, val); | |
1369 | ||
1370 | /* step 1: convert input exposure_time (val) into number of 1[HMAX] */ | |
1371 | ||
1372 | /* obtain HMAX value */ | |
1373 | err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, ®_val[0]); | |
1374 | if (err) | |
1375 | goto fail; | |
1376 | err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, ®_val[1]); | |
1377 | if (err) | |
1378 | goto fail; | |
1379 | hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; | |
1380 | if (hmax == 0) { | |
1381 | err = -EINVAL; | |
1382 | goto fail; | |
1383 | } | |
1384 | ||
1385 | coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val | |
1386 | - nocpiop[priv->mode_index]) / hmax; | |
1387 | ||
1388 | /* step 2: convert exposure_time into SHR value */ | |
1389 | ||
1390 | /* set SHR */ | |
1391 | err = imx274_set_coarse_time(priv, &coarse_time); | |
1392 | if (err) | |
1393 | goto fail; | |
1394 | ||
1395 | priv->ctrls.exposure->val = | |
1396 | (coarse_time * hmax + nocpiop[priv->mode_index]) | |
1397 | / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2); | |
1398 | ||
1399 | dev_dbg(&priv->client->dev, | |
1400 | "%s : EXPOSURE control success\n", __func__); | |
1401 | return 0; | |
1402 | ||
1403 | fail: | |
1404 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); | |
1405 | ||
1406 | return err; | |
1407 | } | |
1408 | ||
1409 | /* | |
1410 | * imx274_set_vflip - Function called when setting vertical flip | |
1411 | * @priv: Pointer to device structure | |
1412 | * @val: Value for vflip setting | |
1413 | * | |
1414 | * Set vertical flip based on input value. | |
1415 | * val = 0: normal, no vertical flip | |
1416 | * val = 1: vertical flip enabled | |
1417 | * The caller should hold the mutex lock imx274->lock if necessary | |
1418 | * | |
1419 | * Return: 0 on success | |
1420 | */ | |
1421 | static int imx274_set_vflip(struct stimx274 *priv, int val) | |
1422 | { | |
1423 | int err; | |
1424 | ||
1425 | err = imx274_write_reg(priv, IMX274_VFLIP_REG, val); | |
1426 | if (err) { | |
1427 | dev_err(&priv->client->dev, "VFILP control error\n"); | |
1428 | return err; | |
1429 | } | |
1430 | ||
1431 | dev_dbg(&priv->client->dev, | |
1432 | "%s : VFLIP control success\n", __func__); | |
1433 | ||
1434 | return 0; | |
1435 | } | |
1436 | ||
1437 | /* | |
1438 | * imx274_set_test_pattern - Function called when setting test pattern | |
1439 | * @priv: Pointer to device structure | |
1440 | * @val: Variable for test pattern | |
1441 | * | |
1442 | * Set to different test patterns based on input value. | |
1443 | * | |
1444 | * Return: 0 on success | |
1445 | */ | |
1446 | static int imx274_set_test_pattern(struct stimx274 *priv, int val) | |
1447 | { | |
1448 | int err = 0; | |
1449 | ||
1450 | if (val == TEST_PATTERN_DISABLED) { | |
1451 | err = imx274_write_table(priv, imx274_tp_disabled); | |
1452 | } else if (val <= TEST_PATTERN_V_COLOR_BARS) { | |
1453 | err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1); | |
1454 | if (!err) | |
1455 | err = imx274_write_table(priv, imx274_tp_regs); | |
1456 | } else { | |
1457 | err = -EINVAL; | |
1458 | } | |
1459 | ||
1460 | if (!err) | |
1461 | dev_dbg(&priv->client->dev, | |
1462 | "%s : TEST PATTERN control success\n", __func__); | |
1463 | else | |
1464 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); | |
1465 | ||
1466 | return err; | |
1467 | } | |
1468 | ||
1469 | static inline void imx274_calculate_frame_length_regs(struct reg_8 regs[3], | |
1470 | u32 frame_length) | |
1471 | { | |
1472 | regs->addr = IMX274_FRAME_LENGTH_ADDR_1; | |
1473 | regs->val = (frame_length >> IMX274_SHIFT_16_BITS) | |
1474 | & IMX274_MASK_LSB_4_BITS; | |
1475 | (regs + 1)->addr = IMX274_FRAME_LENGTH_ADDR_2; | |
1476 | (regs + 1)->val = (frame_length >> IMX274_SHIFT_8_BITS) | |
1477 | & IMX274_MASK_LSB_8_BITS; | |
1478 | (regs + 2)->addr = IMX274_FRAME_LENGTH_ADDR_3; | |
1479 | (regs + 2)->val = (frame_length) & IMX274_MASK_LSB_8_BITS; | |
1480 | } | |
1481 | ||
1482 | /* | |
1483 | * imx274_set_frame_length - Function called when setting frame length | |
1484 | * @priv: Pointer to device structure | |
1485 | * @val: Variable for frame length (= VMAX, i.e. vertical drive period length) | |
1486 | * | |
1487 | * Set frame length based on input value. | |
1488 | * | |
1489 | * Return: 0 on success | |
1490 | */ | |
1491 | static int imx274_set_frame_length(struct stimx274 *priv, u32 val) | |
1492 | { | |
1493 | struct reg_8 reg_list[3]; | |
1494 | int err; | |
1495 | u32 frame_length; | |
1496 | int i; | |
1497 | ||
1498 | dev_dbg(&priv->client->dev, "%s : input length = %d\n", | |
1499 | __func__, val); | |
1500 | ||
1501 | frame_length = (u32)val; | |
1502 | ||
1503 | imx274_calculate_frame_length_regs(reg_list, frame_length); | |
1504 | for (i = 0; i < ARRAY_SIZE(reg_list); i++) { | |
1505 | err = imx274_write_reg(priv, reg_list[i].addr, | |
1506 | reg_list[i].val); | |
1507 | if (err) | |
1508 | goto fail; | |
1509 | } | |
1510 | ||
1511 | return 0; | |
1512 | ||
1513 | fail: | |
1514 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); | |
1515 | return err; | |
1516 | } | |
1517 | ||
1518 | /* | |
1519 | * imx274_set_frame_interval - Function called when setting frame interval | |
1520 | * @priv: Pointer to device structure | |
1521 | * @frame_interval: Variable for frame interval | |
1522 | * | |
1523 | * Change frame interval by updating VMAX value | |
1524 | * The caller should hold the mutex lock imx274->lock if necessary | |
1525 | * | |
1526 | * Return: 0 on success | |
1527 | */ | |
1528 | static int imx274_set_frame_interval(struct stimx274 *priv, | |
1529 | struct v4l2_fract frame_interval) | |
1530 | { | |
1531 | int err; | |
1532 | u32 frame_length, req_frame_rate; | |
1533 | u16 svr; | |
1534 | u16 hmax; | |
1535 | u8 reg_val[2]; | |
1536 | ||
1537 | dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d", | |
1538 | __func__, frame_interval.numerator, | |
1539 | frame_interval.denominator); | |
1540 | ||
1541 | if (frame_interval.numerator == 0) { | |
1542 | err = -EINVAL; | |
1543 | goto fail; | |
1544 | } | |
1545 | ||
1546 | req_frame_rate = (u32)(frame_interval.denominator | |
1547 | / frame_interval.numerator); | |
1548 | ||
1549 | /* boundary check */ | |
1550 | if (req_frame_rate > max_frame_rate[priv->mode_index]) { | |
1551 | frame_interval.numerator = 1; | |
1552 | frame_interval.denominator = | |
1553 | max_frame_rate[priv->mode_index]; | |
1554 | } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) { | |
1555 | frame_interval.numerator = 1; | |
1556 | frame_interval.denominator = IMX274_MIN_FRAME_RATE; | |
1557 | } | |
1558 | ||
1559 | /* | |
1560 | * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX | |
1561 | * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX | |
1562 | */ | |
1563 | ||
1564 | /* SVR */ | |
1565 | err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, ®_val[0]); | |
1566 | if (err) | |
1567 | goto fail; | |
1568 | err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, ®_val[1]); | |
1569 | if (err) | |
1570 | goto fail; | |
1571 | svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; | |
1572 | dev_dbg(&priv->client->dev, | |
1573 | "%s : register SVR = %d\n", __func__, svr); | |
1574 | ||
1575 | /* HMAX */ | |
1576 | err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, ®_val[0]); | |
1577 | if (err) | |
1578 | goto fail; | |
1579 | err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, ®_val[1]); | |
1580 | if (err) | |
1581 | goto fail; | |
1582 | hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; | |
1583 | dev_dbg(&priv->client->dev, | |
1584 | "%s : register HMAX = %d\n", __func__, hmax); | |
1585 | ||
1586 | if (hmax == 0 || frame_interval.denominator == 0) { | |
1587 | err = -EINVAL; | |
1588 | goto fail; | |
1589 | } | |
1590 | ||
1591 | frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax | |
1592 | * frame_interval.numerator | |
1593 | / frame_interval.denominator; | |
1594 | ||
1595 | err = imx274_set_frame_length(priv, frame_length); | |
1596 | if (err) | |
1597 | goto fail; | |
1598 | ||
1599 | priv->frame_interval = frame_interval; | |
1600 | return 0; | |
1601 | ||
1602 | fail: | |
1603 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); | |
1604 | return err; | |
1605 | } | |
1606 | ||
1607 | static const struct v4l2_subdev_pad_ops imx274_pad_ops = { | |
1608 | .get_fmt = imx274_get_fmt, | |
1609 | .set_fmt = imx274_set_fmt, | |
1610 | }; | |
1611 | ||
1612 | static const struct v4l2_subdev_video_ops imx274_video_ops = { | |
1613 | .g_frame_interval = imx274_g_frame_interval, | |
1614 | .s_frame_interval = imx274_s_frame_interval, | |
1615 | .s_stream = imx274_s_stream, | |
1616 | }; | |
1617 | ||
1618 | static const struct v4l2_subdev_ops imx274_subdev_ops = { | |
1619 | .pad = &imx274_pad_ops, | |
1620 | .video = &imx274_video_ops, | |
1621 | }; | |
1622 | ||
1623 | static const struct v4l2_ctrl_ops imx274_ctrl_ops = { | |
1624 | .s_ctrl = imx274_s_ctrl, | |
1625 | }; | |
1626 | ||
1627 | static const struct of_device_id imx274_of_id_table[] = { | |
1628 | { .compatible = "sony,imx274" }, | |
1629 | { } | |
1630 | }; | |
1631 | MODULE_DEVICE_TABLE(of, imx274_of_id_table); | |
1632 | ||
1633 | static const struct i2c_device_id imx274_id[] = { | |
1634 | { "IMX274", 0 }, | |
1635 | { } | |
1636 | }; | |
1637 | MODULE_DEVICE_TABLE(i2c, imx274_id); | |
1638 | ||
1639 | static int imx274_probe(struct i2c_client *client, | |
1640 | const struct i2c_device_id *id) | |
1641 | { | |
1642 | struct v4l2_subdev *sd; | |
1643 | struct stimx274 *imx274; | |
1644 | int ret; | |
1645 | ||
1646 | /* initialize imx274 */ | |
1647 | imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL); | |
1648 | if (!imx274) | |
1649 | return -ENOMEM; | |
1650 | ||
1651 | mutex_init(&imx274->lock); | |
1652 | ||
1653 | /* initialize regmap */ | |
1654 | imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config); | |
1655 | if (IS_ERR(imx274->regmap)) { | |
1656 | dev_err(&client->dev, | |
1657 | "regmap init failed: %ld\n", PTR_ERR(imx274->regmap)); | |
1658 | ret = -ENODEV; | |
1659 | goto err_regmap; | |
1660 | } | |
1661 | ||
1662 | /* initialize subdevice */ | |
1663 | imx274->client = client; | |
1664 | sd = &imx274->sd; | |
1665 | v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops); | |
1666 | strlcpy(sd->name, DRIVER_NAME, sizeof(sd->name)); | |
1667 | sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; | |
1668 | ||
1669 | /* initialize subdev media pad */ | |
1670 | imx274->pad.flags = MEDIA_PAD_FL_SOURCE; | |
1671 | sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; | |
1672 | ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad); | |
1673 | if (ret < 0) { | |
1674 | dev_err(&client->dev, | |
1675 | "%s : media entity init Failed %d\n", __func__, ret); | |
1676 | goto err_regmap; | |
1677 | } | |
1678 | ||
1679 | /* initialize sensor reset gpio */ | |
1680 | imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", | |
1681 | GPIOD_OUT_HIGH); | |
1682 | if (IS_ERR(imx274->reset_gpio)) { | |
1683 | if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER) | |
1684 | dev_err(&client->dev, "Reset GPIO not setup in DT"); | |
1685 | ret = PTR_ERR(imx274->reset_gpio); | |
1686 | goto err_me; | |
1687 | } | |
1688 | ||
1689 | /* pull sensor out of reset */ | |
1690 | imx274_reset(imx274, 1); | |
1691 | ||
1692 | /* initialize controls */ | |
1693 | ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 2); | |
1694 | if (ret < 0) { | |
1695 | dev_err(&client->dev, | |
1696 | "%s : ctrl handler init Failed\n", __func__); | |
1697 | goto err_me; | |
1698 | } | |
1699 | ||
1700 | imx274->ctrls.handler.lock = &imx274->lock; | |
1701 | ||
1702 | /* add new controls */ | |
1703 | imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items( | |
1704 | &imx274->ctrls.handler, &imx274_ctrl_ops, | |
1705 | V4L2_CID_TEST_PATTERN, | |
1706 | ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu); | |
1707 | ||
1708 | imx274->ctrls.gain = v4l2_ctrl_new_std( | |
1709 | &imx274->ctrls.handler, | |
1710 | &imx274_ctrl_ops, | |
1711 | V4L2_CID_GAIN, IMX274_MIN_GAIN, | |
1712 | IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1, | |
1713 | IMX274_DEF_GAIN); | |
1714 | ||
1715 | imx274->ctrls.exposure = v4l2_ctrl_new_std( | |
1716 | &imx274->ctrls.handler, | |
1717 | &imx274_ctrl_ops, | |
1718 | V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME, | |
1719 | 1000000 / IMX274_DEF_FRAME_RATE, 1, | |
1720 | IMX274_MIN_EXPOSURE_TIME); | |
1721 | ||
1722 | imx274->ctrls.vflip = v4l2_ctrl_new_std( | |
1723 | &imx274->ctrls.handler, | |
1724 | &imx274_ctrl_ops, | |
1725 | V4L2_CID_VFLIP, 0, 1, 1, 0); | |
1726 | ||
1727 | imx274->sd.ctrl_handler = &imx274->ctrls.handler; | |
1728 | if (imx274->ctrls.handler.error) { | |
1729 | ret = imx274->ctrls.handler.error; | |
1730 | goto err_ctrls; | |
1731 | } | |
1732 | ||
1733 | /* setup default controls */ | |
1734 | ret = v4l2_ctrl_handler_setup(&imx274->ctrls.handler); | |
1735 | if (ret) { | |
1736 | dev_err(&client->dev, | |
1737 | "Error %d setup default controls\n", ret); | |
1738 | goto err_ctrls; | |
1739 | } | |
1740 | ||
1741 | /* initialize format */ | |
1742 | imx274->mode_index = IMX274_MODE_3840X2160; | |
1743 | imx274->format.width = imx274_formats[0].size.width; | |
1744 | imx274->format.height = imx274_formats[0].size.height; | |
1745 | imx274->format.field = V4L2_FIELD_NONE; | |
1746 | imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10; | |
1747 | imx274->format.colorspace = V4L2_COLORSPACE_SRGB; | |
1748 | imx274->frame_interval.numerator = 1; | |
1749 | imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE; | |
1750 | ||
1751 | /* load default control values */ | |
1752 | ret = imx274_load_default(imx274); | |
1753 | if (ret) { | |
1754 | dev_err(&client->dev, | |
1755 | "%s : imx274_load_default failed %d\n", | |
1756 | __func__, ret); | |
1757 | goto err_ctrls; | |
1758 | } | |
1759 | ||
1760 | /* register subdevice */ | |
1761 | ret = v4l2_async_register_subdev(sd); | |
1762 | if (ret < 0) { | |
1763 | dev_err(&client->dev, | |
1764 | "%s : v4l2_async_register_subdev failed %d\n", | |
1765 | __func__, ret); | |
1766 | goto err_ctrls; | |
1767 | } | |
1768 | ||
1769 | dev_info(&client->dev, "imx274 : imx274 probe success !\n"); | |
1770 | return 0; | |
1771 | ||
1772 | err_ctrls: | |
781b045b | 1773 | v4l2_ctrl_handler_free(&imx274->ctrls.handler); |
0985dd30 LL |
1774 | err_me: |
1775 | media_entity_cleanup(&sd->entity); | |
1776 | err_regmap: | |
1777 | mutex_destroy(&imx274->lock); | |
1778 | return ret; | |
1779 | } | |
1780 | ||
1781 | static int imx274_remove(struct i2c_client *client) | |
1782 | { | |
1783 | struct v4l2_subdev *sd = i2c_get_clientdata(client); | |
1784 | struct stimx274 *imx274 = to_imx274(sd); | |
1785 | ||
1786 | /* stop stream */ | |
1787 | imx274_write_table(imx274, mode_table[IMX274_MODE_STOP_STREAM]); | |
1788 | ||
1789 | v4l2_async_unregister_subdev(sd); | |
781b045b | 1790 | v4l2_ctrl_handler_free(&imx274->ctrls.handler); |
0985dd30 LL |
1791 | media_entity_cleanup(&sd->entity); |
1792 | mutex_destroy(&imx274->lock); | |
1793 | return 0; | |
1794 | } | |
1795 | ||
1796 | static struct i2c_driver imx274_i2c_driver = { | |
1797 | .driver = { | |
1798 | .name = DRIVER_NAME, | |
1799 | .of_match_table = imx274_of_id_table, | |
1800 | }, | |
1801 | .probe = imx274_probe, | |
1802 | .remove = imx274_remove, | |
1803 | .id_table = imx274_id, | |
1804 | }; | |
1805 | ||
1806 | module_i2c_driver(imx274_i2c_driver); | |
1807 | ||
1808 | MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>"); | |
1809 | MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver"); | |
1810 | MODULE_LICENSE("GPL v2"); |