Luca Ceresoli | e6002df | 2018-08-24 12:35:25 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 2 | /* |
| 3 | * imx274.c - IMX274 CMOS Image Sensor driver |
| 4 | * |
| 5 | * Copyright (C) 2017, Leopard Imaging, Inc. |
| 6 | * |
| 7 | * Leon Luo <leonl@leopardimaging.com> |
| 8 | * Edwin Zou <edwinz@leopardimaging.com> |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 9 | * Luca Ceresoli <luca@lucaceresoli.net> |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 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> |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 18 | #include <linux/kernel.h> |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/of_gpio.h> |
| 21 | #include <linux/regmap.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/v4l2-mediabus.h> |
| 24 | #include <linux/videodev2.h> |
| 25 | |
| 26 | #include <media/v4l2-ctrls.h> |
| 27 | #include <media/v4l2-device.h> |
| 28 | #include <media/v4l2-subdev.h> |
| 29 | |
| 30 | /* |
| 31 | * See "SHR, SVR Setting" in datasheet |
| 32 | */ |
| 33 | #define IMX274_DEFAULT_FRAME_LENGTH (4550) |
| 34 | #define IMX274_MAX_FRAME_LENGTH (0x000fffff) |
| 35 | |
| 36 | /* |
| 37 | * See "Frame Rate Adjustment" in datasheet |
| 38 | */ |
| 39 | #define IMX274_PIXCLK_CONST1 (72000000) |
| 40 | #define IMX274_PIXCLK_CONST2 (1000000) |
| 41 | |
| 42 | /* |
| 43 | * The input gain is shifted by IMX274_GAIN_SHIFT to get |
| 44 | * decimal number. The real gain is |
| 45 | * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT) |
| 46 | */ |
| 47 | #define IMX274_GAIN_SHIFT (8) |
| 48 | #define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1) |
| 49 | |
| 50 | /* |
| 51 | * See "Analog Gain" and "Digital Gain" in datasheet |
| 52 | * min gain is 1X |
| 53 | * max gain is calculated based on IMX274_GAIN_REG_MAX |
| 54 | */ |
| 55 | #define IMX274_GAIN_REG_MAX (1957) |
| 56 | #define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT) |
| 57 | #define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\ |
| 58 | / (2048 - IMX274_GAIN_REG_MAX)) |
| 59 | #define IMX274_MAX_DIGITAL_GAIN (8) |
| 60 | #define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT) |
| 61 | #define IMX274_GAIN_CONST (2048) /* for gain formula */ |
| 62 | |
| 63 | /* |
| 64 | * 1 line time in us = (HMAX / 72), minimal is 4 lines |
| 65 | */ |
| 66 | #define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72) |
| 67 | |
Luca Ceresoli | 3df8adb | 2018-08-24 12:35:19 -0400 | [diff] [blame] | 68 | #define IMX274_DEFAULT_BINNING IMX274_BINNING_OFF |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 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) |
| 74 | |
| 75 | /* |
| 76 | * register SHR is limited to (SVR value + 1) x VMAX value - 4 |
| 77 | */ |
| 78 | #define IMX274_SHR_LIMIT_CONST (4) |
| 79 | |
| 80 | /* |
Luca Ceresoli | 4c858e9 | 2018-04-24 04:24:06 -0400 | [diff] [blame] | 81 | * Min and max sensor reset delay (microseconds) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 82 | */ |
| 83 | #define IMX274_RESET_DELAY1 (2000) |
| 84 | #define IMX274_RESET_DELAY2 (2200) |
| 85 | |
| 86 | /* |
| 87 | * shift and mask constants |
| 88 | */ |
| 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) |
| 95 | |
| 96 | #define DRIVER_NAME "IMX274" |
| 97 | |
| 98 | /* |
| 99 | * IMX274 register definitions |
| 100 | */ |
Luca Ceresoli | 4eb7846 | 2018-04-24 04:24:10 -0400 | [diff] [blame] | 101 | #define IMX274_SHR_REG_MSB 0x300D /* SHR */ |
| 102 | #define IMX274_SHR_REG_LSB 0x300C /* SHR */ |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 103 | #define IMX274_SVR_REG_MSB 0x300F /* SVR */ |
| 104 | #define IMX274_SVR_REG_LSB 0x300E /* SVR */ |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 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 |
Luca Ceresoli | 4eb7846 | 2018-04-24 04:24:10 -0400 | [diff] [blame] | 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 */ |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 122 | #define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */ |
| 123 | #define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */ |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 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 */ |
| 130 | |
| 131 | #define IMX274_TABLE_WAIT_MS 0 |
| 132 | #define IMX274_TABLE_END 1 |
| 133 | |
| 134 | /* |
| 135 | * imx274 I2C operation related structure |
| 136 | */ |
| 137 | struct reg_8 { |
| 138 | u16 addr; |
| 139 | u8 val; |
| 140 | }; |
| 141 | |
| 142 | static const struct regmap_config imx274_regmap_config = { |
| 143 | .reg_bits = 16, |
| 144 | .val_bits = 8, |
| 145 | .cache_type = REGCACHE_RBTREE, |
| 146 | }; |
| 147 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 148 | enum imx274_binning { |
| 149 | IMX274_BINNING_OFF, |
| 150 | IMX274_BINNING_2_1, |
| 151 | IMX274_BINNING_3_1, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /* |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 155 | * Parameters for each imx274 readout mode. |
| 156 | * |
| 157 | * These are the values to configure the sensor in one of the |
| 158 | * implemented modes. |
| 159 | * |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 160 | * @init_regs: registers to initialize the mode |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 161 | * @bin_ratio: downscale factor (e.g. 3 for 3:1 binning) |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 162 | * @min_frame_len: Minimum frame length for each mode (see "Frame Rate |
| 163 | * Adjustment (CSI-2)" in the datasheet) |
| 164 | * @min_SHR: Minimum SHR register value (see "Shutter Setting (CSI-2)" in the |
| 165 | * datasheet) |
| 166 | * @max_fps: Maximum frames per second |
| 167 | * @nocpiop: Number of clocks per internal offset period (see "Integration Time |
| 168 | * in Each Readout Drive Mode (CSI-2)" in the datasheet) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 169 | */ |
Luca Ceresoli | 9648cb5 | 2018-08-24 12:35:22 -0400 | [diff] [blame] | 170 | struct imx274_mode { |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 171 | const struct reg_8 *init_regs; |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 172 | unsigned int bin_ratio; |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 173 | int min_frame_len; |
| 174 | int min_SHR; |
| 175 | int max_fps; |
| 176 | int nocpiop; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | /* |
| 180 | * imx274 test pattern related structure |
| 181 | */ |
| 182 | enum { |
| 183 | TEST_PATTERN_DISABLED = 0, |
| 184 | TEST_PATTERN_ALL_000H, |
| 185 | TEST_PATTERN_ALL_FFFH, |
| 186 | TEST_PATTERN_ALL_555H, |
| 187 | TEST_PATTERN_ALL_AAAH, |
| 188 | TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */ |
| 189 | TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */ |
| 190 | TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */ |
| 191 | TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */ |
| 192 | TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */ |
| 193 | TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */ |
| 194 | TEST_PATTERN_H_COLOR_BARS, |
| 195 | TEST_PATTERN_V_COLOR_BARS, |
| 196 | }; |
| 197 | |
| 198 | static const char * const tp_qmenu[] = { |
| 199 | "Disabled", |
| 200 | "All 000h Pattern", |
| 201 | "All FFFh Pattern", |
| 202 | "All 555h Pattern", |
| 203 | "All AAAh Pattern", |
| 204 | "Vertical Stripe (555h / AAAh)", |
| 205 | "Vertical Stripe (AAAh / 555h)", |
| 206 | "Vertical Stripe (000h / 555h)", |
| 207 | "Vertical Stripe (555h / 000h)", |
| 208 | "Vertical Stripe (000h / FFFh)", |
| 209 | "Vertical Stripe (FFFh / 000h)", |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 210 | "Vertical Color Bars", |
Luca Ceresoli | 47ee7bd | 2018-12-05 11:32:21 -0500 | [diff] [blame^] | 211 | "Horizontal Color Bars", |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | /* |
| 215 | * All-pixel scan mode (10-bit) |
| 216 | * imx274 mode1(refer to datasheet) register configuration with |
| 217 | * 3840x2160 resolution, raw10 data and mipi four lane output |
| 218 | */ |
| 219 | static const struct reg_8 imx274_mode1_3840x2160_raw10[] = { |
| 220 | {0x3004, 0x01}, |
| 221 | {0x3005, 0x01}, |
| 222 | {0x3006, 0x00}, |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 223 | {0x3007, 0xa2}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 224 | |
| 225 | {0x3018, 0xA2}, /* output XVS, HVS */ |
| 226 | |
| 227 | {0x306B, 0x05}, |
| 228 | {0x30E2, 0x01}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 229 | |
| 230 | {0x30EE, 0x01}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 231 | {0x3342, 0x0A}, |
| 232 | {0x3343, 0x00}, |
| 233 | {0x3344, 0x16}, |
| 234 | {0x3345, 0x00}, |
| 235 | {0x33A6, 0x01}, |
| 236 | {0x3528, 0x0E}, |
| 237 | {0x3554, 0x1F}, |
| 238 | {0x3555, 0x01}, |
| 239 | {0x3556, 0x01}, |
| 240 | {0x3557, 0x01}, |
| 241 | {0x3558, 0x01}, |
| 242 | {0x3559, 0x00}, |
| 243 | {0x355A, 0x00}, |
| 244 | {0x35BA, 0x0E}, |
| 245 | {0x366A, 0x1B}, |
| 246 | {0x366B, 0x1A}, |
| 247 | {0x366C, 0x19}, |
| 248 | {0x366D, 0x17}, |
| 249 | {0x3A41, 0x08}, |
| 250 | |
| 251 | {IMX274_TABLE_END, 0x00} |
| 252 | }; |
| 253 | |
| 254 | /* |
| 255 | * Horizontal/vertical 2/2-line binning |
| 256 | * (Horizontal and vertical weightedbinning, 10-bit) |
| 257 | * imx274 mode3(refer to datasheet) register configuration with |
| 258 | * 1920x1080 resolution, raw10 data and mipi four lane output |
| 259 | */ |
| 260 | static const struct reg_8 imx274_mode3_1920x1080_raw10[] = { |
| 261 | {0x3004, 0x02}, |
| 262 | {0x3005, 0x21}, |
| 263 | {0x3006, 0x00}, |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 264 | {0x3007, 0xb1}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 265 | |
| 266 | {0x3018, 0xA2}, /* output XVS, HVS */ |
| 267 | |
| 268 | {0x306B, 0x05}, |
| 269 | {0x30E2, 0x02}, |
| 270 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 271 | {0x30EE, 0x01}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 272 | {0x3342, 0x0A}, |
| 273 | {0x3343, 0x00}, |
| 274 | {0x3344, 0x1A}, |
| 275 | {0x3345, 0x00}, |
| 276 | {0x33A6, 0x01}, |
| 277 | {0x3528, 0x0E}, |
| 278 | {0x3554, 0x00}, |
| 279 | {0x3555, 0x01}, |
| 280 | {0x3556, 0x01}, |
| 281 | {0x3557, 0x01}, |
| 282 | {0x3558, 0x01}, |
| 283 | {0x3559, 0x00}, |
| 284 | {0x355A, 0x00}, |
| 285 | {0x35BA, 0x0E}, |
| 286 | {0x366A, 0x1B}, |
| 287 | {0x366B, 0x1A}, |
| 288 | {0x366C, 0x19}, |
| 289 | {0x366D, 0x17}, |
| 290 | {0x3A41, 0x08}, |
| 291 | |
| 292 | {IMX274_TABLE_END, 0x00} |
| 293 | }; |
| 294 | |
| 295 | /* |
| 296 | * Vertical 2/3 subsampling binning horizontal 3 binning |
| 297 | * imx274 mode5(refer to datasheet) register configuration with |
| 298 | * 1280x720 resolution, raw10 data and mipi four lane output |
| 299 | */ |
| 300 | static const struct reg_8 imx274_mode5_1280x720_raw10[] = { |
| 301 | {0x3004, 0x03}, |
| 302 | {0x3005, 0x31}, |
| 303 | {0x3006, 0x00}, |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 304 | {0x3007, 0xa9}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 305 | |
| 306 | {0x3018, 0xA2}, /* output XVS, HVS */ |
| 307 | |
| 308 | {0x306B, 0x05}, |
| 309 | {0x30E2, 0x03}, |
| 310 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 311 | {0x30EE, 0x01}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 312 | {0x3342, 0x0A}, |
| 313 | {0x3343, 0x00}, |
| 314 | {0x3344, 0x1B}, |
| 315 | {0x3345, 0x00}, |
| 316 | {0x33A6, 0x01}, |
| 317 | {0x3528, 0x0E}, |
| 318 | {0x3554, 0x00}, |
| 319 | {0x3555, 0x01}, |
| 320 | {0x3556, 0x01}, |
| 321 | {0x3557, 0x01}, |
| 322 | {0x3558, 0x01}, |
| 323 | {0x3559, 0x00}, |
| 324 | {0x355A, 0x00}, |
| 325 | {0x35BA, 0x0E}, |
| 326 | {0x366A, 0x1B}, |
| 327 | {0x366B, 0x19}, |
| 328 | {0x366C, 0x17}, |
| 329 | {0x366D, 0x17}, |
| 330 | {0x3A41, 0x04}, |
| 331 | |
| 332 | {IMX274_TABLE_END, 0x00} |
| 333 | }; |
| 334 | |
| 335 | /* |
| 336 | * imx274 first step register configuration for |
| 337 | * starting stream |
| 338 | */ |
| 339 | static const struct reg_8 imx274_start_1[] = { |
| 340 | {IMX274_STANDBY_REG, 0x12}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 341 | |
Luca Ceresoli | 7d2332c | 2018-08-24 12:35:20 -0400 | [diff] [blame] | 342 | /* PLRD: clock settings */ |
| 343 | {0x3120, 0xF0}, |
| 344 | {0x3121, 0x00}, |
| 345 | {0x3122, 0x02}, |
| 346 | {0x3129, 0x9C}, |
| 347 | {0x312A, 0x02}, |
| 348 | {0x312D, 0x02}, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 349 | |
| 350 | {0x310B, 0x00}, |
| 351 | |
| 352 | /* PLSTMG */ |
| 353 | {0x304C, 0x00}, /* PLSTMG01 */ |
| 354 | {0x304D, 0x03}, |
| 355 | {0x331C, 0x1A}, |
| 356 | {0x331D, 0x00}, |
| 357 | {0x3502, 0x02}, |
| 358 | {0x3529, 0x0E}, |
| 359 | {0x352A, 0x0E}, |
| 360 | {0x352B, 0x0E}, |
| 361 | {0x3538, 0x0E}, |
| 362 | {0x3539, 0x0E}, |
| 363 | {0x3553, 0x00}, |
| 364 | {0x357D, 0x05}, |
| 365 | {0x357F, 0x05}, |
| 366 | {0x3581, 0x04}, |
| 367 | {0x3583, 0x76}, |
| 368 | {0x3587, 0x01}, |
| 369 | {0x35BB, 0x0E}, |
| 370 | {0x35BC, 0x0E}, |
| 371 | {0x35BD, 0x0E}, |
| 372 | {0x35BE, 0x0E}, |
| 373 | {0x35BF, 0x0E}, |
| 374 | {0x366E, 0x00}, |
| 375 | {0x366F, 0x00}, |
| 376 | {0x3670, 0x00}, |
| 377 | {0x3671, 0x00}, |
| 378 | |
| 379 | /* PSMIPI */ |
| 380 | {0x3304, 0x32}, /* PSMIPI1 */ |
| 381 | {0x3305, 0x00}, |
| 382 | {0x3306, 0x32}, |
| 383 | {0x3307, 0x00}, |
| 384 | {0x3590, 0x32}, |
| 385 | {0x3591, 0x00}, |
| 386 | {0x3686, 0x32}, |
| 387 | {0x3687, 0x00}, |
| 388 | |
| 389 | {IMX274_TABLE_END, 0x00} |
| 390 | }; |
| 391 | |
| 392 | /* |
Luca Ceresoli | 7d2332c | 2018-08-24 12:35:20 -0400 | [diff] [blame] | 393 | * imx274 second step register configuration for |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 394 | * starting stream |
| 395 | */ |
Luca Ceresoli | 7d2332c | 2018-08-24 12:35:20 -0400 | [diff] [blame] | 396 | static const struct reg_8 imx274_start_2[] = { |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 397 | {IMX274_STANDBY_REG, 0x00}, |
| 398 | {0x303E, 0x02}, /* SYS_MODE = 2 */ |
| 399 | {IMX274_TABLE_END, 0x00} |
| 400 | }; |
| 401 | |
| 402 | /* |
Luca Ceresoli | 7d2332c | 2018-08-24 12:35:20 -0400 | [diff] [blame] | 403 | * imx274 third step register configuration for |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 404 | * starting stream |
| 405 | */ |
Luca Ceresoli | 7d2332c | 2018-08-24 12:35:20 -0400 | [diff] [blame] | 406 | static const struct reg_8 imx274_start_3[] = { |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 407 | {0x30F4, 0x00}, |
| 408 | {0x3018, 0xA2}, /* XHS VHS OUTUPT */ |
| 409 | {IMX274_TABLE_END, 0x00} |
| 410 | }; |
| 411 | |
| 412 | /* |
| 413 | * imx274 register configuration for stoping stream |
| 414 | */ |
| 415 | static const struct reg_8 imx274_stop[] = { |
| 416 | {IMX274_STANDBY_REG, 0x01}, |
| 417 | {IMX274_TABLE_END, 0x00} |
| 418 | }; |
| 419 | |
| 420 | /* |
| 421 | * imx274 disable test pattern register configuration |
| 422 | */ |
| 423 | static const struct reg_8 imx274_tp_disabled[] = { |
| 424 | {0x303C, 0x00}, |
| 425 | {0x377F, 0x00}, |
| 426 | {0x3781, 0x00}, |
| 427 | {0x370B, 0x00}, |
| 428 | {IMX274_TABLE_END, 0x00} |
| 429 | }; |
| 430 | |
| 431 | /* |
| 432 | * imx274 test pattern register configuration |
| 433 | * reg 0x303D defines the test pattern modes |
| 434 | */ |
| 435 | static const struct reg_8 imx274_tp_regs[] = { |
| 436 | {0x303C, 0x11}, |
| 437 | {0x370E, 0x01}, |
| 438 | {0x377F, 0x01}, |
| 439 | {0x3781, 0x01}, |
| 440 | {0x370B, 0x11}, |
| 441 | {IMX274_TABLE_END, 0x00} |
| 442 | }; |
| 443 | |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 444 | /* nocpiop happens to be the same number for the implemented modes */ |
Luca Ceresoli | 9648cb5 | 2018-08-24 12:35:22 -0400 | [diff] [blame] | 445 | static const struct imx274_mode imx274_modes[] = { |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 446 | { |
| 447 | /* mode 1, 4K */ |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 448 | .bin_ratio = 1, |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 449 | .init_regs = imx274_mode1_3840x2160_raw10, |
| 450 | .min_frame_len = 4550, |
| 451 | .min_SHR = 12, |
| 452 | .max_fps = 60, |
| 453 | .nocpiop = 112, |
| 454 | }, |
| 455 | { |
| 456 | /* mode 3, 1080p */ |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 457 | .bin_ratio = 2, |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 458 | .init_regs = imx274_mode3_1920x1080_raw10, |
| 459 | .min_frame_len = 2310, |
| 460 | .min_SHR = 8, |
| 461 | .max_fps = 120, |
| 462 | .nocpiop = 112, |
| 463 | }, |
| 464 | { |
| 465 | /* mode 5, 720p */ |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 466 | .bin_ratio = 3, |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 467 | .init_regs = imx274_mode5_1280x720_raw10, |
| 468 | .min_frame_len = 2310, |
| 469 | .min_SHR = 8, |
| 470 | .max_fps = 120, |
| 471 | .nocpiop = 112, |
| 472 | }, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | /* |
| 476 | * struct imx274_ctrls - imx274 ctrl structure |
| 477 | * @handler: V4L2 ctrl handler structure |
| 478 | * @exposure: Pointer to expsure ctrl structure |
| 479 | * @gain: Pointer to gain ctrl structure |
| 480 | * @vflip: Pointer to vflip ctrl structure |
| 481 | * @test_pattern: Pointer to test pattern ctrl structure |
| 482 | */ |
| 483 | struct imx274_ctrls { |
| 484 | struct v4l2_ctrl_handler handler; |
| 485 | struct v4l2_ctrl *exposure; |
| 486 | struct v4l2_ctrl *gain; |
| 487 | struct v4l2_ctrl *vflip; |
| 488 | struct v4l2_ctrl *test_pattern; |
| 489 | }; |
| 490 | |
| 491 | /* |
| 492 | * struct stim274 - imx274 device structure |
| 493 | * @sd: V4L2 subdevice structure |
Luca Ceresoli | 19d38b2 | 2018-06-11 07:35:38 -0400 | [diff] [blame] | 494 | * @pad: Media pad structure |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 495 | * @client: Pointer to I2C client |
| 496 | * @ctrls: imx274 control structure |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 497 | * @crop: rect to be captured |
| 498 | * @compose: compose rect, i.e. output resolution |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 499 | * @format: V4L2 media bus frame format structure |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 500 | * (width and height are in sync with the compose rect) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 501 | * @frame_rate: V4L2 frame rate structure |
| 502 | * @regmap: Pointer to regmap structure |
| 503 | * @reset_gpio: Pointer to reset gpio |
| 504 | * @lock: Mutex structure |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 505 | * @mode: Parameters for the selected readout mode |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 506 | */ |
| 507 | struct stimx274 { |
| 508 | struct v4l2_subdev sd; |
| 509 | struct media_pad pad; |
| 510 | struct i2c_client *client; |
| 511 | struct imx274_ctrls ctrls; |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 512 | struct v4l2_rect crop; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 513 | struct v4l2_mbus_framefmt format; |
| 514 | struct v4l2_fract frame_interval; |
| 515 | struct regmap *regmap; |
| 516 | struct gpio_desc *reset_gpio; |
| 517 | struct mutex lock; /* mutex lock for operations */ |
Luca Ceresoli | 9648cb5 | 2018-08-24 12:35:22 -0400 | [diff] [blame] | 518 | const struct imx274_mode *mode; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 519 | }; |
| 520 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 521 | #define IMX274_ROUND(dim, step, flags) \ |
| 522 | ((flags) & V4L2_SEL_FLAG_GE \ |
| 523 | ? roundup((dim), (step)) \ |
| 524 | : ((flags) & V4L2_SEL_FLAG_LE \ |
| 525 | ? rounddown((dim), (step)) \ |
| 526 | : rounddown((dim) + (step) / 2, (step)))) |
| 527 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 528 | /* |
| 529 | * Function declaration |
| 530 | */ |
| 531 | static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl); |
| 532 | static int imx274_set_exposure(struct stimx274 *priv, int val); |
| 533 | static int imx274_set_vflip(struct stimx274 *priv, int val); |
| 534 | static int imx274_set_test_pattern(struct stimx274 *priv, int val); |
| 535 | static int imx274_set_frame_interval(struct stimx274 *priv, |
| 536 | struct v4l2_fract frame_interval); |
| 537 | |
| 538 | static inline void msleep_range(unsigned int delay_base) |
| 539 | { |
| 540 | usleep_range(delay_base * 1000, delay_base * 1000 + 500); |
| 541 | } |
| 542 | |
| 543 | /* |
| 544 | * v4l2_ctrl and v4l2_subdev related operations |
| 545 | */ |
| 546 | static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) |
| 547 | { |
| 548 | return &container_of(ctrl->handler, |
| 549 | struct stimx274, ctrls.handler)->sd; |
| 550 | } |
| 551 | |
| 552 | static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd) |
| 553 | { |
| 554 | return container_of(sd, struct stimx274, sd); |
| 555 | } |
| 556 | |
| 557 | /* |
Luca Ceresoli | 7ff6786 | 2018-06-11 07:35:36 -0400 | [diff] [blame] | 558 | * Writing a register table |
| 559 | * |
| 560 | * @priv: Pointer to device |
| 561 | * @table: Table containing register values (with optional delays) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 562 | * |
| 563 | * This is used to write register table into sensor's reg map. |
| 564 | * |
| 565 | * Return: 0 on success, errors otherwise |
| 566 | */ |
Luca Ceresoli | 7ff6786 | 2018-06-11 07:35:36 -0400 | [diff] [blame] | 567 | static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[]) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 568 | { |
Luca Ceresoli | 7ff6786 | 2018-06-11 07:35:36 -0400 | [diff] [blame] | 569 | struct regmap *regmap = priv->regmap; |
Dan Carpenter | 021741a | 2017-12-05 09:37:39 -0500 | [diff] [blame] | 570 | int err = 0; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 571 | const struct reg_8 *next; |
| 572 | u8 val; |
| 573 | |
| 574 | int range_start = -1; |
| 575 | int range_count = 0; |
| 576 | u8 range_vals[16]; |
| 577 | int max_range_vals = ARRAY_SIZE(range_vals); |
| 578 | |
| 579 | for (next = table;; next++) { |
| 580 | if ((next->addr != range_start + range_count) || |
Luca Ceresoli | 7ff6786 | 2018-06-11 07:35:36 -0400 | [diff] [blame] | 581 | (next->addr == IMX274_TABLE_END) || |
| 582 | (next->addr == IMX274_TABLE_WAIT_MS) || |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 583 | (range_count == max_range_vals)) { |
| 584 | if (range_count == 1) |
| 585 | err = regmap_write(regmap, |
| 586 | range_start, range_vals[0]); |
| 587 | else if (range_count > 1) |
| 588 | err = regmap_bulk_write(regmap, range_start, |
| 589 | &range_vals[0], |
| 590 | range_count); |
Mauro Carvalho Chehab | 00b4bac7 | 2017-11-01 17:05:57 -0400 | [diff] [blame] | 591 | else |
| 592 | err = 0; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 593 | |
| 594 | if (err) |
| 595 | return err; |
| 596 | |
| 597 | range_start = -1; |
| 598 | range_count = 0; |
| 599 | |
| 600 | /* Handle special address values */ |
Luca Ceresoli | 7ff6786 | 2018-06-11 07:35:36 -0400 | [diff] [blame] | 601 | if (next->addr == IMX274_TABLE_END) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 602 | break; |
| 603 | |
Luca Ceresoli | 7ff6786 | 2018-06-11 07:35:36 -0400 | [diff] [blame] | 604 | if (next->addr == IMX274_TABLE_WAIT_MS) { |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 605 | msleep_range(next->val); |
| 606 | continue; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | val = next->val; |
| 611 | |
| 612 | if (range_start == -1) |
| 613 | range_start = next->addr; |
| 614 | |
| 615 | range_vals[range_count++] = val; |
| 616 | } |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static inline int imx274_read_reg(struct stimx274 *priv, u16 addr, u8 *val) |
| 621 | { |
Luca Ceresoli | cea8c007 | 2018-11-26 11:35:07 -0500 | [diff] [blame] | 622 | unsigned int uint_val; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 623 | int err; |
| 624 | |
Luca Ceresoli | cea8c007 | 2018-11-26 11:35:07 -0500 | [diff] [blame] | 625 | err = regmap_read(priv->regmap, addr, &uint_val); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 626 | if (err) |
| 627 | dev_err(&priv->client->dev, |
| 628 | "%s : i2c read failed, addr = %x\n", __func__, addr); |
| 629 | else |
| 630 | dev_dbg(&priv->client->dev, |
| 631 | "%s : addr 0x%x, val=0x%x\n", __func__, |
Luca Ceresoli | cea8c007 | 2018-11-26 11:35:07 -0500 | [diff] [blame] | 632 | addr, uint_val); |
| 633 | |
| 634 | *val = uint_val; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 635 | return err; |
| 636 | } |
| 637 | |
| 638 | static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val) |
| 639 | { |
| 640 | int err; |
| 641 | |
| 642 | err = regmap_write(priv->regmap, addr, val); |
| 643 | if (err) |
| 644 | dev_err(&priv->client->dev, |
| 645 | "%s : i2c write failed, %x = %x\n", __func__, |
| 646 | addr, val); |
| 647 | else |
| 648 | dev_dbg(&priv->client->dev, |
| 649 | "%s : addr 0x%x, val=0x%x\n", __func__, |
| 650 | addr, val); |
| 651 | return err; |
| 652 | } |
| 653 | |
Luca Ceresoli | 279b4b9a | 2018-07-25 12:24:54 -0400 | [diff] [blame] | 654 | /** |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 655 | * Read a multibyte register. |
| 656 | * |
| 657 | * Uses a bulk read where possible. |
| 658 | * |
| 659 | * @priv: Pointer to device structure |
| 660 | * @addr: Address of the LSB register. Other registers must be |
| 661 | * consecutive, least-to-most significant. |
| 662 | * @val: Pointer to store the register value (cpu endianness) |
| 663 | * @nbytes: Number of bytes to read (range: [1..3]). |
| 664 | * Other bytes are zet to 0. |
| 665 | * |
| 666 | * Return: 0 on success, errors otherwise |
| 667 | */ |
| 668 | static int imx274_read_mbreg(struct stimx274 *priv, u16 addr, u32 *val, |
| 669 | size_t nbytes) |
| 670 | { |
| 671 | __le32 val_le = 0; |
| 672 | int err; |
| 673 | |
| 674 | err = regmap_bulk_read(priv->regmap, addr, &val_le, nbytes); |
| 675 | if (err) { |
| 676 | dev_err(&priv->client->dev, |
| 677 | "%s : i2c bulk read failed, %x (%zu bytes)\n", |
| 678 | __func__, addr, nbytes); |
| 679 | } else { |
| 680 | *val = le32_to_cpu(val_le); |
| 681 | dev_dbg(&priv->client->dev, |
| 682 | "%s : addr 0x%x, val=0x%x (%zu bytes)\n", |
| 683 | __func__, addr, *val, nbytes); |
| 684 | } |
| 685 | |
| 686 | return err; |
| 687 | } |
| 688 | |
| 689 | /** |
Luca Ceresoli | 279b4b9a | 2018-07-25 12:24:54 -0400 | [diff] [blame] | 690 | * Write a multibyte register. |
| 691 | * |
| 692 | * Uses a bulk write where possible. |
| 693 | * |
| 694 | * @priv: Pointer to device structure |
| 695 | * @addr: Address of the LSB register. Other registers must be |
| 696 | * consecutive, least-to-most significant. |
| 697 | * @val: Value to be written to the register (cpu endianness) |
Luca Ceresoli | 1657c28 | 2018-08-24 12:35:23 -0400 | [diff] [blame] | 698 | * @nbytes: Number of bytes to write (range: [1..3]) |
Luca Ceresoli | 279b4b9a | 2018-07-25 12:24:54 -0400 | [diff] [blame] | 699 | */ |
| 700 | static int imx274_write_mbreg(struct stimx274 *priv, u16 addr, u32 val, |
| 701 | size_t nbytes) |
| 702 | { |
| 703 | __le32 val_le = cpu_to_le32(val); |
| 704 | int err; |
| 705 | |
| 706 | err = regmap_bulk_write(priv->regmap, addr, &val_le, nbytes); |
| 707 | if (err) |
| 708 | dev_err(&priv->client->dev, |
| 709 | "%s : i2c bulk write failed, %x = %x (%zu bytes)\n", |
| 710 | __func__, addr, val, nbytes); |
| 711 | else |
| 712 | dev_dbg(&priv->client->dev, |
| 713 | "%s : addr 0x%x, val=0x%x (%zu bytes)\n", |
| 714 | __func__, addr, val, nbytes); |
| 715 | return err; |
| 716 | } |
| 717 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 718 | /* |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 719 | * Set mode registers to start stream. |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 720 | * @priv: Pointer to device structure |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 721 | * |
| 722 | * Return: 0 on success, errors otherwise |
| 723 | */ |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 724 | static int imx274_mode_regs(struct stimx274 *priv) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 725 | { |
| 726 | int err = 0; |
| 727 | |
Luca Ceresoli | 8ed8bba70 | 2018-04-24 04:24:11 -0400 | [diff] [blame] | 728 | err = imx274_write_table(priv, imx274_start_1); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 729 | if (err) |
| 730 | return err; |
| 731 | |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 732 | err = imx274_write_table(priv, priv->mode->init_regs); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 733 | |
| 734 | return err; |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * imx274_start_stream - Function for starting stream per mode index |
| 739 | * @priv: Pointer to device structure |
| 740 | * |
| 741 | * Return: 0 on success, errors otherwise |
| 742 | */ |
| 743 | static int imx274_start_stream(struct stimx274 *priv) |
| 744 | { |
| 745 | int err = 0; |
| 746 | |
| 747 | /* |
| 748 | * Refer to "Standby Cancel Sequence when using CSI-2" in |
| 749 | * imx274 datasheet, it should wait 10ms or more here. |
| 750 | * give it 1 extra ms for margin |
| 751 | */ |
| 752 | msleep_range(11); |
Luca Ceresoli | 7d2332c | 2018-08-24 12:35:20 -0400 | [diff] [blame] | 753 | err = imx274_write_table(priv, imx274_start_2); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 754 | if (err) |
| 755 | return err; |
| 756 | |
| 757 | /* |
| 758 | * Refer to "Standby Cancel Sequence when using CSI-2" in |
| 759 | * imx274 datasheet, it should wait 7ms or more here. |
| 760 | * give it 1 extra ms for margin |
| 761 | */ |
| 762 | msleep_range(8); |
Luca Ceresoli | 7d2332c | 2018-08-24 12:35:20 -0400 | [diff] [blame] | 763 | err = imx274_write_table(priv, imx274_start_3); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 764 | if (err) |
| 765 | return err; |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | /* |
| 771 | * imx274_reset - Function called to reset the sensor |
| 772 | * @priv: Pointer to device structure |
| 773 | * @rst: Input value for determining the sensor's end state after reset |
| 774 | * |
| 775 | * Set the senor in reset and then |
| 776 | * if rst = 0, keep it in reset; |
| 777 | * if rst = 1, bring it out of reset. |
| 778 | * |
| 779 | */ |
| 780 | static void imx274_reset(struct stimx274 *priv, int rst) |
| 781 | { |
| 782 | gpiod_set_value_cansleep(priv->reset_gpio, 0); |
| 783 | usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2); |
| 784 | gpiod_set_value_cansleep(priv->reset_gpio, !!rst); |
| 785 | usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * imx274_s_ctrl - This is used to set the imx274 V4L2 controls |
| 790 | * @ctrl: V4L2 control to be set |
| 791 | * |
| 792 | * This function is used to set the V4L2 controls for the imx274 sensor. |
| 793 | * |
| 794 | * Return: 0 on success, errors otherwise |
| 795 | */ |
| 796 | static int imx274_s_ctrl(struct v4l2_ctrl *ctrl) |
| 797 | { |
| 798 | struct v4l2_subdev *sd = ctrl_to_sd(ctrl); |
| 799 | struct stimx274 *imx274 = to_imx274(sd); |
| 800 | int ret = -EINVAL; |
| 801 | |
| 802 | dev_dbg(&imx274->client->dev, |
| 803 | "%s : s_ctrl: %s, value: %d\n", __func__, |
| 804 | ctrl->name, ctrl->val); |
| 805 | |
| 806 | switch (ctrl->id) { |
| 807 | case V4L2_CID_EXPOSURE: |
| 808 | dev_dbg(&imx274->client->dev, |
| 809 | "%s : set V4L2_CID_EXPOSURE\n", __func__); |
| 810 | ret = imx274_set_exposure(imx274, ctrl->val); |
| 811 | break; |
| 812 | |
| 813 | case V4L2_CID_GAIN: |
| 814 | dev_dbg(&imx274->client->dev, |
| 815 | "%s : set V4L2_CID_GAIN\n", __func__); |
| 816 | ret = imx274_set_gain(imx274, ctrl); |
| 817 | break; |
| 818 | |
| 819 | case V4L2_CID_VFLIP: |
| 820 | dev_dbg(&imx274->client->dev, |
| 821 | "%s : set V4L2_CID_VFLIP\n", __func__); |
| 822 | ret = imx274_set_vflip(imx274, ctrl->val); |
| 823 | break; |
| 824 | |
| 825 | case V4L2_CID_TEST_PATTERN: |
| 826 | dev_dbg(&imx274->client->dev, |
| 827 | "%s : set V4L2_CID_TEST_PATTERN\n", __func__); |
| 828 | ret = imx274_set_test_pattern(imx274, ctrl->val); |
| 829 | break; |
| 830 | } |
| 831 | |
| 832 | return ret; |
| 833 | } |
| 834 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 835 | static int imx274_binning_goodness(struct stimx274 *imx274, |
| 836 | int w, int ask_w, |
| 837 | int h, int ask_h, u32 flags) |
| 838 | { |
| 839 | struct device *dev = &imx274->client->dev; |
| 840 | const int goodness = 100000; |
| 841 | int val = 0; |
| 842 | |
| 843 | if (flags & V4L2_SEL_FLAG_GE) { |
| 844 | if (w < ask_w) |
| 845 | val -= goodness; |
| 846 | if (h < ask_h) |
| 847 | val -= goodness; |
| 848 | } |
| 849 | |
| 850 | if (flags & V4L2_SEL_FLAG_LE) { |
| 851 | if (w > ask_w) |
| 852 | val -= goodness; |
| 853 | if (h > ask_h) |
| 854 | val -= goodness; |
| 855 | } |
| 856 | |
| 857 | val -= abs(w - ask_w); |
| 858 | val -= abs(h - ask_h); |
| 859 | |
| 860 | dev_dbg(dev, "%s: ask %dx%d, size %dx%d, goodness %d\n", |
| 861 | __func__, ask_w, ask_h, w, h, val); |
| 862 | |
| 863 | return val; |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * Helper function to change binning and set both compose and format. |
| 868 | * |
| 869 | * We have two entry points to change binning: set_fmt and |
| 870 | * set_selection(COMPOSE). Both have to compute the new output size |
| 871 | * and set it in both the compose rect and the frame format size. We |
| 872 | * also need to do the same things after setting cropping to restore |
| 873 | * 1:1 binning. |
| 874 | * |
| 875 | * This function contains the common code for these three cases, it |
| 876 | * has many arguments in order to accommodate the needs of all of |
| 877 | * them. |
| 878 | * |
| 879 | * Must be called with imx274->lock locked. |
| 880 | * |
| 881 | * @imx274: The device object |
| 882 | * @cfg: The pad config we are editing for TRY requests |
| 883 | * @which: V4L2_SUBDEV_FORMAT_ACTIVE or V4L2_SUBDEV_FORMAT_TRY from the caller |
| 884 | * @width: Input-output parameter: set to the desired width before |
| 885 | * the call, contains the chosen value after returning successfully |
| 886 | * @height: Input-output parameter for height (see @width) |
| 887 | * @flags: Selection flags from struct v4l2_subdev_selection, or 0 if not |
| 888 | * available (when called from set_fmt) |
| 889 | */ |
| 890 | static int __imx274_change_compose(struct stimx274 *imx274, |
| 891 | struct v4l2_subdev_pad_config *cfg, |
| 892 | u32 which, |
| 893 | u32 *width, |
| 894 | u32 *height, |
| 895 | u32 flags) |
| 896 | { |
| 897 | struct device *dev = &imx274->client->dev; |
| 898 | const struct v4l2_rect *cur_crop; |
| 899 | struct v4l2_mbus_framefmt *tgt_fmt; |
| 900 | unsigned int i; |
Luca Ceresoli | 9648cb5 | 2018-08-24 12:35:22 -0400 | [diff] [blame] | 901 | const struct imx274_mode *best_mode = &imx274_modes[0]; |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 902 | int best_goodness = INT_MIN; |
| 903 | |
| 904 | if (which == V4L2_SUBDEV_FORMAT_TRY) { |
| 905 | cur_crop = &cfg->try_crop; |
| 906 | tgt_fmt = &cfg->try_fmt; |
| 907 | } else { |
| 908 | cur_crop = &imx274->crop; |
| 909 | tgt_fmt = &imx274->format; |
| 910 | } |
| 911 | |
Luca Ceresoli | 9648cb5 | 2018-08-24 12:35:22 -0400 | [diff] [blame] | 912 | for (i = 0; i < ARRAY_SIZE(imx274_modes); i++) { |
| 913 | unsigned int ratio = imx274_modes[i].bin_ratio; |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 914 | |
| 915 | int goodness = imx274_binning_goodness( |
| 916 | imx274, |
| 917 | cur_crop->width / ratio, *width, |
| 918 | cur_crop->height / ratio, *height, |
| 919 | flags); |
| 920 | |
| 921 | if (goodness >= best_goodness) { |
| 922 | best_goodness = goodness; |
Luca Ceresoli | 9648cb5 | 2018-08-24 12:35:22 -0400 | [diff] [blame] | 923 | best_mode = &imx274_modes[i]; |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
| 927 | *width = cur_crop->width / best_mode->bin_ratio; |
| 928 | *height = cur_crop->height / best_mode->bin_ratio; |
| 929 | |
| 930 | if (which == V4L2_SUBDEV_FORMAT_ACTIVE) |
| 931 | imx274->mode = best_mode; |
| 932 | |
| 933 | dev_dbg(dev, "%s: selected %u:1 binning\n", |
| 934 | __func__, best_mode->bin_ratio); |
| 935 | |
| 936 | tgt_fmt->width = *width; |
| 937 | tgt_fmt->height = *height; |
| 938 | tgt_fmt->field = V4L2_FIELD_NONE; |
| 939 | |
| 940 | return 0; |
| 941 | } |
| 942 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 943 | /** |
| 944 | * imx274_get_fmt - Get the pad format |
| 945 | * @sd: Pointer to V4L2 Sub device structure |
| 946 | * @cfg: Pointer to sub device pad information structure |
| 947 | * @fmt: Pointer to pad level media bus format |
| 948 | * |
| 949 | * This function is used to get the pad format information. |
| 950 | * |
| 951 | * Return: 0 on success |
| 952 | */ |
| 953 | static int imx274_get_fmt(struct v4l2_subdev *sd, |
| 954 | struct v4l2_subdev_pad_config *cfg, |
| 955 | struct v4l2_subdev_format *fmt) |
| 956 | { |
| 957 | struct stimx274 *imx274 = to_imx274(sd); |
| 958 | |
| 959 | mutex_lock(&imx274->lock); |
| 960 | fmt->format = imx274->format; |
| 961 | mutex_unlock(&imx274->lock); |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * imx274_set_fmt - This is used to set the pad format |
| 967 | * @sd: Pointer to V4L2 Sub device structure |
| 968 | * @cfg: Pointer to sub device pad information structure |
| 969 | * @format: Pointer to pad level media bus format |
| 970 | * |
| 971 | * This function is used to set the pad format. |
| 972 | * |
| 973 | * Return: 0 on success |
| 974 | */ |
| 975 | static int imx274_set_fmt(struct v4l2_subdev *sd, |
| 976 | struct v4l2_subdev_pad_config *cfg, |
| 977 | struct v4l2_subdev_format *format) |
| 978 | { |
| 979 | struct v4l2_mbus_framefmt *fmt = &format->format; |
| 980 | struct stimx274 *imx274 = to_imx274(sd); |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 981 | int err = 0; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 982 | |
| 983 | mutex_lock(&imx274->lock); |
| 984 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 985 | err = __imx274_change_compose(imx274, cfg, format->which, |
| 986 | &fmt->width, &fmt->height, 0); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 987 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 988 | if (err) |
| 989 | goto out; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 990 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 991 | /* |
| 992 | * __imx274_change_compose already set width and height in the |
| 993 | * applicable format, but we need to keep all other format |
| 994 | * values, so do a full copy here |
| 995 | */ |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 996 | fmt->field = V4L2_FIELD_NONE; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 997 | if (format->which == V4L2_SUBDEV_FORMAT_TRY) |
| 998 | cfg->try_fmt = *fmt; |
| 999 | else |
| 1000 | imx274->format = *fmt; |
| 1001 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 1002 | out: |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1003 | mutex_unlock(&imx274->lock); |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 1004 | |
| 1005 | return err; |
| 1006 | } |
| 1007 | |
| 1008 | static int imx274_get_selection(struct v4l2_subdev *sd, |
| 1009 | struct v4l2_subdev_pad_config *cfg, |
| 1010 | struct v4l2_subdev_selection *sel) |
| 1011 | { |
| 1012 | struct stimx274 *imx274 = to_imx274(sd); |
| 1013 | const struct v4l2_rect *src_crop; |
| 1014 | const struct v4l2_mbus_framefmt *src_fmt; |
| 1015 | int ret = 0; |
| 1016 | |
| 1017 | if (sel->pad != 0) |
| 1018 | return -EINVAL; |
| 1019 | |
| 1020 | if (sel->target == V4L2_SEL_TGT_CROP_BOUNDS) { |
| 1021 | sel->r.left = 0; |
| 1022 | sel->r.top = 0; |
| 1023 | sel->r.width = IMX274_MAX_WIDTH; |
| 1024 | sel->r.height = IMX274_MAX_HEIGHT; |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
| 1028 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) { |
| 1029 | src_crop = &cfg->try_crop; |
| 1030 | src_fmt = &cfg->try_fmt; |
| 1031 | } else { |
| 1032 | src_crop = &imx274->crop; |
| 1033 | src_fmt = &imx274->format; |
| 1034 | } |
| 1035 | |
| 1036 | mutex_lock(&imx274->lock); |
| 1037 | |
| 1038 | switch (sel->target) { |
| 1039 | case V4L2_SEL_TGT_CROP: |
| 1040 | sel->r = *src_crop; |
| 1041 | break; |
| 1042 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
| 1043 | sel->r.top = 0; |
| 1044 | sel->r.left = 0; |
| 1045 | sel->r.width = src_crop->width; |
| 1046 | sel->r.height = src_crop->height; |
| 1047 | break; |
| 1048 | case V4L2_SEL_TGT_COMPOSE: |
| 1049 | sel->r.top = 0; |
| 1050 | sel->r.left = 0; |
| 1051 | sel->r.width = src_fmt->width; |
| 1052 | sel->r.height = src_fmt->height; |
| 1053 | break; |
| 1054 | default: |
| 1055 | ret = -EINVAL; |
| 1056 | } |
| 1057 | |
| 1058 | mutex_unlock(&imx274->lock); |
| 1059 | |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | static int imx274_set_selection_crop(struct stimx274 *imx274, |
| 1064 | struct v4l2_subdev_pad_config *cfg, |
| 1065 | struct v4l2_subdev_selection *sel) |
| 1066 | { |
| 1067 | struct v4l2_rect *tgt_crop; |
| 1068 | struct v4l2_rect new_crop; |
| 1069 | bool size_changed; |
| 1070 | |
| 1071 | /* |
| 1072 | * h_step could be 12 or 24 depending on the binning. But we |
| 1073 | * won't know the binning until we choose the mode later in |
| 1074 | * __imx274_change_compose(). Thus let's be safe and use the |
| 1075 | * most conservative value in all cases. |
| 1076 | */ |
| 1077 | const u32 h_step = 24; |
| 1078 | |
| 1079 | new_crop.width = min_t(u32, |
| 1080 | IMX274_ROUND(sel->r.width, h_step, sel->flags), |
| 1081 | IMX274_MAX_WIDTH); |
| 1082 | |
| 1083 | /* Constraint: HTRIMMING_END - HTRIMMING_START >= 144 */ |
| 1084 | if (new_crop.width < 144) |
| 1085 | new_crop.width = 144; |
| 1086 | |
| 1087 | new_crop.left = min_t(u32, |
| 1088 | IMX274_ROUND(sel->r.left, h_step, 0), |
| 1089 | IMX274_MAX_WIDTH - new_crop.width); |
| 1090 | |
| 1091 | new_crop.height = min_t(u32, |
| 1092 | IMX274_ROUND(sel->r.height, 2, sel->flags), |
| 1093 | IMX274_MAX_HEIGHT); |
| 1094 | |
| 1095 | new_crop.top = min_t(u32, IMX274_ROUND(sel->r.top, 2, 0), |
| 1096 | IMX274_MAX_HEIGHT - new_crop.height); |
| 1097 | |
| 1098 | sel->r = new_crop; |
| 1099 | |
| 1100 | if (sel->which == V4L2_SUBDEV_FORMAT_TRY) |
| 1101 | tgt_crop = &cfg->try_crop; |
| 1102 | else |
| 1103 | tgt_crop = &imx274->crop; |
| 1104 | |
| 1105 | mutex_lock(&imx274->lock); |
| 1106 | |
| 1107 | size_changed = (new_crop.width != tgt_crop->width || |
| 1108 | new_crop.height != tgt_crop->height); |
| 1109 | |
| 1110 | /* __imx274_change_compose needs the new size in *tgt_crop */ |
| 1111 | *tgt_crop = new_crop; |
| 1112 | |
| 1113 | /* if crop size changed then reset the output image size */ |
| 1114 | if (size_changed) |
| 1115 | __imx274_change_compose(imx274, cfg, sel->which, |
| 1116 | &new_crop.width, &new_crop.height, |
| 1117 | sel->flags); |
| 1118 | |
| 1119 | mutex_unlock(&imx274->lock); |
| 1120 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 1124 | static int imx274_set_selection(struct v4l2_subdev *sd, |
| 1125 | struct v4l2_subdev_pad_config *cfg, |
| 1126 | struct v4l2_subdev_selection *sel) |
| 1127 | { |
| 1128 | struct stimx274 *imx274 = to_imx274(sd); |
| 1129 | |
| 1130 | if (sel->pad != 0) |
| 1131 | return -EINVAL; |
| 1132 | |
| 1133 | if (sel->target == V4L2_SEL_TGT_CROP) |
| 1134 | return imx274_set_selection_crop(imx274, cfg, sel); |
| 1135 | |
| 1136 | if (sel->target == V4L2_SEL_TGT_COMPOSE) { |
| 1137 | int err; |
| 1138 | |
| 1139 | mutex_lock(&imx274->lock); |
| 1140 | err = __imx274_change_compose(imx274, cfg, sel->which, |
| 1141 | &sel->r.width, &sel->r.height, |
| 1142 | sel->flags); |
| 1143 | mutex_unlock(&imx274->lock); |
| 1144 | |
| 1145 | /* |
| 1146 | * __imx274_change_compose already set width and |
| 1147 | * height in set->r, we still need to set top-left |
| 1148 | */ |
| 1149 | if (!err) { |
| 1150 | sel->r.top = 0; |
| 1151 | sel->r.left = 0; |
| 1152 | } |
| 1153 | |
| 1154 | return err; |
| 1155 | } |
| 1156 | |
| 1157 | return -EINVAL; |
| 1158 | } |
| 1159 | |
| 1160 | static int imx274_apply_trimming(struct stimx274 *imx274) |
| 1161 | { |
| 1162 | u32 h_start; |
| 1163 | u32 h_end; |
| 1164 | u32 hmax; |
| 1165 | u32 v_cut; |
| 1166 | s32 v_pos; |
| 1167 | u32 write_v_size; |
| 1168 | u32 y_out_size; |
| 1169 | int err; |
| 1170 | |
| 1171 | h_start = imx274->crop.left + 12; |
| 1172 | h_end = h_start + imx274->crop.width; |
| 1173 | |
| 1174 | /* Use the minimum allowed value of HMAX */ |
| 1175 | /* Note: except in mode 1, (width / 16 + 23) is always < hmax_min */ |
| 1176 | /* Note: 260 is the minimum HMAX in all implemented modes */ |
| 1177 | hmax = max_t(u32, 260, (imx274->crop.width) / 16 + 23); |
| 1178 | |
| 1179 | /* invert v_pos if VFLIP */ |
| 1180 | v_pos = imx274->ctrls.vflip->cur.val ? |
| 1181 | (-imx274->crop.top / 2) : (imx274->crop.top / 2); |
| 1182 | v_cut = (IMX274_MAX_HEIGHT - imx274->crop.height) / 2; |
| 1183 | write_v_size = imx274->crop.height + 22; |
| 1184 | y_out_size = imx274->crop.height + 14; |
| 1185 | |
| 1186 | err = imx274_write_mbreg(imx274, IMX274_HMAX_REG_LSB, hmax, 2); |
| 1187 | if (!err) |
| 1188 | err = imx274_write_mbreg(imx274, IMX274_HTRIM_EN_REG, 1, 1); |
| 1189 | if (!err) |
| 1190 | err = imx274_write_mbreg(imx274, IMX274_HTRIM_START_REG_LSB, |
| 1191 | h_start, 2); |
| 1192 | if (!err) |
| 1193 | err = imx274_write_mbreg(imx274, IMX274_HTRIM_END_REG_LSB, |
| 1194 | h_end, 2); |
| 1195 | if (!err) |
| 1196 | err = imx274_write_mbreg(imx274, IMX274_VWIDCUTEN_REG, 1, 1); |
| 1197 | if (!err) |
| 1198 | err = imx274_write_mbreg(imx274, IMX274_VWIDCUT_REG_LSB, |
| 1199 | v_cut, 2); |
| 1200 | if (!err) |
| 1201 | err = imx274_write_mbreg(imx274, IMX274_VWINPOS_REG_LSB, |
| 1202 | v_pos, 2); |
| 1203 | if (!err) |
| 1204 | err = imx274_write_mbreg(imx274, IMX274_WRITE_VSIZE_REG_LSB, |
| 1205 | write_v_size, 2); |
| 1206 | if (!err) |
| 1207 | err = imx274_write_mbreg(imx274, IMX274_Y_OUT_SIZE_REG_LSB, |
| 1208 | y_out_size, 2); |
| 1209 | |
| 1210 | return err; |
| 1211 | } |
| 1212 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1213 | /** |
| 1214 | * imx274_g_frame_interval - Get the frame interval |
| 1215 | * @sd: Pointer to V4L2 Sub device structure |
| 1216 | * @fi: Pointer to V4l2 Sub device frame interval structure |
| 1217 | * |
| 1218 | * This function is used to get the frame interval. |
| 1219 | * |
| 1220 | * Return: 0 on success |
| 1221 | */ |
| 1222 | static int imx274_g_frame_interval(struct v4l2_subdev *sd, |
| 1223 | struct v4l2_subdev_frame_interval *fi) |
| 1224 | { |
| 1225 | struct stimx274 *imx274 = to_imx274(sd); |
| 1226 | |
| 1227 | fi->interval = imx274->frame_interval; |
| 1228 | dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n", |
| 1229 | __func__, imx274->frame_interval.numerator, |
| 1230 | imx274->frame_interval.denominator); |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | /** |
| 1236 | * imx274_s_frame_interval - Set the frame interval |
| 1237 | * @sd: Pointer to V4L2 Sub device structure |
| 1238 | * @fi: Pointer to V4l2 Sub device frame interval structure |
| 1239 | * |
| 1240 | * This function is used to set the frame intervavl. |
| 1241 | * |
| 1242 | * Return: 0 on success |
| 1243 | */ |
| 1244 | static int imx274_s_frame_interval(struct v4l2_subdev *sd, |
| 1245 | struct v4l2_subdev_frame_interval *fi) |
| 1246 | { |
| 1247 | struct stimx274 *imx274 = to_imx274(sd); |
| 1248 | struct v4l2_ctrl *ctrl = imx274->ctrls.exposure; |
| 1249 | int min, max, def; |
| 1250 | int ret; |
| 1251 | |
| 1252 | mutex_lock(&imx274->lock); |
| 1253 | ret = imx274_set_frame_interval(imx274, fi->interval); |
| 1254 | |
| 1255 | if (!ret) { |
| 1256 | /* |
| 1257 | * exposure time range is decided by frame interval |
Luca Ceresoli | 0134339 | 2018-04-24 04:24:07 -0400 | [diff] [blame] | 1258 | * need to update it after frame interval changes |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1259 | */ |
| 1260 | min = IMX274_MIN_EXPOSURE_TIME; |
| 1261 | max = fi->interval.numerator * 1000000 |
| 1262 | / fi->interval.denominator; |
| 1263 | def = max; |
| 1264 | if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def)) { |
| 1265 | dev_err(&imx274->client->dev, |
| 1266 | "Exposure ctrl range update failed\n"); |
| 1267 | goto unlock; |
| 1268 | } |
| 1269 | |
| 1270 | /* update exposure time accordingly */ |
Luca Ceresoli | cf90870 | 2018-04-24 04:24:08 -0400 | [diff] [blame] | 1271 | imx274_set_exposure(imx274, ctrl->val); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1272 | |
| 1273 | dev_dbg(&imx274->client->dev, "set frame interval to %uus\n", |
| 1274 | fi->interval.numerator * 1000000 |
| 1275 | / fi->interval.denominator); |
| 1276 | } |
| 1277 | |
| 1278 | unlock: |
| 1279 | mutex_unlock(&imx274->lock); |
| 1280 | |
| 1281 | return ret; |
| 1282 | } |
| 1283 | |
| 1284 | /** |
| 1285 | * imx274_load_default - load default control values |
| 1286 | * @priv: Pointer to device structure |
| 1287 | * |
| 1288 | * Return: 0 on success, errors otherwise |
| 1289 | */ |
| 1290 | static int imx274_load_default(struct stimx274 *priv) |
| 1291 | { |
| 1292 | int ret; |
| 1293 | |
| 1294 | /* load default control values */ |
| 1295 | priv->frame_interval.numerator = 1; |
| 1296 | priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE; |
| 1297 | priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE; |
| 1298 | priv->ctrls.gain->val = IMX274_DEF_GAIN; |
| 1299 | priv->ctrls.vflip->val = 0; |
| 1300 | priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED; |
| 1301 | |
| 1302 | /* update frame rate */ |
| 1303 | ret = imx274_set_frame_interval(priv, |
| 1304 | priv->frame_interval); |
| 1305 | if (ret) |
| 1306 | return ret; |
| 1307 | |
| 1308 | /* update exposure time */ |
| 1309 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.exposure, priv->ctrls.exposure->val); |
| 1310 | if (ret) |
| 1311 | return ret; |
| 1312 | |
| 1313 | /* update gain */ |
| 1314 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.gain, priv->ctrls.gain->val); |
| 1315 | if (ret) |
| 1316 | return ret; |
| 1317 | |
| 1318 | /* update vflip */ |
| 1319 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.vflip, priv->ctrls.vflip->val); |
| 1320 | if (ret) |
| 1321 | return ret; |
| 1322 | |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
| 1326 | /** |
| 1327 | * imx274_s_stream - It is used to start/stop the streaming. |
| 1328 | * @sd: V4L2 Sub device |
| 1329 | * @on: Flag (True / False) |
| 1330 | * |
| 1331 | * This function controls the start or stop of streaming for the |
| 1332 | * imx274 sensor. |
| 1333 | * |
| 1334 | * Return: 0 on success, errors otherwise |
| 1335 | */ |
| 1336 | static int imx274_s_stream(struct v4l2_subdev *sd, int on) |
| 1337 | { |
| 1338 | struct stimx274 *imx274 = to_imx274(sd); |
| 1339 | int ret = 0; |
| 1340 | |
Luca Ceresoli | 4317322 | 2018-06-11 07:35:34 -0400 | [diff] [blame] | 1341 | dev_dbg(&imx274->client->dev, "%s : %s, mode index = %td\n", __func__, |
| 1342 | on ? "Stream Start" : "Stream Stop", |
Luca Ceresoli | 9648cb5 | 2018-08-24 12:35:22 -0400 | [diff] [blame] | 1343 | imx274->mode - &imx274_modes[0]); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1344 | |
| 1345 | mutex_lock(&imx274->lock); |
| 1346 | |
| 1347 | if (on) { |
| 1348 | /* load mode registers */ |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 1349 | ret = imx274_mode_regs(imx274); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1350 | if (ret) |
| 1351 | goto fail; |
| 1352 | |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 1353 | ret = imx274_apply_trimming(imx274); |
| 1354 | if (ret) |
| 1355 | goto fail; |
| 1356 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1357 | /* |
| 1358 | * update frame rate & expsoure. if the last mode is different, |
| 1359 | * HMAX could be changed. As the result, frame rate & exposure |
| 1360 | * are changed. |
| 1361 | * gain is not affected. |
| 1362 | */ |
| 1363 | ret = imx274_set_frame_interval(imx274, |
| 1364 | imx274->frame_interval); |
| 1365 | if (ret) |
| 1366 | goto fail; |
| 1367 | |
| 1368 | /* update exposure time */ |
| 1369 | ret = __v4l2_ctrl_s_ctrl(imx274->ctrls.exposure, |
| 1370 | imx274->ctrls.exposure->val); |
| 1371 | if (ret) |
| 1372 | goto fail; |
| 1373 | |
| 1374 | /* start stream */ |
| 1375 | ret = imx274_start_stream(imx274); |
| 1376 | if (ret) |
| 1377 | goto fail; |
| 1378 | } else { |
| 1379 | /* stop stream */ |
Luca Ceresoli | 8ed8bba70 | 2018-04-24 04:24:11 -0400 | [diff] [blame] | 1380 | ret = imx274_write_table(imx274, imx274_stop); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1381 | if (ret) |
| 1382 | goto fail; |
| 1383 | } |
| 1384 | |
| 1385 | mutex_unlock(&imx274->lock); |
Luca Ceresoli | 4317322 | 2018-06-11 07:35:34 -0400 | [diff] [blame] | 1386 | dev_dbg(&imx274->client->dev, "%s : Done\n", __func__); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1387 | return 0; |
| 1388 | |
| 1389 | fail: |
| 1390 | mutex_unlock(&imx274->lock); |
| 1391 | dev_err(&imx274->client->dev, "s_stream failed\n"); |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * imx274_get_frame_length - Function for obtaining current frame length |
| 1397 | * @priv: Pointer to device structure |
| 1398 | * @val: Pointer to obainted value |
| 1399 | * |
| 1400 | * frame_length = vmax x (svr + 1), in unit of hmax. |
| 1401 | * |
| 1402 | * Return: 0 on success |
| 1403 | */ |
| 1404 | static int imx274_get_frame_length(struct stimx274 *priv, u32 *val) |
| 1405 | { |
| 1406 | int err; |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1407 | u32 svr; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1408 | u32 vmax; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1409 | |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1410 | err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1411 | if (err) |
| 1412 | goto fail; |
| 1413 | |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1414 | err = imx274_read_mbreg(priv, IMX274_VMAX_REG_3, &vmax, 3); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1415 | if (err) |
| 1416 | goto fail; |
| 1417 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1418 | *val = vmax * (svr + 1); |
| 1419 | |
| 1420 | return 0; |
| 1421 | |
| 1422 | fail: |
| 1423 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1424 | return err; |
| 1425 | } |
| 1426 | |
| 1427 | static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val, |
| 1428 | u32 *frame_length) |
| 1429 | { |
| 1430 | int err; |
| 1431 | |
| 1432 | err = imx274_get_frame_length(priv, frame_length); |
| 1433 | if (err) |
| 1434 | return err; |
| 1435 | |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 1436 | if (*frame_length < priv->mode->min_frame_len) |
| 1437 | *frame_length = priv->mode->min_frame_len; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1438 | |
| 1439 | *val = *frame_length - *val; /* convert to raw shr */ |
| 1440 | if (*val > *frame_length - IMX274_SHR_LIMIT_CONST) |
| 1441 | *val = *frame_length - IMX274_SHR_LIMIT_CONST; |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 1442 | else if (*val < priv->mode->min_SHR) |
| 1443 | *val = priv->mode->min_SHR; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1444 | |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | /* |
| 1449 | * imx274_set_digital gain - Function called when setting digital gain |
| 1450 | * @priv: Pointer to device structure |
| 1451 | * @dgain: Value of digital gain. |
| 1452 | * |
| 1453 | * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x |
| 1454 | * |
| 1455 | * Return: 0 on success |
| 1456 | */ |
| 1457 | static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain) |
| 1458 | { |
| 1459 | u8 reg_val; |
| 1460 | |
| 1461 | reg_val = ffs(dgain); |
| 1462 | |
| 1463 | if (reg_val) |
| 1464 | reg_val--; |
| 1465 | |
| 1466 | reg_val = clamp(reg_val, (u8)0, (u8)3); |
| 1467 | |
| 1468 | return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG, |
| 1469 | reg_val & IMX274_MASK_LSB_4_BITS); |
| 1470 | } |
| 1471 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1472 | /* |
| 1473 | * imx274_set_gain - Function called when setting gain |
| 1474 | * @priv: Pointer to device structure |
| 1475 | * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT; |
| 1476 | * @ctrl: v4l2 control pointer |
| 1477 | * |
| 1478 | * Set the gain based on input value. |
| 1479 | * The caller should hold the mutex lock imx274->lock if necessary |
| 1480 | * |
| 1481 | * Return: 0 on success |
| 1482 | */ |
| 1483 | static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl) |
| 1484 | { |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1485 | int err; |
| 1486 | u32 gain, analog_gain, digital_gain, gain_reg; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1487 | |
| 1488 | gain = (u32)(ctrl->val); |
| 1489 | |
| 1490 | dev_dbg(&priv->client->dev, |
| 1491 | "%s : input gain = %d.%d\n", __func__, |
| 1492 | gain >> IMX274_GAIN_SHIFT, |
| 1493 | ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT); |
| 1494 | |
| 1495 | if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN) |
| 1496 | gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN; |
| 1497 | else if (gain < IMX274_MIN_GAIN) |
| 1498 | gain = IMX274_MIN_GAIN; |
| 1499 | |
| 1500 | if (gain <= IMX274_MAX_ANALOG_GAIN) |
| 1501 | digital_gain = 1; |
| 1502 | else if (gain <= IMX274_MAX_ANALOG_GAIN * 2) |
| 1503 | digital_gain = 2; |
| 1504 | else if (gain <= IMX274_MAX_ANALOG_GAIN * 4) |
| 1505 | digital_gain = 4; |
| 1506 | else |
| 1507 | digital_gain = IMX274_MAX_DIGITAL_GAIN; |
| 1508 | |
| 1509 | analog_gain = gain / digital_gain; |
| 1510 | |
| 1511 | dev_dbg(&priv->client->dev, |
| 1512 | "%s : digital gain = %d, analog gain = %d.%d\n", |
| 1513 | __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT, |
| 1514 | ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100) |
| 1515 | >> IMX274_GAIN_SHIFT); |
| 1516 | |
| 1517 | err = imx274_set_digital_gain(priv, digital_gain); |
| 1518 | if (err) |
| 1519 | goto fail; |
| 1520 | |
| 1521 | /* convert to register value, refer to imx274 datasheet */ |
| 1522 | gain_reg = (u32)IMX274_GAIN_CONST - |
| 1523 | (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain; |
| 1524 | if (gain_reg > IMX274_GAIN_REG_MAX) |
| 1525 | gain_reg = IMX274_GAIN_REG_MAX; |
| 1526 | |
Luca Ceresoli | 279b4b9a | 2018-07-25 12:24:54 -0400 | [diff] [blame] | 1527 | err = imx274_write_mbreg(priv, IMX274_ANALOG_GAIN_ADDR_LSB, gain_reg, |
| 1528 | 2); |
| 1529 | if (err) |
| 1530 | goto fail; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1531 | |
| 1532 | if (IMX274_GAIN_CONST - gain_reg == 0) { |
| 1533 | err = -EINVAL; |
| 1534 | goto fail; |
| 1535 | } |
| 1536 | |
| 1537 | /* convert register value back to gain value */ |
| 1538 | ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) |
| 1539 | / (IMX274_GAIN_CONST - gain_reg) * digital_gain; |
| 1540 | |
| 1541 | dev_dbg(&priv->client->dev, |
| 1542 | "%s : GAIN control success, gain_reg = %d, new gain = %d\n", |
| 1543 | __func__, gain_reg, ctrl->val); |
| 1544 | |
| 1545 | return 0; |
| 1546 | |
| 1547 | fail: |
| 1548 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1549 | return err; |
| 1550 | } |
| 1551 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1552 | /* |
| 1553 | * imx274_set_coarse_time - Function called when setting SHR value |
| 1554 | * @priv: Pointer to device structure |
| 1555 | * @val: Value for exposure time in number of line_length, or [HMAX] |
| 1556 | * |
| 1557 | * Set SHR value based on input value. |
| 1558 | * |
| 1559 | * Return: 0 on success |
| 1560 | */ |
| 1561 | static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val) |
| 1562 | { |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1563 | int err; |
| 1564 | u32 coarse_time, frame_length; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1565 | |
| 1566 | coarse_time = *val; |
| 1567 | |
| 1568 | /* convert exposure_time to appropriate SHR value */ |
| 1569 | err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length); |
| 1570 | if (err) |
| 1571 | goto fail; |
| 1572 | |
Luca Ceresoli | 279b4b9a | 2018-07-25 12:24:54 -0400 | [diff] [blame] | 1573 | err = imx274_write_mbreg(priv, IMX274_SHR_REG_LSB, coarse_time, 2); |
| 1574 | if (err) |
| 1575 | goto fail; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1576 | |
| 1577 | *val = frame_length - coarse_time; |
| 1578 | return 0; |
| 1579 | |
| 1580 | fail: |
| 1581 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1582 | return err; |
| 1583 | } |
| 1584 | |
| 1585 | /* |
| 1586 | * imx274_set_exposure - Function called when setting exposure time |
| 1587 | * @priv: Pointer to device structure |
| 1588 | * @val: Variable for exposure time, in the unit of micro-second |
| 1589 | * |
| 1590 | * Set exposure time based on input value. |
| 1591 | * The caller should hold the mutex lock imx274->lock if necessary |
| 1592 | * |
| 1593 | * Return: 0 on success |
| 1594 | */ |
| 1595 | static int imx274_set_exposure(struct stimx274 *priv, int val) |
| 1596 | { |
| 1597 | int err; |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1598 | u32 hmax; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1599 | u32 coarse_time; /* exposure time in unit of line (HMAX)*/ |
| 1600 | |
| 1601 | dev_dbg(&priv->client->dev, |
| 1602 | "%s : EXPOSURE control input = %d\n", __func__, val); |
| 1603 | |
| 1604 | /* step 1: convert input exposure_time (val) into number of 1[HMAX] */ |
| 1605 | |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1606 | err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1607 | if (err) |
| 1608 | goto fail; |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1609 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1610 | if (hmax == 0) { |
| 1611 | err = -EINVAL; |
| 1612 | goto fail; |
| 1613 | } |
| 1614 | |
| 1615 | coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 1616 | - priv->mode->nocpiop) / hmax; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1617 | |
| 1618 | /* step 2: convert exposure_time into SHR value */ |
| 1619 | |
| 1620 | /* set SHR */ |
| 1621 | err = imx274_set_coarse_time(priv, &coarse_time); |
| 1622 | if (err) |
| 1623 | goto fail; |
| 1624 | |
| 1625 | priv->ctrls.exposure->val = |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 1626 | (coarse_time * hmax + priv->mode->nocpiop) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1627 | / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2); |
| 1628 | |
| 1629 | dev_dbg(&priv->client->dev, |
| 1630 | "%s : EXPOSURE control success\n", __func__); |
| 1631 | return 0; |
| 1632 | |
| 1633 | fail: |
| 1634 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1635 | |
| 1636 | return err; |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * imx274_set_vflip - Function called when setting vertical flip |
| 1641 | * @priv: Pointer to device structure |
| 1642 | * @val: Value for vflip setting |
| 1643 | * |
| 1644 | * Set vertical flip based on input value. |
| 1645 | * val = 0: normal, no vertical flip |
| 1646 | * val = 1: vertical flip enabled |
| 1647 | * The caller should hold the mutex lock imx274->lock if necessary |
| 1648 | * |
| 1649 | * Return: 0 on success |
| 1650 | */ |
| 1651 | static int imx274_set_vflip(struct stimx274 *priv, int val) |
| 1652 | { |
| 1653 | int err; |
| 1654 | |
| 1655 | err = imx274_write_reg(priv, IMX274_VFLIP_REG, val); |
| 1656 | if (err) { |
Luca Ceresoli | 9f67a5e | 2018-02-23 03:57:15 -0500 | [diff] [blame] | 1657 | dev_err(&priv->client->dev, "VFLIP control error\n"); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1658 | return err; |
| 1659 | } |
| 1660 | |
| 1661 | dev_dbg(&priv->client->dev, |
| 1662 | "%s : VFLIP control success\n", __func__); |
| 1663 | |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * imx274_set_test_pattern - Function called when setting test pattern |
| 1669 | * @priv: Pointer to device structure |
| 1670 | * @val: Variable for test pattern |
| 1671 | * |
| 1672 | * Set to different test patterns based on input value. |
| 1673 | * |
| 1674 | * Return: 0 on success |
| 1675 | */ |
| 1676 | static int imx274_set_test_pattern(struct stimx274 *priv, int val) |
| 1677 | { |
| 1678 | int err = 0; |
| 1679 | |
| 1680 | if (val == TEST_PATTERN_DISABLED) { |
| 1681 | err = imx274_write_table(priv, imx274_tp_disabled); |
| 1682 | } else if (val <= TEST_PATTERN_V_COLOR_BARS) { |
| 1683 | err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1); |
| 1684 | if (!err) |
| 1685 | err = imx274_write_table(priv, imx274_tp_regs); |
| 1686 | } else { |
| 1687 | err = -EINVAL; |
| 1688 | } |
| 1689 | |
| 1690 | if (!err) |
| 1691 | dev_dbg(&priv->client->dev, |
| 1692 | "%s : TEST PATTERN control success\n", __func__); |
| 1693 | else |
| 1694 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1695 | |
| 1696 | return err; |
| 1697 | } |
| 1698 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1699 | /* |
| 1700 | * imx274_set_frame_length - Function called when setting frame length |
| 1701 | * @priv: Pointer to device structure |
| 1702 | * @val: Variable for frame length (= VMAX, i.e. vertical drive period length) |
| 1703 | * |
| 1704 | * Set frame length based on input value. |
| 1705 | * |
| 1706 | * Return: 0 on success |
| 1707 | */ |
| 1708 | static int imx274_set_frame_length(struct stimx274 *priv, u32 val) |
| 1709 | { |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1710 | int err; |
| 1711 | u32 frame_length; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1712 | |
| 1713 | dev_dbg(&priv->client->dev, "%s : input length = %d\n", |
| 1714 | __func__, val); |
| 1715 | |
| 1716 | frame_length = (u32)val; |
| 1717 | |
Luca Ceresoli | 279b4b9a | 2018-07-25 12:24:54 -0400 | [diff] [blame] | 1718 | err = imx274_write_mbreg(priv, IMX274_VMAX_REG_3, frame_length, 3); |
| 1719 | if (err) |
| 1720 | goto fail; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1721 | |
| 1722 | return 0; |
| 1723 | |
| 1724 | fail: |
| 1725 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1726 | return err; |
| 1727 | } |
| 1728 | |
| 1729 | /* |
| 1730 | * imx274_set_frame_interval - Function called when setting frame interval |
| 1731 | * @priv: Pointer to device structure |
| 1732 | * @frame_interval: Variable for frame interval |
| 1733 | * |
| 1734 | * Change frame interval by updating VMAX value |
| 1735 | * The caller should hold the mutex lock imx274->lock if necessary |
| 1736 | * |
| 1737 | * Return: 0 on success |
| 1738 | */ |
| 1739 | static int imx274_set_frame_interval(struct stimx274 *priv, |
| 1740 | struct v4l2_fract frame_interval) |
| 1741 | { |
| 1742 | int err; |
| 1743 | u32 frame_length, req_frame_rate; |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1744 | u32 svr; |
| 1745 | u32 hmax; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1746 | |
| 1747 | dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d", |
| 1748 | __func__, frame_interval.numerator, |
| 1749 | frame_interval.denominator); |
| 1750 | |
| 1751 | if (frame_interval.numerator == 0) { |
| 1752 | err = -EINVAL; |
| 1753 | goto fail; |
| 1754 | } |
| 1755 | |
| 1756 | req_frame_rate = (u32)(frame_interval.denominator |
| 1757 | / frame_interval.numerator); |
| 1758 | |
| 1759 | /* boundary check */ |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 1760 | if (req_frame_rate > priv->mode->max_fps) { |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1761 | frame_interval.numerator = 1; |
Luca Ceresoli | 96a2c73 | 2018-06-11 07:35:33 -0400 | [diff] [blame] | 1762 | frame_interval.denominator = priv->mode->max_fps; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1763 | } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) { |
| 1764 | frame_interval.numerator = 1; |
| 1765 | frame_interval.denominator = IMX274_MIN_FRAME_RATE; |
| 1766 | } |
| 1767 | |
| 1768 | /* |
| 1769 | * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX |
| 1770 | * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX |
| 1771 | */ |
| 1772 | |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1773 | err = imx274_read_mbreg(priv, IMX274_SVR_REG_LSB, &svr, 2); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1774 | if (err) |
| 1775 | goto fail; |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1776 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1777 | dev_dbg(&priv->client->dev, |
| 1778 | "%s : register SVR = %d\n", __func__, svr); |
| 1779 | |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1780 | err = imx274_read_mbreg(priv, IMX274_HMAX_REG_LSB, &hmax, 2); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1781 | if (err) |
| 1782 | goto fail; |
Luca Ceresoli | ca017467 | 2018-08-24 12:35:24 -0400 | [diff] [blame] | 1783 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1784 | dev_dbg(&priv->client->dev, |
| 1785 | "%s : register HMAX = %d\n", __func__, hmax); |
| 1786 | |
| 1787 | if (hmax == 0 || frame_interval.denominator == 0) { |
| 1788 | err = -EINVAL; |
| 1789 | goto fail; |
| 1790 | } |
| 1791 | |
| 1792 | frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax |
| 1793 | * frame_interval.numerator |
| 1794 | / frame_interval.denominator; |
| 1795 | |
| 1796 | err = imx274_set_frame_length(priv, frame_length); |
| 1797 | if (err) |
| 1798 | goto fail; |
| 1799 | |
| 1800 | priv->frame_interval = frame_interval; |
| 1801 | return 0; |
| 1802 | |
| 1803 | fail: |
| 1804 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1805 | return err; |
| 1806 | } |
| 1807 | |
| 1808 | static const struct v4l2_subdev_pad_ops imx274_pad_ops = { |
| 1809 | .get_fmt = imx274_get_fmt, |
| 1810 | .set_fmt = imx274_set_fmt, |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 1811 | .get_selection = imx274_get_selection, |
| 1812 | .set_selection = imx274_set_selection, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1813 | }; |
| 1814 | |
| 1815 | static const struct v4l2_subdev_video_ops imx274_video_ops = { |
| 1816 | .g_frame_interval = imx274_g_frame_interval, |
| 1817 | .s_frame_interval = imx274_s_frame_interval, |
| 1818 | .s_stream = imx274_s_stream, |
| 1819 | }; |
| 1820 | |
| 1821 | static const struct v4l2_subdev_ops imx274_subdev_ops = { |
| 1822 | .pad = &imx274_pad_ops, |
| 1823 | .video = &imx274_video_ops, |
| 1824 | }; |
| 1825 | |
| 1826 | static const struct v4l2_ctrl_ops imx274_ctrl_ops = { |
| 1827 | .s_ctrl = imx274_s_ctrl, |
| 1828 | }; |
| 1829 | |
| 1830 | static const struct of_device_id imx274_of_id_table[] = { |
| 1831 | { .compatible = "sony,imx274" }, |
| 1832 | { } |
| 1833 | }; |
| 1834 | MODULE_DEVICE_TABLE(of, imx274_of_id_table); |
| 1835 | |
| 1836 | static const struct i2c_device_id imx274_id[] = { |
| 1837 | { "IMX274", 0 }, |
| 1838 | { } |
| 1839 | }; |
| 1840 | MODULE_DEVICE_TABLE(i2c, imx274_id); |
| 1841 | |
| 1842 | static int imx274_probe(struct i2c_client *client, |
| 1843 | const struct i2c_device_id *id) |
| 1844 | { |
| 1845 | struct v4l2_subdev *sd; |
| 1846 | struct stimx274 *imx274; |
| 1847 | int ret; |
| 1848 | |
| 1849 | /* initialize imx274 */ |
| 1850 | imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL); |
| 1851 | if (!imx274) |
| 1852 | return -ENOMEM; |
| 1853 | |
| 1854 | mutex_init(&imx274->lock); |
| 1855 | |
Luca Ceresoli | 438ac1f | 2018-06-11 07:35:32 -0400 | [diff] [blame] | 1856 | /* initialize format */ |
Luca Ceresoli | 9648cb5 | 2018-08-24 12:35:22 -0400 | [diff] [blame] | 1857 | imx274->mode = &imx274_modes[IMX274_DEFAULT_BINNING]; |
Luca Ceresoli | 39dd23d | 2018-07-25 12:24:55 -0400 | [diff] [blame] | 1858 | imx274->crop.width = IMX274_MAX_WIDTH; |
| 1859 | imx274->crop.height = IMX274_MAX_HEIGHT; |
| 1860 | imx274->format.width = imx274->crop.width / imx274->mode->bin_ratio; |
| 1861 | imx274->format.height = imx274->crop.height / imx274->mode->bin_ratio; |
Luca Ceresoli | 438ac1f | 2018-06-11 07:35:32 -0400 | [diff] [blame] | 1862 | imx274->format.field = V4L2_FIELD_NONE; |
| 1863 | imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10; |
| 1864 | imx274->format.colorspace = V4L2_COLORSPACE_SRGB; |
| 1865 | imx274->frame_interval.numerator = 1; |
| 1866 | imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE; |
| 1867 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1868 | /* initialize regmap */ |
| 1869 | imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config); |
| 1870 | if (IS_ERR(imx274->regmap)) { |
| 1871 | dev_err(&client->dev, |
| 1872 | "regmap init failed: %ld\n", PTR_ERR(imx274->regmap)); |
| 1873 | ret = -ENODEV; |
| 1874 | goto err_regmap; |
| 1875 | } |
| 1876 | |
| 1877 | /* initialize subdevice */ |
| 1878 | imx274->client = client; |
| 1879 | sd = &imx274->sd; |
| 1880 | v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1881 | sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; |
| 1882 | |
| 1883 | /* initialize subdev media pad */ |
| 1884 | imx274->pad.flags = MEDIA_PAD_FL_SOURCE; |
| 1885 | sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; |
| 1886 | ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad); |
| 1887 | if (ret < 0) { |
| 1888 | dev_err(&client->dev, |
| 1889 | "%s : media entity init Failed %d\n", __func__, ret); |
| 1890 | goto err_regmap; |
| 1891 | } |
| 1892 | |
| 1893 | /* initialize sensor reset gpio */ |
| 1894 | imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", |
| 1895 | GPIOD_OUT_HIGH); |
| 1896 | if (IS_ERR(imx274->reset_gpio)) { |
| 1897 | if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER) |
| 1898 | dev_err(&client->dev, "Reset GPIO not setup in DT"); |
| 1899 | ret = PTR_ERR(imx274->reset_gpio); |
| 1900 | goto err_me; |
| 1901 | } |
| 1902 | |
| 1903 | /* pull sensor out of reset */ |
| 1904 | imx274_reset(imx274, 1); |
| 1905 | |
| 1906 | /* initialize controls */ |
Luca Ceresoli | 82f5b50 | 2018-11-27 03:34:44 -0500 | [diff] [blame] | 1907 | ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 4); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1908 | if (ret < 0) { |
| 1909 | dev_err(&client->dev, |
| 1910 | "%s : ctrl handler init Failed\n", __func__); |
| 1911 | goto err_me; |
| 1912 | } |
| 1913 | |
| 1914 | imx274->ctrls.handler.lock = &imx274->lock; |
| 1915 | |
| 1916 | /* add new controls */ |
| 1917 | imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items( |
| 1918 | &imx274->ctrls.handler, &imx274_ctrl_ops, |
| 1919 | V4L2_CID_TEST_PATTERN, |
| 1920 | ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu); |
| 1921 | |
| 1922 | imx274->ctrls.gain = v4l2_ctrl_new_std( |
| 1923 | &imx274->ctrls.handler, |
| 1924 | &imx274_ctrl_ops, |
| 1925 | V4L2_CID_GAIN, IMX274_MIN_GAIN, |
| 1926 | IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1, |
| 1927 | IMX274_DEF_GAIN); |
| 1928 | |
| 1929 | imx274->ctrls.exposure = v4l2_ctrl_new_std( |
| 1930 | &imx274->ctrls.handler, |
| 1931 | &imx274_ctrl_ops, |
| 1932 | V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME, |
| 1933 | 1000000 / IMX274_DEF_FRAME_RATE, 1, |
| 1934 | IMX274_MIN_EXPOSURE_TIME); |
| 1935 | |
| 1936 | imx274->ctrls.vflip = v4l2_ctrl_new_std( |
| 1937 | &imx274->ctrls.handler, |
| 1938 | &imx274_ctrl_ops, |
| 1939 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 1940 | |
| 1941 | imx274->sd.ctrl_handler = &imx274->ctrls.handler; |
| 1942 | if (imx274->ctrls.handler.error) { |
| 1943 | ret = imx274->ctrls.handler.error; |
| 1944 | goto err_ctrls; |
| 1945 | } |
| 1946 | |
| 1947 | /* setup default controls */ |
| 1948 | ret = v4l2_ctrl_handler_setup(&imx274->ctrls.handler); |
| 1949 | if (ret) { |
| 1950 | dev_err(&client->dev, |
| 1951 | "Error %d setup default controls\n", ret); |
| 1952 | goto err_ctrls; |
| 1953 | } |
| 1954 | |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1955 | /* load default control values */ |
| 1956 | ret = imx274_load_default(imx274); |
| 1957 | if (ret) { |
| 1958 | dev_err(&client->dev, |
| 1959 | "%s : imx274_load_default failed %d\n", |
| 1960 | __func__, ret); |
| 1961 | goto err_ctrls; |
| 1962 | } |
| 1963 | |
| 1964 | /* register subdevice */ |
| 1965 | ret = v4l2_async_register_subdev(sd); |
| 1966 | if (ret < 0) { |
| 1967 | dev_err(&client->dev, |
| 1968 | "%s : v4l2_async_register_subdev failed %d\n", |
| 1969 | __func__, ret); |
| 1970 | goto err_ctrls; |
| 1971 | } |
| 1972 | |
| 1973 | dev_info(&client->dev, "imx274 : imx274 probe success !\n"); |
| 1974 | return 0; |
| 1975 | |
| 1976 | err_ctrls: |
Sakari Ailus | 781b045 | 2017-11-01 05:40:58 -0400 | [diff] [blame] | 1977 | v4l2_ctrl_handler_free(&imx274->ctrls.handler); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1978 | err_me: |
| 1979 | media_entity_cleanup(&sd->entity); |
| 1980 | err_regmap: |
| 1981 | mutex_destroy(&imx274->lock); |
| 1982 | return ret; |
| 1983 | } |
| 1984 | |
| 1985 | static int imx274_remove(struct i2c_client *client) |
| 1986 | { |
| 1987 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 1988 | struct stimx274 *imx274 = to_imx274(sd); |
| 1989 | |
| 1990 | /* stop stream */ |
Luca Ceresoli | 8ed8bba70 | 2018-04-24 04:24:11 -0400 | [diff] [blame] | 1991 | imx274_write_table(imx274, imx274_stop); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1992 | |
| 1993 | v4l2_async_unregister_subdev(sd); |
Sakari Ailus | 781b045 | 2017-11-01 05:40:58 -0400 | [diff] [blame] | 1994 | v4l2_ctrl_handler_free(&imx274->ctrls.handler); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1995 | media_entity_cleanup(&sd->entity); |
| 1996 | mutex_destroy(&imx274->lock); |
| 1997 | return 0; |
| 1998 | } |
| 1999 | |
| 2000 | static struct i2c_driver imx274_i2c_driver = { |
| 2001 | .driver = { |
| 2002 | .name = DRIVER_NAME, |
| 2003 | .of_match_table = imx274_of_id_table, |
| 2004 | }, |
| 2005 | .probe = imx274_probe, |
| 2006 | .remove = imx274_remove, |
| 2007 | .id_table = imx274_id, |
| 2008 | }; |
| 2009 | |
| 2010 | module_i2c_driver(imx274_i2c_driver); |
| 2011 | |
| 2012 | MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>"); |
| 2013 | MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver"); |
| 2014 | MODULE_LICENSE("GPL v2"); |