1 // SPDX-License-Identifier: GPL-2.0
3 * imx274.c - IMX274 CMOS Image Sensor driver
5 * Copyright (C) 2017, Leopard Imaging, Inc.
7 * Leon Luo <leonl@leopardimaging.com>
8 * Edwin Zou <edwinz@leopardimaging.com>
9 * Luca Ceresoli <luca@lucaceresoli.net>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/slab.h>
23 #include <linux/v4l2-mediabus.h>
24 #include <linux/videodev2.h>
26 #include <media/v4l2-ctrls.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-fwnode.h>
29 #include <media/v4l2-subdev.h>
32 * See "SHR, SVR Setting" in datasheet
34 #define IMX274_DEFAULT_FRAME_LENGTH (4550)
35 #define IMX274_MAX_FRAME_LENGTH (0x000fffff)
38 * See "Frame Rate Adjustment" in datasheet
40 #define IMX274_PIXCLK_CONST1 (72000000)
41 #define IMX274_PIXCLK_CONST2 (1000000)
44 * The input gain is shifted by IMX274_GAIN_SHIFT to get
45 * decimal number. The real gain is
46 * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
48 #define IMX274_GAIN_SHIFT (8)
49 #define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1)
52 * See "Analog Gain" and "Digital Gain" in datasheet
54 * max gain is calculated based on IMX274_GAIN_REG_MAX
56 #define IMX274_GAIN_REG_MAX (1957)
57 #define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT)
58 #define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\
59 / (2048 - IMX274_GAIN_REG_MAX))
60 #define IMX274_MAX_DIGITAL_GAIN (8)
61 #define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT)
62 #define IMX274_GAIN_CONST (2048) /* for gain formula */
65 * 1 line time in us = (HMAX / 72), minimal is 4 lines
67 #define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72)
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
134 /* regulator supplies */
135 static const char * const imx274_supply_names
[] = {
136 "vddl", /* IF (1.2V) supply */
137 "vdig", /* Digital Core (1.8V) supply */
138 "vana", /* Analog (2.8V) supply */
141 #define IMX274_NUM_SUPPLIES ARRAY_SIZE(imx274_supply_names)
144 * imx274 I2C operation related structure
151 static const struct regmap_config imx274_regmap_config
= {
154 .cache_type
= REGCACHE_MAPLE
,
158 * Parameters for each imx274 readout mode.
160 * These are the values to configure the sensor in one of the
163 * @init_regs: registers to initialize the mode
164 * @wbin_ratio: width downscale factor (e.g. 3 for 1280; 3 = 3840/1280)
165 * @hbin_ratio: height downscale factor (e.g. 3 for 720; 3 = 2160/720)
166 * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
167 * Adjustment (CSI-2)" in the datasheet)
168 * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
170 * @max_fps: Maximum frames per second
171 * @nocpiop: Number of clocks per internal offset period (see "Integration Time
172 * in Each Readout Drive Mode (CSI-2)" in the datasheet)
175 const struct reg_8
*init_regs
;
185 * imx274 test pattern related structure
188 TEST_PATTERN_DISABLED
= 0,
189 TEST_PATTERN_ALL_000H
,
190 TEST_PATTERN_ALL_FFFH
,
191 TEST_PATTERN_ALL_555H
,
192 TEST_PATTERN_ALL_AAAH
,
193 TEST_PATTERN_VSP_5AH
, /* VERTICAL STRIPE PATTERN 555H/AAAH */
194 TEST_PATTERN_VSP_A5H
, /* VERTICAL STRIPE PATTERN AAAH/555H */
195 TEST_PATTERN_VSP_05H
, /* VERTICAL STRIPE PATTERN 000H/555H */
196 TEST_PATTERN_VSP_50H
, /* VERTICAL STRIPE PATTERN 555H/000H */
197 TEST_PATTERN_VSP_0FH
, /* VERTICAL STRIPE PATTERN 000H/FFFH */
198 TEST_PATTERN_VSP_F0H
, /* VERTICAL STRIPE PATTERN FFFH/000H */
199 TEST_PATTERN_H_COLOR_BARS
,
200 TEST_PATTERN_V_COLOR_BARS
,
203 static const char * const tp_qmenu
[] = {
209 "Vertical Stripe (555h / AAAh)",
210 "Vertical Stripe (AAAh / 555h)",
211 "Vertical Stripe (000h / 555h)",
212 "Vertical Stripe (555h / 000h)",
213 "Vertical Stripe (000h / FFFh)",
214 "Vertical Stripe (FFFh / 000h)",
215 "Vertical Color Bars",
216 "Horizontal Color Bars",
220 * All-pixel scan mode (10-bit)
221 * imx274 mode1(refer to datasheet) register configuration with
222 * 3840x2160 resolution, raw10 data and mipi four lane output
224 static const struct reg_8 imx274_mode1_3840x2160_raw10
[] = {
230 {0x3018, 0xA2}, /* output XVS, HVS */
256 {IMX274_TABLE_END
, 0x00}
260 * Horizontal/vertical 2/2-line binning
261 * (Horizontal and vertical weightedbinning, 10-bit)
262 * imx274 mode3(refer to datasheet) register configuration with
263 * 1920x1080 resolution, raw10 data and mipi four lane output
265 static const struct reg_8 imx274_mode3_1920x1080_raw10
[] = {
271 {0x3018, 0xA2}, /* output XVS, HVS */
297 {IMX274_TABLE_END
, 0x00}
301 * Vertical 2/3 subsampling binning horizontal 3 binning
302 * imx274 mode5(refer to datasheet) register configuration with
303 * 1280x720 resolution, raw10 data and mipi four lane output
305 static const struct reg_8 imx274_mode5_1280x720_raw10
[] = {
311 {0x3018, 0xA2}, /* output XVS, HVS */
337 {IMX274_TABLE_END
, 0x00}
341 * Vertical 2/8 subsampling horizontal 3 binning
342 * imx274 mode6(refer to datasheet) register configuration with
343 * 1280x540 resolution, raw10 data and mipi four lane output
345 static const struct reg_8 imx274_mode6_1280x540_raw10
[] = {
346 {0x3004, 0x04}, /* mode setting */
349 {0x3007, 0x02}, /* mode setting */
351 {0x3018, 0xA2}, /* output XVS, HVS */
354 {0x30E2, 0x04}, /* mode setting */
377 {IMX274_TABLE_END
, 0x00}
381 * imx274 first step register configuration for
384 static const struct reg_8 imx274_start_1
[] = {
385 {IMX274_STANDBY_REG
, 0x12},
387 /* PLRD: clock settings */
398 {0x304C, 0x00}, /* PLSTMG01 */
425 {0x3304, 0x32}, /* PSMIPI1 */
434 {IMX274_TABLE_END
, 0x00}
438 * imx274 second step register configuration for
441 static const struct reg_8 imx274_start_2
[] = {
442 {IMX274_STANDBY_REG
, 0x00},
443 {0x303E, 0x02}, /* SYS_MODE = 2 */
444 {IMX274_TABLE_END
, 0x00}
448 * imx274 third step register configuration for
451 static const struct reg_8 imx274_start_3
[] = {
453 {0x3018, 0xA2}, /* XHS VHS OUTPUT */
454 {IMX274_TABLE_END
, 0x00}
458 * imx274 register configuration for stopping stream
460 static const struct reg_8 imx274_stop
[] = {
461 {IMX274_STANDBY_REG
, 0x01},
462 {IMX274_TABLE_END
, 0x00}
466 * imx274 disable test pattern register configuration
468 static const struct reg_8 imx274_tp_disabled
[] = {
473 {IMX274_TABLE_END
, 0x00}
477 * imx274 test pattern register configuration
478 * reg 0x303D defines the test pattern modes
480 static const struct reg_8 imx274_tp_regs
[] = {
486 {IMX274_TABLE_END
, 0x00}
489 /* nocpiop happens to be the same number for the implemented modes */
490 static const struct imx274_mode imx274_modes
[] = {
493 .wbin_ratio
= 1, /* 3840 */
494 .hbin_ratio
= 1, /* 2160 */
495 .init_regs
= imx274_mode1_3840x2160_raw10
,
496 .min_frame_len
= 4550,
503 .wbin_ratio
= 2, /* 1920 */
504 .hbin_ratio
= 2, /* 1080 */
505 .init_regs
= imx274_mode3_1920x1080_raw10
,
506 .min_frame_len
= 2310,
513 .wbin_ratio
= 3, /* 1280 */
514 .hbin_ratio
= 3, /* 720 */
515 .init_regs
= imx274_mode5_1280x720_raw10
,
516 .min_frame_len
= 2310,
523 .wbin_ratio
= 3, /* 1280 */
524 .hbin_ratio
= 4, /* 540 */
525 .init_regs
= imx274_mode6_1280x540_raw10
,
526 .min_frame_len
= 2310,
534 * struct imx274_ctrls - imx274 ctrl structure
535 * @handler: V4L2 ctrl handler structure
536 * @exposure: Pointer to expsure ctrl structure
537 * @gain: Pointer to gain ctrl structure
538 * @vflip: Pointer to vflip ctrl structure
539 * @test_pattern: Pointer to test pattern ctrl structure
541 struct imx274_ctrls
{
542 struct v4l2_ctrl_handler handler
;
543 struct v4l2_ctrl
*exposure
;
544 struct v4l2_ctrl
*gain
;
545 struct v4l2_ctrl
*vflip
;
546 struct v4l2_ctrl
*test_pattern
;
550 * struct stim274 - imx274 device structure
551 * @sd: V4L2 subdevice structure
552 * @pad: Media pad structure
553 * @client: Pointer to I2C client
554 * @ctrls: imx274 control structure
555 * @crop: rect to be captured
556 * @compose: compose rect, i.e. output resolution
557 * @format: V4L2 media bus frame format structure
558 * (width and height are in sync with the compose rect)
559 * @frame_rate: V4L2 frame rate structure
560 * @regmap: Pointer to regmap structure
561 * @reset_gpio: Pointer to reset gpio
562 * @supplies: List of analog and digital supply regulators
563 * @inck: Pointer to sensor input clock
564 * @lock: Mutex structure
565 * @mode: Parameters for the selected readout mode
568 struct v4l2_subdev sd
;
569 struct media_pad pad
;
570 struct i2c_client
*client
;
571 struct imx274_ctrls ctrls
;
572 struct v4l2_rect crop
;
573 struct v4l2_mbus_framefmt format
;
574 struct v4l2_fract frame_interval
;
575 struct regmap
*regmap
;
576 struct gpio_desc
*reset_gpio
;
577 struct regulator_bulk_data supplies
[IMX274_NUM_SUPPLIES
];
579 struct mutex lock
; /* mutex lock for operations */
580 const struct imx274_mode
*mode
;
583 #define IMX274_ROUND(dim, step, flags) \
584 ((flags) & V4L2_SEL_FLAG_GE \
585 ? roundup((dim), (step)) \
586 : ((flags) & V4L2_SEL_FLAG_LE \
587 ? rounddown((dim), (step)) \
588 : rounddown((dim) + (step) / 2, (step))))
591 * Function declaration
593 static int imx274_set_gain(struct stimx274
*priv
, struct v4l2_ctrl
*ctrl
);
594 static int imx274_set_exposure(struct stimx274
*priv
, int val
);
595 static int imx274_set_vflip(struct stimx274
*priv
, int val
);
596 static int imx274_set_test_pattern(struct stimx274
*priv
, int val
);
597 static int __imx274_set_frame_interval(struct stimx274
*priv
,
598 struct v4l2_fract frame_interval
);
600 static inline void msleep_range(unsigned int delay_base
)
602 usleep_range(delay_base
* 1000, delay_base
* 1000 + 500);
606 * v4l2_ctrl and v4l2_subdev related operations
608 static inline struct v4l2_subdev
*ctrl_to_sd(struct v4l2_ctrl
*ctrl
)
610 return &container_of(ctrl
->handler
,
611 struct stimx274
, ctrls
.handler
)->sd
;
614 static inline struct stimx274
*to_imx274(struct v4l2_subdev
*sd
)
616 return container_of(sd
, struct stimx274
, sd
);
620 * Writing a register table
622 * @priv: Pointer to device
623 * @table: Table containing register values (with optional delays)
625 * This is used to write register table into sensor's reg map.
627 * Return: 0 on success, errors otherwise
629 static int imx274_write_table(struct stimx274
*priv
, const struct reg_8 table
[])
631 struct regmap
*regmap
= priv
->regmap
;
633 const struct reg_8
*next
;
636 int range_start
= -1;
639 int max_range_vals
= ARRAY_SIZE(range_vals
);
641 for (next
= table
;; next
++) {
642 if ((next
->addr
!= range_start
+ range_count
) ||
643 (next
->addr
== IMX274_TABLE_END
) ||
644 (next
->addr
== IMX274_TABLE_WAIT_MS
) ||
645 (range_count
== max_range_vals
)) {
646 if (range_count
== 1)
647 err
= regmap_write(regmap
,
648 range_start
, range_vals
[0]);
649 else if (range_count
> 1)
650 err
= regmap_bulk_write(regmap
, range_start
,
662 /* Handle special address values */
663 if (next
->addr
== IMX274_TABLE_END
)
666 if (next
->addr
== IMX274_TABLE_WAIT_MS
) {
667 msleep_range(next
->val
);
674 if (range_start
== -1)
675 range_start
= next
->addr
;
677 range_vals
[range_count
++] = val
;
682 static inline int imx274_write_reg(struct stimx274
*priv
, u16 addr
, u8 val
)
686 err
= regmap_write(priv
->regmap
, addr
, val
);
688 dev_err(&priv
->client
->dev
,
689 "%s : i2c write failed, %x = %x\n", __func__
,
692 dev_dbg(&priv
->client
->dev
,
693 "%s : addr 0x%x, val=0x%x\n", __func__
,
699 * imx274_read_mbreg - Read a multibyte register.
701 * Uses a bulk read where possible.
703 * @priv: Pointer to device structure
704 * @addr: Address of the LSB register. Other registers must be
705 * consecutive, least-to-most significant.
706 * @val: Pointer to store the register value (cpu endianness)
707 * @nbytes: Number of bytes to read (range: [1..3]).
708 * Other bytes are zet to 0.
710 * Return: 0 on success, errors otherwise
712 static int imx274_read_mbreg(struct stimx274
*priv
, u16 addr
, u32
*val
,
718 err
= regmap_bulk_read(priv
->regmap
, addr
, &val_le
, nbytes
);
720 dev_err(&priv
->client
->dev
,
721 "%s : i2c bulk read failed, %x (%zu bytes)\n",
722 __func__
, addr
, nbytes
);
724 *val
= le32_to_cpu(val_le
);
725 dev_dbg(&priv
->client
->dev
,
726 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
727 __func__
, addr
, *val
, nbytes
);
734 * imx274_write_mbreg - Write a multibyte register.
736 * Uses a bulk write where possible.
738 * @priv: Pointer to device structure
739 * @addr: Address of the LSB register. Other registers must be
740 * consecutive, least-to-most significant.
741 * @val: Value to be written to the register (cpu endianness)
742 * @nbytes: Number of bytes to write (range: [1..3])
744 static int imx274_write_mbreg(struct stimx274
*priv
, u16 addr
, u32 val
,
747 __le32 val_le
= cpu_to_le32(val
);
750 err
= regmap_bulk_write(priv
->regmap
, addr
, &val_le
, nbytes
);
752 dev_err(&priv
->client
->dev
,
753 "%s : i2c bulk write failed, %x = %x (%zu bytes)\n",
754 __func__
, addr
, val
, nbytes
);
756 dev_dbg(&priv
->client
->dev
,
757 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
758 __func__
, addr
, val
, nbytes
);
763 * Set mode registers to start stream.
764 * @priv: Pointer to device structure
766 * Return: 0 on success, errors otherwise
768 static int imx274_mode_regs(struct stimx274
*priv
)
772 err
= imx274_write_table(priv
, imx274_start_1
);
776 err
= imx274_write_table(priv
, priv
->mode
->init_regs
);
782 * imx274_start_stream - Function for starting stream per mode index
783 * @priv: Pointer to device structure
785 * Return: 0 on success, errors otherwise
787 static int imx274_start_stream(struct stimx274
*priv
)
791 err
= __v4l2_ctrl_handler_setup(&priv
->ctrls
.handler
);
793 dev_err(&priv
->client
->dev
, "Error %d setup controls\n", err
);
798 * Refer to "Standby Cancel Sequence when using CSI-2" in
799 * imx274 datasheet, it should wait 10ms or more here.
800 * give it 1 extra ms for margin
803 err
= imx274_write_table(priv
, imx274_start_2
);
808 * Refer to "Standby Cancel Sequence when using CSI-2" in
809 * imx274 datasheet, it should wait 7ms or more here.
810 * give it 1 extra ms for margin
813 err
= imx274_write_table(priv
, imx274_start_3
);
821 * imx274_reset - Function called to reset the sensor
822 * @priv: Pointer to device structure
823 * @rst: Input value for determining the sensor's end state after reset
825 * Set the senor in reset and then
826 * if rst = 0, keep it in reset;
827 * if rst = 1, bring it out of reset.
830 static void imx274_reset(struct stimx274
*priv
, int rst
)
832 gpiod_set_value_cansleep(priv
->reset_gpio
, 0);
833 usleep_range(IMX274_RESET_DELAY1
, IMX274_RESET_DELAY2
);
834 gpiod_set_value_cansleep(priv
->reset_gpio
, !!rst
);
835 usleep_range(IMX274_RESET_DELAY1
, IMX274_RESET_DELAY2
);
838 static int imx274_power_on(struct device
*dev
)
840 struct i2c_client
*client
= to_i2c_client(dev
);
841 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
842 struct stimx274
*imx274
= to_imx274(sd
);
845 /* keep sensor in reset before power on */
846 imx274_reset(imx274
, 0);
848 ret
= clk_prepare_enable(imx274
->inck
);
850 dev_err(&imx274
->client
->dev
,
851 "Failed to enable input clock: %d\n", ret
);
855 ret
= regulator_bulk_enable(IMX274_NUM_SUPPLIES
, imx274
->supplies
);
857 dev_err(&imx274
->client
->dev
,
858 "Failed to enable regulators: %d\n", ret
);
863 imx274_reset(imx274
, 1);
868 clk_disable_unprepare(imx274
->inck
);
872 static int imx274_power_off(struct device
*dev
)
874 struct i2c_client
*client
= to_i2c_client(dev
);
875 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
876 struct stimx274
*imx274
= to_imx274(sd
);
878 imx274_reset(imx274
, 0);
880 regulator_bulk_disable(IMX274_NUM_SUPPLIES
, imx274
->supplies
);
882 clk_disable_unprepare(imx274
->inck
);
887 static int imx274_regulators_get(struct device
*dev
, struct stimx274
*imx274
)
891 for (i
= 0; i
< IMX274_NUM_SUPPLIES
; i
++)
892 imx274
->supplies
[i
].supply
= imx274_supply_names
[i
];
894 return devm_regulator_bulk_get(dev
, IMX274_NUM_SUPPLIES
,
899 * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
900 * @ctrl: V4L2 control to be set
902 * This function is used to set the V4L2 controls for the imx274 sensor.
904 * Return: 0 on success, errors otherwise
906 static int imx274_s_ctrl(struct v4l2_ctrl
*ctrl
)
908 struct v4l2_subdev
*sd
= ctrl_to_sd(ctrl
);
909 struct stimx274
*imx274
= to_imx274(sd
);
912 if (!pm_runtime_get_if_in_use(&imx274
->client
->dev
))
915 dev_dbg(&imx274
->client
->dev
,
916 "%s : s_ctrl: %s, value: %d\n", __func__
,
917 ctrl
->name
, ctrl
->val
);
920 case V4L2_CID_EXPOSURE
:
921 dev_dbg(&imx274
->client
->dev
,
922 "%s : set V4L2_CID_EXPOSURE\n", __func__
);
923 ret
= imx274_set_exposure(imx274
, ctrl
->val
);
927 dev_dbg(&imx274
->client
->dev
,
928 "%s : set V4L2_CID_GAIN\n", __func__
);
929 ret
= imx274_set_gain(imx274
, ctrl
);
933 dev_dbg(&imx274
->client
->dev
,
934 "%s : set V4L2_CID_VFLIP\n", __func__
);
935 ret
= imx274_set_vflip(imx274
, ctrl
->val
);
938 case V4L2_CID_TEST_PATTERN
:
939 dev_dbg(&imx274
->client
->dev
,
940 "%s : set V4L2_CID_TEST_PATTERN\n", __func__
);
941 ret
= imx274_set_test_pattern(imx274
, ctrl
->val
);
945 pm_runtime_put(&imx274
->client
->dev
);
950 static int imx274_binning_goodness(struct stimx274
*imx274
,
952 int h
, int ask_h
, u32 flags
)
954 struct device
*dev
= &imx274
->client
->dev
;
955 const int goodness
= 100000;
958 if (flags
& V4L2_SEL_FLAG_GE
) {
965 if (flags
& V4L2_SEL_FLAG_LE
) {
972 val
-= abs(w
- ask_w
);
973 val
-= abs(h
- ask_h
);
975 dev_dbg(dev
, "%s: ask %dx%d, size %dx%d, goodness %d\n",
976 __func__
, ask_w
, ask_h
, w
, h
, val
);
982 * __imx274_change_compose - Helper function to change binning and set both
983 * compose and format.
985 * We have two entry points to change binning: set_fmt and
986 * set_selection(COMPOSE). Both have to compute the new output size
987 * and set it in both the compose rect and the frame format size. We
988 * also need to do the same things after setting cropping to restore
991 * This function contains the common code for these three cases, it
992 * has many arguments in order to accommodate the needs of all of
995 * Must be called with imx274->lock locked.
997 * @imx274: The device object
998 * @sd_state: The subdev state we are editing for TRY requests
999 * @which: V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller
1000 * @width: Input-output parameter: set to the desired width before
1001 * the call, contains the chosen value after returning successfully
1002 * @height: Input-output parameter for height (see @width)
1003 * @flags: Selection flags from struct v4l2_subdev_selection, or 0 if not
1004 * available (when called from set_fmt)
1006 static int __imx274_change_compose(struct stimx274
*imx274
,
1007 struct v4l2_subdev_state
*sd_state
,
1013 struct device
*dev
= &imx274
->client
->dev
;
1014 const struct v4l2_rect
*cur_crop
;
1015 struct v4l2_mbus_framefmt
*tgt_fmt
;
1017 const struct imx274_mode
*best_mode
= &imx274_modes
[0];
1018 int best_goodness
= INT_MIN
;
1020 if (which
== V4L2_SUBDEV_FORMAT_TRY
) {
1021 cur_crop
= v4l2_subdev_state_get_crop(sd_state
, 0);
1022 tgt_fmt
= v4l2_subdev_state_get_format(sd_state
, 0);
1024 cur_crop
= &imx274
->crop
;
1025 tgt_fmt
= &imx274
->format
;
1028 for (i
= 0; i
< ARRAY_SIZE(imx274_modes
); i
++) {
1029 u8 wratio
= imx274_modes
[i
].wbin_ratio
;
1030 u8 hratio
= imx274_modes
[i
].hbin_ratio
;
1032 int goodness
= imx274_binning_goodness(
1034 cur_crop
->width
/ wratio
, *width
,
1035 cur_crop
->height
/ hratio
, *height
,
1038 if (goodness
>= best_goodness
) {
1039 best_goodness
= goodness
;
1040 best_mode
= &imx274_modes
[i
];
1044 *width
= cur_crop
->width
/ best_mode
->wbin_ratio
;
1045 *height
= cur_crop
->height
/ best_mode
->hbin_ratio
;
1047 if (which
== V4L2_SUBDEV_FORMAT_ACTIVE
)
1048 imx274
->mode
= best_mode
;
1050 dev_dbg(dev
, "%s: selected %ux%u binning\n",
1051 __func__
, best_mode
->wbin_ratio
, best_mode
->hbin_ratio
);
1053 tgt_fmt
->width
= *width
;
1054 tgt_fmt
->height
= *height
;
1055 tgt_fmt
->field
= V4L2_FIELD_NONE
;
1061 * imx274_get_fmt - Get the pad format
1062 * @sd: Pointer to V4L2 Sub device structure
1063 * @sd_state: Pointer to sub device state structure
1064 * @fmt: Pointer to pad level media bus format
1066 * This function is used to get the pad format information.
1068 * Return: 0 on success
1070 static int imx274_get_fmt(struct v4l2_subdev
*sd
,
1071 struct v4l2_subdev_state
*sd_state
,
1072 struct v4l2_subdev_format
*fmt
)
1074 struct stimx274
*imx274
= to_imx274(sd
);
1076 mutex_lock(&imx274
->lock
);
1077 fmt
->format
= imx274
->format
;
1078 mutex_unlock(&imx274
->lock
);
1083 * imx274_set_fmt - This is used to set the pad format
1084 * @sd: Pointer to V4L2 Sub device structure
1085 * @sd_state: Pointer to sub device state information structure
1086 * @format: Pointer to pad level media bus format
1088 * This function is used to set the pad format.
1090 * Return: 0 on success
1092 static int imx274_set_fmt(struct v4l2_subdev
*sd
,
1093 struct v4l2_subdev_state
*sd_state
,
1094 struct v4l2_subdev_format
*format
)
1096 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1097 struct stimx274
*imx274
= to_imx274(sd
);
1100 mutex_lock(&imx274
->lock
);
1102 err
= __imx274_change_compose(imx274
, sd_state
, format
->which
,
1103 &fmt
->width
, &fmt
->height
, 0);
1109 * __imx274_change_compose already set width and height in the
1110 * applicable format, but we need to keep all other format
1111 * values, so do a full copy here
1113 fmt
->field
= V4L2_FIELD_NONE
;
1114 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
)
1115 *v4l2_subdev_state_get_format(sd_state
, 0) = *fmt
;
1117 imx274
->format
= *fmt
;
1120 mutex_unlock(&imx274
->lock
);
1125 static int imx274_get_selection(struct v4l2_subdev
*sd
,
1126 struct v4l2_subdev_state
*sd_state
,
1127 struct v4l2_subdev_selection
*sel
)
1129 struct stimx274
*imx274
= to_imx274(sd
);
1130 const struct v4l2_rect
*src_crop
;
1131 const struct v4l2_mbus_framefmt
*src_fmt
;
1137 if (sel
->target
== V4L2_SEL_TGT_CROP_BOUNDS
) {
1140 sel
->r
.width
= IMX274_MAX_WIDTH
;
1141 sel
->r
.height
= IMX274_MAX_HEIGHT
;
1145 if (sel
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1146 src_crop
= v4l2_subdev_state_get_crop(sd_state
, 0);
1147 src_fmt
= v4l2_subdev_state_get_format(sd_state
, 0);
1149 src_crop
= &imx274
->crop
;
1150 src_fmt
= &imx274
->format
;
1153 mutex_lock(&imx274
->lock
);
1155 switch (sel
->target
) {
1156 case V4L2_SEL_TGT_CROP
:
1159 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
1162 sel
->r
.width
= src_crop
->width
;
1163 sel
->r
.height
= src_crop
->height
;
1165 case V4L2_SEL_TGT_COMPOSE
:
1168 sel
->r
.width
= src_fmt
->width
;
1169 sel
->r
.height
= src_fmt
->height
;
1175 mutex_unlock(&imx274
->lock
);
1180 static int imx274_set_selection_crop(struct stimx274
*imx274
,
1181 struct v4l2_subdev_state
*sd_state
,
1182 struct v4l2_subdev_selection
*sel
)
1184 struct v4l2_rect
*tgt_crop
;
1185 struct v4l2_rect new_crop
;
1189 * h_step could be 12 or 24 depending on the binning. But we
1190 * won't know the binning until we choose the mode later in
1191 * __imx274_change_compose(). Thus let's be safe and use the
1192 * most conservative value in all cases.
1194 const u32 h_step
= 24;
1196 new_crop
.width
= min_t(u32
,
1197 IMX274_ROUND(sel
->r
.width
, h_step
, sel
->flags
),
1200 /* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */
1201 if (new_crop
.width
< 144)
1202 new_crop
.width
= 144;
1204 new_crop
.left
= min_t(u32
,
1205 IMX274_ROUND(sel
->r
.left
, h_step
, 0),
1206 IMX274_MAX_WIDTH
- new_crop
.width
);
1208 new_crop
.height
= min_t(u32
,
1209 IMX274_ROUND(sel
->r
.height
, 2, sel
->flags
),
1212 new_crop
.top
= min_t(u32
, IMX274_ROUND(sel
->r
.top
, 2, 0),
1213 IMX274_MAX_HEIGHT
- new_crop
.height
);
1217 if (sel
->which
== V4L2_SUBDEV_FORMAT_TRY
)
1218 tgt_crop
= v4l2_subdev_state_get_crop(sd_state
, 0);
1220 tgt_crop
= &imx274
->crop
;
1222 mutex_lock(&imx274
->lock
);
1224 size_changed
= (new_crop
.width
!= tgt_crop
->width
||
1225 new_crop
.height
!= tgt_crop
->height
);
1227 /* __imx274_change_compose needs the new size in *tgt_crop */
1228 *tgt_crop
= new_crop
;
1230 /* if crop size changed then reset the output image size */
1232 __imx274_change_compose(imx274
, sd_state
, sel
->which
,
1233 &new_crop
.width
, &new_crop
.height
,
1236 mutex_unlock(&imx274
->lock
);
1241 static int imx274_set_selection(struct v4l2_subdev
*sd
,
1242 struct v4l2_subdev_state
*sd_state
,
1243 struct v4l2_subdev_selection
*sel
)
1245 struct stimx274
*imx274
= to_imx274(sd
);
1250 if (sel
->target
== V4L2_SEL_TGT_CROP
)
1251 return imx274_set_selection_crop(imx274
, sd_state
, sel
);
1253 if (sel
->target
== V4L2_SEL_TGT_COMPOSE
) {
1256 mutex_lock(&imx274
->lock
);
1257 err
= __imx274_change_compose(imx274
, sd_state
, sel
->which
,
1258 &sel
->r
.width
, &sel
->r
.height
,
1260 mutex_unlock(&imx274
->lock
);
1263 * __imx274_change_compose already set width and
1264 * height in set->r, we still need to set top-left
1277 static int imx274_apply_trimming(struct stimx274
*imx274
)
1288 h_start
= imx274
->crop
.left
+ 12;
1289 h_end
= h_start
+ imx274
->crop
.width
;
1291 /* Use the minimum allowed value of HMAX */
1292 /* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */
1293 /* Note: 260 is the minimum HMAX in all implemented modes */
1294 hmax
= max_t(u32
, 260, (imx274
->crop
.width
) / 16 + 23);
1296 /* invert v_pos if VFLIP */
1297 v_pos
= imx274
->ctrls
.vflip
->cur
.val
?
1298 (-imx274
->crop
.top
/ 2) : (imx274
->crop
.top
/ 2);
1299 v_cut
= (IMX274_MAX_HEIGHT
- imx274
->crop
.height
) / 2;
1300 write_v_size
= imx274
->crop
.height
+ 22;
1301 y_out_size
= imx274
->crop
.height
;
1303 err
= imx274_write_mbreg(imx274
, IMX274_HMAX_REG_LSB
, hmax
, 2);
1305 err
= imx274_write_mbreg(imx274
, IMX274_HTRIM_EN_REG
, 1, 1);
1307 err
= imx274_write_mbreg(imx274
, IMX274_HTRIM_START_REG_LSB
,
1310 err
= imx274_write_mbreg(imx274
, IMX274_HTRIM_END_REG_LSB
,
1313 err
= imx274_write_mbreg(imx274
, IMX274_VWIDCUTEN_REG
, 1, 1);
1315 err
= imx274_write_mbreg(imx274
, IMX274_VWIDCUT_REG_LSB
,
1318 err
= imx274_write_mbreg(imx274
, IMX274_VWINPOS_REG_LSB
,
1321 err
= imx274_write_mbreg(imx274
, IMX274_WRITE_VSIZE_REG_LSB
,
1324 err
= imx274_write_mbreg(imx274
, IMX274_Y_OUT_SIZE_REG_LSB
,
1330 static int imx274_get_frame_interval(struct v4l2_subdev
*sd
,
1331 struct v4l2_subdev_state
*sd_state
,
1332 struct v4l2_subdev_frame_interval
*fi
)
1334 struct stimx274
*imx274
= to_imx274(sd
);
1337 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
1338 * subdev active state API.
1340 if (fi
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1343 fi
->interval
= imx274
->frame_interval
;
1344 dev_dbg(&imx274
->client
->dev
, "%s frame rate = %d / %d\n",
1345 __func__
, imx274
->frame_interval
.numerator
,
1346 imx274
->frame_interval
.denominator
);
1351 static int imx274_set_frame_interval(struct v4l2_subdev
*sd
,
1352 struct v4l2_subdev_state
*sd_state
,
1353 struct v4l2_subdev_frame_interval
*fi
)
1355 struct stimx274
*imx274
= to_imx274(sd
);
1356 struct v4l2_ctrl
*ctrl
= imx274
->ctrls
.exposure
;
1361 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
1362 * subdev active state API.
1364 if (fi
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
1367 ret
= pm_runtime_resume_and_get(&imx274
->client
->dev
);
1371 mutex_lock(&imx274
->lock
);
1372 ret
= __imx274_set_frame_interval(imx274
, fi
->interval
);
1375 fi
->interval
= imx274
->frame_interval
;
1378 * exposure time range is decided by frame interval
1379 * need to update it after frame interval changes
1381 min
= IMX274_MIN_EXPOSURE_TIME
;
1382 max
= fi
->interval
.numerator
* 1000000
1383 / fi
->interval
.denominator
;
1385 ret
= __v4l2_ctrl_modify_range(ctrl
, min
, max
, 1, def
);
1387 dev_err(&imx274
->client
->dev
,
1388 "Exposure ctrl range update failed\n");
1392 /* update exposure time accordingly */
1393 imx274_set_exposure(imx274
, ctrl
->val
);
1395 dev_dbg(&imx274
->client
->dev
, "set frame interval to %uus\n",
1396 fi
->interval
.numerator
* 1000000
1397 / fi
->interval
.denominator
);
1401 mutex_unlock(&imx274
->lock
);
1402 pm_runtime_put(&imx274
->client
->dev
);
1408 * imx274_load_default - load default control values
1409 * @priv: Pointer to device structure
1411 * Return: 0 on success, errors otherwise
1413 static void imx274_load_default(struct stimx274
*priv
)
1415 /* load default control values */
1416 priv
->frame_interval
.numerator
= 1;
1417 priv
->frame_interval
.denominator
= IMX274_DEF_FRAME_RATE
;
1418 priv
->ctrls
.exposure
->val
= 1000000 / IMX274_DEF_FRAME_RATE
;
1419 priv
->ctrls
.gain
->val
= IMX274_DEF_GAIN
;
1420 priv
->ctrls
.vflip
->val
= 0;
1421 priv
->ctrls
.test_pattern
->val
= TEST_PATTERN_DISABLED
;
1425 * imx274_s_stream - It is used to start/stop the streaming.
1426 * @sd: V4L2 Sub device
1427 * @on: Flag (True / False)
1429 * This function controls the start or stop of streaming for the
1432 * Return: 0 on success, errors otherwise
1434 static int imx274_s_stream(struct v4l2_subdev
*sd
, int on
)
1436 struct stimx274
*imx274
= to_imx274(sd
);
1439 dev_dbg(&imx274
->client
->dev
, "%s : %s, mode index = %td\n", __func__
,
1440 on
? "Stream Start" : "Stream Stop",
1441 imx274
->mode
- &imx274_modes
[0]);
1443 mutex_lock(&imx274
->lock
);
1446 ret
= pm_runtime_resume_and_get(&imx274
->client
->dev
);
1448 mutex_unlock(&imx274
->lock
);
1452 /* load mode registers */
1453 ret
= imx274_mode_regs(imx274
);
1457 ret
= imx274_apply_trimming(imx274
);
1462 * update frame rate & exposure. if the last mode is different,
1463 * HMAX could be changed. As the result, frame rate & exposure
1465 * gain is not affected.
1467 ret
= __imx274_set_frame_interval(imx274
,
1468 imx274
->frame_interval
);
1473 ret
= imx274_start_stream(imx274
);
1478 ret
= imx274_write_table(imx274
, imx274_stop
);
1482 pm_runtime_put(&imx274
->client
->dev
);
1485 mutex_unlock(&imx274
->lock
);
1486 dev_dbg(&imx274
->client
->dev
, "%s : Done\n", __func__
);
1490 pm_runtime_put(&imx274
->client
->dev
);
1491 mutex_unlock(&imx274
->lock
);
1492 dev_err(&imx274
->client
->dev
, "s_stream failed\n");
1497 * imx274_get_frame_length - Function for obtaining current frame length
1498 * @priv: Pointer to device structure
1499 * @val: Pointer to obtained value
1501 * frame_length = vmax x (svr + 1), in unit of hmax.
1503 * Return: 0 on success
1505 static int imx274_get_frame_length(struct stimx274
*priv
, u32
*val
)
1511 err
= imx274_read_mbreg(priv
, IMX274_SVR_REG_LSB
, &svr
, 2);
1515 err
= imx274_read_mbreg(priv
, IMX274_VMAX_REG_3
, &vmax
, 3);
1519 *val
= vmax
* (svr
+ 1);
1524 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1528 static int imx274_clamp_coarse_time(struct stimx274
*priv
, u32
*val
,
1533 err
= imx274_get_frame_length(priv
, frame_length
);
1537 if (*frame_length
< priv
->mode
->min_frame_len
)
1538 *frame_length
= priv
->mode
->min_frame_len
;
1540 *val
= *frame_length
- *val
; /* convert to raw shr */
1541 if (*val
> *frame_length
- IMX274_SHR_LIMIT_CONST
)
1542 *val
= *frame_length
- IMX274_SHR_LIMIT_CONST
;
1543 else if (*val
< priv
->mode
->min_SHR
)
1544 *val
= priv
->mode
->min_SHR
;
1550 * imx274_set_digital gain - Function called when setting digital gain
1551 * @priv: Pointer to device structure
1552 * @dgain: Value of digital gain.
1554 * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
1556 * Return: 0 on success
1558 static int imx274_set_digital_gain(struct stimx274
*priv
, u32 dgain
)
1562 reg_val
= ffs(dgain
);
1567 reg_val
= clamp(reg_val
, (u8
)0, (u8
)3);
1569 return imx274_write_reg(priv
, IMX274_DIGITAL_GAIN_REG
,
1570 reg_val
& IMX274_MASK_LSB_4_BITS
);
1574 * imx274_set_gain - Function called when setting gain
1575 * @priv: Pointer to device structure
1576 * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
1577 * @ctrl: v4l2 control pointer
1579 * Set the gain based on input value.
1580 * The caller should hold the mutex lock imx274->lock if necessary
1582 * Return: 0 on success
1584 static int imx274_set_gain(struct stimx274
*priv
, struct v4l2_ctrl
*ctrl
)
1587 u32 gain
, analog_gain
, digital_gain
, gain_reg
;
1589 gain
= (u32
)(ctrl
->val
);
1591 dev_dbg(&priv
->client
->dev
,
1592 "%s : input gain = %d.%d\n", __func__
,
1593 gain
>> IMX274_GAIN_SHIFT
,
1594 ((gain
& IMX274_GAIN_SHIFT_MASK
) * 100) >> IMX274_GAIN_SHIFT
);
1596 if (gain
> IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
)
1597 gain
= IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
;
1598 else if (gain
< IMX274_MIN_GAIN
)
1599 gain
= IMX274_MIN_GAIN
;
1601 if (gain
<= IMX274_MAX_ANALOG_GAIN
)
1603 else if (gain
<= IMX274_MAX_ANALOG_GAIN
* 2)
1605 else if (gain
<= IMX274_MAX_ANALOG_GAIN
* 4)
1608 digital_gain
= IMX274_MAX_DIGITAL_GAIN
;
1610 analog_gain
= gain
/ digital_gain
;
1612 dev_dbg(&priv
->client
->dev
,
1613 "%s : digital gain = %d, analog gain = %d.%d\n",
1614 __func__
, digital_gain
, analog_gain
>> IMX274_GAIN_SHIFT
,
1615 ((analog_gain
& IMX274_GAIN_SHIFT_MASK
) * 100)
1616 >> IMX274_GAIN_SHIFT
);
1618 err
= imx274_set_digital_gain(priv
, digital_gain
);
1622 /* convert to register value, refer to imx274 datasheet */
1623 gain_reg
= (u32
)IMX274_GAIN_CONST
-
1624 (IMX274_GAIN_CONST
<< IMX274_GAIN_SHIFT
) / analog_gain
;
1625 if (gain_reg
> IMX274_GAIN_REG_MAX
)
1626 gain_reg
= IMX274_GAIN_REG_MAX
;
1628 err
= imx274_write_mbreg(priv
, IMX274_ANALOG_GAIN_ADDR_LSB
, gain_reg
,
1633 if (IMX274_GAIN_CONST
- gain_reg
== 0) {
1638 /* convert register value back to gain value */
1639 ctrl
->val
= (IMX274_GAIN_CONST
<< IMX274_GAIN_SHIFT
)
1640 / (IMX274_GAIN_CONST
- gain_reg
) * digital_gain
;
1642 dev_dbg(&priv
->client
->dev
,
1643 "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
1644 __func__
, gain_reg
, ctrl
->val
);
1649 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1654 * imx274_set_coarse_time - Function called when setting SHR value
1655 * @priv: Pointer to device structure
1656 * @val: Value for exposure time in number of line_length, or [HMAX]
1658 * Set SHR value based on input value.
1660 * Return: 0 on success
1662 static int imx274_set_coarse_time(struct stimx274
*priv
, u32
*val
)
1665 u32 coarse_time
, frame_length
;
1669 /* convert exposure_time to appropriate SHR value */
1670 err
= imx274_clamp_coarse_time(priv
, &coarse_time
, &frame_length
);
1674 err
= imx274_write_mbreg(priv
, IMX274_SHR_REG_LSB
, coarse_time
, 2);
1678 *val
= frame_length
- coarse_time
;
1682 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1687 * imx274_set_exposure - Function called when setting exposure time
1688 * @priv: Pointer to device structure
1689 * @val: Variable for exposure time, in the unit of micro-second
1691 * Set exposure time based on input value.
1692 * The caller should hold the mutex lock imx274->lock if necessary
1694 * Return: 0 on success
1696 static int imx274_set_exposure(struct stimx274
*priv
, int val
)
1700 u32 coarse_time
; /* exposure time in unit of line (HMAX)*/
1702 dev_dbg(&priv
->client
->dev
,
1703 "%s : EXPOSURE control input = %d\n", __func__
, val
);
1705 /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
1707 err
= imx274_read_mbreg(priv
, IMX274_HMAX_REG_LSB
, &hmax
, 2);
1716 coarse_time
= (IMX274_PIXCLK_CONST1
/ IMX274_PIXCLK_CONST2
* val
1717 - priv
->mode
->nocpiop
) / hmax
;
1719 /* step 2: convert exposure_time into SHR value */
1722 err
= imx274_set_coarse_time(priv
, &coarse_time
);
1726 priv
->ctrls
.exposure
->val
=
1727 (coarse_time
* hmax
+ priv
->mode
->nocpiop
)
1728 / (IMX274_PIXCLK_CONST1
/ IMX274_PIXCLK_CONST2
);
1730 dev_dbg(&priv
->client
->dev
,
1731 "%s : EXPOSURE control success\n", __func__
);
1735 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1741 * imx274_set_vflip - Function called when setting vertical flip
1742 * @priv: Pointer to device structure
1743 * @val: Value for vflip setting
1745 * Set vertical flip based on input value.
1746 * val = 0: normal, no vertical flip
1747 * val = 1: vertical flip enabled
1748 * The caller should hold the mutex lock imx274->lock if necessary
1750 * Return: 0 on success
1752 static int imx274_set_vflip(struct stimx274
*priv
, int val
)
1756 err
= imx274_write_reg(priv
, IMX274_VFLIP_REG
, val
);
1758 dev_err(&priv
->client
->dev
, "VFLIP control error\n");
1762 dev_dbg(&priv
->client
->dev
,
1763 "%s : VFLIP control success\n", __func__
);
1769 * imx274_set_test_pattern - Function called when setting test pattern
1770 * @priv: Pointer to device structure
1771 * @val: Variable for test pattern
1773 * Set to different test patterns based on input value.
1775 * Return: 0 on success
1777 static int imx274_set_test_pattern(struct stimx274
*priv
, int val
)
1781 if (val
== TEST_PATTERN_DISABLED
) {
1782 err
= imx274_write_table(priv
, imx274_tp_disabled
);
1783 } else if (val
<= TEST_PATTERN_V_COLOR_BARS
) {
1784 err
= imx274_write_reg(priv
, IMX274_TEST_PATTERN_REG
, val
- 1);
1786 err
= imx274_write_table(priv
, imx274_tp_regs
);
1792 dev_dbg(&priv
->client
->dev
,
1793 "%s : TEST PATTERN control success\n", __func__
);
1795 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1801 * imx274_set_frame_length - Function called when setting frame length
1802 * @priv: Pointer to device structure
1803 * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
1805 * Set frame length based on input value.
1807 * Return: 0 on success
1809 static int imx274_set_frame_length(struct stimx274
*priv
, u32 val
)
1814 dev_dbg(&priv
->client
->dev
, "%s : input length = %d\n",
1817 frame_length
= (u32
)val
;
1819 err
= imx274_write_mbreg(priv
, IMX274_VMAX_REG_3
, frame_length
, 3);
1826 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1831 * __imx274_set_frame_interval - Function called when setting frame interval
1832 * @priv: Pointer to device structure
1833 * @frame_interval: Variable for frame interval
1835 * Change frame interval by updating VMAX value
1836 * The caller should hold the mutex lock imx274->lock if necessary
1838 * Return: 0 on success
1840 static int __imx274_set_frame_interval(struct stimx274
*priv
,
1841 struct v4l2_fract frame_interval
)
1844 u32 frame_length
, req_frame_rate
;
1848 dev_dbg(&priv
->client
->dev
, "%s: input frame interval = %d / %d",
1849 __func__
, frame_interval
.numerator
,
1850 frame_interval
.denominator
);
1852 if (frame_interval
.numerator
== 0 || frame_interval
.denominator
== 0) {
1853 frame_interval
.denominator
= IMX274_DEF_FRAME_RATE
;
1854 frame_interval
.numerator
= 1;
1857 req_frame_rate
= (u32
)(frame_interval
.denominator
1858 / frame_interval
.numerator
);
1860 /* boundary check */
1861 if (req_frame_rate
> priv
->mode
->max_fps
) {
1862 frame_interval
.numerator
= 1;
1863 frame_interval
.denominator
= priv
->mode
->max_fps
;
1864 } else if (req_frame_rate
< IMX274_MIN_FRAME_RATE
) {
1865 frame_interval
.numerator
= 1;
1866 frame_interval
.denominator
= IMX274_MIN_FRAME_RATE
;
1870 * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
1871 * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
1874 err
= imx274_read_mbreg(priv
, IMX274_SVR_REG_LSB
, &svr
, 2);
1878 dev_dbg(&priv
->client
->dev
,
1879 "%s : register SVR = %d\n", __func__
, svr
);
1881 err
= imx274_read_mbreg(priv
, IMX274_HMAX_REG_LSB
, &hmax
, 2);
1885 dev_dbg(&priv
->client
->dev
,
1886 "%s : register HMAX = %d\n", __func__
, hmax
);
1888 if (hmax
== 0 || frame_interval
.denominator
== 0) {
1893 frame_length
= IMX274_PIXCLK_CONST1
/ (svr
+ 1) / hmax
1894 * frame_interval
.numerator
1895 / frame_interval
.denominator
;
1897 err
= imx274_set_frame_length(priv
, frame_length
);
1901 priv
->frame_interval
= frame_interval
;
1905 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1909 static int imx274_enum_mbus_code(struct v4l2_subdev
*sd
,
1910 struct v4l2_subdev_state
*sd_state
,
1911 struct v4l2_subdev_mbus_code_enum
*code
)
1913 if (code
->index
> 0)
1916 /* only supported format in the driver is Raw 10 bits SRGGB */
1917 code
->code
= MEDIA_BUS_FMT_SRGGB10_1X10
;
1922 static const struct v4l2_subdev_pad_ops imx274_pad_ops
= {
1923 .enum_mbus_code
= imx274_enum_mbus_code
,
1924 .get_fmt
= imx274_get_fmt
,
1925 .set_fmt
= imx274_set_fmt
,
1926 .get_selection
= imx274_get_selection
,
1927 .set_selection
= imx274_set_selection
,
1928 .get_frame_interval
= imx274_get_frame_interval
,
1929 .set_frame_interval
= imx274_set_frame_interval
,
1932 static const struct v4l2_subdev_video_ops imx274_video_ops
= {
1933 .s_stream
= imx274_s_stream
,
1936 static const struct v4l2_subdev_ops imx274_subdev_ops
= {
1937 .pad
= &imx274_pad_ops
,
1938 .video
= &imx274_video_ops
,
1941 static const struct v4l2_ctrl_ops imx274_ctrl_ops
= {
1942 .s_ctrl
= imx274_s_ctrl
,
1945 static const struct of_device_id imx274_of_id_table
[] = {
1946 { .compatible
= "sony,imx274" },
1949 MODULE_DEVICE_TABLE(of
, imx274_of_id_table
);
1951 static const struct i2c_device_id imx274_id
[] = {
1955 MODULE_DEVICE_TABLE(i2c
, imx274_id
);
1957 static int imx274_fwnode_parse(struct device
*dev
)
1959 struct fwnode_handle
*endpoint
;
1960 /* Only CSI2 is supported */
1961 struct v4l2_fwnode_endpoint ep
= {
1962 .bus_type
= V4L2_MBUS_CSI2_DPHY
1966 endpoint
= fwnode_graph_get_next_endpoint(dev_fwnode(dev
), NULL
);
1968 dev_err(dev
, "Endpoint node not found\n");
1972 ret
= v4l2_fwnode_endpoint_parse(endpoint
, &ep
);
1973 fwnode_handle_put(endpoint
);
1974 if (ret
== -ENXIO
) {
1975 dev_err(dev
, "Unsupported bus type, should be CSI2\n");
1978 dev_err(dev
, "Parsing endpoint node failed %d\n", ret
);
1982 /* Check number of data lanes, only 4 lanes supported */
1983 if (ep
.bus
.mipi_csi2
.num_data_lanes
!= 4) {
1984 dev_err(dev
, "Invalid data lanes: %d\n",
1985 ep
.bus
.mipi_csi2
.num_data_lanes
);
1992 static int imx274_probe(struct i2c_client
*client
)
1994 struct v4l2_subdev
*sd
;
1995 struct stimx274
*imx274
;
1996 struct device
*dev
= &client
->dev
;
1999 /* initialize imx274 */
2000 imx274
= devm_kzalloc(dev
, sizeof(*imx274
), GFP_KERNEL
);
2004 mutex_init(&imx274
->lock
);
2006 ret
= imx274_fwnode_parse(dev
);
2010 imx274
->inck
= devm_clk_get_optional(dev
, "inck");
2011 if (IS_ERR(imx274
->inck
))
2012 return PTR_ERR(imx274
->inck
);
2014 ret
= imx274_regulators_get(dev
, imx274
);
2016 dev_err(dev
, "Failed to get power regulators, err: %d\n", ret
);
2020 /* initialize format */
2021 imx274
->mode
= &imx274_modes
[0];
2022 imx274
->crop
.width
= IMX274_MAX_WIDTH
;
2023 imx274
->crop
.height
= IMX274_MAX_HEIGHT
;
2024 imx274
->format
.width
= imx274
->crop
.width
/ imx274
->mode
->wbin_ratio
;
2025 imx274
->format
.height
= imx274
->crop
.height
/ imx274
->mode
->hbin_ratio
;
2026 imx274
->format
.field
= V4L2_FIELD_NONE
;
2027 imx274
->format
.code
= MEDIA_BUS_FMT_SRGGB10_1X10
;
2028 imx274
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
2029 imx274
->frame_interval
.numerator
= 1;
2030 imx274
->frame_interval
.denominator
= IMX274_DEF_FRAME_RATE
;
2032 /* initialize regmap */
2033 imx274
->regmap
= devm_regmap_init_i2c(client
, &imx274_regmap_config
);
2034 if (IS_ERR(imx274
->regmap
)) {
2036 "regmap init failed: %ld\n", PTR_ERR(imx274
->regmap
));
2041 /* initialize subdevice */
2042 imx274
->client
= client
;
2044 v4l2_i2c_subdev_init(sd
, client
, &imx274_subdev_ops
);
2045 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
| V4L2_SUBDEV_FL_HAS_EVENTS
;
2047 /* initialize subdev media pad */
2048 imx274
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
2049 sd
->entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
2050 ret
= media_entity_pads_init(&sd
->entity
, 1, &imx274
->pad
);
2053 "%s : media entity init Failed %d\n", __func__
, ret
);
2057 /* initialize sensor reset gpio */
2058 imx274
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
2060 if (IS_ERR(imx274
->reset_gpio
)) {
2061 ret
= dev_err_probe(dev
, PTR_ERR(imx274
->reset_gpio
),
2062 "Reset GPIO not setup in DT\n");
2066 /* power on the sensor */
2067 ret
= imx274_power_on(dev
);
2069 dev_err(dev
, "%s : imx274 power on failed\n", __func__
);
2073 /* initialize controls */
2074 ret
= v4l2_ctrl_handler_init(&imx274
->ctrls
.handler
, 4);
2076 dev_err(dev
, "%s : ctrl handler init Failed\n", __func__
);
2080 imx274
->ctrls
.handler
.lock
= &imx274
->lock
;
2082 /* add new controls */
2083 imx274
->ctrls
.test_pattern
= v4l2_ctrl_new_std_menu_items(
2084 &imx274
->ctrls
.handler
, &imx274_ctrl_ops
,
2085 V4L2_CID_TEST_PATTERN
,
2086 ARRAY_SIZE(tp_qmenu
) - 1, 0, 0, tp_qmenu
);
2088 imx274
->ctrls
.gain
= v4l2_ctrl_new_std(
2089 &imx274
->ctrls
.handler
,
2091 V4L2_CID_GAIN
, IMX274_MIN_GAIN
,
2092 IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
, 1,
2095 imx274
->ctrls
.exposure
= v4l2_ctrl_new_std(
2096 &imx274
->ctrls
.handler
,
2098 V4L2_CID_EXPOSURE
, IMX274_MIN_EXPOSURE_TIME
,
2099 1000000 / IMX274_DEF_FRAME_RATE
, 1,
2100 IMX274_MIN_EXPOSURE_TIME
);
2102 imx274
->ctrls
.vflip
= v4l2_ctrl_new_std(
2103 &imx274
->ctrls
.handler
,
2105 V4L2_CID_VFLIP
, 0, 1, 1, 0);
2107 imx274
->sd
.ctrl_handler
= &imx274
->ctrls
.handler
;
2108 if (imx274
->ctrls
.handler
.error
) {
2109 ret
= imx274
->ctrls
.handler
.error
;
2113 /* load default control values */
2114 imx274_load_default(imx274
);
2116 /* register subdevice */
2117 ret
= v4l2_async_register_subdev(sd
);
2119 dev_err(dev
, "%s : v4l2_async_register_subdev failed %d\n",
2124 pm_runtime_set_active(dev
);
2125 pm_runtime_enable(dev
);
2126 pm_runtime_idle(dev
);
2128 dev_info(dev
, "imx274 : imx274 probe success !\n");
2132 v4l2_ctrl_handler_free(&imx274
->ctrls
.handler
);
2134 imx274_power_off(dev
);
2136 media_entity_cleanup(&sd
->entity
);
2138 mutex_destroy(&imx274
->lock
);
2142 static void imx274_remove(struct i2c_client
*client
)
2144 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2145 struct stimx274
*imx274
= to_imx274(sd
);
2147 pm_runtime_disable(&client
->dev
);
2148 if (!pm_runtime_status_suspended(&client
->dev
))
2149 imx274_power_off(&client
->dev
);
2150 pm_runtime_set_suspended(&client
->dev
);
2152 v4l2_async_unregister_subdev(sd
);
2153 v4l2_ctrl_handler_free(&imx274
->ctrls
.handler
);
2155 media_entity_cleanup(&sd
->entity
);
2156 mutex_destroy(&imx274
->lock
);
2159 static const struct dev_pm_ops imx274_pm_ops
= {
2160 SET_RUNTIME_PM_OPS(imx274_power_off
, imx274_power_on
, NULL
)
2163 static struct i2c_driver imx274_i2c_driver
= {
2165 .name
= DRIVER_NAME
,
2166 .pm
= &imx274_pm_ops
,
2167 .of_match_table
= imx274_of_id_table
,
2169 .probe
= imx274_probe
,
2170 .remove
= imx274_remove
,
2171 .id_table
= imx274_id
,
2174 module_i2c_driver(imx274_i2c_driver
);
2176 MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
2177 MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
2178 MODULE_LICENSE("GPL v2");