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.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/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/slab.h>
25 #include <linux/v4l2-mediabus.h>
26 #include <linux/videodev2.h>
28 #include <media/v4l2-ctrls.h>
29 #include <media/v4l2-device.h>
30 #include <media/v4l2-subdev.h>
33 * See "SHR, SVR Setting" in datasheet
35 #define IMX274_DEFAULT_FRAME_LENGTH (4550)
36 #define IMX274_MAX_FRAME_LENGTH (0x000fffff)
39 * See "Frame Rate Adjustment" in datasheet
41 #define IMX274_PIXCLK_CONST1 (72000000)
42 #define IMX274_PIXCLK_CONST2 (1000000)
45 * The input gain is shifted by IMX274_GAIN_SHIFT to get
46 * decimal number. The real gain is
47 * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT)
49 #define IMX274_GAIN_SHIFT (8)
50 #define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1)
53 * See "Analog Gain" and "Digital Gain" in datasheet
55 * max gain is calculated based on IMX274_GAIN_REG_MAX
57 #define IMX274_GAIN_REG_MAX (1957)
58 #define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT)
59 #define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\
60 / (2048 - IMX274_GAIN_REG_MAX))
61 #define IMX274_MAX_DIGITAL_GAIN (8)
62 #define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT)
63 #define IMX274_GAIN_CONST (2048) /* for gain formula */
66 * 1 line time in us = (HMAX / 72), minimal is 4 lines
68 #define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72)
70 #define IMX274_MAX_WIDTH (3840)
71 #define IMX274_MAX_HEIGHT (2160)
72 #define IMX274_MAX_FRAME_RATE (120)
73 #define IMX274_MIN_FRAME_RATE (5)
74 #define IMX274_DEF_FRAME_RATE (60)
77 * register SHR is limited to (SVR value + 1) x VMAX value - 4
79 #define IMX274_SHR_LIMIT_CONST (4)
82 * Min and max sensor reset delay (microseconds)
84 #define IMX274_RESET_DELAY1 (2000)
85 #define IMX274_RESET_DELAY2 (2200)
88 * shift and mask constants
90 #define IMX274_SHIFT_8_BITS (8)
91 #define IMX274_SHIFT_16_BITS (16)
92 #define IMX274_MASK_LSB_2_BITS (0x03)
93 #define IMX274_MASK_LSB_3_BITS (0x07)
94 #define IMX274_MASK_LSB_4_BITS (0x0f)
95 #define IMX274_MASK_LSB_8_BITS (0x00ff)
97 #define DRIVER_NAME "IMX274"
100 * IMX274 register definitions
102 #define IMX274_SHR_REG_MSB 0x300D /* SHR */
103 #define IMX274_SHR_REG_LSB 0x300C /* SHR */
104 #define IMX274_SVR_REG_MSB 0x300F /* SVR */
105 #define IMX274_SVR_REG_LSB 0x300E /* SVR */
106 #define IMX274_HTRIM_EN_REG 0x3037
107 #define IMX274_HTRIM_START_REG_LSB 0x3038
108 #define IMX274_HTRIM_START_REG_MSB 0x3039
109 #define IMX274_HTRIM_END_REG_LSB 0x303A
110 #define IMX274_HTRIM_END_REG_MSB 0x303B
111 #define IMX274_VWIDCUTEN_REG 0x30DD
112 #define IMX274_VWIDCUT_REG_LSB 0x30DE
113 #define IMX274_VWIDCUT_REG_MSB 0x30DF
114 #define IMX274_VWINPOS_REG_LSB 0x30E0
115 #define IMX274_VWINPOS_REG_MSB 0x30E1
116 #define IMX274_WRITE_VSIZE_REG_LSB 0x3130
117 #define IMX274_WRITE_VSIZE_REG_MSB 0x3131
118 #define IMX274_Y_OUT_SIZE_REG_LSB 0x3132
119 #define IMX274_Y_OUT_SIZE_REG_MSB 0x3133
120 #define IMX274_VMAX_REG_1 0x30FA /* VMAX, MSB */
121 #define IMX274_VMAX_REG_2 0x30F9 /* VMAX */
122 #define IMX274_VMAX_REG_3 0x30F8 /* VMAX, LSB */
123 #define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */
124 #define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */
125 #define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */
126 #define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */
127 #define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */
128 #define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */
129 #define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */
130 #define IMX274_STANDBY_REG 0x3000 /* STANDBY */
132 #define IMX274_TABLE_WAIT_MS 0
133 #define IMX274_TABLE_END 1
135 /* regulator supplies */
136 static const char * const imx274_supply_names
[] = {
137 "vddl", /* IF (1.2V) supply */
138 "vdig", /* Digital Core (1.8V) supply */
139 "vana", /* Analog (2.8V) supply */
142 #define IMX274_NUM_SUPPLIES ARRAY_SIZE(imx274_supply_names)
145 * imx274 I2C operation related structure
152 static const struct regmap_config imx274_regmap_config
= {
155 .cache_type
= REGCACHE_RBTREE
,
159 * Parameters for each imx274 readout mode.
161 * These are the values to configure the sensor in one of the
164 * @init_regs: registers to initialize the mode
165 * @wbin_ratio: width downscale factor (e.g. 3 for 1280; 3 = 3840/1280)
166 * @hbin_ratio: height downscale factor (e.g. 3 for 720; 3 = 2160/720)
167 * @min_frame_len: Minimum frame length for each mode (see "Frame Rate
168 * Adjustment (CSI-2)" in the datasheet)
169 * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the
171 * @max_fps: Maximum frames per second
172 * @nocpiop: Number of clocks per internal offset period (see "Integration Time
173 * in Each Readout Drive Mode (CSI-2)" in the datasheet)
176 const struct reg_8
*init_regs
;
186 * imx274 test pattern related structure
189 TEST_PATTERN_DISABLED
= 0,
190 TEST_PATTERN_ALL_000H
,
191 TEST_PATTERN_ALL_FFFH
,
192 TEST_PATTERN_ALL_555H
,
193 TEST_PATTERN_ALL_AAAH
,
194 TEST_PATTERN_VSP_5AH
, /* VERTICAL STRIPE PATTERN 555H/AAAH */
195 TEST_PATTERN_VSP_A5H
, /* VERTICAL STRIPE PATTERN AAAH/555H */
196 TEST_PATTERN_VSP_05H
, /* VERTICAL STRIPE PATTERN 000H/555H */
197 TEST_PATTERN_VSP_50H
, /* VERTICAL STRIPE PATTERN 555H/000H */
198 TEST_PATTERN_VSP_0FH
, /* VERTICAL STRIPE PATTERN 000H/FFFH */
199 TEST_PATTERN_VSP_F0H
, /* VERTICAL STRIPE PATTERN FFFH/000H */
200 TEST_PATTERN_H_COLOR_BARS
,
201 TEST_PATTERN_V_COLOR_BARS
,
204 static const char * const tp_qmenu
[] = {
210 "Vertical Stripe (555h / AAAh)",
211 "Vertical Stripe (AAAh / 555h)",
212 "Vertical Stripe (000h / 555h)",
213 "Vertical Stripe (555h / 000h)",
214 "Vertical Stripe (000h / FFFh)",
215 "Vertical Stripe (FFFh / 000h)",
216 "Vertical Color Bars",
217 "Horizontal Color Bars",
221 * All-pixel scan mode (10-bit)
222 * imx274 mode1(refer to datasheet) register configuration with
223 * 3840x2160 resolution, raw10 data and mipi four lane output
225 static const struct reg_8 imx274_mode1_3840x2160_raw10
[] = {
231 {0x3018, 0xA2}, /* output XVS, HVS */
257 {IMX274_TABLE_END
, 0x00}
261 * Horizontal/vertical 2/2-line binning
262 * (Horizontal and vertical weightedbinning, 10-bit)
263 * imx274 mode3(refer to datasheet) register configuration with
264 * 1920x1080 resolution, raw10 data and mipi four lane output
266 static const struct reg_8 imx274_mode3_1920x1080_raw10
[] = {
272 {0x3018, 0xA2}, /* output XVS, HVS */
298 {IMX274_TABLE_END
, 0x00}
302 * Vertical 2/3 subsampling binning horizontal 3 binning
303 * imx274 mode5(refer to datasheet) register configuration with
304 * 1280x720 resolution, raw10 data and mipi four lane output
306 static const struct reg_8 imx274_mode5_1280x720_raw10
[] = {
312 {0x3018, 0xA2}, /* output XVS, HVS */
338 {IMX274_TABLE_END
, 0x00}
342 * Vertical 2/8 subsampling horizontal 3 binning
343 * imx274 mode6(refer to datasheet) register configuration with
344 * 1280x540 resolution, raw10 data and mipi four lane output
346 static const struct reg_8 imx274_mode6_1280x540_raw10
[] = {
347 {0x3004, 0x04}, /* mode setting */
350 {0x3007, 0x02}, /* mode setting */
352 {0x3018, 0xA2}, /* output XVS, HVS */
355 {0x30E2, 0x04}, /* mode setting */
378 {IMX274_TABLE_END
, 0x00}
382 * imx274 first step register configuration for
385 static const struct reg_8 imx274_start_1
[] = {
386 {IMX274_STANDBY_REG
, 0x12},
388 /* PLRD: clock settings */
399 {0x304C, 0x00}, /* PLSTMG01 */
426 {0x3304, 0x32}, /* PSMIPI1 */
435 {IMX274_TABLE_END
, 0x00}
439 * imx274 second step register configuration for
442 static const struct reg_8 imx274_start_2
[] = {
443 {IMX274_STANDBY_REG
, 0x00},
444 {0x303E, 0x02}, /* SYS_MODE = 2 */
445 {IMX274_TABLE_END
, 0x00}
449 * imx274 third step register configuration for
452 static const struct reg_8 imx274_start_3
[] = {
454 {0x3018, 0xA2}, /* XHS VHS OUTPUT */
455 {IMX274_TABLE_END
, 0x00}
459 * imx274 register configuration for stopping stream
461 static const struct reg_8 imx274_stop
[] = {
462 {IMX274_STANDBY_REG
, 0x01},
463 {IMX274_TABLE_END
, 0x00}
467 * imx274 disable test pattern register configuration
469 static const struct reg_8 imx274_tp_disabled
[] = {
474 {IMX274_TABLE_END
, 0x00}
478 * imx274 test pattern register configuration
479 * reg 0x303D defines the test pattern modes
481 static const struct reg_8 imx274_tp_regs
[] = {
487 {IMX274_TABLE_END
, 0x00}
490 /* nocpiop happens to be the same number for the implemented modes */
491 static const struct imx274_mode imx274_modes
[] = {
494 .wbin_ratio
= 1, /* 3840 */
495 .hbin_ratio
= 1, /* 2160 */
496 .init_regs
= imx274_mode1_3840x2160_raw10
,
497 .min_frame_len
= 4550,
504 .wbin_ratio
= 2, /* 1920 */
505 .hbin_ratio
= 2, /* 1080 */
506 .init_regs
= imx274_mode3_1920x1080_raw10
,
507 .min_frame_len
= 2310,
514 .wbin_ratio
= 3, /* 1280 */
515 .hbin_ratio
= 3, /* 720 */
516 .init_regs
= imx274_mode5_1280x720_raw10
,
517 .min_frame_len
= 2310,
524 .wbin_ratio
= 3, /* 1280 */
525 .hbin_ratio
= 4, /* 540 */
526 .init_regs
= imx274_mode6_1280x540_raw10
,
527 .min_frame_len
= 2310,
535 * struct imx274_ctrls - imx274 ctrl structure
536 * @handler: V4L2 ctrl handler structure
537 * @exposure: Pointer to expsure ctrl structure
538 * @gain: Pointer to gain ctrl structure
539 * @vflip: Pointer to vflip ctrl structure
540 * @test_pattern: Pointer to test pattern ctrl structure
542 struct imx274_ctrls
{
543 struct v4l2_ctrl_handler handler
;
544 struct v4l2_ctrl
*exposure
;
545 struct v4l2_ctrl
*gain
;
546 struct v4l2_ctrl
*vflip
;
547 struct v4l2_ctrl
*test_pattern
;
551 * struct stim274 - imx274 device structure
552 * @sd: V4L2 subdevice structure
553 * @pad: Media pad structure
554 * @client: Pointer to I2C client
555 * @ctrls: imx274 control structure
556 * @crop: rect to be captured
557 * @compose: compose rect, i.e. output resolution
558 * @format: V4L2 media bus frame format structure
559 * (width and height are in sync with the compose rect)
560 * @frame_rate: V4L2 frame rate structure
561 * @regmap: Pointer to regmap structure
562 * @reset_gpio: Pointer to reset gpio
563 * @supplies: List of analog and digital supply regulators
564 * @inck: Pointer to sensor input clock
565 * @lock: Mutex structure
566 * @mode: Parameters for the selected readout mode
569 struct v4l2_subdev sd
;
570 struct media_pad pad
;
571 struct i2c_client
*client
;
572 struct imx274_ctrls ctrls
;
573 struct v4l2_rect crop
;
574 struct v4l2_mbus_framefmt format
;
575 struct v4l2_fract frame_interval
;
576 struct regmap
*regmap
;
577 struct gpio_desc
*reset_gpio
;
578 struct regulator_bulk_data supplies
[IMX274_NUM_SUPPLIES
];
580 struct mutex lock
; /* mutex lock for operations */
581 const struct imx274_mode
*mode
;
584 #define IMX274_ROUND(dim, step, flags) \
585 ((flags) & V4L2_SEL_FLAG_GE \
586 ? roundup((dim), (step)) \
587 : ((flags) & V4L2_SEL_FLAG_LE \
588 ? rounddown((dim), (step)) \
589 : rounddown((dim) + (step) / 2, (step))))
592 * Function declaration
594 static int imx274_set_gain(struct stimx274
*priv
, struct v4l2_ctrl
*ctrl
);
595 static int imx274_set_exposure(struct stimx274
*priv
, int val
);
596 static int imx274_set_vflip(struct stimx274
*priv
, int val
);
597 static int imx274_set_test_pattern(struct stimx274
*priv
, int val
);
598 static int imx274_set_frame_interval(struct stimx274
*priv
,
599 struct v4l2_fract frame_interval
);
601 static inline void msleep_range(unsigned int delay_base
)
603 usleep_range(delay_base
* 1000, delay_base
* 1000 + 500);
607 * v4l2_ctrl and v4l2_subdev related operations
609 static inline struct v4l2_subdev
*ctrl_to_sd(struct v4l2_ctrl
*ctrl
)
611 return &container_of(ctrl
->handler
,
612 struct stimx274
, ctrls
.handler
)->sd
;
615 static inline struct stimx274
*to_imx274(struct v4l2_subdev
*sd
)
617 return container_of(sd
, struct stimx274
, sd
);
621 * Writing a register table
623 * @priv: Pointer to device
624 * @table: Table containing register values (with optional delays)
626 * This is used to write register table into sensor's reg map.
628 * Return: 0 on success, errors otherwise
630 static int imx274_write_table(struct stimx274
*priv
, const struct reg_8 table
[])
632 struct regmap
*regmap
= priv
->regmap
;
634 const struct reg_8
*next
;
637 int range_start
= -1;
640 int max_range_vals
= ARRAY_SIZE(range_vals
);
642 for (next
= table
;; next
++) {
643 if ((next
->addr
!= range_start
+ range_count
) ||
644 (next
->addr
== IMX274_TABLE_END
) ||
645 (next
->addr
== IMX274_TABLE_WAIT_MS
) ||
646 (range_count
== max_range_vals
)) {
647 if (range_count
== 1)
648 err
= regmap_write(regmap
,
649 range_start
, range_vals
[0]);
650 else if (range_count
> 1)
651 err
= regmap_bulk_write(regmap
, range_start
,
663 /* Handle special address values */
664 if (next
->addr
== IMX274_TABLE_END
)
667 if (next
->addr
== IMX274_TABLE_WAIT_MS
) {
668 msleep_range(next
->val
);
675 if (range_start
== -1)
676 range_start
= next
->addr
;
678 range_vals
[range_count
++] = val
;
683 static inline int imx274_write_reg(struct stimx274
*priv
, u16 addr
, u8 val
)
687 err
= regmap_write(priv
->regmap
, addr
, val
);
689 dev_err(&priv
->client
->dev
,
690 "%s : i2c write failed, %x = %x\n", __func__
,
693 dev_dbg(&priv
->client
->dev
,
694 "%s : addr 0x%x, val=0x%x\n", __func__
,
700 * Read a multibyte register.
702 * Uses a bulk read where possible.
704 * @priv: Pointer to device structure
705 * @addr: Address of the LSB register. Other registers must be
706 * consecutive, least-to-most significant.
707 * @val: Pointer to store the register value (cpu endianness)
708 * @nbytes: Number of bytes to read (range: [1..3]).
709 * Other bytes are zet to 0.
711 * Return: 0 on success, errors otherwise
713 static int imx274_read_mbreg(struct stimx274
*priv
, u16 addr
, u32
*val
,
719 err
= regmap_bulk_read(priv
->regmap
, addr
, &val_le
, nbytes
);
721 dev_err(&priv
->client
->dev
,
722 "%s : i2c bulk read failed, %x (%zu bytes)\n",
723 __func__
, addr
, nbytes
);
725 *val
= le32_to_cpu(val_le
);
726 dev_dbg(&priv
->client
->dev
,
727 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
728 __func__
, addr
, *val
, nbytes
);
735 * Write a multibyte register.
737 * Uses a bulk write where possible.
739 * @priv: Pointer to device structure
740 * @addr: Address of the LSB register. Other registers must be
741 * consecutive, least-to-most significant.
742 * @val: Value to be written to the register (cpu endianness)
743 * @nbytes: Number of bytes to write (range: [1..3])
745 static int imx274_write_mbreg(struct stimx274
*priv
, u16 addr
, u32 val
,
748 __le32 val_le
= cpu_to_le32(val
);
751 err
= regmap_bulk_write(priv
->regmap
, addr
, &val_le
, nbytes
);
753 dev_err(&priv
->client
->dev
,
754 "%s : i2c bulk write failed, %x = %x (%zu bytes)\n",
755 __func__
, addr
, val
, nbytes
);
757 dev_dbg(&priv
->client
->dev
,
758 "%s : addr 0x%x, val=0x%x (%zu bytes)\n",
759 __func__
, addr
, val
, nbytes
);
764 * Set mode registers to start stream.
765 * @priv: Pointer to device structure
767 * Return: 0 on success, errors otherwise
769 static int imx274_mode_regs(struct stimx274
*priv
)
773 err
= imx274_write_table(priv
, imx274_start_1
);
777 err
= imx274_write_table(priv
, priv
->mode
->init_regs
);
783 * imx274_start_stream - Function for starting stream per mode index
784 * @priv: Pointer to device structure
786 * Return: 0 on success, errors otherwise
788 static int imx274_start_stream(struct stimx274
*priv
)
792 err
= __v4l2_ctrl_handler_setup(&priv
->ctrls
.handler
);
794 dev_err(&priv
->client
->dev
, "Error %d setup controls\n", err
);
799 * Refer to "Standby Cancel Sequence when using CSI-2" in
800 * imx274 datasheet, it should wait 10ms or more here.
801 * give it 1 extra ms for margin
804 err
= imx274_write_table(priv
, imx274_start_2
);
809 * Refer to "Standby Cancel Sequence when using CSI-2" in
810 * imx274 datasheet, it should wait 7ms or more here.
811 * give it 1 extra ms for margin
814 err
= imx274_write_table(priv
, imx274_start_3
);
822 * imx274_reset - Function called to reset the sensor
823 * @priv: Pointer to device structure
824 * @rst: Input value for determining the sensor's end state after reset
826 * Set the senor in reset and then
827 * if rst = 0, keep it in reset;
828 * if rst = 1, bring it out of reset.
831 static void imx274_reset(struct stimx274
*priv
, int rst
)
833 gpiod_set_value_cansleep(priv
->reset_gpio
, 0);
834 usleep_range(IMX274_RESET_DELAY1
, IMX274_RESET_DELAY2
);
835 gpiod_set_value_cansleep(priv
->reset_gpio
, !!rst
);
836 usleep_range(IMX274_RESET_DELAY1
, IMX274_RESET_DELAY2
);
839 static int imx274_power_on(struct device
*dev
)
841 struct i2c_client
*client
= to_i2c_client(dev
);
842 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
843 struct stimx274
*imx274
= to_imx274(sd
);
846 /* keep sensor in reset before power on */
847 imx274_reset(imx274
, 0);
849 ret
= clk_prepare_enable(imx274
->inck
);
851 dev_err(&imx274
->client
->dev
,
852 "Failed to enable input clock: %d\n", ret
);
856 ret
= regulator_bulk_enable(IMX274_NUM_SUPPLIES
, imx274
->supplies
);
858 dev_err(&imx274
->client
->dev
,
859 "Failed to enable regulators: %d\n", ret
);
864 imx274_reset(imx274
, 1);
869 clk_disable_unprepare(imx274
->inck
);
873 static int imx274_power_off(struct device
*dev
)
875 struct i2c_client
*client
= to_i2c_client(dev
);
876 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
877 struct stimx274
*imx274
= to_imx274(sd
);
879 imx274_reset(imx274
, 0);
881 regulator_bulk_disable(IMX274_NUM_SUPPLIES
, imx274
->supplies
);
883 clk_disable_unprepare(imx274
->inck
);
888 static int imx274_regulators_get(struct device
*dev
, struct stimx274
*imx274
)
892 for (i
= 0; i
< IMX274_NUM_SUPPLIES
; i
++)
893 imx274
->supplies
[i
].supply
= imx274_supply_names
[i
];
895 return devm_regulator_bulk_get(dev
, IMX274_NUM_SUPPLIES
,
900 * imx274_s_ctrl - This is used to set the imx274 V4L2 controls
901 * @ctrl: V4L2 control to be set
903 * This function is used to set the V4L2 controls for the imx274 sensor.
905 * Return: 0 on success, errors otherwise
907 static int imx274_s_ctrl(struct v4l2_ctrl
*ctrl
)
909 struct v4l2_subdev
*sd
= ctrl_to_sd(ctrl
);
910 struct stimx274
*imx274
= to_imx274(sd
);
913 if (!pm_runtime_get_if_in_use(&imx274
->client
->dev
))
916 dev_dbg(&imx274
->client
->dev
,
917 "%s : s_ctrl: %s, value: %d\n", __func__
,
918 ctrl
->name
, ctrl
->val
);
921 case V4L2_CID_EXPOSURE
:
922 dev_dbg(&imx274
->client
->dev
,
923 "%s : set V4L2_CID_EXPOSURE\n", __func__
);
924 ret
= imx274_set_exposure(imx274
, ctrl
->val
);
928 dev_dbg(&imx274
->client
->dev
,
929 "%s : set V4L2_CID_GAIN\n", __func__
);
930 ret
= imx274_set_gain(imx274
, ctrl
);
934 dev_dbg(&imx274
->client
->dev
,
935 "%s : set V4L2_CID_VFLIP\n", __func__
);
936 ret
= imx274_set_vflip(imx274
, ctrl
->val
);
939 case V4L2_CID_TEST_PATTERN
:
940 dev_dbg(&imx274
->client
->dev
,
941 "%s : set V4L2_CID_TEST_PATTERN\n", __func__
);
942 ret
= imx274_set_test_pattern(imx274
, ctrl
->val
);
946 pm_runtime_put(&imx274
->client
->dev
);
951 static int imx274_binning_goodness(struct stimx274
*imx274
,
953 int h
, int ask_h
, u32 flags
)
955 struct device
*dev
= &imx274
->client
->dev
;
956 const int goodness
= 100000;
959 if (flags
& V4L2_SEL_FLAG_GE
) {
966 if (flags
& V4L2_SEL_FLAG_LE
) {
973 val
-= abs(w
- ask_w
);
974 val
-= abs(h
- ask_h
);
976 dev_dbg(dev
, "%s: ask %dx%d, size %dx%d, goodness %d\n",
977 __func__
, ask_w
, ask_h
, w
, h
, val
);
983 * Helper function to change binning and set both 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 * @cfg: The pad config 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_pad_config
*cfg
,
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
= &cfg
->try_crop
;
1022 tgt_fmt
= &cfg
->try_fmt
;
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 * @cfg: Pointer to sub device pad information 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_pad_config
*cfg
,
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 * @cfg: Pointer to sub device pad 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_pad_config
*cfg
,
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
, cfg
, 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 cfg
->try_fmt
= *fmt
;
1117 imx274
->format
= *fmt
;
1120 mutex_unlock(&imx274
->lock
);
1125 static int imx274_get_selection(struct v4l2_subdev
*sd
,
1126 struct v4l2_subdev_pad_config
*cfg
,
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
= &cfg
->try_crop
;
1147 src_fmt
= &cfg
->try_fmt
;
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_pad_config
*cfg
,
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
= &cfg
->try_crop
;
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
, cfg
, 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_pad_config
*cfg
,
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
, cfg
, sel
);
1253 if (sel
->target
== V4L2_SEL_TGT_COMPOSE
) {
1256 mutex_lock(&imx274
->lock
);
1257 err
= __imx274_change_compose(imx274
, cfg
, 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
,
1331 * imx274_g_frame_interval - Get the frame interval
1332 * @sd: Pointer to V4L2 Sub device structure
1333 * @fi: Pointer to V4l2 Sub device frame interval structure
1335 * This function is used to get the frame interval.
1337 * Return: 0 on success
1339 static int imx274_g_frame_interval(struct v4l2_subdev
*sd
,
1340 struct v4l2_subdev_frame_interval
*fi
)
1342 struct stimx274
*imx274
= to_imx274(sd
);
1344 fi
->interval
= imx274
->frame_interval
;
1345 dev_dbg(&imx274
->client
->dev
, "%s frame rate = %d / %d\n",
1346 __func__
, imx274
->frame_interval
.numerator
,
1347 imx274
->frame_interval
.denominator
);
1353 * imx274_s_frame_interval - Set the frame interval
1354 * @sd: Pointer to V4L2 Sub device structure
1355 * @fi: Pointer to V4l2 Sub device frame interval structure
1357 * This function is used to set the frame intervavl.
1359 * Return: 0 on success
1361 static int imx274_s_frame_interval(struct v4l2_subdev
*sd
,
1362 struct v4l2_subdev_frame_interval
*fi
)
1364 struct stimx274
*imx274
= to_imx274(sd
);
1365 struct v4l2_ctrl
*ctrl
= imx274
->ctrls
.exposure
;
1369 mutex_lock(&imx274
->lock
);
1370 ret
= imx274_set_frame_interval(imx274
, fi
->interval
);
1373 fi
->interval
= imx274
->frame_interval
;
1376 * exposure time range is decided by frame interval
1377 * need to update it after frame interval changes
1379 min
= IMX274_MIN_EXPOSURE_TIME
;
1380 max
= fi
->interval
.numerator
* 1000000
1381 / fi
->interval
.denominator
;
1383 if (__v4l2_ctrl_modify_range(ctrl
, min
, max
, 1, def
)) {
1384 dev_err(&imx274
->client
->dev
,
1385 "Exposure ctrl range update failed\n");
1389 /* update exposure time accordingly */
1390 imx274_set_exposure(imx274
, ctrl
->val
);
1392 dev_dbg(&imx274
->client
->dev
, "set frame interval to %uus\n",
1393 fi
->interval
.numerator
* 1000000
1394 / fi
->interval
.denominator
);
1398 mutex_unlock(&imx274
->lock
);
1404 * imx274_load_default - load default control values
1405 * @priv: Pointer to device structure
1407 * Return: 0 on success, errors otherwise
1409 static void imx274_load_default(struct stimx274
*priv
)
1411 /* load default control values */
1412 priv
->frame_interval
.numerator
= 1;
1413 priv
->frame_interval
.denominator
= IMX274_DEF_FRAME_RATE
;
1414 priv
->ctrls
.exposure
->val
= 1000000 / IMX274_DEF_FRAME_RATE
;
1415 priv
->ctrls
.gain
->val
= IMX274_DEF_GAIN
;
1416 priv
->ctrls
.vflip
->val
= 0;
1417 priv
->ctrls
.test_pattern
->val
= TEST_PATTERN_DISABLED
;
1421 * imx274_s_stream - It is used to start/stop the streaming.
1422 * @sd: V4L2 Sub device
1423 * @on: Flag (True / False)
1425 * This function controls the start or stop of streaming for the
1428 * Return: 0 on success, errors otherwise
1430 static int imx274_s_stream(struct v4l2_subdev
*sd
, int on
)
1432 struct stimx274
*imx274
= to_imx274(sd
);
1435 dev_dbg(&imx274
->client
->dev
, "%s : %s, mode index = %td\n", __func__
,
1436 on
? "Stream Start" : "Stream Stop",
1437 imx274
->mode
- &imx274_modes
[0]);
1439 mutex_lock(&imx274
->lock
);
1442 ret
= pm_runtime_get_sync(&imx274
->client
->dev
);
1444 pm_runtime_put_noidle(&imx274
->client
->dev
);
1445 mutex_unlock(&imx274
->lock
);
1449 /* load mode registers */
1450 ret
= imx274_mode_regs(imx274
);
1454 ret
= imx274_apply_trimming(imx274
);
1459 * update frame rate & expsoure. if the last mode is different,
1460 * HMAX could be changed. As the result, frame rate & exposure
1462 * gain is not affected.
1464 ret
= imx274_set_frame_interval(imx274
,
1465 imx274
->frame_interval
);
1470 ret
= imx274_start_stream(imx274
);
1475 ret
= imx274_write_table(imx274
, imx274_stop
);
1479 pm_runtime_put(&imx274
->client
->dev
);
1482 mutex_unlock(&imx274
->lock
);
1483 dev_dbg(&imx274
->client
->dev
, "%s : Done\n", __func__
);
1487 pm_runtime_put(&imx274
->client
->dev
);
1488 mutex_unlock(&imx274
->lock
);
1489 dev_err(&imx274
->client
->dev
, "s_stream failed\n");
1494 * imx274_get_frame_length - Function for obtaining current frame length
1495 * @priv: Pointer to device structure
1496 * @val: Pointer to obainted value
1498 * frame_length = vmax x (svr + 1), in unit of hmax.
1500 * Return: 0 on success
1502 static int imx274_get_frame_length(struct stimx274
*priv
, u32
*val
)
1508 err
= imx274_read_mbreg(priv
, IMX274_SVR_REG_LSB
, &svr
, 2);
1512 err
= imx274_read_mbreg(priv
, IMX274_VMAX_REG_3
, &vmax
, 3);
1516 *val
= vmax
* (svr
+ 1);
1521 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1525 static int imx274_clamp_coarse_time(struct stimx274
*priv
, u32
*val
,
1530 err
= imx274_get_frame_length(priv
, frame_length
);
1534 if (*frame_length
< priv
->mode
->min_frame_len
)
1535 *frame_length
= priv
->mode
->min_frame_len
;
1537 *val
= *frame_length
- *val
; /* convert to raw shr */
1538 if (*val
> *frame_length
- IMX274_SHR_LIMIT_CONST
)
1539 *val
= *frame_length
- IMX274_SHR_LIMIT_CONST
;
1540 else if (*val
< priv
->mode
->min_SHR
)
1541 *val
= priv
->mode
->min_SHR
;
1547 * imx274_set_digital gain - Function called when setting digital gain
1548 * @priv: Pointer to device structure
1549 * @dgain: Value of digital gain.
1551 * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x
1553 * Return: 0 on success
1555 static int imx274_set_digital_gain(struct stimx274
*priv
, u32 dgain
)
1559 reg_val
= ffs(dgain
);
1564 reg_val
= clamp(reg_val
, (u8
)0, (u8
)3);
1566 return imx274_write_reg(priv
, IMX274_DIGITAL_GAIN_REG
,
1567 reg_val
& IMX274_MASK_LSB_4_BITS
);
1571 * imx274_set_gain - Function called when setting gain
1572 * @priv: Pointer to device structure
1573 * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT;
1574 * @ctrl: v4l2 control pointer
1576 * Set the gain based on input value.
1577 * The caller should hold the mutex lock imx274->lock if necessary
1579 * Return: 0 on success
1581 static int imx274_set_gain(struct stimx274
*priv
, struct v4l2_ctrl
*ctrl
)
1584 u32 gain
, analog_gain
, digital_gain
, gain_reg
;
1586 gain
= (u32
)(ctrl
->val
);
1588 dev_dbg(&priv
->client
->dev
,
1589 "%s : input gain = %d.%d\n", __func__
,
1590 gain
>> IMX274_GAIN_SHIFT
,
1591 ((gain
& IMX274_GAIN_SHIFT_MASK
) * 100) >> IMX274_GAIN_SHIFT
);
1593 if (gain
> IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
)
1594 gain
= IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
;
1595 else if (gain
< IMX274_MIN_GAIN
)
1596 gain
= IMX274_MIN_GAIN
;
1598 if (gain
<= IMX274_MAX_ANALOG_GAIN
)
1600 else if (gain
<= IMX274_MAX_ANALOG_GAIN
* 2)
1602 else if (gain
<= IMX274_MAX_ANALOG_GAIN
* 4)
1605 digital_gain
= IMX274_MAX_DIGITAL_GAIN
;
1607 analog_gain
= gain
/ digital_gain
;
1609 dev_dbg(&priv
->client
->dev
,
1610 "%s : digital gain = %d, analog gain = %d.%d\n",
1611 __func__
, digital_gain
, analog_gain
>> IMX274_GAIN_SHIFT
,
1612 ((analog_gain
& IMX274_GAIN_SHIFT_MASK
) * 100)
1613 >> IMX274_GAIN_SHIFT
);
1615 err
= imx274_set_digital_gain(priv
, digital_gain
);
1619 /* convert to register value, refer to imx274 datasheet */
1620 gain_reg
= (u32
)IMX274_GAIN_CONST
-
1621 (IMX274_GAIN_CONST
<< IMX274_GAIN_SHIFT
) / analog_gain
;
1622 if (gain_reg
> IMX274_GAIN_REG_MAX
)
1623 gain_reg
= IMX274_GAIN_REG_MAX
;
1625 err
= imx274_write_mbreg(priv
, IMX274_ANALOG_GAIN_ADDR_LSB
, gain_reg
,
1630 if (IMX274_GAIN_CONST
- gain_reg
== 0) {
1635 /* convert register value back to gain value */
1636 ctrl
->val
= (IMX274_GAIN_CONST
<< IMX274_GAIN_SHIFT
)
1637 / (IMX274_GAIN_CONST
- gain_reg
) * digital_gain
;
1639 dev_dbg(&priv
->client
->dev
,
1640 "%s : GAIN control success, gain_reg = %d, new gain = %d\n",
1641 __func__
, gain_reg
, ctrl
->val
);
1646 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1651 * imx274_set_coarse_time - Function called when setting SHR value
1652 * @priv: Pointer to device structure
1653 * @val: Value for exposure time in number of line_length, or [HMAX]
1655 * Set SHR value based on input value.
1657 * Return: 0 on success
1659 static int imx274_set_coarse_time(struct stimx274
*priv
, u32
*val
)
1662 u32 coarse_time
, frame_length
;
1666 /* convert exposure_time to appropriate SHR value */
1667 err
= imx274_clamp_coarse_time(priv
, &coarse_time
, &frame_length
);
1671 err
= imx274_write_mbreg(priv
, IMX274_SHR_REG_LSB
, coarse_time
, 2);
1675 *val
= frame_length
- coarse_time
;
1679 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1684 * imx274_set_exposure - Function called when setting exposure time
1685 * @priv: Pointer to device structure
1686 * @val: Variable for exposure time, in the unit of micro-second
1688 * Set exposure time based on input value.
1689 * The caller should hold the mutex lock imx274->lock if necessary
1691 * Return: 0 on success
1693 static int imx274_set_exposure(struct stimx274
*priv
, int val
)
1697 u32 coarse_time
; /* exposure time in unit of line (HMAX)*/
1699 dev_dbg(&priv
->client
->dev
,
1700 "%s : EXPOSURE control input = %d\n", __func__
, val
);
1702 /* step 1: convert input exposure_time (val) into number of 1[HMAX] */
1704 err
= imx274_read_mbreg(priv
, IMX274_HMAX_REG_LSB
, &hmax
, 2);
1713 coarse_time
= (IMX274_PIXCLK_CONST1
/ IMX274_PIXCLK_CONST2
* val
1714 - priv
->mode
->nocpiop
) / hmax
;
1716 /* step 2: convert exposure_time into SHR value */
1719 err
= imx274_set_coarse_time(priv
, &coarse_time
);
1723 priv
->ctrls
.exposure
->val
=
1724 (coarse_time
* hmax
+ priv
->mode
->nocpiop
)
1725 / (IMX274_PIXCLK_CONST1
/ IMX274_PIXCLK_CONST2
);
1727 dev_dbg(&priv
->client
->dev
,
1728 "%s : EXPOSURE control success\n", __func__
);
1732 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1738 * imx274_set_vflip - Function called when setting vertical flip
1739 * @priv: Pointer to device structure
1740 * @val: Value for vflip setting
1742 * Set vertical flip based on input value.
1743 * val = 0: normal, no vertical flip
1744 * val = 1: vertical flip enabled
1745 * The caller should hold the mutex lock imx274->lock if necessary
1747 * Return: 0 on success
1749 static int imx274_set_vflip(struct stimx274
*priv
, int val
)
1753 err
= imx274_write_reg(priv
, IMX274_VFLIP_REG
, val
);
1755 dev_err(&priv
->client
->dev
, "VFLIP control error\n");
1759 dev_dbg(&priv
->client
->dev
,
1760 "%s : VFLIP control success\n", __func__
);
1766 * imx274_set_test_pattern - Function called when setting test pattern
1767 * @priv: Pointer to device structure
1768 * @val: Variable for test pattern
1770 * Set to different test patterns based on input value.
1772 * Return: 0 on success
1774 static int imx274_set_test_pattern(struct stimx274
*priv
, int val
)
1778 if (val
== TEST_PATTERN_DISABLED
) {
1779 err
= imx274_write_table(priv
, imx274_tp_disabled
);
1780 } else if (val
<= TEST_PATTERN_V_COLOR_BARS
) {
1781 err
= imx274_write_reg(priv
, IMX274_TEST_PATTERN_REG
, val
- 1);
1783 err
= imx274_write_table(priv
, imx274_tp_regs
);
1789 dev_dbg(&priv
->client
->dev
,
1790 "%s : TEST PATTERN control success\n", __func__
);
1792 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1798 * imx274_set_frame_length - Function called when setting frame length
1799 * @priv: Pointer to device structure
1800 * @val: Variable for frame length (= VMAX, i.e. vertical drive period length)
1802 * Set frame length based on input value.
1804 * Return: 0 on success
1806 static int imx274_set_frame_length(struct stimx274
*priv
, u32 val
)
1811 dev_dbg(&priv
->client
->dev
, "%s : input length = %d\n",
1814 frame_length
= (u32
)val
;
1816 err
= imx274_write_mbreg(priv
, IMX274_VMAX_REG_3
, frame_length
, 3);
1823 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1828 * imx274_set_frame_interval - Function called when setting frame interval
1829 * @priv: Pointer to device structure
1830 * @frame_interval: Variable for frame interval
1832 * Change frame interval by updating VMAX value
1833 * The caller should hold the mutex lock imx274->lock if necessary
1835 * Return: 0 on success
1837 static int imx274_set_frame_interval(struct stimx274
*priv
,
1838 struct v4l2_fract frame_interval
)
1841 u32 frame_length
, req_frame_rate
;
1845 dev_dbg(&priv
->client
->dev
, "%s: input frame interval = %d / %d",
1846 __func__
, frame_interval
.numerator
,
1847 frame_interval
.denominator
);
1849 if (frame_interval
.numerator
== 0 || frame_interval
.denominator
== 0) {
1850 frame_interval
.denominator
= IMX274_DEF_FRAME_RATE
;
1851 frame_interval
.numerator
= 1;
1854 req_frame_rate
= (u32
)(frame_interval
.denominator
1855 / frame_interval
.numerator
);
1857 /* boundary check */
1858 if (req_frame_rate
> priv
->mode
->max_fps
) {
1859 frame_interval
.numerator
= 1;
1860 frame_interval
.denominator
= priv
->mode
->max_fps
;
1861 } else if (req_frame_rate
< IMX274_MIN_FRAME_RATE
) {
1862 frame_interval
.numerator
= 1;
1863 frame_interval
.denominator
= IMX274_MIN_FRAME_RATE
;
1867 * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX
1868 * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX
1871 err
= imx274_read_mbreg(priv
, IMX274_SVR_REG_LSB
, &svr
, 2);
1875 dev_dbg(&priv
->client
->dev
,
1876 "%s : register SVR = %d\n", __func__
, svr
);
1878 err
= imx274_read_mbreg(priv
, IMX274_HMAX_REG_LSB
, &hmax
, 2);
1882 dev_dbg(&priv
->client
->dev
,
1883 "%s : register HMAX = %d\n", __func__
, hmax
);
1885 if (hmax
== 0 || frame_interval
.denominator
== 0) {
1890 frame_length
= IMX274_PIXCLK_CONST1
/ (svr
+ 1) / hmax
1891 * frame_interval
.numerator
1892 / frame_interval
.denominator
;
1894 err
= imx274_set_frame_length(priv
, frame_length
);
1898 priv
->frame_interval
= frame_interval
;
1902 dev_err(&priv
->client
->dev
, "%s error = %d\n", __func__
, err
);
1906 static const struct v4l2_subdev_pad_ops imx274_pad_ops
= {
1907 .get_fmt
= imx274_get_fmt
,
1908 .set_fmt
= imx274_set_fmt
,
1909 .get_selection
= imx274_get_selection
,
1910 .set_selection
= imx274_set_selection
,
1913 static const struct v4l2_subdev_video_ops imx274_video_ops
= {
1914 .g_frame_interval
= imx274_g_frame_interval
,
1915 .s_frame_interval
= imx274_s_frame_interval
,
1916 .s_stream
= imx274_s_stream
,
1919 static const struct v4l2_subdev_ops imx274_subdev_ops
= {
1920 .pad
= &imx274_pad_ops
,
1921 .video
= &imx274_video_ops
,
1924 static const struct v4l2_ctrl_ops imx274_ctrl_ops
= {
1925 .s_ctrl
= imx274_s_ctrl
,
1928 static const struct of_device_id imx274_of_id_table
[] = {
1929 { .compatible
= "sony,imx274" },
1932 MODULE_DEVICE_TABLE(of
, imx274_of_id_table
);
1934 static const struct i2c_device_id imx274_id
[] = {
1938 MODULE_DEVICE_TABLE(i2c
, imx274_id
);
1940 static int imx274_probe(struct i2c_client
*client
)
1942 struct v4l2_subdev
*sd
;
1943 struct stimx274
*imx274
;
1946 /* initialize imx274 */
1947 imx274
= devm_kzalloc(&client
->dev
, sizeof(*imx274
), GFP_KERNEL
);
1951 mutex_init(&imx274
->lock
);
1953 imx274
->inck
= devm_clk_get_optional(&client
->dev
, "inck");
1954 if (IS_ERR(imx274
->inck
))
1955 return PTR_ERR(imx274
->inck
);
1957 ret
= imx274_regulators_get(&client
->dev
, imx274
);
1959 dev_err(&client
->dev
,
1960 "Failed to get power regulators, err: %d\n", ret
);
1964 /* initialize format */
1965 imx274
->mode
= &imx274_modes
[0];
1966 imx274
->crop
.width
= IMX274_MAX_WIDTH
;
1967 imx274
->crop
.height
= IMX274_MAX_HEIGHT
;
1968 imx274
->format
.width
= imx274
->crop
.width
/ imx274
->mode
->wbin_ratio
;
1969 imx274
->format
.height
= imx274
->crop
.height
/ imx274
->mode
->hbin_ratio
;
1970 imx274
->format
.field
= V4L2_FIELD_NONE
;
1971 imx274
->format
.code
= MEDIA_BUS_FMT_SRGGB10_1X10
;
1972 imx274
->format
.colorspace
= V4L2_COLORSPACE_SRGB
;
1973 imx274
->frame_interval
.numerator
= 1;
1974 imx274
->frame_interval
.denominator
= IMX274_DEF_FRAME_RATE
;
1976 /* initialize regmap */
1977 imx274
->regmap
= devm_regmap_init_i2c(client
, &imx274_regmap_config
);
1978 if (IS_ERR(imx274
->regmap
)) {
1979 dev_err(&client
->dev
,
1980 "regmap init failed: %ld\n", PTR_ERR(imx274
->regmap
));
1985 /* initialize subdevice */
1986 imx274
->client
= client
;
1988 v4l2_i2c_subdev_init(sd
, client
, &imx274_subdev_ops
);
1989 sd
->flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
| V4L2_SUBDEV_FL_HAS_EVENTS
;
1991 /* initialize subdev media pad */
1992 imx274
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1993 sd
->entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1994 ret
= media_entity_pads_init(&sd
->entity
, 1, &imx274
->pad
);
1996 dev_err(&client
->dev
,
1997 "%s : media entity init Failed %d\n", __func__
, ret
);
2001 /* initialize sensor reset gpio */
2002 imx274
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
, "reset",
2004 if (IS_ERR(imx274
->reset_gpio
)) {
2005 if (PTR_ERR(imx274
->reset_gpio
) != -EPROBE_DEFER
)
2006 dev_err(&client
->dev
, "Reset GPIO not setup in DT");
2007 ret
= PTR_ERR(imx274
->reset_gpio
);
2011 /* power on the sensor */
2012 ret
= imx274_power_on(&client
->dev
);
2014 dev_err(&client
->dev
,
2015 "%s : imx274 power on failed\n", __func__
);
2019 /* initialize controls */
2020 ret
= v4l2_ctrl_handler_init(&imx274
->ctrls
.handler
, 4);
2022 dev_err(&client
->dev
,
2023 "%s : ctrl handler init Failed\n", __func__
);
2027 imx274
->ctrls
.handler
.lock
= &imx274
->lock
;
2029 /* add new controls */
2030 imx274
->ctrls
.test_pattern
= v4l2_ctrl_new_std_menu_items(
2031 &imx274
->ctrls
.handler
, &imx274_ctrl_ops
,
2032 V4L2_CID_TEST_PATTERN
,
2033 ARRAY_SIZE(tp_qmenu
) - 1, 0, 0, tp_qmenu
);
2035 imx274
->ctrls
.gain
= v4l2_ctrl_new_std(
2036 &imx274
->ctrls
.handler
,
2038 V4L2_CID_GAIN
, IMX274_MIN_GAIN
,
2039 IMX274_MAX_DIGITAL_GAIN
* IMX274_MAX_ANALOG_GAIN
, 1,
2042 imx274
->ctrls
.exposure
= v4l2_ctrl_new_std(
2043 &imx274
->ctrls
.handler
,
2045 V4L2_CID_EXPOSURE
, IMX274_MIN_EXPOSURE_TIME
,
2046 1000000 / IMX274_DEF_FRAME_RATE
, 1,
2047 IMX274_MIN_EXPOSURE_TIME
);
2049 imx274
->ctrls
.vflip
= v4l2_ctrl_new_std(
2050 &imx274
->ctrls
.handler
,
2052 V4L2_CID_VFLIP
, 0, 1, 1, 0);
2054 imx274
->sd
.ctrl_handler
= &imx274
->ctrls
.handler
;
2055 if (imx274
->ctrls
.handler
.error
) {
2056 ret
= imx274
->ctrls
.handler
.error
;
2060 /* load default control values */
2061 imx274_load_default(imx274
);
2063 /* register subdevice */
2064 ret
= v4l2_async_register_subdev(sd
);
2066 dev_err(&client
->dev
,
2067 "%s : v4l2_async_register_subdev failed %d\n",
2072 pm_runtime_set_active(&client
->dev
);
2073 pm_runtime_enable(&client
->dev
);
2074 pm_runtime_idle(&client
->dev
);
2076 dev_info(&client
->dev
, "imx274 : imx274 probe success !\n");
2080 v4l2_ctrl_handler_free(&imx274
->ctrls
.handler
);
2082 imx274_power_off(&client
->dev
);
2084 media_entity_cleanup(&sd
->entity
);
2086 mutex_destroy(&imx274
->lock
);
2090 static int imx274_remove(struct i2c_client
*client
)
2092 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
2093 struct stimx274
*imx274
= to_imx274(sd
);
2095 pm_runtime_disable(&client
->dev
);
2096 if (!pm_runtime_status_suspended(&client
->dev
))
2097 imx274_power_off(&client
->dev
);
2098 pm_runtime_set_suspended(&client
->dev
);
2100 v4l2_async_unregister_subdev(sd
);
2101 v4l2_ctrl_handler_free(&imx274
->ctrls
.handler
);
2103 media_entity_cleanup(&sd
->entity
);
2104 mutex_destroy(&imx274
->lock
);
2108 static const struct dev_pm_ops imx274_pm_ops
= {
2109 SET_RUNTIME_PM_OPS(imx274_power_off
, imx274_power_on
, NULL
)
2112 static struct i2c_driver imx274_i2c_driver
= {
2114 .name
= DRIVER_NAME
,
2115 .pm
= &imx274_pm_ops
,
2116 .of_match_table
= imx274_of_id_table
,
2118 .probe_new
= imx274_probe
,
2119 .remove
= imx274_remove
,
2120 .id_table
= imx274_id
,
2123 module_i2c_driver(imx274_i2c_driver
);
2125 MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>");
2126 MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver");
2127 MODULE_LICENSE("GPL v2");