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
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
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
137 static const struct regmap_config imx274_regmap_config
= {
140 .cache_type
= REGCACHE_RBTREE
,
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
{
160 enum v4l2_colorspace colorspace
;
161 struct v4l2_frmsize_discrete size
;
162 enum imx274_mode mode
;
166 * imx274 test pattern related structure
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
[] = {
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
[] = {
211 {0x3018, 0xA2}, /* output XVS, HVS */
215 {0x30F6, 0x07}, /* HMAX, 263 */
216 {0x30F7, 0x01}, /* HMAX */
218 {0x30dd, 0x01}, /* crop to 2160 */
223 {0x3037, 0x01}, /* to crop to 3840 */
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
[] = {
269 {0x3018, 0xA2}, /* output XVS, HVS */
274 {0x30F6, 0x04}, /* HMAX, 260 */
275 {0x30F7, 0x01}, /* HMAX */
277 {0x30dd, 0x01}, /* to crop to 1920x1080 */
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
[] = {
327 {0x3018, 0xA2}, /* output XVS, HVS */
332 {0x30F6, 0x04}, /* HMAX, 260 */
333 {0x30F7, 0x01}, /* HMAX */
370 {IMX274_TABLE_END
, 0x00}
374 * imx274 first step register configuration for
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
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 */
397 {0x304C, 0x00}, /* PLSTMG01 */
424 {0x3304, 0x32}, /* PSMIPI1 */
433 {IMX274_TABLE_END
, 0x00}
437 * imx274 third step register configuration for
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
450 static const struct reg_8 imx274_start_4
[] = {
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
[] = {
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
[] = {
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
[] = {
528 8, /* mode 3, 1080p */
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)"
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
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 */
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
)
638 const struct reg_8
*next
;
641 int range_start
= -1;
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
,
665 /* Handle special address values */
666 if (next
->addr
== end_addr
)
669 if (next
->addr
== wait_ms_addr
) {
670 msleep_range(next
->val
);
677 if (range_start
== -1)
678 range_start
= next
->addr
;
680 range_vals
[range_count
++] = val
;
685 static inline int imx274_read_reg(struct stimx274
*priv
, u16 addr
, u8
*val
)
689 err
= regmap_read(priv
->regmap
, addr
, (unsigned int *)val
);
691 dev_err(&priv
->client
->dev
,
692 "%s : i2c read failed, addr = %x\n", __func__
, addr
);
694 dev_dbg(&priv
->client
->dev
,
695 "%s : addr 0x%x, val=0x%x\n", __func__
,
700 static inline int imx274_write_reg(struct stimx274
*priv
, u16 addr
, u8 val
)
704 err
= regmap_write(priv
->regmap
, addr
, val
);
706 dev_err(&priv
->client
->dev
,
707 "%s : i2c write failed, %x = %x\n", __func__
,
710 dev_dbg(&priv
->client
->dev
,
711 "%s : addr 0x%x, val=0x%x\n", __func__
,
716 static int imx274_write_table(struct stimx274
*priv
, const struct reg_8 table
[])
718 return imx274_regmap_util_write_table_8(priv
->regmap
,
719 table
, IMX274_TABLE_WAIT_MS
, IMX274_TABLE_END
);
723 * imx274_mode_regs - Function for set mode registers per mode index
724 * @priv: Pointer to device structure
725 * @mode: Mode index value
727 * This is used to start steam per mode index.
728 * mode = 0, start stream for sensor Mode 1: 4K/raw10
729 * mode = 1, start stream for sensor Mode 3: 1080p/raw10
730 * mode = 2, start stream for sensor Mode 5: 720p/raw10
732 * Return: 0 on success, errors otherwise
734 static int imx274_mode_regs(struct stimx274
*priv
, int mode
)
738 err
= imx274_write_table(priv
, mode_table
[IMX274_MODE_START_STREAM_1
]);
742 err
= imx274_write_table(priv
, mode_table
[IMX274_MODE_START_STREAM_2
]);
746 err
= imx274_write_table(priv
, mode_table
[mode
]);
752 * imx274_start_stream - Function for starting stream per mode index
753 * @priv: Pointer to device structure
755 * Return: 0 on success, errors otherwise
757 static int imx274_start_stream(struct stimx274
*priv
)
762 * Refer to "Standby Cancel Sequence when using CSI-2" in
763 * imx274 datasheet, it should wait 10ms or more here.
764 * give it 1 extra ms for margin
767 err
= imx274_write_table(priv
, mode_table
[IMX274_MODE_START_STREAM_3
]);
772 * Refer to "Standby Cancel Sequence when using CSI-2" in
773 * imx274 datasheet, it should wait 7ms or more here.
774 * give it 1 extra ms for margin
777 err
= imx274_write_table(priv
, mode_table
[IMX274_MODE_START_STREAM_4
]);
785 * imx274_reset - Function called to reset the sensor
786 * @priv: Pointer to device structure
787 * @rst: Input value for determining the sensor's end state after reset
789 * Set the senor in reset and then
790 * if rst = 0, keep it in reset;
791 * if rst = 1, bring it out of reset.
794 static void imx274_reset(struct stimx274
*priv
, int rst
)
796 gpiod_set_value_cansleep(priv
->reset_gpio
, 0);
797 usleep_range(IMX274_RESET_DELAY1
, IMX274_RESET_DELAY2
);
798 gpiod_set_value_cansleep(priv
->reset_gpio
, !!rst
);
799 usleep_range(IMX274_RESET_DELAY1
, IMX274_RESET_DELAY2
);
803 * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
804 * @ctrl: V4L2 control to be set
806 * This function is used to set the V4L2 controls for the imx274 sensor.
808 * Return: 0 on success, errors otherwise
810 static int imx274_s_ctrl(struct v4l2_ctrl
*ctrl
)
812 struct v4l2_subdev
*sd
= ctrl_to_sd(ctrl
);
813 struct stimx274
*imx274
= to_imx274(sd
);
816 dev_dbg(&imx274
->client
->dev
,
817 "%s : s_ctrl: %s, value: %d\n", __func__
,
818 ctrl
->name
, ctrl
->val
);
821 case V4L2_CID_EXPOSURE
:
822 dev_dbg(&imx274
->client
->dev
,
823 "%s : set V4L2_CID_EXPOSURE\n", __func__
);
824 ret
= imx274_set_exposure(imx274
, ctrl
->val
);
828 dev_dbg(&imx274
->client
->dev
,
829 "%s : set V4L2_CID_GAIN\n", __func__
);
830 ret
= imx274_set_gain(imx274
, ctrl
);
834 dev_dbg(&imx274
->client
->dev
,
835 "%s : set V4L2_CID_VFLIP\n", __func__
);
836 ret
= imx274_set_vflip(imx274
, ctrl
->val
);
839 case V4L2_CID_TEST_PATTERN
:
840 dev_dbg(&imx274
->client
->dev
,
841 "%s : set V4L2_CID_TEST_PATTERN\n", __func__
);
842 ret
= imx274_set_test_pattern(imx274
, ctrl
->val
);
850 * imx274_get_fmt - Get the pad format
851 * @sd: Pointer to V4L2 Sub device structure
852 * @cfg: Pointer to sub device pad information structure
853 * @fmt: Pointer to pad level media bus format
855 * This function is used to get the pad format information.
857 * Return: 0 on success
859 static int imx274_get_fmt(struct v4l2_subdev
*sd
,
860 struct v4l2_subdev_pad_config
*cfg
,
861 struct v4l2_subdev_format
*fmt
)
863 struct stimx274
*imx274
= to_imx274(sd
);
865 mutex_lock(&imx274
->lock
);
866 fmt
->format
= imx274
->format
;
867 mutex_unlock(&imx274
->lock
);
872 * imx274_set_fmt - This is used to set the pad format
873 * @sd: Pointer to V4L2 Sub device structure
874 * @cfg: Pointer to sub device pad information structure
875 * @format: Pointer to pad level media bus format
877 * This function is used to set the pad format.
879 * Return: 0 on success
881 static int imx274_set_fmt(struct v4l2_subdev
*sd
,
882 struct v4l2_subdev_pad_config
*cfg
,
883 struct v4l2_subdev_format
*format
)
885 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
886 struct stimx274
*imx274
= to_imx274(sd
);
887 struct i2c_client
*client
= imx274
->client
;
890 dev_dbg(&client
->dev
,
891 "%s: width = %d height = %d code = %d mbus_code = %d\n",
892 __func__
, fmt
->width
, fmt
->height
, fmt
->code
,
893 imx274_formats
[imx274
->mode_index
].mbus_code
);
895 mutex_lock(&imx274
->lock
);
897 for (index
= 0; index
< ARRAY_SIZE(imx274_formats
); index
++) {
898 if (imx274_formats
[index
].size
.width
== fmt
->width
&&
899 imx274_formats
[index
].size
.height
== fmt
->height
)
903 if (index
>= ARRAY_SIZE(imx274_formats
)) {
904 /* default to first format */
908 imx274
->mode_index
= index
;
910 if (fmt
->width
> IMX274_MAX_WIDTH
)
911 fmt
->width
= IMX274_MAX_WIDTH
;
912 if (fmt
->height
> IMX274_MAX_HEIGHT
)
913 fmt
->height
= IMX274_MAX_HEIGHT
;
914 fmt
->width
= fmt
->width
& (~IMX274_MASK_LSB_2_BITS
);
915 fmt
->height
= fmt
->height
& (~IMX274_MASK_LSB_2_BITS
);
916 fmt
->field
= V4L2_FIELD_NONE
;
918 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
)
921 imx274
->format
= *fmt
;
923 mutex_unlock(&imx274
->lock
);
928 * imx274_g_frame_interval - Get the frame interval
929 * @sd: Pointer to V4L2 Sub device structure
930 * @fi: Pointer to V4l2 Sub device frame interval structure
932 * This function is used to get the frame interval.
934 * Return: 0 on success
936 static int imx274_g_frame_interval(struct v4l2_subdev
*sd
,
937 struct v4l2_subdev_frame_interval
*fi
)
939 struct stimx274
*imx274
= to_imx274(sd
);
941 fi
->interval
= imx274
->frame_interval
;
942 dev_dbg(&imx274
->client
->dev
, "%s frame rate = %d / %d\n",
943 __func__
, imx274
->frame_interval
.numerator
,
944 imx274
->frame_interval
.denominator
);
950 * imx274_s_frame_interval - Set the frame interval
951 * @sd: Pointer to V4L2 Sub device structure
952 * @fi: Pointer to V4l2 Sub device frame interval structure
954 * This function is used to set the frame intervavl.
956 * Return: 0 on success
958 static int imx274_s_frame_interval(struct v4l2_subdev
*sd
,
959 struct v4l2_subdev_frame_interval
*fi
)
961 struct stimx274
*imx274
= to_imx274(sd
);
962 struct v4l2_ctrl
*ctrl
= imx274
->ctrls
.exposure
;
966 mutex_lock(&imx274
->lock
);
967 ret
= imx274_set_frame_interval(imx274
, fi
->interval
);
971 * exposure time range is decided by frame interval
972 * need to update it after frame interal changes
974 min
= IMX274_MIN_EXPOSURE_TIME
;
975 max
= fi
->interval
.numerator
* 1000000
976 / fi
->interval
.denominator
;
978 if (__v4l2_ctrl_modify_range(ctrl
, min
, max
, 1, def
)) {
979 dev_err(&imx274
->client
->dev
,
980 "Exposure ctrl range update failed\n");
984 /* update exposure time accordingly */
985 imx274_set_exposure(imx274
, imx274
->ctrls
.exposure
->val
);
987 dev_dbg(&imx274
->client
->dev
, "set frame interval to %uus\n",
988 fi
->interval
.numerator
* 1000000
989 / fi
->interval
.denominator
);
993 mutex_unlock(&imx274
->lock
);
999 * imx274_load_default - load default control values
1000 * @priv: Pointer to device structure
1002 * Return: 0 on success, errors otherwise
1004 static int imx274_load_default(struct stimx274
*priv
)
1008 /* load default control values */
1009 priv
->frame_interval
.numerator
= 1;
1010 priv
->frame_interval
.denominator
= IMX274_DEF_FRAME_RATE
;
1011 priv
->ctrls
.exposure
->val
= 1000000 / IMX274_DEF_FRAME_RATE
;
1012 priv
->ctrls
.gain
->val
= IMX274_DEF_GAIN
;
1013 priv
->ctrls
.vflip
->val
= 0;
1014 priv
->ctrls
.test_pattern
->val
= TEST_PATTERN_DISABLED
;
1016 /* update frame rate */
1017 ret
= imx274_set_frame_interval(priv
,
1018 priv
->frame_interval
);
1022 /* update exposure time */
1023 ret
= v4l2_ctrl_s_ctrl(priv
->ctrls
.exposure
, priv
->ctrls
.exposure
->val
);
1028 ret
= v4l2_ctrl_s_ctrl(priv
->ctrls
.gain
, priv
->ctrls
.gain
->val
);
1033 ret
= v4l2_ctrl_s_ctrl(priv
->ctrls
.vflip
, priv
->ctrls
.vflip
->val
);
1041 * imx274_s_stream - It is used to start/stop the streaming.
1042 * @sd: V4L2 Sub device
1043 * @on: Flag (True / False)
1045 * This function controls the start or stop of streaming for the
1048 * Return: 0 on success, errors otherwise
1050 static int imx274_s_stream(struct v4l2_subdev
*sd
, int on
)
1052 struct stimx274
*imx274
= to_imx274(sd
);
1055 dev_dbg(&imx274
->client
->dev
, "%s : %s, mode index = %d\n", __func__
,
1056 on
? "Stream Start" : "Stream Stop", imx274
->mode_index
);
1058 mutex_lock(&imx274
->lock
);
1061 /* load mode registers */
1062 ret
= imx274_mode_regs(imx274
, imx274
->mode_index
);
1067 * update frame rate & expsoure. if the last mode is different,
1068 * HMAX could be changed. As the result, frame rate & exposure
1070 * gain is not affected.
1072 ret
= imx274_set_frame_interval(imx274
,
1073 imx274
->frame_interval
);
1077 /* update exposure time */
1078 ret
= __v4l2_ctrl_s_ctrl(imx274
->ctrls
.exposure
,
1079 imx274
->ctrls
.exposure
->val
);
1084 ret
= imx274_start_stream(imx274
);
1089 ret
= imx274_write_table(imx274
,
1090 mode_table
[IMX274_MODE_STOP_STREAM
]);
1095 mutex_unlock(&imx274
->lock
);
1096 dev_dbg(&imx274
->client
->dev
,
1097 "%s : Done: mode = %d\n", __func__
, imx274
->mode_index
);
1101 mutex_unlock(&imx274
->lock
);
1102 dev_err(&imx274
->client
->dev
, "s_stream failed\n");
1107 * imx274_get_frame_length - Function for obtaining current frame length
1108 * @priv: Pointer to device structure
1109 * @val: Pointer to obainted value
1111 * frame_length = vmax x (svr + 1), in unit of hmax.
1113 * Return: 0 on success
1115 static int imx274_get_frame_length(struct stimx274
*priv
, u32
*val
)
1123 err
= imx274_read_reg(priv
, IMX274_SVR_REG_LSB
, ®_val
[0]);
1127 err
= imx274_read_reg(priv
, IMX274_SVR_REG_MSB
, ®_val
[1]);
1131 svr
= (reg_val
[1] << IMX274_SHIFT_8_BITS
) + reg_val
[0];
1134 err
= imx274_read_reg(priv
, IMX274_FRAME_LENGTH_ADDR_3
, ®_val
[0]);
1138 err
= imx274_read_reg(priv
, IMX274_FRAME_LENGTH_ADDR_2
, ®_val
[1]);
1142 err
= imx274_read_reg(priv
, IMX274_FRAME_LENGTH_ADDR_1
, ®_val
[2]);
1146 vmax
= ((reg_val
[2] & IMX274_MASK_LSB_3_BITS
) << IMX274_SHIFT_16_BITS
)
1147 + (reg_val
[1] << IMX274_SHIFT_8_BITS
) + reg_val
[0];
1149 *val
= vmax
* (svr
+ 1);
1154 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1158 static int imx274_clamp_coarse_time(struct stimx274
*priv
, u32
*val
,
1163 err
= imx274_get_frame_length(priv
, frame_length
);
1167 if (*frame_length
< min_frame_len
[priv
->mode_index
])
1168 *frame_length
= min_frame_len
[priv
->mode_index
];
1170 *val
= *frame_length
- *val
; /* convert to raw shr */
1171 if (*val
> *frame_length
- IMX274_SHR_LIMIT_CONST
)
1172 *val
= *frame_length
- IMX274_SHR_LIMIT_CONST
;
1173 else if (*val
< min_SHR
[priv
->mode_index
])
1174 *val
= min_SHR
[priv
->mode_index
];
1180 * imx274_set_digital gain - Function called when setting digital gain
1181 * @priv: Pointer to device structure
1182 * @dgain: Value of digital gain.
1184 * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
1186 * Return: 0 on success
1188 static int imx274_set_digital_gain(struct stimx274
*priv
, u32 dgain
)
1192 reg_val
= ffs(dgain
);
1197 reg_val
= clamp(reg_val
, (u8
)0, (u8
)3);
1199 return imx274_write_reg(priv
, IMX274_DIGITAL_GAIN_REG
,
1200 reg_val
& IMX274_MASK_LSB_4_BITS
);
1203 static inline void imx274_calculate_gain_regs(struct reg_8 regs
[2], u16 gain
)
1205 regs
->addr
= IMX274_ANALOG_GAIN_ADDR_MSB
;
1206 regs
->val
= (gain
>> IMX274_SHIFT_8_BITS
) & IMX274_MASK_LSB_3_BITS
;
1208 (regs
+ 1)->addr
= IMX274_ANALOG_GAIN_ADDR_LSB
;
1209 (regs
+ 1)->val
= (gain
) & IMX274_MASK_LSB_8_BITS
;
1213 * imx274_set_gain - Function called when setting gain
1214 * @priv: Pointer to device structure
1215 * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
1216 * @ctrl: v4l2 control pointer
1218 * Set the gain based on input value.
1219 * The caller should hold the mutex lock imx274->lock if necessary
1221 * Return: 0 on success
1223 static int imx274_set_gain(struct stimx274
*priv
, struct v4l2_ctrl
*ctrl
)
1225 struct reg_8 reg_list
[2];
1227 u32 gain
, analog_gain
, digital_gain
, gain_reg
;
1230 gain
= (u32
)(ctrl
->val
);
1232 dev_dbg(&priv
->client
->dev
,
1233 "%s : input gain = %d.%d\n", __func__
,
1234 gain
>> IMX274_GAIN_SHIFT
,
1235 ((gain
& IMX274_GAIN_SHIFT_MASK
) * 100) >> IMX274_GAIN_SHIFT
);
1237 if (gain
> IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
)
1238 gain
= IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
;
1239 else if (gain
< IMX274_MIN_GAIN
)
1240 gain
= IMX274_MIN_GAIN
;
1242 if (gain
<= IMX274_MAX_ANALOG_GAIN
)
1244 else if (gain
<= IMX274_MAX_ANALOG_GAIN
* 2)
1246 else if (gain
<= IMX274_MAX_ANALOG_GAIN
* 4)
1249 digital_gain
= IMX274_MAX_DIGITAL_GAIN
;
1251 analog_gain
= gain
/ digital_gain
;
1253 dev_dbg(&priv
->client
->dev
,
1254 "%s : digital gain = %d, analog gain = %d.%d\n",
1255 __func__
, digital_gain
, analog_gain
>> IMX274_GAIN_SHIFT
,
1256 ((analog_gain
& IMX274_GAIN_SHIFT_MASK
) * 100)
1257 >> IMX274_GAIN_SHIFT
);
1259 err
= imx274_set_digital_gain(priv
, digital_gain
);
1263 /* convert to register value, refer to imx274 datasheet */
1264 gain_reg
= (u32
)IMX274_GAIN_CONST
-
1265 (IMX274_GAIN_CONST
<< IMX274_GAIN_SHIFT
) / analog_gain
;
1266 if (gain_reg
> IMX274_GAIN_REG_MAX
)
1267 gain_reg
= IMX274_GAIN_REG_MAX
;
1269 imx274_calculate_gain_regs(reg_list
, (u16
)gain_reg
);
1271 for (i
= 0; i
< ARRAY_SIZE(reg_list
); i
++) {
1272 err
= imx274_write_reg(priv
, reg_list
[i
].addr
,
1278 if (IMX274_GAIN_CONST
- gain_reg
== 0) {
1283 /* convert register value back to gain value */
1284 ctrl
->val
= (IMX274_GAIN_CONST
<< IMX274_GAIN_SHIFT
)
1285 / (IMX274_GAIN_CONST
- gain_reg
) * digital_gain
;
1287 dev_dbg(&priv
->client
->dev
,
1288 "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
1289 __func__
, gain_reg
, ctrl
->val
);
1294 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1298 static inline void imx274_calculate_coarse_time_regs(struct reg_8 regs
[2],
1301 regs
->addr
= IMX274_COARSE_TIME_ADDR_MSB
;
1302 regs
->val
= (coarse_time
>> IMX274_SHIFT_8_BITS
)
1303 & IMX274_MASK_LSB_8_BITS
;
1304 (regs
+ 1)->addr
= IMX274_COARSE_TIME_ADDR_LSB
;
1305 (regs
+ 1)->val
= (coarse_time
) & IMX274_MASK_LSB_8_BITS
;
1309 * imx274_set_coarse_time - Function called when setting SHR value
1310 * @priv: Pointer to device structure
1311 * @val: Value for exposure time in number of line_length, or [HMAX]
1313 * Set SHR value based on input value.
1315 * Return: 0 on success
1317 static int imx274_set_coarse_time(struct stimx274
*priv
, u32
*val
)
1319 struct reg_8 reg_list
[2];
1321 u32 coarse_time
, frame_length
;
1326 /* convert exposure_time to appropriate SHR value */
1327 err
= imx274_clamp_coarse_time(priv
, &coarse_time
, &frame_length
);
1331 /* prepare SHR registers */
1332 imx274_calculate_coarse_time_regs(reg_list
, coarse_time
);
1334 /* write to SHR registers */
1335 for (i
= 0; i
< ARRAY_SIZE(reg_list
); i
++) {
1336 err
= imx274_write_reg(priv
, reg_list
[i
].addr
,
1342 *val
= frame_length
- coarse_time
;
1346 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1351 * imx274_set_exposure - Function called when setting exposure time
1352 * @priv: Pointer to device structure
1353 * @val: Variable for exposure time, in the unit of micro-second
1355 * Set exposure time based on input value.
1356 * The caller should hold the mutex lock imx274->lock if necessary
1358 * Return: 0 on success
1360 static int imx274_set_exposure(struct stimx274
*priv
, int val
)
1365 u32 coarse_time
; /* exposure time in unit of line (HMAX)*/
1367 dev_dbg(&priv
->client
->dev
,
1368 "%s : EXPOSURE control input = %d\n", __func__
, val
);
1370 /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
1372 /* obtain HMAX value */
1373 err
= imx274_read_reg(priv
, IMX274_HMAX_REG_LSB
, ®_val
[0]);
1376 err
= imx274_read_reg(priv
, IMX274_HMAX_REG_MSB
, ®_val
[1]);
1379 hmax
= (reg_val
[1] << IMX274_SHIFT_8_BITS
) + reg_val
[0];
1385 coarse_time
= (IMX274_PIXCLK_CONST1
/ IMX274_PIXCLK_CONST2
* val
1386 - nocpiop
[priv
->mode_index
]) / hmax
;
1388 /* step 2: convert exposure_time into SHR value */
1391 err
= imx274_set_coarse_time(priv
, &coarse_time
);
1395 priv
->ctrls
.exposure
->val
=
1396 (coarse_time
* hmax
+ nocpiop
[priv
->mode_index
])
1397 / (IMX274_PIXCLK_CONST1
/ IMX274_PIXCLK_CONST2
);
1399 dev_dbg(&priv
->client
->dev
,
1400 "%s : EXPOSURE control success\n", __func__
);
1404 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1410 * imx274_set_vflip - Function called when setting vertical flip
1411 * @priv: Pointer to device structure
1412 * @val: Value for vflip setting
1414 * Set vertical flip based on input value.
1415 * val = 0: normal, no vertical flip
1416 * val = 1: vertical flip enabled
1417 * The caller should hold the mutex lock imx274->lock if necessary
1419 * Return: 0 on success
1421 static int imx274_set_vflip(struct stimx274
*priv
, int val
)
1425 err
= imx274_write_reg(priv
, IMX274_VFLIP_REG
, val
);
1427 dev_err(&priv
->client
->dev
, "VFILP control error\n");
1431 dev_dbg(&priv
->client
->dev
,
1432 "%s : VFLIP control success\n", __func__
);
1438 * imx274_set_test_pattern - Function called when setting test pattern
1439 * @priv: Pointer to device structure
1440 * @val: Variable for test pattern
1442 * Set to different test patterns based on input value.
1444 * Return: 0 on success
1446 static int imx274_set_test_pattern(struct stimx274
*priv
, int val
)
1450 if (val
== TEST_PATTERN_DISABLED
) {
1451 err
= imx274_write_table(priv
, imx274_tp_disabled
);
1452 } else if (val
<= TEST_PATTERN_V_COLOR_BARS
) {
1453 err
= imx274_write_reg(priv
, IMX274_TEST_PATTERN_REG
, val
- 1);
1455 err
= imx274_write_table(priv
, imx274_tp_regs
);
1461 dev_dbg(&priv
->client
->dev
,
1462 "%s : TEST PATTERN control success\n", __func__
);
1464 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1469 static inline void imx274_calculate_frame_length_regs(struct reg_8 regs
[3],
1472 regs
->addr
= IMX274_FRAME_LENGTH_ADDR_1
;
1473 regs
->val
= (frame_length
>> IMX274_SHIFT_16_BITS
)
1474 & IMX274_MASK_LSB_4_BITS
;
1475 (regs
+ 1)->addr
= IMX274_FRAME_LENGTH_ADDR_2
;
1476 (regs
+ 1)->val
= (frame_length
>> IMX274_SHIFT_8_BITS
)
1477 & IMX274_MASK_LSB_8_BITS
;
1478 (regs
+ 2)->addr
= IMX274_FRAME_LENGTH_ADDR_3
;
1479 (regs
+ 2)->val
= (frame_length
) & IMX274_MASK_LSB_8_BITS
;
1483 * imx274_set_frame_length - Function called when setting frame length
1484 * @priv: Pointer to device structure
1485 * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
1487 * Set frame length based on input value.
1489 * Return: 0 on success
1491 static int imx274_set_frame_length(struct stimx274
*priv
, u32 val
)
1493 struct reg_8 reg_list
[3];
1498 dev_dbg(&priv
->client
->dev
, "%s : input length = %d\n",
1501 frame_length
= (u32
)val
;
1503 imx274_calculate_frame_length_regs(reg_list
, frame_length
);
1504 for (i
= 0; i
< ARRAY_SIZE(reg_list
); i
++) {
1505 err
= imx274_write_reg(priv
, reg_list
[i
].addr
,
1514 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1519 * imx274_set_frame_interval - Function called when setting frame interval
1520 * @priv: Pointer to device structure
1521 * @frame_interval: Variable for frame interval
1523 * Change frame interval by updating VMAX value
1524 * The caller should hold the mutex lock imx274->lock if necessary
1526 * Return: 0 on success
1528 static int imx274_set_frame_interval(struct stimx274
*priv
,
1529 struct v4l2_fract frame_interval
)
1532 u32 frame_length
, req_frame_rate
;
1537 dev_dbg(&priv
->client
->dev
, "%s: input frame interval = %d / %d",
1538 __func__
, frame_interval
.numerator
,
1539 frame_interval
.denominator
);
1541 if (frame_interval
.numerator
== 0) {
1546 req_frame_rate
= (u32
)(frame_interval
.denominator
1547 / frame_interval
.numerator
);
1549 /* boundary check */
1550 if (req_frame_rate
> max_frame_rate
[priv
->mode_index
]) {
1551 frame_interval
.numerator
= 1;
1552 frame_interval
.denominator
=
1553 max_frame_rate
[priv
->mode_index
];
1554 } else if (req_frame_rate
< IMX274_MIN_FRAME_RATE
) {
1555 frame_interval
.numerator
= 1;
1556 frame_interval
.denominator
= IMX274_MIN_FRAME_RATE
;
1560 * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
1561 * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
1565 err
= imx274_read_reg(priv
, IMX274_SVR_REG_LSB
, ®_val
[0]);
1568 err
= imx274_read_reg(priv
, IMX274_SVR_REG_MSB
, ®_val
[1]);
1571 svr
= (reg_val
[1] << IMX274_SHIFT_8_BITS
) + reg_val
[0];
1572 dev_dbg(&priv
->client
->dev
,
1573 "%s : register SVR = %d\n", __func__
, svr
);
1576 err
= imx274_read_reg(priv
, IMX274_HMAX_REG_LSB
, ®_val
[0]);
1579 err
= imx274_read_reg(priv
, IMX274_HMAX_REG_MSB
, ®_val
[1]);
1582 hmax
= (reg_val
[1] << IMX274_SHIFT_8_BITS
) + reg_val
[0];
1583 dev_dbg(&priv
->client
->dev
,
1584 "%s : register HMAX = %d\n", __func__
, hmax
);
1586 if (hmax
== 0 || frame_interval
.denominator
== 0) {
1591 frame_length
= IMX274_PIXCLK_CONST1
/ (svr
+ 1) / hmax
1592 * frame_interval
.numerator
1593 / frame_interval
.denominator
;
1595 err
= imx274_set_frame_length(priv
, frame_length
);
1599 priv
->frame_interval
= frame_interval
;
1603 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1607 static const struct v4l2_subdev_pad_ops imx274_pad_ops
= {
1608 .get_fmt
= imx274_get_fmt
,
1609 .set_fmt
= imx274_set_fmt
,
1612 static const struct v4l2_subdev_video_ops imx274_video_ops
= {
1613 .g_frame_interval
= imx274_g_frame_interval
,
1614 .s_frame_interval
= imx274_s_frame_interval
,
1615 .s_stream
= imx274_s_stream
,
1618 static const struct v4l2_subdev_ops imx274_subdev_ops
= {
1619 .pad
= &imx274_pad_ops
,
1620 .video
= &imx274_video_ops
,
1623 static const struct v4l2_ctrl_ops imx274_ctrl_ops
= {
1624 .s_ctrl
= imx274_s_ctrl
,
1627 static const struct of_device_id imx274_of_id_table
[] = {
1628 { .compatible
= "sony,imx274" },
1631 MODULE_DEVICE_TABLE(of
, imx274_of_id_table
);
1633 static const struct i2c_device_id imx274_id
[] = {
1637 MODULE_DEVICE_TABLE(i2c
, imx274_id
);
1639 static int imx274_probe(struct i2c_client
*client
,
1640 const struct i2c_device_id
*id
)
1642 struct v4l2_subdev
*sd
;
1643 struct stimx274
*imx274
;
1646 /* initialize imx274 */
1647 imx274
= devm_kzalloc(&client
->dev
, sizeof(*imx274
), GFP_KERNEL
);
1651 mutex_init(&imx274
->lock
);
1653 /* initialize regmap */
1654 imx274
->regmap
= devm_regmap_init_i2c(client
, &imx274_regmap_config
);
1655 if (IS_ERR(imx274
->regmap
)) {
1656 dev_err(&client
->dev
,
1657 "regmap init failed: %ld\n", PTR_ERR(imx274
->regmap
));
1662 /* initialize subdevice */
1663 imx274
->client
= client
;
1665 v4l2_i2c_subdev_init(sd
, client
, &imx274_subdev_ops
);
1666 strlcpy(sd
->name
, DRIVER_NAME
, sizeof(sd
->name
));
1667 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
| V4L2_SUBDEV_FL_HAS_EVENTS
;
1669 /* initialize subdev media pad */
1670 imx274
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1671 sd
->entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1672 ret
= media_entity_pads_init(&sd
->entity
, 1, &imx274
->pad
);
1674 dev_err(&client
->dev
,
1675 "%s : media entity init Failed %d\n", __func__
, ret
);
1679 /* initialize sensor reset gpio */
1680 imx274
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
1682 if (IS_ERR(imx274
->reset_gpio
)) {
1683 if (PTR_ERR(imx274
->reset_gpio
) != -EPROBE_DEFER
)
1684 dev_err(&client
->dev
, "Reset GPIO not setup in DT");
1685 ret
= PTR_ERR(imx274
->reset_gpio
);
1689 /* pull sensor out of reset */
1690 imx274_reset(imx274
, 1);
1692 /* initialize controls */
1693 ret
= v4l2_ctrl_handler_init(&imx274
->ctrls
.handler
, 2);
1695 dev_err(&client
->dev
,
1696 "%s : ctrl handler init Failed\n", __func__
);
1700 imx274
->ctrls
.handler
.lock
= &imx274
->lock
;
1702 /* add new controls */
1703 imx274
->ctrls
.test_pattern
= v4l2_ctrl_new_std_menu_items(
1704 &imx274
->ctrls
.handler
, &imx274_ctrl_ops
,
1705 V4L2_CID_TEST_PATTERN
,
1706 ARRAY_SIZE(tp_qmenu
) - 1, 0, 0, tp_qmenu
);
1708 imx274
->ctrls
.gain
= v4l2_ctrl_new_std(
1709 &imx274
->ctrls
.handler
,
1711 V4L2_CID_GAIN
, IMX274_MIN_GAIN
,
1712 IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
, 1,
1715 imx274
->ctrls
.exposure
= v4l2_ctrl_new_std(
1716 &imx274
->ctrls
.handler
,
1718 V4L2_CID_EXPOSURE
, IMX274_MIN_EXPOSURE_TIME
,
1719 1000000 / IMX274_DEF_FRAME_RATE
, 1,
1720 IMX274_MIN_EXPOSURE_TIME
);
1722 imx274
->ctrls
.vflip
= v4l2_ctrl_new_std(
1723 &imx274
->ctrls
.handler
,
1725 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1727 imx274
->sd
.ctrl_handler
= &imx274
->ctrls
.handler
;
1728 if (imx274
->ctrls
.handler
.error
) {
1729 ret
= imx274
->ctrls
.handler
.error
;
1733 /* setup default controls */
1734 ret
= v4l2_ctrl_handler_setup(&imx274
->ctrls
.handler
);
1736 dev_err(&client
->dev
,
1737 "Error %d setup default controls\n", ret
);
1741 /* initialize format */
1742 imx274
->mode_index
= IMX274_MODE_3840X2160
;
1743 imx274
->format
.width
= imx274_formats
[0].size
.width
;
1744 imx274
->format
.height
= imx274_formats
[0].size
.height
;
1745 imx274
->format
.field
= V4L2_FIELD_NONE
;
1746 imx274
->format
.code
= MEDIA_BUS_FMT_SRGGB10_1X10
;
1747 imx274
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
1748 imx274
->frame_interval
.numerator
= 1;
1749 imx274
->frame_interval
.denominator
= IMX274_DEF_FRAME_RATE
;
1751 /* load default control values */
1752 ret
= imx274_load_default(imx274
);
1754 dev_err(&client
->dev
,
1755 "%s : imx274_load_default failed %d\n",
1760 /* register subdevice */
1761 ret
= v4l2_async_register_subdev(sd
);
1763 dev_err(&client
->dev
,
1764 "%s : v4l2_async_register_subdev failed %d\n",
1769 dev_info(&client
->dev
, "imx274 : imx274 probe success !\n");
1773 v4l2_ctrl_handler_free(&imx274
->ctrls
.handler
);
1775 media_entity_cleanup(&sd
->entity
);
1777 mutex_destroy(&imx274
->lock
);
1781 static int imx274_remove(struct i2c_client
*client
)
1783 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1784 struct stimx274
*imx274
= to_imx274(sd
);
1787 imx274_write_table(imx274
, mode_table
[IMX274_MODE_STOP_STREAM
]);
1789 v4l2_async_unregister_subdev(sd
);
1790 v4l2_ctrl_handler_free(&imx274
->ctrls
.handler
);
1791 media_entity_cleanup(&sd
->entity
);
1792 mutex_destroy(&imx274
->lock
);
1796 static struct i2c_driver imx274_i2c_driver
= {
1798 .name
= DRIVER_NAME
,
1799 .of_match_table
= imx274_of_id_table
,
1801 .probe
= imx274_probe
,
1802 .remove
= imx274_remove
,
1803 .id_table
= imx274_id
,
1806 module_i2c_driver(imx274_i2c_driver
);
1808 MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
1809 MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
1810 MODULE_LICENSE("GPL v2");