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