1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * A V4L2 driver for Sony IMX219 cameras. 4 * Copyright (C) 2019, Raspberry Pi (Trading) Ltd 5 * 6 * Based on Sony imx258 camera driver 7 * Copyright (C) 2018 Intel Corporation 8 * 9 * DT / fwnode changes, and regulator / GPIO control taken from imx214 driver 10 * Copyright 2018 Qtechnology A/S 11 * 12 * Flip handling taken from the Sony IMX319 driver. 13 * Copyright (C) 2018 Intel Corporation 14 * 15 */ 16 17 #include <linux/clk.h> 18 #include <linux/delay.h> 19 #include <linux/gpio/consumer.h> 20 #include <linux/i2c.h> 21 #include <linux/minmax.h> 22 #include <linux/module.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/regulator/consumer.h> 25 26 #include <media/v4l2-cci.h> 27 #include <media/v4l2-ctrls.h> 28 #include <media/v4l2-device.h> 29 #include <media/v4l2-fwnode.h> 30 #include <media/v4l2-mediabus.h> 31 32 /* Chip ID */ 33 #define IMX219_REG_CHIP_ID CCI_REG16(0x0000) 34 #define IMX219_CHIP_ID 0x0219 35 36 #define IMX219_REG_MODE_SELECT CCI_REG8(0x0100) 37 #define IMX219_MODE_STANDBY 0x00 38 #define IMX219_MODE_STREAMING 0x01 39 40 #define IMX219_REG_CSI_LANE_MODE CCI_REG8(0x0114) 41 #define IMX219_CSI_2_LANE_MODE 0x01 42 #define IMX219_CSI_4_LANE_MODE 0x03 43 44 #define IMX219_REG_DPHY_CTRL CCI_REG8(0x0128) 45 #define IMX219_DPHY_CTRL_TIMING_AUTO 0 46 #define IMX219_DPHY_CTRL_TIMING_MANUAL 1 47 48 #define IMX219_REG_EXCK_FREQ CCI_REG16(0x012a) 49 #define IMX219_EXCK_FREQ(n) ((n) * 256) /* n expressed in MHz */ 50 51 /* Analog gain control */ 52 #define IMX219_REG_ANALOG_GAIN CCI_REG8(0x0157) 53 #define IMX219_ANA_GAIN_MIN 0 54 #define IMX219_ANA_GAIN_MAX 232 55 #define IMX219_ANA_GAIN_STEP 1 56 #define IMX219_ANA_GAIN_DEFAULT 0x0 57 58 /* Digital gain control */ 59 #define IMX219_REG_DIGITAL_GAIN CCI_REG16(0x0158) 60 #define IMX219_DGTL_GAIN_MIN 0x0100 61 #define IMX219_DGTL_GAIN_MAX 0x0fff 62 #define IMX219_DGTL_GAIN_DEFAULT 0x0100 63 #define IMX219_DGTL_GAIN_STEP 1 64 65 /* Exposure control */ 66 #define IMX219_REG_EXPOSURE CCI_REG16(0x015a) 67 #define IMX219_EXPOSURE_MIN 4 68 #define IMX219_EXPOSURE_STEP 1 69 #define IMX219_EXPOSURE_DEFAULT 0x640 70 #define IMX219_EXPOSURE_MAX 65535 71 #define IMX219_EXPOSURE_OFFSET 4 72 73 /* V_TIMING internal */ 74 #define IMX219_REG_FRM_LENGTH_A CCI_REG16(0x0160) 75 #define IMX219_FLL_MAX 0xffff 76 #define IMX219_VBLANK_MIN 32 77 #define IMX219_REG_LINE_LENGTH_A CCI_REG16(0x0162) 78 #define IMX219_LLP_MIN 0x0d78 79 #define IMX219_BINNED_LLP_MIN 0x0de8 80 #define IMX219_LLP_MAX 0x7ff0 81 82 #define IMX219_REG_X_ADD_STA_A CCI_REG16(0x0164) 83 #define IMX219_REG_X_ADD_END_A CCI_REG16(0x0166) 84 #define IMX219_REG_Y_ADD_STA_A CCI_REG16(0x0168) 85 #define IMX219_REG_Y_ADD_END_A CCI_REG16(0x016a) 86 #define IMX219_REG_X_OUTPUT_SIZE CCI_REG16(0x016c) 87 #define IMX219_REG_Y_OUTPUT_SIZE CCI_REG16(0x016e) 88 #define IMX219_REG_X_ODD_INC_A CCI_REG8(0x0170) 89 #define IMX219_REG_Y_ODD_INC_A CCI_REG8(0x0171) 90 #define IMX219_REG_ORIENTATION CCI_REG8(0x0172) 91 92 /* Binning Mode */ 93 #define IMX219_REG_BINNING_MODE_H CCI_REG8(0x0174) 94 #define IMX219_REG_BINNING_MODE_V CCI_REG8(0x0175) 95 #define IMX219_BINNING_NONE 0x00 96 #define IMX219_BINNING_X2 0x01 97 #define IMX219_BINNING_X2_ANALOG 0x03 98 99 #define IMX219_REG_CSI_DATA_FORMAT_A CCI_REG16(0x018c) 100 101 /* PLL Settings */ 102 #define IMX219_REG_VTPXCK_DIV CCI_REG8(0x0301) 103 #define IMX219_REG_VTSYCK_DIV CCI_REG8(0x0303) 104 #define IMX219_REG_PREPLLCK_VT_DIV CCI_REG8(0x0304) 105 #define IMX219_REG_PREPLLCK_OP_DIV CCI_REG8(0x0305) 106 #define IMX219_REG_PLL_VT_MPY CCI_REG16(0x0306) 107 #define IMX219_REG_OPPXCK_DIV CCI_REG8(0x0309) 108 #define IMX219_REG_OPSYCK_DIV CCI_REG8(0x030b) 109 #define IMX219_REG_PLL_OP_MPY CCI_REG16(0x030c) 110 111 /* Test Pattern Control */ 112 #define IMX219_REG_TEST_PATTERN CCI_REG16(0x0600) 113 #define IMX219_TEST_PATTERN_DISABLE 0 114 #define IMX219_TEST_PATTERN_SOLID_COLOR 1 115 #define IMX219_TEST_PATTERN_COLOR_BARS 2 116 #define IMX219_TEST_PATTERN_GREY_COLOR 3 117 #define IMX219_TEST_PATTERN_PN9 4 118 119 /* Test pattern colour components */ 120 #define IMX219_REG_TESTP_RED CCI_REG16(0x0602) 121 #define IMX219_REG_TESTP_GREENR CCI_REG16(0x0604) 122 #define IMX219_REG_TESTP_BLUE CCI_REG16(0x0606) 123 #define IMX219_REG_TESTP_GREENB CCI_REG16(0x0608) 124 #define IMX219_TESTP_COLOUR_MIN 0 125 #define IMX219_TESTP_COLOUR_MAX 0x03ff 126 #define IMX219_TESTP_COLOUR_STEP 1 127 128 #define IMX219_REG_TP_WINDOW_WIDTH CCI_REG16(0x0624) 129 #define IMX219_REG_TP_WINDOW_HEIGHT CCI_REG16(0x0626) 130 131 /* External clock frequency is 24.0M */ 132 #define IMX219_XCLK_FREQ 24000000 133 134 /* Pixel rate is fixed for all the modes */ 135 #define IMX219_PIXEL_RATE 182400000 136 #define IMX219_PIXEL_RATE_4LANE 281600000 137 138 #define IMX219_DEFAULT_LINK_FREQ 456000000 139 #define IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED 363000000 140 #define IMX219_DEFAULT_LINK_FREQ_4LANE 364000000 141 142 /* IMX219 native and active pixel array size. */ 143 #define IMX219_NATIVE_WIDTH 3296U 144 #define IMX219_NATIVE_HEIGHT 2480U 145 #define IMX219_PIXEL_ARRAY_LEFT 8U 146 #define IMX219_PIXEL_ARRAY_TOP 8U 147 #define IMX219_PIXEL_ARRAY_WIDTH 3280U 148 #define IMX219_PIXEL_ARRAY_HEIGHT 2464U 149 150 /* Mode : resolution and related config&values */ 151 struct imx219_mode { 152 /* Frame width */ 153 unsigned int width; 154 /* Frame height */ 155 unsigned int height; 156 157 /* V-timing */ 158 unsigned int fll_def; 159 }; 160 161 static const struct cci_reg_sequence imx219_common_regs[] = { 162 { IMX219_REG_MODE_SELECT, 0x00 }, /* Mode Select */ 163 164 /* To Access Addresses 3000-5fff, send the following commands */ 165 { CCI_REG8(0x30eb), 0x05 }, 166 { CCI_REG8(0x30eb), 0x0c }, 167 { CCI_REG8(0x300a), 0xff }, 168 { CCI_REG8(0x300b), 0xff }, 169 { CCI_REG8(0x30eb), 0x05 }, 170 { CCI_REG8(0x30eb), 0x09 }, 171 172 /* Undocumented registers */ 173 { CCI_REG8(0x455e), 0x00 }, 174 { CCI_REG8(0x471e), 0x4b }, 175 { CCI_REG8(0x4767), 0x0f }, 176 { CCI_REG8(0x4750), 0x14 }, 177 { CCI_REG8(0x4540), 0x00 }, 178 { CCI_REG8(0x47b4), 0x14 }, 179 { CCI_REG8(0x4713), 0x30 }, 180 { CCI_REG8(0x478b), 0x10 }, 181 { CCI_REG8(0x478f), 0x10 }, 182 { CCI_REG8(0x4793), 0x10 }, 183 { CCI_REG8(0x4797), 0x0e }, 184 { CCI_REG8(0x479b), 0x0e }, 185 186 /* Frame Bank Register Group "A" */ 187 { IMX219_REG_X_ODD_INC_A, 1 }, 188 { IMX219_REG_Y_ODD_INC_A, 1 }, 189 190 /* Output setup registers */ 191 { IMX219_REG_DPHY_CTRL, IMX219_DPHY_CTRL_TIMING_AUTO }, 192 { IMX219_REG_EXCK_FREQ, IMX219_EXCK_FREQ(IMX219_XCLK_FREQ / 1000000) }, 193 }; 194 195 static const struct cci_reg_sequence imx219_2lane_regs[] = { 196 /* PLL Clock Table */ 197 { IMX219_REG_VTPXCK_DIV, 5 }, 198 { IMX219_REG_VTSYCK_DIV, 1 }, 199 { IMX219_REG_PREPLLCK_VT_DIV, 3 }, /* 0x03 = AUTO set */ 200 { IMX219_REG_PREPLLCK_OP_DIV, 3 }, /* 0x03 = AUTO set */ 201 { IMX219_REG_PLL_VT_MPY, 57 }, 202 { IMX219_REG_OPSYCK_DIV, 1 }, 203 { IMX219_REG_PLL_OP_MPY, 114 }, 204 205 /* 2-Lane CSI Mode */ 206 { IMX219_REG_CSI_LANE_MODE, IMX219_CSI_2_LANE_MODE }, 207 }; 208 209 static const struct cci_reg_sequence imx219_4lane_regs[] = { 210 /* PLL Clock Table */ 211 { IMX219_REG_VTPXCK_DIV, 5 }, 212 { IMX219_REG_VTSYCK_DIV, 1 }, 213 { IMX219_REG_PREPLLCK_VT_DIV, 3 }, /* 0x03 = AUTO set */ 214 { IMX219_REG_PREPLLCK_OP_DIV, 3 }, /* 0x03 = AUTO set */ 215 { IMX219_REG_PLL_VT_MPY, 88 }, 216 { IMX219_REG_OPSYCK_DIV, 1 }, 217 { IMX219_REG_PLL_OP_MPY, 91 }, 218 219 /* 4-Lane CSI Mode */ 220 { IMX219_REG_CSI_LANE_MODE, IMX219_CSI_4_LANE_MODE }, 221 }; 222 223 static const s64 imx219_link_freq_menu[] = { 224 IMX219_DEFAULT_LINK_FREQ, 225 }; 226 227 static const s64 imx219_link_freq_4lane_menu[] = { 228 IMX219_DEFAULT_LINK_FREQ_4LANE, 229 /* 230 * This will never be advertised to userspace, but will be used for 231 * v4l2_link_freq_to_bitmap 232 */ 233 IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED, 234 }; 235 236 static const char * const imx219_test_pattern_menu[] = { 237 "Disabled", 238 "Color Bars", 239 "Solid Color", 240 "Grey Color Bars", 241 "PN9" 242 }; 243 244 static const int imx219_test_pattern_val[] = { 245 IMX219_TEST_PATTERN_DISABLE, 246 IMX219_TEST_PATTERN_COLOR_BARS, 247 IMX219_TEST_PATTERN_SOLID_COLOR, 248 IMX219_TEST_PATTERN_GREY_COLOR, 249 IMX219_TEST_PATTERN_PN9, 250 }; 251 252 /* regulator supplies */ 253 static const char * const imx219_supply_name[] = { 254 /* Supplies can be enabled in any order */ 255 "VANA", /* Analog (2.8V) supply */ 256 "VDIG", /* Digital Core (1.8V) supply */ 257 "VDDL", /* IF (1.2V) supply */ 258 }; 259 260 #define IMX219_NUM_SUPPLIES ARRAY_SIZE(imx219_supply_name) 261 262 /* 263 * The supported formats. 264 * This table MUST contain 4 entries per format, to cover the various flip 265 * combinations in the order 266 * - no flip 267 * - h flip 268 * - v flip 269 * - h&v flips 270 */ 271 static const u32 imx219_mbus_formats[] = { 272 MEDIA_BUS_FMT_SRGGB10_1X10, 273 MEDIA_BUS_FMT_SGRBG10_1X10, 274 MEDIA_BUS_FMT_SGBRG10_1X10, 275 MEDIA_BUS_FMT_SBGGR10_1X10, 276 277 MEDIA_BUS_FMT_SRGGB8_1X8, 278 MEDIA_BUS_FMT_SGRBG8_1X8, 279 MEDIA_BUS_FMT_SGBRG8_1X8, 280 MEDIA_BUS_FMT_SBGGR8_1X8, 281 }; 282 283 /* 284 * Initialisation delay between XCLR low->high and the moment when the sensor 285 * can start capture (i.e. can leave software stanby) must be not less than: 286 * t4 + max(t5, t6 + <time to initialize the sensor register over I2C>) 287 * where 288 * t4 is fixed, and is max 200uS, 289 * t5 is fixed, and is 6000uS, 290 * t6 depends on the sensor external clock, and is max 32000 clock periods. 291 * As per sensor datasheet, the external clock must be from 6MHz to 27MHz. 292 * So for any acceptable external clock t6 is always within the range of 293 * 1185 to 5333 uS, and is always less than t5. 294 * For this reason this is always safe to wait (t4 + t5) = 6200 uS, then 295 * initialize the sensor over I2C, and then exit the software standby. 296 * 297 * This start-up time can be optimized a bit more, if we start the writes 298 * over I2C after (t4+t6), but before (t4+t5) expires. But then sensor 299 * initialization over I2C may complete before (t4+t5) expires, and we must 300 * ensure that capture is not started before (t4+t5). 301 * 302 * This delay doesn't account for the power supply startup time. If needed, 303 * this should be taken care of via the regulator framework. E.g. in the 304 * case of DT for regulator-fixed one should define the startup-delay-us 305 * property. 306 */ 307 #define IMX219_XCLR_MIN_DELAY_US 6200 308 #define IMX219_XCLR_DELAY_RANGE_US 1000 309 310 /* Mode configs */ 311 static const struct imx219_mode supported_modes[] = { 312 { 313 /* 8MPix 15fps mode */ 314 .width = 3280, 315 .height = 2464, 316 .fll_def = 3526, 317 }, 318 { 319 /* 1080P 30fps cropped */ 320 .width = 1920, 321 .height = 1080, 322 .fll_def = 1763, 323 }, 324 { 325 /* 2x2 binned 60fps mode */ 326 .width = 1640, 327 .height = 1232, 328 .fll_def = 1707, 329 }, 330 { 331 /* 640x480 60fps mode */ 332 .width = 640, 333 .height = 480, 334 .fll_def = 1707, 335 }, 336 }; 337 338 struct imx219 { 339 struct v4l2_subdev sd; 340 struct media_pad pad; 341 342 struct regmap *regmap; 343 struct clk *xclk; /* system clock to IMX219 */ 344 u32 xclk_freq; 345 346 struct gpio_desc *reset_gpio; 347 struct regulator_bulk_data supplies[IMX219_NUM_SUPPLIES]; 348 349 struct v4l2_ctrl_handler ctrl_handler; 350 /* V4L2 Controls */ 351 struct v4l2_ctrl *pixel_rate; 352 struct v4l2_ctrl *link_freq; 353 struct v4l2_ctrl *exposure; 354 struct v4l2_ctrl *vflip; 355 struct v4l2_ctrl *hflip; 356 struct v4l2_ctrl *vblank; 357 struct v4l2_ctrl *hblank; 358 359 /* Two or Four lanes */ 360 u8 lanes; 361 }; 362 363 static inline struct imx219 *to_imx219(struct v4l2_subdev *_sd) 364 { 365 return container_of(_sd, struct imx219, sd); 366 } 367 368 /* Get bayer order based on flip setting. */ 369 static u32 imx219_get_format_code(struct imx219 *imx219, u32 code) 370 { 371 unsigned int i; 372 373 for (i = 0; i < ARRAY_SIZE(imx219_mbus_formats); i++) 374 if (imx219_mbus_formats[i] == code) 375 break; 376 377 if (i >= ARRAY_SIZE(imx219_mbus_formats)) 378 i = 0; 379 380 i = (i & ~3) | (imx219->vflip->val ? 2 : 0) | 381 (imx219->hflip->val ? 1 : 0); 382 383 return imx219_mbus_formats[i]; 384 } 385 386 static u32 imx219_get_format_bpp(const struct v4l2_mbus_framefmt *format) 387 { 388 switch (format->code) { 389 case MEDIA_BUS_FMT_SRGGB8_1X8: 390 case MEDIA_BUS_FMT_SGRBG8_1X8: 391 case MEDIA_BUS_FMT_SGBRG8_1X8: 392 case MEDIA_BUS_FMT_SBGGR8_1X8: 393 return 8; 394 395 case MEDIA_BUS_FMT_SRGGB10_1X10: 396 case MEDIA_BUS_FMT_SGRBG10_1X10: 397 case MEDIA_BUS_FMT_SGBRG10_1X10: 398 case MEDIA_BUS_FMT_SBGGR10_1X10: 399 default: 400 return 10; 401 } 402 } 403 404 static void imx219_get_binning(struct v4l2_subdev_state *state, u8 *bin_h, 405 u8 *bin_v) 406 { 407 const struct v4l2_mbus_framefmt *format = 408 v4l2_subdev_state_get_format(state, 0); 409 const struct v4l2_rect *crop = v4l2_subdev_state_get_crop(state, 0); 410 u32 hbin = crop->width / format->width; 411 u32 vbin = crop->height / format->height; 412 413 if (hbin == 2 && vbin == 2) { 414 *bin_h = IMX219_BINNING_X2_ANALOG; 415 *bin_v = IMX219_BINNING_X2_ANALOG; 416 } else { 417 *bin_h = IMX219_BINNING_NONE; 418 *bin_v = IMX219_BINNING_NONE; 419 } 420 421 } 422 423 static inline u32 imx219_get_rate_factor(struct v4l2_subdev_state *state) 424 { 425 u8 bin_h, bin_v; 426 427 imx219_get_binning(state, &bin_h, &bin_v); 428 429 return (bin_h & bin_v) == IMX219_BINNING_X2_ANALOG ? 2 : 1; 430 } 431 432 /* ----------------------------------------------------------------------------- 433 * Controls 434 */ 435 436 static int imx219_set_ctrl(struct v4l2_ctrl *ctrl) 437 { 438 struct imx219 *imx219 = 439 container_of(ctrl->handler, struct imx219, ctrl_handler); 440 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 441 const struct v4l2_mbus_framefmt *format; 442 struct v4l2_subdev_state *state; 443 u32 rate_factor; 444 int ret = 0; 445 446 state = v4l2_subdev_get_locked_active_state(&imx219->sd); 447 format = v4l2_subdev_state_get_format(state, 0); 448 rate_factor = imx219_get_rate_factor(state); 449 450 if (ctrl->id == V4L2_CID_VBLANK) { 451 int exposure_max, exposure_def; 452 453 /* Update max exposure while meeting expected vblanking */ 454 exposure_max = format->height + ctrl->val - IMX219_EXPOSURE_OFFSET; 455 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ? 456 exposure_max : IMX219_EXPOSURE_DEFAULT; 457 ret = __v4l2_ctrl_modify_range(imx219->exposure, 458 imx219->exposure->minimum, 459 exposure_max, 460 imx219->exposure->step, 461 exposure_def); 462 if (ret) 463 return ret; 464 465 } 466 467 /* 468 * Applying V4L2 control value only happens 469 * when power is up for streaming 470 */ 471 if (pm_runtime_get_if_in_use(&client->dev) == 0) 472 return 0; 473 474 switch (ctrl->id) { 475 case V4L2_CID_ANALOGUE_GAIN: 476 cci_write(imx219->regmap, IMX219_REG_ANALOG_GAIN, 477 ctrl->val, &ret); 478 break; 479 case V4L2_CID_EXPOSURE: 480 cci_write(imx219->regmap, IMX219_REG_EXPOSURE, 481 ctrl->val / rate_factor, &ret); 482 break; 483 case V4L2_CID_DIGITAL_GAIN: 484 cci_write(imx219->regmap, IMX219_REG_DIGITAL_GAIN, 485 ctrl->val, &ret); 486 break; 487 case V4L2_CID_TEST_PATTERN: 488 cci_write(imx219->regmap, IMX219_REG_TEST_PATTERN, 489 imx219_test_pattern_val[ctrl->val], &ret); 490 break; 491 case V4L2_CID_HFLIP: 492 case V4L2_CID_VFLIP: 493 cci_write(imx219->regmap, IMX219_REG_ORIENTATION, 494 imx219->hflip->val | imx219->vflip->val << 1, &ret); 495 break; 496 case V4L2_CID_VBLANK: 497 cci_write(imx219->regmap, IMX219_REG_FRM_LENGTH_A, 498 (format->height + ctrl->val) / rate_factor, &ret); 499 break; 500 case V4L2_CID_HBLANK: 501 cci_write(imx219->regmap, IMX219_REG_LINE_LENGTH_A, 502 format->width + ctrl->val, &ret); 503 break; 504 case V4L2_CID_TEST_PATTERN_RED: 505 cci_write(imx219->regmap, IMX219_REG_TESTP_RED, 506 ctrl->val, &ret); 507 break; 508 case V4L2_CID_TEST_PATTERN_GREENR: 509 cci_write(imx219->regmap, IMX219_REG_TESTP_GREENR, 510 ctrl->val, &ret); 511 break; 512 case V4L2_CID_TEST_PATTERN_BLUE: 513 cci_write(imx219->regmap, IMX219_REG_TESTP_BLUE, 514 ctrl->val, &ret); 515 break; 516 case V4L2_CID_TEST_PATTERN_GREENB: 517 cci_write(imx219->regmap, IMX219_REG_TESTP_GREENB, 518 ctrl->val, &ret); 519 break; 520 default: 521 dev_info(&client->dev, 522 "ctrl(id:0x%x,val:0x%x) is not handled\n", 523 ctrl->id, ctrl->val); 524 ret = -EINVAL; 525 break; 526 } 527 528 pm_runtime_put(&client->dev); 529 530 return ret; 531 } 532 533 static const struct v4l2_ctrl_ops imx219_ctrl_ops = { 534 .s_ctrl = imx219_set_ctrl, 535 }; 536 537 static unsigned long imx219_get_pixel_rate(struct imx219 *imx219) 538 { 539 return (imx219->lanes == 2) ? IMX219_PIXEL_RATE : IMX219_PIXEL_RATE_4LANE; 540 } 541 542 /* Initialize control handlers */ 543 static int imx219_init_controls(struct imx219 *imx219) 544 { 545 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 546 const struct imx219_mode *mode = &supported_modes[0]; 547 struct v4l2_ctrl_handler *ctrl_hdlr; 548 struct v4l2_fwnode_device_properties props; 549 int exposure_max, exposure_def; 550 int i, ret; 551 552 ctrl_hdlr = &imx219->ctrl_handler; 553 ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12); 554 if (ret) 555 return ret; 556 557 /* By default, PIXEL_RATE is read only */ 558 imx219->pixel_rate = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 559 V4L2_CID_PIXEL_RATE, 560 imx219_get_pixel_rate(imx219), 561 imx219_get_pixel_rate(imx219), 1, 562 imx219_get_pixel_rate(imx219)); 563 564 imx219->link_freq = 565 v4l2_ctrl_new_int_menu(ctrl_hdlr, &imx219_ctrl_ops, 566 V4L2_CID_LINK_FREQ, 567 ARRAY_SIZE(imx219_link_freq_menu) - 1, 0, 568 (imx219->lanes == 2) ? imx219_link_freq_menu : 569 imx219_link_freq_4lane_menu); 570 if (imx219->link_freq) 571 imx219->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 572 573 /* Initial blanking and exposure. Limits are updated during set_fmt */ 574 imx219->vblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 575 V4L2_CID_VBLANK, IMX219_VBLANK_MIN, 576 IMX219_FLL_MAX - mode->height, 1, 577 mode->fll_def - mode->height); 578 imx219->hblank = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 579 V4L2_CID_HBLANK, 580 IMX219_LLP_MIN - mode->width, 581 IMX219_LLP_MAX - mode->width, 1, 582 IMX219_LLP_MIN - mode->width); 583 exposure_max = mode->fll_def - IMX219_EXPOSURE_OFFSET; 584 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ? 585 exposure_max : IMX219_EXPOSURE_DEFAULT; 586 imx219->exposure = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 587 V4L2_CID_EXPOSURE, 588 IMX219_EXPOSURE_MIN, exposure_max, 589 IMX219_EXPOSURE_STEP, 590 exposure_def); 591 592 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_ANALOGUE_GAIN, 593 IMX219_ANA_GAIN_MIN, IMX219_ANA_GAIN_MAX, 594 IMX219_ANA_GAIN_STEP, IMX219_ANA_GAIN_DEFAULT); 595 596 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, V4L2_CID_DIGITAL_GAIN, 597 IMX219_DGTL_GAIN_MIN, IMX219_DGTL_GAIN_MAX, 598 IMX219_DGTL_GAIN_STEP, IMX219_DGTL_GAIN_DEFAULT); 599 600 imx219->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 601 V4L2_CID_HFLIP, 0, 1, 1, 0); 602 if (imx219->hflip) 603 imx219->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 604 605 imx219->vflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 606 V4L2_CID_VFLIP, 0, 1, 1, 0); 607 if (imx219->vflip) 608 imx219->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT; 609 610 v4l2_ctrl_new_std_menu_items(ctrl_hdlr, &imx219_ctrl_ops, 611 V4L2_CID_TEST_PATTERN, 612 ARRAY_SIZE(imx219_test_pattern_menu) - 1, 613 0, 0, imx219_test_pattern_menu); 614 for (i = 0; i < 4; i++) { 615 /* 616 * The assumption is that 617 * V4L2_CID_TEST_PATTERN_GREENR == V4L2_CID_TEST_PATTERN_RED + 1 618 * V4L2_CID_TEST_PATTERN_BLUE == V4L2_CID_TEST_PATTERN_RED + 2 619 * V4L2_CID_TEST_PATTERN_GREENB == V4L2_CID_TEST_PATTERN_RED + 3 620 */ 621 v4l2_ctrl_new_std(ctrl_hdlr, &imx219_ctrl_ops, 622 V4L2_CID_TEST_PATTERN_RED + i, 623 IMX219_TESTP_COLOUR_MIN, 624 IMX219_TESTP_COLOUR_MAX, 625 IMX219_TESTP_COLOUR_STEP, 626 IMX219_TESTP_COLOUR_MAX); 627 /* The "Solid color" pattern is white by default */ 628 } 629 630 if (ctrl_hdlr->error) { 631 ret = ctrl_hdlr->error; 632 dev_err_probe(&client->dev, ret, "Control init failed\n"); 633 goto error; 634 } 635 636 ret = v4l2_fwnode_device_parse(&client->dev, &props); 637 if (ret) 638 goto error; 639 640 ret = v4l2_ctrl_new_fwnode_properties(ctrl_hdlr, &imx219_ctrl_ops, 641 &props); 642 if (ret) 643 goto error; 644 645 imx219->sd.ctrl_handler = ctrl_hdlr; 646 647 return 0; 648 649 error: 650 v4l2_ctrl_handler_free(ctrl_hdlr); 651 652 return ret; 653 } 654 655 static void imx219_free_controls(struct imx219 *imx219) 656 { 657 v4l2_ctrl_handler_free(imx219->sd.ctrl_handler); 658 } 659 660 /* ----------------------------------------------------------------------------- 661 * Subdev operations 662 */ 663 664 static int imx219_set_framefmt(struct imx219 *imx219, 665 struct v4l2_subdev_state *state) 666 { 667 const struct v4l2_mbus_framefmt *format; 668 const struct v4l2_rect *crop; 669 u8 bin_h, bin_v; 670 u32 bpp; 671 int ret = 0; 672 673 format = v4l2_subdev_state_get_format(state, 0); 674 crop = v4l2_subdev_state_get_crop(state, 0); 675 bpp = imx219_get_format_bpp(format); 676 677 cci_write(imx219->regmap, IMX219_REG_X_ADD_STA_A, 678 crop->left - IMX219_PIXEL_ARRAY_LEFT, &ret); 679 cci_write(imx219->regmap, IMX219_REG_X_ADD_END_A, 680 crop->left - IMX219_PIXEL_ARRAY_LEFT + crop->width - 1, &ret); 681 cci_write(imx219->regmap, IMX219_REG_Y_ADD_STA_A, 682 crop->top - IMX219_PIXEL_ARRAY_TOP, &ret); 683 cci_write(imx219->regmap, IMX219_REG_Y_ADD_END_A, 684 crop->top - IMX219_PIXEL_ARRAY_TOP + crop->height - 1, &ret); 685 686 imx219_get_binning(state, &bin_h, &bin_v); 687 cci_write(imx219->regmap, IMX219_REG_BINNING_MODE_H, bin_h, &ret); 688 cci_write(imx219->regmap, IMX219_REG_BINNING_MODE_V, bin_v, &ret); 689 690 cci_write(imx219->regmap, IMX219_REG_X_OUTPUT_SIZE, 691 format->width, &ret); 692 cci_write(imx219->regmap, IMX219_REG_Y_OUTPUT_SIZE, 693 format->height, &ret); 694 695 cci_write(imx219->regmap, IMX219_REG_TP_WINDOW_WIDTH, 696 format->width, &ret); 697 cci_write(imx219->regmap, IMX219_REG_TP_WINDOW_HEIGHT, 698 format->height, &ret); 699 700 cci_write(imx219->regmap, IMX219_REG_CSI_DATA_FORMAT_A, 701 (bpp << 8) | bpp, &ret); 702 cci_write(imx219->regmap, IMX219_REG_OPPXCK_DIV, bpp, &ret); 703 704 return ret; 705 } 706 707 static int imx219_configure_lanes(struct imx219 *imx219) 708 { 709 /* Write the appropriate PLL settings for the number of MIPI lanes */ 710 return cci_multi_reg_write(imx219->regmap, 711 imx219->lanes == 2 ? imx219_2lane_regs : imx219_4lane_regs, 712 imx219->lanes == 2 ? ARRAY_SIZE(imx219_2lane_regs) : 713 ARRAY_SIZE(imx219_4lane_regs), NULL); 714 }; 715 716 static int imx219_enable_streams(struct v4l2_subdev *sd, 717 struct v4l2_subdev_state *state, u32 pad, 718 u64 streams_mask) 719 { 720 struct imx219 *imx219 = to_imx219(sd); 721 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 722 int ret; 723 724 ret = pm_runtime_resume_and_get(&client->dev); 725 if (ret < 0) 726 return ret; 727 728 /* Send all registers that are common to all modes */ 729 ret = cci_multi_reg_write(imx219->regmap, imx219_common_regs, 730 ARRAY_SIZE(imx219_common_regs), NULL); 731 if (ret) { 732 dev_err(&client->dev, "%s failed to send mfg header\n", __func__); 733 goto err_rpm_put; 734 } 735 736 /* Configure two or four Lane mode */ 737 ret = imx219_configure_lanes(imx219); 738 if (ret) { 739 dev_err(&client->dev, "%s failed to configure lanes\n", __func__); 740 goto err_rpm_put; 741 } 742 743 /* Apply format and crop settings. */ 744 ret = imx219_set_framefmt(imx219, state); 745 if (ret) { 746 dev_err(&client->dev, "%s failed to set frame format: %d\n", 747 __func__, ret); 748 goto err_rpm_put; 749 } 750 751 /* Apply customized values from user */ 752 ret = __v4l2_ctrl_handler_setup(imx219->sd.ctrl_handler); 753 if (ret) 754 goto err_rpm_put; 755 756 /* set stream on register */ 757 ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT, 758 IMX219_MODE_STREAMING, NULL); 759 if (ret) 760 goto err_rpm_put; 761 762 /* vflip and hflip cannot change during streaming */ 763 __v4l2_ctrl_grab(imx219->vflip, true); 764 __v4l2_ctrl_grab(imx219->hflip, true); 765 766 return 0; 767 768 err_rpm_put: 769 pm_runtime_put_autosuspend(&client->dev); 770 return ret; 771 } 772 773 static int imx219_disable_streams(struct v4l2_subdev *sd, 774 struct v4l2_subdev_state *state, u32 pad, 775 u64 streams_mask) 776 { 777 struct imx219 *imx219 = to_imx219(sd); 778 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 779 int ret; 780 781 /* set stream off register */ 782 ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT, 783 IMX219_MODE_STANDBY, NULL); 784 if (ret) 785 dev_err(&client->dev, "%s failed to set stream\n", __func__); 786 787 __v4l2_ctrl_grab(imx219->vflip, false); 788 __v4l2_ctrl_grab(imx219->hflip, false); 789 790 pm_runtime_put_autosuspend(&client->dev); 791 792 return ret; 793 } 794 795 static int imx219_enum_mbus_code(struct v4l2_subdev *sd, 796 struct v4l2_subdev_state *state, 797 struct v4l2_subdev_mbus_code_enum *code) 798 { 799 struct imx219 *imx219 = to_imx219(sd); 800 801 if (code->index >= (ARRAY_SIZE(imx219_mbus_formats) / 4)) 802 return -EINVAL; 803 804 code->code = imx219_get_format_code(imx219, imx219_mbus_formats[code->index * 4]); 805 806 return 0; 807 } 808 809 static int imx219_enum_frame_size(struct v4l2_subdev *sd, 810 struct v4l2_subdev_state *state, 811 struct v4l2_subdev_frame_size_enum *fse) 812 { 813 struct imx219 *imx219 = to_imx219(sd); 814 u32 code; 815 816 if (fse->index >= ARRAY_SIZE(supported_modes)) 817 return -EINVAL; 818 819 code = imx219_get_format_code(imx219, fse->code); 820 if (fse->code != code) 821 return -EINVAL; 822 823 fse->min_width = supported_modes[fse->index].width; 824 fse->max_width = fse->min_width; 825 fse->min_height = supported_modes[fse->index].height; 826 fse->max_height = fse->min_height; 827 828 return 0; 829 } 830 831 static int imx219_set_pad_format(struct v4l2_subdev *sd, 832 struct v4l2_subdev_state *state, 833 struct v4l2_subdev_format *fmt) 834 { 835 struct imx219 *imx219 = to_imx219(sd); 836 const struct imx219_mode *mode; 837 struct v4l2_mbus_framefmt *format; 838 struct v4l2_rect *crop; 839 u8 bin_h, bin_v, binning; 840 u32 prev_line_len; 841 int ret; 842 843 format = v4l2_subdev_state_get_format(state, 0); 844 prev_line_len = format->width + imx219->hblank->val; 845 846 /* 847 * Adjust the requested format to match the closest mode. The Bayer 848 * order varies with flips. 849 */ 850 mode = v4l2_find_nearest_size(supported_modes, 851 ARRAY_SIZE(supported_modes), 852 width, height, 853 fmt->format.width, fmt->format.height); 854 855 fmt->format.code = imx219_get_format_code(imx219, fmt->format.code); 856 fmt->format.width = mode->width; 857 fmt->format.height = mode->height; 858 fmt->format.field = V4L2_FIELD_NONE; 859 fmt->format.colorspace = V4L2_COLORSPACE_RAW; 860 fmt->format.ycbcr_enc = V4L2_YCBCR_ENC_601; 861 fmt->format.quantization = V4L2_QUANTIZATION_FULL_RANGE; 862 fmt->format.xfer_func = V4L2_XFER_FUNC_NONE; 863 864 *format = fmt->format; 865 866 /* 867 * Use binning to maximize the crop rectangle size, and centre it in the 868 * sensor. 869 */ 870 bin_h = min(IMX219_PIXEL_ARRAY_WIDTH / format->width, 2U); 871 bin_v = min(IMX219_PIXEL_ARRAY_HEIGHT / format->height, 2U); 872 873 /* Ensure bin_h and bin_v are same to avoid 1:2 or 2:1 stretching */ 874 binning = min(bin_h, bin_v); 875 876 crop = v4l2_subdev_state_get_crop(state, 0); 877 crop->width = format->width * binning; 878 crop->height = format->height * binning; 879 crop->left = (IMX219_NATIVE_WIDTH - crop->width) / 2; 880 crop->top = (IMX219_NATIVE_HEIGHT - crop->height) / 2; 881 882 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 883 int exposure_max; 884 int exposure_def; 885 int hblank, llp_min; 886 int pixel_rate; 887 888 /* Update limits and set FPS to default */ 889 ret = __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN, 890 IMX219_FLL_MAX - mode->height, 1, 891 mode->fll_def - mode->height); 892 if (ret) 893 return ret; 894 895 ret = __v4l2_ctrl_s_ctrl(imx219->vblank, 896 mode->fll_def - mode->height); 897 if (ret) 898 return ret; 899 900 /* Update max exposure while meeting expected vblanking */ 901 exposure_max = mode->fll_def - IMX219_EXPOSURE_OFFSET; 902 exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ? 903 exposure_max : IMX219_EXPOSURE_DEFAULT; 904 ret = __v4l2_ctrl_modify_range(imx219->exposure, 905 imx219->exposure->minimum, 906 exposure_max, 907 imx219->exposure->step, 908 exposure_def); 909 if (ret) 910 return ret; 911 912 /* 913 * With analog binning the default minimum line length of 3448 914 * can cause artefacts with RAW10 formats, because the ADC 915 * operates on two lines together. So we switch to a higher 916 * minimum of 3560. 917 */ 918 imx219_get_binning(state, &bin_h, &bin_v); 919 llp_min = (bin_h & bin_v) == IMX219_BINNING_X2_ANALOG ? 920 IMX219_BINNED_LLP_MIN : IMX219_LLP_MIN; 921 ret = __v4l2_ctrl_modify_range(imx219->hblank, 922 llp_min - mode->width, 923 IMX219_LLP_MAX - mode->width, 1, 924 llp_min - mode->width); 925 if (ret) 926 return ret; 927 /* 928 * Retain PPL setting from previous mode so that the 929 * line time does not change on a mode change. 930 * Limits have to be recomputed as the controls define 931 * the blanking only, so PPL values need to have the 932 * mode width subtracted. 933 */ 934 hblank = prev_line_len - mode->width; 935 ret = __v4l2_ctrl_s_ctrl(imx219->hblank, hblank); 936 if (ret) 937 return ret; 938 939 /* Scale the pixel rate based on the mode specific factor */ 940 pixel_rate = imx219_get_pixel_rate(imx219) * 941 imx219_get_rate_factor(state); 942 ret = __v4l2_ctrl_modify_range(imx219->pixel_rate, pixel_rate, 943 pixel_rate, 1, pixel_rate); 944 if (ret) 945 return ret; 946 } 947 948 return 0; 949 } 950 951 static int imx219_get_selection(struct v4l2_subdev *sd, 952 struct v4l2_subdev_state *state, 953 struct v4l2_subdev_selection *sel) 954 { 955 switch (sel->target) { 956 case V4L2_SEL_TGT_CROP: 957 sel->r = *v4l2_subdev_state_get_crop(state, 0); 958 return 0; 959 960 case V4L2_SEL_TGT_NATIVE_SIZE: 961 sel->r.top = 0; 962 sel->r.left = 0; 963 sel->r.width = IMX219_NATIVE_WIDTH; 964 sel->r.height = IMX219_NATIVE_HEIGHT; 965 966 return 0; 967 968 case V4L2_SEL_TGT_CROP_DEFAULT: 969 case V4L2_SEL_TGT_CROP_BOUNDS: 970 sel->r.top = IMX219_PIXEL_ARRAY_TOP; 971 sel->r.left = IMX219_PIXEL_ARRAY_LEFT; 972 sel->r.width = IMX219_PIXEL_ARRAY_WIDTH; 973 sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT; 974 975 return 0; 976 } 977 978 return -EINVAL; 979 } 980 981 static int imx219_init_state(struct v4l2_subdev *sd, 982 struct v4l2_subdev_state *state) 983 { 984 struct v4l2_subdev_format fmt = { 985 .which = V4L2_SUBDEV_FORMAT_TRY, 986 .pad = 0, 987 .format = { 988 .code = MEDIA_BUS_FMT_SRGGB10_1X10, 989 .width = supported_modes[0].width, 990 .height = supported_modes[0].height, 991 }, 992 }; 993 994 return imx219_set_pad_format(sd, state, &fmt); 995 } 996 997 static const struct v4l2_subdev_video_ops imx219_video_ops = { 998 .s_stream = v4l2_subdev_s_stream_helper, 999 }; 1000 1001 static const struct v4l2_subdev_pad_ops imx219_pad_ops = { 1002 .enum_mbus_code = imx219_enum_mbus_code, 1003 .get_fmt = v4l2_subdev_get_fmt, 1004 .set_fmt = imx219_set_pad_format, 1005 .get_selection = imx219_get_selection, 1006 .enum_frame_size = imx219_enum_frame_size, 1007 .enable_streams = imx219_enable_streams, 1008 .disable_streams = imx219_disable_streams, 1009 }; 1010 1011 static const struct v4l2_subdev_ops imx219_subdev_ops = { 1012 .video = &imx219_video_ops, 1013 .pad = &imx219_pad_ops, 1014 }; 1015 1016 static const struct v4l2_subdev_internal_ops imx219_internal_ops = { 1017 .init_state = imx219_init_state, 1018 }; 1019 1020 /* ----------------------------------------------------------------------------- 1021 * Power management 1022 */ 1023 1024 static int imx219_power_on(struct device *dev) 1025 { 1026 struct v4l2_subdev *sd = dev_get_drvdata(dev); 1027 struct imx219 *imx219 = to_imx219(sd); 1028 int ret; 1029 1030 ret = regulator_bulk_enable(IMX219_NUM_SUPPLIES, 1031 imx219->supplies); 1032 if (ret) { 1033 dev_err(dev, "%s: failed to enable regulators\n", 1034 __func__); 1035 return ret; 1036 } 1037 1038 ret = clk_prepare_enable(imx219->xclk); 1039 if (ret) { 1040 dev_err(dev, "%s: failed to enable clock\n", 1041 __func__); 1042 goto reg_off; 1043 } 1044 1045 /* 1046 * Note: Misinterpretation of reset assertion - do not re-use this code. 1047 * XCLR pin is using incorrect (for reset signal) logical level. 1048 */ 1049 gpiod_set_value_cansleep(imx219->reset_gpio, 1); 1050 usleep_range(IMX219_XCLR_MIN_DELAY_US, 1051 IMX219_XCLR_MIN_DELAY_US + IMX219_XCLR_DELAY_RANGE_US); 1052 1053 return 0; 1054 1055 reg_off: 1056 regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies); 1057 1058 return ret; 1059 } 1060 1061 static int imx219_power_off(struct device *dev) 1062 { 1063 struct v4l2_subdev *sd = dev_get_drvdata(dev); 1064 struct imx219 *imx219 = to_imx219(sd); 1065 1066 gpiod_set_value_cansleep(imx219->reset_gpio, 0); 1067 regulator_bulk_disable(IMX219_NUM_SUPPLIES, imx219->supplies); 1068 clk_disable_unprepare(imx219->xclk); 1069 1070 return 0; 1071 } 1072 1073 /* ----------------------------------------------------------------------------- 1074 * Probe & remove 1075 */ 1076 1077 static int imx219_get_regulators(struct imx219 *imx219) 1078 { 1079 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 1080 unsigned int i; 1081 1082 for (i = 0; i < IMX219_NUM_SUPPLIES; i++) 1083 imx219->supplies[i].supply = imx219_supply_name[i]; 1084 1085 return devm_regulator_bulk_get(&client->dev, 1086 IMX219_NUM_SUPPLIES, 1087 imx219->supplies); 1088 } 1089 1090 /* Verify chip ID */ 1091 static int imx219_identify_module(struct imx219 *imx219) 1092 { 1093 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd); 1094 int ret; 1095 u64 val; 1096 1097 ret = cci_read(imx219->regmap, IMX219_REG_CHIP_ID, &val, NULL); 1098 if (ret) 1099 return dev_err_probe(&client->dev, ret, 1100 "failed to read chip id %x\n", 1101 IMX219_CHIP_ID); 1102 1103 if (val != IMX219_CHIP_ID) 1104 return dev_err_probe(&client->dev, -EIO, 1105 "chip id mismatch: %x!=%llx\n", 1106 IMX219_CHIP_ID, val); 1107 1108 return 0; 1109 } 1110 1111 static int imx219_check_hwcfg(struct device *dev, struct imx219 *imx219) 1112 { 1113 struct fwnode_handle *endpoint; 1114 struct v4l2_fwnode_endpoint ep_cfg = { 1115 .bus_type = V4L2_MBUS_CSI2_DPHY 1116 }; 1117 unsigned long link_freq_bitmap; 1118 int ret = -EINVAL; 1119 1120 endpoint = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL); 1121 if (!endpoint) 1122 return dev_err_probe(dev, -EINVAL, "endpoint node not found\n"); 1123 1124 if (v4l2_fwnode_endpoint_alloc_parse(endpoint, &ep_cfg)) { 1125 dev_err_probe(dev, -EINVAL, "could not parse endpoint\n"); 1126 goto error_out; 1127 } 1128 1129 /* Check the number of MIPI CSI2 data lanes */ 1130 if (ep_cfg.bus.mipi_csi2.num_data_lanes != 2 && 1131 ep_cfg.bus.mipi_csi2.num_data_lanes != 4) { 1132 dev_err_probe(dev, -EINVAL, 1133 "only 2 or 4 data lanes are currently supported\n"); 1134 goto error_out; 1135 } 1136 imx219->lanes = ep_cfg.bus.mipi_csi2.num_data_lanes; 1137 1138 /* Check the link frequency set in device tree */ 1139 switch (imx219->lanes) { 1140 case 2: 1141 ret = v4l2_link_freq_to_bitmap(dev, 1142 ep_cfg.link_frequencies, 1143 ep_cfg.nr_of_link_frequencies, 1144 imx219_link_freq_menu, 1145 ARRAY_SIZE(imx219_link_freq_menu), 1146 &link_freq_bitmap); 1147 break; 1148 case 4: 1149 ret = v4l2_link_freq_to_bitmap(dev, 1150 ep_cfg.link_frequencies, 1151 ep_cfg.nr_of_link_frequencies, 1152 imx219_link_freq_4lane_menu, 1153 ARRAY_SIZE(imx219_link_freq_4lane_menu), 1154 &link_freq_bitmap); 1155 1156 if (!ret && (link_freq_bitmap & BIT(1))) { 1157 dev_warn(dev, "Link frequency of %d not supported, but has been incorrectly advertised previously\n", 1158 IMX219_DEFAULT_LINK_FREQ_4LANE_UNSUPPORTED); 1159 dev_warn(dev, "Using link frequency of %d\n", 1160 IMX219_DEFAULT_LINK_FREQ_4LANE); 1161 link_freq_bitmap |= BIT(0); 1162 } 1163 break; 1164 } 1165 1166 if (ret || !(link_freq_bitmap & BIT(0))) { 1167 ret = -EINVAL; 1168 dev_err_probe(dev, -EINVAL, 1169 "Link frequency not supported: %lld\n", 1170 ep_cfg.link_frequencies[0]); 1171 } 1172 1173 error_out: 1174 v4l2_fwnode_endpoint_free(&ep_cfg); 1175 fwnode_handle_put(endpoint); 1176 1177 return ret; 1178 } 1179 1180 static int imx219_probe(struct i2c_client *client) 1181 { 1182 struct device *dev = &client->dev; 1183 struct imx219 *imx219; 1184 int ret; 1185 1186 imx219 = devm_kzalloc(&client->dev, sizeof(*imx219), GFP_KERNEL); 1187 if (!imx219) 1188 return -ENOMEM; 1189 1190 v4l2_i2c_subdev_init(&imx219->sd, client, &imx219_subdev_ops); 1191 imx219->sd.internal_ops = &imx219_internal_ops; 1192 1193 /* Check the hardware configuration in device tree */ 1194 if (imx219_check_hwcfg(dev, imx219)) 1195 return -EINVAL; 1196 1197 imx219->regmap = devm_cci_regmap_init_i2c(client, 16); 1198 if (IS_ERR(imx219->regmap)) 1199 return dev_err_probe(dev, PTR_ERR(imx219->regmap), 1200 "failed to initialize CCI\n"); 1201 1202 /* Get system clock (xclk) */ 1203 imx219->xclk = devm_v4l2_sensor_clk_get(dev, NULL); 1204 if (IS_ERR(imx219->xclk)) 1205 return dev_err_probe(dev, PTR_ERR(imx219->xclk), 1206 "failed to get xclk\n"); 1207 1208 imx219->xclk_freq = clk_get_rate(imx219->xclk); 1209 if (imx219->xclk_freq != IMX219_XCLK_FREQ) 1210 return dev_err_probe(dev, -EINVAL, 1211 "xclk frequency not supported: %d Hz\n", 1212 imx219->xclk_freq); 1213 1214 ret = imx219_get_regulators(imx219); 1215 if (ret) 1216 return dev_err_probe(dev, ret, "failed to get regulators\n"); 1217 1218 /* Request optional enable pin */ 1219 imx219->reset_gpio = devm_gpiod_get_optional(dev, "reset", 1220 GPIOD_OUT_HIGH); 1221 1222 /* 1223 * The sensor must be powered for imx219_identify_module() 1224 * to be able to read the CHIP_ID register 1225 */ 1226 ret = imx219_power_on(dev); 1227 if (ret) 1228 return ret; 1229 1230 ret = imx219_identify_module(imx219); 1231 if (ret) 1232 goto error_power_off; 1233 1234 /* 1235 * Sensor doesn't enter LP-11 state upon power up until and unless 1236 * streaming is started, so upon power up switch the modes to: 1237 * streaming -> standby 1238 */ 1239 ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT, 1240 IMX219_MODE_STREAMING, NULL); 1241 if (ret < 0) 1242 goto error_power_off; 1243 1244 usleep_range(100, 110); 1245 1246 /* put sensor back to standby mode */ 1247 ret = cci_write(imx219->regmap, IMX219_REG_MODE_SELECT, 1248 IMX219_MODE_STANDBY, NULL); 1249 if (ret < 0) 1250 goto error_power_off; 1251 1252 usleep_range(100, 110); 1253 1254 ret = imx219_init_controls(imx219); 1255 if (ret) 1256 goto error_power_off; 1257 1258 /* Initialize subdev */ 1259 imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 1260 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 1261 1262 /* Initialize source pad */ 1263 imx219->pad.flags = MEDIA_PAD_FL_SOURCE; 1264 1265 ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad); 1266 if (ret) { 1267 dev_err_probe(dev, ret, "failed to init entity pads\n"); 1268 goto error_handler_free; 1269 } 1270 1271 imx219->sd.state_lock = imx219->ctrl_handler.lock; 1272 ret = v4l2_subdev_init_finalize(&imx219->sd); 1273 if (ret < 0) { 1274 dev_err_probe(dev, ret, "subdev init error\n"); 1275 goto error_media_entity; 1276 } 1277 1278 pm_runtime_set_active(dev); 1279 pm_runtime_enable(dev); 1280 1281 ret = v4l2_async_register_subdev_sensor(&imx219->sd); 1282 if (ret < 0) { 1283 dev_err_probe(dev, ret, 1284 "failed to register sensor sub-device\n"); 1285 goto error_subdev_cleanup; 1286 } 1287 1288 pm_runtime_idle(dev); 1289 pm_runtime_set_autosuspend_delay(dev, 1000); 1290 pm_runtime_use_autosuspend(dev); 1291 1292 return 0; 1293 1294 error_subdev_cleanup: 1295 v4l2_subdev_cleanup(&imx219->sd); 1296 pm_runtime_disable(dev); 1297 pm_runtime_set_suspended(dev); 1298 1299 error_media_entity: 1300 media_entity_cleanup(&imx219->sd.entity); 1301 1302 error_handler_free: 1303 imx219_free_controls(imx219); 1304 1305 error_power_off: 1306 imx219_power_off(dev); 1307 1308 return ret; 1309 } 1310 1311 static void imx219_remove(struct i2c_client *client) 1312 { 1313 struct v4l2_subdev *sd = i2c_get_clientdata(client); 1314 struct imx219 *imx219 = to_imx219(sd); 1315 1316 v4l2_async_unregister_subdev(sd); 1317 v4l2_subdev_cleanup(sd); 1318 media_entity_cleanup(&sd->entity); 1319 imx219_free_controls(imx219); 1320 1321 pm_runtime_disable(&client->dev); 1322 if (!pm_runtime_status_suspended(&client->dev)) { 1323 imx219_power_off(&client->dev); 1324 pm_runtime_set_suspended(&client->dev); 1325 } 1326 } 1327 1328 static const struct of_device_id imx219_dt_ids[] = { 1329 { .compatible = "sony,imx219" }, 1330 { /* sentinel */ } 1331 }; 1332 MODULE_DEVICE_TABLE(of, imx219_dt_ids); 1333 1334 static const struct dev_pm_ops imx219_pm_ops = { 1335 SET_RUNTIME_PM_OPS(imx219_power_off, imx219_power_on, NULL) 1336 }; 1337 1338 static struct i2c_driver imx219_i2c_driver = { 1339 .driver = { 1340 .name = "imx219", 1341 .of_match_table = imx219_dt_ids, 1342 .pm = &imx219_pm_ops, 1343 }, 1344 .probe = imx219_probe, 1345 .remove = imx219_remove, 1346 }; 1347 1348 module_i2c_driver(imx219_i2c_driver); 1349 1350 MODULE_AUTHOR("Dave Stevenson <dave.stevenson@raspberrypi.com"); 1351 MODULE_DESCRIPTION("Sony IMX219 sensor driver"); 1352 MODULE_LICENSE("GPL v2"); 1353