blob: 0fe8b869245b8a2098a0691238cb43354032468a [file] [log] [blame]
Leon Luo0985dd32017-10-05 02:06:21 +02001/*
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>
Luca Ceresoli39dd23d2018-07-25 12:24:55 -04008 * Luca Ceresoli <luca@lucaceresoli.net>
Leon Luo0985dd32017-10-05 02:06:21 +02009 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include <linux/clk.h>
24#include <linux/delay.h>
25#include <linux/gpio.h>
26#include <linux/gpio/consumer.h>
27#include <linux/i2c.h>
28#include <linux/init.h>
Luca Ceresoli39dd23d2018-07-25 12:24:55 -040029#include <linux/kernel.h>
Leon Luo0985dd32017-10-05 02:06:21 +020030#include <linux/module.h>
31#include <linux/of_gpio.h>
32#include <linux/regmap.h>
33#include <linux/slab.h>
34#include <linux/v4l2-mediabus.h>
35#include <linux/videodev2.h>
36
37#include <media/v4l2-ctrls.h>
38#include <media/v4l2-device.h>
39#include <media/v4l2-subdev.h>
40
41/*
42 * See "SHR, SVR Setting" in datasheet
43 */
44#define IMX274_DEFAULT_FRAME_LENGTH (4550)
45#define IMX274_MAX_FRAME_LENGTH (0x000fffff)
46
47/*
48 * See "Frame Rate Adjustment" in datasheet
49 */
50#define IMX274_PIXCLK_CONST1 (72000000)
51#define IMX274_PIXCLK_CONST2 (1000000)
52
53/*
54 * The input gain is shifted by IMX274_GAIN_SHIFT to get
55 * decimal number. The real gain is
56 * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
57 */
58#define IMX274_GAIN_SHIFT (8)
59#define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1)
60
61/*
62 * See "Analog Gain" and "Digital Gain" in datasheet
63 * min gain is 1X
64 * max gain is calculated based on IMX274_GAIN_REG_MAX
65 */
66#define IMX274_GAIN_REG_MAX (1957)
67#define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT)
68#define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\
69 / (2048 - IMX274_GAIN_REG_MAX))
70#define IMX274_MAX_DIGITAL_GAIN (8)
71#define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT)
72#define IMX274_GAIN_CONST (2048) /* for gain formula */
73
74/*
75 * 1 line time in us = (HMAX / 72), minimal is 4 lines
76 */
77#define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72)
78
Luca Ceresoli39dd23d2018-07-25 12:24:55 -040079#define IMX274_DEFAULT_MODE IMX274_BINNING_OFF
Leon Luo0985dd32017-10-05 02:06:21 +020080#define IMX274_MAX_WIDTH (3840)
81#define IMX274_MAX_HEIGHT (2160)
82#define IMX274_MAX_FRAME_RATE (120)
83#define IMX274_MIN_FRAME_RATE (5)
84#define IMX274_DEF_FRAME_RATE (60)
85
86/*
87 * register SHR is limited to (SVR value + 1) x VMAX value - 4
88 */
89#define IMX274_SHR_LIMIT_CONST (4)
90
91/*
Luca Ceresoli4c858e92018-04-24 04:24:06 -040092 * Min and max sensor reset delay (microseconds)
Leon Luo0985dd32017-10-05 02:06:21 +020093 */
94#define IMX274_RESET_DELAY1 (2000)
95#define IMX274_RESET_DELAY2 (2200)
96
97/*
98 * shift and mask constants
99 */
100#define IMX274_SHIFT_8_BITS (8)
101#define IMX274_SHIFT_16_BITS (16)
102#define IMX274_MASK_LSB_2_BITS (0x03)
103#define IMX274_MASK_LSB_3_BITS (0x07)
104#define IMX274_MASK_LSB_4_BITS (0x0f)
105#define IMX274_MASK_LSB_8_BITS (0x00ff)
106
107#define DRIVER_NAME "IMX274"
108
109/*
110 * IMX274 register definitions
111 */
Luca Ceresoli4eb78462018-04-24 04:24:10 -0400112#define IMX274_SHR_REG_MSB 0x300D /* SHR */
113#define IMX274_SHR_REG_LSB 0x300C /* SHR */
Leon Luo0985dd32017-10-05 02:06:21 +0200114#define IMX274_SVR_REG_MSB 0x300F /* SVR */
115#define IMX274_SVR_REG_LSB 0x300E /* SVR */
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400116#define IMX274_HTRIM_EN_REG 0x3037
117#define IMX274_HTRIM_START_REG_LSB 0x3038
118#define IMX274_HTRIM_START_REG_MSB 0x3039
119#define IMX274_HTRIM_END_REG_LSB 0x303A
120#define IMX274_HTRIM_END_REG_MSB 0x303B
121#define IMX274_VWIDCUTEN_REG 0x30DD
122#define IMX274_VWIDCUT_REG_LSB 0x30DE
123#define IMX274_VWIDCUT_REG_MSB 0x30DF
124#define IMX274_VWINPOS_REG_LSB 0x30E0
125#define IMX274_VWINPOS_REG_MSB 0x30E1
126#define IMX274_WRITE_VSIZE_REG_LSB 0x3130
127#define IMX274_WRITE_VSIZE_REG_MSB 0x3131
128#define IMX274_Y_OUT_SIZE_REG_LSB 0x3132
129#define IMX274_Y_OUT_SIZE_REG_MSB 0x3133
Luca Ceresoli4eb78462018-04-24 04:24:10 -0400130#define IMX274_VMAX_REG_1 0x30FA /* VMAX, MSB */
131#define IMX274_VMAX_REG_2 0x30F9 /* VMAX */
132#define IMX274_VMAX_REG_3 0x30F8 /* VMAX, LSB */
Leon Luo0985dd32017-10-05 02:06:21 +0200133#define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */
134#define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */
Leon Luo0985dd32017-10-05 02:06:21 +0200135#define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */
136#define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */
137#define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */
138#define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */
139#define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */
140#define IMX274_STANDBY_REG 0x3000 /* STANDBY */
141
142#define IMX274_TABLE_WAIT_MS 0
143#define IMX274_TABLE_END 1
144
145/*
146 * imx274 I2C operation related structure
147 */
148struct reg_8 {
149 u16 addr;
150 u8 val;
151};
152
153static const struct regmap_config imx274_regmap_config = {
154 .reg_bits = 16,
155 .val_bits = 8,
156 .cache_type = REGCACHE_RBTREE,
157};
158
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400159enum imx274_binning {
160 IMX274_BINNING_OFF,
161 IMX274_BINNING_2_1,
162 IMX274_BINNING_3_1,
Leon Luo0985dd32017-10-05 02:06:21 +0200163};
164
165/*
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400166 * Parameters for each imx274 readout mode.
167 *
168 * These are the values to configure the sensor in one of the
169 * implemented modes.
170 *
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400171 * @init_regs: registers to initialize the mode
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400172 * @bin_ratio: downscale factor (e.g. 3 for 3:1 binning)
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400173 * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
174 * Adjustment (CSI-2)" in the datasheet)
175 * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
176 * datasheet)
177 * @max_fps: Maximum frames per second
178 * @nocpiop: Number of clocks per internal offset period (see "Integration Time
179 * in Each Readout Drive Mode (CSI-2)" in the datasheet)
Leon Luo0985dd32017-10-05 02:06:21 +0200180 */
181struct imx274_frmfmt {
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400182 const struct reg_8 *init_regs;
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400183 unsigned int bin_ratio;
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400184 int min_frame_len;
185 int min_SHR;
186 int max_fps;
187 int nocpiop;
Leon Luo0985dd32017-10-05 02:06:21 +0200188};
189
190/*
191 * imx274 test pattern related structure
192 */
193enum {
194 TEST_PATTERN_DISABLED = 0,
195 TEST_PATTERN_ALL_000H,
196 TEST_PATTERN_ALL_FFFH,
197 TEST_PATTERN_ALL_555H,
198 TEST_PATTERN_ALL_AAAH,
199 TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */
200 TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */
201 TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */
202 TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */
203 TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */
204 TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */
205 TEST_PATTERN_H_COLOR_BARS,
206 TEST_PATTERN_V_COLOR_BARS,
207};
208
209static const char * const tp_qmenu[] = {
210 "Disabled",
211 "All 000h Pattern",
212 "All FFFh Pattern",
213 "All 555h Pattern",
214 "All AAAh Pattern",
215 "Vertical Stripe (555h / AAAh)",
216 "Vertical Stripe (AAAh / 555h)",
217 "Vertical Stripe (000h / 555h)",
218 "Vertical Stripe (555h / 000h)",
219 "Vertical Stripe (000h / FFFh)",
220 "Vertical Stripe (FFFh / 000h)",
221 "Horizontal Color Bars",
222 "Vertical Color Bars",
223};
224
225/*
226 * All-pixel scan mode (10-bit)
227 * imx274 mode1(refer to datasheet) register configuration with
228 * 3840x2160 resolution, raw10 data and mipi four lane output
229 */
230static const struct reg_8 imx274_mode1_3840x2160_raw10[] = {
231 {0x3004, 0x01},
232 {0x3005, 0x01},
233 {0x3006, 0x00},
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400234 {0x3007, 0xa2},
Leon Luo0985dd32017-10-05 02:06:21 +0200235
236 {0x3018, 0xA2}, /* output XVS, HVS */
237
238 {0x306B, 0x05},
239 {0x30E2, 0x01},
Leon Luo0985dd32017-10-05 02:06:21 +0200240
241 {0x30EE, 0x01},
Leon Luo0985dd32017-10-05 02:06:21 +0200242 {0x3342, 0x0A},
243 {0x3343, 0x00},
244 {0x3344, 0x16},
245 {0x3345, 0x00},
246 {0x33A6, 0x01},
247 {0x3528, 0x0E},
248 {0x3554, 0x1F},
249 {0x3555, 0x01},
250 {0x3556, 0x01},
251 {0x3557, 0x01},
252 {0x3558, 0x01},
253 {0x3559, 0x00},
254 {0x355A, 0x00},
255 {0x35BA, 0x0E},
256 {0x366A, 0x1B},
257 {0x366B, 0x1A},
258 {0x366C, 0x19},
259 {0x366D, 0x17},
260 {0x3A41, 0x08},
261
262 {IMX274_TABLE_END, 0x00}
263};
264
265/*
266 * Horizontal/vertical 2/2-line binning
267 * (Horizontal and vertical weightedbinning, 10-bit)
268 * imx274 mode3(refer to datasheet) register configuration with
269 * 1920x1080 resolution, raw10 data and mipi four lane output
270 */
271static const struct reg_8 imx274_mode3_1920x1080_raw10[] = {
272 {0x3004, 0x02},
273 {0x3005, 0x21},
274 {0x3006, 0x00},
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400275 {0x3007, 0xb1},
Leon Luo0985dd32017-10-05 02:06:21 +0200276
277 {0x3018, 0xA2}, /* output XVS, HVS */
278
279 {0x306B, 0x05},
280 {0x30E2, 0x02},
281
Leon Luo0985dd32017-10-05 02:06:21 +0200282 {0x30EE, 0x01},
Leon Luo0985dd32017-10-05 02:06:21 +0200283 {0x3342, 0x0A},
284 {0x3343, 0x00},
285 {0x3344, 0x1A},
286 {0x3345, 0x00},
287 {0x33A6, 0x01},
288 {0x3528, 0x0E},
289 {0x3554, 0x00},
290 {0x3555, 0x01},
291 {0x3556, 0x01},
292 {0x3557, 0x01},
293 {0x3558, 0x01},
294 {0x3559, 0x00},
295 {0x355A, 0x00},
296 {0x35BA, 0x0E},
297 {0x366A, 0x1B},
298 {0x366B, 0x1A},
299 {0x366C, 0x19},
300 {0x366D, 0x17},
301 {0x3A41, 0x08},
302
303 {IMX274_TABLE_END, 0x00}
304};
305
306/*
307 * Vertical 2/3 subsampling binning horizontal 3 binning
308 * imx274 mode5(refer to datasheet) register configuration with
309 * 1280x720 resolution, raw10 data and mipi four lane output
310 */
311static const struct reg_8 imx274_mode5_1280x720_raw10[] = {
312 {0x3004, 0x03},
313 {0x3005, 0x31},
314 {0x3006, 0x00},
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400315 {0x3007, 0xa9},
Leon Luo0985dd32017-10-05 02:06:21 +0200316
317 {0x3018, 0xA2}, /* output XVS, HVS */
318
319 {0x306B, 0x05},
320 {0x30E2, 0x03},
321
Leon Luo0985dd32017-10-05 02:06:21 +0200322 {0x30EE, 0x01},
Leon Luo0985dd32017-10-05 02:06:21 +0200323 {0x3342, 0x0A},
324 {0x3343, 0x00},
325 {0x3344, 0x1B},
326 {0x3345, 0x00},
327 {0x33A6, 0x01},
328 {0x3528, 0x0E},
329 {0x3554, 0x00},
330 {0x3555, 0x01},
331 {0x3556, 0x01},
332 {0x3557, 0x01},
333 {0x3558, 0x01},
334 {0x3559, 0x00},
335 {0x355A, 0x00},
336 {0x35BA, 0x0E},
337 {0x366A, 0x1B},
338 {0x366B, 0x19},
339 {0x366C, 0x17},
340 {0x366D, 0x17},
341 {0x3A41, 0x04},
342
343 {IMX274_TABLE_END, 0x00}
344};
345
346/*
347 * imx274 first step register configuration for
348 * starting stream
349 */
350static const struct reg_8 imx274_start_1[] = {
351 {IMX274_STANDBY_REG, 0x12},
352 {IMX274_TABLE_END, 0x00}
353};
354
355/*
356 * imx274 second step register configuration for
357 * starting stream
358 */
359static const struct reg_8 imx274_start_2[] = {
360 {0x3120, 0xF0}, /* clock settings */
361 {0x3121, 0x00}, /* clock settings */
362 {0x3122, 0x02}, /* clock settings */
363 {0x3129, 0x9C}, /* clock settings */
364 {0x312A, 0x02}, /* clock settings */
365 {0x312D, 0x02}, /* clock settings */
366
367 {0x310B, 0x00},
368
369 /* PLSTMG */
370 {0x304C, 0x00}, /* PLSTMG01 */
371 {0x304D, 0x03},
372 {0x331C, 0x1A},
373 {0x331D, 0x00},
374 {0x3502, 0x02},
375 {0x3529, 0x0E},
376 {0x352A, 0x0E},
377 {0x352B, 0x0E},
378 {0x3538, 0x0E},
379 {0x3539, 0x0E},
380 {0x3553, 0x00},
381 {0x357D, 0x05},
382 {0x357F, 0x05},
383 {0x3581, 0x04},
384 {0x3583, 0x76},
385 {0x3587, 0x01},
386 {0x35BB, 0x0E},
387 {0x35BC, 0x0E},
388 {0x35BD, 0x0E},
389 {0x35BE, 0x0E},
390 {0x35BF, 0x0E},
391 {0x366E, 0x00},
392 {0x366F, 0x00},
393 {0x3670, 0x00},
394 {0x3671, 0x00},
395
396 /* PSMIPI */
397 {0x3304, 0x32}, /* PSMIPI1 */
398 {0x3305, 0x00},
399 {0x3306, 0x32},
400 {0x3307, 0x00},
401 {0x3590, 0x32},
402 {0x3591, 0x00},
403 {0x3686, 0x32},
404 {0x3687, 0x00},
405
406 {IMX274_TABLE_END, 0x00}
407};
408
409/*
410 * imx274 third step register configuration for
411 * starting stream
412 */
413static const struct reg_8 imx274_start_3[] = {
414 {IMX274_STANDBY_REG, 0x00},
415 {0x303E, 0x02}, /* SYS_MODE = 2 */
416 {IMX274_TABLE_END, 0x00}
417};
418
419/*
420 * imx274 forth step register configuration for
421 * starting stream
422 */
423static const struct reg_8 imx274_start_4[] = {
424 {0x30F4, 0x00},
425 {0x3018, 0xA2}, /* XHS VHS OUTUPT */
426 {IMX274_TABLE_END, 0x00}
427};
428
429/*
430 * imx274 register configuration for stoping stream
431 */
432static const struct reg_8 imx274_stop[] = {
433 {IMX274_STANDBY_REG, 0x01},
434 {IMX274_TABLE_END, 0x00}
435};
436
437/*
438 * imx274 disable test pattern register configuration
439 */
440static const struct reg_8 imx274_tp_disabled[] = {
441 {0x303C, 0x00},
442 {0x377F, 0x00},
443 {0x3781, 0x00},
444 {0x370B, 0x00},
445 {IMX274_TABLE_END, 0x00}
446};
447
448/*
449 * imx274 test pattern register configuration
450 * reg 0x303D defines the test pattern modes
451 */
452static const struct reg_8 imx274_tp_regs[] = {
453 {0x303C, 0x11},
454 {0x370E, 0x01},
455 {0x377F, 0x01},
456 {0x3781, 0x01},
457 {0x370B, 0x11},
458 {IMX274_TABLE_END, 0x00}
459};
460
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400461/* nocpiop happens to be the same number for the implemented modes */
Leon Luo0985dd32017-10-05 02:06:21 +0200462static const struct imx274_frmfmt imx274_formats[] = {
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400463 {
464 /* mode 1, 4K */
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400465 .bin_ratio = 1,
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400466 .init_regs = imx274_mode1_3840x2160_raw10,
467 .min_frame_len = 4550,
468 .min_SHR = 12,
469 .max_fps = 60,
470 .nocpiop = 112,
471 },
472 {
473 /* mode 3, 1080p */
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400474 .bin_ratio = 2,
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400475 .init_regs = imx274_mode3_1920x1080_raw10,
476 .min_frame_len = 2310,
477 .min_SHR = 8,
478 .max_fps = 120,
479 .nocpiop = 112,
480 },
481 {
482 /* mode 5, 720p */
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400483 .bin_ratio = 3,
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400484 .init_regs = imx274_mode5_1280x720_raw10,
485 .min_frame_len = 2310,
486 .min_SHR = 8,
487 .max_fps = 120,
488 .nocpiop = 112,
489 },
Leon Luo0985dd32017-10-05 02:06:21 +0200490};
491
492/*
493 * struct imx274_ctrls - imx274 ctrl structure
494 * @handler: V4L2 ctrl handler structure
495 * @exposure: Pointer to expsure ctrl structure
496 * @gain: Pointer to gain ctrl structure
497 * @vflip: Pointer to vflip ctrl structure
498 * @test_pattern: Pointer to test pattern ctrl structure
499 */
500struct imx274_ctrls {
501 struct v4l2_ctrl_handler handler;
502 struct v4l2_ctrl *exposure;
503 struct v4l2_ctrl *gain;
504 struct v4l2_ctrl *vflip;
505 struct v4l2_ctrl *test_pattern;
506};
507
508/*
509 * struct stim274 - imx274 device structure
510 * @sd: V4L2 subdevice structure
Luca Ceresoli19d38b22018-06-11 07:35:38 -0400511 * @pad: Media pad structure
Leon Luo0985dd32017-10-05 02:06:21 +0200512 * @client: Pointer to I2C client
513 * @ctrls: imx274 control structure
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400514 * @crop: rect to be captured
515 * @compose: compose rect, i.e. output resolution
Leon Luo0985dd32017-10-05 02:06:21 +0200516 * @format: V4L2 media bus frame format structure
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400517 * (width and height are in sync with the compose rect)
Leon Luo0985dd32017-10-05 02:06:21 +0200518 * @frame_rate: V4L2 frame rate structure
519 * @regmap: Pointer to regmap structure
520 * @reset_gpio: Pointer to reset gpio
521 * @lock: Mutex structure
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400522 * @mode: Parameters for the selected readout mode
Leon Luo0985dd32017-10-05 02:06:21 +0200523 */
524struct stimx274 {
525 struct v4l2_subdev sd;
526 struct media_pad pad;
527 struct i2c_client *client;
528 struct imx274_ctrls ctrls;
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400529 struct v4l2_rect crop;
Leon Luo0985dd32017-10-05 02:06:21 +0200530 struct v4l2_mbus_framefmt format;
531 struct v4l2_fract frame_interval;
532 struct regmap *regmap;
533 struct gpio_desc *reset_gpio;
534 struct mutex lock; /* mutex lock for operations */
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400535 const struct imx274_frmfmt *mode;
Leon Luo0985dd32017-10-05 02:06:21 +0200536};
537
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400538#define IMX274_ROUND(dim, step, flags) \
539 ((flags) & V4L2_SEL_FLAG_GE \
540 ? roundup((dim), (step)) \
541 : ((flags) & V4L2_SEL_FLAG_LE \
542 ? rounddown((dim), (step)) \
543 : rounddown((dim) + (step) / 2, (step))))
544
Leon Luo0985dd32017-10-05 02:06:21 +0200545/*
546 * Function declaration
547 */
548static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl);
549static int imx274_set_exposure(struct stimx274 *priv, int val);
550static int imx274_set_vflip(struct stimx274 *priv, int val);
551static int imx274_set_test_pattern(struct stimx274 *priv, int val);
552static int imx274_set_frame_interval(struct stimx274 *priv,
553 struct v4l2_fract frame_interval);
554
555static inline void msleep_range(unsigned int delay_base)
556{
557 usleep_range(delay_base * 1000, delay_base * 1000 + 500);
558}
559
560/*
561 * v4l2_ctrl and v4l2_subdev related operations
562 */
563static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
564{
565 return &container_of(ctrl->handler,
566 struct stimx274, ctrls.handler)->sd;
567}
568
569static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd)
570{
571 return container_of(sd, struct stimx274, sd);
572}
573
574/*
Luca Ceresoli7ff67862018-06-11 07:35:36 -0400575 * Writing a register table
576 *
577 * @priv: Pointer to device
578 * @table: Table containing register values (with optional delays)
Leon Luo0985dd32017-10-05 02:06:21 +0200579 *
580 * This is used to write register table into sensor's reg map.
581 *
582 * Return: 0 on success, errors otherwise
583 */
Luca Ceresoli7ff67862018-06-11 07:35:36 -0400584static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[])
Leon Luo0985dd32017-10-05 02:06:21 +0200585{
Luca Ceresoli7ff67862018-06-11 07:35:36 -0400586 struct regmap *regmap = priv->regmap;
Dan Carpenter021741a2017-12-05 09:37:39 -0500587 int err = 0;
Leon Luo0985dd32017-10-05 02:06:21 +0200588 const struct reg_8 *next;
589 u8 val;
590
591 int range_start = -1;
592 int range_count = 0;
593 u8 range_vals[16];
594 int max_range_vals = ARRAY_SIZE(range_vals);
595
596 for (next = table;; next++) {
597 if ((next->addr != range_start + range_count) ||
Luca Ceresoli7ff67862018-06-11 07:35:36 -0400598 (next->addr == IMX274_TABLE_END) ||
599 (next->addr == IMX274_TABLE_WAIT_MS) ||
Leon Luo0985dd32017-10-05 02:06:21 +0200600 (range_count == max_range_vals)) {
601 if (range_count == 1)
602 err = regmap_write(regmap,
603 range_start, range_vals[0]);
604 else if (range_count > 1)
605 err = regmap_bulk_write(regmap, range_start,
606 &range_vals[0],
607 range_count);
Mauro Carvalho Chehab00b4bac72017-11-01 17:05:57 -0400608 else
609 err = 0;
Leon Luo0985dd32017-10-05 02:06:21 +0200610
611 if (err)
612 return err;
613
614 range_start = -1;
615 range_count = 0;
616
617 /* Handle special address values */
Luca Ceresoli7ff67862018-06-11 07:35:36 -0400618 if (next->addr == IMX274_TABLE_END)
Leon Luo0985dd32017-10-05 02:06:21 +0200619 break;
620
Luca Ceresoli7ff67862018-06-11 07:35:36 -0400621 if (next->addr == IMX274_TABLE_WAIT_MS) {
Leon Luo0985dd32017-10-05 02:06:21 +0200622 msleep_range(next->val);
623 continue;
624 }
625 }
626
627 val = next->val;
628
629 if (range_start == -1)
630 range_start = next->addr;
631
632 range_vals[range_count++] = val;
633 }
634 return 0;
635}
636
637static inline int imx274_read_reg(struct stimx274 *priv, u16 addr, u8 *val)
638{
Luca Ceresoli257ad262018-11-26 11:35:07 -0500639 unsigned int uint_val;
Leon Luo0985dd32017-10-05 02:06:21 +0200640 int err;
641
Luca Ceresoli257ad262018-11-26 11:35:07 -0500642 err = regmap_read(priv->regmap, addr, &uint_val);
Leon Luo0985dd32017-10-05 02:06:21 +0200643 if (err)
644 dev_err(&priv->client->dev,
645 "%s : i2c read failed, addr = %x\n", __func__, addr);
646 else
647 dev_dbg(&priv->client->dev,
648 "%s : addr 0x%x, val=0x%x\n", __func__,
Luca Ceresoli257ad262018-11-26 11:35:07 -0500649 addr, uint_val);
650
651 *val = uint_val;
Leon Luo0985dd32017-10-05 02:06:21 +0200652 return err;
653}
654
655static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val)
656{
657 int err;
658
659 err = regmap_write(priv->regmap, addr, val);
660 if (err)
661 dev_err(&priv->client->dev,
662 "%s : i2c write failed, %x = %x\n", __func__,
663 addr, val);
664 else
665 dev_dbg(&priv->client->dev,
666 "%s : addr 0x%x, val=0x%x\n", __func__,
667 addr, val);
668 return err;
669}
670
Luca Ceresoli279b4b9a2018-07-25 12:24:54 -0400671/**
672 * Write a multibyte register.
673 *
674 * Uses a bulk write where possible.
675 *
676 * @priv: Pointer to device structure
677 * @addr: Address of the LSB register. Other registers must be
678 * consecutive, least-to-most significant.
679 * @val: Value to be written to the register (cpu endianness)
680 * @nbytes: Number of bits to write (range: [1..3])
681 */
682static int imx274_write_mbreg(struct stimx274 *priv, u16 addr, u32 val,
683 size_t nbytes)
684{
685 __le32 val_le = cpu_to_le32(val);
686 int err;
687
688 err = regmap_bulk_write(priv->regmap, addr, &val_le, nbytes);
689 if (err)
690 dev_err(&priv->client->dev,
691 "%s : i2c bulk write failed, %x = %x (%zu bytes)\n",
692 __func__, addr, val, nbytes);
693 else
694 dev_dbg(&priv->client->dev,
695 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
696 __func__, addr, val, nbytes);
697 return err;
698}
699
Leon Luo0985dd32017-10-05 02:06:21 +0200700/*
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400701 * Set mode registers to start stream.
Leon Luo0985dd32017-10-05 02:06:21 +0200702 * @priv: Pointer to device structure
Leon Luo0985dd32017-10-05 02:06:21 +0200703 *
704 * Return: 0 on success, errors otherwise
705 */
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400706static int imx274_mode_regs(struct stimx274 *priv)
Leon Luo0985dd32017-10-05 02:06:21 +0200707{
708 int err = 0;
709
Luca Ceresoli8ed8bba2018-04-24 04:24:11 -0400710 err = imx274_write_table(priv, imx274_start_1);
Leon Luo0985dd32017-10-05 02:06:21 +0200711 if (err)
712 return err;
713
Luca Ceresoli8ed8bba2018-04-24 04:24:11 -0400714 err = imx274_write_table(priv, imx274_start_2);
Leon Luo0985dd32017-10-05 02:06:21 +0200715 if (err)
716 return err;
717
Luca Ceresoli96a2c732018-06-11 07:35:33 -0400718 err = imx274_write_table(priv, priv->mode->init_regs);
Leon Luo0985dd32017-10-05 02:06:21 +0200719
720 return err;
721}
722
723/*
724 * imx274_start_stream - Function for starting stream per mode index
725 * @priv: Pointer to device structure
726 *
727 * Return: 0 on success, errors otherwise
728 */
729static int imx274_start_stream(struct stimx274 *priv)
730{
731 int err = 0;
732
733 /*
734 * Refer to "Standby Cancel Sequence when using CSI-2" in
735 * imx274 datasheet, it should wait 10ms or more here.
736 * give it 1 extra ms for margin
737 */
738 msleep_range(11);
Luca Ceresoli8ed8bba2018-04-24 04:24:11 -0400739 err = imx274_write_table(priv, imx274_start_3);
Leon Luo0985dd32017-10-05 02:06:21 +0200740 if (err)
741 return err;
742
743 /*
744 * Refer to "Standby Cancel Sequence when using CSI-2" in
745 * imx274 datasheet, it should wait 7ms or more here.
746 * give it 1 extra ms for margin
747 */
748 msleep_range(8);
Luca Ceresoli8ed8bba2018-04-24 04:24:11 -0400749 err = imx274_write_table(priv, imx274_start_4);
Leon Luo0985dd32017-10-05 02:06:21 +0200750 if (err)
751 return err;
752
753 return 0;
754}
755
756/*
757 * imx274_reset - Function called to reset the sensor
758 * @priv: Pointer to device structure
759 * @rst: Input value for determining the sensor's end state after reset
760 *
761 * Set the senor in reset and then
762 * if rst = 0, keep it in reset;
763 * if rst = 1, bring it out of reset.
764 *
765 */
766static void imx274_reset(struct stimx274 *priv, int rst)
767{
768 gpiod_set_value_cansleep(priv->reset_gpio, 0);
769 usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
770 gpiod_set_value_cansleep(priv->reset_gpio, !!rst);
771 usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
772}
773
774/**
775 * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
776 * @ctrl: V4L2 control to be set
777 *
778 * This function is used to set the V4L2 controls for the imx274 sensor.
779 *
780 * Return: 0 on success, errors otherwise
781 */
782static int imx274_s_ctrl(struct v4l2_ctrl *ctrl)
783{
784 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
785 struct stimx274 *imx274 = to_imx274(sd);
786 int ret = -EINVAL;
787
788 dev_dbg(&imx274->client->dev,
789 "%s : s_ctrl: %s, value: %d\n", __func__,
790 ctrl->name, ctrl->val);
791
792 switch (ctrl->id) {
793 case V4L2_CID_EXPOSURE:
794 dev_dbg(&imx274->client->dev,
795 "%s : set V4L2_CID_EXPOSURE\n", __func__);
796 ret = imx274_set_exposure(imx274, ctrl->val);
797 break;
798
799 case V4L2_CID_GAIN:
800 dev_dbg(&imx274->client->dev,
801 "%s : set V4L2_CID_GAIN\n", __func__);
802 ret = imx274_set_gain(imx274, ctrl);
803 break;
804
805 case V4L2_CID_VFLIP:
806 dev_dbg(&imx274->client->dev,
807 "%s : set V4L2_CID_VFLIP\n", __func__);
808 ret = imx274_set_vflip(imx274, ctrl->val);
809 break;
810
811 case V4L2_CID_TEST_PATTERN:
812 dev_dbg(&imx274->client->dev,
813 "%s : set V4L2_CID_TEST_PATTERN\n", __func__);
814 ret = imx274_set_test_pattern(imx274, ctrl->val);
815 break;
816 }
817
818 return ret;
819}
820
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400821static int imx274_binning_goodness(struct stimx274 *imx274,
822 int w, int ask_w,
823 int h, int ask_h, u32 flags)
824{
825 struct device *dev = &imx274->client->dev;
826 const int goodness = 100000;
827 int val = 0;
828
829 if (flags & V4L2_SEL_FLAG_GE) {
830 if (w < ask_w)
831 val -= goodness;
832 if (h < ask_h)
833 val -= goodness;
834 }
835
836 if (flags & V4L2_SEL_FLAG_LE) {
837 if (w > ask_w)
838 val -= goodness;
839 if (h > ask_h)
840 val -= goodness;
841 }
842
843 val -= abs(w - ask_w);
844 val -= abs(h - ask_h);
845
846 dev_dbg(dev, "%s: ask %dx%d, size %dx%d, goodness %d\n",
847 __func__, ask_w, ask_h, w, h, val);
848
849 return val;
850}
851
852/**
853 * Helper function to change binning and set both compose and format.
854 *
855 * We have two entry points to change binning: set_fmt and
856 * set_selection(COMPOSE). Both have to compute the new output size
857 * and set it in both the compose rect and the frame format size. We
858 * also need to do the same things after setting cropping to restore
859 * 1:1 binning.
860 *
861 * This function contains the common code for these three cases, it
862 * has many arguments in order to accommodate the needs of all of
863 * them.
864 *
865 * Must be called with imx274->lock locked.
866 *
867 * @imx274: The device object
868 * @cfg: The pad config we are editing for TRY requests
869 * @which: V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller
870 * @width: Input-output parameter: set to the desired width before
871 * the call, contains the chosen value after returning successfully
872 * @height: Input-output parameter for height (see @width)
873 * @flags: Selection flags from struct v4l2_subdev_selection, or 0 if not
874 * available (when called from set_fmt)
875 */
876static int __imx274_change_compose(struct stimx274 *imx274,
877 struct v4l2_subdev_pad_config *cfg,
878 u32 which,
879 u32 *width,
880 u32 *height,
881 u32 flags)
882{
883 struct device *dev = &imx274->client->dev;
884 const struct v4l2_rect *cur_crop;
885 struct v4l2_mbus_framefmt *tgt_fmt;
886 unsigned int i;
887 const struct imx274_frmfmt *best_mode = &imx274_formats[0];
888 int best_goodness = INT_MIN;
889
890 if (which == V4L2_SUBDEV_FORMAT_TRY) {
891 cur_crop = &cfg->try_crop;
892 tgt_fmt = &cfg->try_fmt;
893 } else {
894 cur_crop = &imx274->crop;
895 tgt_fmt = &imx274->format;
896 }
897
898 for (i = 0; i < ARRAY_SIZE(imx274_formats); i++) {
899 unsigned int ratio = imx274_formats[i].bin_ratio;
900
901 int goodness = imx274_binning_goodness(
902 imx274,
903 cur_crop->width / ratio, *width,
904 cur_crop->height / ratio, *height,
905 flags);
906
907 if (goodness >= best_goodness) {
908 best_goodness = goodness;
909 best_mode = &imx274_formats[i];
910 }
911 }
912
913 *width = cur_crop->width / best_mode->bin_ratio;
914 *height = cur_crop->height / best_mode->bin_ratio;
915
916 if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
917 imx274->mode = best_mode;
918
919 dev_dbg(dev, "%s: selected %u:1 binning\n",
920 __func__, best_mode->bin_ratio);
921
922 tgt_fmt->width = *width;
923 tgt_fmt->height = *height;
924 tgt_fmt->field = V4L2_FIELD_NONE;
925
926 return 0;
927}
928
Leon Luo0985dd32017-10-05 02:06:21 +0200929/**
930 * imx274_get_fmt - Get the pad format
931 * @sd: Pointer to V4L2 Sub device structure
932 * @cfg: Pointer to sub device pad information structure
933 * @fmt: Pointer to pad level media bus format
934 *
935 * This function is used to get the pad format information.
936 *
937 * Return: 0 on success
938 */
939static int imx274_get_fmt(struct v4l2_subdev *sd,
940 struct v4l2_subdev_pad_config *cfg,
941 struct v4l2_subdev_format *fmt)
942{
943 struct stimx274 *imx274 = to_imx274(sd);
944
945 mutex_lock(&imx274->lock);
946 fmt->format = imx274->format;
947 mutex_unlock(&imx274->lock);
948 return 0;
949}
950
951/**
952 * imx274_set_fmt - This is used to set the pad format
953 * @sd: Pointer to V4L2 Sub device structure
954 * @cfg: Pointer to sub device pad information structure
955 * @format: Pointer to pad level media bus format
956 *
957 * This function is used to set the pad format.
958 *
959 * Return: 0 on success
960 */
961static int imx274_set_fmt(struct v4l2_subdev *sd,
962 struct v4l2_subdev_pad_config *cfg,
963 struct v4l2_subdev_format *format)
964{
965 struct v4l2_mbus_framefmt *fmt = &format->format;
966 struct stimx274 *imx274 = to_imx274(sd);
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400967 int err = 0;
Leon Luo0985dd32017-10-05 02:06:21 +0200968
969 mutex_lock(&imx274->lock);
970
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400971 err = __imx274_change_compose(imx274, cfg, format->which,
972 &fmt->width, &fmt->height, 0);
Leon Luo0985dd32017-10-05 02:06:21 +0200973
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400974 if (err)
975 goto out;
Leon Luo0985dd32017-10-05 02:06:21 +0200976
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400977 /*
978 * __imx274_change_compose already set width and height in the
979 * applicable format, but we need to keep all other format
980 * values, so do a full copy here
981 */
Leon Luo0985dd32017-10-05 02:06:21 +0200982 fmt->field = V4L2_FIELD_NONE;
Leon Luo0985dd32017-10-05 02:06:21 +0200983 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
984 cfg->try_fmt = *fmt;
985 else
986 imx274->format = *fmt;
987
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400988out:
Leon Luo0985dd32017-10-05 02:06:21 +0200989 mutex_unlock(&imx274->lock);
Luca Ceresoli39dd23d2018-07-25 12:24:55 -0400990
991 return err;
992}
993
994static int imx274_get_selection(struct v4l2_subdev *sd,
995 struct v4l2_subdev_pad_config *cfg,
996 struct v4l2_subdev_selection *sel)
997{
998 struct stimx274 *imx274 = to_imx274(sd);
999 const struct v4l2_rect *src_crop;
1000 const struct v4l2_mbus_framefmt *src_fmt;
1001 int ret = 0;
1002
1003 if (sel->pad != 0)
1004 return -EINVAL;
1005
1006 if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
1007 sel->r.left = 0;
1008 sel->r.top = 0;
1009 sel->r.width = IMX274_MAX_WIDTH;
1010 sel->r.height = IMX274_MAX_HEIGHT;
1011 return 0;
1012 }
1013
1014 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1015 src_crop = &cfg->try_crop;
1016 src_fmt = &cfg->try_fmt;
1017 } else {
1018 src_crop = &imx274->crop;
1019 src_fmt = &imx274->format;
1020 }
1021
1022 mutex_lock(&imx274->lock);
1023
1024 switch (sel->target) {
1025 case V4L2_SEL_TGT_CROP:
1026 sel->r = *src_crop;
1027 break;
1028 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1029 sel->r.top = 0;
1030 sel->r.left = 0;
1031 sel->r.width = src_crop->width;
1032 sel->r.height = src_crop->height;
1033 break;
1034 case V4L2_SEL_TGT_COMPOSE:
1035 sel->r.top = 0;
1036 sel->r.left = 0;
1037 sel->r.width = src_fmt->width;
1038 sel->r.height = src_fmt->height;
1039 break;
1040 default:
1041 ret = -EINVAL;
1042 }
1043
1044 mutex_unlock(&imx274->lock);
1045
1046 return ret;
1047}
1048
1049static int imx274_set_selection_crop(struct stimx274 *imx274,
1050 struct v4l2_subdev_pad_config *cfg,
1051 struct v4l2_subdev_selection *sel)
1052{
1053 struct v4l2_rect *tgt_crop;
1054 struct v4l2_rect new_crop;
1055 bool size_changed;
1056
1057 /*
1058 * h_step could be 12 or 24 depending on the binning. But we
1059 * won't know the binning until we choose the mode later in
1060 * __imx274_change_compose(). Thus let's be safe and use the
1061 * most conservative value in all cases.
1062 */
1063 const u32 h_step = 24;
1064
1065 new_crop.width = min_t(u32,
1066 IMX274_ROUND(sel->r.width, h_step, sel->flags),
1067 IMX274_MAX_WIDTH);
1068
1069 /* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */
1070 if (new_crop.width < 144)
1071 new_crop.width = 144;
1072
1073 new_crop.left = min_t(u32,
1074 IMX274_ROUND(sel->r.left, h_step, 0),
1075 IMX274_MAX_WIDTH - new_crop.width);
1076
1077 new_crop.height = min_t(u32,
1078 IMX274_ROUND(sel->r.height, 2, sel->flags),
1079 IMX274_MAX_HEIGHT);
1080
1081 new_crop.top = min_t(u32, IMX274_ROUND(sel->r.top, 2, 0),
1082 IMX274_MAX_HEIGHT - new_crop.height);
1083
1084 sel->r = new_crop;
1085
1086 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1087 tgt_crop = &cfg->try_crop;
1088 else
1089 tgt_crop = &imx274->crop;
1090
1091 mutex_lock(&imx274->lock);
1092
1093 size_changed = (new_crop.width != tgt_crop->width ||
1094 new_crop.height != tgt_crop->height);
1095
1096 /* __imx274_change_compose needs the new size in *tgt_crop */
1097 *tgt_crop = new_crop;
1098
1099 /* if crop size changed then reset the output image size */
1100 if (size_changed)
1101 __imx274_change_compose(imx274, cfg, sel->which,
1102 &new_crop.width, &new_crop.height,
1103 sel->flags);
1104
1105 mutex_unlock(&imx274->lock);
1106
Leon Luo0985dd32017-10-05 02:06:21 +02001107 return 0;
1108}
1109
Luca Ceresoli39dd23d2018-07-25 12:24:55 -04001110static int imx274_set_selection(struct v4l2_subdev *sd,
1111 struct v4l2_subdev_pad_config *cfg,
1112 struct v4l2_subdev_selection *sel)
1113{
1114 struct stimx274 *imx274 = to_imx274(sd);
1115
1116 if (sel->pad != 0)
1117 return -EINVAL;
1118
1119 if (sel->target == V4L2_SEL_TGT_CROP)
1120 return imx274_set_selection_crop(imx274, cfg, sel);
1121
1122 if (sel->target == V4L2_SEL_TGT_COMPOSE) {
1123 int err;
1124
1125 mutex_lock(&imx274->lock);
1126 err = __imx274_change_compose(imx274, cfg, sel->which,
1127 &sel->r.width, &sel->r.height,
1128 sel->flags);
1129 mutex_unlock(&imx274->lock);
1130
1131 /*
1132 * __imx274_change_compose already set width and
1133 * height in set->r, we still need to set top-left
1134 */
1135 if (!err) {
1136 sel->r.top = 0;
1137 sel->r.left = 0;
1138 }
1139
1140 return err;
1141 }
1142
1143 return -EINVAL;
1144}
1145
1146static int imx274_apply_trimming(struct stimx274 *imx274)
1147{
1148 u32 h_start;
1149 u32 h_end;
1150 u32 hmax;
1151 u32 v_cut;
1152 s32 v_pos;
1153 u32 write_v_size;
1154 u32 y_out_size;
1155 int err;
1156
1157 h_start = imx274->crop.left + 12;
1158 h_end = h_start + imx274->crop.width;
1159
1160 /* Use the minimum allowed value of HMAX */
1161 /* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */
1162 /* Note: 260 is the minimum HMAX in all implemented modes */
1163 hmax = max_t(u32, 260, (imx274->crop.width) / 16 + 23);
1164
1165 /* invert v_pos if VFLIP */
1166 v_pos = imx274->ctrls.vflip->cur.val ?
1167 (-imx274->crop.top / 2) : (imx274->crop.top / 2);
1168 v_cut = (IMX274_MAX_HEIGHT - imx274->crop.height) / 2;
1169 write_v_size = imx274->crop.height + 22;
1170 y_out_size = imx274->crop.height + 14;
1171
1172 err = imx274_write_mbreg(imx274, IMX274_HMAX_REG_LSB, hmax, 2);
1173 if (!err)
1174 err = imx274_write_mbreg(imx274, IMX274_HTRIM_EN_REG, 1, 1);
1175 if (!err)
1176 err = imx274_write_mbreg(imx274, IMX274_HTRIM_START_REG_LSB,
1177 h_start, 2);
1178 if (!err)
1179 err = imx274_write_mbreg(imx274, IMX274_HTRIM_END_REG_LSB,
1180 h_end, 2);
1181 if (!err)
1182 err = imx274_write_mbreg(imx274, IMX274_VWIDCUTEN_REG, 1, 1);
1183 if (!err)
1184 err = imx274_write_mbreg(imx274, IMX274_VWIDCUT_REG_LSB,
1185 v_cut, 2);
1186 if (!err)
1187 err = imx274_write_mbreg(imx274, IMX274_VWINPOS_REG_LSB,
1188 v_pos, 2);
1189 if (!err)
1190 err = imx274_write_mbreg(imx274, IMX274_WRITE_VSIZE_REG_LSB,
1191 write_v_size, 2);
1192 if (!err)
1193 err = imx274_write_mbreg(imx274, IMX274_Y_OUT_SIZE_REG_LSB,
1194 y_out_size, 2);
1195
1196 return err;
1197}
1198
Leon Luo0985dd32017-10-05 02:06:21 +02001199/**
1200 * imx274_g_frame_interval - Get the frame interval
1201 * @sd: Pointer to V4L2 Sub device structure
1202 * @fi: Pointer to V4l2 Sub device frame interval structure
1203 *
1204 * This function is used to get the frame interval.
1205 *
1206 * Return: 0 on success
1207 */
1208static int imx274_g_frame_interval(struct v4l2_subdev *sd,
1209 struct v4l2_subdev_frame_interval *fi)
1210{
1211 struct stimx274 *imx274 = to_imx274(sd);
1212
1213 fi->interval = imx274->frame_interval;
1214 dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n",
1215 __func__, imx274->frame_interval.numerator,
1216 imx274->frame_interval.denominator);
1217
1218 return 0;
1219}
1220
1221/**
1222 * imx274_s_frame_interval - Set the frame interval
1223 * @sd: Pointer to V4L2 Sub device structure
1224 * @fi: Pointer to V4l2 Sub device frame interval structure
1225 *
1226 * This function is used to set the frame intervavl.
1227 *
1228 * Return: 0 on success
1229 */
1230static int imx274_s_frame_interval(struct v4l2_subdev *sd,
1231 struct v4l2_subdev_frame_interval *fi)
1232{
1233 struct stimx274 *imx274 = to_imx274(sd);
1234 struct v4l2_ctrl *ctrl = imx274->ctrls.exposure;
1235 int min, max, def;
1236 int ret;
1237
1238 mutex_lock(&imx274->lock);
1239 ret = imx274_set_frame_interval(imx274, fi->interval);
1240
1241 if (!ret) {
Hans Verkuil4550a7f2020-07-03 11:20:32 +02001242 fi->interval = imx274->frame_interval;
1243
Leon Luo0985dd32017-10-05 02:06:21 +02001244 /*
1245 * exposure time range is decided by frame interval
Luca Ceresoli01343392018-04-24 04:24:07 -04001246 * need to update it after frame interval changes
Leon Luo0985dd32017-10-05 02:06:21 +02001247 */
1248 min = IMX274_MIN_EXPOSURE_TIME;
1249 max = fi->interval.numerator * 1000000
1250 / fi->interval.denominator;
1251 def = max;
1252 if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def)) {
1253 dev_err(&imx274->client->dev,
1254 "Exposure ctrl range update failed\n");
1255 goto unlock;
1256 }
1257
1258 /* update exposure time accordingly */
Luca Ceresolicf908702018-04-24 04:24:08 -04001259 imx274_set_exposure(imx274, ctrl->val);
Leon Luo0985dd32017-10-05 02:06:21 +02001260
1261 dev_dbg(&imx274->client->dev, "set frame interval to %uus\n",
1262 fi->interval.numerator * 1000000
1263 / fi->interval.denominator);
1264 }
1265
1266unlock:
1267 mutex_unlock(&imx274->lock);
1268
1269 return ret;
1270}
1271
1272/**
1273 * imx274_load_default - load default control values
1274 * @priv: Pointer to device structure
1275 *
1276 * Return: 0 on success, errors otherwise
1277 */
1278static int imx274_load_default(struct stimx274 *priv)
1279{
1280 int ret;
1281
1282 /* load default control values */
1283 priv->frame_interval.numerator = 1;
1284 priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1285 priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE;
1286 priv->ctrls.gain->val = IMX274_DEF_GAIN;
1287 priv->ctrls.vflip->val = 0;
1288 priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED;
1289
1290 /* update frame rate */
1291 ret = imx274_set_frame_interval(priv,
1292 priv->frame_interval);
1293 if (ret)
1294 return ret;
1295
1296 /* update exposure time */
1297 ret = v4l2_ctrl_s_ctrl(priv->ctrls.exposure, priv->ctrls.exposure->val);
1298 if (ret)
1299 return ret;
1300
1301 /* update gain */
1302 ret = v4l2_ctrl_s_ctrl(priv->ctrls.gain, priv->ctrls.gain->val);
1303 if (ret)
1304 return ret;
1305
1306 /* update vflip */
1307 ret = v4l2_ctrl_s_ctrl(priv->ctrls.vflip, priv->ctrls.vflip->val);
1308 if (ret)
1309 return ret;
1310
1311 return 0;
1312}
1313
1314/**
1315 * imx274_s_stream - It is used to start/stop the streaming.
1316 * @sd: V4L2 Sub device
1317 * @on: Flag (True / False)
1318 *
1319 * This function controls the start or stop of streaming for the
1320 * imx274 sensor.
1321 *
1322 * Return: 0 on success, errors otherwise
1323 */
1324static int imx274_s_stream(struct v4l2_subdev *sd, int on)
1325{
1326 struct stimx274 *imx274 = to_imx274(sd);
1327 int ret = 0;
1328
Luca Ceresoli43173222018-06-11 07:35:34 -04001329 dev_dbg(&imx274->client->dev, "%s : %s, mode index = %td\n", __func__,
1330 on ? "Stream Start" : "Stream Stop",
1331 imx274->mode - &imx274_formats[0]);
Leon Luo0985dd32017-10-05 02:06:21 +02001332
1333 mutex_lock(&imx274->lock);
1334
1335 if (on) {
1336 /* load mode registers */
Luca Ceresoli96a2c732018-06-11 07:35:33 -04001337 ret = imx274_mode_regs(imx274);
Leon Luo0985dd32017-10-05 02:06:21 +02001338 if (ret)
1339 goto fail;
1340
Luca Ceresoli39dd23d2018-07-25 12:24:55 -04001341 ret = imx274_apply_trimming(imx274);
1342 if (ret)
1343 goto fail;
1344
Leon Luo0985dd32017-10-05 02:06:21 +02001345 /*
1346 * update frame rate & expsoure. if the last mode is different,
1347 * HMAX could be changed. As the result, frame rate & exposure
1348 * are changed.
1349 * gain is not affected.
1350 */
1351 ret = imx274_set_frame_interval(imx274,
1352 imx274->frame_interval);
1353 if (ret)
1354 goto fail;
1355
1356 /* update exposure time */
1357 ret = __v4l2_ctrl_s_ctrl(imx274->ctrls.exposure,
1358 imx274->ctrls.exposure->val);
1359 if (ret)
1360 goto fail;
1361
1362 /* start stream */
1363 ret = imx274_start_stream(imx274);
1364 if (ret)
1365 goto fail;
1366 } else {
1367 /* stop stream */
Luca Ceresoli8ed8bba2018-04-24 04:24:11 -04001368 ret = imx274_write_table(imx274, imx274_stop);
Leon Luo0985dd32017-10-05 02:06:21 +02001369 if (ret)
1370 goto fail;
1371 }
1372
1373 mutex_unlock(&imx274->lock);
Luca Ceresoli43173222018-06-11 07:35:34 -04001374 dev_dbg(&imx274->client->dev, "%s : Done\n", __func__);
Leon Luo0985dd32017-10-05 02:06:21 +02001375 return 0;
1376
1377fail:
1378 mutex_unlock(&imx274->lock);
1379 dev_err(&imx274->client->dev, "s_stream failed\n");
1380 return ret;
1381}
1382
1383/*
1384 * imx274_get_frame_length - Function for obtaining current frame length
1385 * @priv: Pointer to device structure
1386 * @val: Pointer to obainted value
1387 *
1388 * frame_length = vmax x (svr + 1), in unit of hmax.
1389 *
1390 * Return: 0 on success
1391 */
1392static int imx274_get_frame_length(struct stimx274 *priv, u32 *val)
1393{
1394 int err;
1395 u16 svr;
1396 u32 vmax;
1397 u8 reg_val[3];
1398
1399 /* svr */
1400 err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, &reg_val[0]);
1401 if (err)
1402 goto fail;
1403
1404 err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, &reg_val[1]);
1405 if (err)
1406 goto fail;
1407
1408 svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
1409
1410 /* vmax */
Luca Ceresoli4eb78462018-04-24 04:24:10 -04001411 err = imx274_read_reg(priv, IMX274_VMAX_REG_3, &reg_val[0]);
Leon Luo0985dd32017-10-05 02:06:21 +02001412 if (err)
1413 goto fail;
1414
Luca Ceresoli4eb78462018-04-24 04:24:10 -04001415 err = imx274_read_reg(priv, IMX274_VMAX_REG_2, &reg_val[1]);
Leon Luo0985dd32017-10-05 02:06:21 +02001416 if (err)
1417 goto fail;
1418
Luca Ceresoli4eb78462018-04-24 04:24:10 -04001419 err = imx274_read_reg(priv, IMX274_VMAX_REG_1, &reg_val[2]);
Leon Luo0985dd32017-10-05 02:06:21 +02001420 if (err)
1421 goto fail;
1422
1423 vmax = ((reg_val[2] & IMX274_MASK_LSB_3_BITS) << IMX274_SHIFT_16_BITS)
1424 + (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
1425
1426 *val = vmax * (svr + 1);
1427
1428 return 0;
1429
1430fail:
1431 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1432 return err;
1433}
1434
1435static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val,
1436 u32 *frame_length)
1437{
1438 int err;
1439
1440 err = imx274_get_frame_length(priv, frame_length);
1441 if (err)
1442 return err;
1443
Luca Ceresoli96a2c732018-06-11 07:35:33 -04001444 if (*frame_length < priv->mode->min_frame_len)
1445 *frame_length = priv->mode->min_frame_len;
Leon Luo0985dd32017-10-05 02:06:21 +02001446
1447 *val = *frame_length - *val; /* convert to raw shr */
1448 if (*val > *frame_length - IMX274_SHR_LIMIT_CONST)
1449 *val = *frame_length - IMX274_SHR_LIMIT_CONST;
Luca Ceresoli96a2c732018-06-11 07:35:33 -04001450 else if (*val < priv->mode->min_SHR)
1451 *val = priv->mode->min_SHR;
Leon Luo0985dd32017-10-05 02:06:21 +02001452
1453 return 0;
1454}
1455
1456/*
1457 * imx274_set_digital gain - Function called when setting digital gain
1458 * @priv: Pointer to device structure
1459 * @dgain: Value of digital gain.
1460 *
1461 * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
1462 *
1463 * Return: 0 on success
1464 */
1465static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain)
1466{
1467 u8 reg_val;
1468
1469 reg_val = ffs(dgain);
1470
1471 if (reg_val)
1472 reg_val--;
1473
1474 reg_val = clamp(reg_val, (u8)0, (u8)3);
1475
1476 return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG,
1477 reg_val & IMX274_MASK_LSB_4_BITS);
1478}
1479
Leon Luo0985dd32017-10-05 02:06:21 +02001480/*
1481 * imx274_set_gain - Function called when setting gain
1482 * @priv: Pointer to device structure
1483 * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
1484 * @ctrl: v4l2 control pointer
1485 *
1486 * Set the gain based on input value.
1487 * The caller should hold the mutex lock imx274->lock if necessary
1488 *
1489 * Return: 0 on success
1490 */
1491static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl)
1492{
Leon Luo0985dd32017-10-05 02:06:21 +02001493 int err;
1494 u32 gain, analog_gain, digital_gain, gain_reg;
Leon Luo0985dd32017-10-05 02:06:21 +02001495
1496 gain = (u32)(ctrl->val);
1497
1498 dev_dbg(&priv->client->dev,
1499 "%s : input gain = %d.%d\n", __func__,
1500 gain >> IMX274_GAIN_SHIFT,
1501 ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT);
1502
1503 if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN)
1504 gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN;
1505 else if (gain < IMX274_MIN_GAIN)
1506 gain = IMX274_MIN_GAIN;
1507
1508 if (gain <= IMX274_MAX_ANALOG_GAIN)
1509 digital_gain = 1;
1510 else if (gain <= IMX274_MAX_ANALOG_GAIN * 2)
1511 digital_gain = 2;
1512 else if (gain <= IMX274_MAX_ANALOG_GAIN * 4)
1513 digital_gain = 4;
1514 else
1515 digital_gain = IMX274_MAX_DIGITAL_GAIN;
1516
1517 analog_gain = gain / digital_gain;
1518
1519 dev_dbg(&priv->client->dev,
1520 "%s : digital gain = %d, analog gain = %d.%d\n",
1521 __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT,
1522 ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100)
1523 >> IMX274_GAIN_SHIFT);
1524
1525 err = imx274_set_digital_gain(priv, digital_gain);
1526 if (err)
1527 goto fail;
1528
1529 /* convert to register value, refer to imx274 datasheet */
1530 gain_reg = (u32)IMX274_GAIN_CONST -
1531 (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain;
1532 if (gain_reg > IMX274_GAIN_REG_MAX)
1533 gain_reg = IMX274_GAIN_REG_MAX;
1534
Luca Ceresoli279b4b9a2018-07-25 12:24:54 -04001535 err = imx274_write_mbreg(priv, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg,
1536 2);
1537 if (err)
1538 goto fail;
Leon Luo0985dd32017-10-05 02:06:21 +02001539
1540 if (IMX274_GAIN_CONST - gain_reg == 0) {
1541 err = -EINVAL;
1542 goto fail;
1543 }
1544
1545 /* convert register value back to gain value */
1546 ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT)
1547 / (IMX274_GAIN_CONST - gain_reg) * digital_gain;
1548
1549 dev_dbg(&priv->client->dev,
1550 "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
1551 __func__, gain_reg, ctrl->val);
1552
1553 return 0;
1554
1555fail:
1556 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1557 return err;
1558}
1559
Leon Luo0985dd32017-10-05 02:06:21 +02001560/*
1561 * imx274_set_coarse_time - Function called when setting SHR value
1562 * @priv: Pointer to device structure
1563 * @val: Value for exposure time in number of line_length, or [HMAX]
1564 *
1565 * Set SHR value based on input value.
1566 *
1567 * Return: 0 on success
1568 */
1569static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val)
1570{
Leon Luo0985dd32017-10-05 02:06:21 +02001571 int err;
1572 u32 coarse_time, frame_length;
Leon Luo0985dd32017-10-05 02:06:21 +02001573
1574 coarse_time = *val;
1575
1576 /* convert exposure_time to appropriate SHR value */
1577 err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length);
1578 if (err)
1579 goto fail;
1580
Luca Ceresoli279b4b9a2018-07-25 12:24:54 -04001581 err = imx274_write_mbreg(priv, IMX274_SHR_REG_LSB, coarse_time, 2);
1582 if (err)
1583 goto fail;
Leon Luo0985dd32017-10-05 02:06:21 +02001584
1585 *val = frame_length - coarse_time;
1586 return 0;
1587
1588fail:
1589 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1590 return err;
1591}
1592
1593/*
1594 * imx274_set_exposure - Function called when setting exposure time
1595 * @priv: Pointer to device structure
1596 * @val: Variable for exposure time, in the unit of micro-second
1597 *
1598 * Set exposure time based on input value.
1599 * The caller should hold the mutex lock imx274->lock if necessary
1600 *
1601 * Return: 0 on success
1602 */
1603static int imx274_set_exposure(struct stimx274 *priv, int val)
1604{
1605 int err;
1606 u16 hmax;
1607 u8 reg_val[2];
1608 u32 coarse_time; /* exposure time in unit of line (HMAX)*/
1609
1610 dev_dbg(&priv->client->dev,
1611 "%s : EXPOSURE control input = %d\n", __func__, val);
1612
1613 /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
1614
1615 /* obtain HMAX value */
1616 err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, &reg_val[0]);
1617 if (err)
1618 goto fail;
1619 err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, &reg_val[1]);
1620 if (err)
1621 goto fail;
1622 hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
1623 if (hmax == 0) {
1624 err = -EINVAL;
1625 goto fail;
1626 }
1627
1628 coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val
Luca Ceresoli96a2c732018-06-11 07:35:33 -04001629 - priv->mode->nocpiop) / hmax;
Leon Luo0985dd32017-10-05 02:06:21 +02001630
1631 /* step 2: convert exposure_time into SHR value */
1632
1633 /* set SHR */
1634 err = imx274_set_coarse_time(priv, &coarse_time);
1635 if (err)
1636 goto fail;
1637
1638 priv->ctrls.exposure->val =
Luca Ceresoli96a2c732018-06-11 07:35:33 -04001639 (coarse_time * hmax + priv->mode->nocpiop)
Leon Luo0985dd32017-10-05 02:06:21 +02001640 / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2);
1641
1642 dev_dbg(&priv->client->dev,
1643 "%s : EXPOSURE control success\n", __func__);
1644 return 0;
1645
1646fail:
1647 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1648
1649 return err;
1650}
1651
1652/*
1653 * imx274_set_vflip - Function called when setting vertical flip
1654 * @priv: Pointer to device structure
1655 * @val: Value for vflip setting
1656 *
1657 * Set vertical flip based on input value.
1658 * val = 0: normal, no vertical flip
1659 * val = 1: vertical flip enabled
1660 * The caller should hold the mutex lock imx274->lock if necessary
1661 *
1662 * Return: 0 on success
1663 */
1664static int imx274_set_vflip(struct stimx274 *priv, int val)
1665{
1666 int err;
1667
1668 err = imx274_write_reg(priv, IMX274_VFLIP_REG, val);
1669 if (err) {
Luca Ceresoli9f67a5e2018-02-23 03:57:15 -05001670 dev_err(&priv->client->dev, "VFLIP control error\n");
Leon Luo0985dd32017-10-05 02:06:21 +02001671 return err;
1672 }
1673
1674 dev_dbg(&priv->client->dev,
1675 "%s : VFLIP control success\n", __func__);
1676
1677 return 0;
1678}
1679
1680/*
1681 * imx274_set_test_pattern - Function called when setting test pattern
1682 * @priv: Pointer to device structure
1683 * @val: Variable for test pattern
1684 *
1685 * Set to different test patterns based on input value.
1686 *
1687 * Return: 0 on success
1688 */
1689static int imx274_set_test_pattern(struct stimx274 *priv, int val)
1690{
1691 int err = 0;
1692
1693 if (val == TEST_PATTERN_DISABLED) {
1694 err = imx274_write_table(priv, imx274_tp_disabled);
1695 } else if (val <= TEST_PATTERN_V_COLOR_BARS) {
1696 err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1);
1697 if (!err)
1698 err = imx274_write_table(priv, imx274_tp_regs);
1699 } else {
1700 err = -EINVAL;
1701 }
1702
1703 if (!err)
1704 dev_dbg(&priv->client->dev,
1705 "%s : TEST PATTERN control success\n", __func__);
1706 else
1707 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1708
1709 return err;
1710}
1711
Leon Luo0985dd32017-10-05 02:06:21 +02001712/*
1713 * imx274_set_frame_length - Function called when setting frame length
1714 * @priv: Pointer to device structure
1715 * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
1716 *
1717 * Set frame length based on input value.
1718 *
1719 * Return: 0 on success
1720 */
1721static int imx274_set_frame_length(struct stimx274 *priv, u32 val)
1722{
Leon Luo0985dd32017-10-05 02:06:21 +02001723 int err;
1724 u32 frame_length;
Leon Luo0985dd32017-10-05 02:06:21 +02001725
1726 dev_dbg(&priv->client->dev, "%s : input length = %d\n",
1727 __func__, val);
1728
1729 frame_length = (u32)val;
1730
Luca Ceresoli279b4b9a2018-07-25 12:24:54 -04001731 err = imx274_write_mbreg(priv, IMX274_VMAX_REG_3, frame_length, 3);
1732 if (err)
1733 goto fail;
Leon Luo0985dd32017-10-05 02:06:21 +02001734
1735 return 0;
1736
1737fail:
1738 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1739 return err;
1740}
1741
1742/*
1743 * imx274_set_frame_interval - Function called when setting frame interval
1744 * @priv: Pointer to device structure
1745 * @frame_interval: Variable for frame interval
1746 *
1747 * Change frame interval by updating VMAX value
1748 * The caller should hold the mutex lock imx274->lock if necessary
1749 *
1750 * Return: 0 on success
1751 */
1752static int imx274_set_frame_interval(struct stimx274 *priv,
1753 struct v4l2_fract frame_interval)
1754{
1755 int err;
1756 u32 frame_length, req_frame_rate;
1757 u16 svr;
1758 u16 hmax;
1759 u8 reg_val[2];
1760
1761 dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d",
1762 __func__, frame_interval.numerator,
1763 frame_interval.denominator);
1764
Hans Verkuil4550a7f2020-07-03 11:20:32 +02001765 if (frame_interval.numerator == 0 || frame_interval.denominator == 0) {
1766 frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1767 frame_interval.numerator = 1;
Leon Luo0985dd32017-10-05 02:06:21 +02001768 }
1769
1770 req_frame_rate = (u32)(frame_interval.denominator
1771 / frame_interval.numerator);
1772
1773 /* boundary check */
Luca Ceresoli96a2c732018-06-11 07:35:33 -04001774 if (req_frame_rate > priv->mode->max_fps) {
Leon Luo0985dd32017-10-05 02:06:21 +02001775 frame_interval.numerator = 1;
Luca Ceresoli96a2c732018-06-11 07:35:33 -04001776 frame_interval.denominator = priv->mode->max_fps;
Leon Luo0985dd32017-10-05 02:06:21 +02001777 } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) {
1778 frame_interval.numerator = 1;
1779 frame_interval.denominator = IMX274_MIN_FRAME_RATE;
1780 }
1781
1782 /*
1783 * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
1784 * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
1785 */
1786
1787 /* SVR */
1788 err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, &reg_val[0]);
1789 if (err)
1790 goto fail;
1791 err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, &reg_val[1]);
1792 if (err)
1793 goto fail;
1794 svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
1795 dev_dbg(&priv->client->dev,
1796 "%s : register SVR = %d\n", __func__, svr);
1797
1798 /* HMAX */
1799 err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, &reg_val[0]);
1800 if (err)
1801 goto fail;
1802 err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, &reg_val[1]);
1803 if (err)
1804 goto fail;
1805 hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0];
1806 dev_dbg(&priv->client->dev,
1807 "%s : register HMAX = %d\n", __func__, hmax);
1808
1809 if (hmax == 0 || frame_interval.denominator == 0) {
1810 err = -EINVAL;
1811 goto fail;
1812 }
1813
1814 frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax
1815 * frame_interval.numerator
1816 / frame_interval.denominator;
1817
1818 err = imx274_set_frame_length(priv, frame_length);
1819 if (err)
1820 goto fail;
1821
1822 priv->frame_interval = frame_interval;
1823 return 0;
1824
1825fail:
1826 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1827 return err;
1828}
1829
1830static const struct v4l2_subdev_pad_ops imx274_pad_ops = {
1831 .get_fmt = imx274_get_fmt,
1832 .set_fmt = imx274_set_fmt,
Luca Ceresoli39dd23d2018-07-25 12:24:55 -04001833 .get_selection = imx274_get_selection,
1834 .set_selection = imx274_set_selection,
Leon Luo0985dd32017-10-05 02:06:21 +02001835};
1836
1837static const struct v4l2_subdev_video_ops imx274_video_ops = {
1838 .g_frame_interval = imx274_g_frame_interval,
1839 .s_frame_interval = imx274_s_frame_interval,
1840 .s_stream = imx274_s_stream,
1841};
1842
1843static const struct v4l2_subdev_ops imx274_subdev_ops = {
1844 .pad = &imx274_pad_ops,
1845 .video = &imx274_video_ops,
1846};
1847
1848static const struct v4l2_ctrl_ops imx274_ctrl_ops = {
1849 .s_ctrl = imx274_s_ctrl,
1850};
1851
1852static const struct of_device_id imx274_of_id_table[] = {
1853 { .compatible = "sony,imx274" },
1854 { }
1855};
1856MODULE_DEVICE_TABLE(of, imx274_of_id_table);
1857
1858static const struct i2c_device_id imx274_id[] = {
1859 { "IMX274", 0 },
1860 { }
1861};
1862MODULE_DEVICE_TABLE(i2c, imx274_id);
1863
1864static int imx274_probe(struct i2c_client *client,
1865 const struct i2c_device_id *id)
1866{
1867 struct v4l2_subdev *sd;
1868 struct stimx274 *imx274;
1869 int ret;
1870
1871 /* initialize imx274 */
1872 imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL);
1873 if (!imx274)
1874 return -ENOMEM;
1875
1876 mutex_init(&imx274->lock);
1877
Luca Ceresoli438ac1f2018-06-11 07:35:32 -04001878 /* initialize format */
Luca Ceresoli0b4c9552018-06-11 07:35:35 -04001879 imx274->mode = &imx274_formats[IMX274_DEFAULT_MODE];
Luca Ceresoli39dd23d2018-07-25 12:24:55 -04001880 imx274->crop.width = IMX274_MAX_WIDTH;
1881 imx274->crop.height = IMX274_MAX_HEIGHT;
1882 imx274->format.width = imx274->crop.width / imx274->mode->bin_ratio;
1883 imx274->format.height = imx274->crop.height / imx274->mode->bin_ratio;
Luca Ceresoli438ac1f2018-06-11 07:35:32 -04001884 imx274->format.field = V4L2_FIELD_NONE;
1885 imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
1886 imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
1887 imx274->frame_interval.numerator = 1;
1888 imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1889
Leon Luo0985dd32017-10-05 02:06:21 +02001890 /* initialize regmap */
1891 imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config);
1892 if (IS_ERR(imx274->regmap)) {
1893 dev_err(&client->dev,
1894 "regmap init failed: %ld\n", PTR_ERR(imx274->regmap));
1895 ret = -ENODEV;
1896 goto err_regmap;
1897 }
1898
1899 /* initialize subdevice */
1900 imx274->client = client;
1901 sd = &imx274->sd;
1902 v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops);
1903 strlcpy(sd->name, DRIVER_NAME, sizeof(sd->name));
1904 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1905
1906 /* initialize subdev media pad */
1907 imx274->pad.flags = MEDIA_PAD_FL_SOURCE;
1908 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1909 ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad);
1910 if (ret < 0) {
1911 dev_err(&client->dev,
1912 "%s : media entity init Failed %d\n", __func__, ret);
1913 goto err_regmap;
1914 }
1915
1916 /* initialize sensor reset gpio */
1917 imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1918 GPIOD_OUT_HIGH);
1919 if (IS_ERR(imx274->reset_gpio)) {
1920 if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER)
1921 dev_err(&client->dev, "Reset GPIO not setup in DT");
1922 ret = PTR_ERR(imx274->reset_gpio);
1923 goto err_me;
1924 }
1925
1926 /* pull sensor out of reset */
1927 imx274_reset(imx274, 1);
1928
1929 /* initialize controls */
1930 ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 2);
1931 if (ret < 0) {
1932 dev_err(&client->dev,
1933 "%s : ctrl handler init Failed\n", __func__);
1934 goto err_me;
1935 }
1936
1937 imx274->ctrls.handler.lock = &imx274->lock;
1938
1939 /* add new controls */
1940 imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items(
1941 &imx274->ctrls.handler, &imx274_ctrl_ops,
1942 V4L2_CID_TEST_PATTERN,
1943 ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu);
1944
1945 imx274->ctrls.gain = v4l2_ctrl_new_std(
1946 &imx274->ctrls.handler,
1947 &imx274_ctrl_ops,
1948 V4L2_CID_GAIN, IMX274_MIN_GAIN,
1949 IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1,
1950 IMX274_DEF_GAIN);
1951
1952 imx274->ctrls.exposure = v4l2_ctrl_new_std(
1953 &imx274->ctrls.handler,
1954 &imx274_ctrl_ops,
1955 V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME,
1956 1000000 / IMX274_DEF_FRAME_RATE, 1,
1957 IMX274_MIN_EXPOSURE_TIME);
1958
1959 imx274->ctrls.vflip = v4l2_ctrl_new_std(
1960 &imx274->ctrls.handler,
1961 &imx274_ctrl_ops,
1962 V4L2_CID_VFLIP, 0, 1, 1, 0);
1963
1964 imx274->sd.ctrl_handler = &imx274->ctrls.handler;
1965 if (imx274->ctrls.handler.error) {
1966 ret = imx274->ctrls.handler.error;
1967 goto err_ctrls;
1968 }
1969
1970 /* setup default controls */
1971 ret = v4l2_ctrl_handler_setup(&imx274->ctrls.handler);
1972 if (ret) {
1973 dev_err(&client->dev,
1974 "Error %d setup default controls\n", ret);
1975 goto err_ctrls;
1976 }
1977
Leon Luo0985dd32017-10-05 02:06:21 +02001978 /* load default control values */
1979 ret = imx274_load_default(imx274);
1980 if (ret) {
1981 dev_err(&client->dev,
1982 "%s : imx274_load_default failed %d\n",
1983 __func__, ret);
1984 goto err_ctrls;
1985 }
1986
1987 /* register subdevice */
1988 ret = v4l2_async_register_subdev(sd);
1989 if (ret < 0) {
1990 dev_err(&client->dev,
1991 "%s : v4l2_async_register_subdev failed %d\n",
1992 __func__, ret);
1993 goto err_ctrls;
1994 }
1995
1996 dev_info(&client->dev, "imx274 : imx274 probe success !\n");
1997 return 0;
1998
1999err_ctrls:
Sakari Ailus781b0452017-11-01 05:40:58 -04002000 v4l2_ctrl_handler_free(&imx274->ctrls.handler);
Leon Luo0985dd32017-10-05 02:06:21 +02002001err_me:
2002 media_entity_cleanup(&sd->entity);
2003err_regmap:
2004 mutex_destroy(&imx274->lock);
2005 return ret;
2006}
2007
2008static int imx274_remove(struct i2c_client *client)
2009{
2010 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2011 struct stimx274 *imx274 = to_imx274(sd);
2012
2013 /* stop stream */
Luca Ceresoli8ed8bba2018-04-24 04:24:11 -04002014 imx274_write_table(imx274, imx274_stop);
Leon Luo0985dd32017-10-05 02:06:21 +02002015
2016 v4l2_async_unregister_subdev(sd);
Sakari Ailus781b0452017-11-01 05:40:58 -04002017 v4l2_ctrl_handler_free(&imx274->ctrls.handler);
Leon Luo0985dd32017-10-05 02:06:21 +02002018 media_entity_cleanup(&sd->entity);
2019 mutex_destroy(&imx274->lock);
2020 return 0;
2021}
2022
2023static struct i2c_driver imx274_i2c_driver = {
2024 .driver = {
2025 .name = DRIVER_NAME,
2026 .of_match_table = imx274_of_id_table,
2027 },
2028 .probe = imx274_probe,
2029 .remove = imx274_remove,
2030 .id_table = imx274_id,
2031};
2032
2033module_i2c_driver(imx274_i2c_driver);
2034
2035MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
2036MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
2037MODULE_LICENSE("GPL v2");