Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * imx274.c - IMX274 CMOS Image Sensor driver
4 *
5 * Copyright (C) 2017, Leopard Imaging, Inc.
6 *
7 * Leon Luo <leonl@leopardimaging.com>
8 * Edwin Zou <edwinz@leopardimaging.com>
9 * Luca Ceresoli <luca@lucaceresoli.net>
10 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/gpio/consumer.h>
15#include <linux/i2c.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/pm_runtime.h>
20#include <linux/regmap.h>
21#include <linux/regulator/consumer.h>
22#include <linux/slab.h>
23#include <linux/v4l2-mediabus.h>
24#include <linux/videodev2.h>
25
26#include <media/v4l2-ctrls.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-fwnode.h>
29#include <media/v4l2-subdev.h>
30
31/*
32 * See "SHR, SVR Setting" in datasheet
33 */
34#define IMX274_DEFAULT_FRAME_LENGTH (4550)
35#define IMX274_MAX_FRAME_LENGTH (0x000fffff)
36
37/*
38 * See "Frame Rate Adjustment" in datasheet
39 */
40#define IMX274_PIXCLK_CONST1 (72000000)
41#define IMX274_PIXCLK_CONST2 (1000000)
42
43/*
44 * The input gain is shifted by IMX274_GAIN_SHIFT to get
45 * decimal number. The real gain is
46 * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
47 */
48#define IMX274_GAIN_SHIFT (8)
49#define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1)
50
51/*
52 * See "Analog Gain" and "Digital Gain" in datasheet
53 * min gain is 1X
54 * max gain is calculated based on IMX274_GAIN_REG_MAX
55 */
56#define IMX274_GAIN_REG_MAX (1957)
57#define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT)
58#define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\
59 / (2048 - IMX274_GAIN_REG_MAX))
60#define IMX274_MAX_DIGITAL_GAIN (8)
61#define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT)
62#define IMX274_GAIN_CONST (2048) /* for gain formula */
63
64/*
65 * 1 line time in us = (HMAX / 72), minimal is 4 lines
66 */
67#define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72)
68
69#define IMX274_MAX_WIDTH (3840)
70#define IMX274_MAX_HEIGHT (2160)
71#define IMX274_MAX_FRAME_RATE (120)
72#define IMX274_MIN_FRAME_RATE (5)
73#define IMX274_DEF_FRAME_RATE (60)
74
75/*
76 * register SHR is limited to (SVR value + 1) x VMAX value - 4
77 */
78#define IMX274_SHR_LIMIT_CONST (4)
79
80/*
81 * Min and max sensor reset delay (microseconds)
82 */
83#define IMX274_RESET_DELAY1 (2000)
84#define IMX274_RESET_DELAY2 (2200)
85
86/*
87 * shift and mask constants
88 */
89#define IMX274_SHIFT_8_BITS (8)
90#define IMX274_SHIFT_16_BITS (16)
91#define IMX274_MASK_LSB_2_BITS (0x03)
92#define IMX274_MASK_LSB_3_BITS (0x07)
93#define IMX274_MASK_LSB_4_BITS (0x0f)
94#define IMX274_MASK_LSB_8_BITS (0x00ff)
95
96#define DRIVER_NAME "IMX274"
97
98/*
99 * IMX274 register definitions
100 */
101#define IMX274_SHR_REG_MSB 0x300D /* SHR */
102#define IMX274_SHR_REG_LSB 0x300C /* SHR */
103#define IMX274_SVR_REG_MSB 0x300F /* SVR */
104#define IMX274_SVR_REG_LSB 0x300E /* SVR */
105#define IMX274_HTRIM_EN_REG 0x3037
106#define IMX274_HTRIM_START_REG_LSB 0x3038
107#define IMX274_HTRIM_START_REG_MSB 0x3039
108#define IMX274_HTRIM_END_REG_LSB 0x303A
109#define IMX274_HTRIM_END_REG_MSB 0x303B
110#define IMX274_VWIDCUTEN_REG 0x30DD
111#define IMX274_VWIDCUT_REG_LSB 0x30DE
112#define IMX274_VWIDCUT_REG_MSB 0x30DF
113#define IMX274_VWINPOS_REG_LSB 0x30E0
114#define IMX274_VWINPOS_REG_MSB 0x30E1
115#define IMX274_WRITE_VSIZE_REG_LSB 0x3130
116#define IMX274_WRITE_VSIZE_REG_MSB 0x3131
117#define IMX274_Y_OUT_SIZE_REG_LSB 0x3132
118#define IMX274_Y_OUT_SIZE_REG_MSB 0x3133
119#define IMX274_VMAX_REG_1 0x30FA /* VMAX, MSB */
120#define IMX274_VMAX_REG_2 0x30F9 /* VMAX */
121#define IMX274_VMAX_REG_3 0x30F8 /* VMAX, LSB */
122#define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */
123#define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */
124#define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */
125#define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */
126#define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */
127#define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */
128#define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */
129#define IMX274_STANDBY_REG 0x3000 /* STANDBY */
130
131#define IMX274_TABLE_WAIT_MS 0
132#define IMX274_TABLE_END 1
133
134/* regulator supplies */
135static const char * const imx274_supply_names[] = {
136 "vddl", /* IF (1.2V) supply */
137 "vdig", /* Digital Core (1.8V) supply */
138 "vana", /* Analog (2.8V) supply */
139};
140
141#define IMX274_NUM_SUPPLIES ARRAY_SIZE(imx274_supply_names)
142
143/*
144 * imx274 I2C operation related structure
145 */
146struct reg_8 {
147 u16 addr;
148 u8 val;
149};
150
151static const struct regmap_config imx274_regmap_config = {
152 .reg_bits = 16,
153 .val_bits = 8,
154 .cache_type = REGCACHE_RBTREE,
155};
156
157/*
158 * Parameters for each imx274 readout mode.
159 *
160 * These are the values to configure the sensor in one of the
161 * implemented modes.
162 *
163 * @init_regs: registers to initialize the mode
164 * @wbin_ratio: width downscale factor (e.g. 3 for 1280; 3 = 3840/1280)
165 * @hbin_ratio: height downscale factor (e.g. 3 for 720; 3 = 2160/720)
166 * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
167 * Adjustment (CSI-2)" in the datasheet)
168 * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
169 * datasheet)
170 * @max_fps: Maximum frames per second
171 * @nocpiop: Number of clocks per internal offset period (see "Integration Time
172 * in Each Readout Drive Mode (CSI-2)" in the datasheet)
173 */
174struct imx274_mode {
175 const struct reg_8 *init_regs;
176 u8 wbin_ratio;
177 u8 hbin_ratio;
178 int min_frame_len;
179 int min_SHR;
180 int max_fps;
181 int nocpiop;
182};
183
184/*
185 * imx274 test pattern related structure
186 */
187enum {
188 TEST_PATTERN_DISABLED = 0,
189 TEST_PATTERN_ALL_000H,
190 TEST_PATTERN_ALL_FFFH,
191 TEST_PATTERN_ALL_555H,
192 TEST_PATTERN_ALL_AAAH,
193 TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */
194 TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */
195 TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */
196 TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */
197 TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */
198 TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */
199 TEST_PATTERN_H_COLOR_BARS,
200 TEST_PATTERN_V_COLOR_BARS,
201};
202
203static const char * const tp_qmenu[] = {
204 "Disabled",
205 "All 000h Pattern",
206 "All FFFh Pattern",
207 "All 555h Pattern",
208 "All AAAh Pattern",
209 "Vertical Stripe (555h / AAAh)",
210 "Vertical Stripe (AAAh / 555h)",
211 "Vertical Stripe (000h / 555h)",
212 "Vertical Stripe (555h / 000h)",
213 "Vertical Stripe (000h / FFFh)",
214 "Vertical Stripe (FFFh / 000h)",
215 "Vertical Color Bars",
216 "Horizontal Color Bars",
217};
218
219/*
220 * All-pixel scan mode (10-bit)
221 * imx274 mode1(refer to datasheet) register configuration with
222 * 3840x2160 resolution, raw10 data and mipi four lane output
223 */
224static const struct reg_8 imx274_mode1_3840x2160_raw10[] = {
225 {0x3004, 0x01},
226 {0x3005, 0x01},
227 {0x3006, 0x00},
228 {0x3007, 0xa2},
229
230 {0x3018, 0xA2}, /* output XVS, HVS */
231
232 {0x306B, 0x05},
233 {0x30E2, 0x01},
234
235 {0x30EE, 0x01},
236 {0x3342, 0x0A},
237 {0x3343, 0x00},
238 {0x3344, 0x16},
239 {0x3345, 0x00},
240 {0x33A6, 0x01},
241 {0x3528, 0x0E},
242 {0x3554, 0x1F},
243 {0x3555, 0x01},
244 {0x3556, 0x01},
245 {0x3557, 0x01},
246 {0x3558, 0x01},
247 {0x3559, 0x00},
248 {0x355A, 0x00},
249 {0x35BA, 0x0E},
250 {0x366A, 0x1B},
251 {0x366B, 0x1A},
252 {0x366C, 0x19},
253 {0x366D, 0x17},
254 {0x3A41, 0x08},
255
256 {IMX274_TABLE_END, 0x00}
257};
258
259/*
260 * Horizontal/vertical 2/2-line binning
261 * (Horizontal and vertical weightedbinning, 10-bit)
262 * imx274 mode3(refer to datasheet) register configuration with
263 * 1920x1080 resolution, raw10 data and mipi four lane output
264 */
265static const struct reg_8 imx274_mode3_1920x1080_raw10[] = {
266 {0x3004, 0x02},
267 {0x3005, 0x21},
268 {0x3006, 0x00},
269 {0x3007, 0xb1},
270
271 {0x3018, 0xA2}, /* output XVS, HVS */
272
273 {0x306B, 0x05},
274 {0x30E2, 0x02},
275
276 {0x30EE, 0x01},
277 {0x3342, 0x0A},
278 {0x3343, 0x00},
279 {0x3344, 0x1A},
280 {0x3345, 0x00},
281 {0x33A6, 0x01},
282 {0x3528, 0x0E},
283 {0x3554, 0x00},
284 {0x3555, 0x01},
285 {0x3556, 0x01},
286 {0x3557, 0x01},
287 {0x3558, 0x01},
288 {0x3559, 0x00},
289 {0x355A, 0x00},
290 {0x35BA, 0x0E},
291 {0x366A, 0x1B},
292 {0x366B, 0x1A},
293 {0x366C, 0x19},
294 {0x366D, 0x17},
295 {0x3A41, 0x08},
296
297 {IMX274_TABLE_END, 0x00}
298};
299
300/*
301 * Vertical 2/3 subsampling binning horizontal 3 binning
302 * imx274 mode5(refer to datasheet) register configuration with
303 * 1280x720 resolution, raw10 data and mipi four lane output
304 */
305static const struct reg_8 imx274_mode5_1280x720_raw10[] = {
306 {0x3004, 0x03},
307 {0x3005, 0x31},
308 {0x3006, 0x00},
309 {0x3007, 0xa9},
310
311 {0x3018, 0xA2}, /* output XVS, HVS */
312
313 {0x306B, 0x05},
314 {0x30E2, 0x03},
315
316 {0x30EE, 0x01},
317 {0x3342, 0x0A},
318 {0x3343, 0x00},
319 {0x3344, 0x1B},
320 {0x3345, 0x00},
321 {0x33A6, 0x01},
322 {0x3528, 0x0E},
323 {0x3554, 0x00},
324 {0x3555, 0x01},
325 {0x3556, 0x01},
326 {0x3557, 0x01},
327 {0x3558, 0x01},
328 {0x3559, 0x00},
329 {0x355A, 0x00},
330 {0x35BA, 0x0E},
331 {0x366A, 0x1B},
332 {0x366B, 0x19},
333 {0x366C, 0x17},
334 {0x366D, 0x17},
335 {0x3A41, 0x04},
336
337 {IMX274_TABLE_END, 0x00}
338};
339
340/*
341 * Vertical 2/8 subsampling horizontal 3 binning
342 * imx274 mode6(refer to datasheet) register configuration with
343 * 1280x540 resolution, raw10 data and mipi four lane output
344 */
345static const struct reg_8 imx274_mode6_1280x540_raw10[] = {
346 {0x3004, 0x04}, /* mode setting */
347 {0x3005, 0x31},
348 {0x3006, 0x00},
349 {0x3007, 0x02}, /* mode setting */
350
351 {0x3018, 0xA2}, /* output XVS, HVS */
352
353 {0x306B, 0x05},
354 {0x30E2, 0x04}, /* mode setting */
355
356 {0x30EE, 0x01},
357 {0x3342, 0x0A},
358 {0x3343, 0x00},
359 {0x3344, 0x16},
360 {0x3345, 0x00},
361 {0x33A6, 0x01},
362 {0x3528, 0x0E},
363 {0x3554, 0x1F},
364 {0x3555, 0x01},
365 {0x3556, 0x01},
366 {0x3557, 0x01},
367 {0x3558, 0x01},
368 {0x3559, 0x00},
369 {0x355A, 0x00},
370 {0x35BA, 0x0E},
371 {0x366A, 0x1B},
372 {0x366B, 0x1A},
373 {0x366C, 0x19},
374 {0x366D, 0x17},
375 {0x3A41, 0x04},
376
377 {IMX274_TABLE_END, 0x00}
378};
379
380/*
381 * imx274 first step register configuration for
382 * starting stream
383 */
384static const struct reg_8 imx274_start_1[] = {
385 {IMX274_STANDBY_REG, 0x12},
386
387 /* PLRD: clock settings */
388 {0x3120, 0xF0},
389 {0x3121, 0x00},
390 {0x3122, 0x02},
391 {0x3129, 0x9C},
392 {0x312A, 0x02},
393 {0x312D, 0x02},
394
395 {0x310B, 0x00},
396
397 /* PLSTMG */
398 {0x304C, 0x00}, /* PLSTMG01 */
399 {0x304D, 0x03},
400 {0x331C, 0x1A},
401 {0x331D, 0x00},
402 {0x3502, 0x02},
403 {0x3529, 0x0E},
404 {0x352A, 0x0E},
405 {0x352B, 0x0E},
406 {0x3538, 0x0E},
407 {0x3539, 0x0E},
408 {0x3553, 0x00},
409 {0x357D, 0x05},
410 {0x357F, 0x05},
411 {0x3581, 0x04},
412 {0x3583, 0x76},
413 {0x3587, 0x01},
414 {0x35BB, 0x0E},
415 {0x35BC, 0x0E},
416 {0x35BD, 0x0E},
417 {0x35BE, 0x0E},
418 {0x35BF, 0x0E},
419 {0x366E, 0x00},
420 {0x366F, 0x00},
421 {0x3670, 0x00},
422 {0x3671, 0x00},
423
424 /* PSMIPI */
425 {0x3304, 0x32}, /* PSMIPI1 */
426 {0x3305, 0x00},
427 {0x3306, 0x32},
428 {0x3307, 0x00},
429 {0x3590, 0x32},
430 {0x3591, 0x00},
431 {0x3686, 0x32},
432 {0x3687, 0x00},
433
434 {IMX274_TABLE_END, 0x00}
435};
436
437/*
438 * imx274 second step register configuration for
439 * starting stream
440 */
441static const struct reg_8 imx274_start_2[] = {
442 {IMX274_STANDBY_REG, 0x00},
443 {0x303E, 0x02}, /* SYS_MODE = 2 */
444 {IMX274_TABLE_END, 0x00}
445};
446
447/*
448 * imx274 third step register configuration for
449 * starting stream
450 */
451static const struct reg_8 imx274_start_3[] = {
452 {0x30F4, 0x00},
453 {0x3018, 0xA2}, /* XHS VHS OUTPUT */
454 {IMX274_TABLE_END, 0x00}
455};
456
457/*
458 * imx274 register configuration for stopping stream
459 */
460static const struct reg_8 imx274_stop[] = {
461 {IMX274_STANDBY_REG, 0x01},
462 {IMX274_TABLE_END, 0x00}
463};
464
465/*
466 * imx274 disable test pattern register configuration
467 */
468static const struct reg_8 imx274_tp_disabled[] = {
469 {0x303C, 0x00},
470 {0x377F, 0x00},
471 {0x3781, 0x00},
472 {0x370B, 0x00},
473 {IMX274_TABLE_END, 0x00}
474};
475
476/*
477 * imx274 test pattern register configuration
478 * reg 0x303D defines the test pattern modes
479 */
480static const struct reg_8 imx274_tp_regs[] = {
481 {0x303C, 0x11},
482 {0x370E, 0x01},
483 {0x377F, 0x01},
484 {0x3781, 0x01},
485 {0x370B, 0x11},
486 {IMX274_TABLE_END, 0x00}
487};
488
489/* nocpiop happens to be the same number for the implemented modes */
490static const struct imx274_mode imx274_modes[] = {
491 {
492 /* mode 1, 4K */
493 .wbin_ratio = 1, /* 3840 */
494 .hbin_ratio = 1, /* 2160 */
495 .init_regs = imx274_mode1_3840x2160_raw10,
496 .min_frame_len = 4550,
497 .min_SHR = 12,
498 .max_fps = 60,
499 .nocpiop = 112,
500 },
501 {
502 /* mode 3, 1080p */
503 .wbin_ratio = 2, /* 1920 */
504 .hbin_ratio = 2, /* 1080 */
505 .init_regs = imx274_mode3_1920x1080_raw10,
506 .min_frame_len = 2310,
507 .min_SHR = 8,
508 .max_fps = 120,
509 .nocpiop = 112,
510 },
511 {
512 /* mode 5, 720p */
513 .wbin_ratio = 3, /* 1280 */
514 .hbin_ratio = 3, /* 720 */
515 .init_regs = imx274_mode5_1280x720_raw10,
516 .min_frame_len = 2310,
517 .min_SHR = 8,
518 .max_fps = 120,
519 .nocpiop = 112,
520 },
521 {
522 /* mode 6, 540p */
523 .wbin_ratio = 3, /* 1280 */
524 .hbin_ratio = 4, /* 540 */
525 .init_regs = imx274_mode6_1280x540_raw10,
526 .min_frame_len = 2310,
527 .min_SHR = 4,
528 .max_fps = 120,
529 .nocpiop = 112,
530 },
531};
532
533/*
534 * struct imx274_ctrls - imx274 ctrl structure
535 * @handler: V4L2 ctrl handler structure
536 * @exposure: Pointer to expsure ctrl structure
537 * @gain: Pointer to gain ctrl structure
538 * @vflip: Pointer to vflip ctrl structure
539 * @test_pattern: Pointer to test pattern ctrl structure
540 */
541struct imx274_ctrls {
542 struct v4l2_ctrl_handler handler;
543 struct v4l2_ctrl *exposure;
544 struct v4l2_ctrl *gain;
545 struct v4l2_ctrl *vflip;
546 struct v4l2_ctrl *test_pattern;
547};
548
549/*
550 * struct stim274 - imx274 device structure
551 * @sd: V4L2 subdevice structure
552 * @pad: Media pad structure
553 * @client: Pointer to I2C client
554 * @ctrls: imx274 control structure
555 * @crop: rect to be captured
556 * @compose: compose rect, i.e. output resolution
557 * @format: V4L2 media bus frame format structure
558 * (width and height are in sync with the compose rect)
559 * @frame_rate: V4L2 frame rate structure
560 * @regmap: Pointer to regmap structure
561 * @reset_gpio: Pointer to reset gpio
562 * @supplies: List of analog and digital supply regulators
563 * @inck: Pointer to sensor input clock
564 * @lock: Mutex structure
565 * @mode: Parameters for the selected readout mode
566 */
567struct stimx274 {
568 struct v4l2_subdev sd;
569 struct media_pad pad;
570 struct i2c_client *client;
571 struct imx274_ctrls ctrls;
572 struct v4l2_rect crop;
573 struct v4l2_mbus_framefmt format;
574 struct v4l2_fract frame_interval;
575 struct regmap *regmap;
576 struct gpio_desc *reset_gpio;
577 struct regulator_bulk_data supplies[IMX274_NUM_SUPPLIES];
578 struct clk *inck;
579 struct mutex lock; /* mutex lock for operations */
580 const struct imx274_mode *mode;
581};
582
583#define IMX274_ROUND(dim, step, flags) \
584 ((flags) & V4L2_SEL_FLAG_GE \
585 ? roundup((dim), (step)) \
586 : ((flags) & V4L2_SEL_FLAG_LE \
587 ? rounddown((dim), (step)) \
588 : rounddown((dim) + (step) / 2, (step))))
589
590/*
591 * Function declaration
592 */
593static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl);
594static int imx274_set_exposure(struct stimx274 *priv, int val);
595static int imx274_set_vflip(struct stimx274 *priv, int val);
596static int imx274_set_test_pattern(struct stimx274 *priv, int val);
597static int imx274_set_frame_interval(struct stimx274 *priv,
598 struct v4l2_fract frame_interval);
599
600static inline void msleep_range(unsigned int delay_base)
601{
602 usleep_range(delay_base * 1000, delay_base * 1000 + 500);
603}
604
605/*
606 * v4l2_ctrl and v4l2_subdev related operations
607 */
608static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
609{
610 return &container_of(ctrl->handler,
611 struct stimx274, ctrls.handler)->sd;
612}
613
614static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd)
615{
616 return container_of(sd, struct stimx274, sd);
617}
618
619/*
620 * Writing a register table
621 *
622 * @priv: Pointer to device
623 * @table: Table containing register values (with optional delays)
624 *
625 * This is used to write register table into sensor's reg map.
626 *
627 * Return: 0 on success, errors otherwise
628 */
629static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[])
630{
631 struct regmap *regmap = priv->regmap;
632 int err = 0;
633 const struct reg_8 *next;
634 u8 val;
635
636 int range_start = -1;
637 int range_count = 0;
638 u8 range_vals[16];
639 int max_range_vals = ARRAY_SIZE(range_vals);
640
641 for (next = table;; next++) {
642 if ((next->addr != range_start + range_count) ||
643 (next->addr == IMX274_TABLE_END) ||
644 (next->addr == IMX274_TABLE_WAIT_MS) ||
645 (range_count == max_range_vals)) {
646 if (range_count == 1)
647 err = regmap_write(regmap,
648 range_start, range_vals[0]);
649 else if (range_count > 1)
650 err = regmap_bulk_write(regmap, range_start,
651 &range_vals[0],
652 range_count);
653 else
654 err = 0;
655
656 if (err)
657 return err;
658
659 range_start = -1;
660 range_count = 0;
661
662 /* Handle special address values */
663 if (next->addr == IMX274_TABLE_END)
664 break;
665
666 if (next->addr == IMX274_TABLE_WAIT_MS) {
667 msleep_range(next->val);
668 continue;
669 }
670 }
671
672 val = next->val;
673
674 if (range_start == -1)
675 range_start = next->addr;
676
677 range_vals[range_count++] = val;
678 }
679 return 0;
680}
681
682static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val)
683{
684 int err;
685
686 err = regmap_write(priv->regmap, addr, val);
687 if (err)
688 dev_err(&priv->client->dev,
689 "%s : i2c write failed, %x = %x\n", __func__,
690 addr, val);
691 else
692 dev_dbg(&priv->client->dev,
693 "%s : addr 0x%x, val=0x%x\n", __func__,
694 addr, val);
695 return err;
696}
697
698/**
699 * imx274_read_mbreg - Read a multibyte register.
700 *
701 * Uses a bulk read where possible.
702 *
703 * @priv: Pointer to device structure
704 * @addr: Address of the LSB register. Other registers must be
705 * consecutive, least-to-most significant.
706 * @val: Pointer to store the register value (cpu endianness)
707 * @nbytes: Number of bytes to read (range: [1..3]).
708 * Other bytes are zet to 0.
709 *
710 * Return: 0 on success, errors otherwise
711 */
712static int imx274_read_mbreg(struct stimx274 *priv, u16 addr, u32 *val,
713 size_t nbytes)
714{
715 __le32 val_le = 0;
716 int err;
717
718 err = regmap_bulk_read(priv->regmap, addr, &val_le, nbytes);
719 if (err) {
720 dev_err(&priv->client->dev,
721 "%s : i2c bulk read failed, %x (%zu bytes)\n",
722 __func__, addr, nbytes);
723 } else {
724 *val = le32_to_cpu(val_le);
725 dev_dbg(&priv->client->dev,
726 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
727 __func__, addr, *val, nbytes);
728 }
729
730 return err;
731}
732
733/**
734 * imx274_write_mbreg - Write a multibyte register.
735 *
736 * Uses a bulk write where possible.
737 *
738 * @priv: Pointer to device structure
739 * @addr: Address of the LSB register. Other registers must be
740 * consecutive, least-to-most significant.
741 * @val: Value to be written to the register (cpu endianness)
742 * @nbytes: Number of bytes to write (range: [1..3])
743 */
744static int imx274_write_mbreg(struct stimx274 *priv, u16 addr, u32 val,
745 size_t nbytes)
746{
747 __le32 val_le = cpu_to_le32(val);
748 int err;
749
750 err = regmap_bulk_write(priv->regmap, addr, &val_le, nbytes);
751 if (err)
752 dev_err(&priv->client->dev,
753 "%s : i2c bulk write failed, %x = %x (%zu bytes)\n",
754 __func__, addr, val, nbytes);
755 else
756 dev_dbg(&priv->client->dev,
757 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
758 __func__, addr, val, nbytes);
759 return err;
760}
761
762/*
763 * Set mode registers to start stream.
764 * @priv: Pointer to device structure
765 *
766 * Return: 0 on success, errors otherwise
767 */
768static int imx274_mode_regs(struct stimx274 *priv)
769{
770 int err = 0;
771
772 err = imx274_write_table(priv, imx274_start_1);
773 if (err)
774 return err;
775
776 err = imx274_write_table(priv, priv->mode->init_regs);
777
778 return err;
779}
780
781/*
782 * imx274_start_stream - Function for starting stream per mode index
783 * @priv: Pointer to device structure
784 *
785 * Return: 0 on success, errors otherwise
786 */
787static int imx274_start_stream(struct stimx274 *priv)
788{
789 int err = 0;
790
791 err = __v4l2_ctrl_handler_setup(&priv->ctrls.handler);
792 if (err) {
793 dev_err(&priv->client->dev, "Error %d setup controls\n", err);
794 return err;
795 }
796
797 /*
798 * Refer to "Standby Cancel Sequence when using CSI-2" in
799 * imx274 datasheet, it should wait 10ms or more here.
800 * give it 1 extra ms for margin
801 */
802 msleep_range(11);
803 err = imx274_write_table(priv, imx274_start_2);
804 if (err)
805 return err;
806
807 /*
808 * Refer to "Standby Cancel Sequence when using CSI-2" in
809 * imx274 datasheet, it should wait 7ms or more here.
810 * give it 1 extra ms for margin
811 */
812 msleep_range(8);
813 err = imx274_write_table(priv, imx274_start_3);
814 if (err)
815 return err;
816
817 return 0;
818}
819
820/*
821 * imx274_reset - Function called to reset the sensor
822 * @priv: Pointer to device structure
823 * @rst: Input value for determining the sensor's end state after reset
824 *
825 * Set the senor in reset and then
826 * if rst = 0, keep it in reset;
827 * if rst = 1, bring it out of reset.
828 *
829 */
830static void imx274_reset(struct stimx274 *priv, int rst)
831{
832 gpiod_set_value_cansleep(priv->reset_gpio, 0);
833 usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
834 gpiod_set_value_cansleep(priv->reset_gpio, !!rst);
835 usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
836}
837
838static int imx274_power_on(struct device *dev)
839{
840 struct i2c_client *client = to_i2c_client(dev);
841 struct v4l2_subdev *sd = i2c_get_clientdata(client);
842 struct stimx274 *imx274 = to_imx274(sd);
843 int ret;
844
845 /* keep sensor in reset before power on */
846 imx274_reset(imx274, 0);
847
848 ret = clk_prepare_enable(imx274->inck);
849 if (ret) {
850 dev_err(&imx274->client->dev,
851 "Failed to enable input clock: %d\n", ret);
852 return ret;
853 }
854
855 ret = regulator_bulk_enable(IMX274_NUM_SUPPLIES, imx274->supplies);
856 if (ret) {
857 dev_err(&imx274->client->dev,
858 "Failed to enable regulators: %d\n", ret);
859 goto fail_reg;
860 }
861
862 udelay(2);
863 imx274_reset(imx274, 1);
864
865 return 0;
866
867fail_reg:
868 clk_disable_unprepare(imx274->inck);
869 return ret;
870}
871
872static int imx274_power_off(struct device *dev)
873{
874 struct i2c_client *client = to_i2c_client(dev);
875 struct v4l2_subdev *sd = i2c_get_clientdata(client);
876 struct stimx274 *imx274 = to_imx274(sd);
877
878 imx274_reset(imx274, 0);
879
880 regulator_bulk_disable(IMX274_NUM_SUPPLIES, imx274->supplies);
881
882 clk_disable_unprepare(imx274->inck);
883
884 return 0;
885}
886
887static int imx274_regulators_get(struct device *dev, struct stimx274 *imx274)
888{
889 unsigned int i;
890
891 for (i = 0; i < IMX274_NUM_SUPPLIES; i++)
892 imx274->supplies[i].supply = imx274_supply_names[i];
893
894 return devm_regulator_bulk_get(dev, IMX274_NUM_SUPPLIES,
895 imx274->supplies);
896}
897
898/**
899 * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
900 * @ctrl: V4L2 control to be set
901 *
902 * This function is used to set the V4L2 controls for the imx274 sensor.
903 *
904 * Return: 0 on success, errors otherwise
905 */
906static int imx274_s_ctrl(struct v4l2_ctrl *ctrl)
907{
908 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
909 struct stimx274 *imx274 = to_imx274(sd);
910 int ret = -EINVAL;
911
912 if (!pm_runtime_get_if_in_use(&imx274->client->dev))
913 return 0;
914
915 dev_dbg(&imx274->client->dev,
916 "%s : s_ctrl: %s, value: %d\n", __func__,
917 ctrl->name, ctrl->val);
918
919 switch (ctrl->id) {
920 case V4L2_CID_EXPOSURE:
921 dev_dbg(&imx274->client->dev,
922 "%s : set V4L2_CID_EXPOSURE\n", __func__);
923 ret = imx274_set_exposure(imx274, ctrl->val);
924 break;
925
926 case V4L2_CID_GAIN:
927 dev_dbg(&imx274->client->dev,
928 "%s : set V4L2_CID_GAIN\n", __func__);
929 ret = imx274_set_gain(imx274, ctrl);
930 break;
931
932 case V4L2_CID_VFLIP:
933 dev_dbg(&imx274->client->dev,
934 "%s : set V4L2_CID_VFLIP\n", __func__);
935 ret = imx274_set_vflip(imx274, ctrl->val);
936 break;
937
938 case V4L2_CID_TEST_PATTERN:
939 dev_dbg(&imx274->client->dev,
940 "%s : set V4L2_CID_TEST_PATTERN\n", __func__);
941 ret = imx274_set_test_pattern(imx274, ctrl->val);
942 break;
943 }
944
945 pm_runtime_put(&imx274->client->dev);
946
947 return ret;
948}
949
950static int imx274_binning_goodness(struct stimx274 *imx274,
951 int w, int ask_w,
952 int h, int ask_h, u32 flags)
953{
954 struct device *dev = &imx274->client->dev;
955 const int goodness = 100000;
956 int val = 0;
957
958 if (flags & V4L2_SEL_FLAG_GE) {
959 if (w < ask_w)
960 val -= goodness;
961 if (h < ask_h)
962 val -= goodness;
963 }
964
965 if (flags & V4L2_SEL_FLAG_LE) {
966 if (w > ask_w)
967 val -= goodness;
968 if (h > ask_h)
969 val -= goodness;
970 }
971
972 val -= abs(w - ask_w);
973 val -= abs(h - ask_h);
974
975 dev_dbg(dev, "%s: ask %dx%d, size %dx%d, goodness %d\n",
976 __func__, ask_w, ask_h, w, h, val);
977
978 return val;
979}
980
981/**
982 * __imx274_change_compose - Helper function to change binning and set both
983 * compose and format.
984 *
985 * We have two entry points to change binning: set_fmt and
986 * set_selection(COMPOSE). Both have to compute the new output size
987 * and set it in both the compose rect and the frame format size. We
988 * also need to do the same things after setting cropping to restore
989 * 1:1 binning.
990 *
991 * This function contains the common code for these three cases, it
992 * has many arguments in order to accommodate the needs of all of
993 * them.
994 *
995 * Must be called with imx274->lock locked.
996 *
997 * @imx274: The device object
998 * @sd_state: The subdev state we are editing for TRY requests
999 * @which: V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller
1000 * @width: Input-output parameter: set to the desired width before
1001 * the call, contains the chosen value after returning successfully
1002 * @height: Input-output parameter for height (see @width)
1003 * @flags: Selection flags from struct v4l2_subdev_selection, or 0 if not
1004 * available (when called from set_fmt)
1005 */
1006static int __imx274_change_compose(struct stimx274 *imx274,
1007 struct v4l2_subdev_state *sd_state,
1008 u32 which,
1009 u32 *width,
1010 u32 *height,
1011 u32 flags)
1012{
1013 struct device *dev = &imx274->client->dev;
1014 const struct v4l2_rect *cur_crop;
1015 struct v4l2_mbus_framefmt *tgt_fmt;
1016 unsigned int i;
1017 const struct imx274_mode *best_mode = &imx274_modes[0];
1018 int best_goodness = INT_MIN;
1019
1020 if (which == V4L2_SUBDEV_FORMAT_TRY) {
1021 cur_crop = &sd_state->pads->try_crop;
1022 tgt_fmt = &sd_state->pads->try_fmt;
1023 } else {
1024 cur_crop = &imx274->crop;
1025 tgt_fmt = &imx274->format;
1026 }
1027
1028 for (i = 0; i < ARRAY_SIZE(imx274_modes); i++) {
1029 u8 wratio = imx274_modes[i].wbin_ratio;
1030 u8 hratio = imx274_modes[i].hbin_ratio;
1031
1032 int goodness = imx274_binning_goodness(
1033 imx274,
1034 cur_crop->width / wratio, *width,
1035 cur_crop->height / hratio, *height,
1036 flags);
1037
1038 if (goodness >= best_goodness) {
1039 best_goodness = goodness;
1040 best_mode = &imx274_modes[i];
1041 }
1042 }
1043
1044 *width = cur_crop->width / best_mode->wbin_ratio;
1045 *height = cur_crop->height / best_mode->hbin_ratio;
1046
1047 if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
1048 imx274->mode = best_mode;
1049
1050 dev_dbg(dev, "%s: selected %ux%u binning\n",
1051 __func__, best_mode->wbin_ratio, best_mode->hbin_ratio);
1052
1053 tgt_fmt->width = *width;
1054 tgt_fmt->height = *height;
1055 tgt_fmt->field = V4L2_FIELD_NONE;
1056
1057 return 0;
1058}
1059
1060/**
1061 * imx274_get_fmt - Get the pad format
1062 * @sd: Pointer to V4L2 Sub device structure
1063 * @sd_state: Pointer to sub device state structure
1064 * @fmt: Pointer to pad level media bus format
1065 *
1066 * This function is used to get the pad format information.
1067 *
1068 * Return: 0 on success
1069 */
1070static int imx274_get_fmt(struct v4l2_subdev *sd,
1071 struct v4l2_subdev_state *sd_state,
1072 struct v4l2_subdev_format *fmt)
1073{
1074 struct stimx274 *imx274 = to_imx274(sd);
1075
1076 mutex_lock(&imx274->lock);
1077 fmt->format = imx274->format;
1078 mutex_unlock(&imx274->lock);
1079 return 0;
1080}
1081
1082/**
1083 * imx274_set_fmt - This is used to set the pad format
1084 * @sd: Pointer to V4L2 Sub device structure
1085 * @sd_state: Pointer to sub device state information structure
1086 * @format: Pointer to pad level media bus format
1087 *
1088 * This function is used to set the pad format.
1089 *
1090 * Return: 0 on success
1091 */
1092static int imx274_set_fmt(struct v4l2_subdev *sd,
1093 struct v4l2_subdev_state *sd_state,
1094 struct v4l2_subdev_format *format)
1095{
1096 struct v4l2_mbus_framefmt *fmt = &format->format;
1097 struct stimx274 *imx274 = to_imx274(sd);
1098 int err = 0;
1099
1100 mutex_lock(&imx274->lock);
1101
1102 err = __imx274_change_compose(imx274, sd_state, format->which,
1103 &fmt->width, &fmt->height, 0);
1104
1105 if (err)
1106 goto out;
1107
1108 /*
1109 * __imx274_change_compose already set width and height in the
1110 * applicable format, but we need to keep all other format
1111 * values, so do a full copy here
1112 */
1113 fmt->field = V4L2_FIELD_NONE;
1114 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
1115 sd_state->pads->try_fmt = *fmt;
1116 else
1117 imx274->format = *fmt;
1118
1119out:
1120 mutex_unlock(&imx274->lock);
1121
1122 return err;
1123}
1124
1125static int imx274_get_selection(struct v4l2_subdev *sd,
1126 struct v4l2_subdev_state *sd_state,
1127 struct v4l2_subdev_selection *sel)
1128{
1129 struct stimx274 *imx274 = to_imx274(sd);
1130 const struct v4l2_rect *src_crop;
1131 const struct v4l2_mbus_framefmt *src_fmt;
1132 int ret = 0;
1133
1134 if (sel->pad != 0)
1135 return -EINVAL;
1136
1137 if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
1138 sel->r.left = 0;
1139 sel->r.top = 0;
1140 sel->r.width = IMX274_MAX_WIDTH;
1141 sel->r.height = IMX274_MAX_HEIGHT;
1142 return 0;
1143 }
1144
1145 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1146 src_crop = &sd_state->pads->try_crop;
1147 src_fmt = &sd_state->pads->try_fmt;
1148 } else {
1149 src_crop = &imx274->crop;
1150 src_fmt = &imx274->format;
1151 }
1152
1153 mutex_lock(&imx274->lock);
1154
1155 switch (sel->target) {
1156 case V4L2_SEL_TGT_CROP:
1157 sel->r = *src_crop;
1158 break;
1159 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1160 sel->r.top = 0;
1161 sel->r.left = 0;
1162 sel->r.width = src_crop->width;
1163 sel->r.height = src_crop->height;
1164 break;
1165 case V4L2_SEL_TGT_COMPOSE:
1166 sel->r.top = 0;
1167 sel->r.left = 0;
1168 sel->r.width = src_fmt->width;
1169 sel->r.height = src_fmt->height;
1170 break;
1171 default:
1172 ret = -EINVAL;
1173 }
1174
1175 mutex_unlock(&imx274->lock);
1176
1177 return ret;
1178}
1179
1180static int imx274_set_selection_crop(struct stimx274 *imx274,
1181 struct v4l2_subdev_state *sd_state,
1182 struct v4l2_subdev_selection *sel)
1183{
1184 struct v4l2_rect *tgt_crop;
1185 struct v4l2_rect new_crop;
1186 bool size_changed;
1187
1188 /*
1189 * h_step could be 12 or 24 depending on the binning. But we
1190 * won't know the binning until we choose the mode later in
1191 * __imx274_change_compose(). Thus let's be safe and use the
1192 * most conservative value in all cases.
1193 */
1194 const u32 h_step = 24;
1195
1196 new_crop.width = min_t(u32,
1197 IMX274_ROUND(sel->r.width, h_step, sel->flags),
1198 IMX274_MAX_WIDTH);
1199
1200 /* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */
1201 if (new_crop.width < 144)
1202 new_crop.width = 144;
1203
1204 new_crop.left = min_t(u32,
1205 IMX274_ROUND(sel->r.left, h_step, 0),
1206 IMX274_MAX_WIDTH - new_crop.width);
1207
1208 new_crop.height = min_t(u32,
1209 IMX274_ROUND(sel->r.height, 2, sel->flags),
1210 IMX274_MAX_HEIGHT);
1211
1212 new_crop.top = min_t(u32, IMX274_ROUND(sel->r.top, 2, 0),
1213 IMX274_MAX_HEIGHT - new_crop.height);
1214
1215 sel->r = new_crop;
1216
1217 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1218 tgt_crop = &sd_state->pads->try_crop;
1219 else
1220 tgt_crop = &imx274->crop;
1221
1222 mutex_lock(&imx274->lock);
1223
1224 size_changed = (new_crop.width != tgt_crop->width ||
1225 new_crop.height != tgt_crop->height);
1226
1227 /* __imx274_change_compose needs the new size in *tgt_crop */
1228 *tgt_crop = new_crop;
1229
1230 /* if crop size changed then reset the output image size */
1231 if (size_changed)
1232 __imx274_change_compose(imx274, sd_state, sel->which,
1233 &new_crop.width, &new_crop.height,
1234 sel->flags);
1235
1236 mutex_unlock(&imx274->lock);
1237
1238 return 0;
1239}
1240
1241static int imx274_set_selection(struct v4l2_subdev *sd,
1242 struct v4l2_subdev_state *sd_state,
1243 struct v4l2_subdev_selection *sel)
1244{
1245 struct stimx274 *imx274 = to_imx274(sd);
1246
1247 if (sel->pad != 0)
1248 return -EINVAL;
1249
1250 if (sel->target == V4L2_SEL_TGT_CROP)
1251 return imx274_set_selection_crop(imx274, sd_state, sel);
1252
1253 if (sel->target == V4L2_SEL_TGT_COMPOSE) {
1254 int err;
1255
1256 mutex_lock(&imx274->lock);
1257 err = __imx274_change_compose(imx274, sd_state, sel->which,
1258 &sel->r.width, &sel->r.height,
1259 sel->flags);
1260 mutex_unlock(&imx274->lock);
1261
1262 /*
1263 * __imx274_change_compose already set width and
1264 * height in set->r, we still need to set top-left
1265 */
1266 if (!err) {
1267 sel->r.top = 0;
1268 sel->r.left = 0;
1269 }
1270
1271 return err;
1272 }
1273
1274 return -EINVAL;
1275}
1276
1277static int imx274_apply_trimming(struct stimx274 *imx274)
1278{
1279 u32 h_start;
1280 u32 h_end;
1281 u32 hmax;
1282 u32 v_cut;
1283 s32 v_pos;
1284 u32 write_v_size;
1285 u32 y_out_size;
1286 int err;
1287
1288 h_start = imx274->crop.left + 12;
1289 h_end = h_start + imx274->crop.width;
1290
1291 /* Use the minimum allowed value of HMAX */
1292 /* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */
1293 /* Note: 260 is the minimum HMAX in all implemented modes */
1294 hmax = max_t(u32, 260, (imx274->crop.width) / 16 + 23);
1295
1296 /* invert v_pos if VFLIP */
1297 v_pos = imx274->ctrls.vflip->cur.val ?
1298 (-imx274->crop.top / 2) : (imx274->crop.top / 2);
1299 v_cut = (IMX274_MAX_HEIGHT - imx274->crop.height) / 2;
1300 write_v_size = imx274->crop.height + 22;
1301 y_out_size = imx274->crop.height;
1302
1303 err = imx274_write_mbreg(imx274, IMX274_HMAX_REG_LSB, hmax, 2);
1304 if (!err)
1305 err = imx274_write_mbreg(imx274, IMX274_HTRIM_EN_REG, 1, 1);
1306 if (!err)
1307 err = imx274_write_mbreg(imx274, IMX274_HTRIM_START_REG_LSB,
1308 h_start, 2);
1309 if (!err)
1310 err = imx274_write_mbreg(imx274, IMX274_HTRIM_END_REG_LSB,
1311 h_end, 2);
1312 if (!err)
1313 err = imx274_write_mbreg(imx274, IMX274_VWIDCUTEN_REG, 1, 1);
1314 if (!err)
1315 err = imx274_write_mbreg(imx274, IMX274_VWIDCUT_REG_LSB,
1316 v_cut, 2);
1317 if (!err)
1318 err = imx274_write_mbreg(imx274, IMX274_VWINPOS_REG_LSB,
1319 v_pos, 2);
1320 if (!err)
1321 err = imx274_write_mbreg(imx274, IMX274_WRITE_VSIZE_REG_LSB,
1322 write_v_size, 2);
1323 if (!err)
1324 err = imx274_write_mbreg(imx274, IMX274_Y_OUT_SIZE_REG_LSB,
1325 y_out_size, 2);
1326
1327 return err;
1328}
1329
1330/**
1331 * imx274_g_frame_interval - Get the frame interval
1332 * @sd: Pointer to V4L2 Sub device structure
1333 * @fi: Pointer to V4l2 Sub device frame interval structure
1334 *
1335 * This function is used to get the frame interval.
1336 *
1337 * Return: 0 on success
1338 */
1339static int imx274_g_frame_interval(struct v4l2_subdev *sd,
1340 struct v4l2_subdev_frame_interval *fi)
1341{
1342 struct stimx274 *imx274 = to_imx274(sd);
1343
1344 fi->interval = imx274->frame_interval;
1345 dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n",
1346 __func__, imx274->frame_interval.numerator,
1347 imx274->frame_interval.denominator);
1348
1349 return 0;
1350}
1351
1352/**
1353 * imx274_s_frame_interval - Set the frame interval
1354 * @sd: Pointer to V4L2 Sub device structure
1355 * @fi: Pointer to V4l2 Sub device frame interval structure
1356 *
1357 * This function is used to set the frame intervavl.
1358 *
1359 * Return: 0 on success
1360 */
1361static int imx274_s_frame_interval(struct v4l2_subdev *sd,
1362 struct v4l2_subdev_frame_interval *fi)
1363{
1364 struct stimx274 *imx274 = to_imx274(sd);
1365 struct v4l2_ctrl *ctrl = imx274->ctrls.exposure;
1366 int min, max, def;
1367 int ret;
1368
1369 ret = pm_runtime_resume_and_get(&imx274->client->dev);
1370 if (ret < 0)
1371 return ret;
1372
1373 mutex_lock(&imx274->lock);
1374 ret = imx274_set_frame_interval(imx274, fi->interval);
1375
1376 if (!ret) {
1377 fi->interval = imx274->frame_interval;
1378
1379 /*
1380 * exposure time range is decided by frame interval
1381 * need to update it after frame interval changes
1382 */
1383 min = IMX274_MIN_EXPOSURE_TIME;
1384 max = fi->interval.numerator * 1000000
1385 / fi->interval.denominator;
1386 def = max;
1387 ret = __v4l2_ctrl_modify_range(ctrl, min, max, 1, def);
1388 if (ret) {
1389 dev_err(&imx274->client->dev,
1390 "Exposure ctrl range update failed\n");
1391 goto unlock;
1392 }
1393
1394 /* update exposure time accordingly */
1395 imx274_set_exposure(imx274, ctrl->val);
1396
1397 dev_dbg(&imx274->client->dev, "set frame interval to %uus\n",
1398 fi->interval.numerator * 1000000
1399 / fi->interval.denominator);
1400 }
1401
1402unlock:
1403 mutex_unlock(&imx274->lock);
1404 pm_runtime_put(&imx274->client->dev);
1405
1406 return ret;
1407}
1408
1409/**
1410 * imx274_load_default - load default control values
1411 * @priv: Pointer to device structure
1412 *
1413 * Return: 0 on success, errors otherwise
1414 */
1415static void imx274_load_default(struct stimx274 *priv)
1416{
1417 /* load default control values */
1418 priv->frame_interval.numerator = 1;
1419 priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1420 priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE;
1421 priv->ctrls.gain->val = IMX274_DEF_GAIN;
1422 priv->ctrls.vflip->val = 0;
1423 priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED;
1424}
1425
1426/**
1427 * imx274_s_stream - It is used to start/stop the streaming.
1428 * @sd: V4L2 Sub device
1429 * @on: Flag (True / False)
1430 *
1431 * This function controls the start or stop of streaming for the
1432 * imx274 sensor.
1433 *
1434 * Return: 0 on success, errors otherwise
1435 */
1436static int imx274_s_stream(struct v4l2_subdev *sd, int on)
1437{
1438 struct stimx274 *imx274 = to_imx274(sd);
1439 int ret = 0;
1440
1441 dev_dbg(&imx274->client->dev, "%s : %s, mode index = %td\n", __func__,
1442 on ? "Stream Start" : "Stream Stop",
1443 imx274->mode - &imx274_modes[0]);
1444
1445 mutex_lock(&imx274->lock);
1446
1447 if (on) {
1448 ret = pm_runtime_resume_and_get(&imx274->client->dev);
1449 if (ret < 0) {
1450 mutex_unlock(&imx274->lock);
1451 return ret;
1452 }
1453
1454 /* load mode registers */
1455 ret = imx274_mode_regs(imx274);
1456 if (ret)
1457 goto fail;
1458
1459 ret = imx274_apply_trimming(imx274);
1460 if (ret)
1461 goto fail;
1462
1463 /*
1464 * update frame rate & exposure. if the last mode is different,
1465 * HMAX could be changed. As the result, frame rate & exposure
1466 * are changed.
1467 * gain is not affected.
1468 */
1469 ret = imx274_set_frame_interval(imx274,
1470 imx274->frame_interval);
1471 if (ret)
1472 goto fail;
1473
1474 /* start stream */
1475 ret = imx274_start_stream(imx274);
1476 if (ret)
1477 goto fail;
1478 } else {
1479 /* stop stream */
1480 ret = imx274_write_table(imx274, imx274_stop);
1481 if (ret)
1482 goto fail;
1483
1484 pm_runtime_put(&imx274->client->dev);
1485 }
1486
1487 mutex_unlock(&imx274->lock);
1488 dev_dbg(&imx274->client->dev, "%s : Done\n", __func__);
1489 return 0;
1490
1491fail:
1492 pm_runtime_put(&imx274->client->dev);
1493 mutex_unlock(&imx274->lock);
1494 dev_err(&imx274->client->dev, "s_stream failed\n");
1495 return ret;
1496}
1497
1498/*
1499 * imx274_get_frame_length - Function for obtaining current frame length
1500 * @priv: Pointer to device structure
1501 * @val: Pointer to obtained value
1502 *
1503 * frame_length = vmax x (svr + 1), in unit of hmax.
1504 *
1505 * Return: 0 on success
1506 */
1507static int imx274_get_frame_length(struct stimx274 *priv, u32 *val)
1508{
1509 int err;
1510 u32 svr;
1511 u32 vmax;
1512
1513 err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
1514 if (err)
1515 goto fail;
1516
1517 err = imx274_read_mbreg(priv, IMX274_VMAX_REG_3, &vmax, 3);
1518 if (err)
1519 goto fail;
1520
1521 *val = vmax * (svr + 1);
1522
1523 return 0;
1524
1525fail:
1526 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1527 return err;
1528}
1529
1530static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val,
1531 u32 *frame_length)
1532{
1533 int err;
1534
1535 err = imx274_get_frame_length(priv, frame_length);
1536 if (err)
1537 return err;
1538
1539 if (*frame_length < priv->mode->min_frame_len)
1540 *frame_length = priv->mode->min_frame_len;
1541
1542 *val = *frame_length - *val; /* convert to raw shr */
1543 if (*val > *frame_length - IMX274_SHR_LIMIT_CONST)
1544 *val = *frame_length - IMX274_SHR_LIMIT_CONST;
1545 else if (*val < priv->mode->min_SHR)
1546 *val = priv->mode->min_SHR;
1547
1548 return 0;
1549}
1550
1551/*
1552 * imx274_set_digital gain - Function called when setting digital gain
1553 * @priv: Pointer to device structure
1554 * @dgain: Value of digital gain.
1555 *
1556 * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
1557 *
1558 * Return: 0 on success
1559 */
1560static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain)
1561{
1562 u8 reg_val;
1563
1564 reg_val = ffs(dgain);
1565
1566 if (reg_val)
1567 reg_val--;
1568
1569 reg_val = clamp(reg_val, (u8)0, (u8)3);
1570
1571 return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG,
1572 reg_val & IMX274_MASK_LSB_4_BITS);
1573}
1574
1575/*
1576 * imx274_set_gain - Function called when setting gain
1577 * @priv: Pointer to device structure
1578 * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
1579 * @ctrl: v4l2 control pointer
1580 *
1581 * Set the gain based on input value.
1582 * The caller should hold the mutex lock imx274->lock if necessary
1583 *
1584 * Return: 0 on success
1585 */
1586static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl)
1587{
1588 int err;
1589 u32 gain, analog_gain, digital_gain, gain_reg;
1590
1591 gain = (u32)(ctrl->val);
1592
1593 dev_dbg(&priv->client->dev,
1594 "%s : input gain = %d.%d\n", __func__,
1595 gain >> IMX274_GAIN_SHIFT,
1596 ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT);
1597
1598 if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN)
1599 gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN;
1600 else if (gain < IMX274_MIN_GAIN)
1601 gain = IMX274_MIN_GAIN;
1602
1603 if (gain <= IMX274_MAX_ANALOG_GAIN)
1604 digital_gain = 1;
1605 else if (gain <= IMX274_MAX_ANALOG_GAIN * 2)
1606 digital_gain = 2;
1607 else if (gain <= IMX274_MAX_ANALOG_GAIN * 4)
1608 digital_gain = 4;
1609 else
1610 digital_gain = IMX274_MAX_DIGITAL_GAIN;
1611
1612 analog_gain = gain / digital_gain;
1613
1614 dev_dbg(&priv->client->dev,
1615 "%s : digital gain = %d, analog gain = %d.%d\n",
1616 __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT,
1617 ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100)
1618 >> IMX274_GAIN_SHIFT);
1619
1620 err = imx274_set_digital_gain(priv, digital_gain);
1621 if (err)
1622 goto fail;
1623
1624 /* convert to register value, refer to imx274 datasheet */
1625 gain_reg = (u32)IMX274_GAIN_CONST -
1626 (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain;
1627 if (gain_reg > IMX274_GAIN_REG_MAX)
1628 gain_reg = IMX274_GAIN_REG_MAX;
1629
1630 err = imx274_write_mbreg(priv, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg,
1631 2);
1632 if (err)
1633 goto fail;
1634
1635 if (IMX274_GAIN_CONST - gain_reg == 0) {
1636 err = -EINVAL;
1637 goto fail;
1638 }
1639
1640 /* convert register value back to gain value */
1641 ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT)
1642 / (IMX274_GAIN_CONST - gain_reg) * digital_gain;
1643
1644 dev_dbg(&priv->client->dev,
1645 "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
1646 __func__, gain_reg, ctrl->val);
1647
1648 return 0;
1649
1650fail:
1651 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1652 return err;
1653}
1654
1655/*
1656 * imx274_set_coarse_time - Function called when setting SHR value
1657 * @priv: Pointer to device structure
1658 * @val: Value for exposure time in number of line_length, or [HMAX]
1659 *
1660 * Set SHR value based on input value.
1661 *
1662 * Return: 0 on success
1663 */
1664static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val)
1665{
1666 int err;
1667 u32 coarse_time, frame_length;
1668
1669 coarse_time = *val;
1670
1671 /* convert exposure_time to appropriate SHR value */
1672 err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length);
1673 if (err)
1674 goto fail;
1675
1676 err = imx274_write_mbreg(priv, IMX274_SHR_REG_LSB, coarse_time, 2);
1677 if (err)
1678 goto fail;
1679
1680 *val = frame_length - coarse_time;
1681 return 0;
1682
1683fail:
1684 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1685 return err;
1686}
1687
1688/*
1689 * imx274_set_exposure - Function called when setting exposure time
1690 * @priv: Pointer to device structure
1691 * @val: Variable for exposure time, in the unit of micro-second
1692 *
1693 * Set exposure time based on input value.
1694 * The caller should hold the mutex lock imx274->lock if necessary
1695 *
1696 * Return: 0 on success
1697 */
1698static int imx274_set_exposure(struct stimx274 *priv, int val)
1699{
1700 int err;
1701 u32 hmax;
1702 u32 coarse_time; /* exposure time in unit of line (HMAX)*/
1703
1704 dev_dbg(&priv->client->dev,
1705 "%s : EXPOSURE control input = %d\n", __func__, val);
1706
1707 /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
1708
1709 err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
1710 if (err)
1711 goto fail;
1712
1713 if (hmax == 0) {
1714 err = -EINVAL;
1715 goto fail;
1716 }
1717
1718 coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val
1719 - priv->mode->nocpiop) / hmax;
1720
1721 /* step 2: convert exposure_time into SHR value */
1722
1723 /* set SHR */
1724 err = imx274_set_coarse_time(priv, &coarse_time);
1725 if (err)
1726 goto fail;
1727
1728 priv->ctrls.exposure->val =
1729 (coarse_time * hmax + priv->mode->nocpiop)
1730 / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2);
1731
1732 dev_dbg(&priv->client->dev,
1733 "%s : EXPOSURE control success\n", __func__);
1734 return 0;
1735
1736fail:
1737 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1738
1739 return err;
1740}
1741
1742/*
1743 * imx274_set_vflip - Function called when setting vertical flip
1744 * @priv: Pointer to device structure
1745 * @val: Value for vflip setting
1746 *
1747 * Set vertical flip based on input value.
1748 * val = 0: normal, no vertical flip
1749 * val = 1: vertical flip enabled
1750 * The caller should hold the mutex lock imx274->lock if necessary
1751 *
1752 * Return: 0 on success
1753 */
1754static int imx274_set_vflip(struct stimx274 *priv, int val)
1755{
1756 int err;
1757
1758 err = imx274_write_reg(priv, IMX274_VFLIP_REG, val);
1759 if (err) {
1760 dev_err(&priv->client->dev, "VFLIP control error\n");
1761 return err;
1762 }
1763
1764 dev_dbg(&priv->client->dev,
1765 "%s : VFLIP control success\n", __func__);
1766
1767 return 0;
1768}
1769
1770/*
1771 * imx274_set_test_pattern - Function called when setting test pattern
1772 * @priv: Pointer to device structure
1773 * @val: Variable for test pattern
1774 *
1775 * Set to different test patterns based on input value.
1776 *
1777 * Return: 0 on success
1778 */
1779static int imx274_set_test_pattern(struct stimx274 *priv, int val)
1780{
1781 int err = 0;
1782
1783 if (val == TEST_PATTERN_DISABLED) {
1784 err = imx274_write_table(priv, imx274_tp_disabled);
1785 } else if (val <= TEST_PATTERN_V_COLOR_BARS) {
1786 err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1);
1787 if (!err)
1788 err = imx274_write_table(priv, imx274_tp_regs);
1789 } else {
1790 err = -EINVAL;
1791 }
1792
1793 if (!err)
1794 dev_dbg(&priv->client->dev,
1795 "%s : TEST PATTERN control success\n", __func__);
1796 else
1797 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1798
1799 return err;
1800}
1801
1802/*
1803 * imx274_set_frame_length - Function called when setting frame length
1804 * @priv: Pointer to device structure
1805 * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
1806 *
1807 * Set frame length based on input value.
1808 *
1809 * Return: 0 on success
1810 */
1811static int imx274_set_frame_length(struct stimx274 *priv, u32 val)
1812{
1813 int err;
1814 u32 frame_length;
1815
1816 dev_dbg(&priv->client->dev, "%s : input length = %d\n",
1817 __func__, val);
1818
1819 frame_length = (u32)val;
1820
1821 err = imx274_write_mbreg(priv, IMX274_VMAX_REG_3, frame_length, 3);
1822 if (err)
1823 goto fail;
1824
1825 return 0;
1826
1827fail:
1828 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1829 return err;
1830}
1831
1832/*
1833 * imx274_set_frame_interval - Function called when setting frame interval
1834 * @priv: Pointer to device structure
1835 * @frame_interval: Variable for frame interval
1836 *
1837 * Change frame interval by updating VMAX value
1838 * The caller should hold the mutex lock imx274->lock if necessary
1839 *
1840 * Return: 0 on success
1841 */
1842static int imx274_set_frame_interval(struct stimx274 *priv,
1843 struct v4l2_fract frame_interval)
1844{
1845 int err;
1846 u32 frame_length, req_frame_rate;
1847 u32 svr;
1848 u32 hmax;
1849
1850 dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d",
1851 __func__, frame_interval.numerator,
1852 frame_interval.denominator);
1853
1854 if (frame_interval.numerator == 0 || frame_interval.denominator == 0) {
1855 frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1856 frame_interval.numerator = 1;
1857 }
1858
1859 req_frame_rate = (u32)(frame_interval.denominator
1860 / frame_interval.numerator);
1861
1862 /* boundary check */
1863 if (req_frame_rate > priv->mode->max_fps) {
1864 frame_interval.numerator = 1;
1865 frame_interval.denominator = priv->mode->max_fps;
1866 } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) {
1867 frame_interval.numerator = 1;
1868 frame_interval.denominator = IMX274_MIN_FRAME_RATE;
1869 }
1870
1871 /*
1872 * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
1873 * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
1874 */
1875
1876 err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
1877 if (err)
1878 goto fail;
1879
1880 dev_dbg(&priv->client->dev,
1881 "%s : register SVR = %d\n", __func__, svr);
1882
1883 err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
1884 if (err)
1885 goto fail;
1886
1887 dev_dbg(&priv->client->dev,
1888 "%s : register HMAX = %d\n", __func__, hmax);
1889
1890 if (hmax == 0 || frame_interval.denominator == 0) {
1891 err = -EINVAL;
1892 goto fail;
1893 }
1894
1895 frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax
1896 * frame_interval.numerator
1897 / frame_interval.denominator;
1898
1899 err = imx274_set_frame_length(priv, frame_length);
1900 if (err)
1901 goto fail;
1902
1903 priv->frame_interval = frame_interval;
1904 return 0;
1905
1906fail:
1907 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1908 return err;
1909}
1910
1911static int imx274_enum_mbus_code(struct v4l2_subdev *sd,
1912 struct v4l2_subdev_state *sd_state,
1913 struct v4l2_subdev_mbus_code_enum *code)
1914{
1915 if (code->index > 0)
1916 return -EINVAL;
1917
1918 /* only supported format in the driver is Raw 10 bits SRGGB */
1919 code->code = MEDIA_BUS_FMT_SRGGB10_1X10;
1920
1921 return 0;
1922}
1923
1924static const struct v4l2_subdev_pad_ops imx274_pad_ops = {
1925 .enum_mbus_code = imx274_enum_mbus_code,
1926 .get_fmt = imx274_get_fmt,
1927 .set_fmt = imx274_set_fmt,
1928 .get_selection = imx274_get_selection,
1929 .set_selection = imx274_set_selection,
1930};
1931
1932static const struct v4l2_subdev_video_ops imx274_video_ops = {
1933 .g_frame_interval = imx274_g_frame_interval,
1934 .s_frame_interval = imx274_s_frame_interval,
1935 .s_stream = imx274_s_stream,
1936};
1937
1938static const struct v4l2_subdev_ops imx274_subdev_ops = {
1939 .pad = &imx274_pad_ops,
1940 .video = &imx274_video_ops,
1941};
1942
1943static const struct v4l2_ctrl_ops imx274_ctrl_ops = {
1944 .s_ctrl = imx274_s_ctrl,
1945};
1946
1947static const struct of_device_id imx274_of_id_table[] = {
1948 { .compatible = "sony,imx274" },
1949 { }
1950};
1951MODULE_DEVICE_TABLE(of, imx274_of_id_table);
1952
1953static const struct i2c_device_id imx274_id[] = {
1954 { "IMX274", 0 },
1955 { }
1956};
1957MODULE_DEVICE_TABLE(i2c, imx274_id);
1958
1959static int imx274_fwnode_parse(struct device *dev)
1960{
1961 struct fwnode_handle *endpoint;
1962 /* Only CSI2 is supported */
1963 struct v4l2_fwnode_endpoint ep = {
1964 .bus_type = V4L2_MBUS_CSI2_DPHY
1965 };
1966 int ret;
1967
1968 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1969 if (!endpoint) {
1970 dev_err(dev, "Endpoint node not found\n");
1971 return -EINVAL;
1972 }
1973
1974 ret = v4l2_fwnode_endpoint_parse(endpoint, &ep);
1975 fwnode_handle_put(endpoint);
1976 if (ret == -ENXIO) {
1977 dev_err(dev, "Unsupported bus type, should be CSI2\n");
1978 return ret;
1979 } else if (ret) {
1980 dev_err(dev, "Parsing endpoint node failed %d\n", ret);
1981 return ret;
1982 }
1983
1984 /* Check number of data lanes, only 4 lanes supported */
1985 if (ep.bus.mipi_csi2.num_data_lanes != 4) {
1986 dev_err(dev, "Invalid data lanes: %d\n",
1987 ep.bus.mipi_csi2.num_data_lanes);
1988 return -EINVAL;
1989 }
1990
1991 return 0;
1992}
1993
1994static int imx274_probe(struct i2c_client *client)
1995{
1996 struct v4l2_subdev *sd;
1997 struct stimx274 *imx274;
1998 struct device *dev = &client->dev;
1999 int ret;
2000
2001 /* initialize imx274 */
2002 imx274 = devm_kzalloc(dev, sizeof(*imx274), GFP_KERNEL);
2003 if (!imx274)
2004 return -ENOMEM;
2005
2006 mutex_init(&imx274->lock);
2007
2008 ret = imx274_fwnode_parse(dev);
2009 if (ret)
2010 return ret;
2011
2012 imx274->inck = devm_clk_get_optional(dev, "inck");
2013 if (IS_ERR(imx274->inck))
2014 return PTR_ERR(imx274->inck);
2015
2016 ret = imx274_regulators_get(dev, imx274);
2017 if (ret) {
2018 dev_err(dev, "Failed to get power regulators, err: %d\n", ret);
2019 return ret;
2020 }
2021
2022 /* initialize format */
2023 imx274->mode = &imx274_modes[0];
2024 imx274->crop.width = IMX274_MAX_WIDTH;
2025 imx274->crop.height = IMX274_MAX_HEIGHT;
2026 imx274->format.width = imx274->crop.width / imx274->mode->wbin_ratio;
2027 imx274->format.height = imx274->crop.height / imx274->mode->hbin_ratio;
2028 imx274->format.field = V4L2_FIELD_NONE;
2029 imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
2030 imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
2031 imx274->frame_interval.numerator = 1;
2032 imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
2033
2034 /* initialize regmap */
2035 imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config);
2036 if (IS_ERR(imx274->regmap)) {
2037 dev_err(dev,
2038 "regmap init failed: %ld\n", PTR_ERR(imx274->regmap));
2039 ret = -ENODEV;
2040 goto err_regmap;
2041 }
2042
2043 /* initialize subdevice */
2044 imx274->client = client;
2045 sd = &imx274->sd;
2046 v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops);
2047 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
2048
2049 /* initialize subdev media pad */
2050 imx274->pad.flags = MEDIA_PAD_FL_SOURCE;
2051 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
2052 ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad);
2053 if (ret < 0) {
2054 dev_err(dev,
2055 "%s : media entity init Failed %d\n", __func__, ret);
2056 goto err_regmap;
2057 }
2058
2059 /* initialize sensor reset gpio */
2060 imx274->reset_gpio = devm_gpiod_get_optional(dev, "reset",
2061 GPIOD_OUT_HIGH);
2062 if (IS_ERR(imx274->reset_gpio)) {
2063 ret = dev_err_probe(dev, PTR_ERR(imx274->reset_gpio),
2064 "Reset GPIO not setup in DT\n");
2065 goto err_me;
2066 }
2067
2068 /* power on the sensor */
2069 ret = imx274_power_on(dev);
2070 if (ret < 0) {
2071 dev_err(dev, "%s : imx274 power on failed\n", __func__);
2072 goto err_me;
2073 }
2074
2075 /* initialize controls */
2076 ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 4);
2077 if (ret < 0) {
2078 dev_err(dev, "%s : ctrl handler init Failed\n", __func__);
2079 goto err_power_off;
2080 }
2081
2082 imx274->ctrls.handler.lock = &imx274->lock;
2083
2084 /* add new controls */
2085 imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items(
2086 &imx274->ctrls.handler, &imx274_ctrl_ops,
2087 V4L2_CID_TEST_PATTERN,
2088 ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu);
2089
2090 imx274->ctrls.gain = v4l2_ctrl_new_std(
2091 &imx274->ctrls.handler,
2092 &imx274_ctrl_ops,
2093 V4L2_CID_GAIN, IMX274_MIN_GAIN,
2094 IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1,
2095 IMX274_DEF_GAIN);
2096
2097 imx274->ctrls.exposure = v4l2_ctrl_new_std(
2098 &imx274->ctrls.handler,
2099 &imx274_ctrl_ops,
2100 V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME,
2101 1000000 / IMX274_DEF_FRAME_RATE, 1,
2102 IMX274_MIN_EXPOSURE_TIME);
2103
2104 imx274->ctrls.vflip = v4l2_ctrl_new_std(
2105 &imx274->ctrls.handler,
2106 &imx274_ctrl_ops,
2107 V4L2_CID_VFLIP, 0, 1, 1, 0);
2108
2109 imx274->sd.ctrl_handler = &imx274->ctrls.handler;
2110 if (imx274->ctrls.handler.error) {
2111 ret = imx274->ctrls.handler.error;
2112 goto err_ctrls;
2113 }
2114
2115 /* load default control values */
2116 imx274_load_default(imx274);
2117
2118 /* register subdevice */
2119 ret = v4l2_async_register_subdev(sd);
2120 if (ret < 0) {
2121 dev_err(dev, "%s : v4l2_async_register_subdev failed %d\n",
2122 __func__, ret);
2123 goto err_ctrls;
2124 }
2125
2126 pm_runtime_set_active(dev);
2127 pm_runtime_enable(dev);
2128 pm_runtime_idle(dev);
2129
2130 dev_info(dev, "imx274 : imx274 probe success !\n");
2131 return 0;
2132
2133err_ctrls:
2134 v4l2_ctrl_handler_free(&imx274->ctrls.handler);
2135err_power_off:
2136 imx274_power_off(dev);
2137err_me:
2138 media_entity_cleanup(&sd->entity);
2139err_regmap:
2140 mutex_destroy(&imx274->lock);
2141 return ret;
2142}
2143
2144static void imx274_remove(struct i2c_client *client)
2145{
2146 struct v4l2_subdev *sd = i2c_get_clientdata(client);
2147 struct stimx274 *imx274 = to_imx274(sd);
2148
2149 pm_runtime_disable(&client->dev);
2150 if (!pm_runtime_status_suspended(&client->dev))
2151 imx274_power_off(&client->dev);
2152 pm_runtime_set_suspended(&client->dev);
2153
2154 v4l2_async_unregister_subdev(sd);
2155 v4l2_ctrl_handler_free(&imx274->ctrls.handler);
2156
2157 media_entity_cleanup(&sd->entity);
2158 mutex_destroy(&imx274->lock);
2159}
2160
2161static const struct dev_pm_ops imx274_pm_ops = {
2162 SET_RUNTIME_PM_OPS(imx274_power_off, imx274_power_on, NULL)
2163};
2164
2165static struct i2c_driver imx274_i2c_driver = {
2166 .driver = {
2167 .name = DRIVER_NAME,
2168 .pm = &imx274_pm_ops,
2169 .of_match_table = imx274_of_id_table,
2170 },
2171 .probe_new = imx274_probe,
2172 .remove = imx274_remove,
2173 .id_table = imx274_id,
2174};
2175
2176module_i2c_driver(imx274_i2c_driver);
2177
2178MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
2179MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
2180MODULE_LICENSE("GPL v2");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * imx274.c - IMX274 CMOS Image Sensor driver
4 *
5 * Copyright (C) 2017, Leopard Imaging, Inc.
6 *
7 * Leon Luo <leonl@leopardimaging.com>
8 * Edwin Zou <edwinz@leopardimaging.com>
9 * Luca Ceresoli <luca@lucaceresoli.net>
10 */
11
12#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/gpio.h>
15#include <linux/gpio/consumer.h>
16#include <linux/i2c.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/of_gpio.h>
21#include <linux/regmap.h>
22#include <linux/slab.h>
23#include <linux/v4l2-mediabus.h>
24#include <linux/videodev2.h>
25
26#include <media/v4l2-ctrls.h>
27#include <media/v4l2-device.h>
28#include <media/v4l2-subdev.h>
29
30/*
31 * See "SHR, SVR Setting" in datasheet
32 */
33#define IMX274_DEFAULT_FRAME_LENGTH (4550)
34#define IMX274_MAX_FRAME_LENGTH (0x000fffff)
35
36/*
37 * See "Frame Rate Adjustment" in datasheet
38 */
39#define IMX274_PIXCLK_CONST1 (72000000)
40#define IMX274_PIXCLK_CONST2 (1000000)
41
42/*
43 * The input gain is shifted by IMX274_GAIN_SHIFT to get
44 * decimal number. The real gain is
45 * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
46 */
47#define IMX274_GAIN_SHIFT (8)
48#define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1)
49
50/*
51 * See "Analog Gain" and "Digital Gain" in datasheet
52 * min gain is 1X
53 * max gain is calculated based on IMX274_GAIN_REG_MAX
54 */
55#define IMX274_GAIN_REG_MAX (1957)
56#define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT)
57#define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\
58 / (2048 - IMX274_GAIN_REG_MAX))
59#define IMX274_MAX_DIGITAL_GAIN (8)
60#define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT)
61#define IMX274_GAIN_CONST (2048) /* for gain formula */
62
63/*
64 * 1 line time in us = (HMAX / 72), minimal is 4 lines
65 */
66#define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72)
67
68#define IMX274_DEFAULT_BINNING IMX274_BINNING_OFF
69#define IMX274_MAX_WIDTH (3840)
70#define IMX274_MAX_HEIGHT (2160)
71#define IMX274_MAX_FRAME_RATE (120)
72#define IMX274_MIN_FRAME_RATE (5)
73#define IMX274_DEF_FRAME_RATE (60)
74
75/*
76 * register SHR is limited to (SVR value + 1) x VMAX value - 4
77 */
78#define IMX274_SHR_LIMIT_CONST (4)
79
80/*
81 * Min and max sensor reset delay (microseconds)
82 */
83#define IMX274_RESET_DELAY1 (2000)
84#define IMX274_RESET_DELAY2 (2200)
85
86/*
87 * shift and mask constants
88 */
89#define IMX274_SHIFT_8_BITS (8)
90#define IMX274_SHIFT_16_BITS (16)
91#define IMX274_MASK_LSB_2_BITS (0x03)
92#define IMX274_MASK_LSB_3_BITS (0x07)
93#define IMX274_MASK_LSB_4_BITS (0x0f)
94#define IMX274_MASK_LSB_8_BITS (0x00ff)
95
96#define DRIVER_NAME "IMX274"
97
98/*
99 * IMX274 register definitions
100 */
101#define IMX274_SHR_REG_MSB 0x300D /* SHR */
102#define IMX274_SHR_REG_LSB 0x300C /* SHR */
103#define IMX274_SVR_REG_MSB 0x300F /* SVR */
104#define IMX274_SVR_REG_LSB 0x300E /* SVR */
105#define IMX274_HTRIM_EN_REG 0x3037
106#define IMX274_HTRIM_START_REG_LSB 0x3038
107#define IMX274_HTRIM_START_REG_MSB 0x3039
108#define IMX274_HTRIM_END_REG_LSB 0x303A
109#define IMX274_HTRIM_END_REG_MSB 0x303B
110#define IMX274_VWIDCUTEN_REG 0x30DD
111#define IMX274_VWIDCUT_REG_LSB 0x30DE
112#define IMX274_VWIDCUT_REG_MSB 0x30DF
113#define IMX274_VWINPOS_REG_LSB 0x30E0
114#define IMX274_VWINPOS_REG_MSB 0x30E1
115#define IMX274_WRITE_VSIZE_REG_LSB 0x3130
116#define IMX274_WRITE_VSIZE_REG_MSB 0x3131
117#define IMX274_Y_OUT_SIZE_REG_LSB 0x3132
118#define IMX274_Y_OUT_SIZE_REG_MSB 0x3133
119#define IMX274_VMAX_REG_1 0x30FA /* VMAX, MSB */
120#define IMX274_VMAX_REG_2 0x30F9 /* VMAX */
121#define IMX274_VMAX_REG_3 0x30F8 /* VMAX, LSB */
122#define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */
123#define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */
124#define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */
125#define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */
126#define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */
127#define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */
128#define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */
129#define IMX274_STANDBY_REG 0x3000 /* STANDBY */
130
131#define IMX274_TABLE_WAIT_MS 0
132#define IMX274_TABLE_END 1
133
134/*
135 * imx274 I2C operation related structure
136 */
137struct reg_8 {
138 u16 addr;
139 u8 val;
140};
141
142static const struct regmap_config imx274_regmap_config = {
143 .reg_bits = 16,
144 .val_bits = 8,
145 .cache_type = REGCACHE_RBTREE,
146};
147
148enum imx274_binning {
149 IMX274_BINNING_OFF,
150 IMX274_BINNING_2_1,
151 IMX274_BINNING_3_1,
152};
153
154/*
155 * Parameters for each imx274 readout mode.
156 *
157 * These are the values to configure the sensor in one of the
158 * implemented modes.
159 *
160 * @init_regs: registers to initialize the mode
161 * @bin_ratio: downscale factor (e.g. 3 for 3:1 binning)
162 * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
163 * Adjustment (CSI-2)" in the datasheet)
164 * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
165 * datasheet)
166 * @max_fps: Maximum frames per second
167 * @nocpiop: Number of clocks per internal offset period (see "Integration Time
168 * in Each Readout Drive Mode (CSI-2)" in the datasheet)
169 */
170struct imx274_mode {
171 const struct reg_8 *init_regs;
172 unsigned int bin_ratio;
173 int min_frame_len;
174 int min_SHR;
175 int max_fps;
176 int nocpiop;
177};
178
179/*
180 * imx274 test pattern related structure
181 */
182enum {
183 TEST_PATTERN_DISABLED = 0,
184 TEST_PATTERN_ALL_000H,
185 TEST_PATTERN_ALL_FFFH,
186 TEST_PATTERN_ALL_555H,
187 TEST_PATTERN_ALL_AAAH,
188 TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */
189 TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */
190 TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */
191 TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */
192 TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */
193 TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */
194 TEST_PATTERN_H_COLOR_BARS,
195 TEST_PATTERN_V_COLOR_BARS,
196};
197
198static const char * const tp_qmenu[] = {
199 "Disabled",
200 "All 000h Pattern",
201 "All FFFh Pattern",
202 "All 555h Pattern",
203 "All AAAh Pattern",
204 "Vertical Stripe (555h / AAAh)",
205 "Vertical Stripe (AAAh / 555h)",
206 "Vertical Stripe (000h / 555h)",
207 "Vertical Stripe (555h / 000h)",
208 "Vertical Stripe (000h / FFFh)",
209 "Vertical Stripe (FFFh / 000h)",
210 "Vertical Color Bars",
211 "Horizontal Color Bars",
212};
213
214/*
215 * All-pixel scan mode (10-bit)
216 * imx274 mode1(refer to datasheet) register configuration with
217 * 3840x2160 resolution, raw10 data and mipi four lane output
218 */
219static const struct reg_8 imx274_mode1_3840x2160_raw10[] = {
220 {0x3004, 0x01},
221 {0x3005, 0x01},
222 {0x3006, 0x00},
223 {0x3007, 0xa2},
224
225 {0x3018, 0xA2}, /* output XVS, HVS */
226
227 {0x306B, 0x05},
228 {0x30E2, 0x01},
229
230 {0x30EE, 0x01},
231 {0x3342, 0x0A},
232 {0x3343, 0x00},
233 {0x3344, 0x16},
234 {0x3345, 0x00},
235 {0x33A6, 0x01},
236 {0x3528, 0x0E},
237 {0x3554, 0x1F},
238 {0x3555, 0x01},
239 {0x3556, 0x01},
240 {0x3557, 0x01},
241 {0x3558, 0x01},
242 {0x3559, 0x00},
243 {0x355A, 0x00},
244 {0x35BA, 0x0E},
245 {0x366A, 0x1B},
246 {0x366B, 0x1A},
247 {0x366C, 0x19},
248 {0x366D, 0x17},
249 {0x3A41, 0x08},
250
251 {IMX274_TABLE_END, 0x00}
252};
253
254/*
255 * Horizontal/vertical 2/2-line binning
256 * (Horizontal and vertical weightedbinning, 10-bit)
257 * imx274 mode3(refer to datasheet) register configuration with
258 * 1920x1080 resolution, raw10 data and mipi four lane output
259 */
260static const struct reg_8 imx274_mode3_1920x1080_raw10[] = {
261 {0x3004, 0x02},
262 {0x3005, 0x21},
263 {0x3006, 0x00},
264 {0x3007, 0xb1},
265
266 {0x3018, 0xA2}, /* output XVS, HVS */
267
268 {0x306B, 0x05},
269 {0x30E2, 0x02},
270
271 {0x30EE, 0x01},
272 {0x3342, 0x0A},
273 {0x3343, 0x00},
274 {0x3344, 0x1A},
275 {0x3345, 0x00},
276 {0x33A6, 0x01},
277 {0x3528, 0x0E},
278 {0x3554, 0x00},
279 {0x3555, 0x01},
280 {0x3556, 0x01},
281 {0x3557, 0x01},
282 {0x3558, 0x01},
283 {0x3559, 0x00},
284 {0x355A, 0x00},
285 {0x35BA, 0x0E},
286 {0x366A, 0x1B},
287 {0x366B, 0x1A},
288 {0x366C, 0x19},
289 {0x366D, 0x17},
290 {0x3A41, 0x08},
291
292 {IMX274_TABLE_END, 0x00}
293};
294
295/*
296 * Vertical 2/3 subsampling binning horizontal 3 binning
297 * imx274 mode5(refer to datasheet) register configuration with
298 * 1280x720 resolution, raw10 data and mipi four lane output
299 */
300static const struct reg_8 imx274_mode5_1280x720_raw10[] = {
301 {0x3004, 0x03},
302 {0x3005, 0x31},
303 {0x3006, 0x00},
304 {0x3007, 0xa9},
305
306 {0x3018, 0xA2}, /* output XVS, HVS */
307
308 {0x306B, 0x05},
309 {0x30E2, 0x03},
310
311 {0x30EE, 0x01},
312 {0x3342, 0x0A},
313 {0x3343, 0x00},
314 {0x3344, 0x1B},
315 {0x3345, 0x00},
316 {0x33A6, 0x01},
317 {0x3528, 0x0E},
318 {0x3554, 0x00},
319 {0x3555, 0x01},
320 {0x3556, 0x01},
321 {0x3557, 0x01},
322 {0x3558, 0x01},
323 {0x3559, 0x00},
324 {0x355A, 0x00},
325 {0x35BA, 0x0E},
326 {0x366A, 0x1B},
327 {0x366B, 0x19},
328 {0x366C, 0x17},
329 {0x366D, 0x17},
330 {0x3A41, 0x04},
331
332 {IMX274_TABLE_END, 0x00}
333};
334
335/*
336 * imx274 first step register configuration for
337 * starting stream
338 */
339static const struct reg_8 imx274_start_1[] = {
340 {IMX274_STANDBY_REG, 0x12},
341
342 /* PLRD: clock settings */
343 {0x3120, 0xF0},
344 {0x3121, 0x00},
345 {0x3122, 0x02},
346 {0x3129, 0x9C},
347 {0x312A, 0x02},
348 {0x312D, 0x02},
349
350 {0x310B, 0x00},
351
352 /* PLSTMG */
353 {0x304C, 0x00}, /* PLSTMG01 */
354 {0x304D, 0x03},
355 {0x331C, 0x1A},
356 {0x331D, 0x00},
357 {0x3502, 0x02},
358 {0x3529, 0x0E},
359 {0x352A, 0x0E},
360 {0x352B, 0x0E},
361 {0x3538, 0x0E},
362 {0x3539, 0x0E},
363 {0x3553, 0x00},
364 {0x357D, 0x05},
365 {0x357F, 0x05},
366 {0x3581, 0x04},
367 {0x3583, 0x76},
368 {0x3587, 0x01},
369 {0x35BB, 0x0E},
370 {0x35BC, 0x0E},
371 {0x35BD, 0x0E},
372 {0x35BE, 0x0E},
373 {0x35BF, 0x0E},
374 {0x366E, 0x00},
375 {0x366F, 0x00},
376 {0x3670, 0x00},
377 {0x3671, 0x00},
378
379 /* PSMIPI */
380 {0x3304, 0x32}, /* PSMIPI1 */
381 {0x3305, 0x00},
382 {0x3306, 0x32},
383 {0x3307, 0x00},
384 {0x3590, 0x32},
385 {0x3591, 0x00},
386 {0x3686, 0x32},
387 {0x3687, 0x00},
388
389 {IMX274_TABLE_END, 0x00}
390};
391
392/*
393 * imx274 second step register configuration for
394 * starting stream
395 */
396static const struct reg_8 imx274_start_2[] = {
397 {IMX274_STANDBY_REG, 0x00},
398 {0x303E, 0x02}, /* SYS_MODE = 2 */
399 {IMX274_TABLE_END, 0x00}
400};
401
402/*
403 * imx274 third step register configuration for
404 * starting stream
405 */
406static const struct reg_8 imx274_start_3[] = {
407 {0x30F4, 0x00},
408 {0x3018, 0xA2}, /* XHS VHS OUTPUT */
409 {IMX274_TABLE_END, 0x00}
410};
411
412/*
413 * imx274 register configuration for stopping stream
414 */
415static const struct reg_8 imx274_stop[] = {
416 {IMX274_STANDBY_REG, 0x01},
417 {IMX274_TABLE_END, 0x00}
418};
419
420/*
421 * imx274 disable test pattern register configuration
422 */
423static const struct reg_8 imx274_tp_disabled[] = {
424 {0x303C, 0x00},
425 {0x377F, 0x00},
426 {0x3781, 0x00},
427 {0x370B, 0x00},
428 {IMX274_TABLE_END, 0x00}
429};
430
431/*
432 * imx274 test pattern register configuration
433 * reg 0x303D defines the test pattern modes
434 */
435static const struct reg_8 imx274_tp_regs[] = {
436 {0x303C, 0x11},
437 {0x370E, 0x01},
438 {0x377F, 0x01},
439 {0x3781, 0x01},
440 {0x370B, 0x11},
441 {IMX274_TABLE_END, 0x00}
442};
443
444/* nocpiop happens to be the same number for the implemented modes */
445static const struct imx274_mode imx274_modes[] = {
446 {
447 /* mode 1, 4K */
448 .bin_ratio = 1,
449 .init_regs = imx274_mode1_3840x2160_raw10,
450 .min_frame_len = 4550,
451 .min_SHR = 12,
452 .max_fps = 60,
453 .nocpiop = 112,
454 },
455 {
456 /* mode 3, 1080p */
457 .bin_ratio = 2,
458 .init_regs = imx274_mode3_1920x1080_raw10,
459 .min_frame_len = 2310,
460 .min_SHR = 8,
461 .max_fps = 120,
462 .nocpiop = 112,
463 },
464 {
465 /* mode 5, 720p */
466 .bin_ratio = 3,
467 .init_regs = imx274_mode5_1280x720_raw10,
468 .min_frame_len = 2310,
469 .min_SHR = 8,
470 .max_fps = 120,
471 .nocpiop = 112,
472 },
473};
474
475/*
476 * struct imx274_ctrls - imx274 ctrl structure
477 * @handler: V4L2 ctrl handler structure
478 * @exposure: Pointer to expsure ctrl structure
479 * @gain: Pointer to gain ctrl structure
480 * @vflip: Pointer to vflip ctrl structure
481 * @test_pattern: Pointer to test pattern ctrl structure
482 */
483struct imx274_ctrls {
484 struct v4l2_ctrl_handler handler;
485 struct v4l2_ctrl *exposure;
486 struct v4l2_ctrl *gain;
487 struct v4l2_ctrl *vflip;
488 struct v4l2_ctrl *test_pattern;
489};
490
491/*
492 * struct stim274 - imx274 device structure
493 * @sd: V4L2 subdevice structure
494 * @pad: Media pad structure
495 * @client: Pointer to I2C client
496 * @ctrls: imx274 control structure
497 * @crop: rect to be captured
498 * @compose: compose rect, i.e. output resolution
499 * @format: V4L2 media bus frame format structure
500 * (width and height are in sync with the compose rect)
501 * @frame_rate: V4L2 frame rate structure
502 * @regmap: Pointer to regmap structure
503 * @reset_gpio: Pointer to reset gpio
504 * @lock: Mutex structure
505 * @mode: Parameters for the selected readout mode
506 */
507struct stimx274 {
508 struct v4l2_subdev sd;
509 struct media_pad pad;
510 struct i2c_client *client;
511 struct imx274_ctrls ctrls;
512 struct v4l2_rect crop;
513 struct v4l2_mbus_framefmt format;
514 struct v4l2_fract frame_interval;
515 struct regmap *regmap;
516 struct gpio_desc *reset_gpio;
517 struct mutex lock; /* mutex lock for operations */
518 const struct imx274_mode *mode;
519};
520
521#define IMX274_ROUND(dim, step, flags) \
522 ((flags) & V4L2_SEL_FLAG_GE \
523 ? roundup((dim), (step)) \
524 : ((flags) & V4L2_SEL_FLAG_LE \
525 ? rounddown((dim), (step)) \
526 : rounddown((dim) + (step) / 2, (step))))
527
528/*
529 * Function declaration
530 */
531static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl);
532static int imx274_set_exposure(struct stimx274 *priv, int val);
533static int imx274_set_vflip(struct stimx274 *priv, int val);
534static int imx274_set_test_pattern(struct stimx274 *priv, int val);
535static int imx274_set_frame_interval(struct stimx274 *priv,
536 struct v4l2_fract frame_interval);
537
538static inline void msleep_range(unsigned int delay_base)
539{
540 usleep_range(delay_base * 1000, delay_base * 1000 + 500);
541}
542
543/*
544 * v4l2_ctrl and v4l2_subdev related operations
545 */
546static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
547{
548 return &container_of(ctrl->handler,
549 struct stimx274, ctrls.handler)->sd;
550}
551
552static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd)
553{
554 return container_of(sd, struct stimx274, sd);
555}
556
557/*
558 * Writing a register table
559 *
560 * @priv: Pointer to device
561 * @table: Table containing register values (with optional delays)
562 *
563 * This is used to write register table into sensor's reg map.
564 *
565 * Return: 0 on success, errors otherwise
566 */
567static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[])
568{
569 struct regmap *regmap = priv->regmap;
570 int err = 0;
571 const struct reg_8 *next;
572 u8 val;
573
574 int range_start = -1;
575 int range_count = 0;
576 u8 range_vals[16];
577 int max_range_vals = ARRAY_SIZE(range_vals);
578
579 for (next = table;; next++) {
580 if ((next->addr != range_start + range_count) ||
581 (next->addr == IMX274_TABLE_END) ||
582 (next->addr == IMX274_TABLE_WAIT_MS) ||
583 (range_count == max_range_vals)) {
584 if (range_count == 1)
585 err = regmap_write(regmap,
586 range_start, range_vals[0]);
587 else if (range_count > 1)
588 err = regmap_bulk_write(regmap, range_start,
589 &range_vals[0],
590 range_count);
591 else
592 err = 0;
593
594 if (err)
595 return err;
596
597 range_start = -1;
598 range_count = 0;
599
600 /* Handle special address values */
601 if (next->addr == IMX274_TABLE_END)
602 break;
603
604 if (next->addr == IMX274_TABLE_WAIT_MS) {
605 msleep_range(next->val);
606 continue;
607 }
608 }
609
610 val = next->val;
611
612 if (range_start == -1)
613 range_start = next->addr;
614
615 range_vals[range_count++] = val;
616 }
617 return 0;
618}
619
620static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val)
621{
622 int err;
623
624 err = regmap_write(priv->regmap, addr, val);
625 if (err)
626 dev_err(&priv->client->dev,
627 "%s : i2c write failed, %x = %x\n", __func__,
628 addr, val);
629 else
630 dev_dbg(&priv->client->dev,
631 "%s : addr 0x%x, val=0x%x\n", __func__,
632 addr, val);
633 return err;
634}
635
636/**
637 * Read a multibyte register.
638 *
639 * Uses a bulk read where possible.
640 *
641 * @priv: Pointer to device structure
642 * @addr: Address of the LSB register. Other registers must be
643 * consecutive, least-to-most significant.
644 * @val: Pointer to store the register value (cpu endianness)
645 * @nbytes: Number of bytes to read (range: [1..3]).
646 * Other bytes are zet to 0.
647 *
648 * Return: 0 on success, errors otherwise
649 */
650static int imx274_read_mbreg(struct stimx274 *priv, u16 addr, u32 *val,
651 size_t nbytes)
652{
653 __le32 val_le = 0;
654 int err;
655
656 err = regmap_bulk_read(priv->regmap, addr, &val_le, nbytes);
657 if (err) {
658 dev_err(&priv->client->dev,
659 "%s : i2c bulk read failed, %x (%zu bytes)\n",
660 __func__, addr, nbytes);
661 } else {
662 *val = le32_to_cpu(val_le);
663 dev_dbg(&priv->client->dev,
664 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
665 __func__, addr, *val, nbytes);
666 }
667
668 return err;
669}
670
671/**
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 bytes 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
700/*
701 * Set mode registers to start stream.
702 * @priv: Pointer to device structure
703 *
704 * Return: 0 on success, errors otherwise
705 */
706static int imx274_mode_regs(struct stimx274 *priv)
707{
708 int err = 0;
709
710 err = imx274_write_table(priv, imx274_start_1);
711 if (err)
712 return err;
713
714 err = imx274_write_table(priv, priv->mode->init_regs);
715
716 return err;
717}
718
719/*
720 * imx274_start_stream - Function for starting stream per mode index
721 * @priv: Pointer to device structure
722 *
723 * Return: 0 on success, errors otherwise
724 */
725static int imx274_start_stream(struct stimx274 *priv)
726{
727 int err = 0;
728
729 /*
730 * Refer to "Standby Cancel Sequence when using CSI-2" in
731 * imx274 datasheet, it should wait 10ms or more here.
732 * give it 1 extra ms for margin
733 */
734 msleep_range(11);
735 err = imx274_write_table(priv, imx274_start_2);
736 if (err)
737 return err;
738
739 /*
740 * Refer to "Standby Cancel Sequence when using CSI-2" in
741 * imx274 datasheet, it should wait 7ms or more here.
742 * give it 1 extra ms for margin
743 */
744 msleep_range(8);
745 err = imx274_write_table(priv, imx274_start_3);
746 if (err)
747 return err;
748
749 return 0;
750}
751
752/*
753 * imx274_reset - Function called to reset the sensor
754 * @priv: Pointer to device structure
755 * @rst: Input value for determining the sensor's end state after reset
756 *
757 * Set the senor in reset and then
758 * if rst = 0, keep it in reset;
759 * if rst = 1, bring it out of reset.
760 *
761 */
762static void imx274_reset(struct stimx274 *priv, int rst)
763{
764 gpiod_set_value_cansleep(priv->reset_gpio, 0);
765 usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
766 gpiod_set_value_cansleep(priv->reset_gpio, !!rst);
767 usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
768}
769
770/**
771 * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
772 * @ctrl: V4L2 control to be set
773 *
774 * This function is used to set the V4L2 controls for the imx274 sensor.
775 *
776 * Return: 0 on success, errors otherwise
777 */
778static int imx274_s_ctrl(struct v4l2_ctrl *ctrl)
779{
780 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
781 struct stimx274 *imx274 = to_imx274(sd);
782 int ret = -EINVAL;
783
784 dev_dbg(&imx274->client->dev,
785 "%s : s_ctrl: %s, value: %d\n", __func__,
786 ctrl->name, ctrl->val);
787
788 switch (ctrl->id) {
789 case V4L2_CID_EXPOSURE:
790 dev_dbg(&imx274->client->dev,
791 "%s : set V4L2_CID_EXPOSURE\n", __func__);
792 ret = imx274_set_exposure(imx274, ctrl->val);
793 break;
794
795 case V4L2_CID_GAIN:
796 dev_dbg(&imx274->client->dev,
797 "%s : set V4L2_CID_GAIN\n", __func__);
798 ret = imx274_set_gain(imx274, ctrl);
799 break;
800
801 case V4L2_CID_VFLIP:
802 dev_dbg(&imx274->client->dev,
803 "%s : set V4L2_CID_VFLIP\n", __func__);
804 ret = imx274_set_vflip(imx274, ctrl->val);
805 break;
806
807 case V4L2_CID_TEST_PATTERN:
808 dev_dbg(&imx274->client->dev,
809 "%s : set V4L2_CID_TEST_PATTERN\n", __func__);
810 ret = imx274_set_test_pattern(imx274, ctrl->val);
811 break;
812 }
813
814 return ret;
815}
816
817static int imx274_binning_goodness(struct stimx274 *imx274,
818 int w, int ask_w,
819 int h, int ask_h, u32 flags)
820{
821 struct device *dev = &imx274->client->dev;
822 const int goodness = 100000;
823 int val = 0;
824
825 if (flags & V4L2_SEL_FLAG_GE) {
826 if (w < ask_w)
827 val -= goodness;
828 if (h < ask_h)
829 val -= goodness;
830 }
831
832 if (flags & V4L2_SEL_FLAG_LE) {
833 if (w > ask_w)
834 val -= goodness;
835 if (h > ask_h)
836 val -= goodness;
837 }
838
839 val -= abs(w - ask_w);
840 val -= abs(h - ask_h);
841
842 dev_dbg(dev, "%s: ask %dx%d, size %dx%d, goodness %d\n",
843 __func__, ask_w, ask_h, w, h, val);
844
845 return val;
846}
847
848/**
849 * Helper function to change binning and set both compose and format.
850 *
851 * We have two entry points to change binning: set_fmt and
852 * set_selection(COMPOSE). Both have to compute the new output size
853 * and set it in both the compose rect and the frame format size. We
854 * also need to do the same things after setting cropping to restore
855 * 1:1 binning.
856 *
857 * This function contains the common code for these three cases, it
858 * has many arguments in order to accommodate the needs of all of
859 * them.
860 *
861 * Must be called with imx274->lock locked.
862 *
863 * @imx274: The device object
864 * @cfg: The pad config we are editing for TRY requests
865 * @which: V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller
866 * @width: Input-output parameter: set to the desired width before
867 * the call, contains the chosen value after returning successfully
868 * @height: Input-output parameter for height (see @width)
869 * @flags: Selection flags from struct v4l2_subdev_selection, or 0 if not
870 * available (when called from set_fmt)
871 */
872static int __imx274_change_compose(struct stimx274 *imx274,
873 struct v4l2_subdev_pad_config *cfg,
874 u32 which,
875 u32 *width,
876 u32 *height,
877 u32 flags)
878{
879 struct device *dev = &imx274->client->dev;
880 const struct v4l2_rect *cur_crop;
881 struct v4l2_mbus_framefmt *tgt_fmt;
882 unsigned int i;
883 const struct imx274_mode *best_mode = &imx274_modes[0];
884 int best_goodness = INT_MIN;
885
886 if (which == V4L2_SUBDEV_FORMAT_TRY) {
887 cur_crop = &cfg->try_crop;
888 tgt_fmt = &cfg->try_fmt;
889 } else {
890 cur_crop = &imx274->crop;
891 tgt_fmt = &imx274->format;
892 }
893
894 for (i = 0; i < ARRAY_SIZE(imx274_modes); i++) {
895 unsigned int ratio = imx274_modes[i].bin_ratio;
896
897 int goodness = imx274_binning_goodness(
898 imx274,
899 cur_crop->width / ratio, *width,
900 cur_crop->height / ratio, *height,
901 flags);
902
903 if (goodness >= best_goodness) {
904 best_goodness = goodness;
905 best_mode = &imx274_modes[i];
906 }
907 }
908
909 *width = cur_crop->width / best_mode->bin_ratio;
910 *height = cur_crop->height / best_mode->bin_ratio;
911
912 if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
913 imx274->mode = best_mode;
914
915 dev_dbg(dev, "%s: selected %u:1 binning\n",
916 __func__, best_mode->bin_ratio);
917
918 tgt_fmt->width = *width;
919 tgt_fmt->height = *height;
920 tgt_fmt->field = V4L2_FIELD_NONE;
921
922 return 0;
923}
924
925/**
926 * imx274_get_fmt - Get the pad format
927 * @sd: Pointer to V4L2 Sub device structure
928 * @cfg: Pointer to sub device pad information structure
929 * @fmt: Pointer to pad level media bus format
930 *
931 * This function is used to get the pad format information.
932 *
933 * Return: 0 on success
934 */
935static int imx274_get_fmt(struct v4l2_subdev *sd,
936 struct v4l2_subdev_pad_config *cfg,
937 struct v4l2_subdev_format *fmt)
938{
939 struct stimx274 *imx274 = to_imx274(sd);
940
941 mutex_lock(&imx274->lock);
942 fmt->format = imx274->format;
943 mutex_unlock(&imx274->lock);
944 return 0;
945}
946
947/**
948 * imx274_set_fmt - This is used to set the pad format
949 * @sd: Pointer to V4L2 Sub device structure
950 * @cfg: Pointer to sub device pad information structure
951 * @format: Pointer to pad level media bus format
952 *
953 * This function is used to set the pad format.
954 *
955 * Return: 0 on success
956 */
957static int imx274_set_fmt(struct v4l2_subdev *sd,
958 struct v4l2_subdev_pad_config *cfg,
959 struct v4l2_subdev_format *format)
960{
961 struct v4l2_mbus_framefmt *fmt = &format->format;
962 struct stimx274 *imx274 = to_imx274(sd);
963 int err = 0;
964
965 mutex_lock(&imx274->lock);
966
967 err = __imx274_change_compose(imx274, cfg, format->which,
968 &fmt->width, &fmt->height, 0);
969
970 if (err)
971 goto out;
972
973 /*
974 * __imx274_change_compose already set width and height in the
975 * applicable format, but we need to keep all other format
976 * values, so do a full copy here
977 */
978 fmt->field = V4L2_FIELD_NONE;
979 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
980 cfg->try_fmt = *fmt;
981 else
982 imx274->format = *fmt;
983
984out:
985 mutex_unlock(&imx274->lock);
986
987 return err;
988}
989
990static int imx274_get_selection(struct v4l2_subdev *sd,
991 struct v4l2_subdev_pad_config *cfg,
992 struct v4l2_subdev_selection *sel)
993{
994 struct stimx274 *imx274 = to_imx274(sd);
995 const struct v4l2_rect *src_crop;
996 const struct v4l2_mbus_framefmt *src_fmt;
997 int ret = 0;
998
999 if (sel->pad != 0)
1000 return -EINVAL;
1001
1002 if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
1003 sel->r.left = 0;
1004 sel->r.top = 0;
1005 sel->r.width = IMX274_MAX_WIDTH;
1006 sel->r.height = IMX274_MAX_HEIGHT;
1007 return 0;
1008 }
1009
1010 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1011 src_crop = &cfg->try_crop;
1012 src_fmt = &cfg->try_fmt;
1013 } else {
1014 src_crop = &imx274->crop;
1015 src_fmt = &imx274->format;
1016 }
1017
1018 mutex_lock(&imx274->lock);
1019
1020 switch (sel->target) {
1021 case V4L2_SEL_TGT_CROP:
1022 sel->r = *src_crop;
1023 break;
1024 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1025 sel->r.top = 0;
1026 sel->r.left = 0;
1027 sel->r.width = src_crop->width;
1028 sel->r.height = src_crop->height;
1029 break;
1030 case V4L2_SEL_TGT_COMPOSE:
1031 sel->r.top = 0;
1032 sel->r.left = 0;
1033 sel->r.width = src_fmt->width;
1034 sel->r.height = src_fmt->height;
1035 break;
1036 default:
1037 ret = -EINVAL;
1038 }
1039
1040 mutex_unlock(&imx274->lock);
1041
1042 return ret;
1043}
1044
1045static int imx274_set_selection_crop(struct stimx274 *imx274,
1046 struct v4l2_subdev_pad_config *cfg,
1047 struct v4l2_subdev_selection *sel)
1048{
1049 struct v4l2_rect *tgt_crop;
1050 struct v4l2_rect new_crop;
1051 bool size_changed;
1052
1053 /*
1054 * h_step could be 12 or 24 depending on the binning. But we
1055 * won't know the binning until we choose the mode later in
1056 * __imx274_change_compose(). Thus let's be safe and use the
1057 * most conservative value in all cases.
1058 */
1059 const u32 h_step = 24;
1060
1061 new_crop.width = min_t(u32,
1062 IMX274_ROUND(sel->r.width, h_step, sel->flags),
1063 IMX274_MAX_WIDTH);
1064
1065 /* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */
1066 if (new_crop.width < 144)
1067 new_crop.width = 144;
1068
1069 new_crop.left = min_t(u32,
1070 IMX274_ROUND(sel->r.left, h_step, 0),
1071 IMX274_MAX_WIDTH - new_crop.width);
1072
1073 new_crop.height = min_t(u32,
1074 IMX274_ROUND(sel->r.height, 2, sel->flags),
1075 IMX274_MAX_HEIGHT);
1076
1077 new_crop.top = min_t(u32, IMX274_ROUND(sel->r.top, 2, 0),
1078 IMX274_MAX_HEIGHT - new_crop.height);
1079
1080 sel->r = new_crop;
1081
1082 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1083 tgt_crop = &cfg->try_crop;
1084 else
1085 tgt_crop = &imx274->crop;
1086
1087 mutex_lock(&imx274->lock);
1088
1089 size_changed = (new_crop.width != tgt_crop->width ||
1090 new_crop.height != tgt_crop->height);
1091
1092 /* __imx274_change_compose needs the new size in *tgt_crop */
1093 *tgt_crop = new_crop;
1094
1095 /* if crop size changed then reset the output image size */
1096 if (size_changed)
1097 __imx274_change_compose(imx274, cfg, sel->which,
1098 &new_crop.width, &new_crop.height,
1099 sel->flags);
1100
1101 mutex_unlock(&imx274->lock);
1102
1103 return 0;
1104}
1105
1106static int imx274_set_selection(struct v4l2_subdev *sd,
1107 struct v4l2_subdev_pad_config *cfg,
1108 struct v4l2_subdev_selection *sel)
1109{
1110 struct stimx274 *imx274 = to_imx274(sd);
1111
1112 if (sel->pad != 0)
1113 return -EINVAL;
1114
1115 if (sel->target == V4L2_SEL_TGT_CROP)
1116 return imx274_set_selection_crop(imx274, cfg, sel);
1117
1118 if (sel->target == V4L2_SEL_TGT_COMPOSE) {
1119 int err;
1120
1121 mutex_lock(&imx274->lock);
1122 err = __imx274_change_compose(imx274, cfg, sel->which,
1123 &sel->r.width, &sel->r.height,
1124 sel->flags);
1125 mutex_unlock(&imx274->lock);
1126
1127 /*
1128 * __imx274_change_compose already set width and
1129 * height in set->r, we still need to set top-left
1130 */
1131 if (!err) {
1132 sel->r.top = 0;
1133 sel->r.left = 0;
1134 }
1135
1136 return err;
1137 }
1138
1139 return -EINVAL;
1140}
1141
1142static int imx274_apply_trimming(struct stimx274 *imx274)
1143{
1144 u32 h_start;
1145 u32 h_end;
1146 u32 hmax;
1147 u32 v_cut;
1148 s32 v_pos;
1149 u32 write_v_size;
1150 u32 y_out_size;
1151 int err;
1152
1153 h_start = imx274->crop.left + 12;
1154 h_end = h_start + imx274->crop.width;
1155
1156 /* Use the minimum allowed value of HMAX */
1157 /* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */
1158 /* Note: 260 is the minimum HMAX in all implemented modes */
1159 hmax = max_t(u32, 260, (imx274->crop.width) / 16 + 23);
1160
1161 /* invert v_pos if VFLIP */
1162 v_pos = imx274->ctrls.vflip->cur.val ?
1163 (-imx274->crop.top / 2) : (imx274->crop.top / 2);
1164 v_cut = (IMX274_MAX_HEIGHT - imx274->crop.height) / 2;
1165 write_v_size = imx274->crop.height + 22;
1166 y_out_size = imx274->crop.height + 14;
1167
1168 err = imx274_write_mbreg(imx274, IMX274_HMAX_REG_LSB, hmax, 2);
1169 if (!err)
1170 err = imx274_write_mbreg(imx274, IMX274_HTRIM_EN_REG, 1, 1);
1171 if (!err)
1172 err = imx274_write_mbreg(imx274, IMX274_HTRIM_START_REG_LSB,
1173 h_start, 2);
1174 if (!err)
1175 err = imx274_write_mbreg(imx274, IMX274_HTRIM_END_REG_LSB,
1176 h_end, 2);
1177 if (!err)
1178 err = imx274_write_mbreg(imx274, IMX274_VWIDCUTEN_REG, 1, 1);
1179 if (!err)
1180 err = imx274_write_mbreg(imx274, IMX274_VWIDCUT_REG_LSB,
1181 v_cut, 2);
1182 if (!err)
1183 err = imx274_write_mbreg(imx274, IMX274_VWINPOS_REG_LSB,
1184 v_pos, 2);
1185 if (!err)
1186 err = imx274_write_mbreg(imx274, IMX274_WRITE_VSIZE_REG_LSB,
1187 write_v_size, 2);
1188 if (!err)
1189 err = imx274_write_mbreg(imx274, IMX274_Y_OUT_SIZE_REG_LSB,
1190 y_out_size, 2);
1191
1192 return err;
1193}
1194
1195/**
1196 * imx274_g_frame_interval - Get the frame interval
1197 * @sd: Pointer to V4L2 Sub device structure
1198 * @fi: Pointer to V4l2 Sub device frame interval structure
1199 *
1200 * This function is used to get the frame interval.
1201 *
1202 * Return: 0 on success
1203 */
1204static int imx274_g_frame_interval(struct v4l2_subdev *sd,
1205 struct v4l2_subdev_frame_interval *fi)
1206{
1207 struct stimx274 *imx274 = to_imx274(sd);
1208
1209 fi->interval = imx274->frame_interval;
1210 dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n",
1211 __func__, imx274->frame_interval.numerator,
1212 imx274->frame_interval.denominator);
1213
1214 return 0;
1215}
1216
1217/**
1218 * imx274_s_frame_interval - Set the frame interval
1219 * @sd: Pointer to V4L2 Sub device structure
1220 * @fi: Pointer to V4l2 Sub device frame interval structure
1221 *
1222 * This function is used to set the frame intervavl.
1223 *
1224 * Return: 0 on success
1225 */
1226static int imx274_s_frame_interval(struct v4l2_subdev *sd,
1227 struct v4l2_subdev_frame_interval *fi)
1228{
1229 struct stimx274 *imx274 = to_imx274(sd);
1230 struct v4l2_ctrl *ctrl = imx274->ctrls.exposure;
1231 int min, max, def;
1232 int ret;
1233
1234 mutex_lock(&imx274->lock);
1235 ret = imx274_set_frame_interval(imx274, fi->interval);
1236
1237 if (!ret) {
1238 /*
1239 * exposure time range is decided by frame interval
1240 * need to update it after frame interval changes
1241 */
1242 min = IMX274_MIN_EXPOSURE_TIME;
1243 max = fi->interval.numerator * 1000000
1244 / fi->interval.denominator;
1245 def = max;
1246 if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def)) {
1247 dev_err(&imx274->client->dev,
1248 "Exposure ctrl range update failed\n");
1249 goto unlock;
1250 }
1251
1252 /* update exposure time accordingly */
1253 imx274_set_exposure(imx274, ctrl->val);
1254
1255 dev_dbg(&imx274->client->dev, "set frame interval to %uus\n",
1256 fi->interval.numerator * 1000000
1257 / fi->interval.denominator);
1258 }
1259
1260unlock:
1261 mutex_unlock(&imx274->lock);
1262
1263 return ret;
1264}
1265
1266/**
1267 * imx274_load_default - load default control values
1268 * @priv: Pointer to device structure
1269 *
1270 * Return: 0 on success, errors otherwise
1271 */
1272static int imx274_load_default(struct stimx274 *priv)
1273{
1274 int ret;
1275
1276 /* load default control values */
1277 priv->frame_interval.numerator = 1;
1278 priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1279 priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE;
1280 priv->ctrls.gain->val = IMX274_DEF_GAIN;
1281 priv->ctrls.vflip->val = 0;
1282 priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED;
1283
1284 /* update frame rate */
1285 ret = imx274_set_frame_interval(priv,
1286 priv->frame_interval);
1287 if (ret)
1288 return ret;
1289
1290 /* update exposure time */
1291 ret = v4l2_ctrl_s_ctrl(priv->ctrls.exposure, priv->ctrls.exposure->val);
1292 if (ret)
1293 return ret;
1294
1295 /* update gain */
1296 ret = v4l2_ctrl_s_ctrl(priv->ctrls.gain, priv->ctrls.gain->val);
1297 if (ret)
1298 return ret;
1299
1300 /* update vflip */
1301 ret = v4l2_ctrl_s_ctrl(priv->ctrls.vflip, priv->ctrls.vflip->val);
1302 if (ret)
1303 return ret;
1304
1305 return 0;
1306}
1307
1308/**
1309 * imx274_s_stream - It is used to start/stop the streaming.
1310 * @sd: V4L2 Sub device
1311 * @on: Flag (True / False)
1312 *
1313 * This function controls the start or stop of streaming for the
1314 * imx274 sensor.
1315 *
1316 * Return: 0 on success, errors otherwise
1317 */
1318static int imx274_s_stream(struct v4l2_subdev *sd, int on)
1319{
1320 struct stimx274 *imx274 = to_imx274(sd);
1321 int ret = 0;
1322
1323 dev_dbg(&imx274->client->dev, "%s : %s, mode index = %td\n", __func__,
1324 on ? "Stream Start" : "Stream Stop",
1325 imx274->mode - &imx274_modes[0]);
1326
1327 mutex_lock(&imx274->lock);
1328
1329 if (on) {
1330 /* load mode registers */
1331 ret = imx274_mode_regs(imx274);
1332 if (ret)
1333 goto fail;
1334
1335 ret = imx274_apply_trimming(imx274);
1336 if (ret)
1337 goto fail;
1338
1339 /*
1340 * update frame rate & expsoure. if the last mode is different,
1341 * HMAX could be changed. As the result, frame rate & exposure
1342 * are changed.
1343 * gain is not affected.
1344 */
1345 ret = imx274_set_frame_interval(imx274,
1346 imx274->frame_interval);
1347 if (ret)
1348 goto fail;
1349
1350 /* update exposure time */
1351 ret = __v4l2_ctrl_s_ctrl(imx274->ctrls.exposure,
1352 imx274->ctrls.exposure->val);
1353 if (ret)
1354 goto fail;
1355
1356 /* start stream */
1357 ret = imx274_start_stream(imx274);
1358 if (ret)
1359 goto fail;
1360 } else {
1361 /* stop stream */
1362 ret = imx274_write_table(imx274, imx274_stop);
1363 if (ret)
1364 goto fail;
1365 }
1366
1367 mutex_unlock(&imx274->lock);
1368 dev_dbg(&imx274->client->dev, "%s : Done\n", __func__);
1369 return 0;
1370
1371fail:
1372 mutex_unlock(&imx274->lock);
1373 dev_err(&imx274->client->dev, "s_stream failed\n");
1374 return ret;
1375}
1376
1377/*
1378 * imx274_get_frame_length - Function for obtaining current frame length
1379 * @priv: Pointer to device structure
1380 * @val: Pointer to obainted value
1381 *
1382 * frame_length = vmax x (svr + 1), in unit of hmax.
1383 *
1384 * Return: 0 on success
1385 */
1386static int imx274_get_frame_length(struct stimx274 *priv, u32 *val)
1387{
1388 int err;
1389 u32 svr;
1390 u32 vmax;
1391
1392 err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
1393 if (err)
1394 goto fail;
1395
1396 err = imx274_read_mbreg(priv, IMX274_VMAX_REG_3, &vmax, 3);
1397 if (err)
1398 goto fail;
1399
1400 *val = vmax * (svr + 1);
1401
1402 return 0;
1403
1404fail:
1405 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1406 return err;
1407}
1408
1409static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val,
1410 u32 *frame_length)
1411{
1412 int err;
1413
1414 err = imx274_get_frame_length(priv, frame_length);
1415 if (err)
1416 return err;
1417
1418 if (*frame_length < priv->mode->min_frame_len)
1419 *frame_length = priv->mode->min_frame_len;
1420
1421 *val = *frame_length - *val; /* convert to raw shr */
1422 if (*val > *frame_length - IMX274_SHR_LIMIT_CONST)
1423 *val = *frame_length - IMX274_SHR_LIMIT_CONST;
1424 else if (*val < priv->mode->min_SHR)
1425 *val = priv->mode->min_SHR;
1426
1427 return 0;
1428}
1429
1430/*
1431 * imx274_set_digital gain - Function called when setting digital gain
1432 * @priv: Pointer to device structure
1433 * @dgain: Value of digital gain.
1434 *
1435 * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
1436 *
1437 * Return: 0 on success
1438 */
1439static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain)
1440{
1441 u8 reg_val;
1442
1443 reg_val = ffs(dgain);
1444
1445 if (reg_val)
1446 reg_val--;
1447
1448 reg_val = clamp(reg_val, (u8)0, (u8)3);
1449
1450 return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG,
1451 reg_val & IMX274_MASK_LSB_4_BITS);
1452}
1453
1454/*
1455 * imx274_set_gain - Function called when setting gain
1456 * @priv: Pointer to device structure
1457 * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
1458 * @ctrl: v4l2 control pointer
1459 *
1460 * Set the gain based on input value.
1461 * The caller should hold the mutex lock imx274->lock if necessary
1462 *
1463 * Return: 0 on success
1464 */
1465static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl)
1466{
1467 int err;
1468 u32 gain, analog_gain, digital_gain, gain_reg;
1469
1470 gain = (u32)(ctrl->val);
1471
1472 dev_dbg(&priv->client->dev,
1473 "%s : input gain = %d.%d\n", __func__,
1474 gain >> IMX274_GAIN_SHIFT,
1475 ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT);
1476
1477 if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN)
1478 gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN;
1479 else if (gain < IMX274_MIN_GAIN)
1480 gain = IMX274_MIN_GAIN;
1481
1482 if (gain <= IMX274_MAX_ANALOG_GAIN)
1483 digital_gain = 1;
1484 else if (gain <= IMX274_MAX_ANALOG_GAIN * 2)
1485 digital_gain = 2;
1486 else if (gain <= IMX274_MAX_ANALOG_GAIN * 4)
1487 digital_gain = 4;
1488 else
1489 digital_gain = IMX274_MAX_DIGITAL_GAIN;
1490
1491 analog_gain = gain / digital_gain;
1492
1493 dev_dbg(&priv->client->dev,
1494 "%s : digital gain = %d, analog gain = %d.%d\n",
1495 __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT,
1496 ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100)
1497 >> IMX274_GAIN_SHIFT);
1498
1499 err = imx274_set_digital_gain(priv, digital_gain);
1500 if (err)
1501 goto fail;
1502
1503 /* convert to register value, refer to imx274 datasheet */
1504 gain_reg = (u32)IMX274_GAIN_CONST -
1505 (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain;
1506 if (gain_reg > IMX274_GAIN_REG_MAX)
1507 gain_reg = IMX274_GAIN_REG_MAX;
1508
1509 err = imx274_write_mbreg(priv, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg,
1510 2);
1511 if (err)
1512 goto fail;
1513
1514 if (IMX274_GAIN_CONST - gain_reg == 0) {
1515 err = -EINVAL;
1516 goto fail;
1517 }
1518
1519 /* convert register value back to gain value */
1520 ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT)
1521 / (IMX274_GAIN_CONST - gain_reg) * digital_gain;
1522
1523 dev_dbg(&priv->client->dev,
1524 "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
1525 __func__, gain_reg, ctrl->val);
1526
1527 return 0;
1528
1529fail:
1530 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1531 return err;
1532}
1533
1534/*
1535 * imx274_set_coarse_time - Function called when setting SHR value
1536 * @priv: Pointer to device structure
1537 * @val: Value for exposure time in number of line_length, or [HMAX]
1538 *
1539 * Set SHR value based on input value.
1540 *
1541 * Return: 0 on success
1542 */
1543static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val)
1544{
1545 int err;
1546 u32 coarse_time, frame_length;
1547
1548 coarse_time = *val;
1549
1550 /* convert exposure_time to appropriate SHR value */
1551 err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length);
1552 if (err)
1553 goto fail;
1554
1555 err = imx274_write_mbreg(priv, IMX274_SHR_REG_LSB, coarse_time, 2);
1556 if (err)
1557 goto fail;
1558
1559 *val = frame_length - coarse_time;
1560 return 0;
1561
1562fail:
1563 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1564 return err;
1565}
1566
1567/*
1568 * imx274_set_exposure - Function called when setting exposure time
1569 * @priv: Pointer to device structure
1570 * @val: Variable for exposure time, in the unit of micro-second
1571 *
1572 * Set exposure time based on input value.
1573 * The caller should hold the mutex lock imx274->lock if necessary
1574 *
1575 * Return: 0 on success
1576 */
1577static int imx274_set_exposure(struct stimx274 *priv, int val)
1578{
1579 int err;
1580 u32 hmax;
1581 u32 coarse_time; /* exposure time in unit of line (HMAX)*/
1582
1583 dev_dbg(&priv->client->dev,
1584 "%s : EXPOSURE control input = %d\n", __func__, val);
1585
1586 /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
1587
1588 err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
1589 if (err)
1590 goto fail;
1591
1592 if (hmax == 0) {
1593 err = -EINVAL;
1594 goto fail;
1595 }
1596
1597 coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val
1598 - priv->mode->nocpiop) / hmax;
1599
1600 /* step 2: convert exposure_time into SHR value */
1601
1602 /* set SHR */
1603 err = imx274_set_coarse_time(priv, &coarse_time);
1604 if (err)
1605 goto fail;
1606
1607 priv->ctrls.exposure->val =
1608 (coarse_time * hmax + priv->mode->nocpiop)
1609 / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2);
1610
1611 dev_dbg(&priv->client->dev,
1612 "%s : EXPOSURE control success\n", __func__);
1613 return 0;
1614
1615fail:
1616 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1617
1618 return err;
1619}
1620
1621/*
1622 * imx274_set_vflip - Function called when setting vertical flip
1623 * @priv: Pointer to device structure
1624 * @val: Value for vflip setting
1625 *
1626 * Set vertical flip based on input value.
1627 * val = 0: normal, no vertical flip
1628 * val = 1: vertical flip enabled
1629 * The caller should hold the mutex lock imx274->lock if necessary
1630 *
1631 * Return: 0 on success
1632 */
1633static int imx274_set_vflip(struct stimx274 *priv, int val)
1634{
1635 int err;
1636
1637 err = imx274_write_reg(priv, IMX274_VFLIP_REG, val);
1638 if (err) {
1639 dev_err(&priv->client->dev, "VFLIP control error\n");
1640 return err;
1641 }
1642
1643 dev_dbg(&priv->client->dev,
1644 "%s : VFLIP control success\n", __func__);
1645
1646 return 0;
1647}
1648
1649/*
1650 * imx274_set_test_pattern - Function called when setting test pattern
1651 * @priv: Pointer to device structure
1652 * @val: Variable for test pattern
1653 *
1654 * Set to different test patterns based on input value.
1655 *
1656 * Return: 0 on success
1657 */
1658static int imx274_set_test_pattern(struct stimx274 *priv, int val)
1659{
1660 int err = 0;
1661
1662 if (val == TEST_PATTERN_DISABLED) {
1663 err = imx274_write_table(priv, imx274_tp_disabled);
1664 } else if (val <= TEST_PATTERN_V_COLOR_BARS) {
1665 err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1);
1666 if (!err)
1667 err = imx274_write_table(priv, imx274_tp_regs);
1668 } else {
1669 err = -EINVAL;
1670 }
1671
1672 if (!err)
1673 dev_dbg(&priv->client->dev,
1674 "%s : TEST PATTERN control success\n", __func__);
1675 else
1676 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1677
1678 return err;
1679}
1680
1681/*
1682 * imx274_set_frame_length - Function called when setting frame length
1683 * @priv: Pointer to device structure
1684 * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
1685 *
1686 * Set frame length based on input value.
1687 *
1688 * Return: 0 on success
1689 */
1690static int imx274_set_frame_length(struct stimx274 *priv, u32 val)
1691{
1692 int err;
1693 u32 frame_length;
1694
1695 dev_dbg(&priv->client->dev, "%s : input length = %d\n",
1696 __func__, val);
1697
1698 frame_length = (u32)val;
1699
1700 err = imx274_write_mbreg(priv, IMX274_VMAX_REG_3, frame_length, 3);
1701 if (err)
1702 goto fail;
1703
1704 return 0;
1705
1706fail:
1707 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1708 return err;
1709}
1710
1711/*
1712 * imx274_set_frame_interval - Function called when setting frame interval
1713 * @priv: Pointer to device structure
1714 * @frame_interval: Variable for frame interval
1715 *
1716 * Change frame interval by updating VMAX value
1717 * The caller should hold the mutex lock imx274->lock if necessary
1718 *
1719 * Return: 0 on success
1720 */
1721static int imx274_set_frame_interval(struct stimx274 *priv,
1722 struct v4l2_fract frame_interval)
1723{
1724 int err;
1725 u32 frame_length, req_frame_rate;
1726 u32 svr;
1727 u32 hmax;
1728
1729 dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d",
1730 __func__, frame_interval.numerator,
1731 frame_interval.denominator);
1732
1733 if (frame_interval.numerator == 0) {
1734 err = -EINVAL;
1735 goto fail;
1736 }
1737
1738 req_frame_rate = (u32)(frame_interval.denominator
1739 / frame_interval.numerator);
1740
1741 /* boundary check */
1742 if (req_frame_rate > priv->mode->max_fps) {
1743 frame_interval.numerator = 1;
1744 frame_interval.denominator = priv->mode->max_fps;
1745 } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) {
1746 frame_interval.numerator = 1;
1747 frame_interval.denominator = IMX274_MIN_FRAME_RATE;
1748 }
1749
1750 /*
1751 * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
1752 * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
1753 */
1754
1755 err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
1756 if (err)
1757 goto fail;
1758
1759 dev_dbg(&priv->client->dev,
1760 "%s : register SVR = %d\n", __func__, svr);
1761
1762 err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
1763 if (err)
1764 goto fail;
1765
1766 dev_dbg(&priv->client->dev,
1767 "%s : register HMAX = %d\n", __func__, hmax);
1768
1769 if (hmax == 0 || frame_interval.denominator == 0) {
1770 err = -EINVAL;
1771 goto fail;
1772 }
1773
1774 frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax
1775 * frame_interval.numerator
1776 / frame_interval.denominator;
1777
1778 err = imx274_set_frame_length(priv, frame_length);
1779 if (err)
1780 goto fail;
1781
1782 priv->frame_interval = frame_interval;
1783 return 0;
1784
1785fail:
1786 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1787 return err;
1788}
1789
1790static const struct v4l2_subdev_pad_ops imx274_pad_ops = {
1791 .get_fmt = imx274_get_fmt,
1792 .set_fmt = imx274_set_fmt,
1793 .get_selection = imx274_get_selection,
1794 .set_selection = imx274_set_selection,
1795};
1796
1797static const struct v4l2_subdev_video_ops imx274_video_ops = {
1798 .g_frame_interval = imx274_g_frame_interval,
1799 .s_frame_interval = imx274_s_frame_interval,
1800 .s_stream = imx274_s_stream,
1801};
1802
1803static const struct v4l2_subdev_ops imx274_subdev_ops = {
1804 .pad = &imx274_pad_ops,
1805 .video = &imx274_video_ops,
1806};
1807
1808static const struct v4l2_ctrl_ops imx274_ctrl_ops = {
1809 .s_ctrl = imx274_s_ctrl,
1810};
1811
1812static const struct of_device_id imx274_of_id_table[] = {
1813 { .compatible = "sony,imx274" },
1814 { }
1815};
1816MODULE_DEVICE_TABLE(of, imx274_of_id_table);
1817
1818static const struct i2c_device_id imx274_id[] = {
1819 { "IMX274", 0 },
1820 { }
1821};
1822MODULE_DEVICE_TABLE(i2c, imx274_id);
1823
1824static int imx274_probe(struct i2c_client *client)
1825{
1826 struct v4l2_subdev *sd;
1827 struct stimx274 *imx274;
1828 int ret;
1829
1830 /* initialize imx274 */
1831 imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL);
1832 if (!imx274)
1833 return -ENOMEM;
1834
1835 mutex_init(&imx274->lock);
1836
1837 /* initialize format */
1838 imx274->mode = &imx274_modes[IMX274_DEFAULT_BINNING];
1839 imx274->crop.width = IMX274_MAX_WIDTH;
1840 imx274->crop.height = IMX274_MAX_HEIGHT;
1841 imx274->format.width = imx274->crop.width / imx274->mode->bin_ratio;
1842 imx274->format.height = imx274->crop.height / imx274->mode->bin_ratio;
1843 imx274->format.field = V4L2_FIELD_NONE;
1844 imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
1845 imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
1846 imx274->frame_interval.numerator = 1;
1847 imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1848
1849 /* initialize regmap */
1850 imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config);
1851 if (IS_ERR(imx274->regmap)) {
1852 dev_err(&client->dev,
1853 "regmap init failed: %ld\n", PTR_ERR(imx274->regmap));
1854 ret = -ENODEV;
1855 goto err_regmap;
1856 }
1857
1858 /* initialize subdevice */
1859 imx274->client = client;
1860 sd = &imx274->sd;
1861 v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops);
1862 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1863
1864 /* initialize subdev media pad */
1865 imx274->pad.flags = MEDIA_PAD_FL_SOURCE;
1866 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1867 ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad);
1868 if (ret < 0) {
1869 dev_err(&client->dev,
1870 "%s : media entity init Failed %d\n", __func__, ret);
1871 goto err_regmap;
1872 }
1873
1874 /* initialize sensor reset gpio */
1875 imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1876 GPIOD_OUT_HIGH);
1877 if (IS_ERR(imx274->reset_gpio)) {
1878 if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER)
1879 dev_err(&client->dev, "Reset GPIO not setup in DT");
1880 ret = PTR_ERR(imx274->reset_gpio);
1881 goto err_me;
1882 }
1883
1884 /* pull sensor out of reset */
1885 imx274_reset(imx274, 1);
1886
1887 /* initialize controls */
1888 ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 4);
1889 if (ret < 0) {
1890 dev_err(&client->dev,
1891 "%s : ctrl handler init Failed\n", __func__);
1892 goto err_me;
1893 }
1894
1895 imx274->ctrls.handler.lock = &imx274->lock;
1896
1897 /* add new controls */
1898 imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items(
1899 &imx274->ctrls.handler, &imx274_ctrl_ops,
1900 V4L2_CID_TEST_PATTERN,
1901 ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu);
1902
1903 imx274->ctrls.gain = v4l2_ctrl_new_std(
1904 &imx274->ctrls.handler,
1905 &imx274_ctrl_ops,
1906 V4L2_CID_GAIN, IMX274_MIN_GAIN,
1907 IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1,
1908 IMX274_DEF_GAIN);
1909
1910 imx274->ctrls.exposure = v4l2_ctrl_new_std(
1911 &imx274->ctrls.handler,
1912 &imx274_ctrl_ops,
1913 V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME,
1914 1000000 / IMX274_DEF_FRAME_RATE, 1,
1915 IMX274_MIN_EXPOSURE_TIME);
1916
1917 imx274->ctrls.vflip = v4l2_ctrl_new_std(
1918 &imx274->ctrls.handler,
1919 &imx274_ctrl_ops,
1920 V4L2_CID_VFLIP, 0, 1, 1, 0);
1921
1922 imx274->sd.ctrl_handler = &imx274->ctrls.handler;
1923 if (imx274->ctrls.handler.error) {
1924 ret = imx274->ctrls.handler.error;
1925 goto err_ctrls;
1926 }
1927
1928 /* setup default controls */
1929 ret = v4l2_ctrl_handler_setup(&imx274->ctrls.handler);
1930 if (ret) {
1931 dev_err(&client->dev,
1932 "Error %d setup default controls\n", ret);
1933 goto err_ctrls;
1934 }
1935
1936 /* load default control values */
1937 ret = imx274_load_default(imx274);
1938 if (ret) {
1939 dev_err(&client->dev,
1940 "%s : imx274_load_default failed %d\n",
1941 __func__, ret);
1942 goto err_ctrls;
1943 }
1944
1945 /* register subdevice */
1946 ret = v4l2_async_register_subdev(sd);
1947 if (ret < 0) {
1948 dev_err(&client->dev,
1949 "%s : v4l2_async_register_subdev failed %d\n",
1950 __func__, ret);
1951 goto err_ctrls;
1952 }
1953
1954 dev_info(&client->dev, "imx274 : imx274 probe success !\n");
1955 return 0;
1956
1957err_ctrls:
1958 v4l2_ctrl_handler_free(&imx274->ctrls.handler);
1959err_me:
1960 media_entity_cleanup(&sd->entity);
1961err_regmap:
1962 mutex_destroy(&imx274->lock);
1963 return ret;
1964}
1965
1966static int imx274_remove(struct i2c_client *client)
1967{
1968 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1969 struct stimx274 *imx274 = to_imx274(sd);
1970
1971 /* stop stream */
1972 imx274_write_table(imx274, imx274_stop);
1973
1974 v4l2_async_unregister_subdev(sd);
1975 v4l2_ctrl_handler_free(&imx274->ctrls.handler);
1976 media_entity_cleanup(&sd->entity);
1977 mutex_destroy(&imx274->lock);
1978 return 0;
1979}
1980
1981static struct i2c_driver imx274_i2c_driver = {
1982 .driver = {
1983 .name = DRIVER_NAME,
1984 .of_match_table = imx274_of_id_table,
1985 },
1986 .probe_new = imx274_probe,
1987 .remove = imx274_remove,
1988 .id_table = imx274_id,
1989};
1990
1991module_i2c_driver(imx274_i2c_driver);
1992
1993MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
1994MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
1995MODULE_LICENSE("GPL v2");