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