1 // SPDX-License-Identifier: GPL-2.0
3 * imx274.c - IMX274 CMOS Image Sensor driver
5 * Copyright (C) 2017, Leopard Imaging, Inc.
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/i2c.h>
17 #include <linux/init.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of_gpio.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <linux/v4l2-mediabus.h>
24 #include <linux/videodev2.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-subdev.h>
31 * See "SHR, SVR Setting" in datasheet
33 #define IMX274_DEFAULT_FRAME_LENGTH (4550)
34 #define IMX274_MAX_FRAME_LENGTH (0x000fffff)
37 * See "Frame Rate Adjustment" in datasheet
39 #define IMX274_PIXCLK_CONST1 (72000000)
40 #define IMX274_PIXCLK_CONST2 (1000000)
43 * The input gain is shifted by IMX274_GAIN_SHIFT to get
44 * decimal number. The real gain is
45 * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
47 #define IMX274_GAIN_SHIFT (8)
48 #define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1)
51 * See "Analog Gain" and "Digital Gain" in datasheet
53 * max gain is calculated based on IMX274_GAIN_REG_MAX
55 #define IMX274_GAIN_REG_MAX (1957)
56 #define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT)
57 #define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\
58 / (2048 - IMX274_GAIN_REG_MAX))
59 #define IMX274_MAX_DIGITAL_GAIN (8)
60 #define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT)
61 #define IMX274_GAIN_CONST (2048) /* for gain formula */
64 * 1 line time in us = (HMAX / 72), minimal is 4 lines
66 #define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72)
68 #define IMX274_DEFAULT_BINNING IMX274_BINNING_OFF
69 #define IMX274_MAX_WIDTH (3840)
70 #define IMX274_MAX_HEIGHT (2160)
71 #define IMX274_MAX_FRAME_RATE (120)
72 #define IMX274_MIN_FRAME_RATE (5)
73 #define IMX274_DEF_FRAME_RATE (60)
76 * register SHR is limited to (SVR value + 1) x VMAX value - 4
78 #define IMX274_SHR_LIMIT_CONST (4)
81 * Min and max sensor reset delay (microseconds)
83 #define IMX274_RESET_DELAY1 (2000)
84 #define IMX274_RESET_DELAY2 (2200)
87 * shift and mask constants
89 #define IMX274_SHIFT_8_BITS (8)
90 #define IMX274_SHIFT_16_BITS (16)
91 #define IMX274_MASK_LSB_2_BITS (0x03)
92 #define IMX274_MASK_LSB_3_BITS (0x07)
93 #define IMX274_MASK_LSB_4_BITS (0x0f)
94 #define IMX274_MASK_LSB_8_BITS (0x00ff)
96 #define DRIVER_NAME "IMX274"
99 * IMX274 register definitions
101 #define IMX274_SHR_REG_MSB 0x300D /* SHR */
102 #define IMX274_SHR_REG_LSB 0x300C /* SHR */
103 #define IMX274_SVR_REG_MSB 0x300F /* SVR */
104 #define IMX274_SVR_REG_LSB 0x300E /* SVR */
105 #define IMX274_HTRIM_EN_REG 0x3037
106 #define IMX274_HTRIM_START_REG_LSB 0x3038
107 #define IMX274_HTRIM_START_REG_MSB 0x3039
108 #define IMX274_HTRIM_END_REG_LSB 0x303A
109 #define IMX274_HTRIM_END_REG_MSB 0x303B
110 #define IMX274_VWIDCUTEN_REG 0x30DD
111 #define IMX274_VWIDCUT_REG_LSB 0x30DE
112 #define IMX274_VWIDCUT_REG_MSB 0x30DF
113 #define IMX274_VWINPOS_REG_LSB 0x30E0
114 #define IMX274_VWINPOS_REG_MSB 0x30E1
115 #define IMX274_WRITE_VSIZE_REG_LSB 0x3130
116 #define IMX274_WRITE_VSIZE_REG_MSB 0x3131
117 #define IMX274_Y_OUT_SIZE_REG_LSB 0x3132
118 #define IMX274_Y_OUT_SIZE_REG_MSB 0x3133
119 #define IMX274_VMAX_REG_1 0x30FA /* VMAX, MSB */
120 #define IMX274_VMAX_REG_2 0x30F9 /* VMAX */
121 #define IMX274_VMAX_REG_3 0x30F8 /* VMAX, LSB */
122 #define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */
123 #define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */
124 #define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */
125 #define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */
126 #define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */
127 #define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */
128 #define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */
129 #define IMX274_STANDBY_REG 0x3000 /* STANDBY */
131 #define IMX274_TABLE_WAIT_MS 0
132 #define IMX274_TABLE_END 1
135 * imx274 I2C operation related structure
142 static const struct regmap_config imx274_regmap_config = {
145 .cache_type = REGCACHE_RBTREE,
148 enum imx274_binning {
155 * Parameters for each imx274 readout mode.
157 * These are the values to configure the sensor in one of the
160 * @init_regs: registers to initialize the mode
161 * @bin_ratio: downscale factor (e.g. 3 for 3:1 binning)
162 * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
163 * Adjustment (CSI-2)" in the datasheet)
164 * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
166 * @max_fps: Maximum frames per second
167 * @nocpiop: Number of clocks per internal offset period (see "Integration Time
168 * in Each Readout Drive Mode (CSI-2)" in the datasheet)
171 const struct reg_8 *init_regs;
172 unsigned int bin_ratio;
180 * imx274 test pattern related structure
183 TEST_PATTERN_DISABLED = 0,
184 TEST_PATTERN_ALL_000H,
185 TEST_PATTERN_ALL_FFFH,
186 TEST_PATTERN_ALL_555H,
187 TEST_PATTERN_ALL_AAAH,
188 TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */
189 TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */
190 TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */
191 TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */
192 TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */
193 TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */
194 TEST_PATTERN_H_COLOR_BARS,
195 TEST_PATTERN_V_COLOR_BARS,
198 static const char * const tp_qmenu[] = {
204 "Vertical Stripe (555h / AAAh)",
205 "Vertical Stripe (AAAh / 555h)",
206 "Vertical Stripe (000h / 555h)",
207 "Vertical Stripe (555h / 000h)",
208 "Vertical Stripe (000h / FFFh)",
209 "Vertical Stripe (FFFh / 000h)",
210 "Vertical Color Bars",
211 "Horizontal Color Bars",
215 * All-pixel scan mode (10-bit)
216 * imx274 mode1(refer to datasheet) register configuration with
217 * 3840x2160 resolution, raw10 data and mipi four lane output
219 static const struct reg_8 imx274_mode1_3840x2160_raw10[] = {
225 {0x3018, 0xA2}, /* output XVS, HVS */
251 {IMX274_TABLE_END, 0x00}
255 * Horizontal/vertical 2/2-line binning
256 * (Horizontal and vertical weightedbinning, 10-bit)
257 * imx274 mode3(refer to datasheet) register configuration with
258 * 1920x1080 resolution, raw10 data and mipi four lane output
260 static const struct reg_8 imx274_mode3_1920x1080_raw10[] = {
266 {0x3018, 0xA2}, /* output XVS, HVS */
292 {IMX274_TABLE_END, 0x00}
296 * Vertical 2/3 subsampling binning horizontal 3 binning
297 * imx274 mode5(refer to datasheet) register configuration with
298 * 1280x720 resolution, raw10 data and mipi four lane output
300 static const struct reg_8 imx274_mode5_1280x720_raw10[] = {
306 {0x3018, 0xA2}, /* output XVS, HVS */
332 {IMX274_TABLE_END, 0x00}
336 * imx274 first step register configuration for
339 static const struct reg_8 imx274_start_1[] = {
340 {IMX274_STANDBY_REG, 0x12},
342 /* PLRD: clock settings */
353 {0x304C, 0x00}, /* PLSTMG01 */
380 {0x3304, 0x32}, /* PSMIPI1 */
389 {IMX274_TABLE_END, 0x00}
393 * imx274 second step register configuration for
396 static const struct reg_8 imx274_start_2[] = {
397 {IMX274_STANDBY_REG, 0x00},
398 {0x303E, 0x02}, /* SYS_MODE = 2 */
399 {IMX274_TABLE_END, 0x00}
403 * imx274 third step register configuration for
406 static const struct reg_8 imx274_start_3[] = {
408 {0x3018, 0xA2}, /* XHS VHS OUTPUT */
409 {IMX274_TABLE_END, 0x00}
413 * imx274 register configuration for stopping stream
415 static const struct reg_8 imx274_stop[] = {
416 {IMX274_STANDBY_REG, 0x01},
417 {IMX274_TABLE_END, 0x00}
421 * imx274 disable test pattern register configuration
423 static const struct reg_8 imx274_tp_disabled[] = {
428 {IMX274_TABLE_END, 0x00}
432 * imx274 test pattern register configuration
433 * reg 0x303D defines the test pattern modes
435 static const struct reg_8 imx274_tp_regs[] = {
441 {IMX274_TABLE_END, 0x00}
444 /* nocpiop happens to be the same number for the implemented modes */
445 static const struct imx274_mode imx274_modes[] = {
449 .init_regs = imx274_mode1_3840x2160_raw10,
450 .min_frame_len = 4550,
458 .init_regs = imx274_mode3_1920x1080_raw10,
459 .min_frame_len = 2310,
467 .init_regs = imx274_mode5_1280x720_raw10,
468 .min_frame_len = 2310,
476 * struct imx274_ctrls - imx274 ctrl structure
477 * @handler: V4L2 ctrl handler structure
478 * @exposure: Pointer to expsure ctrl structure
479 * @gain: Pointer to gain ctrl structure
480 * @vflip: Pointer to vflip ctrl structure
481 * @test_pattern: Pointer to test pattern ctrl structure
483 struct imx274_ctrls {
484 struct v4l2_ctrl_handler handler;
485 struct v4l2_ctrl *exposure;
486 struct v4l2_ctrl *gain;
487 struct v4l2_ctrl *vflip;
488 struct v4l2_ctrl *test_pattern;
492 * struct stim274 - imx274 device structure
493 * @sd: V4L2 subdevice structure
494 * @pad: Media pad structure
495 * @client: Pointer to I2C client
496 * @ctrls: imx274 control structure
497 * @crop: rect to be captured
498 * @compose: compose rect, i.e. output resolution
499 * @format: V4L2 media bus frame format structure
500 * (width and height are in sync with the compose rect)
501 * @frame_rate: V4L2 frame rate structure
502 * @regmap: Pointer to regmap structure
503 * @reset_gpio: Pointer to reset gpio
504 * @lock: Mutex structure
505 * @mode: Parameters for the selected readout mode
508 struct v4l2_subdev sd;
509 struct media_pad pad;
510 struct i2c_client *client;
511 struct imx274_ctrls ctrls;
512 struct v4l2_rect crop;
513 struct v4l2_mbus_framefmt format;
514 struct v4l2_fract frame_interval;
515 struct regmap *regmap;
516 struct gpio_desc *reset_gpio;
517 struct mutex lock; /* mutex lock for operations */
518 const struct imx274_mode *mode;
521 #define IMX274_ROUND(dim, step, flags) \
522 ((flags) & V4L2_SEL_FLAG_GE \
523 ? roundup((dim), (step)) \
524 : ((flags) & V4L2_SEL_FLAG_LE \
525 ? rounddown((dim), (step)) \
526 : rounddown((dim) + (step) / 2, (step))))
529 * Function declaration
531 static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl);
532 static int imx274_set_exposure(struct stimx274 *priv, int val);
533 static int imx274_set_vflip(struct stimx274 *priv, int val);
534 static int imx274_set_test_pattern(struct stimx274 *priv, int val);
535 static int imx274_set_frame_interval(struct stimx274 *priv,
536 struct v4l2_fract frame_interval);
538 static inline void msleep_range(unsigned int delay_base)
540 usleep_range(delay_base * 1000, delay_base * 1000 + 500);
544 * v4l2_ctrl and v4l2_subdev related operations
546 static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
548 return &container_of(ctrl->handler,
549 struct stimx274, ctrls.handler)->sd;
552 static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd)
554 return container_of(sd, struct stimx274, sd);
558 * Writing a register table
560 * @priv: Pointer to device
561 * @table: Table containing register values (with optional delays)
563 * This is used to write register table into sensor's reg map.
565 * Return: 0 on success, errors otherwise
567 static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[])
569 struct regmap *regmap = priv->regmap;
571 const struct reg_8 *next;
574 int range_start = -1;
577 int max_range_vals = ARRAY_SIZE(range_vals);
579 for (next = table;; next++) {
580 if ((next->addr != range_start + range_count) ||
581 (next->addr == IMX274_TABLE_END) ||
582 (next->addr == IMX274_TABLE_WAIT_MS) ||
583 (range_count == max_range_vals)) {
584 if (range_count == 1)
585 err = regmap_write(regmap,
586 range_start, range_vals[0]);
587 else if (range_count > 1)
588 err = regmap_bulk_write(regmap, range_start,
600 /* Handle special address values */
601 if (next->addr == IMX274_TABLE_END)
604 if (next->addr == IMX274_TABLE_WAIT_MS) {
605 msleep_range(next->val);
612 if (range_start == -1)
613 range_start = next->addr;
615 range_vals[range_count++] = val;
620 static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val)
624 err = regmap_write(priv->regmap, addr, val);
626 dev_err(&priv->client->dev,
627 "%s : i2c write failed, %x = %x\n", __func__,
630 dev_dbg(&priv->client->dev,
631 "%s : addr 0x%x, val=0x%x\n", __func__,
637 * Read a multibyte register.
639 * Uses a bulk read where possible.
641 * @priv: Pointer to device structure
642 * @addr: Address of the LSB register. Other registers must be
643 * consecutive, least-to-most significant.
644 * @val: Pointer to store the register value (cpu endianness)
645 * @nbytes: Number of bytes to read (range: [1..3]).
646 * Other bytes are zet to 0.
648 * Return: 0 on success, errors otherwise
650 static int imx274_read_mbreg(struct stimx274 *priv, u16 addr, u32 *val,
656 err = regmap_bulk_read(priv->regmap, addr, &val_le, nbytes);
658 dev_err(&priv->client->dev,
659 "%s : i2c bulk read failed, %x (%zu bytes)\n",
660 __func__, addr, nbytes);
662 *val = le32_to_cpu(val_le);
663 dev_dbg(&priv->client->dev,
664 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
665 __func__, addr, *val, nbytes);
672 * Write a multibyte register.
674 * Uses a bulk write where possible.
676 * @priv: Pointer to device structure
677 * @addr: Address of the LSB register. Other registers must be
678 * consecutive, least-to-most significant.
679 * @val: Value to be written to the register (cpu endianness)
680 * @nbytes: Number of bytes to write (range: [1..3])
682 static int imx274_write_mbreg(struct stimx274 *priv, u16 addr, u32 val,
685 __le32 val_le = cpu_to_le32(val);
688 err = regmap_bulk_write(priv->regmap, addr, &val_le, nbytes);
690 dev_err(&priv->client->dev,
691 "%s : i2c bulk write failed, %x = %x (%zu bytes)\n",
692 __func__, addr, val, nbytes);
694 dev_dbg(&priv->client->dev,
695 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
696 __func__, addr, val, nbytes);
701 * Set mode registers to start stream.
702 * @priv: Pointer to device structure
704 * Return: 0 on success, errors otherwise
706 static int imx274_mode_regs(struct stimx274 *priv)
710 err = imx274_write_table(priv, imx274_start_1);
714 err = imx274_write_table(priv, priv->mode->init_regs);
720 * imx274_start_stream - Function for starting stream per mode index
721 * @priv: Pointer to device structure
723 * Return: 0 on success, errors otherwise
725 static int imx274_start_stream(struct stimx274 *priv)
730 * Refer to "Standby Cancel Sequence when using CSI-2" in
731 * imx274 datasheet, it should wait 10ms or more here.
732 * give it 1 extra ms for margin
735 err = imx274_write_table(priv, imx274_start_2);
740 * Refer to "Standby Cancel Sequence when using CSI-2" in
741 * imx274 datasheet, it should wait 7ms or more here.
742 * give it 1 extra ms for margin
745 err = imx274_write_table(priv, imx274_start_3);
753 * imx274_reset - Function called to reset the sensor
754 * @priv: Pointer to device structure
755 * @rst: Input value for determining the sensor's end state after reset
757 * Set the senor in reset and then
758 * if rst = 0, keep it in reset;
759 * if rst = 1, bring it out of reset.
762 static void imx274_reset(struct stimx274 *priv, int rst)
764 gpiod_set_value_cansleep(priv->reset_gpio, 0);
765 usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
766 gpiod_set_value_cansleep(priv->reset_gpio, !!rst);
767 usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2);
771 * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
772 * @ctrl: V4L2 control to be set
774 * This function is used to set the V4L2 controls for the imx274 sensor.
776 * Return: 0 on success, errors otherwise
778 static int imx274_s_ctrl(struct v4l2_ctrl *ctrl)
780 struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
781 struct stimx274 *imx274 = to_imx274(sd);
784 dev_dbg(&imx274->client->dev,
785 "%s : s_ctrl: %s, value: %d\n", __func__,
786 ctrl->name, ctrl->val);
789 case V4L2_CID_EXPOSURE:
790 dev_dbg(&imx274->client->dev,
791 "%s : set V4L2_CID_EXPOSURE\n", __func__);
792 ret = imx274_set_exposure(imx274, ctrl->val);
796 dev_dbg(&imx274->client->dev,
797 "%s : set V4L2_CID_GAIN\n", __func__);
798 ret = imx274_set_gain(imx274, ctrl);
802 dev_dbg(&imx274->client->dev,
803 "%s : set V4L2_CID_VFLIP\n", __func__);
804 ret = imx274_set_vflip(imx274, ctrl->val);
807 case V4L2_CID_TEST_PATTERN:
808 dev_dbg(&imx274->client->dev,
809 "%s : set V4L2_CID_TEST_PATTERN\n", __func__);
810 ret = imx274_set_test_pattern(imx274, ctrl->val);
817 static int imx274_binning_goodness(struct stimx274 *imx274,
819 int h, int ask_h, u32 flags)
821 struct device *dev = &imx274->client->dev;
822 const int goodness = 100000;
825 if (flags & V4L2_SEL_FLAG_GE) {
832 if (flags & V4L2_SEL_FLAG_LE) {
839 val -= abs(w - ask_w);
840 val -= abs(h - ask_h);
842 dev_dbg(dev, "%s: ask %dx%d, size %dx%d, goodness %d\n",
843 __func__, ask_w, ask_h, w, h, val);
849 * Helper function to change binning and set both compose and format.
851 * We have two entry points to change binning: set_fmt and
852 * set_selection(COMPOSE). Both have to compute the new output size
853 * and set it in both the compose rect and the frame format size. We
854 * also need to do the same things after setting cropping to restore
857 * This function contains the common code for these three cases, it
858 * has many arguments in order to accommodate the needs of all of
861 * Must be called with imx274->lock locked.
863 * @imx274: The device object
864 * @cfg: The pad config we are editing for TRY requests
865 * @which: V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller
866 * @width: Input-output parameter: set to the desired width before
867 * the call, contains the chosen value after returning successfully
868 * @height: Input-output parameter for height (see @width)
869 * @flags: Selection flags from struct v4l2_subdev_selection, or 0 if not
870 * available (when called from set_fmt)
872 static int __imx274_change_compose(struct stimx274 *imx274,
873 struct v4l2_subdev_pad_config *cfg,
879 struct device *dev = &imx274->client->dev;
880 const struct v4l2_rect *cur_crop;
881 struct v4l2_mbus_framefmt *tgt_fmt;
883 const struct imx274_mode *best_mode = &imx274_modes[0];
884 int best_goodness = INT_MIN;
886 if (which == V4L2_SUBDEV_FORMAT_TRY) {
887 cur_crop = &cfg->try_crop;
888 tgt_fmt = &cfg->try_fmt;
890 cur_crop = &imx274->crop;
891 tgt_fmt = &imx274->format;
894 for (i = 0; i < ARRAY_SIZE(imx274_modes); i++) {
895 unsigned int ratio = imx274_modes[i].bin_ratio;
897 int goodness = imx274_binning_goodness(
899 cur_crop->width / ratio, *width,
900 cur_crop->height / ratio, *height,
903 if (goodness >= best_goodness) {
904 best_goodness = goodness;
905 best_mode = &imx274_modes[i];
909 *width = cur_crop->width / best_mode->bin_ratio;
910 *height = cur_crop->height / best_mode->bin_ratio;
912 if (which == V4L2_SUBDEV_FORMAT_ACTIVE)
913 imx274->mode = best_mode;
915 dev_dbg(dev, "%s: selected %u:1 binning\n",
916 __func__, best_mode->bin_ratio);
918 tgt_fmt->width = *width;
919 tgt_fmt->height = *height;
920 tgt_fmt->field = V4L2_FIELD_NONE;
926 * imx274_get_fmt - Get the pad format
927 * @sd: Pointer to V4L2 Sub device structure
928 * @cfg: Pointer to sub device pad information structure
929 * @fmt: Pointer to pad level media bus format
931 * This function is used to get the pad format information.
933 * Return: 0 on success
935 static int imx274_get_fmt(struct v4l2_subdev *sd,
936 struct v4l2_subdev_pad_config *cfg,
937 struct v4l2_subdev_format *fmt)
939 struct stimx274 *imx274 = to_imx274(sd);
941 mutex_lock(&imx274->lock);
942 fmt->format = imx274->format;
943 mutex_unlock(&imx274->lock);
948 * imx274_set_fmt - This is used to set the pad format
949 * @sd: Pointer to V4L2 Sub device structure
950 * @cfg: Pointer to sub device pad information structure
951 * @format: Pointer to pad level media bus format
953 * This function is used to set the pad format.
955 * Return: 0 on success
957 static int imx274_set_fmt(struct v4l2_subdev *sd,
958 struct v4l2_subdev_pad_config *cfg,
959 struct v4l2_subdev_format *format)
961 struct v4l2_mbus_framefmt *fmt = &format->format;
962 struct stimx274 *imx274 = to_imx274(sd);
965 mutex_lock(&imx274->lock);
967 err = __imx274_change_compose(imx274, cfg, format->which,
968 &fmt->width, &fmt->height, 0);
974 * __imx274_change_compose already set width and height in the
975 * applicable format, but we need to keep all other format
976 * values, so do a full copy here
978 fmt->field = V4L2_FIELD_NONE;
979 if (format->which == V4L2_SUBDEV_FORMAT_TRY)
982 imx274->format = *fmt;
985 mutex_unlock(&imx274->lock);
990 static int imx274_get_selection(struct v4l2_subdev *sd,
991 struct v4l2_subdev_pad_config *cfg,
992 struct v4l2_subdev_selection *sel)
994 struct stimx274 *imx274 = to_imx274(sd);
995 const struct v4l2_rect *src_crop;
996 const struct v4l2_mbus_framefmt *src_fmt;
1002 if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) {
1005 sel->r.width = IMX274_MAX_WIDTH;
1006 sel->r.height = IMX274_MAX_HEIGHT;
1010 if (sel->which == V4L2_SUBDEV_FORMAT_TRY) {
1011 src_crop = &cfg->try_crop;
1012 src_fmt = &cfg->try_fmt;
1014 src_crop = &imx274->crop;
1015 src_fmt = &imx274->format;
1018 mutex_lock(&imx274->lock);
1020 switch (sel->target) {
1021 case V4L2_SEL_TGT_CROP:
1024 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1027 sel->r.width = src_crop->width;
1028 sel->r.height = src_crop->height;
1030 case V4L2_SEL_TGT_COMPOSE:
1033 sel->r.width = src_fmt->width;
1034 sel->r.height = src_fmt->height;
1040 mutex_unlock(&imx274->lock);
1045 static int imx274_set_selection_crop(struct stimx274 *imx274,
1046 struct v4l2_subdev_pad_config *cfg,
1047 struct v4l2_subdev_selection *sel)
1049 struct v4l2_rect *tgt_crop;
1050 struct v4l2_rect new_crop;
1054 * h_step could be 12 or 24 depending on the binning. But we
1055 * won't know the binning until we choose the mode later in
1056 * __imx274_change_compose(). Thus let's be safe and use the
1057 * most conservative value in all cases.
1059 const u32 h_step = 24;
1061 new_crop.width = min_t(u32,
1062 IMX274_ROUND(sel->r.width, h_step, sel->flags),
1065 /* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */
1066 if (new_crop.width < 144)
1067 new_crop.width = 144;
1069 new_crop.left = min_t(u32,
1070 IMX274_ROUND(sel->r.left, h_step, 0),
1071 IMX274_MAX_WIDTH - new_crop.width);
1073 new_crop.height = min_t(u32,
1074 IMX274_ROUND(sel->r.height, 2, sel->flags),
1077 new_crop.top = min_t(u32, IMX274_ROUND(sel->r.top, 2, 0),
1078 IMX274_MAX_HEIGHT - new_crop.height);
1082 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
1083 tgt_crop = &cfg->try_crop;
1085 tgt_crop = &imx274->crop;
1087 mutex_lock(&imx274->lock);
1089 size_changed = (new_crop.width != tgt_crop->width ||
1090 new_crop.height != tgt_crop->height);
1092 /* __imx274_change_compose needs the new size in *tgt_crop */
1093 *tgt_crop = new_crop;
1095 /* if crop size changed then reset the output image size */
1097 __imx274_change_compose(imx274, cfg, sel->which,
1098 &new_crop.width, &new_crop.height,
1101 mutex_unlock(&imx274->lock);
1106 static int imx274_set_selection(struct v4l2_subdev *sd,
1107 struct v4l2_subdev_pad_config *cfg,
1108 struct v4l2_subdev_selection *sel)
1110 struct stimx274 *imx274 = to_imx274(sd);
1115 if (sel->target == V4L2_SEL_TGT_CROP)
1116 return imx274_set_selection_crop(imx274, cfg, sel);
1118 if (sel->target == V4L2_SEL_TGT_COMPOSE) {
1121 mutex_lock(&imx274->lock);
1122 err = __imx274_change_compose(imx274, cfg, sel->which,
1123 &sel->r.width, &sel->r.height,
1125 mutex_unlock(&imx274->lock);
1128 * __imx274_change_compose already set width and
1129 * height in set->r, we still need to set top-left
1142 static int imx274_apply_trimming(struct stimx274 *imx274)
1153 h_start = imx274->crop.left + 12;
1154 h_end = h_start + imx274->crop.width;
1156 /* Use the minimum allowed value of HMAX */
1157 /* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */
1158 /* Note: 260 is the minimum HMAX in all implemented modes */
1159 hmax = max_t(u32, 260, (imx274->crop.width) / 16 + 23);
1161 /* invert v_pos if VFLIP */
1162 v_pos = imx274->ctrls.vflip->cur.val ?
1163 (-imx274->crop.top / 2) : (imx274->crop.top / 2);
1164 v_cut = (IMX274_MAX_HEIGHT - imx274->crop.height) / 2;
1165 write_v_size = imx274->crop.height + 22;
1166 y_out_size = imx274->crop.height + 14;
1168 err = imx274_write_mbreg(imx274, IMX274_HMAX_REG_LSB, hmax, 2);
1170 err = imx274_write_mbreg(imx274, IMX274_HTRIM_EN_REG, 1, 1);
1172 err = imx274_write_mbreg(imx274, IMX274_HTRIM_START_REG_LSB,
1175 err = imx274_write_mbreg(imx274, IMX274_HTRIM_END_REG_LSB,
1178 err = imx274_write_mbreg(imx274, IMX274_VWIDCUTEN_REG, 1, 1);
1180 err = imx274_write_mbreg(imx274, IMX274_VWIDCUT_REG_LSB,
1183 err = imx274_write_mbreg(imx274, IMX274_VWINPOS_REG_LSB,
1186 err = imx274_write_mbreg(imx274, IMX274_WRITE_VSIZE_REG_LSB,
1189 err = imx274_write_mbreg(imx274, IMX274_Y_OUT_SIZE_REG_LSB,
1196 * imx274_g_frame_interval - Get the frame interval
1197 * @sd: Pointer to V4L2 Sub device structure
1198 * @fi: Pointer to V4l2 Sub device frame interval structure
1200 * This function is used to get the frame interval.
1202 * Return: 0 on success
1204 static int imx274_g_frame_interval(struct v4l2_subdev *sd,
1205 struct v4l2_subdev_frame_interval *fi)
1207 struct stimx274 *imx274 = to_imx274(sd);
1209 fi->interval = imx274->frame_interval;
1210 dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n",
1211 __func__, imx274->frame_interval.numerator,
1212 imx274->frame_interval.denominator);
1218 * imx274_s_frame_interval - Set the frame interval
1219 * @sd: Pointer to V4L2 Sub device structure
1220 * @fi: Pointer to V4l2 Sub device frame interval structure
1222 * This function is used to set the frame intervavl.
1224 * Return: 0 on success
1226 static int imx274_s_frame_interval(struct v4l2_subdev *sd,
1227 struct v4l2_subdev_frame_interval *fi)
1229 struct stimx274 *imx274 = to_imx274(sd);
1230 struct v4l2_ctrl *ctrl = imx274->ctrls.exposure;
1234 mutex_lock(&imx274->lock);
1235 ret = imx274_set_frame_interval(imx274, fi->interval);
1239 * exposure time range is decided by frame interval
1240 * need to update it after frame interval changes
1242 min = IMX274_MIN_EXPOSURE_TIME;
1243 max = fi->interval.numerator * 1000000
1244 / fi->interval.denominator;
1246 if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def)) {
1247 dev_err(&imx274->client->dev,
1248 "Exposure ctrl range update failed\n");
1252 /* update exposure time accordingly */
1253 imx274_set_exposure(imx274, ctrl->val);
1255 dev_dbg(&imx274->client->dev, "set frame interval to %uus\n",
1256 fi->interval.numerator * 1000000
1257 / fi->interval.denominator);
1261 mutex_unlock(&imx274->lock);
1267 * imx274_load_default - load default control values
1268 * @priv: Pointer to device structure
1270 * Return: 0 on success, errors otherwise
1272 static int imx274_load_default(struct stimx274 *priv)
1276 /* load default control values */
1277 priv->frame_interval.numerator = 1;
1278 priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1279 priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE;
1280 priv->ctrls.gain->val = IMX274_DEF_GAIN;
1281 priv->ctrls.vflip->val = 0;
1282 priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED;
1284 /* update frame rate */
1285 ret = imx274_set_frame_interval(priv,
1286 priv->frame_interval);
1290 /* update exposure time */
1291 ret = v4l2_ctrl_s_ctrl(priv->ctrls.exposure, priv->ctrls.exposure->val);
1296 ret = v4l2_ctrl_s_ctrl(priv->ctrls.gain, priv->ctrls.gain->val);
1301 ret = v4l2_ctrl_s_ctrl(priv->ctrls.vflip, priv->ctrls.vflip->val);
1309 * imx274_s_stream - It is used to start/stop the streaming.
1310 * @sd: V4L2 Sub device
1311 * @on: Flag (True / False)
1313 * This function controls the start or stop of streaming for the
1316 * Return: 0 on success, errors otherwise
1318 static int imx274_s_stream(struct v4l2_subdev *sd, int on)
1320 struct stimx274 *imx274 = to_imx274(sd);
1323 dev_dbg(&imx274->client->dev, "%s : %s, mode index = %td\n", __func__,
1324 on ? "Stream Start" : "Stream Stop",
1325 imx274->mode - &imx274_modes[0]);
1327 mutex_lock(&imx274->lock);
1330 /* load mode registers */
1331 ret = imx274_mode_regs(imx274);
1335 ret = imx274_apply_trimming(imx274);
1340 * update frame rate & expsoure. if the last mode is different,
1341 * HMAX could be changed. As the result, frame rate & exposure
1343 * gain is not affected.
1345 ret = imx274_set_frame_interval(imx274,
1346 imx274->frame_interval);
1350 /* update exposure time */
1351 ret = __v4l2_ctrl_s_ctrl(imx274->ctrls.exposure,
1352 imx274->ctrls.exposure->val);
1357 ret = imx274_start_stream(imx274);
1362 ret = imx274_write_table(imx274, imx274_stop);
1367 mutex_unlock(&imx274->lock);
1368 dev_dbg(&imx274->client->dev, "%s : Done\n", __func__);
1372 mutex_unlock(&imx274->lock);
1373 dev_err(&imx274->client->dev, "s_stream failed\n");
1378 * imx274_get_frame_length - Function for obtaining current frame length
1379 * @priv: Pointer to device structure
1380 * @val: Pointer to obainted value
1382 * frame_length = vmax x (svr + 1), in unit of hmax.
1384 * Return: 0 on success
1386 static int imx274_get_frame_length(struct stimx274 *priv, u32 *val)
1392 err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
1396 err = imx274_read_mbreg(priv, IMX274_VMAX_REG_3, &vmax, 3);
1400 *val = vmax * (svr + 1);
1405 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1409 static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val,
1414 err = imx274_get_frame_length(priv, frame_length);
1418 if (*frame_length < priv->mode->min_frame_len)
1419 *frame_length = priv->mode->min_frame_len;
1421 *val = *frame_length - *val; /* convert to raw shr */
1422 if (*val > *frame_length - IMX274_SHR_LIMIT_CONST)
1423 *val = *frame_length - IMX274_SHR_LIMIT_CONST;
1424 else if (*val < priv->mode->min_SHR)
1425 *val = priv->mode->min_SHR;
1431 * imx274_set_digital gain - Function called when setting digital gain
1432 * @priv: Pointer to device structure
1433 * @dgain: Value of digital gain.
1435 * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
1437 * Return: 0 on success
1439 static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain)
1443 reg_val = ffs(dgain);
1448 reg_val = clamp(reg_val, (u8)0, (u8)3);
1450 return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG,
1451 reg_val & IMX274_MASK_LSB_4_BITS);
1455 * imx274_set_gain - Function called when setting gain
1456 * @priv: Pointer to device structure
1457 * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
1458 * @ctrl: v4l2 control pointer
1460 * Set the gain based on input value.
1461 * The caller should hold the mutex lock imx274->lock if necessary
1463 * Return: 0 on success
1465 static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl)
1468 u32 gain, analog_gain, digital_gain, gain_reg;
1470 gain = (u32)(ctrl->val);
1472 dev_dbg(&priv->client->dev,
1473 "%s : input gain = %d.%d\n", __func__,
1474 gain >> IMX274_GAIN_SHIFT,
1475 ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT);
1477 if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN)
1478 gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN;
1479 else if (gain < IMX274_MIN_GAIN)
1480 gain = IMX274_MIN_GAIN;
1482 if (gain <= IMX274_MAX_ANALOG_GAIN)
1484 else if (gain <= IMX274_MAX_ANALOG_GAIN * 2)
1486 else if (gain <= IMX274_MAX_ANALOG_GAIN * 4)
1489 digital_gain = IMX274_MAX_DIGITAL_GAIN;
1491 analog_gain = gain / digital_gain;
1493 dev_dbg(&priv->client->dev,
1494 "%s : digital gain = %d, analog gain = %d.%d\n",
1495 __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT,
1496 ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100)
1497 >> IMX274_GAIN_SHIFT);
1499 err = imx274_set_digital_gain(priv, digital_gain);
1503 /* convert to register value, refer to imx274 datasheet */
1504 gain_reg = (u32)IMX274_GAIN_CONST -
1505 (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain;
1506 if (gain_reg > IMX274_GAIN_REG_MAX)
1507 gain_reg = IMX274_GAIN_REG_MAX;
1509 err = imx274_write_mbreg(priv, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg,
1514 if (IMX274_GAIN_CONST - gain_reg == 0) {
1519 /* convert register value back to gain value */
1520 ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT)
1521 / (IMX274_GAIN_CONST - gain_reg) * digital_gain;
1523 dev_dbg(&priv->client->dev,
1524 "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
1525 __func__, gain_reg, ctrl->val);
1530 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1535 * imx274_set_coarse_time - Function called when setting SHR value
1536 * @priv: Pointer to device structure
1537 * @val: Value for exposure time in number of line_length, or [HMAX]
1539 * Set SHR value based on input value.
1541 * Return: 0 on success
1543 static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val)
1546 u32 coarse_time, frame_length;
1550 /* convert exposure_time to appropriate SHR value */
1551 err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length);
1555 err = imx274_write_mbreg(priv, IMX274_SHR_REG_LSB, coarse_time, 2);
1559 *val = frame_length - coarse_time;
1563 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1568 * imx274_set_exposure - Function called when setting exposure time
1569 * @priv: Pointer to device structure
1570 * @val: Variable for exposure time, in the unit of micro-second
1572 * Set exposure time based on input value.
1573 * The caller should hold the mutex lock imx274->lock if necessary
1575 * Return: 0 on success
1577 static int imx274_set_exposure(struct stimx274 *priv, int val)
1581 u32 coarse_time; /* exposure time in unit of line (HMAX)*/
1583 dev_dbg(&priv->client->dev,
1584 "%s : EXPOSURE control input = %d\n", __func__, val);
1586 /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
1588 err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
1597 coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val
1598 - priv->mode->nocpiop) / hmax;
1600 /* step 2: convert exposure_time into SHR value */
1603 err = imx274_set_coarse_time(priv, &coarse_time);
1607 priv->ctrls.exposure->val =
1608 (coarse_time * hmax + priv->mode->nocpiop)
1609 / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2);
1611 dev_dbg(&priv->client->dev,
1612 "%s : EXPOSURE control success\n", __func__);
1616 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1622 * imx274_set_vflip - Function called when setting vertical flip
1623 * @priv: Pointer to device structure
1624 * @val: Value for vflip setting
1626 * Set vertical flip based on input value.
1627 * val = 0: normal, no vertical flip
1628 * val = 1: vertical flip enabled
1629 * The caller should hold the mutex lock imx274->lock if necessary
1631 * Return: 0 on success
1633 static int imx274_set_vflip(struct stimx274 *priv, int val)
1637 err = imx274_write_reg(priv, IMX274_VFLIP_REG, val);
1639 dev_err(&priv->client->dev, "VFLIP control error\n");
1643 dev_dbg(&priv->client->dev,
1644 "%s : VFLIP control success\n", __func__);
1650 * imx274_set_test_pattern - Function called when setting test pattern
1651 * @priv: Pointer to device structure
1652 * @val: Variable for test pattern
1654 * Set to different test patterns based on input value.
1656 * Return: 0 on success
1658 static int imx274_set_test_pattern(struct stimx274 *priv, int val)
1662 if (val == TEST_PATTERN_DISABLED) {
1663 err = imx274_write_table(priv, imx274_tp_disabled);
1664 } else if (val <= TEST_PATTERN_V_COLOR_BARS) {
1665 err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1);
1667 err = imx274_write_table(priv, imx274_tp_regs);
1673 dev_dbg(&priv->client->dev,
1674 "%s : TEST PATTERN control success\n", __func__);
1676 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1682 * imx274_set_frame_length - Function called when setting frame length
1683 * @priv: Pointer to device structure
1684 * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
1686 * Set frame length based on input value.
1688 * Return: 0 on success
1690 static int imx274_set_frame_length(struct stimx274 *priv, u32 val)
1695 dev_dbg(&priv->client->dev, "%s : input length = %d\n",
1698 frame_length = (u32)val;
1700 err = imx274_write_mbreg(priv, IMX274_VMAX_REG_3, frame_length, 3);
1707 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1712 * imx274_set_frame_interval - Function called when setting frame interval
1713 * @priv: Pointer to device structure
1714 * @frame_interval: Variable for frame interval
1716 * Change frame interval by updating VMAX value
1717 * The caller should hold the mutex lock imx274->lock if necessary
1719 * Return: 0 on success
1721 static int imx274_set_frame_interval(struct stimx274 *priv,
1722 struct v4l2_fract frame_interval)
1725 u32 frame_length, req_frame_rate;
1729 dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d",
1730 __func__, frame_interval.numerator,
1731 frame_interval.denominator);
1733 if (frame_interval.numerator == 0) {
1738 req_frame_rate = (u32)(frame_interval.denominator
1739 / frame_interval.numerator);
1741 /* boundary check */
1742 if (req_frame_rate > priv->mode->max_fps) {
1743 frame_interval.numerator = 1;
1744 frame_interval.denominator = priv->mode->max_fps;
1745 } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) {
1746 frame_interval.numerator = 1;
1747 frame_interval.denominator = IMX274_MIN_FRAME_RATE;
1751 * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
1752 * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
1755 err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2);
1759 dev_dbg(&priv->client->dev,
1760 "%s : register SVR = %d\n", __func__, svr);
1762 err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2);
1766 dev_dbg(&priv->client->dev,
1767 "%s : register HMAX = %d\n", __func__, hmax);
1769 if (hmax == 0 || frame_interval.denominator == 0) {
1774 frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax
1775 * frame_interval.numerator
1776 / frame_interval.denominator;
1778 err = imx274_set_frame_length(priv, frame_length);
1782 priv->frame_interval = frame_interval;
1786 dev_err(&priv->client->dev, "%s error = %d\n", __func__, err);
1790 static const struct v4l2_subdev_pad_ops imx274_pad_ops = {
1791 .get_fmt = imx274_get_fmt,
1792 .set_fmt = imx274_set_fmt,
1793 .get_selection = imx274_get_selection,
1794 .set_selection = imx274_set_selection,
1797 static const struct v4l2_subdev_video_ops imx274_video_ops = {
1798 .g_frame_interval = imx274_g_frame_interval,
1799 .s_frame_interval = imx274_s_frame_interval,
1800 .s_stream = imx274_s_stream,
1803 static const struct v4l2_subdev_ops imx274_subdev_ops = {
1804 .pad = &imx274_pad_ops,
1805 .video = &imx274_video_ops,
1808 static const struct v4l2_ctrl_ops imx274_ctrl_ops = {
1809 .s_ctrl = imx274_s_ctrl,
1812 static const struct of_device_id imx274_of_id_table[] = {
1813 { .compatible = "sony,imx274" },
1816 MODULE_DEVICE_TABLE(of, imx274_of_id_table);
1818 static const struct i2c_device_id imx274_id[] = {
1822 MODULE_DEVICE_TABLE(i2c, imx274_id);
1824 static int imx274_probe(struct i2c_client *client,
1825 const struct i2c_device_id *id)
1827 struct v4l2_subdev *sd;
1828 struct stimx274 *imx274;
1831 /* initialize imx274 */
1832 imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL);
1836 mutex_init(&imx274->lock);
1838 /* initialize format */
1839 imx274->mode = &imx274_modes[IMX274_DEFAULT_BINNING];
1840 imx274->crop.width = IMX274_MAX_WIDTH;
1841 imx274->crop.height = IMX274_MAX_HEIGHT;
1842 imx274->format.width = imx274->crop.width / imx274->mode->bin_ratio;
1843 imx274->format.height = imx274->crop.height / imx274->mode->bin_ratio;
1844 imx274->format.field = V4L2_FIELD_NONE;
1845 imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10;
1846 imx274->format.colorspace = V4L2_COLORSPACE_SRGB;
1847 imx274->frame_interval.numerator = 1;
1848 imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE;
1850 /* initialize regmap */
1851 imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config);
1852 if (IS_ERR(imx274->regmap)) {
1853 dev_err(&client->dev,
1854 "regmap init failed: %ld\n", PTR_ERR(imx274->regmap));
1859 /* initialize subdevice */
1860 imx274->client = client;
1862 v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops);
1863 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS;
1865 /* initialize subdev media pad */
1866 imx274->pad.flags = MEDIA_PAD_FL_SOURCE;
1867 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1868 ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad);
1870 dev_err(&client->dev,
1871 "%s : media entity init Failed %d\n", __func__, ret);
1875 /* initialize sensor reset gpio */
1876 imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1878 if (IS_ERR(imx274->reset_gpio)) {
1879 if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER)
1880 dev_err(&client->dev, "Reset GPIO not setup in DT");
1881 ret = PTR_ERR(imx274->reset_gpio);
1885 /* pull sensor out of reset */
1886 imx274_reset(imx274, 1);
1888 /* initialize controls */
1889 ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 4);
1891 dev_err(&client->dev,
1892 "%s : ctrl handler init Failed\n", __func__);
1896 imx274->ctrls.handler.lock = &imx274->lock;
1898 /* add new controls */
1899 imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items(
1900 &imx274->ctrls.handler, &imx274_ctrl_ops,
1901 V4L2_CID_TEST_PATTERN,
1902 ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu);
1904 imx274->ctrls.gain = v4l2_ctrl_new_std(
1905 &imx274->ctrls.handler,
1907 V4L2_CID_GAIN, IMX274_MIN_GAIN,
1908 IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1,
1911 imx274->ctrls.exposure = v4l2_ctrl_new_std(
1912 &imx274->ctrls.handler,
1914 V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME,
1915 1000000 / IMX274_DEF_FRAME_RATE, 1,
1916 IMX274_MIN_EXPOSURE_TIME);
1918 imx274->ctrls.vflip = v4l2_ctrl_new_std(
1919 &imx274->ctrls.handler,
1921 V4L2_CID_VFLIP, 0, 1, 1, 0);
1923 imx274->sd.ctrl_handler = &imx274->ctrls.handler;
1924 if (imx274->ctrls.handler.error) {
1925 ret = imx274->ctrls.handler.error;
1929 /* setup default controls */
1930 ret = v4l2_ctrl_handler_setup(&imx274->ctrls.handler);
1932 dev_err(&client->dev,
1933 "Error %d setup default controls\n", ret);
1937 /* load default control values */
1938 ret = imx274_load_default(imx274);
1940 dev_err(&client->dev,
1941 "%s : imx274_load_default failed %d\n",
1946 /* register subdevice */
1947 ret = v4l2_async_register_subdev(sd);
1949 dev_err(&client->dev,
1950 "%s : v4l2_async_register_subdev failed %d\n",
1955 dev_info(&client->dev, "imx274 : imx274 probe success !\n");
1959 v4l2_ctrl_handler_free(&imx274->ctrls.handler);
1961 media_entity_cleanup(&sd->entity);
1963 mutex_destroy(&imx274->lock);
1967 static int imx274_remove(struct i2c_client *client)
1969 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1970 struct stimx274 *imx274 = to_imx274(sd);
1973 imx274_write_table(imx274, imx274_stop);
1975 v4l2_async_unregister_subdev(sd);
1976 v4l2_ctrl_handler_free(&imx274->ctrls.handler);
1977 media_entity_cleanup(&sd->entity);
1978 mutex_destroy(&imx274->lock);
1982 static struct i2c_driver imx274_i2c_driver = {
1984 .name = DRIVER_NAME,
1985 .of_match_table = imx274_of_id_table,
1987 .probe = imx274_probe,
1988 .remove = imx274_remove,
1989 .id_table = imx274_id,
1992 module_i2c_driver(imx274_i2c_driver);
1995 MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
1996 MODULE_LICENSE("GPL v2");