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
,
667 /* Handle special address values */
668 if (next
->addr
== end_addr
)
671 if (next
->addr
== wait_ms_addr
) {
672 msleep_range(next
->val
);
679 if (range_start
== -1)
680 range_start
= next
->addr
;
682 range_vals
[range_count
++] = val
;
687 static inline int imx274_read_reg(struct stimx274
*priv
, u16 addr
, u8
*val
)
691 err
= regmap_read(priv
->regmap
, addr
, (unsigned int *)val
);
693 dev_err(&priv
->client
->dev
,
694 "%s : i2c read failed, addr = %x\n", __func__
, addr
);
696 dev_dbg(&priv
->client
->dev
,
697 "%s : addr 0x%x, val=0x%x\n", __func__
,
702 static inline int imx274_write_reg(struct stimx274
*priv
, u16 addr
, u8 val
)
706 err
= regmap_write(priv
->regmap
, addr
, val
);
708 dev_err(&priv
->client
->dev
,
709 "%s : i2c write failed, %x = %x\n", __func__
,
712 dev_dbg(&priv
->client
->dev
,
713 "%s : addr 0x%x, val=0x%x\n", __func__
,
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
)
740 err
= imx274_write_table(priv
, mode_table
[IMX274_MODE_START_STREAM_1
]);
744 err
= imx274_write_table(priv
, mode_table
[IMX274_MODE_START_STREAM_2
]);
748 err
= imx274_write_table(priv
, mode_table
[mode
]);
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
)
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
769 err
= imx274_write_table(priv
, mode_table
[IMX274_MODE_START_STREAM_3
]);
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
779 err
= imx274_write_table(priv
, mode_table
[IMX274_MODE_START_STREAM_4
]);
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
);
818 dev_dbg(&imx274
->client
->dev
,
819 "%s : s_ctrl: %s, value: %d\n", __func__
,
820 ctrl
->name
, ctrl
->val
);
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
);
830 dev_dbg(&imx274
->client
->dev
,
831 "%s : set V4L2_CID_GAIN\n", __func__
);
832 ret
= imx274_set_gain(imx274
, ctrl
);
836 dev_dbg(&imx274
->client
->dev
,
837 "%s : set V4L2_CID_VFLIP\n", __func__
);
838 ret
= imx274_set_vflip(imx274
, ctrl
->val
);
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
);
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
);
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
;
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
)
905 if (index
>= ARRAY_SIZE(imx274_formats
)) {
906 /* default to first format */
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
)
923 imx274
->format
= *fmt
;
925 mutex_unlock(&imx274
->lock
);
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
);
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
;
968 mutex_lock(&imx274
->lock
);
969 ret
= imx274_set_frame_interval(imx274
, fi
->interval
);
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
;
980 if (__v4l2_ctrl_modify_range(ctrl
, min
, max
, 1, def
)) {
981 dev_err(&imx274
->client
->dev
,
982 "Exposure ctrl range update failed\n");
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
);
995 mutex_unlock(&imx274
->lock
);
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
)
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
);
1024 /* update exposure time */
1025 ret
= v4l2_ctrl_s_ctrl(priv
->ctrls
.exposure
, priv
->ctrls
.exposure
->val
);
1030 ret
= v4l2_ctrl_s_ctrl(priv
->ctrls
.gain
, priv
->ctrls
.gain
->val
);
1035 ret
= v4l2_ctrl_s_ctrl(priv
->ctrls
.vflip
, priv
->ctrls
.vflip
->val
);
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
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
);
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
);
1063 /* load mode registers */
1064 ret
= imx274_mode_regs(imx274
, imx274
->mode_index
);
1069 * update frame rate & expsoure. if the last mode is different,
1070 * HMAX could be changed. As the result, frame rate & exposure
1072 * gain is not affected.
1074 ret
= imx274_set_frame_interval(imx274
,
1075 imx274
->frame_interval
);
1079 /* update exposure time */
1080 ret
= __v4l2_ctrl_s_ctrl(imx274
->ctrls
.exposure
,
1081 imx274
->ctrls
.exposure
->val
);
1086 ret
= imx274_start_stream(imx274
);
1091 ret
= imx274_write_table(imx274
,
1092 mode_table
[IMX274_MODE_STOP_STREAM
]);
1097 mutex_unlock(&imx274
->lock
);
1098 dev_dbg(&imx274
->client
->dev
,
1099 "%s : Done: mode = %d\n", __func__
, imx274
->mode_index
);
1103 mutex_unlock(&imx274
->lock
);
1104 dev_err(&imx274
->client
->dev
, "s_stream failed\n");
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
)
1125 err
= imx274_read_reg(priv
, IMX274_SVR_REG_LSB
, ®_val
[0]);
1129 err
= imx274_read_reg(priv
, IMX274_SVR_REG_MSB
, ®_val
[1]);
1133 svr
= (reg_val
[1] << IMX274_SHIFT_8_BITS
) + reg_val
[0];
1136 err
= imx274_read_reg(priv
, IMX274_FRAME_LENGTH_ADDR_3
, ®_val
[0]);
1140 err
= imx274_read_reg(priv
, IMX274_FRAME_LENGTH_ADDR_2
, ®_val
[1]);
1144 err
= imx274_read_reg(priv
, IMX274_FRAME_LENGTH_ADDR_1
, ®_val
[2]);
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);
1156 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1160 static int imx274_clamp_coarse_time(struct stimx274
*priv
, u32
*val
,
1165 err
= imx274_get_frame_length(priv
, frame_length
);
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
];
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
)
1194 reg_val
= ffs(dgain
);
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];
1229 u32 gain
, analog_gain
, digital_gain
, gain_reg
;
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
)
1246 else if (gain
<= IMX274_MAX_ANALOG_GAIN
* 2)
1248 else if (gain
<= IMX274_MAX_ANALOG_GAIN
* 4)
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
);
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
,
1280 if (IMX274_GAIN_CONST
- gain_reg
== 0) {
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
);
1296 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1300 static inline void imx274_calculate_coarse_time_regs(struct reg_8 regs
[2],
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];
1323 u32 coarse_time
, frame_length
;
1328 /* convert exposure_time to appropriate SHR value */
1329 err
= imx274_clamp_coarse_time(priv
, &coarse_time
, &frame_length
);
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
,
1344 *val
= frame_length
- coarse_time
;
1348 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, 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
)
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
, ®_val
[0]);
1378 err
= imx274_read_reg(priv
, IMX274_HMAX_REG_MSB
, ®_val
[1]);
1381 hmax
= (reg_val
[1] << IMX274_SHIFT_8_BITS
) + reg_val
[0];
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 */
1393 err
= imx274_set_coarse_time(priv
, &coarse_time
);
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__
);
1406 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, 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
)
1427 err
= imx274_write_reg(priv
, IMX274_VFLIP_REG
, val
);
1429 dev_err(&priv
->client
->dev
, "VFILP control error\n");
1433 dev_dbg(&priv
->client
->dev
,
1434 "%s : VFLIP control success\n", __func__
);
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
)
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);
1457 err
= imx274_write_table(priv
, imx274_tp_regs
);
1463 dev_dbg(&priv
->client
->dev
,
1464 "%s : TEST PATTERN control success\n", __func__
);
1466 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1471 static inline void imx274_calculate_frame_length_regs(struct reg_8 regs
[3],
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];
1500 dev_dbg(&priv
->client
->dev
, "%s : input length = %d\n",
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
,
1516 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, 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
)
1534 u32 frame_length
, req_frame_rate
;
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) {
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
1567 err
= imx274_read_reg(priv
, IMX274_SVR_REG_LSB
, ®_val
[0]);
1570 err
= imx274_read_reg(priv
, IMX274_SVR_REG_MSB
, ®_val
[1]);
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
);
1578 err
= imx274_read_reg(priv
, IMX274_HMAX_REG_LSB
, ®_val
[0]);
1581 err
= imx274_read_reg(priv
, IMX274_HMAX_REG_MSB
, ®_val
[1]);
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) {
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
);
1601 priv
->frame_interval
= frame_interval
;
1605 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, 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
[] = {
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
;
1648 /* initialize imx274 */
1649 imx274
= devm_kzalloc(&client
->dev
, sizeof(*imx274
), GFP_KERNEL
);
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
));
1664 /* initialize subdevice */
1665 imx274
->client
= client
;
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
);
1676 dev_err(&client
->dev
,
1677 "%s : media entity init Failed %d\n", __func__
, ret
);
1681 /* initialize sensor reset gpio */
1682 imx274
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
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
);
1691 /* pull sensor out of reset */
1692 imx274_reset(imx274
, 1);
1694 /* initialize controls */
1695 ret
= v4l2_ctrl_handler_init(&imx274
->ctrls
.handler
, 2);
1697 dev_err(&client
->dev
,
1698 "%s : ctrl handler init Failed\n", __func__
);
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
,
1713 V4L2_CID_GAIN
, IMX274_MIN_GAIN
,
1714 IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
, 1,
1717 imx274
->ctrls
.exposure
= v4l2_ctrl_new_std(
1718 &imx274
->ctrls
.handler
,
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
,
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
;
1735 /* setup default controls */
1736 ret
= v4l2_ctrl_handler_setup(&imx274
->ctrls
.handler
);
1738 dev_err(&client
->dev
,
1739 "Error %d setup default controls\n", ret
);
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
);
1756 dev_err(&client
->dev
,
1757 "%s : imx274_load_default failed %d\n",
1762 /* register subdevice */
1763 ret
= v4l2_async_register_subdev(sd
);
1765 dev_err(&client
->dev
,
1766 "%s : v4l2_async_register_subdev failed %d\n",
1771 dev_info(&client
->dev
, "imx274 : imx274 probe success !\n");
1775 v4l2_ctrl_handler_free(&imx274
->ctrls
.handler
);
1777 media_entity_cleanup(&sd
->entity
);
1779 mutex_destroy(&imx274
->lock
);
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
);
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
);
1798 static struct i2c_driver imx274_i2c_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");