Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * imx274.c - IMX274 CMOS Image Sensor driver |
| 3 | * |
| 4 | * Copyright (C) 2017, Leopard Imaging, Inc. |
| 5 | * |
| 6 | * Leon Luo <leonl@leopardimaging.com> |
| 7 | * Edwin Zou <edwinz@leopardimaging.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms and conditions of the GNU General Public License, |
| 11 | * version 2, as published by the Free Software Foundation. |
| 12 | * |
| 13 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/gpio.h> |
| 25 | #include <linux/gpio/consumer.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/of_gpio.h> |
| 30 | #include <linux/regmap.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/v4l2-mediabus.h> |
| 33 | #include <linux/videodev2.h> |
| 34 | |
| 35 | #include <media/v4l2-ctrls.h> |
| 36 | #include <media/v4l2-device.h> |
| 37 | #include <media/v4l2-subdev.h> |
| 38 | |
| 39 | /* |
| 40 | * See "SHR, SVR Setting" in datasheet |
| 41 | */ |
| 42 | #define IMX274_DEFAULT_FRAME_LENGTH (4550) |
| 43 | #define IMX274_MAX_FRAME_LENGTH (0x000fffff) |
| 44 | |
| 45 | /* |
| 46 | * See "Frame Rate Adjustment" in datasheet |
| 47 | */ |
| 48 | #define IMX274_PIXCLK_CONST1 (72000000) |
| 49 | #define IMX274_PIXCLK_CONST2 (1000000) |
| 50 | |
| 51 | /* |
| 52 | * The input gain is shifted by IMX274_GAIN_SHIFT to get |
| 53 | * decimal number. The real gain is |
| 54 | * (float)input_gain_value / (1 << IMX274_GAIN_SHIFT) |
| 55 | */ |
| 56 | #define IMX274_GAIN_SHIFT (8) |
| 57 | #define IMX274_GAIN_SHIFT_MASK ((1 << IMX274_GAIN_SHIFT) - 1) |
| 58 | |
| 59 | /* |
| 60 | * See "Analog Gain" and "Digital Gain" in datasheet |
| 61 | * min gain is 1X |
| 62 | * max gain is calculated based on IMX274_GAIN_REG_MAX |
| 63 | */ |
| 64 | #define IMX274_GAIN_REG_MAX (1957) |
| 65 | #define IMX274_MIN_GAIN (0x01 << IMX274_GAIN_SHIFT) |
| 66 | #define IMX274_MAX_ANALOG_GAIN ((2048 << IMX274_GAIN_SHIFT)\ |
| 67 | / (2048 - IMX274_GAIN_REG_MAX)) |
| 68 | #define IMX274_MAX_DIGITAL_GAIN (8) |
| 69 | #define IMX274_DEF_GAIN (20 << IMX274_GAIN_SHIFT) |
| 70 | #define IMX274_GAIN_CONST (2048) /* for gain formula */ |
| 71 | |
| 72 | /* |
| 73 | * 1 line time in us = (HMAX / 72), minimal is 4 lines |
| 74 | */ |
| 75 | #define IMX274_MIN_EXPOSURE_TIME (4 * 260 / 72) |
| 76 | |
| 77 | #define IMX274_DEFAULT_MODE IMX274_MODE_3840X2160 |
| 78 | #define IMX274_MAX_WIDTH (3840) |
| 79 | #define IMX274_MAX_HEIGHT (2160) |
| 80 | #define IMX274_MAX_FRAME_RATE (120) |
| 81 | #define IMX274_MIN_FRAME_RATE (5) |
| 82 | #define IMX274_DEF_FRAME_RATE (60) |
| 83 | |
| 84 | /* |
| 85 | * register SHR is limited to (SVR value + 1) x VMAX value - 4 |
| 86 | */ |
| 87 | #define IMX274_SHR_LIMIT_CONST (4) |
| 88 | |
| 89 | /* |
Luca Ceresoli | 4c858e9 | 2018-04-24 04:24:06 -0400 | [diff] [blame] | 90 | * Min and max sensor reset delay (microseconds) |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 91 | */ |
| 92 | #define IMX274_RESET_DELAY1 (2000) |
| 93 | #define IMX274_RESET_DELAY2 (2200) |
| 94 | |
| 95 | /* |
| 96 | * shift and mask constants |
| 97 | */ |
| 98 | #define IMX274_SHIFT_8_BITS (8) |
| 99 | #define IMX274_SHIFT_16_BITS (16) |
| 100 | #define IMX274_MASK_LSB_2_BITS (0x03) |
| 101 | #define IMX274_MASK_LSB_3_BITS (0x07) |
| 102 | #define IMX274_MASK_LSB_4_BITS (0x0f) |
| 103 | #define IMX274_MASK_LSB_8_BITS (0x00ff) |
| 104 | |
| 105 | #define DRIVER_NAME "IMX274" |
| 106 | |
| 107 | /* |
| 108 | * IMX274 register definitions |
| 109 | */ |
| 110 | #define IMX274_FRAME_LENGTH_ADDR_1 0x30FA /* VMAX, MSB */ |
| 111 | #define IMX274_FRAME_LENGTH_ADDR_2 0x30F9 /* VMAX */ |
| 112 | #define IMX274_FRAME_LENGTH_ADDR_3 0x30F8 /* VMAX, LSB */ |
| 113 | #define IMX274_SVR_REG_MSB 0x300F /* SVR */ |
| 114 | #define IMX274_SVR_REG_LSB 0x300E /* SVR */ |
| 115 | #define IMX274_HMAX_REG_MSB 0x30F7 /* HMAX */ |
| 116 | #define IMX274_HMAX_REG_LSB 0x30F6 /* HMAX */ |
| 117 | #define IMX274_COARSE_TIME_ADDR_MSB 0x300D /* SHR */ |
| 118 | #define IMX274_COARSE_TIME_ADDR_LSB 0x300C /* SHR */ |
| 119 | #define IMX274_ANALOG_GAIN_ADDR_LSB 0x300A /* ANALOG GAIN LSB */ |
| 120 | #define IMX274_ANALOG_GAIN_ADDR_MSB 0x300B /* ANALOG GAIN MSB */ |
| 121 | #define IMX274_DIGITAL_GAIN_REG 0x3012 /* Digital Gain */ |
| 122 | #define IMX274_VFLIP_REG 0x301A /* VERTICAL FLIP */ |
| 123 | #define IMX274_TEST_PATTERN_REG 0x303D /* TEST PATTERN */ |
| 124 | #define IMX274_STANDBY_REG 0x3000 /* STANDBY */ |
| 125 | |
| 126 | #define IMX274_TABLE_WAIT_MS 0 |
| 127 | #define IMX274_TABLE_END 1 |
| 128 | |
| 129 | /* |
| 130 | * imx274 I2C operation related structure |
| 131 | */ |
| 132 | struct reg_8 { |
| 133 | u16 addr; |
| 134 | u8 val; |
| 135 | }; |
| 136 | |
| 137 | static const struct regmap_config imx274_regmap_config = { |
| 138 | .reg_bits = 16, |
| 139 | .val_bits = 8, |
| 140 | .cache_type = REGCACHE_RBTREE, |
| 141 | }; |
| 142 | |
| 143 | enum imx274_mode { |
| 144 | IMX274_MODE_3840X2160, |
| 145 | IMX274_MODE_1920X1080, |
| 146 | IMX274_MODE_1280X720, |
| 147 | |
| 148 | IMX274_MODE_START_STREAM_1, |
| 149 | IMX274_MODE_START_STREAM_2, |
| 150 | IMX274_MODE_START_STREAM_3, |
| 151 | IMX274_MODE_START_STREAM_4, |
| 152 | IMX274_MODE_STOP_STREAM |
| 153 | }; |
| 154 | |
| 155 | /* |
| 156 | * imx274 format related structure |
| 157 | */ |
| 158 | struct imx274_frmfmt { |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 159 | struct v4l2_frmsize_discrete size; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | /* |
| 163 | * imx274 test pattern related structure |
| 164 | */ |
| 165 | enum { |
| 166 | TEST_PATTERN_DISABLED = 0, |
| 167 | TEST_PATTERN_ALL_000H, |
| 168 | TEST_PATTERN_ALL_FFFH, |
| 169 | TEST_PATTERN_ALL_555H, |
| 170 | TEST_PATTERN_ALL_AAAH, |
| 171 | TEST_PATTERN_VSP_5AH, /* VERTICAL STRIPE PATTERN 555H/AAAH */ |
| 172 | TEST_PATTERN_VSP_A5H, /* VERTICAL STRIPE PATTERN AAAH/555H */ |
| 173 | TEST_PATTERN_VSP_05H, /* VERTICAL STRIPE PATTERN 000H/555H */ |
| 174 | TEST_PATTERN_VSP_50H, /* VERTICAL STRIPE PATTERN 555H/000H */ |
| 175 | TEST_PATTERN_VSP_0FH, /* VERTICAL STRIPE PATTERN 000H/FFFH */ |
| 176 | TEST_PATTERN_VSP_F0H, /* VERTICAL STRIPE PATTERN FFFH/000H */ |
| 177 | TEST_PATTERN_H_COLOR_BARS, |
| 178 | TEST_PATTERN_V_COLOR_BARS, |
| 179 | }; |
| 180 | |
| 181 | static const char * const tp_qmenu[] = { |
| 182 | "Disabled", |
| 183 | "All 000h Pattern", |
| 184 | "All FFFh Pattern", |
| 185 | "All 555h Pattern", |
| 186 | "All AAAh Pattern", |
| 187 | "Vertical Stripe (555h / AAAh)", |
| 188 | "Vertical Stripe (AAAh / 555h)", |
| 189 | "Vertical Stripe (000h / 555h)", |
| 190 | "Vertical Stripe (555h / 000h)", |
| 191 | "Vertical Stripe (000h / FFFh)", |
| 192 | "Vertical Stripe (FFFh / 000h)", |
| 193 | "Horizontal Color Bars", |
| 194 | "Vertical Color Bars", |
| 195 | }; |
| 196 | |
| 197 | /* |
| 198 | * All-pixel scan mode (10-bit) |
| 199 | * imx274 mode1(refer to datasheet) register configuration with |
| 200 | * 3840x2160 resolution, raw10 data and mipi four lane output |
| 201 | */ |
| 202 | static const struct reg_8 imx274_mode1_3840x2160_raw10[] = { |
| 203 | {0x3004, 0x01}, |
| 204 | {0x3005, 0x01}, |
| 205 | {0x3006, 0x00}, |
| 206 | {0x3007, 0x02}, |
| 207 | |
| 208 | {0x3018, 0xA2}, /* output XVS, HVS */ |
| 209 | |
| 210 | {0x306B, 0x05}, |
| 211 | {0x30E2, 0x01}, |
| 212 | {0x30F6, 0x07}, /* HMAX, 263 */ |
| 213 | {0x30F7, 0x01}, /* HMAX */ |
| 214 | |
| 215 | {0x30dd, 0x01}, /* crop to 2160 */ |
| 216 | {0x30de, 0x06}, |
| 217 | {0x30df, 0x00}, |
| 218 | {0x30e0, 0x12}, |
| 219 | {0x30e1, 0x00}, |
| 220 | {0x3037, 0x01}, /* to crop to 3840 */ |
| 221 | {0x3038, 0x0c}, |
| 222 | {0x3039, 0x00}, |
| 223 | {0x303a, 0x0c}, |
| 224 | {0x303b, 0x0f}, |
| 225 | |
| 226 | {0x30EE, 0x01}, |
| 227 | {0x3130, 0x86}, |
| 228 | {0x3131, 0x08}, |
| 229 | {0x3132, 0x7E}, |
| 230 | {0x3133, 0x08}, |
| 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}, |
| 264 | {0x3007, 0x11}, |
| 265 | |
| 266 | {0x3018, 0xA2}, /* output XVS, HVS */ |
| 267 | |
| 268 | {0x306B, 0x05}, |
| 269 | {0x30E2, 0x02}, |
| 270 | |
| 271 | {0x30F6, 0x04}, /* HMAX, 260 */ |
| 272 | {0x30F7, 0x01}, /* HMAX */ |
| 273 | |
| 274 | {0x30dd, 0x01}, /* to crop to 1920x1080 */ |
| 275 | {0x30de, 0x05}, |
| 276 | {0x30df, 0x00}, |
| 277 | {0x30e0, 0x04}, |
| 278 | {0x30e1, 0x00}, |
| 279 | {0x3037, 0x01}, |
| 280 | {0x3038, 0x0c}, |
| 281 | {0x3039, 0x00}, |
| 282 | {0x303a, 0x0c}, |
| 283 | {0x303b, 0x0f}, |
| 284 | |
| 285 | {0x30EE, 0x01}, |
| 286 | {0x3130, 0x4E}, |
| 287 | {0x3131, 0x04}, |
| 288 | {0x3132, 0x46}, |
| 289 | {0x3133, 0x04}, |
| 290 | {0x3342, 0x0A}, |
| 291 | {0x3343, 0x00}, |
| 292 | {0x3344, 0x1A}, |
| 293 | {0x3345, 0x00}, |
| 294 | {0x33A6, 0x01}, |
| 295 | {0x3528, 0x0E}, |
| 296 | {0x3554, 0x00}, |
| 297 | {0x3555, 0x01}, |
| 298 | {0x3556, 0x01}, |
| 299 | {0x3557, 0x01}, |
| 300 | {0x3558, 0x01}, |
| 301 | {0x3559, 0x00}, |
| 302 | {0x355A, 0x00}, |
| 303 | {0x35BA, 0x0E}, |
| 304 | {0x366A, 0x1B}, |
| 305 | {0x366B, 0x1A}, |
| 306 | {0x366C, 0x19}, |
| 307 | {0x366D, 0x17}, |
| 308 | {0x3A41, 0x08}, |
| 309 | |
| 310 | {IMX274_TABLE_END, 0x00} |
| 311 | }; |
| 312 | |
| 313 | /* |
| 314 | * Vertical 2/3 subsampling binning horizontal 3 binning |
| 315 | * imx274 mode5(refer to datasheet) register configuration with |
| 316 | * 1280x720 resolution, raw10 data and mipi four lane output |
| 317 | */ |
| 318 | static const struct reg_8 imx274_mode5_1280x720_raw10[] = { |
| 319 | {0x3004, 0x03}, |
| 320 | {0x3005, 0x31}, |
| 321 | {0x3006, 0x00}, |
| 322 | {0x3007, 0x09}, |
| 323 | |
| 324 | {0x3018, 0xA2}, /* output XVS, HVS */ |
| 325 | |
| 326 | {0x306B, 0x05}, |
| 327 | {0x30E2, 0x03}, |
| 328 | |
| 329 | {0x30F6, 0x04}, /* HMAX, 260 */ |
| 330 | {0x30F7, 0x01}, /* HMAX */ |
| 331 | |
| 332 | {0x30DD, 0x01}, |
| 333 | {0x30DE, 0x07}, |
| 334 | {0x30DF, 0x00}, |
| 335 | {0x40E0, 0x04}, |
| 336 | {0x30E1, 0x00}, |
| 337 | {0x3030, 0xD4}, |
| 338 | {0x3031, 0x02}, |
| 339 | {0x3032, 0xD0}, |
| 340 | {0x3033, 0x02}, |
| 341 | |
| 342 | {0x30EE, 0x01}, |
| 343 | {0x3130, 0xE2}, |
| 344 | {0x3131, 0x02}, |
| 345 | {0x3132, 0xDE}, |
| 346 | {0x3133, 0x02}, |
| 347 | {0x3342, 0x0A}, |
| 348 | {0x3343, 0x00}, |
| 349 | {0x3344, 0x1B}, |
| 350 | {0x3345, 0x00}, |
| 351 | {0x33A6, 0x01}, |
| 352 | {0x3528, 0x0E}, |
| 353 | {0x3554, 0x00}, |
| 354 | {0x3555, 0x01}, |
| 355 | {0x3556, 0x01}, |
| 356 | {0x3557, 0x01}, |
| 357 | {0x3558, 0x01}, |
| 358 | {0x3559, 0x00}, |
| 359 | {0x355A, 0x00}, |
| 360 | {0x35BA, 0x0E}, |
| 361 | {0x366A, 0x1B}, |
| 362 | {0x366B, 0x19}, |
| 363 | {0x366C, 0x17}, |
| 364 | {0x366D, 0x17}, |
| 365 | {0x3A41, 0x04}, |
| 366 | |
| 367 | {IMX274_TABLE_END, 0x00} |
| 368 | }; |
| 369 | |
| 370 | /* |
| 371 | * imx274 first step register configuration for |
| 372 | * starting stream |
| 373 | */ |
| 374 | static const struct reg_8 imx274_start_1[] = { |
| 375 | {IMX274_STANDBY_REG, 0x12}, |
| 376 | {IMX274_TABLE_END, 0x00} |
| 377 | }; |
| 378 | |
| 379 | /* |
| 380 | * imx274 second step register configuration for |
| 381 | * starting stream |
| 382 | */ |
| 383 | static const struct reg_8 imx274_start_2[] = { |
| 384 | {0x3120, 0xF0}, /* clock settings */ |
| 385 | {0x3121, 0x00}, /* clock settings */ |
| 386 | {0x3122, 0x02}, /* clock settings */ |
| 387 | {0x3129, 0x9C}, /* clock settings */ |
| 388 | {0x312A, 0x02}, /* clock settings */ |
| 389 | {0x312D, 0x02}, /* clock settings */ |
| 390 | |
| 391 | {0x310B, 0x00}, |
| 392 | |
| 393 | /* PLSTMG */ |
| 394 | {0x304C, 0x00}, /* PLSTMG01 */ |
| 395 | {0x304D, 0x03}, |
| 396 | {0x331C, 0x1A}, |
| 397 | {0x331D, 0x00}, |
| 398 | {0x3502, 0x02}, |
| 399 | {0x3529, 0x0E}, |
| 400 | {0x352A, 0x0E}, |
| 401 | {0x352B, 0x0E}, |
| 402 | {0x3538, 0x0E}, |
| 403 | {0x3539, 0x0E}, |
| 404 | {0x3553, 0x00}, |
| 405 | {0x357D, 0x05}, |
| 406 | {0x357F, 0x05}, |
| 407 | {0x3581, 0x04}, |
| 408 | {0x3583, 0x76}, |
| 409 | {0x3587, 0x01}, |
| 410 | {0x35BB, 0x0E}, |
| 411 | {0x35BC, 0x0E}, |
| 412 | {0x35BD, 0x0E}, |
| 413 | {0x35BE, 0x0E}, |
| 414 | {0x35BF, 0x0E}, |
| 415 | {0x366E, 0x00}, |
| 416 | {0x366F, 0x00}, |
| 417 | {0x3670, 0x00}, |
| 418 | {0x3671, 0x00}, |
| 419 | |
| 420 | /* PSMIPI */ |
| 421 | {0x3304, 0x32}, /* PSMIPI1 */ |
| 422 | {0x3305, 0x00}, |
| 423 | {0x3306, 0x32}, |
| 424 | {0x3307, 0x00}, |
| 425 | {0x3590, 0x32}, |
| 426 | {0x3591, 0x00}, |
| 427 | {0x3686, 0x32}, |
| 428 | {0x3687, 0x00}, |
| 429 | |
| 430 | {IMX274_TABLE_END, 0x00} |
| 431 | }; |
| 432 | |
| 433 | /* |
| 434 | * imx274 third step register configuration for |
| 435 | * starting stream |
| 436 | */ |
| 437 | static const struct reg_8 imx274_start_3[] = { |
| 438 | {IMX274_STANDBY_REG, 0x00}, |
| 439 | {0x303E, 0x02}, /* SYS_MODE = 2 */ |
| 440 | {IMX274_TABLE_END, 0x00} |
| 441 | }; |
| 442 | |
| 443 | /* |
| 444 | * imx274 forth step register configuration for |
| 445 | * starting stream |
| 446 | */ |
| 447 | static const struct reg_8 imx274_start_4[] = { |
| 448 | {0x30F4, 0x00}, |
| 449 | {0x3018, 0xA2}, /* XHS VHS OUTUPT */ |
| 450 | {IMX274_TABLE_END, 0x00} |
| 451 | }; |
| 452 | |
| 453 | /* |
| 454 | * imx274 register configuration for stoping stream |
| 455 | */ |
| 456 | static const struct reg_8 imx274_stop[] = { |
| 457 | {IMX274_STANDBY_REG, 0x01}, |
| 458 | {IMX274_TABLE_END, 0x00} |
| 459 | }; |
| 460 | |
| 461 | /* |
| 462 | * imx274 disable test pattern register configuration |
| 463 | */ |
| 464 | static const struct reg_8 imx274_tp_disabled[] = { |
| 465 | {0x303C, 0x00}, |
| 466 | {0x377F, 0x00}, |
| 467 | {0x3781, 0x00}, |
| 468 | {0x370B, 0x00}, |
| 469 | {IMX274_TABLE_END, 0x00} |
| 470 | }; |
| 471 | |
| 472 | /* |
| 473 | * imx274 test pattern register configuration |
| 474 | * reg 0x303D defines the test pattern modes |
| 475 | */ |
| 476 | static const struct reg_8 imx274_tp_regs[] = { |
| 477 | {0x303C, 0x11}, |
| 478 | {0x370E, 0x01}, |
| 479 | {0x377F, 0x01}, |
| 480 | {0x3781, 0x01}, |
| 481 | {0x370B, 0x11}, |
| 482 | {IMX274_TABLE_END, 0x00} |
| 483 | }; |
| 484 | |
| 485 | static const struct reg_8 *mode_table[] = { |
| 486 | [IMX274_MODE_3840X2160] = imx274_mode1_3840x2160_raw10, |
| 487 | [IMX274_MODE_1920X1080] = imx274_mode3_1920x1080_raw10, |
| 488 | [IMX274_MODE_1280X720] = imx274_mode5_1280x720_raw10, |
| 489 | |
| 490 | [IMX274_MODE_START_STREAM_1] = imx274_start_1, |
| 491 | [IMX274_MODE_START_STREAM_2] = imx274_start_2, |
| 492 | [IMX274_MODE_START_STREAM_3] = imx274_start_3, |
| 493 | [IMX274_MODE_START_STREAM_4] = imx274_start_4, |
| 494 | [IMX274_MODE_STOP_STREAM] = imx274_stop, |
| 495 | }; |
| 496 | |
| 497 | /* |
| 498 | * imx274 format related structure |
| 499 | */ |
| 500 | static const struct imx274_frmfmt imx274_formats[] = { |
Luca Ceresoli | f067dda | 2018-04-24 04:24:09 -0400 | [diff] [blame^] | 501 | { {3840, 2160} }, |
| 502 | { {1920, 1080} }, |
| 503 | { {1280, 720} }, |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | /* |
| 507 | * minimal frame length for each mode |
| 508 | * refer to datasheet section "Frame Rate Adjustment (CSI-2)" |
| 509 | */ |
| 510 | static const int min_frame_len[] = { |
| 511 | 4550, /* mode 1, 4K */ |
| 512 | 2310, /* mode 3, 1080p */ |
| 513 | 2310 /* mode 5, 720p */ |
| 514 | }; |
| 515 | |
| 516 | /* |
| 517 | * minimal numbers of SHR register |
| 518 | * refer to datasheet table "Shutter Setting (CSI-2)" |
| 519 | */ |
| 520 | static const int min_SHR[] = { |
| 521 | 12, /* mode 1, 4K */ |
| 522 | 8, /* mode 3, 1080p */ |
| 523 | 8 /* mode 5, 720p */ |
| 524 | }; |
| 525 | |
| 526 | static const int max_frame_rate[] = { |
| 527 | 60, /* mode 1 , 4K */ |
| 528 | 120, /* mode 3, 1080p */ |
| 529 | 120 /* mode 5, 720p */ |
| 530 | }; |
| 531 | |
| 532 | /* |
| 533 | * Number of clocks per internal offset period |
| 534 | * a constant based on mode |
| 535 | * refer to section "Integration Time in Each Readout Drive Mode (CSI-2)" |
| 536 | * in the datasheet |
| 537 | * for the implemented 3 modes, it happens to be the same number |
| 538 | */ |
| 539 | static const int nocpiop[] = { |
| 540 | 112, /* mode 1 , 4K */ |
| 541 | 112, /* mode 3, 1080p */ |
| 542 | 112 /* mode 5, 720p */ |
| 543 | }; |
| 544 | |
| 545 | /* |
| 546 | * struct imx274_ctrls - imx274 ctrl structure |
| 547 | * @handler: V4L2 ctrl handler structure |
| 548 | * @exposure: Pointer to expsure ctrl structure |
| 549 | * @gain: Pointer to gain ctrl structure |
| 550 | * @vflip: Pointer to vflip ctrl structure |
| 551 | * @test_pattern: Pointer to test pattern ctrl structure |
| 552 | */ |
| 553 | struct imx274_ctrls { |
| 554 | struct v4l2_ctrl_handler handler; |
| 555 | struct v4l2_ctrl *exposure; |
| 556 | struct v4l2_ctrl *gain; |
| 557 | struct v4l2_ctrl *vflip; |
| 558 | struct v4l2_ctrl *test_pattern; |
| 559 | }; |
| 560 | |
| 561 | /* |
| 562 | * struct stim274 - imx274 device structure |
| 563 | * @sd: V4L2 subdevice structure |
| 564 | * @pd: Media pad structure |
| 565 | * @client: Pointer to I2C client |
| 566 | * @ctrls: imx274 control structure |
| 567 | * @format: V4L2 media bus frame format structure |
| 568 | * @frame_rate: V4L2 frame rate structure |
| 569 | * @regmap: Pointer to regmap structure |
| 570 | * @reset_gpio: Pointer to reset gpio |
| 571 | * @lock: Mutex structure |
| 572 | * @mode_index: Resolution mode index |
| 573 | */ |
| 574 | struct stimx274 { |
| 575 | struct v4l2_subdev sd; |
| 576 | struct media_pad pad; |
| 577 | struct i2c_client *client; |
| 578 | struct imx274_ctrls ctrls; |
| 579 | struct v4l2_mbus_framefmt format; |
| 580 | struct v4l2_fract frame_interval; |
| 581 | struct regmap *regmap; |
| 582 | struct gpio_desc *reset_gpio; |
| 583 | struct mutex lock; /* mutex lock for operations */ |
| 584 | u32 mode_index; |
| 585 | }; |
| 586 | |
| 587 | /* |
| 588 | * Function declaration |
| 589 | */ |
| 590 | static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl); |
| 591 | static int imx274_set_exposure(struct stimx274 *priv, int val); |
| 592 | static int imx274_set_vflip(struct stimx274 *priv, int val); |
| 593 | static int imx274_set_test_pattern(struct stimx274 *priv, int val); |
| 594 | static int imx274_set_frame_interval(struct stimx274 *priv, |
| 595 | struct v4l2_fract frame_interval); |
| 596 | |
| 597 | static inline void msleep_range(unsigned int delay_base) |
| 598 | { |
| 599 | usleep_range(delay_base * 1000, delay_base * 1000 + 500); |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * v4l2_ctrl and v4l2_subdev related operations |
| 604 | */ |
| 605 | static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl) |
| 606 | { |
| 607 | return &container_of(ctrl->handler, |
| 608 | struct stimx274, ctrls.handler)->sd; |
| 609 | } |
| 610 | |
| 611 | static inline struct stimx274 *to_imx274(struct v4l2_subdev *sd) |
| 612 | { |
| 613 | return container_of(sd, struct stimx274, sd); |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * imx274_regmap_util_write_table_8 - Function for writing register table |
| 618 | * @regmap: Pointer to device reg map structure |
| 619 | * @table: Table containing register values |
| 620 | * @wait_ms_addr: Flag for performing delay |
| 621 | * @end_addr: Flag for incating end of table |
| 622 | * |
| 623 | * This is used to write register table into sensor's reg map. |
| 624 | * |
| 625 | * Return: 0 on success, errors otherwise |
| 626 | */ |
| 627 | static int imx274_regmap_util_write_table_8(struct regmap *regmap, |
| 628 | const struct reg_8 table[], |
| 629 | u16 wait_ms_addr, u16 end_addr) |
| 630 | { |
Dan Carpenter | 021741a | 2017-12-05 09:37:39 -0500 | [diff] [blame] | 631 | int err = 0; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 632 | const struct reg_8 *next; |
| 633 | u8 val; |
| 634 | |
| 635 | int range_start = -1; |
| 636 | int range_count = 0; |
| 637 | u8 range_vals[16]; |
| 638 | int max_range_vals = ARRAY_SIZE(range_vals); |
| 639 | |
| 640 | for (next = table;; next++) { |
| 641 | if ((next->addr != range_start + range_count) || |
| 642 | (next->addr == end_addr) || |
| 643 | (next->addr == wait_ms_addr) || |
| 644 | (range_count == max_range_vals)) { |
| 645 | if (range_count == 1) |
| 646 | err = regmap_write(regmap, |
| 647 | range_start, range_vals[0]); |
| 648 | else if (range_count > 1) |
| 649 | err = regmap_bulk_write(regmap, range_start, |
| 650 | &range_vals[0], |
| 651 | range_count); |
Mauro Carvalho Chehab | 00b4bac7 | 2017-11-01 17:05:57 -0400 | [diff] [blame] | 652 | else |
| 653 | err = 0; |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 654 | |
| 655 | if (err) |
| 656 | return err; |
| 657 | |
| 658 | range_start = -1; |
| 659 | range_count = 0; |
| 660 | |
| 661 | /* Handle special address values */ |
| 662 | if (next->addr == end_addr) |
| 663 | break; |
| 664 | |
| 665 | if (next->addr == wait_ms_addr) { |
| 666 | msleep_range(next->val); |
| 667 | continue; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | val = next->val; |
| 672 | |
| 673 | if (range_start == -1) |
| 674 | range_start = next->addr; |
| 675 | |
| 676 | range_vals[range_count++] = val; |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | static inline int imx274_read_reg(struct stimx274 *priv, u16 addr, u8 *val) |
| 682 | { |
| 683 | int err; |
| 684 | |
| 685 | err = regmap_read(priv->regmap, addr, (unsigned int *)val); |
| 686 | if (err) |
| 687 | dev_err(&priv->client->dev, |
| 688 | "%s : i2c read failed, addr = %x\n", __func__, addr); |
| 689 | else |
| 690 | dev_dbg(&priv->client->dev, |
| 691 | "%s : addr 0x%x, val=0x%x\n", __func__, |
| 692 | addr, *val); |
| 693 | return err; |
| 694 | } |
| 695 | |
| 696 | static inline int imx274_write_reg(struct stimx274 *priv, u16 addr, u8 val) |
| 697 | { |
| 698 | int err; |
| 699 | |
| 700 | err = regmap_write(priv->regmap, addr, val); |
| 701 | if (err) |
| 702 | dev_err(&priv->client->dev, |
| 703 | "%s : i2c write failed, %x = %x\n", __func__, |
| 704 | addr, val); |
| 705 | else |
| 706 | dev_dbg(&priv->client->dev, |
| 707 | "%s : addr 0x%x, val=0x%x\n", __func__, |
| 708 | addr, val); |
| 709 | return err; |
| 710 | } |
| 711 | |
| 712 | static int imx274_write_table(struct stimx274 *priv, const struct reg_8 table[]) |
| 713 | { |
| 714 | return imx274_regmap_util_write_table_8(priv->regmap, |
| 715 | table, IMX274_TABLE_WAIT_MS, IMX274_TABLE_END); |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * imx274_mode_regs - Function for set mode registers per mode index |
| 720 | * @priv: Pointer to device structure |
| 721 | * @mode: Mode index value |
| 722 | * |
| 723 | * This is used to start steam per mode index. |
| 724 | * mode = 0, start stream for sensor Mode 1: 4K/raw10 |
| 725 | * mode = 1, start stream for sensor Mode 3: 1080p/raw10 |
| 726 | * mode = 2, start stream for sensor Mode 5: 720p/raw10 |
| 727 | * |
| 728 | * Return: 0 on success, errors otherwise |
| 729 | */ |
| 730 | static int imx274_mode_regs(struct stimx274 *priv, int mode) |
| 731 | { |
| 732 | int err = 0; |
| 733 | |
| 734 | err = imx274_write_table(priv, mode_table[IMX274_MODE_START_STREAM_1]); |
| 735 | if (err) |
| 736 | return err; |
| 737 | |
| 738 | err = imx274_write_table(priv, mode_table[IMX274_MODE_START_STREAM_2]); |
| 739 | if (err) |
| 740 | return err; |
| 741 | |
| 742 | err = imx274_write_table(priv, mode_table[mode]); |
| 743 | |
| 744 | return err; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * imx274_start_stream - Function for starting stream per mode index |
| 749 | * @priv: Pointer to device structure |
| 750 | * |
| 751 | * Return: 0 on success, errors otherwise |
| 752 | */ |
| 753 | static int imx274_start_stream(struct stimx274 *priv) |
| 754 | { |
| 755 | int err = 0; |
| 756 | |
| 757 | /* |
| 758 | * Refer to "Standby Cancel Sequence when using CSI-2" in |
| 759 | * imx274 datasheet, it should wait 10ms or more here. |
| 760 | * give it 1 extra ms for margin |
| 761 | */ |
| 762 | msleep_range(11); |
| 763 | err = imx274_write_table(priv, mode_table[IMX274_MODE_START_STREAM_3]); |
| 764 | if (err) |
| 765 | return err; |
| 766 | |
| 767 | /* |
| 768 | * Refer to "Standby Cancel Sequence when using CSI-2" in |
| 769 | * imx274 datasheet, it should wait 7ms or more here. |
| 770 | * give it 1 extra ms for margin |
| 771 | */ |
| 772 | msleep_range(8); |
| 773 | err = imx274_write_table(priv, mode_table[IMX274_MODE_START_STREAM_4]); |
| 774 | if (err) |
| 775 | return err; |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * imx274_reset - Function called to reset the sensor |
| 782 | * @priv: Pointer to device structure |
| 783 | * @rst: Input value for determining the sensor's end state after reset |
| 784 | * |
| 785 | * Set the senor in reset and then |
| 786 | * if rst = 0, keep it in reset; |
| 787 | * if rst = 1, bring it out of reset. |
| 788 | * |
| 789 | */ |
| 790 | static void imx274_reset(struct stimx274 *priv, int rst) |
| 791 | { |
| 792 | gpiod_set_value_cansleep(priv->reset_gpio, 0); |
| 793 | usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2); |
| 794 | gpiod_set_value_cansleep(priv->reset_gpio, !!rst); |
| 795 | usleep_range(IMX274_RESET_DELAY1, IMX274_RESET_DELAY2); |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * imx274_s_ctrl - This is used to set the imx274 V4L2 controls |
| 800 | * @ctrl: V4L2 control to be set |
| 801 | * |
| 802 | * This function is used to set the V4L2 controls for the imx274 sensor. |
| 803 | * |
| 804 | * Return: 0 on success, errors otherwise |
| 805 | */ |
| 806 | static int imx274_s_ctrl(struct v4l2_ctrl *ctrl) |
| 807 | { |
| 808 | struct v4l2_subdev *sd = ctrl_to_sd(ctrl); |
| 809 | struct stimx274 *imx274 = to_imx274(sd); |
| 810 | int ret = -EINVAL; |
| 811 | |
| 812 | dev_dbg(&imx274->client->dev, |
| 813 | "%s : s_ctrl: %s, value: %d\n", __func__, |
| 814 | ctrl->name, ctrl->val); |
| 815 | |
| 816 | switch (ctrl->id) { |
| 817 | case V4L2_CID_EXPOSURE: |
| 818 | dev_dbg(&imx274->client->dev, |
| 819 | "%s : set V4L2_CID_EXPOSURE\n", __func__); |
| 820 | ret = imx274_set_exposure(imx274, ctrl->val); |
| 821 | break; |
| 822 | |
| 823 | case V4L2_CID_GAIN: |
| 824 | dev_dbg(&imx274->client->dev, |
| 825 | "%s : set V4L2_CID_GAIN\n", __func__); |
| 826 | ret = imx274_set_gain(imx274, ctrl); |
| 827 | break; |
| 828 | |
| 829 | case V4L2_CID_VFLIP: |
| 830 | dev_dbg(&imx274->client->dev, |
| 831 | "%s : set V4L2_CID_VFLIP\n", __func__); |
| 832 | ret = imx274_set_vflip(imx274, ctrl->val); |
| 833 | break; |
| 834 | |
| 835 | case V4L2_CID_TEST_PATTERN: |
| 836 | dev_dbg(&imx274->client->dev, |
| 837 | "%s : set V4L2_CID_TEST_PATTERN\n", __func__); |
| 838 | ret = imx274_set_test_pattern(imx274, ctrl->val); |
| 839 | break; |
| 840 | } |
| 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * imx274_get_fmt - Get the pad format |
| 847 | * @sd: Pointer to V4L2 Sub device structure |
| 848 | * @cfg: Pointer to sub device pad information structure |
| 849 | * @fmt: Pointer to pad level media bus format |
| 850 | * |
| 851 | * This function is used to get the pad format information. |
| 852 | * |
| 853 | * Return: 0 on success |
| 854 | */ |
| 855 | static int imx274_get_fmt(struct v4l2_subdev *sd, |
| 856 | struct v4l2_subdev_pad_config *cfg, |
| 857 | struct v4l2_subdev_format *fmt) |
| 858 | { |
| 859 | struct stimx274 *imx274 = to_imx274(sd); |
| 860 | |
| 861 | mutex_lock(&imx274->lock); |
| 862 | fmt->format = imx274->format; |
| 863 | mutex_unlock(&imx274->lock); |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | /** |
| 868 | * imx274_set_fmt - This is used to set the pad format |
| 869 | * @sd: Pointer to V4L2 Sub device structure |
| 870 | * @cfg: Pointer to sub device pad information structure |
| 871 | * @format: Pointer to pad level media bus format |
| 872 | * |
| 873 | * This function is used to set the pad format. |
| 874 | * |
| 875 | * Return: 0 on success |
| 876 | */ |
| 877 | static int imx274_set_fmt(struct v4l2_subdev *sd, |
| 878 | struct v4l2_subdev_pad_config *cfg, |
| 879 | struct v4l2_subdev_format *format) |
| 880 | { |
| 881 | struct v4l2_mbus_framefmt *fmt = &format->format; |
| 882 | struct stimx274 *imx274 = to_imx274(sd); |
| 883 | struct i2c_client *client = imx274->client; |
| 884 | int index; |
| 885 | |
| 886 | dev_dbg(&client->dev, |
Luca Ceresoli | f067dda | 2018-04-24 04:24:09 -0400 | [diff] [blame^] | 887 | "%s: width = %d height = %d code = %d\n", |
| 888 | __func__, fmt->width, fmt->height, fmt->code); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 889 | |
| 890 | mutex_lock(&imx274->lock); |
| 891 | |
| 892 | for (index = 0; index < ARRAY_SIZE(imx274_formats); index++) { |
| 893 | if (imx274_formats[index].size.width == fmt->width && |
| 894 | imx274_formats[index].size.height == fmt->height) |
| 895 | break; |
| 896 | } |
| 897 | |
| 898 | if (index >= ARRAY_SIZE(imx274_formats)) { |
| 899 | /* default to first format */ |
| 900 | index = 0; |
| 901 | } |
| 902 | |
| 903 | imx274->mode_index = index; |
| 904 | |
| 905 | if (fmt->width > IMX274_MAX_WIDTH) |
| 906 | fmt->width = IMX274_MAX_WIDTH; |
| 907 | if (fmt->height > IMX274_MAX_HEIGHT) |
| 908 | fmt->height = IMX274_MAX_HEIGHT; |
| 909 | fmt->width = fmt->width & (~IMX274_MASK_LSB_2_BITS); |
| 910 | fmt->height = fmt->height & (~IMX274_MASK_LSB_2_BITS); |
| 911 | fmt->field = V4L2_FIELD_NONE; |
| 912 | |
| 913 | if (format->which == V4L2_SUBDEV_FORMAT_TRY) |
| 914 | cfg->try_fmt = *fmt; |
| 915 | else |
| 916 | imx274->format = *fmt; |
| 917 | |
| 918 | mutex_unlock(&imx274->lock); |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * imx274_g_frame_interval - Get the frame interval |
| 924 | * @sd: Pointer to V4L2 Sub device structure |
| 925 | * @fi: Pointer to V4l2 Sub device frame interval structure |
| 926 | * |
| 927 | * This function is used to get the frame interval. |
| 928 | * |
| 929 | * Return: 0 on success |
| 930 | */ |
| 931 | static int imx274_g_frame_interval(struct v4l2_subdev *sd, |
| 932 | struct v4l2_subdev_frame_interval *fi) |
| 933 | { |
| 934 | struct stimx274 *imx274 = to_imx274(sd); |
| 935 | |
| 936 | fi->interval = imx274->frame_interval; |
| 937 | dev_dbg(&imx274->client->dev, "%s frame rate = %d / %d\n", |
| 938 | __func__, imx274->frame_interval.numerator, |
| 939 | imx274->frame_interval.denominator); |
| 940 | |
| 941 | return 0; |
| 942 | } |
| 943 | |
| 944 | /** |
| 945 | * imx274_s_frame_interval - Set the frame interval |
| 946 | * @sd: Pointer to V4L2 Sub device structure |
| 947 | * @fi: Pointer to V4l2 Sub device frame interval structure |
| 948 | * |
| 949 | * This function is used to set the frame intervavl. |
| 950 | * |
| 951 | * Return: 0 on success |
| 952 | */ |
| 953 | static int imx274_s_frame_interval(struct v4l2_subdev *sd, |
| 954 | struct v4l2_subdev_frame_interval *fi) |
| 955 | { |
| 956 | struct stimx274 *imx274 = to_imx274(sd); |
| 957 | struct v4l2_ctrl *ctrl = imx274->ctrls.exposure; |
| 958 | int min, max, def; |
| 959 | int ret; |
| 960 | |
| 961 | mutex_lock(&imx274->lock); |
| 962 | ret = imx274_set_frame_interval(imx274, fi->interval); |
| 963 | |
| 964 | if (!ret) { |
| 965 | /* |
| 966 | * exposure time range is decided by frame interval |
Luca Ceresoli | 0134339 | 2018-04-24 04:24:07 -0400 | [diff] [blame] | 967 | * need to update it after frame interval changes |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 968 | */ |
| 969 | min = IMX274_MIN_EXPOSURE_TIME; |
| 970 | max = fi->interval.numerator * 1000000 |
| 971 | / fi->interval.denominator; |
| 972 | def = max; |
| 973 | if (__v4l2_ctrl_modify_range(ctrl, min, max, 1, def)) { |
| 974 | dev_err(&imx274->client->dev, |
| 975 | "Exposure ctrl range update failed\n"); |
| 976 | goto unlock; |
| 977 | } |
| 978 | |
| 979 | /* update exposure time accordingly */ |
Luca Ceresoli | cf90870 | 2018-04-24 04:24:08 -0400 | [diff] [blame] | 980 | imx274_set_exposure(imx274, ctrl->val); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 981 | |
| 982 | dev_dbg(&imx274->client->dev, "set frame interval to %uus\n", |
| 983 | fi->interval.numerator * 1000000 |
| 984 | / fi->interval.denominator); |
| 985 | } |
| 986 | |
| 987 | unlock: |
| 988 | mutex_unlock(&imx274->lock); |
| 989 | |
| 990 | return ret; |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * imx274_load_default - load default control values |
| 995 | * @priv: Pointer to device structure |
| 996 | * |
| 997 | * Return: 0 on success, errors otherwise |
| 998 | */ |
| 999 | static int imx274_load_default(struct stimx274 *priv) |
| 1000 | { |
| 1001 | int ret; |
| 1002 | |
| 1003 | /* load default control values */ |
| 1004 | priv->frame_interval.numerator = 1; |
| 1005 | priv->frame_interval.denominator = IMX274_DEF_FRAME_RATE; |
| 1006 | priv->ctrls.exposure->val = 1000000 / IMX274_DEF_FRAME_RATE; |
| 1007 | priv->ctrls.gain->val = IMX274_DEF_GAIN; |
| 1008 | priv->ctrls.vflip->val = 0; |
| 1009 | priv->ctrls.test_pattern->val = TEST_PATTERN_DISABLED; |
| 1010 | |
| 1011 | /* update frame rate */ |
| 1012 | ret = imx274_set_frame_interval(priv, |
| 1013 | priv->frame_interval); |
| 1014 | if (ret) |
| 1015 | return ret; |
| 1016 | |
| 1017 | /* update exposure time */ |
| 1018 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.exposure, priv->ctrls.exposure->val); |
| 1019 | if (ret) |
| 1020 | return ret; |
| 1021 | |
| 1022 | /* update gain */ |
| 1023 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.gain, priv->ctrls.gain->val); |
| 1024 | if (ret) |
| 1025 | return ret; |
| 1026 | |
| 1027 | /* update vflip */ |
| 1028 | ret = v4l2_ctrl_s_ctrl(priv->ctrls.vflip, priv->ctrls.vflip->val); |
| 1029 | if (ret) |
| 1030 | return ret; |
| 1031 | |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * imx274_s_stream - It is used to start/stop the streaming. |
| 1037 | * @sd: V4L2 Sub device |
| 1038 | * @on: Flag (True / False) |
| 1039 | * |
| 1040 | * This function controls the start or stop of streaming for the |
| 1041 | * imx274 sensor. |
| 1042 | * |
| 1043 | * Return: 0 on success, errors otherwise |
| 1044 | */ |
| 1045 | static int imx274_s_stream(struct v4l2_subdev *sd, int on) |
| 1046 | { |
| 1047 | struct stimx274 *imx274 = to_imx274(sd); |
| 1048 | int ret = 0; |
| 1049 | |
| 1050 | dev_dbg(&imx274->client->dev, "%s : %s, mode index = %d\n", __func__, |
| 1051 | on ? "Stream Start" : "Stream Stop", imx274->mode_index); |
| 1052 | |
| 1053 | mutex_lock(&imx274->lock); |
| 1054 | |
| 1055 | if (on) { |
| 1056 | /* load mode registers */ |
Colin Ian King | 2b00e30 | 2017-11-03 02:41:06 -0400 | [diff] [blame] | 1057 | ret = imx274_mode_regs(imx274, imx274->mode_index); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1058 | if (ret) |
| 1059 | goto fail; |
| 1060 | |
| 1061 | /* |
| 1062 | * update frame rate & expsoure. if the last mode is different, |
| 1063 | * HMAX could be changed. As the result, frame rate & exposure |
| 1064 | * are changed. |
| 1065 | * gain is not affected. |
| 1066 | */ |
| 1067 | ret = imx274_set_frame_interval(imx274, |
| 1068 | imx274->frame_interval); |
| 1069 | if (ret) |
| 1070 | goto fail; |
| 1071 | |
| 1072 | /* update exposure time */ |
| 1073 | ret = __v4l2_ctrl_s_ctrl(imx274->ctrls.exposure, |
| 1074 | imx274->ctrls.exposure->val); |
| 1075 | if (ret) |
| 1076 | goto fail; |
| 1077 | |
| 1078 | /* start stream */ |
| 1079 | ret = imx274_start_stream(imx274); |
| 1080 | if (ret) |
| 1081 | goto fail; |
| 1082 | } else { |
| 1083 | /* stop stream */ |
| 1084 | ret = imx274_write_table(imx274, |
| 1085 | mode_table[IMX274_MODE_STOP_STREAM]); |
| 1086 | if (ret) |
| 1087 | goto fail; |
| 1088 | } |
| 1089 | |
| 1090 | mutex_unlock(&imx274->lock); |
| 1091 | dev_dbg(&imx274->client->dev, |
| 1092 | "%s : Done: mode = %d\n", __func__, imx274->mode_index); |
| 1093 | return 0; |
| 1094 | |
| 1095 | fail: |
| 1096 | mutex_unlock(&imx274->lock); |
| 1097 | dev_err(&imx274->client->dev, "s_stream failed\n"); |
| 1098 | return ret; |
| 1099 | } |
| 1100 | |
| 1101 | /* |
| 1102 | * imx274_get_frame_length - Function for obtaining current frame length |
| 1103 | * @priv: Pointer to device structure |
| 1104 | * @val: Pointer to obainted value |
| 1105 | * |
| 1106 | * frame_length = vmax x (svr + 1), in unit of hmax. |
| 1107 | * |
| 1108 | * Return: 0 on success |
| 1109 | */ |
| 1110 | static int imx274_get_frame_length(struct stimx274 *priv, u32 *val) |
| 1111 | { |
| 1112 | int err; |
| 1113 | u16 svr; |
| 1114 | u32 vmax; |
| 1115 | u8 reg_val[3]; |
| 1116 | |
| 1117 | /* svr */ |
| 1118 | err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, ®_val[0]); |
| 1119 | if (err) |
| 1120 | goto fail; |
| 1121 | |
| 1122 | err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, ®_val[1]); |
| 1123 | if (err) |
| 1124 | goto fail; |
| 1125 | |
| 1126 | svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; |
| 1127 | |
| 1128 | /* vmax */ |
| 1129 | err = imx274_read_reg(priv, IMX274_FRAME_LENGTH_ADDR_3, ®_val[0]); |
| 1130 | if (err) |
| 1131 | goto fail; |
| 1132 | |
| 1133 | err = imx274_read_reg(priv, IMX274_FRAME_LENGTH_ADDR_2, ®_val[1]); |
| 1134 | if (err) |
| 1135 | goto fail; |
| 1136 | |
| 1137 | err = imx274_read_reg(priv, IMX274_FRAME_LENGTH_ADDR_1, ®_val[2]); |
| 1138 | if (err) |
| 1139 | goto fail; |
| 1140 | |
| 1141 | vmax = ((reg_val[2] & IMX274_MASK_LSB_3_BITS) << IMX274_SHIFT_16_BITS) |
| 1142 | + (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; |
| 1143 | |
| 1144 | *val = vmax * (svr + 1); |
| 1145 | |
| 1146 | return 0; |
| 1147 | |
| 1148 | fail: |
| 1149 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1150 | return err; |
| 1151 | } |
| 1152 | |
| 1153 | static int imx274_clamp_coarse_time(struct stimx274 *priv, u32 *val, |
| 1154 | u32 *frame_length) |
| 1155 | { |
| 1156 | int err; |
| 1157 | |
| 1158 | err = imx274_get_frame_length(priv, frame_length); |
| 1159 | if (err) |
| 1160 | return err; |
| 1161 | |
| 1162 | if (*frame_length < min_frame_len[priv->mode_index]) |
| 1163 | *frame_length = min_frame_len[priv->mode_index]; |
| 1164 | |
| 1165 | *val = *frame_length - *val; /* convert to raw shr */ |
| 1166 | if (*val > *frame_length - IMX274_SHR_LIMIT_CONST) |
| 1167 | *val = *frame_length - IMX274_SHR_LIMIT_CONST; |
| 1168 | else if (*val < min_SHR[priv->mode_index]) |
| 1169 | *val = min_SHR[priv->mode_index]; |
| 1170 | |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | /* |
| 1175 | * imx274_set_digital gain - Function called when setting digital gain |
| 1176 | * @priv: Pointer to device structure |
| 1177 | * @dgain: Value of digital gain. |
| 1178 | * |
| 1179 | * Digital gain has only 4 steps: 1x, 2x, 4x, and 8x |
| 1180 | * |
| 1181 | * Return: 0 on success |
| 1182 | */ |
| 1183 | static int imx274_set_digital_gain(struct stimx274 *priv, u32 dgain) |
| 1184 | { |
| 1185 | u8 reg_val; |
| 1186 | |
| 1187 | reg_val = ffs(dgain); |
| 1188 | |
| 1189 | if (reg_val) |
| 1190 | reg_val--; |
| 1191 | |
| 1192 | reg_val = clamp(reg_val, (u8)0, (u8)3); |
| 1193 | |
| 1194 | return imx274_write_reg(priv, IMX274_DIGITAL_GAIN_REG, |
| 1195 | reg_val & IMX274_MASK_LSB_4_BITS); |
| 1196 | } |
| 1197 | |
| 1198 | static inline void imx274_calculate_gain_regs(struct reg_8 regs[2], u16 gain) |
| 1199 | { |
| 1200 | regs->addr = IMX274_ANALOG_GAIN_ADDR_MSB; |
| 1201 | regs->val = (gain >> IMX274_SHIFT_8_BITS) & IMX274_MASK_LSB_3_BITS; |
| 1202 | |
| 1203 | (regs + 1)->addr = IMX274_ANALOG_GAIN_ADDR_LSB; |
| 1204 | (regs + 1)->val = (gain) & IMX274_MASK_LSB_8_BITS; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * imx274_set_gain - Function called when setting gain |
| 1209 | * @priv: Pointer to device structure |
| 1210 | * @val: Value of gain. the real value = val << IMX274_GAIN_SHIFT; |
| 1211 | * @ctrl: v4l2 control pointer |
| 1212 | * |
| 1213 | * Set the gain based on input value. |
| 1214 | * The caller should hold the mutex lock imx274->lock if necessary |
| 1215 | * |
| 1216 | * Return: 0 on success |
| 1217 | */ |
| 1218 | static int imx274_set_gain(struct stimx274 *priv, struct v4l2_ctrl *ctrl) |
| 1219 | { |
| 1220 | struct reg_8 reg_list[2]; |
| 1221 | int err; |
| 1222 | u32 gain, analog_gain, digital_gain, gain_reg; |
| 1223 | int i; |
| 1224 | |
| 1225 | gain = (u32)(ctrl->val); |
| 1226 | |
| 1227 | dev_dbg(&priv->client->dev, |
| 1228 | "%s : input gain = %d.%d\n", __func__, |
| 1229 | gain >> IMX274_GAIN_SHIFT, |
| 1230 | ((gain & IMX274_GAIN_SHIFT_MASK) * 100) >> IMX274_GAIN_SHIFT); |
| 1231 | |
| 1232 | if (gain > IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN) |
| 1233 | gain = IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN; |
| 1234 | else if (gain < IMX274_MIN_GAIN) |
| 1235 | gain = IMX274_MIN_GAIN; |
| 1236 | |
| 1237 | if (gain <= IMX274_MAX_ANALOG_GAIN) |
| 1238 | digital_gain = 1; |
| 1239 | else if (gain <= IMX274_MAX_ANALOG_GAIN * 2) |
| 1240 | digital_gain = 2; |
| 1241 | else if (gain <= IMX274_MAX_ANALOG_GAIN * 4) |
| 1242 | digital_gain = 4; |
| 1243 | else |
| 1244 | digital_gain = IMX274_MAX_DIGITAL_GAIN; |
| 1245 | |
| 1246 | analog_gain = gain / digital_gain; |
| 1247 | |
| 1248 | dev_dbg(&priv->client->dev, |
| 1249 | "%s : digital gain = %d, analog gain = %d.%d\n", |
| 1250 | __func__, digital_gain, analog_gain >> IMX274_GAIN_SHIFT, |
| 1251 | ((analog_gain & IMX274_GAIN_SHIFT_MASK) * 100) |
| 1252 | >> IMX274_GAIN_SHIFT); |
| 1253 | |
| 1254 | err = imx274_set_digital_gain(priv, digital_gain); |
| 1255 | if (err) |
| 1256 | goto fail; |
| 1257 | |
| 1258 | /* convert to register value, refer to imx274 datasheet */ |
| 1259 | gain_reg = (u32)IMX274_GAIN_CONST - |
| 1260 | (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) / analog_gain; |
| 1261 | if (gain_reg > IMX274_GAIN_REG_MAX) |
| 1262 | gain_reg = IMX274_GAIN_REG_MAX; |
| 1263 | |
| 1264 | imx274_calculate_gain_regs(reg_list, (u16)gain_reg); |
| 1265 | |
| 1266 | for (i = 0; i < ARRAY_SIZE(reg_list); i++) { |
| 1267 | err = imx274_write_reg(priv, reg_list[i].addr, |
| 1268 | reg_list[i].val); |
| 1269 | if (err) |
| 1270 | goto fail; |
| 1271 | } |
| 1272 | |
| 1273 | if (IMX274_GAIN_CONST - gain_reg == 0) { |
| 1274 | err = -EINVAL; |
| 1275 | goto fail; |
| 1276 | } |
| 1277 | |
| 1278 | /* convert register value back to gain value */ |
| 1279 | ctrl->val = (IMX274_GAIN_CONST << IMX274_GAIN_SHIFT) |
| 1280 | / (IMX274_GAIN_CONST - gain_reg) * digital_gain; |
| 1281 | |
| 1282 | dev_dbg(&priv->client->dev, |
| 1283 | "%s : GAIN control success, gain_reg = %d, new gain = %d\n", |
| 1284 | __func__, gain_reg, ctrl->val); |
| 1285 | |
| 1286 | return 0; |
| 1287 | |
| 1288 | fail: |
| 1289 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1290 | return err; |
| 1291 | } |
| 1292 | |
| 1293 | static inline void imx274_calculate_coarse_time_regs(struct reg_8 regs[2], |
| 1294 | u32 coarse_time) |
| 1295 | { |
| 1296 | regs->addr = IMX274_COARSE_TIME_ADDR_MSB; |
| 1297 | regs->val = (coarse_time >> IMX274_SHIFT_8_BITS) |
| 1298 | & IMX274_MASK_LSB_8_BITS; |
| 1299 | (regs + 1)->addr = IMX274_COARSE_TIME_ADDR_LSB; |
| 1300 | (regs + 1)->val = (coarse_time) & IMX274_MASK_LSB_8_BITS; |
| 1301 | } |
| 1302 | |
| 1303 | /* |
| 1304 | * imx274_set_coarse_time - Function called when setting SHR value |
| 1305 | * @priv: Pointer to device structure |
| 1306 | * @val: Value for exposure time in number of line_length, or [HMAX] |
| 1307 | * |
| 1308 | * Set SHR value based on input value. |
| 1309 | * |
| 1310 | * Return: 0 on success |
| 1311 | */ |
| 1312 | static int imx274_set_coarse_time(struct stimx274 *priv, u32 *val) |
| 1313 | { |
| 1314 | struct reg_8 reg_list[2]; |
| 1315 | int err; |
| 1316 | u32 coarse_time, frame_length; |
| 1317 | int i; |
| 1318 | |
| 1319 | coarse_time = *val; |
| 1320 | |
| 1321 | /* convert exposure_time to appropriate SHR value */ |
| 1322 | err = imx274_clamp_coarse_time(priv, &coarse_time, &frame_length); |
| 1323 | if (err) |
| 1324 | goto fail; |
| 1325 | |
| 1326 | /* prepare SHR registers */ |
| 1327 | imx274_calculate_coarse_time_regs(reg_list, coarse_time); |
| 1328 | |
| 1329 | /* write to SHR registers */ |
| 1330 | for (i = 0; i < ARRAY_SIZE(reg_list); i++) { |
| 1331 | err = imx274_write_reg(priv, reg_list[i].addr, |
| 1332 | reg_list[i].val); |
| 1333 | if (err) |
| 1334 | goto fail; |
| 1335 | } |
| 1336 | |
| 1337 | *val = frame_length - coarse_time; |
| 1338 | return 0; |
| 1339 | |
| 1340 | fail: |
| 1341 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1342 | return err; |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * imx274_set_exposure - Function called when setting exposure time |
| 1347 | * @priv: Pointer to device structure |
| 1348 | * @val: Variable for exposure time, in the unit of micro-second |
| 1349 | * |
| 1350 | * Set exposure time based on input value. |
| 1351 | * The caller should hold the mutex lock imx274->lock if necessary |
| 1352 | * |
| 1353 | * Return: 0 on success |
| 1354 | */ |
| 1355 | static int imx274_set_exposure(struct stimx274 *priv, int val) |
| 1356 | { |
| 1357 | int err; |
| 1358 | u16 hmax; |
| 1359 | u8 reg_val[2]; |
| 1360 | u32 coarse_time; /* exposure time in unit of line (HMAX)*/ |
| 1361 | |
| 1362 | dev_dbg(&priv->client->dev, |
| 1363 | "%s : EXPOSURE control input = %d\n", __func__, val); |
| 1364 | |
| 1365 | /* step 1: convert input exposure_time (val) into number of 1[HMAX] */ |
| 1366 | |
| 1367 | /* obtain HMAX value */ |
| 1368 | err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, ®_val[0]); |
| 1369 | if (err) |
| 1370 | goto fail; |
| 1371 | err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, ®_val[1]); |
| 1372 | if (err) |
| 1373 | goto fail; |
| 1374 | hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; |
| 1375 | if (hmax == 0) { |
| 1376 | err = -EINVAL; |
| 1377 | goto fail; |
| 1378 | } |
| 1379 | |
| 1380 | coarse_time = (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2 * val |
| 1381 | - nocpiop[priv->mode_index]) / hmax; |
| 1382 | |
| 1383 | /* step 2: convert exposure_time into SHR value */ |
| 1384 | |
| 1385 | /* set SHR */ |
| 1386 | err = imx274_set_coarse_time(priv, &coarse_time); |
| 1387 | if (err) |
| 1388 | goto fail; |
| 1389 | |
| 1390 | priv->ctrls.exposure->val = |
| 1391 | (coarse_time * hmax + nocpiop[priv->mode_index]) |
| 1392 | / (IMX274_PIXCLK_CONST1 / IMX274_PIXCLK_CONST2); |
| 1393 | |
| 1394 | dev_dbg(&priv->client->dev, |
| 1395 | "%s : EXPOSURE control success\n", __func__); |
| 1396 | return 0; |
| 1397 | |
| 1398 | fail: |
| 1399 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1400 | |
| 1401 | return err; |
| 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | * imx274_set_vflip - Function called when setting vertical flip |
| 1406 | * @priv: Pointer to device structure |
| 1407 | * @val: Value for vflip setting |
| 1408 | * |
| 1409 | * Set vertical flip based on input value. |
| 1410 | * val = 0: normal, no vertical flip |
| 1411 | * val = 1: vertical flip enabled |
| 1412 | * The caller should hold the mutex lock imx274->lock if necessary |
| 1413 | * |
| 1414 | * Return: 0 on success |
| 1415 | */ |
| 1416 | static int imx274_set_vflip(struct stimx274 *priv, int val) |
| 1417 | { |
| 1418 | int err; |
| 1419 | |
| 1420 | err = imx274_write_reg(priv, IMX274_VFLIP_REG, val); |
| 1421 | if (err) { |
Luca Ceresoli | 9f67a5e | 2018-02-23 03:57:15 -0500 | [diff] [blame] | 1422 | dev_err(&priv->client->dev, "VFLIP control error\n"); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1423 | return err; |
| 1424 | } |
| 1425 | |
| 1426 | dev_dbg(&priv->client->dev, |
| 1427 | "%s : VFLIP control success\n", __func__); |
| 1428 | |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * imx274_set_test_pattern - Function called when setting test pattern |
| 1434 | * @priv: Pointer to device structure |
| 1435 | * @val: Variable for test pattern |
| 1436 | * |
| 1437 | * Set to different test patterns based on input value. |
| 1438 | * |
| 1439 | * Return: 0 on success |
| 1440 | */ |
| 1441 | static int imx274_set_test_pattern(struct stimx274 *priv, int val) |
| 1442 | { |
| 1443 | int err = 0; |
| 1444 | |
| 1445 | if (val == TEST_PATTERN_DISABLED) { |
| 1446 | err = imx274_write_table(priv, imx274_tp_disabled); |
| 1447 | } else if (val <= TEST_PATTERN_V_COLOR_BARS) { |
| 1448 | err = imx274_write_reg(priv, IMX274_TEST_PATTERN_REG, val - 1); |
| 1449 | if (!err) |
| 1450 | err = imx274_write_table(priv, imx274_tp_regs); |
| 1451 | } else { |
| 1452 | err = -EINVAL; |
| 1453 | } |
| 1454 | |
| 1455 | if (!err) |
| 1456 | dev_dbg(&priv->client->dev, |
| 1457 | "%s : TEST PATTERN control success\n", __func__); |
| 1458 | else |
| 1459 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1460 | |
| 1461 | return err; |
| 1462 | } |
| 1463 | |
| 1464 | static inline void imx274_calculate_frame_length_regs(struct reg_8 regs[3], |
| 1465 | u32 frame_length) |
| 1466 | { |
| 1467 | regs->addr = IMX274_FRAME_LENGTH_ADDR_1; |
| 1468 | regs->val = (frame_length >> IMX274_SHIFT_16_BITS) |
| 1469 | & IMX274_MASK_LSB_4_BITS; |
| 1470 | (regs + 1)->addr = IMX274_FRAME_LENGTH_ADDR_2; |
| 1471 | (regs + 1)->val = (frame_length >> IMX274_SHIFT_8_BITS) |
| 1472 | & IMX274_MASK_LSB_8_BITS; |
| 1473 | (regs + 2)->addr = IMX274_FRAME_LENGTH_ADDR_3; |
| 1474 | (regs + 2)->val = (frame_length) & IMX274_MASK_LSB_8_BITS; |
| 1475 | } |
| 1476 | |
| 1477 | /* |
| 1478 | * imx274_set_frame_length - Function called when setting frame length |
| 1479 | * @priv: Pointer to device structure |
| 1480 | * @val: Variable for frame length (= VMAX, i.e. vertical drive period length) |
| 1481 | * |
| 1482 | * Set frame length based on input value. |
| 1483 | * |
| 1484 | * Return: 0 on success |
| 1485 | */ |
| 1486 | static int imx274_set_frame_length(struct stimx274 *priv, u32 val) |
| 1487 | { |
| 1488 | struct reg_8 reg_list[3]; |
| 1489 | int err; |
| 1490 | u32 frame_length; |
| 1491 | int i; |
| 1492 | |
| 1493 | dev_dbg(&priv->client->dev, "%s : input length = %d\n", |
| 1494 | __func__, val); |
| 1495 | |
| 1496 | frame_length = (u32)val; |
| 1497 | |
| 1498 | imx274_calculate_frame_length_regs(reg_list, frame_length); |
| 1499 | for (i = 0; i < ARRAY_SIZE(reg_list); i++) { |
| 1500 | err = imx274_write_reg(priv, reg_list[i].addr, |
| 1501 | reg_list[i].val); |
| 1502 | if (err) |
| 1503 | goto fail; |
| 1504 | } |
| 1505 | |
| 1506 | return 0; |
| 1507 | |
| 1508 | fail: |
| 1509 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1510 | return err; |
| 1511 | } |
| 1512 | |
| 1513 | /* |
| 1514 | * imx274_set_frame_interval - Function called when setting frame interval |
| 1515 | * @priv: Pointer to device structure |
| 1516 | * @frame_interval: Variable for frame interval |
| 1517 | * |
| 1518 | * Change frame interval by updating VMAX value |
| 1519 | * The caller should hold the mutex lock imx274->lock if necessary |
| 1520 | * |
| 1521 | * Return: 0 on success |
| 1522 | */ |
| 1523 | static int imx274_set_frame_interval(struct stimx274 *priv, |
| 1524 | struct v4l2_fract frame_interval) |
| 1525 | { |
| 1526 | int err; |
| 1527 | u32 frame_length, req_frame_rate; |
| 1528 | u16 svr; |
| 1529 | u16 hmax; |
| 1530 | u8 reg_val[2]; |
| 1531 | |
| 1532 | dev_dbg(&priv->client->dev, "%s: input frame interval = %d / %d", |
| 1533 | __func__, frame_interval.numerator, |
| 1534 | frame_interval.denominator); |
| 1535 | |
| 1536 | if (frame_interval.numerator == 0) { |
| 1537 | err = -EINVAL; |
| 1538 | goto fail; |
| 1539 | } |
| 1540 | |
| 1541 | req_frame_rate = (u32)(frame_interval.denominator |
| 1542 | / frame_interval.numerator); |
| 1543 | |
| 1544 | /* boundary check */ |
| 1545 | if (req_frame_rate > max_frame_rate[priv->mode_index]) { |
| 1546 | frame_interval.numerator = 1; |
| 1547 | frame_interval.denominator = |
| 1548 | max_frame_rate[priv->mode_index]; |
| 1549 | } else if (req_frame_rate < IMX274_MIN_FRAME_RATE) { |
| 1550 | frame_interval.numerator = 1; |
| 1551 | frame_interval.denominator = IMX274_MIN_FRAME_RATE; |
| 1552 | } |
| 1553 | |
| 1554 | /* |
| 1555 | * VMAX = 1/frame_rate x 72M / (SVR+1) / HMAX |
| 1556 | * frame_length (i.e. VMAX) = (frame_interval) x 72M /(SVR+1) / HMAX |
| 1557 | */ |
| 1558 | |
| 1559 | /* SVR */ |
| 1560 | err = imx274_read_reg(priv, IMX274_SVR_REG_LSB, ®_val[0]); |
| 1561 | if (err) |
| 1562 | goto fail; |
| 1563 | err = imx274_read_reg(priv, IMX274_SVR_REG_MSB, ®_val[1]); |
| 1564 | if (err) |
| 1565 | goto fail; |
| 1566 | svr = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; |
| 1567 | dev_dbg(&priv->client->dev, |
| 1568 | "%s : register SVR = %d\n", __func__, svr); |
| 1569 | |
| 1570 | /* HMAX */ |
| 1571 | err = imx274_read_reg(priv, IMX274_HMAX_REG_LSB, ®_val[0]); |
| 1572 | if (err) |
| 1573 | goto fail; |
| 1574 | err = imx274_read_reg(priv, IMX274_HMAX_REG_MSB, ®_val[1]); |
| 1575 | if (err) |
| 1576 | goto fail; |
| 1577 | hmax = (reg_val[1] << IMX274_SHIFT_8_BITS) + reg_val[0]; |
| 1578 | dev_dbg(&priv->client->dev, |
| 1579 | "%s : register HMAX = %d\n", __func__, hmax); |
| 1580 | |
| 1581 | if (hmax == 0 || frame_interval.denominator == 0) { |
| 1582 | err = -EINVAL; |
| 1583 | goto fail; |
| 1584 | } |
| 1585 | |
| 1586 | frame_length = IMX274_PIXCLK_CONST1 / (svr + 1) / hmax |
| 1587 | * frame_interval.numerator |
| 1588 | / frame_interval.denominator; |
| 1589 | |
| 1590 | err = imx274_set_frame_length(priv, frame_length); |
| 1591 | if (err) |
| 1592 | goto fail; |
| 1593 | |
| 1594 | priv->frame_interval = frame_interval; |
| 1595 | return 0; |
| 1596 | |
| 1597 | fail: |
| 1598 | dev_err(&priv->client->dev, "%s error = %d\n", __func__, err); |
| 1599 | return err; |
| 1600 | } |
| 1601 | |
| 1602 | static const struct v4l2_subdev_pad_ops imx274_pad_ops = { |
| 1603 | .get_fmt = imx274_get_fmt, |
| 1604 | .set_fmt = imx274_set_fmt, |
| 1605 | }; |
| 1606 | |
| 1607 | static const struct v4l2_subdev_video_ops imx274_video_ops = { |
| 1608 | .g_frame_interval = imx274_g_frame_interval, |
| 1609 | .s_frame_interval = imx274_s_frame_interval, |
| 1610 | .s_stream = imx274_s_stream, |
| 1611 | }; |
| 1612 | |
| 1613 | static const struct v4l2_subdev_ops imx274_subdev_ops = { |
| 1614 | .pad = &imx274_pad_ops, |
| 1615 | .video = &imx274_video_ops, |
| 1616 | }; |
| 1617 | |
| 1618 | static const struct v4l2_ctrl_ops imx274_ctrl_ops = { |
| 1619 | .s_ctrl = imx274_s_ctrl, |
| 1620 | }; |
| 1621 | |
| 1622 | static const struct of_device_id imx274_of_id_table[] = { |
| 1623 | { .compatible = "sony,imx274" }, |
| 1624 | { } |
| 1625 | }; |
| 1626 | MODULE_DEVICE_TABLE(of, imx274_of_id_table); |
| 1627 | |
| 1628 | static const struct i2c_device_id imx274_id[] = { |
| 1629 | { "IMX274", 0 }, |
| 1630 | { } |
| 1631 | }; |
| 1632 | MODULE_DEVICE_TABLE(i2c, imx274_id); |
| 1633 | |
| 1634 | static int imx274_probe(struct i2c_client *client, |
| 1635 | const struct i2c_device_id *id) |
| 1636 | { |
| 1637 | struct v4l2_subdev *sd; |
| 1638 | struct stimx274 *imx274; |
| 1639 | int ret; |
| 1640 | |
| 1641 | /* initialize imx274 */ |
| 1642 | imx274 = devm_kzalloc(&client->dev, sizeof(*imx274), GFP_KERNEL); |
| 1643 | if (!imx274) |
| 1644 | return -ENOMEM; |
| 1645 | |
| 1646 | mutex_init(&imx274->lock); |
| 1647 | |
| 1648 | /* initialize regmap */ |
| 1649 | imx274->regmap = devm_regmap_init_i2c(client, &imx274_regmap_config); |
| 1650 | if (IS_ERR(imx274->regmap)) { |
| 1651 | dev_err(&client->dev, |
| 1652 | "regmap init failed: %ld\n", PTR_ERR(imx274->regmap)); |
| 1653 | ret = -ENODEV; |
| 1654 | goto err_regmap; |
| 1655 | } |
| 1656 | |
| 1657 | /* initialize subdevice */ |
| 1658 | imx274->client = client; |
| 1659 | sd = &imx274->sd; |
| 1660 | v4l2_i2c_subdev_init(sd, client, &imx274_subdev_ops); |
| 1661 | strlcpy(sd->name, DRIVER_NAME, sizeof(sd->name)); |
| 1662 | sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; |
| 1663 | |
| 1664 | /* initialize subdev media pad */ |
| 1665 | imx274->pad.flags = MEDIA_PAD_FL_SOURCE; |
| 1666 | sd->entity.function = MEDIA_ENT_F_CAM_SENSOR; |
| 1667 | ret = media_entity_pads_init(&sd->entity, 1, &imx274->pad); |
| 1668 | if (ret < 0) { |
| 1669 | dev_err(&client->dev, |
| 1670 | "%s : media entity init Failed %d\n", __func__, ret); |
| 1671 | goto err_regmap; |
| 1672 | } |
| 1673 | |
| 1674 | /* initialize sensor reset gpio */ |
| 1675 | imx274->reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", |
| 1676 | GPIOD_OUT_HIGH); |
| 1677 | if (IS_ERR(imx274->reset_gpio)) { |
| 1678 | if (PTR_ERR(imx274->reset_gpio) != -EPROBE_DEFER) |
| 1679 | dev_err(&client->dev, "Reset GPIO not setup in DT"); |
| 1680 | ret = PTR_ERR(imx274->reset_gpio); |
| 1681 | goto err_me; |
| 1682 | } |
| 1683 | |
| 1684 | /* pull sensor out of reset */ |
| 1685 | imx274_reset(imx274, 1); |
| 1686 | |
| 1687 | /* initialize controls */ |
| 1688 | ret = v4l2_ctrl_handler_init(&imx274->ctrls.handler, 2); |
| 1689 | if (ret < 0) { |
| 1690 | dev_err(&client->dev, |
| 1691 | "%s : ctrl handler init Failed\n", __func__); |
| 1692 | goto err_me; |
| 1693 | } |
| 1694 | |
| 1695 | imx274->ctrls.handler.lock = &imx274->lock; |
| 1696 | |
| 1697 | /* add new controls */ |
| 1698 | imx274->ctrls.test_pattern = v4l2_ctrl_new_std_menu_items( |
| 1699 | &imx274->ctrls.handler, &imx274_ctrl_ops, |
| 1700 | V4L2_CID_TEST_PATTERN, |
| 1701 | ARRAY_SIZE(tp_qmenu) - 1, 0, 0, tp_qmenu); |
| 1702 | |
| 1703 | imx274->ctrls.gain = v4l2_ctrl_new_std( |
| 1704 | &imx274->ctrls.handler, |
| 1705 | &imx274_ctrl_ops, |
| 1706 | V4L2_CID_GAIN, IMX274_MIN_GAIN, |
| 1707 | IMX274_MAX_DIGITAL_GAIN * IMX274_MAX_ANALOG_GAIN, 1, |
| 1708 | IMX274_DEF_GAIN); |
| 1709 | |
| 1710 | imx274->ctrls.exposure = v4l2_ctrl_new_std( |
| 1711 | &imx274->ctrls.handler, |
| 1712 | &imx274_ctrl_ops, |
| 1713 | V4L2_CID_EXPOSURE, IMX274_MIN_EXPOSURE_TIME, |
| 1714 | 1000000 / IMX274_DEF_FRAME_RATE, 1, |
| 1715 | IMX274_MIN_EXPOSURE_TIME); |
| 1716 | |
| 1717 | imx274->ctrls.vflip = v4l2_ctrl_new_std( |
| 1718 | &imx274->ctrls.handler, |
| 1719 | &imx274_ctrl_ops, |
| 1720 | V4L2_CID_VFLIP, 0, 1, 1, 0); |
| 1721 | |
| 1722 | imx274->sd.ctrl_handler = &imx274->ctrls.handler; |
| 1723 | if (imx274->ctrls.handler.error) { |
| 1724 | ret = imx274->ctrls.handler.error; |
| 1725 | goto err_ctrls; |
| 1726 | } |
| 1727 | |
| 1728 | /* setup default controls */ |
| 1729 | ret = v4l2_ctrl_handler_setup(&imx274->ctrls.handler); |
| 1730 | if (ret) { |
| 1731 | dev_err(&client->dev, |
| 1732 | "Error %d setup default controls\n", ret); |
| 1733 | goto err_ctrls; |
| 1734 | } |
| 1735 | |
| 1736 | /* initialize format */ |
| 1737 | imx274->mode_index = IMX274_MODE_3840X2160; |
| 1738 | imx274->format.width = imx274_formats[0].size.width; |
| 1739 | imx274->format.height = imx274_formats[0].size.height; |
| 1740 | imx274->format.field = V4L2_FIELD_NONE; |
| 1741 | imx274->format.code = MEDIA_BUS_FMT_SRGGB10_1X10; |
| 1742 | imx274->format.colorspace = V4L2_COLORSPACE_SRGB; |
| 1743 | imx274->frame_interval.numerator = 1; |
| 1744 | imx274->frame_interval.denominator = IMX274_DEF_FRAME_RATE; |
| 1745 | |
| 1746 | /* load default control values */ |
| 1747 | ret = imx274_load_default(imx274); |
| 1748 | if (ret) { |
| 1749 | dev_err(&client->dev, |
| 1750 | "%s : imx274_load_default failed %d\n", |
| 1751 | __func__, ret); |
| 1752 | goto err_ctrls; |
| 1753 | } |
| 1754 | |
| 1755 | /* register subdevice */ |
| 1756 | ret = v4l2_async_register_subdev(sd); |
| 1757 | if (ret < 0) { |
| 1758 | dev_err(&client->dev, |
| 1759 | "%s : v4l2_async_register_subdev failed %d\n", |
| 1760 | __func__, ret); |
| 1761 | goto err_ctrls; |
| 1762 | } |
| 1763 | |
| 1764 | dev_info(&client->dev, "imx274 : imx274 probe success !\n"); |
| 1765 | return 0; |
| 1766 | |
| 1767 | err_ctrls: |
Sakari Ailus | 781b045 | 2017-11-01 05:40:58 -0400 | [diff] [blame] | 1768 | v4l2_ctrl_handler_free(&imx274->ctrls.handler); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1769 | err_me: |
| 1770 | media_entity_cleanup(&sd->entity); |
| 1771 | err_regmap: |
| 1772 | mutex_destroy(&imx274->lock); |
| 1773 | return ret; |
| 1774 | } |
| 1775 | |
| 1776 | static int imx274_remove(struct i2c_client *client) |
| 1777 | { |
| 1778 | struct v4l2_subdev *sd = i2c_get_clientdata(client); |
| 1779 | struct stimx274 *imx274 = to_imx274(sd); |
| 1780 | |
| 1781 | /* stop stream */ |
| 1782 | imx274_write_table(imx274, mode_table[IMX274_MODE_STOP_STREAM]); |
| 1783 | |
| 1784 | v4l2_async_unregister_subdev(sd); |
Sakari Ailus | 781b045 | 2017-11-01 05:40:58 -0400 | [diff] [blame] | 1785 | v4l2_ctrl_handler_free(&imx274->ctrls.handler); |
Leon Luo | 0985dd3 | 2017-10-05 02:06:21 +0200 | [diff] [blame] | 1786 | media_entity_cleanup(&sd->entity); |
| 1787 | mutex_destroy(&imx274->lock); |
| 1788 | return 0; |
| 1789 | } |
| 1790 | |
| 1791 | static struct i2c_driver imx274_i2c_driver = { |
| 1792 | .driver = { |
| 1793 | .name = DRIVER_NAME, |
| 1794 | .of_match_table = imx274_of_id_table, |
| 1795 | }, |
| 1796 | .probe = imx274_probe, |
| 1797 | .remove = imx274_remove, |
| 1798 | .id_table = imx274_id, |
| 1799 | }; |
| 1800 | |
| 1801 | module_i2c_driver(imx274_i2c_driver); |
| 1802 | |
| 1803 | MODULE_AUTHOR("Leon Luo <leonl@leopardimaging.com>"); |
| 1804 | MODULE_DESCRIPTION("IMX274 CMOS Image Sensor driver"); |
| 1805 | MODULE_LICENSE("GPL v2"); |